xref: /freebsd/sys/netinet/tcp_usrreq.c (revision d3b6c96b7dfd20f16785fdbe02141b5a3664290d)
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 #include "opt_tcpdebug.h"
490cc12cc5SJoerg Wunsch 
50df8bae1dSRodney W. Grimes #include <sys/param.h>
51df8bae1dSRodney W. Grimes #include <sys/systm.h>
52adc56f5aSEdward Tomasz Napierala #include <sys/arb.h>
539077f387SGleb Smirnoff #include <sys/limits.h>
54f76fcf6dSJeffrey Hsu #include <sys/malloc.h>
5555bceb1eSRandall Stewart #include <sys/refcount.h>
56c7a82f90SGarrett Wollman #include <sys/kernel.h>
57b2e60773SJohn Baldwin #include <sys/ktls.h>
58adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h>
5998163b98SPoul-Henning Kamp #include <sys/sysctl.h>
60df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
61fb59c426SYoshinobu Inoue #ifdef INET6
62fb59c426SYoshinobu Inoue #include <sys/domain.h>
63fb59c426SYoshinobu Inoue #endif /* INET6 */
64df8bae1dSRodney W. Grimes #include <sys/socket.h>
65df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
66df8bae1dSRodney W. Grimes #include <sys/protosw.h>
6791421ba2SRobert Watson #include <sys/proc.h>
6891421ba2SRobert Watson #include <sys/jail.h>
69f5cf1e5fSJulien Charbon #include <sys/syslog.h>
70adc56f5aSEdward Tomasz Napierala #include <sys/stats.h>
71df8bae1dSRodney W. Grimes 
72497057eeSRobert Watson #ifdef DDB
73497057eeSRobert Watson #include <ddb/ddb.h>
74497057eeSRobert Watson #endif
75497057eeSRobert Watson 
76df8bae1dSRodney W. Grimes #include <net/if.h>
7776039bc8SGleb Smirnoff #include <net/if_var.h>
78df8bae1dSRodney W. Grimes #include <net/route.h>
79530c0060SRobert Watson #include <net/vnet.h>
80df8bae1dSRodney W. Grimes 
81df8bae1dSRodney W. Grimes #include <netinet/in.h>
825d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h>
83df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
84b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h>
85b5e8ce9fSBruce Evans #include <netinet/in_var.h>
86df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
87fb59c426SYoshinobu Inoue #ifdef INET6
88b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h>
89b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
90fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
91a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
92fb59c426SYoshinobu Inoue #endif
932de3e790SGleb Smirnoff #include <netinet/tcp.h>
94df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
95df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
96df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
97df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
982529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h>
99df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
1004644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
101c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h>
102fd389e7cSRandall Stewart #include <netinet/tcp_hpts.h>
10386a996e6SHiren Panchasara #ifdef TCPPCAP
10486a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
10586a996e6SHiren Panchasara #endif
106610ee2f9SDavid Greenman #ifdef TCPDEBUG
107df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
108610ee2f9SDavid Greenman #endif
10909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
110bc65987aSKip Macy #include <netinet/tcp_offload.h>
11109fe6320SNavdeep Parhar #endif
112fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
113df8bae1dSRodney W. Grimes 
114adc56f5aSEdward Tomasz Napierala #include <vm/vm.h>
115adc56f5aSEdward Tomasz Napierala #include <vm/vm_param.h>
116adc56f5aSEdward Tomasz Napierala #include <vm/pmap.h>
117adc56f5aSEdward Tomasz Napierala #include <vm/vm_extern.h>
118adc56f5aSEdward Tomasz Napierala #include <vm/vm_map.h>
119adc56f5aSEdward Tomasz Napierala #include <vm/vm_page.h>
120adc56f5aSEdward Tomasz Napierala 
121df8bae1dSRodney W. Grimes /*
122df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
123df8bae1dSRodney W. Grimes  */
124b287c6c7SBjoern A. Zeeb #ifdef INET
1254d77a549SAlfred Perlstein static int	tcp_connect(struct tcpcb *, struct sockaddr *,
1264d77a549SAlfred Perlstein 		    struct thread *td);
127b287c6c7SBjoern A. Zeeb #endif /* INET */
128fb59c426SYoshinobu Inoue #ifdef INET6
1294d77a549SAlfred Perlstein static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
1304d77a549SAlfred Perlstein 		    struct thread *td);
131fb59c426SYoshinobu Inoue #endif /* INET6 */
132623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
133623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
134b8af5dfaSRobert Watson static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
1352c37256eSGarrett Wollman 
136*d3b6c96bSRandall Stewart static int	tcp_pru_options_support(struct tcpcb *tp, int flags);
137*d3b6c96bSRandall Stewart 
1382c37256eSGarrett Wollman #ifdef TCPDEBUG
1391db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1402c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
1414cc20ab1SSeigo Tanimura #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
1424cc20ab1SSeigo Tanimura 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1432c37256eSGarrett Wollman #else
1442c37256eSGarrett Wollman #define	TCPDEBUG0
1452c37256eSGarrett Wollman #define	TCPDEBUG1()
1462c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1472c37256eSGarrett Wollman #endif
1482c37256eSGarrett Wollman 
1492c37256eSGarrett Wollman /*
1502c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1512c37256eSGarrett Wollman  * and an internet control block.
1522c37256eSGarrett Wollman  */
1532c37256eSGarrett Wollman static int
154b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1552c37256eSGarrett Wollman {
156f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
157623dce13SRobert Watson 	struct tcpcb *tp = NULL;
158623dce13SRobert Watson 	int error;
1592c37256eSGarrett Wollman 	TCPDEBUG0;
1602c37256eSGarrett Wollman 
161623dce13SRobert Watson 	inp = sotoinpcb(so);
162623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1632c37256eSGarrett Wollman 	TCPDEBUG1();
1642c37256eSGarrett Wollman 
1650f6385e7SGleb Smirnoff 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1660f6385e7SGleb Smirnoff 		error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1672c37256eSGarrett Wollman 		if (error)
1682c37256eSGarrett Wollman 			goto out;
1690f6385e7SGleb Smirnoff 	}
1702c37256eSGarrett Wollman 
1710f6385e7SGleb Smirnoff 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
1720f6385e7SGleb Smirnoff 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1730f6385e7SGleb Smirnoff 	error = in_pcballoc(so, &V_tcbinfo);
1747669c586SGleb Smirnoff 	if (error)
1750f6385e7SGleb Smirnoff 		goto out;
1760f6385e7SGleb Smirnoff 	inp = sotoinpcb(so);
1770f6385e7SGleb Smirnoff #ifdef INET6
1780f6385e7SGleb Smirnoff 	if (inp->inp_vflag & INP_IPV6PROTO) {
1790f6385e7SGleb Smirnoff 		inp->inp_vflag |= INP_IPV6;
1800f6385e7SGleb Smirnoff 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
1810f6385e7SGleb Smirnoff 			inp->inp_vflag |= INP_IPV4;
1820f6385e7SGleb Smirnoff 		inp->in6p_hops = -1;	/* use kernel default */
1830f6385e7SGleb Smirnoff 	}
1840f6385e7SGleb Smirnoff 	else
1850f6385e7SGleb Smirnoff #endif
1860f6385e7SGleb Smirnoff 		inp->inp_vflag |= INP_IPV4;
1870f6385e7SGleb Smirnoff 	tp = tcp_newtcpcb(inp);
1880f6385e7SGleb Smirnoff 	if (tp == NULL) {
1897669c586SGleb Smirnoff 		error = ENOBUFS;
1900f6385e7SGleb Smirnoff 		in_pcbdetach(inp);
1910f6385e7SGleb Smirnoff 		in_pcbfree(inp);
1920f6385e7SGleb Smirnoff 		goto out;
1930f6385e7SGleb Smirnoff 	}
1940f6385e7SGleb Smirnoff 	tp->t_state = TCPS_CLOSED;
1950f6385e7SGleb Smirnoff 	INP_WUNLOCK(inp);
1960f6385e7SGleb Smirnoff 	TCPSTATES_INC(TCPS_CLOSED);
1972c37256eSGarrett Wollman 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1983879597fSAndrey A. Chernov 		so->so_linger = TCP_LINGERTIME;
1992c37256eSGarrett Wollman out:
2002c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
2015d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
2020f6385e7SGleb Smirnoff 	return (error);
2032c37256eSGarrett Wollman }
2042c37256eSGarrett Wollman 
2052c37256eSGarrett Wollman /*
2063fed74e9SGleb Smirnoff  * tcp_usr_detach is called when the socket layer loses its final reference
207a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
208a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
209a152f8a3SRobert Watson  * inpcb state: time wait.
2102c37256eSGarrett Wollman  */
211bc725eafSRobert Watson static void
2123fed74e9SGleb Smirnoff tcp_usr_detach(struct socket *so)
2132c37256eSGarrett Wollman {
2143fed74e9SGleb Smirnoff 	struct inpcb *inp;
2152c37256eSGarrett Wollman 	struct tcpcb *tp;
2162c37256eSGarrett Wollman 
2173fed74e9SGleb Smirnoff 	inp = sotoinpcb(so);
2183fed74e9SGleb Smirnoff 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
2193fed74e9SGleb Smirnoff 	INP_WLOCK(inp);
2203fed74e9SGleb Smirnoff 	KASSERT(so->so_pcb == inp && inp->inp_socket == so,
2213fed74e9SGleb Smirnoff 		("%s: socket %p inp %p mismatch", __func__, so, inp));
222953b5606SRobert Watson 
223a152f8a3SRobert Watson 	tp = intotcpcb(inp);
224a152f8a3SRobert Watson 
225ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
226623dce13SRobert Watson 		/*
227a152f8a3SRobert Watson 		 * There are two cases to handle: one in which the time wait
228a152f8a3SRobert Watson 		 * state is being discarded (INP_DROPPED), and one in which
229a152f8a3SRobert Watson 		 * this connection will remain in timewait.  In the former,
230a152f8a3SRobert Watson 		 * it is time to discard all state (except tcptw, which has
231a152f8a3SRobert Watson 		 * already been discarded by the timewait close code, which
232a152f8a3SRobert Watson 		 * should be further up the call stack somewhere).  In the
233a152f8a3SRobert Watson 		 * latter case, we detach from the socket, but leave the pcb
234a152f8a3SRobert Watson 		 * present until timewait ends.
235623dce13SRobert Watson 		 *
236a152f8a3SRobert Watson 		 * XXXRW: Would it be cleaner to free the tcptw here?
237cea40c48SJulien Charbon 		 *
238cea40c48SJulien Charbon 		 * Astute question indeed, from twtcp perspective there are
239dd388cfdSGleb Smirnoff 		 * four cases to consider:
240cea40c48SJulien Charbon 		 *
2413fed74e9SGleb Smirnoff 		 * #1 tcp_usr_detach is called at tcptw creation time by
242cea40c48SJulien Charbon 		 *  tcp_twstart, then do not discard the newly created tcptw
243cea40c48SJulien Charbon 		 *  and leave inpcb present until timewait ends
2443fed74e9SGleb Smirnoff 		 * #2 tcp_usr_detach is called at tcptw creation time by
245dd388cfdSGleb Smirnoff 		 *  tcp_twstart, but connection is local and tw will be
246dd388cfdSGleb Smirnoff 		 *  discarded immediately
2473fed74e9SGleb Smirnoff 		 * #3 tcp_usr_detach is called at timewait end (or reuse) by
248cea40c48SJulien Charbon 		 *  tcp_twclose, then the tcptw has already been discarded
249ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
2503fed74e9SGleb Smirnoff 		 * #4 tcp_usr_detach is called() after timewait ends (or reuse)
251cea40c48SJulien Charbon 		 *  (e.g. by soclose), then tcptw has already been discarded
252ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
253cea40c48SJulien Charbon 		 *
254cea40c48SJulien Charbon 		 *  In all three cases the tcptw should not be freed here.
255623dce13SRobert Watson 		 */
256ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED) {
257623dce13SRobert Watson 			in_pcbdetach(inp);
258f5cf1e5fSJulien Charbon 			if (__predict_true(tp == NULL)) {
2590206cdb8SBjoern A. Zeeb 				in_pcbfree(inp);
2600206cdb8SBjoern A. Zeeb 			} else {
261f5cf1e5fSJulien Charbon 				/*
262f5cf1e5fSJulien Charbon 				 * This case should not happen as in TIMEWAIT
263f5cf1e5fSJulien Charbon 				 * state the inp should not be destroyed before
264f5cf1e5fSJulien Charbon 				 * its tcptw.  If INVARIANTS is defined, panic.
265f5cf1e5fSJulien Charbon 				 */
266f5cf1e5fSJulien Charbon #ifdef INVARIANTS
267f5cf1e5fSJulien Charbon 				panic("%s: Panic before an inp double-free: "
268f5cf1e5fSJulien Charbon 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
269f5cf1e5fSJulien Charbon 				    , __func__);
270f5cf1e5fSJulien Charbon #else
271f5cf1e5fSJulien Charbon 				log(LOG_ERR, "%s: Avoid an inp double-free: "
272f5cf1e5fSJulien Charbon 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
273f5cf1e5fSJulien Charbon 				    , __func__);
274f5cf1e5fSJulien Charbon #endif
275f5cf1e5fSJulien Charbon 				INP_WUNLOCK(inp);
276f5cf1e5fSJulien Charbon 			}
277f5cf1e5fSJulien Charbon 		} else {
278623dce13SRobert Watson 			in_pcbdetach(inp);
2798501a69cSRobert Watson 			INP_WUNLOCK(inp);
280623dce13SRobert Watson 		}
281623dce13SRobert Watson 	} else {
282e6e65783SRobert Watson 		/*
283a152f8a3SRobert Watson 		 * If the connection is not in timewait, we consider two
284a152f8a3SRobert Watson 		 * two conditions: one in which no further processing is
285a152f8a3SRobert Watson 		 * necessary (dropped || embryonic), and one in which TCP is
286a152f8a3SRobert Watson 		 * not yet done, but no longer requires the socket, so the
287a152f8a3SRobert Watson 		 * pcb will persist for the time being.
288a152f8a3SRobert Watson 		 *
289a152f8a3SRobert Watson 		 * XXXRW: Does the second case still occur?
290e6e65783SRobert Watson 		 */
291ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED ||
292623dce13SRobert Watson 		    tp->t_state < TCPS_SYN_SENT) {
293623dce13SRobert Watson 			tcp_discardcb(tp);
294623dce13SRobert Watson 			in_pcbdetach(inp);
2950206cdb8SBjoern A. Zeeb 			in_pcbfree(inp);
296db3cee51SNavdeep Parhar 		} else {
297a152f8a3SRobert Watson 			in_pcbdetach(inp);
298db3cee51SNavdeep Parhar 			INP_WUNLOCK(inp);
299db3cee51SNavdeep Parhar 		}
300623dce13SRobert Watson 	}
301623dce13SRobert Watson }
302c78cbc7bSRobert Watson 
303b287c6c7SBjoern A. Zeeb #ifdef INET
3042c37256eSGarrett Wollman /*
3052c37256eSGarrett Wollman  * Give the socket an address.
3062c37256eSGarrett Wollman  */
3072c37256eSGarrett Wollman static int
308b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
3092c37256eSGarrett Wollman {
3102c37256eSGarrett Wollman 	int error = 0;
311f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
312623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3132c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
3142c37256eSGarrett Wollman 
31552710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
31652710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sinp))
31752710de1SPawel Jakub Dawidek 		return (EINVAL);
3182c37256eSGarrett Wollman 	/*
3192c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
3202c37256eSGarrett Wollman 	 * to them.
3212c37256eSGarrett Wollman 	 */
3222c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
32352710de1SPawel Jakub Dawidek 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
32452710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
32552710de1SPawel Jakub Dawidek 
326623dce13SRobert Watson 	TCPDEBUG0;
327623dce13SRobert Watson 	inp = sotoinpcb(so);
328623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
3298501a69cSRobert Watson 	INP_WLOCK(inp);
330ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
331623dce13SRobert Watson 		error = EINVAL;
3322c37256eSGarrett Wollman 		goto out;
333623dce13SRobert Watson 	}
334623dce13SRobert Watson 	tp = intotcpcb(inp);
335623dce13SRobert Watson 	TCPDEBUG1();
336fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
337623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
338fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
339623dce13SRobert Watson out:
340623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3415d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3428501a69cSRobert Watson 	INP_WUNLOCK(inp);
343623dce13SRobert Watson 
344623dce13SRobert Watson 	return (error);
3452c37256eSGarrett Wollman }
346b287c6c7SBjoern A. Zeeb #endif /* INET */
3472c37256eSGarrett Wollman 
348fb59c426SYoshinobu Inoue #ifdef INET6
349fb59c426SYoshinobu Inoue static int
350b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
351fb59c426SYoshinobu Inoue {
352fb59c426SYoshinobu Inoue 	int error = 0;
353f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
354623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3550ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
3564a91aa8fSMichael Tuexen 	u_char vflagsav;
357fb59c426SYoshinobu Inoue 
3580ecd976eSBjoern A. Zeeb 	sin6 = (struct sockaddr_in6 *)nam;
3590ecd976eSBjoern A. Zeeb 	if (nam->sa_len != sizeof (*sin6))
36052710de1SPawel Jakub Dawidek 		return (EINVAL);
361fb59c426SYoshinobu Inoue 	/*
362fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
363fb59c426SYoshinobu Inoue 	 * to them.
364fb59c426SYoshinobu Inoue 	 */
3650ecd976eSBjoern A. Zeeb 	if (sin6->sin6_family == AF_INET6 &&
3660ecd976eSBjoern A. Zeeb 	    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
36752710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
36852710de1SPawel Jakub Dawidek 
369623dce13SRobert Watson 	TCPDEBUG0;
370623dce13SRobert Watson 	inp = sotoinpcb(so);
371623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
3728501a69cSRobert Watson 	INP_WLOCK(inp);
3734a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
374ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
375623dce13SRobert Watson 		error = EINVAL;
376623dce13SRobert Watson 		goto out;
377623dce13SRobert Watson 	}
378623dce13SRobert Watson 	tp = intotcpcb(inp);
379623dce13SRobert Watson 	TCPDEBUG1();
380fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
381fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
382fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
383b287c6c7SBjoern A. Zeeb #ifdef INET
38466ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
3850ecd976eSBjoern A. Zeeb 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
386fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
3870ecd976eSBjoern A. Zeeb 		else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
388fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
389fb59c426SYoshinobu Inoue 
3900ecd976eSBjoern A. Zeeb 			in6_sin6_2_sin(&sin, sin6);
391888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
392888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
393888973f5SMichael Tuexen 				INP_HASH_WUNLOCK(&V_tcbinfo);
394888973f5SMichael Tuexen 				goto out;
395888973f5SMichael Tuexen 			}
396fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
397fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
398b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
399b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
400fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
401fb59c426SYoshinobu Inoue 			goto out;
402fb59c426SYoshinobu Inoue 		}
403fb59c426SYoshinobu Inoue 	}
404b287c6c7SBjoern A. Zeeb #endif
405b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
406fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
407623dce13SRobert Watson out:
4084a91aa8fSMichael Tuexen 	if (error != 0)
4094a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
410623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
4115d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
4128501a69cSRobert Watson 	INP_WUNLOCK(inp);
413623dce13SRobert Watson 	return (error);
414fb59c426SYoshinobu Inoue }
415fb59c426SYoshinobu Inoue #endif /* INET6 */
416fb59c426SYoshinobu Inoue 
417b287c6c7SBjoern A. Zeeb #ifdef INET
4182c37256eSGarrett Wollman /*
4192c37256eSGarrett Wollman  * Prepare to accept connections.
4202c37256eSGarrett Wollman  */
4212c37256eSGarrett Wollman static int
422d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
4232c37256eSGarrett Wollman {
4242c37256eSGarrett Wollman 	int error = 0;
425f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
426623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4272c37256eSGarrett Wollman 
428623dce13SRobert Watson 	TCPDEBUG0;
429623dce13SRobert Watson 	inp = sotoinpcb(so);
430623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
4318501a69cSRobert Watson 	INP_WLOCK(inp);
432ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
433623dce13SRobert Watson 		error = EINVAL;
434623dce13SRobert Watson 		goto out;
435623dce13SRobert Watson 	}
436623dce13SRobert Watson 	tp = intotcpcb(inp);
437623dce13SRobert Watson 	TCPDEBUG1();
4380daccb9cSRobert Watson 	SOCK_LOCK(so);
4390daccb9cSRobert Watson 	error = solisten_proto_check(so);
440fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
4410daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0)
442b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
443fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4440daccb9cSRobert Watson 	if (error == 0) {
44557f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
446d374e81eSRobert Watson 		solisten_proto(so, backlog);
44709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
44837cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
44909fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
45009fe6320SNavdeep Parhar #endif
4510daccb9cSRobert Watson 	}
4520daccb9cSRobert Watson 	SOCK_UNLOCK(so);
453623dce13SRobert Watson 
45468bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
455281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
45618a75309SPatrick Kelsey 
457623dce13SRobert Watson out:
458623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4595d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4608501a69cSRobert Watson 	INP_WUNLOCK(inp);
461623dce13SRobert Watson 	return (error);
4622c37256eSGarrett Wollman }
463b287c6c7SBjoern A. Zeeb #endif /* INET */
4642c37256eSGarrett Wollman 
465fb59c426SYoshinobu Inoue #ifdef INET6
466fb59c426SYoshinobu Inoue static int
467d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
468fb59c426SYoshinobu Inoue {
469fb59c426SYoshinobu Inoue 	int error = 0;
470f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
471623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4724a91aa8fSMichael Tuexen 	u_char vflagsav;
473fb59c426SYoshinobu Inoue 
474623dce13SRobert Watson 	TCPDEBUG0;
475623dce13SRobert Watson 	inp = sotoinpcb(so);
476623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4778501a69cSRobert Watson 	INP_WLOCK(inp);
478ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
479623dce13SRobert Watson 		error = EINVAL;
480623dce13SRobert Watson 		goto out;
481623dce13SRobert Watson 	}
4824a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
483623dce13SRobert Watson 	tp = intotcpcb(inp);
484623dce13SRobert Watson 	TCPDEBUG1();
4850daccb9cSRobert Watson 	SOCK_LOCK(so);
4860daccb9cSRobert Watson 	error = solisten_proto_check(so);
487fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
4880daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0) {
489fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
49066ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
491fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
492b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
493fb59c426SYoshinobu Inoue 	}
494fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4950daccb9cSRobert Watson 	if (error == 0) {
49657f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
497d374e81eSRobert Watson 		solisten_proto(so, backlog);
49809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
49937cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
50009fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
50109fe6320SNavdeep Parhar #endif
5020daccb9cSRobert Watson 	}
5030daccb9cSRobert Watson 	SOCK_UNLOCK(so);
504623dce13SRobert Watson 
50568bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
506281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
50718a75309SPatrick Kelsey 
5084a91aa8fSMichael Tuexen 	if (error != 0)
5094a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
5104a91aa8fSMichael Tuexen 
511623dce13SRobert Watson out:
512623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
5135d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
5148501a69cSRobert Watson 	INP_WUNLOCK(inp);
515623dce13SRobert Watson 	return (error);
516fb59c426SYoshinobu Inoue }
517fb59c426SYoshinobu Inoue #endif /* INET6 */
518fb59c426SYoshinobu Inoue 
519b287c6c7SBjoern A. Zeeb #ifdef INET
5202c37256eSGarrett Wollman /*
5212c37256eSGarrett Wollman  * Initiate connection to peer.
5222c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
5232c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
5242c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
5252c37256eSGarrett Wollman  * Send initial segment on connection.
5262c37256eSGarrett Wollman  */
5272c37256eSGarrett Wollman static int
528b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
5292c37256eSGarrett Wollman {
530109eb549SGleb Smirnoff 	struct epoch_tracker et;
5312c37256eSGarrett Wollman 	int error = 0;
532f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
533623dce13SRobert Watson 	struct tcpcb *tp = NULL;
5342c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
5352c37256eSGarrett Wollman 
53657bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
537e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
538e29ef13fSDon Lewis 		return (EINVAL);
53952710de1SPawel Jakub Dawidek 	/*
54052710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
54152710de1SPawel Jakub Dawidek 	 */
5422c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
54352710de1SPawel Jakub Dawidek 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
54452710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
545b89e82ddSJamie Gritton 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
546b89e82ddSJamie Gritton 		return (error);
54775c13541SPoul-Henning Kamp 
548623dce13SRobert Watson 	TCPDEBUG0;
549623dce13SRobert Watson 	inp = sotoinpcb(so);
550623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
5518501a69cSRobert Watson 	INP_WLOCK(inp);
552eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
553eb96dc33SJulien Charbon 		error = EADDRINUSE;
554eb96dc33SJulien Charbon 		goto out;
555eb96dc33SJulien Charbon 	}
556eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
557eb96dc33SJulien Charbon 		error = ECONNREFUSED;
558623dce13SRobert Watson 		goto out;
559623dce13SRobert Watson 	}
560623dce13SRobert Watson 	tp = intotcpcb(inp);
561623dce13SRobert Watson 	TCPDEBUG1();
562c1604fe4SGleb Smirnoff 	NET_EPOCH_ENTER(et);
563b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
564c1604fe4SGleb Smirnoff 		goto out_in_epoch;
56509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
56609fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
56737cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
56809fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
569c1604fe4SGleb Smirnoff 		goto out_in_epoch;
57009fe6320SNavdeep Parhar #endif
57109fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
57255bceb1eSRandall Stewart 	error = tp->t_fb->tfb_tcp_output(tp);
573c1604fe4SGleb Smirnoff out_in_epoch:
574109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
575623dce13SRobert Watson out:
576623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
577e79cb051SGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
5788501a69cSRobert Watson 	INP_WUNLOCK(inp);
579623dce13SRobert Watson 	return (error);
5802c37256eSGarrett Wollman }
581b287c6c7SBjoern A. Zeeb #endif /* INET */
5822c37256eSGarrett Wollman 
583fb59c426SYoshinobu Inoue #ifdef INET6
584fb59c426SYoshinobu Inoue static int
585b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
586fb59c426SYoshinobu Inoue {
587109eb549SGleb Smirnoff 	struct epoch_tracker et;
588fb59c426SYoshinobu Inoue 	int error = 0;
589f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
590623dce13SRobert Watson 	struct tcpcb *tp = NULL;
5910ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
5924a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
5934a91aa8fSMichael Tuexen 	u_char vflagsav;
594623dce13SRobert Watson 
595623dce13SRobert Watson 	TCPDEBUG0;
596fb59c426SYoshinobu Inoue 
5970ecd976eSBjoern A. Zeeb 	sin6 = (struct sockaddr_in6 *)nam;
5980ecd976eSBjoern A. Zeeb 	if (nam->sa_len != sizeof (*sin6))
599e29ef13fSDon Lewis 		return (EINVAL);
60052710de1SPawel Jakub Dawidek 	/*
60152710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
60252710de1SPawel Jakub Dawidek 	 */
6030ecd976eSBjoern A. Zeeb 	if (sin6->sin6_family == AF_INET6
6040ecd976eSBjoern A. Zeeb 	    && IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
60552710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
606fb59c426SYoshinobu Inoue 
607623dce13SRobert Watson 	inp = sotoinpcb(so);
608623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
6098501a69cSRobert Watson 	INP_WLOCK(inp);
6104a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
6114a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
612eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
613eb96dc33SJulien Charbon 		error = EADDRINUSE;
614eb96dc33SJulien Charbon 		goto out;
615eb96dc33SJulien Charbon 	}
616eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
617eb96dc33SJulien Charbon 		error = ECONNREFUSED;
618623dce13SRobert Watson 		goto out;
619623dce13SRobert Watson 	}
620623dce13SRobert Watson 	tp = intotcpcb(inp);
621623dce13SRobert Watson 	TCPDEBUG1();
622b287c6c7SBjoern A. Zeeb #ifdef INET
623fa046d87SRobert Watson 	/*
624fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
625fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
626fa046d87SRobert Watson 	 * Is this a significant problem?
627fa046d87SRobert Watson 	 */
6280ecd976eSBjoern A. Zeeb 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
629fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
630fb59c426SYoshinobu Inoue 
631d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
632d46a5312SMaxim Konovalov 			error = EINVAL;
633d46a5312SMaxim Konovalov 			goto out;
634d46a5312SMaxim Konovalov 		}
6355dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV4) == 0) {
6365dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6375dba6adaSMichael Tuexen 			goto out;
6385dba6adaSMichael Tuexen 		}
63933841545SHajimu UMEMOTO 
6400ecd976eSBjoern A. Zeeb 		in6_sin6_2_sin(&sin, sin6);
641888973f5SMichael Tuexen 		if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
642888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
643888973f5SMichael Tuexen 			goto out;
644888973f5SMichael Tuexen 		}
645b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
646b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
647413628a7SBjoern A. Zeeb 			goto out;
6484a91aa8fSMichael Tuexen 		inp->inp_vflag |= INP_IPV4;
6494a91aa8fSMichael Tuexen 		inp->inp_vflag &= ~INP_IPV6;
650c1604fe4SGleb Smirnoff 		NET_EPOCH_ENTER(et);
651b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
652c1604fe4SGleb Smirnoff 			goto out_in_epoch;
65309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
65409fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
655adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
65609fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
657c1604fe4SGleb Smirnoff 			goto out_in_epoch;
65809fe6320SNavdeep Parhar #endif
65955bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
660c1604fe4SGleb Smirnoff 		goto out_in_epoch;
6615dba6adaSMichael Tuexen 	} else {
6625dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV6) == 0) {
6635dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6645dba6adaSMichael Tuexen 			goto out;
6655dba6adaSMichael Tuexen 		}
666fb59c426SYoshinobu Inoue 	}
667b287c6c7SBjoern A. Zeeb #endif
6684a91aa8fSMichael Tuexen 	if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
6694a91aa8fSMichael Tuexen 		goto out;
670fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
671fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
672dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
673b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
674fb59c426SYoshinobu Inoue 		goto out;
67509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
67609fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
677adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
67809fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
67909fe6320SNavdeep Parhar 		goto out;
68009fe6320SNavdeep Parhar #endif
68109fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
682109eb549SGleb Smirnoff 	NET_EPOCH_ENTER(et);
68355bceb1eSRandall Stewart 	error = tp->t_fb->tfb_tcp_output(tp);
6847754e281SBjoern A. Zeeb #ifdef INET
685c1604fe4SGleb Smirnoff out_in_epoch:
6867754e281SBjoern A. Zeeb #endif
687109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
688623dce13SRobert Watson out:
6894a91aa8fSMichael Tuexen 	/*
6904a91aa8fSMichael Tuexen 	 * If the implicit bind in the connect call fails, restore
6914a91aa8fSMichael Tuexen 	 * the flags we modified.
6924a91aa8fSMichael Tuexen 	 */
6934a91aa8fSMichael Tuexen 	if (error != 0 && inp->inp_lport == 0) {
6944a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
6954a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
6964a91aa8fSMichael Tuexen 	}
6974a91aa8fSMichael Tuexen 
698623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
6995d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
7008501a69cSRobert Watson 	INP_WUNLOCK(inp);
701623dce13SRobert Watson 	return (error);
702fb59c426SYoshinobu Inoue }
703fb59c426SYoshinobu Inoue #endif /* INET6 */
704fb59c426SYoshinobu Inoue 
7052c37256eSGarrett Wollman /*
7062c37256eSGarrett Wollman  * Initiate disconnect from peer.
7072c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
7082c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
7092c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
7102c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
7112c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
7122c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
7132c37256eSGarrett Wollman  *
7142c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
7152c37256eSGarrett Wollman  */
7162c37256eSGarrett Wollman static int
7172c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
7182c37256eSGarrett Wollman {
719f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
720623dce13SRobert Watson 	struct tcpcb *tp = NULL;
7216573d758SMatt Macy 	struct epoch_tracker et;
722623dce13SRobert Watson 	int error = 0;
7232c37256eSGarrett Wollman 
724623dce13SRobert Watson 	TCPDEBUG0;
72597a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
726623dce13SRobert Watson 	inp = sotoinpcb(so);
727623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
7288501a69cSRobert Watson 	INP_WLOCK(inp);
729489dcc92SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT)
730489dcc92SJulien Charbon 		goto out;
731489dcc92SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
73221367f63SSam Leffler 		error = ECONNRESET;
733623dce13SRobert Watson 		goto out;
734623dce13SRobert Watson 	}
735623dce13SRobert Watson 	tp = intotcpcb(inp);
736623dce13SRobert Watson 	TCPDEBUG1();
737623dce13SRobert Watson 	tcp_disconnect(tp);
738623dce13SRobert Watson out:
739623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
7405d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
7418501a69cSRobert Watson 	INP_WUNLOCK(inp);
74297a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
743623dce13SRobert Watson 	return (error);
7442c37256eSGarrett Wollman }
7452c37256eSGarrett Wollman 
746b287c6c7SBjoern A. Zeeb #ifdef INET
7472c37256eSGarrett Wollman /*
7488296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
7498296cddfSRobert Watson  * just return the address of the peer, storing through addr.
7502c37256eSGarrett Wollman  */
7512c37256eSGarrett Wollman static int
75257bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
7532c37256eSGarrett Wollman {
7542c37256eSGarrett Wollman 	int error = 0;
755f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
7561db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
75726ef6ac4SDon Lewis 	struct in_addr addr;
75826ef6ac4SDon Lewis 	in_port_t port = 0;
7591db24ffbSJonathan Lemon 	TCPDEBUG0;
7602c37256eSGarrett Wollman 
7613d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
7623d2d3ef4SRobert Watson 		return (ECONNABORTED);
763f76fcf6dSJeffrey Hsu 
764f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
765623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
7668501a69cSRobert Watson 	INP_WLOCK(inp);
767ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
7683d2d3ef4SRobert Watson 		error = ECONNABORTED;
769623dce13SRobert Watson 		goto out;
770623dce13SRobert Watson 	}
7711db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
7721db24ffbSJonathan Lemon 	TCPDEBUG1();
773f76fcf6dSJeffrey Hsu 
774f76fcf6dSJeffrey Hsu 	/*
77554d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
77626ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
77726ef6ac4SDon Lewis 	 * release the lock.
778f76fcf6dSJeffrey Hsu 	 */
77926ef6ac4SDon Lewis 	port = inp->inp_fport;
78026ef6ac4SDon Lewis 	addr = inp->inp_faddr;
781f76fcf6dSJeffrey Hsu 
782623dce13SRobert Watson out:
783623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
7845d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7858501a69cSRobert Watson 	INP_WUNLOCK(inp);
78626ef6ac4SDon Lewis 	if (error == 0)
78726ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
78826ef6ac4SDon Lewis 	return error;
7892c37256eSGarrett Wollman }
790b287c6c7SBjoern A. Zeeb #endif /* INET */
7912c37256eSGarrett Wollman 
792fb59c426SYoshinobu Inoue #ifdef INET6
793fb59c426SYoshinobu Inoue static int
794fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
795fb59c426SYoshinobu Inoue {
796f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
797fb59c426SYoshinobu Inoue 	int error = 0;
7981db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
79926ef6ac4SDon Lewis 	struct in_addr addr;
80026ef6ac4SDon Lewis 	struct in6_addr addr6;
8016573d758SMatt Macy 	struct epoch_tracker et;
80226ef6ac4SDon Lewis 	in_port_t port = 0;
80326ef6ac4SDon Lewis 	int v4 = 0;
8041db24ffbSJonathan Lemon 	TCPDEBUG0;
805fb59c426SYoshinobu Inoue 
806b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
807b4470c16SRobert Watson 		return (ECONNABORTED);
808f76fcf6dSJeffrey Hsu 
809f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
810623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
81197a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
8128501a69cSRobert Watson 	INP_WLOCK(inp);
813ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
81421367f63SSam Leffler 		error = ECONNABORTED;
815623dce13SRobert Watson 		goto out;
816623dce13SRobert Watson 	}
8171db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
8181db24ffbSJonathan Lemon 	TCPDEBUG1();
819623dce13SRobert Watson 
82026ef6ac4SDon Lewis 	/*
82126ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
82226ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
82326ef6ac4SDon Lewis 	 * release the lock.
82426ef6ac4SDon Lewis 	 */
82526ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
82626ef6ac4SDon Lewis 		v4 = 1;
82726ef6ac4SDon Lewis 		port = inp->inp_fport;
82826ef6ac4SDon Lewis 		addr = inp->inp_faddr;
82926ef6ac4SDon Lewis 	} else {
83026ef6ac4SDon Lewis 		port = inp->inp_fport;
83126ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
83226ef6ac4SDon Lewis 	}
83326ef6ac4SDon Lewis 
834623dce13SRobert Watson out:
835623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
8365d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
8378501a69cSRobert Watson 	INP_WUNLOCK(inp);
83897a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
83926ef6ac4SDon Lewis 	if (error == 0) {
84026ef6ac4SDon Lewis 		if (v4)
84126ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
84226ef6ac4SDon Lewis 		else
84326ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
84426ef6ac4SDon Lewis 	}
84526ef6ac4SDon Lewis 	return error;
846fb59c426SYoshinobu Inoue }
847fb59c426SYoshinobu Inoue #endif /* INET6 */
848f76fcf6dSJeffrey Hsu 
849f76fcf6dSJeffrey Hsu /*
8502c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
8512c37256eSGarrett Wollman  */
8522c37256eSGarrett Wollman static int
8532c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
8542c37256eSGarrett Wollman {
8552c37256eSGarrett Wollman 	int error = 0;
856f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
857623dce13SRobert Watson 	struct tcpcb *tp = NULL;
8586573d758SMatt Macy 	struct epoch_tracker et;
8592c37256eSGarrett Wollman 
860623dce13SRobert Watson 	TCPDEBUG0;
86197a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
862623dce13SRobert Watson 	inp = sotoinpcb(so);
863623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
8648501a69cSRobert Watson 	INP_WLOCK(inp);
865ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
86621367f63SSam Leffler 		error = ECONNRESET;
867623dce13SRobert Watson 		goto out;
868623dce13SRobert Watson 	}
869623dce13SRobert Watson 	tp = intotcpcb(inp);
870623dce13SRobert Watson 	TCPDEBUG1();
8712c37256eSGarrett Wollman 	socantsendmore(so);
872623dce13SRobert Watson 	tcp_usrclosed(tp);
873ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
87455bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
875623dce13SRobert Watson 
876623dce13SRobert Watson out:
877623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
8785d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
8798501a69cSRobert Watson 	INP_WUNLOCK(inp);
88097a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
881623dce13SRobert Watson 
882623dce13SRobert Watson 	return (error);
8832c37256eSGarrett Wollman }
8842c37256eSGarrett Wollman 
8852c37256eSGarrett Wollman /*
8862c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
8872c37256eSGarrett Wollman  */
8882c37256eSGarrett Wollman static int
8892c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
8902c37256eSGarrett Wollman {
891109eb549SGleb Smirnoff 	struct epoch_tracker et;
892f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
893623dce13SRobert Watson 	struct tcpcb *tp = NULL;
894623dce13SRobert Watson 	int error = 0;
8952c37256eSGarrett Wollman 
896623dce13SRobert Watson 	TCPDEBUG0;
897623dce13SRobert Watson 	inp = sotoinpcb(so);
898623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
8998501a69cSRobert Watson 	INP_WLOCK(inp);
900ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
90121367f63SSam Leffler 		error = ECONNRESET;
902623dce13SRobert Watson 		goto out;
903623dce13SRobert Watson 	}
904623dce13SRobert Watson 	tp = intotcpcb(inp);
905623dce13SRobert Watson 	TCPDEBUG1();
906281a0fd4SPatrick Kelsey 	/*
907281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
908281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
909281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
910281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
911281a0fd4SPatrick Kelsey 	 * expires.
912281a0fd4SPatrick Kelsey 	 */
91368bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags) &&
914281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
915281a0fd4SPatrick Kelsey 		goto out;
91642ce7937SGleb Smirnoff 	NET_EPOCH_ENTER(et);
91709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
91809fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
91909fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
920460cf046SNavdeep Parhar 	else
92109fe6320SNavdeep Parhar #endif
92255bceb1eSRandall Stewart 	tp->t_fb->tfb_tcp_output(tp);
923109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
924623dce13SRobert Watson out:
925623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
9265d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
9278501a69cSRobert Watson 	INP_WUNLOCK(inp);
928623dce13SRobert Watson 	return (error);
9292c37256eSGarrett Wollman }
9302c37256eSGarrett Wollman 
9312c37256eSGarrett Wollman /*
9322c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
9339c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
9349c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
9359c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
9369c9906e9SPeter Wemm  * generally are caller-frees.
9372c37256eSGarrett Wollman  */
9382c37256eSGarrett Wollman static int
93957bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
940b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
9412c37256eSGarrett Wollman {
94297a95ee1SGleb Smirnoff 	struct epoch_tracker et;
9432c37256eSGarrett Wollman 	int error = 0;
944f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
945623dce13SRobert Watson 	struct tcpcb *tp = NULL;
946888973f5SMichael Tuexen #ifdef INET
94751e08d53SMichael Tuexen #ifdef INET6
94851e08d53SMichael Tuexen 	struct sockaddr_in sin;
94951e08d53SMichael Tuexen #endif
95051e08d53SMichael Tuexen 	struct sockaddr_in *sinp;
951888973f5SMichael Tuexen #endif
952fb59c426SYoshinobu Inoue #ifdef INET6
953fb59c426SYoshinobu Inoue 	int isipv6;
954fb59c426SYoshinobu Inoue #endif
9554a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
9564a91aa8fSMichael Tuexen 	u_char vflagsav;
9574a91aa8fSMichael Tuexen 	bool restoreflags;
9589c9906e9SPeter Wemm 	TCPDEBUG0;
9592c37256eSGarrett Wollman 
960f76fcf6dSJeffrey Hsu 	/*
96197a95ee1SGleb Smirnoff 	 * We require the pcbinfo "read lock" if we will close the socket
96297a95ee1SGleb Smirnoff 	 * as part of this call.
963f76fcf6dSJeffrey Hsu 	 */
96497a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
965f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
966623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
9678501a69cSRobert Watson 	INP_WLOCK(inp);
9684a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
9694a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
9704a91aa8fSMichael Tuexen 	restoreflags = false;
971ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
9727ff0b850SAndre Oppermann 		if (control)
9737ff0b850SAndre Oppermann 			m_freem(control);
9742cbcd3c1SGleb Smirnoff 		/*
9752cbcd3c1SGleb Smirnoff 		 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
9762cbcd3c1SGleb Smirnoff 		 * for freeing memory.
9772cbcd3c1SGleb Smirnoff 		 */
9782cbcd3c1SGleb Smirnoff 		if (m && (flags & PRUS_NOTREADY) == 0)
9797ff0b850SAndre Oppermann 			m_freem(m);
98021367f63SSam Leffler 		error = ECONNRESET;
9819c9906e9SPeter Wemm 		goto out;
9829c9906e9SPeter Wemm 	}
9839c9906e9SPeter Wemm 	tp = intotcpcb(inp);
984*d3b6c96bSRandall Stewart 	if (flags & PRUS_OOB) {
985*d3b6c96bSRandall Stewart 		if ((error = tcp_pru_options_support(tp, PRUS_OOB)) != 0) {
986*d3b6c96bSRandall Stewart 			if (control)
987*d3b6c96bSRandall Stewart 				m_freem(control);
988*d3b6c96bSRandall Stewart 			if (m && (flags & PRUS_NOTREADY) == 0)
989*d3b6c96bSRandall Stewart 				m_freem(m);
990*d3b6c96bSRandall Stewart 			goto out;
991*d3b6c96bSRandall Stewart 		}
992*d3b6c96bSRandall Stewart 	}
9939c9906e9SPeter Wemm 	TCPDEBUG1();
994888973f5SMichael Tuexen 	if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
995888973f5SMichael Tuexen 		switch (nam->sa_family) {
996888973f5SMichael Tuexen #ifdef INET
997888973f5SMichael Tuexen 		case AF_INET:
998888973f5SMichael Tuexen 			sinp = (struct sockaddr_in *)nam;
999888973f5SMichael Tuexen 			if (sinp->sin_len != sizeof(struct sockaddr_in)) {
1000888973f5SMichael Tuexen 				if (m)
1001888973f5SMichael Tuexen 					m_freem(m);
1002888973f5SMichael Tuexen 				error = EINVAL;
1003888973f5SMichael Tuexen 				goto out;
1004888973f5SMichael Tuexen 			}
1005888973f5SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6) != 0) {
1006888973f5SMichael Tuexen 				if (m)
1007888973f5SMichael Tuexen 					m_freem(m);
1008888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1009888973f5SMichael Tuexen 				goto out;
1010888973f5SMichael Tuexen 			}
1011888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
1012888973f5SMichael Tuexen 				if (m)
1013888973f5SMichael Tuexen 					m_freem(m);
1014888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1015888973f5SMichael Tuexen 				goto out;
1016888973f5SMichael Tuexen 			}
1017888973f5SMichael Tuexen 			if ((error = prison_remote_ip4(td->td_ucred,
1018888973f5SMichael Tuexen 			    &sinp->sin_addr))) {
1019888973f5SMichael Tuexen 				if (m)
1020888973f5SMichael Tuexen 					m_freem(m);
1021888973f5SMichael Tuexen 				goto out;
1022888973f5SMichael Tuexen 			}
1023888973f5SMichael Tuexen #ifdef INET6
1024888973f5SMichael Tuexen 			isipv6 = 0;
1025888973f5SMichael Tuexen #endif
1026888973f5SMichael Tuexen 			break;
1027888973f5SMichael Tuexen #endif /* INET */
1028888973f5SMichael Tuexen #ifdef INET6
1029888973f5SMichael Tuexen 		case AF_INET6:
1030888973f5SMichael Tuexen 		{
10310ecd976eSBjoern A. Zeeb 			struct sockaddr_in6 *sin6;
1032888973f5SMichael Tuexen 
10330ecd976eSBjoern A. Zeeb 			sin6 = (struct sockaddr_in6 *)nam;
10340ecd976eSBjoern A. Zeeb 			if (sin6->sin6_len != sizeof(*sin6)) {
1035888973f5SMichael Tuexen 				if (m)
1036888973f5SMichael Tuexen 					m_freem(m);
1037888973f5SMichael Tuexen 				error = EINVAL;
1038888973f5SMichael Tuexen 				goto out;
1039888973f5SMichael Tuexen 			}
10400ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1041888973f5SMichael Tuexen 				if (m)
1042888973f5SMichael Tuexen 					m_freem(m);
1043888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1044888973f5SMichael Tuexen 				goto out;
1045888973f5SMichael Tuexen 			}
10460ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1047888973f5SMichael Tuexen #ifdef INET
1048888973f5SMichael Tuexen 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1049888973f5SMichael Tuexen 					error = EINVAL;
1050888973f5SMichael Tuexen 					if (m)
1051888973f5SMichael Tuexen 						m_freem(m);
1052888973f5SMichael Tuexen 					goto out;
1053888973f5SMichael Tuexen 				}
1054888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV4) == 0) {
1055888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1056888973f5SMichael Tuexen 					if (m)
1057888973f5SMichael Tuexen 						m_freem(m);
1058888973f5SMichael Tuexen 					goto out;
1059888973f5SMichael Tuexen 				}
10604a91aa8fSMichael Tuexen 				restoreflags = true;
1061888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV6;
1062888973f5SMichael Tuexen 				sinp = &sin;
10630ecd976eSBjoern A. Zeeb 				in6_sin6_2_sin(sinp, sin6);
1064888973f5SMichael Tuexen 				if (IN_MULTICAST(
1065888973f5SMichael Tuexen 				    ntohl(sinp->sin_addr.s_addr))) {
1066888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1067888973f5SMichael Tuexen 					if (m)
1068888973f5SMichael Tuexen 						m_freem(m);
1069888973f5SMichael Tuexen 					goto out;
1070888973f5SMichael Tuexen 				}
1071888973f5SMichael Tuexen 				if ((error = prison_remote_ip4(td->td_ucred,
1072888973f5SMichael Tuexen 				    &sinp->sin_addr))) {
1073888973f5SMichael Tuexen 					if (m)
1074888973f5SMichael Tuexen 						m_freem(m);
1075888973f5SMichael Tuexen 					goto out;
1076888973f5SMichael Tuexen 				}
1077888973f5SMichael Tuexen 				isipv6 = 0;
1078888973f5SMichael Tuexen #else /* !INET */
1079888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1080888973f5SMichael Tuexen 				if (m)
1081888973f5SMichael Tuexen 					m_freem(m);
1082888973f5SMichael Tuexen 				goto out;
1083888973f5SMichael Tuexen #endif /* INET */
1084888973f5SMichael Tuexen 			} else {
1085888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV6) == 0) {
1086888973f5SMichael Tuexen 					if (m)
1087888973f5SMichael Tuexen 						m_freem(m);
1088888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1089888973f5SMichael Tuexen 					goto out;
1090888973f5SMichael Tuexen 				}
10914a91aa8fSMichael Tuexen 				restoreflags = true;
1092888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV4;
1093888973f5SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_ISIPV6;
1094888973f5SMichael Tuexen 				if ((error = prison_remote_ip6(td->td_ucred,
10950ecd976eSBjoern A. Zeeb 				    &sin6->sin6_addr))) {
1096888973f5SMichael Tuexen 					if (m)
1097888973f5SMichael Tuexen 						m_freem(m);
1098888973f5SMichael Tuexen 					goto out;
1099888973f5SMichael Tuexen 				}
1100888973f5SMichael Tuexen 				isipv6 = 1;
1101888973f5SMichael Tuexen 			}
1102888973f5SMichael Tuexen 			break;
1103888973f5SMichael Tuexen 		}
1104888973f5SMichael Tuexen #endif /* INET6 */
1105888973f5SMichael Tuexen 		default:
1106888973f5SMichael Tuexen 			if (m)
1107888973f5SMichael Tuexen 				m_freem(m);
1108888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
1109888973f5SMichael Tuexen 			goto out;
1110888973f5SMichael Tuexen 		}
1111888973f5SMichael Tuexen 	}
11129c9906e9SPeter Wemm 	if (control) {
11139c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
11149c9906e9SPeter Wemm 		if (control->m_len) {
11159c9906e9SPeter Wemm 			m_freem(control);
11162c37256eSGarrett Wollman 			if (m)
11172c37256eSGarrett Wollman 				m_freem(m);
1118744f87eaSDavid Greenman 			error = EINVAL;
1119744f87eaSDavid Greenman 			goto out;
11202c37256eSGarrett Wollman 		}
11219c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
11229c9906e9SPeter Wemm 	}
11232c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
1124651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
11252c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
11262c37256eSGarrett Wollman 			/*
11272c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
11282c37256eSGarrett Wollman 			 * initialize window to default value, and
11290c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
11302c37256eSGarrett Wollman 			 */
1131fb59c426SYoshinobu Inoue #ifdef INET6
1132fb59c426SYoshinobu Inoue 			if (isipv6)
1133b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1134fb59c426SYoshinobu Inoue #endif /* INET6 */
1135b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1136b287c6c7SBjoern A. Zeeb 			else
1137b287c6c7SBjoern A. Zeeb #endif
1138b287c6c7SBjoern A. Zeeb #ifdef INET
1139888973f5SMichael Tuexen 				error = tcp_connect(tp,
1140888973f5SMichael Tuexen 				    (struct sockaddr *)sinp, td);
1141b287c6c7SBjoern A. Zeeb #endif
11424a91aa8fSMichael Tuexen 			/*
11434a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
11444a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
11454a91aa8fSMichael Tuexen 			 * operations fail.
11464a91aa8fSMichael Tuexen 			 */
11474a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
11484a91aa8fSMichael Tuexen 				restoreflags = false;
11494a91aa8fSMichael Tuexen 
11502c37256eSGarrett Wollman 			if (error)
11512c37256eSGarrett Wollman 				goto out;
1152c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1153c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
115418a75309SPatrick Kelsey 			else {
11552c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
11562c37256eSGarrett Wollman 				tcp_mss(tp, -1);
11572c37256eSGarrett Wollman 			}
1158c560df6fSPatrick Kelsey 		}
11592c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
11602c37256eSGarrett Wollman 			/*
11612c37256eSGarrett Wollman 			 * Close the send side of the connection after
11622c37256eSGarrett Wollman 			 * the data is sent.
11632c37256eSGarrett Wollman 			 */
11642c37256eSGarrett Wollman 			socantsendmore(so);
1165623dce13SRobert Watson 			tcp_usrclosed(tp);
11662c37256eSGarrett Wollman 		}
11672cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
11682cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
1169b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1170b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
117155bceb1eSRandall Stewart 			error = tp->t_fb->tfb_tcp_output(tp);
1172b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1173b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
1174b0acefa8SBill Fenner 		}
11752c37256eSGarrett Wollman 	} else {
1176623dce13SRobert Watson 		/*
1177623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1178623dce13SRobert Watson 		 */
1179d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
11802c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
1181d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
11822c37256eSGarrett Wollman 			m_freem(m);
11832c37256eSGarrett Wollman 			error = ENOBUFS;
11842c37256eSGarrett Wollman 			goto out;
11852c37256eSGarrett Wollman 		}
11862c37256eSGarrett Wollman 		/*
11872c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
11882c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
11892c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
11902c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
11912c37256eSGarrett Wollman 		 * of data past the urgent section.
11922c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
11932c37256eSGarrett Wollman 		 */
1194651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
1195d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
1196ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1197ef53690bSGarrett Wollman 			/*
1198ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
1199ef53690bSGarrett Wollman 			 * initialize window to default value, and
12000c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1201ef53690bSGarrett Wollman 			 */
120218a75309SPatrick Kelsey 
1203c560df6fSPatrick Kelsey 			/*
1204c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1205c560df6fSPatrick Kelsey 			 */
1206c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1207c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1208fb59c426SYoshinobu Inoue #ifdef INET6
1209fb59c426SYoshinobu Inoue 			if (isipv6)
1210b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1211fb59c426SYoshinobu Inoue #endif /* INET6 */
1212b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1213b287c6c7SBjoern A. Zeeb 			else
1214b287c6c7SBjoern A. Zeeb #endif
1215b287c6c7SBjoern A. Zeeb #ifdef INET
1216888973f5SMichael Tuexen 				error = tcp_connect(tp,
1217888973f5SMichael Tuexen 				    (struct sockaddr *)sinp, td);
1218b287c6c7SBjoern A. Zeeb #endif
12194a91aa8fSMichael Tuexen 			/*
12204a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
12214a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
12224a91aa8fSMichael Tuexen 			 * operations fail.
12234a91aa8fSMichael Tuexen 			 */
12244a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
12254a91aa8fSMichael Tuexen 				restoreflags = false;
12264a91aa8fSMichael Tuexen 
1227ef53690bSGarrett Wollman 			if (error)
1228ef53690bSGarrett Wollman 				goto out;
1229ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1230ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
1231623dce13SRobert Watson 		}
1232300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
12332cbcd3c1SGleb Smirnoff 		if (!(flags & PRUS_NOTREADY)) {
12342cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
123555bceb1eSRandall Stewart 			error = tp->t_fb->tfb_tcp_output(tp);
12362cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
12372c37256eSGarrett Wollman 		}
12382cbcd3c1SGleb Smirnoff 	}
12392529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL,
12402529f56eSJonathan T. Looney 	    &inp->inp_socket->so_rcv,
12412529f56eSJonathan T. Looney 	    &inp->inp_socket->so_snd,
12422529f56eSJonathan T. Looney 	    TCP_LOG_USERSEND, error,
12432529f56eSJonathan T. Looney 	    0, NULL, false);
1244d1401c90SRobert Watson out:
12454a91aa8fSMichael Tuexen 	/*
12464a91aa8fSMichael Tuexen 	 * If the request was unsuccessful and we changed flags,
12474a91aa8fSMichael Tuexen 	 * restore the original flags.
12484a91aa8fSMichael Tuexen 	 */
12494a91aa8fSMichael Tuexen 	if (error != 0 && restoreflags) {
12504a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
12514a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
12524a91aa8fSMichael Tuexen 	}
1253d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
12542c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
12555d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
12565d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
12578501a69cSRobert Watson 	INP_WUNLOCK(inp);
125897a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
125973fddedaSPeter Grehan 	return (error);
12602c37256eSGarrett Wollman }
12612c37256eSGarrett Wollman 
12622cbcd3c1SGleb Smirnoff static int
12632cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
12642cbcd3c1SGleb Smirnoff {
1265109eb549SGleb Smirnoff 	struct epoch_tracker et;
12662cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
12672cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
12682cbcd3c1SGleb Smirnoff 	int error;
12692cbcd3c1SGleb Smirnoff 
12702cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
12712cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
12722cbcd3c1SGleb Smirnoff 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
12732cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
127482334850SJohn Baldwin 		mb_free_notready(m, count);
12752cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
12762cbcd3c1SGleb Smirnoff 	}
12772cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
12782cbcd3c1SGleb Smirnoff 
12792cbcd3c1SGleb Smirnoff 	SOCKBUF_LOCK(&so->so_snd);
12802cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
12812cbcd3c1SGleb Smirnoff 	SOCKBUF_UNLOCK(&so->so_snd);
1282109eb549SGleb Smirnoff 	if (error == 0) {
1283109eb549SGleb Smirnoff 		NET_EPOCH_ENTER(et);
128455bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
1285109eb549SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1286109eb549SGleb Smirnoff 	}
12872cbcd3c1SGleb Smirnoff 	INP_WUNLOCK(inp);
12882cbcd3c1SGleb Smirnoff 
12892cbcd3c1SGleb Smirnoff 	return (error);
12902cbcd3c1SGleb Smirnoff }
12912cbcd3c1SGleb Smirnoff 
12922c37256eSGarrett Wollman /*
1293a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
12942c37256eSGarrett Wollman  */
1295ac45e92fSRobert Watson static void
12962c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
12972c37256eSGarrett Wollman {
1298f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1299a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
13006573d758SMatt Macy 	struct epoch_tracker et;
1301623dce13SRobert Watson 	TCPDEBUG0;
1302c78cbc7bSRobert Watson 
1303ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1304c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1305c78cbc7bSRobert Watson 
130697a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
13078501a69cSRobert Watson 	INP_WLOCK(inp);
1308c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1309c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1310c78cbc7bSRobert Watson 
1311c78cbc7bSRobert Watson 	/*
1312a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1313c78cbc7bSRobert Watson 	 */
1314ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1315ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1316c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
1317a152f8a3SRobert Watson 		TCPDEBUG1();
13188fa799bdSJonathan T. Looney 		tp = tcp_drop(tp, ECONNABORTED);
13198fa799bdSJonathan T. Looney 		if (tp == NULL)
13208fa799bdSJonathan T. Looney 			goto dropped;
1321a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
13225d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1323c78cbc7bSRobert Watson 	}
1324ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1325a152f8a3SRobert Watson 		SOCK_LOCK(so);
1326a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1327a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1328ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1329a152f8a3SRobert Watson 	}
13308501a69cSRobert Watson 	INP_WUNLOCK(inp);
13318fa799bdSJonathan T. Looney dropped:
133297a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1333a152f8a3SRobert Watson }
1334a152f8a3SRobert Watson 
1335a152f8a3SRobert Watson /*
1336a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1337a152f8a3SRobert Watson  */
1338a152f8a3SRobert Watson static void
1339a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1340a152f8a3SRobert Watson {
1341a152f8a3SRobert Watson 	struct inpcb *inp;
1342a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
13436573d758SMatt Macy 	struct epoch_tracker et;
1344a152f8a3SRobert Watson 	TCPDEBUG0;
1345a152f8a3SRobert Watson 
1346a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1347a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1348a152f8a3SRobert Watson 
134997a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
13508501a69cSRobert Watson 	INP_WLOCK(inp);
1351a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1352a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1353a152f8a3SRobert Watson 
1354a152f8a3SRobert Watson 	/*
1355a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
1356a152f8a3SRobert Watson 	 * a disconnect.
1357a152f8a3SRobert Watson 	 */
1358ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1359ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1360a152f8a3SRobert Watson 		tp = intotcpcb(inp);
1361a152f8a3SRobert Watson 		TCPDEBUG1();
1362a152f8a3SRobert Watson 		tcp_disconnect(tp);
1363a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
13645d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1365a152f8a3SRobert Watson 	}
1366ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1367a152f8a3SRobert Watson 		SOCK_LOCK(so);
1368a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1369a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1370ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1371a152f8a3SRobert Watson 	}
13728501a69cSRobert Watson 	INP_WUNLOCK(inp);
137397a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
13742c37256eSGarrett Wollman }
13752c37256eSGarrett Wollman 
1376*d3b6c96bSRandall Stewart static int
1377*d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags)
1378*d3b6c96bSRandall Stewart {
1379*d3b6c96bSRandall Stewart 	/*
1380*d3b6c96bSRandall Stewart 	 * If the specific TCP stack has a pru_options
1381*d3b6c96bSRandall Stewart 	 * specified then it does not always support
1382*d3b6c96bSRandall Stewart 	 * all the PRU_XX options and we must ask it.
1383*d3b6c96bSRandall Stewart 	 * If the function is not specified then all
1384*d3b6c96bSRandall Stewart 	 * of the PRU_XX options are supported.
1385*d3b6c96bSRandall Stewart 	 */
1386*d3b6c96bSRandall Stewart 	int ret = 0;
1387*d3b6c96bSRandall Stewart 
1388*d3b6c96bSRandall Stewart 	if (tp->t_fb->tfb_pru_options) {
1389*d3b6c96bSRandall Stewart 		ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1390*d3b6c96bSRandall Stewart 	}
1391*d3b6c96bSRandall Stewart 	return (ret);
1392*d3b6c96bSRandall Stewart }
1393*d3b6c96bSRandall Stewart 
13942c37256eSGarrett Wollman /*
13952c37256eSGarrett Wollman  * Receive out-of-band data.
13962c37256eSGarrett Wollman  */
13972c37256eSGarrett Wollman static int
13982c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
13992c37256eSGarrett Wollman {
14002c37256eSGarrett Wollman 	int error = 0;
1401f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1402623dce13SRobert Watson 	struct tcpcb *tp = NULL;
14032c37256eSGarrett Wollman 
1404623dce13SRobert Watson 	TCPDEBUG0;
1405623dce13SRobert Watson 	inp = sotoinpcb(so);
1406623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
14078501a69cSRobert Watson 	INP_WLOCK(inp);
1408ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
140921367f63SSam Leffler 		error = ECONNRESET;
1410623dce13SRobert Watson 		goto out;
1411623dce13SRobert Watson 	}
1412623dce13SRobert Watson 	tp = intotcpcb(inp);
1413*d3b6c96bSRandall Stewart 	error = tcp_pru_options_support(tp, PRUS_OOB);
1414*d3b6c96bSRandall Stewart 	if (error) {
1415*d3b6c96bSRandall Stewart 		goto out;
1416*d3b6c96bSRandall Stewart 	}
1417623dce13SRobert Watson 	TCPDEBUG1();
14182c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1419c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
14204cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
14214cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
14222c37256eSGarrett Wollman 		error = EINVAL;
14232c37256eSGarrett Wollman 		goto out;
14242c37256eSGarrett Wollman 	}
14252c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
14262c37256eSGarrett Wollman 		error = EWOULDBLOCK;
14272c37256eSGarrett Wollman 		goto out;
14282c37256eSGarrett Wollman 	}
14292c37256eSGarrett Wollman 	m->m_len = 1;
14302c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
14312c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
14322c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1433623dce13SRobert Watson 
1434623dce13SRobert Watson out:
1435623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
14365d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
14378501a69cSRobert Watson 	INP_WUNLOCK(inp);
1438623dce13SRobert Watson 	return (error);
14392c37256eSGarrett Wollman }
14402c37256eSGarrett Wollman 
1441b287c6c7SBjoern A. Zeeb #ifdef INET
14422c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1443756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1444756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1445756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1446756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1447756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1448756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1449756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1450756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1451756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
145254d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1453756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1454756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1455756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
14562cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1457756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
145854d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1459a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1460a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
14612c37256eSGarrett Wollman };
1462b287c6c7SBjoern A. Zeeb #endif /* INET */
1463df8bae1dSRodney W. Grimes 
1464fb59c426SYoshinobu Inoue #ifdef INET6
1465fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1466756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1467756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1468756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1469756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1470756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1471756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1472756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1473756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1474756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1475756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1476756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1477756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1478756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
14792cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1480756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1481756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1482a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1483a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1484fb59c426SYoshinobu Inoue };
1485fb59c426SYoshinobu Inoue #endif /* INET6 */
1486fb59c426SYoshinobu Inoue 
1487b287c6c7SBjoern A. Zeeb #ifdef INET
1488a0292f23SGarrett Wollman /*
1489a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1490a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
14915200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
14925200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
14935200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
14945200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1495a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1496a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1497a0292f23SGarrett Wollman  */
14980312fbe9SPoul-Henning Kamp static int
1499ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1500a0292f23SGarrett Wollman {
1501a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1502a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
15035200e00eSIan Dowse 	struct in_addr laddr;
15045200e00eSIan Dowse 	u_short lport;
1505c3229e05SDavid Greenman 	int error;
1506a0292f23SGarrett Wollman 
1507c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
15088501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1509fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1510623dce13SRobert Watson 
1511a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
15124616026fSErmal Luçi 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
15134616026fSErmal Luçi 		if (error)
1514fa046d87SRobert Watson 			goto out;
1515a0292f23SGarrett Wollman 	}
1516a0292f23SGarrett Wollman 
1517a0292f23SGarrett Wollman 	/*
1518a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1519a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1520a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1521a0292f23SGarrett Wollman 	 */
15225200e00eSIan Dowse 	laddr = inp->inp_laddr;
15235200e00eSIan Dowse 	lport = inp->inp_lport;
15245200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1525b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
15265200e00eSIan Dowse 	if (error && oinp == NULL)
1527fa046d87SRobert Watson 		goto out;
1528fa046d87SRobert Watson 	if (oinp) {
1529fa046d87SRobert Watson 		error = EADDRINUSE;
1530fa046d87SRobert Watson 		goto out;
1531fa046d87SRobert Watson 	}
15325200e00eSIan Dowse 	inp->inp_laddr = laddr;
153315bd2b43SDavid Greenman 	in_pcbrehash(inp);
1534fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1535a0292f23SGarrett Wollman 
1536087b55eaSAndre Oppermann 	/*
1537087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1538087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1539087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1540087b55eaSAndre Oppermann 	 */
1541a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
15429b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1543a0292f23SGarrett Wollman 		tp->request_r_scale++;
1544a0292f23SGarrett Wollman 
1545a0292f23SGarrett Wollman 	soisconnecting(so);
154678b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
154757f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
15488e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
15498e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
15508e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1551a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1552a45d2726SAndras Olah 
1553a0292f23SGarrett Wollman 	return 0;
1554fa046d87SRobert Watson 
1555fa046d87SRobert Watson out:
1556fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1557fa046d87SRobert Watson 	return (error);
1558a0292f23SGarrett Wollman }
1559b287c6c7SBjoern A. Zeeb #endif /* INET */
1560a0292f23SGarrett Wollman 
1561fb59c426SYoshinobu Inoue #ifdef INET6
1562fb59c426SYoshinobu Inoue static int
1563ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1564fb59c426SYoshinobu Inoue {
1565a7e201bbSAndrey V. Elsukov 	struct inpcb *inp = tp->t_inpcb;
1566fb59c426SYoshinobu Inoue 	int error;
1567fb59c426SYoshinobu Inoue 
15688501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1569fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1570623dce13SRobert Watson 
1571fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
15724616026fSErmal Luçi 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
15734616026fSErmal Luçi 		if (error)
1574fa046d87SRobert Watson 			goto out;
1575fb59c426SYoshinobu Inoue 	}
1576a7e201bbSAndrey V. Elsukov 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1577a7e201bbSAndrey V. Elsukov 	if (error != 0)
1578b598155aSRobert Watson 		goto out;
1579fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1580fb59c426SYoshinobu Inoue 
1581fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1582fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1583970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1584fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1585fb59c426SYoshinobu Inoue 
1586a7e201bbSAndrey V. Elsukov 	soisconnecting(inp->inp_socket);
158778b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
158857f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
15898e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
15908e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
15918e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1592fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1593fb59c426SYoshinobu Inoue 
1594fb59c426SYoshinobu Inoue 	return 0;
1595fa046d87SRobert Watson 
1596fa046d87SRobert Watson out:
1597fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1598fa046d87SRobert Watson 	return error;
1599fb59c426SYoshinobu Inoue }
1600fb59c426SYoshinobu Inoue #endif /* INET6 */
1601fb59c426SYoshinobu Inoue 
1602cfe8b629SGarrett Wollman /*
1603b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1604b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1605b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1606b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1607b8af5dfaSRobert Watson  * from Linux.
1608b8af5dfaSRobert Watson  */
1609b8af5dfaSRobert Watson static void
1610ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1611b8af5dfaSRobert Watson {
1612b8af5dfaSRobert Watson 
16138501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1614b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1615b8af5dfaSRobert Watson 
1616b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1617b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1618b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
16193529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1620b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1621b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1622b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1623b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1624b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1625b8af5dfaSRobert Watson 	}
16263cf38784SMichael Tuexen 	if (tp->t_flags2 & TF2_ECN_PERMIT)
16275a17b6adSMichael Tuexen 		ti->tcpi_options |= TCPI_OPT_ECN;
16281baaf834SBruce M Simpson 
162943d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
16303ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
16311baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
16321baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
16331baaf834SBruce M Simpson 
1634b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1635b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1636b8af5dfaSRobert Watson 
1637b8af5dfaSRobert Watson 	/*
1638b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1639b8af5dfaSRobert Watson 	 */
1640c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1641535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1642b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
16431c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1644535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
164543d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
164643d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1647f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1648f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1649f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1650a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1651a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1652a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1653a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1654a6456410SNavdeep Parhar 	}
1655a6456410SNavdeep Parhar #endif
1656b8af5dfaSRobert Watson }
1657b8af5dfaSRobert Watson 
1658b8af5dfaSRobert Watson /*
16591e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
16601e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
16611e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
16621e8f5ffaSRobert Watson  * option.
1663cfe8b629SGarrett Wollman  */
1664bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
16658501a69cSRobert Watson 	INP_WLOCK(inp);							\
1666ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
16678501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1668bac5bedfSConrad Meyer 		cleanup;						\
16691e8f5ffaSRobert Watson 		return (ECONNRESET);					\
16701e8f5ffaSRobert Watson 	}								\
16711e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
16721e8f5ffaSRobert Watson } while(0)
1673bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
16741e8f5ffaSRobert Watson 
1675df8bae1dSRodney W. Grimes int
1676ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1677df8bae1dSRodney W. Grimes {
167855bceb1eSRandall Stewart 	int	error;
1679df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1680cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
168155bceb1eSRandall Stewart 	struct tcp_function_block *blk;
168255bceb1eSRandall Stewart 	struct tcp_function_set fsn;
1683df8bae1dSRodney W. Grimes 
1684cfe8b629SGarrett Wollman 	error = 0;
1685df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1686623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1687cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1688fb59c426SYoshinobu Inoue #ifdef INET6
16895cd54324SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6PROTO) {
1690fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
16915dff1c38SMichael Tuexen 			/*
16925dff1c38SMichael Tuexen 			 * In case of the IPV6_USE_MIN_MTU socket option,
16935dff1c38SMichael Tuexen 			 * the INC_IPV6MINMTU flag to announce a corresponding
16945dff1c38SMichael Tuexen 			 * MSS during the initial handshake.
16955dff1c38SMichael Tuexen 			 * If the TCP connection is not in the front states,
16965dff1c38SMichael Tuexen 			 * just reduce the MSS being used.
16975dff1c38SMichael Tuexen 			 * This avoids the sending of TCP segments which will
16985dff1c38SMichael Tuexen 			 * be fragmented at the IPv6 layer.
16995dff1c38SMichael Tuexen 			 */
17005dff1c38SMichael Tuexen 			if ((error == 0) &&
17015dff1c38SMichael Tuexen 			    (sopt->sopt_dir == SOPT_SET) &&
17025dff1c38SMichael Tuexen 			    (sopt->sopt_level == IPPROTO_IPV6) &&
17035dff1c38SMichael Tuexen 			    (sopt->sopt_name == IPV6_USE_MIN_MTU)) {
17045dff1c38SMichael Tuexen 				INP_WLOCK(inp);
17055dff1c38SMichael Tuexen 				if ((inp->inp_flags &
17065dff1c38SMichael Tuexen 				    (INP_TIMEWAIT | INP_DROPPED))) {
17075dff1c38SMichael Tuexen 					INP_WUNLOCK(inp);
17085dff1c38SMichael Tuexen 					return (ECONNRESET);
17095dff1c38SMichael Tuexen 				}
17105dff1c38SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_IPV6MINMTU;
17115dff1c38SMichael Tuexen 				tp = intotcpcb(inp);
17125dff1c38SMichael Tuexen 				if ((tp->t_state >= TCPS_SYN_SENT) &&
17135dff1c38SMichael Tuexen 				    (inp->inp_inc.inc_flags & INC_ISIPV6)) {
17145dff1c38SMichael Tuexen 					struct ip6_pktopts *opt;
17155dff1c38SMichael Tuexen 
17165dff1c38SMichael Tuexen 					opt = inp->in6p_outputopts;
17175dff1c38SMichael Tuexen 					if ((opt != NULL) &&
17185dff1c38SMichael Tuexen 					    (opt->ip6po_minmtu ==
17195dff1c38SMichael Tuexen 					    IP6PO_MINMTU_ALL)) {
17205dff1c38SMichael Tuexen 						if (tp->t_maxseg > TCP6_MSS) {
17215dff1c38SMichael Tuexen 							tp->t_maxseg = TCP6_MSS;
17225dff1c38SMichael Tuexen 						}
17235dff1c38SMichael Tuexen 					}
17245dff1c38SMichael Tuexen 				}
17255dff1c38SMichael Tuexen 				INP_WUNLOCK(inp);
17265dff1c38SMichael Tuexen 			}
1727b287c6c7SBjoern A. Zeeb 		}
1728fb59c426SYoshinobu Inoue #endif /* INET6 */
1729b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1730b287c6c7SBjoern A. Zeeb 		else
1731b287c6c7SBjoern A. Zeeb #endif
1732b287c6c7SBjoern A. Zeeb #ifdef INET
1733b287c6c7SBjoern A. Zeeb 		{
1734cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
17351e8f5ffaSRobert Watson 		}
17361e8f5ffaSRobert Watson #endif
1737df8bae1dSRodney W. Grimes 		return (error);
1738df8bae1dSRodney W. Grimes 	}
173968cea2b1SJohn Baldwin 	INP_WLOCK(inp);
1740ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
17418501a69cSRobert Watson 		INP_WUNLOCK(inp);
17421e8f5ffaSRobert Watson 		return (ECONNRESET);
1743623dce13SRobert Watson 	}
174455bceb1eSRandall Stewart 	tp = intotcpcb(inp);
174555bceb1eSRandall Stewart 	/*
174655bceb1eSRandall Stewart 	 * Protect the TCP option TCP_FUNCTION_BLK so
174755bceb1eSRandall Stewart 	 * that a sub-function can *never* overwrite this.
174855bceb1eSRandall Stewart 	 */
174955bceb1eSRandall Stewart 	if ((sopt->sopt_dir == SOPT_SET) &&
175055bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
175155bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
175255bceb1eSRandall Stewart 		error = sooptcopyin(sopt, &fsn, sizeof fsn,
175355bceb1eSRandall Stewart 		    sizeof fsn);
175455bceb1eSRandall Stewart 		if (error)
175555bceb1eSRandall Stewart 			return (error);
175655bceb1eSRandall Stewart 		INP_WLOCK_RECHECK(inp);
175755bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
175855bceb1eSRandall Stewart 		if (blk == NULL) {
175955bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
176055bceb1eSRandall Stewart 			return (ENOENT);
176155bceb1eSRandall Stewart 		}
1762587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1763587d67c0SRandall Stewart 			/* You already have this */
1764587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1765587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1766587d67c0SRandall Stewart 			return (0);
1767587d67c0SRandall Stewart 		}
1768587d67c0SRandall Stewart 		if (tp->t_state != TCPS_CLOSED) {
1769587d67c0SRandall Stewart 			/*
1770587d67c0SRandall Stewart 			 * The user has advanced the state
1771587d67c0SRandall Stewart 			 * past the initial point, we may not
1772587d67c0SRandall Stewart 			 * be able to switch.
1773587d67c0SRandall Stewart 			 */
1774587d67c0SRandall Stewart 			if (blk->tfb_tcp_handoff_ok != NULL) {
1775587d67c0SRandall Stewart 				/*
1776587d67c0SRandall Stewart 				 * Does the stack provide a
1777587d67c0SRandall Stewart 				 * query mechanism, if so it may
1778587d67c0SRandall Stewart 				 * still be possible?
1779587d67c0SRandall Stewart 				 */
1780587d67c0SRandall Stewart 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1781c6c0be27SMichael Tuexen 			} else
1782c6c0be27SMichael Tuexen 				error = EINVAL;
1783587d67c0SRandall Stewart 			if (error) {
1784587d67c0SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1785587d67c0SRandall Stewart 				INP_WUNLOCK(inp);
1786587d67c0SRandall Stewart 				return(error);
1787587d67c0SRandall Stewart 			}
1788587d67c0SRandall Stewart 		}
178955bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
179055bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
179155bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
179255bceb1eSRandall Stewart 			return (ENOENT);
179355bceb1eSRandall Stewart 		}
179455bceb1eSRandall Stewart 		/*
179555bceb1eSRandall Stewart 		 * Release the old refcnt, the
1796587d67c0SRandall Stewart 		 * lookup acquired a ref on the
1797587d67c0SRandall Stewart 		 * new one already.
179855bceb1eSRandall Stewart 		 */
1799587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1800587d67c0SRandall Stewart 			/*
1801587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1802587d67c0SRandall Stewart 			 * the tcb is not going away.
1803587d67c0SRandall Stewart 			 */
1804587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1805587d67c0SRandall Stewart 		}
18063ee9c3c4SRandall Stewart #ifdef TCPHPTS
18073ee9c3c4SRandall Stewart 		/* Assure that we are not on any hpts */
18083ee9c3c4SRandall Stewart 		tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
18093ee9c3c4SRandall Stewart #endif
18103ee9c3c4SRandall Stewart 		if (blk->tfb_tcp_fb_init) {
18113ee9c3c4SRandall Stewart 			error = (*blk->tfb_tcp_fb_init)(tp);
18123ee9c3c4SRandall Stewart 			if (error) {
18133ee9c3c4SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
18143ee9c3c4SRandall Stewart 				if (tp->t_fb->tfb_tcp_fb_init) {
18153ee9c3c4SRandall Stewart 					if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0)  {
18163ee9c3c4SRandall Stewart 						/* Fall back failed, drop the connection */
18173ee9c3c4SRandall Stewart 						INP_WUNLOCK(inp);
18183ee9c3c4SRandall Stewart 						soabort(so);
18193ee9c3c4SRandall Stewart 						return(error);
18203ee9c3c4SRandall Stewart 					}
18213ee9c3c4SRandall Stewart 				}
18223ee9c3c4SRandall Stewart 				goto err_out;
18233ee9c3c4SRandall Stewart 			}
18243ee9c3c4SRandall Stewart 		}
182555bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
182655bceb1eSRandall Stewart 		tp->t_fb = blk;
182755bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
182855bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
182955bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
183055bceb1eSRandall Stewart 			     sopt->sopt_name);
183155bceb1eSRandall Stewart 		}
183255bceb1eSRandall Stewart #endif
18333ee9c3c4SRandall Stewart err_out:
183455bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
183555bceb1eSRandall Stewart 		return (error);
183655bceb1eSRandall Stewart 	} else if ((sopt->sopt_dir == SOPT_GET) &&
183755bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1838c73b6f4dSEd Maste 		strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1839c73b6f4dSEd Maste 		    TCP_FUNCTION_NAME_LEN_MAX);
1840c73b6f4dSEd Maste 		fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
184155bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
184255bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
184355bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
184455bceb1eSRandall Stewart 		return (error);
184555bceb1eSRandall Stewart 	}
184655bceb1eSRandall Stewart 	/* Pass in the INP locked, called must unlock it */
184755bceb1eSRandall Stewart 	return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
184855bceb1eSRandall Stewart }
184955bceb1eSRandall Stewart 
18502529f56eSJonathan T. Looney /*
18512529f56eSJonathan T. Looney  * If this assert becomes untrue, we need to change the size of the buf
18522529f56eSJonathan T. Looney  * variable in tcp_default_ctloutput().
18532529f56eSJonathan T. Looney  */
18542529f56eSJonathan T. Looney #ifdef CTASSERT
18552529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
18562529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
18572529f56eSJonathan T. Looney #endif
18582529f56eSJonathan T. Looney 
1859ec1db6e1SJohn Baldwin #ifdef KERN_TLS
1860ec1db6e1SJohn Baldwin static int
1861ec1db6e1SJohn Baldwin copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1862ec1db6e1SJohn Baldwin {
1863ec1db6e1SJohn Baldwin 	struct tls_enable_v0 tls_v0;
1864ec1db6e1SJohn Baldwin 	int error;
1865ec1db6e1SJohn Baldwin 
1866ec1db6e1SJohn Baldwin 	if (sopt->sopt_valsize == sizeof(tls_v0)) {
1867ec1db6e1SJohn Baldwin 		error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1868ec1db6e1SJohn Baldwin 		    sizeof(tls_v0));
1869ec1db6e1SJohn Baldwin 		if (error)
1870ec1db6e1SJohn Baldwin 			return (error);
1871ec1db6e1SJohn Baldwin 		memset(tls, 0, sizeof(*tls));
1872ec1db6e1SJohn Baldwin 		tls->cipher_key = tls_v0.cipher_key;
1873ec1db6e1SJohn Baldwin 		tls->iv = tls_v0.iv;
1874ec1db6e1SJohn Baldwin 		tls->auth_key = tls_v0.auth_key;
1875ec1db6e1SJohn Baldwin 		tls->cipher_algorithm = tls_v0.cipher_algorithm;
1876ec1db6e1SJohn Baldwin 		tls->cipher_key_len = tls_v0.cipher_key_len;
1877ec1db6e1SJohn Baldwin 		tls->iv_len = tls_v0.iv_len;
1878ec1db6e1SJohn Baldwin 		tls->auth_algorithm = tls_v0.auth_algorithm;
1879ec1db6e1SJohn Baldwin 		tls->auth_key_len = tls_v0.auth_key_len;
1880ec1db6e1SJohn Baldwin 		tls->flags = tls_v0.flags;
1881ec1db6e1SJohn Baldwin 		tls->tls_vmajor = tls_v0.tls_vmajor;
1882ec1db6e1SJohn Baldwin 		tls->tls_vminor = tls_v0.tls_vminor;
1883ec1db6e1SJohn Baldwin 		return (0);
1884ec1db6e1SJohn Baldwin 	}
1885ec1db6e1SJohn Baldwin 
1886ec1db6e1SJohn Baldwin 	return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1887ec1db6e1SJohn Baldwin }
1888ec1db6e1SJohn Baldwin #endif
1889ec1db6e1SJohn Baldwin 
189055bceb1eSRandall Stewart int
189155bceb1eSRandall Stewart tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
189255bceb1eSRandall Stewart {
189355bceb1eSRandall Stewart 	int	error, opt, optval;
189455bceb1eSRandall Stewart 	u_int	ui;
189555bceb1eSRandall Stewart 	struct	tcp_info ti;
1896b2e60773SJohn Baldwin #ifdef KERN_TLS
1897b2e60773SJohn Baldwin 	struct tls_enable tls;
1898b2e60773SJohn Baldwin #endif
189955bceb1eSRandall Stewart 	struct cc_algo *algo;
19002529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
1901adc56f5aSEdward Tomasz Napierala #ifdef STATS
1902adc56f5aSEdward Tomasz Napierala 	struct statsblob *sbp;
1903adc56f5aSEdward Tomasz Napierala #endif
1904af6fef3aSGleb Smirnoff 	size_t	len;
1905df8bae1dSRodney W. Grimes 
1906d519cedbSGleb Smirnoff 	/*
1907d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
1908d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
1909d519cedbSGleb Smirnoff 	 */
1910d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
1911d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
1912d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1913c8b53cedSMichael Tuexen 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
1914c8b53cedSMichael Tuexen 			return (EINVAL);
1915af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1916af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1917d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
1918d519cedbSGleb Smirnoff 		if (error) {
1919af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
1920d519cedbSGleb Smirnoff 			return (error);
1921d519cedbSGleb Smirnoff 		}
1922bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1923d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
1924af6fef3aSGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1925d519cedbSGleb Smirnoff 		else
1926d519cedbSGleb Smirnoff 			error = ENOENT;
1927d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1928d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
1929af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1930af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
1931d519cedbSGleb Smirnoff 		return (error);
1932d519cedbSGleb Smirnoff 	}
1933d519cedbSGleb Smirnoff 
1934cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1935cfe8b629SGarrett Wollman 	case SOPT_SET:
1936cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1937fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
193888f6b043SBruce M Simpson 		case TCP_MD5SIG:
1939fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
19408501a69cSRobert Watson 				INP_WUNLOCK(inp);
1941fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
1942fcf59617SAndrey V. Elsukov 			}
1943fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
19441cfd4b53SBruce M Simpson 			if (error)
19451e8f5ffaSRobert Watson 				return (error);
194609fe6320SNavdeep Parhar 			goto unlock_and_done;
1947fcf59617SAndrey V. Elsukov #endif /* IPSEC */
194809fe6320SNavdeep Parhar 
1949df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1950cfe8b629SGarrett Wollman 		case TCP_NOOPT:
19518501a69cSRobert Watson 			INP_WUNLOCK(inp);
1952cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1953cfe8b629SGarrett Wollman 			    sizeof optval);
1954cfe8b629SGarrett Wollman 			if (error)
19551e8f5ffaSRobert Watson 				return (error);
1956cfe8b629SGarrett Wollman 
19578501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1958cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1959cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1960cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1961cfe8b629SGarrett Wollman 				break;
1962cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1963cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1964cfe8b629SGarrett Wollman 				break;
1965cfe8b629SGarrett Wollman 			default:
1966cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1967cfe8b629SGarrett Wollman 				break;
1968cfe8b629SGarrett Wollman 			}
1969cfe8b629SGarrett Wollman 
1970cfe8b629SGarrett Wollman 			if (optval)
1971cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1972df8bae1dSRodney W. Grimes 			else
1973cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
197409fe6320SNavdeep Parhar unlock_and_done:
197509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
197609fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
197709fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
197809fe6320SNavdeep Parhar 				    sopt->sopt_name);
197909fe6320SNavdeep Parhar 			}
198009fe6320SNavdeep Parhar #endif
19818501a69cSRobert Watson 			INP_WUNLOCK(inp);
1982df8bae1dSRodney W. Grimes 			break;
1983df8bae1dSRodney W. Grimes 
1984007581c0SJonathan Lemon 		case TCP_NOPUSH:
19858501a69cSRobert Watson 			INP_WUNLOCK(inp);
1986007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1987007581c0SJonathan Lemon 			    sizeof optval);
1988007581c0SJonathan Lemon 			if (error)
19891e8f5ffaSRobert Watson 				return (error);
1990007581c0SJonathan Lemon 
19918501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1992007581c0SJonathan Lemon 			if (optval)
1993007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1994d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
1995007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1996109eb549SGleb Smirnoff 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
1997109eb549SGleb Smirnoff 					struct epoch_tracker et;
1998109eb549SGleb Smirnoff 
1999109eb549SGleb Smirnoff 					NET_EPOCH_ENTER(et);
200055bceb1eSRandall Stewart 					error = tp->t_fb->tfb_tcp_output(tp);
2001109eb549SGleb Smirnoff 					NET_EPOCH_EXIT(et);
2002109eb549SGleb Smirnoff 				}
2003007581c0SJonathan Lemon 			}
200409fe6320SNavdeep Parhar 			goto unlock_and_done;
2005007581c0SJonathan Lemon 
2006df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
20078501a69cSRobert Watson 			INP_WUNLOCK(inp);
2008cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2009cfe8b629SGarrett Wollman 			    sizeof optval);
2010cfe8b629SGarrett Wollman 			if (error)
20111e8f5ffaSRobert Watson 				return (error);
2012df8bae1dSRodney W. Grimes 
20138501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
201453369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
2015603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
2016cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
2017a0292f23SGarrett Wollman 			else
2018a0292f23SGarrett Wollman 				error = EINVAL;
201909fe6320SNavdeep Parhar 			goto unlock_and_done;
2020a0292f23SGarrett Wollman 
2021b8af5dfaSRobert Watson 		case TCP_INFO:
20228501a69cSRobert Watson 			INP_WUNLOCK(inp);
2023b8af5dfaSRobert Watson 			error = EINVAL;
2024b8af5dfaSRobert Watson 			break;
2025b8af5dfaSRobert Watson 
2026adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2027adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2028adc56f5aSEdward Tomasz Napierala #ifdef STATS
2029adc56f5aSEdward Tomasz Napierala 			error = sooptcopyin(sopt, &optval, sizeof optval,
2030adc56f5aSEdward Tomasz Napierala 			    sizeof optval);
2031adc56f5aSEdward Tomasz Napierala 			if (error)
2032adc56f5aSEdward Tomasz Napierala 				return (error);
2033adc56f5aSEdward Tomasz Napierala 
2034adc56f5aSEdward Tomasz Napierala 			if (optval > 0)
2035adc56f5aSEdward Tomasz Napierala 				sbp = stats_blob_alloc(
2036adc56f5aSEdward Tomasz Napierala 				    V_tcp_perconn_stats_dflt_tpl, 0);
2037adc56f5aSEdward Tomasz Napierala 			else
2038adc56f5aSEdward Tomasz Napierala 				sbp = NULL;
2039adc56f5aSEdward Tomasz Napierala 
2040adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2041adc56f5aSEdward Tomasz Napierala 			if ((tp->t_stats != NULL && sbp == NULL) ||
2042adc56f5aSEdward Tomasz Napierala 			    (tp->t_stats == NULL && sbp != NULL)) {
2043adc56f5aSEdward Tomasz Napierala 				struct statsblob *t = tp->t_stats;
2044adc56f5aSEdward Tomasz Napierala 				tp->t_stats = sbp;
2045adc56f5aSEdward Tomasz Napierala 				sbp = t;
2046adc56f5aSEdward Tomasz Napierala 			}
2047adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2048adc56f5aSEdward Tomasz Napierala 
2049adc56f5aSEdward Tomasz Napierala 			stats_blob_destroy(sbp);
2050adc56f5aSEdward Tomasz Napierala #else
2051adc56f5aSEdward Tomasz Napierala 			return (EOPNOTSUPP);
2052adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2053adc56f5aSEdward Tomasz Napierala 			break;
2054adc56f5aSEdward Tomasz Napierala 
2055dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2056dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2057af6fef3aSGleb Smirnoff 			error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
2058af6fef3aSGleb Smirnoff 			if (error)
2059dbc42409SLawrence Stewart 				break;
2060af6fef3aSGleb Smirnoff 			buf[sopt->sopt_valsize] = '\0';
2061af6fef3aSGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
206273e263b1SGleb Smirnoff 			CC_LIST_RLOCK();
206373e263b1SGleb Smirnoff 			STAILQ_FOREACH(algo, &cc_list, entries)
206473e263b1SGleb Smirnoff 				if (strncmp(buf, algo->name,
206573e263b1SGleb Smirnoff 				    TCP_CA_NAME_MAX) == 0)
206673e263b1SGleb Smirnoff 					break;
206773e263b1SGleb Smirnoff 			CC_LIST_RUNLOCK();
206873e263b1SGleb Smirnoff 			if (algo == NULL) {
2069af6fef3aSGleb Smirnoff 				INP_WUNLOCK(inp);
207073e263b1SGleb Smirnoff 				error = EINVAL;
207173e263b1SGleb Smirnoff 				break;
207273e263b1SGleb Smirnoff 			}
2073dbc42409SLawrence Stewart 			/*
207473e263b1SGleb Smirnoff 			 * We hold a write lock over the tcb so it's safe to
207573e263b1SGleb Smirnoff 			 * do these things without ordering concerns.
2076dbc42409SLawrence Stewart 			 */
2077dbc42409SLawrence Stewart 			if (CC_ALGO(tp)->cb_destroy != NULL)
2078dbc42409SLawrence Stewart 				CC_ALGO(tp)->cb_destroy(tp->ccv);
207922699887SMatt Macy 			CC_DATA(tp) = NULL;
2080dbc42409SLawrence Stewart 			CC_ALGO(tp) = algo;
2081dbc42409SLawrence Stewart 			/*
208273e263b1SGleb Smirnoff 			 * If something goes pear shaped initialising the new
208373e263b1SGleb Smirnoff 			 * algo, fall back to newreno (which does not
208473e263b1SGleb Smirnoff 			 * require initialisation).
2085dbc42409SLawrence Stewart 			 */
208673e263b1SGleb Smirnoff 			if (algo->cb_init != NULL &&
208773e263b1SGleb Smirnoff 			    algo->cb_init(tp->ccv) != 0) {
2088dbc42409SLawrence Stewart 				CC_ALGO(tp) = &newreno_cc_algo;
2089dbc42409SLawrence Stewart 				/*
209073e263b1SGleb Smirnoff 				 * The only reason init should fail is
2091dbc42409SLawrence Stewart 				 * because of malloc.
2092dbc42409SLawrence Stewart 				 */
2093dbc42409SLawrence Stewart 				error = ENOMEM;
2094dbc42409SLawrence Stewart 			}
209573e263b1SGleb Smirnoff 			INP_WUNLOCK(inp);
209673e263b1SGleb Smirnoff 			break;
2097dbc42409SLawrence Stewart 
2098b2e60773SJohn Baldwin #ifdef KERN_TLS
2099b2e60773SJohn Baldwin 		case TCP_TXTLS_ENABLE:
2100b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2101ec1db6e1SJohn Baldwin 			error = copyin_tls_enable(sopt, &tls);
2102b2e60773SJohn Baldwin 			if (error)
2103b2e60773SJohn Baldwin 				break;
2104b2e60773SJohn Baldwin 			error = ktls_enable_tx(so, &tls);
2105b2e60773SJohn Baldwin 			break;
2106b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2107b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2108b2e60773SJohn Baldwin 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2109b2e60773SJohn Baldwin 			if (error)
2110b2e60773SJohn Baldwin 				return (error);
2111b2e60773SJohn Baldwin 
2112b2e60773SJohn Baldwin 			INP_WLOCK_RECHECK(inp);
2113b2e60773SJohn Baldwin 			error = ktls_set_tx_mode(so, ui);
2114b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2115b2e60773SJohn Baldwin 			break;
2116f1f93475SJohn Baldwin 		case TCP_RXTLS_ENABLE:
2117f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2118f1f93475SJohn Baldwin 			error = sooptcopyin(sopt, &tls, sizeof(tls),
2119f1f93475SJohn Baldwin 			    sizeof(tls));
2120f1f93475SJohn Baldwin 			if (error)
2121f1f93475SJohn Baldwin 				break;
2122f1f93475SJohn Baldwin 			error = ktls_enable_rx(so, &tls);
2123f1f93475SJohn Baldwin 			break;
2124b2e60773SJohn Baldwin #endif
2125b2e60773SJohn Baldwin 
21269077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
21279077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
21289077f387SGleb Smirnoff 		case TCP_KEEPINIT:
21299077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
21309077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
21319077f387SGleb Smirnoff 			if (error)
21329077f387SGleb Smirnoff 				return (error);
21339077f387SGleb Smirnoff 
21349077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
21359077f387SGleb Smirnoff 				error = EINVAL;
21369077f387SGleb Smirnoff 				break;
21379077f387SGleb Smirnoff 			}
21389077f387SGleb Smirnoff 			ui *= hz;
21399077f387SGleb Smirnoff 
21409077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
21419077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
21429077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
21439077f387SGleb Smirnoff 				tp->t_keepidle = ui;
21449077f387SGleb Smirnoff 				/*
21459077f387SGleb Smirnoff 				 * XXX: better check current remaining
21469077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
21479077f387SGleb Smirnoff 				 */
21489077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
21499077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
21509077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
21519077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
21529077f387SGleb Smirnoff 				break;
21539077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
21549077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
21559077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
21569077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
21579077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
21589077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
21599077f387SGleb Smirnoff 				break;
21609077f387SGleb Smirnoff 			case TCP_KEEPINIT:
21619077f387SGleb Smirnoff 				tp->t_keepinit = ui;
21629077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
21639077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
21649077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
21659077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
21669077f387SGleb Smirnoff 				break;
21679077f387SGleb Smirnoff 			}
216809fe6320SNavdeep Parhar 			goto unlock_and_done;
21699077f387SGleb Smirnoff 
217085c05144SGleb Smirnoff 		case TCP_KEEPCNT:
217185c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
217285c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
217385c05144SGleb Smirnoff 			if (error)
217485c05144SGleb Smirnoff 				return (error);
217585c05144SGleb Smirnoff 
217685c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
217785c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
217885c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
217985c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
218085c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
218185c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
218285c05144SGleb Smirnoff 			goto unlock_and_done;
218385c05144SGleb Smirnoff 
218486a996e6SHiren Panchasara #ifdef TCPPCAP
218586a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
218686a996e6SHiren Panchasara 		case TCP_PCAP_IN:
218786a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
218886a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
218986a996e6SHiren Panchasara 			    sizeof optval);
219086a996e6SHiren Panchasara 			if (error)
219186a996e6SHiren Panchasara 				return (error);
219286a996e6SHiren Panchasara 
219386a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
219486a996e6SHiren Panchasara 			if (optval >= 0)
219586a996e6SHiren Panchasara 				tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
219686a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
219786a996e6SHiren Panchasara 					optval);
219886a996e6SHiren Panchasara 			else
219986a996e6SHiren Panchasara 				error = EINVAL;
220086a996e6SHiren Panchasara 			goto unlock_and_done;
220186a996e6SHiren Panchasara #endif
220286a996e6SHiren Panchasara 
2203c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
2204c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
2205c560df6fSPatrick Kelsey 
2206281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2207c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
2208c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
2209281a0fd4SPatrick Kelsey 				return (EPERM);
2210281a0fd4SPatrick Kelsey 
2211c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
2212c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
2213281a0fd4SPatrick Kelsey 			if (error)
2214281a0fd4SPatrick Kelsey 				return (error);
2215281a0fd4SPatrick Kelsey 
2216281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
2217c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
2218c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
2219c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
2220c560df6fSPatrick Kelsey 						error = EPERM;
2221c560df6fSPatrick Kelsey 						goto unlock_and_done;
2222c560df6fSPatrick Kelsey 					}
2223c560df6fSPatrick Kelsey 
2224281a0fd4SPatrick Kelsey 					tp->t_flags |= TF_FASTOPEN;
2225c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
2226281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
2227281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
2228c560df6fSPatrick Kelsey 				} else {
2229c560df6fSPatrick Kelsey 					/*
2230c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
2231c560df6fSPatrick Kelsey 					 * stash it in the client cookie
2232c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
2233c560df6fSPatrick Kelsey 					 * connect.
2234c560df6fSPatrick Kelsey 					 */
2235c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
2236c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
2237c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
2238c560df6fSPatrick Kelsey 						       tfo_optval.psk,
2239c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
2240c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
2241c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
2242c560df6fSPatrick Kelsey 					}
2243c560df6fSPatrick Kelsey 					tp->t_flags |= TF_FASTOPEN;
2244c560df6fSPatrick Kelsey 				}
2245281a0fd4SPatrick Kelsey 			} else
2246281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
2247281a0fd4SPatrick Kelsey 			goto unlock_and_done;
2248c560df6fSPatrick Kelsey 		}
2249281a0fd4SPatrick Kelsey 
2250e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
22512529f56eSJonathan T. Looney 		case TCP_LOG:
22522529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
22532529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
22542529f56eSJonathan T. Looney 			    sizeof optval);
22552529f56eSJonathan T. Looney 			if (error)
22562529f56eSJonathan T. Looney 				return (error);
22572529f56eSJonathan T. Looney 
22582529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
22592529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
22602529f56eSJonathan T. Looney 			goto unlock_and_done;
22612529f56eSJonathan T. Looney 
22622529f56eSJonathan T. Looney 		case TCP_LOGBUF:
22632529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
22642529f56eSJonathan T. Looney 			error = EINVAL;
22652529f56eSJonathan T. Looney 			break;
22662529f56eSJonathan T. Looney 
22672529f56eSJonathan T. Looney 		case TCP_LOGID:
22682529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
22692529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
22702529f56eSJonathan T. Looney 			if (error)
22712529f56eSJonathan T. Looney 				break;
22722529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
22732529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
22742529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
22752529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
22762529f56eSJonathan T. Looney 			break;
22772529f56eSJonathan T. Looney 
22782529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
22792529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
22802529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
22812529f56eSJonathan T. Looney 			error =
22822529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
22832529f56eSJonathan T. Looney 			if (error)
22842529f56eSJonathan T. Looney 				break;
22852529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
22862529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
22872529f56eSJonathan T. Looney 			if (sopt->sopt_name == TCP_LOGDUMP) {
22882529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
22892529f56eSJonathan T. Looney 				    M_WAITOK, true);
22902529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
22912529f56eSJonathan T. Looney 			} else {
22922529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
22932529f56eSJonathan T. Looney 				/*
22942529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
22952529f56eSJonathan T. Looney 				 * INP lock.
22962529f56eSJonathan T. Looney 				 */
22972529f56eSJonathan T. Looney 			}
22982529f56eSJonathan T. Looney 			break;
2299e24e5683SJonathan T. Looney #endif
23002529f56eSJonathan T. Looney 
2301df8bae1dSRodney W. Grimes 		default:
23028501a69cSRobert Watson 			INP_WUNLOCK(inp);
2303df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2304df8bae1dSRodney W. Grimes 			break;
2305df8bae1dSRodney W. Grimes 		}
2306df8bae1dSRodney W. Grimes 		break;
2307df8bae1dSRodney W. Grimes 
2308cfe8b629SGarrett Wollman 	case SOPT_GET:
23091e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
2310cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2311fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
231288f6b043SBruce M Simpson 		case TCP_MD5SIG:
2313fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
23148501a69cSRobert Watson 				INP_WUNLOCK(inp);
2315fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2316fcf59617SAndrey V. Elsukov 			}
2317fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
23181cfd4b53SBruce M Simpson 			break;
2319265ed012SBruce M Simpson #endif
23201e8f5ffaSRobert Watson 
2321df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2322cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
23238501a69cSRobert Watson 			INP_WUNLOCK(inp);
2324b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2325df8bae1dSRodney W. Grimes 			break;
2326df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
2327cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
23288501a69cSRobert Watson 			INP_WUNLOCK(inp);
2329b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2330df8bae1dSRodney W. Grimes 			break;
2331a0292f23SGarrett Wollman 		case TCP_NOOPT:
2332cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
23338501a69cSRobert Watson 			INP_WUNLOCK(inp);
2334b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2335a0292f23SGarrett Wollman 			break;
2336a0292f23SGarrett Wollman 		case TCP_NOPUSH:
2337cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
23388501a69cSRobert Watson 			INP_WUNLOCK(inp);
2339b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2340b8af5dfaSRobert Watson 			break;
2341b8af5dfaSRobert Watson 		case TCP_INFO:
2342b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
23438501a69cSRobert Watson 			INP_WUNLOCK(inp);
2344b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
2345a0292f23SGarrett Wollman 			break;
2346adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2347adc56f5aSEdward Tomasz Napierala 			{
2348adc56f5aSEdward Tomasz Napierala #ifdef STATS
2349adc56f5aSEdward Tomasz Napierala 			int nheld;
2350adc56f5aSEdward Tomasz Napierala 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2351adc56f5aSEdward Tomasz Napierala 
2352adc56f5aSEdward Tomasz Napierala 			error = 0;
2353adc56f5aSEdward Tomasz Napierala 			socklen_t outsbsz = sopt->sopt_valsize;
2354adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats == NULL)
2355adc56f5aSEdward Tomasz Napierala 				error = ENOENT;
2356adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= tp->t_stats->cursz)
2357adc56f5aSEdward Tomasz Napierala 				outsbsz = tp->t_stats->cursz;
2358adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= sizeof(struct statsblob))
2359adc56f5aSEdward Tomasz Napierala 				outsbsz = sizeof(struct statsblob);
2360adc56f5aSEdward Tomasz Napierala 			else
2361adc56f5aSEdward Tomasz Napierala 				error = EINVAL;
2362adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2363adc56f5aSEdward Tomasz Napierala 			if (error)
2364adc56f5aSEdward Tomasz Napierala 				break;
2365adc56f5aSEdward Tomasz Napierala 
2366adc56f5aSEdward Tomasz Napierala 			sbp = sopt->sopt_val;
2367adc56f5aSEdward Tomasz Napierala 			nheld = atop(round_page(((vm_offset_t)sbp) +
2368adc56f5aSEdward Tomasz Napierala 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2369adc56f5aSEdward Tomasz Napierala 			vm_page_t ma[nheld];
2370adc56f5aSEdward Tomasz Napierala 			if (vm_fault_quick_hold_pages(
2371adc56f5aSEdward Tomasz Napierala 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2372adc56f5aSEdward Tomasz Napierala 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2373adc56f5aSEdward Tomasz Napierala 			    nheld) < 0) {
2374adc56f5aSEdward Tomasz Napierala 				error = EFAULT;
2375adc56f5aSEdward Tomasz Napierala 				break;
2376adc56f5aSEdward Tomasz Napierala 			}
2377adc56f5aSEdward Tomasz Napierala 
2378adc56f5aSEdward Tomasz Napierala 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2379adc56f5aSEdward Tomasz Napierala 			    SIZEOF_MEMBER(struct statsblob, flags))))
2380adc56f5aSEdward Tomasz Napierala 				goto unhold;
2381adc56f5aSEdward Tomasz Napierala 
2382adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2383adc56f5aSEdward Tomasz Napierala 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2384adc56f5aSEdward Tomasz Napierala 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2385adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2386adc56f5aSEdward Tomasz Napierala 			sopt->sopt_valsize = outsbsz;
2387adc56f5aSEdward Tomasz Napierala unhold:
2388adc56f5aSEdward Tomasz Napierala 			vm_page_unhold_pages(ma, nheld);
2389adc56f5aSEdward Tomasz Napierala #else
2390adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2391adc56f5aSEdward Tomasz Napierala 			error = EOPNOTSUPP;
2392adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2393adc56f5aSEdward Tomasz Napierala 			break;
2394adc56f5aSEdward Tomasz Napierala 			}
2395dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2396af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2397dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2398af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
2399dbc42409SLawrence Stewart 			break;
24002f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
24012f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
24022f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
24032f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
24042f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
24052f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
24065a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
24072f3eb7f4SGleb Smirnoff 				break;
24082f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
24095a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
24102f3eb7f4SGleb Smirnoff 				break;
24112f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
24125a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
24132f3eb7f4SGleb Smirnoff 				break;
24142f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
24155a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
24162f3eb7f4SGleb Smirnoff 				break;
24172f3eb7f4SGleb Smirnoff 			}
24182f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
24192f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
24202f3eb7f4SGleb Smirnoff 			break;
242186a996e6SHiren Panchasara #ifdef TCPPCAP
242286a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
242386a996e6SHiren Panchasara 		case TCP_PCAP_IN:
242486a996e6SHiren Panchasara 			optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
242586a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
242686a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
242786a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
242886a996e6SHiren Panchasara 			break;
242986a996e6SHiren Panchasara #endif
2430281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
2431281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
2432281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2433281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
2434281a0fd4SPatrick Kelsey 			break;
2435e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
24362529f56eSJonathan T. Looney 		case TCP_LOG:
24372529f56eSJonathan T. Looney 			optval = tp->t_logstate;
24382529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24392529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
24402529f56eSJonathan T. Looney 			break;
24412529f56eSJonathan T. Looney 		case TCP_LOGBUF:
24422529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
24432529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
24442529f56eSJonathan T. Looney 			break;
24452529f56eSJonathan T. Looney 		case TCP_LOGID:
24462529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
24472529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24482529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
24492529f56eSJonathan T. Looney 			break;
24502529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
24512529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
24522529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24532529f56eSJonathan T. Looney 			error = EINVAL;
24542529f56eSJonathan T. Looney 			break;
2455e24e5683SJonathan T. Looney #endif
2456b2e60773SJohn Baldwin #ifdef KERN_TLS
2457b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2458b2e60773SJohn Baldwin 			optval = ktls_get_tx_mode(so);
2459b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2460b2e60773SJohn Baldwin 			error = sooptcopyout(sopt, &optval, sizeof(optval));
2461b2e60773SJohn Baldwin 			break;
2462f1f93475SJohn Baldwin 		case TCP_RXTLS_MODE:
2463f1f93475SJohn Baldwin 			optval = ktls_get_rx_mode(so);
2464f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2465f1f93475SJohn Baldwin 			error = sooptcopyout(sopt, &optval, sizeof(optval));
2466f1f93475SJohn Baldwin 			break;
2467b2e60773SJohn Baldwin #endif
2468df8bae1dSRodney W. Grimes 		default:
24698501a69cSRobert Watson 			INP_WUNLOCK(inp);
2470df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2471df8bae1dSRodney W. Grimes 			break;
2472df8bae1dSRodney W. Grimes 		}
2473df8bae1dSRodney W. Grimes 		break;
2474df8bae1dSRodney W. Grimes 	}
2475df8bae1dSRodney W. Grimes 	return (error);
2476df8bae1dSRodney W. Grimes }
24778501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2478bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2479df8bae1dSRodney W. Grimes 
248026e30fbbSDavid Greenman /*
2481df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2482df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2483df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2484df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2485df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2486df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2487df8bae1dSRodney W. Grimes  */
2488623dce13SRobert Watson static void
2489ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2490df8bae1dSRodney W. Grimes {
2491e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
2492e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
2493e6e0b5ffSRobert Watson 
249497a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
24958501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2496df8bae1dSRodney W. Grimes 
2497623dce13SRobert Watson 	/*
2498623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2499623dce13SRobert Watson 	 * socket is still open.
2500623dce13SRobert Watson 	 */
25018db239dcSMichael Tuexen 	if (tp->t_state < TCPS_ESTABLISHED &&
25028db239dcSMichael Tuexen 	    !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2503df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2504623dce13SRobert Watson 		KASSERT(tp != NULL,
2505623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2506623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2507243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2508623dce13SRobert Watson 		KASSERT(tp != NULL,
2509623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2510623dce13SRobert Watson 	} else {
2511df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2512df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2513623dce13SRobert Watson 		tcp_usrclosed(tp);
2514ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
251555bceb1eSRandall Stewart 			tp->t_fb->tfb_tcp_output(tp);
2516df8bae1dSRodney W. Grimes 	}
2517df8bae1dSRodney W. Grimes }
2518df8bae1dSRodney W. Grimes 
2519df8bae1dSRodney W. Grimes /*
2520df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2521df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2522df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2523df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2524df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2525df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2526df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2527df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2528df8bae1dSRodney W. Grimes  */
2529623dce13SRobert Watson static void
2530ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2531df8bae1dSRodney W. Grimes {
2532df8bae1dSRodney W. Grimes 
253397a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
25348501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
2535e6e0b5ffSRobert Watson 
2536df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2537df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
253809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
253909fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
254009fe6320SNavdeep Parhar #endif
2541550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2542bc65987aSKip Macy 		/* FALLTHROUGH */
2543bc65987aSKip Macy 	case TCPS_CLOSED:
2544df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2545623dce13SRobert Watson 		/*
2546623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2547623dce13SRobert Watson 		 * still open.
2548623dce13SRobert Watson 		 */
2549623dce13SRobert Watson 		KASSERT(tp != NULL,
2550623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2551df8bae1dSRodney W. Grimes 		break;
2552df8bae1dSRodney W. Grimes 
2553a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2554df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2555a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2556a0292f23SGarrett Wollman 		break;
2557a0292f23SGarrett Wollman 
2558df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
255957f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2560df8bae1dSRodney W. Grimes 		break;
2561df8bae1dSRodney W. Grimes 
2562df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
256357f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2564df8bae1dSRodney W. Grimes 		break;
2565df8bae1dSRodney W. Grimes 	}
2566abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
2567df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
2568abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
25697c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
25707c72af87SMohan Srinivasan 			int timeout;
25717c72af87SMohan Srinivasan 
25727c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
25739077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2574b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2575b6239c4aSAndras Olah 		}
2576df8bae1dSRodney W. Grimes 	}
25777c72af87SMohan Srinivasan }
2578497057eeSRobert Watson 
2579497057eeSRobert Watson #ifdef DDB
2580497057eeSRobert Watson static void
2581497057eeSRobert Watson db_print_indent(int indent)
2582497057eeSRobert Watson {
2583497057eeSRobert Watson 	int i;
2584497057eeSRobert Watson 
2585497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2586497057eeSRobert Watson 		db_printf(" ");
2587497057eeSRobert Watson }
2588497057eeSRobert Watson 
2589497057eeSRobert Watson static void
2590497057eeSRobert Watson db_print_tstate(int t_state)
2591497057eeSRobert Watson {
2592497057eeSRobert Watson 
2593497057eeSRobert Watson 	switch (t_state) {
2594497057eeSRobert Watson 	case TCPS_CLOSED:
2595497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2596497057eeSRobert Watson 		return;
2597497057eeSRobert Watson 
2598497057eeSRobert Watson 	case TCPS_LISTEN:
2599497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2600497057eeSRobert Watson 		return;
2601497057eeSRobert Watson 
2602497057eeSRobert Watson 	case TCPS_SYN_SENT:
2603497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2604497057eeSRobert Watson 		return;
2605497057eeSRobert Watson 
2606497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2607497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2608497057eeSRobert Watson 		return;
2609497057eeSRobert Watson 
2610497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2611497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2612497057eeSRobert Watson 		return;
2613497057eeSRobert Watson 
2614497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2615497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2616497057eeSRobert Watson 		return;
2617497057eeSRobert Watson 
2618497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2619497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2620497057eeSRobert Watson 		return;
2621497057eeSRobert Watson 
2622497057eeSRobert Watson 	case TCPS_CLOSING:
2623497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2624497057eeSRobert Watson 		return;
2625497057eeSRobert Watson 
2626497057eeSRobert Watson 	case TCPS_LAST_ACK:
2627497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2628497057eeSRobert Watson 		return;
2629497057eeSRobert Watson 
2630497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2631497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2632497057eeSRobert Watson 		return;
2633497057eeSRobert Watson 
2634497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2635497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2636497057eeSRobert Watson 		return;
2637497057eeSRobert Watson 
2638497057eeSRobert Watson 	default:
2639497057eeSRobert Watson 		db_printf("unknown");
2640497057eeSRobert Watson 		return;
2641497057eeSRobert Watson 	}
2642497057eeSRobert Watson }
2643497057eeSRobert Watson 
2644497057eeSRobert Watson static void
2645497057eeSRobert Watson db_print_tflags(u_int t_flags)
2646497057eeSRobert Watson {
2647497057eeSRobert Watson 	int comma;
2648497057eeSRobert Watson 
2649497057eeSRobert Watson 	comma = 0;
2650497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2651497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2652497057eeSRobert Watson 		comma = 1;
2653497057eeSRobert Watson 	}
2654497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2655497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2656497057eeSRobert Watson 		comma = 1;
2657497057eeSRobert Watson 	}
2658497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2659497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2660497057eeSRobert Watson 		comma = 1;
2661497057eeSRobert Watson 	}
2662497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2663497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2664497057eeSRobert Watson 		comma = 1;
2665497057eeSRobert Watson 	}
2666497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2667497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2668497057eeSRobert Watson 		comma = 1;
2669497057eeSRobert Watson 	}
2670497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2671497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2672497057eeSRobert Watson 		comma = 1;
2673497057eeSRobert Watson 	}
2674497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2675497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2676497057eeSRobert Watson 		comma = 1;
2677497057eeSRobert Watson 	}
2678497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2679497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2680497057eeSRobert Watson 		comma = 1;
2681497057eeSRobert Watson 	}
2682497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2683497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2684497057eeSRobert Watson 		comma = 1;
2685497057eeSRobert Watson 	}
2686497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2687497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2688497057eeSRobert Watson 		comma = 1;
2689497057eeSRobert Watson 	}
2690497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2691497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2692497057eeSRobert Watson 		comma = 1;
2693497057eeSRobert Watson 	}
2694497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2695497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2696497057eeSRobert Watson 		comma = 1;
2697497057eeSRobert Watson 	}
2698497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2699497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2700497057eeSRobert Watson 		comma = 1;
2701497057eeSRobert Watson 	}
2702497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2703497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2704497057eeSRobert Watson 		comma = 1;
2705497057eeSRobert Watson 	}
2706497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
2707497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2708497057eeSRobert Watson 		comma = 1;
2709497057eeSRobert Watson 	}
2710497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2711497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2712497057eeSRobert Watson 		comma = 1;
2713497057eeSRobert Watson 	}
2714497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2715497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2716497057eeSRobert Watson 		comma = 1;
2717497057eeSRobert Watson 	}
2718497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2719497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2720497057eeSRobert Watson 		comma = 1;
2721497057eeSRobert Watson 	}
2722dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
2723dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2724dbc42409SLawrence Stewart 		comma = 1;
2725dbc42409SLawrence Stewart 	}
2726497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2727497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2728497057eeSRobert Watson 		comma = 1;
2729497057eeSRobert Watson 	}
2730497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2731497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2732497057eeSRobert Watson 		comma = 1;
2733497057eeSRobert Watson 	}
2734497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2735497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2736497057eeSRobert Watson 		comma = 1;
2737497057eeSRobert Watson 	}
2738497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2739497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2740497057eeSRobert Watson 		comma = 1;
2741497057eeSRobert Watson 	}
2742281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2743281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2744281a0fd4SPatrick Kelsey 		comma = 1;
2745281a0fd4SPatrick Kelsey 	}
2746497057eeSRobert Watson }
2747497057eeSRobert Watson 
2748497057eeSRobert Watson static void
27493cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2)
27503cf38784SMichael Tuexen {
27513cf38784SMichael Tuexen 	int comma;
27523cf38784SMichael Tuexen 
27533cf38784SMichael Tuexen 	comma = 0;
27543cf38784SMichael Tuexen 	if (t_flags2 & TF2_ECN_PERMIT) {
27553cf38784SMichael Tuexen 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
27563cf38784SMichael Tuexen 		comma = 1;
27573cf38784SMichael Tuexen 	}
27583cf38784SMichael Tuexen }
27593cf38784SMichael Tuexen 
27603cf38784SMichael Tuexen 
27613cf38784SMichael Tuexen static void
2762497057eeSRobert Watson db_print_toobflags(char t_oobflags)
2763497057eeSRobert Watson {
2764497057eeSRobert Watson 	int comma;
2765497057eeSRobert Watson 
2766497057eeSRobert Watson 	comma = 0;
2767497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
2768497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2769497057eeSRobert Watson 		comma = 1;
2770497057eeSRobert Watson 	}
2771497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
2772497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2773497057eeSRobert Watson 		comma = 1;
2774497057eeSRobert Watson 	}
2775497057eeSRobert Watson }
2776497057eeSRobert Watson 
2777497057eeSRobert Watson static void
2778497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2779497057eeSRobert Watson {
2780497057eeSRobert Watson 
2781497057eeSRobert Watson 	db_print_indent(indent);
2782497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
2783497057eeSRobert Watson 
2784497057eeSRobert Watson 	indent += 2;
2785497057eeSRobert Watson 
2786497057eeSRobert Watson 	db_print_indent(indent);
2787497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
2788c28440dbSRandall Stewart 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2789497057eeSRobert Watson 
2790497057eeSRobert Watson 	db_print_indent(indent);
279185d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
2792e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2793497057eeSRobert Watson 
2794497057eeSRobert Watson 	db_print_indent(indent);
2795e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2796e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
2797497057eeSRobert Watson 
2798497057eeSRobert Watson 	db_print_indent(indent);
2799497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
2800497057eeSRobert Watson 	db_print_tstate(tp->t_state);
2801497057eeSRobert Watson 	db_printf(")\n");
2802497057eeSRobert Watson 
2803497057eeSRobert Watson 	db_print_indent(indent);
2804497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
2805497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
2806497057eeSRobert Watson 	db_printf(")\n");
2807497057eeSRobert Watson 
2808497057eeSRobert Watson 	db_print_indent(indent);
28093cf38784SMichael Tuexen 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
28103cf38784SMichael Tuexen 	db_print_tflags2(tp->t_flags2);
28113cf38784SMichael Tuexen 	db_printf(")\n");
28123cf38784SMichael Tuexen 
28133cf38784SMichael Tuexen 	db_print_indent(indent);
2814497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
2815497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
2816497057eeSRobert Watson 
2817497057eeSRobert Watson 	db_print_indent(indent);
2818497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
2819497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2820497057eeSRobert Watson 
2821497057eeSRobert Watson 	db_print_indent(indent);
2822497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
2823497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
2824497057eeSRobert Watson 
2825497057eeSRobert Watson 	db_print_indent(indent);
28263ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
2827497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2828497057eeSRobert Watson 
2829497057eeSRobert Watson 	db_print_indent(indent);
28303ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
28311c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
2832497057eeSRobert Watson 
2833497057eeSRobert Watson 	db_print_indent(indent);
28343ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
28351c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2836497057eeSRobert Watson 
2837497057eeSRobert Watson 	db_print_indent(indent);
28380c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
28390c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
2840497057eeSRobert Watson 
2841497057eeSRobert Watson 	db_print_indent(indent);
28421c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
28431c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
2844497057eeSRobert Watson 
2845497057eeSRobert Watson 	db_print_indent(indent);
28461c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
28471c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2848497057eeSRobert Watson 
2849497057eeSRobert Watson 	db_print_indent(indent);
2850497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
2851497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2852497057eeSRobert Watson 	    tp->t_rttbest);
2853497057eeSRobert Watson 
2854497057eeSRobert Watson 	db_print_indent(indent);
28553ac12506SJonathan T. Looney 	db_printf("t_rttupdated: %lu   max_sndwnd: %u   t_softerror: %d\n",
2856497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2857497057eeSRobert Watson 
2858497057eeSRobert Watson 	db_print_indent(indent);
2859497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2860497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
2861497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
2862497057eeSRobert Watson 
2863497057eeSRobert Watson 	db_print_indent(indent);
2864497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
2865497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2866497057eeSRobert Watson 
2867497057eeSRobert Watson 	db_print_indent(indent);
28689f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
28691a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
2870497057eeSRobert Watson 
2871497057eeSRobert Watson 	db_print_indent(indent);
2872497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
28733ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2874497057eeSRobert Watson 
2875497057eeSRobert Watson 	db_print_indent(indent);
28763ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
28779f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2878497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
2879497057eeSRobert Watson 
2880497057eeSRobert Watson 	db_print_indent(indent);
28813529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
28823529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2883497057eeSRobert Watson 
2884497057eeSRobert Watson 	db_print_indent(indent);
2885a3574665SMichael Tuexen 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
2886a3574665SMichael Tuexen 	    tp->snd_fack, tp->rcv_numsacks);
2887497057eeSRobert Watson 
2888497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
2889497057eeSRobert Watson 
2890497057eeSRobert Watson 	db_print_indent(indent);
2891497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
2892497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2893497057eeSRobert Watson }
2894497057eeSRobert Watson 
2895497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2896497057eeSRobert Watson {
2897497057eeSRobert Watson 	struct tcpcb *tp;
2898497057eeSRobert Watson 
2899497057eeSRobert Watson 	if (!have_addr) {
2900497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
2901497057eeSRobert Watson 		return;
2902497057eeSRobert Watson 	}
2903497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
2904497057eeSRobert Watson 
2905497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
2906497057eeSRobert Watson }
2907497057eeSRobert Watson #endif
2908