xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 2de3e790f537301e0a1cf202d022afca122acdbc)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
3623dce13SRobert Watson  *	The Regents of the University of California.
4497057eeSRobert Watson  * Copyright (c) 2006-2007 Robert N. M. Watson
5fa046d87SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
6623dce13SRobert Watson  * All rights reserved.
7df8bae1dSRodney W. Grimes  *
8fa046d87SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
9fa046d87SRobert Watson  * contract to Juniper Networks, Inc.
10fa046d87SRobert Watson  *
11df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
12df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
13df8bae1dSRodney W. Grimes  * are met:
14df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
15df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
16df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
18df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
20df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
21df8bae1dSRodney W. Grimes  *    without specific prior written permission.
22df8bae1dSRodney W. Grimes  *
23df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
34df8bae1dSRodney W. Grimes  *
351fdbc7aeSGarrett Wollman  *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
36df8bae1dSRodney W. Grimes  */
37df8bae1dSRodney W. Grimes 
384b421e2dSMike Silbersack #include <sys/cdefs.h>
394b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
404b421e2dSMike Silbersack 
41497057eeSRobert Watson #include "opt_ddb.h"
421cfd4b53SBruce M Simpson #include "opt_inet.h"
43fb59c426SYoshinobu Inoue #include "opt_inet6.h"
440cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
450cc12cc5SJoerg Wunsch 
46df8bae1dSRodney W. Grimes #include <sys/param.h>
47df8bae1dSRodney W. Grimes #include <sys/systm.h>
489077f387SGleb Smirnoff #include <sys/limits.h>
49f76fcf6dSJeffrey Hsu #include <sys/malloc.h>
5055bceb1eSRandall Stewart #include <sys/refcount.h>
51c7a82f90SGarrett Wollman #include <sys/kernel.h>
5298163b98SPoul-Henning Kamp #include <sys/sysctl.h>
53df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
54fb59c426SYoshinobu Inoue #ifdef INET6
55fb59c426SYoshinobu Inoue #include <sys/domain.h>
56fb59c426SYoshinobu Inoue #endif /* INET6 */
57df8bae1dSRodney W. Grimes #include <sys/socket.h>
58df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
59df8bae1dSRodney W. Grimes #include <sys/protosw.h>
6091421ba2SRobert Watson #include <sys/proc.h>
6191421ba2SRobert Watson #include <sys/jail.h>
62df8bae1dSRodney W. Grimes 
63497057eeSRobert Watson #ifdef DDB
64497057eeSRobert Watson #include <ddb/ddb.h>
65497057eeSRobert Watson #endif
66497057eeSRobert Watson 
67df8bae1dSRodney W. Grimes #include <net/if.h>
6876039bc8SGleb Smirnoff #include <net/if_var.h>
69df8bae1dSRodney W. Grimes #include <net/route.h>
70530c0060SRobert Watson #include <net/vnet.h>
71df8bae1dSRodney W. Grimes 
72df8bae1dSRodney W. Grimes #include <netinet/in.h>
735d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h>
74df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
75b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h>
76b5e8ce9fSBruce Evans #include <netinet/in_var.h>
77df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
78fb59c426SYoshinobu Inoue #ifdef INET6
79b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h>
80b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
81fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
82a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
83fb59c426SYoshinobu Inoue #endif
84281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
85281a0fd4SPatrick Kelsey #include <netinet/tcp_fastopen.h>
86281a0fd4SPatrick Kelsey #endif
87*2de3e790SGleb Smirnoff #include <netinet/tcp.h>
88df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
89df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
90df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
91df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
92df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
93*2de3e790SGleb Smirnoff #include <netinet/tcp_cc.h>
9486a996e6SHiren Panchasara #ifdef TCPPCAP
9586a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
9686a996e6SHiren Panchasara #endif
97610ee2f9SDavid Greenman #ifdef TCPDEBUG
98df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
99610ee2f9SDavid Greenman #endif
10009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
101bc65987aSKip Macy #include <netinet/tcp_offload.h>
10209fe6320SNavdeep Parhar #endif
103df8bae1dSRodney W. Grimes 
104df8bae1dSRodney W. Grimes /*
105df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
106df8bae1dSRodney W. Grimes  */
10756dc72c3SPawel Jakub Dawidek static int	tcp_attach(struct socket *);
108b287c6c7SBjoern A. Zeeb #ifdef INET
1094d77a549SAlfred Perlstein static int	tcp_connect(struct tcpcb *, struct sockaddr *,
1104d77a549SAlfred Perlstein 		    struct thread *td);
111b287c6c7SBjoern A. Zeeb #endif /* INET */
112fb59c426SYoshinobu Inoue #ifdef INET6
1134d77a549SAlfred Perlstein static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
1144d77a549SAlfred Perlstein 		    struct thread *td);
115fb59c426SYoshinobu Inoue #endif /* INET6 */
116623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
117623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
118b8af5dfaSRobert Watson static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
1192c37256eSGarrett Wollman 
1202c37256eSGarrett Wollman #ifdef TCPDEBUG
1211db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1222c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
1234cc20ab1SSeigo Tanimura #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
1244cc20ab1SSeigo Tanimura 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1252c37256eSGarrett Wollman #else
1262c37256eSGarrett Wollman #define	TCPDEBUG0
1272c37256eSGarrett Wollman #define	TCPDEBUG1()
1282c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1292c37256eSGarrett Wollman #endif
1302c37256eSGarrett Wollman 
1312c37256eSGarrett Wollman /*
1322c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1332c37256eSGarrett Wollman  * and an internet control block.
1342c37256eSGarrett Wollman  */
1352c37256eSGarrett Wollman static int
136b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1372c37256eSGarrett Wollman {
138f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
139623dce13SRobert Watson 	struct tcpcb *tp = NULL;
140623dce13SRobert Watson 	int error;
1412c37256eSGarrett Wollman 	TCPDEBUG0;
1422c37256eSGarrett Wollman 
143623dce13SRobert Watson 	inp = sotoinpcb(so);
144623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1452c37256eSGarrett Wollman 	TCPDEBUG1();
1462c37256eSGarrett Wollman 
14756dc72c3SPawel Jakub Dawidek 	error = tcp_attach(so);
1482c37256eSGarrett Wollman 	if (error)
1492c37256eSGarrett Wollman 		goto out;
1502c37256eSGarrett Wollman 
1512c37256eSGarrett Wollman 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1523879597fSAndrey A. Chernov 		so->so_linger = TCP_LINGERTIME;
153f76fcf6dSJeffrey Hsu 
154f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
155f76fcf6dSJeffrey Hsu 	tp = intotcpcb(inp);
1562c37256eSGarrett Wollman out:
1572c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
1585d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
1592c37256eSGarrett Wollman 	return error;
1602c37256eSGarrett Wollman }
1612c37256eSGarrett Wollman 
1622c37256eSGarrett Wollman /*
163a152f8a3SRobert Watson  * tcp_detach is called when the socket layer loses its final reference
164a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
165a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
166a152f8a3SRobert Watson  * inpcb state: time wait.
167c78cbc7bSRobert Watson  *
168a152f8a3SRobert Watson  * This function can probably be re-absorbed back into tcp_usr_detach() now
169a152f8a3SRobert Watson  * that there is a single detach path.
1702c37256eSGarrett Wollman  */
171bc725eafSRobert Watson static void
172c78cbc7bSRobert Watson tcp_detach(struct socket *so, struct inpcb *inp)
1732c37256eSGarrett Wollman {
1742c37256eSGarrett Wollman 	struct tcpcb *tp;
1752c37256eSGarrett Wollman 
176079672cbSJulien Charbon 	INP_INFO_LOCK_ASSERT(&V_tcbinfo);
1778501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
178623dce13SRobert Watson 
179c78cbc7bSRobert Watson 	KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
180c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
181953b5606SRobert Watson 
182a152f8a3SRobert Watson 	tp = intotcpcb(inp);
183a152f8a3SRobert Watson 
184ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
185623dce13SRobert Watson 		/*
186a152f8a3SRobert Watson 		 * There are two cases to handle: one in which the time wait
187a152f8a3SRobert Watson 		 * state is being discarded (INP_DROPPED), and one in which
188a152f8a3SRobert Watson 		 * this connection will remain in timewait.  In the former,
189a152f8a3SRobert Watson 		 * it is time to discard all state (except tcptw, which has
190a152f8a3SRobert Watson 		 * already been discarded by the timewait close code, which
191a152f8a3SRobert Watson 		 * should be further up the call stack somewhere).  In the
192a152f8a3SRobert Watson 		 * latter case, we detach from the socket, but leave the pcb
193a152f8a3SRobert Watson 		 * present until timewait ends.
194623dce13SRobert Watson 		 *
195a152f8a3SRobert Watson 		 * XXXRW: Would it be cleaner to free the tcptw here?
196cea40c48SJulien Charbon 		 *
197cea40c48SJulien Charbon 		 * Astute question indeed, from twtcp perspective there are
198cea40c48SJulien Charbon 		 * three cases to consider:
199cea40c48SJulien Charbon 		 *
200cea40c48SJulien Charbon 		 * #1 tcp_detach is called at tcptw creation time by
201cea40c48SJulien Charbon 		 *  tcp_twstart, then do not discard the newly created tcptw
202cea40c48SJulien Charbon 		 *  and leave inpcb present until timewait ends
203cea40c48SJulien Charbon 		 * #2 tcp_detach is called at timewait end (or reuse) by
204cea40c48SJulien Charbon 		 *  tcp_twclose, then the tcptw has already been discarded
205ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
206cea40c48SJulien Charbon 		 * #3 tcp_detach is called() after timewait ends (or reuse)
207cea40c48SJulien Charbon 		 *  (e.g. by soclose), then tcptw has already been discarded
208ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
209cea40c48SJulien Charbon 		 *
210cea40c48SJulien Charbon 		 *  In all three cases the tcptw should not be freed here.
211623dce13SRobert Watson 		 */
212ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED) {
213a152f8a3SRobert Watson 			KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
214a152f8a3SRobert Watson 			    "INP_DROPPED && tp != NULL"));
215623dce13SRobert Watson 			in_pcbdetach(inp);
2160206cdb8SBjoern A. Zeeb 			in_pcbfree(inp);
2170206cdb8SBjoern A. Zeeb 		} else {
218623dce13SRobert Watson 			in_pcbdetach(inp);
2198501a69cSRobert Watson 			INP_WUNLOCK(inp);
220623dce13SRobert Watson 		}
221623dce13SRobert Watson 	} else {
222e6e65783SRobert Watson 		/*
223a152f8a3SRobert Watson 		 * If the connection is not in timewait, we consider two
224a152f8a3SRobert Watson 		 * two conditions: one in which no further processing is
225a152f8a3SRobert Watson 		 * necessary (dropped || embryonic), and one in which TCP is
226a152f8a3SRobert Watson 		 * not yet done, but no longer requires the socket, so the
227a152f8a3SRobert Watson 		 * pcb will persist for the time being.
228a152f8a3SRobert Watson 		 *
229a152f8a3SRobert Watson 		 * XXXRW: Does the second case still occur?
230e6e65783SRobert Watson 		 */
231ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED ||
232623dce13SRobert Watson 		    tp->t_state < TCPS_SYN_SENT) {
233623dce13SRobert Watson 			tcp_discardcb(tp);
234623dce13SRobert Watson 			in_pcbdetach(inp);
2350206cdb8SBjoern A. Zeeb 			in_pcbfree(inp);
236db3cee51SNavdeep Parhar 		} else {
237a152f8a3SRobert Watson 			in_pcbdetach(inp);
238db3cee51SNavdeep Parhar 			INP_WUNLOCK(inp);
239db3cee51SNavdeep Parhar 		}
240623dce13SRobert Watson 	}
241623dce13SRobert Watson }
242c78cbc7bSRobert Watson 
243c78cbc7bSRobert Watson /*
244c78cbc7bSRobert Watson  * pru_detach() detaches the TCP protocol from the socket.
245c78cbc7bSRobert Watson  * If the protocol state is non-embryonic, then can't
246c78cbc7bSRobert Watson  * do this directly: have to initiate a pru_disconnect(),
247c78cbc7bSRobert Watson  * which may finish later; embryonic TCB's can just
248c78cbc7bSRobert Watson  * be discarded here.
249c78cbc7bSRobert Watson  */
250c78cbc7bSRobert Watson static void
251c78cbc7bSRobert Watson tcp_usr_detach(struct socket *so)
252c78cbc7bSRobert Watson {
253c78cbc7bSRobert Watson 	struct inpcb *inp;
254079672cbSJulien Charbon 	int rlock = 0;
255c78cbc7bSRobert Watson 
256c78cbc7bSRobert Watson 	inp = sotoinpcb(so);
257c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
258079672cbSJulien Charbon 	if (!INP_INFO_WLOCKED(&V_tcbinfo)) {
259ff9b006dSJulien Charbon 		INP_INFO_RLOCK(&V_tcbinfo);
260079672cbSJulien Charbon 		rlock = 1;
261079672cbSJulien Charbon 	}
2628501a69cSRobert Watson 	INP_WLOCK(inp);
263c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
264c78cbc7bSRobert Watson 	    ("tcp_usr_detach: inp_socket == NULL"));
265c78cbc7bSRobert Watson 	tcp_detach(so, inp);
266079672cbSJulien Charbon 	if (rlock)
267ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
2682c37256eSGarrett Wollman }
2692c37256eSGarrett Wollman 
270b287c6c7SBjoern A. Zeeb #ifdef INET
2712c37256eSGarrett Wollman /*
2722c37256eSGarrett Wollman  * Give the socket an address.
2732c37256eSGarrett Wollman  */
2742c37256eSGarrett Wollman static int
275b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2762c37256eSGarrett Wollman {
2772c37256eSGarrett Wollman 	int error = 0;
278f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
279623dce13SRobert Watson 	struct tcpcb *tp = NULL;
2802c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
2812c37256eSGarrett Wollman 
28252710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
28352710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sinp))
28452710de1SPawel Jakub Dawidek 		return (EINVAL);
2852c37256eSGarrett Wollman 	/*
2862c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2872c37256eSGarrett Wollman 	 * to them.
2882c37256eSGarrett Wollman 	 */
2892c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
29052710de1SPawel Jakub Dawidek 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
29152710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
29252710de1SPawel Jakub Dawidek 
293623dce13SRobert Watson 	TCPDEBUG0;
294623dce13SRobert Watson 	inp = sotoinpcb(so);
295623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
2968501a69cSRobert Watson 	INP_WLOCK(inp);
297ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
298623dce13SRobert Watson 		error = EINVAL;
2992c37256eSGarrett Wollman 		goto out;
300623dce13SRobert Watson 	}
301623dce13SRobert Watson 	tp = intotcpcb(inp);
302623dce13SRobert Watson 	TCPDEBUG1();
303fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
304623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
305fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
306623dce13SRobert Watson out:
307623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3085d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3098501a69cSRobert Watson 	INP_WUNLOCK(inp);
310623dce13SRobert Watson 
311623dce13SRobert Watson 	return (error);
3122c37256eSGarrett Wollman }
313b287c6c7SBjoern A. Zeeb #endif /* INET */
3142c37256eSGarrett Wollman 
315fb59c426SYoshinobu Inoue #ifdef INET6
316fb59c426SYoshinobu Inoue static int
317b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
318fb59c426SYoshinobu Inoue {
319fb59c426SYoshinobu Inoue 	int error = 0;
320f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
321623dce13SRobert Watson 	struct tcpcb *tp = NULL;
322fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
323fb59c426SYoshinobu Inoue 
32452710de1SPawel Jakub Dawidek 	sin6p = (struct sockaddr_in6 *)nam;
32552710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sin6p))
32652710de1SPawel Jakub Dawidek 		return (EINVAL);
327fb59c426SYoshinobu Inoue 	/*
328fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
329fb59c426SYoshinobu Inoue 	 * to them.
330fb59c426SYoshinobu Inoue 	 */
331fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6 &&
33252710de1SPawel Jakub Dawidek 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
33352710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
33452710de1SPawel Jakub Dawidek 
335623dce13SRobert Watson 	TCPDEBUG0;
336623dce13SRobert Watson 	inp = sotoinpcb(so);
337623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
3388501a69cSRobert Watson 	INP_WLOCK(inp);
339ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
340623dce13SRobert Watson 		error = EINVAL;
341623dce13SRobert Watson 		goto out;
342623dce13SRobert Watson 	}
343623dce13SRobert Watson 	tp = intotcpcb(inp);
344623dce13SRobert Watson 	TCPDEBUG1();
345fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
346fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
347fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
348b287c6c7SBjoern A. Zeeb #ifdef INET
34966ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
350fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
351fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
352fb59c426SYoshinobu Inoue 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
353fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
354fb59c426SYoshinobu Inoue 
355fb59c426SYoshinobu Inoue 			in6_sin6_2_sin(&sin, sin6p);
356fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
357fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
358b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
359b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
360fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
361fb59c426SYoshinobu Inoue 			goto out;
362fb59c426SYoshinobu Inoue 		}
363fb59c426SYoshinobu Inoue 	}
364b287c6c7SBjoern A. Zeeb #endif
365b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
366fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
367623dce13SRobert Watson out:
368623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3695d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3708501a69cSRobert Watson 	INP_WUNLOCK(inp);
371623dce13SRobert Watson 	return (error);
372fb59c426SYoshinobu Inoue }
373fb59c426SYoshinobu Inoue #endif /* INET6 */
374fb59c426SYoshinobu Inoue 
375b287c6c7SBjoern A. Zeeb #ifdef INET
3762c37256eSGarrett Wollman /*
3772c37256eSGarrett Wollman  * Prepare to accept connections.
3782c37256eSGarrett Wollman  */
3792c37256eSGarrett Wollman static int
380d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
3812c37256eSGarrett Wollman {
3822c37256eSGarrett Wollman 	int error = 0;
383f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
384623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3852c37256eSGarrett Wollman 
386623dce13SRobert Watson 	TCPDEBUG0;
387623dce13SRobert Watson 	inp = sotoinpcb(so);
388623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
3898501a69cSRobert Watson 	INP_WLOCK(inp);
390ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
391623dce13SRobert Watson 		error = EINVAL;
392623dce13SRobert Watson 		goto out;
393623dce13SRobert Watson 	}
394623dce13SRobert Watson 	tp = intotcpcb(inp);
395623dce13SRobert Watson 	TCPDEBUG1();
3960daccb9cSRobert Watson 	SOCK_LOCK(so);
3970daccb9cSRobert Watson 	error = solisten_proto_check(so);
398fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
3990daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0)
400b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
401fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4020daccb9cSRobert Watson 	if (error == 0) {
40357f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
404d374e81eSRobert Watson 		solisten_proto(so, backlog);
40509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
40637cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
40709fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
40809fe6320SNavdeep Parhar #endif
4090daccb9cSRobert Watson 	}
4100daccb9cSRobert Watson 	SOCK_UNLOCK(so);
411623dce13SRobert Watson 
412281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
413281a0fd4SPatrick Kelsey 	if (tp->t_flags & TF_FASTOPEN)
414281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
415281a0fd4SPatrick Kelsey #endif
416623dce13SRobert Watson out:
417623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4185d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4198501a69cSRobert Watson 	INP_WUNLOCK(inp);
420623dce13SRobert Watson 	return (error);
4212c37256eSGarrett Wollman }
422b287c6c7SBjoern A. Zeeb #endif /* INET */
4232c37256eSGarrett Wollman 
424fb59c426SYoshinobu Inoue #ifdef INET6
425fb59c426SYoshinobu Inoue static int
426d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
427fb59c426SYoshinobu Inoue {
428fb59c426SYoshinobu Inoue 	int error = 0;
429f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
430623dce13SRobert Watson 	struct tcpcb *tp = NULL;
431fb59c426SYoshinobu Inoue 
432623dce13SRobert Watson 	TCPDEBUG0;
433623dce13SRobert Watson 	inp = sotoinpcb(so);
434623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4358501a69cSRobert Watson 	INP_WLOCK(inp);
436ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
437623dce13SRobert Watson 		error = EINVAL;
438623dce13SRobert Watson 		goto out;
439623dce13SRobert Watson 	}
440623dce13SRobert Watson 	tp = intotcpcb(inp);
441623dce13SRobert Watson 	TCPDEBUG1();
4420daccb9cSRobert Watson 	SOCK_LOCK(so);
4430daccb9cSRobert Watson 	error = solisten_proto_check(so);
444fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
4450daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0) {
446fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
44766ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
448fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
449b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
450fb59c426SYoshinobu Inoue 	}
451fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4520daccb9cSRobert Watson 	if (error == 0) {
45357f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
454d374e81eSRobert Watson 		solisten_proto(so, backlog);
45509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
45637cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
45709fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
45809fe6320SNavdeep Parhar #endif
4590daccb9cSRobert Watson 	}
4600daccb9cSRobert Watson 	SOCK_UNLOCK(so);
461623dce13SRobert Watson 
462281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
463281a0fd4SPatrick Kelsey 	if (tp->t_flags & TF_FASTOPEN)
464281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
465281a0fd4SPatrick Kelsey #endif
466623dce13SRobert Watson out:
467623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4685d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4698501a69cSRobert Watson 	INP_WUNLOCK(inp);
470623dce13SRobert Watson 	return (error);
471fb59c426SYoshinobu Inoue }
472fb59c426SYoshinobu Inoue #endif /* INET6 */
473fb59c426SYoshinobu Inoue 
474b287c6c7SBjoern A. Zeeb #ifdef INET
4752c37256eSGarrett Wollman /*
4762c37256eSGarrett Wollman  * Initiate connection to peer.
4772c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
4782c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
4792c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
4802c37256eSGarrett Wollman  * Send initial segment on connection.
4812c37256eSGarrett Wollman  */
4822c37256eSGarrett Wollman static int
483b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
4842c37256eSGarrett Wollman {
4852c37256eSGarrett Wollman 	int error = 0;
486f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
487623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4882c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
4892c37256eSGarrett Wollman 
49057bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
491e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
492e29ef13fSDon Lewis 		return (EINVAL);
49352710de1SPawel Jakub Dawidek 	/*
49452710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
49552710de1SPawel Jakub Dawidek 	 */
4962c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
49752710de1SPawel Jakub Dawidek 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
49852710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
499b89e82ddSJamie Gritton 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
500b89e82ddSJamie Gritton 		return (error);
50175c13541SPoul-Henning Kamp 
502623dce13SRobert Watson 	TCPDEBUG0;
503623dce13SRobert Watson 	inp = sotoinpcb(so);
504623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
5058501a69cSRobert Watson 	INP_WLOCK(inp);
506eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
507eb96dc33SJulien Charbon 		error = EADDRINUSE;
508eb96dc33SJulien Charbon 		goto out;
509eb96dc33SJulien Charbon 	}
510eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
511eb96dc33SJulien Charbon 		error = ECONNREFUSED;
512623dce13SRobert Watson 		goto out;
513623dce13SRobert Watson 	}
514623dce13SRobert Watson 	tp = intotcpcb(inp);
515623dce13SRobert Watson 	TCPDEBUG1();
516b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
5172c37256eSGarrett Wollman 		goto out;
51809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
51909fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
52037cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
52109fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
52209fe6320SNavdeep Parhar 		goto out;
52309fe6320SNavdeep Parhar #endif
52409fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
52555bceb1eSRandall Stewart 	error = tp->t_fb->tfb_tcp_output(tp);
526623dce13SRobert Watson out:
527623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
5288501a69cSRobert Watson 	INP_WUNLOCK(inp);
529623dce13SRobert Watson 	return (error);
5302c37256eSGarrett Wollman }
531b287c6c7SBjoern A. Zeeb #endif /* INET */
5322c37256eSGarrett Wollman 
533fb59c426SYoshinobu Inoue #ifdef INET6
534fb59c426SYoshinobu Inoue static int
535b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
536fb59c426SYoshinobu Inoue {
537fb59c426SYoshinobu Inoue 	int error = 0;
538f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
539623dce13SRobert Watson 	struct tcpcb *tp = NULL;
540fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
541623dce13SRobert Watson 
542623dce13SRobert Watson 	TCPDEBUG0;
543fb59c426SYoshinobu Inoue 
544fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
545e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sin6p))
546e29ef13fSDon Lewis 		return (EINVAL);
54752710de1SPawel Jakub Dawidek 	/*
54852710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
54952710de1SPawel Jakub Dawidek 	 */
550fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6
55152710de1SPawel Jakub Dawidek 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
55252710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
553fb59c426SYoshinobu Inoue 
554623dce13SRobert Watson 	inp = sotoinpcb(so);
555623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
5568501a69cSRobert Watson 	INP_WLOCK(inp);
557eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
558eb96dc33SJulien Charbon 		error = EADDRINUSE;
559eb96dc33SJulien Charbon 		goto out;
560eb96dc33SJulien Charbon 	}
561eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
562eb96dc33SJulien Charbon 		error = ECONNREFUSED;
563623dce13SRobert Watson 		goto out;
564623dce13SRobert Watson 	}
565623dce13SRobert Watson 	tp = intotcpcb(inp);
566623dce13SRobert Watson 	TCPDEBUG1();
567b287c6c7SBjoern A. Zeeb #ifdef INET
568fa046d87SRobert Watson 	/*
569fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
570fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
571fa046d87SRobert Watson 	 * Is this a significant problem?
572fa046d87SRobert Watson 	 */
57333841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
574fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
575fb59c426SYoshinobu Inoue 
576d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
577d46a5312SMaxim Konovalov 			error = EINVAL;
578d46a5312SMaxim Konovalov 			goto out;
579d46a5312SMaxim Konovalov 		}
58033841545SHajimu UMEMOTO 
581fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
582fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
583fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
584b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
585b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
586413628a7SBjoern A. Zeeb 			goto out;
587b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
588fb59c426SYoshinobu Inoue 			goto out;
58909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
59009fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
591adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
59209fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
59309fe6320SNavdeep Parhar 			goto out;
59409fe6320SNavdeep Parhar #endif
59555bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
596fb59c426SYoshinobu Inoue 		goto out;
597fb59c426SYoshinobu Inoue 	}
598b287c6c7SBjoern A. Zeeb #endif
599fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
600fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
601dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
602b89e82ddSJamie Gritton 	if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
603413628a7SBjoern A. Zeeb 		goto out;
604b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
605fb59c426SYoshinobu Inoue 		goto out;
60609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
60709fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
608adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
60909fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
61009fe6320SNavdeep Parhar 		goto out;
61109fe6320SNavdeep Parhar #endif
61209fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
61355bceb1eSRandall Stewart 	error = tp->t_fb->tfb_tcp_output(tp);
614623dce13SRobert Watson 
615623dce13SRobert Watson out:
616623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
6175d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
6188501a69cSRobert Watson 	INP_WUNLOCK(inp);
619623dce13SRobert Watson 	return (error);
620fb59c426SYoshinobu Inoue }
621fb59c426SYoshinobu Inoue #endif /* INET6 */
622fb59c426SYoshinobu Inoue 
6232c37256eSGarrett Wollman /*
6242c37256eSGarrett Wollman  * Initiate disconnect from peer.
6252c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
6262c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
6272c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
6282c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
6292c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
6302c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
6312c37256eSGarrett Wollman  *
6322c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
6332c37256eSGarrett Wollman  */
6342c37256eSGarrett Wollman static int
6352c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
6362c37256eSGarrett Wollman {
637f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
638623dce13SRobert Watson 	struct tcpcb *tp = NULL;
639623dce13SRobert Watson 	int error = 0;
6402c37256eSGarrett Wollman 
641623dce13SRobert Watson 	TCPDEBUG0;
642ff9b006dSJulien Charbon 	INP_INFO_RLOCK(&V_tcbinfo);
643623dce13SRobert Watson 	inp = sotoinpcb(so);
644623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
6458501a69cSRobert Watson 	INP_WLOCK(inp);
646489dcc92SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT)
647489dcc92SJulien Charbon 		goto out;
648489dcc92SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
64921367f63SSam Leffler 		error = ECONNRESET;
650623dce13SRobert Watson 		goto out;
651623dce13SRobert Watson 	}
652623dce13SRobert Watson 	tp = intotcpcb(inp);
653623dce13SRobert Watson 	TCPDEBUG1();
654623dce13SRobert Watson 	tcp_disconnect(tp);
655623dce13SRobert Watson out:
656623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
6575d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
6588501a69cSRobert Watson 	INP_WUNLOCK(inp);
659ff9b006dSJulien Charbon 	INP_INFO_RUNLOCK(&V_tcbinfo);
660623dce13SRobert Watson 	return (error);
6612c37256eSGarrett Wollman }
6622c37256eSGarrett Wollman 
663b287c6c7SBjoern A. Zeeb #ifdef INET
6642c37256eSGarrett Wollman /*
6658296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
6668296cddfSRobert Watson  * just return the address of the peer, storing through addr.
6672c37256eSGarrett Wollman  */
6682c37256eSGarrett Wollman static int
66957bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
6702c37256eSGarrett Wollman {
6712c37256eSGarrett Wollman 	int error = 0;
672f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
6731db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
67426ef6ac4SDon Lewis 	struct in_addr addr;
67526ef6ac4SDon Lewis 	in_port_t port = 0;
6761db24ffbSJonathan Lemon 	TCPDEBUG0;
6772c37256eSGarrett Wollman 
6783d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
6793d2d3ef4SRobert Watson 		return (ECONNABORTED);
680f76fcf6dSJeffrey Hsu 
681f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
682623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
6838501a69cSRobert Watson 	INP_WLOCK(inp);
684ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
6853d2d3ef4SRobert Watson 		error = ECONNABORTED;
686623dce13SRobert Watson 		goto out;
687623dce13SRobert Watson 	}
6881db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
6891db24ffbSJonathan Lemon 	TCPDEBUG1();
690f76fcf6dSJeffrey Hsu 
691f76fcf6dSJeffrey Hsu 	/*
69254d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
69326ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
69426ef6ac4SDon Lewis 	 * release the lock.
695f76fcf6dSJeffrey Hsu 	 */
69626ef6ac4SDon Lewis 	port = inp->inp_fport;
69726ef6ac4SDon Lewis 	addr = inp->inp_faddr;
698f76fcf6dSJeffrey Hsu 
699623dce13SRobert Watson out:
700623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
7015d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7028501a69cSRobert Watson 	INP_WUNLOCK(inp);
70326ef6ac4SDon Lewis 	if (error == 0)
70426ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
70526ef6ac4SDon Lewis 	return error;
7062c37256eSGarrett Wollman }
707b287c6c7SBjoern A. Zeeb #endif /* INET */
7082c37256eSGarrett Wollman 
709fb59c426SYoshinobu Inoue #ifdef INET6
710fb59c426SYoshinobu Inoue static int
711fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
712fb59c426SYoshinobu Inoue {
713f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
714fb59c426SYoshinobu Inoue 	int error = 0;
7151db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
71626ef6ac4SDon Lewis 	struct in_addr addr;
71726ef6ac4SDon Lewis 	struct in6_addr addr6;
71826ef6ac4SDon Lewis 	in_port_t port = 0;
71926ef6ac4SDon Lewis 	int v4 = 0;
7201db24ffbSJonathan Lemon 	TCPDEBUG0;
721fb59c426SYoshinobu Inoue 
722b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
723b4470c16SRobert Watson 		return (ECONNABORTED);
724f76fcf6dSJeffrey Hsu 
725f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
726623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
727fa046d87SRobert Watson 	INP_INFO_RLOCK(&V_tcbinfo);
7288501a69cSRobert Watson 	INP_WLOCK(inp);
729ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
73021367f63SSam Leffler 		error = ECONNABORTED;
731623dce13SRobert Watson 		goto out;
732623dce13SRobert Watson 	}
7331db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
7341db24ffbSJonathan Lemon 	TCPDEBUG1();
735623dce13SRobert Watson 
73626ef6ac4SDon Lewis 	/*
73726ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
73826ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
73926ef6ac4SDon Lewis 	 * release the lock.
74026ef6ac4SDon Lewis 	 */
74126ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
74226ef6ac4SDon Lewis 		v4 = 1;
74326ef6ac4SDon Lewis 		port = inp->inp_fport;
74426ef6ac4SDon Lewis 		addr = inp->inp_faddr;
74526ef6ac4SDon Lewis 	} else {
74626ef6ac4SDon Lewis 		port = inp->inp_fport;
74726ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
74826ef6ac4SDon Lewis 	}
74926ef6ac4SDon Lewis 
750623dce13SRobert Watson out:
751623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
7525d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7538501a69cSRobert Watson 	INP_WUNLOCK(inp);
754fa046d87SRobert Watson 	INP_INFO_RUNLOCK(&V_tcbinfo);
75526ef6ac4SDon Lewis 	if (error == 0) {
75626ef6ac4SDon Lewis 		if (v4)
75726ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
75826ef6ac4SDon Lewis 		else
75926ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
76026ef6ac4SDon Lewis 	}
76126ef6ac4SDon Lewis 	return error;
762fb59c426SYoshinobu Inoue }
763fb59c426SYoshinobu Inoue #endif /* INET6 */
764f76fcf6dSJeffrey Hsu 
765f76fcf6dSJeffrey Hsu /*
7662c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
7672c37256eSGarrett Wollman  */
7682c37256eSGarrett Wollman static int
7692c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
7702c37256eSGarrett Wollman {
7712c37256eSGarrett Wollman 	int error = 0;
772f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
773623dce13SRobert Watson 	struct tcpcb *tp = NULL;
7742c37256eSGarrett Wollman 
775623dce13SRobert Watson 	TCPDEBUG0;
776ff9b006dSJulien Charbon 	INP_INFO_RLOCK(&V_tcbinfo);
777623dce13SRobert Watson 	inp = sotoinpcb(so);
778623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
7798501a69cSRobert Watson 	INP_WLOCK(inp);
780ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
78121367f63SSam Leffler 		error = ECONNRESET;
782623dce13SRobert Watson 		goto out;
783623dce13SRobert Watson 	}
784623dce13SRobert Watson 	tp = intotcpcb(inp);
785623dce13SRobert Watson 	TCPDEBUG1();
7862c37256eSGarrett Wollman 	socantsendmore(so);
787623dce13SRobert Watson 	tcp_usrclosed(tp);
788ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
78955bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
790623dce13SRobert Watson 
791623dce13SRobert Watson out:
792623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
7935d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
7948501a69cSRobert Watson 	INP_WUNLOCK(inp);
795ff9b006dSJulien Charbon 	INP_INFO_RUNLOCK(&V_tcbinfo);
796623dce13SRobert Watson 
797623dce13SRobert Watson 	return (error);
7982c37256eSGarrett Wollman }
7992c37256eSGarrett Wollman 
8002c37256eSGarrett Wollman /*
8012c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
8022c37256eSGarrett Wollman  */
8032c37256eSGarrett Wollman static int
8042c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
8052c37256eSGarrett Wollman {
806f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
807623dce13SRobert Watson 	struct tcpcb *tp = NULL;
808623dce13SRobert Watson 	int error = 0;
8092c37256eSGarrett Wollman 
810623dce13SRobert Watson 	TCPDEBUG0;
811623dce13SRobert Watson 	inp = sotoinpcb(so);
812623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
8138501a69cSRobert Watson 	INP_WLOCK(inp);
814ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
81521367f63SSam Leffler 		error = ECONNRESET;
816623dce13SRobert Watson 		goto out;
817623dce13SRobert Watson 	}
818623dce13SRobert Watson 	tp = intotcpcb(inp);
819623dce13SRobert Watson 	TCPDEBUG1();
820281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
821281a0fd4SPatrick Kelsey 	/*
822281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
823281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
824281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
825281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
826281a0fd4SPatrick Kelsey 	 * expires.
827281a0fd4SPatrick Kelsey 	 */
828281a0fd4SPatrick Kelsey 	if ((tp->t_flags & TF_FASTOPEN) &&
829281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
830281a0fd4SPatrick Kelsey 		goto out;
831281a0fd4SPatrick Kelsey #endif
83209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
83309fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
83409fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
835460cf046SNavdeep Parhar 	else
83609fe6320SNavdeep Parhar #endif
83755bceb1eSRandall Stewart 	tp->t_fb->tfb_tcp_output(tp);
838623dce13SRobert Watson 
839623dce13SRobert Watson out:
840623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
8415d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
8428501a69cSRobert Watson 	INP_WUNLOCK(inp);
843623dce13SRobert Watson 	return (error);
8442c37256eSGarrett Wollman }
8452c37256eSGarrett Wollman 
8462c37256eSGarrett Wollman /*
8472c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
8489c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
8499c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
8509c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
8519c9906e9SPeter Wemm  * generally are caller-frees.
8522c37256eSGarrett Wollman  */
8532c37256eSGarrett Wollman static int
85457bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
855b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
8562c37256eSGarrett Wollman {
8572c37256eSGarrett Wollman 	int error = 0;
858f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
859623dce13SRobert Watson 	struct tcpcb *tp = NULL;
860fb59c426SYoshinobu Inoue #ifdef INET6
861fb59c426SYoshinobu Inoue 	int isipv6;
862fb59c426SYoshinobu Inoue #endif
8639c9906e9SPeter Wemm 	TCPDEBUG0;
8642c37256eSGarrett Wollman 
865f76fcf6dSJeffrey Hsu 	/*
866fa046d87SRobert Watson 	 * We require the pcbinfo lock if we will close the socket as part of
867fa046d87SRobert Watson 	 * this call.
868f76fcf6dSJeffrey Hsu 	 */
869fa046d87SRobert Watson 	if (flags & PRUS_EOF)
870ff9b006dSJulien Charbon 		INP_INFO_RLOCK(&V_tcbinfo);
871f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
872623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
8738501a69cSRobert Watson 	INP_WLOCK(inp);
874ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
8757ff0b850SAndre Oppermann 		if (control)
8767ff0b850SAndre Oppermann 			m_freem(control);
8772cbcd3c1SGleb Smirnoff 		/*
8782cbcd3c1SGleb Smirnoff 		 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
8792cbcd3c1SGleb Smirnoff 		 * for freeing memory.
8802cbcd3c1SGleb Smirnoff 		 */
8812cbcd3c1SGleb Smirnoff 		if (m && (flags & PRUS_NOTREADY) == 0)
8827ff0b850SAndre Oppermann 			m_freem(m);
88321367f63SSam Leffler 		error = ECONNRESET;
8849c9906e9SPeter Wemm 		goto out;
8859c9906e9SPeter Wemm 	}
886fb59c426SYoshinobu Inoue #ifdef INET6
887fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
888fb59c426SYoshinobu Inoue #endif /* INET6 */
8899c9906e9SPeter Wemm 	tp = intotcpcb(inp);
8909c9906e9SPeter Wemm 	TCPDEBUG1();
8919c9906e9SPeter Wemm 	if (control) {
8929c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
8939c9906e9SPeter Wemm 		if (control->m_len) {
8949c9906e9SPeter Wemm 			m_freem(control);
8952c37256eSGarrett Wollman 			if (m)
8962c37256eSGarrett Wollman 				m_freem(m);
897744f87eaSDavid Greenman 			error = EINVAL;
898744f87eaSDavid Greenman 			goto out;
8992c37256eSGarrett Wollman 		}
9009c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
9019c9906e9SPeter Wemm 	}
9022c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
903651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
9042c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
9052c37256eSGarrett Wollman 			/*
9062c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
9072c37256eSGarrett Wollman 			 * initialize window to default value, and
9080c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
9092c37256eSGarrett Wollman 			 */
910fb59c426SYoshinobu Inoue #ifdef INET6
911fb59c426SYoshinobu Inoue 			if (isipv6)
912b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
913fb59c426SYoshinobu Inoue #endif /* INET6 */
914b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
915b287c6c7SBjoern A. Zeeb 			else
916b287c6c7SBjoern A. Zeeb #endif
917b287c6c7SBjoern A. Zeeb #ifdef INET
918b40ce416SJulian Elischer 				error = tcp_connect(tp, nam, td);
919b287c6c7SBjoern A. Zeeb #endif
9202c37256eSGarrett Wollman 			if (error)
9212c37256eSGarrett Wollman 				goto out;
9222c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
9232c37256eSGarrett Wollman 			tcp_mss(tp, -1);
9242c37256eSGarrett Wollman 		}
9252c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
9262c37256eSGarrett Wollman 			/*
9272c37256eSGarrett Wollman 			 * Close the send side of the connection after
9282c37256eSGarrett Wollman 			 * the data is sent.
9292c37256eSGarrett Wollman 			 */
930ff9b006dSJulien Charbon 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
9312c37256eSGarrett Wollman 			socantsendmore(so);
932623dce13SRobert Watson 			tcp_usrclosed(tp);
9332c37256eSGarrett Wollman 		}
9342cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
9352cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
936b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
937b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
93855bceb1eSRandall Stewart 			error = tp->t_fb->tfb_tcp_output(tp);
939b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
940b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
941b0acefa8SBill Fenner 		}
9422c37256eSGarrett Wollman 	} else {
943623dce13SRobert Watson 		/*
944623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
945623dce13SRobert Watson 		 */
946d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
9472c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
948d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
9492c37256eSGarrett Wollman 			m_freem(m);
9502c37256eSGarrett Wollman 			error = ENOBUFS;
9512c37256eSGarrett Wollman 			goto out;
9522c37256eSGarrett Wollman 		}
9532c37256eSGarrett Wollman 		/*
9542c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
9552c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
9562c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
9572c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
9582c37256eSGarrett Wollman 		 * of data past the urgent section.
9592c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
9602c37256eSGarrett Wollman 		 */
961651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
962d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
963ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
964ef53690bSGarrett Wollman 			/*
965ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
966ef53690bSGarrett Wollman 			 * initialize window to default value, and
9670c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
968ef53690bSGarrett Wollman 			 */
969fb59c426SYoshinobu Inoue #ifdef INET6
970fb59c426SYoshinobu Inoue 			if (isipv6)
971b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
972fb59c426SYoshinobu Inoue #endif /* INET6 */
973b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
974b287c6c7SBjoern A. Zeeb 			else
975b287c6c7SBjoern A. Zeeb #endif
976b287c6c7SBjoern A. Zeeb #ifdef INET
977b40ce416SJulian Elischer 				error = tcp_connect(tp, nam, td);
978b287c6c7SBjoern A. Zeeb #endif
979ef53690bSGarrett Wollman 			if (error)
980ef53690bSGarrett Wollman 				goto out;
981ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
982ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
983623dce13SRobert Watson 		}
984300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
9852cbcd3c1SGleb Smirnoff 		if (!(flags & PRUS_NOTREADY)) {
9862cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
98755bceb1eSRandall Stewart 			error = tp->t_fb->tfb_tcp_output(tp);
9882cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
9892c37256eSGarrett Wollman 		}
9902cbcd3c1SGleb Smirnoff 	}
991d1401c90SRobert Watson out:
992d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
9932c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
9945d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
9955d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
9968501a69cSRobert Watson 	INP_WUNLOCK(inp);
997fa046d87SRobert Watson 	if (flags & PRUS_EOF)
998ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
99973fddedaSPeter Grehan 	return (error);
10002c37256eSGarrett Wollman }
10012c37256eSGarrett Wollman 
10022cbcd3c1SGleb Smirnoff static int
10032cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
10042cbcd3c1SGleb Smirnoff {
10052cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
10062cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
10072cbcd3c1SGleb Smirnoff 	int error;
10082cbcd3c1SGleb Smirnoff 
10092cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
10102cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
10112cbcd3c1SGleb Smirnoff 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
10122cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
10132cbcd3c1SGleb Smirnoff 		for (int i = 0; i < count; i++)
10142cbcd3c1SGleb Smirnoff 			m = m_free(m);
10152cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
10162cbcd3c1SGleb Smirnoff 	}
10172cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
10182cbcd3c1SGleb Smirnoff 
10192cbcd3c1SGleb Smirnoff 	SOCKBUF_LOCK(&so->so_snd);
10202cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
10212cbcd3c1SGleb Smirnoff 	SOCKBUF_UNLOCK(&so->so_snd);
10222cbcd3c1SGleb Smirnoff 	if (error == 0)
102355bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
10242cbcd3c1SGleb Smirnoff 	INP_WUNLOCK(inp);
10252cbcd3c1SGleb Smirnoff 
10262cbcd3c1SGleb Smirnoff 	return (error);
10272cbcd3c1SGleb Smirnoff }
10282cbcd3c1SGleb Smirnoff 
10292c37256eSGarrett Wollman /*
1030a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
10312c37256eSGarrett Wollman  */
1032ac45e92fSRobert Watson static void
10332c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
10342c37256eSGarrett Wollman {
1035f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1036a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
1037623dce13SRobert Watson 	TCPDEBUG0;
1038c78cbc7bSRobert Watson 
1039ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1040c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1041c78cbc7bSRobert Watson 
1042ff9b006dSJulien Charbon 	INP_INFO_RLOCK(&V_tcbinfo);
10438501a69cSRobert Watson 	INP_WLOCK(inp);
1044c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1045c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1046c78cbc7bSRobert Watson 
1047c78cbc7bSRobert Watson 	/*
1048a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1049c78cbc7bSRobert Watson 	 */
1050ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1051ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1052c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
1053a152f8a3SRobert Watson 		TCPDEBUG1();
1054c78cbc7bSRobert Watson 		tcp_drop(tp, ECONNABORTED);
1055a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
10565d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1057c78cbc7bSRobert Watson 	}
1058ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1059a152f8a3SRobert Watson 		SOCK_LOCK(so);
1060a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1061a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1062ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1063a152f8a3SRobert Watson 	}
10648501a69cSRobert Watson 	INP_WUNLOCK(inp);
1065ff9b006dSJulien Charbon 	INP_INFO_RUNLOCK(&V_tcbinfo);
1066a152f8a3SRobert Watson }
1067a152f8a3SRobert Watson 
1068a152f8a3SRobert Watson /*
1069a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1070a152f8a3SRobert Watson  */
1071a152f8a3SRobert Watson static void
1072a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1073a152f8a3SRobert Watson {
1074a152f8a3SRobert Watson 	struct inpcb *inp;
1075a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
1076a152f8a3SRobert Watson 	TCPDEBUG0;
1077a152f8a3SRobert Watson 
1078a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1079a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1080a152f8a3SRobert Watson 
1081ff9b006dSJulien Charbon 	INP_INFO_RLOCK(&V_tcbinfo);
10828501a69cSRobert Watson 	INP_WLOCK(inp);
1083a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1084a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1085a152f8a3SRobert Watson 
1086a152f8a3SRobert Watson 	/*
1087a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
1088a152f8a3SRobert Watson 	 * a disconnect.
1089a152f8a3SRobert Watson 	 */
1090ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1091ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1092a152f8a3SRobert Watson 		tp = intotcpcb(inp);
1093a152f8a3SRobert Watson 		TCPDEBUG1();
1094a152f8a3SRobert Watson 		tcp_disconnect(tp);
1095a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
10965d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1097a152f8a3SRobert Watson 	}
1098ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1099a152f8a3SRobert Watson 		SOCK_LOCK(so);
1100a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1101a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1102ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1103a152f8a3SRobert Watson 	}
11048501a69cSRobert Watson 	INP_WUNLOCK(inp);
1105ff9b006dSJulien Charbon 	INP_INFO_RUNLOCK(&V_tcbinfo);
11062c37256eSGarrett Wollman }
11072c37256eSGarrett Wollman 
11082c37256eSGarrett Wollman /*
11092c37256eSGarrett Wollman  * Receive out-of-band data.
11102c37256eSGarrett Wollman  */
11112c37256eSGarrett Wollman static int
11122c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
11132c37256eSGarrett Wollman {
11142c37256eSGarrett Wollman 	int error = 0;
1115f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1116623dce13SRobert Watson 	struct tcpcb *tp = NULL;
11172c37256eSGarrett Wollman 
1118623dce13SRobert Watson 	TCPDEBUG0;
1119623dce13SRobert Watson 	inp = sotoinpcb(so);
1120623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
11218501a69cSRobert Watson 	INP_WLOCK(inp);
1122ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
112321367f63SSam Leffler 		error = ECONNRESET;
1124623dce13SRobert Watson 		goto out;
1125623dce13SRobert Watson 	}
1126623dce13SRobert Watson 	tp = intotcpcb(inp);
1127623dce13SRobert Watson 	TCPDEBUG1();
11282c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1129c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
11304cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
11314cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
11322c37256eSGarrett Wollman 		error = EINVAL;
11332c37256eSGarrett Wollman 		goto out;
11342c37256eSGarrett Wollman 	}
11352c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
11362c37256eSGarrett Wollman 		error = EWOULDBLOCK;
11372c37256eSGarrett Wollman 		goto out;
11382c37256eSGarrett Wollman 	}
11392c37256eSGarrett Wollman 	m->m_len = 1;
11402c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
11412c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
11422c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1143623dce13SRobert Watson 
1144623dce13SRobert Watson out:
1145623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
11465d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
11478501a69cSRobert Watson 	INP_WUNLOCK(inp);
1148623dce13SRobert Watson 	return (error);
11492c37256eSGarrett Wollman }
11502c37256eSGarrett Wollman 
1151b287c6c7SBjoern A. Zeeb #ifdef INET
11522c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1153756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1154756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1155756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1156756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1157756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1158756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1159756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1160756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1161756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
116254d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1163756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1164756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1165756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
11662cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1167756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
116854d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1169a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1170a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
11712c37256eSGarrett Wollman };
1172b287c6c7SBjoern A. Zeeb #endif /* INET */
1173df8bae1dSRodney W. Grimes 
1174fb59c426SYoshinobu Inoue #ifdef INET6
1175fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1176756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1177756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1178756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1179756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1180756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1181756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1182756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1183756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1184756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1185756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1186756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1187756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1188756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
11892cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1190756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1191756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1192a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1193a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1194fb59c426SYoshinobu Inoue };
1195fb59c426SYoshinobu Inoue #endif /* INET6 */
1196fb59c426SYoshinobu Inoue 
1197b287c6c7SBjoern A. Zeeb #ifdef INET
1198a0292f23SGarrett Wollman /*
1199a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1200a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
12015200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
12025200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
12035200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
12045200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1205a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1206a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1207a0292f23SGarrett Wollman  */
12080312fbe9SPoul-Henning Kamp static int
1209ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1210a0292f23SGarrett Wollman {
1211a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1212a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
12135200e00eSIan Dowse 	struct in_addr laddr;
12145200e00eSIan Dowse 	u_short lport;
1215c3229e05SDavid Greenman 	int error;
1216a0292f23SGarrett Wollman 
12178501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1218fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1219623dce13SRobert Watson 
1220a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
1221b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1222a0292f23SGarrett Wollman 		if (error)
1223fa046d87SRobert Watson 			goto out;
1224a0292f23SGarrett Wollman 	}
1225a0292f23SGarrett Wollman 
1226a0292f23SGarrett Wollman 	/*
1227a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1228a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1229a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1230a0292f23SGarrett Wollman 	 */
12315200e00eSIan Dowse 	laddr = inp->inp_laddr;
12325200e00eSIan Dowse 	lport = inp->inp_lport;
12335200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1234b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
12355200e00eSIan Dowse 	if (error && oinp == NULL)
1236fa046d87SRobert Watson 		goto out;
1237fa046d87SRobert Watson 	if (oinp) {
1238fa046d87SRobert Watson 		error = EADDRINUSE;
1239fa046d87SRobert Watson 		goto out;
1240fa046d87SRobert Watson 	}
12415200e00eSIan Dowse 	inp->inp_laddr = laddr;
124215bd2b43SDavid Greenman 	in_pcbrehash(inp);
1243fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1244a0292f23SGarrett Wollman 
1245087b55eaSAndre Oppermann 	/*
1246087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1247087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1248087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1249087b55eaSAndre Oppermann 	 */
1250a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
12519b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1252a0292f23SGarrett Wollman 		tp->request_r_scale++;
1253a0292f23SGarrett Wollman 
1254a0292f23SGarrett Wollman 	soisconnecting(so);
125578b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
125657f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
1257b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
1258a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1259a45d2726SAndras Olah 
1260a0292f23SGarrett Wollman 	return 0;
1261fa046d87SRobert Watson 
1262fa046d87SRobert Watson out:
1263fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1264fa046d87SRobert Watson 	return (error);
1265a0292f23SGarrett Wollman }
1266b287c6c7SBjoern A. Zeeb #endif /* INET */
1267a0292f23SGarrett Wollman 
1268fb59c426SYoshinobu Inoue #ifdef INET6
1269fb59c426SYoshinobu Inoue static int
1270ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1271fb59c426SYoshinobu Inoue {
1272a7e201bbSAndrey V. Elsukov 	struct inpcb *inp = tp->t_inpcb;
1273fb59c426SYoshinobu Inoue 	int error;
1274fb59c426SYoshinobu Inoue 
12758501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1276fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1277623dce13SRobert Watson 
1278fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
1279b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1280fb59c426SYoshinobu Inoue 		if (error)
1281fa046d87SRobert Watson 			goto out;
1282fb59c426SYoshinobu Inoue 	}
1283a7e201bbSAndrey V. Elsukov 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1284a7e201bbSAndrey V. Elsukov 	if (error != 0)
1285b598155aSRobert Watson 		goto out;
1286fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1287fb59c426SYoshinobu Inoue 
1288fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1289fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1290970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1291fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1292fb59c426SYoshinobu Inoue 
1293a7e201bbSAndrey V. Elsukov 	soisconnecting(inp->inp_socket);
129478b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
129557f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
1296b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
1297fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1298fb59c426SYoshinobu Inoue 
1299fb59c426SYoshinobu Inoue 	return 0;
1300fa046d87SRobert Watson 
1301fa046d87SRobert Watson out:
1302fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1303fa046d87SRobert Watson 	return error;
1304fb59c426SYoshinobu Inoue }
1305fb59c426SYoshinobu Inoue #endif /* INET6 */
1306fb59c426SYoshinobu Inoue 
1307cfe8b629SGarrett Wollman /*
1308b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1309b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1310b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1311b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1312b8af5dfaSRobert Watson  * from Linux.
1313b8af5dfaSRobert Watson  */
1314b8af5dfaSRobert Watson static void
1315ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1316b8af5dfaSRobert Watson {
1317b8af5dfaSRobert Watson 
13188501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1319b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1320b8af5dfaSRobert Watson 
1321b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1322b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1323b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
13243529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1325b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1326b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1327b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1328b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1329b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1330b8af5dfaSRobert Watson 	}
13311baaf834SBruce M Simpson 
133243d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
133343d94734SJohn Baldwin 	ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
13341baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
13351baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
13361baaf834SBruce M Simpson 
1337b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1338b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1339b8af5dfaSRobert Watson 
1340b8af5dfaSRobert Watson 	/*
1341b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1342b8af5dfaSRobert Watson 	 */
1343c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1344535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1345b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
13461c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1347535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
134843d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
134943d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1350535fbad6SKip Macy 	if (tp->t_flags & TF_TOE)
1351535fbad6SKip Macy 		ti->tcpi_options |= TCPI_OPT_TOE;
1352f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1353f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1354f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1355b8af5dfaSRobert Watson }
1356b8af5dfaSRobert Watson 
1357b8af5dfaSRobert Watson /*
13581e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
13591e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
13601e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
13611e8f5ffaSRobert Watson  * option.
1362cfe8b629SGarrett Wollman  */
13638501a69cSRobert Watson #define INP_WLOCK_RECHECK(inp) do {					\
13648501a69cSRobert Watson 	INP_WLOCK(inp);							\
1365ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
13668501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
13671e8f5ffaSRobert Watson 		return (ECONNRESET);					\
13681e8f5ffaSRobert Watson 	}								\
13691e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
13701e8f5ffaSRobert Watson } while(0)
13711e8f5ffaSRobert Watson 
1372df8bae1dSRodney W. Grimes int
1373ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1374df8bae1dSRodney W. Grimes {
137555bceb1eSRandall Stewart 	int	error;
1376df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1377cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
137855bceb1eSRandall Stewart 	struct tcp_function_block *blk;
137955bceb1eSRandall Stewart 	struct tcp_function_set fsn;
1380df8bae1dSRodney W. Grimes 
1381cfe8b629SGarrett Wollman 	error = 0;
1382df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1383623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
13848501a69cSRobert Watson 	INP_WLOCK(inp);
1385cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1386fb59c426SYoshinobu Inoue #ifdef INET6
13875cd54324SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6PROTO) {
13888501a69cSRobert Watson 			INP_WUNLOCK(inp);
1389fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
1390b287c6c7SBjoern A. Zeeb 		}
1391fb59c426SYoshinobu Inoue #endif /* INET6 */
1392b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1393b287c6c7SBjoern A. Zeeb 		else
1394b287c6c7SBjoern A. Zeeb #endif
1395b287c6c7SBjoern A. Zeeb #ifdef INET
1396b287c6c7SBjoern A. Zeeb 		{
13978501a69cSRobert Watson 			INP_WUNLOCK(inp);
1398cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
13991e8f5ffaSRobert Watson 		}
14001e8f5ffaSRobert Watson #endif
1401df8bae1dSRodney W. Grimes 		return (error);
1402df8bae1dSRodney W. Grimes 	}
1403ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
14048501a69cSRobert Watson 		INP_WUNLOCK(inp);
14051e8f5ffaSRobert Watson 		return (ECONNRESET);
1406623dce13SRobert Watson 	}
140755bceb1eSRandall Stewart 	tp = intotcpcb(inp);
140855bceb1eSRandall Stewart 	/*
140955bceb1eSRandall Stewart 	 * Protect the TCP option TCP_FUNCTION_BLK so
141055bceb1eSRandall Stewart 	 * that a sub-function can *never* overwrite this.
141155bceb1eSRandall Stewart 	 */
141255bceb1eSRandall Stewart 	if ((sopt->sopt_dir == SOPT_SET) &&
141355bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
141455bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
141555bceb1eSRandall Stewart 		error = sooptcopyin(sopt, &fsn, sizeof fsn,
141655bceb1eSRandall Stewart 		    sizeof fsn);
141755bceb1eSRandall Stewart 		if (error)
141855bceb1eSRandall Stewart 			return (error);
141955bceb1eSRandall Stewart 		INP_WLOCK_RECHECK(inp);
142055bceb1eSRandall Stewart 		if (tp->t_state != TCPS_CLOSED) {
142155bceb1eSRandall Stewart 			/*
142255bceb1eSRandall Stewart 			 * The user has advanced the state
142355bceb1eSRandall Stewart 			 * past the initial point, we can't
142455bceb1eSRandall Stewart 			 * switch since we are down the road
142555bceb1eSRandall Stewart 			 * and a new set of functions may
142655bceb1eSRandall Stewart 			 * not be compatibile.
142755bceb1eSRandall Stewart 			 */
142855bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
142955bceb1eSRandall Stewart 			return(EINVAL);
143055bceb1eSRandall Stewart 		}
143155bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
143255bceb1eSRandall Stewart 		if (blk == NULL) {
143355bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
143455bceb1eSRandall Stewart 			return (ENOENT);
143555bceb1eSRandall Stewart 		}
143655bceb1eSRandall Stewart 		if (tp->t_fb != blk) {
143755bceb1eSRandall Stewart 			if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
143855bceb1eSRandall Stewart 				refcount_release(&blk->tfb_refcnt);
143955bceb1eSRandall Stewart 				INP_WUNLOCK(inp);
144055bceb1eSRandall Stewart 				return (ENOENT);
144155bceb1eSRandall Stewart 			}
144255bceb1eSRandall Stewart 			/*
144355bceb1eSRandall Stewart 			 * Release the old refcnt, the
144455bceb1eSRandall Stewart 			 * lookup acquires a ref on the
144555bceb1eSRandall Stewart 			 * new one.
144655bceb1eSRandall Stewart 			 */
144755bceb1eSRandall Stewart 			if (tp->t_fb->tfb_tcp_fb_fini)
144855bceb1eSRandall Stewart 				(*tp->t_fb->tfb_tcp_fb_fini)(tp);
144955bceb1eSRandall Stewart 			refcount_release(&tp->t_fb->tfb_refcnt);
145055bceb1eSRandall Stewart 			tp->t_fb = blk;
145155bceb1eSRandall Stewart 			if (tp->t_fb->tfb_tcp_fb_init) {
145255bceb1eSRandall Stewart 				(*tp->t_fb->tfb_tcp_fb_init)(tp);
145355bceb1eSRandall Stewart 			}
145455bceb1eSRandall Stewart 		}
145555bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
145655bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
145755bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
145855bceb1eSRandall Stewart 			     sopt->sopt_name);
145955bceb1eSRandall Stewart 		}
146055bceb1eSRandall Stewart #endif
146155bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
146255bceb1eSRandall Stewart 		return (error);
146355bceb1eSRandall Stewart 	} else if ((sopt->sopt_dir == SOPT_GET) &&
146455bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
146555bceb1eSRandall Stewart 		strcpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name);
146655bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
146755bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
146855bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
146955bceb1eSRandall Stewart 		return (error);
147055bceb1eSRandall Stewart 	}
147155bceb1eSRandall Stewart 	/* Pass in the INP locked, called must unlock it */
147255bceb1eSRandall Stewart 	return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
147355bceb1eSRandall Stewart }
147455bceb1eSRandall Stewart 
147555bceb1eSRandall Stewart int
147655bceb1eSRandall Stewart tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
147755bceb1eSRandall Stewart {
147855bceb1eSRandall Stewart 	int	error, opt, optval;
147955bceb1eSRandall Stewart 	u_int	ui;
148055bceb1eSRandall Stewart 	struct	tcp_info ti;
148155bceb1eSRandall Stewart 	struct cc_algo *algo;
148255bceb1eSRandall Stewart 	char buf[TCP_CA_NAME_MAX];
1483df8bae1dSRodney W. Grimes 
1484cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1485cfe8b629SGarrett Wollman 	case SOPT_SET:
1486cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
14871cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
148888f6b043SBruce M Simpson 		case TCP_MD5SIG:
14898501a69cSRobert Watson 			INP_WUNLOCK(inp);
14901cfd4b53SBruce M Simpson 			error = sooptcopyin(sopt, &optval, sizeof optval,
14911cfd4b53SBruce M Simpson 			    sizeof optval);
14921cfd4b53SBruce M Simpson 			if (error)
14931e8f5ffaSRobert Watson 				return (error);
14941cfd4b53SBruce M Simpson 
14958501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
14961cfd4b53SBruce M Simpson 			if (optval > 0)
14971cfd4b53SBruce M Simpson 				tp->t_flags |= TF_SIGNATURE;
14981cfd4b53SBruce M Simpson 			else
14991cfd4b53SBruce M Simpson 				tp->t_flags &= ~TF_SIGNATURE;
150009fe6320SNavdeep Parhar 			goto unlock_and_done;
15011cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
150209fe6320SNavdeep Parhar 
1503df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1504cfe8b629SGarrett Wollman 		case TCP_NOOPT:
15058501a69cSRobert Watson 			INP_WUNLOCK(inp);
1506cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1507cfe8b629SGarrett Wollman 			    sizeof optval);
1508cfe8b629SGarrett Wollman 			if (error)
15091e8f5ffaSRobert Watson 				return (error);
1510cfe8b629SGarrett Wollman 
15118501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1512cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1513cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1514cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1515cfe8b629SGarrett Wollman 				break;
1516cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1517cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1518cfe8b629SGarrett Wollman 				break;
1519cfe8b629SGarrett Wollman 			default:
1520cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1521cfe8b629SGarrett Wollman 				break;
1522cfe8b629SGarrett Wollman 			}
1523cfe8b629SGarrett Wollman 
1524cfe8b629SGarrett Wollman 			if (optval)
1525cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1526df8bae1dSRodney W. Grimes 			else
1527cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
152809fe6320SNavdeep Parhar unlock_and_done:
152909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
153009fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
153109fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
153209fe6320SNavdeep Parhar 				    sopt->sopt_name);
153309fe6320SNavdeep Parhar 			}
153409fe6320SNavdeep Parhar #endif
15358501a69cSRobert Watson 			INP_WUNLOCK(inp);
1536df8bae1dSRodney W. Grimes 			break;
1537df8bae1dSRodney W. Grimes 
1538007581c0SJonathan Lemon 		case TCP_NOPUSH:
15398501a69cSRobert Watson 			INP_WUNLOCK(inp);
1540007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1541007581c0SJonathan Lemon 			    sizeof optval);
1542007581c0SJonathan Lemon 			if (error)
15431e8f5ffaSRobert Watson 				return (error);
1544007581c0SJonathan Lemon 
15458501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1546007581c0SJonathan Lemon 			if (optval)
1547007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1548d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
1549007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1550d28b9e89SJohn Baldwin 				if (TCPS_HAVEESTABLISHED(tp->t_state))
155155bceb1eSRandall Stewart 					error = tp->t_fb->tfb_tcp_output(tp);
1552007581c0SJonathan Lemon 			}
155309fe6320SNavdeep Parhar 			goto unlock_and_done;
1554007581c0SJonathan Lemon 
1555df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
15568501a69cSRobert Watson 			INP_WUNLOCK(inp);
1557cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1558cfe8b629SGarrett Wollman 			    sizeof optval);
1559cfe8b629SGarrett Wollman 			if (error)
15601e8f5ffaSRobert Watson 				return (error);
1561df8bae1dSRodney W. Grimes 
15628501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
156353369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
1564603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
1565cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1566a0292f23SGarrett Wollman 			else
1567a0292f23SGarrett Wollman 				error = EINVAL;
156809fe6320SNavdeep Parhar 			goto unlock_and_done;
1569a0292f23SGarrett Wollman 
1570b8af5dfaSRobert Watson 		case TCP_INFO:
15718501a69cSRobert Watson 			INP_WUNLOCK(inp);
1572b8af5dfaSRobert Watson 			error = EINVAL;
1573b8af5dfaSRobert Watson 			break;
1574b8af5dfaSRobert Watson 
1575dbc42409SLawrence Stewart 		case TCP_CONGESTION:
1576dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
1577dbc42409SLawrence Stewart 			bzero(buf, sizeof(buf));
1578dbc42409SLawrence Stewart 			error = sooptcopyin(sopt, &buf, sizeof(buf), 1);
1579dbc42409SLawrence Stewart 			if (error)
1580dbc42409SLawrence Stewart 				break;
1581dbc42409SLawrence Stewart 			INP_WLOCK_RECHECK(inp);
1582dbc42409SLawrence Stewart 			/*
1583dbc42409SLawrence Stewart 			 * Return EINVAL if we can't find the requested cc algo.
1584dbc42409SLawrence Stewart 			 */
1585dbc42409SLawrence Stewart 			error = EINVAL;
1586dbc42409SLawrence Stewart 			CC_LIST_RLOCK();
1587dbc42409SLawrence Stewart 			STAILQ_FOREACH(algo, &cc_list, entries) {
1588dbc42409SLawrence Stewart 				if (strncmp(buf, algo->name, TCP_CA_NAME_MAX)
1589dbc42409SLawrence Stewart 				    == 0) {
1590dbc42409SLawrence Stewart 					/* We've found the requested algo. */
1591dbc42409SLawrence Stewart 					error = 0;
1592dbc42409SLawrence Stewart 					/*
1593dbc42409SLawrence Stewart 					 * We hold a write lock over the tcb
1594dbc42409SLawrence Stewart 					 * so it's safe to do these things
1595dbc42409SLawrence Stewart 					 * without ordering concerns.
1596dbc42409SLawrence Stewart 					 */
1597dbc42409SLawrence Stewart 					if (CC_ALGO(tp)->cb_destroy != NULL)
1598dbc42409SLawrence Stewart 						CC_ALGO(tp)->cb_destroy(tp->ccv);
1599dbc42409SLawrence Stewart 					CC_ALGO(tp) = algo;
1600dbc42409SLawrence Stewart 					/*
1601dbc42409SLawrence Stewart 					 * If something goes pear shaped
1602dbc42409SLawrence Stewart 					 * initialising the new algo,
1603dbc42409SLawrence Stewart 					 * fall back to newreno (which
1604dbc42409SLawrence Stewart 					 * does not require initialisation).
1605dbc42409SLawrence Stewart 					 */
1606dbc42409SLawrence Stewart 					if (algo->cb_init != NULL)
1607dbc42409SLawrence Stewart 						if (algo->cb_init(tp->ccv) > 0) {
1608dbc42409SLawrence Stewart 							CC_ALGO(tp) = &newreno_cc_algo;
1609dbc42409SLawrence Stewart 							/*
1610dbc42409SLawrence Stewart 							 * The only reason init
1611dbc42409SLawrence Stewart 							 * should fail is
1612dbc42409SLawrence Stewart 							 * because of malloc.
1613dbc42409SLawrence Stewart 							 */
1614dbc42409SLawrence Stewart 							error = ENOMEM;
1615dbc42409SLawrence Stewart 						}
1616dbc42409SLawrence Stewart 					break; /* Break the STAILQ_FOREACH. */
1617dbc42409SLawrence Stewart 				}
1618dbc42409SLawrence Stewart 			}
1619dbc42409SLawrence Stewart 			CC_LIST_RUNLOCK();
162009fe6320SNavdeep Parhar 			goto unlock_and_done;
1621dbc42409SLawrence Stewart 
16229077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
16239077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
16249077f387SGleb Smirnoff 		case TCP_KEEPINIT:
16259077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
16269077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
16279077f387SGleb Smirnoff 			if (error)
16289077f387SGleb Smirnoff 				return (error);
16299077f387SGleb Smirnoff 
16309077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
16319077f387SGleb Smirnoff 				error = EINVAL;
16329077f387SGleb Smirnoff 				break;
16339077f387SGleb Smirnoff 			}
16349077f387SGleb Smirnoff 			ui *= hz;
16359077f387SGleb Smirnoff 
16369077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
16379077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
16389077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
16399077f387SGleb Smirnoff 				tp->t_keepidle = ui;
16409077f387SGleb Smirnoff 				/*
16419077f387SGleb Smirnoff 				 * XXX: better check current remaining
16429077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
16439077f387SGleb Smirnoff 				 */
16449077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
16459077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
16469077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
16479077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
16489077f387SGleb Smirnoff 				break;
16499077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
16509077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
16519077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
16529077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
16539077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
16549077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
16559077f387SGleb Smirnoff 				break;
16569077f387SGleb Smirnoff 			case TCP_KEEPINIT:
16579077f387SGleb Smirnoff 				tp->t_keepinit = ui;
16589077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
16599077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
16609077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
16619077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
16629077f387SGleb Smirnoff 				break;
16639077f387SGleb Smirnoff 			}
166409fe6320SNavdeep Parhar 			goto unlock_and_done;
16659077f387SGleb Smirnoff 
166685c05144SGleb Smirnoff 		case TCP_KEEPCNT:
166785c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
166885c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
166985c05144SGleb Smirnoff 			if (error)
167085c05144SGleb Smirnoff 				return (error);
167185c05144SGleb Smirnoff 
167285c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
167385c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
167485c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
167585c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
167685c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
167785c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
167885c05144SGleb Smirnoff 			goto unlock_and_done;
167985c05144SGleb Smirnoff 
168086a996e6SHiren Panchasara #ifdef TCPPCAP
168186a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
168286a996e6SHiren Panchasara 		case TCP_PCAP_IN:
168386a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
168486a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
168586a996e6SHiren Panchasara 			    sizeof optval);
168686a996e6SHiren Panchasara 			if (error)
168786a996e6SHiren Panchasara 				return (error);
168886a996e6SHiren Panchasara 
168986a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
169086a996e6SHiren Panchasara 			if (optval >= 0)
169186a996e6SHiren Panchasara 				tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
169286a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
169386a996e6SHiren Panchasara 					optval);
169486a996e6SHiren Panchasara 			else
169586a996e6SHiren Panchasara 				error = EINVAL;
169686a996e6SHiren Panchasara 			goto unlock_and_done;
169786a996e6SHiren Panchasara #endif
169886a996e6SHiren Panchasara 
1699281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
1700281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
1701281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
1702281a0fd4SPatrick Kelsey 			if (!V_tcp_fastopen_enabled)
1703281a0fd4SPatrick Kelsey 				return (EPERM);
1704281a0fd4SPatrick Kelsey 
1705281a0fd4SPatrick Kelsey 			error = sooptcopyin(sopt, &optval, sizeof optval,
1706281a0fd4SPatrick Kelsey 			    sizeof optval);
1707281a0fd4SPatrick Kelsey 			if (error)
1708281a0fd4SPatrick Kelsey 				return (error);
1709281a0fd4SPatrick Kelsey 
1710281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
1711281a0fd4SPatrick Kelsey 			if (optval) {
1712281a0fd4SPatrick Kelsey 				tp->t_flags |= TF_FASTOPEN;
1713281a0fd4SPatrick Kelsey 				if ((tp->t_state == TCPS_LISTEN) &&
1714281a0fd4SPatrick Kelsey 				    (tp->t_tfo_pending == NULL))
1715281a0fd4SPatrick Kelsey 					tp->t_tfo_pending =
1716281a0fd4SPatrick Kelsey 					    tcp_fastopen_alloc_counter();
1717281a0fd4SPatrick Kelsey 			} else
1718281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1719281a0fd4SPatrick Kelsey 			goto unlock_and_done;
1720281a0fd4SPatrick Kelsey #endif
1721281a0fd4SPatrick Kelsey 
1722df8bae1dSRodney W. Grimes 		default:
17238501a69cSRobert Watson 			INP_WUNLOCK(inp);
1724df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1725df8bae1dSRodney W. Grimes 			break;
1726df8bae1dSRodney W. Grimes 		}
1727df8bae1dSRodney W. Grimes 		break;
1728df8bae1dSRodney W. Grimes 
1729cfe8b629SGarrett Wollman 	case SOPT_GET:
17301e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
1731cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
17321cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
173388f6b043SBruce M Simpson 		case TCP_MD5SIG:
17341cfd4b53SBruce M Simpson 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
17358501a69cSRobert Watson 			INP_WUNLOCK(inp);
1736b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
17371cfd4b53SBruce M Simpson 			break;
1738265ed012SBruce M Simpson #endif
17391e8f5ffaSRobert Watson 
1740df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1741cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
17428501a69cSRobert Watson 			INP_WUNLOCK(inp);
1743b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1744df8bae1dSRodney W. Grimes 			break;
1745df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1746cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
17478501a69cSRobert Watson 			INP_WUNLOCK(inp);
1748b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1749df8bae1dSRodney W. Grimes 			break;
1750a0292f23SGarrett Wollman 		case TCP_NOOPT:
1751cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
17528501a69cSRobert Watson 			INP_WUNLOCK(inp);
1753b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1754a0292f23SGarrett Wollman 			break;
1755a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1756cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
17578501a69cSRobert Watson 			INP_WUNLOCK(inp);
1758b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1759b8af5dfaSRobert Watson 			break;
1760b8af5dfaSRobert Watson 		case TCP_INFO:
1761b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
17628501a69cSRobert Watson 			INP_WUNLOCK(inp);
1763b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
1764a0292f23SGarrett Wollman 			break;
1765dbc42409SLawrence Stewart 		case TCP_CONGESTION:
1766dbc42409SLawrence Stewart 			bzero(buf, sizeof(buf));
1767dbc42409SLawrence Stewart 			strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1768dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
1769dbc42409SLawrence Stewart 			error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX);
1770dbc42409SLawrence Stewart 			break;
17712f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
17722f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
17732f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
17742f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
17752f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
17762f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
17772f3eb7f4SGleb Smirnoff 				ui = tp->t_keepidle / hz;
17782f3eb7f4SGleb Smirnoff 				break;
17792f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
17802f3eb7f4SGleb Smirnoff 				ui = tp->t_keepintvl / hz;
17812f3eb7f4SGleb Smirnoff 				break;
17822f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
17832f3eb7f4SGleb Smirnoff 				ui = tp->t_keepinit / hz;
17842f3eb7f4SGleb Smirnoff 				break;
17852f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
17862f3eb7f4SGleb Smirnoff 				ui = tp->t_keepcnt;
17872f3eb7f4SGleb Smirnoff 				break;
17882f3eb7f4SGleb Smirnoff 			}
17892f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
17902f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
17912f3eb7f4SGleb Smirnoff 			break;
179286a996e6SHiren Panchasara #ifdef TCPPCAP
179386a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
179486a996e6SHiren Panchasara 		case TCP_PCAP_IN:
179586a996e6SHiren Panchasara 			optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
179686a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
179786a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
179886a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
179986a996e6SHiren Panchasara 			break;
180086a996e6SHiren Panchasara #endif
1801281a0fd4SPatrick Kelsey 
1802281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
1803281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
1804281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
1805281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
1806281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
1807281a0fd4SPatrick Kelsey 			break;
1808281a0fd4SPatrick Kelsey #endif
1809df8bae1dSRodney W. Grimes 		default:
18108501a69cSRobert Watson 			INP_WUNLOCK(inp);
1811df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1812df8bae1dSRodney W. Grimes 			break;
1813df8bae1dSRodney W. Grimes 		}
1814df8bae1dSRodney W. Grimes 		break;
1815df8bae1dSRodney W. Grimes 	}
1816df8bae1dSRodney W. Grimes 	return (error);
1817df8bae1dSRodney W. Grimes }
18188501a69cSRobert Watson #undef INP_WLOCK_RECHECK
1819df8bae1dSRodney W. Grimes 
182026e30fbbSDavid Greenman /*
1821df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1822df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1823df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1824df8bae1dSRodney W. Grimes  */
18250312fbe9SPoul-Henning Kamp static int
1826ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so)
1827df8bae1dSRodney W. Grimes {
1828ad3f9ab3SAndre Oppermann 	struct tcpcb *tp;
1829df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1830df8bae1dSRodney W. Grimes 	int error;
1831df8bae1dSRodney W. Grimes 
1832df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1833e233e2acSAndre Oppermann 		error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1834df8bae1dSRodney W. Grimes 		if (error)
1835df8bae1dSRodney W. Grimes 			return (error);
1836df8bae1dSRodney W. Grimes 	}
18376741ecf5SAndre Oppermann 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
18386741ecf5SAndre Oppermann 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1839ff9b006dSJulien Charbon 	INP_INFO_RLOCK(&V_tcbinfo);
1840603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_tcbinfo);
1841f2de87feSRobert Watson 	if (error) {
1842ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
1843df8bae1dSRodney W. Grimes 		return (error);
1844f2de87feSRobert Watson 	}
1845df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1846fb59c426SYoshinobu Inoue #ifdef INET6
18475cd54324SBjoern A. Zeeb 	if (inp->inp_vflag & INP_IPV6PROTO) {
1848fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1849fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1850fb59c426SYoshinobu Inoue 	}
1851fb59c426SYoshinobu Inoue 	else
1852fb59c426SYoshinobu Inoue #endif
1853cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1854df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1855623dce13SRobert Watson 	if (tp == NULL) {
1856df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
18570206cdb8SBjoern A. Zeeb 		in_pcbfree(inp);
1858ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
1859df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1860df8bae1dSRodney W. Grimes 	}
1861df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
18628501a69cSRobert Watson 	INP_WUNLOCK(inp);
1863ff9b006dSJulien Charbon 	INP_INFO_RUNLOCK(&V_tcbinfo);
1864df8bae1dSRodney W. Grimes 	return (0);
1865df8bae1dSRodney W. Grimes }
1866df8bae1dSRodney W. Grimes 
1867df8bae1dSRodney W. Grimes /*
1868df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1869df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1870df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1871df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1872df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1873df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1874df8bae1dSRodney W. Grimes  */
1875623dce13SRobert Watson static void
1876ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
1877df8bae1dSRodney W. Grimes {
1878e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
1879e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
1880e6e0b5ffSRobert Watson 
1881ff9b006dSJulien Charbon 	INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
18828501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1883df8bae1dSRodney W. Grimes 
1884623dce13SRobert Watson 	/*
1885623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1886623dce13SRobert Watson 	 * socket is still open.
1887623dce13SRobert Watson 	 */
1888623dce13SRobert Watson 	if (tp->t_state < TCPS_ESTABLISHED) {
1889df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1890623dce13SRobert Watson 		KASSERT(tp != NULL,
1891623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
1892623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1893243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
1894623dce13SRobert Watson 		KASSERT(tp != NULL,
1895623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
1896623dce13SRobert Watson 	} else {
1897df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
1898df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
1899623dce13SRobert Watson 		tcp_usrclosed(tp);
1900ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
190155bceb1eSRandall Stewart 			tp->t_fb->tfb_tcp_output(tp);
1902df8bae1dSRodney W. Grimes 	}
1903df8bae1dSRodney W. Grimes }
1904df8bae1dSRodney W. Grimes 
1905df8bae1dSRodney W. Grimes /*
1906df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1907df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1908df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1909df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1910df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1911df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1912df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1913df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1914df8bae1dSRodney W. Grimes  */
1915623dce13SRobert Watson static void
1916ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
1917df8bae1dSRodney W. Grimes {
1918df8bae1dSRodney W. Grimes 
1919ff9b006dSJulien Charbon 	INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
19208501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1921e6e0b5ffSRobert Watson 
1922df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1923df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
192409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
192509fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
192609fe6320SNavdeep Parhar #endif
1927550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
1928bc65987aSKip Macy 		/* FALLTHROUGH */
1929bc65987aSKip Macy 	case TCPS_CLOSED:
1930df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1931623dce13SRobert Watson 		/*
1932623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
1933623dce13SRobert Watson 		 * still open.
1934623dce13SRobert Watson 		 */
1935623dce13SRobert Watson 		KASSERT(tp != NULL,
1936623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
1937df8bae1dSRodney W. Grimes 		break;
1938df8bae1dSRodney W. Grimes 
1939a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
1940df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1941a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
1942a0292f23SGarrett Wollman 		break;
1943a0292f23SGarrett Wollman 
1944df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
194557f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
1946df8bae1dSRodney W. Grimes 		break;
1947df8bae1dSRodney W. Grimes 
1948df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
194957f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
1950df8bae1dSRodney W. Grimes 		break;
1951df8bae1dSRodney W. Grimes 	}
1952abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
1953df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
1954abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
19557c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
19567c72af87SMohan Srinivasan 			int timeout;
19577c72af87SMohan Srinivasan 
19587c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
19599077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
1960b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
1961b6239c4aSAndras Olah 		}
1962df8bae1dSRodney W. Grimes 	}
19637c72af87SMohan Srinivasan }
1964497057eeSRobert Watson 
1965497057eeSRobert Watson #ifdef DDB
1966497057eeSRobert Watson static void
1967497057eeSRobert Watson db_print_indent(int indent)
1968497057eeSRobert Watson {
1969497057eeSRobert Watson 	int i;
1970497057eeSRobert Watson 
1971497057eeSRobert Watson 	for (i = 0; i < indent; i++)
1972497057eeSRobert Watson 		db_printf(" ");
1973497057eeSRobert Watson }
1974497057eeSRobert Watson 
1975497057eeSRobert Watson static void
1976497057eeSRobert Watson db_print_tstate(int t_state)
1977497057eeSRobert Watson {
1978497057eeSRobert Watson 
1979497057eeSRobert Watson 	switch (t_state) {
1980497057eeSRobert Watson 	case TCPS_CLOSED:
1981497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
1982497057eeSRobert Watson 		return;
1983497057eeSRobert Watson 
1984497057eeSRobert Watson 	case TCPS_LISTEN:
1985497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
1986497057eeSRobert Watson 		return;
1987497057eeSRobert Watson 
1988497057eeSRobert Watson 	case TCPS_SYN_SENT:
1989497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
1990497057eeSRobert Watson 		return;
1991497057eeSRobert Watson 
1992497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
1993497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
1994497057eeSRobert Watson 		return;
1995497057eeSRobert Watson 
1996497057eeSRobert Watson 	case TCPS_ESTABLISHED:
1997497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
1998497057eeSRobert Watson 		return;
1999497057eeSRobert Watson 
2000497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2001497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2002497057eeSRobert Watson 		return;
2003497057eeSRobert Watson 
2004497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2005497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2006497057eeSRobert Watson 		return;
2007497057eeSRobert Watson 
2008497057eeSRobert Watson 	case TCPS_CLOSING:
2009497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2010497057eeSRobert Watson 		return;
2011497057eeSRobert Watson 
2012497057eeSRobert Watson 	case TCPS_LAST_ACK:
2013497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2014497057eeSRobert Watson 		return;
2015497057eeSRobert Watson 
2016497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2017497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2018497057eeSRobert Watson 		return;
2019497057eeSRobert Watson 
2020497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2021497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2022497057eeSRobert Watson 		return;
2023497057eeSRobert Watson 
2024497057eeSRobert Watson 	default:
2025497057eeSRobert Watson 		db_printf("unknown");
2026497057eeSRobert Watson 		return;
2027497057eeSRobert Watson 	}
2028497057eeSRobert Watson }
2029497057eeSRobert Watson 
2030497057eeSRobert Watson static void
2031497057eeSRobert Watson db_print_tflags(u_int t_flags)
2032497057eeSRobert Watson {
2033497057eeSRobert Watson 	int comma;
2034497057eeSRobert Watson 
2035497057eeSRobert Watson 	comma = 0;
2036497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2037497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2038497057eeSRobert Watson 		comma = 1;
2039497057eeSRobert Watson 	}
2040497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2041497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2042497057eeSRobert Watson 		comma = 1;
2043497057eeSRobert Watson 	}
2044497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2045497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2046497057eeSRobert Watson 		comma = 1;
2047497057eeSRobert Watson 	}
2048497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2049497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2050497057eeSRobert Watson 		comma = 1;
2051497057eeSRobert Watson 	}
2052497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2053497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2054497057eeSRobert Watson 		comma = 1;
2055497057eeSRobert Watson 	}
2056497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2057497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2058497057eeSRobert Watson 		comma = 1;
2059497057eeSRobert Watson 	}
2060497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2061497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2062497057eeSRobert Watson 		comma = 1;
2063497057eeSRobert Watson 	}
2064497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2065497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2066497057eeSRobert Watson 		comma = 1;
2067497057eeSRobert Watson 	}
2068497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2069497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2070497057eeSRobert Watson 		comma = 1;
2071497057eeSRobert Watson 	}
2072497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2073497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2074497057eeSRobert Watson 		comma = 1;
2075497057eeSRobert Watson 	}
2076497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2077497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2078497057eeSRobert Watson 		comma = 1;
2079497057eeSRobert Watson 	}
2080497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2081497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2082497057eeSRobert Watson 		comma = 1;
2083497057eeSRobert Watson 	}
2084497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2085497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2086497057eeSRobert Watson 		comma = 1;
2087497057eeSRobert Watson 	}
2088497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2089497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2090497057eeSRobert Watson 		comma = 1;
2091497057eeSRobert Watson 	}
2092497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
2093497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2094497057eeSRobert Watson 		comma = 1;
2095497057eeSRobert Watson 	}
2096497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2097497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2098497057eeSRobert Watson 		comma = 1;
2099497057eeSRobert Watson 	}
2100497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2101497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2102497057eeSRobert Watson 		comma = 1;
2103497057eeSRobert Watson 	}
2104497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2105497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2106497057eeSRobert Watson 		comma = 1;
2107497057eeSRobert Watson 	}
2108dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
2109dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2110dbc42409SLawrence Stewart 		comma = 1;
2111dbc42409SLawrence Stewart 	}
2112497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2113497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2114497057eeSRobert Watson 		comma = 1;
2115497057eeSRobert Watson 	}
2116497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2117497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2118497057eeSRobert Watson 		comma = 1;
2119497057eeSRobert Watson 	}
2120497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2121497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2122497057eeSRobert Watson 		comma = 1;
2123497057eeSRobert Watson 	}
2124497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2125497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2126497057eeSRobert Watson 		comma = 1;
2127497057eeSRobert Watson 	}
2128f2512ba1SRui Paulo 	if (t_flags & TF_ECN_PERMIT) {
2129f2512ba1SRui Paulo 		db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2130f2512ba1SRui Paulo 		comma = 1;
2131f2512ba1SRui Paulo 	}
2132281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2133281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2134281a0fd4SPatrick Kelsey 		comma = 1;
2135281a0fd4SPatrick Kelsey 	}
2136497057eeSRobert Watson }
2137497057eeSRobert Watson 
2138497057eeSRobert Watson static void
2139497057eeSRobert Watson db_print_toobflags(char t_oobflags)
2140497057eeSRobert Watson {
2141497057eeSRobert Watson 	int comma;
2142497057eeSRobert Watson 
2143497057eeSRobert Watson 	comma = 0;
2144497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
2145497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2146497057eeSRobert Watson 		comma = 1;
2147497057eeSRobert Watson 	}
2148497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
2149497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2150497057eeSRobert Watson 		comma = 1;
2151497057eeSRobert Watson 	}
2152497057eeSRobert Watson }
2153497057eeSRobert Watson 
2154497057eeSRobert Watson static void
2155497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2156497057eeSRobert Watson {
2157497057eeSRobert Watson 
2158497057eeSRobert Watson 	db_print_indent(indent);
2159497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
2160497057eeSRobert Watson 
2161497057eeSRobert Watson 	indent += 2;
2162497057eeSRobert Watson 
2163497057eeSRobert Watson 	db_print_indent(indent);
2164497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
21654741bfcbSPatrick Kelsey 	   LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2166497057eeSRobert Watson 
2167497057eeSRobert Watson 	db_print_indent(indent);
216885d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
2169e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2170497057eeSRobert Watson 
2171497057eeSRobert Watson 	db_print_indent(indent);
2172e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2173e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
2174497057eeSRobert Watson 
2175497057eeSRobert Watson 	db_print_indent(indent);
2176497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
2177497057eeSRobert Watson 	db_print_tstate(tp->t_state);
2178497057eeSRobert Watson 	db_printf(")\n");
2179497057eeSRobert Watson 
2180497057eeSRobert Watson 	db_print_indent(indent);
2181497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
2182497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
2183497057eeSRobert Watson 	db_printf(")\n");
2184497057eeSRobert Watson 
2185497057eeSRobert Watson 	db_print_indent(indent);
2186497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
2187497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
2188497057eeSRobert Watson 
2189497057eeSRobert Watson 	db_print_indent(indent);
2190497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
2191497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2192497057eeSRobert Watson 
2193497057eeSRobert Watson 	db_print_indent(indent);
2194497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
2195497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
2196497057eeSRobert Watson 
2197497057eeSRobert Watson 	db_print_indent(indent);
2198497057eeSRobert Watson 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %lu   rcv_up: 0x%08x\n",
2199497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2200497057eeSRobert Watson 
2201497057eeSRobert Watson 	db_print_indent(indent);
22021c18314dSAndre Oppermann 	db_printf("snd_wnd: %lu   snd_cwnd: %lu\n",
22031c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
2204497057eeSRobert Watson 
2205497057eeSRobert Watson 	db_print_indent(indent);
22061c18314dSAndre Oppermann 	db_printf("snd_ssthresh: %lu   snd_recover: "
22071c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2208497057eeSRobert Watson 
2209497057eeSRobert Watson 	db_print_indent(indent);
22100c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
22110c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
2212497057eeSRobert Watson 
2213497057eeSRobert Watson 	db_print_indent(indent);
22141c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
22151c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
2216497057eeSRobert Watson 
2217497057eeSRobert Watson 	db_print_indent(indent);
22181c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
22191c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2220497057eeSRobert Watson 
2221497057eeSRobert Watson 	db_print_indent(indent);
2222497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
2223497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2224497057eeSRobert Watson 	    tp->t_rttbest);
2225497057eeSRobert Watson 
2226497057eeSRobert Watson 	db_print_indent(indent);
2227497057eeSRobert Watson 	db_printf("t_rttupdated: %lu   max_sndwnd: %lu   t_softerror: %d\n",
2228497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2229497057eeSRobert Watson 
2230497057eeSRobert Watson 	db_print_indent(indent);
2231497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2232497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
2233497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
2234497057eeSRobert Watson 
2235497057eeSRobert Watson 	db_print_indent(indent);
2236497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
2237497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2238497057eeSRobert Watson 
2239497057eeSRobert Watson 	db_print_indent(indent);
22409f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
22411a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
2242497057eeSRobert Watson 
2243497057eeSRobert Watson 	db_print_indent(indent);
2244497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
2245497057eeSRobert Watson 	    "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2246497057eeSRobert Watson 
2247497057eeSRobert Watson 	db_print_indent(indent);
2248497057eeSRobert Watson 	db_printf("snd_ssthresh_prev: %lu   snd_recover_prev: 0x%08x   "
22499f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2250497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
2251497057eeSRobert Watson 
2252497057eeSRobert Watson 	db_print_indent(indent);
22533529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
22543529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2255497057eeSRobert Watson 
2256497057eeSRobert Watson 	db_print_indent(indent);
2257497057eeSRobert Watson 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
2258497057eeSRobert Watson 	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2259497057eeSRobert Watson 
2260497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
2261497057eeSRobert Watson 
2262497057eeSRobert Watson 	db_print_indent(indent);
2263497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
2264497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2265497057eeSRobert Watson }
2266497057eeSRobert Watson 
2267497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2268497057eeSRobert Watson {
2269497057eeSRobert Watson 	struct tcpcb *tp;
2270497057eeSRobert Watson 
2271497057eeSRobert Watson 	if (!have_addr) {
2272497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
2273497057eeSRobert Watson 		return;
2274497057eeSRobert Watson 	}
2275497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
2276497057eeSRobert Watson 
2277497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
2278497057eeSRobert Watson }
2279497057eeSRobert Watson #endif
2280