xref: /freebsd/sys/netinet/tcp_usrreq.c (revision f1f93475463891194c453aff5f7c872fa9109b45)
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 
1362c37256eSGarrett Wollman #ifdef TCPDEBUG
1371db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1382c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
1394cc20ab1SSeigo Tanimura #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
1404cc20ab1SSeigo Tanimura 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1412c37256eSGarrett Wollman #else
1422c37256eSGarrett Wollman #define	TCPDEBUG0
1432c37256eSGarrett Wollman #define	TCPDEBUG1()
1442c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1452c37256eSGarrett Wollman #endif
1462c37256eSGarrett Wollman 
1472c37256eSGarrett Wollman /*
1482c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1492c37256eSGarrett Wollman  * and an internet control block.
1502c37256eSGarrett Wollman  */
1512c37256eSGarrett Wollman static int
152b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1532c37256eSGarrett Wollman {
154f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
155623dce13SRobert Watson 	struct tcpcb *tp = NULL;
156623dce13SRobert Watson 	int error;
1572c37256eSGarrett Wollman 	TCPDEBUG0;
1582c37256eSGarrett Wollman 
159623dce13SRobert Watson 	inp = sotoinpcb(so);
160623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1612c37256eSGarrett Wollman 	TCPDEBUG1();
1622c37256eSGarrett Wollman 
1630f6385e7SGleb Smirnoff 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1640f6385e7SGleb Smirnoff 		error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1652c37256eSGarrett Wollman 		if (error)
1662c37256eSGarrett Wollman 			goto out;
1670f6385e7SGleb Smirnoff 	}
1682c37256eSGarrett Wollman 
1690f6385e7SGleb Smirnoff 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
1700f6385e7SGleb Smirnoff 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1710f6385e7SGleb Smirnoff 	error = in_pcballoc(so, &V_tcbinfo);
1727669c586SGleb Smirnoff 	if (error)
1730f6385e7SGleb Smirnoff 		goto out;
1740f6385e7SGleb Smirnoff 	inp = sotoinpcb(so);
1750f6385e7SGleb Smirnoff #ifdef INET6
1760f6385e7SGleb Smirnoff 	if (inp->inp_vflag & INP_IPV6PROTO) {
1770f6385e7SGleb Smirnoff 		inp->inp_vflag |= INP_IPV6;
1780f6385e7SGleb Smirnoff 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
1790f6385e7SGleb Smirnoff 			inp->inp_vflag |= INP_IPV4;
1800f6385e7SGleb Smirnoff 		inp->in6p_hops = -1;	/* use kernel default */
1810f6385e7SGleb Smirnoff 	}
1820f6385e7SGleb Smirnoff 	else
1830f6385e7SGleb Smirnoff #endif
1840f6385e7SGleb Smirnoff 		inp->inp_vflag |= INP_IPV4;
1850f6385e7SGleb Smirnoff 	tp = tcp_newtcpcb(inp);
1860f6385e7SGleb Smirnoff 	if (tp == NULL) {
1877669c586SGleb Smirnoff 		error = ENOBUFS;
1880f6385e7SGleb Smirnoff 		in_pcbdetach(inp);
1890f6385e7SGleb Smirnoff 		in_pcbfree(inp);
1900f6385e7SGleb Smirnoff 		goto out;
1910f6385e7SGleb Smirnoff 	}
1920f6385e7SGleb Smirnoff 	tp->t_state = TCPS_CLOSED;
1930f6385e7SGleb Smirnoff 	INP_WUNLOCK(inp);
1940f6385e7SGleb Smirnoff 	TCPSTATES_INC(TCPS_CLOSED);
1952c37256eSGarrett Wollman 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1963879597fSAndrey A. Chernov 		so->so_linger = TCP_LINGERTIME;
1972c37256eSGarrett Wollman out:
1982c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
1995d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
2000f6385e7SGleb Smirnoff 	return (error);
2012c37256eSGarrett Wollman }
2022c37256eSGarrett Wollman 
2032c37256eSGarrett Wollman /*
2043fed74e9SGleb Smirnoff  * tcp_usr_detach is called when the socket layer loses its final reference
205a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
206a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
207a152f8a3SRobert Watson  * inpcb state: time wait.
2082c37256eSGarrett Wollman  */
209bc725eafSRobert Watson static void
2103fed74e9SGleb Smirnoff tcp_usr_detach(struct socket *so)
2112c37256eSGarrett Wollman {
2123fed74e9SGleb Smirnoff 	struct inpcb *inp;
2132c37256eSGarrett Wollman 	struct tcpcb *tp;
2142c37256eSGarrett Wollman 
2153fed74e9SGleb Smirnoff 	inp = sotoinpcb(so);
2163fed74e9SGleb Smirnoff 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
2173fed74e9SGleb Smirnoff 	INP_WLOCK(inp);
2183fed74e9SGleb Smirnoff 	KASSERT(so->so_pcb == inp && inp->inp_socket == so,
2193fed74e9SGleb Smirnoff 		("%s: socket %p inp %p mismatch", __func__, so, inp));
220953b5606SRobert Watson 
221a152f8a3SRobert Watson 	tp = intotcpcb(inp);
222a152f8a3SRobert Watson 
223ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
224623dce13SRobert Watson 		/*
225a152f8a3SRobert Watson 		 * There are two cases to handle: one in which the time wait
226a152f8a3SRobert Watson 		 * state is being discarded (INP_DROPPED), and one in which
227a152f8a3SRobert Watson 		 * this connection will remain in timewait.  In the former,
228a152f8a3SRobert Watson 		 * it is time to discard all state (except tcptw, which has
229a152f8a3SRobert Watson 		 * already been discarded by the timewait close code, which
230a152f8a3SRobert Watson 		 * should be further up the call stack somewhere).  In the
231a152f8a3SRobert Watson 		 * latter case, we detach from the socket, but leave the pcb
232a152f8a3SRobert Watson 		 * present until timewait ends.
233623dce13SRobert Watson 		 *
234a152f8a3SRobert Watson 		 * XXXRW: Would it be cleaner to free the tcptw here?
235cea40c48SJulien Charbon 		 *
236cea40c48SJulien Charbon 		 * Astute question indeed, from twtcp perspective there are
237dd388cfdSGleb Smirnoff 		 * four cases to consider:
238cea40c48SJulien Charbon 		 *
2393fed74e9SGleb Smirnoff 		 * #1 tcp_usr_detach is called at tcptw creation time by
240cea40c48SJulien Charbon 		 *  tcp_twstart, then do not discard the newly created tcptw
241cea40c48SJulien Charbon 		 *  and leave inpcb present until timewait ends
2423fed74e9SGleb Smirnoff 		 * #2 tcp_usr_detach is called at tcptw creation time by
243dd388cfdSGleb Smirnoff 		 *  tcp_twstart, but connection is local and tw will be
244dd388cfdSGleb Smirnoff 		 *  discarded immediately
2453fed74e9SGleb Smirnoff 		 * #3 tcp_usr_detach is called at timewait end (or reuse) by
246cea40c48SJulien Charbon 		 *  tcp_twclose, then the tcptw has already been discarded
247ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
2483fed74e9SGleb Smirnoff 		 * #4 tcp_usr_detach is called() after timewait ends (or reuse)
249cea40c48SJulien Charbon 		 *  (e.g. by soclose), then tcptw has already been discarded
250ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
251cea40c48SJulien Charbon 		 *
252cea40c48SJulien Charbon 		 *  In all three cases the tcptw should not be freed here.
253623dce13SRobert Watson 		 */
254ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED) {
255623dce13SRobert Watson 			in_pcbdetach(inp);
256f5cf1e5fSJulien Charbon 			if (__predict_true(tp == NULL)) {
2570206cdb8SBjoern A. Zeeb 				in_pcbfree(inp);
2580206cdb8SBjoern A. Zeeb 			} else {
259f5cf1e5fSJulien Charbon 				/*
260f5cf1e5fSJulien Charbon 				 * This case should not happen as in TIMEWAIT
261f5cf1e5fSJulien Charbon 				 * state the inp should not be destroyed before
262f5cf1e5fSJulien Charbon 				 * its tcptw.  If INVARIANTS is defined, panic.
263f5cf1e5fSJulien Charbon 				 */
264f5cf1e5fSJulien Charbon #ifdef INVARIANTS
265f5cf1e5fSJulien Charbon 				panic("%s: Panic before an inp double-free: "
266f5cf1e5fSJulien Charbon 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
267f5cf1e5fSJulien Charbon 				    , __func__);
268f5cf1e5fSJulien Charbon #else
269f5cf1e5fSJulien Charbon 				log(LOG_ERR, "%s: Avoid an inp double-free: "
270f5cf1e5fSJulien Charbon 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
271f5cf1e5fSJulien Charbon 				    , __func__);
272f5cf1e5fSJulien Charbon #endif
273f5cf1e5fSJulien Charbon 				INP_WUNLOCK(inp);
274f5cf1e5fSJulien Charbon 			}
275f5cf1e5fSJulien Charbon 		} else {
276623dce13SRobert Watson 			in_pcbdetach(inp);
2778501a69cSRobert Watson 			INP_WUNLOCK(inp);
278623dce13SRobert Watson 		}
279623dce13SRobert Watson 	} else {
280e6e65783SRobert Watson 		/*
281a152f8a3SRobert Watson 		 * If the connection is not in timewait, we consider two
282a152f8a3SRobert Watson 		 * two conditions: one in which no further processing is
283a152f8a3SRobert Watson 		 * necessary (dropped || embryonic), and one in which TCP is
284a152f8a3SRobert Watson 		 * not yet done, but no longer requires the socket, so the
285a152f8a3SRobert Watson 		 * pcb will persist for the time being.
286a152f8a3SRobert Watson 		 *
287a152f8a3SRobert Watson 		 * XXXRW: Does the second case still occur?
288e6e65783SRobert Watson 		 */
289ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED ||
290623dce13SRobert Watson 		    tp->t_state < TCPS_SYN_SENT) {
291623dce13SRobert Watson 			tcp_discardcb(tp);
292623dce13SRobert Watson 			in_pcbdetach(inp);
2930206cdb8SBjoern A. Zeeb 			in_pcbfree(inp);
294db3cee51SNavdeep Parhar 		} else {
295a152f8a3SRobert Watson 			in_pcbdetach(inp);
296db3cee51SNavdeep Parhar 			INP_WUNLOCK(inp);
297db3cee51SNavdeep Parhar 		}
298623dce13SRobert Watson 	}
299623dce13SRobert Watson }
300c78cbc7bSRobert Watson 
301b287c6c7SBjoern A. Zeeb #ifdef INET
3022c37256eSGarrett Wollman /*
3032c37256eSGarrett Wollman  * Give the socket an address.
3042c37256eSGarrett Wollman  */
3052c37256eSGarrett Wollman static int
306b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
3072c37256eSGarrett Wollman {
3082c37256eSGarrett Wollman 	int error = 0;
309f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
310623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3112c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
3122c37256eSGarrett Wollman 
31352710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
31452710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sinp))
31552710de1SPawel Jakub Dawidek 		return (EINVAL);
3162c37256eSGarrett Wollman 	/*
3172c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
3182c37256eSGarrett Wollman 	 * to them.
3192c37256eSGarrett Wollman 	 */
3202c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
32152710de1SPawel Jakub Dawidek 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
32252710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
32352710de1SPawel Jakub Dawidek 
324623dce13SRobert Watson 	TCPDEBUG0;
325623dce13SRobert Watson 	inp = sotoinpcb(so);
326623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
3278501a69cSRobert Watson 	INP_WLOCK(inp);
328ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
329623dce13SRobert Watson 		error = EINVAL;
3302c37256eSGarrett Wollman 		goto out;
331623dce13SRobert Watson 	}
332623dce13SRobert Watson 	tp = intotcpcb(inp);
333623dce13SRobert Watson 	TCPDEBUG1();
334fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
335623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
336fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
337623dce13SRobert Watson out:
338623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3395d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3408501a69cSRobert Watson 	INP_WUNLOCK(inp);
341623dce13SRobert Watson 
342623dce13SRobert Watson 	return (error);
3432c37256eSGarrett Wollman }
344b287c6c7SBjoern A. Zeeb #endif /* INET */
3452c37256eSGarrett Wollman 
346fb59c426SYoshinobu Inoue #ifdef INET6
347fb59c426SYoshinobu Inoue static int
348b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
349fb59c426SYoshinobu Inoue {
350fb59c426SYoshinobu Inoue 	int error = 0;
351f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
352623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3530ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
3544a91aa8fSMichael Tuexen 	u_char vflagsav;
355fb59c426SYoshinobu Inoue 
3560ecd976eSBjoern A. Zeeb 	sin6 = (struct sockaddr_in6 *)nam;
3570ecd976eSBjoern A. Zeeb 	if (nam->sa_len != sizeof (*sin6))
35852710de1SPawel Jakub Dawidek 		return (EINVAL);
359fb59c426SYoshinobu Inoue 	/*
360fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
361fb59c426SYoshinobu Inoue 	 * to them.
362fb59c426SYoshinobu Inoue 	 */
3630ecd976eSBjoern A. Zeeb 	if (sin6->sin6_family == AF_INET6 &&
3640ecd976eSBjoern A. Zeeb 	    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
36552710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
36652710de1SPawel Jakub Dawidek 
367623dce13SRobert Watson 	TCPDEBUG0;
368623dce13SRobert Watson 	inp = sotoinpcb(so);
369623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
3708501a69cSRobert Watson 	INP_WLOCK(inp);
3714a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
372ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
373623dce13SRobert Watson 		error = EINVAL;
374623dce13SRobert Watson 		goto out;
375623dce13SRobert Watson 	}
376623dce13SRobert Watson 	tp = intotcpcb(inp);
377623dce13SRobert Watson 	TCPDEBUG1();
378fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
379fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
380fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
381b287c6c7SBjoern A. Zeeb #ifdef INET
38266ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
3830ecd976eSBjoern A. Zeeb 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
384fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
3850ecd976eSBjoern A. Zeeb 		else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
386fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
387fb59c426SYoshinobu Inoue 
3880ecd976eSBjoern A. Zeeb 			in6_sin6_2_sin(&sin, sin6);
389888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
390888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
391888973f5SMichael Tuexen 				INP_HASH_WUNLOCK(&V_tcbinfo);
392888973f5SMichael Tuexen 				goto out;
393888973f5SMichael Tuexen 			}
394fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
395fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
396b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
397b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
398fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
399fb59c426SYoshinobu Inoue 			goto out;
400fb59c426SYoshinobu Inoue 		}
401fb59c426SYoshinobu Inoue 	}
402b287c6c7SBjoern A. Zeeb #endif
403b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
404fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
405623dce13SRobert Watson out:
4064a91aa8fSMichael Tuexen 	if (error != 0)
4074a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
408623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
4095d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
4108501a69cSRobert Watson 	INP_WUNLOCK(inp);
411623dce13SRobert Watson 	return (error);
412fb59c426SYoshinobu Inoue }
413fb59c426SYoshinobu Inoue #endif /* INET6 */
414fb59c426SYoshinobu Inoue 
415b287c6c7SBjoern A. Zeeb #ifdef INET
4162c37256eSGarrett Wollman /*
4172c37256eSGarrett Wollman  * Prepare to accept connections.
4182c37256eSGarrett Wollman  */
4192c37256eSGarrett Wollman static int
420d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
4212c37256eSGarrett Wollman {
4222c37256eSGarrett Wollman 	int error = 0;
423f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
424623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4252c37256eSGarrett Wollman 
426623dce13SRobert Watson 	TCPDEBUG0;
427623dce13SRobert Watson 	inp = sotoinpcb(so);
428623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
4298501a69cSRobert Watson 	INP_WLOCK(inp);
430ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
431623dce13SRobert Watson 		error = EINVAL;
432623dce13SRobert Watson 		goto out;
433623dce13SRobert Watson 	}
434623dce13SRobert Watson 	tp = intotcpcb(inp);
435623dce13SRobert Watson 	TCPDEBUG1();
4360daccb9cSRobert Watson 	SOCK_LOCK(so);
4370daccb9cSRobert Watson 	error = solisten_proto_check(so);
438fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
4390daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0)
440b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
441fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4420daccb9cSRobert Watson 	if (error == 0) {
44357f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
444d374e81eSRobert Watson 		solisten_proto(so, backlog);
44509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
44637cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
44709fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
44809fe6320SNavdeep Parhar #endif
4490daccb9cSRobert Watson 	}
4500daccb9cSRobert Watson 	SOCK_UNLOCK(so);
451623dce13SRobert Watson 
45268bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
453281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
45418a75309SPatrick Kelsey 
455623dce13SRobert Watson out:
456623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4575d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4588501a69cSRobert Watson 	INP_WUNLOCK(inp);
459623dce13SRobert Watson 	return (error);
4602c37256eSGarrett Wollman }
461b287c6c7SBjoern A. Zeeb #endif /* INET */
4622c37256eSGarrett Wollman 
463fb59c426SYoshinobu Inoue #ifdef INET6
464fb59c426SYoshinobu Inoue static int
465d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
466fb59c426SYoshinobu Inoue {
467fb59c426SYoshinobu Inoue 	int error = 0;
468f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
469623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4704a91aa8fSMichael Tuexen 	u_char vflagsav;
471fb59c426SYoshinobu Inoue 
472623dce13SRobert Watson 	TCPDEBUG0;
473623dce13SRobert Watson 	inp = sotoinpcb(so);
474623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4758501a69cSRobert Watson 	INP_WLOCK(inp);
476ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
477623dce13SRobert Watson 		error = EINVAL;
478623dce13SRobert Watson 		goto out;
479623dce13SRobert Watson 	}
4804a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
481623dce13SRobert Watson 	tp = intotcpcb(inp);
482623dce13SRobert Watson 	TCPDEBUG1();
4830daccb9cSRobert Watson 	SOCK_LOCK(so);
4840daccb9cSRobert Watson 	error = solisten_proto_check(so);
485fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
4860daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0) {
487fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
48866ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
489fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
490b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
491fb59c426SYoshinobu Inoue 	}
492fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4930daccb9cSRobert Watson 	if (error == 0) {
49457f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
495d374e81eSRobert Watson 		solisten_proto(so, backlog);
49609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
49737cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
49809fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
49909fe6320SNavdeep Parhar #endif
5000daccb9cSRobert Watson 	}
5010daccb9cSRobert Watson 	SOCK_UNLOCK(so);
502623dce13SRobert Watson 
50368bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
504281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
50518a75309SPatrick Kelsey 
5064a91aa8fSMichael Tuexen 	if (error != 0)
5074a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
5084a91aa8fSMichael Tuexen 
509623dce13SRobert Watson out:
510623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
5115d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
5128501a69cSRobert Watson 	INP_WUNLOCK(inp);
513623dce13SRobert Watson 	return (error);
514fb59c426SYoshinobu Inoue }
515fb59c426SYoshinobu Inoue #endif /* INET6 */
516fb59c426SYoshinobu Inoue 
517b287c6c7SBjoern A. Zeeb #ifdef INET
5182c37256eSGarrett Wollman /*
5192c37256eSGarrett Wollman  * Initiate connection to peer.
5202c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
5212c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
5222c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
5232c37256eSGarrett Wollman  * Send initial segment on connection.
5242c37256eSGarrett Wollman  */
5252c37256eSGarrett Wollman static int
526b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
5272c37256eSGarrett Wollman {
528109eb549SGleb Smirnoff 	struct epoch_tracker et;
5292c37256eSGarrett Wollman 	int error = 0;
530f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
531623dce13SRobert Watson 	struct tcpcb *tp = NULL;
5322c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
5332c37256eSGarrett Wollman 
53457bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
535e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
536e29ef13fSDon Lewis 		return (EINVAL);
53752710de1SPawel Jakub Dawidek 	/*
53852710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
53952710de1SPawel Jakub Dawidek 	 */
5402c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
54152710de1SPawel Jakub Dawidek 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
54252710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
543b89e82ddSJamie Gritton 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
544b89e82ddSJamie Gritton 		return (error);
54575c13541SPoul-Henning Kamp 
546623dce13SRobert Watson 	TCPDEBUG0;
547623dce13SRobert Watson 	inp = sotoinpcb(so);
548623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
5498501a69cSRobert Watson 	INP_WLOCK(inp);
550eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
551eb96dc33SJulien Charbon 		error = EADDRINUSE;
552eb96dc33SJulien Charbon 		goto out;
553eb96dc33SJulien Charbon 	}
554eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
555eb96dc33SJulien Charbon 		error = ECONNREFUSED;
556623dce13SRobert Watson 		goto out;
557623dce13SRobert Watson 	}
558623dce13SRobert Watson 	tp = intotcpcb(inp);
559623dce13SRobert Watson 	TCPDEBUG1();
560c1604fe4SGleb Smirnoff 	NET_EPOCH_ENTER(et);
561b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
562c1604fe4SGleb Smirnoff 		goto out_in_epoch;
56309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
56409fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
56537cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
56609fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
567c1604fe4SGleb Smirnoff 		goto out_in_epoch;
56809fe6320SNavdeep Parhar #endif
56909fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
57055bceb1eSRandall Stewart 	error = tp->t_fb->tfb_tcp_output(tp);
571c1604fe4SGleb Smirnoff out_in_epoch:
572109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
573623dce13SRobert Watson out:
574623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
575e79cb051SGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
5768501a69cSRobert Watson 	INP_WUNLOCK(inp);
577623dce13SRobert Watson 	return (error);
5782c37256eSGarrett Wollman }
579b287c6c7SBjoern A. Zeeb #endif /* INET */
5802c37256eSGarrett Wollman 
581fb59c426SYoshinobu Inoue #ifdef INET6
582fb59c426SYoshinobu Inoue static int
583b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
584fb59c426SYoshinobu Inoue {
585109eb549SGleb Smirnoff 	struct epoch_tracker et;
586fb59c426SYoshinobu Inoue 	int error = 0;
587f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
588623dce13SRobert Watson 	struct tcpcb *tp = NULL;
5890ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
5904a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
5914a91aa8fSMichael Tuexen 	u_char vflagsav;
592623dce13SRobert Watson 
593623dce13SRobert Watson 	TCPDEBUG0;
594fb59c426SYoshinobu Inoue 
5950ecd976eSBjoern A. Zeeb 	sin6 = (struct sockaddr_in6 *)nam;
5960ecd976eSBjoern A. Zeeb 	if (nam->sa_len != sizeof (*sin6))
597e29ef13fSDon Lewis 		return (EINVAL);
59852710de1SPawel Jakub Dawidek 	/*
59952710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
60052710de1SPawel Jakub Dawidek 	 */
6010ecd976eSBjoern A. Zeeb 	if (sin6->sin6_family == AF_INET6
6020ecd976eSBjoern A. Zeeb 	    && IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
60352710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
604fb59c426SYoshinobu Inoue 
605623dce13SRobert Watson 	inp = sotoinpcb(so);
606623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
6078501a69cSRobert Watson 	INP_WLOCK(inp);
6084a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
6094a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
610eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
611eb96dc33SJulien Charbon 		error = EADDRINUSE;
612eb96dc33SJulien Charbon 		goto out;
613eb96dc33SJulien Charbon 	}
614eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
615eb96dc33SJulien Charbon 		error = ECONNREFUSED;
616623dce13SRobert Watson 		goto out;
617623dce13SRobert Watson 	}
618623dce13SRobert Watson 	tp = intotcpcb(inp);
619623dce13SRobert Watson 	TCPDEBUG1();
620b287c6c7SBjoern A. Zeeb #ifdef INET
621fa046d87SRobert Watson 	/*
622fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
623fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
624fa046d87SRobert Watson 	 * Is this a significant problem?
625fa046d87SRobert Watson 	 */
6260ecd976eSBjoern A. Zeeb 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
627fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
628fb59c426SYoshinobu Inoue 
629d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
630d46a5312SMaxim Konovalov 			error = EINVAL;
631d46a5312SMaxim Konovalov 			goto out;
632d46a5312SMaxim Konovalov 		}
6335dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV4) == 0) {
6345dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6355dba6adaSMichael Tuexen 			goto out;
6365dba6adaSMichael Tuexen 		}
63733841545SHajimu UMEMOTO 
6380ecd976eSBjoern A. Zeeb 		in6_sin6_2_sin(&sin, sin6);
639888973f5SMichael Tuexen 		if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
640888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
641888973f5SMichael Tuexen 			goto out;
642888973f5SMichael Tuexen 		}
643b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
644b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
645413628a7SBjoern A. Zeeb 			goto out;
6464a91aa8fSMichael Tuexen 		inp->inp_vflag |= INP_IPV4;
6474a91aa8fSMichael Tuexen 		inp->inp_vflag &= ~INP_IPV6;
648c1604fe4SGleb Smirnoff 		NET_EPOCH_ENTER(et);
649b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
650c1604fe4SGleb Smirnoff 			goto out_in_epoch;
65109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
65209fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
653adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
65409fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
655c1604fe4SGleb Smirnoff 			goto out_in_epoch;
65609fe6320SNavdeep Parhar #endif
65755bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
658c1604fe4SGleb Smirnoff 		goto out_in_epoch;
6595dba6adaSMichael Tuexen 	} else {
6605dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV6) == 0) {
6615dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6625dba6adaSMichael Tuexen 			goto out;
6635dba6adaSMichael Tuexen 		}
664fb59c426SYoshinobu Inoue 	}
665b287c6c7SBjoern A. Zeeb #endif
6664a91aa8fSMichael Tuexen 	if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
6674a91aa8fSMichael Tuexen 		goto out;
668fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
669fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
670dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
671b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
672fb59c426SYoshinobu Inoue 		goto out;
67309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
67409fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
675adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
67609fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
67709fe6320SNavdeep Parhar 		goto out;
67809fe6320SNavdeep Parhar #endif
67909fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
680109eb549SGleb Smirnoff 	NET_EPOCH_ENTER(et);
68155bceb1eSRandall Stewart 	error = tp->t_fb->tfb_tcp_output(tp);
6827754e281SBjoern A. Zeeb #ifdef INET
683c1604fe4SGleb Smirnoff out_in_epoch:
6847754e281SBjoern A. Zeeb #endif
685109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
686623dce13SRobert Watson out:
6874a91aa8fSMichael Tuexen 	/*
6884a91aa8fSMichael Tuexen 	 * If the implicit bind in the connect call fails, restore
6894a91aa8fSMichael Tuexen 	 * the flags we modified.
6904a91aa8fSMichael Tuexen 	 */
6914a91aa8fSMichael Tuexen 	if (error != 0 && inp->inp_lport == 0) {
6924a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
6934a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
6944a91aa8fSMichael Tuexen 	}
6954a91aa8fSMichael Tuexen 
696623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
6975d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
6988501a69cSRobert Watson 	INP_WUNLOCK(inp);
699623dce13SRobert Watson 	return (error);
700fb59c426SYoshinobu Inoue }
701fb59c426SYoshinobu Inoue #endif /* INET6 */
702fb59c426SYoshinobu Inoue 
7032c37256eSGarrett Wollman /*
7042c37256eSGarrett Wollman  * Initiate disconnect from peer.
7052c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
7062c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
7072c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
7082c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
7092c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
7102c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
7112c37256eSGarrett Wollman  *
7122c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
7132c37256eSGarrett Wollman  */
7142c37256eSGarrett Wollman static int
7152c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
7162c37256eSGarrett Wollman {
717f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
718623dce13SRobert Watson 	struct tcpcb *tp = NULL;
7196573d758SMatt Macy 	struct epoch_tracker et;
720623dce13SRobert Watson 	int error = 0;
7212c37256eSGarrett Wollman 
722623dce13SRobert Watson 	TCPDEBUG0;
72397a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
724623dce13SRobert Watson 	inp = sotoinpcb(so);
725623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
7268501a69cSRobert Watson 	INP_WLOCK(inp);
727489dcc92SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT)
728489dcc92SJulien Charbon 		goto out;
729489dcc92SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
73021367f63SSam Leffler 		error = ECONNRESET;
731623dce13SRobert Watson 		goto out;
732623dce13SRobert Watson 	}
733623dce13SRobert Watson 	tp = intotcpcb(inp);
734623dce13SRobert Watson 	TCPDEBUG1();
735623dce13SRobert Watson 	tcp_disconnect(tp);
736623dce13SRobert Watson out:
737623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
7385d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
7398501a69cSRobert Watson 	INP_WUNLOCK(inp);
74097a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
741623dce13SRobert Watson 	return (error);
7422c37256eSGarrett Wollman }
7432c37256eSGarrett Wollman 
744b287c6c7SBjoern A. Zeeb #ifdef INET
7452c37256eSGarrett Wollman /*
7468296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
7478296cddfSRobert Watson  * just return the address of the peer, storing through addr.
7482c37256eSGarrett Wollman  */
7492c37256eSGarrett Wollman static int
75057bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
7512c37256eSGarrett Wollman {
7522c37256eSGarrett Wollman 	int error = 0;
753f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
7541db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
75526ef6ac4SDon Lewis 	struct in_addr addr;
75626ef6ac4SDon Lewis 	in_port_t port = 0;
7571db24ffbSJonathan Lemon 	TCPDEBUG0;
7582c37256eSGarrett Wollman 
7593d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
7603d2d3ef4SRobert Watson 		return (ECONNABORTED);
761f76fcf6dSJeffrey Hsu 
762f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
763623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
7648501a69cSRobert Watson 	INP_WLOCK(inp);
765ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
7663d2d3ef4SRobert Watson 		error = ECONNABORTED;
767623dce13SRobert Watson 		goto out;
768623dce13SRobert Watson 	}
7691db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
7701db24ffbSJonathan Lemon 	TCPDEBUG1();
771f76fcf6dSJeffrey Hsu 
772f76fcf6dSJeffrey Hsu 	/*
77354d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
77426ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
77526ef6ac4SDon Lewis 	 * release the lock.
776f76fcf6dSJeffrey Hsu 	 */
77726ef6ac4SDon Lewis 	port = inp->inp_fport;
77826ef6ac4SDon Lewis 	addr = inp->inp_faddr;
779f76fcf6dSJeffrey Hsu 
780623dce13SRobert Watson out:
781623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
7825d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7838501a69cSRobert Watson 	INP_WUNLOCK(inp);
78426ef6ac4SDon Lewis 	if (error == 0)
78526ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
78626ef6ac4SDon Lewis 	return error;
7872c37256eSGarrett Wollman }
788b287c6c7SBjoern A. Zeeb #endif /* INET */
7892c37256eSGarrett Wollman 
790fb59c426SYoshinobu Inoue #ifdef INET6
791fb59c426SYoshinobu Inoue static int
792fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
793fb59c426SYoshinobu Inoue {
794f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
795fb59c426SYoshinobu Inoue 	int error = 0;
7961db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
79726ef6ac4SDon Lewis 	struct in_addr addr;
79826ef6ac4SDon Lewis 	struct in6_addr addr6;
7996573d758SMatt Macy 	struct epoch_tracker et;
80026ef6ac4SDon Lewis 	in_port_t port = 0;
80126ef6ac4SDon Lewis 	int v4 = 0;
8021db24ffbSJonathan Lemon 	TCPDEBUG0;
803fb59c426SYoshinobu Inoue 
804b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
805b4470c16SRobert Watson 		return (ECONNABORTED);
806f76fcf6dSJeffrey Hsu 
807f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
808623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
80997a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
8108501a69cSRobert Watson 	INP_WLOCK(inp);
811ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
81221367f63SSam Leffler 		error = ECONNABORTED;
813623dce13SRobert Watson 		goto out;
814623dce13SRobert Watson 	}
8151db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
8161db24ffbSJonathan Lemon 	TCPDEBUG1();
817623dce13SRobert Watson 
81826ef6ac4SDon Lewis 	/*
81926ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
82026ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
82126ef6ac4SDon Lewis 	 * release the lock.
82226ef6ac4SDon Lewis 	 */
82326ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
82426ef6ac4SDon Lewis 		v4 = 1;
82526ef6ac4SDon Lewis 		port = inp->inp_fport;
82626ef6ac4SDon Lewis 		addr = inp->inp_faddr;
82726ef6ac4SDon Lewis 	} else {
82826ef6ac4SDon Lewis 		port = inp->inp_fport;
82926ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
83026ef6ac4SDon Lewis 	}
83126ef6ac4SDon Lewis 
832623dce13SRobert Watson out:
833623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
8345d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
8358501a69cSRobert Watson 	INP_WUNLOCK(inp);
83697a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
83726ef6ac4SDon Lewis 	if (error == 0) {
83826ef6ac4SDon Lewis 		if (v4)
83926ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
84026ef6ac4SDon Lewis 		else
84126ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
84226ef6ac4SDon Lewis 	}
84326ef6ac4SDon Lewis 	return error;
844fb59c426SYoshinobu Inoue }
845fb59c426SYoshinobu Inoue #endif /* INET6 */
846f76fcf6dSJeffrey Hsu 
847f76fcf6dSJeffrey Hsu /*
8482c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
8492c37256eSGarrett Wollman  */
8502c37256eSGarrett Wollman static int
8512c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
8522c37256eSGarrett Wollman {
8532c37256eSGarrett Wollman 	int error = 0;
854f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
855623dce13SRobert Watson 	struct tcpcb *tp = NULL;
8566573d758SMatt Macy 	struct epoch_tracker et;
8572c37256eSGarrett Wollman 
858623dce13SRobert Watson 	TCPDEBUG0;
85997a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
860623dce13SRobert Watson 	inp = sotoinpcb(so);
861623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
8628501a69cSRobert Watson 	INP_WLOCK(inp);
863ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
86421367f63SSam Leffler 		error = ECONNRESET;
865623dce13SRobert Watson 		goto out;
866623dce13SRobert Watson 	}
867623dce13SRobert Watson 	tp = intotcpcb(inp);
868623dce13SRobert Watson 	TCPDEBUG1();
8692c37256eSGarrett Wollman 	socantsendmore(so);
870623dce13SRobert Watson 	tcp_usrclosed(tp);
871ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
87255bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
873623dce13SRobert Watson 
874623dce13SRobert Watson out:
875623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
8765d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
8778501a69cSRobert Watson 	INP_WUNLOCK(inp);
87897a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
879623dce13SRobert Watson 
880623dce13SRobert Watson 	return (error);
8812c37256eSGarrett Wollman }
8822c37256eSGarrett Wollman 
8832c37256eSGarrett Wollman /*
8842c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
8852c37256eSGarrett Wollman  */
8862c37256eSGarrett Wollman static int
8872c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
8882c37256eSGarrett Wollman {
889109eb549SGleb Smirnoff 	struct epoch_tracker et;
890f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
891623dce13SRobert Watson 	struct tcpcb *tp = NULL;
892623dce13SRobert Watson 	int error = 0;
8932c37256eSGarrett Wollman 
894623dce13SRobert Watson 	TCPDEBUG0;
895623dce13SRobert Watson 	inp = sotoinpcb(so);
896623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
8978501a69cSRobert Watson 	INP_WLOCK(inp);
898ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
89921367f63SSam Leffler 		error = ECONNRESET;
900623dce13SRobert Watson 		goto out;
901623dce13SRobert Watson 	}
902623dce13SRobert Watson 	tp = intotcpcb(inp);
903623dce13SRobert Watson 	TCPDEBUG1();
904281a0fd4SPatrick Kelsey 	/*
905281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
906281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
907281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
908281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
909281a0fd4SPatrick Kelsey 	 * expires.
910281a0fd4SPatrick Kelsey 	 */
91168bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags) &&
912281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
913281a0fd4SPatrick Kelsey 		goto out;
91442ce7937SGleb Smirnoff 	NET_EPOCH_ENTER(et);
91509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
91609fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
91709fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
918460cf046SNavdeep Parhar 	else
91909fe6320SNavdeep Parhar #endif
92055bceb1eSRandall Stewart 	tp->t_fb->tfb_tcp_output(tp);
921109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
922623dce13SRobert Watson out:
923623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
9245d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
9258501a69cSRobert Watson 	INP_WUNLOCK(inp);
926623dce13SRobert Watson 	return (error);
9272c37256eSGarrett Wollman }
9282c37256eSGarrett Wollman 
9292c37256eSGarrett Wollman /*
9302c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
9319c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
9329c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
9339c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
9349c9906e9SPeter Wemm  * generally are caller-frees.
9352c37256eSGarrett Wollman  */
9362c37256eSGarrett Wollman static int
93757bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
938b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
9392c37256eSGarrett Wollman {
94097a95ee1SGleb Smirnoff 	struct epoch_tracker et;
9412c37256eSGarrett Wollman 	int error = 0;
942f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
943623dce13SRobert Watson 	struct tcpcb *tp = NULL;
944888973f5SMichael Tuexen #ifdef INET
94551e08d53SMichael Tuexen #ifdef INET6
94651e08d53SMichael Tuexen 	struct sockaddr_in sin;
94751e08d53SMichael Tuexen #endif
94851e08d53SMichael Tuexen 	struct sockaddr_in *sinp;
949888973f5SMichael Tuexen #endif
950fb59c426SYoshinobu Inoue #ifdef INET6
951fb59c426SYoshinobu Inoue 	int isipv6;
952fb59c426SYoshinobu Inoue #endif
9534a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
9544a91aa8fSMichael Tuexen 	u_char vflagsav;
9554a91aa8fSMichael Tuexen 	bool restoreflags;
9569c9906e9SPeter Wemm 	TCPDEBUG0;
9572c37256eSGarrett Wollman 
958f76fcf6dSJeffrey Hsu 	/*
95997a95ee1SGleb Smirnoff 	 * We require the pcbinfo "read lock" if we will close the socket
96097a95ee1SGleb Smirnoff 	 * as part of this call.
961f76fcf6dSJeffrey Hsu 	 */
96297a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
963f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
964623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
9658501a69cSRobert Watson 	INP_WLOCK(inp);
9664a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
9674a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
9684a91aa8fSMichael Tuexen 	restoreflags = false;
969ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
9707ff0b850SAndre Oppermann 		if (control)
9717ff0b850SAndre Oppermann 			m_freem(control);
9722cbcd3c1SGleb Smirnoff 		/*
9732cbcd3c1SGleb Smirnoff 		 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
9742cbcd3c1SGleb Smirnoff 		 * for freeing memory.
9752cbcd3c1SGleb Smirnoff 		 */
9762cbcd3c1SGleb Smirnoff 		if (m && (flags & PRUS_NOTREADY) == 0)
9777ff0b850SAndre Oppermann 			m_freem(m);
97821367f63SSam Leffler 		error = ECONNRESET;
9799c9906e9SPeter Wemm 		goto out;
9809c9906e9SPeter Wemm 	}
9819c9906e9SPeter Wemm 	tp = intotcpcb(inp);
9829c9906e9SPeter Wemm 	TCPDEBUG1();
983888973f5SMichael Tuexen 	if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
984888973f5SMichael Tuexen 		switch (nam->sa_family) {
985888973f5SMichael Tuexen #ifdef INET
986888973f5SMichael Tuexen 		case AF_INET:
987888973f5SMichael Tuexen 			sinp = (struct sockaddr_in *)nam;
988888973f5SMichael Tuexen 			if (sinp->sin_len != sizeof(struct sockaddr_in)) {
989888973f5SMichael Tuexen 				if (m)
990888973f5SMichael Tuexen 					m_freem(m);
991888973f5SMichael Tuexen 				error = EINVAL;
992888973f5SMichael Tuexen 				goto out;
993888973f5SMichael Tuexen 			}
994888973f5SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6) != 0) {
995888973f5SMichael Tuexen 				if (m)
996888973f5SMichael Tuexen 					m_freem(m);
997888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
998888973f5SMichael Tuexen 				goto out;
999888973f5SMichael Tuexen 			}
1000888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
1001888973f5SMichael Tuexen 				if (m)
1002888973f5SMichael Tuexen 					m_freem(m);
1003888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1004888973f5SMichael Tuexen 				goto out;
1005888973f5SMichael Tuexen 			}
1006888973f5SMichael Tuexen 			if ((error = prison_remote_ip4(td->td_ucred,
1007888973f5SMichael Tuexen 			    &sinp->sin_addr))) {
1008888973f5SMichael Tuexen 				if (m)
1009888973f5SMichael Tuexen 					m_freem(m);
1010888973f5SMichael Tuexen 				goto out;
1011888973f5SMichael Tuexen 			}
1012888973f5SMichael Tuexen #ifdef INET6
1013888973f5SMichael Tuexen 			isipv6 = 0;
1014888973f5SMichael Tuexen #endif
1015888973f5SMichael Tuexen 			break;
1016888973f5SMichael Tuexen #endif /* INET */
1017888973f5SMichael Tuexen #ifdef INET6
1018888973f5SMichael Tuexen 		case AF_INET6:
1019888973f5SMichael Tuexen 		{
10200ecd976eSBjoern A. Zeeb 			struct sockaddr_in6 *sin6;
1021888973f5SMichael Tuexen 
10220ecd976eSBjoern A. Zeeb 			sin6 = (struct sockaddr_in6 *)nam;
10230ecd976eSBjoern A. Zeeb 			if (sin6->sin6_len != sizeof(*sin6)) {
1024888973f5SMichael Tuexen 				if (m)
1025888973f5SMichael Tuexen 					m_freem(m);
1026888973f5SMichael Tuexen 				error = EINVAL;
1027888973f5SMichael Tuexen 				goto out;
1028888973f5SMichael Tuexen 			}
10290ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1030888973f5SMichael Tuexen 				if (m)
1031888973f5SMichael Tuexen 					m_freem(m);
1032888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1033888973f5SMichael Tuexen 				goto out;
1034888973f5SMichael Tuexen 			}
10350ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1036888973f5SMichael Tuexen #ifdef INET
1037888973f5SMichael Tuexen 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1038888973f5SMichael Tuexen 					error = EINVAL;
1039888973f5SMichael Tuexen 					if (m)
1040888973f5SMichael Tuexen 						m_freem(m);
1041888973f5SMichael Tuexen 					goto out;
1042888973f5SMichael Tuexen 				}
1043888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV4) == 0) {
1044888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1045888973f5SMichael Tuexen 					if (m)
1046888973f5SMichael Tuexen 						m_freem(m);
1047888973f5SMichael Tuexen 					goto out;
1048888973f5SMichael Tuexen 				}
10494a91aa8fSMichael Tuexen 				restoreflags = true;
1050888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV6;
1051888973f5SMichael Tuexen 				sinp = &sin;
10520ecd976eSBjoern A. Zeeb 				in6_sin6_2_sin(sinp, sin6);
1053888973f5SMichael Tuexen 				if (IN_MULTICAST(
1054888973f5SMichael Tuexen 				    ntohl(sinp->sin_addr.s_addr))) {
1055888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1056888973f5SMichael Tuexen 					if (m)
1057888973f5SMichael Tuexen 						m_freem(m);
1058888973f5SMichael Tuexen 					goto out;
1059888973f5SMichael Tuexen 				}
1060888973f5SMichael Tuexen 				if ((error = prison_remote_ip4(td->td_ucred,
1061888973f5SMichael Tuexen 				    &sinp->sin_addr))) {
1062888973f5SMichael Tuexen 					if (m)
1063888973f5SMichael Tuexen 						m_freem(m);
1064888973f5SMichael Tuexen 					goto out;
1065888973f5SMichael Tuexen 				}
1066888973f5SMichael Tuexen 				isipv6 = 0;
1067888973f5SMichael Tuexen #else /* !INET */
1068888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1069888973f5SMichael Tuexen 				if (m)
1070888973f5SMichael Tuexen 					m_freem(m);
1071888973f5SMichael Tuexen 				goto out;
1072888973f5SMichael Tuexen #endif /* INET */
1073888973f5SMichael Tuexen 			} else {
1074888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV6) == 0) {
1075888973f5SMichael Tuexen 					if (m)
1076888973f5SMichael Tuexen 						m_freem(m);
1077888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1078888973f5SMichael Tuexen 					goto out;
1079888973f5SMichael Tuexen 				}
10804a91aa8fSMichael Tuexen 				restoreflags = true;
1081888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV4;
1082888973f5SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_ISIPV6;
1083888973f5SMichael Tuexen 				if ((error = prison_remote_ip6(td->td_ucred,
10840ecd976eSBjoern A. Zeeb 				    &sin6->sin6_addr))) {
1085888973f5SMichael Tuexen 					if (m)
1086888973f5SMichael Tuexen 						m_freem(m);
1087888973f5SMichael Tuexen 					goto out;
1088888973f5SMichael Tuexen 				}
1089888973f5SMichael Tuexen 				isipv6 = 1;
1090888973f5SMichael Tuexen 			}
1091888973f5SMichael Tuexen 			break;
1092888973f5SMichael Tuexen 		}
1093888973f5SMichael Tuexen #endif /* INET6 */
1094888973f5SMichael Tuexen 		default:
1095888973f5SMichael Tuexen 			if (m)
1096888973f5SMichael Tuexen 				m_freem(m);
1097888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
1098888973f5SMichael Tuexen 			goto out;
1099888973f5SMichael Tuexen 		}
1100888973f5SMichael Tuexen 	}
11019c9906e9SPeter Wemm 	if (control) {
11029c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
11039c9906e9SPeter Wemm 		if (control->m_len) {
11049c9906e9SPeter Wemm 			m_freem(control);
11052c37256eSGarrett Wollman 			if (m)
11062c37256eSGarrett Wollman 				m_freem(m);
1107744f87eaSDavid Greenman 			error = EINVAL;
1108744f87eaSDavid Greenman 			goto out;
11092c37256eSGarrett Wollman 		}
11109c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
11119c9906e9SPeter Wemm 	}
11122c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
1113651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
11142c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
11152c37256eSGarrett Wollman 			/*
11162c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
11172c37256eSGarrett Wollman 			 * initialize window to default value, and
11180c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
11192c37256eSGarrett Wollman 			 */
1120fb59c426SYoshinobu Inoue #ifdef INET6
1121fb59c426SYoshinobu Inoue 			if (isipv6)
1122b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1123fb59c426SYoshinobu Inoue #endif /* INET6 */
1124b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1125b287c6c7SBjoern A. Zeeb 			else
1126b287c6c7SBjoern A. Zeeb #endif
1127b287c6c7SBjoern A. Zeeb #ifdef INET
1128888973f5SMichael Tuexen 				error = tcp_connect(tp,
1129888973f5SMichael Tuexen 				    (struct sockaddr *)sinp, td);
1130b287c6c7SBjoern A. Zeeb #endif
11314a91aa8fSMichael Tuexen 			/*
11324a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
11334a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
11344a91aa8fSMichael Tuexen 			 * operations fail.
11354a91aa8fSMichael Tuexen 			 */
11364a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
11374a91aa8fSMichael Tuexen 				restoreflags = false;
11384a91aa8fSMichael Tuexen 
11392c37256eSGarrett Wollman 			if (error)
11402c37256eSGarrett Wollman 				goto out;
1141c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1142c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
114318a75309SPatrick Kelsey 			else {
11442c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
11452c37256eSGarrett Wollman 				tcp_mss(tp, -1);
11462c37256eSGarrett Wollman 			}
1147c560df6fSPatrick Kelsey 		}
11482c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
11492c37256eSGarrett Wollman 			/*
11502c37256eSGarrett Wollman 			 * Close the send side of the connection after
11512c37256eSGarrett Wollman 			 * the data is sent.
11522c37256eSGarrett Wollman 			 */
11532c37256eSGarrett Wollman 			socantsendmore(so);
1154623dce13SRobert Watson 			tcp_usrclosed(tp);
11552c37256eSGarrett Wollman 		}
11562cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
11572cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
1158b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1159b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
116055bceb1eSRandall Stewart 			error = tp->t_fb->tfb_tcp_output(tp);
1161b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1162b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
1163b0acefa8SBill Fenner 		}
11642c37256eSGarrett Wollman 	} else {
1165623dce13SRobert Watson 		/*
1166623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1167623dce13SRobert Watson 		 */
1168d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
11692c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
1170d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
11712c37256eSGarrett Wollman 			m_freem(m);
11722c37256eSGarrett Wollman 			error = ENOBUFS;
11732c37256eSGarrett Wollman 			goto out;
11742c37256eSGarrett Wollman 		}
11752c37256eSGarrett Wollman 		/*
11762c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
11772c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
11782c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
11792c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
11802c37256eSGarrett Wollman 		 * of data past the urgent section.
11812c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
11822c37256eSGarrett Wollman 		 */
1183651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
1184d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
1185ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1186ef53690bSGarrett Wollman 			/*
1187ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
1188ef53690bSGarrett Wollman 			 * initialize window to default value, and
11890c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1190ef53690bSGarrett Wollman 			 */
119118a75309SPatrick Kelsey 
1192c560df6fSPatrick Kelsey 			/*
1193c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1194c560df6fSPatrick Kelsey 			 */
1195c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1196c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1197fb59c426SYoshinobu Inoue #ifdef INET6
1198fb59c426SYoshinobu Inoue 			if (isipv6)
1199b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1200fb59c426SYoshinobu Inoue #endif /* INET6 */
1201b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1202b287c6c7SBjoern A. Zeeb 			else
1203b287c6c7SBjoern A. Zeeb #endif
1204b287c6c7SBjoern A. Zeeb #ifdef INET
1205888973f5SMichael Tuexen 				error = tcp_connect(tp,
1206888973f5SMichael Tuexen 				    (struct sockaddr *)sinp, td);
1207b287c6c7SBjoern A. Zeeb #endif
12084a91aa8fSMichael Tuexen 			/*
12094a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
12104a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
12114a91aa8fSMichael Tuexen 			 * operations fail.
12124a91aa8fSMichael Tuexen 			 */
12134a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
12144a91aa8fSMichael Tuexen 				restoreflags = false;
12154a91aa8fSMichael Tuexen 
1216ef53690bSGarrett Wollman 			if (error)
1217ef53690bSGarrett Wollman 				goto out;
1218ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1219ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
1220623dce13SRobert Watson 		}
1221300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
12222cbcd3c1SGleb Smirnoff 		if (!(flags & PRUS_NOTREADY)) {
12232cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
122455bceb1eSRandall Stewart 			error = tp->t_fb->tfb_tcp_output(tp);
12252cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
12262c37256eSGarrett Wollman 		}
12272cbcd3c1SGleb Smirnoff 	}
12282529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL,
12292529f56eSJonathan T. Looney 	    &inp->inp_socket->so_rcv,
12302529f56eSJonathan T. Looney 	    &inp->inp_socket->so_snd,
12312529f56eSJonathan T. Looney 	    TCP_LOG_USERSEND, error,
12322529f56eSJonathan T. Looney 	    0, NULL, false);
1233d1401c90SRobert Watson out:
12344a91aa8fSMichael Tuexen 	/*
12354a91aa8fSMichael Tuexen 	 * If the request was unsuccessful and we changed flags,
12364a91aa8fSMichael Tuexen 	 * restore the original flags.
12374a91aa8fSMichael Tuexen 	 */
12384a91aa8fSMichael Tuexen 	if (error != 0 && restoreflags) {
12394a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
12404a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
12414a91aa8fSMichael Tuexen 	}
1242d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
12432c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
12445d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
12455d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
12468501a69cSRobert Watson 	INP_WUNLOCK(inp);
124797a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
124873fddedaSPeter Grehan 	return (error);
12492c37256eSGarrett Wollman }
12502c37256eSGarrett Wollman 
12512cbcd3c1SGleb Smirnoff static int
12522cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
12532cbcd3c1SGleb Smirnoff {
1254109eb549SGleb Smirnoff 	struct epoch_tracker et;
12552cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
12562cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
12572cbcd3c1SGleb Smirnoff 	int error;
12582cbcd3c1SGleb Smirnoff 
12592cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
12602cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
12612cbcd3c1SGleb Smirnoff 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
12622cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
126382334850SJohn Baldwin 		mb_free_notready(m, count);
12642cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
12652cbcd3c1SGleb Smirnoff 	}
12662cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
12672cbcd3c1SGleb Smirnoff 
12682cbcd3c1SGleb Smirnoff 	SOCKBUF_LOCK(&so->so_snd);
12692cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
12702cbcd3c1SGleb Smirnoff 	SOCKBUF_UNLOCK(&so->so_snd);
1271109eb549SGleb Smirnoff 	if (error == 0) {
1272109eb549SGleb Smirnoff 		NET_EPOCH_ENTER(et);
127355bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
1274109eb549SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1275109eb549SGleb Smirnoff 	}
12762cbcd3c1SGleb Smirnoff 	INP_WUNLOCK(inp);
12772cbcd3c1SGleb Smirnoff 
12782cbcd3c1SGleb Smirnoff 	return (error);
12792cbcd3c1SGleb Smirnoff }
12802cbcd3c1SGleb Smirnoff 
12812c37256eSGarrett Wollman /*
1282a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
12832c37256eSGarrett Wollman  */
1284ac45e92fSRobert Watson static void
12852c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
12862c37256eSGarrett Wollman {
1287f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1288a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
12896573d758SMatt Macy 	struct epoch_tracker et;
1290623dce13SRobert Watson 	TCPDEBUG0;
1291c78cbc7bSRobert Watson 
1292ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1293c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1294c78cbc7bSRobert Watson 
129597a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
12968501a69cSRobert Watson 	INP_WLOCK(inp);
1297c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1298c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1299c78cbc7bSRobert Watson 
1300c78cbc7bSRobert Watson 	/*
1301a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1302c78cbc7bSRobert Watson 	 */
1303ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1304ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1305c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
1306a152f8a3SRobert Watson 		TCPDEBUG1();
13078fa799bdSJonathan T. Looney 		tp = tcp_drop(tp, ECONNABORTED);
13088fa799bdSJonathan T. Looney 		if (tp == NULL)
13098fa799bdSJonathan T. Looney 			goto dropped;
1310a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
13115d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1312c78cbc7bSRobert Watson 	}
1313ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1314a152f8a3SRobert Watson 		SOCK_LOCK(so);
1315a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1316a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1317ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1318a152f8a3SRobert Watson 	}
13198501a69cSRobert Watson 	INP_WUNLOCK(inp);
13208fa799bdSJonathan T. Looney dropped:
132197a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1322a152f8a3SRobert Watson }
1323a152f8a3SRobert Watson 
1324a152f8a3SRobert Watson /*
1325a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1326a152f8a3SRobert Watson  */
1327a152f8a3SRobert Watson static void
1328a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1329a152f8a3SRobert Watson {
1330a152f8a3SRobert Watson 	struct inpcb *inp;
1331a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
13326573d758SMatt Macy 	struct epoch_tracker et;
1333a152f8a3SRobert Watson 	TCPDEBUG0;
1334a152f8a3SRobert Watson 
1335a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1336a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1337a152f8a3SRobert Watson 
133897a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
13398501a69cSRobert Watson 	INP_WLOCK(inp);
1340a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1341a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1342a152f8a3SRobert Watson 
1343a152f8a3SRobert Watson 	/*
1344a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
1345a152f8a3SRobert Watson 	 * a disconnect.
1346a152f8a3SRobert Watson 	 */
1347ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1348ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1349a152f8a3SRobert Watson 		tp = intotcpcb(inp);
1350a152f8a3SRobert Watson 		TCPDEBUG1();
1351a152f8a3SRobert Watson 		tcp_disconnect(tp);
1352a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
13535d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1354a152f8a3SRobert Watson 	}
1355ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1356a152f8a3SRobert Watson 		SOCK_LOCK(so);
1357a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1358a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1359ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1360a152f8a3SRobert Watson 	}
13618501a69cSRobert Watson 	INP_WUNLOCK(inp);
136297a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
13632c37256eSGarrett Wollman }
13642c37256eSGarrett Wollman 
13652c37256eSGarrett Wollman /*
13662c37256eSGarrett Wollman  * Receive out-of-band data.
13672c37256eSGarrett Wollman  */
13682c37256eSGarrett Wollman static int
13692c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
13702c37256eSGarrett Wollman {
13712c37256eSGarrett Wollman 	int error = 0;
1372f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1373623dce13SRobert Watson 	struct tcpcb *tp = NULL;
13742c37256eSGarrett Wollman 
1375623dce13SRobert Watson 	TCPDEBUG0;
1376623dce13SRobert Watson 	inp = sotoinpcb(so);
1377623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
13788501a69cSRobert Watson 	INP_WLOCK(inp);
1379ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
138021367f63SSam Leffler 		error = ECONNRESET;
1381623dce13SRobert Watson 		goto out;
1382623dce13SRobert Watson 	}
1383623dce13SRobert Watson 	tp = intotcpcb(inp);
1384623dce13SRobert Watson 	TCPDEBUG1();
13852c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1386c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
13874cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
13884cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
13892c37256eSGarrett Wollman 		error = EINVAL;
13902c37256eSGarrett Wollman 		goto out;
13912c37256eSGarrett Wollman 	}
13922c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
13932c37256eSGarrett Wollman 		error = EWOULDBLOCK;
13942c37256eSGarrett Wollman 		goto out;
13952c37256eSGarrett Wollman 	}
13962c37256eSGarrett Wollman 	m->m_len = 1;
13972c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
13982c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
13992c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1400623dce13SRobert Watson 
1401623dce13SRobert Watson out:
1402623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
14035d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
14048501a69cSRobert Watson 	INP_WUNLOCK(inp);
1405623dce13SRobert Watson 	return (error);
14062c37256eSGarrett Wollman }
14072c37256eSGarrett Wollman 
1408b287c6c7SBjoern A. Zeeb #ifdef INET
14092c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1410756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1411756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1412756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1413756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1414756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1415756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1416756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1417756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1418756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
141954d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1420756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1421756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1422756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
14232cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1424756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
142554d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1426a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1427a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
14282c37256eSGarrett Wollman };
1429b287c6c7SBjoern A. Zeeb #endif /* INET */
1430df8bae1dSRodney W. Grimes 
1431fb59c426SYoshinobu Inoue #ifdef INET6
1432fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1433756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1434756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1435756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1436756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1437756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1438756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1439756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1440756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1441756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1442756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1443756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1444756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1445756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
14462cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1447756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1448756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1449a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1450a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1451fb59c426SYoshinobu Inoue };
1452fb59c426SYoshinobu Inoue #endif /* INET6 */
1453fb59c426SYoshinobu Inoue 
1454b287c6c7SBjoern A. Zeeb #ifdef INET
1455a0292f23SGarrett Wollman /*
1456a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1457a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
14585200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
14595200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
14605200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
14615200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1462a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1463a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1464a0292f23SGarrett Wollman  */
14650312fbe9SPoul-Henning Kamp static int
1466ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1467a0292f23SGarrett Wollman {
1468a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1469a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
14705200e00eSIan Dowse 	struct in_addr laddr;
14715200e00eSIan Dowse 	u_short lport;
1472c3229e05SDavid Greenman 	int error;
1473a0292f23SGarrett Wollman 
1474c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
14758501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1476fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1477623dce13SRobert Watson 
1478a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
14794616026fSErmal Luçi 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
14804616026fSErmal Luçi 		if (error)
1481fa046d87SRobert Watson 			goto out;
1482a0292f23SGarrett Wollman 	}
1483a0292f23SGarrett Wollman 
1484a0292f23SGarrett Wollman 	/*
1485a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1486a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1487a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1488a0292f23SGarrett Wollman 	 */
14895200e00eSIan Dowse 	laddr = inp->inp_laddr;
14905200e00eSIan Dowse 	lport = inp->inp_lport;
14915200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1492b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
14935200e00eSIan Dowse 	if (error && oinp == NULL)
1494fa046d87SRobert Watson 		goto out;
1495fa046d87SRobert Watson 	if (oinp) {
1496fa046d87SRobert Watson 		error = EADDRINUSE;
1497fa046d87SRobert Watson 		goto out;
1498fa046d87SRobert Watson 	}
14995200e00eSIan Dowse 	inp->inp_laddr = laddr;
150015bd2b43SDavid Greenman 	in_pcbrehash(inp);
1501fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1502a0292f23SGarrett Wollman 
1503087b55eaSAndre Oppermann 	/*
1504087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1505087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1506087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1507087b55eaSAndre Oppermann 	 */
1508a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
15099b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1510a0292f23SGarrett Wollman 		tp->request_r_scale++;
1511a0292f23SGarrett Wollman 
1512a0292f23SGarrett Wollman 	soisconnecting(so);
151378b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
151457f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
15158e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
15168e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
15178e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1518a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1519a45d2726SAndras Olah 
1520a0292f23SGarrett Wollman 	return 0;
1521fa046d87SRobert Watson 
1522fa046d87SRobert Watson out:
1523fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1524fa046d87SRobert Watson 	return (error);
1525a0292f23SGarrett Wollman }
1526b287c6c7SBjoern A. Zeeb #endif /* INET */
1527a0292f23SGarrett Wollman 
1528fb59c426SYoshinobu Inoue #ifdef INET6
1529fb59c426SYoshinobu Inoue static int
1530ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1531fb59c426SYoshinobu Inoue {
1532a7e201bbSAndrey V. Elsukov 	struct inpcb *inp = tp->t_inpcb;
1533fb59c426SYoshinobu Inoue 	int error;
1534fb59c426SYoshinobu Inoue 
15358501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1536fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1537623dce13SRobert Watson 
1538fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
15394616026fSErmal Luçi 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
15404616026fSErmal Luçi 		if (error)
1541fa046d87SRobert Watson 			goto out;
1542fb59c426SYoshinobu Inoue 	}
1543a7e201bbSAndrey V. Elsukov 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1544a7e201bbSAndrey V. Elsukov 	if (error != 0)
1545b598155aSRobert Watson 		goto out;
1546fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1547fb59c426SYoshinobu Inoue 
1548fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1549fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1550970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1551fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1552fb59c426SYoshinobu Inoue 
1553a7e201bbSAndrey V. Elsukov 	soisconnecting(inp->inp_socket);
155478b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
155557f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
15568e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
15578e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
15588e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1559fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1560fb59c426SYoshinobu Inoue 
1561fb59c426SYoshinobu Inoue 	return 0;
1562fa046d87SRobert Watson 
1563fa046d87SRobert Watson out:
1564fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1565fa046d87SRobert Watson 	return error;
1566fb59c426SYoshinobu Inoue }
1567fb59c426SYoshinobu Inoue #endif /* INET6 */
1568fb59c426SYoshinobu Inoue 
1569cfe8b629SGarrett Wollman /*
1570b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1571b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1572b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1573b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1574b8af5dfaSRobert Watson  * from Linux.
1575b8af5dfaSRobert Watson  */
1576b8af5dfaSRobert Watson static void
1577ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1578b8af5dfaSRobert Watson {
1579b8af5dfaSRobert Watson 
15808501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1581b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1582b8af5dfaSRobert Watson 
1583b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1584b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1585b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
15863529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1587b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1588b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1589b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1590b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1591b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1592b8af5dfaSRobert Watson 	}
15933cf38784SMichael Tuexen 	if (tp->t_flags2 & TF2_ECN_PERMIT)
15945a17b6adSMichael Tuexen 		ti->tcpi_options |= TCPI_OPT_ECN;
15951baaf834SBruce M Simpson 
159643d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
15973ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
15981baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
15991baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
16001baaf834SBruce M Simpson 
1601b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1602b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1603b8af5dfaSRobert Watson 
1604b8af5dfaSRobert Watson 	/*
1605b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1606b8af5dfaSRobert Watson 	 */
1607c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1608535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1609b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
16101c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1611535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
161243d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
161343d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1614f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1615f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1616f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1617a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1618a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1619a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1620a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1621a6456410SNavdeep Parhar 	}
1622a6456410SNavdeep Parhar #endif
1623b8af5dfaSRobert Watson }
1624b8af5dfaSRobert Watson 
1625b8af5dfaSRobert Watson /*
16261e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
16271e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
16281e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
16291e8f5ffaSRobert Watson  * option.
1630cfe8b629SGarrett Wollman  */
1631bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
16328501a69cSRobert Watson 	INP_WLOCK(inp);							\
1633ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
16348501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1635bac5bedfSConrad Meyer 		cleanup;						\
16361e8f5ffaSRobert Watson 		return (ECONNRESET);					\
16371e8f5ffaSRobert Watson 	}								\
16381e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
16391e8f5ffaSRobert Watson } while(0)
1640bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
16411e8f5ffaSRobert Watson 
1642df8bae1dSRodney W. Grimes int
1643ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1644df8bae1dSRodney W. Grimes {
164555bceb1eSRandall Stewart 	int	error;
1646df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1647cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
164855bceb1eSRandall Stewart 	struct tcp_function_block *blk;
164955bceb1eSRandall Stewart 	struct tcp_function_set fsn;
1650df8bae1dSRodney W. Grimes 
1651cfe8b629SGarrett Wollman 	error = 0;
1652df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1653623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1654cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1655fb59c426SYoshinobu Inoue #ifdef INET6
16565cd54324SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6PROTO) {
1657fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
16585dff1c38SMichael Tuexen 			/*
16595dff1c38SMichael Tuexen 			 * In case of the IPV6_USE_MIN_MTU socket option,
16605dff1c38SMichael Tuexen 			 * the INC_IPV6MINMTU flag to announce a corresponding
16615dff1c38SMichael Tuexen 			 * MSS during the initial handshake.
16625dff1c38SMichael Tuexen 			 * If the TCP connection is not in the front states,
16635dff1c38SMichael Tuexen 			 * just reduce the MSS being used.
16645dff1c38SMichael Tuexen 			 * This avoids the sending of TCP segments which will
16655dff1c38SMichael Tuexen 			 * be fragmented at the IPv6 layer.
16665dff1c38SMichael Tuexen 			 */
16675dff1c38SMichael Tuexen 			if ((error == 0) &&
16685dff1c38SMichael Tuexen 			    (sopt->sopt_dir == SOPT_SET) &&
16695dff1c38SMichael Tuexen 			    (sopt->sopt_level == IPPROTO_IPV6) &&
16705dff1c38SMichael Tuexen 			    (sopt->sopt_name == IPV6_USE_MIN_MTU)) {
16715dff1c38SMichael Tuexen 				INP_WLOCK(inp);
16725dff1c38SMichael Tuexen 				if ((inp->inp_flags &
16735dff1c38SMichael Tuexen 				    (INP_TIMEWAIT | INP_DROPPED))) {
16745dff1c38SMichael Tuexen 					INP_WUNLOCK(inp);
16755dff1c38SMichael Tuexen 					return (ECONNRESET);
16765dff1c38SMichael Tuexen 				}
16775dff1c38SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_IPV6MINMTU;
16785dff1c38SMichael Tuexen 				tp = intotcpcb(inp);
16795dff1c38SMichael Tuexen 				if ((tp->t_state >= TCPS_SYN_SENT) &&
16805dff1c38SMichael Tuexen 				    (inp->inp_inc.inc_flags & INC_ISIPV6)) {
16815dff1c38SMichael Tuexen 					struct ip6_pktopts *opt;
16825dff1c38SMichael Tuexen 
16835dff1c38SMichael Tuexen 					opt = inp->in6p_outputopts;
16845dff1c38SMichael Tuexen 					if ((opt != NULL) &&
16855dff1c38SMichael Tuexen 					    (opt->ip6po_minmtu ==
16865dff1c38SMichael Tuexen 					    IP6PO_MINMTU_ALL)) {
16875dff1c38SMichael Tuexen 						if (tp->t_maxseg > TCP6_MSS) {
16885dff1c38SMichael Tuexen 							tp->t_maxseg = TCP6_MSS;
16895dff1c38SMichael Tuexen 						}
16905dff1c38SMichael Tuexen 					}
16915dff1c38SMichael Tuexen 				}
16925dff1c38SMichael Tuexen 				INP_WUNLOCK(inp);
16935dff1c38SMichael Tuexen 			}
1694b287c6c7SBjoern A. Zeeb 		}
1695fb59c426SYoshinobu Inoue #endif /* INET6 */
1696b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1697b287c6c7SBjoern A. Zeeb 		else
1698b287c6c7SBjoern A. Zeeb #endif
1699b287c6c7SBjoern A. Zeeb #ifdef INET
1700b287c6c7SBjoern A. Zeeb 		{
1701cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
17021e8f5ffaSRobert Watson 		}
17031e8f5ffaSRobert Watson #endif
1704df8bae1dSRodney W. Grimes 		return (error);
1705df8bae1dSRodney W. Grimes 	}
170668cea2b1SJohn Baldwin 	INP_WLOCK(inp);
1707ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
17088501a69cSRobert Watson 		INP_WUNLOCK(inp);
17091e8f5ffaSRobert Watson 		return (ECONNRESET);
1710623dce13SRobert Watson 	}
171155bceb1eSRandall Stewart 	tp = intotcpcb(inp);
171255bceb1eSRandall Stewart 	/*
171355bceb1eSRandall Stewart 	 * Protect the TCP option TCP_FUNCTION_BLK so
171455bceb1eSRandall Stewart 	 * that a sub-function can *never* overwrite this.
171555bceb1eSRandall Stewart 	 */
171655bceb1eSRandall Stewart 	if ((sopt->sopt_dir == SOPT_SET) &&
171755bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
171855bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
171955bceb1eSRandall Stewart 		error = sooptcopyin(sopt, &fsn, sizeof fsn,
172055bceb1eSRandall Stewart 		    sizeof fsn);
172155bceb1eSRandall Stewart 		if (error)
172255bceb1eSRandall Stewart 			return (error);
172355bceb1eSRandall Stewart 		INP_WLOCK_RECHECK(inp);
172455bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
172555bceb1eSRandall Stewart 		if (blk == NULL) {
172655bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
172755bceb1eSRandall Stewart 			return (ENOENT);
172855bceb1eSRandall Stewart 		}
1729587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1730587d67c0SRandall Stewart 			/* You already have this */
1731587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1732587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1733587d67c0SRandall Stewart 			return (0);
1734587d67c0SRandall Stewart 		}
1735587d67c0SRandall Stewart 		if (tp->t_state != TCPS_CLOSED) {
1736587d67c0SRandall Stewart 			/*
1737587d67c0SRandall Stewart 			 * The user has advanced the state
1738587d67c0SRandall Stewart 			 * past the initial point, we may not
1739587d67c0SRandall Stewart 			 * be able to switch.
1740587d67c0SRandall Stewart 			 */
1741587d67c0SRandall Stewart 			if (blk->tfb_tcp_handoff_ok != NULL) {
1742587d67c0SRandall Stewart 				/*
1743587d67c0SRandall Stewart 				 * Does the stack provide a
1744587d67c0SRandall Stewart 				 * query mechanism, if so it may
1745587d67c0SRandall Stewart 				 * still be possible?
1746587d67c0SRandall Stewart 				 */
1747587d67c0SRandall Stewart 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1748c6c0be27SMichael Tuexen 			} else
1749c6c0be27SMichael Tuexen 				error = EINVAL;
1750587d67c0SRandall Stewart 			if (error) {
1751587d67c0SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1752587d67c0SRandall Stewart 				INP_WUNLOCK(inp);
1753587d67c0SRandall Stewart 				return(error);
1754587d67c0SRandall Stewart 			}
1755587d67c0SRandall Stewart 		}
175655bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
175755bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
175855bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
175955bceb1eSRandall Stewart 			return (ENOENT);
176055bceb1eSRandall Stewart 		}
176155bceb1eSRandall Stewart 		/*
176255bceb1eSRandall Stewart 		 * Release the old refcnt, the
1763587d67c0SRandall Stewart 		 * lookup acquired a ref on the
1764587d67c0SRandall Stewart 		 * new one already.
176555bceb1eSRandall Stewart 		 */
1766587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1767587d67c0SRandall Stewart 			/*
1768587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1769587d67c0SRandall Stewart 			 * the tcb is not going away.
1770587d67c0SRandall Stewart 			 */
1771587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1772587d67c0SRandall Stewart 		}
17733ee9c3c4SRandall Stewart #ifdef TCPHPTS
17743ee9c3c4SRandall Stewart 		/* Assure that we are not on any hpts */
17753ee9c3c4SRandall Stewart 		tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
17763ee9c3c4SRandall Stewart #endif
17773ee9c3c4SRandall Stewart 		if (blk->tfb_tcp_fb_init) {
17783ee9c3c4SRandall Stewart 			error = (*blk->tfb_tcp_fb_init)(tp);
17793ee9c3c4SRandall Stewart 			if (error) {
17803ee9c3c4SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
17813ee9c3c4SRandall Stewart 				if (tp->t_fb->tfb_tcp_fb_init) {
17823ee9c3c4SRandall Stewart 					if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0)  {
17833ee9c3c4SRandall Stewart 						/* Fall back failed, drop the connection */
17843ee9c3c4SRandall Stewart 						INP_WUNLOCK(inp);
17853ee9c3c4SRandall Stewart 						soabort(so);
17863ee9c3c4SRandall Stewart 						return(error);
17873ee9c3c4SRandall Stewart 					}
17883ee9c3c4SRandall Stewart 				}
17893ee9c3c4SRandall Stewart 				goto err_out;
17903ee9c3c4SRandall Stewart 			}
17913ee9c3c4SRandall Stewart 		}
179255bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
179355bceb1eSRandall Stewart 		tp->t_fb = blk;
179455bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
179555bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
179655bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
179755bceb1eSRandall Stewart 			     sopt->sopt_name);
179855bceb1eSRandall Stewart 		}
179955bceb1eSRandall Stewart #endif
18003ee9c3c4SRandall Stewart err_out:
180155bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
180255bceb1eSRandall Stewart 		return (error);
180355bceb1eSRandall Stewart 	} else if ((sopt->sopt_dir == SOPT_GET) &&
180455bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1805c73b6f4dSEd Maste 		strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1806c73b6f4dSEd Maste 		    TCP_FUNCTION_NAME_LEN_MAX);
1807c73b6f4dSEd Maste 		fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
180855bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
180955bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
181055bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
181155bceb1eSRandall Stewart 		return (error);
181255bceb1eSRandall Stewart 	}
181355bceb1eSRandall Stewart 	/* Pass in the INP locked, called must unlock it */
181455bceb1eSRandall Stewart 	return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
181555bceb1eSRandall Stewart }
181655bceb1eSRandall Stewart 
18172529f56eSJonathan T. Looney /*
18182529f56eSJonathan T. Looney  * If this assert becomes untrue, we need to change the size of the buf
18192529f56eSJonathan T. Looney  * variable in tcp_default_ctloutput().
18202529f56eSJonathan T. Looney  */
18212529f56eSJonathan T. Looney #ifdef CTASSERT
18222529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
18232529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
18242529f56eSJonathan T. Looney #endif
18252529f56eSJonathan T. Looney 
1826ec1db6e1SJohn Baldwin #ifdef KERN_TLS
1827ec1db6e1SJohn Baldwin static int
1828ec1db6e1SJohn Baldwin copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1829ec1db6e1SJohn Baldwin {
1830ec1db6e1SJohn Baldwin 	struct tls_enable_v0 tls_v0;
1831ec1db6e1SJohn Baldwin 	int error;
1832ec1db6e1SJohn Baldwin 
1833ec1db6e1SJohn Baldwin 	if (sopt->sopt_valsize == sizeof(tls_v0)) {
1834ec1db6e1SJohn Baldwin 		error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1835ec1db6e1SJohn Baldwin 		    sizeof(tls_v0));
1836ec1db6e1SJohn Baldwin 		if (error)
1837ec1db6e1SJohn Baldwin 			return (error);
1838ec1db6e1SJohn Baldwin 		memset(tls, 0, sizeof(*tls));
1839ec1db6e1SJohn Baldwin 		tls->cipher_key = tls_v0.cipher_key;
1840ec1db6e1SJohn Baldwin 		tls->iv = tls_v0.iv;
1841ec1db6e1SJohn Baldwin 		tls->auth_key = tls_v0.auth_key;
1842ec1db6e1SJohn Baldwin 		tls->cipher_algorithm = tls_v0.cipher_algorithm;
1843ec1db6e1SJohn Baldwin 		tls->cipher_key_len = tls_v0.cipher_key_len;
1844ec1db6e1SJohn Baldwin 		tls->iv_len = tls_v0.iv_len;
1845ec1db6e1SJohn Baldwin 		tls->auth_algorithm = tls_v0.auth_algorithm;
1846ec1db6e1SJohn Baldwin 		tls->auth_key_len = tls_v0.auth_key_len;
1847ec1db6e1SJohn Baldwin 		tls->flags = tls_v0.flags;
1848ec1db6e1SJohn Baldwin 		tls->tls_vmajor = tls_v0.tls_vmajor;
1849ec1db6e1SJohn Baldwin 		tls->tls_vminor = tls_v0.tls_vminor;
1850ec1db6e1SJohn Baldwin 		return (0);
1851ec1db6e1SJohn Baldwin 	}
1852ec1db6e1SJohn Baldwin 
1853ec1db6e1SJohn Baldwin 	return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1854ec1db6e1SJohn Baldwin }
1855ec1db6e1SJohn Baldwin #endif
1856ec1db6e1SJohn Baldwin 
185755bceb1eSRandall Stewart int
185855bceb1eSRandall Stewart tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
185955bceb1eSRandall Stewart {
186055bceb1eSRandall Stewart 	int	error, opt, optval;
186155bceb1eSRandall Stewart 	u_int	ui;
186255bceb1eSRandall Stewart 	struct	tcp_info ti;
1863b2e60773SJohn Baldwin #ifdef KERN_TLS
1864b2e60773SJohn Baldwin 	struct tls_enable tls;
1865b2e60773SJohn Baldwin #endif
186655bceb1eSRandall Stewart 	struct cc_algo *algo;
18672529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
1868adc56f5aSEdward Tomasz Napierala #ifdef STATS
1869adc56f5aSEdward Tomasz Napierala 	struct statsblob *sbp;
1870adc56f5aSEdward Tomasz Napierala #endif
1871af6fef3aSGleb Smirnoff 	size_t	len;
1872df8bae1dSRodney W. Grimes 
1873d519cedbSGleb Smirnoff 	/*
1874d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
1875d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
1876d519cedbSGleb Smirnoff 	 */
1877d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
1878d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
1879d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1880c8b53cedSMichael Tuexen 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
1881c8b53cedSMichael Tuexen 			return (EINVAL);
1882af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1883af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1884d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
1885d519cedbSGleb Smirnoff 		if (error) {
1886af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
1887d519cedbSGleb Smirnoff 			return (error);
1888d519cedbSGleb Smirnoff 		}
1889bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1890d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
1891af6fef3aSGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1892d519cedbSGleb Smirnoff 		else
1893d519cedbSGleb Smirnoff 			error = ENOENT;
1894d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1895d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
1896af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1897af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
1898d519cedbSGleb Smirnoff 		return (error);
1899d519cedbSGleb Smirnoff 	}
1900d519cedbSGleb Smirnoff 
1901cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1902cfe8b629SGarrett Wollman 	case SOPT_SET:
1903cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1904fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
190588f6b043SBruce M Simpson 		case TCP_MD5SIG:
1906fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
19078501a69cSRobert Watson 				INP_WUNLOCK(inp);
1908fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
1909fcf59617SAndrey V. Elsukov 			}
1910fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
19111cfd4b53SBruce M Simpson 			if (error)
19121e8f5ffaSRobert Watson 				return (error);
191309fe6320SNavdeep Parhar 			goto unlock_and_done;
1914fcf59617SAndrey V. Elsukov #endif /* IPSEC */
191509fe6320SNavdeep Parhar 
1916df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1917cfe8b629SGarrett Wollman 		case TCP_NOOPT:
19188501a69cSRobert Watson 			INP_WUNLOCK(inp);
1919cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1920cfe8b629SGarrett Wollman 			    sizeof optval);
1921cfe8b629SGarrett Wollman 			if (error)
19221e8f5ffaSRobert Watson 				return (error);
1923cfe8b629SGarrett Wollman 
19248501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1925cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1926cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1927cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1928cfe8b629SGarrett Wollman 				break;
1929cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1930cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1931cfe8b629SGarrett Wollman 				break;
1932cfe8b629SGarrett Wollman 			default:
1933cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1934cfe8b629SGarrett Wollman 				break;
1935cfe8b629SGarrett Wollman 			}
1936cfe8b629SGarrett Wollman 
1937cfe8b629SGarrett Wollman 			if (optval)
1938cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1939df8bae1dSRodney W. Grimes 			else
1940cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
194109fe6320SNavdeep Parhar unlock_and_done:
194209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
194309fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
194409fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
194509fe6320SNavdeep Parhar 				    sopt->sopt_name);
194609fe6320SNavdeep Parhar 			}
194709fe6320SNavdeep Parhar #endif
19488501a69cSRobert Watson 			INP_WUNLOCK(inp);
1949df8bae1dSRodney W. Grimes 			break;
1950df8bae1dSRodney W. Grimes 
1951007581c0SJonathan Lemon 		case TCP_NOPUSH:
19528501a69cSRobert Watson 			INP_WUNLOCK(inp);
1953007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1954007581c0SJonathan Lemon 			    sizeof optval);
1955007581c0SJonathan Lemon 			if (error)
19561e8f5ffaSRobert Watson 				return (error);
1957007581c0SJonathan Lemon 
19588501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1959007581c0SJonathan Lemon 			if (optval)
1960007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1961d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
1962007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1963109eb549SGleb Smirnoff 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
1964109eb549SGleb Smirnoff 					struct epoch_tracker et;
1965109eb549SGleb Smirnoff 
1966109eb549SGleb Smirnoff 					NET_EPOCH_ENTER(et);
196755bceb1eSRandall Stewart 					error = tp->t_fb->tfb_tcp_output(tp);
1968109eb549SGleb Smirnoff 					NET_EPOCH_EXIT(et);
1969109eb549SGleb Smirnoff 				}
1970007581c0SJonathan Lemon 			}
197109fe6320SNavdeep Parhar 			goto unlock_and_done;
1972007581c0SJonathan Lemon 
1973df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
19748501a69cSRobert Watson 			INP_WUNLOCK(inp);
1975cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1976cfe8b629SGarrett Wollman 			    sizeof optval);
1977cfe8b629SGarrett Wollman 			if (error)
19781e8f5ffaSRobert Watson 				return (error);
1979df8bae1dSRodney W. Grimes 
19808501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
198153369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
1982603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
1983cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1984a0292f23SGarrett Wollman 			else
1985a0292f23SGarrett Wollman 				error = EINVAL;
198609fe6320SNavdeep Parhar 			goto unlock_and_done;
1987a0292f23SGarrett Wollman 
1988b8af5dfaSRobert Watson 		case TCP_INFO:
19898501a69cSRobert Watson 			INP_WUNLOCK(inp);
1990b8af5dfaSRobert Watson 			error = EINVAL;
1991b8af5dfaSRobert Watson 			break;
1992b8af5dfaSRobert Watson 
1993adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
1994adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
1995adc56f5aSEdward Tomasz Napierala #ifdef STATS
1996adc56f5aSEdward Tomasz Napierala 			error = sooptcopyin(sopt, &optval, sizeof optval,
1997adc56f5aSEdward Tomasz Napierala 			    sizeof optval);
1998adc56f5aSEdward Tomasz Napierala 			if (error)
1999adc56f5aSEdward Tomasz Napierala 				return (error);
2000adc56f5aSEdward Tomasz Napierala 
2001adc56f5aSEdward Tomasz Napierala 			if (optval > 0)
2002adc56f5aSEdward Tomasz Napierala 				sbp = stats_blob_alloc(
2003adc56f5aSEdward Tomasz Napierala 				    V_tcp_perconn_stats_dflt_tpl, 0);
2004adc56f5aSEdward Tomasz Napierala 			else
2005adc56f5aSEdward Tomasz Napierala 				sbp = NULL;
2006adc56f5aSEdward Tomasz Napierala 
2007adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2008adc56f5aSEdward Tomasz Napierala 			if ((tp->t_stats != NULL && sbp == NULL) ||
2009adc56f5aSEdward Tomasz Napierala 			    (tp->t_stats == NULL && sbp != NULL)) {
2010adc56f5aSEdward Tomasz Napierala 				struct statsblob *t = tp->t_stats;
2011adc56f5aSEdward Tomasz Napierala 				tp->t_stats = sbp;
2012adc56f5aSEdward Tomasz Napierala 				sbp = t;
2013adc56f5aSEdward Tomasz Napierala 			}
2014adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2015adc56f5aSEdward Tomasz Napierala 
2016adc56f5aSEdward Tomasz Napierala 			stats_blob_destroy(sbp);
2017adc56f5aSEdward Tomasz Napierala #else
2018adc56f5aSEdward Tomasz Napierala 			return (EOPNOTSUPP);
2019adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2020adc56f5aSEdward Tomasz Napierala 			break;
2021adc56f5aSEdward Tomasz Napierala 
2022dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2023dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2024af6fef3aSGleb Smirnoff 			error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
2025af6fef3aSGleb Smirnoff 			if (error)
2026dbc42409SLawrence Stewart 				break;
2027af6fef3aSGleb Smirnoff 			buf[sopt->sopt_valsize] = '\0';
2028af6fef3aSGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
202973e263b1SGleb Smirnoff 			CC_LIST_RLOCK();
203073e263b1SGleb Smirnoff 			STAILQ_FOREACH(algo, &cc_list, entries)
203173e263b1SGleb Smirnoff 				if (strncmp(buf, algo->name,
203273e263b1SGleb Smirnoff 				    TCP_CA_NAME_MAX) == 0)
203373e263b1SGleb Smirnoff 					break;
203473e263b1SGleb Smirnoff 			CC_LIST_RUNLOCK();
203573e263b1SGleb Smirnoff 			if (algo == NULL) {
2036af6fef3aSGleb Smirnoff 				INP_WUNLOCK(inp);
203773e263b1SGleb Smirnoff 				error = EINVAL;
203873e263b1SGleb Smirnoff 				break;
203973e263b1SGleb Smirnoff 			}
2040dbc42409SLawrence Stewart 			/*
204173e263b1SGleb Smirnoff 			 * We hold a write lock over the tcb so it's safe to
204273e263b1SGleb Smirnoff 			 * do these things without ordering concerns.
2043dbc42409SLawrence Stewart 			 */
2044dbc42409SLawrence Stewart 			if (CC_ALGO(tp)->cb_destroy != NULL)
2045dbc42409SLawrence Stewart 				CC_ALGO(tp)->cb_destroy(tp->ccv);
204622699887SMatt Macy 			CC_DATA(tp) = NULL;
2047dbc42409SLawrence Stewart 			CC_ALGO(tp) = algo;
2048dbc42409SLawrence Stewart 			/*
204973e263b1SGleb Smirnoff 			 * If something goes pear shaped initialising the new
205073e263b1SGleb Smirnoff 			 * algo, fall back to newreno (which does not
205173e263b1SGleb Smirnoff 			 * require initialisation).
2052dbc42409SLawrence Stewart 			 */
205373e263b1SGleb Smirnoff 			if (algo->cb_init != NULL &&
205473e263b1SGleb Smirnoff 			    algo->cb_init(tp->ccv) != 0) {
2055dbc42409SLawrence Stewart 				CC_ALGO(tp) = &newreno_cc_algo;
2056dbc42409SLawrence Stewart 				/*
205773e263b1SGleb Smirnoff 				 * The only reason init should fail is
2058dbc42409SLawrence Stewart 				 * because of malloc.
2059dbc42409SLawrence Stewart 				 */
2060dbc42409SLawrence Stewart 				error = ENOMEM;
2061dbc42409SLawrence Stewart 			}
206273e263b1SGleb Smirnoff 			INP_WUNLOCK(inp);
206373e263b1SGleb Smirnoff 			break;
2064dbc42409SLawrence Stewart 
2065b2e60773SJohn Baldwin #ifdef KERN_TLS
2066b2e60773SJohn Baldwin 		case TCP_TXTLS_ENABLE:
2067b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2068ec1db6e1SJohn Baldwin 			error = copyin_tls_enable(sopt, &tls);
2069b2e60773SJohn Baldwin 			if (error)
2070b2e60773SJohn Baldwin 				break;
2071b2e60773SJohn Baldwin 			error = ktls_enable_tx(so, &tls);
2072b2e60773SJohn Baldwin 			break;
2073b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2074b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2075b2e60773SJohn Baldwin 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2076b2e60773SJohn Baldwin 			if (error)
2077b2e60773SJohn Baldwin 				return (error);
2078b2e60773SJohn Baldwin 
2079b2e60773SJohn Baldwin 			INP_WLOCK_RECHECK(inp);
2080b2e60773SJohn Baldwin 			error = ktls_set_tx_mode(so, ui);
2081b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2082b2e60773SJohn Baldwin 			break;
2083*f1f93475SJohn Baldwin 		case TCP_RXTLS_ENABLE:
2084*f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2085*f1f93475SJohn Baldwin 			error = sooptcopyin(sopt, &tls, sizeof(tls),
2086*f1f93475SJohn Baldwin 			    sizeof(tls));
2087*f1f93475SJohn Baldwin 			if (error)
2088*f1f93475SJohn Baldwin 				break;
2089*f1f93475SJohn Baldwin 			error = ktls_enable_rx(so, &tls);
2090*f1f93475SJohn Baldwin 			break;
2091b2e60773SJohn Baldwin #endif
2092b2e60773SJohn Baldwin 
20939077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
20949077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
20959077f387SGleb Smirnoff 		case TCP_KEEPINIT:
20969077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
20979077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
20989077f387SGleb Smirnoff 			if (error)
20999077f387SGleb Smirnoff 				return (error);
21009077f387SGleb Smirnoff 
21019077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
21029077f387SGleb Smirnoff 				error = EINVAL;
21039077f387SGleb Smirnoff 				break;
21049077f387SGleb Smirnoff 			}
21059077f387SGleb Smirnoff 			ui *= hz;
21069077f387SGleb Smirnoff 
21079077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
21089077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
21099077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
21109077f387SGleb Smirnoff 				tp->t_keepidle = ui;
21119077f387SGleb Smirnoff 				/*
21129077f387SGleb Smirnoff 				 * XXX: better check current remaining
21139077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
21149077f387SGleb Smirnoff 				 */
21159077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
21169077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
21179077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
21189077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
21199077f387SGleb Smirnoff 				break;
21209077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
21219077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
21229077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
21239077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
21249077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
21259077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
21269077f387SGleb Smirnoff 				break;
21279077f387SGleb Smirnoff 			case TCP_KEEPINIT:
21289077f387SGleb Smirnoff 				tp->t_keepinit = ui;
21299077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
21309077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
21319077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
21329077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
21339077f387SGleb Smirnoff 				break;
21349077f387SGleb Smirnoff 			}
213509fe6320SNavdeep Parhar 			goto unlock_and_done;
21369077f387SGleb Smirnoff 
213785c05144SGleb Smirnoff 		case TCP_KEEPCNT:
213885c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
213985c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
214085c05144SGleb Smirnoff 			if (error)
214185c05144SGleb Smirnoff 				return (error);
214285c05144SGleb Smirnoff 
214385c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
214485c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
214585c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
214685c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
214785c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
214885c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
214985c05144SGleb Smirnoff 			goto unlock_and_done;
215085c05144SGleb Smirnoff 
215186a996e6SHiren Panchasara #ifdef TCPPCAP
215286a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
215386a996e6SHiren Panchasara 		case TCP_PCAP_IN:
215486a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
215586a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
215686a996e6SHiren Panchasara 			    sizeof optval);
215786a996e6SHiren Panchasara 			if (error)
215886a996e6SHiren Panchasara 				return (error);
215986a996e6SHiren Panchasara 
216086a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
216186a996e6SHiren Panchasara 			if (optval >= 0)
216286a996e6SHiren Panchasara 				tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
216386a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
216486a996e6SHiren Panchasara 					optval);
216586a996e6SHiren Panchasara 			else
216686a996e6SHiren Panchasara 				error = EINVAL;
216786a996e6SHiren Panchasara 			goto unlock_and_done;
216886a996e6SHiren Panchasara #endif
216986a996e6SHiren Panchasara 
2170c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
2171c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
2172c560df6fSPatrick Kelsey 
2173281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2174c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
2175c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
2176281a0fd4SPatrick Kelsey 				return (EPERM);
2177281a0fd4SPatrick Kelsey 
2178c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
2179c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
2180281a0fd4SPatrick Kelsey 			if (error)
2181281a0fd4SPatrick Kelsey 				return (error);
2182281a0fd4SPatrick Kelsey 
2183281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
2184c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
2185c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
2186c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
2187c560df6fSPatrick Kelsey 						error = EPERM;
2188c560df6fSPatrick Kelsey 						goto unlock_and_done;
2189c560df6fSPatrick Kelsey 					}
2190c560df6fSPatrick Kelsey 
2191281a0fd4SPatrick Kelsey 					tp->t_flags |= TF_FASTOPEN;
2192c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
2193281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
2194281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
2195c560df6fSPatrick Kelsey 				} else {
2196c560df6fSPatrick Kelsey 					/*
2197c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
2198c560df6fSPatrick Kelsey 					 * stash it in the client cookie
2199c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
2200c560df6fSPatrick Kelsey 					 * connect.
2201c560df6fSPatrick Kelsey 					 */
2202c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
2203c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
2204c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
2205c560df6fSPatrick Kelsey 						       tfo_optval.psk,
2206c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
2207c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
2208c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
2209c560df6fSPatrick Kelsey 					}
2210c560df6fSPatrick Kelsey 					tp->t_flags |= TF_FASTOPEN;
2211c560df6fSPatrick Kelsey 				}
2212281a0fd4SPatrick Kelsey 			} else
2213281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
2214281a0fd4SPatrick Kelsey 			goto unlock_and_done;
2215c560df6fSPatrick Kelsey 		}
2216281a0fd4SPatrick Kelsey 
2217e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
22182529f56eSJonathan T. Looney 		case TCP_LOG:
22192529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
22202529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
22212529f56eSJonathan T. Looney 			    sizeof optval);
22222529f56eSJonathan T. Looney 			if (error)
22232529f56eSJonathan T. Looney 				return (error);
22242529f56eSJonathan T. Looney 
22252529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
22262529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
22272529f56eSJonathan T. Looney 			goto unlock_and_done;
22282529f56eSJonathan T. Looney 
22292529f56eSJonathan T. Looney 		case TCP_LOGBUF:
22302529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
22312529f56eSJonathan T. Looney 			error = EINVAL;
22322529f56eSJonathan T. Looney 			break;
22332529f56eSJonathan T. Looney 
22342529f56eSJonathan T. Looney 		case TCP_LOGID:
22352529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
22362529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
22372529f56eSJonathan T. Looney 			if (error)
22382529f56eSJonathan T. Looney 				break;
22392529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
22402529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
22412529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
22422529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
22432529f56eSJonathan T. Looney 			break;
22442529f56eSJonathan T. Looney 
22452529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
22462529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
22472529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
22482529f56eSJonathan T. Looney 			error =
22492529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
22502529f56eSJonathan T. Looney 			if (error)
22512529f56eSJonathan T. Looney 				break;
22522529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
22532529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
22542529f56eSJonathan T. Looney 			if (sopt->sopt_name == TCP_LOGDUMP) {
22552529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
22562529f56eSJonathan T. Looney 				    M_WAITOK, true);
22572529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
22582529f56eSJonathan T. Looney 			} else {
22592529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
22602529f56eSJonathan T. Looney 				/*
22612529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
22622529f56eSJonathan T. Looney 				 * INP lock.
22632529f56eSJonathan T. Looney 				 */
22642529f56eSJonathan T. Looney 			}
22652529f56eSJonathan T. Looney 			break;
2266e24e5683SJonathan T. Looney #endif
22672529f56eSJonathan T. Looney 
2268df8bae1dSRodney W. Grimes 		default:
22698501a69cSRobert Watson 			INP_WUNLOCK(inp);
2270df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2271df8bae1dSRodney W. Grimes 			break;
2272df8bae1dSRodney W. Grimes 		}
2273df8bae1dSRodney W. Grimes 		break;
2274df8bae1dSRodney W. Grimes 
2275cfe8b629SGarrett Wollman 	case SOPT_GET:
22761e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
2277cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2278fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
227988f6b043SBruce M Simpson 		case TCP_MD5SIG:
2280fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
22818501a69cSRobert Watson 				INP_WUNLOCK(inp);
2282fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2283fcf59617SAndrey V. Elsukov 			}
2284fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
22851cfd4b53SBruce M Simpson 			break;
2286265ed012SBruce M Simpson #endif
22871e8f5ffaSRobert Watson 
2288df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2289cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
22908501a69cSRobert Watson 			INP_WUNLOCK(inp);
2291b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2292df8bae1dSRodney W. Grimes 			break;
2293df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
2294cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
22958501a69cSRobert Watson 			INP_WUNLOCK(inp);
2296b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2297df8bae1dSRodney W. Grimes 			break;
2298a0292f23SGarrett Wollman 		case TCP_NOOPT:
2299cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
23008501a69cSRobert Watson 			INP_WUNLOCK(inp);
2301b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2302a0292f23SGarrett Wollman 			break;
2303a0292f23SGarrett Wollman 		case TCP_NOPUSH:
2304cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
23058501a69cSRobert Watson 			INP_WUNLOCK(inp);
2306b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2307b8af5dfaSRobert Watson 			break;
2308b8af5dfaSRobert Watson 		case TCP_INFO:
2309b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
23108501a69cSRobert Watson 			INP_WUNLOCK(inp);
2311b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
2312a0292f23SGarrett Wollman 			break;
2313adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2314adc56f5aSEdward Tomasz Napierala 			{
2315adc56f5aSEdward Tomasz Napierala #ifdef STATS
2316adc56f5aSEdward Tomasz Napierala 			int nheld;
2317adc56f5aSEdward Tomasz Napierala 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2318adc56f5aSEdward Tomasz Napierala 
2319adc56f5aSEdward Tomasz Napierala 			error = 0;
2320adc56f5aSEdward Tomasz Napierala 			socklen_t outsbsz = sopt->sopt_valsize;
2321adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats == NULL)
2322adc56f5aSEdward Tomasz Napierala 				error = ENOENT;
2323adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= tp->t_stats->cursz)
2324adc56f5aSEdward Tomasz Napierala 				outsbsz = tp->t_stats->cursz;
2325adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= sizeof(struct statsblob))
2326adc56f5aSEdward Tomasz Napierala 				outsbsz = sizeof(struct statsblob);
2327adc56f5aSEdward Tomasz Napierala 			else
2328adc56f5aSEdward Tomasz Napierala 				error = EINVAL;
2329adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2330adc56f5aSEdward Tomasz Napierala 			if (error)
2331adc56f5aSEdward Tomasz Napierala 				break;
2332adc56f5aSEdward Tomasz Napierala 
2333adc56f5aSEdward Tomasz Napierala 			sbp = sopt->sopt_val;
2334adc56f5aSEdward Tomasz Napierala 			nheld = atop(round_page(((vm_offset_t)sbp) +
2335adc56f5aSEdward Tomasz Napierala 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2336adc56f5aSEdward Tomasz Napierala 			vm_page_t ma[nheld];
2337adc56f5aSEdward Tomasz Napierala 			if (vm_fault_quick_hold_pages(
2338adc56f5aSEdward Tomasz Napierala 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2339adc56f5aSEdward Tomasz Napierala 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2340adc56f5aSEdward Tomasz Napierala 			    nheld) < 0) {
2341adc56f5aSEdward Tomasz Napierala 				error = EFAULT;
2342adc56f5aSEdward Tomasz Napierala 				break;
2343adc56f5aSEdward Tomasz Napierala 			}
2344adc56f5aSEdward Tomasz Napierala 
2345adc56f5aSEdward Tomasz Napierala 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2346adc56f5aSEdward Tomasz Napierala 			    SIZEOF_MEMBER(struct statsblob, flags))))
2347adc56f5aSEdward Tomasz Napierala 				goto unhold;
2348adc56f5aSEdward Tomasz Napierala 
2349adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2350adc56f5aSEdward Tomasz Napierala 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2351adc56f5aSEdward Tomasz Napierala 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2352adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2353adc56f5aSEdward Tomasz Napierala 			sopt->sopt_valsize = outsbsz;
2354adc56f5aSEdward Tomasz Napierala unhold:
2355adc56f5aSEdward Tomasz Napierala 			vm_page_unhold_pages(ma, nheld);
2356adc56f5aSEdward Tomasz Napierala #else
2357adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2358adc56f5aSEdward Tomasz Napierala 			error = EOPNOTSUPP;
2359adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2360adc56f5aSEdward Tomasz Napierala 			break;
2361adc56f5aSEdward Tomasz Napierala 			}
2362dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2363af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2364dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2365af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
2366dbc42409SLawrence Stewart 			break;
23672f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
23682f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
23692f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
23702f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
23712f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
23722f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
23735a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
23742f3eb7f4SGleb Smirnoff 				break;
23752f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
23765a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
23772f3eb7f4SGleb Smirnoff 				break;
23782f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
23795a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
23802f3eb7f4SGleb Smirnoff 				break;
23812f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
23825a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
23832f3eb7f4SGleb Smirnoff 				break;
23842f3eb7f4SGleb Smirnoff 			}
23852f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
23862f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
23872f3eb7f4SGleb Smirnoff 			break;
238886a996e6SHiren Panchasara #ifdef TCPPCAP
238986a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
239086a996e6SHiren Panchasara 		case TCP_PCAP_IN:
239186a996e6SHiren Panchasara 			optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
239286a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
239386a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
239486a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
239586a996e6SHiren Panchasara 			break;
239686a996e6SHiren Panchasara #endif
2397281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
2398281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
2399281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2400281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
2401281a0fd4SPatrick Kelsey 			break;
2402e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
24032529f56eSJonathan T. Looney 		case TCP_LOG:
24042529f56eSJonathan T. Looney 			optval = tp->t_logstate;
24052529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24062529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
24072529f56eSJonathan T. Looney 			break;
24082529f56eSJonathan T. Looney 		case TCP_LOGBUF:
24092529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
24102529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
24112529f56eSJonathan T. Looney 			break;
24122529f56eSJonathan T. Looney 		case TCP_LOGID:
24132529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
24142529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24152529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
24162529f56eSJonathan T. Looney 			break;
24172529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
24182529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
24192529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24202529f56eSJonathan T. Looney 			error = EINVAL;
24212529f56eSJonathan T. Looney 			break;
2422e24e5683SJonathan T. Looney #endif
2423b2e60773SJohn Baldwin #ifdef KERN_TLS
2424b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2425b2e60773SJohn Baldwin 			optval = ktls_get_tx_mode(so);
2426b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2427b2e60773SJohn Baldwin 			error = sooptcopyout(sopt, &optval, sizeof(optval));
2428b2e60773SJohn Baldwin 			break;
2429*f1f93475SJohn Baldwin 		case TCP_RXTLS_MODE:
2430*f1f93475SJohn Baldwin 			optval = ktls_get_rx_mode(so);
2431*f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2432*f1f93475SJohn Baldwin 			error = sooptcopyout(sopt, &optval, sizeof(optval));
2433*f1f93475SJohn Baldwin 			break;
2434b2e60773SJohn Baldwin #endif
2435df8bae1dSRodney W. Grimes 		default:
24368501a69cSRobert Watson 			INP_WUNLOCK(inp);
2437df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2438df8bae1dSRodney W. Grimes 			break;
2439df8bae1dSRodney W. Grimes 		}
2440df8bae1dSRodney W. Grimes 		break;
2441df8bae1dSRodney W. Grimes 	}
2442df8bae1dSRodney W. Grimes 	return (error);
2443df8bae1dSRodney W. Grimes }
24448501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2445bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2446df8bae1dSRodney W. Grimes 
244726e30fbbSDavid Greenman /*
2448df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2449df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2450df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2451df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2452df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2453df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2454df8bae1dSRodney W. Grimes  */
2455623dce13SRobert Watson static void
2456ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2457df8bae1dSRodney W. Grimes {
2458e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
2459e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
2460e6e0b5ffSRobert Watson 
246197a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
24628501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2463df8bae1dSRodney W. Grimes 
2464623dce13SRobert Watson 	/*
2465623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2466623dce13SRobert Watson 	 * socket is still open.
2467623dce13SRobert Watson 	 */
24688db239dcSMichael Tuexen 	if (tp->t_state < TCPS_ESTABLISHED &&
24698db239dcSMichael Tuexen 	    !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2470df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2471623dce13SRobert Watson 		KASSERT(tp != NULL,
2472623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2473623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2474243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2475623dce13SRobert Watson 		KASSERT(tp != NULL,
2476623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2477623dce13SRobert Watson 	} else {
2478df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2479df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2480623dce13SRobert Watson 		tcp_usrclosed(tp);
2481ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
248255bceb1eSRandall Stewart 			tp->t_fb->tfb_tcp_output(tp);
2483df8bae1dSRodney W. Grimes 	}
2484df8bae1dSRodney W. Grimes }
2485df8bae1dSRodney W. Grimes 
2486df8bae1dSRodney W. Grimes /*
2487df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2488df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2489df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2490df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2491df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2492df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2493df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2494df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2495df8bae1dSRodney W. Grimes  */
2496623dce13SRobert Watson static void
2497ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2498df8bae1dSRodney W. Grimes {
2499df8bae1dSRodney W. Grimes 
250097a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
25018501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
2502e6e0b5ffSRobert Watson 
2503df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2504df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
250509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
250609fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
250709fe6320SNavdeep Parhar #endif
2508550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2509bc65987aSKip Macy 		/* FALLTHROUGH */
2510bc65987aSKip Macy 	case TCPS_CLOSED:
2511df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2512623dce13SRobert Watson 		/*
2513623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2514623dce13SRobert Watson 		 * still open.
2515623dce13SRobert Watson 		 */
2516623dce13SRobert Watson 		KASSERT(tp != NULL,
2517623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2518df8bae1dSRodney W. Grimes 		break;
2519df8bae1dSRodney W. Grimes 
2520a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2521df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2522a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2523a0292f23SGarrett Wollman 		break;
2524a0292f23SGarrett Wollman 
2525df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
252657f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2527df8bae1dSRodney W. Grimes 		break;
2528df8bae1dSRodney W. Grimes 
2529df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
253057f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2531df8bae1dSRodney W. Grimes 		break;
2532df8bae1dSRodney W. Grimes 	}
2533abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
2534df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
2535abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
25367c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
25377c72af87SMohan Srinivasan 			int timeout;
25387c72af87SMohan Srinivasan 
25397c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
25409077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2541b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2542b6239c4aSAndras Olah 		}
2543df8bae1dSRodney W. Grimes 	}
25447c72af87SMohan Srinivasan }
2545497057eeSRobert Watson 
2546497057eeSRobert Watson #ifdef DDB
2547497057eeSRobert Watson static void
2548497057eeSRobert Watson db_print_indent(int indent)
2549497057eeSRobert Watson {
2550497057eeSRobert Watson 	int i;
2551497057eeSRobert Watson 
2552497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2553497057eeSRobert Watson 		db_printf(" ");
2554497057eeSRobert Watson }
2555497057eeSRobert Watson 
2556497057eeSRobert Watson static void
2557497057eeSRobert Watson db_print_tstate(int t_state)
2558497057eeSRobert Watson {
2559497057eeSRobert Watson 
2560497057eeSRobert Watson 	switch (t_state) {
2561497057eeSRobert Watson 	case TCPS_CLOSED:
2562497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2563497057eeSRobert Watson 		return;
2564497057eeSRobert Watson 
2565497057eeSRobert Watson 	case TCPS_LISTEN:
2566497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2567497057eeSRobert Watson 		return;
2568497057eeSRobert Watson 
2569497057eeSRobert Watson 	case TCPS_SYN_SENT:
2570497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2571497057eeSRobert Watson 		return;
2572497057eeSRobert Watson 
2573497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2574497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2575497057eeSRobert Watson 		return;
2576497057eeSRobert Watson 
2577497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2578497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2579497057eeSRobert Watson 		return;
2580497057eeSRobert Watson 
2581497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2582497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2583497057eeSRobert Watson 		return;
2584497057eeSRobert Watson 
2585497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2586497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2587497057eeSRobert Watson 		return;
2588497057eeSRobert Watson 
2589497057eeSRobert Watson 	case TCPS_CLOSING:
2590497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2591497057eeSRobert Watson 		return;
2592497057eeSRobert Watson 
2593497057eeSRobert Watson 	case TCPS_LAST_ACK:
2594497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2595497057eeSRobert Watson 		return;
2596497057eeSRobert Watson 
2597497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2598497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2599497057eeSRobert Watson 		return;
2600497057eeSRobert Watson 
2601497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2602497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2603497057eeSRobert Watson 		return;
2604497057eeSRobert Watson 
2605497057eeSRobert Watson 	default:
2606497057eeSRobert Watson 		db_printf("unknown");
2607497057eeSRobert Watson 		return;
2608497057eeSRobert Watson 	}
2609497057eeSRobert Watson }
2610497057eeSRobert Watson 
2611497057eeSRobert Watson static void
2612497057eeSRobert Watson db_print_tflags(u_int t_flags)
2613497057eeSRobert Watson {
2614497057eeSRobert Watson 	int comma;
2615497057eeSRobert Watson 
2616497057eeSRobert Watson 	comma = 0;
2617497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2618497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2619497057eeSRobert Watson 		comma = 1;
2620497057eeSRobert Watson 	}
2621497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2622497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2623497057eeSRobert Watson 		comma = 1;
2624497057eeSRobert Watson 	}
2625497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2626497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2627497057eeSRobert Watson 		comma = 1;
2628497057eeSRobert Watson 	}
2629497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2630497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2631497057eeSRobert Watson 		comma = 1;
2632497057eeSRobert Watson 	}
2633497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2634497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2635497057eeSRobert Watson 		comma = 1;
2636497057eeSRobert Watson 	}
2637497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2638497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2639497057eeSRobert Watson 		comma = 1;
2640497057eeSRobert Watson 	}
2641497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2642497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2643497057eeSRobert Watson 		comma = 1;
2644497057eeSRobert Watson 	}
2645497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2646497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2647497057eeSRobert Watson 		comma = 1;
2648497057eeSRobert Watson 	}
2649497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2650497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2651497057eeSRobert Watson 		comma = 1;
2652497057eeSRobert Watson 	}
2653497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2654497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2655497057eeSRobert Watson 		comma = 1;
2656497057eeSRobert Watson 	}
2657497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2658497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2659497057eeSRobert Watson 		comma = 1;
2660497057eeSRobert Watson 	}
2661497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2662497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2663497057eeSRobert Watson 		comma = 1;
2664497057eeSRobert Watson 	}
2665497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2666497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2667497057eeSRobert Watson 		comma = 1;
2668497057eeSRobert Watson 	}
2669497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2670497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2671497057eeSRobert Watson 		comma = 1;
2672497057eeSRobert Watson 	}
2673497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
2674497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2675497057eeSRobert Watson 		comma = 1;
2676497057eeSRobert Watson 	}
2677497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2678497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2679497057eeSRobert Watson 		comma = 1;
2680497057eeSRobert Watson 	}
2681497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2682497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2683497057eeSRobert Watson 		comma = 1;
2684497057eeSRobert Watson 	}
2685497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2686497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2687497057eeSRobert Watson 		comma = 1;
2688497057eeSRobert Watson 	}
2689dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
2690dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2691dbc42409SLawrence Stewart 		comma = 1;
2692dbc42409SLawrence Stewart 	}
2693497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2694497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2695497057eeSRobert Watson 		comma = 1;
2696497057eeSRobert Watson 	}
2697497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2698497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2699497057eeSRobert Watson 		comma = 1;
2700497057eeSRobert Watson 	}
2701497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2702497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2703497057eeSRobert Watson 		comma = 1;
2704497057eeSRobert Watson 	}
2705497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2706497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2707497057eeSRobert Watson 		comma = 1;
2708497057eeSRobert Watson 	}
2709281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2710281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2711281a0fd4SPatrick Kelsey 		comma = 1;
2712281a0fd4SPatrick Kelsey 	}
2713497057eeSRobert Watson }
2714497057eeSRobert Watson 
2715497057eeSRobert Watson static void
27163cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2)
27173cf38784SMichael Tuexen {
27183cf38784SMichael Tuexen 	int comma;
27193cf38784SMichael Tuexen 
27203cf38784SMichael Tuexen 	comma = 0;
27213cf38784SMichael Tuexen 	if (t_flags2 & TF2_ECN_PERMIT) {
27223cf38784SMichael Tuexen 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
27233cf38784SMichael Tuexen 		comma = 1;
27243cf38784SMichael Tuexen 	}
27253cf38784SMichael Tuexen }
27263cf38784SMichael Tuexen 
27273cf38784SMichael Tuexen 
27283cf38784SMichael Tuexen static void
2729497057eeSRobert Watson db_print_toobflags(char t_oobflags)
2730497057eeSRobert Watson {
2731497057eeSRobert Watson 	int comma;
2732497057eeSRobert Watson 
2733497057eeSRobert Watson 	comma = 0;
2734497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
2735497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2736497057eeSRobert Watson 		comma = 1;
2737497057eeSRobert Watson 	}
2738497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
2739497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2740497057eeSRobert Watson 		comma = 1;
2741497057eeSRobert Watson 	}
2742497057eeSRobert Watson }
2743497057eeSRobert Watson 
2744497057eeSRobert Watson static void
2745497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2746497057eeSRobert Watson {
2747497057eeSRobert Watson 
2748497057eeSRobert Watson 	db_print_indent(indent);
2749497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
2750497057eeSRobert Watson 
2751497057eeSRobert Watson 	indent += 2;
2752497057eeSRobert Watson 
2753497057eeSRobert Watson 	db_print_indent(indent);
2754497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
2755c28440dbSRandall Stewart 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2756497057eeSRobert Watson 
2757497057eeSRobert Watson 	db_print_indent(indent);
275885d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
2759e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2760497057eeSRobert Watson 
2761497057eeSRobert Watson 	db_print_indent(indent);
2762e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2763e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
2764497057eeSRobert Watson 
2765497057eeSRobert Watson 	db_print_indent(indent);
2766497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
2767497057eeSRobert Watson 	db_print_tstate(tp->t_state);
2768497057eeSRobert Watson 	db_printf(")\n");
2769497057eeSRobert Watson 
2770497057eeSRobert Watson 	db_print_indent(indent);
2771497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
2772497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
2773497057eeSRobert Watson 	db_printf(")\n");
2774497057eeSRobert Watson 
2775497057eeSRobert Watson 	db_print_indent(indent);
27763cf38784SMichael Tuexen 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
27773cf38784SMichael Tuexen 	db_print_tflags2(tp->t_flags2);
27783cf38784SMichael Tuexen 	db_printf(")\n");
27793cf38784SMichael Tuexen 
27803cf38784SMichael Tuexen 	db_print_indent(indent);
2781497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
2782497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
2783497057eeSRobert Watson 
2784497057eeSRobert Watson 	db_print_indent(indent);
2785497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
2786497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2787497057eeSRobert Watson 
2788497057eeSRobert Watson 	db_print_indent(indent);
2789497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
2790497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
2791497057eeSRobert Watson 
2792497057eeSRobert Watson 	db_print_indent(indent);
27933ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
2794497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2795497057eeSRobert Watson 
2796497057eeSRobert Watson 	db_print_indent(indent);
27973ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
27981c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
2799497057eeSRobert Watson 
2800497057eeSRobert Watson 	db_print_indent(indent);
28013ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
28021c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2803497057eeSRobert Watson 
2804497057eeSRobert Watson 	db_print_indent(indent);
28050c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
28060c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
2807497057eeSRobert Watson 
2808497057eeSRobert Watson 	db_print_indent(indent);
28091c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
28101c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
2811497057eeSRobert Watson 
2812497057eeSRobert Watson 	db_print_indent(indent);
28131c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
28141c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2815497057eeSRobert Watson 
2816497057eeSRobert Watson 	db_print_indent(indent);
2817497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
2818497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2819497057eeSRobert Watson 	    tp->t_rttbest);
2820497057eeSRobert Watson 
2821497057eeSRobert Watson 	db_print_indent(indent);
28223ac12506SJonathan T. Looney 	db_printf("t_rttupdated: %lu   max_sndwnd: %u   t_softerror: %d\n",
2823497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2824497057eeSRobert Watson 
2825497057eeSRobert Watson 	db_print_indent(indent);
2826497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2827497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
2828497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
2829497057eeSRobert Watson 
2830497057eeSRobert Watson 	db_print_indent(indent);
2831497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
2832497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2833497057eeSRobert Watson 
2834497057eeSRobert Watson 	db_print_indent(indent);
28359f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
28361a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
2837497057eeSRobert Watson 
2838497057eeSRobert Watson 	db_print_indent(indent);
2839497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
28403ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2841497057eeSRobert Watson 
2842497057eeSRobert Watson 	db_print_indent(indent);
28433ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
28449f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2845497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
2846497057eeSRobert Watson 
2847497057eeSRobert Watson 	db_print_indent(indent);
28483529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
28493529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2850497057eeSRobert Watson 
2851497057eeSRobert Watson 	db_print_indent(indent);
2852a3574665SMichael Tuexen 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
2853a3574665SMichael Tuexen 	    tp->snd_fack, tp->rcv_numsacks);
2854497057eeSRobert Watson 
2855497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
2856497057eeSRobert Watson 
2857497057eeSRobert Watson 	db_print_indent(indent);
2858497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
2859497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2860497057eeSRobert Watson }
2861497057eeSRobert Watson 
2862497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2863497057eeSRobert Watson {
2864497057eeSRobert Watson 	struct tcpcb *tp;
2865497057eeSRobert Watson 
2866497057eeSRobert Watson 	if (!have_addr) {
2867497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
2868497057eeSRobert Watson 		return;
2869497057eeSRobert Watson 	}
2870497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
2871497057eeSRobert Watson 
2872497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
2873497057eeSRobert Watson }
2874497057eeSRobert Watson #endif
2875