xref: /freebsd/sys/netinet/tcp_usrreq.c (revision c8b53ced95f3bf1dcd2763e01ff309785fa761e7)
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"
470cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
480cc12cc5SJoerg Wunsch 
49df8bae1dSRodney W. Grimes #include <sys/param.h>
50df8bae1dSRodney W. Grimes #include <sys/systm.h>
519077f387SGleb Smirnoff #include <sys/limits.h>
52f76fcf6dSJeffrey Hsu #include <sys/malloc.h>
5355bceb1eSRandall Stewart #include <sys/refcount.h>
54c7a82f90SGarrett Wollman #include <sys/kernel.h>
5598163b98SPoul-Henning Kamp #include <sys/sysctl.h>
56df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
57fb59c426SYoshinobu Inoue #ifdef INET6
58fb59c426SYoshinobu Inoue #include <sys/domain.h>
59fb59c426SYoshinobu Inoue #endif /* INET6 */
60df8bae1dSRodney W. Grimes #include <sys/socket.h>
61df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
62df8bae1dSRodney W. Grimes #include <sys/protosw.h>
6391421ba2SRobert Watson #include <sys/proc.h>
6491421ba2SRobert Watson #include <sys/jail.h>
65f5cf1e5fSJulien Charbon #include <sys/syslog.h>
66df8bae1dSRodney W. Grimes 
67497057eeSRobert Watson #ifdef DDB
68497057eeSRobert Watson #include <ddb/ddb.h>
69497057eeSRobert Watson #endif
70497057eeSRobert Watson 
71df8bae1dSRodney W. Grimes #include <net/if.h>
7276039bc8SGleb Smirnoff #include <net/if_var.h>
73df8bae1dSRodney W. Grimes #include <net/route.h>
74530c0060SRobert Watson #include <net/vnet.h>
75df8bae1dSRodney W. Grimes 
76df8bae1dSRodney W. Grimes #include <netinet/in.h>
775d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h>
78df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
79b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h>
80b5e8ce9fSBruce Evans #include <netinet/in_var.h>
81df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
82fb59c426SYoshinobu Inoue #ifdef INET6
83b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h>
84b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
85fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
86a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
87fb59c426SYoshinobu Inoue #endif
882de3e790SGleb Smirnoff #include <netinet/tcp.h>
89df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
90df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
91df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
92df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
932529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h>
94df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
954644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
96c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h>
97fd389e7cSRandall Stewart #include <netinet/tcp_hpts.h>
9886a996e6SHiren Panchasara #ifdef TCPPCAP
9986a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
10086a996e6SHiren Panchasara #endif
101610ee2f9SDavid Greenman #ifdef TCPDEBUG
102df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
103610ee2f9SDavid Greenman #endif
10409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
105bc65987aSKip Macy #include <netinet/tcp_offload.h>
10609fe6320SNavdeep Parhar #endif
107fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
108df8bae1dSRodney W. Grimes 
109df8bae1dSRodney W. Grimes /*
110df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
111df8bae1dSRodney W. Grimes  */
11256dc72c3SPawel Jakub Dawidek static int	tcp_attach(struct socket *);
113b287c6c7SBjoern A. Zeeb #ifdef INET
1144d77a549SAlfred Perlstein static int	tcp_connect(struct tcpcb *, struct sockaddr *,
1154d77a549SAlfred Perlstein 		    struct thread *td);
116b287c6c7SBjoern A. Zeeb #endif /* INET */
117fb59c426SYoshinobu Inoue #ifdef INET6
1184d77a549SAlfred Perlstein static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
1194d77a549SAlfred Perlstein 		    struct thread *td);
120fb59c426SYoshinobu Inoue #endif /* INET6 */
121623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
122623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
123b8af5dfaSRobert Watson static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
1242c37256eSGarrett Wollman 
1252c37256eSGarrett Wollman #ifdef TCPDEBUG
1261db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1272c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
1284cc20ab1SSeigo Tanimura #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
1294cc20ab1SSeigo Tanimura 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1302c37256eSGarrett Wollman #else
1312c37256eSGarrett Wollman #define	TCPDEBUG0
1322c37256eSGarrett Wollman #define	TCPDEBUG1()
1332c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1342c37256eSGarrett Wollman #endif
1352c37256eSGarrett Wollman 
1362c37256eSGarrett Wollman /*
1372c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1382c37256eSGarrett Wollman  * and an internet control block.
1392c37256eSGarrett Wollman  */
1402c37256eSGarrett Wollman static int
141b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1422c37256eSGarrett Wollman {
143f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
144623dce13SRobert Watson 	struct tcpcb *tp = NULL;
145623dce13SRobert Watson 	int error;
1462c37256eSGarrett Wollman 	TCPDEBUG0;
1472c37256eSGarrett Wollman 
148623dce13SRobert Watson 	inp = sotoinpcb(so);
149623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1502c37256eSGarrett Wollman 	TCPDEBUG1();
1512c37256eSGarrett Wollman 
15256dc72c3SPawel Jakub Dawidek 	error = tcp_attach(so);
1532c37256eSGarrett Wollman 	if (error)
1542c37256eSGarrett Wollman 		goto out;
1552c37256eSGarrett Wollman 
1562c37256eSGarrett Wollman 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1573879597fSAndrey A. Chernov 		so->so_linger = TCP_LINGERTIME;
158f76fcf6dSJeffrey Hsu 
159f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
160f76fcf6dSJeffrey Hsu 	tp = intotcpcb(inp);
1612c37256eSGarrett Wollman out:
1622c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
1635d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
1642c37256eSGarrett Wollman 	return error;
1652c37256eSGarrett Wollman }
1662c37256eSGarrett Wollman 
1672c37256eSGarrett Wollman /*
168a152f8a3SRobert Watson  * tcp_detach is called when the socket layer loses its final reference
169a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
170a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
171a152f8a3SRobert Watson  * inpcb state: time wait.
172c78cbc7bSRobert Watson  *
173a152f8a3SRobert Watson  * This function can probably be re-absorbed back into tcp_usr_detach() now
174a152f8a3SRobert Watson  * that there is a single detach path.
1752c37256eSGarrett Wollman  */
176bc725eafSRobert Watson static void
177c78cbc7bSRobert Watson tcp_detach(struct socket *so, struct inpcb *inp)
1782c37256eSGarrett Wollman {
1792c37256eSGarrett Wollman 	struct tcpcb *tp;
1802c37256eSGarrett Wollman 
181079672cbSJulien Charbon 	INP_INFO_LOCK_ASSERT(&V_tcbinfo);
1828501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
183623dce13SRobert Watson 
184c78cbc7bSRobert Watson 	KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
185c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
186953b5606SRobert Watson 
187a152f8a3SRobert Watson 	tp = intotcpcb(inp);
188a152f8a3SRobert Watson 
189ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
190623dce13SRobert Watson 		/*
191a152f8a3SRobert Watson 		 * There are two cases to handle: one in which the time wait
192a152f8a3SRobert Watson 		 * state is being discarded (INP_DROPPED), and one in which
193a152f8a3SRobert Watson 		 * this connection will remain in timewait.  In the former,
194a152f8a3SRobert Watson 		 * it is time to discard all state (except tcptw, which has
195a152f8a3SRobert Watson 		 * already been discarded by the timewait close code, which
196a152f8a3SRobert Watson 		 * should be further up the call stack somewhere).  In the
197a152f8a3SRobert Watson 		 * latter case, we detach from the socket, but leave the pcb
198a152f8a3SRobert Watson 		 * present until timewait ends.
199623dce13SRobert Watson 		 *
200a152f8a3SRobert Watson 		 * XXXRW: Would it be cleaner to free the tcptw here?
201cea40c48SJulien Charbon 		 *
202cea40c48SJulien Charbon 		 * Astute question indeed, from twtcp perspective there are
203dd388cfdSGleb Smirnoff 		 * four cases to consider:
204cea40c48SJulien Charbon 		 *
205cea40c48SJulien Charbon 		 * #1 tcp_detach is called at tcptw creation time by
206cea40c48SJulien Charbon 		 *  tcp_twstart, then do not discard the newly created tcptw
207cea40c48SJulien Charbon 		 *  and leave inpcb present until timewait ends
208dd388cfdSGleb Smirnoff 		 * #2 tcp_detach is called at tcptw creation time by
209dd388cfdSGleb Smirnoff 		 *  tcp_twstart, but connection is local and tw will be
210dd388cfdSGleb Smirnoff 		 *  discarded immediately
211dd388cfdSGleb Smirnoff 		 * #3 tcp_detach is called at timewait end (or reuse) by
212cea40c48SJulien Charbon 		 *  tcp_twclose, then the tcptw has already been discarded
213ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
214dd388cfdSGleb Smirnoff 		 * #4 tcp_detach is called() after timewait ends (or reuse)
215cea40c48SJulien Charbon 		 *  (e.g. by soclose), then tcptw has already been discarded
216ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
217cea40c48SJulien Charbon 		 *
218cea40c48SJulien Charbon 		 *  In all three cases the tcptw should not be freed here.
219623dce13SRobert Watson 		 */
220ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED) {
221623dce13SRobert Watson 			in_pcbdetach(inp);
222f5cf1e5fSJulien Charbon 			if (__predict_true(tp == NULL)) {
2230206cdb8SBjoern A. Zeeb 				in_pcbfree(inp);
2240206cdb8SBjoern A. Zeeb 			} else {
225f5cf1e5fSJulien Charbon 				/*
226f5cf1e5fSJulien Charbon 				 * This case should not happen as in TIMEWAIT
227f5cf1e5fSJulien Charbon 				 * state the inp should not be destroyed before
228f5cf1e5fSJulien Charbon 				 * its tcptw.  If INVARIANTS is defined, panic.
229f5cf1e5fSJulien Charbon 				 */
230f5cf1e5fSJulien Charbon #ifdef INVARIANTS
231f5cf1e5fSJulien Charbon 				panic("%s: Panic before an inp double-free: "
232f5cf1e5fSJulien Charbon 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
233f5cf1e5fSJulien Charbon 				    , __func__);
234f5cf1e5fSJulien Charbon #else
235f5cf1e5fSJulien Charbon 				log(LOG_ERR, "%s: Avoid an inp double-free: "
236f5cf1e5fSJulien Charbon 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
237f5cf1e5fSJulien Charbon 				    , __func__);
238f5cf1e5fSJulien Charbon #endif
239f5cf1e5fSJulien Charbon 				INP_WUNLOCK(inp);
240f5cf1e5fSJulien Charbon 			}
241f5cf1e5fSJulien Charbon 		} else {
242623dce13SRobert Watson 			in_pcbdetach(inp);
2438501a69cSRobert Watson 			INP_WUNLOCK(inp);
244623dce13SRobert Watson 		}
245623dce13SRobert Watson 	} else {
246e6e65783SRobert Watson 		/*
247a152f8a3SRobert Watson 		 * If the connection is not in timewait, we consider two
248a152f8a3SRobert Watson 		 * two conditions: one in which no further processing is
249a152f8a3SRobert Watson 		 * necessary (dropped || embryonic), and one in which TCP is
250a152f8a3SRobert Watson 		 * not yet done, but no longer requires the socket, so the
251a152f8a3SRobert Watson 		 * pcb will persist for the time being.
252a152f8a3SRobert Watson 		 *
253a152f8a3SRobert Watson 		 * XXXRW: Does the second case still occur?
254e6e65783SRobert Watson 		 */
255ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED ||
256623dce13SRobert Watson 		    tp->t_state < TCPS_SYN_SENT) {
257623dce13SRobert Watson 			tcp_discardcb(tp);
258623dce13SRobert Watson 			in_pcbdetach(inp);
2590206cdb8SBjoern A. Zeeb 			in_pcbfree(inp);
260db3cee51SNavdeep Parhar 		} else {
261a152f8a3SRobert Watson 			in_pcbdetach(inp);
262db3cee51SNavdeep Parhar 			INP_WUNLOCK(inp);
263db3cee51SNavdeep Parhar 		}
264623dce13SRobert Watson 	}
265623dce13SRobert Watson }
266c78cbc7bSRobert Watson 
267c78cbc7bSRobert Watson /*
268c78cbc7bSRobert Watson  * pru_detach() detaches the TCP protocol from the socket.
269c78cbc7bSRobert Watson  * If the protocol state is non-embryonic, then can't
270c78cbc7bSRobert Watson  * do this directly: have to initiate a pru_disconnect(),
271c78cbc7bSRobert Watson  * which may finish later; embryonic TCB's can just
272c78cbc7bSRobert Watson  * be discarded here.
273c78cbc7bSRobert Watson  */
274c78cbc7bSRobert Watson static void
275c78cbc7bSRobert Watson tcp_usr_detach(struct socket *so)
276c78cbc7bSRobert Watson {
277c78cbc7bSRobert Watson 	struct inpcb *inp;
278079672cbSJulien Charbon 	int rlock = 0;
2796573d758SMatt Macy 	struct epoch_tracker et;
280c78cbc7bSRobert Watson 
281c78cbc7bSRobert Watson 	inp = sotoinpcb(so);
282c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
283079672cbSJulien Charbon 	if (!INP_INFO_WLOCKED(&V_tcbinfo)) {
2846573d758SMatt Macy 		INP_INFO_RLOCK_ET(&V_tcbinfo, et);
285079672cbSJulien Charbon 		rlock = 1;
286079672cbSJulien Charbon 	}
2878501a69cSRobert Watson 	INP_WLOCK(inp);
288c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
289c78cbc7bSRobert Watson 	    ("tcp_usr_detach: inp_socket == NULL"));
290c78cbc7bSRobert Watson 	tcp_detach(so, inp);
291079672cbSJulien Charbon 	if (rlock)
2926573d758SMatt Macy 		INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
2932c37256eSGarrett Wollman }
2942c37256eSGarrett Wollman 
295b287c6c7SBjoern A. Zeeb #ifdef INET
2962c37256eSGarrett Wollman /*
2972c37256eSGarrett Wollman  * Give the socket an address.
2982c37256eSGarrett Wollman  */
2992c37256eSGarrett Wollman static int
300b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
3012c37256eSGarrett Wollman {
3022c37256eSGarrett Wollman 	int error = 0;
303f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
304623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3052c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
3062c37256eSGarrett Wollman 
30752710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
30852710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sinp))
30952710de1SPawel Jakub Dawidek 		return (EINVAL);
3102c37256eSGarrett Wollman 	/*
3112c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
3122c37256eSGarrett Wollman 	 * to them.
3132c37256eSGarrett Wollman 	 */
3142c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
31552710de1SPawel Jakub Dawidek 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
31652710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
31752710de1SPawel Jakub Dawidek 
318623dce13SRobert Watson 	TCPDEBUG0;
319623dce13SRobert Watson 	inp = sotoinpcb(so);
320623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
3218501a69cSRobert Watson 	INP_WLOCK(inp);
322ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
323623dce13SRobert Watson 		error = EINVAL;
3242c37256eSGarrett Wollman 		goto out;
325623dce13SRobert Watson 	}
326623dce13SRobert Watson 	tp = intotcpcb(inp);
327623dce13SRobert Watson 	TCPDEBUG1();
328fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
329623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
330fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
331623dce13SRobert Watson out:
332623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3335d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3348501a69cSRobert Watson 	INP_WUNLOCK(inp);
335623dce13SRobert Watson 
336623dce13SRobert Watson 	return (error);
3372c37256eSGarrett Wollman }
338b287c6c7SBjoern A. Zeeb #endif /* INET */
3392c37256eSGarrett Wollman 
340fb59c426SYoshinobu Inoue #ifdef INET6
341fb59c426SYoshinobu Inoue static int
342b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
343fb59c426SYoshinobu Inoue {
344fb59c426SYoshinobu Inoue 	int error = 0;
345f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
346623dce13SRobert Watson 	struct tcpcb *tp = NULL;
347fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
348fb59c426SYoshinobu Inoue 
34952710de1SPawel Jakub Dawidek 	sin6p = (struct sockaddr_in6 *)nam;
35052710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sin6p))
35152710de1SPawel Jakub Dawidek 		return (EINVAL);
352fb59c426SYoshinobu Inoue 	/*
353fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
354fb59c426SYoshinobu Inoue 	 * to them.
355fb59c426SYoshinobu Inoue 	 */
356fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6 &&
35752710de1SPawel Jakub Dawidek 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
35852710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
35952710de1SPawel Jakub Dawidek 
360623dce13SRobert Watson 	TCPDEBUG0;
361623dce13SRobert Watson 	inp = sotoinpcb(so);
362623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
3638501a69cSRobert Watson 	INP_WLOCK(inp);
364ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
365623dce13SRobert Watson 		error = EINVAL;
366623dce13SRobert Watson 		goto out;
367623dce13SRobert Watson 	}
368623dce13SRobert Watson 	tp = intotcpcb(inp);
369623dce13SRobert Watson 	TCPDEBUG1();
370fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
371fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
372fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
373b287c6c7SBjoern A. Zeeb #ifdef INET
37466ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
375fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
376fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
377fb59c426SYoshinobu Inoue 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
378fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
379fb59c426SYoshinobu Inoue 
380fb59c426SYoshinobu Inoue 			in6_sin6_2_sin(&sin, sin6p);
381888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
382888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
383888973f5SMichael Tuexen 				INP_HASH_WUNLOCK(&V_tcbinfo);
384888973f5SMichael Tuexen 				goto out;
385888973f5SMichael Tuexen 			}
386fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
387fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
388b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
389b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
390fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
391fb59c426SYoshinobu Inoue 			goto out;
392fb59c426SYoshinobu Inoue 		}
393fb59c426SYoshinobu Inoue 	}
394b287c6c7SBjoern A. Zeeb #endif
395b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
396fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
397623dce13SRobert Watson out:
398623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3995d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
4008501a69cSRobert Watson 	INP_WUNLOCK(inp);
401623dce13SRobert Watson 	return (error);
402fb59c426SYoshinobu Inoue }
403fb59c426SYoshinobu Inoue #endif /* INET6 */
404fb59c426SYoshinobu Inoue 
405b287c6c7SBjoern A. Zeeb #ifdef INET
4062c37256eSGarrett Wollman /*
4072c37256eSGarrett Wollman  * Prepare to accept connections.
4082c37256eSGarrett Wollman  */
4092c37256eSGarrett Wollman static int
410d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
4112c37256eSGarrett Wollman {
4122c37256eSGarrett Wollman 	int error = 0;
413f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
414623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4152c37256eSGarrett Wollman 
416623dce13SRobert Watson 	TCPDEBUG0;
417623dce13SRobert Watson 	inp = sotoinpcb(so);
418623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
4198501a69cSRobert Watson 	INP_WLOCK(inp);
420ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
421623dce13SRobert Watson 		error = EINVAL;
422623dce13SRobert Watson 		goto out;
423623dce13SRobert Watson 	}
424623dce13SRobert Watson 	tp = intotcpcb(inp);
425623dce13SRobert Watson 	TCPDEBUG1();
4260daccb9cSRobert Watson 	SOCK_LOCK(so);
4270daccb9cSRobert Watson 	error = solisten_proto_check(so);
428fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
4290daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0)
430b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
431fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4320daccb9cSRobert Watson 	if (error == 0) {
43357f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
434d374e81eSRobert Watson 		solisten_proto(so, backlog);
43509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
43637cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
43709fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
43809fe6320SNavdeep Parhar #endif
4390daccb9cSRobert Watson 	}
4400daccb9cSRobert Watson 	SOCK_UNLOCK(so);
441623dce13SRobert Watson 
44268bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
443281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
44418a75309SPatrick Kelsey 
445623dce13SRobert Watson out:
446623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4475d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4488501a69cSRobert Watson 	INP_WUNLOCK(inp);
449623dce13SRobert Watson 	return (error);
4502c37256eSGarrett Wollman }
451b287c6c7SBjoern A. Zeeb #endif /* INET */
4522c37256eSGarrett Wollman 
453fb59c426SYoshinobu Inoue #ifdef INET6
454fb59c426SYoshinobu Inoue static int
455d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
456fb59c426SYoshinobu Inoue {
457fb59c426SYoshinobu Inoue 	int error = 0;
458f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
459623dce13SRobert Watson 	struct tcpcb *tp = NULL;
460fb59c426SYoshinobu Inoue 
461623dce13SRobert Watson 	TCPDEBUG0;
462623dce13SRobert Watson 	inp = sotoinpcb(so);
463623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4648501a69cSRobert Watson 	INP_WLOCK(inp);
465ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
466623dce13SRobert Watson 		error = EINVAL;
467623dce13SRobert Watson 		goto out;
468623dce13SRobert Watson 	}
469623dce13SRobert Watson 	tp = intotcpcb(inp);
470623dce13SRobert Watson 	TCPDEBUG1();
4710daccb9cSRobert Watson 	SOCK_LOCK(so);
4720daccb9cSRobert Watson 	error = solisten_proto_check(so);
473fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
4740daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0) {
475fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
47666ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
477fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
478b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
479fb59c426SYoshinobu Inoue 	}
480fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4810daccb9cSRobert Watson 	if (error == 0) {
48257f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
483d374e81eSRobert Watson 		solisten_proto(so, backlog);
48409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
48537cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
48609fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
48709fe6320SNavdeep Parhar #endif
4880daccb9cSRobert Watson 	}
4890daccb9cSRobert Watson 	SOCK_UNLOCK(so);
490623dce13SRobert Watson 
49168bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
492281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
49318a75309SPatrick Kelsey 
494623dce13SRobert Watson out:
495623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4965d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4978501a69cSRobert Watson 	INP_WUNLOCK(inp);
498623dce13SRobert Watson 	return (error);
499fb59c426SYoshinobu Inoue }
500fb59c426SYoshinobu Inoue #endif /* INET6 */
501fb59c426SYoshinobu Inoue 
502b287c6c7SBjoern A. Zeeb #ifdef INET
5032c37256eSGarrett Wollman /*
5042c37256eSGarrett Wollman  * Initiate connection to peer.
5052c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
5062c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
5072c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
5082c37256eSGarrett Wollman  * Send initial segment on connection.
5092c37256eSGarrett Wollman  */
5102c37256eSGarrett Wollman static int
511b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
5122c37256eSGarrett Wollman {
5132c37256eSGarrett Wollman 	int error = 0;
514f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
515623dce13SRobert Watson 	struct tcpcb *tp = NULL;
5162c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
5172c37256eSGarrett Wollman 
51857bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
519e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
520e29ef13fSDon Lewis 		return (EINVAL);
52152710de1SPawel Jakub Dawidek 	/*
52252710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
52352710de1SPawel Jakub Dawidek 	 */
5242c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
52552710de1SPawel Jakub Dawidek 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
52652710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
527b89e82ddSJamie Gritton 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
528b89e82ddSJamie Gritton 		return (error);
52975c13541SPoul-Henning Kamp 
530623dce13SRobert Watson 	TCPDEBUG0;
531623dce13SRobert Watson 	inp = sotoinpcb(so);
532623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
5338501a69cSRobert Watson 	INP_WLOCK(inp);
534eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
535eb96dc33SJulien Charbon 		error = EADDRINUSE;
536eb96dc33SJulien Charbon 		goto out;
537eb96dc33SJulien Charbon 	}
538eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
539eb96dc33SJulien Charbon 		error = ECONNREFUSED;
540623dce13SRobert Watson 		goto out;
541623dce13SRobert Watson 	}
542623dce13SRobert Watson 	tp = intotcpcb(inp);
543623dce13SRobert Watson 	TCPDEBUG1();
544b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
5452c37256eSGarrett Wollman 		goto out;
54609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
54709fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
54837cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
54909fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
55009fe6320SNavdeep Parhar 		goto out;
55109fe6320SNavdeep Parhar #endif
55209fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
55355bceb1eSRandall Stewart 	error = tp->t_fb->tfb_tcp_output(tp);
554623dce13SRobert Watson out:
555623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
556e79cb051SGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
5578501a69cSRobert Watson 	INP_WUNLOCK(inp);
558623dce13SRobert Watson 	return (error);
5592c37256eSGarrett Wollman }
560b287c6c7SBjoern A. Zeeb #endif /* INET */
5612c37256eSGarrett Wollman 
562fb59c426SYoshinobu Inoue #ifdef INET6
563fb59c426SYoshinobu Inoue static int
564b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
565fb59c426SYoshinobu Inoue {
566fb59c426SYoshinobu Inoue 	int error = 0;
567f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
568623dce13SRobert Watson 	struct tcpcb *tp = NULL;
569fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
570623dce13SRobert Watson 
571623dce13SRobert Watson 	TCPDEBUG0;
572fb59c426SYoshinobu Inoue 
573fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
574e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sin6p))
575e29ef13fSDon Lewis 		return (EINVAL);
57652710de1SPawel Jakub Dawidek 	/*
57752710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
57852710de1SPawel Jakub Dawidek 	 */
579fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6
58052710de1SPawel Jakub Dawidek 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
58152710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
582fb59c426SYoshinobu Inoue 
583623dce13SRobert Watson 	inp = sotoinpcb(so);
584623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
5858501a69cSRobert Watson 	INP_WLOCK(inp);
586eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
587eb96dc33SJulien Charbon 		error = EADDRINUSE;
588eb96dc33SJulien Charbon 		goto out;
589eb96dc33SJulien Charbon 	}
590eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
591eb96dc33SJulien Charbon 		error = ECONNREFUSED;
592623dce13SRobert Watson 		goto out;
593623dce13SRobert Watson 	}
594623dce13SRobert Watson 	tp = intotcpcb(inp);
595623dce13SRobert Watson 	TCPDEBUG1();
596b287c6c7SBjoern A. Zeeb #ifdef INET
597fa046d87SRobert Watson 	/*
598fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
599fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
600fa046d87SRobert Watson 	 * Is this a significant problem?
601fa046d87SRobert Watson 	 */
60233841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
603fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
604fb59c426SYoshinobu Inoue 
605d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
606d46a5312SMaxim Konovalov 			error = EINVAL;
607d46a5312SMaxim Konovalov 			goto out;
608d46a5312SMaxim Konovalov 		}
6095dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV4) == 0) {
6105dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6115dba6adaSMichael Tuexen 			goto out;
6125dba6adaSMichael Tuexen 		}
61333841545SHajimu UMEMOTO 
614fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
615888973f5SMichael Tuexen 		if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
616888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
617888973f5SMichael Tuexen 			goto out;
618888973f5SMichael Tuexen 		}
619fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
620fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
621b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
622b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
623413628a7SBjoern A. Zeeb 			goto out;
624b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
625fb59c426SYoshinobu Inoue 			goto out;
62609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
62709fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
628adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
62909fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
63009fe6320SNavdeep Parhar 			goto out;
63109fe6320SNavdeep Parhar #endif
63255bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
633fb59c426SYoshinobu Inoue 		goto out;
6345dba6adaSMichael Tuexen 	} else {
6355dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV6) == 0) {
6365dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6375dba6adaSMichael Tuexen 			goto out;
6385dba6adaSMichael Tuexen 		}
639fb59c426SYoshinobu Inoue 	}
640b287c6c7SBjoern A. Zeeb #endif
641fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
642fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
643dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
644b89e82ddSJamie Gritton 	if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
645413628a7SBjoern A. Zeeb 		goto out;
646b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
647fb59c426SYoshinobu Inoue 		goto out;
64809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
64909fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
650adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
65109fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
65209fe6320SNavdeep Parhar 		goto out;
65309fe6320SNavdeep Parhar #endif
65409fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
65555bceb1eSRandall Stewart 	error = tp->t_fb->tfb_tcp_output(tp);
656623dce13SRobert Watson 
657623dce13SRobert Watson out:
658623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
6595d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
6608501a69cSRobert Watson 	INP_WUNLOCK(inp);
661623dce13SRobert Watson 	return (error);
662fb59c426SYoshinobu Inoue }
663fb59c426SYoshinobu Inoue #endif /* INET6 */
664fb59c426SYoshinobu Inoue 
6652c37256eSGarrett Wollman /*
6662c37256eSGarrett Wollman  * Initiate disconnect from peer.
6672c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
6682c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
6692c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
6702c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
6712c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
6722c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
6732c37256eSGarrett Wollman  *
6742c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
6752c37256eSGarrett Wollman  */
6762c37256eSGarrett Wollman static int
6772c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
6782c37256eSGarrett Wollman {
679f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
680623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6816573d758SMatt Macy 	struct epoch_tracker et;
682623dce13SRobert Watson 	int error = 0;
6832c37256eSGarrett Wollman 
684623dce13SRobert Watson 	TCPDEBUG0;
6856573d758SMatt Macy 	INP_INFO_RLOCK_ET(&V_tcbinfo, et);
686623dce13SRobert Watson 	inp = sotoinpcb(so);
687623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
6888501a69cSRobert Watson 	INP_WLOCK(inp);
689489dcc92SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT)
690489dcc92SJulien Charbon 		goto out;
691489dcc92SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
69221367f63SSam Leffler 		error = ECONNRESET;
693623dce13SRobert Watson 		goto out;
694623dce13SRobert Watson 	}
695623dce13SRobert Watson 	tp = intotcpcb(inp);
696623dce13SRobert Watson 	TCPDEBUG1();
697623dce13SRobert Watson 	tcp_disconnect(tp);
698623dce13SRobert Watson out:
699623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
7005d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
7018501a69cSRobert Watson 	INP_WUNLOCK(inp);
7026573d758SMatt Macy 	INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
703623dce13SRobert Watson 	return (error);
7042c37256eSGarrett Wollman }
7052c37256eSGarrett Wollman 
706b287c6c7SBjoern A. Zeeb #ifdef INET
7072c37256eSGarrett Wollman /*
7088296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
7098296cddfSRobert Watson  * just return the address of the peer, storing through addr.
7102c37256eSGarrett Wollman  */
7112c37256eSGarrett Wollman static int
71257bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
7132c37256eSGarrett Wollman {
7142c37256eSGarrett Wollman 	int error = 0;
715f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
7161db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
71726ef6ac4SDon Lewis 	struct in_addr addr;
71826ef6ac4SDon Lewis 	in_port_t port = 0;
7191db24ffbSJonathan Lemon 	TCPDEBUG0;
7202c37256eSGarrett Wollman 
7213d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
7223d2d3ef4SRobert Watson 		return (ECONNABORTED);
723f76fcf6dSJeffrey Hsu 
724f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
725623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
7268501a69cSRobert Watson 	INP_WLOCK(inp);
727ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
7283d2d3ef4SRobert Watson 		error = ECONNABORTED;
729623dce13SRobert Watson 		goto out;
730623dce13SRobert Watson 	}
7311db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
7321db24ffbSJonathan Lemon 	TCPDEBUG1();
733f76fcf6dSJeffrey Hsu 
734f76fcf6dSJeffrey Hsu 	/*
73554d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
73626ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
73726ef6ac4SDon Lewis 	 * release the lock.
738f76fcf6dSJeffrey Hsu 	 */
73926ef6ac4SDon Lewis 	port = inp->inp_fport;
74026ef6ac4SDon Lewis 	addr = inp->inp_faddr;
741f76fcf6dSJeffrey Hsu 
742623dce13SRobert Watson out:
743623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
7445d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7458501a69cSRobert Watson 	INP_WUNLOCK(inp);
74626ef6ac4SDon Lewis 	if (error == 0)
74726ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
74826ef6ac4SDon Lewis 	return error;
7492c37256eSGarrett Wollman }
750b287c6c7SBjoern A. Zeeb #endif /* INET */
7512c37256eSGarrett Wollman 
752fb59c426SYoshinobu Inoue #ifdef INET6
753fb59c426SYoshinobu Inoue static int
754fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
755fb59c426SYoshinobu Inoue {
756f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
757fb59c426SYoshinobu Inoue 	int error = 0;
7581db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
75926ef6ac4SDon Lewis 	struct in_addr addr;
76026ef6ac4SDon Lewis 	struct in6_addr addr6;
7616573d758SMatt Macy 	struct epoch_tracker et;
76226ef6ac4SDon Lewis 	in_port_t port = 0;
76326ef6ac4SDon Lewis 	int v4 = 0;
7641db24ffbSJonathan Lemon 	TCPDEBUG0;
765fb59c426SYoshinobu Inoue 
766b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
767b4470c16SRobert Watson 		return (ECONNABORTED);
768f76fcf6dSJeffrey Hsu 
769f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
770623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
7716573d758SMatt Macy 	INP_INFO_RLOCK_ET(&V_tcbinfo, et);
7728501a69cSRobert Watson 	INP_WLOCK(inp);
773ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
77421367f63SSam Leffler 		error = ECONNABORTED;
775623dce13SRobert Watson 		goto out;
776623dce13SRobert Watson 	}
7771db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
7781db24ffbSJonathan Lemon 	TCPDEBUG1();
779623dce13SRobert Watson 
78026ef6ac4SDon Lewis 	/*
78126ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
78226ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
78326ef6ac4SDon Lewis 	 * release the lock.
78426ef6ac4SDon Lewis 	 */
78526ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
78626ef6ac4SDon Lewis 		v4 = 1;
78726ef6ac4SDon Lewis 		port = inp->inp_fport;
78826ef6ac4SDon Lewis 		addr = inp->inp_faddr;
78926ef6ac4SDon Lewis 	} else {
79026ef6ac4SDon Lewis 		port = inp->inp_fport;
79126ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
79226ef6ac4SDon Lewis 	}
79326ef6ac4SDon Lewis 
794623dce13SRobert Watson out:
795623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
7965d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7978501a69cSRobert Watson 	INP_WUNLOCK(inp);
7986573d758SMatt Macy 	INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
79926ef6ac4SDon Lewis 	if (error == 0) {
80026ef6ac4SDon Lewis 		if (v4)
80126ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
80226ef6ac4SDon Lewis 		else
80326ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
80426ef6ac4SDon Lewis 	}
80526ef6ac4SDon Lewis 	return error;
806fb59c426SYoshinobu Inoue }
807fb59c426SYoshinobu Inoue #endif /* INET6 */
808f76fcf6dSJeffrey Hsu 
809f76fcf6dSJeffrey Hsu /*
8102c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
8112c37256eSGarrett Wollman  */
8122c37256eSGarrett Wollman static int
8132c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
8142c37256eSGarrett Wollman {
8152c37256eSGarrett Wollman 	int error = 0;
816f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
817623dce13SRobert Watson 	struct tcpcb *tp = NULL;
8186573d758SMatt Macy 	struct epoch_tracker et;
8192c37256eSGarrett Wollman 
820623dce13SRobert Watson 	TCPDEBUG0;
8216573d758SMatt Macy 	INP_INFO_RLOCK_ET(&V_tcbinfo, et);
822623dce13SRobert Watson 	inp = sotoinpcb(so);
823623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
8248501a69cSRobert Watson 	INP_WLOCK(inp);
825ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
82621367f63SSam Leffler 		error = ECONNRESET;
827623dce13SRobert Watson 		goto out;
828623dce13SRobert Watson 	}
829623dce13SRobert Watson 	tp = intotcpcb(inp);
830623dce13SRobert Watson 	TCPDEBUG1();
8312c37256eSGarrett Wollman 	socantsendmore(so);
832623dce13SRobert Watson 	tcp_usrclosed(tp);
833ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
83455bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
835623dce13SRobert Watson 
836623dce13SRobert Watson out:
837623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
8385d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
8398501a69cSRobert Watson 	INP_WUNLOCK(inp);
8406573d758SMatt Macy 	INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
841623dce13SRobert Watson 
842623dce13SRobert Watson 	return (error);
8432c37256eSGarrett Wollman }
8442c37256eSGarrett Wollman 
8452c37256eSGarrett Wollman /*
8462c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
8472c37256eSGarrett Wollman  */
8482c37256eSGarrett Wollman static int
8492c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
8502c37256eSGarrett Wollman {
851f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
852623dce13SRobert Watson 	struct tcpcb *tp = NULL;
853623dce13SRobert Watson 	int error = 0;
8542c37256eSGarrett Wollman 
855623dce13SRobert Watson 	TCPDEBUG0;
856623dce13SRobert Watson 	inp = sotoinpcb(so);
857623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: 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();
865281a0fd4SPatrick Kelsey 	/*
866281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
867281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
868281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
869281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
870281a0fd4SPatrick Kelsey 	 * expires.
871281a0fd4SPatrick Kelsey 	 */
87268bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags) &&
873281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
874281a0fd4SPatrick Kelsey 		goto out;
87509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
87609fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
87709fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
878460cf046SNavdeep Parhar 	else
87909fe6320SNavdeep Parhar #endif
88055bceb1eSRandall Stewart 	tp->t_fb->tfb_tcp_output(tp);
881623dce13SRobert Watson 
882623dce13SRobert Watson out:
883623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
8845d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
8858501a69cSRobert Watson 	INP_WUNLOCK(inp);
886623dce13SRobert Watson 	return (error);
8872c37256eSGarrett Wollman }
8882c37256eSGarrett Wollman 
8892c37256eSGarrett Wollman /*
8902c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
8919c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
8929c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
8939c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
8949c9906e9SPeter Wemm  * generally are caller-frees.
8952c37256eSGarrett Wollman  */
8962c37256eSGarrett Wollman static int
89757bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
898b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
8992c37256eSGarrett Wollman {
9002c37256eSGarrett Wollman 	int error = 0;
901f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
902623dce13SRobert Watson 	struct tcpcb *tp = NULL;
9036573d758SMatt Macy 	struct epoch_tracker net_et;
904888973f5SMichael Tuexen #ifdef INET
90551e08d53SMichael Tuexen #ifdef INET6
90651e08d53SMichael Tuexen 	struct sockaddr_in sin;
90751e08d53SMichael Tuexen #endif
90851e08d53SMichael Tuexen 	struct sockaddr_in *sinp;
909888973f5SMichael Tuexen #endif
910fb59c426SYoshinobu Inoue #ifdef INET6
911fb59c426SYoshinobu Inoue 	int isipv6;
912fb59c426SYoshinobu Inoue #endif
9139c9906e9SPeter Wemm 	TCPDEBUG0;
9142c37256eSGarrett Wollman 
915f76fcf6dSJeffrey Hsu 	/*
916fa046d87SRobert Watson 	 * We require the pcbinfo lock if we will close the socket as part of
917fa046d87SRobert Watson 	 * this call.
918f76fcf6dSJeffrey Hsu 	 */
919fa046d87SRobert Watson 	if (flags & PRUS_EOF)
9206573d758SMatt Macy 		INP_INFO_RLOCK_ET(&V_tcbinfo, net_et);
921f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
922623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
9238501a69cSRobert Watson 	INP_WLOCK(inp);
924ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
9257ff0b850SAndre Oppermann 		if (control)
9267ff0b850SAndre Oppermann 			m_freem(control);
9272cbcd3c1SGleb Smirnoff 		/*
9282cbcd3c1SGleb Smirnoff 		 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
9292cbcd3c1SGleb Smirnoff 		 * for freeing memory.
9302cbcd3c1SGleb Smirnoff 		 */
9312cbcd3c1SGleb Smirnoff 		if (m && (flags & PRUS_NOTREADY) == 0)
9327ff0b850SAndre Oppermann 			m_freem(m);
93321367f63SSam Leffler 		error = ECONNRESET;
9349c9906e9SPeter Wemm 		goto out;
9359c9906e9SPeter Wemm 	}
9369c9906e9SPeter Wemm 	tp = intotcpcb(inp);
9379c9906e9SPeter Wemm 	TCPDEBUG1();
938888973f5SMichael Tuexen 	if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
939888973f5SMichael Tuexen 		switch (nam->sa_family) {
940888973f5SMichael Tuexen #ifdef INET
941888973f5SMichael Tuexen 		case AF_INET:
942888973f5SMichael Tuexen 			sinp = (struct sockaddr_in *)nam;
943888973f5SMichael Tuexen 			if (sinp->sin_len != sizeof(struct sockaddr_in)) {
944888973f5SMichael Tuexen 				if (m)
945888973f5SMichael Tuexen 					m_freem(m);
946888973f5SMichael Tuexen 				error = EINVAL;
947888973f5SMichael Tuexen 				goto out;
948888973f5SMichael Tuexen 			}
949888973f5SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6) != 0) {
950888973f5SMichael Tuexen 				if (m)
951888973f5SMichael Tuexen 					m_freem(m);
952888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
953888973f5SMichael Tuexen 				goto out;
954888973f5SMichael Tuexen 			}
955888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
956888973f5SMichael Tuexen 				if (m)
957888973f5SMichael Tuexen 					m_freem(m);
958888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
959888973f5SMichael Tuexen 				goto out;
960888973f5SMichael Tuexen 			}
961888973f5SMichael Tuexen 			if ((error = prison_remote_ip4(td->td_ucred,
962888973f5SMichael Tuexen 			    &sinp->sin_addr))) {
963888973f5SMichael Tuexen 				if (m)
964888973f5SMichael Tuexen 					m_freem(m);
965888973f5SMichael Tuexen 				goto out;
966888973f5SMichael Tuexen 			}
967888973f5SMichael Tuexen #ifdef INET6
968888973f5SMichael Tuexen 			isipv6 = 0;
969888973f5SMichael Tuexen #endif
970888973f5SMichael Tuexen 			break;
971888973f5SMichael Tuexen #endif /* INET */
972888973f5SMichael Tuexen #ifdef INET6
973888973f5SMichael Tuexen 		case AF_INET6:
974888973f5SMichael Tuexen 		{
975888973f5SMichael Tuexen 			struct sockaddr_in6 *sin6p;
976888973f5SMichael Tuexen 
977888973f5SMichael Tuexen 			sin6p = (struct sockaddr_in6 *)nam;
978888973f5SMichael Tuexen 			if (sin6p->sin6_len != sizeof(struct sockaddr_in6)) {
979888973f5SMichael Tuexen 				if (m)
980888973f5SMichael Tuexen 					m_freem(m);
981888973f5SMichael Tuexen 				error = EINVAL;
982888973f5SMichael Tuexen 				goto out;
983888973f5SMichael Tuexen 			}
984888973f5SMichael Tuexen 			if (IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
985888973f5SMichael Tuexen 				if (m)
986888973f5SMichael Tuexen 					m_freem(m);
987888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
988888973f5SMichael Tuexen 				goto out;
989888973f5SMichael Tuexen 			}
990888973f5SMichael Tuexen 			if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
991888973f5SMichael Tuexen #ifdef INET
992888973f5SMichael Tuexen 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
993888973f5SMichael Tuexen 					error = EINVAL;
994888973f5SMichael Tuexen 					if (m)
995888973f5SMichael Tuexen 						m_freem(m);
996888973f5SMichael Tuexen 					goto out;
997888973f5SMichael Tuexen 				}
998888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV4) == 0) {
999888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1000888973f5SMichael Tuexen 					if (m)
1001888973f5SMichael Tuexen 						m_freem(m);
1002888973f5SMichael Tuexen 					goto out;
1003888973f5SMichael Tuexen 				}
1004888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV6;
1005888973f5SMichael Tuexen 				sinp = &sin;
1006888973f5SMichael Tuexen 				in6_sin6_2_sin(sinp, sin6p);
1007888973f5SMichael Tuexen 				if (IN_MULTICAST(
1008888973f5SMichael Tuexen 				    ntohl(sinp->sin_addr.s_addr))) {
1009888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1010888973f5SMichael Tuexen 					if (m)
1011888973f5SMichael Tuexen 						m_freem(m);
1012888973f5SMichael Tuexen 					goto out;
1013888973f5SMichael Tuexen 				}
1014888973f5SMichael Tuexen 				if ((error = prison_remote_ip4(td->td_ucred,
1015888973f5SMichael Tuexen 				    &sinp->sin_addr))) {
1016888973f5SMichael Tuexen 					if (m)
1017888973f5SMichael Tuexen 						m_freem(m);
1018888973f5SMichael Tuexen 					goto out;
1019888973f5SMichael Tuexen 				}
1020888973f5SMichael Tuexen 				isipv6 = 0;
1021888973f5SMichael Tuexen #else /* !INET */
1022888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1023888973f5SMichael Tuexen 				if (m)
1024888973f5SMichael Tuexen 					m_freem(m);
1025888973f5SMichael Tuexen 				goto out;
1026888973f5SMichael Tuexen #endif /* INET */
1027888973f5SMichael Tuexen 			} else {
1028888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV6) == 0) {
1029888973f5SMichael Tuexen 					if (m)
1030888973f5SMichael Tuexen 						m_freem(m);
1031888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1032888973f5SMichael Tuexen 					goto out;
1033888973f5SMichael Tuexen 				}
1034888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV4;
1035888973f5SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_ISIPV6;
1036888973f5SMichael Tuexen 				if ((error = prison_remote_ip6(td->td_ucred,
1037888973f5SMichael Tuexen 				    &sin6p->sin6_addr))) {
1038888973f5SMichael Tuexen 					if (m)
1039888973f5SMichael Tuexen 						m_freem(m);
1040888973f5SMichael Tuexen 					goto out;
1041888973f5SMichael Tuexen 				}
1042888973f5SMichael Tuexen 				isipv6 = 1;
1043888973f5SMichael Tuexen 			}
1044888973f5SMichael Tuexen 			break;
1045888973f5SMichael Tuexen 		}
1046888973f5SMichael Tuexen #endif /* INET6 */
1047888973f5SMichael Tuexen 		default:
1048888973f5SMichael Tuexen 			if (m)
1049888973f5SMichael Tuexen 				m_freem(m);
1050888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
1051888973f5SMichael Tuexen 			goto out;
1052888973f5SMichael Tuexen 		}
1053888973f5SMichael Tuexen 	}
10549c9906e9SPeter Wemm 	if (control) {
10559c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
10569c9906e9SPeter Wemm 		if (control->m_len) {
10579c9906e9SPeter Wemm 			m_freem(control);
10582c37256eSGarrett Wollman 			if (m)
10592c37256eSGarrett Wollman 				m_freem(m);
1060744f87eaSDavid Greenman 			error = EINVAL;
1061744f87eaSDavid Greenman 			goto out;
10622c37256eSGarrett Wollman 		}
10639c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
10649c9906e9SPeter Wemm 	}
10652c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
1066651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
10672c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
10682c37256eSGarrett Wollman 			/*
10692c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
10702c37256eSGarrett Wollman 			 * initialize window to default value, and
10710c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
10722c37256eSGarrett Wollman 			 */
1073fb59c426SYoshinobu Inoue #ifdef INET6
1074fb59c426SYoshinobu Inoue 			if (isipv6)
1075b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1076fb59c426SYoshinobu Inoue #endif /* INET6 */
1077b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1078b287c6c7SBjoern A. Zeeb 			else
1079b287c6c7SBjoern A. Zeeb #endif
1080b287c6c7SBjoern A. Zeeb #ifdef INET
1081888973f5SMichael Tuexen 				error = tcp_connect(tp,
1082888973f5SMichael Tuexen 				    (struct sockaddr *)sinp, td);
1083b287c6c7SBjoern A. Zeeb #endif
10842c37256eSGarrett Wollman 			if (error)
10852c37256eSGarrett Wollman 				goto out;
1086c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1087c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
108818a75309SPatrick Kelsey 			else {
10892c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
10902c37256eSGarrett Wollman 				tcp_mss(tp, -1);
10912c37256eSGarrett Wollman 			}
1092c560df6fSPatrick Kelsey 		}
10932c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
10942c37256eSGarrett Wollman 			/*
10952c37256eSGarrett Wollman 			 * Close the send side of the connection after
10962c37256eSGarrett Wollman 			 * the data is sent.
10972c37256eSGarrett Wollman 			 */
1098ff9b006dSJulien Charbon 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
10992c37256eSGarrett Wollman 			socantsendmore(so);
1100623dce13SRobert Watson 			tcp_usrclosed(tp);
11012c37256eSGarrett Wollman 		}
11022cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
11032cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
1104b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1105b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
110655bceb1eSRandall Stewart 			error = tp->t_fb->tfb_tcp_output(tp);
1107b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1108b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
1109b0acefa8SBill Fenner 		}
11102c37256eSGarrett Wollman 	} else {
1111623dce13SRobert Watson 		/*
1112623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1113623dce13SRobert Watson 		 */
1114d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
11152c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
1116d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
11172c37256eSGarrett Wollman 			m_freem(m);
11182c37256eSGarrett Wollman 			error = ENOBUFS;
11192c37256eSGarrett Wollman 			goto out;
11202c37256eSGarrett Wollman 		}
11212c37256eSGarrett Wollman 		/*
11222c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
11232c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
11242c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
11252c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
11262c37256eSGarrett Wollman 		 * of data past the urgent section.
11272c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
11282c37256eSGarrett Wollman 		 */
1129651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
1130d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
1131ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1132ef53690bSGarrett Wollman 			/*
1133ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
1134ef53690bSGarrett Wollman 			 * initialize window to default value, and
11350c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1136ef53690bSGarrett Wollman 			 */
113718a75309SPatrick Kelsey 
1138c560df6fSPatrick Kelsey 			/*
1139c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1140c560df6fSPatrick Kelsey 			 */
1141c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1142c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1143fb59c426SYoshinobu Inoue #ifdef INET6
1144fb59c426SYoshinobu Inoue 			if (isipv6)
1145b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1146fb59c426SYoshinobu Inoue #endif /* INET6 */
1147b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1148b287c6c7SBjoern A. Zeeb 			else
1149b287c6c7SBjoern A. Zeeb #endif
1150b287c6c7SBjoern A. Zeeb #ifdef INET
1151888973f5SMichael Tuexen 				error = tcp_connect(tp,
1152888973f5SMichael Tuexen 				    (struct sockaddr *)sinp, td);
1153b287c6c7SBjoern A. Zeeb #endif
1154ef53690bSGarrett Wollman 			if (error)
1155ef53690bSGarrett Wollman 				goto out;
1156ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1157ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
1158623dce13SRobert Watson 		}
1159300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
11602cbcd3c1SGleb Smirnoff 		if (!(flags & PRUS_NOTREADY)) {
11612cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
116255bceb1eSRandall Stewart 			error = tp->t_fb->tfb_tcp_output(tp);
11632cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
11642c37256eSGarrett Wollman 		}
11652cbcd3c1SGleb Smirnoff 	}
11662529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL,
11672529f56eSJonathan T. Looney 	    &inp->inp_socket->so_rcv,
11682529f56eSJonathan T. Looney 	    &inp->inp_socket->so_snd,
11692529f56eSJonathan T. Looney 	    TCP_LOG_USERSEND, error,
11702529f56eSJonathan T. Looney 	    0, NULL, false);
1171d1401c90SRobert Watson out:
1172d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
11732c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
11745d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
11755d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
11768501a69cSRobert Watson 	INP_WUNLOCK(inp);
1177fa046d87SRobert Watson 	if (flags & PRUS_EOF)
11786573d758SMatt Macy 		INP_INFO_RUNLOCK_ET(&V_tcbinfo, net_et);
117973fddedaSPeter Grehan 	return (error);
11802c37256eSGarrett Wollman }
11812c37256eSGarrett Wollman 
11822cbcd3c1SGleb Smirnoff static int
11832cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
11842cbcd3c1SGleb Smirnoff {
11852cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
11862cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
11872cbcd3c1SGleb Smirnoff 	int error;
11882cbcd3c1SGleb Smirnoff 
11892cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
11902cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
11912cbcd3c1SGleb Smirnoff 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
11922cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
11932cbcd3c1SGleb Smirnoff 		for (int i = 0; i < count; i++)
11942cbcd3c1SGleb Smirnoff 			m = m_free(m);
11952cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
11962cbcd3c1SGleb Smirnoff 	}
11972cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
11982cbcd3c1SGleb Smirnoff 
11992cbcd3c1SGleb Smirnoff 	SOCKBUF_LOCK(&so->so_snd);
12002cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
12012cbcd3c1SGleb Smirnoff 	SOCKBUF_UNLOCK(&so->so_snd);
12022cbcd3c1SGleb Smirnoff 	if (error == 0)
120355bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
12042cbcd3c1SGleb Smirnoff 	INP_WUNLOCK(inp);
12052cbcd3c1SGleb Smirnoff 
12062cbcd3c1SGleb Smirnoff 	return (error);
12072cbcd3c1SGleb Smirnoff }
12082cbcd3c1SGleb Smirnoff 
12092c37256eSGarrett Wollman /*
1210a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
12112c37256eSGarrett Wollman  */
1212ac45e92fSRobert Watson static void
12132c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
12142c37256eSGarrett Wollman {
1215f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1216a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
12176573d758SMatt Macy 	struct epoch_tracker et;
1218623dce13SRobert Watson 	TCPDEBUG0;
1219c78cbc7bSRobert Watson 
1220ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1221c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1222c78cbc7bSRobert Watson 
12236573d758SMatt Macy 	INP_INFO_RLOCK_ET(&V_tcbinfo, et);
12248501a69cSRobert Watson 	INP_WLOCK(inp);
1225c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1226c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1227c78cbc7bSRobert Watson 
1228c78cbc7bSRobert Watson 	/*
1229a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1230c78cbc7bSRobert Watson 	 */
1231ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1232ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1233c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
1234a152f8a3SRobert Watson 		TCPDEBUG1();
12358fa799bdSJonathan T. Looney 		tp = tcp_drop(tp, ECONNABORTED);
12368fa799bdSJonathan T. Looney 		if (tp == NULL)
12378fa799bdSJonathan T. Looney 			goto dropped;
1238a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
12395d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1240c78cbc7bSRobert Watson 	}
1241ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1242a152f8a3SRobert Watson 		SOCK_LOCK(so);
1243a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1244a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1245ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1246a152f8a3SRobert Watson 	}
12478501a69cSRobert Watson 	INP_WUNLOCK(inp);
12488fa799bdSJonathan T. Looney dropped:
12496573d758SMatt Macy 	INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
1250a152f8a3SRobert Watson }
1251a152f8a3SRobert Watson 
1252a152f8a3SRobert Watson /*
1253a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1254a152f8a3SRobert Watson  */
1255a152f8a3SRobert Watson static void
1256a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1257a152f8a3SRobert Watson {
1258a152f8a3SRobert Watson 	struct inpcb *inp;
1259a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
12606573d758SMatt Macy 	struct epoch_tracker et;
1261a152f8a3SRobert Watson 	TCPDEBUG0;
1262a152f8a3SRobert Watson 
1263a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1264a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1265a152f8a3SRobert Watson 
12666573d758SMatt Macy 	INP_INFO_RLOCK_ET(&V_tcbinfo, et);
12678501a69cSRobert Watson 	INP_WLOCK(inp);
1268a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1269a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1270a152f8a3SRobert Watson 
1271a152f8a3SRobert Watson 	/*
1272a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
1273a152f8a3SRobert Watson 	 * a disconnect.
1274a152f8a3SRobert Watson 	 */
1275ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1276ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1277a152f8a3SRobert Watson 		tp = intotcpcb(inp);
1278a152f8a3SRobert Watson 		TCPDEBUG1();
1279a152f8a3SRobert Watson 		tcp_disconnect(tp);
1280a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
12815d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1282a152f8a3SRobert Watson 	}
1283ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1284a152f8a3SRobert Watson 		SOCK_LOCK(so);
1285a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1286a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1287ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1288a152f8a3SRobert Watson 	}
12898501a69cSRobert Watson 	INP_WUNLOCK(inp);
12906573d758SMatt Macy 	INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
12912c37256eSGarrett Wollman }
12922c37256eSGarrett Wollman 
12932c37256eSGarrett Wollman /*
12942c37256eSGarrett Wollman  * Receive out-of-band data.
12952c37256eSGarrett Wollman  */
12962c37256eSGarrett Wollman static int
12972c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
12982c37256eSGarrett Wollman {
12992c37256eSGarrett Wollman 	int error = 0;
1300f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1301623dce13SRobert Watson 	struct tcpcb *tp = NULL;
13022c37256eSGarrett Wollman 
1303623dce13SRobert Watson 	TCPDEBUG0;
1304623dce13SRobert Watson 	inp = sotoinpcb(so);
1305623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
13068501a69cSRobert Watson 	INP_WLOCK(inp);
1307ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
130821367f63SSam Leffler 		error = ECONNRESET;
1309623dce13SRobert Watson 		goto out;
1310623dce13SRobert Watson 	}
1311623dce13SRobert Watson 	tp = intotcpcb(inp);
1312623dce13SRobert Watson 	TCPDEBUG1();
13132c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1314c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
13154cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
13164cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
13172c37256eSGarrett Wollman 		error = EINVAL;
13182c37256eSGarrett Wollman 		goto out;
13192c37256eSGarrett Wollman 	}
13202c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
13212c37256eSGarrett Wollman 		error = EWOULDBLOCK;
13222c37256eSGarrett Wollman 		goto out;
13232c37256eSGarrett Wollman 	}
13242c37256eSGarrett Wollman 	m->m_len = 1;
13252c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
13262c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
13272c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1328623dce13SRobert Watson 
1329623dce13SRobert Watson out:
1330623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
13315d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
13328501a69cSRobert Watson 	INP_WUNLOCK(inp);
1333623dce13SRobert Watson 	return (error);
13342c37256eSGarrett Wollman }
13352c37256eSGarrett Wollman 
1336b287c6c7SBjoern A. Zeeb #ifdef INET
13372c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1338756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1339756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1340756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1341756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1342756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1343756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1344756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1345756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1346756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
134754d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1348756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1349756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1350756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
13512cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1352756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
135354d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1354a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1355a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
13562c37256eSGarrett Wollman };
1357b287c6c7SBjoern A. Zeeb #endif /* INET */
1358df8bae1dSRodney W. Grimes 
1359fb59c426SYoshinobu Inoue #ifdef INET6
1360fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1361756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1362756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1363756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1364756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1365756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1366756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1367756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1368756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1369756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1370756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1371756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1372756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1373756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
13742cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1375756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1376756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1377a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1378a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1379fb59c426SYoshinobu Inoue };
1380fb59c426SYoshinobu Inoue #endif /* INET6 */
1381fb59c426SYoshinobu Inoue 
1382b287c6c7SBjoern A. Zeeb #ifdef INET
1383a0292f23SGarrett Wollman /*
1384a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1385a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
13865200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
13875200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
13885200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
13895200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1390a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1391a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1392a0292f23SGarrett Wollman  */
13930312fbe9SPoul-Henning Kamp static int
1394ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1395a0292f23SGarrett Wollman {
1396a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1397a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
13985200e00eSIan Dowse 	struct in_addr laddr;
13995200e00eSIan Dowse 	u_short lport;
1400c3229e05SDavid Greenman 	int error;
1401a0292f23SGarrett Wollman 
14028501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1403fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1404623dce13SRobert Watson 
1405a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
14064616026fSErmal Luçi 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
14074616026fSErmal Luçi 		if (error)
1408fa046d87SRobert Watson 			goto out;
1409a0292f23SGarrett Wollman 	}
1410a0292f23SGarrett Wollman 
1411a0292f23SGarrett Wollman 	/*
1412a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1413a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1414a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1415a0292f23SGarrett Wollman 	 */
14165200e00eSIan Dowse 	laddr = inp->inp_laddr;
14175200e00eSIan Dowse 	lport = inp->inp_lport;
14185200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1419b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
14205200e00eSIan Dowse 	if (error && oinp == NULL)
1421fa046d87SRobert Watson 		goto out;
1422fa046d87SRobert Watson 	if (oinp) {
1423fa046d87SRobert Watson 		error = EADDRINUSE;
1424fa046d87SRobert Watson 		goto out;
1425fa046d87SRobert Watson 	}
14265200e00eSIan Dowse 	inp->inp_laddr = laddr;
142715bd2b43SDavid Greenman 	in_pcbrehash(inp);
1428fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1429a0292f23SGarrett Wollman 
1430087b55eaSAndre Oppermann 	/*
1431087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1432087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1433087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1434087b55eaSAndre Oppermann 	 */
1435a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
14369b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1437a0292f23SGarrett Wollman 		tp->request_r_scale++;
1438a0292f23SGarrett Wollman 
1439a0292f23SGarrett Wollman 	soisconnecting(so);
144078b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
144157f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
14428e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
14438e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
14448e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1445a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1446a45d2726SAndras Olah 
1447a0292f23SGarrett Wollman 	return 0;
1448fa046d87SRobert Watson 
1449fa046d87SRobert Watson out:
1450fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1451fa046d87SRobert Watson 	return (error);
1452a0292f23SGarrett Wollman }
1453b287c6c7SBjoern A. Zeeb #endif /* INET */
1454a0292f23SGarrett Wollman 
1455fb59c426SYoshinobu Inoue #ifdef INET6
1456fb59c426SYoshinobu Inoue static int
1457ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1458fb59c426SYoshinobu Inoue {
1459a7e201bbSAndrey V. Elsukov 	struct inpcb *inp = tp->t_inpcb;
1460fb59c426SYoshinobu Inoue 	int error;
1461fb59c426SYoshinobu Inoue 
14628501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1463fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1464623dce13SRobert Watson 
1465fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
14664616026fSErmal Luçi 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
14674616026fSErmal Luçi 		if (error)
1468fa046d87SRobert Watson 			goto out;
1469fb59c426SYoshinobu Inoue 	}
1470a7e201bbSAndrey V. Elsukov 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1471a7e201bbSAndrey V. Elsukov 	if (error != 0)
1472b598155aSRobert Watson 		goto out;
1473fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1474fb59c426SYoshinobu Inoue 
1475fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1476fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1477970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1478fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1479fb59c426SYoshinobu Inoue 
1480a7e201bbSAndrey V. Elsukov 	soisconnecting(inp->inp_socket);
148178b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
148257f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
14838e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
14848e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
14858e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1486fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1487fb59c426SYoshinobu Inoue 
1488fb59c426SYoshinobu Inoue 	return 0;
1489fa046d87SRobert Watson 
1490fa046d87SRobert Watson out:
1491fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1492fa046d87SRobert Watson 	return error;
1493fb59c426SYoshinobu Inoue }
1494fb59c426SYoshinobu Inoue #endif /* INET6 */
1495fb59c426SYoshinobu Inoue 
1496cfe8b629SGarrett Wollman /*
1497b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1498b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1499b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1500b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1501b8af5dfaSRobert Watson  * from Linux.
1502b8af5dfaSRobert Watson  */
1503b8af5dfaSRobert Watson static void
1504ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1505b8af5dfaSRobert Watson {
1506b8af5dfaSRobert Watson 
15078501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1508b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1509b8af5dfaSRobert Watson 
1510b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1511b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1512b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
15133529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1514b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1515b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1516b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1517b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1518b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1519b8af5dfaSRobert Watson 	}
15205a17b6adSMichael Tuexen 	if (tp->t_flags & TF_ECN_PERMIT)
15215a17b6adSMichael Tuexen 		ti->tcpi_options |= TCPI_OPT_ECN;
15221baaf834SBruce M Simpson 
152343d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
15243ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
15251baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
15261baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
15271baaf834SBruce M Simpson 
1528b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1529b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1530b8af5dfaSRobert Watson 
1531b8af5dfaSRobert Watson 	/*
1532b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1533b8af5dfaSRobert Watson 	 */
1534c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1535535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1536b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
15371c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1538535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
153943d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
154043d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1541f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1542f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1543f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1544a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1545a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1546a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1547a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1548a6456410SNavdeep Parhar 	}
1549a6456410SNavdeep Parhar #endif
1550b8af5dfaSRobert Watson }
1551b8af5dfaSRobert Watson 
1552b8af5dfaSRobert Watson /*
15531e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
15541e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
15551e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
15561e8f5ffaSRobert Watson  * option.
1557cfe8b629SGarrett Wollman  */
1558bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
15598501a69cSRobert Watson 	INP_WLOCK(inp);							\
1560ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
15618501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1562bac5bedfSConrad Meyer 		cleanup;						\
15631e8f5ffaSRobert Watson 		return (ECONNRESET);					\
15641e8f5ffaSRobert Watson 	}								\
15651e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
15661e8f5ffaSRobert Watson } while(0)
1567bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
15681e8f5ffaSRobert Watson 
1569df8bae1dSRodney W. Grimes int
1570ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1571df8bae1dSRodney W. Grimes {
157255bceb1eSRandall Stewart 	int	error;
1573df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1574cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
157555bceb1eSRandall Stewart 	struct tcp_function_block *blk;
157655bceb1eSRandall Stewart 	struct tcp_function_set fsn;
1577df8bae1dSRodney W. Grimes 
1578cfe8b629SGarrett Wollman 	error = 0;
1579df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1580623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
15818501a69cSRobert Watson 	INP_WLOCK(inp);
1582cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1583fb59c426SYoshinobu Inoue #ifdef INET6
15845cd54324SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6PROTO) {
15858501a69cSRobert Watson 			INP_WUNLOCK(inp);
1586fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
15875dff1c38SMichael Tuexen 			/*
15885dff1c38SMichael Tuexen 			 * In case of the IPV6_USE_MIN_MTU socket option,
15895dff1c38SMichael Tuexen 			 * the INC_IPV6MINMTU flag to announce a corresponding
15905dff1c38SMichael Tuexen 			 * MSS during the initial handshake.
15915dff1c38SMichael Tuexen 			 * If the TCP connection is not in the front states,
15925dff1c38SMichael Tuexen 			 * just reduce the MSS being used.
15935dff1c38SMichael Tuexen 			 * This avoids the sending of TCP segments which will
15945dff1c38SMichael Tuexen 			 * be fragmented at the IPv6 layer.
15955dff1c38SMichael Tuexen 			 */
15965dff1c38SMichael Tuexen 			if ((error == 0) &&
15975dff1c38SMichael Tuexen 			    (sopt->sopt_dir == SOPT_SET) &&
15985dff1c38SMichael Tuexen 			    (sopt->sopt_level == IPPROTO_IPV6) &&
15995dff1c38SMichael Tuexen 			    (sopt->sopt_name == IPV6_USE_MIN_MTU)) {
16005dff1c38SMichael Tuexen 				INP_WLOCK(inp);
16015dff1c38SMichael Tuexen 				if ((inp->inp_flags &
16025dff1c38SMichael Tuexen 				    (INP_TIMEWAIT | INP_DROPPED))) {
16035dff1c38SMichael Tuexen 					INP_WUNLOCK(inp);
16045dff1c38SMichael Tuexen 					return (ECONNRESET);
16055dff1c38SMichael Tuexen 				}
16065dff1c38SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_IPV6MINMTU;
16075dff1c38SMichael Tuexen 				tp = intotcpcb(inp);
16085dff1c38SMichael Tuexen 				if ((tp->t_state >= TCPS_SYN_SENT) &&
16095dff1c38SMichael Tuexen 				    (inp->inp_inc.inc_flags & INC_ISIPV6)) {
16105dff1c38SMichael Tuexen 					struct ip6_pktopts *opt;
16115dff1c38SMichael Tuexen 
16125dff1c38SMichael Tuexen 					opt = inp->in6p_outputopts;
16135dff1c38SMichael Tuexen 					if ((opt != NULL) &&
16145dff1c38SMichael Tuexen 					    (opt->ip6po_minmtu ==
16155dff1c38SMichael Tuexen 					    IP6PO_MINMTU_ALL)) {
16165dff1c38SMichael Tuexen 						if (tp->t_maxseg > TCP6_MSS) {
16175dff1c38SMichael Tuexen 							tp->t_maxseg = TCP6_MSS;
16185dff1c38SMichael Tuexen 						}
16195dff1c38SMichael Tuexen 					}
16205dff1c38SMichael Tuexen 				}
16215dff1c38SMichael Tuexen 				INP_WUNLOCK(inp);
16225dff1c38SMichael Tuexen 			}
1623b287c6c7SBjoern A. Zeeb 		}
1624fb59c426SYoshinobu Inoue #endif /* INET6 */
1625b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1626b287c6c7SBjoern A. Zeeb 		else
1627b287c6c7SBjoern A. Zeeb #endif
1628b287c6c7SBjoern A. Zeeb #ifdef INET
1629b287c6c7SBjoern A. Zeeb 		{
16308501a69cSRobert Watson 			INP_WUNLOCK(inp);
1631cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
16321e8f5ffaSRobert Watson 		}
16331e8f5ffaSRobert Watson #endif
1634df8bae1dSRodney W. Grimes 		return (error);
1635df8bae1dSRodney W. Grimes 	}
1636ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
16378501a69cSRobert Watson 		INP_WUNLOCK(inp);
16381e8f5ffaSRobert Watson 		return (ECONNRESET);
1639623dce13SRobert Watson 	}
164055bceb1eSRandall Stewart 	tp = intotcpcb(inp);
164155bceb1eSRandall Stewart 	/*
164255bceb1eSRandall Stewart 	 * Protect the TCP option TCP_FUNCTION_BLK so
164355bceb1eSRandall Stewart 	 * that a sub-function can *never* overwrite this.
164455bceb1eSRandall Stewart 	 */
164555bceb1eSRandall Stewart 	if ((sopt->sopt_dir == SOPT_SET) &&
164655bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
164755bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
164855bceb1eSRandall Stewart 		error = sooptcopyin(sopt, &fsn, sizeof fsn,
164955bceb1eSRandall Stewart 		    sizeof fsn);
165055bceb1eSRandall Stewart 		if (error)
165155bceb1eSRandall Stewart 			return (error);
165255bceb1eSRandall Stewart 		INP_WLOCK_RECHECK(inp);
165355bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
165455bceb1eSRandall Stewart 		if (blk == NULL) {
165555bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
165655bceb1eSRandall Stewart 			return (ENOENT);
165755bceb1eSRandall Stewart 		}
1658587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1659587d67c0SRandall Stewart 			/* You already have this */
1660587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1661587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1662587d67c0SRandall Stewart 			return (0);
1663587d67c0SRandall Stewart 		}
1664587d67c0SRandall Stewart 		if (tp->t_state != TCPS_CLOSED) {
1665587d67c0SRandall Stewart 			/*
1666587d67c0SRandall Stewart 			 * The user has advanced the state
1667587d67c0SRandall Stewart 			 * past the initial point, we may not
1668587d67c0SRandall Stewart 			 * be able to switch.
1669587d67c0SRandall Stewart 			 */
1670587d67c0SRandall Stewart 			if (blk->tfb_tcp_handoff_ok != NULL) {
1671587d67c0SRandall Stewart 				/*
1672587d67c0SRandall Stewart 				 * Does the stack provide a
1673587d67c0SRandall Stewart 				 * query mechanism, if so it may
1674587d67c0SRandall Stewart 				 * still be possible?
1675587d67c0SRandall Stewart 				 */
1676587d67c0SRandall Stewart 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1677c6c0be27SMichael Tuexen 			} else
1678c6c0be27SMichael Tuexen 				error = EINVAL;
1679587d67c0SRandall Stewart 			if (error) {
1680587d67c0SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1681587d67c0SRandall Stewart 				INP_WUNLOCK(inp);
1682587d67c0SRandall Stewart 				return(error);
1683587d67c0SRandall Stewart 			}
1684587d67c0SRandall Stewart 		}
168555bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
168655bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
168755bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
168855bceb1eSRandall Stewart 			return (ENOENT);
168955bceb1eSRandall Stewart 		}
169055bceb1eSRandall Stewart 		/*
169155bceb1eSRandall Stewart 		 * Release the old refcnt, the
1692587d67c0SRandall Stewart 		 * lookup acquired a ref on the
1693587d67c0SRandall Stewart 		 * new one already.
169455bceb1eSRandall Stewart 		 */
1695587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1696587d67c0SRandall Stewart 			/*
1697587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1698587d67c0SRandall Stewart 			 * the tcb is not going away.
1699587d67c0SRandall Stewart 			 */
1700587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1701587d67c0SRandall Stewart 		}
17023ee9c3c4SRandall Stewart #ifdef TCPHPTS
17033ee9c3c4SRandall Stewart 		/* Assure that we are not on any hpts */
17043ee9c3c4SRandall Stewart 		tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
17053ee9c3c4SRandall Stewart #endif
17063ee9c3c4SRandall Stewart 		if (blk->tfb_tcp_fb_init) {
17073ee9c3c4SRandall Stewart 			error = (*blk->tfb_tcp_fb_init)(tp);
17083ee9c3c4SRandall Stewart 			if (error) {
17093ee9c3c4SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
17103ee9c3c4SRandall Stewart 				if (tp->t_fb->tfb_tcp_fb_init) {
17113ee9c3c4SRandall Stewart 					if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0)  {
17123ee9c3c4SRandall Stewart 						/* Fall back failed, drop the connection */
17133ee9c3c4SRandall Stewart 						INP_WUNLOCK(inp);
17143ee9c3c4SRandall Stewart 						soabort(so);
17153ee9c3c4SRandall Stewart 						return(error);
17163ee9c3c4SRandall Stewart 					}
17173ee9c3c4SRandall Stewart 				}
17183ee9c3c4SRandall Stewart 				goto err_out;
17193ee9c3c4SRandall Stewart 			}
17203ee9c3c4SRandall Stewart 		}
172155bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
172255bceb1eSRandall Stewart 		tp->t_fb = blk;
172355bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
172455bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
172555bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
172655bceb1eSRandall Stewart 			     sopt->sopt_name);
172755bceb1eSRandall Stewart 		}
172855bceb1eSRandall Stewart #endif
17293ee9c3c4SRandall Stewart err_out:
173055bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
173155bceb1eSRandall Stewart 		return (error);
173255bceb1eSRandall Stewart 	} else if ((sopt->sopt_dir == SOPT_GET) &&
173355bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1734c73b6f4dSEd Maste 		strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1735c73b6f4dSEd Maste 		    TCP_FUNCTION_NAME_LEN_MAX);
1736c73b6f4dSEd Maste 		fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
173755bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
173855bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
173955bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
174055bceb1eSRandall Stewart 		return (error);
174155bceb1eSRandall Stewart 	}
174255bceb1eSRandall Stewart 	/* Pass in the INP locked, called must unlock it */
174355bceb1eSRandall Stewart 	return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
174455bceb1eSRandall Stewart }
174555bceb1eSRandall Stewart 
17462529f56eSJonathan T. Looney /*
17472529f56eSJonathan T. Looney  * If this assert becomes untrue, we need to change the size of the buf
17482529f56eSJonathan T. Looney  * variable in tcp_default_ctloutput().
17492529f56eSJonathan T. Looney  */
17502529f56eSJonathan T. Looney #ifdef CTASSERT
17512529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
17522529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
17532529f56eSJonathan T. Looney #endif
17542529f56eSJonathan T. Looney 
175555bceb1eSRandall Stewart int
175655bceb1eSRandall Stewart tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
175755bceb1eSRandall Stewart {
175855bceb1eSRandall Stewart 	int	error, opt, optval;
175955bceb1eSRandall Stewart 	u_int	ui;
176055bceb1eSRandall Stewart 	struct	tcp_info ti;
176155bceb1eSRandall Stewart 	struct cc_algo *algo;
17622529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
1763af6fef3aSGleb Smirnoff 	size_t	len;
1764df8bae1dSRodney W. Grimes 
1765d519cedbSGleb Smirnoff 	/*
1766d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
1767d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
1768d519cedbSGleb Smirnoff 	 */
1769d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
1770d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
1771d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1772*c8b53cedSMichael Tuexen 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
1773*c8b53cedSMichael Tuexen 			return (EINVAL);
1774af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1775af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1776d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
1777d519cedbSGleb Smirnoff 		if (error) {
1778af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
1779d519cedbSGleb Smirnoff 			return (error);
1780d519cedbSGleb Smirnoff 		}
1781bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1782d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
1783af6fef3aSGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1784d519cedbSGleb Smirnoff 		else
1785d519cedbSGleb Smirnoff 			error = ENOENT;
1786d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1787d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
1788af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1789af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
1790d519cedbSGleb Smirnoff 		return (error);
1791d519cedbSGleb Smirnoff 	}
1792d519cedbSGleb Smirnoff 
1793cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1794cfe8b629SGarrett Wollman 	case SOPT_SET:
1795cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1796fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
179788f6b043SBruce M Simpson 		case TCP_MD5SIG:
1798fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
17998501a69cSRobert Watson 				INP_WUNLOCK(inp);
1800fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
1801fcf59617SAndrey V. Elsukov 			}
1802fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
18031cfd4b53SBruce M Simpson 			if (error)
18041e8f5ffaSRobert Watson 				return (error);
180509fe6320SNavdeep Parhar 			goto unlock_and_done;
1806fcf59617SAndrey V. Elsukov #endif /* IPSEC */
180709fe6320SNavdeep Parhar 
1808df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1809cfe8b629SGarrett Wollman 		case TCP_NOOPT:
18108501a69cSRobert Watson 			INP_WUNLOCK(inp);
1811cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1812cfe8b629SGarrett Wollman 			    sizeof optval);
1813cfe8b629SGarrett Wollman 			if (error)
18141e8f5ffaSRobert Watson 				return (error);
1815cfe8b629SGarrett Wollman 
18168501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1817cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1818cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1819cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1820cfe8b629SGarrett Wollman 				break;
1821cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1822cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1823cfe8b629SGarrett Wollman 				break;
1824cfe8b629SGarrett Wollman 			default:
1825cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1826cfe8b629SGarrett Wollman 				break;
1827cfe8b629SGarrett Wollman 			}
1828cfe8b629SGarrett Wollman 
1829cfe8b629SGarrett Wollman 			if (optval)
1830cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1831df8bae1dSRodney W. Grimes 			else
1832cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
183309fe6320SNavdeep Parhar unlock_and_done:
183409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
183509fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
183609fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
183709fe6320SNavdeep Parhar 				    sopt->sopt_name);
183809fe6320SNavdeep Parhar 			}
183909fe6320SNavdeep Parhar #endif
18408501a69cSRobert Watson 			INP_WUNLOCK(inp);
1841df8bae1dSRodney W. Grimes 			break;
1842df8bae1dSRodney W. Grimes 
1843007581c0SJonathan Lemon 		case TCP_NOPUSH:
18448501a69cSRobert Watson 			INP_WUNLOCK(inp);
1845007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1846007581c0SJonathan Lemon 			    sizeof optval);
1847007581c0SJonathan Lemon 			if (error)
18481e8f5ffaSRobert Watson 				return (error);
1849007581c0SJonathan Lemon 
18508501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1851007581c0SJonathan Lemon 			if (optval)
1852007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1853d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
1854007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1855d28b9e89SJohn Baldwin 				if (TCPS_HAVEESTABLISHED(tp->t_state))
185655bceb1eSRandall Stewart 					error = tp->t_fb->tfb_tcp_output(tp);
1857007581c0SJonathan Lemon 			}
185809fe6320SNavdeep Parhar 			goto unlock_and_done;
1859007581c0SJonathan Lemon 
1860df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
18618501a69cSRobert Watson 			INP_WUNLOCK(inp);
1862cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1863cfe8b629SGarrett Wollman 			    sizeof optval);
1864cfe8b629SGarrett Wollman 			if (error)
18651e8f5ffaSRobert Watson 				return (error);
1866df8bae1dSRodney W. Grimes 
18678501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
186853369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
1869603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
1870cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1871a0292f23SGarrett Wollman 			else
1872a0292f23SGarrett Wollman 				error = EINVAL;
187309fe6320SNavdeep Parhar 			goto unlock_and_done;
1874a0292f23SGarrett Wollman 
1875b8af5dfaSRobert Watson 		case TCP_INFO:
18768501a69cSRobert Watson 			INP_WUNLOCK(inp);
1877b8af5dfaSRobert Watson 			error = EINVAL;
1878b8af5dfaSRobert Watson 			break;
1879b8af5dfaSRobert Watson 
1880dbc42409SLawrence Stewart 		case TCP_CONGESTION:
1881dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
1882af6fef3aSGleb Smirnoff 			error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1883af6fef3aSGleb Smirnoff 			if (error)
1884dbc42409SLawrence Stewart 				break;
1885af6fef3aSGleb Smirnoff 			buf[sopt->sopt_valsize] = '\0';
1886af6fef3aSGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
188773e263b1SGleb Smirnoff 			CC_LIST_RLOCK();
188873e263b1SGleb Smirnoff 			STAILQ_FOREACH(algo, &cc_list, entries)
188973e263b1SGleb Smirnoff 				if (strncmp(buf, algo->name,
189073e263b1SGleb Smirnoff 				    TCP_CA_NAME_MAX) == 0)
189173e263b1SGleb Smirnoff 					break;
189273e263b1SGleb Smirnoff 			CC_LIST_RUNLOCK();
189373e263b1SGleb Smirnoff 			if (algo == NULL) {
1894af6fef3aSGleb Smirnoff 				INP_WUNLOCK(inp);
189573e263b1SGleb Smirnoff 				error = EINVAL;
189673e263b1SGleb Smirnoff 				break;
189773e263b1SGleb Smirnoff 			}
1898dbc42409SLawrence Stewart 			/*
189973e263b1SGleb Smirnoff 			 * We hold a write lock over the tcb so it's safe to
190073e263b1SGleb Smirnoff 			 * do these things without ordering concerns.
1901dbc42409SLawrence Stewart 			 */
1902dbc42409SLawrence Stewart 			if (CC_ALGO(tp)->cb_destroy != NULL)
1903dbc42409SLawrence Stewart 				CC_ALGO(tp)->cb_destroy(tp->ccv);
190422699887SMatt Macy 			CC_DATA(tp) = NULL;
1905dbc42409SLawrence Stewart 			CC_ALGO(tp) = algo;
1906dbc42409SLawrence Stewart 			/*
190773e263b1SGleb Smirnoff 			 * If something goes pear shaped initialising the new
190873e263b1SGleb Smirnoff 			 * algo, fall back to newreno (which does not
190973e263b1SGleb Smirnoff 			 * require initialisation).
1910dbc42409SLawrence Stewart 			 */
191173e263b1SGleb Smirnoff 			if (algo->cb_init != NULL &&
191273e263b1SGleb Smirnoff 			    algo->cb_init(tp->ccv) != 0) {
1913dbc42409SLawrence Stewart 				CC_ALGO(tp) = &newreno_cc_algo;
1914dbc42409SLawrence Stewart 				/*
191573e263b1SGleb Smirnoff 				 * The only reason init should fail is
1916dbc42409SLawrence Stewart 				 * because of malloc.
1917dbc42409SLawrence Stewart 				 */
1918dbc42409SLawrence Stewart 				error = ENOMEM;
1919dbc42409SLawrence Stewart 			}
192073e263b1SGleb Smirnoff 			INP_WUNLOCK(inp);
192173e263b1SGleb Smirnoff 			break;
1922dbc42409SLawrence Stewart 
19239077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
19249077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
19259077f387SGleb Smirnoff 		case TCP_KEEPINIT:
19269077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
19279077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
19289077f387SGleb Smirnoff 			if (error)
19299077f387SGleb Smirnoff 				return (error);
19309077f387SGleb Smirnoff 
19319077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
19329077f387SGleb Smirnoff 				error = EINVAL;
19339077f387SGleb Smirnoff 				break;
19349077f387SGleb Smirnoff 			}
19359077f387SGleb Smirnoff 			ui *= hz;
19369077f387SGleb Smirnoff 
19379077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
19389077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
19399077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
19409077f387SGleb Smirnoff 				tp->t_keepidle = ui;
19419077f387SGleb Smirnoff 				/*
19429077f387SGleb Smirnoff 				 * XXX: better check current remaining
19439077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
19449077f387SGleb Smirnoff 				 */
19459077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
19469077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
19479077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
19489077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
19499077f387SGleb Smirnoff 				break;
19509077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
19519077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
19529077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
19539077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
19549077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
19559077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
19569077f387SGleb Smirnoff 				break;
19579077f387SGleb Smirnoff 			case TCP_KEEPINIT:
19589077f387SGleb Smirnoff 				tp->t_keepinit = ui;
19599077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
19609077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
19619077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
19629077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
19639077f387SGleb Smirnoff 				break;
19649077f387SGleb Smirnoff 			}
196509fe6320SNavdeep Parhar 			goto unlock_and_done;
19669077f387SGleb Smirnoff 
196785c05144SGleb Smirnoff 		case TCP_KEEPCNT:
196885c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
196985c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
197085c05144SGleb Smirnoff 			if (error)
197185c05144SGleb Smirnoff 				return (error);
197285c05144SGleb Smirnoff 
197385c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
197485c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
197585c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
197685c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
197785c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
197885c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
197985c05144SGleb Smirnoff 			goto unlock_and_done;
198085c05144SGleb Smirnoff 
198186a996e6SHiren Panchasara #ifdef TCPPCAP
198286a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
198386a996e6SHiren Panchasara 		case TCP_PCAP_IN:
198486a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
198586a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
198686a996e6SHiren Panchasara 			    sizeof optval);
198786a996e6SHiren Panchasara 			if (error)
198886a996e6SHiren Panchasara 				return (error);
198986a996e6SHiren Panchasara 
199086a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
199186a996e6SHiren Panchasara 			if (optval >= 0)
199286a996e6SHiren Panchasara 				tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
199386a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
199486a996e6SHiren Panchasara 					optval);
199586a996e6SHiren Panchasara 			else
199686a996e6SHiren Panchasara 				error = EINVAL;
199786a996e6SHiren Panchasara 			goto unlock_and_done;
199886a996e6SHiren Panchasara #endif
199986a996e6SHiren Panchasara 
2000c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
2001c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
2002c560df6fSPatrick Kelsey 
2003281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2004c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
2005c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
2006281a0fd4SPatrick Kelsey 				return (EPERM);
2007281a0fd4SPatrick Kelsey 
2008c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
2009c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
2010281a0fd4SPatrick Kelsey 			if (error)
2011281a0fd4SPatrick Kelsey 				return (error);
2012281a0fd4SPatrick Kelsey 
2013281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
2014c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
2015c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
2016c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
2017c560df6fSPatrick Kelsey 						error = EPERM;
2018c560df6fSPatrick Kelsey 						goto unlock_and_done;
2019c560df6fSPatrick Kelsey 					}
2020c560df6fSPatrick Kelsey 
2021281a0fd4SPatrick Kelsey 					tp->t_flags |= TF_FASTOPEN;
2022c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
2023281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
2024281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
2025c560df6fSPatrick Kelsey 				} else {
2026c560df6fSPatrick Kelsey 					/*
2027c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
2028c560df6fSPatrick Kelsey 					 * stash it in the client cookie
2029c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
2030c560df6fSPatrick Kelsey 					 * connect.
2031c560df6fSPatrick Kelsey 					 */
2032c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
2033c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
2034c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
2035c560df6fSPatrick Kelsey 						       tfo_optval.psk,
2036c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
2037c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
2038c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
2039c560df6fSPatrick Kelsey 					}
2040c560df6fSPatrick Kelsey 					tp->t_flags |= TF_FASTOPEN;
2041c560df6fSPatrick Kelsey 				}
2042281a0fd4SPatrick Kelsey 			} else
2043281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
2044281a0fd4SPatrick Kelsey 			goto unlock_and_done;
2045c560df6fSPatrick Kelsey 		}
2046281a0fd4SPatrick Kelsey 
2047e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
20482529f56eSJonathan T. Looney 		case TCP_LOG:
20492529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
20502529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
20512529f56eSJonathan T. Looney 			    sizeof optval);
20522529f56eSJonathan T. Looney 			if (error)
20532529f56eSJonathan T. Looney 				return (error);
20542529f56eSJonathan T. Looney 
20552529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
20562529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
20572529f56eSJonathan T. Looney 			goto unlock_and_done;
20582529f56eSJonathan T. Looney 
20592529f56eSJonathan T. Looney 		case TCP_LOGBUF:
20602529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
20612529f56eSJonathan T. Looney 			error = EINVAL;
20622529f56eSJonathan T. Looney 			break;
20632529f56eSJonathan T. Looney 
20642529f56eSJonathan T. Looney 		case TCP_LOGID:
20652529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
20662529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
20672529f56eSJonathan T. Looney 			if (error)
20682529f56eSJonathan T. Looney 				break;
20692529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
20702529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
20712529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
20722529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
20732529f56eSJonathan T. Looney 			break;
20742529f56eSJonathan T. Looney 
20752529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
20762529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
20772529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
20782529f56eSJonathan T. Looney 			error =
20792529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
20802529f56eSJonathan T. Looney 			if (error)
20812529f56eSJonathan T. Looney 				break;
20822529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
20832529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
20842529f56eSJonathan T. Looney 			if (sopt->sopt_name == TCP_LOGDUMP) {
20852529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
20862529f56eSJonathan T. Looney 				    M_WAITOK, true);
20872529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
20882529f56eSJonathan T. Looney 			} else {
20892529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
20902529f56eSJonathan T. Looney 				/*
20912529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
20922529f56eSJonathan T. Looney 				 * INP lock.
20932529f56eSJonathan T. Looney 				 */
20942529f56eSJonathan T. Looney 			}
20952529f56eSJonathan T. Looney 			break;
2096e24e5683SJonathan T. Looney #endif
20972529f56eSJonathan T. Looney 
2098df8bae1dSRodney W. Grimes 		default:
20998501a69cSRobert Watson 			INP_WUNLOCK(inp);
2100df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2101df8bae1dSRodney W. Grimes 			break;
2102df8bae1dSRodney W. Grimes 		}
2103df8bae1dSRodney W. Grimes 		break;
2104df8bae1dSRodney W. Grimes 
2105cfe8b629SGarrett Wollman 	case SOPT_GET:
21061e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
2107cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2108fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
210988f6b043SBruce M Simpson 		case TCP_MD5SIG:
2110fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
21118501a69cSRobert Watson 				INP_WUNLOCK(inp);
2112fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2113fcf59617SAndrey V. Elsukov 			}
2114fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
21151cfd4b53SBruce M Simpson 			break;
2116265ed012SBruce M Simpson #endif
21171e8f5ffaSRobert Watson 
2118df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2119cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
21208501a69cSRobert Watson 			INP_WUNLOCK(inp);
2121b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2122df8bae1dSRodney W. Grimes 			break;
2123df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
2124cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
21258501a69cSRobert Watson 			INP_WUNLOCK(inp);
2126b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2127df8bae1dSRodney W. Grimes 			break;
2128a0292f23SGarrett Wollman 		case TCP_NOOPT:
2129cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
21308501a69cSRobert Watson 			INP_WUNLOCK(inp);
2131b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2132a0292f23SGarrett Wollman 			break;
2133a0292f23SGarrett Wollman 		case TCP_NOPUSH:
2134cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
21358501a69cSRobert Watson 			INP_WUNLOCK(inp);
2136b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2137b8af5dfaSRobert Watson 			break;
2138b8af5dfaSRobert Watson 		case TCP_INFO:
2139b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
21408501a69cSRobert Watson 			INP_WUNLOCK(inp);
2141b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
2142a0292f23SGarrett Wollman 			break;
2143dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2144af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2145dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2146af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
2147dbc42409SLawrence Stewart 			break;
21482f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
21492f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
21502f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
21512f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
21522f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
21532f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
21545a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
21552f3eb7f4SGleb Smirnoff 				break;
21562f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
21575a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
21582f3eb7f4SGleb Smirnoff 				break;
21592f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
21605a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
21612f3eb7f4SGleb Smirnoff 				break;
21622f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
21635a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
21642f3eb7f4SGleb Smirnoff 				break;
21652f3eb7f4SGleb Smirnoff 			}
21662f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
21672f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
21682f3eb7f4SGleb Smirnoff 			break;
216986a996e6SHiren Panchasara #ifdef TCPPCAP
217086a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
217186a996e6SHiren Panchasara 		case TCP_PCAP_IN:
217286a996e6SHiren Panchasara 			optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
217386a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
217486a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
217586a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
217686a996e6SHiren Panchasara 			break;
217786a996e6SHiren Panchasara #endif
2178281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
2179281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
2180281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2181281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
2182281a0fd4SPatrick Kelsey 			break;
2183e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
21842529f56eSJonathan T. Looney 		case TCP_LOG:
21852529f56eSJonathan T. Looney 			optval = tp->t_logstate;
21862529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
21872529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
21882529f56eSJonathan T. Looney 			break;
21892529f56eSJonathan T. Looney 		case TCP_LOGBUF:
21902529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
21912529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
21922529f56eSJonathan T. Looney 			break;
21932529f56eSJonathan T. Looney 		case TCP_LOGID:
21942529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
21952529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
21962529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
21972529f56eSJonathan T. Looney 			break;
21982529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
21992529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
22002529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
22012529f56eSJonathan T. Looney 			error = EINVAL;
22022529f56eSJonathan T. Looney 			break;
2203e24e5683SJonathan T. Looney #endif
2204df8bae1dSRodney W. Grimes 		default:
22058501a69cSRobert Watson 			INP_WUNLOCK(inp);
2206df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2207df8bae1dSRodney W. Grimes 			break;
2208df8bae1dSRodney W. Grimes 		}
2209df8bae1dSRodney W. Grimes 		break;
2210df8bae1dSRodney W. Grimes 	}
2211df8bae1dSRodney W. Grimes 	return (error);
2212df8bae1dSRodney W. Grimes }
22138501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2214bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2215df8bae1dSRodney W. Grimes 
221626e30fbbSDavid Greenman /*
2217df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
2218df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
2219df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
2220df8bae1dSRodney W. Grimes  */
22210312fbe9SPoul-Henning Kamp static int
2222ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so)
2223df8bae1dSRodney W. Grimes {
2224ad3f9ab3SAndre Oppermann 	struct tcpcb *tp;
2225df8bae1dSRodney W. Grimes 	struct inpcb *inp;
22266573d758SMatt Macy 	struct epoch_tracker et;
2227df8bae1dSRodney W. Grimes 	int error;
2228df8bae1dSRodney W. Grimes 
2229df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
2230e233e2acSAndre Oppermann 		error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
2231df8bae1dSRodney W. Grimes 		if (error)
2232df8bae1dSRodney W. Grimes 			return (error);
2233df8bae1dSRodney W. Grimes 	}
22346741ecf5SAndre Oppermann 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
22356741ecf5SAndre Oppermann 	so->so_snd.sb_flags |= SB_AUTOSIZE;
22366573d758SMatt Macy 	INP_INFO_RLOCK_ET(&V_tcbinfo, et);
2237603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_tcbinfo);
2238f2de87feSRobert Watson 	if (error) {
22396573d758SMatt Macy 		INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
2240df8bae1dSRodney W. Grimes 		return (error);
2241f2de87feSRobert Watson 	}
2242df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
2243fb59c426SYoshinobu Inoue #ifdef INET6
22445cd54324SBjoern A. Zeeb 	if (inp->inp_vflag & INP_IPV6PROTO) {
2245fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
224663ec505aSMichael Tuexen 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
224763ec505aSMichael Tuexen 			inp->inp_vflag |= INP_IPV4;
2248fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
2249fb59c426SYoshinobu Inoue 	}
2250fb59c426SYoshinobu Inoue 	else
2251fb59c426SYoshinobu Inoue #endif
2252cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
2253df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
2254623dce13SRobert Watson 	if (tp == NULL) {
2255df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
22560206cdb8SBjoern A. Zeeb 		in_pcbfree(inp);
22576573d758SMatt Macy 		INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
2258df8bae1dSRodney W. Grimes 		return (ENOBUFS);
2259df8bae1dSRodney W. Grimes 	}
2260df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
22618501a69cSRobert Watson 	INP_WUNLOCK(inp);
22626573d758SMatt Macy 	INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
2263bf840a17SGleb Smirnoff 	TCPSTATES_INC(TCPS_CLOSED);
2264df8bae1dSRodney W. Grimes 	return (0);
2265df8bae1dSRodney W. Grimes }
2266df8bae1dSRodney W. Grimes 
2267df8bae1dSRodney W. Grimes /*
2268df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2269df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2270df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2271df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2272df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2273df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2274df8bae1dSRodney W. Grimes  */
2275623dce13SRobert Watson static void
2276ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2277df8bae1dSRodney W. Grimes {
2278e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
2279e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
2280e6e0b5ffSRobert Watson 
2281ff9b006dSJulien Charbon 	INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
22828501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2283df8bae1dSRodney W. Grimes 
2284623dce13SRobert Watson 	/*
2285623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2286623dce13SRobert Watson 	 * socket is still open.
2287623dce13SRobert Watson 	 */
22888db239dcSMichael Tuexen 	if (tp->t_state < TCPS_ESTABLISHED &&
22898db239dcSMichael Tuexen 	    !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2290df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2291623dce13SRobert Watson 		KASSERT(tp != NULL,
2292623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2293623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2294243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2295623dce13SRobert Watson 		KASSERT(tp != NULL,
2296623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2297623dce13SRobert Watson 	} else {
2298df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2299df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2300623dce13SRobert Watson 		tcp_usrclosed(tp);
2301ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
230255bceb1eSRandall Stewart 			tp->t_fb->tfb_tcp_output(tp);
2303df8bae1dSRodney W. Grimes 	}
2304df8bae1dSRodney W. Grimes }
2305df8bae1dSRodney W. Grimes 
2306df8bae1dSRodney W. Grimes /*
2307df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2308df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2309df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2310df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2311df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2312df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2313df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2314df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2315df8bae1dSRodney W. Grimes  */
2316623dce13SRobert Watson static void
2317ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2318df8bae1dSRodney W. Grimes {
2319df8bae1dSRodney W. Grimes 
2320ff9b006dSJulien Charbon 	INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
23218501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
2322e6e0b5ffSRobert Watson 
2323df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2324df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
232509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
232609fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
232709fe6320SNavdeep Parhar #endif
2328550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2329bc65987aSKip Macy 		/* FALLTHROUGH */
2330bc65987aSKip Macy 	case TCPS_CLOSED:
2331df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2332623dce13SRobert Watson 		/*
2333623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2334623dce13SRobert Watson 		 * still open.
2335623dce13SRobert Watson 		 */
2336623dce13SRobert Watson 		KASSERT(tp != NULL,
2337623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2338df8bae1dSRodney W. Grimes 		break;
2339df8bae1dSRodney W. Grimes 
2340a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2341df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2342a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2343a0292f23SGarrett Wollman 		break;
2344a0292f23SGarrett Wollman 
2345df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
234657f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2347df8bae1dSRodney W. Grimes 		break;
2348df8bae1dSRodney W. Grimes 
2349df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
235057f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2351df8bae1dSRodney W. Grimes 		break;
2352df8bae1dSRodney W. Grimes 	}
2353abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
2354df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
2355abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
23567c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
23577c72af87SMohan Srinivasan 			int timeout;
23587c72af87SMohan Srinivasan 
23597c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
23609077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2361b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2362b6239c4aSAndras Olah 		}
2363df8bae1dSRodney W. Grimes 	}
23647c72af87SMohan Srinivasan }
2365497057eeSRobert Watson 
2366497057eeSRobert Watson #ifdef DDB
2367497057eeSRobert Watson static void
2368497057eeSRobert Watson db_print_indent(int indent)
2369497057eeSRobert Watson {
2370497057eeSRobert Watson 	int i;
2371497057eeSRobert Watson 
2372497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2373497057eeSRobert Watson 		db_printf(" ");
2374497057eeSRobert Watson }
2375497057eeSRobert Watson 
2376497057eeSRobert Watson static void
2377497057eeSRobert Watson db_print_tstate(int t_state)
2378497057eeSRobert Watson {
2379497057eeSRobert Watson 
2380497057eeSRobert Watson 	switch (t_state) {
2381497057eeSRobert Watson 	case TCPS_CLOSED:
2382497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2383497057eeSRobert Watson 		return;
2384497057eeSRobert Watson 
2385497057eeSRobert Watson 	case TCPS_LISTEN:
2386497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2387497057eeSRobert Watson 		return;
2388497057eeSRobert Watson 
2389497057eeSRobert Watson 	case TCPS_SYN_SENT:
2390497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2391497057eeSRobert Watson 		return;
2392497057eeSRobert Watson 
2393497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2394497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2395497057eeSRobert Watson 		return;
2396497057eeSRobert Watson 
2397497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2398497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2399497057eeSRobert Watson 		return;
2400497057eeSRobert Watson 
2401497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2402497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2403497057eeSRobert Watson 		return;
2404497057eeSRobert Watson 
2405497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2406497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2407497057eeSRobert Watson 		return;
2408497057eeSRobert Watson 
2409497057eeSRobert Watson 	case TCPS_CLOSING:
2410497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2411497057eeSRobert Watson 		return;
2412497057eeSRobert Watson 
2413497057eeSRobert Watson 	case TCPS_LAST_ACK:
2414497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2415497057eeSRobert Watson 		return;
2416497057eeSRobert Watson 
2417497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2418497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2419497057eeSRobert Watson 		return;
2420497057eeSRobert Watson 
2421497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2422497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2423497057eeSRobert Watson 		return;
2424497057eeSRobert Watson 
2425497057eeSRobert Watson 	default:
2426497057eeSRobert Watson 		db_printf("unknown");
2427497057eeSRobert Watson 		return;
2428497057eeSRobert Watson 	}
2429497057eeSRobert Watson }
2430497057eeSRobert Watson 
2431497057eeSRobert Watson static void
2432497057eeSRobert Watson db_print_tflags(u_int t_flags)
2433497057eeSRobert Watson {
2434497057eeSRobert Watson 	int comma;
2435497057eeSRobert Watson 
2436497057eeSRobert Watson 	comma = 0;
2437497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2438497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2439497057eeSRobert Watson 		comma = 1;
2440497057eeSRobert Watson 	}
2441497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2442497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2443497057eeSRobert Watson 		comma = 1;
2444497057eeSRobert Watson 	}
2445497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2446497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2447497057eeSRobert Watson 		comma = 1;
2448497057eeSRobert Watson 	}
2449497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2450497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2451497057eeSRobert Watson 		comma = 1;
2452497057eeSRobert Watson 	}
2453497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2454497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2455497057eeSRobert Watson 		comma = 1;
2456497057eeSRobert Watson 	}
2457497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2458497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2459497057eeSRobert Watson 		comma = 1;
2460497057eeSRobert Watson 	}
2461497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2462497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2463497057eeSRobert Watson 		comma = 1;
2464497057eeSRobert Watson 	}
2465497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2466497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2467497057eeSRobert Watson 		comma = 1;
2468497057eeSRobert Watson 	}
2469497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2470497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2471497057eeSRobert Watson 		comma = 1;
2472497057eeSRobert Watson 	}
2473497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2474497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2475497057eeSRobert Watson 		comma = 1;
2476497057eeSRobert Watson 	}
2477497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2478497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2479497057eeSRobert Watson 		comma = 1;
2480497057eeSRobert Watson 	}
2481497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2482497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2483497057eeSRobert Watson 		comma = 1;
2484497057eeSRobert Watson 	}
2485497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2486497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2487497057eeSRobert Watson 		comma = 1;
2488497057eeSRobert Watson 	}
2489497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2490497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2491497057eeSRobert Watson 		comma = 1;
2492497057eeSRobert Watson 	}
2493497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
2494497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2495497057eeSRobert Watson 		comma = 1;
2496497057eeSRobert Watson 	}
2497497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2498497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2499497057eeSRobert Watson 		comma = 1;
2500497057eeSRobert Watson 	}
2501497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2502497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2503497057eeSRobert Watson 		comma = 1;
2504497057eeSRobert Watson 	}
2505497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2506497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2507497057eeSRobert Watson 		comma = 1;
2508497057eeSRobert Watson 	}
2509dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
2510dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2511dbc42409SLawrence Stewart 		comma = 1;
2512dbc42409SLawrence Stewart 	}
2513497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2514497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2515497057eeSRobert Watson 		comma = 1;
2516497057eeSRobert Watson 	}
2517497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2518497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2519497057eeSRobert Watson 		comma = 1;
2520497057eeSRobert Watson 	}
2521497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2522497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2523497057eeSRobert Watson 		comma = 1;
2524497057eeSRobert Watson 	}
2525497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2526497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2527497057eeSRobert Watson 		comma = 1;
2528497057eeSRobert Watson 	}
2529f2512ba1SRui Paulo 	if (t_flags & TF_ECN_PERMIT) {
2530f2512ba1SRui Paulo 		db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2531f2512ba1SRui Paulo 		comma = 1;
2532f2512ba1SRui Paulo 	}
2533281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2534281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2535281a0fd4SPatrick Kelsey 		comma = 1;
2536281a0fd4SPatrick Kelsey 	}
2537497057eeSRobert Watson }
2538497057eeSRobert Watson 
2539497057eeSRobert Watson static void
2540497057eeSRobert Watson db_print_toobflags(char t_oobflags)
2541497057eeSRobert Watson {
2542497057eeSRobert Watson 	int comma;
2543497057eeSRobert Watson 
2544497057eeSRobert Watson 	comma = 0;
2545497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
2546497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2547497057eeSRobert Watson 		comma = 1;
2548497057eeSRobert Watson 	}
2549497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
2550497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2551497057eeSRobert Watson 		comma = 1;
2552497057eeSRobert Watson 	}
2553497057eeSRobert Watson }
2554497057eeSRobert Watson 
2555497057eeSRobert Watson static void
2556497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2557497057eeSRobert Watson {
2558497057eeSRobert Watson 
2559497057eeSRobert Watson 	db_print_indent(indent);
2560497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
2561497057eeSRobert Watson 
2562497057eeSRobert Watson 	indent += 2;
2563497057eeSRobert Watson 
2564497057eeSRobert Watson 	db_print_indent(indent);
2565497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
2566c28440dbSRandall Stewart 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2567497057eeSRobert Watson 
2568497057eeSRobert Watson 	db_print_indent(indent);
256985d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
2570e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2571497057eeSRobert Watson 
2572497057eeSRobert Watson 	db_print_indent(indent);
2573e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2574e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
2575497057eeSRobert Watson 
2576497057eeSRobert Watson 	db_print_indent(indent);
2577497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
2578497057eeSRobert Watson 	db_print_tstate(tp->t_state);
2579497057eeSRobert Watson 	db_printf(")\n");
2580497057eeSRobert Watson 
2581497057eeSRobert Watson 	db_print_indent(indent);
2582497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
2583497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
2584497057eeSRobert Watson 	db_printf(")\n");
2585497057eeSRobert Watson 
2586497057eeSRobert Watson 	db_print_indent(indent);
2587497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
2588497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
2589497057eeSRobert Watson 
2590497057eeSRobert Watson 	db_print_indent(indent);
2591497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
2592497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2593497057eeSRobert Watson 
2594497057eeSRobert Watson 	db_print_indent(indent);
2595497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
2596497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
2597497057eeSRobert Watson 
2598497057eeSRobert Watson 	db_print_indent(indent);
25993ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
2600497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2601497057eeSRobert Watson 
2602497057eeSRobert Watson 	db_print_indent(indent);
26033ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
26041c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
2605497057eeSRobert Watson 
2606497057eeSRobert Watson 	db_print_indent(indent);
26073ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
26081c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2609497057eeSRobert Watson 
2610497057eeSRobert Watson 	db_print_indent(indent);
26110c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
26120c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
2613497057eeSRobert Watson 
2614497057eeSRobert Watson 	db_print_indent(indent);
26151c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
26161c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
2617497057eeSRobert Watson 
2618497057eeSRobert Watson 	db_print_indent(indent);
26191c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
26201c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2621497057eeSRobert Watson 
2622497057eeSRobert Watson 	db_print_indent(indent);
2623497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
2624497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2625497057eeSRobert Watson 	    tp->t_rttbest);
2626497057eeSRobert Watson 
2627497057eeSRobert Watson 	db_print_indent(indent);
26283ac12506SJonathan T. Looney 	db_printf("t_rttupdated: %lu   max_sndwnd: %u   t_softerror: %d\n",
2629497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2630497057eeSRobert Watson 
2631497057eeSRobert Watson 	db_print_indent(indent);
2632497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2633497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
2634497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
2635497057eeSRobert Watson 
2636497057eeSRobert Watson 	db_print_indent(indent);
2637497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
2638497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2639497057eeSRobert Watson 
2640497057eeSRobert Watson 	db_print_indent(indent);
26419f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
26421a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
2643497057eeSRobert Watson 
2644497057eeSRobert Watson 	db_print_indent(indent);
2645497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
26463ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2647497057eeSRobert Watson 
2648497057eeSRobert Watson 	db_print_indent(indent);
26493ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
26509f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2651497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
2652497057eeSRobert Watson 
2653497057eeSRobert Watson 	db_print_indent(indent);
26543529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
26553529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2656497057eeSRobert Watson 
2657497057eeSRobert Watson 	db_print_indent(indent);
2658497057eeSRobert Watson 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
2659497057eeSRobert Watson 	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2660497057eeSRobert Watson 
2661497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
2662497057eeSRobert Watson 
2663497057eeSRobert Watson 	db_print_indent(indent);
2664497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
2665497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2666497057eeSRobert Watson }
2667497057eeSRobert Watson 
2668497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2669497057eeSRobert Watson {
2670497057eeSRobert Watson 	struct tcpcb *tp;
2671497057eeSRobert Watson 
2672497057eeSRobert Watson 	if (!have_addr) {
2673497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
2674497057eeSRobert Watson 		return;
2675497057eeSRobert Watson 	}
2676497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
2677497057eeSRobert Watson 
2678497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
2679497057eeSRobert Watson }
2680497057eeSRobert Watson #endif
2681