xref: /freebsd/sys/netinet/tcp_usrreq.c (revision adc56f5a383771f594829b7db9c263b6f0dcf1bd)
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>
52*adc56f5aSEdward 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>
58*adc56f5aSEdward 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>
70*adc56f5aSEdward 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 
114*adc56f5aSEdward Tomasz Napierala #include <vm/vm.h>
115*adc56f5aSEdward Tomasz Napierala #include <vm/vm_param.h>
116*adc56f5aSEdward Tomasz Napierala #include <vm/pmap.h>
117*adc56f5aSEdward Tomasz Napierala #include <vm/vm_extern.h>
118*adc56f5aSEdward Tomasz Napierala #include <vm/vm_map.h>
119*adc56f5aSEdward Tomasz Napierala #include <vm/vm_page.h>
120*adc56f5aSEdward Tomasz Napierala 
121df8bae1dSRodney W. Grimes /*
122df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
123df8bae1dSRodney W. Grimes  */
12456dc72c3SPawel Jakub Dawidek static int	tcp_attach(struct socket *);
125b287c6c7SBjoern A. Zeeb #ifdef INET
1264d77a549SAlfred Perlstein static int	tcp_connect(struct tcpcb *, struct sockaddr *,
1274d77a549SAlfred Perlstein 		    struct thread *td);
128b287c6c7SBjoern A. Zeeb #endif /* INET */
129fb59c426SYoshinobu Inoue #ifdef INET6
1304d77a549SAlfred Perlstein static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
1314d77a549SAlfred Perlstein 		    struct thread *td);
132fb59c426SYoshinobu Inoue #endif /* INET6 */
133623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
134623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
135b8af5dfaSRobert Watson static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
1362c37256eSGarrett Wollman 
1372c37256eSGarrett Wollman #ifdef TCPDEBUG
1381db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1392c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
1404cc20ab1SSeigo Tanimura #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
1414cc20ab1SSeigo Tanimura 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1422c37256eSGarrett Wollman #else
1432c37256eSGarrett Wollman #define	TCPDEBUG0
1442c37256eSGarrett Wollman #define	TCPDEBUG1()
1452c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1462c37256eSGarrett Wollman #endif
1472c37256eSGarrett Wollman 
1482c37256eSGarrett Wollman /*
1492c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1502c37256eSGarrett Wollman  * and an internet control block.
1512c37256eSGarrett Wollman  */
1522c37256eSGarrett Wollman static int
153b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1542c37256eSGarrett Wollman {
155f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
156623dce13SRobert Watson 	struct tcpcb *tp = NULL;
157623dce13SRobert Watson 	int error;
1582c37256eSGarrett Wollman 	TCPDEBUG0;
1592c37256eSGarrett Wollman 
160623dce13SRobert Watson 	inp = sotoinpcb(so);
161623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1622c37256eSGarrett Wollman 	TCPDEBUG1();
1632c37256eSGarrett Wollman 
16456dc72c3SPawel Jakub Dawidek 	error = tcp_attach(so);
1652c37256eSGarrett Wollman 	if (error)
1662c37256eSGarrett Wollman 		goto out;
1672c37256eSGarrett Wollman 
1682c37256eSGarrett Wollman 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1693879597fSAndrey A. Chernov 		so->so_linger = TCP_LINGERTIME;
170f76fcf6dSJeffrey Hsu 
171f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
172f76fcf6dSJeffrey Hsu 	tp = intotcpcb(inp);
1732c37256eSGarrett Wollman out:
1742c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
1755d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
1762c37256eSGarrett Wollman 	return error;
1772c37256eSGarrett Wollman }
1782c37256eSGarrett Wollman 
1792c37256eSGarrett Wollman /*
180a152f8a3SRobert Watson  * tcp_detach is called when the socket layer loses its final reference
181a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
182a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
183a152f8a3SRobert Watson  * inpcb state: time wait.
184c78cbc7bSRobert Watson  *
185a152f8a3SRobert Watson  * This function can probably be re-absorbed back into tcp_usr_detach() now
186a152f8a3SRobert Watson  * that there is a single detach path.
1872c37256eSGarrett Wollman  */
188bc725eafSRobert Watson static void
189c78cbc7bSRobert Watson tcp_detach(struct socket *so, struct inpcb *inp)
1902c37256eSGarrett Wollman {
1912c37256eSGarrett Wollman 	struct tcpcb *tp;
1922c37256eSGarrett Wollman 
193079672cbSJulien Charbon 	INP_INFO_LOCK_ASSERT(&V_tcbinfo);
1948501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
195623dce13SRobert Watson 
196c78cbc7bSRobert Watson 	KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
197c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
198953b5606SRobert Watson 
199a152f8a3SRobert Watson 	tp = intotcpcb(inp);
200a152f8a3SRobert Watson 
201ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
202623dce13SRobert Watson 		/*
203a152f8a3SRobert Watson 		 * There are two cases to handle: one in which the time wait
204a152f8a3SRobert Watson 		 * state is being discarded (INP_DROPPED), and one in which
205a152f8a3SRobert Watson 		 * this connection will remain in timewait.  In the former,
206a152f8a3SRobert Watson 		 * it is time to discard all state (except tcptw, which has
207a152f8a3SRobert Watson 		 * already been discarded by the timewait close code, which
208a152f8a3SRobert Watson 		 * should be further up the call stack somewhere).  In the
209a152f8a3SRobert Watson 		 * latter case, we detach from the socket, but leave the pcb
210a152f8a3SRobert Watson 		 * present until timewait ends.
211623dce13SRobert Watson 		 *
212a152f8a3SRobert Watson 		 * XXXRW: Would it be cleaner to free the tcptw here?
213cea40c48SJulien Charbon 		 *
214cea40c48SJulien Charbon 		 * Astute question indeed, from twtcp perspective there are
215dd388cfdSGleb Smirnoff 		 * four cases to consider:
216cea40c48SJulien Charbon 		 *
217cea40c48SJulien Charbon 		 * #1 tcp_detach is called at tcptw creation time by
218cea40c48SJulien Charbon 		 *  tcp_twstart, then do not discard the newly created tcptw
219cea40c48SJulien Charbon 		 *  and leave inpcb present until timewait ends
220dd388cfdSGleb Smirnoff 		 * #2 tcp_detach is called at tcptw creation time by
221dd388cfdSGleb Smirnoff 		 *  tcp_twstart, but connection is local and tw will be
222dd388cfdSGleb Smirnoff 		 *  discarded immediately
223dd388cfdSGleb Smirnoff 		 * #3 tcp_detach is called at timewait end (or reuse) by
224cea40c48SJulien Charbon 		 *  tcp_twclose, then the tcptw has already been discarded
225ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
226dd388cfdSGleb Smirnoff 		 * #4 tcp_detach is called() after timewait ends (or reuse)
227cea40c48SJulien Charbon 		 *  (e.g. by soclose), then tcptw has already been discarded
228ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
229cea40c48SJulien Charbon 		 *
230cea40c48SJulien Charbon 		 *  In all three cases the tcptw should not be freed here.
231623dce13SRobert Watson 		 */
232ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED) {
233623dce13SRobert Watson 			in_pcbdetach(inp);
234f5cf1e5fSJulien Charbon 			if (__predict_true(tp == NULL)) {
2350206cdb8SBjoern A. Zeeb 				in_pcbfree(inp);
2360206cdb8SBjoern A. Zeeb 			} else {
237f5cf1e5fSJulien Charbon 				/*
238f5cf1e5fSJulien Charbon 				 * This case should not happen as in TIMEWAIT
239f5cf1e5fSJulien Charbon 				 * state the inp should not be destroyed before
240f5cf1e5fSJulien Charbon 				 * its tcptw.  If INVARIANTS is defined, panic.
241f5cf1e5fSJulien Charbon 				 */
242f5cf1e5fSJulien Charbon #ifdef INVARIANTS
243f5cf1e5fSJulien Charbon 				panic("%s: Panic before an inp double-free: "
244f5cf1e5fSJulien Charbon 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
245f5cf1e5fSJulien Charbon 				    , __func__);
246f5cf1e5fSJulien Charbon #else
247f5cf1e5fSJulien Charbon 				log(LOG_ERR, "%s: Avoid an inp double-free: "
248f5cf1e5fSJulien Charbon 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
249f5cf1e5fSJulien Charbon 				    , __func__);
250f5cf1e5fSJulien Charbon #endif
251f5cf1e5fSJulien Charbon 				INP_WUNLOCK(inp);
252f5cf1e5fSJulien Charbon 			}
253f5cf1e5fSJulien Charbon 		} else {
254623dce13SRobert Watson 			in_pcbdetach(inp);
2558501a69cSRobert Watson 			INP_WUNLOCK(inp);
256623dce13SRobert Watson 		}
257623dce13SRobert Watson 	} else {
258e6e65783SRobert Watson 		/*
259a152f8a3SRobert Watson 		 * If the connection is not in timewait, we consider two
260a152f8a3SRobert Watson 		 * two conditions: one in which no further processing is
261a152f8a3SRobert Watson 		 * necessary (dropped || embryonic), and one in which TCP is
262a152f8a3SRobert Watson 		 * not yet done, but no longer requires the socket, so the
263a152f8a3SRobert Watson 		 * pcb will persist for the time being.
264a152f8a3SRobert Watson 		 *
265a152f8a3SRobert Watson 		 * XXXRW: Does the second case still occur?
266e6e65783SRobert Watson 		 */
267ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED ||
268623dce13SRobert Watson 		    tp->t_state < TCPS_SYN_SENT) {
269623dce13SRobert Watson 			tcp_discardcb(tp);
270623dce13SRobert Watson 			in_pcbdetach(inp);
2710206cdb8SBjoern A. Zeeb 			in_pcbfree(inp);
272db3cee51SNavdeep Parhar 		} else {
273a152f8a3SRobert Watson 			in_pcbdetach(inp);
274db3cee51SNavdeep Parhar 			INP_WUNLOCK(inp);
275db3cee51SNavdeep Parhar 		}
276623dce13SRobert Watson 	}
277623dce13SRobert Watson }
278c78cbc7bSRobert Watson 
279c78cbc7bSRobert Watson /*
280c78cbc7bSRobert Watson  * pru_detach() detaches the TCP protocol from the socket.
281c78cbc7bSRobert Watson  * If the protocol state is non-embryonic, then can't
282c78cbc7bSRobert Watson  * do this directly: have to initiate a pru_disconnect(),
283c78cbc7bSRobert Watson  * which may finish later; embryonic TCB's can just
284c78cbc7bSRobert Watson  * be discarded here.
285c78cbc7bSRobert Watson  */
286c78cbc7bSRobert Watson static void
287c78cbc7bSRobert Watson tcp_usr_detach(struct socket *so)
288c78cbc7bSRobert Watson {
289c78cbc7bSRobert Watson 	struct inpcb *inp;
290079672cbSJulien Charbon 	int rlock = 0;
2916573d758SMatt Macy 	struct epoch_tracker et;
292c78cbc7bSRobert Watson 
293c78cbc7bSRobert Watson 	inp = sotoinpcb(so);
294c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
295079672cbSJulien Charbon 	if (!INP_INFO_WLOCKED(&V_tcbinfo)) {
29697a95ee1SGleb Smirnoff 		NET_EPOCH_ENTER(et);
297079672cbSJulien Charbon 		rlock = 1;
298079672cbSJulien Charbon 	}
2998501a69cSRobert Watson 	INP_WLOCK(inp);
300c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
301c78cbc7bSRobert Watson 	    ("tcp_usr_detach: inp_socket == NULL"));
302c78cbc7bSRobert Watson 	tcp_detach(so, inp);
303079672cbSJulien Charbon 	if (rlock)
30497a95ee1SGleb Smirnoff 		NET_EPOCH_EXIT(et);
3052c37256eSGarrett Wollman }
3062c37256eSGarrett Wollman 
307b287c6c7SBjoern A. Zeeb #ifdef INET
3082c37256eSGarrett Wollman /*
3092c37256eSGarrett Wollman  * Give the socket an address.
3102c37256eSGarrett Wollman  */
3112c37256eSGarrett Wollman static int
312b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
3132c37256eSGarrett Wollman {
3142c37256eSGarrett Wollman 	int error = 0;
315f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
316623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3172c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
3182c37256eSGarrett Wollman 
31952710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
32052710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sinp))
32152710de1SPawel Jakub Dawidek 		return (EINVAL);
3222c37256eSGarrett Wollman 	/*
3232c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
3242c37256eSGarrett Wollman 	 * to them.
3252c37256eSGarrett Wollman 	 */
3262c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
32752710de1SPawel Jakub Dawidek 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
32852710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
32952710de1SPawel Jakub Dawidek 
330623dce13SRobert Watson 	TCPDEBUG0;
331623dce13SRobert Watson 	inp = sotoinpcb(so);
332623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
3338501a69cSRobert Watson 	INP_WLOCK(inp);
334ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
335623dce13SRobert Watson 		error = EINVAL;
3362c37256eSGarrett Wollman 		goto out;
337623dce13SRobert Watson 	}
338623dce13SRobert Watson 	tp = intotcpcb(inp);
339623dce13SRobert Watson 	TCPDEBUG1();
340fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
341623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
342fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
343623dce13SRobert Watson out:
344623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3455d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3468501a69cSRobert Watson 	INP_WUNLOCK(inp);
347623dce13SRobert Watson 
348623dce13SRobert Watson 	return (error);
3492c37256eSGarrett Wollman }
350b287c6c7SBjoern A. Zeeb #endif /* INET */
3512c37256eSGarrett Wollman 
352fb59c426SYoshinobu Inoue #ifdef INET6
353fb59c426SYoshinobu Inoue static int
354b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
355fb59c426SYoshinobu Inoue {
356fb59c426SYoshinobu Inoue 	int error = 0;
357f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
358623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3590ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
3604a91aa8fSMichael Tuexen 	u_char vflagsav;
361fb59c426SYoshinobu Inoue 
3620ecd976eSBjoern A. Zeeb 	sin6 = (struct sockaddr_in6 *)nam;
3630ecd976eSBjoern A. Zeeb 	if (nam->sa_len != sizeof (*sin6))
36452710de1SPawel Jakub Dawidek 		return (EINVAL);
365fb59c426SYoshinobu Inoue 	/*
366fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
367fb59c426SYoshinobu Inoue 	 * to them.
368fb59c426SYoshinobu Inoue 	 */
3690ecd976eSBjoern A. Zeeb 	if (sin6->sin6_family == AF_INET6 &&
3700ecd976eSBjoern A. Zeeb 	    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
37152710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
37252710de1SPawel Jakub Dawidek 
373623dce13SRobert Watson 	TCPDEBUG0;
374623dce13SRobert Watson 	inp = sotoinpcb(so);
375623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
3768501a69cSRobert Watson 	INP_WLOCK(inp);
3774a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
378ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
379623dce13SRobert Watson 		error = EINVAL;
380623dce13SRobert Watson 		goto out;
381623dce13SRobert Watson 	}
382623dce13SRobert Watson 	tp = intotcpcb(inp);
383623dce13SRobert Watson 	TCPDEBUG1();
384fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
385fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
386fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
387b287c6c7SBjoern A. Zeeb #ifdef INET
38866ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
3890ecd976eSBjoern A. Zeeb 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
390fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
3910ecd976eSBjoern A. Zeeb 		else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
392fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
393fb59c426SYoshinobu Inoue 
3940ecd976eSBjoern A. Zeeb 			in6_sin6_2_sin(&sin, sin6);
395888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
396888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
397888973f5SMichael Tuexen 				INP_HASH_WUNLOCK(&V_tcbinfo);
398888973f5SMichael Tuexen 				goto out;
399888973f5SMichael Tuexen 			}
400fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
401fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
402b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
403b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
404fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
405fb59c426SYoshinobu Inoue 			goto out;
406fb59c426SYoshinobu Inoue 		}
407fb59c426SYoshinobu Inoue 	}
408b287c6c7SBjoern A. Zeeb #endif
409b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
410fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
411623dce13SRobert Watson out:
4124a91aa8fSMichael Tuexen 	if (error != 0)
4134a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
414623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
4155d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
4168501a69cSRobert Watson 	INP_WUNLOCK(inp);
417623dce13SRobert Watson 	return (error);
418fb59c426SYoshinobu Inoue }
419fb59c426SYoshinobu Inoue #endif /* INET6 */
420fb59c426SYoshinobu Inoue 
421b287c6c7SBjoern A. Zeeb #ifdef INET
4222c37256eSGarrett Wollman /*
4232c37256eSGarrett Wollman  * Prepare to accept connections.
4242c37256eSGarrett Wollman  */
4252c37256eSGarrett Wollman static int
426d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
4272c37256eSGarrett Wollman {
4282c37256eSGarrett Wollman 	int error = 0;
429f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
430623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4312c37256eSGarrett Wollman 
432623dce13SRobert Watson 	TCPDEBUG0;
433623dce13SRobert Watson 	inp = sotoinpcb(so);
434623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
4358501a69cSRobert Watson 	INP_WLOCK(inp);
436ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
437623dce13SRobert Watson 		error = EINVAL;
438623dce13SRobert Watson 		goto out;
439623dce13SRobert Watson 	}
440623dce13SRobert Watson 	tp = intotcpcb(inp);
441623dce13SRobert Watson 	TCPDEBUG1();
4420daccb9cSRobert Watson 	SOCK_LOCK(so);
4430daccb9cSRobert Watson 	error = solisten_proto_check(so);
444fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
4450daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0)
446b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
447fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4480daccb9cSRobert Watson 	if (error == 0) {
44957f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
450d374e81eSRobert Watson 		solisten_proto(so, backlog);
45109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
45237cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
45309fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
45409fe6320SNavdeep Parhar #endif
4550daccb9cSRobert Watson 	}
4560daccb9cSRobert Watson 	SOCK_UNLOCK(so);
457623dce13SRobert Watson 
45868bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
459281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
46018a75309SPatrick Kelsey 
461623dce13SRobert Watson out:
462623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4635d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4648501a69cSRobert Watson 	INP_WUNLOCK(inp);
465623dce13SRobert Watson 	return (error);
4662c37256eSGarrett Wollman }
467b287c6c7SBjoern A. Zeeb #endif /* INET */
4682c37256eSGarrett Wollman 
469fb59c426SYoshinobu Inoue #ifdef INET6
470fb59c426SYoshinobu Inoue static int
471d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
472fb59c426SYoshinobu Inoue {
473fb59c426SYoshinobu Inoue 	int error = 0;
474f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
475623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4764a91aa8fSMichael Tuexen 	u_char vflagsav;
477fb59c426SYoshinobu Inoue 
478623dce13SRobert Watson 	TCPDEBUG0;
479623dce13SRobert Watson 	inp = sotoinpcb(so);
480623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4818501a69cSRobert Watson 	INP_WLOCK(inp);
482ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
483623dce13SRobert Watson 		error = EINVAL;
484623dce13SRobert Watson 		goto out;
485623dce13SRobert Watson 	}
4864a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
487623dce13SRobert Watson 	tp = intotcpcb(inp);
488623dce13SRobert Watson 	TCPDEBUG1();
4890daccb9cSRobert Watson 	SOCK_LOCK(so);
4900daccb9cSRobert Watson 	error = solisten_proto_check(so);
491fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
4920daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0) {
493fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
49466ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
495fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
496b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
497fb59c426SYoshinobu Inoue 	}
498fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4990daccb9cSRobert Watson 	if (error == 0) {
50057f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
501d374e81eSRobert Watson 		solisten_proto(so, backlog);
50209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
50337cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
50409fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
50509fe6320SNavdeep Parhar #endif
5060daccb9cSRobert Watson 	}
5070daccb9cSRobert Watson 	SOCK_UNLOCK(so);
508623dce13SRobert Watson 
50968bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
510281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
51118a75309SPatrick Kelsey 
5124a91aa8fSMichael Tuexen 	if (error != 0)
5134a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
5144a91aa8fSMichael Tuexen 
515623dce13SRobert Watson out:
516623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
5175d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
5188501a69cSRobert Watson 	INP_WUNLOCK(inp);
519623dce13SRobert Watson 	return (error);
520fb59c426SYoshinobu Inoue }
521fb59c426SYoshinobu Inoue #endif /* INET6 */
522fb59c426SYoshinobu Inoue 
523b287c6c7SBjoern A. Zeeb #ifdef INET
5242c37256eSGarrett Wollman /*
5252c37256eSGarrett Wollman  * Initiate connection to peer.
5262c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
5272c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
5282c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
5292c37256eSGarrett Wollman  * Send initial segment on connection.
5302c37256eSGarrett Wollman  */
5312c37256eSGarrett Wollman static int
532b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
5332c37256eSGarrett Wollman {
5342c37256eSGarrett Wollman 	int error = 0;
535f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
536623dce13SRobert Watson 	struct tcpcb *tp = NULL;
5372c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
5382c37256eSGarrett Wollman 
53957bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
540e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
541e29ef13fSDon Lewis 		return (EINVAL);
54252710de1SPawel Jakub Dawidek 	/*
54352710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
54452710de1SPawel Jakub Dawidek 	 */
5452c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
54652710de1SPawel Jakub Dawidek 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
54752710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
548b89e82ddSJamie Gritton 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
549b89e82ddSJamie Gritton 		return (error);
55075c13541SPoul-Henning Kamp 
551623dce13SRobert Watson 	TCPDEBUG0;
552623dce13SRobert Watson 	inp = sotoinpcb(so);
553623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
5548501a69cSRobert Watson 	INP_WLOCK(inp);
555eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
556eb96dc33SJulien Charbon 		error = EADDRINUSE;
557eb96dc33SJulien Charbon 		goto out;
558eb96dc33SJulien Charbon 	}
559eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
560eb96dc33SJulien Charbon 		error = ECONNREFUSED;
561623dce13SRobert Watson 		goto out;
562623dce13SRobert Watson 	}
563623dce13SRobert Watson 	tp = intotcpcb(inp);
564623dce13SRobert Watson 	TCPDEBUG1();
565b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
5662c37256eSGarrett Wollman 		goto out;
56709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
56809fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
56937cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
57009fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
57109fe6320SNavdeep Parhar 		goto out;
57209fe6320SNavdeep Parhar #endif
57309fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
57455bceb1eSRandall Stewart 	error = tp->t_fb->tfb_tcp_output(tp);
575623dce13SRobert Watson out:
576623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
577e79cb051SGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
5788501a69cSRobert Watson 	INP_WUNLOCK(inp);
579623dce13SRobert Watson 	return (error);
5802c37256eSGarrett Wollman }
581b287c6c7SBjoern A. Zeeb #endif /* INET */
5822c37256eSGarrett Wollman 
583fb59c426SYoshinobu Inoue #ifdef INET6
584fb59c426SYoshinobu Inoue static int
585b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
586fb59c426SYoshinobu Inoue {
587fb59c426SYoshinobu Inoue 	int error = 0;
588f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
589623dce13SRobert Watson 	struct tcpcb *tp = NULL;
5900ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
5914a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
5924a91aa8fSMichael Tuexen 	u_char vflagsav;
593623dce13SRobert Watson 
594623dce13SRobert Watson 	TCPDEBUG0;
595fb59c426SYoshinobu Inoue 
5960ecd976eSBjoern A. Zeeb 	sin6 = (struct sockaddr_in6 *)nam;
5970ecd976eSBjoern A. Zeeb 	if (nam->sa_len != sizeof (*sin6))
598e29ef13fSDon Lewis 		return (EINVAL);
59952710de1SPawel Jakub Dawidek 	/*
60052710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
60152710de1SPawel Jakub Dawidek 	 */
6020ecd976eSBjoern A. Zeeb 	if (sin6->sin6_family == AF_INET6
6030ecd976eSBjoern A. Zeeb 	    && IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
60452710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
605fb59c426SYoshinobu Inoue 
606623dce13SRobert Watson 	inp = sotoinpcb(so);
607623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
6088501a69cSRobert Watson 	INP_WLOCK(inp);
6094a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
6104a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
611eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
612eb96dc33SJulien Charbon 		error = EADDRINUSE;
613eb96dc33SJulien Charbon 		goto out;
614eb96dc33SJulien Charbon 	}
615eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
616eb96dc33SJulien Charbon 		error = ECONNREFUSED;
617623dce13SRobert Watson 		goto out;
618623dce13SRobert Watson 	}
619623dce13SRobert Watson 	tp = intotcpcb(inp);
620623dce13SRobert Watson 	TCPDEBUG1();
621b287c6c7SBjoern A. Zeeb #ifdef INET
622fa046d87SRobert Watson 	/*
623fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
624fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
625fa046d87SRobert Watson 	 * Is this a significant problem?
626fa046d87SRobert Watson 	 */
6270ecd976eSBjoern A. Zeeb 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
628fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
629fb59c426SYoshinobu Inoue 
630d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
631d46a5312SMaxim Konovalov 			error = EINVAL;
632d46a5312SMaxim Konovalov 			goto out;
633d46a5312SMaxim Konovalov 		}
6345dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV4) == 0) {
6355dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6365dba6adaSMichael Tuexen 			goto out;
6375dba6adaSMichael Tuexen 		}
63833841545SHajimu UMEMOTO 
6390ecd976eSBjoern A. Zeeb 		in6_sin6_2_sin(&sin, sin6);
640888973f5SMichael Tuexen 		if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
641888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
642888973f5SMichael Tuexen 			goto out;
643888973f5SMichael Tuexen 		}
644b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
645b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
646413628a7SBjoern A. Zeeb 			goto out;
6474a91aa8fSMichael Tuexen 		inp->inp_vflag |= INP_IPV4;
6484a91aa8fSMichael Tuexen 		inp->inp_vflag &= ~INP_IPV6;
649b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
650fb59c426SYoshinobu Inoue 			goto out;
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)
65509fe6320SNavdeep Parhar 			goto out;
65609fe6320SNavdeep Parhar #endif
65755bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
658fb59c426SYoshinobu Inoue 		goto out;
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));
68055bceb1eSRandall Stewart 	error = tp->t_fb->tfb_tcp_output(tp);
681623dce13SRobert Watson 
682623dce13SRobert Watson out:
6834a91aa8fSMichael Tuexen 	/*
6844a91aa8fSMichael Tuexen 	 * If the implicit bind in the connect call fails, restore
6854a91aa8fSMichael Tuexen 	 * the flags we modified.
6864a91aa8fSMichael Tuexen 	 */
6874a91aa8fSMichael Tuexen 	if (error != 0 && inp->inp_lport == 0) {
6884a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
6894a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
6904a91aa8fSMichael Tuexen 	}
6914a91aa8fSMichael Tuexen 
692623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
6935d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
6948501a69cSRobert Watson 	INP_WUNLOCK(inp);
695623dce13SRobert Watson 	return (error);
696fb59c426SYoshinobu Inoue }
697fb59c426SYoshinobu Inoue #endif /* INET6 */
698fb59c426SYoshinobu Inoue 
6992c37256eSGarrett Wollman /*
7002c37256eSGarrett Wollman  * Initiate disconnect from peer.
7012c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
7022c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
7032c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
7042c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
7052c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
7062c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
7072c37256eSGarrett Wollman  *
7082c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
7092c37256eSGarrett Wollman  */
7102c37256eSGarrett Wollman static int
7112c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
7122c37256eSGarrett Wollman {
713f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
714623dce13SRobert Watson 	struct tcpcb *tp = NULL;
7156573d758SMatt Macy 	struct epoch_tracker et;
716623dce13SRobert Watson 	int error = 0;
7172c37256eSGarrett Wollman 
718623dce13SRobert Watson 	TCPDEBUG0;
71997a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
720623dce13SRobert Watson 	inp = sotoinpcb(so);
721623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
7228501a69cSRobert Watson 	INP_WLOCK(inp);
723489dcc92SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT)
724489dcc92SJulien Charbon 		goto out;
725489dcc92SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
72621367f63SSam Leffler 		error = ECONNRESET;
727623dce13SRobert Watson 		goto out;
728623dce13SRobert Watson 	}
729623dce13SRobert Watson 	tp = intotcpcb(inp);
730623dce13SRobert Watson 	TCPDEBUG1();
731623dce13SRobert Watson 	tcp_disconnect(tp);
732623dce13SRobert Watson out:
733623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
7345d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
7358501a69cSRobert Watson 	INP_WUNLOCK(inp);
73697a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
737623dce13SRobert Watson 	return (error);
7382c37256eSGarrett Wollman }
7392c37256eSGarrett Wollman 
740b287c6c7SBjoern A. Zeeb #ifdef INET
7412c37256eSGarrett Wollman /*
7428296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
7438296cddfSRobert Watson  * just return the address of the peer, storing through addr.
7442c37256eSGarrett Wollman  */
7452c37256eSGarrett Wollman static int
74657bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
7472c37256eSGarrett Wollman {
7482c37256eSGarrett Wollman 	int error = 0;
749f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
7501db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
75126ef6ac4SDon Lewis 	struct in_addr addr;
75226ef6ac4SDon Lewis 	in_port_t port = 0;
7531db24ffbSJonathan Lemon 	TCPDEBUG0;
7542c37256eSGarrett Wollman 
7553d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
7563d2d3ef4SRobert Watson 		return (ECONNABORTED);
757f76fcf6dSJeffrey Hsu 
758f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
759623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
7608501a69cSRobert Watson 	INP_WLOCK(inp);
761ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
7623d2d3ef4SRobert Watson 		error = ECONNABORTED;
763623dce13SRobert Watson 		goto out;
764623dce13SRobert Watson 	}
7651db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
7661db24ffbSJonathan Lemon 	TCPDEBUG1();
767f76fcf6dSJeffrey Hsu 
768f76fcf6dSJeffrey Hsu 	/*
76954d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
77026ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
77126ef6ac4SDon Lewis 	 * release the lock.
772f76fcf6dSJeffrey Hsu 	 */
77326ef6ac4SDon Lewis 	port = inp->inp_fport;
77426ef6ac4SDon Lewis 	addr = inp->inp_faddr;
775f76fcf6dSJeffrey Hsu 
776623dce13SRobert Watson out:
777623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
7785d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7798501a69cSRobert Watson 	INP_WUNLOCK(inp);
78026ef6ac4SDon Lewis 	if (error == 0)
78126ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
78226ef6ac4SDon Lewis 	return error;
7832c37256eSGarrett Wollman }
784b287c6c7SBjoern A. Zeeb #endif /* INET */
7852c37256eSGarrett Wollman 
786fb59c426SYoshinobu Inoue #ifdef INET6
787fb59c426SYoshinobu Inoue static int
788fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
789fb59c426SYoshinobu Inoue {
790f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
791fb59c426SYoshinobu Inoue 	int error = 0;
7921db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
79326ef6ac4SDon Lewis 	struct in_addr addr;
79426ef6ac4SDon Lewis 	struct in6_addr addr6;
7956573d758SMatt Macy 	struct epoch_tracker et;
79626ef6ac4SDon Lewis 	in_port_t port = 0;
79726ef6ac4SDon Lewis 	int v4 = 0;
7981db24ffbSJonathan Lemon 	TCPDEBUG0;
799fb59c426SYoshinobu Inoue 
800b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
801b4470c16SRobert Watson 		return (ECONNABORTED);
802f76fcf6dSJeffrey Hsu 
803f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
804623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
80597a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
8068501a69cSRobert Watson 	INP_WLOCK(inp);
807ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
80821367f63SSam Leffler 		error = ECONNABORTED;
809623dce13SRobert Watson 		goto out;
810623dce13SRobert Watson 	}
8111db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
8121db24ffbSJonathan Lemon 	TCPDEBUG1();
813623dce13SRobert Watson 
81426ef6ac4SDon Lewis 	/*
81526ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
81626ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
81726ef6ac4SDon Lewis 	 * release the lock.
81826ef6ac4SDon Lewis 	 */
81926ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
82026ef6ac4SDon Lewis 		v4 = 1;
82126ef6ac4SDon Lewis 		port = inp->inp_fport;
82226ef6ac4SDon Lewis 		addr = inp->inp_faddr;
82326ef6ac4SDon Lewis 	} else {
82426ef6ac4SDon Lewis 		port = inp->inp_fport;
82526ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
82626ef6ac4SDon Lewis 	}
82726ef6ac4SDon Lewis 
828623dce13SRobert Watson out:
829623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
8305d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
8318501a69cSRobert Watson 	INP_WUNLOCK(inp);
83297a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
83326ef6ac4SDon Lewis 	if (error == 0) {
83426ef6ac4SDon Lewis 		if (v4)
83526ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
83626ef6ac4SDon Lewis 		else
83726ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
83826ef6ac4SDon Lewis 	}
83926ef6ac4SDon Lewis 	return error;
840fb59c426SYoshinobu Inoue }
841fb59c426SYoshinobu Inoue #endif /* INET6 */
842f76fcf6dSJeffrey Hsu 
843f76fcf6dSJeffrey Hsu /*
8442c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
8452c37256eSGarrett Wollman  */
8462c37256eSGarrett Wollman static int
8472c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
8482c37256eSGarrett Wollman {
8492c37256eSGarrett Wollman 	int error = 0;
850f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
851623dce13SRobert Watson 	struct tcpcb *tp = NULL;
8526573d758SMatt Macy 	struct epoch_tracker et;
8532c37256eSGarrett Wollman 
854623dce13SRobert Watson 	TCPDEBUG0;
85597a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
856623dce13SRobert Watson 	inp = sotoinpcb(so);
857623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
8588501a69cSRobert Watson 	INP_WLOCK(inp);
859ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
86021367f63SSam Leffler 		error = ECONNRESET;
861623dce13SRobert Watson 		goto out;
862623dce13SRobert Watson 	}
863623dce13SRobert Watson 	tp = intotcpcb(inp);
864623dce13SRobert Watson 	TCPDEBUG1();
8652c37256eSGarrett Wollman 	socantsendmore(so);
866623dce13SRobert Watson 	tcp_usrclosed(tp);
867ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
86855bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
869623dce13SRobert Watson 
870623dce13SRobert Watson out:
871623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
8725d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
8738501a69cSRobert Watson 	INP_WUNLOCK(inp);
87497a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
875623dce13SRobert Watson 
876623dce13SRobert Watson 	return (error);
8772c37256eSGarrett Wollman }
8782c37256eSGarrett Wollman 
8792c37256eSGarrett Wollman /*
8802c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
8812c37256eSGarrett Wollman  */
8822c37256eSGarrett Wollman static int
8832c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
8842c37256eSGarrett Wollman {
885f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
886623dce13SRobert Watson 	struct tcpcb *tp = NULL;
887623dce13SRobert Watson 	int error = 0;
8882c37256eSGarrett Wollman 
889623dce13SRobert Watson 	TCPDEBUG0;
890623dce13SRobert Watson 	inp = sotoinpcb(so);
891623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
8928501a69cSRobert Watson 	INP_WLOCK(inp);
893ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
89421367f63SSam Leffler 		error = ECONNRESET;
895623dce13SRobert Watson 		goto out;
896623dce13SRobert Watson 	}
897623dce13SRobert Watson 	tp = intotcpcb(inp);
898623dce13SRobert Watson 	TCPDEBUG1();
899281a0fd4SPatrick Kelsey 	/*
900281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
901281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
902281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
903281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
904281a0fd4SPatrick Kelsey 	 * expires.
905281a0fd4SPatrick Kelsey 	 */
90668bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags) &&
907281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
908281a0fd4SPatrick Kelsey 		goto out;
90909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
91009fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
91109fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
912460cf046SNavdeep Parhar 	else
91309fe6320SNavdeep Parhar #endif
91455bceb1eSRandall Stewart 	tp->t_fb->tfb_tcp_output(tp);
915623dce13SRobert Watson 
916623dce13SRobert Watson out:
917623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
9185d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
9198501a69cSRobert Watson 	INP_WUNLOCK(inp);
920623dce13SRobert Watson 	return (error);
9212c37256eSGarrett Wollman }
9222c37256eSGarrett Wollman 
9232c37256eSGarrett Wollman /*
9242c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
9259c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
9269c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
9279c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
9289c9906e9SPeter Wemm  * generally are caller-frees.
9292c37256eSGarrett Wollman  */
9302c37256eSGarrett Wollman static int
93157bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
932b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
9332c37256eSGarrett Wollman {
93497a95ee1SGleb Smirnoff 	struct epoch_tracker et;
9352c37256eSGarrett Wollman 	int error = 0;
936f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
937623dce13SRobert Watson 	struct tcpcb *tp = NULL;
938888973f5SMichael Tuexen #ifdef INET
93951e08d53SMichael Tuexen #ifdef INET6
94051e08d53SMichael Tuexen 	struct sockaddr_in sin;
94151e08d53SMichael Tuexen #endif
94251e08d53SMichael Tuexen 	struct sockaddr_in *sinp;
943888973f5SMichael Tuexen #endif
944fb59c426SYoshinobu Inoue #ifdef INET6
945fb59c426SYoshinobu Inoue 	int isipv6;
946fb59c426SYoshinobu Inoue #endif
9474a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
9484a91aa8fSMichael Tuexen 	u_char vflagsav;
9494a91aa8fSMichael Tuexen 	bool restoreflags;
9509c9906e9SPeter Wemm 	TCPDEBUG0;
9512c37256eSGarrett Wollman 
952f76fcf6dSJeffrey Hsu 	/*
95397a95ee1SGleb Smirnoff 	 * We require the pcbinfo "read lock" if we will close the socket
95497a95ee1SGleb Smirnoff 	 * as part of this call.
955f76fcf6dSJeffrey Hsu 	 */
956fa046d87SRobert Watson 	if (flags & PRUS_EOF)
95797a95ee1SGleb Smirnoff 		NET_EPOCH_ENTER(et);
958f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
959623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
9608501a69cSRobert Watson 	INP_WLOCK(inp);
9614a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
9624a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
9634a91aa8fSMichael Tuexen 	restoreflags = false;
964ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
9657ff0b850SAndre Oppermann 		if (control)
9667ff0b850SAndre Oppermann 			m_freem(control);
9672cbcd3c1SGleb Smirnoff 		/*
9682cbcd3c1SGleb Smirnoff 		 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
9692cbcd3c1SGleb Smirnoff 		 * for freeing memory.
9702cbcd3c1SGleb Smirnoff 		 */
9712cbcd3c1SGleb Smirnoff 		if (m && (flags & PRUS_NOTREADY) == 0)
9727ff0b850SAndre Oppermann 			m_freem(m);
97321367f63SSam Leffler 		error = ECONNRESET;
9749c9906e9SPeter Wemm 		goto out;
9759c9906e9SPeter Wemm 	}
9769c9906e9SPeter Wemm 	tp = intotcpcb(inp);
9779c9906e9SPeter Wemm 	TCPDEBUG1();
978888973f5SMichael Tuexen 	if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
979888973f5SMichael Tuexen 		switch (nam->sa_family) {
980888973f5SMichael Tuexen #ifdef INET
981888973f5SMichael Tuexen 		case AF_INET:
982888973f5SMichael Tuexen 			sinp = (struct sockaddr_in *)nam;
983888973f5SMichael Tuexen 			if (sinp->sin_len != sizeof(struct sockaddr_in)) {
984888973f5SMichael Tuexen 				if (m)
985888973f5SMichael Tuexen 					m_freem(m);
986888973f5SMichael Tuexen 				error = EINVAL;
987888973f5SMichael Tuexen 				goto out;
988888973f5SMichael Tuexen 			}
989888973f5SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6) != 0) {
990888973f5SMichael Tuexen 				if (m)
991888973f5SMichael Tuexen 					m_freem(m);
992888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
993888973f5SMichael Tuexen 				goto out;
994888973f5SMichael Tuexen 			}
995888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
996888973f5SMichael Tuexen 				if (m)
997888973f5SMichael Tuexen 					m_freem(m);
998888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
999888973f5SMichael Tuexen 				goto out;
1000888973f5SMichael Tuexen 			}
1001888973f5SMichael Tuexen 			if ((error = prison_remote_ip4(td->td_ucred,
1002888973f5SMichael Tuexen 			    &sinp->sin_addr))) {
1003888973f5SMichael Tuexen 				if (m)
1004888973f5SMichael Tuexen 					m_freem(m);
1005888973f5SMichael Tuexen 				goto out;
1006888973f5SMichael Tuexen 			}
1007888973f5SMichael Tuexen #ifdef INET6
1008888973f5SMichael Tuexen 			isipv6 = 0;
1009888973f5SMichael Tuexen #endif
1010888973f5SMichael Tuexen 			break;
1011888973f5SMichael Tuexen #endif /* INET */
1012888973f5SMichael Tuexen #ifdef INET6
1013888973f5SMichael Tuexen 		case AF_INET6:
1014888973f5SMichael Tuexen 		{
10150ecd976eSBjoern A. Zeeb 			struct sockaddr_in6 *sin6;
1016888973f5SMichael Tuexen 
10170ecd976eSBjoern A. Zeeb 			sin6 = (struct sockaddr_in6 *)nam;
10180ecd976eSBjoern A. Zeeb 			if (sin6->sin6_len != sizeof(*sin6)) {
1019888973f5SMichael Tuexen 				if (m)
1020888973f5SMichael Tuexen 					m_freem(m);
1021888973f5SMichael Tuexen 				error = EINVAL;
1022888973f5SMichael Tuexen 				goto out;
1023888973f5SMichael Tuexen 			}
10240ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1025888973f5SMichael Tuexen 				if (m)
1026888973f5SMichael Tuexen 					m_freem(m);
1027888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1028888973f5SMichael Tuexen 				goto out;
1029888973f5SMichael Tuexen 			}
10300ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1031888973f5SMichael Tuexen #ifdef INET
1032888973f5SMichael Tuexen 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1033888973f5SMichael Tuexen 					error = EINVAL;
1034888973f5SMichael Tuexen 					if (m)
1035888973f5SMichael Tuexen 						m_freem(m);
1036888973f5SMichael Tuexen 					goto out;
1037888973f5SMichael Tuexen 				}
1038888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV4) == 0) {
1039888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1040888973f5SMichael Tuexen 					if (m)
1041888973f5SMichael Tuexen 						m_freem(m);
1042888973f5SMichael Tuexen 					goto out;
1043888973f5SMichael Tuexen 				}
10444a91aa8fSMichael Tuexen 				restoreflags = true;
1045888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV6;
1046888973f5SMichael Tuexen 				sinp = &sin;
10470ecd976eSBjoern A. Zeeb 				in6_sin6_2_sin(sinp, sin6);
1048888973f5SMichael Tuexen 				if (IN_MULTICAST(
1049888973f5SMichael Tuexen 				    ntohl(sinp->sin_addr.s_addr))) {
1050888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1051888973f5SMichael Tuexen 					if (m)
1052888973f5SMichael Tuexen 						m_freem(m);
1053888973f5SMichael Tuexen 					goto out;
1054888973f5SMichael Tuexen 				}
1055888973f5SMichael Tuexen 				if ((error = prison_remote_ip4(td->td_ucred,
1056888973f5SMichael Tuexen 				    &sinp->sin_addr))) {
1057888973f5SMichael Tuexen 					if (m)
1058888973f5SMichael Tuexen 						m_freem(m);
1059888973f5SMichael Tuexen 					goto out;
1060888973f5SMichael Tuexen 				}
1061888973f5SMichael Tuexen 				isipv6 = 0;
1062888973f5SMichael Tuexen #else /* !INET */
1063888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1064888973f5SMichael Tuexen 				if (m)
1065888973f5SMichael Tuexen 					m_freem(m);
1066888973f5SMichael Tuexen 				goto out;
1067888973f5SMichael Tuexen #endif /* INET */
1068888973f5SMichael Tuexen 			} else {
1069888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV6) == 0) {
1070888973f5SMichael Tuexen 					if (m)
1071888973f5SMichael Tuexen 						m_freem(m);
1072888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1073888973f5SMichael Tuexen 					goto out;
1074888973f5SMichael Tuexen 				}
10754a91aa8fSMichael Tuexen 				restoreflags = true;
1076888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV4;
1077888973f5SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_ISIPV6;
1078888973f5SMichael Tuexen 				if ((error = prison_remote_ip6(td->td_ucred,
10790ecd976eSBjoern A. Zeeb 				    &sin6->sin6_addr))) {
1080888973f5SMichael Tuexen 					if (m)
1081888973f5SMichael Tuexen 						m_freem(m);
1082888973f5SMichael Tuexen 					goto out;
1083888973f5SMichael Tuexen 				}
1084888973f5SMichael Tuexen 				isipv6 = 1;
1085888973f5SMichael Tuexen 			}
1086888973f5SMichael Tuexen 			break;
1087888973f5SMichael Tuexen 		}
1088888973f5SMichael Tuexen #endif /* INET6 */
1089888973f5SMichael Tuexen 		default:
1090888973f5SMichael Tuexen 			if (m)
1091888973f5SMichael Tuexen 				m_freem(m);
1092888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
1093888973f5SMichael Tuexen 			goto out;
1094888973f5SMichael Tuexen 		}
1095888973f5SMichael Tuexen 	}
10969c9906e9SPeter Wemm 	if (control) {
10979c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
10989c9906e9SPeter Wemm 		if (control->m_len) {
10999c9906e9SPeter Wemm 			m_freem(control);
11002c37256eSGarrett Wollman 			if (m)
11012c37256eSGarrett Wollman 				m_freem(m);
1102744f87eaSDavid Greenman 			error = EINVAL;
1103744f87eaSDavid Greenman 			goto out;
11042c37256eSGarrett Wollman 		}
11059c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
11069c9906e9SPeter Wemm 	}
11072c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
1108651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
11092c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
11102c37256eSGarrett Wollman 			/*
11112c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
11122c37256eSGarrett Wollman 			 * initialize window to default value, and
11130c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
11142c37256eSGarrett Wollman 			 */
1115fb59c426SYoshinobu Inoue #ifdef INET6
1116fb59c426SYoshinobu Inoue 			if (isipv6)
1117b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1118fb59c426SYoshinobu Inoue #endif /* INET6 */
1119b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1120b287c6c7SBjoern A. Zeeb 			else
1121b287c6c7SBjoern A. Zeeb #endif
1122b287c6c7SBjoern A. Zeeb #ifdef INET
1123888973f5SMichael Tuexen 				error = tcp_connect(tp,
1124888973f5SMichael Tuexen 				    (struct sockaddr *)sinp, td);
1125b287c6c7SBjoern A. Zeeb #endif
11264a91aa8fSMichael Tuexen 			/*
11274a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
11284a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
11294a91aa8fSMichael Tuexen 			 * operations fail.
11304a91aa8fSMichael Tuexen 			 */
11314a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
11324a91aa8fSMichael Tuexen 				restoreflags = false;
11334a91aa8fSMichael Tuexen 
11342c37256eSGarrett Wollman 			if (error)
11352c37256eSGarrett Wollman 				goto out;
1136c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1137c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
113818a75309SPatrick Kelsey 			else {
11392c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
11402c37256eSGarrett Wollman 				tcp_mss(tp, -1);
11412c37256eSGarrett Wollman 			}
1142c560df6fSPatrick Kelsey 		}
11432c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
11442c37256eSGarrett Wollman 			/*
11452c37256eSGarrett Wollman 			 * Close the send side of the connection after
11462c37256eSGarrett Wollman 			 * the data is sent.
11472c37256eSGarrett Wollman 			 */
114897a95ee1SGleb Smirnoff 			NET_EPOCH_ASSERT();
11492c37256eSGarrett Wollman 			socantsendmore(so);
1150623dce13SRobert Watson 			tcp_usrclosed(tp);
11512c37256eSGarrett Wollman 		}
11522cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
11532cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
1154b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1155b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
115655bceb1eSRandall Stewart 			error = tp->t_fb->tfb_tcp_output(tp);
1157b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1158b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
1159b0acefa8SBill Fenner 		}
11602c37256eSGarrett Wollman 	} else {
1161623dce13SRobert Watson 		/*
1162623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1163623dce13SRobert Watson 		 */
1164d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
11652c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
1166d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
11672c37256eSGarrett Wollman 			m_freem(m);
11682c37256eSGarrett Wollman 			error = ENOBUFS;
11692c37256eSGarrett Wollman 			goto out;
11702c37256eSGarrett Wollman 		}
11712c37256eSGarrett Wollman 		/*
11722c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
11732c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
11742c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
11752c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
11762c37256eSGarrett Wollman 		 * of data past the urgent section.
11772c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
11782c37256eSGarrett Wollman 		 */
1179651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
1180d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
1181ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1182ef53690bSGarrett Wollman 			/*
1183ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
1184ef53690bSGarrett Wollman 			 * initialize window to default value, and
11850c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1186ef53690bSGarrett Wollman 			 */
118718a75309SPatrick Kelsey 
1188c560df6fSPatrick Kelsey 			/*
1189c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1190c560df6fSPatrick Kelsey 			 */
1191c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1192c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1193fb59c426SYoshinobu Inoue #ifdef INET6
1194fb59c426SYoshinobu Inoue 			if (isipv6)
1195b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1196fb59c426SYoshinobu Inoue #endif /* INET6 */
1197b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1198b287c6c7SBjoern A. Zeeb 			else
1199b287c6c7SBjoern A. Zeeb #endif
1200b287c6c7SBjoern A. Zeeb #ifdef INET
1201888973f5SMichael Tuexen 				error = tcp_connect(tp,
1202888973f5SMichael Tuexen 				    (struct sockaddr *)sinp, td);
1203b287c6c7SBjoern A. Zeeb #endif
12044a91aa8fSMichael Tuexen 			/*
12054a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
12064a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
12074a91aa8fSMichael Tuexen 			 * operations fail.
12084a91aa8fSMichael Tuexen 			 */
12094a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
12104a91aa8fSMichael Tuexen 				restoreflags = false;
12114a91aa8fSMichael Tuexen 
1212ef53690bSGarrett Wollman 			if (error)
1213ef53690bSGarrett Wollman 				goto out;
1214ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1215ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
1216623dce13SRobert Watson 		}
1217300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
12182cbcd3c1SGleb Smirnoff 		if (!(flags & PRUS_NOTREADY)) {
12192cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
122055bceb1eSRandall Stewart 			error = tp->t_fb->tfb_tcp_output(tp);
12212cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
12222c37256eSGarrett Wollman 		}
12232cbcd3c1SGleb Smirnoff 	}
12242529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL,
12252529f56eSJonathan T. Looney 	    &inp->inp_socket->so_rcv,
12262529f56eSJonathan T. Looney 	    &inp->inp_socket->so_snd,
12272529f56eSJonathan T. Looney 	    TCP_LOG_USERSEND, error,
12282529f56eSJonathan T. Looney 	    0, NULL, false);
1229d1401c90SRobert Watson out:
12304a91aa8fSMichael Tuexen 	/*
12314a91aa8fSMichael Tuexen 	 * If the request was unsuccessful and we changed flags,
12324a91aa8fSMichael Tuexen 	 * restore the original flags.
12334a91aa8fSMichael Tuexen 	 */
12344a91aa8fSMichael Tuexen 	if (error != 0 && restoreflags) {
12354a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
12364a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
12374a91aa8fSMichael Tuexen 	}
1238d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
12392c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
12405d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
12415d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
12428501a69cSRobert Watson 	INP_WUNLOCK(inp);
1243fa046d87SRobert Watson 	if (flags & PRUS_EOF)
124497a95ee1SGleb Smirnoff 		NET_EPOCH_EXIT(et);
124573fddedaSPeter Grehan 	return (error);
12462c37256eSGarrett Wollman }
12472c37256eSGarrett Wollman 
12482cbcd3c1SGleb Smirnoff static int
12492cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
12502cbcd3c1SGleb Smirnoff {
12512cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
12522cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
12532cbcd3c1SGleb Smirnoff 	int error;
12542cbcd3c1SGleb Smirnoff 
12552cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
12562cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
12572cbcd3c1SGleb Smirnoff 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
12582cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
125982334850SJohn Baldwin 		mb_free_notready(m, count);
12602cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
12612cbcd3c1SGleb Smirnoff 	}
12622cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
12632cbcd3c1SGleb Smirnoff 
12642cbcd3c1SGleb Smirnoff 	SOCKBUF_LOCK(&so->so_snd);
12652cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
12662cbcd3c1SGleb Smirnoff 	SOCKBUF_UNLOCK(&so->so_snd);
12672cbcd3c1SGleb Smirnoff 	if (error == 0)
126855bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
12692cbcd3c1SGleb Smirnoff 	INP_WUNLOCK(inp);
12702cbcd3c1SGleb Smirnoff 
12712cbcd3c1SGleb Smirnoff 	return (error);
12722cbcd3c1SGleb Smirnoff }
12732cbcd3c1SGleb Smirnoff 
12742c37256eSGarrett Wollman /*
1275a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
12762c37256eSGarrett Wollman  */
1277ac45e92fSRobert Watson static void
12782c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
12792c37256eSGarrett Wollman {
1280f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1281a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
12826573d758SMatt Macy 	struct epoch_tracker et;
1283623dce13SRobert Watson 	TCPDEBUG0;
1284c78cbc7bSRobert Watson 
1285ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1286c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1287c78cbc7bSRobert Watson 
128897a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
12898501a69cSRobert Watson 	INP_WLOCK(inp);
1290c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1291c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1292c78cbc7bSRobert Watson 
1293c78cbc7bSRobert Watson 	/*
1294a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1295c78cbc7bSRobert Watson 	 */
1296ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1297ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1298c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
1299a152f8a3SRobert Watson 		TCPDEBUG1();
13008fa799bdSJonathan T. Looney 		tp = tcp_drop(tp, ECONNABORTED);
13018fa799bdSJonathan T. Looney 		if (tp == NULL)
13028fa799bdSJonathan T. Looney 			goto dropped;
1303a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
13045d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1305c78cbc7bSRobert Watson 	}
1306ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1307a152f8a3SRobert Watson 		SOCK_LOCK(so);
1308a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1309a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1310ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1311a152f8a3SRobert Watson 	}
13128501a69cSRobert Watson 	INP_WUNLOCK(inp);
13138fa799bdSJonathan T. Looney dropped:
131497a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1315a152f8a3SRobert Watson }
1316a152f8a3SRobert Watson 
1317a152f8a3SRobert Watson /*
1318a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1319a152f8a3SRobert Watson  */
1320a152f8a3SRobert Watson static void
1321a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1322a152f8a3SRobert Watson {
1323a152f8a3SRobert Watson 	struct inpcb *inp;
1324a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
13256573d758SMatt Macy 	struct epoch_tracker et;
1326a152f8a3SRobert Watson 	TCPDEBUG0;
1327a152f8a3SRobert Watson 
1328a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1329a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1330a152f8a3SRobert Watson 
133197a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
13328501a69cSRobert Watson 	INP_WLOCK(inp);
1333a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1334a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1335a152f8a3SRobert Watson 
1336a152f8a3SRobert Watson 	/*
1337a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
1338a152f8a3SRobert Watson 	 * a disconnect.
1339a152f8a3SRobert Watson 	 */
1340ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1341ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1342a152f8a3SRobert Watson 		tp = intotcpcb(inp);
1343a152f8a3SRobert Watson 		TCPDEBUG1();
1344a152f8a3SRobert Watson 		tcp_disconnect(tp);
1345a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
13465d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1347a152f8a3SRobert Watson 	}
1348ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1349a152f8a3SRobert Watson 		SOCK_LOCK(so);
1350a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1351a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1352ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1353a152f8a3SRobert Watson 	}
13548501a69cSRobert Watson 	INP_WUNLOCK(inp);
135597a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
13562c37256eSGarrett Wollman }
13572c37256eSGarrett Wollman 
13582c37256eSGarrett Wollman /*
13592c37256eSGarrett Wollman  * Receive out-of-band data.
13602c37256eSGarrett Wollman  */
13612c37256eSGarrett Wollman static int
13622c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
13632c37256eSGarrett Wollman {
13642c37256eSGarrett Wollman 	int error = 0;
1365f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1366623dce13SRobert Watson 	struct tcpcb *tp = NULL;
13672c37256eSGarrett Wollman 
1368623dce13SRobert Watson 	TCPDEBUG0;
1369623dce13SRobert Watson 	inp = sotoinpcb(so);
1370623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
13718501a69cSRobert Watson 	INP_WLOCK(inp);
1372ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
137321367f63SSam Leffler 		error = ECONNRESET;
1374623dce13SRobert Watson 		goto out;
1375623dce13SRobert Watson 	}
1376623dce13SRobert Watson 	tp = intotcpcb(inp);
1377623dce13SRobert Watson 	TCPDEBUG1();
13782c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1379c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
13804cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
13814cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
13822c37256eSGarrett Wollman 		error = EINVAL;
13832c37256eSGarrett Wollman 		goto out;
13842c37256eSGarrett Wollman 	}
13852c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
13862c37256eSGarrett Wollman 		error = EWOULDBLOCK;
13872c37256eSGarrett Wollman 		goto out;
13882c37256eSGarrett Wollman 	}
13892c37256eSGarrett Wollman 	m->m_len = 1;
13902c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
13912c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
13922c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1393623dce13SRobert Watson 
1394623dce13SRobert Watson out:
1395623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
13965d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
13978501a69cSRobert Watson 	INP_WUNLOCK(inp);
1398623dce13SRobert Watson 	return (error);
13992c37256eSGarrett Wollman }
14002c37256eSGarrett Wollman 
1401b287c6c7SBjoern A. Zeeb #ifdef INET
14022c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1403756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1404756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1405756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1406756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1407756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1408756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1409756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1410756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1411756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
141254d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1413756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1414756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1415756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
14162cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1417756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
141854d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1419a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1420a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
14212c37256eSGarrett Wollman };
1422b287c6c7SBjoern A. Zeeb #endif /* INET */
1423df8bae1dSRodney W. Grimes 
1424fb59c426SYoshinobu Inoue #ifdef INET6
1425fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1426756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1427756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1428756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1429756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1430756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1431756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1432756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1433756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1434756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1435756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1436756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1437756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1438756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
14392cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1440756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1441756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1442a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1443a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1444fb59c426SYoshinobu Inoue };
1445fb59c426SYoshinobu Inoue #endif /* INET6 */
1446fb59c426SYoshinobu Inoue 
1447b287c6c7SBjoern A. Zeeb #ifdef INET
1448a0292f23SGarrett Wollman /*
1449a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1450a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
14515200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
14525200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
14535200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
14545200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1455a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1456a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1457a0292f23SGarrett Wollman  */
14580312fbe9SPoul-Henning Kamp static int
1459ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1460a0292f23SGarrett Wollman {
1461a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1462a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
14635200e00eSIan Dowse 	struct in_addr laddr;
14645200e00eSIan Dowse 	u_short lport;
1465c3229e05SDavid Greenman 	int error;
1466a0292f23SGarrett Wollman 
14678501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1468fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1469623dce13SRobert Watson 
1470a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
14714616026fSErmal Luçi 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
14724616026fSErmal Luçi 		if (error)
1473fa046d87SRobert Watson 			goto out;
1474a0292f23SGarrett Wollman 	}
1475a0292f23SGarrett Wollman 
1476a0292f23SGarrett Wollman 	/*
1477a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1478a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1479a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1480a0292f23SGarrett Wollman 	 */
14815200e00eSIan Dowse 	laddr = inp->inp_laddr;
14825200e00eSIan Dowse 	lport = inp->inp_lport;
14835200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1484b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
14855200e00eSIan Dowse 	if (error && oinp == NULL)
1486fa046d87SRobert Watson 		goto out;
1487fa046d87SRobert Watson 	if (oinp) {
1488fa046d87SRobert Watson 		error = EADDRINUSE;
1489fa046d87SRobert Watson 		goto out;
1490fa046d87SRobert Watson 	}
14915200e00eSIan Dowse 	inp->inp_laddr = laddr;
149215bd2b43SDavid Greenman 	in_pcbrehash(inp);
1493fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1494a0292f23SGarrett Wollman 
1495087b55eaSAndre Oppermann 	/*
1496087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1497087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1498087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1499087b55eaSAndre Oppermann 	 */
1500a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
15019b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1502a0292f23SGarrett Wollman 		tp->request_r_scale++;
1503a0292f23SGarrett Wollman 
1504a0292f23SGarrett Wollman 	soisconnecting(so);
150578b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
150657f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
15078e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
15088e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
15098e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1510a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1511a45d2726SAndras Olah 
1512a0292f23SGarrett Wollman 	return 0;
1513fa046d87SRobert Watson 
1514fa046d87SRobert Watson out:
1515fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1516fa046d87SRobert Watson 	return (error);
1517a0292f23SGarrett Wollman }
1518b287c6c7SBjoern A. Zeeb #endif /* INET */
1519a0292f23SGarrett Wollman 
1520fb59c426SYoshinobu Inoue #ifdef INET6
1521fb59c426SYoshinobu Inoue static int
1522ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1523fb59c426SYoshinobu Inoue {
1524a7e201bbSAndrey V. Elsukov 	struct inpcb *inp = tp->t_inpcb;
1525fb59c426SYoshinobu Inoue 	int error;
1526fb59c426SYoshinobu Inoue 
15278501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1528fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1529623dce13SRobert Watson 
1530fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
15314616026fSErmal Luçi 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
15324616026fSErmal Luçi 		if (error)
1533fa046d87SRobert Watson 			goto out;
1534fb59c426SYoshinobu Inoue 	}
1535a7e201bbSAndrey V. Elsukov 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1536a7e201bbSAndrey V. Elsukov 	if (error != 0)
1537b598155aSRobert Watson 		goto out;
1538fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1539fb59c426SYoshinobu Inoue 
1540fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1541fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1542970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1543fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1544fb59c426SYoshinobu Inoue 
1545a7e201bbSAndrey V. Elsukov 	soisconnecting(inp->inp_socket);
154678b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
154757f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
15488e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
15498e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
15508e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1551fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1552fb59c426SYoshinobu Inoue 
1553fb59c426SYoshinobu Inoue 	return 0;
1554fa046d87SRobert Watson 
1555fa046d87SRobert Watson out:
1556fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1557fa046d87SRobert Watson 	return error;
1558fb59c426SYoshinobu Inoue }
1559fb59c426SYoshinobu Inoue #endif /* INET6 */
1560fb59c426SYoshinobu Inoue 
1561cfe8b629SGarrett Wollman /*
1562b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1563b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1564b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1565b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1566b8af5dfaSRobert Watson  * from Linux.
1567b8af5dfaSRobert Watson  */
1568b8af5dfaSRobert Watson static void
1569ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1570b8af5dfaSRobert Watson {
1571b8af5dfaSRobert Watson 
15728501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1573b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1574b8af5dfaSRobert Watson 
1575b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1576b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1577b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
15783529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1579b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1580b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1581b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1582b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1583b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1584b8af5dfaSRobert Watson 	}
15853cf38784SMichael Tuexen 	if (tp->t_flags2 & TF2_ECN_PERMIT)
15865a17b6adSMichael Tuexen 		ti->tcpi_options |= TCPI_OPT_ECN;
15871baaf834SBruce M Simpson 
158843d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
15893ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
15901baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
15911baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
15921baaf834SBruce M Simpson 
1593b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1594b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1595b8af5dfaSRobert Watson 
1596b8af5dfaSRobert Watson 	/*
1597b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1598b8af5dfaSRobert Watson 	 */
1599c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1600535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1601b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
16021c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1603535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
160443d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
160543d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1606f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1607f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1608f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1609a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1610a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1611a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1612a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1613a6456410SNavdeep Parhar 	}
1614a6456410SNavdeep Parhar #endif
1615b8af5dfaSRobert Watson }
1616b8af5dfaSRobert Watson 
1617b8af5dfaSRobert Watson /*
16181e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
16191e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
16201e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
16211e8f5ffaSRobert Watson  * option.
1622cfe8b629SGarrett Wollman  */
1623bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
16248501a69cSRobert Watson 	INP_WLOCK(inp);							\
1625ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
16268501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1627bac5bedfSConrad Meyer 		cleanup;						\
16281e8f5ffaSRobert Watson 		return (ECONNRESET);					\
16291e8f5ffaSRobert Watson 	}								\
16301e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
16311e8f5ffaSRobert Watson } while(0)
1632bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
16331e8f5ffaSRobert Watson 
1634df8bae1dSRodney W. Grimes int
1635ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1636df8bae1dSRodney W. Grimes {
163755bceb1eSRandall Stewart 	int	error;
1638df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1639cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
164055bceb1eSRandall Stewart 	struct tcp_function_block *blk;
164155bceb1eSRandall Stewart 	struct tcp_function_set fsn;
1642df8bae1dSRodney W. Grimes 
1643cfe8b629SGarrett Wollman 	error = 0;
1644df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1645623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1646cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1647fb59c426SYoshinobu Inoue #ifdef INET6
16485cd54324SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6PROTO) {
1649fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
16505dff1c38SMichael Tuexen 			/*
16515dff1c38SMichael Tuexen 			 * In case of the IPV6_USE_MIN_MTU socket option,
16525dff1c38SMichael Tuexen 			 * the INC_IPV6MINMTU flag to announce a corresponding
16535dff1c38SMichael Tuexen 			 * MSS during the initial handshake.
16545dff1c38SMichael Tuexen 			 * If the TCP connection is not in the front states,
16555dff1c38SMichael Tuexen 			 * just reduce the MSS being used.
16565dff1c38SMichael Tuexen 			 * This avoids the sending of TCP segments which will
16575dff1c38SMichael Tuexen 			 * be fragmented at the IPv6 layer.
16585dff1c38SMichael Tuexen 			 */
16595dff1c38SMichael Tuexen 			if ((error == 0) &&
16605dff1c38SMichael Tuexen 			    (sopt->sopt_dir == SOPT_SET) &&
16615dff1c38SMichael Tuexen 			    (sopt->sopt_level == IPPROTO_IPV6) &&
16625dff1c38SMichael Tuexen 			    (sopt->sopt_name == IPV6_USE_MIN_MTU)) {
16635dff1c38SMichael Tuexen 				INP_WLOCK(inp);
16645dff1c38SMichael Tuexen 				if ((inp->inp_flags &
16655dff1c38SMichael Tuexen 				    (INP_TIMEWAIT | INP_DROPPED))) {
16665dff1c38SMichael Tuexen 					INP_WUNLOCK(inp);
16675dff1c38SMichael Tuexen 					return (ECONNRESET);
16685dff1c38SMichael Tuexen 				}
16695dff1c38SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_IPV6MINMTU;
16705dff1c38SMichael Tuexen 				tp = intotcpcb(inp);
16715dff1c38SMichael Tuexen 				if ((tp->t_state >= TCPS_SYN_SENT) &&
16725dff1c38SMichael Tuexen 				    (inp->inp_inc.inc_flags & INC_ISIPV6)) {
16735dff1c38SMichael Tuexen 					struct ip6_pktopts *opt;
16745dff1c38SMichael Tuexen 
16755dff1c38SMichael Tuexen 					opt = inp->in6p_outputopts;
16765dff1c38SMichael Tuexen 					if ((opt != NULL) &&
16775dff1c38SMichael Tuexen 					    (opt->ip6po_minmtu ==
16785dff1c38SMichael Tuexen 					    IP6PO_MINMTU_ALL)) {
16795dff1c38SMichael Tuexen 						if (tp->t_maxseg > TCP6_MSS) {
16805dff1c38SMichael Tuexen 							tp->t_maxseg = TCP6_MSS;
16815dff1c38SMichael Tuexen 						}
16825dff1c38SMichael Tuexen 					}
16835dff1c38SMichael Tuexen 				}
16845dff1c38SMichael Tuexen 				INP_WUNLOCK(inp);
16855dff1c38SMichael Tuexen 			}
1686b287c6c7SBjoern A. Zeeb 		}
1687fb59c426SYoshinobu Inoue #endif /* INET6 */
1688b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1689b287c6c7SBjoern A. Zeeb 		else
1690b287c6c7SBjoern A. Zeeb #endif
1691b287c6c7SBjoern A. Zeeb #ifdef INET
1692b287c6c7SBjoern A. Zeeb 		{
1693cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
16941e8f5ffaSRobert Watson 		}
16951e8f5ffaSRobert Watson #endif
1696df8bae1dSRodney W. Grimes 		return (error);
1697df8bae1dSRodney W. Grimes 	}
169868cea2b1SJohn Baldwin 	INP_WLOCK(inp);
1699ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
17008501a69cSRobert Watson 		INP_WUNLOCK(inp);
17011e8f5ffaSRobert Watson 		return (ECONNRESET);
1702623dce13SRobert Watson 	}
170355bceb1eSRandall Stewart 	tp = intotcpcb(inp);
170455bceb1eSRandall Stewart 	/*
170555bceb1eSRandall Stewart 	 * Protect the TCP option TCP_FUNCTION_BLK so
170655bceb1eSRandall Stewart 	 * that a sub-function can *never* overwrite this.
170755bceb1eSRandall Stewart 	 */
170855bceb1eSRandall Stewart 	if ((sopt->sopt_dir == SOPT_SET) &&
170955bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
171055bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
171155bceb1eSRandall Stewart 		error = sooptcopyin(sopt, &fsn, sizeof fsn,
171255bceb1eSRandall Stewart 		    sizeof fsn);
171355bceb1eSRandall Stewart 		if (error)
171455bceb1eSRandall Stewart 			return (error);
171555bceb1eSRandall Stewart 		INP_WLOCK_RECHECK(inp);
171655bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
171755bceb1eSRandall Stewart 		if (blk == NULL) {
171855bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
171955bceb1eSRandall Stewart 			return (ENOENT);
172055bceb1eSRandall Stewart 		}
1721587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1722587d67c0SRandall Stewart 			/* You already have this */
1723587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1724587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1725587d67c0SRandall Stewart 			return (0);
1726587d67c0SRandall Stewart 		}
1727587d67c0SRandall Stewart 		if (tp->t_state != TCPS_CLOSED) {
1728587d67c0SRandall Stewart 			/*
1729587d67c0SRandall Stewart 			 * The user has advanced the state
1730587d67c0SRandall Stewart 			 * past the initial point, we may not
1731587d67c0SRandall Stewart 			 * be able to switch.
1732587d67c0SRandall Stewart 			 */
1733587d67c0SRandall Stewart 			if (blk->tfb_tcp_handoff_ok != NULL) {
1734587d67c0SRandall Stewart 				/*
1735587d67c0SRandall Stewart 				 * Does the stack provide a
1736587d67c0SRandall Stewart 				 * query mechanism, if so it may
1737587d67c0SRandall Stewart 				 * still be possible?
1738587d67c0SRandall Stewart 				 */
1739587d67c0SRandall Stewart 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1740c6c0be27SMichael Tuexen 			} else
1741c6c0be27SMichael Tuexen 				error = EINVAL;
1742587d67c0SRandall Stewart 			if (error) {
1743587d67c0SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1744587d67c0SRandall Stewart 				INP_WUNLOCK(inp);
1745587d67c0SRandall Stewart 				return(error);
1746587d67c0SRandall Stewart 			}
1747587d67c0SRandall Stewart 		}
174855bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
174955bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
175055bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
175155bceb1eSRandall Stewart 			return (ENOENT);
175255bceb1eSRandall Stewart 		}
175355bceb1eSRandall Stewart 		/*
175455bceb1eSRandall Stewart 		 * Release the old refcnt, the
1755587d67c0SRandall Stewart 		 * lookup acquired a ref on the
1756587d67c0SRandall Stewart 		 * new one already.
175755bceb1eSRandall Stewart 		 */
1758587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1759587d67c0SRandall Stewart 			/*
1760587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1761587d67c0SRandall Stewart 			 * the tcb is not going away.
1762587d67c0SRandall Stewart 			 */
1763587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1764587d67c0SRandall Stewart 		}
17653ee9c3c4SRandall Stewart #ifdef TCPHPTS
17663ee9c3c4SRandall Stewart 		/* Assure that we are not on any hpts */
17673ee9c3c4SRandall Stewart 		tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
17683ee9c3c4SRandall Stewart #endif
17693ee9c3c4SRandall Stewart 		if (blk->tfb_tcp_fb_init) {
17703ee9c3c4SRandall Stewart 			error = (*blk->tfb_tcp_fb_init)(tp);
17713ee9c3c4SRandall Stewart 			if (error) {
17723ee9c3c4SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
17733ee9c3c4SRandall Stewart 				if (tp->t_fb->tfb_tcp_fb_init) {
17743ee9c3c4SRandall Stewart 					if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0)  {
17753ee9c3c4SRandall Stewart 						/* Fall back failed, drop the connection */
17763ee9c3c4SRandall Stewart 						INP_WUNLOCK(inp);
17773ee9c3c4SRandall Stewart 						soabort(so);
17783ee9c3c4SRandall Stewart 						return(error);
17793ee9c3c4SRandall Stewart 					}
17803ee9c3c4SRandall Stewart 				}
17813ee9c3c4SRandall Stewart 				goto err_out;
17823ee9c3c4SRandall Stewart 			}
17833ee9c3c4SRandall Stewart 		}
178455bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
178555bceb1eSRandall Stewart 		tp->t_fb = blk;
178655bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
178755bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
178855bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
178955bceb1eSRandall Stewart 			     sopt->sopt_name);
179055bceb1eSRandall Stewart 		}
179155bceb1eSRandall Stewart #endif
17923ee9c3c4SRandall Stewart err_out:
179355bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
179455bceb1eSRandall Stewart 		return (error);
179555bceb1eSRandall Stewart 	} else if ((sopt->sopt_dir == SOPT_GET) &&
179655bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1797c73b6f4dSEd Maste 		strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1798c73b6f4dSEd Maste 		    TCP_FUNCTION_NAME_LEN_MAX);
1799c73b6f4dSEd Maste 		fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
180055bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
180155bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
180255bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
180355bceb1eSRandall Stewart 		return (error);
180455bceb1eSRandall Stewart 	}
180555bceb1eSRandall Stewart 	/* Pass in the INP locked, called must unlock it */
180655bceb1eSRandall Stewart 	return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
180755bceb1eSRandall Stewart }
180855bceb1eSRandall Stewart 
18092529f56eSJonathan T. Looney /*
18102529f56eSJonathan T. Looney  * If this assert becomes untrue, we need to change the size of the buf
18112529f56eSJonathan T. Looney  * variable in tcp_default_ctloutput().
18122529f56eSJonathan T. Looney  */
18132529f56eSJonathan T. Looney #ifdef CTASSERT
18142529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
18152529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
18162529f56eSJonathan T. Looney #endif
18172529f56eSJonathan T. Looney 
181855bceb1eSRandall Stewart int
181955bceb1eSRandall Stewart tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
182055bceb1eSRandall Stewart {
182155bceb1eSRandall Stewart 	int	error, opt, optval;
182255bceb1eSRandall Stewart 	u_int	ui;
182355bceb1eSRandall Stewart 	struct	tcp_info ti;
1824b2e60773SJohn Baldwin #ifdef KERN_TLS
1825b2e60773SJohn Baldwin 	struct tls_enable tls;
1826b2e60773SJohn Baldwin #endif
182755bceb1eSRandall Stewart 	struct cc_algo *algo;
18282529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
1829*adc56f5aSEdward Tomasz Napierala #ifdef STATS
1830*adc56f5aSEdward Tomasz Napierala 	struct statsblob *sbp;
1831*adc56f5aSEdward Tomasz Napierala #endif
1832af6fef3aSGleb Smirnoff 	size_t	len;
1833df8bae1dSRodney W. Grimes 
1834d519cedbSGleb Smirnoff 	/*
1835d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
1836d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
1837d519cedbSGleb Smirnoff 	 */
1838d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
1839d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
1840d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1841c8b53cedSMichael Tuexen 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
1842c8b53cedSMichael Tuexen 			return (EINVAL);
1843af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1844af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1845d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
1846d519cedbSGleb Smirnoff 		if (error) {
1847af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
1848d519cedbSGleb Smirnoff 			return (error);
1849d519cedbSGleb Smirnoff 		}
1850bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1851d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
1852af6fef3aSGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1853d519cedbSGleb Smirnoff 		else
1854d519cedbSGleb Smirnoff 			error = ENOENT;
1855d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1856d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
1857af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1858af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
1859d519cedbSGleb Smirnoff 		return (error);
1860d519cedbSGleb Smirnoff 	}
1861d519cedbSGleb Smirnoff 
1862cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1863cfe8b629SGarrett Wollman 	case SOPT_SET:
1864cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1865fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
186688f6b043SBruce M Simpson 		case TCP_MD5SIG:
1867fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
18688501a69cSRobert Watson 				INP_WUNLOCK(inp);
1869fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
1870fcf59617SAndrey V. Elsukov 			}
1871fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
18721cfd4b53SBruce M Simpson 			if (error)
18731e8f5ffaSRobert Watson 				return (error);
187409fe6320SNavdeep Parhar 			goto unlock_and_done;
1875fcf59617SAndrey V. Elsukov #endif /* IPSEC */
187609fe6320SNavdeep Parhar 
1877df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1878cfe8b629SGarrett Wollman 		case TCP_NOOPT:
18798501a69cSRobert Watson 			INP_WUNLOCK(inp);
1880cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1881cfe8b629SGarrett Wollman 			    sizeof optval);
1882cfe8b629SGarrett Wollman 			if (error)
18831e8f5ffaSRobert Watson 				return (error);
1884cfe8b629SGarrett Wollman 
18858501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1886cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1887cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1888cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1889cfe8b629SGarrett Wollman 				break;
1890cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1891cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1892cfe8b629SGarrett Wollman 				break;
1893cfe8b629SGarrett Wollman 			default:
1894cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1895cfe8b629SGarrett Wollman 				break;
1896cfe8b629SGarrett Wollman 			}
1897cfe8b629SGarrett Wollman 
1898cfe8b629SGarrett Wollman 			if (optval)
1899cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1900df8bae1dSRodney W. Grimes 			else
1901cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
190209fe6320SNavdeep Parhar unlock_and_done:
190309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
190409fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
190509fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
190609fe6320SNavdeep Parhar 				    sopt->sopt_name);
190709fe6320SNavdeep Parhar 			}
190809fe6320SNavdeep Parhar #endif
19098501a69cSRobert Watson 			INP_WUNLOCK(inp);
1910df8bae1dSRodney W. Grimes 			break;
1911df8bae1dSRodney W. Grimes 
1912007581c0SJonathan Lemon 		case TCP_NOPUSH:
19138501a69cSRobert Watson 			INP_WUNLOCK(inp);
1914007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1915007581c0SJonathan Lemon 			    sizeof optval);
1916007581c0SJonathan Lemon 			if (error)
19171e8f5ffaSRobert Watson 				return (error);
1918007581c0SJonathan Lemon 
19198501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1920007581c0SJonathan Lemon 			if (optval)
1921007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1922d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
1923007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1924d28b9e89SJohn Baldwin 				if (TCPS_HAVEESTABLISHED(tp->t_state))
192555bceb1eSRandall Stewart 					error = tp->t_fb->tfb_tcp_output(tp);
1926007581c0SJonathan Lemon 			}
192709fe6320SNavdeep Parhar 			goto unlock_and_done;
1928007581c0SJonathan Lemon 
1929df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
19308501a69cSRobert Watson 			INP_WUNLOCK(inp);
1931cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1932cfe8b629SGarrett Wollman 			    sizeof optval);
1933cfe8b629SGarrett Wollman 			if (error)
19341e8f5ffaSRobert Watson 				return (error);
1935df8bae1dSRodney W. Grimes 
19368501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
193753369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
1938603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
1939cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1940a0292f23SGarrett Wollman 			else
1941a0292f23SGarrett Wollman 				error = EINVAL;
194209fe6320SNavdeep Parhar 			goto unlock_and_done;
1943a0292f23SGarrett Wollman 
1944b8af5dfaSRobert Watson 		case TCP_INFO:
19458501a69cSRobert Watson 			INP_WUNLOCK(inp);
1946b8af5dfaSRobert Watson 			error = EINVAL;
1947b8af5dfaSRobert Watson 			break;
1948b8af5dfaSRobert Watson 
1949*adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
1950*adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
1951*adc56f5aSEdward Tomasz Napierala #ifdef STATS
1952*adc56f5aSEdward Tomasz Napierala 			error = sooptcopyin(sopt, &optval, sizeof optval,
1953*adc56f5aSEdward Tomasz Napierala 			    sizeof optval);
1954*adc56f5aSEdward Tomasz Napierala 			if (error)
1955*adc56f5aSEdward Tomasz Napierala 				return (error);
1956*adc56f5aSEdward Tomasz Napierala 
1957*adc56f5aSEdward Tomasz Napierala 			if (optval > 0)
1958*adc56f5aSEdward Tomasz Napierala 				sbp = stats_blob_alloc(
1959*adc56f5aSEdward Tomasz Napierala 				    V_tcp_perconn_stats_dflt_tpl, 0);
1960*adc56f5aSEdward Tomasz Napierala 			else
1961*adc56f5aSEdward Tomasz Napierala 				sbp = NULL;
1962*adc56f5aSEdward Tomasz Napierala 
1963*adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
1964*adc56f5aSEdward Tomasz Napierala 			if ((tp->t_stats != NULL && sbp == NULL) ||
1965*adc56f5aSEdward Tomasz Napierala 			    (tp->t_stats == NULL && sbp != NULL)) {
1966*adc56f5aSEdward Tomasz Napierala 				struct statsblob *t = tp->t_stats;
1967*adc56f5aSEdward Tomasz Napierala 				tp->t_stats = sbp;
1968*adc56f5aSEdward Tomasz Napierala 				sbp = t;
1969*adc56f5aSEdward Tomasz Napierala 			}
1970*adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
1971*adc56f5aSEdward Tomasz Napierala 
1972*adc56f5aSEdward Tomasz Napierala 			stats_blob_destroy(sbp);
1973*adc56f5aSEdward Tomasz Napierala #else
1974*adc56f5aSEdward Tomasz Napierala 			return (EOPNOTSUPP);
1975*adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
1976*adc56f5aSEdward Tomasz Napierala 			break;
1977*adc56f5aSEdward Tomasz Napierala 
1978dbc42409SLawrence Stewart 		case TCP_CONGESTION:
1979dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
1980af6fef3aSGleb Smirnoff 			error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1981af6fef3aSGleb Smirnoff 			if (error)
1982dbc42409SLawrence Stewart 				break;
1983af6fef3aSGleb Smirnoff 			buf[sopt->sopt_valsize] = '\0';
1984af6fef3aSGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
198573e263b1SGleb Smirnoff 			CC_LIST_RLOCK();
198673e263b1SGleb Smirnoff 			STAILQ_FOREACH(algo, &cc_list, entries)
198773e263b1SGleb Smirnoff 				if (strncmp(buf, algo->name,
198873e263b1SGleb Smirnoff 				    TCP_CA_NAME_MAX) == 0)
198973e263b1SGleb Smirnoff 					break;
199073e263b1SGleb Smirnoff 			CC_LIST_RUNLOCK();
199173e263b1SGleb Smirnoff 			if (algo == NULL) {
1992af6fef3aSGleb Smirnoff 				INP_WUNLOCK(inp);
199373e263b1SGleb Smirnoff 				error = EINVAL;
199473e263b1SGleb Smirnoff 				break;
199573e263b1SGleb Smirnoff 			}
1996dbc42409SLawrence Stewart 			/*
199773e263b1SGleb Smirnoff 			 * We hold a write lock over the tcb so it's safe to
199873e263b1SGleb Smirnoff 			 * do these things without ordering concerns.
1999dbc42409SLawrence Stewart 			 */
2000dbc42409SLawrence Stewart 			if (CC_ALGO(tp)->cb_destroy != NULL)
2001dbc42409SLawrence Stewart 				CC_ALGO(tp)->cb_destroy(tp->ccv);
200222699887SMatt Macy 			CC_DATA(tp) = NULL;
2003dbc42409SLawrence Stewart 			CC_ALGO(tp) = algo;
2004dbc42409SLawrence Stewart 			/*
200573e263b1SGleb Smirnoff 			 * If something goes pear shaped initialising the new
200673e263b1SGleb Smirnoff 			 * algo, fall back to newreno (which does not
200773e263b1SGleb Smirnoff 			 * require initialisation).
2008dbc42409SLawrence Stewart 			 */
200973e263b1SGleb Smirnoff 			if (algo->cb_init != NULL &&
201073e263b1SGleb Smirnoff 			    algo->cb_init(tp->ccv) != 0) {
2011dbc42409SLawrence Stewart 				CC_ALGO(tp) = &newreno_cc_algo;
2012dbc42409SLawrence Stewart 				/*
201373e263b1SGleb Smirnoff 				 * The only reason init should fail is
2014dbc42409SLawrence Stewart 				 * because of malloc.
2015dbc42409SLawrence Stewart 				 */
2016dbc42409SLawrence Stewart 				error = ENOMEM;
2017dbc42409SLawrence Stewart 			}
201873e263b1SGleb Smirnoff 			INP_WUNLOCK(inp);
201973e263b1SGleb Smirnoff 			break;
2020dbc42409SLawrence Stewart 
2021b2e60773SJohn Baldwin #ifdef KERN_TLS
2022b2e60773SJohn Baldwin 		case TCP_TXTLS_ENABLE:
2023b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2024b2e60773SJohn Baldwin 			error = sooptcopyin(sopt, &tls, sizeof(tls),
2025b2e60773SJohn Baldwin 			    sizeof(tls));
2026b2e60773SJohn Baldwin 			if (error)
2027b2e60773SJohn Baldwin 				break;
2028b2e60773SJohn Baldwin 			error = ktls_enable_tx(so, &tls);
2029b2e60773SJohn Baldwin 			break;
2030b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2031b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2032b2e60773SJohn Baldwin 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2033b2e60773SJohn Baldwin 			if (error)
2034b2e60773SJohn Baldwin 				return (error);
2035b2e60773SJohn Baldwin 
2036b2e60773SJohn Baldwin 			INP_WLOCK_RECHECK(inp);
2037b2e60773SJohn Baldwin 			error = ktls_set_tx_mode(so, ui);
2038b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2039b2e60773SJohn Baldwin 			break;
2040b2e60773SJohn Baldwin #endif
2041b2e60773SJohn Baldwin 
20429077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
20439077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
20449077f387SGleb Smirnoff 		case TCP_KEEPINIT:
20459077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
20469077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
20479077f387SGleb Smirnoff 			if (error)
20489077f387SGleb Smirnoff 				return (error);
20499077f387SGleb Smirnoff 
20509077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
20519077f387SGleb Smirnoff 				error = EINVAL;
20529077f387SGleb Smirnoff 				break;
20539077f387SGleb Smirnoff 			}
20549077f387SGleb Smirnoff 			ui *= hz;
20559077f387SGleb Smirnoff 
20569077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
20579077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
20589077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
20599077f387SGleb Smirnoff 				tp->t_keepidle = ui;
20609077f387SGleb Smirnoff 				/*
20619077f387SGleb Smirnoff 				 * XXX: better check current remaining
20629077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
20639077f387SGleb Smirnoff 				 */
20649077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
20659077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
20669077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
20679077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
20689077f387SGleb Smirnoff 				break;
20699077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
20709077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
20719077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
20729077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
20739077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
20749077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
20759077f387SGleb Smirnoff 				break;
20769077f387SGleb Smirnoff 			case TCP_KEEPINIT:
20779077f387SGleb Smirnoff 				tp->t_keepinit = ui;
20789077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
20799077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
20809077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
20819077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
20829077f387SGleb Smirnoff 				break;
20839077f387SGleb Smirnoff 			}
208409fe6320SNavdeep Parhar 			goto unlock_and_done;
20859077f387SGleb Smirnoff 
208685c05144SGleb Smirnoff 		case TCP_KEEPCNT:
208785c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
208885c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
208985c05144SGleb Smirnoff 			if (error)
209085c05144SGleb Smirnoff 				return (error);
209185c05144SGleb Smirnoff 
209285c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
209385c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
209485c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
209585c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
209685c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
209785c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
209885c05144SGleb Smirnoff 			goto unlock_and_done;
209985c05144SGleb Smirnoff 
210086a996e6SHiren Panchasara #ifdef TCPPCAP
210186a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
210286a996e6SHiren Panchasara 		case TCP_PCAP_IN:
210386a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
210486a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
210586a996e6SHiren Panchasara 			    sizeof optval);
210686a996e6SHiren Panchasara 			if (error)
210786a996e6SHiren Panchasara 				return (error);
210886a996e6SHiren Panchasara 
210986a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
211086a996e6SHiren Panchasara 			if (optval >= 0)
211186a996e6SHiren Panchasara 				tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
211286a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
211386a996e6SHiren Panchasara 					optval);
211486a996e6SHiren Panchasara 			else
211586a996e6SHiren Panchasara 				error = EINVAL;
211686a996e6SHiren Panchasara 			goto unlock_and_done;
211786a996e6SHiren Panchasara #endif
211886a996e6SHiren Panchasara 
2119c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
2120c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
2121c560df6fSPatrick Kelsey 
2122281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2123c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
2124c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
2125281a0fd4SPatrick Kelsey 				return (EPERM);
2126281a0fd4SPatrick Kelsey 
2127c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
2128c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
2129281a0fd4SPatrick Kelsey 			if (error)
2130281a0fd4SPatrick Kelsey 				return (error);
2131281a0fd4SPatrick Kelsey 
2132281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
2133c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
2134c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
2135c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
2136c560df6fSPatrick Kelsey 						error = EPERM;
2137c560df6fSPatrick Kelsey 						goto unlock_and_done;
2138c560df6fSPatrick Kelsey 					}
2139c560df6fSPatrick Kelsey 
2140281a0fd4SPatrick Kelsey 					tp->t_flags |= TF_FASTOPEN;
2141c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
2142281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
2143281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
2144c560df6fSPatrick Kelsey 				} else {
2145c560df6fSPatrick Kelsey 					/*
2146c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
2147c560df6fSPatrick Kelsey 					 * stash it in the client cookie
2148c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
2149c560df6fSPatrick Kelsey 					 * connect.
2150c560df6fSPatrick Kelsey 					 */
2151c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
2152c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
2153c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
2154c560df6fSPatrick Kelsey 						       tfo_optval.psk,
2155c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
2156c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
2157c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
2158c560df6fSPatrick Kelsey 					}
2159c560df6fSPatrick Kelsey 					tp->t_flags |= TF_FASTOPEN;
2160c560df6fSPatrick Kelsey 				}
2161281a0fd4SPatrick Kelsey 			} else
2162281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
2163281a0fd4SPatrick Kelsey 			goto unlock_and_done;
2164c560df6fSPatrick Kelsey 		}
2165281a0fd4SPatrick Kelsey 
2166e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
21672529f56eSJonathan T. Looney 		case TCP_LOG:
21682529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
21692529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
21702529f56eSJonathan T. Looney 			    sizeof optval);
21712529f56eSJonathan T. Looney 			if (error)
21722529f56eSJonathan T. Looney 				return (error);
21732529f56eSJonathan T. Looney 
21742529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
21752529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
21762529f56eSJonathan T. Looney 			goto unlock_and_done;
21772529f56eSJonathan T. Looney 
21782529f56eSJonathan T. Looney 		case TCP_LOGBUF:
21792529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
21802529f56eSJonathan T. Looney 			error = EINVAL;
21812529f56eSJonathan T. Looney 			break;
21822529f56eSJonathan T. Looney 
21832529f56eSJonathan T. Looney 		case TCP_LOGID:
21842529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
21852529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
21862529f56eSJonathan T. Looney 			if (error)
21872529f56eSJonathan T. Looney 				break;
21882529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
21892529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
21902529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
21912529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
21922529f56eSJonathan T. Looney 			break;
21932529f56eSJonathan T. Looney 
21942529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
21952529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
21962529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
21972529f56eSJonathan T. Looney 			error =
21982529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
21992529f56eSJonathan T. Looney 			if (error)
22002529f56eSJonathan T. Looney 				break;
22012529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
22022529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
22032529f56eSJonathan T. Looney 			if (sopt->sopt_name == TCP_LOGDUMP) {
22042529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
22052529f56eSJonathan T. Looney 				    M_WAITOK, true);
22062529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
22072529f56eSJonathan T. Looney 			} else {
22082529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
22092529f56eSJonathan T. Looney 				/*
22102529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
22112529f56eSJonathan T. Looney 				 * INP lock.
22122529f56eSJonathan T. Looney 				 */
22132529f56eSJonathan T. Looney 			}
22142529f56eSJonathan T. Looney 			break;
2215e24e5683SJonathan T. Looney #endif
22162529f56eSJonathan T. Looney 
2217df8bae1dSRodney W. Grimes 		default:
22188501a69cSRobert Watson 			INP_WUNLOCK(inp);
2219df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2220df8bae1dSRodney W. Grimes 			break;
2221df8bae1dSRodney W. Grimes 		}
2222df8bae1dSRodney W. Grimes 		break;
2223df8bae1dSRodney W. Grimes 
2224cfe8b629SGarrett Wollman 	case SOPT_GET:
22251e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
2226cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2227fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
222888f6b043SBruce M Simpson 		case TCP_MD5SIG:
2229fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
22308501a69cSRobert Watson 				INP_WUNLOCK(inp);
2231fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2232fcf59617SAndrey V. Elsukov 			}
2233fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
22341cfd4b53SBruce M Simpson 			break;
2235265ed012SBruce M Simpson #endif
22361e8f5ffaSRobert Watson 
2237df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2238cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
22398501a69cSRobert Watson 			INP_WUNLOCK(inp);
2240b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2241df8bae1dSRodney W. Grimes 			break;
2242df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
2243cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
22448501a69cSRobert Watson 			INP_WUNLOCK(inp);
2245b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2246df8bae1dSRodney W. Grimes 			break;
2247a0292f23SGarrett Wollman 		case TCP_NOOPT:
2248cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
22498501a69cSRobert Watson 			INP_WUNLOCK(inp);
2250b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2251a0292f23SGarrett Wollman 			break;
2252a0292f23SGarrett Wollman 		case TCP_NOPUSH:
2253cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
22548501a69cSRobert Watson 			INP_WUNLOCK(inp);
2255b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2256b8af5dfaSRobert Watson 			break;
2257b8af5dfaSRobert Watson 		case TCP_INFO:
2258b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
22598501a69cSRobert Watson 			INP_WUNLOCK(inp);
2260b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
2261a0292f23SGarrett Wollman 			break;
2262*adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2263*adc56f5aSEdward Tomasz Napierala 			{
2264*adc56f5aSEdward Tomasz Napierala #ifdef STATS
2265*adc56f5aSEdward Tomasz Napierala 			int nheld;
2266*adc56f5aSEdward Tomasz Napierala 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2267*adc56f5aSEdward Tomasz Napierala 
2268*adc56f5aSEdward Tomasz Napierala 			error = 0;
2269*adc56f5aSEdward Tomasz Napierala 			socklen_t outsbsz = sopt->sopt_valsize;
2270*adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats == NULL)
2271*adc56f5aSEdward Tomasz Napierala 				error = ENOENT;
2272*adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= tp->t_stats->cursz)
2273*adc56f5aSEdward Tomasz Napierala 				outsbsz = tp->t_stats->cursz;
2274*adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= sizeof(struct statsblob))
2275*adc56f5aSEdward Tomasz Napierala 				outsbsz = sizeof(struct statsblob);
2276*adc56f5aSEdward Tomasz Napierala 			else
2277*adc56f5aSEdward Tomasz Napierala 				error = EINVAL;
2278*adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2279*adc56f5aSEdward Tomasz Napierala 			if (error)
2280*adc56f5aSEdward Tomasz Napierala 				break;
2281*adc56f5aSEdward Tomasz Napierala 
2282*adc56f5aSEdward Tomasz Napierala 			sbp = sopt->sopt_val;
2283*adc56f5aSEdward Tomasz Napierala 			nheld = atop(round_page(((vm_offset_t)sbp) +
2284*adc56f5aSEdward Tomasz Napierala 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2285*adc56f5aSEdward Tomasz Napierala 			vm_page_t ma[nheld];
2286*adc56f5aSEdward Tomasz Napierala 			if (vm_fault_quick_hold_pages(
2287*adc56f5aSEdward Tomasz Napierala 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2288*adc56f5aSEdward Tomasz Napierala 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2289*adc56f5aSEdward Tomasz Napierala 			    nheld) < 0) {
2290*adc56f5aSEdward Tomasz Napierala 				error = EFAULT;
2291*adc56f5aSEdward Tomasz Napierala 				break;
2292*adc56f5aSEdward Tomasz Napierala 			}
2293*adc56f5aSEdward Tomasz Napierala 
2294*adc56f5aSEdward Tomasz Napierala 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2295*adc56f5aSEdward Tomasz Napierala 			    SIZEOF_MEMBER(struct statsblob, flags))))
2296*adc56f5aSEdward Tomasz Napierala 				goto unhold;
2297*adc56f5aSEdward Tomasz Napierala 
2298*adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2299*adc56f5aSEdward Tomasz Napierala 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2300*adc56f5aSEdward Tomasz Napierala 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2301*adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2302*adc56f5aSEdward Tomasz Napierala 			sopt->sopt_valsize = outsbsz;
2303*adc56f5aSEdward Tomasz Napierala unhold:
2304*adc56f5aSEdward Tomasz Napierala 			vm_page_unhold_pages(ma, nheld);
2305*adc56f5aSEdward Tomasz Napierala #else
2306*adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2307*adc56f5aSEdward Tomasz Napierala 			error = EOPNOTSUPP;
2308*adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2309*adc56f5aSEdward Tomasz Napierala 			break;
2310*adc56f5aSEdward Tomasz Napierala 			}
2311dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2312af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2313dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2314af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
2315dbc42409SLawrence Stewart 			break;
23162f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
23172f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
23182f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
23192f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
23202f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
23212f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
23225a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
23232f3eb7f4SGleb Smirnoff 				break;
23242f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
23255a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
23262f3eb7f4SGleb Smirnoff 				break;
23272f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
23285a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
23292f3eb7f4SGleb Smirnoff 				break;
23302f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
23315a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
23322f3eb7f4SGleb Smirnoff 				break;
23332f3eb7f4SGleb Smirnoff 			}
23342f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
23352f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
23362f3eb7f4SGleb Smirnoff 			break;
233786a996e6SHiren Panchasara #ifdef TCPPCAP
233886a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
233986a996e6SHiren Panchasara 		case TCP_PCAP_IN:
234086a996e6SHiren Panchasara 			optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
234186a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
234286a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
234386a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
234486a996e6SHiren Panchasara 			break;
234586a996e6SHiren Panchasara #endif
2346281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
2347281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
2348281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2349281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
2350281a0fd4SPatrick Kelsey 			break;
2351e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
23522529f56eSJonathan T. Looney 		case TCP_LOG:
23532529f56eSJonathan T. Looney 			optval = tp->t_logstate;
23542529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
23552529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
23562529f56eSJonathan T. Looney 			break;
23572529f56eSJonathan T. Looney 		case TCP_LOGBUF:
23582529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
23592529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
23602529f56eSJonathan T. Looney 			break;
23612529f56eSJonathan T. Looney 		case TCP_LOGID:
23622529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
23632529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
23642529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
23652529f56eSJonathan T. Looney 			break;
23662529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
23672529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
23682529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
23692529f56eSJonathan T. Looney 			error = EINVAL;
23702529f56eSJonathan T. Looney 			break;
2371e24e5683SJonathan T. Looney #endif
2372b2e60773SJohn Baldwin #ifdef KERN_TLS
2373b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2374b2e60773SJohn Baldwin 			optval = ktls_get_tx_mode(so);
2375b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2376b2e60773SJohn Baldwin 			error = sooptcopyout(sopt, &optval, sizeof(optval));
2377b2e60773SJohn Baldwin 			break;
2378b2e60773SJohn Baldwin #endif
2379df8bae1dSRodney W. Grimes 		default:
23808501a69cSRobert Watson 			INP_WUNLOCK(inp);
2381df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2382df8bae1dSRodney W. Grimes 			break;
2383df8bae1dSRodney W. Grimes 		}
2384df8bae1dSRodney W. Grimes 		break;
2385df8bae1dSRodney W. Grimes 	}
2386df8bae1dSRodney W. Grimes 	return (error);
2387df8bae1dSRodney W. Grimes }
23888501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2389bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2390df8bae1dSRodney W. Grimes 
239126e30fbbSDavid Greenman /*
2392df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
2393df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
2394df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
2395df8bae1dSRodney W. Grimes  */
23960312fbe9SPoul-Henning Kamp static int
2397ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so)
2398df8bae1dSRodney W. Grimes {
2399ad3f9ab3SAndre Oppermann 	struct tcpcb *tp;
2400df8bae1dSRodney W. Grimes 	struct inpcb *inp;
24016573d758SMatt Macy 	struct epoch_tracker et;
2402df8bae1dSRodney W. Grimes 	int error;
2403df8bae1dSRodney W. Grimes 
2404df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
2405e233e2acSAndre Oppermann 		error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
2406df8bae1dSRodney W. Grimes 		if (error)
2407df8bae1dSRodney W. Grimes 			return (error);
2408df8bae1dSRodney W. Grimes 	}
24096741ecf5SAndre Oppermann 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
24106741ecf5SAndre Oppermann 	so->so_snd.sb_flags |= SB_AUTOSIZE;
241197a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
2412603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_tcbinfo);
2413f2de87feSRobert Watson 	if (error) {
241497a95ee1SGleb Smirnoff 		NET_EPOCH_EXIT(et);
2415df8bae1dSRodney W. Grimes 		return (error);
2416f2de87feSRobert Watson 	}
2417df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
2418fb59c426SYoshinobu Inoue #ifdef INET6
24195cd54324SBjoern A. Zeeb 	if (inp->inp_vflag & INP_IPV6PROTO) {
2420fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
242163ec505aSMichael Tuexen 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
242263ec505aSMichael Tuexen 			inp->inp_vflag |= INP_IPV4;
2423fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
2424fb59c426SYoshinobu Inoue 	}
2425fb59c426SYoshinobu Inoue 	else
2426fb59c426SYoshinobu Inoue #endif
2427cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
2428df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
2429623dce13SRobert Watson 	if (tp == NULL) {
2430df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
24310206cdb8SBjoern A. Zeeb 		in_pcbfree(inp);
243297a95ee1SGleb Smirnoff 		NET_EPOCH_EXIT(et);
2433df8bae1dSRodney W. Grimes 		return (ENOBUFS);
2434df8bae1dSRodney W. Grimes 	}
2435df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
24368501a69cSRobert Watson 	INP_WUNLOCK(inp);
243797a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
2438bf840a17SGleb Smirnoff 	TCPSTATES_INC(TCPS_CLOSED);
2439df8bae1dSRodney W. Grimes 	return (0);
2440df8bae1dSRodney W. Grimes }
2441df8bae1dSRodney W. Grimes 
2442df8bae1dSRodney W. Grimes /*
2443df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2444df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2445df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2446df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2447df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2448df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2449df8bae1dSRodney W. Grimes  */
2450623dce13SRobert Watson static void
2451ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2452df8bae1dSRodney W. Grimes {
2453e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
2454e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
2455e6e0b5ffSRobert Watson 
245697a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
24578501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2458df8bae1dSRodney W. Grimes 
2459623dce13SRobert Watson 	/*
2460623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2461623dce13SRobert Watson 	 * socket is still open.
2462623dce13SRobert Watson 	 */
24638db239dcSMichael Tuexen 	if (tp->t_state < TCPS_ESTABLISHED &&
24648db239dcSMichael Tuexen 	    !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2465df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2466623dce13SRobert Watson 		KASSERT(tp != NULL,
2467623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2468623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2469243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2470623dce13SRobert Watson 		KASSERT(tp != NULL,
2471623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2472623dce13SRobert Watson 	} else {
2473df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2474df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2475623dce13SRobert Watson 		tcp_usrclosed(tp);
2476ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
247755bceb1eSRandall Stewart 			tp->t_fb->tfb_tcp_output(tp);
2478df8bae1dSRodney W. Grimes 	}
2479df8bae1dSRodney W. Grimes }
2480df8bae1dSRodney W. Grimes 
2481df8bae1dSRodney W. Grimes /*
2482df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2483df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2484df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2485df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2486df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2487df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2488df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2489df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2490df8bae1dSRodney W. Grimes  */
2491623dce13SRobert Watson static void
2492ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2493df8bae1dSRodney W. Grimes {
2494df8bae1dSRodney W. Grimes 
249597a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
24968501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
2497e6e0b5ffSRobert Watson 
2498df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2499df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
250009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
250109fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
250209fe6320SNavdeep Parhar #endif
2503550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2504bc65987aSKip Macy 		/* FALLTHROUGH */
2505bc65987aSKip Macy 	case TCPS_CLOSED:
2506df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2507623dce13SRobert Watson 		/*
2508623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2509623dce13SRobert Watson 		 * still open.
2510623dce13SRobert Watson 		 */
2511623dce13SRobert Watson 		KASSERT(tp != NULL,
2512623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2513df8bae1dSRodney W. Grimes 		break;
2514df8bae1dSRodney W. Grimes 
2515a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2516df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2517a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2518a0292f23SGarrett Wollman 		break;
2519a0292f23SGarrett Wollman 
2520df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
252157f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2522df8bae1dSRodney W. Grimes 		break;
2523df8bae1dSRodney W. Grimes 
2524df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
252557f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2526df8bae1dSRodney W. Grimes 		break;
2527df8bae1dSRodney W. Grimes 	}
2528abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
2529df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
2530abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
25317c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
25327c72af87SMohan Srinivasan 			int timeout;
25337c72af87SMohan Srinivasan 
25347c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
25359077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2536b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2537b6239c4aSAndras Olah 		}
2538df8bae1dSRodney W. Grimes 	}
25397c72af87SMohan Srinivasan }
2540497057eeSRobert Watson 
2541497057eeSRobert Watson #ifdef DDB
2542497057eeSRobert Watson static void
2543497057eeSRobert Watson db_print_indent(int indent)
2544497057eeSRobert Watson {
2545497057eeSRobert Watson 	int i;
2546497057eeSRobert Watson 
2547497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2548497057eeSRobert Watson 		db_printf(" ");
2549497057eeSRobert Watson }
2550497057eeSRobert Watson 
2551497057eeSRobert Watson static void
2552497057eeSRobert Watson db_print_tstate(int t_state)
2553497057eeSRobert Watson {
2554497057eeSRobert Watson 
2555497057eeSRobert Watson 	switch (t_state) {
2556497057eeSRobert Watson 	case TCPS_CLOSED:
2557497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2558497057eeSRobert Watson 		return;
2559497057eeSRobert Watson 
2560497057eeSRobert Watson 	case TCPS_LISTEN:
2561497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2562497057eeSRobert Watson 		return;
2563497057eeSRobert Watson 
2564497057eeSRobert Watson 	case TCPS_SYN_SENT:
2565497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2566497057eeSRobert Watson 		return;
2567497057eeSRobert Watson 
2568497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2569497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2570497057eeSRobert Watson 		return;
2571497057eeSRobert Watson 
2572497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2573497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2574497057eeSRobert Watson 		return;
2575497057eeSRobert Watson 
2576497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2577497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2578497057eeSRobert Watson 		return;
2579497057eeSRobert Watson 
2580497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2581497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2582497057eeSRobert Watson 		return;
2583497057eeSRobert Watson 
2584497057eeSRobert Watson 	case TCPS_CLOSING:
2585497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2586497057eeSRobert Watson 		return;
2587497057eeSRobert Watson 
2588497057eeSRobert Watson 	case TCPS_LAST_ACK:
2589497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2590497057eeSRobert Watson 		return;
2591497057eeSRobert Watson 
2592497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2593497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2594497057eeSRobert Watson 		return;
2595497057eeSRobert Watson 
2596497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2597497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2598497057eeSRobert Watson 		return;
2599497057eeSRobert Watson 
2600497057eeSRobert Watson 	default:
2601497057eeSRobert Watson 		db_printf("unknown");
2602497057eeSRobert Watson 		return;
2603497057eeSRobert Watson 	}
2604497057eeSRobert Watson }
2605497057eeSRobert Watson 
2606497057eeSRobert Watson static void
2607497057eeSRobert Watson db_print_tflags(u_int t_flags)
2608497057eeSRobert Watson {
2609497057eeSRobert Watson 	int comma;
2610497057eeSRobert Watson 
2611497057eeSRobert Watson 	comma = 0;
2612497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2613497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2614497057eeSRobert Watson 		comma = 1;
2615497057eeSRobert Watson 	}
2616497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2617497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2618497057eeSRobert Watson 		comma = 1;
2619497057eeSRobert Watson 	}
2620497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2621497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2622497057eeSRobert Watson 		comma = 1;
2623497057eeSRobert Watson 	}
2624497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2625497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2626497057eeSRobert Watson 		comma = 1;
2627497057eeSRobert Watson 	}
2628497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2629497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2630497057eeSRobert Watson 		comma = 1;
2631497057eeSRobert Watson 	}
2632497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2633497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2634497057eeSRobert Watson 		comma = 1;
2635497057eeSRobert Watson 	}
2636497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2637497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2638497057eeSRobert Watson 		comma = 1;
2639497057eeSRobert Watson 	}
2640497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2641497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2642497057eeSRobert Watson 		comma = 1;
2643497057eeSRobert Watson 	}
2644497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2645497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2646497057eeSRobert Watson 		comma = 1;
2647497057eeSRobert Watson 	}
2648497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2649497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2650497057eeSRobert Watson 		comma = 1;
2651497057eeSRobert Watson 	}
2652497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2653497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2654497057eeSRobert Watson 		comma = 1;
2655497057eeSRobert Watson 	}
2656497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2657497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2658497057eeSRobert Watson 		comma = 1;
2659497057eeSRobert Watson 	}
2660497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2661497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2662497057eeSRobert Watson 		comma = 1;
2663497057eeSRobert Watson 	}
2664497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2665497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2666497057eeSRobert Watson 		comma = 1;
2667497057eeSRobert Watson 	}
2668497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
2669497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2670497057eeSRobert Watson 		comma = 1;
2671497057eeSRobert Watson 	}
2672497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2673497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2674497057eeSRobert Watson 		comma = 1;
2675497057eeSRobert Watson 	}
2676497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2677497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2678497057eeSRobert Watson 		comma = 1;
2679497057eeSRobert Watson 	}
2680497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2681497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2682497057eeSRobert Watson 		comma = 1;
2683497057eeSRobert Watson 	}
2684dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
2685dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2686dbc42409SLawrence Stewart 		comma = 1;
2687dbc42409SLawrence Stewart 	}
2688497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2689497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2690497057eeSRobert Watson 		comma = 1;
2691497057eeSRobert Watson 	}
2692497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2693497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2694497057eeSRobert Watson 		comma = 1;
2695497057eeSRobert Watson 	}
2696497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2697497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2698497057eeSRobert Watson 		comma = 1;
2699497057eeSRobert Watson 	}
2700497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2701497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2702497057eeSRobert Watson 		comma = 1;
2703497057eeSRobert Watson 	}
2704281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2705281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2706281a0fd4SPatrick Kelsey 		comma = 1;
2707281a0fd4SPatrick Kelsey 	}
2708497057eeSRobert Watson }
2709497057eeSRobert Watson 
2710497057eeSRobert Watson static void
27113cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2)
27123cf38784SMichael Tuexen {
27133cf38784SMichael Tuexen 	int comma;
27143cf38784SMichael Tuexen 
27153cf38784SMichael Tuexen 	comma = 0;
27163cf38784SMichael Tuexen 	if (t_flags2 & TF2_ECN_PERMIT) {
27173cf38784SMichael Tuexen 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
27183cf38784SMichael Tuexen 		comma = 1;
27193cf38784SMichael Tuexen 	}
27203cf38784SMichael Tuexen }
27213cf38784SMichael Tuexen 
27223cf38784SMichael Tuexen 
27233cf38784SMichael Tuexen static void
2724497057eeSRobert Watson db_print_toobflags(char t_oobflags)
2725497057eeSRobert Watson {
2726497057eeSRobert Watson 	int comma;
2727497057eeSRobert Watson 
2728497057eeSRobert Watson 	comma = 0;
2729497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
2730497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2731497057eeSRobert Watson 		comma = 1;
2732497057eeSRobert Watson 	}
2733497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
2734497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2735497057eeSRobert Watson 		comma = 1;
2736497057eeSRobert Watson 	}
2737497057eeSRobert Watson }
2738497057eeSRobert Watson 
2739497057eeSRobert Watson static void
2740497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2741497057eeSRobert Watson {
2742497057eeSRobert Watson 
2743497057eeSRobert Watson 	db_print_indent(indent);
2744497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
2745497057eeSRobert Watson 
2746497057eeSRobert Watson 	indent += 2;
2747497057eeSRobert Watson 
2748497057eeSRobert Watson 	db_print_indent(indent);
2749497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
2750c28440dbSRandall Stewart 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2751497057eeSRobert Watson 
2752497057eeSRobert Watson 	db_print_indent(indent);
275385d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
2754e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2755497057eeSRobert Watson 
2756497057eeSRobert Watson 	db_print_indent(indent);
2757e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2758e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
2759497057eeSRobert Watson 
2760497057eeSRobert Watson 	db_print_indent(indent);
2761497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
2762497057eeSRobert Watson 	db_print_tstate(tp->t_state);
2763497057eeSRobert Watson 	db_printf(")\n");
2764497057eeSRobert Watson 
2765497057eeSRobert Watson 	db_print_indent(indent);
2766497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
2767497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
2768497057eeSRobert Watson 	db_printf(")\n");
2769497057eeSRobert Watson 
2770497057eeSRobert Watson 	db_print_indent(indent);
27713cf38784SMichael Tuexen 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
27723cf38784SMichael Tuexen 	db_print_tflags2(tp->t_flags2);
27733cf38784SMichael Tuexen 	db_printf(")\n");
27743cf38784SMichael Tuexen 
27753cf38784SMichael Tuexen 	db_print_indent(indent);
2776497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
2777497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
2778497057eeSRobert Watson 
2779497057eeSRobert Watson 	db_print_indent(indent);
2780497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
2781497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2782497057eeSRobert Watson 
2783497057eeSRobert Watson 	db_print_indent(indent);
2784497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
2785497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
2786497057eeSRobert Watson 
2787497057eeSRobert Watson 	db_print_indent(indent);
27883ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
2789497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2790497057eeSRobert Watson 
2791497057eeSRobert Watson 	db_print_indent(indent);
27923ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
27931c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
2794497057eeSRobert Watson 
2795497057eeSRobert Watson 	db_print_indent(indent);
27963ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
27971c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2798497057eeSRobert Watson 
2799497057eeSRobert Watson 	db_print_indent(indent);
28000c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
28010c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
2802497057eeSRobert Watson 
2803497057eeSRobert Watson 	db_print_indent(indent);
28041c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
28051c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
2806497057eeSRobert Watson 
2807497057eeSRobert Watson 	db_print_indent(indent);
28081c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
28091c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2810497057eeSRobert Watson 
2811497057eeSRobert Watson 	db_print_indent(indent);
2812497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
2813497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2814497057eeSRobert Watson 	    tp->t_rttbest);
2815497057eeSRobert Watson 
2816497057eeSRobert Watson 	db_print_indent(indent);
28173ac12506SJonathan T. Looney 	db_printf("t_rttupdated: %lu   max_sndwnd: %u   t_softerror: %d\n",
2818497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2819497057eeSRobert Watson 
2820497057eeSRobert Watson 	db_print_indent(indent);
2821497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2822497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
2823497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
2824497057eeSRobert Watson 
2825497057eeSRobert Watson 	db_print_indent(indent);
2826497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
2827497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2828497057eeSRobert Watson 
2829497057eeSRobert Watson 	db_print_indent(indent);
28309f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
28311a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
2832497057eeSRobert Watson 
2833497057eeSRobert Watson 	db_print_indent(indent);
2834497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
28353ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2836497057eeSRobert Watson 
2837497057eeSRobert Watson 	db_print_indent(indent);
28383ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
28399f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2840497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
2841497057eeSRobert Watson 
2842497057eeSRobert Watson 	db_print_indent(indent);
28433529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
28443529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2845497057eeSRobert Watson 
2846497057eeSRobert Watson 	db_print_indent(indent);
2847497057eeSRobert Watson 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
2848497057eeSRobert Watson 	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2849497057eeSRobert Watson 
2850497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
2851497057eeSRobert Watson 
2852497057eeSRobert Watson 	db_print_indent(indent);
2853497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
2854497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2855497057eeSRobert Watson }
2856497057eeSRobert Watson 
2857497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2858497057eeSRobert Watson {
2859497057eeSRobert Watson 	struct tcpcb *tp;
2860497057eeSRobert Watson 
2861497057eeSRobert Watson 	if (!have_addr) {
2862497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
2863497057eeSRobert Watson 		return;
2864497057eeSRobert Watson 	}
2865497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
2866497057eeSRobert Watson 
2867497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
2868497057eeSRobert Watson }
2869497057eeSRobert Watson #endif
2870