xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 3ee9c3c4ebf1d1edb3337cef1a840bcfe383ceb8)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
5623dce13SRobert Watson  *	The Regents of the University of California.
6497057eeSRobert Watson  * Copyright (c) 2006-2007 Robert N. M. Watson
7fa046d87SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
8623dce13SRobert Watson  * All rights reserved.
9df8bae1dSRodney W. Grimes  *
10fa046d87SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
11fa046d87SRobert Watson  * contract to Juniper Networks, Inc.
12fa046d87SRobert Watson  *
13df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
14df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
15df8bae1dSRodney W. Grimes  * are met:
16df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
18df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
20df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
21fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
22df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
23df8bae1dSRodney W. Grimes  *    without specific prior written permission.
24df8bae1dSRodney W. Grimes  *
25df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
36df8bae1dSRodney W. Grimes  *
371fdbc7aeSGarrett Wollman  *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
38df8bae1dSRodney W. Grimes  */
39df8bae1dSRodney W. Grimes 
404b421e2dSMike Silbersack #include <sys/cdefs.h>
414b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
424b421e2dSMike Silbersack 
43497057eeSRobert Watson #include "opt_ddb.h"
441cfd4b53SBruce M Simpson #include "opt_inet.h"
45fb59c426SYoshinobu Inoue #include "opt_inet6.h"
46fcf59617SAndrey V. Elsukov #include "opt_ipsec.h"
470cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
480cc12cc5SJoerg Wunsch 
49df8bae1dSRodney W. Grimes #include <sys/param.h>
50df8bae1dSRodney W. Grimes #include <sys/systm.h>
519077f387SGleb Smirnoff #include <sys/limits.h>
52f76fcf6dSJeffrey Hsu #include <sys/malloc.h>
5355bceb1eSRandall Stewart #include <sys/refcount.h>
54c7a82f90SGarrett Wollman #include <sys/kernel.h>
5598163b98SPoul-Henning Kamp #include <sys/sysctl.h>
56df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
57fb59c426SYoshinobu Inoue #ifdef INET6
58fb59c426SYoshinobu Inoue #include <sys/domain.h>
59fb59c426SYoshinobu Inoue #endif /* INET6 */
60df8bae1dSRodney W. Grimes #include <sys/socket.h>
61df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
62df8bae1dSRodney W. Grimes #include <sys/protosw.h>
6391421ba2SRobert Watson #include <sys/proc.h>
6491421ba2SRobert Watson #include <sys/jail.h>
65f5cf1e5fSJulien Charbon #include <sys/syslog.h>
66df8bae1dSRodney W. Grimes 
67497057eeSRobert Watson #ifdef DDB
68497057eeSRobert Watson #include <ddb/ddb.h>
69497057eeSRobert Watson #endif
70497057eeSRobert Watson 
71df8bae1dSRodney W. Grimes #include <net/if.h>
7276039bc8SGleb Smirnoff #include <net/if_var.h>
73df8bae1dSRodney W. Grimes #include <net/route.h>
74530c0060SRobert Watson #include <net/vnet.h>
75df8bae1dSRodney W. Grimes 
76df8bae1dSRodney W. Grimes #include <netinet/in.h>
775d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h>
78df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
79b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h>
80b5e8ce9fSBruce Evans #include <netinet/in_var.h>
81df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
82fb59c426SYoshinobu Inoue #ifdef INET6
83b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h>
84b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
85fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
86a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
87fb59c426SYoshinobu Inoue #endif
882de3e790SGleb Smirnoff #include <netinet/tcp.h>
89df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
90df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
91df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
92df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
932529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h>
94df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
954644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
96c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h>
9786a996e6SHiren Panchasara #ifdef TCPPCAP
9886a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
9986a996e6SHiren Panchasara #endif
100610ee2f9SDavid Greenman #ifdef TCPDEBUG
101df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
102610ee2f9SDavid Greenman #endif
10309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
104bc65987aSKip Macy #include <netinet/tcp_offload.h>
10509fe6320SNavdeep Parhar #endif
106fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
107df8bae1dSRodney W. Grimes 
108df8bae1dSRodney W. Grimes /*
109df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
110df8bae1dSRodney W. Grimes  */
11156dc72c3SPawel Jakub Dawidek static int	tcp_attach(struct socket *);
112b287c6c7SBjoern A. Zeeb #ifdef INET
1134d77a549SAlfred Perlstein static int	tcp_connect(struct tcpcb *, struct sockaddr *,
1144d77a549SAlfred Perlstein 		    struct thread *td);
115b287c6c7SBjoern A. Zeeb #endif /* INET */
116fb59c426SYoshinobu Inoue #ifdef INET6
1174d77a549SAlfred Perlstein static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
1184d77a549SAlfred Perlstein 		    struct thread *td);
119fb59c426SYoshinobu Inoue #endif /* INET6 */
120623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
121623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
122b8af5dfaSRobert Watson static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
1232c37256eSGarrett Wollman 
1242c37256eSGarrett Wollman #ifdef TCPDEBUG
1251db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1262c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
1274cc20ab1SSeigo Tanimura #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
1284cc20ab1SSeigo Tanimura 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1292c37256eSGarrett Wollman #else
1302c37256eSGarrett Wollman #define	TCPDEBUG0
1312c37256eSGarrett Wollman #define	TCPDEBUG1()
1322c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1332c37256eSGarrett Wollman #endif
1342c37256eSGarrett Wollman 
1352c37256eSGarrett Wollman /*
1362c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1372c37256eSGarrett Wollman  * and an internet control block.
1382c37256eSGarrett Wollman  */
1392c37256eSGarrett Wollman static int
140b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1412c37256eSGarrett Wollman {
142f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
143623dce13SRobert Watson 	struct tcpcb *tp = NULL;
144623dce13SRobert Watson 	int error;
1452c37256eSGarrett Wollman 	TCPDEBUG0;
1462c37256eSGarrett Wollman 
147623dce13SRobert Watson 	inp = sotoinpcb(so);
148623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1492c37256eSGarrett Wollman 	TCPDEBUG1();
1502c37256eSGarrett Wollman 
15156dc72c3SPawel Jakub Dawidek 	error = tcp_attach(so);
1522c37256eSGarrett Wollman 	if (error)
1532c37256eSGarrett Wollman 		goto out;
1542c37256eSGarrett Wollman 
1552c37256eSGarrett Wollman 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1563879597fSAndrey A. Chernov 		so->so_linger = TCP_LINGERTIME;
157f76fcf6dSJeffrey Hsu 
158f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
159f76fcf6dSJeffrey Hsu 	tp = intotcpcb(inp);
1602c37256eSGarrett Wollman out:
1612c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
1625d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
1632c37256eSGarrett Wollman 	return error;
1642c37256eSGarrett Wollman }
1652c37256eSGarrett Wollman 
1662c37256eSGarrett Wollman /*
167a152f8a3SRobert Watson  * tcp_detach is called when the socket layer loses its final reference
168a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
169a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
170a152f8a3SRobert Watson  * inpcb state: time wait.
171c78cbc7bSRobert Watson  *
172a152f8a3SRobert Watson  * This function can probably be re-absorbed back into tcp_usr_detach() now
173a152f8a3SRobert Watson  * that there is a single detach path.
1742c37256eSGarrett Wollman  */
175bc725eafSRobert Watson static void
176c78cbc7bSRobert Watson tcp_detach(struct socket *so, struct inpcb *inp)
1772c37256eSGarrett Wollman {
1782c37256eSGarrett Wollman 	struct tcpcb *tp;
1792c37256eSGarrett Wollman 
180079672cbSJulien Charbon 	INP_INFO_LOCK_ASSERT(&V_tcbinfo);
1818501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
182623dce13SRobert Watson 
183c78cbc7bSRobert Watson 	KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
184c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
185953b5606SRobert Watson 
186a152f8a3SRobert Watson 	tp = intotcpcb(inp);
187a152f8a3SRobert Watson 
188ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
189623dce13SRobert Watson 		/*
190a152f8a3SRobert Watson 		 * There are two cases to handle: one in which the time wait
191a152f8a3SRobert Watson 		 * state is being discarded (INP_DROPPED), and one in which
192a152f8a3SRobert Watson 		 * this connection will remain in timewait.  In the former,
193a152f8a3SRobert Watson 		 * it is time to discard all state (except tcptw, which has
194a152f8a3SRobert Watson 		 * already been discarded by the timewait close code, which
195a152f8a3SRobert Watson 		 * should be further up the call stack somewhere).  In the
196a152f8a3SRobert Watson 		 * latter case, we detach from the socket, but leave the pcb
197a152f8a3SRobert Watson 		 * present until timewait ends.
198623dce13SRobert Watson 		 *
199a152f8a3SRobert Watson 		 * XXXRW: Would it be cleaner to free the tcptw here?
200cea40c48SJulien Charbon 		 *
201cea40c48SJulien Charbon 		 * Astute question indeed, from twtcp perspective there are
202dd388cfdSGleb Smirnoff 		 * four cases to consider:
203cea40c48SJulien Charbon 		 *
204cea40c48SJulien Charbon 		 * #1 tcp_detach is called at tcptw creation time by
205cea40c48SJulien Charbon 		 *  tcp_twstart, then do not discard the newly created tcptw
206cea40c48SJulien Charbon 		 *  and leave inpcb present until timewait ends
207dd388cfdSGleb Smirnoff 		 * #2 tcp_detach is called at tcptw creation time by
208dd388cfdSGleb Smirnoff 		 *  tcp_twstart, but connection is local and tw will be
209dd388cfdSGleb Smirnoff 		 *  discarded immediately
210dd388cfdSGleb Smirnoff 		 * #3 tcp_detach is called at timewait end (or reuse) by
211cea40c48SJulien Charbon 		 *  tcp_twclose, then the tcptw has already been discarded
212ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
213dd388cfdSGleb Smirnoff 		 * #4 tcp_detach is called() after timewait ends (or reuse)
214cea40c48SJulien Charbon 		 *  (e.g. by soclose), then tcptw has already been discarded
215ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
216cea40c48SJulien Charbon 		 *
217cea40c48SJulien Charbon 		 *  In all three cases the tcptw should not be freed here.
218623dce13SRobert Watson 		 */
219ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED) {
220623dce13SRobert Watson 			in_pcbdetach(inp);
221f5cf1e5fSJulien Charbon 			if (__predict_true(tp == NULL)) {
2220206cdb8SBjoern A. Zeeb 				in_pcbfree(inp);
2230206cdb8SBjoern A. Zeeb 			} else {
224f5cf1e5fSJulien Charbon 				/*
225f5cf1e5fSJulien Charbon 				 * This case should not happen as in TIMEWAIT
226f5cf1e5fSJulien Charbon 				 * state the inp should not be destroyed before
227f5cf1e5fSJulien Charbon 				 * its tcptw.  If INVARIANTS is defined, panic.
228f5cf1e5fSJulien Charbon 				 */
229f5cf1e5fSJulien Charbon #ifdef INVARIANTS
230f5cf1e5fSJulien Charbon 				panic("%s: Panic before an inp double-free: "
231f5cf1e5fSJulien Charbon 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
232f5cf1e5fSJulien Charbon 				    , __func__);
233f5cf1e5fSJulien Charbon #else
234f5cf1e5fSJulien Charbon 				log(LOG_ERR, "%s: Avoid an inp double-free: "
235f5cf1e5fSJulien Charbon 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
236f5cf1e5fSJulien Charbon 				    , __func__);
237f5cf1e5fSJulien Charbon #endif
238f5cf1e5fSJulien Charbon 				INP_WUNLOCK(inp);
239f5cf1e5fSJulien Charbon 			}
240f5cf1e5fSJulien Charbon 		} else {
241623dce13SRobert Watson 			in_pcbdetach(inp);
2428501a69cSRobert Watson 			INP_WUNLOCK(inp);
243623dce13SRobert Watson 		}
244623dce13SRobert Watson 	} else {
245e6e65783SRobert Watson 		/*
246a152f8a3SRobert Watson 		 * If the connection is not in timewait, we consider two
247a152f8a3SRobert Watson 		 * two conditions: one in which no further processing is
248a152f8a3SRobert Watson 		 * necessary (dropped || embryonic), and one in which TCP is
249a152f8a3SRobert Watson 		 * not yet done, but no longer requires the socket, so the
250a152f8a3SRobert Watson 		 * pcb will persist for the time being.
251a152f8a3SRobert Watson 		 *
252a152f8a3SRobert Watson 		 * XXXRW: Does the second case still occur?
253e6e65783SRobert Watson 		 */
254ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED ||
255623dce13SRobert Watson 		    tp->t_state < TCPS_SYN_SENT) {
256623dce13SRobert Watson 			tcp_discardcb(tp);
257623dce13SRobert Watson 			in_pcbdetach(inp);
2580206cdb8SBjoern A. Zeeb 			in_pcbfree(inp);
259db3cee51SNavdeep Parhar 		} else {
260a152f8a3SRobert Watson 			in_pcbdetach(inp);
261db3cee51SNavdeep Parhar 			INP_WUNLOCK(inp);
262db3cee51SNavdeep Parhar 		}
263623dce13SRobert Watson 	}
264623dce13SRobert Watson }
265c78cbc7bSRobert Watson 
266c78cbc7bSRobert Watson /*
267c78cbc7bSRobert Watson  * pru_detach() detaches the TCP protocol from the socket.
268c78cbc7bSRobert Watson  * If the protocol state is non-embryonic, then can't
269c78cbc7bSRobert Watson  * do this directly: have to initiate a pru_disconnect(),
270c78cbc7bSRobert Watson  * which may finish later; embryonic TCB's can just
271c78cbc7bSRobert Watson  * be discarded here.
272c78cbc7bSRobert Watson  */
273c78cbc7bSRobert Watson static void
274c78cbc7bSRobert Watson tcp_usr_detach(struct socket *so)
275c78cbc7bSRobert Watson {
276c78cbc7bSRobert Watson 	struct inpcb *inp;
277079672cbSJulien Charbon 	int rlock = 0;
278c78cbc7bSRobert Watson 
279c78cbc7bSRobert Watson 	inp = sotoinpcb(so);
280c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
281079672cbSJulien Charbon 	if (!INP_INFO_WLOCKED(&V_tcbinfo)) {
282ff9b006dSJulien Charbon 		INP_INFO_RLOCK(&V_tcbinfo);
283079672cbSJulien Charbon 		rlock = 1;
284079672cbSJulien Charbon 	}
2858501a69cSRobert Watson 	INP_WLOCK(inp);
286c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
287c78cbc7bSRobert Watson 	    ("tcp_usr_detach: inp_socket == NULL"));
288c78cbc7bSRobert Watson 	tcp_detach(so, inp);
289079672cbSJulien Charbon 	if (rlock)
290ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
2912c37256eSGarrett Wollman }
2922c37256eSGarrett Wollman 
293b287c6c7SBjoern A. Zeeb #ifdef INET
2942c37256eSGarrett Wollman /*
2952c37256eSGarrett Wollman  * Give the socket an address.
2962c37256eSGarrett Wollman  */
2972c37256eSGarrett Wollman static int
298b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2992c37256eSGarrett Wollman {
3002c37256eSGarrett Wollman 	int error = 0;
301f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
302623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3032c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
3042c37256eSGarrett Wollman 
30552710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
30652710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sinp))
30752710de1SPawel Jakub Dawidek 		return (EINVAL);
3082c37256eSGarrett Wollman 	/*
3092c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
3102c37256eSGarrett Wollman 	 * to them.
3112c37256eSGarrett Wollman 	 */
3122c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
31352710de1SPawel Jakub Dawidek 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
31452710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
31552710de1SPawel Jakub Dawidek 
316623dce13SRobert Watson 	TCPDEBUG0;
317623dce13SRobert Watson 	inp = sotoinpcb(so);
318623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
3198501a69cSRobert Watson 	INP_WLOCK(inp);
320ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
321623dce13SRobert Watson 		error = EINVAL;
3222c37256eSGarrett Wollman 		goto out;
323623dce13SRobert Watson 	}
324623dce13SRobert Watson 	tp = intotcpcb(inp);
325623dce13SRobert Watson 	TCPDEBUG1();
326fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
327623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
328fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
329623dce13SRobert Watson out:
330623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3315d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3328501a69cSRobert Watson 	INP_WUNLOCK(inp);
333623dce13SRobert Watson 
334623dce13SRobert Watson 	return (error);
3352c37256eSGarrett Wollman }
336b287c6c7SBjoern A. Zeeb #endif /* INET */
3372c37256eSGarrett Wollman 
338fb59c426SYoshinobu Inoue #ifdef INET6
339fb59c426SYoshinobu Inoue static int
340b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
341fb59c426SYoshinobu Inoue {
342fb59c426SYoshinobu Inoue 	int error = 0;
343f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
344623dce13SRobert Watson 	struct tcpcb *tp = NULL;
345fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
346fb59c426SYoshinobu Inoue 
34752710de1SPawel Jakub Dawidek 	sin6p = (struct sockaddr_in6 *)nam;
34852710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sin6p))
34952710de1SPawel Jakub Dawidek 		return (EINVAL);
350fb59c426SYoshinobu Inoue 	/*
351fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
352fb59c426SYoshinobu Inoue 	 * to them.
353fb59c426SYoshinobu Inoue 	 */
354fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6 &&
35552710de1SPawel Jakub Dawidek 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
35652710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
35752710de1SPawel Jakub Dawidek 
358623dce13SRobert Watson 	TCPDEBUG0;
359623dce13SRobert Watson 	inp = sotoinpcb(so);
360623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
3618501a69cSRobert Watson 	INP_WLOCK(inp);
362ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
363623dce13SRobert Watson 		error = EINVAL;
364623dce13SRobert Watson 		goto out;
365623dce13SRobert Watson 	}
366623dce13SRobert Watson 	tp = intotcpcb(inp);
367623dce13SRobert Watson 	TCPDEBUG1();
368fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
369fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
370fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
371b287c6c7SBjoern A. Zeeb #ifdef INET
37266ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
373fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
374fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
375fb59c426SYoshinobu Inoue 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
376fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
377fb59c426SYoshinobu Inoue 
378fb59c426SYoshinobu Inoue 			in6_sin6_2_sin(&sin, sin6p);
379fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
380fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
381b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
382b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
383fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
384fb59c426SYoshinobu Inoue 			goto out;
385fb59c426SYoshinobu Inoue 		}
386fb59c426SYoshinobu Inoue 	}
387b287c6c7SBjoern A. Zeeb #endif
388b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
389fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
390623dce13SRobert Watson out:
391623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3925d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3938501a69cSRobert Watson 	INP_WUNLOCK(inp);
394623dce13SRobert Watson 	return (error);
395fb59c426SYoshinobu Inoue }
396fb59c426SYoshinobu Inoue #endif /* INET6 */
397fb59c426SYoshinobu Inoue 
398b287c6c7SBjoern A. Zeeb #ifdef INET
3992c37256eSGarrett Wollman /*
4002c37256eSGarrett Wollman  * Prepare to accept connections.
4012c37256eSGarrett Wollman  */
4022c37256eSGarrett Wollman static int
403d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
4042c37256eSGarrett Wollman {
4052c37256eSGarrett Wollman 	int error = 0;
406f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
407623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4082c37256eSGarrett Wollman 
409623dce13SRobert Watson 	TCPDEBUG0;
410623dce13SRobert Watson 	inp = sotoinpcb(so);
411623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
4128501a69cSRobert Watson 	INP_WLOCK(inp);
413ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
414623dce13SRobert Watson 		error = EINVAL;
415623dce13SRobert Watson 		goto out;
416623dce13SRobert Watson 	}
417623dce13SRobert Watson 	tp = intotcpcb(inp);
418623dce13SRobert Watson 	TCPDEBUG1();
4190daccb9cSRobert Watson 	SOCK_LOCK(so);
4200daccb9cSRobert Watson 	error = solisten_proto_check(so);
421fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
4220daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0)
423b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
424fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4250daccb9cSRobert Watson 	if (error == 0) {
42657f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
427d374e81eSRobert Watson 		solisten_proto(so, backlog);
42809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
42937cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
43009fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
43109fe6320SNavdeep Parhar #endif
4320daccb9cSRobert Watson 	}
4330daccb9cSRobert Watson 	SOCK_UNLOCK(so);
434623dce13SRobert Watson 
43568bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
436281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
43718a75309SPatrick Kelsey 
438623dce13SRobert Watson out:
439623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4405d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4418501a69cSRobert Watson 	INP_WUNLOCK(inp);
442623dce13SRobert Watson 	return (error);
4432c37256eSGarrett Wollman }
444b287c6c7SBjoern A. Zeeb #endif /* INET */
4452c37256eSGarrett Wollman 
446fb59c426SYoshinobu Inoue #ifdef INET6
447fb59c426SYoshinobu Inoue static int
448d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
449fb59c426SYoshinobu Inoue {
450fb59c426SYoshinobu Inoue 	int error = 0;
451f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
452623dce13SRobert Watson 	struct tcpcb *tp = NULL;
453fb59c426SYoshinobu Inoue 
454623dce13SRobert Watson 	TCPDEBUG0;
455623dce13SRobert Watson 	inp = sotoinpcb(so);
456623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4578501a69cSRobert Watson 	INP_WLOCK(inp);
458ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
459623dce13SRobert Watson 		error = EINVAL;
460623dce13SRobert Watson 		goto out;
461623dce13SRobert Watson 	}
462623dce13SRobert Watson 	tp = intotcpcb(inp);
463623dce13SRobert Watson 	TCPDEBUG1();
4640daccb9cSRobert Watson 	SOCK_LOCK(so);
4650daccb9cSRobert Watson 	error = solisten_proto_check(so);
466fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
4670daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0) {
468fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
46966ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
470fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
471b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
472fb59c426SYoshinobu Inoue 	}
473fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4740daccb9cSRobert Watson 	if (error == 0) {
47557f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
476d374e81eSRobert Watson 		solisten_proto(so, backlog);
47709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
47837cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
47909fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
48009fe6320SNavdeep Parhar #endif
4810daccb9cSRobert Watson 	}
4820daccb9cSRobert Watson 	SOCK_UNLOCK(so);
483623dce13SRobert Watson 
48468bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
485281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
48618a75309SPatrick Kelsey 
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 	/*
852281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
853281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
854281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
855281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
856281a0fd4SPatrick Kelsey 	 * expires.
857281a0fd4SPatrick Kelsey 	 */
85868bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags) &&
859281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
860281a0fd4SPatrick Kelsey 		goto out;
86109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
86209fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
86309fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
864460cf046SNavdeep Parhar 	else
86509fe6320SNavdeep Parhar #endif
86655bceb1eSRandall Stewart 	tp->t_fb->tfb_tcp_output(tp);
867623dce13SRobert Watson 
868623dce13SRobert Watson out:
869623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
8705d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
8718501a69cSRobert Watson 	INP_WUNLOCK(inp);
872623dce13SRobert Watson 	return (error);
8732c37256eSGarrett Wollman }
8742c37256eSGarrett Wollman 
8752c37256eSGarrett Wollman /*
8762c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
8779c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
8789c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
8799c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
8809c9906e9SPeter Wemm  * generally are caller-frees.
8812c37256eSGarrett Wollman  */
8822c37256eSGarrett Wollman static int
88357bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
884b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
8852c37256eSGarrett Wollman {
8862c37256eSGarrett Wollman 	int error = 0;
887f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
888623dce13SRobert Watson 	struct tcpcb *tp = NULL;
889fb59c426SYoshinobu Inoue #ifdef INET6
890fb59c426SYoshinobu Inoue 	int isipv6;
891fb59c426SYoshinobu Inoue #endif
8929c9906e9SPeter Wemm 	TCPDEBUG0;
8932c37256eSGarrett Wollman 
894f76fcf6dSJeffrey Hsu 	/*
895fa046d87SRobert Watson 	 * We require the pcbinfo lock if we will close the socket as part of
896fa046d87SRobert Watson 	 * this call.
897f76fcf6dSJeffrey Hsu 	 */
898fa046d87SRobert Watson 	if (flags & PRUS_EOF)
899ff9b006dSJulien Charbon 		INP_INFO_RLOCK(&V_tcbinfo);
900f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
901623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
9028501a69cSRobert Watson 	INP_WLOCK(inp);
903ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
9047ff0b850SAndre Oppermann 		if (control)
9057ff0b850SAndre Oppermann 			m_freem(control);
9062cbcd3c1SGleb Smirnoff 		/*
9072cbcd3c1SGleb Smirnoff 		 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
9082cbcd3c1SGleb Smirnoff 		 * for freeing memory.
9092cbcd3c1SGleb Smirnoff 		 */
9102cbcd3c1SGleb Smirnoff 		if (m && (flags & PRUS_NOTREADY) == 0)
9117ff0b850SAndre Oppermann 			m_freem(m);
91221367f63SSam Leffler 		error = ECONNRESET;
9139c9906e9SPeter Wemm 		goto out;
9149c9906e9SPeter Wemm 	}
915fb59c426SYoshinobu Inoue #ifdef INET6
916fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
917fb59c426SYoshinobu Inoue #endif /* INET6 */
9189c9906e9SPeter Wemm 	tp = intotcpcb(inp);
9199c9906e9SPeter Wemm 	TCPDEBUG1();
9209c9906e9SPeter Wemm 	if (control) {
9219c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
9229c9906e9SPeter Wemm 		if (control->m_len) {
9239c9906e9SPeter Wemm 			m_freem(control);
9242c37256eSGarrett Wollman 			if (m)
9252c37256eSGarrett Wollman 				m_freem(m);
926744f87eaSDavid Greenman 			error = EINVAL;
927744f87eaSDavid Greenman 			goto out;
9282c37256eSGarrett Wollman 		}
9299c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
9309c9906e9SPeter Wemm 	}
9312c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
932651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
9332c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
9342c37256eSGarrett Wollman 			/*
9352c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
9362c37256eSGarrett Wollman 			 * initialize window to default value, and
9370c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
9382c37256eSGarrett Wollman 			 */
939fb59c426SYoshinobu Inoue #ifdef INET6
940fb59c426SYoshinobu Inoue 			if (isipv6)
941b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
942fb59c426SYoshinobu Inoue #endif /* INET6 */
943b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
944b287c6c7SBjoern A. Zeeb 			else
945b287c6c7SBjoern A. Zeeb #endif
946b287c6c7SBjoern A. Zeeb #ifdef INET
947b40ce416SJulian Elischer 				error = tcp_connect(tp, nam, td);
948b287c6c7SBjoern A. Zeeb #endif
9492c37256eSGarrett Wollman 			if (error)
9502c37256eSGarrett Wollman 				goto out;
951c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
952c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
95318a75309SPatrick Kelsey 			else {
9542c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
9552c37256eSGarrett Wollman 				tcp_mss(tp, -1);
9562c37256eSGarrett Wollman 			}
957c560df6fSPatrick Kelsey 		}
9582c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
9592c37256eSGarrett Wollman 			/*
9602c37256eSGarrett Wollman 			 * Close the send side of the connection after
9612c37256eSGarrett Wollman 			 * the data is sent.
9622c37256eSGarrett Wollman 			 */
963ff9b006dSJulien Charbon 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
9642c37256eSGarrett Wollman 			socantsendmore(so);
965623dce13SRobert Watson 			tcp_usrclosed(tp);
9662c37256eSGarrett Wollman 		}
9672cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
9682cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
969b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
970b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
97155bceb1eSRandall Stewart 			error = tp->t_fb->tfb_tcp_output(tp);
972b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
973b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
974b0acefa8SBill Fenner 		}
9752c37256eSGarrett Wollman 	} else {
976623dce13SRobert Watson 		/*
977623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
978623dce13SRobert Watson 		 */
979d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
9802c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
981d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
9822c37256eSGarrett Wollman 			m_freem(m);
9832c37256eSGarrett Wollman 			error = ENOBUFS;
9842c37256eSGarrett Wollman 			goto out;
9852c37256eSGarrett Wollman 		}
9862c37256eSGarrett Wollman 		/*
9872c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
9882c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
9892c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
9902c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
9912c37256eSGarrett Wollman 		 * of data past the urgent section.
9922c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
9932c37256eSGarrett Wollman 		 */
994651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
995d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
996ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
997ef53690bSGarrett Wollman 			/*
998ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
999ef53690bSGarrett Wollman 			 * initialize window to default value, and
10000c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1001ef53690bSGarrett Wollman 			 */
100218a75309SPatrick Kelsey 
1003c560df6fSPatrick Kelsey 			/*
1004c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1005c560df6fSPatrick Kelsey 			 */
1006c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1007c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1008fb59c426SYoshinobu Inoue #ifdef INET6
1009fb59c426SYoshinobu Inoue 			if (isipv6)
1010b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1011fb59c426SYoshinobu Inoue #endif /* INET6 */
1012b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1013b287c6c7SBjoern A. Zeeb 			else
1014b287c6c7SBjoern A. Zeeb #endif
1015b287c6c7SBjoern A. Zeeb #ifdef INET
1016b40ce416SJulian Elischer 				error = tcp_connect(tp, nam, td);
1017b287c6c7SBjoern A. Zeeb #endif
1018ef53690bSGarrett Wollman 			if (error)
1019ef53690bSGarrett Wollman 				goto out;
1020ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1021ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
1022623dce13SRobert Watson 		}
1023300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
10242cbcd3c1SGleb Smirnoff 		if (!(flags & PRUS_NOTREADY)) {
10252cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
102655bceb1eSRandall Stewart 			error = tp->t_fb->tfb_tcp_output(tp);
10272cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
10282c37256eSGarrett Wollman 		}
10292cbcd3c1SGleb Smirnoff 	}
10302529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL,
10312529f56eSJonathan T. Looney 	    &inp->inp_socket->so_rcv,
10322529f56eSJonathan T. Looney 	    &inp->inp_socket->so_snd,
10332529f56eSJonathan T. Looney 	    TCP_LOG_USERSEND, error,
10342529f56eSJonathan T. Looney 	    0, NULL, false);
1035d1401c90SRobert Watson out:
1036d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
10372c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
10385d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
10395d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
10408501a69cSRobert Watson 	INP_WUNLOCK(inp);
1041fa046d87SRobert Watson 	if (flags & PRUS_EOF)
1042ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
104373fddedaSPeter Grehan 	return (error);
10442c37256eSGarrett Wollman }
10452c37256eSGarrett Wollman 
10462cbcd3c1SGleb Smirnoff static int
10472cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
10482cbcd3c1SGleb Smirnoff {
10492cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
10502cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
10512cbcd3c1SGleb Smirnoff 	int error;
10522cbcd3c1SGleb Smirnoff 
10532cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
10542cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
10552cbcd3c1SGleb Smirnoff 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
10562cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
10572cbcd3c1SGleb Smirnoff 		for (int i = 0; i < count; i++)
10582cbcd3c1SGleb Smirnoff 			m = m_free(m);
10592cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
10602cbcd3c1SGleb Smirnoff 	}
10612cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
10622cbcd3c1SGleb Smirnoff 
10632cbcd3c1SGleb Smirnoff 	SOCKBUF_LOCK(&so->so_snd);
10642cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
10652cbcd3c1SGleb Smirnoff 	SOCKBUF_UNLOCK(&so->so_snd);
10662cbcd3c1SGleb Smirnoff 	if (error == 0)
106755bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
10682cbcd3c1SGleb Smirnoff 	INP_WUNLOCK(inp);
10692cbcd3c1SGleb Smirnoff 
10702cbcd3c1SGleb Smirnoff 	return (error);
10712cbcd3c1SGleb Smirnoff }
10722cbcd3c1SGleb Smirnoff 
10732c37256eSGarrett Wollman /*
1074a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
10752c37256eSGarrett Wollman  */
1076ac45e92fSRobert Watson static void
10772c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
10782c37256eSGarrett Wollman {
1079f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1080a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
1081623dce13SRobert Watson 	TCPDEBUG0;
1082c78cbc7bSRobert Watson 
1083ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1084c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1085c78cbc7bSRobert Watson 
1086ff9b006dSJulien Charbon 	INP_INFO_RLOCK(&V_tcbinfo);
10878501a69cSRobert Watson 	INP_WLOCK(inp);
1088c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1089c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1090c78cbc7bSRobert Watson 
1091c78cbc7bSRobert Watson 	/*
1092a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1093c78cbc7bSRobert Watson 	 */
1094ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1095ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1096c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
1097a152f8a3SRobert Watson 		TCPDEBUG1();
10988fa799bdSJonathan T. Looney 		tp = tcp_drop(tp, ECONNABORTED);
10998fa799bdSJonathan T. Looney 		if (tp == NULL)
11008fa799bdSJonathan T. Looney 			goto dropped;
1101a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
11025d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1103c78cbc7bSRobert Watson 	}
1104ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1105a152f8a3SRobert Watson 		SOCK_LOCK(so);
1106a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1107a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1108ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1109a152f8a3SRobert Watson 	}
11108501a69cSRobert Watson 	INP_WUNLOCK(inp);
11118fa799bdSJonathan T. Looney dropped:
1112ff9b006dSJulien Charbon 	INP_INFO_RUNLOCK(&V_tcbinfo);
1113a152f8a3SRobert Watson }
1114a152f8a3SRobert Watson 
1115a152f8a3SRobert Watson /*
1116a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1117a152f8a3SRobert Watson  */
1118a152f8a3SRobert Watson static void
1119a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1120a152f8a3SRobert Watson {
1121a152f8a3SRobert Watson 	struct inpcb *inp;
1122a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
1123a152f8a3SRobert Watson 	TCPDEBUG0;
1124a152f8a3SRobert Watson 
1125a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1126a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1127a152f8a3SRobert Watson 
1128ff9b006dSJulien Charbon 	INP_INFO_RLOCK(&V_tcbinfo);
11298501a69cSRobert Watson 	INP_WLOCK(inp);
1130a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1131a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1132a152f8a3SRobert Watson 
1133a152f8a3SRobert Watson 	/*
1134a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
1135a152f8a3SRobert Watson 	 * a disconnect.
1136a152f8a3SRobert Watson 	 */
1137ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1138ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1139a152f8a3SRobert Watson 		tp = intotcpcb(inp);
1140a152f8a3SRobert Watson 		TCPDEBUG1();
1141a152f8a3SRobert Watson 		tcp_disconnect(tp);
1142a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
11435d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1144a152f8a3SRobert Watson 	}
1145ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1146a152f8a3SRobert Watson 		SOCK_LOCK(so);
1147a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1148a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1149ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1150a152f8a3SRobert Watson 	}
11518501a69cSRobert Watson 	INP_WUNLOCK(inp);
1152ff9b006dSJulien Charbon 	INP_INFO_RUNLOCK(&V_tcbinfo);
11532c37256eSGarrett Wollman }
11542c37256eSGarrett Wollman 
11552c37256eSGarrett Wollman /*
11562c37256eSGarrett Wollman  * Receive out-of-band data.
11572c37256eSGarrett Wollman  */
11582c37256eSGarrett Wollman static int
11592c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
11602c37256eSGarrett Wollman {
11612c37256eSGarrett Wollman 	int error = 0;
1162f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1163623dce13SRobert Watson 	struct tcpcb *tp = NULL;
11642c37256eSGarrett Wollman 
1165623dce13SRobert Watson 	TCPDEBUG0;
1166623dce13SRobert Watson 	inp = sotoinpcb(so);
1167623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
11688501a69cSRobert Watson 	INP_WLOCK(inp);
1169ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
117021367f63SSam Leffler 		error = ECONNRESET;
1171623dce13SRobert Watson 		goto out;
1172623dce13SRobert Watson 	}
1173623dce13SRobert Watson 	tp = intotcpcb(inp);
1174623dce13SRobert Watson 	TCPDEBUG1();
11752c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1176c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
11774cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
11784cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
11792c37256eSGarrett Wollman 		error = EINVAL;
11802c37256eSGarrett Wollman 		goto out;
11812c37256eSGarrett Wollman 	}
11822c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
11832c37256eSGarrett Wollman 		error = EWOULDBLOCK;
11842c37256eSGarrett Wollman 		goto out;
11852c37256eSGarrett Wollman 	}
11862c37256eSGarrett Wollman 	m->m_len = 1;
11872c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
11882c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
11892c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1190623dce13SRobert Watson 
1191623dce13SRobert Watson out:
1192623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
11935d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
11948501a69cSRobert Watson 	INP_WUNLOCK(inp);
1195623dce13SRobert Watson 	return (error);
11962c37256eSGarrett Wollman }
11972c37256eSGarrett Wollman 
1198b287c6c7SBjoern A. Zeeb #ifdef INET
11992c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1200756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1201756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1202756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1203756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1204756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1205756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1206756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1207756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1208756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
120954d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1210756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1211756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1212756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
12132cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1214756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
121554d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1216a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1217a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
12182c37256eSGarrett Wollman };
1219b287c6c7SBjoern A. Zeeb #endif /* INET */
1220df8bae1dSRodney W. Grimes 
1221fb59c426SYoshinobu Inoue #ifdef INET6
1222fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1223756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1224756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1225756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1226756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1227756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1228756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1229756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1230756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1231756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1232756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1233756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1234756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1235756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
12362cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1237756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1238756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1239a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1240a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1241fb59c426SYoshinobu Inoue };
1242fb59c426SYoshinobu Inoue #endif /* INET6 */
1243fb59c426SYoshinobu Inoue 
1244b287c6c7SBjoern A. Zeeb #ifdef INET
1245a0292f23SGarrett Wollman /*
1246a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1247a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
12485200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
12495200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
12505200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
12515200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1252a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1253a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1254a0292f23SGarrett Wollman  */
12550312fbe9SPoul-Henning Kamp static int
1256ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1257a0292f23SGarrett Wollman {
1258a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1259a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
12605200e00eSIan Dowse 	struct in_addr laddr;
12615200e00eSIan Dowse 	u_short lport;
1262c3229e05SDavid Greenman 	int error;
1263a0292f23SGarrett Wollman 
12648501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1265fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1266623dce13SRobert Watson 
1267a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
12684616026fSErmal Luçi 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
12694616026fSErmal Luçi 		if (error)
1270fa046d87SRobert Watson 			goto out;
1271a0292f23SGarrett Wollman 	}
1272a0292f23SGarrett Wollman 
1273a0292f23SGarrett Wollman 	/*
1274a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1275a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1276a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1277a0292f23SGarrett Wollman 	 */
12785200e00eSIan Dowse 	laddr = inp->inp_laddr;
12795200e00eSIan Dowse 	lport = inp->inp_lport;
12805200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1281b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
12825200e00eSIan Dowse 	if (error && oinp == NULL)
1283fa046d87SRobert Watson 		goto out;
1284fa046d87SRobert Watson 	if (oinp) {
1285fa046d87SRobert Watson 		error = EADDRINUSE;
1286fa046d87SRobert Watson 		goto out;
1287fa046d87SRobert Watson 	}
12885200e00eSIan Dowse 	inp->inp_laddr = laddr;
128915bd2b43SDavid Greenman 	in_pcbrehash(inp);
1290fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1291a0292f23SGarrett Wollman 
1292087b55eaSAndre Oppermann 	/*
1293087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1294087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1295087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1296087b55eaSAndre Oppermann 	 */
1297a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
12989b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1299a0292f23SGarrett Wollman 		tp->request_r_scale++;
1300a0292f23SGarrett Wollman 
1301a0292f23SGarrett Wollman 	soisconnecting(so);
130278b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
130357f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
1304b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
1305a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1306a45d2726SAndras Olah 
1307a0292f23SGarrett Wollman 	return 0;
1308fa046d87SRobert Watson 
1309fa046d87SRobert Watson out:
1310fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1311fa046d87SRobert Watson 	return (error);
1312a0292f23SGarrett Wollman }
1313b287c6c7SBjoern A. Zeeb #endif /* INET */
1314a0292f23SGarrett Wollman 
1315fb59c426SYoshinobu Inoue #ifdef INET6
1316fb59c426SYoshinobu Inoue static int
1317ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1318fb59c426SYoshinobu Inoue {
1319a7e201bbSAndrey V. Elsukov 	struct inpcb *inp = tp->t_inpcb;
1320fb59c426SYoshinobu Inoue 	int error;
1321fb59c426SYoshinobu Inoue 
13228501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1323fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1324623dce13SRobert Watson 
1325fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
13264616026fSErmal Luçi 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
13274616026fSErmal Luçi 		if (error)
1328fa046d87SRobert Watson 			goto out;
1329fb59c426SYoshinobu Inoue 	}
1330a7e201bbSAndrey V. Elsukov 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1331a7e201bbSAndrey V. Elsukov 	if (error != 0)
1332b598155aSRobert Watson 		goto out;
1333fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1334fb59c426SYoshinobu Inoue 
1335fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1336fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1337970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1338fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1339fb59c426SYoshinobu Inoue 
1340a7e201bbSAndrey V. Elsukov 	soisconnecting(inp->inp_socket);
134178b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
134257f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
1343b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
1344fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1345fb59c426SYoshinobu Inoue 
1346fb59c426SYoshinobu Inoue 	return 0;
1347fa046d87SRobert Watson 
1348fa046d87SRobert Watson out:
1349fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1350fa046d87SRobert Watson 	return error;
1351fb59c426SYoshinobu Inoue }
1352fb59c426SYoshinobu Inoue #endif /* INET6 */
1353fb59c426SYoshinobu Inoue 
1354cfe8b629SGarrett Wollman /*
1355b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1356b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1357b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1358b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1359b8af5dfaSRobert Watson  * from Linux.
1360b8af5dfaSRobert Watson  */
1361b8af5dfaSRobert Watson static void
1362ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1363b8af5dfaSRobert Watson {
1364b8af5dfaSRobert Watson 
13658501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1366b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1367b8af5dfaSRobert Watson 
1368b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1369b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1370b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
13713529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1372b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1373b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1374b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1375b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1376b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1377b8af5dfaSRobert Watson 	}
13785a17b6adSMichael Tuexen 	if (tp->t_flags & TF_ECN_PERMIT)
13795a17b6adSMichael Tuexen 		ti->tcpi_options |= TCPI_OPT_ECN;
13801baaf834SBruce M Simpson 
138143d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
13823ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
13831baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
13841baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
13851baaf834SBruce M Simpson 
1386b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1387b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1388b8af5dfaSRobert Watson 
1389b8af5dfaSRobert Watson 	/*
1390b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1391b8af5dfaSRobert Watson 	 */
1392c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1393535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1394b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
13951c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1396535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
139743d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
139843d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
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;
1402a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1403a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1404a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1405a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1406a6456410SNavdeep Parhar 	}
1407a6456410SNavdeep Parhar #endif
1408b8af5dfaSRobert Watson }
1409b8af5dfaSRobert Watson 
1410b8af5dfaSRobert Watson /*
14111e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
14121e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
14131e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
14141e8f5ffaSRobert Watson  * option.
1415cfe8b629SGarrett Wollman  */
1416bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
14178501a69cSRobert Watson 	INP_WLOCK(inp);							\
1418ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
14198501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1420bac5bedfSConrad Meyer 		cleanup;						\
14211e8f5ffaSRobert Watson 		return (ECONNRESET);					\
14221e8f5ffaSRobert Watson 	}								\
14231e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
14241e8f5ffaSRobert Watson } while(0)
1425bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
14261e8f5ffaSRobert Watson 
1427df8bae1dSRodney W. Grimes int
1428ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1429df8bae1dSRodney W. Grimes {
143055bceb1eSRandall Stewart 	int	error;
1431df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1432cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
143355bceb1eSRandall Stewart 	struct tcp_function_block *blk;
143455bceb1eSRandall Stewart 	struct tcp_function_set fsn;
1435df8bae1dSRodney W. Grimes 
1436cfe8b629SGarrett Wollman 	error = 0;
1437df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1438623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
14398501a69cSRobert Watson 	INP_WLOCK(inp);
1440cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1441fb59c426SYoshinobu Inoue #ifdef INET6
14425cd54324SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6PROTO) {
14438501a69cSRobert Watson 			INP_WUNLOCK(inp);
1444fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
1445b287c6c7SBjoern A. Zeeb 		}
1446fb59c426SYoshinobu Inoue #endif /* INET6 */
1447b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1448b287c6c7SBjoern A. Zeeb 		else
1449b287c6c7SBjoern A. Zeeb #endif
1450b287c6c7SBjoern A. Zeeb #ifdef INET
1451b287c6c7SBjoern A. Zeeb 		{
14528501a69cSRobert Watson 			INP_WUNLOCK(inp);
1453cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
14541e8f5ffaSRobert Watson 		}
14551e8f5ffaSRobert Watson #endif
1456df8bae1dSRodney W. Grimes 		return (error);
1457df8bae1dSRodney W. Grimes 	}
1458ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
14598501a69cSRobert Watson 		INP_WUNLOCK(inp);
14601e8f5ffaSRobert Watson 		return (ECONNRESET);
1461623dce13SRobert Watson 	}
146255bceb1eSRandall Stewart 	tp = intotcpcb(inp);
146355bceb1eSRandall Stewart 	/*
146455bceb1eSRandall Stewart 	 * Protect the TCP option TCP_FUNCTION_BLK so
146555bceb1eSRandall Stewart 	 * that a sub-function can *never* overwrite this.
146655bceb1eSRandall Stewart 	 */
146755bceb1eSRandall Stewart 	if ((sopt->sopt_dir == SOPT_SET) &&
146855bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
146955bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
147055bceb1eSRandall Stewart 		error = sooptcopyin(sopt, &fsn, sizeof fsn,
147155bceb1eSRandall Stewart 		    sizeof fsn);
147255bceb1eSRandall Stewart 		if (error)
147355bceb1eSRandall Stewart 			return (error);
147455bceb1eSRandall Stewart 		INP_WLOCK_RECHECK(inp);
147555bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
147655bceb1eSRandall Stewart 		if (blk == NULL) {
147755bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
147855bceb1eSRandall Stewart 			return (ENOENT);
147955bceb1eSRandall Stewart 		}
1480587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1481587d67c0SRandall Stewart 			/* You already have this */
1482587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1483587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1484587d67c0SRandall Stewart 			return (0);
1485587d67c0SRandall Stewart 		}
1486587d67c0SRandall Stewart 		if (tp->t_state != TCPS_CLOSED) {
1487587d67c0SRandall Stewart 			int error=EINVAL;
1488587d67c0SRandall Stewart 			/*
1489587d67c0SRandall Stewart 			 * The user has advanced the state
1490587d67c0SRandall Stewart 			 * past the initial point, we may not
1491587d67c0SRandall Stewart 			 * be able to switch.
1492587d67c0SRandall Stewart 			 */
1493587d67c0SRandall Stewart 			if (blk->tfb_tcp_handoff_ok != NULL) {
1494587d67c0SRandall Stewart 				/*
1495587d67c0SRandall Stewart 				 * Does the stack provide a
1496587d67c0SRandall Stewart 				 * query mechanism, if so it may
1497587d67c0SRandall Stewart 				 * still be possible?
1498587d67c0SRandall Stewart 				 */
1499587d67c0SRandall Stewart 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1500587d67c0SRandall Stewart 			}
1501587d67c0SRandall Stewart 			if (error) {
1502587d67c0SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1503587d67c0SRandall Stewart 				INP_WUNLOCK(inp);
1504587d67c0SRandall Stewart 				return(error);
1505587d67c0SRandall Stewart 			}
1506587d67c0SRandall Stewart 		}
150755bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
150855bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
150955bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
151055bceb1eSRandall Stewart 			return (ENOENT);
151155bceb1eSRandall Stewart 		}
151255bceb1eSRandall Stewart 		/*
151355bceb1eSRandall Stewart 		 * Release the old refcnt, the
1514587d67c0SRandall Stewart 		 * lookup acquired a ref on the
1515587d67c0SRandall Stewart 		 * new one already.
151655bceb1eSRandall Stewart 		 */
1517587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1518587d67c0SRandall Stewart 			/*
1519587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1520587d67c0SRandall Stewart 			 * the tcb is not going away.
1521587d67c0SRandall Stewart 			 */
1522587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1523587d67c0SRandall Stewart 		}
1524*3ee9c3c4SRandall Stewart #ifdef TCPHPTS
1525*3ee9c3c4SRandall Stewart 		/* Assure that we are not on any hpts */
1526*3ee9c3c4SRandall Stewart 		tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
1527*3ee9c3c4SRandall Stewart #endif
1528*3ee9c3c4SRandall Stewart 		if (blk->tfb_tcp_fb_init) {
1529*3ee9c3c4SRandall Stewart 			error = (*blk->tfb_tcp_fb_init)(tp);
1530*3ee9c3c4SRandall Stewart 			if (error) {
1531*3ee9c3c4SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1532*3ee9c3c4SRandall Stewart 				if (tp->t_fb->tfb_tcp_fb_init) {
1533*3ee9c3c4SRandall Stewart 					if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0)  {
1534*3ee9c3c4SRandall Stewart 						/* Fall back failed, drop the connection */
1535*3ee9c3c4SRandall Stewart 						INP_WUNLOCK(inp);
1536*3ee9c3c4SRandall Stewart 						soabort(so);
1537*3ee9c3c4SRandall Stewart 						return(error);
1538*3ee9c3c4SRandall Stewart 					}
1539*3ee9c3c4SRandall Stewart 				}
1540*3ee9c3c4SRandall Stewart 				goto err_out;
1541*3ee9c3c4SRandall Stewart 			}
1542*3ee9c3c4SRandall Stewart 		}
154355bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
154455bceb1eSRandall Stewart 		tp->t_fb = blk;
154555bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
154655bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
154755bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
154855bceb1eSRandall Stewart 			     sopt->sopt_name);
154955bceb1eSRandall Stewart 		}
155055bceb1eSRandall Stewart #endif
1551*3ee9c3c4SRandall Stewart err_out:
155255bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
155355bceb1eSRandall Stewart 		return (error);
155455bceb1eSRandall Stewart 	} else if ((sopt->sopt_dir == SOPT_GET) &&
155555bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1556c73b6f4dSEd Maste 		strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1557c73b6f4dSEd Maste 		    TCP_FUNCTION_NAME_LEN_MAX);
1558c73b6f4dSEd Maste 		fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
155955bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
156055bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
156155bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
156255bceb1eSRandall Stewart 		return (error);
156355bceb1eSRandall Stewart 	}
156455bceb1eSRandall Stewart 	/* Pass in the INP locked, called must unlock it */
156555bceb1eSRandall Stewart 	return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
156655bceb1eSRandall Stewart }
156755bceb1eSRandall Stewart 
15682529f56eSJonathan T. Looney /*
15692529f56eSJonathan T. Looney  * If this assert becomes untrue, we need to change the size of the buf
15702529f56eSJonathan T. Looney  * variable in tcp_default_ctloutput().
15712529f56eSJonathan T. Looney  */
15722529f56eSJonathan T. Looney #ifdef CTASSERT
15732529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
15742529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
15752529f56eSJonathan T. Looney #endif
15762529f56eSJonathan T. Looney 
157755bceb1eSRandall Stewart int
157855bceb1eSRandall Stewart tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
157955bceb1eSRandall Stewart {
158055bceb1eSRandall Stewart 	int	error, opt, optval;
158155bceb1eSRandall Stewart 	u_int	ui;
158255bceb1eSRandall Stewart 	struct	tcp_info ti;
158355bceb1eSRandall Stewart 	struct cc_algo *algo;
15842529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
1585af6fef3aSGleb Smirnoff 	size_t	len;
1586df8bae1dSRodney W. Grimes 
1587d519cedbSGleb Smirnoff 	/*
1588d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
1589d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
1590d519cedbSGleb Smirnoff 	 */
1591d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
1592d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
1593d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1594af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1595af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1596d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
1597d519cedbSGleb Smirnoff 		if (error) {
1598af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
1599d519cedbSGleb Smirnoff 			return (error);
1600d519cedbSGleb Smirnoff 		}
1601bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1602d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
1603af6fef3aSGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1604d519cedbSGleb Smirnoff 		else
1605d519cedbSGleb Smirnoff 			error = ENOENT;
1606d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1607d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
1608af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1609af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
1610d519cedbSGleb Smirnoff 		return (error);
1611d519cedbSGleb Smirnoff 	}
1612d519cedbSGleb Smirnoff 
1613cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1614cfe8b629SGarrett Wollman 	case SOPT_SET:
1615cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1616fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
161788f6b043SBruce M Simpson 		case TCP_MD5SIG:
1618fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
16198501a69cSRobert Watson 				INP_WUNLOCK(inp);
1620fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
1621fcf59617SAndrey V. Elsukov 			}
1622fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
16231cfd4b53SBruce M Simpson 			if (error)
16241e8f5ffaSRobert Watson 				return (error);
162509fe6320SNavdeep Parhar 			goto unlock_and_done;
1626fcf59617SAndrey V. Elsukov #endif /* IPSEC */
162709fe6320SNavdeep Parhar 
1628df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1629cfe8b629SGarrett Wollman 		case TCP_NOOPT:
16308501a69cSRobert Watson 			INP_WUNLOCK(inp);
1631cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1632cfe8b629SGarrett Wollman 			    sizeof optval);
1633cfe8b629SGarrett Wollman 			if (error)
16341e8f5ffaSRobert Watson 				return (error);
1635cfe8b629SGarrett Wollman 
16368501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1637cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1638cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1639cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1640cfe8b629SGarrett Wollman 				break;
1641cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1642cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1643cfe8b629SGarrett Wollman 				break;
1644cfe8b629SGarrett Wollman 			default:
1645cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1646cfe8b629SGarrett Wollman 				break;
1647cfe8b629SGarrett Wollman 			}
1648cfe8b629SGarrett Wollman 
1649cfe8b629SGarrett Wollman 			if (optval)
1650cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1651df8bae1dSRodney W. Grimes 			else
1652cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
165309fe6320SNavdeep Parhar unlock_and_done:
165409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
165509fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
165609fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
165709fe6320SNavdeep Parhar 				    sopt->sopt_name);
165809fe6320SNavdeep Parhar 			}
165909fe6320SNavdeep Parhar #endif
16608501a69cSRobert Watson 			INP_WUNLOCK(inp);
1661df8bae1dSRodney W. Grimes 			break;
1662df8bae1dSRodney W. Grimes 
1663007581c0SJonathan Lemon 		case TCP_NOPUSH:
16648501a69cSRobert Watson 			INP_WUNLOCK(inp);
1665007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1666007581c0SJonathan Lemon 			    sizeof optval);
1667007581c0SJonathan Lemon 			if (error)
16681e8f5ffaSRobert Watson 				return (error);
1669007581c0SJonathan Lemon 
16708501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1671007581c0SJonathan Lemon 			if (optval)
1672007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1673d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
1674007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1675d28b9e89SJohn Baldwin 				if (TCPS_HAVEESTABLISHED(tp->t_state))
167655bceb1eSRandall Stewart 					error = tp->t_fb->tfb_tcp_output(tp);
1677007581c0SJonathan Lemon 			}
167809fe6320SNavdeep Parhar 			goto unlock_and_done;
1679007581c0SJonathan Lemon 
1680df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
16818501a69cSRobert Watson 			INP_WUNLOCK(inp);
1682cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1683cfe8b629SGarrett Wollman 			    sizeof optval);
1684cfe8b629SGarrett Wollman 			if (error)
16851e8f5ffaSRobert Watson 				return (error);
1686df8bae1dSRodney W. Grimes 
16878501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
168853369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
1689603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
1690cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1691a0292f23SGarrett Wollman 			else
1692a0292f23SGarrett Wollman 				error = EINVAL;
169309fe6320SNavdeep Parhar 			goto unlock_and_done;
1694a0292f23SGarrett Wollman 
1695b8af5dfaSRobert Watson 		case TCP_INFO:
16968501a69cSRobert Watson 			INP_WUNLOCK(inp);
1697b8af5dfaSRobert Watson 			error = EINVAL;
1698b8af5dfaSRobert Watson 			break;
1699b8af5dfaSRobert Watson 
1700dbc42409SLawrence Stewart 		case TCP_CONGESTION:
1701dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
1702af6fef3aSGleb Smirnoff 			error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1703af6fef3aSGleb Smirnoff 			if (error)
1704dbc42409SLawrence Stewart 				break;
1705af6fef3aSGleb Smirnoff 			buf[sopt->sopt_valsize] = '\0';
1706af6fef3aSGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
170773e263b1SGleb Smirnoff 			CC_LIST_RLOCK();
170873e263b1SGleb Smirnoff 			STAILQ_FOREACH(algo, &cc_list, entries)
170973e263b1SGleb Smirnoff 				if (strncmp(buf, algo->name,
171073e263b1SGleb Smirnoff 				    TCP_CA_NAME_MAX) == 0)
171173e263b1SGleb Smirnoff 					break;
171273e263b1SGleb Smirnoff 			CC_LIST_RUNLOCK();
171373e263b1SGleb Smirnoff 			if (algo == NULL) {
1714af6fef3aSGleb Smirnoff 				INP_WUNLOCK(inp);
171573e263b1SGleb Smirnoff 				error = EINVAL;
171673e263b1SGleb Smirnoff 				break;
171773e263b1SGleb Smirnoff 			}
1718dbc42409SLawrence Stewart 			/*
171973e263b1SGleb Smirnoff 			 * We hold a write lock over the tcb so it's safe to
172073e263b1SGleb Smirnoff 			 * do these things without ordering concerns.
1721dbc42409SLawrence Stewart 			 */
1722dbc42409SLawrence Stewart 			if (CC_ALGO(tp)->cb_destroy != NULL)
1723dbc42409SLawrence Stewart 				CC_ALGO(tp)->cb_destroy(tp->ccv);
1724dbc42409SLawrence Stewart 			CC_ALGO(tp) = algo;
1725dbc42409SLawrence Stewart 			/*
172673e263b1SGleb Smirnoff 			 * If something goes pear shaped initialising the new
172773e263b1SGleb Smirnoff 			 * algo, fall back to newreno (which does not
172873e263b1SGleb Smirnoff 			 * require initialisation).
1729dbc42409SLawrence Stewart 			 */
173073e263b1SGleb Smirnoff 			if (algo->cb_init != NULL &&
173173e263b1SGleb Smirnoff 			    algo->cb_init(tp->ccv) != 0) {
1732dbc42409SLawrence Stewart 				CC_ALGO(tp) = &newreno_cc_algo;
1733dbc42409SLawrence Stewart 				/*
173473e263b1SGleb Smirnoff 				 * The only reason init should fail is
1735dbc42409SLawrence Stewart 				 * because of malloc.
1736dbc42409SLawrence Stewart 				 */
1737dbc42409SLawrence Stewart 				error = ENOMEM;
1738dbc42409SLawrence Stewart 			}
173973e263b1SGleb Smirnoff 			INP_WUNLOCK(inp);
174073e263b1SGleb Smirnoff 			break;
1741dbc42409SLawrence Stewart 
17429077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
17439077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
17449077f387SGleb Smirnoff 		case TCP_KEEPINIT:
17459077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
17469077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
17479077f387SGleb Smirnoff 			if (error)
17489077f387SGleb Smirnoff 				return (error);
17499077f387SGleb Smirnoff 
17509077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
17519077f387SGleb Smirnoff 				error = EINVAL;
17529077f387SGleb Smirnoff 				break;
17539077f387SGleb Smirnoff 			}
17549077f387SGleb Smirnoff 			ui *= hz;
17559077f387SGleb Smirnoff 
17569077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
17579077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
17589077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
17599077f387SGleb Smirnoff 				tp->t_keepidle = ui;
17609077f387SGleb Smirnoff 				/*
17619077f387SGleb Smirnoff 				 * XXX: better check current remaining
17629077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
17639077f387SGleb Smirnoff 				 */
17649077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
17659077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
17669077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
17679077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
17689077f387SGleb Smirnoff 				break;
17699077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
17709077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
17719077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
17729077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
17739077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
17749077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
17759077f387SGleb Smirnoff 				break;
17769077f387SGleb Smirnoff 			case TCP_KEEPINIT:
17779077f387SGleb Smirnoff 				tp->t_keepinit = ui;
17789077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
17799077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
17809077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
17819077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
17829077f387SGleb Smirnoff 				break;
17839077f387SGleb Smirnoff 			}
178409fe6320SNavdeep Parhar 			goto unlock_and_done;
17859077f387SGleb Smirnoff 
178685c05144SGleb Smirnoff 		case TCP_KEEPCNT:
178785c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
178885c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
178985c05144SGleb Smirnoff 			if (error)
179085c05144SGleb Smirnoff 				return (error);
179185c05144SGleb Smirnoff 
179285c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
179385c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
179485c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
179585c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
179685c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
179785c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
179885c05144SGleb Smirnoff 			goto unlock_and_done;
179985c05144SGleb Smirnoff 
180086a996e6SHiren Panchasara #ifdef TCPPCAP
180186a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
180286a996e6SHiren Panchasara 		case TCP_PCAP_IN:
180386a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
180486a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
180586a996e6SHiren Panchasara 			    sizeof optval);
180686a996e6SHiren Panchasara 			if (error)
180786a996e6SHiren Panchasara 				return (error);
180886a996e6SHiren Panchasara 
180986a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
181086a996e6SHiren Panchasara 			if (optval >= 0)
181186a996e6SHiren Panchasara 				tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
181286a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
181386a996e6SHiren Panchasara 					optval);
181486a996e6SHiren Panchasara 			else
181586a996e6SHiren Panchasara 				error = EINVAL;
181686a996e6SHiren Panchasara 			goto unlock_and_done;
181786a996e6SHiren Panchasara #endif
181886a996e6SHiren Panchasara 
1819c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
1820c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
1821c560df6fSPatrick Kelsey 
1822281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
1823c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
1824c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
1825281a0fd4SPatrick Kelsey 				return (EPERM);
1826281a0fd4SPatrick Kelsey 
1827c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
1828c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
1829281a0fd4SPatrick Kelsey 			if (error)
1830281a0fd4SPatrick Kelsey 				return (error);
1831281a0fd4SPatrick Kelsey 
1832281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
1833c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
1834c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
1835c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
1836c560df6fSPatrick Kelsey 						error = EPERM;
1837c560df6fSPatrick Kelsey 						goto unlock_and_done;
1838c560df6fSPatrick Kelsey 					}
1839c560df6fSPatrick Kelsey 
1840281a0fd4SPatrick Kelsey 					tp->t_flags |= TF_FASTOPEN;
1841c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
1842281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
1843281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
1844c560df6fSPatrick Kelsey 				} else {
1845c560df6fSPatrick Kelsey 					/*
1846c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
1847c560df6fSPatrick Kelsey 					 * stash it in the client cookie
1848c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
1849c560df6fSPatrick Kelsey 					 * connect.
1850c560df6fSPatrick Kelsey 					 */
1851c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
1852c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
1853c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
1854c560df6fSPatrick Kelsey 						       tfo_optval.psk,
1855c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
1856c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
1857c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
1858c560df6fSPatrick Kelsey 					}
1859c560df6fSPatrick Kelsey 					tp->t_flags |= TF_FASTOPEN;
1860c560df6fSPatrick Kelsey 				}
1861281a0fd4SPatrick Kelsey 			} else
1862281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1863281a0fd4SPatrick Kelsey 			goto unlock_and_done;
1864c560df6fSPatrick Kelsey 		}
1865281a0fd4SPatrick Kelsey 
1866e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
18672529f56eSJonathan T. Looney 		case TCP_LOG:
18682529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
18692529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
18702529f56eSJonathan T. Looney 			    sizeof optval);
18712529f56eSJonathan T. Looney 			if (error)
18722529f56eSJonathan T. Looney 				return (error);
18732529f56eSJonathan T. Looney 
18742529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
18752529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
18762529f56eSJonathan T. Looney 			goto unlock_and_done;
18772529f56eSJonathan T. Looney 
18782529f56eSJonathan T. Looney 		case TCP_LOGBUF:
18792529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
18802529f56eSJonathan T. Looney 			error = EINVAL;
18812529f56eSJonathan T. Looney 			break;
18822529f56eSJonathan T. Looney 
18832529f56eSJonathan T. Looney 		case TCP_LOGID:
18842529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
18852529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
18862529f56eSJonathan T. Looney 			if (error)
18872529f56eSJonathan T. Looney 				break;
18882529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
18892529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
18902529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
18912529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
18922529f56eSJonathan T. Looney 			break;
18932529f56eSJonathan T. Looney 
18942529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
18952529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
18962529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
18972529f56eSJonathan T. Looney 			error =
18982529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
18992529f56eSJonathan T. Looney 			if (error)
19002529f56eSJonathan T. Looney 				break;
19012529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
19022529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
19032529f56eSJonathan T. Looney 			if (sopt->sopt_name == TCP_LOGDUMP) {
19042529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
19052529f56eSJonathan T. Looney 				    M_WAITOK, true);
19062529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
19072529f56eSJonathan T. Looney 			} else {
19082529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
19092529f56eSJonathan T. Looney 				/*
19102529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
19112529f56eSJonathan T. Looney 				 * INP lock.
19122529f56eSJonathan T. Looney 				 */
19132529f56eSJonathan T. Looney 			}
19142529f56eSJonathan T. Looney 			break;
1915e24e5683SJonathan T. Looney #endif
19162529f56eSJonathan T. Looney 
1917df8bae1dSRodney W. Grimes 		default:
19188501a69cSRobert Watson 			INP_WUNLOCK(inp);
1919df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1920df8bae1dSRodney W. Grimes 			break;
1921df8bae1dSRodney W. Grimes 		}
1922df8bae1dSRodney W. Grimes 		break;
1923df8bae1dSRodney W. Grimes 
1924cfe8b629SGarrett Wollman 	case SOPT_GET:
19251e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
1926cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1927fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
192888f6b043SBruce M Simpson 		case TCP_MD5SIG:
1929fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
19308501a69cSRobert Watson 				INP_WUNLOCK(inp);
1931fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
1932fcf59617SAndrey V. Elsukov 			}
1933fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
19341cfd4b53SBruce M Simpson 			break;
1935265ed012SBruce M Simpson #endif
19361e8f5ffaSRobert Watson 
1937df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1938cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
19398501a69cSRobert Watson 			INP_WUNLOCK(inp);
1940b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1941df8bae1dSRodney W. Grimes 			break;
1942df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1943cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
19448501a69cSRobert Watson 			INP_WUNLOCK(inp);
1945b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1946df8bae1dSRodney W. Grimes 			break;
1947a0292f23SGarrett Wollman 		case TCP_NOOPT:
1948cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
19498501a69cSRobert Watson 			INP_WUNLOCK(inp);
1950b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1951a0292f23SGarrett Wollman 			break;
1952a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1953cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
19548501a69cSRobert Watson 			INP_WUNLOCK(inp);
1955b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1956b8af5dfaSRobert Watson 			break;
1957b8af5dfaSRobert Watson 		case TCP_INFO:
1958b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
19598501a69cSRobert Watson 			INP_WUNLOCK(inp);
1960b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
1961a0292f23SGarrett Wollman 			break;
1962dbc42409SLawrence Stewart 		case TCP_CONGESTION:
1963af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1964dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
1965af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
1966dbc42409SLawrence Stewart 			break;
19672f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
19682f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
19692f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
19702f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
19712f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
19722f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
19735a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
19742f3eb7f4SGleb Smirnoff 				break;
19752f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
19765a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
19772f3eb7f4SGleb Smirnoff 				break;
19782f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
19795a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
19802f3eb7f4SGleb Smirnoff 				break;
19812f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
19825a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
19832f3eb7f4SGleb Smirnoff 				break;
19842f3eb7f4SGleb Smirnoff 			}
19852f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
19862f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
19872f3eb7f4SGleb Smirnoff 			break;
198886a996e6SHiren Panchasara #ifdef TCPPCAP
198986a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
199086a996e6SHiren Panchasara 		case TCP_PCAP_IN:
199186a996e6SHiren Panchasara 			optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
199286a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
199386a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
199486a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
199586a996e6SHiren Panchasara 			break;
199686a996e6SHiren Panchasara #endif
1997281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
1998281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
1999281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2000281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
2001281a0fd4SPatrick Kelsey 			break;
2002e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
20032529f56eSJonathan T. Looney 		case TCP_LOG:
20042529f56eSJonathan T. Looney 			optval = tp->t_logstate;
20052529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
20062529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
20072529f56eSJonathan T. Looney 			break;
20082529f56eSJonathan T. Looney 		case TCP_LOGBUF:
20092529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
20102529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
20112529f56eSJonathan T. Looney 			break;
20122529f56eSJonathan T. Looney 		case TCP_LOGID:
20132529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
20142529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
20152529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
20162529f56eSJonathan T. Looney 			break;
20172529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
20182529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
20192529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
20202529f56eSJonathan T. Looney 			error = EINVAL;
20212529f56eSJonathan T. Looney 			break;
2022e24e5683SJonathan T. Looney #endif
2023df8bae1dSRodney W. Grimes 		default:
20248501a69cSRobert Watson 			INP_WUNLOCK(inp);
2025df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2026df8bae1dSRodney W. Grimes 			break;
2027df8bae1dSRodney W. Grimes 		}
2028df8bae1dSRodney W. Grimes 		break;
2029df8bae1dSRodney W. Grimes 	}
2030df8bae1dSRodney W. Grimes 	return (error);
2031df8bae1dSRodney W. Grimes }
20328501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2033bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2034df8bae1dSRodney W. Grimes 
203526e30fbbSDavid Greenman /*
2036df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
2037df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
2038df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
2039df8bae1dSRodney W. Grimes  */
20400312fbe9SPoul-Henning Kamp static int
2041ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so)
2042df8bae1dSRodney W. Grimes {
2043ad3f9ab3SAndre Oppermann 	struct tcpcb *tp;
2044df8bae1dSRodney W. Grimes 	struct inpcb *inp;
2045df8bae1dSRodney W. Grimes 	int error;
2046df8bae1dSRodney W. Grimes 
2047df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
2048e233e2acSAndre Oppermann 		error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
2049df8bae1dSRodney W. Grimes 		if (error)
2050df8bae1dSRodney W. Grimes 			return (error);
2051df8bae1dSRodney W. Grimes 	}
20526741ecf5SAndre Oppermann 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
20536741ecf5SAndre Oppermann 	so->so_snd.sb_flags |= SB_AUTOSIZE;
2054ff9b006dSJulien Charbon 	INP_INFO_RLOCK(&V_tcbinfo);
2055603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_tcbinfo);
2056f2de87feSRobert Watson 	if (error) {
2057ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
2058df8bae1dSRodney W. Grimes 		return (error);
2059f2de87feSRobert Watson 	}
2060df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
2061fb59c426SYoshinobu Inoue #ifdef INET6
20625cd54324SBjoern A. Zeeb 	if (inp->inp_vflag & INP_IPV6PROTO) {
2063fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
206463ec505aSMichael Tuexen 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
206563ec505aSMichael Tuexen 			inp->inp_vflag |= INP_IPV4;
2066fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
2067fb59c426SYoshinobu Inoue 	}
2068fb59c426SYoshinobu Inoue 	else
2069fb59c426SYoshinobu Inoue #endif
2070cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
2071df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
2072623dce13SRobert Watson 	if (tp == NULL) {
2073df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
20740206cdb8SBjoern A. Zeeb 		in_pcbfree(inp);
2075ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
2076df8bae1dSRodney W. Grimes 		return (ENOBUFS);
2077df8bae1dSRodney W. Grimes 	}
2078df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
20798501a69cSRobert Watson 	INP_WUNLOCK(inp);
2080ff9b006dSJulien Charbon 	INP_INFO_RUNLOCK(&V_tcbinfo);
2081bf840a17SGleb Smirnoff 	TCPSTATES_INC(TCPS_CLOSED);
2082df8bae1dSRodney W. Grimes 	return (0);
2083df8bae1dSRodney W. Grimes }
2084df8bae1dSRodney W. Grimes 
2085df8bae1dSRodney W. Grimes /*
2086df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2087df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2088df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2089df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2090df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2091df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2092df8bae1dSRodney W. Grimes  */
2093623dce13SRobert Watson static void
2094ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2095df8bae1dSRodney W. Grimes {
2096e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
2097e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
2098e6e0b5ffSRobert Watson 
2099ff9b006dSJulien Charbon 	INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
21008501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2101df8bae1dSRodney W. Grimes 
2102623dce13SRobert Watson 	/*
2103623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2104623dce13SRobert Watson 	 * socket is still open.
2105623dce13SRobert Watson 	 */
2106623dce13SRobert Watson 	if (tp->t_state < TCPS_ESTABLISHED) {
2107df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2108623dce13SRobert Watson 		KASSERT(tp != NULL,
2109623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2110623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2111243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2112623dce13SRobert Watson 		KASSERT(tp != NULL,
2113623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2114623dce13SRobert Watson 	} else {
2115df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2116df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2117623dce13SRobert Watson 		tcp_usrclosed(tp);
2118ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
211955bceb1eSRandall Stewart 			tp->t_fb->tfb_tcp_output(tp);
2120df8bae1dSRodney W. Grimes 	}
2121df8bae1dSRodney W. Grimes }
2122df8bae1dSRodney W. Grimes 
2123df8bae1dSRodney W. Grimes /*
2124df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2125df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2126df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2127df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2128df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2129df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2130df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2131df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2132df8bae1dSRodney W. Grimes  */
2133623dce13SRobert Watson static void
2134ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2135df8bae1dSRodney W. Grimes {
2136df8bae1dSRodney W. Grimes 
2137ff9b006dSJulien Charbon 	INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
21388501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
2139e6e0b5ffSRobert Watson 
2140df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2141df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
214209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
214309fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
214409fe6320SNavdeep Parhar #endif
2145550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2146bc65987aSKip Macy 		/* FALLTHROUGH */
2147bc65987aSKip Macy 	case TCPS_CLOSED:
2148df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2149623dce13SRobert Watson 		/*
2150623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2151623dce13SRobert Watson 		 * still open.
2152623dce13SRobert Watson 		 */
2153623dce13SRobert Watson 		KASSERT(tp != NULL,
2154623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2155df8bae1dSRodney W. Grimes 		break;
2156df8bae1dSRodney W. Grimes 
2157a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2158df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2159a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2160a0292f23SGarrett Wollman 		break;
2161a0292f23SGarrett Wollman 
2162df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
216357f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2164df8bae1dSRodney W. Grimes 		break;
2165df8bae1dSRodney W. Grimes 
2166df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
216757f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2168df8bae1dSRodney W. Grimes 		break;
2169df8bae1dSRodney W. Grimes 	}
2170abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
2171df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
2172abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
21737c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
21747c72af87SMohan Srinivasan 			int timeout;
21757c72af87SMohan Srinivasan 
21767c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
21779077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2178b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2179b6239c4aSAndras Olah 		}
2180df8bae1dSRodney W. Grimes 	}
21817c72af87SMohan Srinivasan }
2182497057eeSRobert Watson 
2183497057eeSRobert Watson #ifdef DDB
2184497057eeSRobert Watson static void
2185497057eeSRobert Watson db_print_indent(int indent)
2186497057eeSRobert Watson {
2187497057eeSRobert Watson 	int i;
2188497057eeSRobert Watson 
2189497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2190497057eeSRobert Watson 		db_printf(" ");
2191497057eeSRobert Watson }
2192497057eeSRobert Watson 
2193497057eeSRobert Watson static void
2194497057eeSRobert Watson db_print_tstate(int t_state)
2195497057eeSRobert Watson {
2196497057eeSRobert Watson 
2197497057eeSRobert Watson 	switch (t_state) {
2198497057eeSRobert Watson 	case TCPS_CLOSED:
2199497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2200497057eeSRobert Watson 		return;
2201497057eeSRobert Watson 
2202497057eeSRobert Watson 	case TCPS_LISTEN:
2203497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2204497057eeSRobert Watson 		return;
2205497057eeSRobert Watson 
2206497057eeSRobert Watson 	case TCPS_SYN_SENT:
2207497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2208497057eeSRobert Watson 		return;
2209497057eeSRobert Watson 
2210497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2211497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2212497057eeSRobert Watson 		return;
2213497057eeSRobert Watson 
2214497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2215497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2216497057eeSRobert Watson 		return;
2217497057eeSRobert Watson 
2218497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2219497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2220497057eeSRobert Watson 		return;
2221497057eeSRobert Watson 
2222497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2223497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2224497057eeSRobert Watson 		return;
2225497057eeSRobert Watson 
2226497057eeSRobert Watson 	case TCPS_CLOSING:
2227497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2228497057eeSRobert Watson 		return;
2229497057eeSRobert Watson 
2230497057eeSRobert Watson 	case TCPS_LAST_ACK:
2231497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2232497057eeSRobert Watson 		return;
2233497057eeSRobert Watson 
2234497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2235497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2236497057eeSRobert Watson 		return;
2237497057eeSRobert Watson 
2238497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2239497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2240497057eeSRobert Watson 		return;
2241497057eeSRobert Watson 
2242497057eeSRobert Watson 	default:
2243497057eeSRobert Watson 		db_printf("unknown");
2244497057eeSRobert Watson 		return;
2245497057eeSRobert Watson 	}
2246497057eeSRobert Watson }
2247497057eeSRobert Watson 
2248497057eeSRobert Watson static void
2249497057eeSRobert Watson db_print_tflags(u_int t_flags)
2250497057eeSRobert Watson {
2251497057eeSRobert Watson 	int comma;
2252497057eeSRobert Watson 
2253497057eeSRobert Watson 	comma = 0;
2254497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2255497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2256497057eeSRobert Watson 		comma = 1;
2257497057eeSRobert Watson 	}
2258497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2259497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2260497057eeSRobert Watson 		comma = 1;
2261497057eeSRobert Watson 	}
2262497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2263497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2264497057eeSRobert Watson 		comma = 1;
2265497057eeSRobert Watson 	}
2266497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2267497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2268497057eeSRobert Watson 		comma = 1;
2269497057eeSRobert Watson 	}
2270497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2271497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2272497057eeSRobert Watson 		comma = 1;
2273497057eeSRobert Watson 	}
2274497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2275497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2276497057eeSRobert Watson 		comma = 1;
2277497057eeSRobert Watson 	}
2278497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2279497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2280497057eeSRobert Watson 		comma = 1;
2281497057eeSRobert Watson 	}
2282497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2283497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2284497057eeSRobert Watson 		comma = 1;
2285497057eeSRobert Watson 	}
2286497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2287497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2288497057eeSRobert Watson 		comma = 1;
2289497057eeSRobert Watson 	}
2290497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2291497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2292497057eeSRobert Watson 		comma = 1;
2293497057eeSRobert Watson 	}
2294497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2295497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2296497057eeSRobert Watson 		comma = 1;
2297497057eeSRobert Watson 	}
2298497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2299497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2300497057eeSRobert Watson 		comma = 1;
2301497057eeSRobert Watson 	}
2302497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2303497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2304497057eeSRobert Watson 		comma = 1;
2305497057eeSRobert Watson 	}
2306497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2307497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2308497057eeSRobert Watson 		comma = 1;
2309497057eeSRobert Watson 	}
2310497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
2311497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2312497057eeSRobert Watson 		comma = 1;
2313497057eeSRobert Watson 	}
2314497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2315497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2316497057eeSRobert Watson 		comma = 1;
2317497057eeSRobert Watson 	}
2318497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2319497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2320497057eeSRobert Watson 		comma = 1;
2321497057eeSRobert Watson 	}
2322497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2323497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2324497057eeSRobert Watson 		comma = 1;
2325497057eeSRobert Watson 	}
2326dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
2327dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2328dbc42409SLawrence Stewart 		comma = 1;
2329dbc42409SLawrence Stewart 	}
2330497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2331497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2332497057eeSRobert Watson 		comma = 1;
2333497057eeSRobert Watson 	}
2334497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2335497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2336497057eeSRobert Watson 		comma = 1;
2337497057eeSRobert Watson 	}
2338497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2339497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2340497057eeSRobert Watson 		comma = 1;
2341497057eeSRobert Watson 	}
2342497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2343497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2344497057eeSRobert Watson 		comma = 1;
2345497057eeSRobert Watson 	}
2346f2512ba1SRui Paulo 	if (t_flags & TF_ECN_PERMIT) {
2347f2512ba1SRui Paulo 		db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2348f2512ba1SRui Paulo 		comma = 1;
2349f2512ba1SRui Paulo 	}
2350281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2351281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2352281a0fd4SPatrick Kelsey 		comma = 1;
2353281a0fd4SPatrick Kelsey 	}
2354497057eeSRobert Watson }
2355497057eeSRobert Watson 
2356497057eeSRobert Watson static void
2357497057eeSRobert Watson db_print_toobflags(char t_oobflags)
2358497057eeSRobert Watson {
2359497057eeSRobert Watson 	int comma;
2360497057eeSRobert Watson 
2361497057eeSRobert Watson 	comma = 0;
2362497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
2363497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2364497057eeSRobert Watson 		comma = 1;
2365497057eeSRobert Watson 	}
2366497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
2367497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2368497057eeSRobert Watson 		comma = 1;
2369497057eeSRobert Watson 	}
2370497057eeSRobert Watson }
2371497057eeSRobert Watson 
2372497057eeSRobert Watson static void
2373497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2374497057eeSRobert Watson {
2375497057eeSRobert Watson 
2376497057eeSRobert Watson 	db_print_indent(indent);
2377497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
2378497057eeSRobert Watson 
2379497057eeSRobert Watson 	indent += 2;
2380497057eeSRobert Watson 
2381497057eeSRobert Watson 	db_print_indent(indent);
2382497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
23834741bfcbSPatrick Kelsey 	   LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2384497057eeSRobert Watson 
2385497057eeSRobert Watson 	db_print_indent(indent);
238685d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
2387e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2388497057eeSRobert Watson 
2389497057eeSRobert Watson 	db_print_indent(indent);
2390e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2391e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
2392497057eeSRobert Watson 
2393497057eeSRobert Watson 	db_print_indent(indent);
2394497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
2395497057eeSRobert Watson 	db_print_tstate(tp->t_state);
2396497057eeSRobert Watson 	db_printf(")\n");
2397497057eeSRobert Watson 
2398497057eeSRobert Watson 	db_print_indent(indent);
2399497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
2400497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
2401497057eeSRobert Watson 	db_printf(")\n");
2402497057eeSRobert Watson 
2403497057eeSRobert Watson 	db_print_indent(indent);
2404497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
2405497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
2406497057eeSRobert Watson 
2407497057eeSRobert Watson 	db_print_indent(indent);
2408497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
2409497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2410497057eeSRobert Watson 
2411497057eeSRobert Watson 	db_print_indent(indent);
2412497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
2413497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
2414497057eeSRobert Watson 
2415497057eeSRobert Watson 	db_print_indent(indent);
24163ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
2417497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2418497057eeSRobert Watson 
2419497057eeSRobert Watson 	db_print_indent(indent);
24203ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
24211c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
2422497057eeSRobert Watson 
2423497057eeSRobert Watson 	db_print_indent(indent);
24243ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
24251c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2426497057eeSRobert Watson 
2427497057eeSRobert Watson 	db_print_indent(indent);
24280c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
24290c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
2430497057eeSRobert Watson 
2431497057eeSRobert Watson 	db_print_indent(indent);
24321c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
24331c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
2434497057eeSRobert Watson 
2435497057eeSRobert Watson 	db_print_indent(indent);
24361c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
24371c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2438497057eeSRobert Watson 
2439497057eeSRobert Watson 	db_print_indent(indent);
2440497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
2441497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2442497057eeSRobert Watson 	    tp->t_rttbest);
2443497057eeSRobert Watson 
2444497057eeSRobert Watson 	db_print_indent(indent);
24453ac12506SJonathan T. Looney 	db_printf("t_rttupdated: %lu   max_sndwnd: %u   t_softerror: %d\n",
2446497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2447497057eeSRobert Watson 
2448497057eeSRobert Watson 	db_print_indent(indent);
2449497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2450497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
2451497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
2452497057eeSRobert Watson 
2453497057eeSRobert Watson 	db_print_indent(indent);
2454497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
2455497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2456497057eeSRobert Watson 
2457497057eeSRobert Watson 	db_print_indent(indent);
24589f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
24591a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
2460497057eeSRobert Watson 
2461497057eeSRobert Watson 	db_print_indent(indent);
2462497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
24633ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2464497057eeSRobert Watson 
2465497057eeSRobert Watson 	db_print_indent(indent);
24663ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
24679f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2468497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
2469497057eeSRobert Watson 
2470497057eeSRobert Watson 	db_print_indent(indent);
24713529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
24723529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2473497057eeSRobert Watson 
2474497057eeSRobert Watson 	db_print_indent(indent);
2475497057eeSRobert Watson 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
2476497057eeSRobert Watson 	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2477497057eeSRobert Watson 
2478497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
2479497057eeSRobert Watson 
2480497057eeSRobert Watson 	db_print_indent(indent);
2481497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
2482497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2483497057eeSRobert Watson }
2484497057eeSRobert Watson 
2485497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2486497057eeSRobert Watson {
2487497057eeSRobert Watson 	struct tcpcb *tp;
2488497057eeSRobert Watson 
2489497057eeSRobert Watson 	if (!have_addr) {
2490497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
2491497057eeSRobert Watson 		return;
2492497057eeSRobert Watson 	}
2493497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
2494497057eeSRobert Watson 
2495497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
2496497057eeSRobert Watson }
2497497057eeSRobert Watson #endif
2498