xref: /freebsd/sys/netinet/tcp_usrreq.c (revision c560df6f12f1406a00b22a2e161a6a97d899fb03)
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>
93df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
944644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
95*c560df6fSPatrick Kelsey #ifdef TCP_RFC7413
96*c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h>
97*c560df6fSPatrick Kelsey #endif
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
203cea40c48SJulien Charbon 		 * three 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
208cea40c48SJulien Charbon 		 * #2 tcp_detach is called at timewait end (or reuse) by
209cea40c48SJulien Charbon 		 *  tcp_twclose, then the tcptw has already been discarded
210ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
211cea40c48SJulien Charbon 		 * #3 tcp_detach is called() after timewait ends (or reuse)
212cea40c48SJulien Charbon 		 *  (e.g. by soclose), then tcptw has already been discarded
213ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
214cea40c48SJulien Charbon 		 *
215cea40c48SJulien Charbon 		 *  In all three cases the tcptw should not be freed here.
216623dce13SRobert Watson 		 */
217ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED) {
218623dce13SRobert Watson 			in_pcbdetach(inp);
219f5cf1e5fSJulien Charbon 			if (__predict_true(tp == NULL)) {
2200206cdb8SBjoern A. Zeeb 				in_pcbfree(inp);
2210206cdb8SBjoern A. Zeeb 			} else {
222f5cf1e5fSJulien Charbon 				/*
223f5cf1e5fSJulien Charbon 				 * This case should not happen as in TIMEWAIT
224f5cf1e5fSJulien Charbon 				 * state the inp should not be destroyed before
225f5cf1e5fSJulien Charbon 				 * its tcptw.  If INVARIANTS is defined, panic.
226f5cf1e5fSJulien Charbon 				 */
227f5cf1e5fSJulien Charbon #ifdef INVARIANTS
228f5cf1e5fSJulien Charbon 				panic("%s: Panic before an inp double-free: "
229f5cf1e5fSJulien Charbon 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
230f5cf1e5fSJulien Charbon 				    , __func__);
231f5cf1e5fSJulien Charbon #else
232f5cf1e5fSJulien Charbon 				log(LOG_ERR, "%s: Avoid an inp double-free: "
233f5cf1e5fSJulien Charbon 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
234f5cf1e5fSJulien Charbon 				    , __func__);
235f5cf1e5fSJulien Charbon #endif
236f5cf1e5fSJulien Charbon 				INP_WUNLOCK(inp);
237f5cf1e5fSJulien Charbon 			}
238f5cf1e5fSJulien Charbon 		} else {
239623dce13SRobert Watson 			in_pcbdetach(inp);
2408501a69cSRobert Watson 			INP_WUNLOCK(inp);
241623dce13SRobert Watson 		}
242623dce13SRobert Watson 	} else {
243e6e65783SRobert Watson 		/*
244a152f8a3SRobert Watson 		 * If the connection is not in timewait, we consider two
245a152f8a3SRobert Watson 		 * two conditions: one in which no further processing is
246a152f8a3SRobert Watson 		 * necessary (dropped || embryonic), and one in which TCP is
247a152f8a3SRobert Watson 		 * not yet done, but no longer requires the socket, so the
248a152f8a3SRobert Watson 		 * pcb will persist for the time being.
249a152f8a3SRobert Watson 		 *
250a152f8a3SRobert Watson 		 * XXXRW: Does the second case still occur?
251e6e65783SRobert Watson 		 */
252ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED ||
253623dce13SRobert Watson 		    tp->t_state < TCPS_SYN_SENT) {
254623dce13SRobert Watson 			tcp_discardcb(tp);
255623dce13SRobert Watson 			in_pcbdetach(inp);
2560206cdb8SBjoern A. Zeeb 			in_pcbfree(inp);
257db3cee51SNavdeep Parhar 		} else {
258a152f8a3SRobert Watson 			in_pcbdetach(inp);
259db3cee51SNavdeep Parhar 			INP_WUNLOCK(inp);
260db3cee51SNavdeep Parhar 		}
261623dce13SRobert Watson 	}
262623dce13SRobert Watson }
263c78cbc7bSRobert Watson 
264c78cbc7bSRobert Watson /*
265c78cbc7bSRobert Watson  * pru_detach() detaches the TCP protocol from the socket.
266c78cbc7bSRobert Watson  * If the protocol state is non-embryonic, then can't
267c78cbc7bSRobert Watson  * do this directly: have to initiate a pru_disconnect(),
268c78cbc7bSRobert Watson  * which may finish later; embryonic TCB's can just
269c78cbc7bSRobert Watson  * be discarded here.
270c78cbc7bSRobert Watson  */
271c78cbc7bSRobert Watson static void
272c78cbc7bSRobert Watson tcp_usr_detach(struct socket *so)
273c78cbc7bSRobert Watson {
274c78cbc7bSRobert Watson 	struct inpcb *inp;
275079672cbSJulien Charbon 	int rlock = 0;
276c78cbc7bSRobert Watson 
277c78cbc7bSRobert Watson 	inp = sotoinpcb(so);
278c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
279079672cbSJulien Charbon 	if (!INP_INFO_WLOCKED(&V_tcbinfo)) {
280ff9b006dSJulien Charbon 		INP_INFO_RLOCK(&V_tcbinfo);
281079672cbSJulien Charbon 		rlock = 1;
282079672cbSJulien Charbon 	}
2838501a69cSRobert Watson 	INP_WLOCK(inp);
284c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
285c78cbc7bSRobert Watson 	    ("tcp_usr_detach: inp_socket == NULL"));
286c78cbc7bSRobert Watson 	tcp_detach(so, inp);
287079672cbSJulien Charbon 	if (rlock)
288ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
2892c37256eSGarrett Wollman }
2902c37256eSGarrett Wollman 
291b287c6c7SBjoern A. Zeeb #ifdef INET
2922c37256eSGarrett Wollman /*
2932c37256eSGarrett Wollman  * Give the socket an address.
2942c37256eSGarrett Wollman  */
2952c37256eSGarrett Wollman static int
296b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2972c37256eSGarrett Wollman {
2982c37256eSGarrett Wollman 	int error = 0;
299f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
300623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3012c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
3022c37256eSGarrett Wollman 
30352710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
30452710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sinp))
30552710de1SPawel Jakub Dawidek 		return (EINVAL);
3062c37256eSGarrett Wollman 	/*
3072c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
3082c37256eSGarrett Wollman 	 * to them.
3092c37256eSGarrett Wollman 	 */
3102c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
31152710de1SPawel Jakub Dawidek 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
31252710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
31352710de1SPawel Jakub Dawidek 
314623dce13SRobert Watson 	TCPDEBUG0;
315623dce13SRobert Watson 	inp = sotoinpcb(so);
316623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
3178501a69cSRobert Watson 	INP_WLOCK(inp);
318ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
319623dce13SRobert Watson 		error = EINVAL;
3202c37256eSGarrett Wollman 		goto out;
321623dce13SRobert Watson 	}
322623dce13SRobert Watson 	tp = intotcpcb(inp);
323623dce13SRobert Watson 	TCPDEBUG1();
324fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
325623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
326fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
327623dce13SRobert Watson out:
328623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3295d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3308501a69cSRobert Watson 	INP_WUNLOCK(inp);
331623dce13SRobert Watson 
332623dce13SRobert Watson 	return (error);
3332c37256eSGarrett Wollman }
334b287c6c7SBjoern A. Zeeb #endif /* INET */
3352c37256eSGarrett Wollman 
336fb59c426SYoshinobu Inoue #ifdef INET6
337fb59c426SYoshinobu Inoue static int
338b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
339fb59c426SYoshinobu Inoue {
340fb59c426SYoshinobu Inoue 	int error = 0;
341f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
342623dce13SRobert Watson 	struct tcpcb *tp = NULL;
343fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
344fb59c426SYoshinobu Inoue 
34552710de1SPawel Jakub Dawidek 	sin6p = (struct sockaddr_in6 *)nam;
34652710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sin6p))
34752710de1SPawel Jakub Dawidek 		return (EINVAL);
348fb59c426SYoshinobu Inoue 	/*
349fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
350fb59c426SYoshinobu Inoue 	 * to them.
351fb59c426SYoshinobu Inoue 	 */
352fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6 &&
35352710de1SPawel Jakub Dawidek 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
35452710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
35552710de1SPawel Jakub Dawidek 
356623dce13SRobert Watson 	TCPDEBUG0;
357623dce13SRobert Watson 	inp = sotoinpcb(so);
358623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
3598501a69cSRobert Watson 	INP_WLOCK(inp);
360ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
361623dce13SRobert Watson 		error = EINVAL;
362623dce13SRobert Watson 		goto out;
363623dce13SRobert Watson 	}
364623dce13SRobert Watson 	tp = intotcpcb(inp);
365623dce13SRobert Watson 	TCPDEBUG1();
366fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
367fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
368fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
369b287c6c7SBjoern A. Zeeb #ifdef INET
37066ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
371fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
372fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
373fb59c426SYoshinobu Inoue 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
374fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
375fb59c426SYoshinobu Inoue 
376fb59c426SYoshinobu Inoue 			in6_sin6_2_sin(&sin, sin6p);
377fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
378fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
379b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
380b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
381fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
382fb59c426SYoshinobu Inoue 			goto out;
383fb59c426SYoshinobu Inoue 		}
384fb59c426SYoshinobu Inoue 	}
385b287c6c7SBjoern A. Zeeb #endif
386b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
387fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
388623dce13SRobert Watson out:
389623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3905d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3918501a69cSRobert Watson 	INP_WUNLOCK(inp);
392623dce13SRobert Watson 	return (error);
393fb59c426SYoshinobu Inoue }
394fb59c426SYoshinobu Inoue #endif /* INET6 */
395fb59c426SYoshinobu Inoue 
396b287c6c7SBjoern A. Zeeb #ifdef INET
3972c37256eSGarrett Wollman /*
3982c37256eSGarrett Wollman  * Prepare to accept connections.
3992c37256eSGarrett Wollman  */
4002c37256eSGarrett Wollman static int
401d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
4022c37256eSGarrett Wollman {
4032c37256eSGarrett Wollman 	int error = 0;
404f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
405623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4062c37256eSGarrett Wollman 
407623dce13SRobert Watson 	TCPDEBUG0;
408623dce13SRobert Watson 	inp = sotoinpcb(so);
409623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
4108501a69cSRobert Watson 	INP_WLOCK(inp);
411ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
412623dce13SRobert Watson 		error = EINVAL;
413623dce13SRobert Watson 		goto out;
414623dce13SRobert Watson 	}
415623dce13SRobert Watson 	tp = intotcpcb(inp);
416623dce13SRobert Watson 	TCPDEBUG1();
4170daccb9cSRobert Watson 	SOCK_LOCK(so);
4180daccb9cSRobert Watson 	error = solisten_proto_check(so);
419fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
4200daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0)
421b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
422fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4230daccb9cSRobert Watson 	if (error == 0) {
42457f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
425d374e81eSRobert Watson 		solisten_proto(so, backlog);
42609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
42737cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
42809fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
42909fe6320SNavdeep Parhar #endif
4300daccb9cSRobert Watson 	}
4310daccb9cSRobert Watson 	SOCK_UNLOCK(so);
432623dce13SRobert Watson 
433281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
43468bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
435281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
436281a0fd4SPatrick Kelsey #endif
437623dce13SRobert Watson out:
438623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4395d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4408501a69cSRobert Watson 	INP_WUNLOCK(inp);
441623dce13SRobert Watson 	return (error);
4422c37256eSGarrett Wollman }
443b287c6c7SBjoern A. Zeeb #endif /* INET */
4442c37256eSGarrett Wollman 
445fb59c426SYoshinobu Inoue #ifdef INET6
446fb59c426SYoshinobu Inoue static int
447d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
448fb59c426SYoshinobu Inoue {
449fb59c426SYoshinobu Inoue 	int error = 0;
450f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
451623dce13SRobert Watson 	struct tcpcb *tp = NULL;
452fb59c426SYoshinobu Inoue 
453623dce13SRobert Watson 	TCPDEBUG0;
454623dce13SRobert Watson 	inp = sotoinpcb(so);
455623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4568501a69cSRobert Watson 	INP_WLOCK(inp);
457ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
458623dce13SRobert Watson 		error = EINVAL;
459623dce13SRobert Watson 		goto out;
460623dce13SRobert Watson 	}
461623dce13SRobert Watson 	tp = intotcpcb(inp);
462623dce13SRobert Watson 	TCPDEBUG1();
4630daccb9cSRobert Watson 	SOCK_LOCK(so);
4640daccb9cSRobert Watson 	error = solisten_proto_check(so);
465fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
4660daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0) {
467fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
46866ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
469fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
470b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
471fb59c426SYoshinobu Inoue 	}
472fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4730daccb9cSRobert Watson 	if (error == 0) {
47457f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
475d374e81eSRobert Watson 		solisten_proto(so, backlog);
47609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
47737cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
47809fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
47909fe6320SNavdeep Parhar #endif
4800daccb9cSRobert Watson 	}
4810daccb9cSRobert Watson 	SOCK_UNLOCK(so);
482623dce13SRobert Watson 
483281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
48468bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
485281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
486281a0fd4SPatrick Kelsey #endif
487623dce13SRobert Watson out:
488623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4895d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4908501a69cSRobert Watson 	INP_WUNLOCK(inp);
491623dce13SRobert Watson 	return (error);
492fb59c426SYoshinobu Inoue }
493fb59c426SYoshinobu Inoue #endif /* INET6 */
494fb59c426SYoshinobu Inoue 
495b287c6c7SBjoern A. Zeeb #ifdef INET
4962c37256eSGarrett Wollman /*
4972c37256eSGarrett Wollman  * Initiate connection to peer.
4982c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
4992c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
5002c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
5012c37256eSGarrett Wollman  * Send initial segment on connection.
5022c37256eSGarrett Wollman  */
5032c37256eSGarrett Wollman static int
504b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
5052c37256eSGarrett Wollman {
5062c37256eSGarrett Wollman 	int error = 0;
507f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
508623dce13SRobert Watson 	struct tcpcb *tp = NULL;
5092c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
5102c37256eSGarrett Wollman 
51157bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
512e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
513e29ef13fSDon Lewis 		return (EINVAL);
51452710de1SPawel Jakub Dawidek 	/*
51552710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
51652710de1SPawel Jakub Dawidek 	 */
5172c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
51852710de1SPawel Jakub Dawidek 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
51952710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
520b89e82ddSJamie Gritton 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
521b89e82ddSJamie Gritton 		return (error);
52275c13541SPoul-Henning Kamp 
523623dce13SRobert Watson 	TCPDEBUG0;
524623dce13SRobert Watson 	inp = sotoinpcb(so);
525623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
5268501a69cSRobert Watson 	INP_WLOCK(inp);
527eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
528eb96dc33SJulien Charbon 		error = EADDRINUSE;
529eb96dc33SJulien Charbon 		goto out;
530eb96dc33SJulien Charbon 	}
531eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
532eb96dc33SJulien Charbon 		error = ECONNREFUSED;
533623dce13SRobert Watson 		goto out;
534623dce13SRobert Watson 	}
535623dce13SRobert Watson 	tp = intotcpcb(inp);
536623dce13SRobert Watson 	TCPDEBUG1();
537b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
5382c37256eSGarrett Wollman 		goto out;
53909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
54009fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
54137cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
54209fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
54309fe6320SNavdeep Parhar 		goto out;
54409fe6320SNavdeep Parhar #endif
54509fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
54655bceb1eSRandall Stewart 	error = tp->t_fb->tfb_tcp_output(tp);
547623dce13SRobert Watson out:
548623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
549e79cb051SGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
5508501a69cSRobert Watson 	INP_WUNLOCK(inp);
551623dce13SRobert Watson 	return (error);
5522c37256eSGarrett Wollman }
553b287c6c7SBjoern A. Zeeb #endif /* INET */
5542c37256eSGarrett Wollman 
555fb59c426SYoshinobu Inoue #ifdef INET6
556fb59c426SYoshinobu Inoue static int
557b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
558fb59c426SYoshinobu Inoue {
559fb59c426SYoshinobu Inoue 	int error = 0;
560f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
561623dce13SRobert Watson 	struct tcpcb *tp = NULL;
562fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
563623dce13SRobert Watson 
564623dce13SRobert Watson 	TCPDEBUG0;
565fb59c426SYoshinobu Inoue 
566fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
567e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sin6p))
568e29ef13fSDon Lewis 		return (EINVAL);
56952710de1SPawel Jakub Dawidek 	/*
57052710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
57152710de1SPawel Jakub Dawidek 	 */
572fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6
57352710de1SPawel Jakub Dawidek 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
57452710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
575fb59c426SYoshinobu Inoue 
576623dce13SRobert Watson 	inp = sotoinpcb(so);
577623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
5788501a69cSRobert Watson 	INP_WLOCK(inp);
579eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
580eb96dc33SJulien Charbon 		error = EADDRINUSE;
581eb96dc33SJulien Charbon 		goto out;
582eb96dc33SJulien Charbon 	}
583eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
584eb96dc33SJulien Charbon 		error = ECONNREFUSED;
585623dce13SRobert Watson 		goto out;
586623dce13SRobert Watson 	}
587623dce13SRobert Watson 	tp = intotcpcb(inp);
588623dce13SRobert Watson 	TCPDEBUG1();
589b287c6c7SBjoern A. Zeeb #ifdef INET
590fa046d87SRobert Watson 	/*
591fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
592fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
593fa046d87SRobert Watson 	 * Is this a significant problem?
594fa046d87SRobert Watson 	 */
59533841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
596fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
597fb59c426SYoshinobu Inoue 
598d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
599d46a5312SMaxim Konovalov 			error = EINVAL;
600d46a5312SMaxim Konovalov 			goto out;
601d46a5312SMaxim Konovalov 		}
6025dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV4) == 0) {
6035dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6045dba6adaSMichael Tuexen 			goto out;
6055dba6adaSMichael Tuexen 		}
60633841545SHajimu UMEMOTO 
607fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
608fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
609fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
610b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
611b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
612413628a7SBjoern A. Zeeb 			goto out;
613b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
614fb59c426SYoshinobu Inoue 			goto out;
61509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
61609fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
617adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
61809fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
61909fe6320SNavdeep Parhar 			goto out;
62009fe6320SNavdeep Parhar #endif
62155bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
622fb59c426SYoshinobu Inoue 		goto out;
6235dba6adaSMichael Tuexen 	} else {
6245dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV6) == 0) {
6255dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6265dba6adaSMichael Tuexen 			goto out;
6275dba6adaSMichael Tuexen 		}
628fb59c426SYoshinobu Inoue 	}
629b287c6c7SBjoern A. Zeeb #endif
630fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
631fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
632dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
633b89e82ddSJamie Gritton 	if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
634413628a7SBjoern A. Zeeb 		goto out;
635b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
636fb59c426SYoshinobu Inoue 		goto out;
63709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
63809fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
639adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
64009fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
64109fe6320SNavdeep Parhar 		goto out;
64209fe6320SNavdeep Parhar #endif
64309fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
64455bceb1eSRandall Stewart 	error = tp->t_fb->tfb_tcp_output(tp);
645623dce13SRobert Watson 
646623dce13SRobert Watson out:
647623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
6485d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
6498501a69cSRobert Watson 	INP_WUNLOCK(inp);
650623dce13SRobert Watson 	return (error);
651fb59c426SYoshinobu Inoue }
652fb59c426SYoshinobu Inoue #endif /* INET6 */
653fb59c426SYoshinobu Inoue 
6542c37256eSGarrett Wollman /*
6552c37256eSGarrett Wollman  * Initiate disconnect from peer.
6562c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
6572c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
6582c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
6592c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
6602c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
6612c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
6622c37256eSGarrett Wollman  *
6632c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
6642c37256eSGarrett Wollman  */
6652c37256eSGarrett Wollman static int
6662c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
6672c37256eSGarrett Wollman {
668f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
669623dce13SRobert Watson 	struct tcpcb *tp = NULL;
670623dce13SRobert Watson 	int error = 0;
6712c37256eSGarrett Wollman 
672623dce13SRobert Watson 	TCPDEBUG0;
673ff9b006dSJulien Charbon 	INP_INFO_RLOCK(&V_tcbinfo);
674623dce13SRobert Watson 	inp = sotoinpcb(so);
675623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
6768501a69cSRobert Watson 	INP_WLOCK(inp);
677489dcc92SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT)
678489dcc92SJulien Charbon 		goto out;
679489dcc92SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
68021367f63SSam Leffler 		error = ECONNRESET;
681623dce13SRobert Watson 		goto out;
682623dce13SRobert Watson 	}
683623dce13SRobert Watson 	tp = intotcpcb(inp);
684623dce13SRobert Watson 	TCPDEBUG1();
685623dce13SRobert Watson 	tcp_disconnect(tp);
686623dce13SRobert Watson out:
687623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
6885d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
6898501a69cSRobert Watson 	INP_WUNLOCK(inp);
690ff9b006dSJulien Charbon 	INP_INFO_RUNLOCK(&V_tcbinfo);
691623dce13SRobert Watson 	return (error);
6922c37256eSGarrett Wollman }
6932c37256eSGarrett Wollman 
694b287c6c7SBjoern A. Zeeb #ifdef INET
6952c37256eSGarrett Wollman /*
6968296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
6978296cddfSRobert Watson  * just return the address of the peer, storing through addr.
6982c37256eSGarrett Wollman  */
6992c37256eSGarrett Wollman static int
70057bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
7012c37256eSGarrett Wollman {
7022c37256eSGarrett Wollman 	int error = 0;
703f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
7041db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
70526ef6ac4SDon Lewis 	struct in_addr addr;
70626ef6ac4SDon Lewis 	in_port_t port = 0;
7071db24ffbSJonathan Lemon 	TCPDEBUG0;
7082c37256eSGarrett Wollman 
7093d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
7103d2d3ef4SRobert Watson 		return (ECONNABORTED);
711f76fcf6dSJeffrey Hsu 
712f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
713623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
7148501a69cSRobert Watson 	INP_WLOCK(inp);
715ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
7163d2d3ef4SRobert Watson 		error = ECONNABORTED;
717623dce13SRobert Watson 		goto out;
718623dce13SRobert Watson 	}
7191db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
7201db24ffbSJonathan Lemon 	TCPDEBUG1();
721f76fcf6dSJeffrey Hsu 
722f76fcf6dSJeffrey Hsu 	/*
72354d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
72426ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
72526ef6ac4SDon Lewis 	 * release the lock.
726f76fcf6dSJeffrey Hsu 	 */
72726ef6ac4SDon Lewis 	port = inp->inp_fport;
72826ef6ac4SDon Lewis 	addr = inp->inp_faddr;
729f76fcf6dSJeffrey Hsu 
730623dce13SRobert Watson out:
731623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
7325d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7338501a69cSRobert Watson 	INP_WUNLOCK(inp);
73426ef6ac4SDon Lewis 	if (error == 0)
73526ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
73626ef6ac4SDon Lewis 	return error;
7372c37256eSGarrett Wollman }
738b287c6c7SBjoern A. Zeeb #endif /* INET */
7392c37256eSGarrett Wollman 
740fb59c426SYoshinobu Inoue #ifdef INET6
741fb59c426SYoshinobu Inoue static int
742fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
743fb59c426SYoshinobu Inoue {
744f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
745fb59c426SYoshinobu Inoue 	int error = 0;
7461db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
74726ef6ac4SDon Lewis 	struct in_addr addr;
74826ef6ac4SDon Lewis 	struct in6_addr addr6;
74926ef6ac4SDon Lewis 	in_port_t port = 0;
75026ef6ac4SDon Lewis 	int v4 = 0;
7511db24ffbSJonathan Lemon 	TCPDEBUG0;
752fb59c426SYoshinobu Inoue 
753b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
754b4470c16SRobert Watson 		return (ECONNABORTED);
755f76fcf6dSJeffrey Hsu 
756f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
757623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
758fa046d87SRobert Watson 	INP_INFO_RLOCK(&V_tcbinfo);
7598501a69cSRobert Watson 	INP_WLOCK(inp);
760ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
76121367f63SSam Leffler 		error = ECONNABORTED;
762623dce13SRobert Watson 		goto out;
763623dce13SRobert Watson 	}
7641db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
7651db24ffbSJonathan Lemon 	TCPDEBUG1();
766623dce13SRobert Watson 
76726ef6ac4SDon Lewis 	/*
76826ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
76926ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
77026ef6ac4SDon Lewis 	 * release the lock.
77126ef6ac4SDon Lewis 	 */
77226ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
77326ef6ac4SDon Lewis 		v4 = 1;
77426ef6ac4SDon Lewis 		port = inp->inp_fport;
77526ef6ac4SDon Lewis 		addr = inp->inp_faddr;
77626ef6ac4SDon Lewis 	} else {
77726ef6ac4SDon Lewis 		port = inp->inp_fport;
77826ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
77926ef6ac4SDon Lewis 	}
78026ef6ac4SDon Lewis 
781623dce13SRobert Watson out:
782623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
7835d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7848501a69cSRobert Watson 	INP_WUNLOCK(inp);
785fa046d87SRobert Watson 	INP_INFO_RUNLOCK(&V_tcbinfo);
78626ef6ac4SDon Lewis 	if (error == 0) {
78726ef6ac4SDon Lewis 		if (v4)
78826ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
78926ef6ac4SDon Lewis 		else
79026ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
79126ef6ac4SDon Lewis 	}
79226ef6ac4SDon Lewis 	return error;
793fb59c426SYoshinobu Inoue }
794fb59c426SYoshinobu Inoue #endif /* INET6 */
795f76fcf6dSJeffrey Hsu 
796f76fcf6dSJeffrey Hsu /*
7972c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
7982c37256eSGarrett Wollman  */
7992c37256eSGarrett Wollman static int
8002c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
8012c37256eSGarrett Wollman {
8022c37256eSGarrett Wollman 	int error = 0;
803f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
804623dce13SRobert Watson 	struct tcpcb *tp = NULL;
8052c37256eSGarrett Wollman 
806623dce13SRobert Watson 	TCPDEBUG0;
807ff9b006dSJulien Charbon 	INP_INFO_RLOCK(&V_tcbinfo);
808623dce13SRobert Watson 	inp = sotoinpcb(so);
809623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
8108501a69cSRobert Watson 	INP_WLOCK(inp);
811ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
81221367f63SSam Leffler 		error = ECONNRESET;
813623dce13SRobert Watson 		goto out;
814623dce13SRobert Watson 	}
815623dce13SRobert Watson 	tp = intotcpcb(inp);
816623dce13SRobert Watson 	TCPDEBUG1();
8172c37256eSGarrett Wollman 	socantsendmore(so);
818623dce13SRobert Watson 	tcp_usrclosed(tp);
819ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
82055bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
821623dce13SRobert Watson 
822623dce13SRobert Watson out:
823623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
8245d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
8258501a69cSRobert Watson 	INP_WUNLOCK(inp);
826ff9b006dSJulien Charbon 	INP_INFO_RUNLOCK(&V_tcbinfo);
827623dce13SRobert Watson 
828623dce13SRobert Watson 	return (error);
8292c37256eSGarrett Wollman }
8302c37256eSGarrett Wollman 
8312c37256eSGarrett Wollman /*
8322c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
8332c37256eSGarrett Wollman  */
8342c37256eSGarrett Wollman static int
8352c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
8362c37256eSGarrett Wollman {
837f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
838623dce13SRobert Watson 	struct tcpcb *tp = NULL;
839623dce13SRobert Watson 	int error = 0;
8402c37256eSGarrett Wollman 
841623dce13SRobert Watson 	TCPDEBUG0;
842623dce13SRobert Watson 	inp = sotoinpcb(so);
843623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
8448501a69cSRobert Watson 	INP_WLOCK(inp);
845ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
84621367f63SSam Leffler 		error = ECONNRESET;
847623dce13SRobert Watson 		goto out;
848623dce13SRobert Watson 	}
849623dce13SRobert Watson 	tp = intotcpcb(inp);
850623dce13SRobert Watson 	TCPDEBUG1();
851281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
852281a0fd4SPatrick Kelsey 	/*
853281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
854281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
855281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
856281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
857281a0fd4SPatrick Kelsey 	 * expires.
858281a0fd4SPatrick Kelsey 	 */
85968bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags) &&
860281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
861281a0fd4SPatrick Kelsey 		goto out;
862281a0fd4SPatrick Kelsey #endif
86309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
86409fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
86509fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
866460cf046SNavdeep Parhar 	else
86709fe6320SNavdeep Parhar #endif
86855bceb1eSRandall Stewart 	tp->t_fb->tfb_tcp_output(tp);
869623dce13SRobert Watson 
870623dce13SRobert Watson out:
871623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
8725d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
8738501a69cSRobert Watson 	INP_WUNLOCK(inp);
874623dce13SRobert Watson 	return (error);
8752c37256eSGarrett Wollman }
8762c37256eSGarrett Wollman 
8772c37256eSGarrett Wollman /*
8782c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
8799c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
8809c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
8819c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
8829c9906e9SPeter Wemm  * generally are caller-frees.
8832c37256eSGarrett Wollman  */
8842c37256eSGarrett Wollman static int
88557bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
886b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
8872c37256eSGarrett Wollman {
8882c37256eSGarrett Wollman 	int error = 0;
889f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
890623dce13SRobert Watson 	struct tcpcb *tp = NULL;
891fb59c426SYoshinobu Inoue #ifdef INET6
892fb59c426SYoshinobu Inoue 	int isipv6;
893fb59c426SYoshinobu Inoue #endif
8949c9906e9SPeter Wemm 	TCPDEBUG0;
8952c37256eSGarrett Wollman 
896f76fcf6dSJeffrey Hsu 	/*
897fa046d87SRobert Watson 	 * We require the pcbinfo lock if we will close the socket as part of
898fa046d87SRobert Watson 	 * this call.
899f76fcf6dSJeffrey Hsu 	 */
900fa046d87SRobert Watson 	if (flags & PRUS_EOF)
901ff9b006dSJulien Charbon 		INP_INFO_RLOCK(&V_tcbinfo);
902f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
903623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
9048501a69cSRobert Watson 	INP_WLOCK(inp);
905ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
9067ff0b850SAndre Oppermann 		if (control)
9077ff0b850SAndre Oppermann 			m_freem(control);
9082cbcd3c1SGleb Smirnoff 		/*
9092cbcd3c1SGleb Smirnoff 		 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
9102cbcd3c1SGleb Smirnoff 		 * for freeing memory.
9112cbcd3c1SGleb Smirnoff 		 */
9122cbcd3c1SGleb Smirnoff 		if (m && (flags & PRUS_NOTREADY) == 0)
9137ff0b850SAndre Oppermann 			m_freem(m);
91421367f63SSam Leffler 		error = ECONNRESET;
9159c9906e9SPeter Wemm 		goto out;
9169c9906e9SPeter Wemm 	}
917fb59c426SYoshinobu Inoue #ifdef INET6
918fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
919fb59c426SYoshinobu Inoue #endif /* INET6 */
9209c9906e9SPeter Wemm 	tp = intotcpcb(inp);
9219c9906e9SPeter Wemm 	TCPDEBUG1();
9229c9906e9SPeter Wemm 	if (control) {
9239c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
9249c9906e9SPeter Wemm 		if (control->m_len) {
9259c9906e9SPeter Wemm 			m_freem(control);
9262c37256eSGarrett Wollman 			if (m)
9272c37256eSGarrett Wollman 				m_freem(m);
928744f87eaSDavid Greenman 			error = EINVAL;
929744f87eaSDavid Greenman 			goto out;
9302c37256eSGarrett Wollman 		}
9319c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
9329c9906e9SPeter Wemm 	}
9332c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
934651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
9352c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
9362c37256eSGarrett Wollman 			/*
9372c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
9382c37256eSGarrett Wollman 			 * initialize window to default value, and
9390c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
9402c37256eSGarrett Wollman 			 */
941fb59c426SYoshinobu Inoue #ifdef INET6
942fb59c426SYoshinobu Inoue 			if (isipv6)
943b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
944fb59c426SYoshinobu Inoue #endif /* INET6 */
945b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
946b287c6c7SBjoern A. Zeeb 			else
947b287c6c7SBjoern A. Zeeb #endif
948b287c6c7SBjoern A. Zeeb #ifdef INET
949b40ce416SJulian Elischer 				error = tcp_connect(tp, nam, td);
950b287c6c7SBjoern A. Zeeb #endif
9512c37256eSGarrett Wollman 			if (error)
9522c37256eSGarrett Wollman 				goto out;
953*c560df6fSPatrick Kelsey #ifdef TCP_RFC7413
954*c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
955*c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
956*c560df6fSPatrick Kelsey 			else
957*c560df6fSPatrick Kelsey #endif
958*c560df6fSPatrick Kelsey 			{
9592c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
9602c37256eSGarrett Wollman 				tcp_mss(tp, -1);
9612c37256eSGarrett Wollman 			}
962*c560df6fSPatrick Kelsey 		}
9632c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
9642c37256eSGarrett Wollman 			/*
9652c37256eSGarrett Wollman 			 * Close the send side of the connection after
9662c37256eSGarrett Wollman 			 * the data is sent.
9672c37256eSGarrett Wollman 			 */
968ff9b006dSJulien Charbon 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
9692c37256eSGarrett Wollman 			socantsendmore(so);
970623dce13SRobert Watson 			tcp_usrclosed(tp);
9712c37256eSGarrett Wollman 		}
9722cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
9732cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
974b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
975b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
97655bceb1eSRandall Stewart 			error = tp->t_fb->tfb_tcp_output(tp);
977b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
978b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
979b0acefa8SBill Fenner 		}
9802c37256eSGarrett Wollman 	} else {
981623dce13SRobert Watson 		/*
982623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
983623dce13SRobert Watson 		 */
984d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
9852c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
986d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
9872c37256eSGarrett Wollman 			m_freem(m);
9882c37256eSGarrett Wollman 			error = ENOBUFS;
9892c37256eSGarrett Wollman 			goto out;
9902c37256eSGarrett Wollman 		}
9912c37256eSGarrett Wollman 		/*
9922c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
9932c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
9942c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
9952c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
9962c37256eSGarrett Wollman 		 * of data past the urgent section.
9972c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
9982c37256eSGarrett Wollman 		 */
999651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
1000d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
1001ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1002ef53690bSGarrett Wollman 			/*
1003ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
1004ef53690bSGarrett Wollman 			 * initialize window to default value, and
10050c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1006ef53690bSGarrett Wollman 			 */
1007*c560df6fSPatrick Kelsey #ifdef TCP_RFC7413
1008*c560df6fSPatrick Kelsey 			/*
1009*c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1010*c560df6fSPatrick Kelsey 			 */
1011*c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1012*c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1013*c560df6fSPatrick Kelsey #endif
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 	}
1036d1401c90SRobert Watson out:
1037d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
10382c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
10395d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
10405d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
10418501a69cSRobert Watson 	INP_WUNLOCK(inp);
1042fa046d87SRobert Watson 	if (flags & PRUS_EOF)
1043ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
104473fddedaSPeter Grehan 	return (error);
10452c37256eSGarrett Wollman }
10462c37256eSGarrett Wollman 
10472cbcd3c1SGleb Smirnoff static int
10482cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
10492cbcd3c1SGleb Smirnoff {
10502cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
10512cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
10522cbcd3c1SGleb Smirnoff 	int error;
10532cbcd3c1SGleb Smirnoff 
10542cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
10552cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
10562cbcd3c1SGleb Smirnoff 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
10572cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
10582cbcd3c1SGleb Smirnoff 		for (int i = 0; i < count; i++)
10592cbcd3c1SGleb Smirnoff 			m = m_free(m);
10602cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
10612cbcd3c1SGleb Smirnoff 	}
10622cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
10632cbcd3c1SGleb Smirnoff 
10642cbcd3c1SGleb Smirnoff 	SOCKBUF_LOCK(&so->so_snd);
10652cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
10662cbcd3c1SGleb Smirnoff 	SOCKBUF_UNLOCK(&so->so_snd);
10672cbcd3c1SGleb Smirnoff 	if (error == 0)
106855bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
10692cbcd3c1SGleb Smirnoff 	INP_WUNLOCK(inp);
10702cbcd3c1SGleb Smirnoff 
10712cbcd3c1SGleb Smirnoff 	return (error);
10722cbcd3c1SGleb Smirnoff }
10732cbcd3c1SGleb Smirnoff 
10742c37256eSGarrett Wollman /*
1075a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
10762c37256eSGarrett Wollman  */
1077ac45e92fSRobert Watson static void
10782c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
10792c37256eSGarrett Wollman {
1080f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1081a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
1082623dce13SRobert Watson 	TCPDEBUG0;
1083c78cbc7bSRobert Watson 
1084ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1085c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1086c78cbc7bSRobert Watson 
1087ff9b006dSJulien Charbon 	INP_INFO_RLOCK(&V_tcbinfo);
10888501a69cSRobert Watson 	INP_WLOCK(inp);
1089c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1090c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1091c78cbc7bSRobert Watson 
1092c78cbc7bSRobert Watson 	/*
1093a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1094c78cbc7bSRobert Watson 	 */
1095ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1096ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1097c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
1098a152f8a3SRobert Watson 		TCPDEBUG1();
1099c78cbc7bSRobert Watson 		tcp_drop(tp, ECONNABORTED);
1100a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
11015d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1102c78cbc7bSRobert Watson 	}
1103ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1104a152f8a3SRobert Watson 		SOCK_LOCK(so);
1105a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1106a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1107ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1108a152f8a3SRobert Watson 	}
11098501a69cSRobert Watson 	INP_WUNLOCK(inp);
1110ff9b006dSJulien Charbon 	INP_INFO_RUNLOCK(&V_tcbinfo);
1111a152f8a3SRobert Watson }
1112a152f8a3SRobert Watson 
1113a152f8a3SRobert Watson /*
1114a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1115a152f8a3SRobert Watson  */
1116a152f8a3SRobert Watson static void
1117a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1118a152f8a3SRobert Watson {
1119a152f8a3SRobert Watson 	struct inpcb *inp;
1120a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
1121a152f8a3SRobert Watson 	TCPDEBUG0;
1122a152f8a3SRobert Watson 
1123a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1124a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1125a152f8a3SRobert Watson 
1126ff9b006dSJulien Charbon 	INP_INFO_RLOCK(&V_tcbinfo);
11278501a69cSRobert Watson 	INP_WLOCK(inp);
1128a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1129a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1130a152f8a3SRobert Watson 
1131a152f8a3SRobert Watson 	/*
1132a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
1133a152f8a3SRobert Watson 	 * a disconnect.
1134a152f8a3SRobert Watson 	 */
1135ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1136ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1137a152f8a3SRobert Watson 		tp = intotcpcb(inp);
1138a152f8a3SRobert Watson 		TCPDEBUG1();
1139a152f8a3SRobert Watson 		tcp_disconnect(tp);
1140a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
11415d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1142a152f8a3SRobert Watson 	}
1143ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1144a152f8a3SRobert Watson 		SOCK_LOCK(so);
1145a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1146a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1147ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1148a152f8a3SRobert Watson 	}
11498501a69cSRobert Watson 	INP_WUNLOCK(inp);
1150ff9b006dSJulien Charbon 	INP_INFO_RUNLOCK(&V_tcbinfo);
11512c37256eSGarrett Wollman }
11522c37256eSGarrett Wollman 
11532c37256eSGarrett Wollman /*
11542c37256eSGarrett Wollman  * Receive out-of-band data.
11552c37256eSGarrett Wollman  */
11562c37256eSGarrett Wollman static int
11572c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
11582c37256eSGarrett Wollman {
11592c37256eSGarrett Wollman 	int error = 0;
1160f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1161623dce13SRobert Watson 	struct tcpcb *tp = NULL;
11622c37256eSGarrett Wollman 
1163623dce13SRobert Watson 	TCPDEBUG0;
1164623dce13SRobert Watson 	inp = sotoinpcb(so);
1165623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
11668501a69cSRobert Watson 	INP_WLOCK(inp);
1167ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
116821367f63SSam Leffler 		error = ECONNRESET;
1169623dce13SRobert Watson 		goto out;
1170623dce13SRobert Watson 	}
1171623dce13SRobert Watson 	tp = intotcpcb(inp);
1172623dce13SRobert Watson 	TCPDEBUG1();
11732c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1174c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
11754cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
11764cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
11772c37256eSGarrett Wollman 		error = EINVAL;
11782c37256eSGarrett Wollman 		goto out;
11792c37256eSGarrett Wollman 	}
11802c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
11812c37256eSGarrett Wollman 		error = EWOULDBLOCK;
11822c37256eSGarrett Wollman 		goto out;
11832c37256eSGarrett Wollman 	}
11842c37256eSGarrett Wollman 	m->m_len = 1;
11852c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
11862c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
11872c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1188623dce13SRobert Watson 
1189623dce13SRobert Watson out:
1190623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
11915d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
11928501a69cSRobert Watson 	INP_WUNLOCK(inp);
1193623dce13SRobert Watson 	return (error);
11942c37256eSGarrett Wollman }
11952c37256eSGarrett Wollman 
1196b287c6c7SBjoern A. Zeeb #ifdef INET
11972c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1198756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1199756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1200756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1201756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1202756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1203756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1204756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1205756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1206756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
120754d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1208756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1209756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1210756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
12112cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1212756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
121354d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1214a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1215a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
12162c37256eSGarrett Wollman };
1217b287c6c7SBjoern A. Zeeb #endif /* INET */
1218df8bae1dSRodney W. Grimes 
1219fb59c426SYoshinobu Inoue #ifdef INET6
1220fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1221756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1222756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1223756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1224756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1225756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1226756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1227756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1228756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1229756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1230756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1231756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1232756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1233756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
12342cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1235756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1236756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1237a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1238a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1239fb59c426SYoshinobu Inoue };
1240fb59c426SYoshinobu Inoue #endif /* INET6 */
1241fb59c426SYoshinobu Inoue 
1242b287c6c7SBjoern A. Zeeb #ifdef INET
1243a0292f23SGarrett Wollman /*
1244a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1245a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
12465200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
12475200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
12485200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
12495200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1250a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1251a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1252a0292f23SGarrett Wollman  */
12530312fbe9SPoul-Henning Kamp static int
1254ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1255a0292f23SGarrett Wollman {
1256a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1257a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
12585200e00eSIan Dowse 	struct in_addr laddr;
12595200e00eSIan Dowse 	u_short lport;
1260c3229e05SDavid Greenman 	int error;
1261a0292f23SGarrett Wollman 
12628501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1263fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1264623dce13SRobert Watson 
1265a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
12664616026fSErmal Luçi 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
12674616026fSErmal Luçi 		if (error)
1268fa046d87SRobert Watson 			goto out;
1269a0292f23SGarrett Wollman 	}
1270a0292f23SGarrett Wollman 
1271a0292f23SGarrett Wollman 	/*
1272a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1273a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1274a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1275a0292f23SGarrett Wollman 	 */
12765200e00eSIan Dowse 	laddr = inp->inp_laddr;
12775200e00eSIan Dowse 	lport = inp->inp_lport;
12785200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1279b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
12805200e00eSIan Dowse 	if (error && oinp == NULL)
1281fa046d87SRobert Watson 		goto out;
1282fa046d87SRobert Watson 	if (oinp) {
1283fa046d87SRobert Watson 		error = EADDRINUSE;
1284fa046d87SRobert Watson 		goto out;
1285fa046d87SRobert Watson 	}
12865200e00eSIan Dowse 	inp->inp_laddr = laddr;
128715bd2b43SDavid Greenman 	in_pcbrehash(inp);
1288fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1289a0292f23SGarrett Wollman 
1290087b55eaSAndre Oppermann 	/*
1291087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1292087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1293087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1294087b55eaSAndre Oppermann 	 */
1295a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
12969b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1297a0292f23SGarrett Wollman 		tp->request_r_scale++;
1298a0292f23SGarrett Wollman 
1299a0292f23SGarrett Wollman 	soisconnecting(so);
130078b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
130157f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
1302b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
1303a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1304a45d2726SAndras Olah 
1305a0292f23SGarrett Wollman 	return 0;
1306fa046d87SRobert Watson 
1307fa046d87SRobert Watson out:
1308fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1309fa046d87SRobert Watson 	return (error);
1310a0292f23SGarrett Wollman }
1311b287c6c7SBjoern A. Zeeb #endif /* INET */
1312a0292f23SGarrett Wollman 
1313fb59c426SYoshinobu Inoue #ifdef INET6
1314fb59c426SYoshinobu Inoue static int
1315ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1316fb59c426SYoshinobu Inoue {
1317a7e201bbSAndrey V. Elsukov 	struct inpcb *inp = tp->t_inpcb;
1318fb59c426SYoshinobu Inoue 	int error;
1319fb59c426SYoshinobu Inoue 
13208501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1321fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1322623dce13SRobert Watson 
1323fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
13244616026fSErmal Luçi 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
13254616026fSErmal Luçi 		if (error)
1326fa046d87SRobert Watson 			goto out;
1327fb59c426SYoshinobu Inoue 	}
1328a7e201bbSAndrey V. Elsukov 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1329a7e201bbSAndrey V. Elsukov 	if (error != 0)
1330b598155aSRobert Watson 		goto out;
1331fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1332fb59c426SYoshinobu Inoue 
1333fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1334fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1335970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1336fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1337fb59c426SYoshinobu Inoue 
1338a7e201bbSAndrey V. Elsukov 	soisconnecting(inp->inp_socket);
133978b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
134057f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
1341b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
1342fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1343fb59c426SYoshinobu Inoue 
1344fb59c426SYoshinobu Inoue 	return 0;
1345fa046d87SRobert Watson 
1346fa046d87SRobert Watson out:
1347fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1348fa046d87SRobert Watson 	return error;
1349fb59c426SYoshinobu Inoue }
1350fb59c426SYoshinobu Inoue #endif /* INET6 */
1351fb59c426SYoshinobu Inoue 
1352cfe8b629SGarrett Wollman /*
1353b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1354b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1355b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1356b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1357b8af5dfaSRobert Watson  * from Linux.
1358b8af5dfaSRobert Watson  */
1359b8af5dfaSRobert Watson static void
1360ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1361b8af5dfaSRobert Watson {
1362b8af5dfaSRobert Watson 
13638501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1364b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1365b8af5dfaSRobert Watson 
1366b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1367b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1368b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
13693529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1370b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1371b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1372b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1373b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1374b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1375b8af5dfaSRobert Watson 	}
13765a17b6adSMichael Tuexen 	if (tp->t_flags & TF_ECN_PERMIT)
13775a17b6adSMichael Tuexen 		ti->tcpi_options |= TCPI_OPT_ECN;
13781baaf834SBruce M Simpson 
137943d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
13803ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
13811baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
13821baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
13831baaf834SBruce M Simpson 
1384b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1385b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1386b8af5dfaSRobert Watson 
1387b8af5dfaSRobert Watson 	/*
1388b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1389b8af5dfaSRobert Watson 	 */
1390c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1391535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1392b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
13931c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1394535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
139543d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
139643d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1397535fbad6SKip Macy 	if (tp->t_flags & TF_TOE)
1398535fbad6SKip Macy 		ti->tcpi_options |= TCPI_OPT_TOE;
1399f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1400f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1401f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1402b8af5dfaSRobert Watson }
1403b8af5dfaSRobert Watson 
1404b8af5dfaSRobert Watson /*
14051e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
14061e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
14071e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
14081e8f5ffaSRobert Watson  * option.
1409cfe8b629SGarrett Wollman  */
1410bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
14118501a69cSRobert Watson 	INP_WLOCK(inp);							\
1412ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
14138501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1414bac5bedfSConrad Meyer 		cleanup;						\
14151e8f5ffaSRobert Watson 		return (ECONNRESET);					\
14161e8f5ffaSRobert Watson 	}								\
14171e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
14181e8f5ffaSRobert Watson } while(0)
1419bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
14201e8f5ffaSRobert Watson 
1421df8bae1dSRodney W. Grimes int
1422ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1423df8bae1dSRodney W. Grimes {
142455bceb1eSRandall Stewart 	int	error;
1425df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1426cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
142755bceb1eSRandall Stewart 	struct tcp_function_block *blk;
142855bceb1eSRandall Stewart 	struct tcp_function_set fsn;
1429df8bae1dSRodney W. Grimes 
1430cfe8b629SGarrett Wollman 	error = 0;
1431df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1432623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
14338501a69cSRobert Watson 	INP_WLOCK(inp);
1434cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1435fb59c426SYoshinobu Inoue #ifdef INET6
14365cd54324SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6PROTO) {
14378501a69cSRobert Watson 			INP_WUNLOCK(inp);
1438fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
1439b287c6c7SBjoern A. Zeeb 		}
1440fb59c426SYoshinobu Inoue #endif /* INET6 */
1441b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1442b287c6c7SBjoern A. Zeeb 		else
1443b287c6c7SBjoern A. Zeeb #endif
1444b287c6c7SBjoern A. Zeeb #ifdef INET
1445b287c6c7SBjoern A. Zeeb 		{
14468501a69cSRobert Watson 			INP_WUNLOCK(inp);
1447cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
14481e8f5ffaSRobert Watson 		}
14491e8f5ffaSRobert Watson #endif
1450df8bae1dSRodney W. Grimes 		return (error);
1451df8bae1dSRodney W. Grimes 	}
1452ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
14538501a69cSRobert Watson 		INP_WUNLOCK(inp);
14541e8f5ffaSRobert Watson 		return (ECONNRESET);
1455623dce13SRobert Watson 	}
145655bceb1eSRandall Stewart 	tp = intotcpcb(inp);
145755bceb1eSRandall Stewart 	/*
145855bceb1eSRandall Stewart 	 * Protect the TCP option TCP_FUNCTION_BLK so
145955bceb1eSRandall Stewart 	 * that a sub-function can *never* overwrite this.
146055bceb1eSRandall Stewart 	 */
146155bceb1eSRandall Stewart 	if ((sopt->sopt_dir == SOPT_SET) &&
146255bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
146355bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
146455bceb1eSRandall Stewart 		error = sooptcopyin(sopt, &fsn, sizeof fsn,
146555bceb1eSRandall Stewart 		    sizeof fsn);
146655bceb1eSRandall Stewart 		if (error)
146755bceb1eSRandall Stewart 			return (error);
146855bceb1eSRandall Stewart 		INP_WLOCK_RECHECK(inp);
146955bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
147055bceb1eSRandall Stewart 		if (blk == NULL) {
147155bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
147255bceb1eSRandall Stewart 			return (ENOENT);
147355bceb1eSRandall Stewart 		}
1474587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1475587d67c0SRandall Stewart 			/* You already have this */
1476587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1477587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1478587d67c0SRandall Stewart 			return (0);
1479587d67c0SRandall Stewart 		}
1480587d67c0SRandall Stewart 		if (tp->t_state != TCPS_CLOSED) {
1481587d67c0SRandall Stewart 			int error=EINVAL;
1482587d67c0SRandall Stewart 			/*
1483587d67c0SRandall Stewart 			 * The user has advanced the state
1484587d67c0SRandall Stewart 			 * past the initial point, we may not
1485587d67c0SRandall Stewart 			 * be able to switch.
1486587d67c0SRandall Stewart 			 */
1487587d67c0SRandall Stewart 			if (blk->tfb_tcp_handoff_ok != NULL) {
1488587d67c0SRandall Stewart 				/*
1489587d67c0SRandall Stewart 				 * Does the stack provide a
1490587d67c0SRandall Stewart 				 * query mechanism, if so it may
1491587d67c0SRandall Stewart 				 * still be possible?
1492587d67c0SRandall Stewart 				 */
1493587d67c0SRandall Stewart 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1494587d67c0SRandall Stewart 			}
1495587d67c0SRandall Stewart 			if (error) {
1496587d67c0SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1497587d67c0SRandall Stewart 				INP_WUNLOCK(inp);
1498587d67c0SRandall Stewart 				return(error);
1499587d67c0SRandall Stewart 			}
1500587d67c0SRandall Stewart 		}
150155bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
150255bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
150355bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
150455bceb1eSRandall Stewart 			return (ENOENT);
150555bceb1eSRandall Stewart 		}
150655bceb1eSRandall Stewart 		/*
150755bceb1eSRandall Stewart 		 * Release the old refcnt, the
1508587d67c0SRandall Stewart 		 * lookup acquired a ref on the
1509587d67c0SRandall Stewart 		 * new one already.
151055bceb1eSRandall Stewart 		 */
1511587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1512587d67c0SRandall Stewart 			/*
1513587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1514587d67c0SRandall Stewart 			 * the tcb is not going away.
1515587d67c0SRandall Stewart 			 */
1516587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1517587d67c0SRandall Stewart 		}
151855bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
151955bceb1eSRandall Stewart 		tp->t_fb = blk;
152055bceb1eSRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_init) {
152155bceb1eSRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_init)(tp);
152255bceb1eSRandall Stewart 		}
152355bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
152455bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
152555bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
152655bceb1eSRandall Stewart 			     sopt->sopt_name);
152755bceb1eSRandall Stewart 		}
152855bceb1eSRandall Stewart #endif
152955bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
153055bceb1eSRandall Stewart 		return (error);
153155bceb1eSRandall Stewart 	} else if ((sopt->sopt_dir == SOPT_GET) &&
153255bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
153355bceb1eSRandall Stewart 		strcpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name);
153455bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
153555bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
153655bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
153755bceb1eSRandall Stewart 		return (error);
153855bceb1eSRandall Stewart 	}
153955bceb1eSRandall Stewart 	/* Pass in the INP locked, called must unlock it */
154055bceb1eSRandall Stewart 	return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
154155bceb1eSRandall Stewart }
154255bceb1eSRandall Stewart 
154355bceb1eSRandall Stewart int
154455bceb1eSRandall Stewart tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
154555bceb1eSRandall Stewart {
154655bceb1eSRandall Stewart 	int	error, opt, optval;
154755bceb1eSRandall Stewart 	u_int	ui;
154855bceb1eSRandall Stewart 	struct	tcp_info ti;
154955bceb1eSRandall Stewart 	struct cc_algo *algo;
1550af6fef3aSGleb Smirnoff 	char	*pbuf, buf[TCP_CA_NAME_MAX];
1551af6fef3aSGleb Smirnoff 	size_t	len;
1552df8bae1dSRodney W. Grimes 
1553d519cedbSGleb Smirnoff 	/*
1554d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
1555d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
1556d519cedbSGleb Smirnoff 	 */
1557d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
1558d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
1559d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1560af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1561af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1562d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
1563d519cedbSGleb Smirnoff 		if (error) {
1564af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
1565d519cedbSGleb Smirnoff 			return (error);
1566d519cedbSGleb Smirnoff 		}
1567bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1568d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
1569af6fef3aSGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1570d519cedbSGleb Smirnoff 		else
1571d519cedbSGleb Smirnoff 			error = ENOENT;
1572d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1573d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
1574af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1575af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
1576d519cedbSGleb Smirnoff 		return (error);
1577d519cedbSGleb Smirnoff 	}
1578d519cedbSGleb Smirnoff 
1579cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1580cfe8b629SGarrett Wollman 	case SOPT_SET:
1581cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1582fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
158388f6b043SBruce M Simpson 		case TCP_MD5SIG:
1584fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
15858501a69cSRobert Watson 				INP_WUNLOCK(inp);
1586fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
1587fcf59617SAndrey V. Elsukov 			}
1588fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
15891cfd4b53SBruce M Simpson 			if (error)
15901e8f5ffaSRobert Watson 				return (error);
159109fe6320SNavdeep Parhar 			goto unlock_and_done;
1592fcf59617SAndrey V. Elsukov #endif /* IPSEC */
159309fe6320SNavdeep Parhar 
1594df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1595cfe8b629SGarrett Wollman 		case TCP_NOOPT:
15968501a69cSRobert Watson 			INP_WUNLOCK(inp);
1597cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1598cfe8b629SGarrett Wollman 			    sizeof optval);
1599cfe8b629SGarrett Wollman 			if (error)
16001e8f5ffaSRobert Watson 				return (error);
1601cfe8b629SGarrett Wollman 
16028501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1603cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1604cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1605cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1606cfe8b629SGarrett Wollman 				break;
1607cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1608cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1609cfe8b629SGarrett Wollman 				break;
1610cfe8b629SGarrett Wollman 			default:
1611cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1612cfe8b629SGarrett Wollman 				break;
1613cfe8b629SGarrett Wollman 			}
1614cfe8b629SGarrett Wollman 
1615cfe8b629SGarrett Wollman 			if (optval)
1616cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1617df8bae1dSRodney W. Grimes 			else
1618cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
161909fe6320SNavdeep Parhar unlock_and_done:
162009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
162109fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
162209fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
162309fe6320SNavdeep Parhar 				    sopt->sopt_name);
162409fe6320SNavdeep Parhar 			}
162509fe6320SNavdeep Parhar #endif
16268501a69cSRobert Watson 			INP_WUNLOCK(inp);
1627df8bae1dSRodney W. Grimes 			break;
1628df8bae1dSRodney W. Grimes 
1629007581c0SJonathan Lemon 		case TCP_NOPUSH:
16308501a69cSRobert Watson 			INP_WUNLOCK(inp);
1631007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1632007581c0SJonathan Lemon 			    sizeof optval);
1633007581c0SJonathan Lemon 			if (error)
16341e8f5ffaSRobert Watson 				return (error);
1635007581c0SJonathan Lemon 
16368501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1637007581c0SJonathan Lemon 			if (optval)
1638007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1639d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
1640007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1641d28b9e89SJohn Baldwin 				if (TCPS_HAVEESTABLISHED(tp->t_state))
164255bceb1eSRandall Stewart 					error = tp->t_fb->tfb_tcp_output(tp);
1643007581c0SJonathan Lemon 			}
164409fe6320SNavdeep Parhar 			goto unlock_and_done;
1645007581c0SJonathan Lemon 
1646df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
16478501a69cSRobert Watson 			INP_WUNLOCK(inp);
1648cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1649cfe8b629SGarrett Wollman 			    sizeof optval);
1650cfe8b629SGarrett Wollman 			if (error)
16511e8f5ffaSRobert Watson 				return (error);
1652df8bae1dSRodney W. Grimes 
16538501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
165453369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
1655603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
1656cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1657a0292f23SGarrett Wollman 			else
1658a0292f23SGarrett Wollman 				error = EINVAL;
165909fe6320SNavdeep Parhar 			goto unlock_and_done;
1660a0292f23SGarrett Wollman 
1661b8af5dfaSRobert Watson 		case TCP_INFO:
16628501a69cSRobert Watson 			INP_WUNLOCK(inp);
1663b8af5dfaSRobert Watson 			error = EINVAL;
1664b8af5dfaSRobert Watson 			break;
1665b8af5dfaSRobert Watson 
1666dbc42409SLawrence Stewart 		case TCP_CONGESTION:
1667dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
1668af6fef3aSGleb Smirnoff 			error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1669af6fef3aSGleb Smirnoff 			if (error)
1670dbc42409SLawrence Stewart 				break;
1671af6fef3aSGleb Smirnoff 			buf[sopt->sopt_valsize] = '\0';
1672af6fef3aSGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
167373e263b1SGleb Smirnoff 			CC_LIST_RLOCK();
167473e263b1SGleb Smirnoff 			STAILQ_FOREACH(algo, &cc_list, entries)
167573e263b1SGleb Smirnoff 				if (strncmp(buf, algo->name,
167673e263b1SGleb Smirnoff 				    TCP_CA_NAME_MAX) == 0)
167773e263b1SGleb Smirnoff 					break;
167873e263b1SGleb Smirnoff 			CC_LIST_RUNLOCK();
167973e263b1SGleb Smirnoff 			if (algo == NULL) {
1680af6fef3aSGleb Smirnoff 				INP_WUNLOCK(inp);
168173e263b1SGleb Smirnoff 				error = EINVAL;
168273e263b1SGleb Smirnoff 				break;
168373e263b1SGleb Smirnoff 			}
1684dbc42409SLawrence Stewart 			/*
168573e263b1SGleb Smirnoff 			 * We hold a write lock over the tcb so it's safe to
168673e263b1SGleb Smirnoff 			 * do these things without ordering concerns.
1687dbc42409SLawrence Stewart 			 */
1688dbc42409SLawrence Stewart 			if (CC_ALGO(tp)->cb_destroy != NULL)
1689dbc42409SLawrence Stewart 				CC_ALGO(tp)->cb_destroy(tp->ccv);
1690dbc42409SLawrence Stewart 			CC_ALGO(tp) = algo;
1691dbc42409SLawrence Stewart 			/*
169273e263b1SGleb Smirnoff 			 * If something goes pear shaped initialising the new
169373e263b1SGleb Smirnoff 			 * algo, fall back to newreno (which does not
169473e263b1SGleb Smirnoff 			 * require initialisation).
1695dbc42409SLawrence Stewart 			 */
169673e263b1SGleb Smirnoff 			if (algo->cb_init != NULL &&
169773e263b1SGleb Smirnoff 			    algo->cb_init(tp->ccv) != 0) {
1698dbc42409SLawrence Stewart 				CC_ALGO(tp) = &newreno_cc_algo;
1699dbc42409SLawrence Stewart 				/*
170073e263b1SGleb Smirnoff 				 * The only reason init should fail is
1701dbc42409SLawrence Stewart 				 * because of malloc.
1702dbc42409SLawrence Stewart 				 */
1703dbc42409SLawrence Stewart 				error = ENOMEM;
1704dbc42409SLawrence Stewart 			}
170573e263b1SGleb Smirnoff 			INP_WUNLOCK(inp);
170673e263b1SGleb Smirnoff 			break;
1707dbc42409SLawrence Stewart 
17089077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
17099077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
17109077f387SGleb Smirnoff 		case TCP_KEEPINIT:
17119077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
17129077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
17139077f387SGleb Smirnoff 			if (error)
17149077f387SGleb Smirnoff 				return (error);
17159077f387SGleb Smirnoff 
17169077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
17179077f387SGleb Smirnoff 				error = EINVAL;
17189077f387SGleb Smirnoff 				break;
17199077f387SGleb Smirnoff 			}
17209077f387SGleb Smirnoff 			ui *= hz;
17219077f387SGleb Smirnoff 
17229077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
17239077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
17249077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
17259077f387SGleb Smirnoff 				tp->t_keepidle = ui;
17269077f387SGleb Smirnoff 				/*
17279077f387SGleb Smirnoff 				 * XXX: better check current remaining
17289077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
17299077f387SGleb Smirnoff 				 */
17309077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
17319077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
17329077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
17339077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
17349077f387SGleb Smirnoff 				break;
17359077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
17369077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
17379077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
17389077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
17399077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
17409077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
17419077f387SGleb Smirnoff 				break;
17429077f387SGleb Smirnoff 			case TCP_KEEPINIT:
17439077f387SGleb Smirnoff 				tp->t_keepinit = ui;
17449077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
17459077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
17469077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
17479077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
17489077f387SGleb Smirnoff 				break;
17499077f387SGleb Smirnoff 			}
175009fe6320SNavdeep Parhar 			goto unlock_and_done;
17519077f387SGleb Smirnoff 
175285c05144SGleb Smirnoff 		case TCP_KEEPCNT:
175385c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
175485c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
175585c05144SGleb Smirnoff 			if (error)
175685c05144SGleb Smirnoff 				return (error);
175785c05144SGleb Smirnoff 
175885c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
175985c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
176085c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
176185c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
176285c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
176385c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
176485c05144SGleb Smirnoff 			goto unlock_and_done;
176585c05144SGleb Smirnoff 
176686a996e6SHiren Panchasara #ifdef TCPPCAP
176786a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
176886a996e6SHiren Panchasara 		case TCP_PCAP_IN:
176986a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
177086a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
177186a996e6SHiren Panchasara 			    sizeof optval);
177286a996e6SHiren Panchasara 			if (error)
177386a996e6SHiren Panchasara 				return (error);
177486a996e6SHiren Panchasara 
177586a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
177686a996e6SHiren Panchasara 			if (optval >= 0)
177786a996e6SHiren Panchasara 				tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
177886a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
177986a996e6SHiren Panchasara 					optval);
178086a996e6SHiren Panchasara 			else
178186a996e6SHiren Panchasara 				error = EINVAL;
178286a996e6SHiren Panchasara 			goto unlock_and_done;
178386a996e6SHiren Panchasara #endif
178486a996e6SHiren Panchasara 
1785281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
1786*c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
1787*c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
1788*c560df6fSPatrick Kelsey 
1789281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
1790*c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
1791*c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
1792281a0fd4SPatrick Kelsey 				return (EPERM);
1793281a0fd4SPatrick Kelsey 
1794*c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
1795*c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
1796281a0fd4SPatrick Kelsey 			if (error)
1797281a0fd4SPatrick Kelsey 				return (error);
1798281a0fd4SPatrick Kelsey 
1799281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
1800*c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
1801*c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
1802*c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
1803*c560df6fSPatrick Kelsey 						error = EPERM;
1804*c560df6fSPatrick Kelsey 						goto unlock_and_done;
1805*c560df6fSPatrick Kelsey 					}
1806*c560df6fSPatrick Kelsey 
1807281a0fd4SPatrick Kelsey 					tp->t_flags |= TF_FASTOPEN;
1808*c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
1809281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
1810281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
1811*c560df6fSPatrick Kelsey 				} else {
1812*c560df6fSPatrick Kelsey 					/*
1813*c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
1814*c560df6fSPatrick Kelsey 					 * stash it in the client cookie
1815*c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
1816*c560df6fSPatrick Kelsey 					 * connect.
1817*c560df6fSPatrick Kelsey 					 */
1818*c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
1819*c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
1820*c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
1821*c560df6fSPatrick Kelsey 						       tfo_optval.psk,
1822*c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
1823*c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
1824*c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
1825*c560df6fSPatrick Kelsey 					}
1826*c560df6fSPatrick Kelsey 					tp->t_flags |= TF_FASTOPEN;
1827*c560df6fSPatrick Kelsey 				}
1828281a0fd4SPatrick Kelsey 			} else
1829281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1830281a0fd4SPatrick Kelsey 			goto unlock_and_done;
1831*c560df6fSPatrick Kelsey 		}
1832281a0fd4SPatrick Kelsey #endif
1833281a0fd4SPatrick Kelsey 
1834df8bae1dSRodney W. Grimes 		default:
18358501a69cSRobert Watson 			INP_WUNLOCK(inp);
1836df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1837df8bae1dSRodney W. Grimes 			break;
1838df8bae1dSRodney W. Grimes 		}
1839df8bae1dSRodney W. Grimes 		break;
1840df8bae1dSRodney W. Grimes 
1841cfe8b629SGarrett Wollman 	case SOPT_GET:
18421e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
1843cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1844fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
184588f6b043SBruce M Simpson 		case TCP_MD5SIG:
1846fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
18478501a69cSRobert Watson 				INP_WUNLOCK(inp);
1848fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
1849fcf59617SAndrey V. Elsukov 			}
1850fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
18511cfd4b53SBruce M Simpson 			break;
1852265ed012SBruce M Simpson #endif
18531e8f5ffaSRobert Watson 
1854df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1855cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
18568501a69cSRobert Watson 			INP_WUNLOCK(inp);
1857b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1858df8bae1dSRodney W. Grimes 			break;
1859df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1860cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
18618501a69cSRobert Watson 			INP_WUNLOCK(inp);
1862b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1863df8bae1dSRodney W. Grimes 			break;
1864a0292f23SGarrett Wollman 		case TCP_NOOPT:
1865cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
18668501a69cSRobert Watson 			INP_WUNLOCK(inp);
1867b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1868a0292f23SGarrett Wollman 			break;
1869a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1870cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
18718501a69cSRobert Watson 			INP_WUNLOCK(inp);
1872b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1873b8af5dfaSRobert Watson 			break;
1874b8af5dfaSRobert Watson 		case TCP_INFO:
1875b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
18768501a69cSRobert Watson 			INP_WUNLOCK(inp);
1877b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
1878a0292f23SGarrett Wollman 			break;
1879dbc42409SLawrence Stewart 		case TCP_CONGESTION:
1880af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1881dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
1882af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
1883dbc42409SLawrence Stewart 			break;
18842f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
18852f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
18862f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
18872f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
18882f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
18892f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
18905a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
18912f3eb7f4SGleb Smirnoff 				break;
18922f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
18935a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
18942f3eb7f4SGleb Smirnoff 				break;
18952f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
18965a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
18972f3eb7f4SGleb Smirnoff 				break;
18982f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
18995a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
19002f3eb7f4SGleb Smirnoff 				break;
19012f3eb7f4SGleb Smirnoff 			}
19022f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
19032f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
19042f3eb7f4SGleb Smirnoff 			break;
190586a996e6SHiren Panchasara #ifdef TCPPCAP
190686a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
190786a996e6SHiren Panchasara 		case TCP_PCAP_IN:
190886a996e6SHiren Panchasara 			optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
190986a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
191086a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
191186a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
191286a996e6SHiren Panchasara 			break;
191386a996e6SHiren Panchasara #endif
1914281a0fd4SPatrick Kelsey 
1915281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
1916281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
1917281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
1918281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
1919281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
1920281a0fd4SPatrick Kelsey 			break;
1921281a0fd4SPatrick Kelsey #endif
1922df8bae1dSRodney W. Grimes 		default:
19238501a69cSRobert Watson 			INP_WUNLOCK(inp);
1924df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1925df8bae1dSRodney W. Grimes 			break;
1926df8bae1dSRodney W. Grimes 		}
1927df8bae1dSRodney W. Grimes 		break;
1928df8bae1dSRodney W. Grimes 	}
1929df8bae1dSRodney W. Grimes 	return (error);
1930df8bae1dSRodney W. Grimes }
19318501a69cSRobert Watson #undef INP_WLOCK_RECHECK
1932bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
1933df8bae1dSRodney W. Grimes 
193426e30fbbSDavid Greenman /*
1935df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1936df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1937df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1938df8bae1dSRodney W. Grimes  */
19390312fbe9SPoul-Henning Kamp static int
1940ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so)
1941df8bae1dSRodney W. Grimes {
1942ad3f9ab3SAndre Oppermann 	struct tcpcb *tp;
1943df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1944df8bae1dSRodney W. Grimes 	int error;
1945df8bae1dSRodney W. Grimes 
1946df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1947e233e2acSAndre Oppermann 		error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1948df8bae1dSRodney W. Grimes 		if (error)
1949df8bae1dSRodney W. Grimes 			return (error);
1950df8bae1dSRodney W. Grimes 	}
19516741ecf5SAndre Oppermann 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
19526741ecf5SAndre Oppermann 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1953ff9b006dSJulien Charbon 	INP_INFO_RLOCK(&V_tcbinfo);
1954603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_tcbinfo);
1955f2de87feSRobert Watson 	if (error) {
1956ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
1957df8bae1dSRodney W. Grimes 		return (error);
1958f2de87feSRobert Watson 	}
1959df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1960fb59c426SYoshinobu Inoue #ifdef INET6
19615cd54324SBjoern A. Zeeb 	if (inp->inp_vflag & INP_IPV6PROTO) {
1962fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
196363ec505aSMichael Tuexen 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
196463ec505aSMichael Tuexen 			inp->inp_vflag |= INP_IPV4;
1965fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1966fb59c426SYoshinobu Inoue 	}
1967fb59c426SYoshinobu Inoue 	else
1968fb59c426SYoshinobu Inoue #endif
1969cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1970df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1971623dce13SRobert Watson 	if (tp == NULL) {
1972df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
19730206cdb8SBjoern A. Zeeb 		in_pcbfree(inp);
1974ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
1975df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1976df8bae1dSRodney W. Grimes 	}
1977df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
19788501a69cSRobert Watson 	INP_WUNLOCK(inp);
1979ff9b006dSJulien Charbon 	INP_INFO_RUNLOCK(&V_tcbinfo);
1980bf840a17SGleb Smirnoff 	TCPSTATES_INC(TCPS_CLOSED);
1981df8bae1dSRodney W. Grimes 	return (0);
1982df8bae1dSRodney W. Grimes }
1983df8bae1dSRodney W. Grimes 
1984df8bae1dSRodney W. Grimes /*
1985df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1986df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1987df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1988df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1989df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1990df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1991df8bae1dSRodney W. Grimes  */
1992623dce13SRobert Watson static void
1993ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
1994df8bae1dSRodney W. Grimes {
1995e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
1996e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
1997e6e0b5ffSRobert Watson 
1998ff9b006dSJulien Charbon 	INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
19998501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2000df8bae1dSRodney W. Grimes 
2001623dce13SRobert Watson 	/*
2002623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2003623dce13SRobert Watson 	 * socket is still open.
2004623dce13SRobert Watson 	 */
2005623dce13SRobert Watson 	if (tp->t_state < TCPS_ESTABLISHED) {
2006df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2007623dce13SRobert Watson 		KASSERT(tp != NULL,
2008623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2009623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2010243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2011623dce13SRobert Watson 		KASSERT(tp != NULL,
2012623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2013623dce13SRobert Watson 	} else {
2014df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2015df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2016623dce13SRobert Watson 		tcp_usrclosed(tp);
2017ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
201855bceb1eSRandall Stewart 			tp->t_fb->tfb_tcp_output(tp);
2019df8bae1dSRodney W. Grimes 	}
2020df8bae1dSRodney W. Grimes }
2021df8bae1dSRodney W. Grimes 
2022df8bae1dSRodney W. Grimes /*
2023df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2024df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2025df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2026df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2027df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2028df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2029df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2030df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2031df8bae1dSRodney W. Grimes  */
2032623dce13SRobert Watson static void
2033ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2034df8bae1dSRodney W. Grimes {
2035df8bae1dSRodney W. Grimes 
2036ff9b006dSJulien Charbon 	INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
20378501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
2038e6e0b5ffSRobert Watson 
2039df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2040df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
204109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
204209fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
204309fe6320SNavdeep Parhar #endif
2044550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2045bc65987aSKip Macy 		/* FALLTHROUGH */
2046bc65987aSKip Macy 	case TCPS_CLOSED:
2047df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2048623dce13SRobert Watson 		/*
2049623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2050623dce13SRobert Watson 		 * still open.
2051623dce13SRobert Watson 		 */
2052623dce13SRobert Watson 		KASSERT(tp != NULL,
2053623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2054df8bae1dSRodney W. Grimes 		break;
2055df8bae1dSRodney W. Grimes 
2056a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2057df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2058a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2059a0292f23SGarrett Wollman 		break;
2060a0292f23SGarrett Wollman 
2061df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
206257f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2063df8bae1dSRodney W. Grimes 		break;
2064df8bae1dSRodney W. Grimes 
2065df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
206657f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2067df8bae1dSRodney W. Grimes 		break;
2068df8bae1dSRodney W. Grimes 	}
2069abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
2070df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
2071abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
20727c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
20737c72af87SMohan Srinivasan 			int timeout;
20747c72af87SMohan Srinivasan 
20757c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
20769077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2077b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2078b6239c4aSAndras Olah 		}
2079df8bae1dSRodney W. Grimes 	}
20807c72af87SMohan Srinivasan }
2081497057eeSRobert Watson 
2082497057eeSRobert Watson #ifdef DDB
2083497057eeSRobert Watson static void
2084497057eeSRobert Watson db_print_indent(int indent)
2085497057eeSRobert Watson {
2086497057eeSRobert Watson 	int i;
2087497057eeSRobert Watson 
2088497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2089497057eeSRobert Watson 		db_printf(" ");
2090497057eeSRobert Watson }
2091497057eeSRobert Watson 
2092497057eeSRobert Watson static void
2093497057eeSRobert Watson db_print_tstate(int t_state)
2094497057eeSRobert Watson {
2095497057eeSRobert Watson 
2096497057eeSRobert Watson 	switch (t_state) {
2097497057eeSRobert Watson 	case TCPS_CLOSED:
2098497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2099497057eeSRobert Watson 		return;
2100497057eeSRobert Watson 
2101497057eeSRobert Watson 	case TCPS_LISTEN:
2102497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2103497057eeSRobert Watson 		return;
2104497057eeSRobert Watson 
2105497057eeSRobert Watson 	case TCPS_SYN_SENT:
2106497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2107497057eeSRobert Watson 		return;
2108497057eeSRobert Watson 
2109497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2110497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2111497057eeSRobert Watson 		return;
2112497057eeSRobert Watson 
2113497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2114497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2115497057eeSRobert Watson 		return;
2116497057eeSRobert Watson 
2117497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2118497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2119497057eeSRobert Watson 		return;
2120497057eeSRobert Watson 
2121497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2122497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2123497057eeSRobert Watson 		return;
2124497057eeSRobert Watson 
2125497057eeSRobert Watson 	case TCPS_CLOSING:
2126497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2127497057eeSRobert Watson 		return;
2128497057eeSRobert Watson 
2129497057eeSRobert Watson 	case TCPS_LAST_ACK:
2130497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2131497057eeSRobert Watson 		return;
2132497057eeSRobert Watson 
2133497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2134497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2135497057eeSRobert Watson 		return;
2136497057eeSRobert Watson 
2137497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2138497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2139497057eeSRobert Watson 		return;
2140497057eeSRobert Watson 
2141497057eeSRobert Watson 	default:
2142497057eeSRobert Watson 		db_printf("unknown");
2143497057eeSRobert Watson 		return;
2144497057eeSRobert Watson 	}
2145497057eeSRobert Watson }
2146497057eeSRobert Watson 
2147497057eeSRobert Watson static void
2148497057eeSRobert Watson db_print_tflags(u_int t_flags)
2149497057eeSRobert Watson {
2150497057eeSRobert Watson 	int comma;
2151497057eeSRobert Watson 
2152497057eeSRobert Watson 	comma = 0;
2153497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2154497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2155497057eeSRobert Watson 		comma = 1;
2156497057eeSRobert Watson 	}
2157497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2158497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2159497057eeSRobert Watson 		comma = 1;
2160497057eeSRobert Watson 	}
2161497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2162497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2163497057eeSRobert Watson 		comma = 1;
2164497057eeSRobert Watson 	}
2165497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2166497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2167497057eeSRobert Watson 		comma = 1;
2168497057eeSRobert Watson 	}
2169497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2170497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2171497057eeSRobert Watson 		comma = 1;
2172497057eeSRobert Watson 	}
2173497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2174497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2175497057eeSRobert Watson 		comma = 1;
2176497057eeSRobert Watson 	}
2177497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2178497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2179497057eeSRobert Watson 		comma = 1;
2180497057eeSRobert Watson 	}
2181497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2182497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2183497057eeSRobert Watson 		comma = 1;
2184497057eeSRobert Watson 	}
2185497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2186497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2187497057eeSRobert Watson 		comma = 1;
2188497057eeSRobert Watson 	}
2189497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2190497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2191497057eeSRobert Watson 		comma = 1;
2192497057eeSRobert Watson 	}
2193497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2194497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2195497057eeSRobert Watson 		comma = 1;
2196497057eeSRobert Watson 	}
2197497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2198497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2199497057eeSRobert Watson 		comma = 1;
2200497057eeSRobert Watson 	}
2201497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2202497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2203497057eeSRobert Watson 		comma = 1;
2204497057eeSRobert Watson 	}
2205497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2206497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2207497057eeSRobert Watson 		comma = 1;
2208497057eeSRobert Watson 	}
2209497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
2210497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2211497057eeSRobert Watson 		comma = 1;
2212497057eeSRobert Watson 	}
2213497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2214497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2215497057eeSRobert Watson 		comma = 1;
2216497057eeSRobert Watson 	}
2217497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2218497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2219497057eeSRobert Watson 		comma = 1;
2220497057eeSRobert Watson 	}
2221497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2222497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2223497057eeSRobert Watson 		comma = 1;
2224497057eeSRobert Watson 	}
2225dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
2226dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2227dbc42409SLawrence Stewart 		comma = 1;
2228dbc42409SLawrence Stewart 	}
2229497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2230497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2231497057eeSRobert Watson 		comma = 1;
2232497057eeSRobert Watson 	}
2233497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2234497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2235497057eeSRobert Watson 		comma = 1;
2236497057eeSRobert Watson 	}
2237497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2238497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2239497057eeSRobert Watson 		comma = 1;
2240497057eeSRobert Watson 	}
2241497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2242497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2243497057eeSRobert Watson 		comma = 1;
2244497057eeSRobert Watson 	}
2245f2512ba1SRui Paulo 	if (t_flags & TF_ECN_PERMIT) {
2246f2512ba1SRui Paulo 		db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2247f2512ba1SRui Paulo 		comma = 1;
2248f2512ba1SRui Paulo 	}
2249281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2250281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2251281a0fd4SPatrick Kelsey 		comma = 1;
2252281a0fd4SPatrick Kelsey 	}
2253497057eeSRobert Watson }
2254497057eeSRobert Watson 
2255497057eeSRobert Watson static void
2256497057eeSRobert Watson db_print_toobflags(char t_oobflags)
2257497057eeSRobert Watson {
2258497057eeSRobert Watson 	int comma;
2259497057eeSRobert Watson 
2260497057eeSRobert Watson 	comma = 0;
2261497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
2262497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2263497057eeSRobert Watson 		comma = 1;
2264497057eeSRobert Watson 	}
2265497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
2266497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2267497057eeSRobert Watson 		comma = 1;
2268497057eeSRobert Watson 	}
2269497057eeSRobert Watson }
2270497057eeSRobert Watson 
2271497057eeSRobert Watson static void
2272497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2273497057eeSRobert Watson {
2274497057eeSRobert Watson 
2275497057eeSRobert Watson 	db_print_indent(indent);
2276497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
2277497057eeSRobert Watson 
2278497057eeSRobert Watson 	indent += 2;
2279497057eeSRobert Watson 
2280497057eeSRobert Watson 	db_print_indent(indent);
2281497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
22824741bfcbSPatrick Kelsey 	   LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2283497057eeSRobert Watson 
2284497057eeSRobert Watson 	db_print_indent(indent);
228585d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
2286e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2287497057eeSRobert Watson 
2288497057eeSRobert Watson 	db_print_indent(indent);
2289e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2290e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
2291497057eeSRobert Watson 
2292497057eeSRobert Watson 	db_print_indent(indent);
2293497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
2294497057eeSRobert Watson 	db_print_tstate(tp->t_state);
2295497057eeSRobert Watson 	db_printf(")\n");
2296497057eeSRobert Watson 
2297497057eeSRobert Watson 	db_print_indent(indent);
2298497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
2299497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
2300497057eeSRobert Watson 	db_printf(")\n");
2301497057eeSRobert Watson 
2302497057eeSRobert Watson 	db_print_indent(indent);
2303497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
2304497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
2305497057eeSRobert Watson 
2306497057eeSRobert Watson 	db_print_indent(indent);
2307497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
2308497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2309497057eeSRobert Watson 
2310497057eeSRobert Watson 	db_print_indent(indent);
2311497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
2312497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
2313497057eeSRobert Watson 
2314497057eeSRobert Watson 	db_print_indent(indent);
23153ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
2316497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2317497057eeSRobert Watson 
2318497057eeSRobert Watson 	db_print_indent(indent);
23193ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
23201c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
2321497057eeSRobert Watson 
2322497057eeSRobert Watson 	db_print_indent(indent);
23233ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
23241c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2325497057eeSRobert Watson 
2326497057eeSRobert Watson 	db_print_indent(indent);
23270c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
23280c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
2329497057eeSRobert Watson 
2330497057eeSRobert Watson 	db_print_indent(indent);
23311c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
23321c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
2333497057eeSRobert Watson 
2334497057eeSRobert Watson 	db_print_indent(indent);
23351c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
23361c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2337497057eeSRobert Watson 
2338497057eeSRobert Watson 	db_print_indent(indent);
2339497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
2340497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2341497057eeSRobert Watson 	    tp->t_rttbest);
2342497057eeSRobert Watson 
2343497057eeSRobert Watson 	db_print_indent(indent);
23443ac12506SJonathan T. Looney 	db_printf("t_rttupdated: %lu   max_sndwnd: %u   t_softerror: %d\n",
2345497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2346497057eeSRobert Watson 
2347497057eeSRobert Watson 	db_print_indent(indent);
2348497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2349497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
2350497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
2351497057eeSRobert Watson 
2352497057eeSRobert Watson 	db_print_indent(indent);
2353497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
2354497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2355497057eeSRobert Watson 
2356497057eeSRobert Watson 	db_print_indent(indent);
23579f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
23581a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
2359497057eeSRobert Watson 
2360497057eeSRobert Watson 	db_print_indent(indent);
2361497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
23623ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2363497057eeSRobert Watson 
2364497057eeSRobert Watson 	db_print_indent(indent);
23653ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
23669f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2367497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
2368497057eeSRobert Watson 
2369497057eeSRobert Watson 	db_print_indent(indent);
23703529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
23713529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2372497057eeSRobert Watson 
2373497057eeSRobert Watson 	db_print_indent(indent);
2374497057eeSRobert Watson 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
2375497057eeSRobert Watson 	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2376497057eeSRobert Watson 
2377497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
2378497057eeSRobert Watson 
2379497057eeSRobert Watson 	db_print_indent(indent);
2380497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
2381497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2382497057eeSRobert Watson }
2383497057eeSRobert Watson 
2384497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2385497057eeSRobert Watson {
2386497057eeSRobert Watson 	struct tcpcb *tp;
2387497057eeSRobert Watson 
2388497057eeSRobert Watson 	if (!have_addr) {
2389497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
2390497057eeSRobert Watson 		return;
2391497057eeSRobert Watson 	}
2392497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
2393497057eeSRobert Watson 
2394497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
2395497057eeSRobert Watson }
2396497057eeSRobert Watson #endif
2397