xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 8db239dc6b88b9f751311c5b9ac7992f0a9f639b)
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);
381fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
382fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
383b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
384b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
385fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
386fb59c426SYoshinobu Inoue 			goto out;
387fb59c426SYoshinobu Inoue 		}
388fb59c426SYoshinobu Inoue 	}
389b287c6c7SBjoern A. Zeeb #endif
390b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
391fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
392623dce13SRobert Watson out:
393623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3945d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3958501a69cSRobert Watson 	INP_WUNLOCK(inp);
396623dce13SRobert Watson 	return (error);
397fb59c426SYoshinobu Inoue }
398fb59c426SYoshinobu Inoue #endif /* INET6 */
399fb59c426SYoshinobu Inoue 
400b287c6c7SBjoern A. Zeeb #ifdef INET
4012c37256eSGarrett Wollman /*
4022c37256eSGarrett Wollman  * Prepare to accept connections.
4032c37256eSGarrett Wollman  */
4042c37256eSGarrett Wollman static int
405d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
4062c37256eSGarrett Wollman {
4072c37256eSGarrett Wollman 	int error = 0;
408f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
409623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4102c37256eSGarrett Wollman 
411623dce13SRobert Watson 	TCPDEBUG0;
412623dce13SRobert Watson 	inp = sotoinpcb(so);
413623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
4148501a69cSRobert Watson 	INP_WLOCK(inp);
415ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
416623dce13SRobert Watson 		error = EINVAL;
417623dce13SRobert Watson 		goto out;
418623dce13SRobert Watson 	}
419623dce13SRobert Watson 	tp = intotcpcb(inp);
420623dce13SRobert Watson 	TCPDEBUG1();
4210daccb9cSRobert Watson 	SOCK_LOCK(so);
4220daccb9cSRobert Watson 	error = solisten_proto_check(so);
423fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
4240daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0)
425b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
426fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4270daccb9cSRobert Watson 	if (error == 0) {
42857f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
429d374e81eSRobert Watson 		solisten_proto(so, backlog);
43009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
43137cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
43209fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
43309fe6320SNavdeep Parhar #endif
4340daccb9cSRobert Watson 	}
4350daccb9cSRobert Watson 	SOCK_UNLOCK(so);
436623dce13SRobert Watson 
43768bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
438281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
43918a75309SPatrick Kelsey 
440623dce13SRobert Watson out:
441623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4425d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4438501a69cSRobert Watson 	INP_WUNLOCK(inp);
444623dce13SRobert Watson 	return (error);
4452c37256eSGarrett Wollman }
446b287c6c7SBjoern A. Zeeb #endif /* INET */
4472c37256eSGarrett Wollman 
448fb59c426SYoshinobu Inoue #ifdef INET6
449fb59c426SYoshinobu Inoue static int
450d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
451fb59c426SYoshinobu Inoue {
452fb59c426SYoshinobu Inoue 	int error = 0;
453f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
454623dce13SRobert Watson 	struct tcpcb *tp = NULL;
455fb59c426SYoshinobu Inoue 
456623dce13SRobert Watson 	TCPDEBUG0;
457623dce13SRobert Watson 	inp = sotoinpcb(so);
458623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4598501a69cSRobert Watson 	INP_WLOCK(inp);
460ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
461623dce13SRobert Watson 		error = EINVAL;
462623dce13SRobert Watson 		goto out;
463623dce13SRobert Watson 	}
464623dce13SRobert Watson 	tp = intotcpcb(inp);
465623dce13SRobert Watson 	TCPDEBUG1();
4660daccb9cSRobert Watson 	SOCK_LOCK(so);
4670daccb9cSRobert Watson 	error = solisten_proto_check(so);
468fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
4690daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0) {
470fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
47166ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
472fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
473b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
474fb59c426SYoshinobu Inoue 	}
475fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4760daccb9cSRobert Watson 	if (error == 0) {
47757f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
478d374e81eSRobert Watson 		solisten_proto(so, backlog);
47909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
48037cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
48109fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
48209fe6320SNavdeep Parhar #endif
4830daccb9cSRobert Watson 	}
4840daccb9cSRobert Watson 	SOCK_UNLOCK(so);
485623dce13SRobert Watson 
48668bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
487281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
48818a75309SPatrick Kelsey 
489623dce13SRobert Watson out:
490623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4915d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4928501a69cSRobert Watson 	INP_WUNLOCK(inp);
493623dce13SRobert Watson 	return (error);
494fb59c426SYoshinobu Inoue }
495fb59c426SYoshinobu Inoue #endif /* INET6 */
496fb59c426SYoshinobu Inoue 
497b287c6c7SBjoern A. Zeeb #ifdef INET
4982c37256eSGarrett Wollman /*
4992c37256eSGarrett Wollman  * Initiate connection to peer.
5002c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
5012c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
5022c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
5032c37256eSGarrett Wollman  * Send initial segment on connection.
5042c37256eSGarrett Wollman  */
5052c37256eSGarrett Wollman static int
506b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
5072c37256eSGarrett Wollman {
5082c37256eSGarrett Wollman 	int error = 0;
509f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
510623dce13SRobert Watson 	struct tcpcb *tp = NULL;
5112c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
5122c37256eSGarrett Wollman 
51357bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
514e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
515e29ef13fSDon Lewis 		return (EINVAL);
51652710de1SPawel Jakub Dawidek 	/*
51752710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
51852710de1SPawel Jakub Dawidek 	 */
5192c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
52052710de1SPawel Jakub Dawidek 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
52152710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
522b89e82ddSJamie Gritton 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
523b89e82ddSJamie Gritton 		return (error);
52475c13541SPoul-Henning Kamp 
525623dce13SRobert Watson 	TCPDEBUG0;
526623dce13SRobert Watson 	inp = sotoinpcb(so);
527623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
5288501a69cSRobert Watson 	INP_WLOCK(inp);
529eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
530eb96dc33SJulien Charbon 		error = EADDRINUSE;
531eb96dc33SJulien Charbon 		goto out;
532eb96dc33SJulien Charbon 	}
533eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
534eb96dc33SJulien Charbon 		error = ECONNREFUSED;
535623dce13SRobert Watson 		goto out;
536623dce13SRobert Watson 	}
537623dce13SRobert Watson 	tp = intotcpcb(inp);
538623dce13SRobert Watson 	TCPDEBUG1();
539b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
5402c37256eSGarrett Wollman 		goto out;
54109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
54209fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
54337cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
54409fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
54509fe6320SNavdeep Parhar 		goto out;
54609fe6320SNavdeep Parhar #endif
54709fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
54855bceb1eSRandall Stewart 	error = tp->t_fb->tfb_tcp_output(tp);
549623dce13SRobert Watson out:
550623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
551e79cb051SGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
5528501a69cSRobert Watson 	INP_WUNLOCK(inp);
553623dce13SRobert Watson 	return (error);
5542c37256eSGarrett Wollman }
555b287c6c7SBjoern A. Zeeb #endif /* INET */
5562c37256eSGarrett Wollman 
557fb59c426SYoshinobu Inoue #ifdef INET6
558fb59c426SYoshinobu Inoue static int
559b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
560fb59c426SYoshinobu Inoue {
561fb59c426SYoshinobu Inoue 	int error = 0;
562f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
563623dce13SRobert Watson 	struct tcpcb *tp = NULL;
564fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
565623dce13SRobert Watson 
566623dce13SRobert Watson 	TCPDEBUG0;
567fb59c426SYoshinobu Inoue 
568fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
569e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sin6p))
570e29ef13fSDon Lewis 		return (EINVAL);
57152710de1SPawel Jakub Dawidek 	/*
57252710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
57352710de1SPawel Jakub Dawidek 	 */
574fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6
57552710de1SPawel Jakub Dawidek 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
57652710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
577fb59c426SYoshinobu Inoue 
578623dce13SRobert Watson 	inp = sotoinpcb(so);
579623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
5808501a69cSRobert Watson 	INP_WLOCK(inp);
581eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
582eb96dc33SJulien Charbon 		error = EADDRINUSE;
583eb96dc33SJulien Charbon 		goto out;
584eb96dc33SJulien Charbon 	}
585eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
586eb96dc33SJulien Charbon 		error = ECONNREFUSED;
587623dce13SRobert Watson 		goto out;
588623dce13SRobert Watson 	}
589623dce13SRobert Watson 	tp = intotcpcb(inp);
590623dce13SRobert Watson 	TCPDEBUG1();
591b287c6c7SBjoern A. Zeeb #ifdef INET
592fa046d87SRobert Watson 	/*
593fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
594fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
595fa046d87SRobert Watson 	 * Is this a significant problem?
596fa046d87SRobert Watson 	 */
59733841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
598fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
599fb59c426SYoshinobu Inoue 
600d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
601d46a5312SMaxim Konovalov 			error = EINVAL;
602d46a5312SMaxim Konovalov 			goto out;
603d46a5312SMaxim Konovalov 		}
6045dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV4) == 0) {
6055dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6065dba6adaSMichael Tuexen 			goto out;
6075dba6adaSMichael Tuexen 		}
60833841545SHajimu UMEMOTO 
609fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
610fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
611fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
612b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
613b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
614413628a7SBjoern A. Zeeb 			goto out;
615b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
616fb59c426SYoshinobu Inoue 			goto out;
61709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
61809fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
619adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
62009fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
62109fe6320SNavdeep Parhar 			goto out;
62209fe6320SNavdeep Parhar #endif
62355bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
624fb59c426SYoshinobu Inoue 		goto out;
6255dba6adaSMichael Tuexen 	} else {
6265dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV6) == 0) {
6275dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6285dba6adaSMichael Tuexen 			goto out;
6295dba6adaSMichael Tuexen 		}
630fb59c426SYoshinobu Inoue 	}
631b287c6c7SBjoern A. Zeeb #endif
632fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
633fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
634dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
635b89e82ddSJamie Gritton 	if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
636413628a7SBjoern A. Zeeb 		goto out;
637b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
638fb59c426SYoshinobu Inoue 		goto out;
63909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
64009fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
641adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
64209fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
64309fe6320SNavdeep Parhar 		goto out;
64409fe6320SNavdeep Parhar #endif
64509fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
64655bceb1eSRandall Stewart 	error = tp->t_fb->tfb_tcp_output(tp);
647623dce13SRobert Watson 
648623dce13SRobert Watson out:
649623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
6505d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
6518501a69cSRobert Watson 	INP_WUNLOCK(inp);
652623dce13SRobert Watson 	return (error);
653fb59c426SYoshinobu Inoue }
654fb59c426SYoshinobu Inoue #endif /* INET6 */
655fb59c426SYoshinobu Inoue 
6562c37256eSGarrett Wollman /*
6572c37256eSGarrett Wollman  * Initiate disconnect from peer.
6582c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
6592c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
6602c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
6612c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
6622c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
6632c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
6642c37256eSGarrett Wollman  *
6652c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
6662c37256eSGarrett Wollman  */
6672c37256eSGarrett Wollman static int
6682c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
6692c37256eSGarrett Wollman {
670f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
671623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6726573d758SMatt Macy 	struct epoch_tracker et;
673623dce13SRobert Watson 	int error = 0;
6742c37256eSGarrett Wollman 
675623dce13SRobert Watson 	TCPDEBUG0;
6766573d758SMatt Macy 	INP_INFO_RLOCK_ET(&V_tcbinfo, et);
677623dce13SRobert Watson 	inp = sotoinpcb(so);
678623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
6798501a69cSRobert Watson 	INP_WLOCK(inp);
680489dcc92SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT)
681489dcc92SJulien Charbon 		goto out;
682489dcc92SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
68321367f63SSam Leffler 		error = ECONNRESET;
684623dce13SRobert Watson 		goto out;
685623dce13SRobert Watson 	}
686623dce13SRobert Watson 	tp = intotcpcb(inp);
687623dce13SRobert Watson 	TCPDEBUG1();
688623dce13SRobert Watson 	tcp_disconnect(tp);
689623dce13SRobert Watson out:
690623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
6915d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
6928501a69cSRobert Watson 	INP_WUNLOCK(inp);
6936573d758SMatt Macy 	INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
694623dce13SRobert Watson 	return (error);
6952c37256eSGarrett Wollman }
6962c37256eSGarrett Wollman 
697b287c6c7SBjoern A. Zeeb #ifdef INET
6982c37256eSGarrett Wollman /*
6998296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
7008296cddfSRobert Watson  * just return the address of the peer, storing through addr.
7012c37256eSGarrett Wollman  */
7022c37256eSGarrett Wollman static int
70357bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
7042c37256eSGarrett Wollman {
7052c37256eSGarrett Wollman 	int error = 0;
706f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
7071db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
70826ef6ac4SDon Lewis 	struct in_addr addr;
70926ef6ac4SDon Lewis 	in_port_t port = 0;
7101db24ffbSJonathan Lemon 	TCPDEBUG0;
7112c37256eSGarrett Wollman 
7123d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
7133d2d3ef4SRobert Watson 		return (ECONNABORTED);
714f76fcf6dSJeffrey Hsu 
715f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
716623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
7178501a69cSRobert Watson 	INP_WLOCK(inp);
718ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
7193d2d3ef4SRobert Watson 		error = ECONNABORTED;
720623dce13SRobert Watson 		goto out;
721623dce13SRobert Watson 	}
7221db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
7231db24ffbSJonathan Lemon 	TCPDEBUG1();
724f76fcf6dSJeffrey Hsu 
725f76fcf6dSJeffrey Hsu 	/*
72654d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
72726ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
72826ef6ac4SDon Lewis 	 * release the lock.
729f76fcf6dSJeffrey Hsu 	 */
73026ef6ac4SDon Lewis 	port = inp->inp_fport;
73126ef6ac4SDon Lewis 	addr = inp->inp_faddr;
732f76fcf6dSJeffrey Hsu 
733623dce13SRobert Watson out:
734623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
7355d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7368501a69cSRobert Watson 	INP_WUNLOCK(inp);
73726ef6ac4SDon Lewis 	if (error == 0)
73826ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
73926ef6ac4SDon Lewis 	return error;
7402c37256eSGarrett Wollman }
741b287c6c7SBjoern A. Zeeb #endif /* INET */
7422c37256eSGarrett Wollman 
743fb59c426SYoshinobu Inoue #ifdef INET6
744fb59c426SYoshinobu Inoue static int
745fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
746fb59c426SYoshinobu Inoue {
747f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
748fb59c426SYoshinobu Inoue 	int error = 0;
7491db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
75026ef6ac4SDon Lewis 	struct in_addr addr;
75126ef6ac4SDon Lewis 	struct in6_addr addr6;
7526573d758SMatt Macy 	struct epoch_tracker et;
75326ef6ac4SDon Lewis 	in_port_t port = 0;
75426ef6ac4SDon Lewis 	int v4 = 0;
7551db24ffbSJonathan Lemon 	TCPDEBUG0;
756fb59c426SYoshinobu Inoue 
757b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
758b4470c16SRobert Watson 		return (ECONNABORTED);
759f76fcf6dSJeffrey Hsu 
760f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
761623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
7626573d758SMatt Macy 	INP_INFO_RLOCK_ET(&V_tcbinfo, et);
7638501a69cSRobert Watson 	INP_WLOCK(inp);
764ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
76521367f63SSam Leffler 		error = ECONNABORTED;
766623dce13SRobert Watson 		goto out;
767623dce13SRobert Watson 	}
7681db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
7691db24ffbSJonathan Lemon 	TCPDEBUG1();
770623dce13SRobert Watson 
77126ef6ac4SDon Lewis 	/*
77226ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
77326ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
77426ef6ac4SDon Lewis 	 * release the lock.
77526ef6ac4SDon Lewis 	 */
77626ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
77726ef6ac4SDon Lewis 		v4 = 1;
77826ef6ac4SDon Lewis 		port = inp->inp_fport;
77926ef6ac4SDon Lewis 		addr = inp->inp_faddr;
78026ef6ac4SDon Lewis 	} else {
78126ef6ac4SDon Lewis 		port = inp->inp_fport;
78226ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
78326ef6ac4SDon Lewis 	}
78426ef6ac4SDon Lewis 
785623dce13SRobert Watson out:
786623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
7875d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7888501a69cSRobert Watson 	INP_WUNLOCK(inp);
7896573d758SMatt Macy 	INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
79026ef6ac4SDon Lewis 	if (error == 0) {
79126ef6ac4SDon Lewis 		if (v4)
79226ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
79326ef6ac4SDon Lewis 		else
79426ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
79526ef6ac4SDon Lewis 	}
79626ef6ac4SDon Lewis 	return error;
797fb59c426SYoshinobu Inoue }
798fb59c426SYoshinobu Inoue #endif /* INET6 */
799f76fcf6dSJeffrey Hsu 
800f76fcf6dSJeffrey Hsu /*
8012c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
8022c37256eSGarrett Wollman  */
8032c37256eSGarrett Wollman static int
8042c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
8052c37256eSGarrett Wollman {
8062c37256eSGarrett Wollman 	int error = 0;
807f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
808623dce13SRobert Watson 	struct tcpcb *tp = NULL;
8096573d758SMatt Macy 	struct epoch_tracker et;
8102c37256eSGarrett Wollman 
811623dce13SRobert Watson 	TCPDEBUG0;
8126573d758SMatt Macy 	INP_INFO_RLOCK_ET(&V_tcbinfo, et);
813623dce13SRobert Watson 	inp = sotoinpcb(so);
814623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
8158501a69cSRobert Watson 	INP_WLOCK(inp);
816ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
81721367f63SSam Leffler 		error = ECONNRESET;
818623dce13SRobert Watson 		goto out;
819623dce13SRobert Watson 	}
820623dce13SRobert Watson 	tp = intotcpcb(inp);
821623dce13SRobert Watson 	TCPDEBUG1();
8222c37256eSGarrett Wollman 	socantsendmore(so);
823623dce13SRobert Watson 	tcp_usrclosed(tp);
824ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
82555bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
826623dce13SRobert Watson 
827623dce13SRobert Watson out:
828623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
8295d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
8308501a69cSRobert Watson 	INP_WUNLOCK(inp);
8316573d758SMatt Macy 	INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
832623dce13SRobert Watson 
833623dce13SRobert Watson 	return (error);
8342c37256eSGarrett Wollman }
8352c37256eSGarrett Wollman 
8362c37256eSGarrett Wollman /*
8372c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
8382c37256eSGarrett Wollman  */
8392c37256eSGarrett Wollman static int
8402c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
8412c37256eSGarrett Wollman {
842f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
843623dce13SRobert Watson 	struct tcpcb *tp = NULL;
844623dce13SRobert Watson 	int error = 0;
8452c37256eSGarrett Wollman 
846623dce13SRobert Watson 	TCPDEBUG0;
847623dce13SRobert Watson 	inp = sotoinpcb(so);
848623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
8498501a69cSRobert Watson 	INP_WLOCK(inp);
850ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
85121367f63SSam Leffler 		error = ECONNRESET;
852623dce13SRobert Watson 		goto out;
853623dce13SRobert Watson 	}
854623dce13SRobert Watson 	tp = intotcpcb(inp);
855623dce13SRobert Watson 	TCPDEBUG1();
856281a0fd4SPatrick Kelsey 	/*
857281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
858281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
859281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
860281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
861281a0fd4SPatrick Kelsey 	 * expires.
862281a0fd4SPatrick Kelsey 	 */
86368bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags) &&
864281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
865281a0fd4SPatrick Kelsey 		goto out;
86609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
86709fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
86809fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
869460cf046SNavdeep Parhar 	else
87009fe6320SNavdeep Parhar #endif
87155bceb1eSRandall Stewart 	tp->t_fb->tfb_tcp_output(tp);
872623dce13SRobert Watson 
873623dce13SRobert Watson out:
874623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
8755d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
8768501a69cSRobert Watson 	INP_WUNLOCK(inp);
877623dce13SRobert Watson 	return (error);
8782c37256eSGarrett Wollman }
8792c37256eSGarrett Wollman 
8802c37256eSGarrett Wollman /*
8812c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
8829c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
8839c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
8849c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
8859c9906e9SPeter Wemm  * generally are caller-frees.
8862c37256eSGarrett Wollman  */
8872c37256eSGarrett Wollman static int
88857bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
889b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
8902c37256eSGarrett Wollman {
8912c37256eSGarrett Wollman 	int error = 0;
892f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
893623dce13SRobert Watson 	struct tcpcb *tp = NULL;
8946573d758SMatt Macy 	struct epoch_tracker net_et;
895fb59c426SYoshinobu Inoue #ifdef INET6
896fb59c426SYoshinobu Inoue 	int isipv6;
897fb59c426SYoshinobu Inoue #endif
8989c9906e9SPeter Wemm 	TCPDEBUG0;
8992c37256eSGarrett Wollman 
900f76fcf6dSJeffrey Hsu 	/*
901fa046d87SRobert Watson 	 * We require the pcbinfo lock if we will close the socket as part of
902fa046d87SRobert Watson 	 * this call.
903f76fcf6dSJeffrey Hsu 	 */
904fa046d87SRobert Watson 	if (flags & PRUS_EOF)
9056573d758SMatt Macy 		INP_INFO_RLOCK_ET(&V_tcbinfo, net_et);
906f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
907623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
9088501a69cSRobert Watson 	INP_WLOCK(inp);
909ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
9107ff0b850SAndre Oppermann 		if (control)
9117ff0b850SAndre Oppermann 			m_freem(control);
9122cbcd3c1SGleb Smirnoff 		/*
9132cbcd3c1SGleb Smirnoff 		 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
9142cbcd3c1SGleb Smirnoff 		 * for freeing memory.
9152cbcd3c1SGleb Smirnoff 		 */
9162cbcd3c1SGleb Smirnoff 		if (m && (flags & PRUS_NOTREADY) == 0)
9177ff0b850SAndre Oppermann 			m_freem(m);
91821367f63SSam Leffler 		error = ECONNRESET;
9199c9906e9SPeter Wemm 		goto out;
9209c9906e9SPeter Wemm 	}
921fb59c426SYoshinobu Inoue #ifdef INET6
922fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
923fb59c426SYoshinobu Inoue #endif /* INET6 */
9249c9906e9SPeter Wemm 	tp = intotcpcb(inp);
9259c9906e9SPeter Wemm 	TCPDEBUG1();
9269c9906e9SPeter Wemm 	if (control) {
9279c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
9289c9906e9SPeter Wemm 		if (control->m_len) {
9299c9906e9SPeter Wemm 			m_freem(control);
9302c37256eSGarrett Wollman 			if (m)
9312c37256eSGarrett Wollman 				m_freem(m);
932744f87eaSDavid Greenman 			error = EINVAL;
933744f87eaSDavid Greenman 			goto out;
9342c37256eSGarrett Wollman 		}
9359c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
9369c9906e9SPeter Wemm 	}
9372c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
938651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
9392c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
9402c37256eSGarrett Wollman 			/*
9412c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
9422c37256eSGarrett Wollman 			 * initialize window to default value, and
9430c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
9442c37256eSGarrett Wollman 			 */
945fb59c426SYoshinobu Inoue #ifdef INET6
946fb59c426SYoshinobu Inoue 			if (isipv6)
947b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
948fb59c426SYoshinobu Inoue #endif /* INET6 */
949b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
950b287c6c7SBjoern A. Zeeb 			else
951b287c6c7SBjoern A. Zeeb #endif
952b287c6c7SBjoern A. Zeeb #ifdef INET
953b40ce416SJulian Elischer 				error = tcp_connect(tp, nam, td);
954b287c6c7SBjoern A. Zeeb #endif
9552c37256eSGarrett Wollman 			if (error)
9562c37256eSGarrett Wollman 				goto out;
957c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
958c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
95918a75309SPatrick Kelsey 			else {
9602c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
9612c37256eSGarrett Wollman 				tcp_mss(tp, -1);
9622c37256eSGarrett Wollman 			}
963c560df6fSPatrick Kelsey 		}
9642c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
9652c37256eSGarrett Wollman 			/*
9662c37256eSGarrett Wollman 			 * Close the send side of the connection after
9672c37256eSGarrett Wollman 			 * the data is sent.
9682c37256eSGarrett Wollman 			 */
969ff9b006dSJulien Charbon 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
9702c37256eSGarrett Wollman 			socantsendmore(so);
971623dce13SRobert Watson 			tcp_usrclosed(tp);
9722c37256eSGarrett Wollman 		}
9732cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
9742cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
975b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
976b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
97755bceb1eSRandall Stewart 			error = tp->t_fb->tfb_tcp_output(tp);
978b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
979b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
980b0acefa8SBill Fenner 		}
9812c37256eSGarrett Wollman 	} else {
982623dce13SRobert Watson 		/*
983623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
984623dce13SRobert Watson 		 */
985d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
9862c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
987d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
9882c37256eSGarrett Wollman 			m_freem(m);
9892c37256eSGarrett Wollman 			error = ENOBUFS;
9902c37256eSGarrett Wollman 			goto out;
9912c37256eSGarrett Wollman 		}
9922c37256eSGarrett Wollman 		/*
9932c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
9942c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
9952c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
9962c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
9972c37256eSGarrett Wollman 		 * of data past the urgent section.
9982c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
9992c37256eSGarrett Wollman 		 */
1000651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
1001d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
1002ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1003ef53690bSGarrett Wollman 			/*
1004ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
1005ef53690bSGarrett Wollman 			 * initialize window to default value, and
10060c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1007ef53690bSGarrett Wollman 			 */
100818a75309SPatrick Kelsey 
1009c560df6fSPatrick Kelsey 			/*
1010c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1011c560df6fSPatrick Kelsey 			 */
1012c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1013c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1014fb59c426SYoshinobu Inoue #ifdef INET6
1015fb59c426SYoshinobu Inoue 			if (isipv6)
1016b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1017fb59c426SYoshinobu Inoue #endif /* INET6 */
1018b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1019b287c6c7SBjoern A. Zeeb 			else
1020b287c6c7SBjoern A. Zeeb #endif
1021b287c6c7SBjoern A. Zeeb #ifdef INET
1022b40ce416SJulian Elischer 				error = tcp_connect(tp, nam, td);
1023b287c6c7SBjoern A. Zeeb #endif
1024ef53690bSGarrett Wollman 			if (error)
1025ef53690bSGarrett Wollman 				goto out;
1026ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1027ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
1028623dce13SRobert Watson 		}
1029300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
10302cbcd3c1SGleb Smirnoff 		if (!(flags & PRUS_NOTREADY)) {
10312cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
103255bceb1eSRandall Stewart 			error = tp->t_fb->tfb_tcp_output(tp);
10332cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
10342c37256eSGarrett Wollman 		}
10352cbcd3c1SGleb Smirnoff 	}
10362529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL,
10372529f56eSJonathan T. Looney 	    &inp->inp_socket->so_rcv,
10382529f56eSJonathan T. Looney 	    &inp->inp_socket->so_snd,
10392529f56eSJonathan T. Looney 	    TCP_LOG_USERSEND, error,
10402529f56eSJonathan T. Looney 	    0, NULL, false);
1041d1401c90SRobert Watson out:
1042d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
10432c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
10445d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
10455d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
10468501a69cSRobert Watson 	INP_WUNLOCK(inp);
1047fa046d87SRobert Watson 	if (flags & PRUS_EOF)
10486573d758SMatt Macy 		INP_INFO_RUNLOCK_ET(&V_tcbinfo, net_et);
104973fddedaSPeter Grehan 	return (error);
10502c37256eSGarrett Wollman }
10512c37256eSGarrett Wollman 
10522cbcd3c1SGleb Smirnoff static int
10532cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
10542cbcd3c1SGleb Smirnoff {
10552cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
10562cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
10572cbcd3c1SGleb Smirnoff 	int error;
10582cbcd3c1SGleb Smirnoff 
10592cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
10602cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
10612cbcd3c1SGleb Smirnoff 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
10622cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
10632cbcd3c1SGleb Smirnoff 		for (int i = 0; i < count; i++)
10642cbcd3c1SGleb Smirnoff 			m = m_free(m);
10652cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
10662cbcd3c1SGleb Smirnoff 	}
10672cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
10682cbcd3c1SGleb Smirnoff 
10692cbcd3c1SGleb Smirnoff 	SOCKBUF_LOCK(&so->so_snd);
10702cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
10712cbcd3c1SGleb Smirnoff 	SOCKBUF_UNLOCK(&so->so_snd);
10722cbcd3c1SGleb Smirnoff 	if (error == 0)
107355bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
10742cbcd3c1SGleb Smirnoff 	INP_WUNLOCK(inp);
10752cbcd3c1SGleb Smirnoff 
10762cbcd3c1SGleb Smirnoff 	return (error);
10772cbcd3c1SGleb Smirnoff }
10782cbcd3c1SGleb Smirnoff 
10792c37256eSGarrett Wollman /*
1080a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
10812c37256eSGarrett Wollman  */
1082ac45e92fSRobert Watson static void
10832c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
10842c37256eSGarrett Wollman {
1085f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1086a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
10876573d758SMatt Macy 	struct epoch_tracker et;
1088623dce13SRobert Watson 	TCPDEBUG0;
1089c78cbc7bSRobert Watson 
1090ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1091c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1092c78cbc7bSRobert Watson 
10936573d758SMatt Macy 	INP_INFO_RLOCK_ET(&V_tcbinfo, et);
10948501a69cSRobert Watson 	INP_WLOCK(inp);
1095c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1096c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1097c78cbc7bSRobert Watson 
1098c78cbc7bSRobert Watson 	/*
1099a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1100c78cbc7bSRobert Watson 	 */
1101ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1102ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1103c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
1104a152f8a3SRobert Watson 		TCPDEBUG1();
11058fa799bdSJonathan T. Looney 		tp = tcp_drop(tp, ECONNABORTED);
11068fa799bdSJonathan T. Looney 		if (tp == NULL)
11078fa799bdSJonathan T. Looney 			goto dropped;
1108a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
11095d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1110c78cbc7bSRobert Watson 	}
1111ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1112a152f8a3SRobert Watson 		SOCK_LOCK(so);
1113a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1114a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1115ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1116a152f8a3SRobert Watson 	}
11178501a69cSRobert Watson 	INP_WUNLOCK(inp);
11188fa799bdSJonathan T. Looney dropped:
11196573d758SMatt Macy 	INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
1120a152f8a3SRobert Watson }
1121a152f8a3SRobert Watson 
1122a152f8a3SRobert Watson /*
1123a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1124a152f8a3SRobert Watson  */
1125a152f8a3SRobert Watson static void
1126a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1127a152f8a3SRobert Watson {
1128a152f8a3SRobert Watson 	struct inpcb *inp;
1129a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
11306573d758SMatt Macy 	struct epoch_tracker et;
1131a152f8a3SRobert Watson 	TCPDEBUG0;
1132a152f8a3SRobert Watson 
1133a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1134a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1135a152f8a3SRobert Watson 
11366573d758SMatt Macy 	INP_INFO_RLOCK_ET(&V_tcbinfo, et);
11378501a69cSRobert Watson 	INP_WLOCK(inp);
1138a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1139a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1140a152f8a3SRobert Watson 
1141a152f8a3SRobert Watson 	/*
1142a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
1143a152f8a3SRobert Watson 	 * a disconnect.
1144a152f8a3SRobert Watson 	 */
1145ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1146ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1147a152f8a3SRobert Watson 		tp = intotcpcb(inp);
1148a152f8a3SRobert Watson 		TCPDEBUG1();
1149a152f8a3SRobert Watson 		tcp_disconnect(tp);
1150a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
11515d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1152a152f8a3SRobert Watson 	}
1153ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1154a152f8a3SRobert Watson 		SOCK_LOCK(so);
1155a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1156a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1157ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1158a152f8a3SRobert Watson 	}
11598501a69cSRobert Watson 	INP_WUNLOCK(inp);
11606573d758SMatt Macy 	INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
11612c37256eSGarrett Wollman }
11622c37256eSGarrett Wollman 
11632c37256eSGarrett Wollman /*
11642c37256eSGarrett Wollman  * Receive out-of-band data.
11652c37256eSGarrett Wollman  */
11662c37256eSGarrett Wollman static int
11672c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
11682c37256eSGarrett Wollman {
11692c37256eSGarrett Wollman 	int error = 0;
1170f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1171623dce13SRobert Watson 	struct tcpcb *tp = NULL;
11722c37256eSGarrett Wollman 
1173623dce13SRobert Watson 	TCPDEBUG0;
1174623dce13SRobert Watson 	inp = sotoinpcb(so);
1175623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
11768501a69cSRobert Watson 	INP_WLOCK(inp);
1177ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
117821367f63SSam Leffler 		error = ECONNRESET;
1179623dce13SRobert Watson 		goto out;
1180623dce13SRobert Watson 	}
1181623dce13SRobert Watson 	tp = intotcpcb(inp);
1182623dce13SRobert Watson 	TCPDEBUG1();
11832c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1184c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
11854cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
11864cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
11872c37256eSGarrett Wollman 		error = EINVAL;
11882c37256eSGarrett Wollman 		goto out;
11892c37256eSGarrett Wollman 	}
11902c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
11912c37256eSGarrett Wollman 		error = EWOULDBLOCK;
11922c37256eSGarrett Wollman 		goto out;
11932c37256eSGarrett Wollman 	}
11942c37256eSGarrett Wollman 	m->m_len = 1;
11952c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
11962c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
11972c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1198623dce13SRobert Watson 
1199623dce13SRobert Watson out:
1200623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
12015d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
12028501a69cSRobert Watson 	INP_WUNLOCK(inp);
1203623dce13SRobert Watson 	return (error);
12042c37256eSGarrett Wollman }
12052c37256eSGarrett Wollman 
1206b287c6c7SBjoern A. Zeeb #ifdef INET
12072c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1208756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1209756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1210756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1211756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1212756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1213756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1214756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1215756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1216756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
121754d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1218756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1219756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1220756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
12212cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1222756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
122354d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1224a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1225a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
12262c37256eSGarrett Wollman };
1227b287c6c7SBjoern A. Zeeb #endif /* INET */
1228df8bae1dSRodney W. Grimes 
1229fb59c426SYoshinobu Inoue #ifdef INET6
1230fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1231756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1232756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1233756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1234756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1235756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1236756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1237756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1238756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1239756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1240756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1241756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1242756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1243756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
12442cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1245756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1246756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1247a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1248a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1249fb59c426SYoshinobu Inoue };
1250fb59c426SYoshinobu Inoue #endif /* INET6 */
1251fb59c426SYoshinobu Inoue 
1252b287c6c7SBjoern A. Zeeb #ifdef INET
1253a0292f23SGarrett Wollman /*
1254a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1255a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
12565200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
12575200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
12585200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
12595200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1260a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1261a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1262a0292f23SGarrett Wollman  */
12630312fbe9SPoul-Henning Kamp static int
1264ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1265a0292f23SGarrett Wollman {
1266a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1267a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
12685200e00eSIan Dowse 	struct in_addr laddr;
12695200e00eSIan Dowse 	u_short lport;
1270c3229e05SDavid Greenman 	int error;
1271a0292f23SGarrett Wollman 
12728501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1273fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1274623dce13SRobert Watson 
1275a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
12764616026fSErmal Luçi 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
12774616026fSErmal Luçi 		if (error)
1278fa046d87SRobert Watson 			goto out;
1279a0292f23SGarrett Wollman 	}
1280a0292f23SGarrett Wollman 
1281a0292f23SGarrett Wollman 	/*
1282a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1283a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1284a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1285a0292f23SGarrett Wollman 	 */
12865200e00eSIan Dowse 	laddr = inp->inp_laddr;
12875200e00eSIan Dowse 	lport = inp->inp_lport;
12885200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1289b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
12905200e00eSIan Dowse 	if (error && oinp == NULL)
1291fa046d87SRobert Watson 		goto out;
1292fa046d87SRobert Watson 	if (oinp) {
1293fa046d87SRobert Watson 		error = EADDRINUSE;
1294fa046d87SRobert Watson 		goto out;
1295fa046d87SRobert Watson 	}
12965200e00eSIan Dowse 	inp->inp_laddr = laddr;
129715bd2b43SDavid Greenman 	in_pcbrehash(inp);
1298fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1299a0292f23SGarrett Wollman 
1300087b55eaSAndre Oppermann 	/*
1301087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1302087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1303087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1304087b55eaSAndre Oppermann 	 */
1305a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
13069b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1307a0292f23SGarrett Wollman 		tp->request_r_scale++;
1308a0292f23SGarrett Wollman 
1309a0292f23SGarrett Wollman 	soisconnecting(so);
131078b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
131157f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
1312b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
1313a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1314a45d2726SAndras Olah 
1315a0292f23SGarrett Wollman 	return 0;
1316fa046d87SRobert Watson 
1317fa046d87SRobert Watson out:
1318fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1319fa046d87SRobert Watson 	return (error);
1320a0292f23SGarrett Wollman }
1321b287c6c7SBjoern A. Zeeb #endif /* INET */
1322a0292f23SGarrett Wollman 
1323fb59c426SYoshinobu Inoue #ifdef INET6
1324fb59c426SYoshinobu Inoue static int
1325ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1326fb59c426SYoshinobu Inoue {
1327a7e201bbSAndrey V. Elsukov 	struct inpcb *inp = tp->t_inpcb;
1328fb59c426SYoshinobu Inoue 	int error;
1329fb59c426SYoshinobu Inoue 
13308501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1331fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1332623dce13SRobert Watson 
1333fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
13344616026fSErmal Luçi 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
13354616026fSErmal Luçi 		if (error)
1336fa046d87SRobert Watson 			goto out;
1337fb59c426SYoshinobu Inoue 	}
1338a7e201bbSAndrey V. Elsukov 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1339a7e201bbSAndrey V. Elsukov 	if (error != 0)
1340b598155aSRobert Watson 		goto out;
1341fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1342fb59c426SYoshinobu Inoue 
1343fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1344fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1345970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1346fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1347fb59c426SYoshinobu Inoue 
1348a7e201bbSAndrey V. Elsukov 	soisconnecting(inp->inp_socket);
134978b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
135057f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
1351b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
1352fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1353fb59c426SYoshinobu Inoue 
1354fb59c426SYoshinobu Inoue 	return 0;
1355fa046d87SRobert Watson 
1356fa046d87SRobert Watson out:
1357fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1358fa046d87SRobert Watson 	return error;
1359fb59c426SYoshinobu Inoue }
1360fb59c426SYoshinobu Inoue #endif /* INET6 */
1361fb59c426SYoshinobu Inoue 
1362cfe8b629SGarrett Wollman /*
1363b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1364b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1365b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1366b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1367b8af5dfaSRobert Watson  * from Linux.
1368b8af5dfaSRobert Watson  */
1369b8af5dfaSRobert Watson static void
1370ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1371b8af5dfaSRobert Watson {
1372b8af5dfaSRobert Watson 
13738501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1374b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1375b8af5dfaSRobert Watson 
1376b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1377b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1378b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
13793529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1380b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1381b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1382b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1383b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1384b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1385b8af5dfaSRobert Watson 	}
13865a17b6adSMichael Tuexen 	if (tp->t_flags & TF_ECN_PERMIT)
13875a17b6adSMichael Tuexen 		ti->tcpi_options |= TCPI_OPT_ECN;
13881baaf834SBruce M Simpson 
138943d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
13903ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
13911baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
13921baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
13931baaf834SBruce M Simpson 
1394b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1395b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1396b8af5dfaSRobert Watson 
1397b8af5dfaSRobert Watson 	/*
1398b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1399b8af5dfaSRobert Watson 	 */
1400c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1401535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1402b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
14031c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1404535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
140543d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
140643d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1407f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1408f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1409f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1410a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1411a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1412a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1413a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1414a6456410SNavdeep Parhar 	}
1415a6456410SNavdeep Parhar #endif
1416b8af5dfaSRobert Watson }
1417b8af5dfaSRobert Watson 
1418b8af5dfaSRobert Watson /*
14191e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
14201e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
14211e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
14221e8f5ffaSRobert Watson  * option.
1423cfe8b629SGarrett Wollman  */
1424bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
14258501a69cSRobert Watson 	INP_WLOCK(inp);							\
1426ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
14278501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1428bac5bedfSConrad Meyer 		cleanup;						\
14291e8f5ffaSRobert Watson 		return (ECONNRESET);					\
14301e8f5ffaSRobert Watson 	}								\
14311e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
14321e8f5ffaSRobert Watson } while(0)
1433bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
14341e8f5ffaSRobert Watson 
1435df8bae1dSRodney W. Grimes int
1436ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1437df8bae1dSRodney W. Grimes {
143855bceb1eSRandall Stewart 	int	error;
1439df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1440cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
144155bceb1eSRandall Stewart 	struct tcp_function_block *blk;
144255bceb1eSRandall Stewart 	struct tcp_function_set fsn;
1443df8bae1dSRodney W. Grimes 
1444cfe8b629SGarrett Wollman 	error = 0;
1445df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1446623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
14478501a69cSRobert Watson 	INP_WLOCK(inp);
1448cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1449fb59c426SYoshinobu Inoue #ifdef INET6
14505cd54324SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6PROTO) {
14518501a69cSRobert Watson 			INP_WUNLOCK(inp);
1452fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
1453b287c6c7SBjoern A. Zeeb 		}
1454fb59c426SYoshinobu Inoue #endif /* INET6 */
1455b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1456b287c6c7SBjoern A. Zeeb 		else
1457b287c6c7SBjoern A. Zeeb #endif
1458b287c6c7SBjoern A. Zeeb #ifdef INET
1459b287c6c7SBjoern A. Zeeb 		{
14608501a69cSRobert Watson 			INP_WUNLOCK(inp);
1461cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
14621e8f5ffaSRobert Watson 		}
14631e8f5ffaSRobert Watson #endif
1464df8bae1dSRodney W. Grimes 		return (error);
1465df8bae1dSRodney W. Grimes 	}
1466ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
14678501a69cSRobert Watson 		INP_WUNLOCK(inp);
14681e8f5ffaSRobert Watson 		return (ECONNRESET);
1469623dce13SRobert Watson 	}
147055bceb1eSRandall Stewart 	tp = intotcpcb(inp);
147155bceb1eSRandall Stewart 	/*
147255bceb1eSRandall Stewart 	 * Protect the TCP option TCP_FUNCTION_BLK so
147355bceb1eSRandall Stewart 	 * that a sub-function can *never* overwrite this.
147455bceb1eSRandall Stewart 	 */
147555bceb1eSRandall Stewart 	if ((sopt->sopt_dir == SOPT_SET) &&
147655bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
147755bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
147855bceb1eSRandall Stewart 		error = sooptcopyin(sopt, &fsn, sizeof fsn,
147955bceb1eSRandall Stewart 		    sizeof fsn);
148055bceb1eSRandall Stewart 		if (error)
148155bceb1eSRandall Stewart 			return (error);
148255bceb1eSRandall Stewart 		INP_WLOCK_RECHECK(inp);
148355bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
148455bceb1eSRandall Stewart 		if (blk == NULL) {
148555bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
148655bceb1eSRandall Stewart 			return (ENOENT);
148755bceb1eSRandall Stewart 		}
1488587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1489587d67c0SRandall Stewart 			/* You already have this */
1490587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1491587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1492587d67c0SRandall Stewart 			return (0);
1493587d67c0SRandall Stewart 		}
1494587d67c0SRandall Stewart 		if (tp->t_state != TCPS_CLOSED) {
1495587d67c0SRandall Stewart 			int error=EINVAL;
1496587d67c0SRandall Stewart 			/*
1497587d67c0SRandall Stewart 			 * The user has advanced the state
1498587d67c0SRandall Stewart 			 * past the initial point, we may not
1499587d67c0SRandall Stewart 			 * be able to switch.
1500587d67c0SRandall Stewart 			 */
1501587d67c0SRandall Stewart 			if (blk->tfb_tcp_handoff_ok != NULL) {
1502587d67c0SRandall Stewart 				/*
1503587d67c0SRandall Stewart 				 * Does the stack provide a
1504587d67c0SRandall Stewart 				 * query mechanism, if so it may
1505587d67c0SRandall Stewart 				 * still be possible?
1506587d67c0SRandall Stewart 				 */
1507587d67c0SRandall Stewart 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1508587d67c0SRandall Stewart 			}
1509587d67c0SRandall Stewart 			if (error) {
1510587d67c0SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1511587d67c0SRandall Stewart 				INP_WUNLOCK(inp);
1512587d67c0SRandall Stewart 				return(error);
1513587d67c0SRandall Stewart 			}
1514587d67c0SRandall Stewart 		}
151555bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
151655bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
151755bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
151855bceb1eSRandall Stewart 			return (ENOENT);
151955bceb1eSRandall Stewart 		}
152055bceb1eSRandall Stewart 		/*
152155bceb1eSRandall Stewart 		 * Release the old refcnt, the
1522587d67c0SRandall Stewart 		 * lookup acquired a ref on the
1523587d67c0SRandall Stewart 		 * new one already.
152455bceb1eSRandall Stewart 		 */
1525587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1526587d67c0SRandall Stewart 			/*
1527587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1528587d67c0SRandall Stewart 			 * the tcb is not going away.
1529587d67c0SRandall Stewart 			 */
1530587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1531587d67c0SRandall Stewart 		}
15323ee9c3c4SRandall Stewart #ifdef TCPHPTS
15333ee9c3c4SRandall Stewart 		/* Assure that we are not on any hpts */
15343ee9c3c4SRandall Stewart 		tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
15353ee9c3c4SRandall Stewart #endif
15363ee9c3c4SRandall Stewart 		if (blk->tfb_tcp_fb_init) {
15373ee9c3c4SRandall Stewart 			error = (*blk->tfb_tcp_fb_init)(tp);
15383ee9c3c4SRandall Stewart 			if (error) {
15393ee9c3c4SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
15403ee9c3c4SRandall Stewart 				if (tp->t_fb->tfb_tcp_fb_init) {
15413ee9c3c4SRandall Stewart 					if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0)  {
15423ee9c3c4SRandall Stewart 						/* Fall back failed, drop the connection */
15433ee9c3c4SRandall Stewart 						INP_WUNLOCK(inp);
15443ee9c3c4SRandall Stewart 						soabort(so);
15453ee9c3c4SRandall Stewart 						return(error);
15463ee9c3c4SRandall Stewart 					}
15473ee9c3c4SRandall Stewart 				}
15483ee9c3c4SRandall Stewart 				goto err_out;
15493ee9c3c4SRandall Stewart 			}
15503ee9c3c4SRandall Stewart 		}
155155bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
155255bceb1eSRandall Stewart 		tp->t_fb = blk;
155355bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
155455bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
155555bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
155655bceb1eSRandall Stewart 			     sopt->sopt_name);
155755bceb1eSRandall Stewart 		}
155855bceb1eSRandall Stewart #endif
15593ee9c3c4SRandall Stewart err_out:
156055bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
156155bceb1eSRandall Stewart 		return (error);
156255bceb1eSRandall Stewart 	} else if ((sopt->sopt_dir == SOPT_GET) &&
156355bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1564c73b6f4dSEd Maste 		strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1565c73b6f4dSEd Maste 		    TCP_FUNCTION_NAME_LEN_MAX);
1566c73b6f4dSEd Maste 		fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
156755bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
156855bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
156955bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
157055bceb1eSRandall Stewart 		return (error);
157155bceb1eSRandall Stewart 	}
157255bceb1eSRandall Stewart 	/* Pass in the INP locked, called must unlock it */
157355bceb1eSRandall Stewart 	return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
157455bceb1eSRandall Stewart }
157555bceb1eSRandall Stewart 
15762529f56eSJonathan T. Looney /*
15772529f56eSJonathan T. Looney  * If this assert becomes untrue, we need to change the size of the buf
15782529f56eSJonathan T. Looney  * variable in tcp_default_ctloutput().
15792529f56eSJonathan T. Looney  */
15802529f56eSJonathan T. Looney #ifdef CTASSERT
15812529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
15822529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
15832529f56eSJonathan T. Looney #endif
15842529f56eSJonathan T. Looney 
158555bceb1eSRandall Stewart int
158655bceb1eSRandall Stewart tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
158755bceb1eSRandall Stewart {
158855bceb1eSRandall Stewart 	int	error, opt, optval;
158955bceb1eSRandall Stewart 	u_int	ui;
159055bceb1eSRandall Stewart 	struct	tcp_info ti;
159155bceb1eSRandall Stewart 	struct cc_algo *algo;
15922529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
1593af6fef3aSGleb Smirnoff 	size_t	len;
1594df8bae1dSRodney W. Grimes 
1595d519cedbSGleb Smirnoff 	/*
1596d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
1597d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
1598d519cedbSGleb Smirnoff 	 */
1599d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
1600d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
1601d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1602af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1603af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1604d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
1605d519cedbSGleb Smirnoff 		if (error) {
1606af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
1607d519cedbSGleb Smirnoff 			return (error);
1608d519cedbSGleb Smirnoff 		}
1609bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1610d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
1611af6fef3aSGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1612d519cedbSGleb Smirnoff 		else
1613d519cedbSGleb Smirnoff 			error = ENOENT;
1614d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1615d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
1616af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1617af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
1618d519cedbSGleb Smirnoff 		return (error);
1619d519cedbSGleb Smirnoff 	}
1620d519cedbSGleb Smirnoff 
1621cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1622cfe8b629SGarrett Wollman 	case SOPT_SET:
1623cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1624fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
162588f6b043SBruce M Simpson 		case TCP_MD5SIG:
1626fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
16278501a69cSRobert Watson 				INP_WUNLOCK(inp);
1628fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
1629fcf59617SAndrey V. Elsukov 			}
1630fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
16311cfd4b53SBruce M Simpson 			if (error)
16321e8f5ffaSRobert Watson 				return (error);
163309fe6320SNavdeep Parhar 			goto unlock_and_done;
1634fcf59617SAndrey V. Elsukov #endif /* IPSEC */
163509fe6320SNavdeep Parhar 
1636df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1637cfe8b629SGarrett Wollman 		case TCP_NOOPT:
16388501a69cSRobert Watson 			INP_WUNLOCK(inp);
1639cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1640cfe8b629SGarrett Wollman 			    sizeof optval);
1641cfe8b629SGarrett Wollman 			if (error)
16421e8f5ffaSRobert Watson 				return (error);
1643cfe8b629SGarrett Wollman 
16448501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1645cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1646cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1647cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1648cfe8b629SGarrett Wollman 				break;
1649cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1650cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1651cfe8b629SGarrett Wollman 				break;
1652cfe8b629SGarrett Wollman 			default:
1653cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1654cfe8b629SGarrett Wollman 				break;
1655cfe8b629SGarrett Wollman 			}
1656cfe8b629SGarrett Wollman 
1657cfe8b629SGarrett Wollman 			if (optval)
1658cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1659df8bae1dSRodney W. Grimes 			else
1660cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
166109fe6320SNavdeep Parhar unlock_and_done:
166209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
166309fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
166409fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
166509fe6320SNavdeep Parhar 				    sopt->sopt_name);
166609fe6320SNavdeep Parhar 			}
166709fe6320SNavdeep Parhar #endif
16688501a69cSRobert Watson 			INP_WUNLOCK(inp);
1669df8bae1dSRodney W. Grimes 			break;
1670df8bae1dSRodney W. Grimes 
1671007581c0SJonathan Lemon 		case TCP_NOPUSH:
16728501a69cSRobert Watson 			INP_WUNLOCK(inp);
1673007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1674007581c0SJonathan Lemon 			    sizeof optval);
1675007581c0SJonathan Lemon 			if (error)
16761e8f5ffaSRobert Watson 				return (error);
1677007581c0SJonathan Lemon 
16788501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1679007581c0SJonathan Lemon 			if (optval)
1680007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1681d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
1682007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1683d28b9e89SJohn Baldwin 				if (TCPS_HAVEESTABLISHED(tp->t_state))
168455bceb1eSRandall Stewart 					error = tp->t_fb->tfb_tcp_output(tp);
1685007581c0SJonathan Lemon 			}
168609fe6320SNavdeep Parhar 			goto unlock_and_done;
1687007581c0SJonathan Lemon 
1688df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
16898501a69cSRobert Watson 			INP_WUNLOCK(inp);
1690cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1691cfe8b629SGarrett Wollman 			    sizeof optval);
1692cfe8b629SGarrett Wollman 			if (error)
16931e8f5ffaSRobert Watson 				return (error);
1694df8bae1dSRodney W. Grimes 
16958501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
169653369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
1697603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
1698cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1699a0292f23SGarrett Wollman 			else
1700a0292f23SGarrett Wollman 				error = EINVAL;
170109fe6320SNavdeep Parhar 			goto unlock_and_done;
1702a0292f23SGarrett Wollman 
1703b8af5dfaSRobert Watson 		case TCP_INFO:
17048501a69cSRobert Watson 			INP_WUNLOCK(inp);
1705b8af5dfaSRobert Watson 			error = EINVAL;
1706b8af5dfaSRobert Watson 			break;
1707b8af5dfaSRobert Watson 
1708dbc42409SLawrence Stewart 		case TCP_CONGESTION:
1709dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
1710af6fef3aSGleb Smirnoff 			error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1711af6fef3aSGleb Smirnoff 			if (error)
1712dbc42409SLawrence Stewart 				break;
1713af6fef3aSGleb Smirnoff 			buf[sopt->sopt_valsize] = '\0';
1714af6fef3aSGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
171573e263b1SGleb Smirnoff 			CC_LIST_RLOCK();
171673e263b1SGleb Smirnoff 			STAILQ_FOREACH(algo, &cc_list, entries)
171773e263b1SGleb Smirnoff 				if (strncmp(buf, algo->name,
171873e263b1SGleb Smirnoff 				    TCP_CA_NAME_MAX) == 0)
171973e263b1SGleb Smirnoff 					break;
172073e263b1SGleb Smirnoff 			CC_LIST_RUNLOCK();
172173e263b1SGleb Smirnoff 			if (algo == NULL) {
1722af6fef3aSGleb Smirnoff 				INP_WUNLOCK(inp);
172373e263b1SGleb Smirnoff 				error = EINVAL;
172473e263b1SGleb Smirnoff 				break;
172573e263b1SGleb Smirnoff 			}
1726dbc42409SLawrence Stewart 			/*
172773e263b1SGleb Smirnoff 			 * We hold a write lock over the tcb so it's safe to
172873e263b1SGleb Smirnoff 			 * do these things without ordering concerns.
1729dbc42409SLawrence Stewart 			 */
1730dbc42409SLawrence Stewart 			if (CC_ALGO(tp)->cb_destroy != NULL)
1731dbc42409SLawrence Stewart 				CC_ALGO(tp)->cb_destroy(tp->ccv);
173222699887SMatt Macy 			CC_DATA(tp) = NULL;
1733dbc42409SLawrence Stewart 			CC_ALGO(tp) = algo;
1734dbc42409SLawrence Stewart 			/*
173573e263b1SGleb Smirnoff 			 * If something goes pear shaped initialising the new
173673e263b1SGleb Smirnoff 			 * algo, fall back to newreno (which does not
173773e263b1SGleb Smirnoff 			 * require initialisation).
1738dbc42409SLawrence Stewart 			 */
173973e263b1SGleb Smirnoff 			if (algo->cb_init != NULL &&
174073e263b1SGleb Smirnoff 			    algo->cb_init(tp->ccv) != 0) {
1741dbc42409SLawrence Stewart 				CC_ALGO(tp) = &newreno_cc_algo;
1742dbc42409SLawrence Stewart 				/*
174373e263b1SGleb Smirnoff 				 * The only reason init should fail is
1744dbc42409SLawrence Stewart 				 * because of malloc.
1745dbc42409SLawrence Stewart 				 */
1746dbc42409SLawrence Stewart 				error = ENOMEM;
1747dbc42409SLawrence Stewart 			}
174873e263b1SGleb Smirnoff 			INP_WUNLOCK(inp);
174973e263b1SGleb Smirnoff 			break;
1750dbc42409SLawrence Stewart 
17519077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
17529077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
17539077f387SGleb Smirnoff 		case TCP_KEEPINIT:
17549077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
17559077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
17569077f387SGleb Smirnoff 			if (error)
17579077f387SGleb Smirnoff 				return (error);
17589077f387SGleb Smirnoff 
17599077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
17609077f387SGleb Smirnoff 				error = EINVAL;
17619077f387SGleb Smirnoff 				break;
17629077f387SGleb Smirnoff 			}
17639077f387SGleb Smirnoff 			ui *= hz;
17649077f387SGleb Smirnoff 
17659077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
17669077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
17679077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
17689077f387SGleb Smirnoff 				tp->t_keepidle = ui;
17699077f387SGleb Smirnoff 				/*
17709077f387SGleb Smirnoff 				 * XXX: better check current remaining
17719077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
17729077f387SGleb Smirnoff 				 */
17739077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
17749077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
17759077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
17769077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
17779077f387SGleb Smirnoff 				break;
17789077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
17799077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
17809077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
17819077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
17829077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
17839077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
17849077f387SGleb Smirnoff 				break;
17859077f387SGleb Smirnoff 			case TCP_KEEPINIT:
17869077f387SGleb Smirnoff 				tp->t_keepinit = ui;
17879077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
17889077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
17899077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
17909077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
17919077f387SGleb Smirnoff 				break;
17929077f387SGleb Smirnoff 			}
179309fe6320SNavdeep Parhar 			goto unlock_and_done;
17949077f387SGleb Smirnoff 
179585c05144SGleb Smirnoff 		case TCP_KEEPCNT:
179685c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
179785c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
179885c05144SGleb Smirnoff 			if (error)
179985c05144SGleb Smirnoff 				return (error);
180085c05144SGleb Smirnoff 
180185c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
180285c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
180385c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
180485c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
180585c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
180685c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
180785c05144SGleb Smirnoff 			goto unlock_and_done;
180885c05144SGleb Smirnoff 
180986a996e6SHiren Panchasara #ifdef TCPPCAP
181086a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
181186a996e6SHiren Panchasara 		case TCP_PCAP_IN:
181286a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
181386a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
181486a996e6SHiren Panchasara 			    sizeof optval);
181586a996e6SHiren Panchasara 			if (error)
181686a996e6SHiren Panchasara 				return (error);
181786a996e6SHiren Panchasara 
181886a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
181986a996e6SHiren Panchasara 			if (optval >= 0)
182086a996e6SHiren Panchasara 				tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
182186a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
182286a996e6SHiren Panchasara 					optval);
182386a996e6SHiren Panchasara 			else
182486a996e6SHiren Panchasara 				error = EINVAL;
182586a996e6SHiren Panchasara 			goto unlock_and_done;
182686a996e6SHiren Panchasara #endif
182786a996e6SHiren Panchasara 
1828c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
1829c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
1830c560df6fSPatrick Kelsey 
1831281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
1832c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
1833c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
1834281a0fd4SPatrick Kelsey 				return (EPERM);
1835281a0fd4SPatrick Kelsey 
1836c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
1837c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
1838281a0fd4SPatrick Kelsey 			if (error)
1839281a0fd4SPatrick Kelsey 				return (error);
1840281a0fd4SPatrick Kelsey 
1841281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
1842c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
1843c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
1844c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
1845c560df6fSPatrick Kelsey 						error = EPERM;
1846c560df6fSPatrick Kelsey 						goto unlock_and_done;
1847c560df6fSPatrick Kelsey 					}
1848c560df6fSPatrick Kelsey 
1849281a0fd4SPatrick Kelsey 					tp->t_flags |= TF_FASTOPEN;
1850c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
1851281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
1852281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
1853c560df6fSPatrick Kelsey 				} else {
1854c560df6fSPatrick Kelsey 					/*
1855c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
1856c560df6fSPatrick Kelsey 					 * stash it in the client cookie
1857c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
1858c560df6fSPatrick Kelsey 					 * connect.
1859c560df6fSPatrick Kelsey 					 */
1860c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
1861c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
1862c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
1863c560df6fSPatrick Kelsey 						       tfo_optval.psk,
1864c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
1865c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
1866c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
1867c560df6fSPatrick Kelsey 					}
1868c560df6fSPatrick Kelsey 					tp->t_flags |= TF_FASTOPEN;
1869c560df6fSPatrick Kelsey 				}
1870281a0fd4SPatrick Kelsey 			} else
1871281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1872281a0fd4SPatrick Kelsey 			goto unlock_and_done;
1873c560df6fSPatrick Kelsey 		}
1874281a0fd4SPatrick Kelsey 
1875e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
18762529f56eSJonathan T. Looney 		case TCP_LOG:
18772529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
18782529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
18792529f56eSJonathan T. Looney 			    sizeof optval);
18802529f56eSJonathan T. Looney 			if (error)
18812529f56eSJonathan T. Looney 				return (error);
18822529f56eSJonathan T. Looney 
18832529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
18842529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
18852529f56eSJonathan T. Looney 			goto unlock_and_done;
18862529f56eSJonathan T. Looney 
18872529f56eSJonathan T. Looney 		case TCP_LOGBUF:
18882529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
18892529f56eSJonathan T. Looney 			error = EINVAL;
18902529f56eSJonathan T. Looney 			break;
18912529f56eSJonathan T. Looney 
18922529f56eSJonathan T. Looney 		case TCP_LOGID:
18932529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
18942529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
18952529f56eSJonathan T. Looney 			if (error)
18962529f56eSJonathan T. Looney 				break;
18972529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
18982529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
18992529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
19002529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
19012529f56eSJonathan T. Looney 			break;
19022529f56eSJonathan T. Looney 
19032529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
19042529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
19052529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
19062529f56eSJonathan T. Looney 			error =
19072529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
19082529f56eSJonathan T. Looney 			if (error)
19092529f56eSJonathan T. Looney 				break;
19102529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
19112529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
19122529f56eSJonathan T. Looney 			if (sopt->sopt_name == TCP_LOGDUMP) {
19132529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
19142529f56eSJonathan T. Looney 				    M_WAITOK, true);
19152529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
19162529f56eSJonathan T. Looney 			} else {
19172529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
19182529f56eSJonathan T. Looney 				/*
19192529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
19202529f56eSJonathan T. Looney 				 * INP lock.
19212529f56eSJonathan T. Looney 				 */
19222529f56eSJonathan T. Looney 			}
19232529f56eSJonathan T. Looney 			break;
1924e24e5683SJonathan T. Looney #endif
19252529f56eSJonathan T. Looney 
1926df8bae1dSRodney W. Grimes 		default:
19278501a69cSRobert Watson 			INP_WUNLOCK(inp);
1928df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1929df8bae1dSRodney W. Grimes 			break;
1930df8bae1dSRodney W. Grimes 		}
1931df8bae1dSRodney W. Grimes 		break;
1932df8bae1dSRodney W. Grimes 
1933cfe8b629SGarrett Wollman 	case SOPT_GET:
19341e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
1935cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1936fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
193788f6b043SBruce M Simpson 		case TCP_MD5SIG:
1938fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
19398501a69cSRobert Watson 				INP_WUNLOCK(inp);
1940fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
1941fcf59617SAndrey V. Elsukov 			}
1942fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
19431cfd4b53SBruce M Simpson 			break;
1944265ed012SBruce M Simpson #endif
19451e8f5ffaSRobert Watson 
1946df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1947cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
19488501a69cSRobert Watson 			INP_WUNLOCK(inp);
1949b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1950df8bae1dSRodney W. Grimes 			break;
1951df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1952cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
19538501a69cSRobert Watson 			INP_WUNLOCK(inp);
1954b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1955df8bae1dSRodney W. Grimes 			break;
1956a0292f23SGarrett Wollman 		case TCP_NOOPT:
1957cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
19588501a69cSRobert Watson 			INP_WUNLOCK(inp);
1959b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1960a0292f23SGarrett Wollman 			break;
1961a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1962cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
19638501a69cSRobert Watson 			INP_WUNLOCK(inp);
1964b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1965b8af5dfaSRobert Watson 			break;
1966b8af5dfaSRobert Watson 		case TCP_INFO:
1967b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
19688501a69cSRobert Watson 			INP_WUNLOCK(inp);
1969b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
1970a0292f23SGarrett Wollman 			break;
1971dbc42409SLawrence Stewart 		case TCP_CONGESTION:
1972af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1973dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
1974af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
1975dbc42409SLawrence Stewart 			break;
19762f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
19772f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
19782f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
19792f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
19802f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
19812f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
19825a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
19832f3eb7f4SGleb Smirnoff 				break;
19842f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
19855a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
19862f3eb7f4SGleb Smirnoff 				break;
19872f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
19885a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
19892f3eb7f4SGleb Smirnoff 				break;
19902f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
19915a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
19922f3eb7f4SGleb Smirnoff 				break;
19932f3eb7f4SGleb Smirnoff 			}
19942f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
19952f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
19962f3eb7f4SGleb Smirnoff 			break;
199786a996e6SHiren Panchasara #ifdef TCPPCAP
199886a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
199986a996e6SHiren Panchasara 		case TCP_PCAP_IN:
200086a996e6SHiren Panchasara 			optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
200186a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
200286a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
200386a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
200486a996e6SHiren Panchasara 			break;
200586a996e6SHiren Panchasara #endif
2006281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
2007281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
2008281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2009281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
2010281a0fd4SPatrick Kelsey 			break;
2011e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
20122529f56eSJonathan T. Looney 		case TCP_LOG:
20132529f56eSJonathan T. Looney 			optval = tp->t_logstate;
20142529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
20152529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
20162529f56eSJonathan T. Looney 			break;
20172529f56eSJonathan T. Looney 		case TCP_LOGBUF:
20182529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
20192529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
20202529f56eSJonathan T. Looney 			break;
20212529f56eSJonathan T. Looney 		case TCP_LOGID:
20222529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
20232529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
20242529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
20252529f56eSJonathan T. Looney 			break;
20262529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
20272529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
20282529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
20292529f56eSJonathan T. Looney 			error = EINVAL;
20302529f56eSJonathan T. Looney 			break;
2031e24e5683SJonathan T. Looney #endif
2032df8bae1dSRodney W. Grimes 		default:
20338501a69cSRobert Watson 			INP_WUNLOCK(inp);
2034df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2035df8bae1dSRodney W. Grimes 			break;
2036df8bae1dSRodney W. Grimes 		}
2037df8bae1dSRodney W. Grimes 		break;
2038df8bae1dSRodney W. Grimes 	}
2039df8bae1dSRodney W. Grimes 	return (error);
2040df8bae1dSRodney W. Grimes }
20418501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2042bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2043df8bae1dSRodney W. Grimes 
204426e30fbbSDavid Greenman /*
2045df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
2046df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
2047df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
2048df8bae1dSRodney W. Grimes  */
20490312fbe9SPoul-Henning Kamp static int
2050ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so)
2051df8bae1dSRodney W. Grimes {
2052ad3f9ab3SAndre Oppermann 	struct tcpcb *tp;
2053df8bae1dSRodney W. Grimes 	struct inpcb *inp;
20546573d758SMatt Macy 	struct epoch_tracker et;
2055df8bae1dSRodney W. Grimes 	int error;
2056df8bae1dSRodney W. Grimes 
2057df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
2058e233e2acSAndre Oppermann 		error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
2059df8bae1dSRodney W. Grimes 		if (error)
2060df8bae1dSRodney W. Grimes 			return (error);
2061df8bae1dSRodney W. Grimes 	}
20626741ecf5SAndre Oppermann 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
20636741ecf5SAndre Oppermann 	so->so_snd.sb_flags |= SB_AUTOSIZE;
20646573d758SMatt Macy 	INP_INFO_RLOCK_ET(&V_tcbinfo, et);
2065603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_tcbinfo);
2066f2de87feSRobert Watson 	if (error) {
20676573d758SMatt Macy 		INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
2068df8bae1dSRodney W. Grimes 		return (error);
2069f2de87feSRobert Watson 	}
2070df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
2071fb59c426SYoshinobu Inoue #ifdef INET6
20725cd54324SBjoern A. Zeeb 	if (inp->inp_vflag & INP_IPV6PROTO) {
2073fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
207463ec505aSMichael Tuexen 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
207563ec505aSMichael Tuexen 			inp->inp_vflag |= INP_IPV4;
2076fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
2077fb59c426SYoshinobu Inoue 	}
2078fb59c426SYoshinobu Inoue 	else
2079fb59c426SYoshinobu Inoue #endif
2080cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
2081df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
2082623dce13SRobert Watson 	if (tp == NULL) {
2083df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
20840206cdb8SBjoern A. Zeeb 		in_pcbfree(inp);
20856573d758SMatt Macy 		INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
2086df8bae1dSRodney W. Grimes 		return (ENOBUFS);
2087df8bae1dSRodney W. Grimes 	}
2088df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
20898501a69cSRobert Watson 	INP_WUNLOCK(inp);
20906573d758SMatt Macy 	INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
2091bf840a17SGleb Smirnoff 	TCPSTATES_INC(TCPS_CLOSED);
2092df8bae1dSRodney W. Grimes 	return (0);
2093df8bae1dSRodney W. Grimes }
2094df8bae1dSRodney W. Grimes 
2095df8bae1dSRodney W. Grimes /*
2096df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2097df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2098df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2099df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2100df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2101df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2102df8bae1dSRodney W. Grimes  */
2103623dce13SRobert Watson static void
2104ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2105df8bae1dSRodney W. Grimes {
2106e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
2107e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
2108e6e0b5ffSRobert Watson 
2109ff9b006dSJulien Charbon 	INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
21108501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2111df8bae1dSRodney W. Grimes 
2112623dce13SRobert Watson 	/*
2113623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2114623dce13SRobert Watson 	 * socket is still open.
2115623dce13SRobert Watson 	 */
2116*8db239dcSMichael Tuexen 	if (tp->t_state < TCPS_ESTABLISHED &&
2117*8db239dcSMichael Tuexen 	    !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2118df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2119623dce13SRobert Watson 		KASSERT(tp != NULL,
2120623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2121623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2122243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2123623dce13SRobert Watson 		KASSERT(tp != NULL,
2124623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2125623dce13SRobert Watson 	} else {
2126df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2127df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2128623dce13SRobert Watson 		tcp_usrclosed(tp);
2129ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
213055bceb1eSRandall Stewart 			tp->t_fb->tfb_tcp_output(tp);
2131df8bae1dSRodney W. Grimes 	}
2132df8bae1dSRodney W. Grimes }
2133df8bae1dSRodney W. Grimes 
2134df8bae1dSRodney W. Grimes /*
2135df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2136df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2137df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2138df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2139df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2140df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2141df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2142df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2143df8bae1dSRodney W. Grimes  */
2144623dce13SRobert Watson static void
2145ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2146df8bae1dSRodney W. Grimes {
2147df8bae1dSRodney W. Grimes 
2148ff9b006dSJulien Charbon 	INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
21498501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
2150e6e0b5ffSRobert Watson 
2151df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2152df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
215309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
215409fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
215509fe6320SNavdeep Parhar #endif
2156550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2157bc65987aSKip Macy 		/* FALLTHROUGH */
2158bc65987aSKip Macy 	case TCPS_CLOSED:
2159df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2160623dce13SRobert Watson 		/*
2161623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2162623dce13SRobert Watson 		 * still open.
2163623dce13SRobert Watson 		 */
2164623dce13SRobert Watson 		KASSERT(tp != NULL,
2165623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2166df8bae1dSRodney W. Grimes 		break;
2167df8bae1dSRodney W. Grimes 
2168a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2169df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2170a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2171a0292f23SGarrett Wollman 		break;
2172a0292f23SGarrett Wollman 
2173df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
217457f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2175df8bae1dSRodney W. Grimes 		break;
2176df8bae1dSRodney W. Grimes 
2177df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
217857f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2179df8bae1dSRodney W. Grimes 		break;
2180df8bae1dSRodney W. Grimes 	}
2181abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
2182df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
2183abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
21847c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
21857c72af87SMohan Srinivasan 			int timeout;
21867c72af87SMohan Srinivasan 
21877c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
21889077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2189b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2190b6239c4aSAndras Olah 		}
2191df8bae1dSRodney W. Grimes 	}
21927c72af87SMohan Srinivasan }
2193497057eeSRobert Watson 
2194497057eeSRobert Watson #ifdef DDB
2195497057eeSRobert Watson static void
2196497057eeSRobert Watson db_print_indent(int indent)
2197497057eeSRobert Watson {
2198497057eeSRobert Watson 	int i;
2199497057eeSRobert Watson 
2200497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2201497057eeSRobert Watson 		db_printf(" ");
2202497057eeSRobert Watson }
2203497057eeSRobert Watson 
2204497057eeSRobert Watson static void
2205497057eeSRobert Watson db_print_tstate(int t_state)
2206497057eeSRobert Watson {
2207497057eeSRobert Watson 
2208497057eeSRobert Watson 	switch (t_state) {
2209497057eeSRobert Watson 	case TCPS_CLOSED:
2210497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2211497057eeSRobert Watson 		return;
2212497057eeSRobert Watson 
2213497057eeSRobert Watson 	case TCPS_LISTEN:
2214497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2215497057eeSRobert Watson 		return;
2216497057eeSRobert Watson 
2217497057eeSRobert Watson 	case TCPS_SYN_SENT:
2218497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2219497057eeSRobert Watson 		return;
2220497057eeSRobert Watson 
2221497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2222497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2223497057eeSRobert Watson 		return;
2224497057eeSRobert Watson 
2225497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2226497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2227497057eeSRobert Watson 		return;
2228497057eeSRobert Watson 
2229497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2230497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2231497057eeSRobert Watson 		return;
2232497057eeSRobert Watson 
2233497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2234497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2235497057eeSRobert Watson 		return;
2236497057eeSRobert Watson 
2237497057eeSRobert Watson 	case TCPS_CLOSING:
2238497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2239497057eeSRobert Watson 		return;
2240497057eeSRobert Watson 
2241497057eeSRobert Watson 	case TCPS_LAST_ACK:
2242497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2243497057eeSRobert Watson 		return;
2244497057eeSRobert Watson 
2245497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2246497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2247497057eeSRobert Watson 		return;
2248497057eeSRobert Watson 
2249497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2250497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2251497057eeSRobert Watson 		return;
2252497057eeSRobert Watson 
2253497057eeSRobert Watson 	default:
2254497057eeSRobert Watson 		db_printf("unknown");
2255497057eeSRobert Watson 		return;
2256497057eeSRobert Watson 	}
2257497057eeSRobert Watson }
2258497057eeSRobert Watson 
2259497057eeSRobert Watson static void
2260497057eeSRobert Watson db_print_tflags(u_int t_flags)
2261497057eeSRobert Watson {
2262497057eeSRobert Watson 	int comma;
2263497057eeSRobert Watson 
2264497057eeSRobert Watson 	comma = 0;
2265497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2266497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2267497057eeSRobert Watson 		comma = 1;
2268497057eeSRobert Watson 	}
2269497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2270497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2271497057eeSRobert Watson 		comma = 1;
2272497057eeSRobert Watson 	}
2273497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2274497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2275497057eeSRobert Watson 		comma = 1;
2276497057eeSRobert Watson 	}
2277497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2278497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2279497057eeSRobert Watson 		comma = 1;
2280497057eeSRobert Watson 	}
2281497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2282497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2283497057eeSRobert Watson 		comma = 1;
2284497057eeSRobert Watson 	}
2285497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2286497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2287497057eeSRobert Watson 		comma = 1;
2288497057eeSRobert Watson 	}
2289497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2290497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2291497057eeSRobert Watson 		comma = 1;
2292497057eeSRobert Watson 	}
2293497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2294497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2295497057eeSRobert Watson 		comma = 1;
2296497057eeSRobert Watson 	}
2297497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2298497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2299497057eeSRobert Watson 		comma = 1;
2300497057eeSRobert Watson 	}
2301497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2302497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2303497057eeSRobert Watson 		comma = 1;
2304497057eeSRobert Watson 	}
2305497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2306497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2307497057eeSRobert Watson 		comma = 1;
2308497057eeSRobert Watson 	}
2309497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2310497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2311497057eeSRobert Watson 		comma = 1;
2312497057eeSRobert Watson 	}
2313497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2314497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2315497057eeSRobert Watson 		comma = 1;
2316497057eeSRobert Watson 	}
2317497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2318497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2319497057eeSRobert Watson 		comma = 1;
2320497057eeSRobert Watson 	}
2321497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
2322497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2323497057eeSRobert Watson 		comma = 1;
2324497057eeSRobert Watson 	}
2325497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2326497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2327497057eeSRobert Watson 		comma = 1;
2328497057eeSRobert Watson 	}
2329497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2330497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2331497057eeSRobert Watson 		comma = 1;
2332497057eeSRobert Watson 	}
2333497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2334497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2335497057eeSRobert Watson 		comma = 1;
2336497057eeSRobert Watson 	}
2337dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
2338dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2339dbc42409SLawrence Stewart 		comma = 1;
2340dbc42409SLawrence Stewart 	}
2341497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2342497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2343497057eeSRobert Watson 		comma = 1;
2344497057eeSRobert Watson 	}
2345497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2346497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2347497057eeSRobert Watson 		comma = 1;
2348497057eeSRobert Watson 	}
2349497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2350497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2351497057eeSRobert Watson 		comma = 1;
2352497057eeSRobert Watson 	}
2353497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2354497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2355497057eeSRobert Watson 		comma = 1;
2356497057eeSRobert Watson 	}
2357f2512ba1SRui Paulo 	if (t_flags & TF_ECN_PERMIT) {
2358f2512ba1SRui Paulo 		db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2359f2512ba1SRui Paulo 		comma = 1;
2360f2512ba1SRui Paulo 	}
2361281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2362281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2363281a0fd4SPatrick Kelsey 		comma = 1;
2364281a0fd4SPatrick Kelsey 	}
2365497057eeSRobert Watson }
2366497057eeSRobert Watson 
2367497057eeSRobert Watson static void
2368497057eeSRobert Watson db_print_toobflags(char t_oobflags)
2369497057eeSRobert Watson {
2370497057eeSRobert Watson 	int comma;
2371497057eeSRobert Watson 
2372497057eeSRobert Watson 	comma = 0;
2373497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
2374497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2375497057eeSRobert Watson 		comma = 1;
2376497057eeSRobert Watson 	}
2377497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
2378497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2379497057eeSRobert Watson 		comma = 1;
2380497057eeSRobert Watson 	}
2381497057eeSRobert Watson }
2382497057eeSRobert Watson 
2383497057eeSRobert Watson static void
2384497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2385497057eeSRobert Watson {
2386497057eeSRobert Watson 
2387497057eeSRobert Watson 	db_print_indent(indent);
2388497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
2389497057eeSRobert Watson 
2390497057eeSRobert Watson 	indent += 2;
2391497057eeSRobert Watson 
2392497057eeSRobert Watson 	db_print_indent(indent);
2393497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
23944741bfcbSPatrick Kelsey 	   LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2395497057eeSRobert Watson 
2396497057eeSRobert Watson 	db_print_indent(indent);
239785d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
2398e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2399497057eeSRobert Watson 
2400497057eeSRobert Watson 	db_print_indent(indent);
2401e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2402e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
2403497057eeSRobert Watson 
2404497057eeSRobert Watson 	db_print_indent(indent);
2405497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
2406497057eeSRobert Watson 	db_print_tstate(tp->t_state);
2407497057eeSRobert Watson 	db_printf(")\n");
2408497057eeSRobert Watson 
2409497057eeSRobert Watson 	db_print_indent(indent);
2410497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
2411497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
2412497057eeSRobert Watson 	db_printf(")\n");
2413497057eeSRobert Watson 
2414497057eeSRobert Watson 	db_print_indent(indent);
2415497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
2416497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
2417497057eeSRobert Watson 
2418497057eeSRobert Watson 	db_print_indent(indent);
2419497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
2420497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2421497057eeSRobert Watson 
2422497057eeSRobert Watson 	db_print_indent(indent);
2423497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
2424497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
2425497057eeSRobert Watson 
2426497057eeSRobert Watson 	db_print_indent(indent);
24273ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
2428497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2429497057eeSRobert Watson 
2430497057eeSRobert Watson 	db_print_indent(indent);
24313ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
24321c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
2433497057eeSRobert Watson 
2434497057eeSRobert Watson 	db_print_indent(indent);
24353ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
24361c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2437497057eeSRobert Watson 
2438497057eeSRobert Watson 	db_print_indent(indent);
24390c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
24400c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
2441497057eeSRobert Watson 
2442497057eeSRobert Watson 	db_print_indent(indent);
24431c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
24441c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
2445497057eeSRobert Watson 
2446497057eeSRobert Watson 	db_print_indent(indent);
24471c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
24481c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2449497057eeSRobert Watson 
2450497057eeSRobert Watson 	db_print_indent(indent);
2451497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
2452497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2453497057eeSRobert Watson 	    tp->t_rttbest);
2454497057eeSRobert Watson 
2455497057eeSRobert Watson 	db_print_indent(indent);
24563ac12506SJonathan T. Looney 	db_printf("t_rttupdated: %lu   max_sndwnd: %u   t_softerror: %d\n",
2457497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2458497057eeSRobert Watson 
2459497057eeSRobert Watson 	db_print_indent(indent);
2460497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2461497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
2462497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
2463497057eeSRobert Watson 
2464497057eeSRobert Watson 	db_print_indent(indent);
2465497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
2466497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2467497057eeSRobert Watson 
2468497057eeSRobert Watson 	db_print_indent(indent);
24699f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
24701a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
2471497057eeSRobert Watson 
2472497057eeSRobert Watson 	db_print_indent(indent);
2473497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
24743ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2475497057eeSRobert Watson 
2476497057eeSRobert Watson 	db_print_indent(indent);
24773ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
24789f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2479497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
2480497057eeSRobert Watson 
2481497057eeSRobert Watson 	db_print_indent(indent);
24823529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
24833529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2484497057eeSRobert Watson 
2485497057eeSRobert Watson 	db_print_indent(indent);
2486497057eeSRobert Watson 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
2487497057eeSRobert Watson 	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2488497057eeSRobert Watson 
2489497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
2490497057eeSRobert Watson 
2491497057eeSRobert Watson 	db_print_indent(indent);
2492497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
2493497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2494497057eeSRobert Watson }
2495497057eeSRobert Watson 
2496497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2497497057eeSRobert Watson {
2498497057eeSRobert Watson 	struct tcpcb *tp;
2499497057eeSRobert Watson 
2500497057eeSRobert Watson 	if (!have_addr) {
2501497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
2502497057eeSRobert Watson 		return;
2503497057eeSRobert Watson 	}
2504497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
2505497057eeSRobert Watson 
2506497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
2507497057eeSRobert Watson }
2508497057eeSRobert Watson #endif
2509