xref: /freebsd/sys/netinet/tcp_usrreq.c (revision c73b6f4da916be77e1c1f0d1b3919bb412768c81)
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();
1098c78cbc7bSRobert Watson 		tcp_drop(tp, ECONNABORTED);
1099a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
11005d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1101c78cbc7bSRobert Watson 	}
1102ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1103a152f8a3SRobert Watson 		SOCK_LOCK(so);
1104a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1105a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1106ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1107a152f8a3SRobert Watson 	}
11088501a69cSRobert Watson 	INP_WUNLOCK(inp);
1109ff9b006dSJulien Charbon 	INP_INFO_RUNLOCK(&V_tcbinfo);
1110a152f8a3SRobert Watson }
1111a152f8a3SRobert Watson 
1112a152f8a3SRobert Watson /*
1113a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1114a152f8a3SRobert Watson  */
1115a152f8a3SRobert Watson static void
1116a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1117a152f8a3SRobert Watson {
1118a152f8a3SRobert Watson 	struct inpcb *inp;
1119a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
1120a152f8a3SRobert Watson 	TCPDEBUG0;
1121a152f8a3SRobert Watson 
1122a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1123a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1124a152f8a3SRobert Watson 
1125ff9b006dSJulien Charbon 	INP_INFO_RLOCK(&V_tcbinfo);
11268501a69cSRobert Watson 	INP_WLOCK(inp);
1127a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1128a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1129a152f8a3SRobert Watson 
1130a152f8a3SRobert Watson 	/*
1131a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
1132a152f8a3SRobert Watson 	 * a disconnect.
1133a152f8a3SRobert Watson 	 */
1134ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1135ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1136a152f8a3SRobert Watson 		tp = intotcpcb(inp);
1137a152f8a3SRobert Watson 		TCPDEBUG1();
1138a152f8a3SRobert Watson 		tcp_disconnect(tp);
1139a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
11405d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1141a152f8a3SRobert Watson 	}
1142ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1143a152f8a3SRobert Watson 		SOCK_LOCK(so);
1144a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1145a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1146ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1147a152f8a3SRobert Watson 	}
11488501a69cSRobert Watson 	INP_WUNLOCK(inp);
1149ff9b006dSJulien Charbon 	INP_INFO_RUNLOCK(&V_tcbinfo);
11502c37256eSGarrett Wollman }
11512c37256eSGarrett Wollman 
11522c37256eSGarrett Wollman /*
11532c37256eSGarrett Wollman  * Receive out-of-band data.
11542c37256eSGarrett Wollman  */
11552c37256eSGarrett Wollman static int
11562c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
11572c37256eSGarrett Wollman {
11582c37256eSGarrett Wollman 	int error = 0;
1159f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1160623dce13SRobert Watson 	struct tcpcb *tp = NULL;
11612c37256eSGarrett Wollman 
1162623dce13SRobert Watson 	TCPDEBUG0;
1163623dce13SRobert Watson 	inp = sotoinpcb(so);
1164623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
11658501a69cSRobert Watson 	INP_WLOCK(inp);
1166ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
116721367f63SSam Leffler 		error = ECONNRESET;
1168623dce13SRobert Watson 		goto out;
1169623dce13SRobert Watson 	}
1170623dce13SRobert Watson 	tp = intotcpcb(inp);
1171623dce13SRobert Watson 	TCPDEBUG1();
11722c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1173c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
11744cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
11754cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
11762c37256eSGarrett Wollman 		error = EINVAL;
11772c37256eSGarrett Wollman 		goto out;
11782c37256eSGarrett Wollman 	}
11792c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
11802c37256eSGarrett Wollman 		error = EWOULDBLOCK;
11812c37256eSGarrett Wollman 		goto out;
11822c37256eSGarrett Wollman 	}
11832c37256eSGarrett Wollman 	m->m_len = 1;
11842c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
11852c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
11862c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1187623dce13SRobert Watson 
1188623dce13SRobert Watson out:
1189623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
11905d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
11918501a69cSRobert Watson 	INP_WUNLOCK(inp);
1192623dce13SRobert Watson 	return (error);
11932c37256eSGarrett Wollman }
11942c37256eSGarrett Wollman 
1195b287c6c7SBjoern A. Zeeb #ifdef INET
11962c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1197756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1198756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1199756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1200756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1201756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1202756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1203756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1204756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1205756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
120654d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1207756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1208756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1209756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
12102cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1211756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
121254d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1213a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1214a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
12152c37256eSGarrett Wollman };
1216b287c6c7SBjoern A. Zeeb #endif /* INET */
1217df8bae1dSRodney W. Grimes 
1218fb59c426SYoshinobu Inoue #ifdef INET6
1219fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1220756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1221756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1222756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1223756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1224756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1225756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1226756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1227756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1228756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1229756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1230756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1231756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1232756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
12332cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1234756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1235756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1236a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1237a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1238fb59c426SYoshinobu Inoue };
1239fb59c426SYoshinobu Inoue #endif /* INET6 */
1240fb59c426SYoshinobu Inoue 
1241b287c6c7SBjoern A. Zeeb #ifdef INET
1242a0292f23SGarrett Wollman /*
1243a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1244a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
12455200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
12465200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
12475200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
12485200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1249a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1250a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1251a0292f23SGarrett Wollman  */
12520312fbe9SPoul-Henning Kamp static int
1253ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1254a0292f23SGarrett Wollman {
1255a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1256a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
12575200e00eSIan Dowse 	struct in_addr laddr;
12585200e00eSIan Dowse 	u_short lport;
1259c3229e05SDavid Greenman 	int error;
1260a0292f23SGarrett Wollman 
12618501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1262fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1263623dce13SRobert Watson 
1264a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
12654616026fSErmal Luçi 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
12664616026fSErmal Luçi 		if (error)
1267fa046d87SRobert Watson 			goto out;
1268a0292f23SGarrett Wollman 	}
1269a0292f23SGarrett Wollman 
1270a0292f23SGarrett Wollman 	/*
1271a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1272a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1273a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1274a0292f23SGarrett Wollman 	 */
12755200e00eSIan Dowse 	laddr = inp->inp_laddr;
12765200e00eSIan Dowse 	lport = inp->inp_lport;
12775200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1278b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
12795200e00eSIan Dowse 	if (error && oinp == NULL)
1280fa046d87SRobert Watson 		goto out;
1281fa046d87SRobert Watson 	if (oinp) {
1282fa046d87SRobert Watson 		error = EADDRINUSE;
1283fa046d87SRobert Watson 		goto out;
1284fa046d87SRobert Watson 	}
12855200e00eSIan Dowse 	inp->inp_laddr = laddr;
128615bd2b43SDavid Greenman 	in_pcbrehash(inp);
1287fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1288a0292f23SGarrett Wollman 
1289087b55eaSAndre Oppermann 	/*
1290087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1291087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1292087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1293087b55eaSAndre Oppermann 	 */
1294a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
12959b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1296a0292f23SGarrett Wollman 		tp->request_r_scale++;
1297a0292f23SGarrett Wollman 
1298a0292f23SGarrett Wollman 	soisconnecting(so);
129978b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
130057f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
1301b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
1302a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1303a45d2726SAndras Olah 
1304a0292f23SGarrett Wollman 	return 0;
1305fa046d87SRobert Watson 
1306fa046d87SRobert Watson out:
1307fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1308fa046d87SRobert Watson 	return (error);
1309a0292f23SGarrett Wollman }
1310b287c6c7SBjoern A. Zeeb #endif /* INET */
1311a0292f23SGarrett Wollman 
1312fb59c426SYoshinobu Inoue #ifdef INET6
1313fb59c426SYoshinobu Inoue static int
1314ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1315fb59c426SYoshinobu Inoue {
1316a7e201bbSAndrey V. Elsukov 	struct inpcb *inp = tp->t_inpcb;
1317fb59c426SYoshinobu Inoue 	int error;
1318fb59c426SYoshinobu Inoue 
13198501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1320fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1321623dce13SRobert Watson 
1322fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
13234616026fSErmal Luçi 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
13244616026fSErmal Luçi 		if (error)
1325fa046d87SRobert Watson 			goto out;
1326fb59c426SYoshinobu Inoue 	}
1327a7e201bbSAndrey V. Elsukov 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1328a7e201bbSAndrey V. Elsukov 	if (error != 0)
1329b598155aSRobert Watson 		goto out;
1330fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1331fb59c426SYoshinobu Inoue 
1332fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1333fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1334970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1335fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1336fb59c426SYoshinobu Inoue 
1337a7e201bbSAndrey V. Elsukov 	soisconnecting(inp->inp_socket);
133878b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
133957f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
1340b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
1341fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1342fb59c426SYoshinobu Inoue 
1343fb59c426SYoshinobu Inoue 	return 0;
1344fa046d87SRobert Watson 
1345fa046d87SRobert Watson out:
1346fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1347fa046d87SRobert Watson 	return error;
1348fb59c426SYoshinobu Inoue }
1349fb59c426SYoshinobu Inoue #endif /* INET6 */
1350fb59c426SYoshinobu Inoue 
1351cfe8b629SGarrett Wollman /*
1352b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1353b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1354b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1355b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1356b8af5dfaSRobert Watson  * from Linux.
1357b8af5dfaSRobert Watson  */
1358b8af5dfaSRobert Watson static void
1359ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1360b8af5dfaSRobert Watson {
1361b8af5dfaSRobert Watson 
13628501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1363b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1364b8af5dfaSRobert Watson 
1365b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1366b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1367b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
13683529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1369b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1370b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1371b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1372b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1373b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1374b8af5dfaSRobert Watson 	}
13755a17b6adSMichael Tuexen 	if (tp->t_flags & TF_ECN_PERMIT)
13765a17b6adSMichael Tuexen 		ti->tcpi_options |= TCPI_OPT_ECN;
13771baaf834SBruce M Simpson 
137843d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
13793ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
13801baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
13811baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
13821baaf834SBruce M Simpson 
1383b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1384b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1385b8af5dfaSRobert Watson 
1386b8af5dfaSRobert Watson 	/*
1387b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1388b8af5dfaSRobert Watson 	 */
1389c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1390535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1391b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
13921c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1393535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
139443d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
139543d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1396f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1397f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1398f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1399a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1400a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1401a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1402a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1403a6456410SNavdeep Parhar 	}
1404a6456410SNavdeep Parhar #endif
1405b8af5dfaSRobert Watson }
1406b8af5dfaSRobert Watson 
1407b8af5dfaSRobert Watson /*
14081e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
14091e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
14101e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
14111e8f5ffaSRobert Watson  * option.
1412cfe8b629SGarrett Wollman  */
1413bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
14148501a69cSRobert Watson 	INP_WLOCK(inp);							\
1415ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
14168501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1417bac5bedfSConrad Meyer 		cleanup;						\
14181e8f5ffaSRobert Watson 		return (ECONNRESET);					\
14191e8f5ffaSRobert Watson 	}								\
14201e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
14211e8f5ffaSRobert Watson } while(0)
1422bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
14231e8f5ffaSRobert Watson 
1424df8bae1dSRodney W. Grimes int
1425ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1426df8bae1dSRodney W. Grimes {
142755bceb1eSRandall Stewart 	int	error;
1428df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1429cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
143055bceb1eSRandall Stewart 	struct tcp_function_block *blk;
143155bceb1eSRandall Stewart 	struct tcp_function_set fsn;
1432df8bae1dSRodney W. Grimes 
1433cfe8b629SGarrett Wollman 	error = 0;
1434df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1435623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
14368501a69cSRobert Watson 	INP_WLOCK(inp);
1437cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1438fb59c426SYoshinobu Inoue #ifdef INET6
14395cd54324SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6PROTO) {
14408501a69cSRobert Watson 			INP_WUNLOCK(inp);
1441fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
1442b287c6c7SBjoern A. Zeeb 		}
1443fb59c426SYoshinobu Inoue #endif /* INET6 */
1444b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1445b287c6c7SBjoern A. Zeeb 		else
1446b287c6c7SBjoern A. Zeeb #endif
1447b287c6c7SBjoern A. Zeeb #ifdef INET
1448b287c6c7SBjoern A. Zeeb 		{
14498501a69cSRobert Watson 			INP_WUNLOCK(inp);
1450cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
14511e8f5ffaSRobert Watson 		}
14521e8f5ffaSRobert Watson #endif
1453df8bae1dSRodney W. Grimes 		return (error);
1454df8bae1dSRodney W. Grimes 	}
1455ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
14568501a69cSRobert Watson 		INP_WUNLOCK(inp);
14571e8f5ffaSRobert Watson 		return (ECONNRESET);
1458623dce13SRobert Watson 	}
145955bceb1eSRandall Stewart 	tp = intotcpcb(inp);
146055bceb1eSRandall Stewart 	/*
146155bceb1eSRandall Stewart 	 * Protect the TCP option TCP_FUNCTION_BLK so
146255bceb1eSRandall Stewart 	 * that a sub-function can *never* overwrite this.
146355bceb1eSRandall Stewart 	 */
146455bceb1eSRandall Stewart 	if ((sopt->sopt_dir == SOPT_SET) &&
146555bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
146655bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
146755bceb1eSRandall Stewart 		error = sooptcopyin(sopt, &fsn, sizeof fsn,
146855bceb1eSRandall Stewart 		    sizeof fsn);
146955bceb1eSRandall Stewart 		if (error)
147055bceb1eSRandall Stewart 			return (error);
147155bceb1eSRandall Stewart 		INP_WLOCK_RECHECK(inp);
147255bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
147355bceb1eSRandall Stewart 		if (blk == NULL) {
147455bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
147555bceb1eSRandall Stewart 			return (ENOENT);
147655bceb1eSRandall Stewart 		}
1477587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1478587d67c0SRandall Stewart 			/* You already have this */
1479587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1480587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1481587d67c0SRandall Stewart 			return (0);
1482587d67c0SRandall Stewart 		}
1483587d67c0SRandall Stewart 		if (tp->t_state != TCPS_CLOSED) {
1484587d67c0SRandall Stewart 			int error=EINVAL;
1485587d67c0SRandall Stewart 			/*
1486587d67c0SRandall Stewart 			 * The user has advanced the state
1487587d67c0SRandall Stewart 			 * past the initial point, we may not
1488587d67c0SRandall Stewart 			 * be able to switch.
1489587d67c0SRandall Stewart 			 */
1490587d67c0SRandall Stewart 			if (blk->tfb_tcp_handoff_ok != NULL) {
1491587d67c0SRandall Stewart 				/*
1492587d67c0SRandall Stewart 				 * Does the stack provide a
1493587d67c0SRandall Stewart 				 * query mechanism, if so it may
1494587d67c0SRandall Stewart 				 * still be possible?
1495587d67c0SRandall Stewart 				 */
1496587d67c0SRandall Stewart 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1497587d67c0SRandall Stewart 			}
1498587d67c0SRandall Stewart 			if (error) {
1499587d67c0SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1500587d67c0SRandall Stewart 				INP_WUNLOCK(inp);
1501587d67c0SRandall Stewart 				return(error);
1502587d67c0SRandall Stewart 			}
1503587d67c0SRandall Stewart 		}
150455bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
150555bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
150655bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
150755bceb1eSRandall Stewart 			return (ENOENT);
150855bceb1eSRandall Stewart 		}
150955bceb1eSRandall Stewart 		/*
151055bceb1eSRandall Stewart 		 * Release the old refcnt, the
1511587d67c0SRandall Stewart 		 * lookup acquired a ref on the
1512587d67c0SRandall Stewart 		 * new one already.
151355bceb1eSRandall Stewart 		 */
1514587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1515587d67c0SRandall Stewart 			/*
1516587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1517587d67c0SRandall Stewart 			 * the tcb is not going away.
1518587d67c0SRandall Stewart 			 */
1519587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1520587d67c0SRandall Stewart 		}
152155bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
152255bceb1eSRandall Stewart 		tp->t_fb = blk;
152355bceb1eSRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_init) {
152455bceb1eSRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_init)(tp);
152555bceb1eSRandall Stewart 		}
152655bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
152755bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
152855bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
152955bceb1eSRandall Stewart 			     sopt->sopt_name);
153055bceb1eSRandall Stewart 		}
153155bceb1eSRandall Stewart #endif
153255bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
153355bceb1eSRandall Stewart 		return (error);
153455bceb1eSRandall Stewart 	} else if ((sopt->sopt_dir == SOPT_GET) &&
153555bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1536*c73b6f4dSEd Maste 		strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1537*c73b6f4dSEd Maste 		    TCP_FUNCTION_NAME_LEN_MAX);
1538*c73b6f4dSEd Maste 		fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
153955bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
154055bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
154155bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
154255bceb1eSRandall Stewart 		return (error);
154355bceb1eSRandall Stewart 	}
154455bceb1eSRandall Stewart 	/* Pass in the INP locked, called must unlock it */
154555bceb1eSRandall Stewart 	return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
154655bceb1eSRandall Stewart }
154755bceb1eSRandall Stewart 
15482529f56eSJonathan T. Looney /*
15492529f56eSJonathan T. Looney  * If this assert becomes untrue, we need to change the size of the buf
15502529f56eSJonathan T. Looney  * variable in tcp_default_ctloutput().
15512529f56eSJonathan T. Looney  */
15522529f56eSJonathan T. Looney #ifdef CTASSERT
15532529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
15542529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
15552529f56eSJonathan T. Looney #endif
15562529f56eSJonathan T. Looney 
155755bceb1eSRandall Stewart int
155855bceb1eSRandall Stewart tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
155955bceb1eSRandall Stewart {
156055bceb1eSRandall Stewart 	int	error, opt, optval;
156155bceb1eSRandall Stewart 	u_int	ui;
156255bceb1eSRandall Stewart 	struct	tcp_info ti;
156355bceb1eSRandall Stewart 	struct cc_algo *algo;
15642529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
1565af6fef3aSGleb Smirnoff 	size_t	len;
1566df8bae1dSRodney W. Grimes 
1567d519cedbSGleb Smirnoff 	/*
1568d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
1569d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
1570d519cedbSGleb Smirnoff 	 */
1571d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
1572d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
1573d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1574af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1575af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1576d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
1577d519cedbSGleb Smirnoff 		if (error) {
1578af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
1579d519cedbSGleb Smirnoff 			return (error);
1580d519cedbSGleb Smirnoff 		}
1581bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1582d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
1583af6fef3aSGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1584d519cedbSGleb Smirnoff 		else
1585d519cedbSGleb Smirnoff 			error = ENOENT;
1586d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1587d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
1588af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1589af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
1590d519cedbSGleb Smirnoff 		return (error);
1591d519cedbSGleb Smirnoff 	}
1592d519cedbSGleb Smirnoff 
1593cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1594cfe8b629SGarrett Wollman 	case SOPT_SET:
1595cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1596fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
159788f6b043SBruce M Simpson 		case TCP_MD5SIG:
1598fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
15998501a69cSRobert Watson 				INP_WUNLOCK(inp);
1600fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
1601fcf59617SAndrey V. Elsukov 			}
1602fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
16031cfd4b53SBruce M Simpson 			if (error)
16041e8f5ffaSRobert Watson 				return (error);
160509fe6320SNavdeep Parhar 			goto unlock_and_done;
1606fcf59617SAndrey V. Elsukov #endif /* IPSEC */
160709fe6320SNavdeep Parhar 
1608df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1609cfe8b629SGarrett Wollman 		case TCP_NOOPT:
16108501a69cSRobert Watson 			INP_WUNLOCK(inp);
1611cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1612cfe8b629SGarrett Wollman 			    sizeof optval);
1613cfe8b629SGarrett Wollman 			if (error)
16141e8f5ffaSRobert Watson 				return (error);
1615cfe8b629SGarrett Wollman 
16168501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1617cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1618cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1619cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1620cfe8b629SGarrett Wollman 				break;
1621cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1622cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1623cfe8b629SGarrett Wollman 				break;
1624cfe8b629SGarrett Wollman 			default:
1625cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1626cfe8b629SGarrett Wollman 				break;
1627cfe8b629SGarrett Wollman 			}
1628cfe8b629SGarrett Wollman 
1629cfe8b629SGarrett Wollman 			if (optval)
1630cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1631df8bae1dSRodney W. Grimes 			else
1632cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
163309fe6320SNavdeep Parhar unlock_and_done:
163409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
163509fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
163609fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
163709fe6320SNavdeep Parhar 				    sopt->sopt_name);
163809fe6320SNavdeep Parhar 			}
163909fe6320SNavdeep Parhar #endif
16408501a69cSRobert Watson 			INP_WUNLOCK(inp);
1641df8bae1dSRodney W. Grimes 			break;
1642df8bae1dSRodney W. Grimes 
1643007581c0SJonathan Lemon 		case TCP_NOPUSH:
16448501a69cSRobert Watson 			INP_WUNLOCK(inp);
1645007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1646007581c0SJonathan Lemon 			    sizeof optval);
1647007581c0SJonathan Lemon 			if (error)
16481e8f5ffaSRobert Watson 				return (error);
1649007581c0SJonathan Lemon 
16508501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1651007581c0SJonathan Lemon 			if (optval)
1652007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1653d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
1654007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1655d28b9e89SJohn Baldwin 				if (TCPS_HAVEESTABLISHED(tp->t_state))
165655bceb1eSRandall Stewart 					error = tp->t_fb->tfb_tcp_output(tp);
1657007581c0SJonathan Lemon 			}
165809fe6320SNavdeep Parhar 			goto unlock_and_done;
1659007581c0SJonathan Lemon 
1660df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
16618501a69cSRobert Watson 			INP_WUNLOCK(inp);
1662cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1663cfe8b629SGarrett Wollman 			    sizeof optval);
1664cfe8b629SGarrett Wollman 			if (error)
16651e8f5ffaSRobert Watson 				return (error);
1666df8bae1dSRodney W. Grimes 
16678501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
166853369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
1669603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
1670cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1671a0292f23SGarrett Wollman 			else
1672a0292f23SGarrett Wollman 				error = EINVAL;
167309fe6320SNavdeep Parhar 			goto unlock_and_done;
1674a0292f23SGarrett Wollman 
1675b8af5dfaSRobert Watson 		case TCP_INFO:
16768501a69cSRobert Watson 			INP_WUNLOCK(inp);
1677b8af5dfaSRobert Watson 			error = EINVAL;
1678b8af5dfaSRobert Watson 			break;
1679b8af5dfaSRobert Watson 
1680dbc42409SLawrence Stewart 		case TCP_CONGESTION:
1681dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
1682af6fef3aSGleb Smirnoff 			error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1683af6fef3aSGleb Smirnoff 			if (error)
1684dbc42409SLawrence Stewart 				break;
1685af6fef3aSGleb Smirnoff 			buf[sopt->sopt_valsize] = '\0';
1686af6fef3aSGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
168773e263b1SGleb Smirnoff 			CC_LIST_RLOCK();
168873e263b1SGleb Smirnoff 			STAILQ_FOREACH(algo, &cc_list, entries)
168973e263b1SGleb Smirnoff 				if (strncmp(buf, algo->name,
169073e263b1SGleb Smirnoff 				    TCP_CA_NAME_MAX) == 0)
169173e263b1SGleb Smirnoff 					break;
169273e263b1SGleb Smirnoff 			CC_LIST_RUNLOCK();
169373e263b1SGleb Smirnoff 			if (algo == NULL) {
1694af6fef3aSGleb Smirnoff 				INP_WUNLOCK(inp);
169573e263b1SGleb Smirnoff 				error = EINVAL;
169673e263b1SGleb Smirnoff 				break;
169773e263b1SGleb Smirnoff 			}
1698dbc42409SLawrence Stewart 			/*
169973e263b1SGleb Smirnoff 			 * We hold a write lock over the tcb so it's safe to
170073e263b1SGleb Smirnoff 			 * do these things without ordering concerns.
1701dbc42409SLawrence Stewart 			 */
1702dbc42409SLawrence Stewart 			if (CC_ALGO(tp)->cb_destroy != NULL)
1703dbc42409SLawrence Stewart 				CC_ALGO(tp)->cb_destroy(tp->ccv);
1704dbc42409SLawrence Stewart 			CC_ALGO(tp) = algo;
1705dbc42409SLawrence Stewart 			/*
170673e263b1SGleb Smirnoff 			 * If something goes pear shaped initialising the new
170773e263b1SGleb Smirnoff 			 * algo, fall back to newreno (which does not
170873e263b1SGleb Smirnoff 			 * require initialisation).
1709dbc42409SLawrence Stewart 			 */
171073e263b1SGleb Smirnoff 			if (algo->cb_init != NULL &&
171173e263b1SGleb Smirnoff 			    algo->cb_init(tp->ccv) != 0) {
1712dbc42409SLawrence Stewart 				CC_ALGO(tp) = &newreno_cc_algo;
1713dbc42409SLawrence Stewart 				/*
171473e263b1SGleb Smirnoff 				 * The only reason init should fail is
1715dbc42409SLawrence Stewart 				 * because of malloc.
1716dbc42409SLawrence Stewart 				 */
1717dbc42409SLawrence Stewart 				error = ENOMEM;
1718dbc42409SLawrence Stewart 			}
171973e263b1SGleb Smirnoff 			INP_WUNLOCK(inp);
172073e263b1SGleb Smirnoff 			break;
1721dbc42409SLawrence Stewart 
17229077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
17239077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
17249077f387SGleb Smirnoff 		case TCP_KEEPINIT:
17259077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
17269077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
17279077f387SGleb Smirnoff 			if (error)
17289077f387SGleb Smirnoff 				return (error);
17299077f387SGleb Smirnoff 
17309077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
17319077f387SGleb Smirnoff 				error = EINVAL;
17329077f387SGleb Smirnoff 				break;
17339077f387SGleb Smirnoff 			}
17349077f387SGleb Smirnoff 			ui *= hz;
17359077f387SGleb Smirnoff 
17369077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
17379077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
17389077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
17399077f387SGleb Smirnoff 				tp->t_keepidle = ui;
17409077f387SGleb Smirnoff 				/*
17419077f387SGleb Smirnoff 				 * XXX: better check current remaining
17429077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
17439077f387SGleb Smirnoff 				 */
17449077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
17459077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
17469077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
17479077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
17489077f387SGleb Smirnoff 				break;
17499077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
17509077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
17519077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
17529077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
17539077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
17549077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
17559077f387SGleb Smirnoff 				break;
17569077f387SGleb Smirnoff 			case TCP_KEEPINIT:
17579077f387SGleb Smirnoff 				tp->t_keepinit = ui;
17589077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
17599077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
17609077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
17619077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
17629077f387SGleb Smirnoff 				break;
17639077f387SGleb Smirnoff 			}
176409fe6320SNavdeep Parhar 			goto unlock_and_done;
17659077f387SGleb Smirnoff 
176685c05144SGleb Smirnoff 		case TCP_KEEPCNT:
176785c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
176885c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
176985c05144SGleb Smirnoff 			if (error)
177085c05144SGleb Smirnoff 				return (error);
177185c05144SGleb Smirnoff 
177285c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
177385c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
177485c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
177585c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
177685c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
177785c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
177885c05144SGleb Smirnoff 			goto unlock_and_done;
177985c05144SGleb Smirnoff 
178086a996e6SHiren Panchasara #ifdef TCPPCAP
178186a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
178286a996e6SHiren Panchasara 		case TCP_PCAP_IN:
178386a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
178486a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
178586a996e6SHiren Panchasara 			    sizeof optval);
178686a996e6SHiren Panchasara 			if (error)
178786a996e6SHiren Panchasara 				return (error);
178886a996e6SHiren Panchasara 
178986a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
179086a996e6SHiren Panchasara 			if (optval >= 0)
179186a996e6SHiren Panchasara 				tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
179286a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
179386a996e6SHiren Panchasara 					optval);
179486a996e6SHiren Panchasara 			else
179586a996e6SHiren Panchasara 				error = EINVAL;
179686a996e6SHiren Panchasara 			goto unlock_and_done;
179786a996e6SHiren Panchasara #endif
179886a996e6SHiren Panchasara 
1799c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
1800c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
1801c560df6fSPatrick Kelsey 
1802281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
1803c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
1804c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
1805281a0fd4SPatrick Kelsey 				return (EPERM);
1806281a0fd4SPatrick Kelsey 
1807c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
1808c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
1809281a0fd4SPatrick Kelsey 			if (error)
1810281a0fd4SPatrick Kelsey 				return (error);
1811281a0fd4SPatrick Kelsey 
1812281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
1813c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
1814c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
1815c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
1816c560df6fSPatrick Kelsey 						error = EPERM;
1817c560df6fSPatrick Kelsey 						goto unlock_and_done;
1818c560df6fSPatrick Kelsey 					}
1819c560df6fSPatrick Kelsey 
1820281a0fd4SPatrick Kelsey 					tp->t_flags |= TF_FASTOPEN;
1821c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
1822281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
1823281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
1824c560df6fSPatrick Kelsey 				} else {
1825c560df6fSPatrick Kelsey 					/*
1826c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
1827c560df6fSPatrick Kelsey 					 * stash it in the client cookie
1828c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
1829c560df6fSPatrick Kelsey 					 * connect.
1830c560df6fSPatrick Kelsey 					 */
1831c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
1832c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
1833c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
1834c560df6fSPatrick Kelsey 						       tfo_optval.psk,
1835c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
1836c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
1837c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
1838c560df6fSPatrick Kelsey 					}
1839c560df6fSPatrick Kelsey 					tp->t_flags |= TF_FASTOPEN;
1840c560df6fSPatrick Kelsey 				}
1841281a0fd4SPatrick Kelsey 			} else
1842281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1843281a0fd4SPatrick Kelsey 			goto unlock_and_done;
1844c560df6fSPatrick Kelsey 		}
1845281a0fd4SPatrick Kelsey 
1846e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
18472529f56eSJonathan T. Looney 		case TCP_LOG:
18482529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
18492529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
18502529f56eSJonathan T. Looney 			    sizeof optval);
18512529f56eSJonathan T. Looney 			if (error)
18522529f56eSJonathan T. Looney 				return (error);
18532529f56eSJonathan T. Looney 
18542529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
18552529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
18562529f56eSJonathan T. Looney 			goto unlock_and_done;
18572529f56eSJonathan T. Looney 
18582529f56eSJonathan T. Looney 		case TCP_LOGBUF:
18592529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
18602529f56eSJonathan T. Looney 			error = EINVAL;
18612529f56eSJonathan T. Looney 			break;
18622529f56eSJonathan T. Looney 
18632529f56eSJonathan T. Looney 		case TCP_LOGID:
18642529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
18652529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
18662529f56eSJonathan T. Looney 			if (error)
18672529f56eSJonathan T. Looney 				break;
18682529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
18692529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
18702529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
18712529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
18722529f56eSJonathan T. Looney 			break;
18732529f56eSJonathan T. Looney 
18742529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
18752529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
18762529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
18772529f56eSJonathan T. Looney 			error =
18782529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
18792529f56eSJonathan T. Looney 			if (error)
18802529f56eSJonathan T. Looney 				break;
18812529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
18822529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
18832529f56eSJonathan T. Looney 			if (sopt->sopt_name == TCP_LOGDUMP) {
18842529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
18852529f56eSJonathan T. Looney 				    M_WAITOK, true);
18862529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
18872529f56eSJonathan T. Looney 			} else {
18882529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
18892529f56eSJonathan T. Looney 				/*
18902529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
18912529f56eSJonathan T. Looney 				 * INP lock.
18922529f56eSJonathan T. Looney 				 */
18932529f56eSJonathan T. Looney 			}
18942529f56eSJonathan T. Looney 			break;
1895e24e5683SJonathan T. Looney #endif
18962529f56eSJonathan T. Looney 
1897df8bae1dSRodney W. Grimes 		default:
18988501a69cSRobert Watson 			INP_WUNLOCK(inp);
1899df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1900df8bae1dSRodney W. Grimes 			break;
1901df8bae1dSRodney W. Grimes 		}
1902df8bae1dSRodney W. Grimes 		break;
1903df8bae1dSRodney W. Grimes 
1904cfe8b629SGarrett Wollman 	case SOPT_GET:
19051e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
1906cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1907fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
190888f6b043SBruce M Simpson 		case TCP_MD5SIG:
1909fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
19108501a69cSRobert Watson 				INP_WUNLOCK(inp);
1911fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
1912fcf59617SAndrey V. Elsukov 			}
1913fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
19141cfd4b53SBruce M Simpson 			break;
1915265ed012SBruce M Simpson #endif
19161e8f5ffaSRobert Watson 
1917df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1918cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
19198501a69cSRobert Watson 			INP_WUNLOCK(inp);
1920b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1921df8bae1dSRodney W. Grimes 			break;
1922df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1923cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
19248501a69cSRobert Watson 			INP_WUNLOCK(inp);
1925b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1926df8bae1dSRodney W. Grimes 			break;
1927a0292f23SGarrett Wollman 		case TCP_NOOPT:
1928cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
19298501a69cSRobert Watson 			INP_WUNLOCK(inp);
1930b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1931a0292f23SGarrett Wollman 			break;
1932a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1933cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
19348501a69cSRobert Watson 			INP_WUNLOCK(inp);
1935b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1936b8af5dfaSRobert Watson 			break;
1937b8af5dfaSRobert Watson 		case TCP_INFO:
1938b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
19398501a69cSRobert Watson 			INP_WUNLOCK(inp);
1940b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
1941a0292f23SGarrett Wollman 			break;
1942dbc42409SLawrence Stewart 		case TCP_CONGESTION:
1943af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1944dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
1945af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
1946dbc42409SLawrence Stewart 			break;
19472f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
19482f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
19492f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
19502f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
19512f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
19522f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
19535a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
19542f3eb7f4SGleb Smirnoff 				break;
19552f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
19565a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
19572f3eb7f4SGleb Smirnoff 				break;
19582f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
19595a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
19602f3eb7f4SGleb Smirnoff 				break;
19612f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
19625a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
19632f3eb7f4SGleb Smirnoff 				break;
19642f3eb7f4SGleb Smirnoff 			}
19652f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
19662f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
19672f3eb7f4SGleb Smirnoff 			break;
196886a996e6SHiren Panchasara #ifdef TCPPCAP
196986a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
197086a996e6SHiren Panchasara 		case TCP_PCAP_IN:
197186a996e6SHiren Panchasara 			optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
197286a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
197386a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
197486a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
197586a996e6SHiren Panchasara 			break;
197686a996e6SHiren Panchasara #endif
1977281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
1978281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
1979281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
1980281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
1981281a0fd4SPatrick Kelsey 			break;
1982e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
19832529f56eSJonathan T. Looney 		case TCP_LOG:
19842529f56eSJonathan T. Looney 			optval = tp->t_logstate;
19852529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
19862529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
19872529f56eSJonathan T. Looney 			break;
19882529f56eSJonathan T. Looney 		case TCP_LOGBUF:
19892529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
19902529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
19912529f56eSJonathan T. Looney 			break;
19922529f56eSJonathan T. Looney 		case TCP_LOGID:
19932529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
19942529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
19952529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
19962529f56eSJonathan T. Looney 			break;
19972529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
19982529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
19992529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
20002529f56eSJonathan T. Looney 			error = EINVAL;
20012529f56eSJonathan T. Looney 			break;
2002e24e5683SJonathan T. Looney #endif
2003df8bae1dSRodney W. Grimes 		default:
20048501a69cSRobert Watson 			INP_WUNLOCK(inp);
2005df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2006df8bae1dSRodney W. Grimes 			break;
2007df8bae1dSRodney W. Grimes 		}
2008df8bae1dSRodney W. Grimes 		break;
2009df8bae1dSRodney W. Grimes 	}
2010df8bae1dSRodney W. Grimes 	return (error);
2011df8bae1dSRodney W. Grimes }
20128501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2013bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2014df8bae1dSRodney W. Grimes 
201526e30fbbSDavid Greenman /*
2016df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
2017df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
2018df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
2019df8bae1dSRodney W. Grimes  */
20200312fbe9SPoul-Henning Kamp static int
2021ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so)
2022df8bae1dSRodney W. Grimes {
2023ad3f9ab3SAndre Oppermann 	struct tcpcb *tp;
2024df8bae1dSRodney W. Grimes 	struct inpcb *inp;
2025df8bae1dSRodney W. Grimes 	int error;
2026df8bae1dSRodney W. Grimes 
2027df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
2028e233e2acSAndre Oppermann 		error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
2029df8bae1dSRodney W. Grimes 		if (error)
2030df8bae1dSRodney W. Grimes 			return (error);
2031df8bae1dSRodney W. Grimes 	}
20326741ecf5SAndre Oppermann 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
20336741ecf5SAndre Oppermann 	so->so_snd.sb_flags |= SB_AUTOSIZE;
2034ff9b006dSJulien Charbon 	INP_INFO_RLOCK(&V_tcbinfo);
2035603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_tcbinfo);
2036f2de87feSRobert Watson 	if (error) {
2037ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
2038df8bae1dSRodney W. Grimes 		return (error);
2039f2de87feSRobert Watson 	}
2040df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
2041fb59c426SYoshinobu Inoue #ifdef INET6
20425cd54324SBjoern A. Zeeb 	if (inp->inp_vflag & INP_IPV6PROTO) {
2043fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
204463ec505aSMichael Tuexen 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
204563ec505aSMichael Tuexen 			inp->inp_vflag |= INP_IPV4;
2046fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
2047fb59c426SYoshinobu Inoue 	}
2048fb59c426SYoshinobu Inoue 	else
2049fb59c426SYoshinobu Inoue #endif
2050cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
2051df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
2052623dce13SRobert Watson 	if (tp == NULL) {
2053df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
20540206cdb8SBjoern A. Zeeb 		in_pcbfree(inp);
2055ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
2056df8bae1dSRodney W. Grimes 		return (ENOBUFS);
2057df8bae1dSRodney W. Grimes 	}
2058df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
20598501a69cSRobert Watson 	INP_WUNLOCK(inp);
2060ff9b006dSJulien Charbon 	INP_INFO_RUNLOCK(&V_tcbinfo);
2061bf840a17SGleb Smirnoff 	TCPSTATES_INC(TCPS_CLOSED);
2062df8bae1dSRodney W. Grimes 	return (0);
2063df8bae1dSRodney W. Grimes }
2064df8bae1dSRodney W. Grimes 
2065df8bae1dSRodney W. Grimes /*
2066df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2067df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2068df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2069df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2070df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2071df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2072df8bae1dSRodney W. Grimes  */
2073623dce13SRobert Watson static void
2074ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2075df8bae1dSRodney W. Grimes {
2076e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
2077e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
2078e6e0b5ffSRobert Watson 
2079ff9b006dSJulien Charbon 	INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
20808501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2081df8bae1dSRodney W. Grimes 
2082623dce13SRobert Watson 	/*
2083623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2084623dce13SRobert Watson 	 * socket is still open.
2085623dce13SRobert Watson 	 */
2086623dce13SRobert Watson 	if (tp->t_state < TCPS_ESTABLISHED) {
2087df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2088623dce13SRobert Watson 		KASSERT(tp != NULL,
2089623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2090623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2091243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2092623dce13SRobert Watson 		KASSERT(tp != NULL,
2093623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2094623dce13SRobert Watson 	} else {
2095df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2096df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2097623dce13SRobert Watson 		tcp_usrclosed(tp);
2098ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
209955bceb1eSRandall Stewart 			tp->t_fb->tfb_tcp_output(tp);
2100df8bae1dSRodney W. Grimes 	}
2101df8bae1dSRodney W. Grimes }
2102df8bae1dSRodney W. Grimes 
2103df8bae1dSRodney W. Grimes /*
2104df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2105df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2106df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2107df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2108df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2109df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2110df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2111df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2112df8bae1dSRodney W. Grimes  */
2113623dce13SRobert Watson static void
2114ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2115df8bae1dSRodney W. Grimes {
2116df8bae1dSRodney W. Grimes 
2117ff9b006dSJulien Charbon 	INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
21188501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
2119e6e0b5ffSRobert Watson 
2120df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2121df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
212209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
212309fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
212409fe6320SNavdeep Parhar #endif
2125550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2126bc65987aSKip Macy 		/* FALLTHROUGH */
2127bc65987aSKip Macy 	case TCPS_CLOSED:
2128df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2129623dce13SRobert Watson 		/*
2130623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2131623dce13SRobert Watson 		 * still open.
2132623dce13SRobert Watson 		 */
2133623dce13SRobert Watson 		KASSERT(tp != NULL,
2134623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2135df8bae1dSRodney W. Grimes 		break;
2136df8bae1dSRodney W. Grimes 
2137a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2138df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2139a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2140a0292f23SGarrett Wollman 		break;
2141a0292f23SGarrett Wollman 
2142df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
214357f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2144df8bae1dSRodney W. Grimes 		break;
2145df8bae1dSRodney W. Grimes 
2146df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
214757f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2148df8bae1dSRodney W. Grimes 		break;
2149df8bae1dSRodney W. Grimes 	}
2150abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
2151df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
2152abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
21537c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
21547c72af87SMohan Srinivasan 			int timeout;
21557c72af87SMohan Srinivasan 
21567c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
21579077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2158b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2159b6239c4aSAndras Olah 		}
2160df8bae1dSRodney W. Grimes 	}
21617c72af87SMohan Srinivasan }
2162497057eeSRobert Watson 
2163497057eeSRobert Watson #ifdef DDB
2164497057eeSRobert Watson static void
2165497057eeSRobert Watson db_print_indent(int indent)
2166497057eeSRobert Watson {
2167497057eeSRobert Watson 	int i;
2168497057eeSRobert Watson 
2169497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2170497057eeSRobert Watson 		db_printf(" ");
2171497057eeSRobert Watson }
2172497057eeSRobert Watson 
2173497057eeSRobert Watson static void
2174497057eeSRobert Watson db_print_tstate(int t_state)
2175497057eeSRobert Watson {
2176497057eeSRobert Watson 
2177497057eeSRobert Watson 	switch (t_state) {
2178497057eeSRobert Watson 	case TCPS_CLOSED:
2179497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2180497057eeSRobert Watson 		return;
2181497057eeSRobert Watson 
2182497057eeSRobert Watson 	case TCPS_LISTEN:
2183497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2184497057eeSRobert Watson 		return;
2185497057eeSRobert Watson 
2186497057eeSRobert Watson 	case TCPS_SYN_SENT:
2187497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2188497057eeSRobert Watson 		return;
2189497057eeSRobert Watson 
2190497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2191497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2192497057eeSRobert Watson 		return;
2193497057eeSRobert Watson 
2194497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2195497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2196497057eeSRobert Watson 		return;
2197497057eeSRobert Watson 
2198497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2199497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2200497057eeSRobert Watson 		return;
2201497057eeSRobert Watson 
2202497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2203497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2204497057eeSRobert Watson 		return;
2205497057eeSRobert Watson 
2206497057eeSRobert Watson 	case TCPS_CLOSING:
2207497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2208497057eeSRobert Watson 		return;
2209497057eeSRobert Watson 
2210497057eeSRobert Watson 	case TCPS_LAST_ACK:
2211497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2212497057eeSRobert Watson 		return;
2213497057eeSRobert Watson 
2214497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2215497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2216497057eeSRobert Watson 		return;
2217497057eeSRobert Watson 
2218497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2219497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2220497057eeSRobert Watson 		return;
2221497057eeSRobert Watson 
2222497057eeSRobert Watson 	default:
2223497057eeSRobert Watson 		db_printf("unknown");
2224497057eeSRobert Watson 		return;
2225497057eeSRobert Watson 	}
2226497057eeSRobert Watson }
2227497057eeSRobert Watson 
2228497057eeSRobert Watson static void
2229497057eeSRobert Watson db_print_tflags(u_int t_flags)
2230497057eeSRobert Watson {
2231497057eeSRobert Watson 	int comma;
2232497057eeSRobert Watson 
2233497057eeSRobert Watson 	comma = 0;
2234497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2235497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2236497057eeSRobert Watson 		comma = 1;
2237497057eeSRobert Watson 	}
2238497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2239497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2240497057eeSRobert Watson 		comma = 1;
2241497057eeSRobert Watson 	}
2242497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2243497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2244497057eeSRobert Watson 		comma = 1;
2245497057eeSRobert Watson 	}
2246497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2247497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2248497057eeSRobert Watson 		comma = 1;
2249497057eeSRobert Watson 	}
2250497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2251497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2252497057eeSRobert Watson 		comma = 1;
2253497057eeSRobert Watson 	}
2254497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2255497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2256497057eeSRobert Watson 		comma = 1;
2257497057eeSRobert Watson 	}
2258497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2259497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2260497057eeSRobert Watson 		comma = 1;
2261497057eeSRobert Watson 	}
2262497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2263497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2264497057eeSRobert Watson 		comma = 1;
2265497057eeSRobert Watson 	}
2266497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2267497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2268497057eeSRobert Watson 		comma = 1;
2269497057eeSRobert Watson 	}
2270497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2271497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2272497057eeSRobert Watson 		comma = 1;
2273497057eeSRobert Watson 	}
2274497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2275497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2276497057eeSRobert Watson 		comma = 1;
2277497057eeSRobert Watson 	}
2278497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2279497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2280497057eeSRobert Watson 		comma = 1;
2281497057eeSRobert Watson 	}
2282497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2283497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2284497057eeSRobert Watson 		comma = 1;
2285497057eeSRobert Watson 	}
2286497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2287497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2288497057eeSRobert Watson 		comma = 1;
2289497057eeSRobert Watson 	}
2290497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
2291497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2292497057eeSRobert Watson 		comma = 1;
2293497057eeSRobert Watson 	}
2294497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2295497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2296497057eeSRobert Watson 		comma = 1;
2297497057eeSRobert Watson 	}
2298497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2299497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2300497057eeSRobert Watson 		comma = 1;
2301497057eeSRobert Watson 	}
2302497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2303497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2304497057eeSRobert Watson 		comma = 1;
2305497057eeSRobert Watson 	}
2306dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
2307dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2308dbc42409SLawrence Stewart 		comma = 1;
2309dbc42409SLawrence Stewart 	}
2310497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2311497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2312497057eeSRobert Watson 		comma = 1;
2313497057eeSRobert Watson 	}
2314497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2315497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2316497057eeSRobert Watson 		comma = 1;
2317497057eeSRobert Watson 	}
2318497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2319497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2320497057eeSRobert Watson 		comma = 1;
2321497057eeSRobert Watson 	}
2322497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2323497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2324497057eeSRobert Watson 		comma = 1;
2325497057eeSRobert Watson 	}
2326f2512ba1SRui Paulo 	if (t_flags & TF_ECN_PERMIT) {
2327f2512ba1SRui Paulo 		db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2328f2512ba1SRui Paulo 		comma = 1;
2329f2512ba1SRui Paulo 	}
2330281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2331281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2332281a0fd4SPatrick Kelsey 		comma = 1;
2333281a0fd4SPatrick Kelsey 	}
2334497057eeSRobert Watson }
2335497057eeSRobert Watson 
2336497057eeSRobert Watson static void
2337497057eeSRobert Watson db_print_toobflags(char t_oobflags)
2338497057eeSRobert Watson {
2339497057eeSRobert Watson 	int comma;
2340497057eeSRobert Watson 
2341497057eeSRobert Watson 	comma = 0;
2342497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
2343497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2344497057eeSRobert Watson 		comma = 1;
2345497057eeSRobert Watson 	}
2346497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
2347497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2348497057eeSRobert Watson 		comma = 1;
2349497057eeSRobert Watson 	}
2350497057eeSRobert Watson }
2351497057eeSRobert Watson 
2352497057eeSRobert Watson static void
2353497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2354497057eeSRobert Watson {
2355497057eeSRobert Watson 
2356497057eeSRobert Watson 	db_print_indent(indent);
2357497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
2358497057eeSRobert Watson 
2359497057eeSRobert Watson 	indent += 2;
2360497057eeSRobert Watson 
2361497057eeSRobert Watson 	db_print_indent(indent);
2362497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
23634741bfcbSPatrick Kelsey 	   LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2364497057eeSRobert Watson 
2365497057eeSRobert Watson 	db_print_indent(indent);
236685d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
2367e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2368497057eeSRobert Watson 
2369497057eeSRobert Watson 	db_print_indent(indent);
2370e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2371e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
2372497057eeSRobert Watson 
2373497057eeSRobert Watson 	db_print_indent(indent);
2374497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
2375497057eeSRobert Watson 	db_print_tstate(tp->t_state);
2376497057eeSRobert Watson 	db_printf(")\n");
2377497057eeSRobert Watson 
2378497057eeSRobert Watson 	db_print_indent(indent);
2379497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
2380497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
2381497057eeSRobert Watson 	db_printf(")\n");
2382497057eeSRobert Watson 
2383497057eeSRobert Watson 	db_print_indent(indent);
2384497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
2385497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
2386497057eeSRobert Watson 
2387497057eeSRobert Watson 	db_print_indent(indent);
2388497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
2389497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2390497057eeSRobert Watson 
2391497057eeSRobert Watson 	db_print_indent(indent);
2392497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
2393497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
2394497057eeSRobert Watson 
2395497057eeSRobert Watson 	db_print_indent(indent);
23963ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
2397497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2398497057eeSRobert Watson 
2399497057eeSRobert Watson 	db_print_indent(indent);
24003ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
24011c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
2402497057eeSRobert Watson 
2403497057eeSRobert Watson 	db_print_indent(indent);
24043ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
24051c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2406497057eeSRobert Watson 
2407497057eeSRobert Watson 	db_print_indent(indent);
24080c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
24090c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
2410497057eeSRobert Watson 
2411497057eeSRobert Watson 	db_print_indent(indent);
24121c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
24131c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
2414497057eeSRobert Watson 
2415497057eeSRobert Watson 	db_print_indent(indent);
24161c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
24171c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2418497057eeSRobert Watson 
2419497057eeSRobert Watson 	db_print_indent(indent);
2420497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
2421497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2422497057eeSRobert Watson 	    tp->t_rttbest);
2423497057eeSRobert Watson 
2424497057eeSRobert Watson 	db_print_indent(indent);
24253ac12506SJonathan T. Looney 	db_printf("t_rttupdated: %lu   max_sndwnd: %u   t_softerror: %d\n",
2426497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2427497057eeSRobert Watson 
2428497057eeSRobert Watson 	db_print_indent(indent);
2429497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2430497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
2431497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
2432497057eeSRobert Watson 
2433497057eeSRobert Watson 	db_print_indent(indent);
2434497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
2435497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2436497057eeSRobert Watson 
2437497057eeSRobert Watson 	db_print_indent(indent);
24389f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
24391a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
2440497057eeSRobert Watson 
2441497057eeSRobert Watson 	db_print_indent(indent);
2442497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
24433ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2444497057eeSRobert Watson 
2445497057eeSRobert Watson 	db_print_indent(indent);
24463ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
24479f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2448497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
2449497057eeSRobert Watson 
2450497057eeSRobert Watson 	db_print_indent(indent);
24513529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
24523529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2453497057eeSRobert Watson 
2454497057eeSRobert Watson 	db_print_indent(indent);
2455497057eeSRobert Watson 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
2456497057eeSRobert Watson 	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2457497057eeSRobert Watson 
2458497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
2459497057eeSRobert Watson 
2460497057eeSRobert Watson 	db_print_indent(indent);
2461497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
2462497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2463497057eeSRobert Watson }
2464497057eeSRobert Watson 
2465497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2466497057eeSRobert Watson {
2467497057eeSRobert Watson 	struct tcpcb *tp;
2468497057eeSRobert Watson 
2469497057eeSRobert Watson 	if (!have_addr) {
2470497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
2471497057eeSRobert Watson 		return;
2472497057eeSRobert Watson 	}
2473497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
2474497057eeSRobert Watson 
2475497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
2476497057eeSRobert Watson }
2477497057eeSRobert Watson #endif
2478