xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 09fe63205c597be4f762c7f3017e2854c121d6d1)
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>
50c7a82f90SGarrett Wollman #include <sys/kernel.h>
5198163b98SPoul-Henning Kamp #include <sys/sysctl.h>
52df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
53fb59c426SYoshinobu Inoue #ifdef INET6
54fb59c426SYoshinobu Inoue #include <sys/domain.h>
55fb59c426SYoshinobu Inoue #endif /* INET6 */
56df8bae1dSRodney W. Grimes #include <sys/socket.h>
57df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
58df8bae1dSRodney W. Grimes #include <sys/protosw.h>
5991421ba2SRobert Watson #include <sys/proc.h>
6091421ba2SRobert Watson #include <sys/jail.h>
61df8bae1dSRodney W. Grimes 
62497057eeSRobert Watson #ifdef DDB
63497057eeSRobert Watson #include <ddb/ddb.h>
64497057eeSRobert Watson #endif
65497057eeSRobert Watson 
66df8bae1dSRodney W. Grimes #include <net/if.h>
67df8bae1dSRodney W. Grimes #include <net/route.h>
68530c0060SRobert Watson #include <net/vnet.h>
69df8bae1dSRodney W. Grimes 
70dbc42409SLawrence Stewart #include <netinet/cc.h>
71df8bae1dSRodney W. Grimes #include <netinet/in.h>
72df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
73b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h>
74b5e8ce9fSBruce Evans #include <netinet/in_var.h>
75df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
76fb59c426SYoshinobu Inoue #ifdef INET6
77b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h>
78b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
79fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
80a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
81fb59c426SYoshinobu Inoue #endif
82df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
83df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
84df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
85df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
86df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
87610ee2f9SDavid Greenman #ifdef TCPDEBUG
88df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
89610ee2f9SDavid Greenman #endif
90*09fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
91bc65987aSKip Macy #include <netinet/tcp_offload.h>
92*09fe6320SNavdeep Parhar #endif
93df8bae1dSRodney W. Grimes 
94df8bae1dSRodney W. Grimes /*
95df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
96df8bae1dSRodney W. Grimes  */
9756dc72c3SPawel Jakub Dawidek static int	tcp_attach(struct socket *);
98b287c6c7SBjoern A. Zeeb #ifdef INET
994d77a549SAlfred Perlstein static int	tcp_connect(struct tcpcb *, struct sockaddr *,
1004d77a549SAlfred Perlstein 		    struct thread *td);
101b287c6c7SBjoern A. Zeeb #endif /* INET */
102fb59c426SYoshinobu Inoue #ifdef INET6
1034d77a549SAlfred Perlstein static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
1044d77a549SAlfred Perlstein 		    struct thread *td);
105fb59c426SYoshinobu Inoue #endif /* INET6 */
106623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
107623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
108b8af5dfaSRobert Watson static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
1092c37256eSGarrett Wollman 
1102c37256eSGarrett Wollman #ifdef TCPDEBUG
1111db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1122c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
1134cc20ab1SSeigo Tanimura #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
1144cc20ab1SSeigo Tanimura 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1152c37256eSGarrett Wollman #else
1162c37256eSGarrett Wollman #define	TCPDEBUG0
1172c37256eSGarrett Wollman #define	TCPDEBUG1()
1182c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1192c37256eSGarrett Wollman #endif
1202c37256eSGarrett Wollman 
1212c37256eSGarrett Wollman /*
1222c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1232c37256eSGarrett Wollman  * and an internet control block.
1242c37256eSGarrett Wollman  */
1252c37256eSGarrett Wollman static int
126b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1272c37256eSGarrett Wollman {
128f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
129623dce13SRobert Watson 	struct tcpcb *tp = NULL;
130623dce13SRobert Watson 	int error;
1312c37256eSGarrett Wollman 	TCPDEBUG0;
1322c37256eSGarrett Wollman 
133623dce13SRobert Watson 	inp = sotoinpcb(so);
134623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1352c37256eSGarrett Wollman 	TCPDEBUG1();
1362c37256eSGarrett Wollman 
13756dc72c3SPawel Jakub Dawidek 	error = tcp_attach(so);
1382c37256eSGarrett Wollman 	if (error)
1392c37256eSGarrett Wollman 		goto out;
1402c37256eSGarrett Wollman 
1412c37256eSGarrett Wollman 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1423879597fSAndrey A. Chernov 		so->so_linger = TCP_LINGERTIME;
143f76fcf6dSJeffrey Hsu 
144f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
145f76fcf6dSJeffrey Hsu 	tp = intotcpcb(inp);
1462c37256eSGarrett Wollman out:
1472c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
1482c37256eSGarrett Wollman 	return error;
1492c37256eSGarrett Wollman }
1502c37256eSGarrett Wollman 
1512c37256eSGarrett Wollman /*
152a152f8a3SRobert Watson  * tcp_detach is called when the socket layer loses its final reference
153a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
154a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
155a152f8a3SRobert Watson  * inpcb state: time wait.
156c78cbc7bSRobert Watson  *
157a152f8a3SRobert Watson  * This function can probably be re-absorbed back into tcp_usr_detach() now
158a152f8a3SRobert Watson  * that there is a single detach path.
1592c37256eSGarrett Wollman  */
160bc725eafSRobert Watson static void
161c78cbc7bSRobert Watson tcp_detach(struct socket *so, struct inpcb *inp)
1622c37256eSGarrett Wollman {
1632c37256eSGarrett Wollman 	struct tcpcb *tp;
1642c37256eSGarrett Wollman 
165603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1668501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
167623dce13SRobert Watson 
168c78cbc7bSRobert Watson 	KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
169c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
170953b5606SRobert Watson 
171a152f8a3SRobert Watson 	tp = intotcpcb(inp);
172a152f8a3SRobert Watson 
173ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
174623dce13SRobert Watson 		/*
175a152f8a3SRobert Watson 		 * There are two cases to handle: one in which the time wait
176a152f8a3SRobert Watson 		 * state is being discarded (INP_DROPPED), and one in which
177a152f8a3SRobert Watson 		 * this connection will remain in timewait.  In the former,
178a152f8a3SRobert Watson 		 * it is time to discard all state (except tcptw, which has
179a152f8a3SRobert Watson 		 * already been discarded by the timewait close code, which
180a152f8a3SRobert Watson 		 * should be further up the call stack somewhere).  In the
181a152f8a3SRobert Watson 		 * latter case, we detach from the socket, but leave the pcb
182a152f8a3SRobert Watson 		 * present until timewait ends.
183623dce13SRobert Watson 		 *
184a152f8a3SRobert Watson 		 * XXXRW: Would it be cleaner to free the tcptw here?
185623dce13SRobert Watson 		 */
186ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED) {
187a152f8a3SRobert Watson 			KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
188a152f8a3SRobert Watson 			    "INP_DROPPED && tp != NULL"));
189623dce13SRobert Watson 			in_pcbdetach(inp);
1900206cdb8SBjoern A. Zeeb 			in_pcbfree(inp);
1910206cdb8SBjoern A. Zeeb 		} else {
192623dce13SRobert Watson 			in_pcbdetach(inp);
1938501a69cSRobert Watson 			INP_WUNLOCK(inp);
194623dce13SRobert Watson 		}
195623dce13SRobert Watson 	} else {
196e6e65783SRobert Watson 		/*
197a152f8a3SRobert Watson 		 * If the connection is not in timewait, we consider two
198a152f8a3SRobert Watson 		 * two conditions: one in which no further processing is
199a152f8a3SRobert Watson 		 * necessary (dropped || embryonic), and one in which TCP is
200a152f8a3SRobert Watson 		 * not yet done, but no longer requires the socket, so the
201a152f8a3SRobert Watson 		 * pcb will persist for the time being.
202a152f8a3SRobert Watson 		 *
203a152f8a3SRobert Watson 		 * XXXRW: Does the second case still occur?
204e6e65783SRobert Watson 		 */
205ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED ||
206623dce13SRobert Watson 		    tp->t_state < TCPS_SYN_SENT) {
207623dce13SRobert Watson 			tcp_discardcb(tp);
208623dce13SRobert Watson 			in_pcbdetach(inp);
2090206cdb8SBjoern A. Zeeb 			in_pcbfree(inp);
210db3cee51SNavdeep Parhar 		} else {
211a152f8a3SRobert Watson 			in_pcbdetach(inp);
212db3cee51SNavdeep Parhar 			INP_WUNLOCK(inp);
213db3cee51SNavdeep Parhar 		}
214623dce13SRobert Watson 	}
215623dce13SRobert Watson }
216c78cbc7bSRobert Watson 
217c78cbc7bSRobert Watson /*
218c78cbc7bSRobert Watson  * pru_detach() detaches the TCP protocol from the socket.
219c78cbc7bSRobert Watson  * If the protocol state is non-embryonic, then can't
220c78cbc7bSRobert Watson  * do this directly: have to initiate a pru_disconnect(),
221c78cbc7bSRobert Watson  * which may finish later; embryonic TCB's can just
222c78cbc7bSRobert Watson  * be discarded here.
223c78cbc7bSRobert Watson  */
224c78cbc7bSRobert Watson static void
225c78cbc7bSRobert Watson tcp_usr_detach(struct socket *so)
226c78cbc7bSRobert Watson {
227c78cbc7bSRobert Watson 	struct inpcb *inp;
228c78cbc7bSRobert Watson 
229c78cbc7bSRobert Watson 	inp = sotoinpcb(so);
230c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
231603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
2328501a69cSRobert Watson 	INP_WLOCK(inp);
233c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
234c78cbc7bSRobert Watson 	    ("tcp_usr_detach: inp_socket == NULL"));
235c78cbc7bSRobert Watson 	tcp_detach(so, inp);
236603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
2372c37256eSGarrett Wollman }
2382c37256eSGarrett Wollman 
239b287c6c7SBjoern A. Zeeb #ifdef INET
2402c37256eSGarrett Wollman /*
2412c37256eSGarrett Wollman  * Give the socket an address.
2422c37256eSGarrett Wollman  */
2432c37256eSGarrett Wollman static int
244b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2452c37256eSGarrett Wollman {
2462c37256eSGarrett Wollman 	int error = 0;
247f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
248623dce13SRobert Watson 	struct tcpcb *tp = NULL;
2492c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
2502c37256eSGarrett Wollman 
25152710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
25252710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sinp))
25352710de1SPawel Jakub Dawidek 		return (EINVAL);
2542c37256eSGarrett Wollman 	/*
2552c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2562c37256eSGarrett Wollman 	 * to them.
2572c37256eSGarrett Wollman 	 */
2582c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
25952710de1SPawel Jakub Dawidek 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
26052710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
26152710de1SPawel Jakub Dawidek 
262623dce13SRobert Watson 	TCPDEBUG0;
263623dce13SRobert Watson 	inp = sotoinpcb(so);
264623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
2658501a69cSRobert Watson 	INP_WLOCK(inp);
266ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
267623dce13SRobert Watson 		error = EINVAL;
2682c37256eSGarrett Wollman 		goto out;
269623dce13SRobert Watson 	}
270623dce13SRobert Watson 	tp = intotcpcb(inp);
271623dce13SRobert Watson 	TCPDEBUG1();
272fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
273623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
274fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
275623dce13SRobert Watson out:
276623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
2778501a69cSRobert Watson 	INP_WUNLOCK(inp);
278623dce13SRobert Watson 
279623dce13SRobert Watson 	return (error);
2802c37256eSGarrett Wollman }
281b287c6c7SBjoern A. Zeeb #endif /* INET */
2822c37256eSGarrett Wollman 
283fb59c426SYoshinobu Inoue #ifdef INET6
284fb59c426SYoshinobu Inoue static int
285b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
286fb59c426SYoshinobu Inoue {
287fb59c426SYoshinobu Inoue 	int error = 0;
288f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
289623dce13SRobert Watson 	struct tcpcb *tp = NULL;
290fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
291fb59c426SYoshinobu Inoue 
29252710de1SPawel Jakub Dawidek 	sin6p = (struct sockaddr_in6 *)nam;
29352710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sin6p))
29452710de1SPawel Jakub Dawidek 		return (EINVAL);
295fb59c426SYoshinobu Inoue 	/*
296fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
297fb59c426SYoshinobu Inoue 	 * to them.
298fb59c426SYoshinobu Inoue 	 */
299fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6 &&
30052710de1SPawel Jakub Dawidek 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
30152710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
30252710de1SPawel Jakub Dawidek 
303623dce13SRobert Watson 	TCPDEBUG0;
304623dce13SRobert Watson 	inp = sotoinpcb(so);
305623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
3068501a69cSRobert Watson 	INP_WLOCK(inp);
307ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
308623dce13SRobert Watson 		error = EINVAL;
309623dce13SRobert Watson 		goto out;
310623dce13SRobert Watson 	}
311623dce13SRobert Watson 	tp = intotcpcb(inp);
312623dce13SRobert Watson 	TCPDEBUG1();
313fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
314fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
315fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
316b287c6c7SBjoern A. Zeeb #ifdef INET
31766ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
318fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
319fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
320fb59c426SYoshinobu Inoue 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
321fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
322fb59c426SYoshinobu Inoue 
323fb59c426SYoshinobu Inoue 			in6_sin6_2_sin(&sin, sin6p);
324fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
325fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
326b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
327b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
328fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
329fb59c426SYoshinobu Inoue 			goto out;
330fb59c426SYoshinobu Inoue 		}
331fb59c426SYoshinobu Inoue 	}
332b287c6c7SBjoern A. Zeeb #endif
333b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
334fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
335623dce13SRobert Watson out:
336623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3378501a69cSRobert Watson 	INP_WUNLOCK(inp);
338623dce13SRobert Watson 	return (error);
339fb59c426SYoshinobu Inoue }
340fb59c426SYoshinobu Inoue #endif /* INET6 */
341fb59c426SYoshinobu Inoue 
342b287c6c7SBjoern A. Zeeb #ifdef INET
3432c37256eSGarrett Wollman /*
3442c37256eSGarrett Wollman  * Prepare to accept connections.
3452c37256eSGarrett Wollman  */
3462c37256eSGarrett Wollman static int
347d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
3482c37256eSGarrett Wollman {
3492c37256eSGarrett Wollman 	int error = 0;
350f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
351623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3522c37256eSGarrett Wollman 
353623dce13SRobert Watson 	TCPDEBUG0;
354623dce13SRobert Watson 	inp = sotoinpcb(so);
355623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
3568501a69cSRobert Watson 	INP_WLOCK(inp);
357ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
358623dce13SRobert Watson 		error = EINVAL;
359623dce13SRobert Watson 		goto out;
360623dce13SRobert Watson 	}
361623dce13SRobert Watson 	tp = intotcpcb(inp);
362623dce13SRobert Watson 	TCPDEBUG1();
3630daccb9cSRobert Watson 	SOCK_LOCK(so);
3640daccb9cSRobert Watson 	error = solisten_proto_check(so);
365fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
3660daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0)
367b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
368fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
3690daccb9cSRobert Watson 	if (error == 0) {
3702c37256eSGarrett Wollman 		tp->t_state = TCPS_LISTEN;
371d374e81eSRobert Watson 		solisten_proto(so, backlog);
372*09fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
373*09fe6320SNavdeep Parhar 		tcp_offload_listen_start(tp);
374*09fe6320SNavdeep Parhar #endif
3750daccb9cSRobert Watson 	}
3760daccb9cSRobert Watson 	SOCK_UNLOCK(so);
377623dce13SRobert Watson 
378623dce13SRobert Watson out:
379623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
3808501a69cSRobert Watson 	INP_WUNLOCK(inp);
381623dce13SRobert Watson 	return (error);
3822c37256eSGarrett Wollman }
383b287c6c7SBjoern A. Zeeb #endif /* INET */
3842c37256eSGarrett Wollman 
385fb59c426SYoshinobu Inoue #ifdef INET6
386fb59c426SYoshinobu Inoue static int
387d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
388fb59c426SYoshinobu Inoue {
389fb59c426SYoshinobu Inoue 	int error = 0;
390f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
391623dce13SRobert Watson 	struct tcpcb *tp = NULL;
392fb59c426SYoshinobu Inoue 
393623dce13SRobert Watson 	TCPDEBUG0;
394623dce13SRobert Watson 	inp = sotoinpcb(so);
395623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
3968501a69cSRobert Watson 	INP_WLOCK(inp);
397ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
398623dce13SRobert Watson 		error = EINVAL;
399623dce13SRobert Watson 		goto out;
400623dce13SRobert Watson 	}
401623dce13SRobert Watson 	tp = intotcpcb(inp);
402623dce13SRobert Watson 	TCPDEBUG1();
4030daccb9cSRobert Watson 	SOCK_LOCK(so);
4040daccb9cSRobert Watson 	error = solisten_proto_check(so);
405fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
4060daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0) {
407fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
40866ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
409fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
410b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
411fb59c426SYoshinobu Inoue 	}
412fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4130daccb9cSRobert Watson 	if (error == 0) {
414fb59c426SYoshinobu Inoue 		tp->t_state = TCPS_LISTEN;
415d374e81eSRobert Watson 		solisten_proto(so, backlog);
416*09fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
417*09fe6320SNavdeep Parhar 		tcp_offload_listen_start(tp);
418*09fe6320SNavdeep Parhar #endif
4190daccb9cSRobert Watson 	}
4200daccb9cSRobert Watson 	SOCK_UNLOCK(so);
421623dce13SRobert Watson 
422623dce13SRobert Watson out:
423623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4248501a69cSRobert Watson 	INP_WUNLOCK(inp);
425623dce13SRobert Watson 	return (error);
426fb59c426SYoshinobu Inoue }
427fb59c426SYoshinobu Inoue #endif /* INET6 */
428fb59c426SYoshinobu Inoue 
429b287c6c7SBjoern A. Zeeb #ifdef INET
4302c37256eSGarrett Wollman /*
4312c37256eSGarrett Wollman  * Initiate connection to peer.
4322c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
4332c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
4342c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
4352c37256eSGarrett Wollman  * Send initial segment on connection.
4362c37256eSGarrett Wollman  */
4372c37256eSGarrett Wollman static int
438b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
4392c37256eSGarrett Wollman {
4402c37256eSGarrett Wollman 	int error = 0;
441f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
442623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4432c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
4442c37256eSGarrett Wollman 
44557bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
446e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
447e29ef13fSDon Lewis 		return (EINVAL);
44852710de1SPawel Jakub Dawidek 	/*
44952710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
45052710de1SPawel Jakub Dawidek 	 */
4512c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
45252710de1SPawel Jakub Dawidek 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
45352710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
454b89e82ddSJamie Gritton 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
455b89e82ddSJamie Gritton 		return (error);
45675c13541SPoul-Henning Kamp 
457623dce13SRobert Watson 	TCPDEBUG0;
458623dce13SRobert Watson 	inp = sotoinpcb(so);
459623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
4608501a69cSRobert Watson 	INP_WLOCK(inp);
461ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
462623dce13SRobert Watson 		error = EINVAL;
463623dce13SRobert Watson 		goto out;
464623dce13SRobert Watson 	}
465623dce13SRobert Watson 	tp = intotcpcb(inp);
466623dce13SRobert Watson 	TCPDEBUG1();
467b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
4682c37256eSGarrett Wollman 		goto out;
469*09fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
470*09fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
471*09fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
472*09fe6320SNavdeep Parhar 		goto out;
473*09fe6320SNavdeep Parhar #endif
474*09fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
475*09fe6320SNavdeep Parhar 	error = tcp_output(tp);
476623dce13SRobert Watson out:
477623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
4788501a69cSRobert Watson 	INP_WUNLOCK(inp);
479623dce13SRobert Watson 	return (error);
4802c37256eSGarrett Wollman }
481b287c6c7SBjoern A. Zeeb #endif /* INET */
4822c37256eSGarrett Wollman 
483fb59c426SYoshinobu Inoue #ifdef INET6
484fb59c426SYoshinobu Inoue static int
485b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
486fb59c426SYoshinobu Inoue {
487fb59c426SYoshinobu Inoue 	int error = 0;
488f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
489623dce13SRobert Watson 	struct tcpcb *tp = NULL;
490fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
491623dce13SRobert Watson 
492623dce13SRobert Watson 	TCPDEBUG0;
493fb59c426SYoshinobu Inoue 
494fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
495e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sin6p))
496e29ef13fSDon Lewis 		return (EINVAL);
49752710de1SPawel Jakub Dawidek 	/*
49852710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
49952710de1SPawel Jakub Dawidek 	 */
500fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6
50152710de1SPawel Jakub Dawidek 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
50252710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
503fb59c426SYoshinobu Inoue 
504623dce13SRobert Watson 	inp = sotoinpcb(so);
505623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
5068501a69cSRobert Watson 	INP_WLOCK(inp);
507ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
508623dce13SRobert Watson 		error = EINVAL;
509623dce13SRobert Watson 		goto out;
510623dce13SRobert Watson 	}
511623dce13SRobert Watson 	tp = intotcpcb(inp);
512623dce13SRobert Watson 	TCPDEBUG1();
513b287c6c7SBjoern A. Zeeb #ifdef INET
514fa046d87SRobert Watson 	/*
515fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
516fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
517fa046d87SRobert Watson 	 * Is this a significant problem?
518fa046d87SRobert Watson 	 */
51933841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
520fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
521fb59c426SYoshinobu Inoue 
522d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
523d46a5312SMaxim Konovalov 			error = EINVAL;
524d46a5312SMaxim Konovalov 			goto out;
525d46a5312SMaxim Konovalov 		}
52633841545SHajimu UMEMOTO 
527fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
528fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
529fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
530b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
531b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
532413628a7SBjoern A. Zeeb 			goto out;
533b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
534fb59c426SYoshinobu Inoue 			goto out;
535*09fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
536*09fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
537*09fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
538*09fe6320SNavdeep Parhar 			goto out;
539*09fe6320SNavdeep Parhar #endif
540*09fe6320SNavdeep Parhar 		error = tcp_output(tp);
541fb59c426SYoshinobu Inoue 		goto out;
542fb59c426SYoshinobu Inoue 	}
543b287c6c7SBjoern A. Zeeb #endif
544fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
545fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
546dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
547b89e82ddSJamie Gritton 	if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
548413628a7SBjoern A. Zeeb 		goto out;
549b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
550fb59c426SYoshinobu Inoue 		goto out;
551*09fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
552*09fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
553*09fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
554*09fe6320SNavdeep Parhar 		goto out;
555*09fe6320SNavdeep Parhar #endif
556*09fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
557*09fe6320SNavdeep Parhar 	error = tcp_output(tp);
558623dce13SRobert Watson 
559623dce13SRobert Watson out:
560623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
5618501a69cSRobert Watson 	INP_WUNLOCK(inp);
562623dce13SRobert Watson 	return (error);
563fb59c426SYoshinobu Inoue }
564fb59c426SYoshinobu Inoue #endif /* INET6 */
565fb59c426SYoshinobu Inoue 
5662c37256eSGarrett Wollman /*
5672c37256eSGarrett Wollman  * Initiate disconnect from peer.
5682c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
5692c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
5702c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
5712c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
5722c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
5732c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
5742c37256eSGarrett Wollman  *
5752c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
5762c37256eSGarrett Wollman  */
5772c37256eSGarrett Wollman static int
5782c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
5792c37256eSGarrett Wollman {
580f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
581623dce13SRobert Watson 	struct tcpcb *tp = NULL;
582623dce13SRobert Watson 	int error = 0;
5832c37256eSGarrett Wollman 
584623dce13SRobert Watson 	TCPDEBUG0;
585603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
586623dce13SRobert Watson 	inp = sotoinpcb(so);
587623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
5888501a69cSRobert Watson 	INP_WLOCK(inp);
589ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
59021367f63SSam Leffler 		error = ECONNRESET;
591623dce13SRobert Watson 		goto out;
592623dce13SRobert Watson 	}
593623dce13SRobert Watson 	tp = intotcpcb(inp);
594623dce13SRobert Watson 	TCPDEBUG1();
595623dce13SRobert Watson 	tcp_disconnect(tp);
596623dce13SRobert Watson out:
597623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
5988501a69cSRobert Watson 	INP_WUNLOCK(inp);
599603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
600623dce13SRobert Watson 	return (error);
6012c37256eSGarrett Wollman }
6022c37256eSGarrett Wollman 
603b287c6c7SBjoern A. Zeeb #ifdef INET
6042c37256eSGarrett Wollman /*
6058296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
6068296cddfSRobert Watson  * just return the address of the peer, storing through addr.
6078296cddfSRobert Watson  *
6088296cddfSRobert Watson  * The rationale for acquiring the tcbinfo lock here is somewhat complicated,
6098296cddfSRobert Watson  * and is described in detail in the commit log entry for r175612.  Acquiring
6108296cddfSRobert Watson  * it delays an accept(2) racing with sonewconn(), which inserts the socket
6118296cddfSRobert Watson  * before the inpcb address/port fields are initialized.  A better fix would
6128296cddfSRobert Watson  * prevent the socket from being placed in the listen queue until all fields
6138296cddfSRobert Watson  * are fully initialized.
6142c37256eSGarrett Wollman  */
6152c37256eSGarrett Wollman static int
61657bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
6172c37256eSGarrett Wollman {
6182c37256eSGarrett Wollman 	int error = 0;
619f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
6201db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
62126ef6ac4SDon Lewis 	struct in_addr addr;
62226ef6ac4SDon Lewis 	in_port_t port = 0;
6231db24ffbSJonathan Lemon 	TCPDEBUG0;
6242c37256eSGarrett Wollman 
6253d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
6263d2d3ef4SRobert Watson 		return (ECONNABORTED);
627f76fcf6dSJeffrey Hsu 
628f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
629623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
630603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_tcbinfo);
6318501a69cSRobert Watson 	INP_WLOCK(inp);
632ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
6333d2d3ef4SRobert Watson 		error = ECONNABORTED;
634623dce13SRobert Watson 		goto out;
635623dce13SRobert Watson 	}
6361db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
6371db24ffbSJonathan Lemon 	TCPDEBUG1();
638f76fcf6dSJeffrey Hsu 
639f76fcf6dSJeffrey Hsu 	/*
64054d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
64126ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
64226ef6ac4SDon Lewis 	 * release the lock.
643f76fcf6dSJeffrey Hsu 	 */
64426ef6ac4SDon Lewis 	port = inp->inp_fport;
64526ef6ac4SDon Lewis 	addr = inp->inp_faddr;
646f76fcf6dSJeffrey Hsu 
647623dce13SRobert Watson out:
648623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
6498501a69cSRobert Watson 	INP_WUNLOCK(inp);
650603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_tcbinfo);
65126ef6ac4SDon Lewis 	if (error == 0)
65226ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
65326ef6ac4SDon Lewis 	return error;
6542c37256eSGarrett Wollman }
655b287c6c7SBjoern A. Zeeb #endif /* INET */
6562c37256eSGarrett Wollman 
657fb59c426SYoshinobu Inoue #ifdef INET6
658fb59c426SYoshinobu Inoue static int
659fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
660fb59c426SYoshinobu Inoue {
661f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
662fb59c426SYoshinobu Inoue 	int error = 0;
6631db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
66426ef6ac4SDon Lewis 	struct in_addr addr;
66526ef6ac4SDon Lewis 	struct in6_addr addr6;
66626ef6ac4SDon Lewis 	in_port_t port = 0;
66726ef6ac4SDon Lewis 	int v4 = 0;
6681db24ffbSJonathan Lemon 	TCPDEBUG0;
669fb59c426SYoshinobu Inoue 
670b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
671b4470c16SRobert Watson 		return (ECONNABORTED);
672f76fcf6dSJeffrey Hsu 
673f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
674623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
675fa046d87SRobert Watson 	INP_INFO_RLOCK(&V_tcbinfo);
6768501a69cSRobert Watson 	INP_WLOCK(inp);
677ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
67821367f63SSam Leffler 		error = ECONNABORTED;
679623dce13SRobert Watson 		goto out;
680623dce13SRobert Watson 	}
6811db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
6821db24ffbSJonathan Lemon 	TCPDEBUG1();
683623dce13SRobert Watson 
68426ef6ac4SDon Lewis 	/*
68526ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
68626ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
68726ef6ac4SDon Lewis 	 * release the lock.
68826ef6ac4SDon Lewis 	 */
68926ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
69026ef6ac4SDon Lewis 		v4 = 1;
69126ef6ac4SDon Lewis 		port = inp->inp_fport;
69226ef6ac4SDon Lewis 		addr = inp->inp_faddr;
69326ef6ac4SDon Lewis 	} else {
69426ef6ac4SDon Lewis 		port = inp->inp_fport;
69526ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
69626ef6ac4SDon Lewis 	}
69726ef6ac4SDon Lewis 
698623dce13SRobert Watson out:
699623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
7008501a69cSRobert Watson 	INP_WUNLOCK(inp);
701fa046d87SRobert Watson 	INP_INFO_RUNLOCK(&V_tcbinfo);
70226ef6ac4SDon Lewis 	if (error == 0) {
70326ef6ac4SDon Lewis 		if (v4)
70426ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
70526ef6ac4SDon Lewis 		else
70626ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
70726ef6ac4SDon Lewis 	}
70826ef6ac4SDon Lewis 	return error;
709fb59c426SYoshinobu Inoue }
710fb59c426SYoshinobu Inoue #endif /* INET6 */
711f76fcf6dSJeffrey Hsu 
712f76fcf6dSJeffrey Hsu /*
7132c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
7142c37256eSGarrett Wollman  */
7152c37256eSGarrett Wollman static int
7162c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
7172c37256eSGarrett Wollman {
7182c37256eSGarrett Wollman 	int error = 0;
719f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
720623dce13SRobert Watson 	struct tcpcb *tp = NULL;
7212c37256eSGarrett Wollman 
722623dce13SRobert Watson 	TCPDEBUG0;
723603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
724623dce13SRobert Watson 	inp = sotoinpcb(so);
725623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
7268501a69cSRobert Watson 	INP_WLOCK(inp);
727ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
72821367f63SSam Leffler 		error = ECONNRESET;
729623dce13SRobert Watson 		goto out;
730623dce13SRobert Watson 	}
731623dce13SRobert Watson 	tp = intotcpcb(inp);
732623dce13SRobert Watson 	TCPDEBUG1();
7332c37256eSGarrett Wollman 	socantsendmore(so);
734623dce13SRobert Watson 	tcp_usrclosed(tp);
735ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
736*09fe6320SNavdeep Parhar 		error = tcp_output(tp);
737623dce13SRobert Watson 
738623dce13SRobert Watson out:
739623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
7408501a69cSRobert Watson 	INP_WUNLOCK(inp);
741603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
742623dce13SRobert Watson 
743623dce13SRobert Watson 	return (error);
7442c37256eSGarrett Wollman }
7452c37256eSGarrett Wollman 
7462c37256eSGarrett Wollman /*
7472c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
7482c37256eSGarrett Wollman  */
7492c37256eSGarrett Wollman static int
7502c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
7512c37256eSGarrett Wollman {
752f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
753623dce13SRobert Watson 	struct tcpcb *tp = NULL;
754623dce13SRobert Watson 	int error = 0;
7552c37256eSGarrett Wollman 
756623dce13SRobert Watson 	TCPDEBUG0;
757623dce13SRobert Watson 	inp = sotoinpcb(so);
758623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
7598501a69cSRobert Watson 	INP_WLOCK(inp);
760ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
76121367f63SSam Leffler 		error = ECONNRESET;
762623dce13SRobert Watson 		goto out;
763623dce13SRobert Watson 	}
764623dce13SRobert Watson 	tp = intotcpcb(inp);
765623dce13SRobert Watson 	TCPDEBUG1();
766*09fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
767*09fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
768*09fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
769*09fe6320SNavdeep Parhar #endif
770*09fe6320SNavdeep Parhar 	tcp_output(tp);
771623dce13SRobert Watson 
772623dce13SRobert Watson out:
773623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
7748501a69cSRobert Watson 	INP_WUNLOCK(inp);
775623dce13SRobert Watson 	return (error);
7762c37256eSGarrett Wollman }
7772c37256eSGarrett Wollman 
7782c37256eSGarrett Wollman /*
7792c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
7809c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
7819c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
7829c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
7839c9906e9SPeter Wemm  * generally are caller-frees.
7842c37256eSGarrett Wollman  */
7852c37256eSGarrett Wollman static int
78657bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
787b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
7882c37256eSGarrett Wollman {
7892c37256eSGarrett Wollman 	int error = 0;
790f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
791623dce13SRobert Watson 	struct tcpcb *tp = NULL;
792fb59c426SYoshinobu Inoue #ifdef INET6
793fb59c426SYoshinobu Inoue 	int isipv6;
794fb59c426SYoshinobu Inoue #endif
7959c9906e9SPeter Wemm 	TCPDEBUG0;
7962c37256eSGarrett Wollman 
797f76fcf6dSJeffrey Hsu 	/*
798fa046d87SRobert Watson 	 * We require the pcbinfo lock if we will close the socket as part of
799fa046d87SRobert Watson 	 * this call.
800f76fcf6dSJeffrey Hsu 	 */
801fa046d87SRobert Watson 	if (flags & PRUS_EOF)
802603724d3SBjoern A. Zeeb 		INP_INFO_WLOCK(&V_tcbinfo);
803f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
804623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
8058501a69cSRobert Watson 	INP_WLOCK(inp);
806ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
8077ff0b850SAndre Oppermann 		if (control)
8087ff0b850SAndre Oppermann 			m_freem(control);
8097ff0b850SAndre Oppermann 		if (m)
8107ff0b850SAndre Oppermann 			m_freem(m);
81121367f63SSam Leffler 		error = ECONNRESET;
8129c9906e9SPeter Wemm 		goto out;
8139c9906e9SPeter Wemm 	}
814fb59c426SYoshinobu Inoue #ifdef INET6
815fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
816fb59c426SYoshinobu Inoue #endif /* INET6 */
8179c9906e9SPeter Wemm 	tp = intotcpcb(inp);
8189c9906e9SPeter Wemm 	TCPDEBUG1();
8199c9906e9SPeter Wemm 	if (control) {
8209c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
8219c9906e9SPeter Wemm 		if (control->m_len) {
8229c9906e9SPeter Wemm 			m_freem(control);
8232c37256eSGarrett Wollman 			if (m)
8242c37256eSGarrett Wollman 				m_freem(m);
825744f87eaSDavid Greenman 			error = EINVAL;
826744f87eaSDavid Greenman 			goto out;
8272c37256eSGarrett Wollman 		}
8289c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
8299c9906e9SPeter Wemm 	}
8302c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
831395bb186SSam Leffler 		sbappendstream(&so->so_snd, m);
8322c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
8332c37256eSGarrett Wollman 			/*
8342c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
8352c37256eSGarrett Wollman 			 * initialize window to default value, and
8362c37256eSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
8372c37256eSGarrett Wollman 			 * MSS.
8382c37256eSGarrett Wollman 			 */
839fb59c426SYoshinobu Inoue #ifdef INET6
840fb59c426SYoshinobu Inoue 			if (isipv6)
841b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
842fb59c426SYoshinobu Inoue #endif /* INET6 */
843b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
844b287c6c7SBjoern A. Zeeb 			else
845b287c6c7SBjoern A. Zeeb #endif
846b287c6c7SBjoern A. Zeeb #ifdef INET
847b40ce416SJulian Elischer 				error = tcp_connect(tp, nam, td);
848b287c6c7SBjoern A. Zeeb #endif
8492c37256eSGarrett Wollman 			if (error)
8502c37256eSGarrett Wollman 				goto out;
8512c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
8522c37256eSGarrett Wollman 			tcp_mss(tp, -1);
8532c37256eSGarrett Wollman 		}
8542c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
8552c37256eSGarrett Wollman 			/*
8562c37256eSGarrett Wollman 			 * Close the send side of the connection after
8572c37256eSGarrett Wollman 			 * the data is sent.
8582c37256eSGarrett Wollman 			 */
859603724d3SBjoern A. Zeeb 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
8602c37256eSGarrett Wollman 			socantsendmore(so);
861623dce13SRobert Watson 			tcp_usrclosed(tp);
8622c37256eSGarrett Wollman 		}
863ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED)) {
864b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
865b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
866*09fe6320SNavdeep Parhar 			error = tcp_output(tp);
867b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
868b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
869b0acefa8SBill Fenner 		}
8702c37256eSGarrett Wollman 	} else {
871623dce13SRobert Watson 		/*
872623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
873623dce13SRobert Watson 		 */
874d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
8752c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
876d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
8772c37256eSGarrett Wollman 			m_freem(m);
8782c37256eSGarrett Wollman 			error = ENOBUFS;
8792c37256eSGarrett Wollman 			goto out;
8802c37256eSGarrett Wollman 		}
8812c37256eSGarrett Wollman 		/*
8822c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
8832c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
8842c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
8852c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
8862c37256eSGarrett Wollman 		 * of data past the urgent section.
8872c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
8882c37256eSGarrett Wollman 		 */
889d2bc35abSRobert Watson 		sbappendstream_locked(&so->so_snd, m);
890d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
891ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
892ef53690bSGarrett Wollman 			/*
893ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
894ef53690bSGarrett Wollman 			 * initialize window to default value, and
895ef53690bSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
896ef53690bSGarrett Wollman 			 * MSS.
897ef53690bSGarrett Wollman 			 */
898fb59c426SYoshinobu Inoue #ifdef INET6
899fb59c426SYoshinobu Inoue 			if (isipv6)
900b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
901fb59c426SYoshinobu Inoue #endif /* INET6 */
902b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
903b287c6c7SBjoern A. Zeeb 			else
904b287c6c7SBjoern A. Zeeb #endif
905b287c6c7SBjoern A. Zeeb #ifdef INET
906b40ce416SJulian Elischer 				error = tcp_connect(tp, nam, td);
907b287c6c7SBjoern A. Zeeb #endif
908ef53690bSGarrett Wollman 			if (error)
909ef53690bSGarrett Wollman 				goto out;
910ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
911ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
912623dce13SRobert Watson 		}
9132c37256eSGarrett Wollman 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
9142cdbfa66SPaul Saab 		tp->t_flags |= TF_FORCEDATA;
915*09fe6320SNavdeep Parhar 		error = tcp_output(tp);
9162cdbfa66SPaul Saab 		tp->t_flags &= ~TF_FORCEDATA;
9172c37256eSGarrett Wollman 	}
918d1401c90SRobert Watson out:
919d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
9202c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
9218501a69cSRobert Watson 	INP_WUNLOCK(inp);
922fa046d87SRobert Watson 	if (flags & PRUS_EOF)
923603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
92473fddedaSPeter Grehan 	return (error);
9252c37256eSGarrett Wollman }
9262c37256eSGarrett Wollman 
9272c37256eSGarrett Wollman /*
928a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
9292c37256eSGarrett Wollman  */
930ac45e92fSRobert Watson static void
9312c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
9322c37256eSGarrett Wollman {
933f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
934a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
935623dce13SRobert Watson 	TCPDEBUG0;
936c78cbc7bSRobert Watson 
937ac45e92fSRobert Watson 	inp = sotoinpcb(so);
938c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
939c78cbc7bSRobert Watson 
940603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
9418501a69cSRobert Watson 	INP_WLOCK(inp);
942c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
943c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
944c78cbc7bSRobert Watson 
945c78cbc7bSRobert Watson 	/*
946a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
947c78cbc7bSRobert Watson 	 */
948ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
949ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
950c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
951a152f8a3SRobert Watson 		TCPDEBUG1();
952c78cbc7bSRobert Watson 		tcp_drop(tp, ECONNABORTED);
953a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
954c78cbc7bSRobert Watson 	}
955ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
956a152f8a3SRobert Watson 		SOCK_LOCK(so);
957a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
958a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
959ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
960a152f8a3SRobert Watson 	}
9618501a69cSRobert Watson 	INP_WUNLOCK(inp);
962603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
963a152f8a3SRobert Watson }
964a152f8a3SRobert Watson 
965a152f8a3SRobert Watson /*
966a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
967a152f8a3SRobert Watson  */
968a152f8a3SRobert Watson static void
969a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
970a152f8a3SRobert Watson {
971a152f8a3SRobert Watson 	struct inpcb *inp;
972a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
973a152f8a3SRobert Watson 	TCPDEBUG0;
974a152f8a3SRobert Watson 
975a152f8a3SRobert Watson 	inp = sotoinpcb(so);
976a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
977a152f8a3SRobert Watson 
978603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
9798501a69cSRobert Watson 	INP_WLOCK(inp);
980a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
981a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
982a152f8a3SRobert Watson 
983a152f8a3SRobert Watson 	/*
984a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
985a152f8a3SRobert Watson 	 * a disconnect.
986a152f8a3SRobert Watson 	 */
987ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
988ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
989a152f8a3SRobert Watson 		tp = intotcpcb(inp);
990a152f8a3SRobert Watson 		TCPDEBUG1();
991a152f8a3SRobert Watson 		tcp_disconnect(tp);
992a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
993a152f8a3SRobert Watson 	}
994ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
995a152f8a3SRobert Watson 		SOCK_LOCK(so);
996a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
997a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
998ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
999a152f8a3SRobert Watson 	}
10008501a69cSRobert Watson 	INP_WUNLOCK(inp);
1001603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
10022c37256eSGarrett Wollman }
10032c37256eSGarrett Wollman 
10042c37256eSGarrett Wollman /*
10052c37256eSGarrett Wollman  * Receive out-of-band data.
10062c37256eSGarrett Wollman  */
10072c37256eSGarrett Wollman static int
10082c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
10092c37256eSGarrett Wollman {
10102c37256eSGarrett Wollman 	int error = 0;
1011f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1012623dce13SRobert Watson 	struct tcpcb *tp = NULL;
10132c37256eSGarrett Wollman 
1014623dce13SRobert Watson 	TCPDEBUG0;
1015623dce13SRobert Watson 	inp = sotoinpcb(so);
1016623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
10178501a69cSRobert Watson 	INP_WLOCK(inp);
1018ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
101921367f63SSam Leffler 		error = ECONNRESET;
1020623dce13SRobert Watson 		goto out;
1021623dce13SRobert Watson 	}
1022623dce13SRobert Watson 	tp = intotcpcb(inp);
1023623dce13SRobert Watson 	TCPDEBUG1();
10242c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1025c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
10264cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
10274cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
10282c37256eSGarrett Wollman 		error = EINVAL;
10292c37256eSGarrett Wollman 		goto out;
10302c37256eSGarrett Wollman 	}
10312c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
10322c37256eSGarrett Wollman 		error = EWOULDBLOCK;
10332c37256eSGarrett Wollman 		goto out;
10342c37256eSGarrett Wollman 	}
10352c37256eSGarrett Wollman 	m->m_len = 1;
10362c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
10372c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
10382c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1039623dce13SRobert Watson 
1040623dce13SRobert Watson out:
1041623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
10428501a69cSRobert Watson 	INP_WUNLOCK(inp);
1043623dce13SRobert Watson 	return (error);
10442c37256eSGarrett Wollman }
10452c37256eSGarrett Wollman 
1046b287c6c7SBjoern A. Zeeb #ifdef INET
10472c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1048756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1049756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1050756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1051756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1052756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1053756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1054756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1055756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1056756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
105754d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1058756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1059756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1060756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
1061756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
106254d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1063a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1064a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
10652c37256eSGarrett Wollman };
1066b287c6c7SBjoern A. Zeeb #endif /* INET */
1067df8bae1dSRodney W. Grimes 
1068fb59c426SYoshinobu Inoue #ifdef INET6
1069fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1070756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1071756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1072756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1073756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1074756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1075756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1076756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1077756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1078756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1079756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1080756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1081756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1082756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
1083756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1084756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1085a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1086a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1087fb59c426SYoshinobu Inoue };
1088fb59c426SYoshinobu Inoue #endif /* INET6 */
1089fb59c426SYoshinobu Inoue 
1090b287c6c7SBjoern A. Zeeb #ifdef INET
1091a0292f23SGarrett Wollman /*
1092a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1093a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
10945200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
10955200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
10965200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
10975200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1098a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1099a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1100a0292f23SGarrett Wollman  */
11010312fbe9SPoul-Henning Kamp static int
1102ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1103a0292f23SGarrett Wollman {
1104a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1105a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
11065200e00eSIan Dowse 	struct in_addr laddr;
11075200e00eSIan Dowse 	u_short lport;
1108c3229e05SDavid Greenman 	int error;
1109a0292f23SGarrett Wollman 
11108501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1111fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1112623dce13SRobert Watson 
1113a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
1114b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1115a0292f23SGarrett Wollman 		if (error)
1116fa046d87SRobert Watson 			goto out;
1117a0292f23SGarrett Wollman 	}
1118a0292f23SGarrett Wollman 
1119a0292f23SGarrett Wollman 	/*
1120a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1121a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1122a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1123a0292f23SGarrett Wollman 	 */
11245200e00eSIan Dowse 	laddr = inp->inp_laddr;
11255200e00eSIan Dowse 	lport = inp->inp_lport;
11265200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1127b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
11285200e00eSIan Dowse 	if (error && oinp == NULL)
1129fa046d87SRobert Watson 		goto out;
1130fa046d87SRobert Watson 	if (oinp) {
1131fa046d87SRobert Watson 		error = EADDRINUSE;
1132fa046d87SRobert Watson 		goto out;
1133fa046d87SRobert Watson 	}
11345200e00eSIan Dowse 	inp->inp_laddr = laddr;
113515bd2b43SDavid Greenman 	in_pcbrehash(inp);
1136fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1137a0292f23SGarrett Wollman 
1138087b55eaSAndre Oppermann 	/*
1139087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1140087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1141087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1142087b55eaSAndre Oppermann 	 */
1143a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
11449b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1145a0292f23SGarrett Wollman 		tp->request_r_scale++;
1146a0292f23SGarrett Wollman 
1147a0292f23SGarrett Wollman 	soisconnecting(so);
114878b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
1149a0292f23SGarrett Wollman 	tp->t_state = TCPS_SYN_SENT;
1150b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
1151a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1152a45d2726SAndras Olah 
1153a0292f23SGarrett Wollman 	return 0;
1154fa046d87SRobert Watson 
1155fa046d87SRobert Watson out:
1156fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1157fa046d87SRobert Watson 	return (error);
1158a0292f23SGarrett Wollman }
1159b287c6c7SBjoern A. Zeeb #endif /* INET */
1160a0292f23SGarrett Wollman 
1161fb59c426SYoshinobu Inoue #ifdef INET6
1162fb59c426SYoshinobu Inoue static int
1163ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1164fb59c426SYoshinobu Inoue {
1165fb59c426SYoshinobu Inoue 	struct inpcb *inp = tp->t_inpcb, *oinp;
1166fb59c426SYoshinobu Inoue 	struct socket *so = inp->inp_socket;
1167fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
116888d166bfSBjoern A. Zeeb 	struct in6_addr addr6;
1169fb59c426SYoshinobu Inoue 	int error;
1170fb59c426SYoshinobu Inoue 
11718501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1172fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1173623dce13SRobert Watson 
1174fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
1175b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1176fb59c426SYoshinobu Inoue 		if (error)
1177fa046d87SRobert Watson 			goto out;
1178fb59c426SYoshinobu Inoue 	}
1179fb59c426SYoshinobu Inoue 
1180fb59c426SYoshinobu Inoue 	/*
1181fb59c426SYoshinobu Inoue 	 * Cannot simply call in_pcbconnect, because there might be an
1182fb59c426SYoshinobu Inoue 	 * earlier incarnation of this same connection still in
1183fb59c426SYoshinobu Inoue 	 * TIME_WAIT state, creating an ADDRINUSE error.
1184a1f7e5f8SHajimu UMEMOTO 	 * in6_pcbladdr() also handles scope zone IDs.
1185fa046d87SRobert Watson 	 *
1186fa046d87SRobert Watson 	 * XXXRW: We wouldn't need to expose in6_pcblookup_hash_locked()
1187fa046d87SRobert Watson 	 * outside of in6_pcb.c if there were an in6_pcbconnect_setup().
1188fb59c426SYoshinobu Inoue 	 */
1189fb59c426SYoshinobu Inoue 	error = in6_pcbladdr(inp, nam, &addr6);
1190fb59c426SYoshinobu Inoue 	if (error)
1191b598155aSRobert Watson 		goto out;
1192fa046d87SRobert Watson 	oinp = in6_pcblookup_hash_locked(inp->inp_pcbinfo,
1193fb59c426SYoshinobu Inoue 				  &sin6->sin6_addr, sin6->sin6_port,
1194fb59c426SYoshinobu Inoue 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
119588d166bfSBjoern A. Zeeb 				  ? &addr6
1196fb59c426SYoshinobu Inoue 				  : &inp->in6p_laddr,
1197fb59c426SYoshinobu Inoue 				  inp->inp_lport,  0, NULL);
1198fa046d87SRobert Watson 	if (oinp) {
1199fa046d87SRobert Watson 		error = EADDRINUSE;
1200fa046d87SRobert Watson 		goto out;
1201fa046d87SRobert Watson 	}
1202fb59c426SYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
120388d166bfSBjoern A. Zeeb 		inp->in6p_laddr = addr6;
1204fb59c426SYoshinobu Inoue 	inp->in6p_faddr = sin6->sin6_addr;
1205fb59c426SYoshinobu Inoue 	inp->inp_fport = sin6->sin6_port;
12068a59da30SHajimu UMEMOTO 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
1207fc384fa5SBjoern A. Zeeb 	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
1208fc384fa5SBjoern A. Zeeb 	if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
1209fc384fa5SBjoern A. Zeeb 		inp->inp_flow |=
12108a59da30SHajimu UMEMOTO 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1211fb59c426SYoshinobu Inoue 	in_pcbrehash(inp);
1212fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1213fb59c426SYoshinobu Inoue 
1214fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1215fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1216970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1217fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1218fb59c426SYoshinobu Inoue 
1219fb59c426SYoshinobu Inoue 	soisconnecting(so);
122078b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
1221fb59c426SYoshinobu Inoue 	tp->t_state = TCPS_SYN_SENT;
1222b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
1223fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1224fb59c426SYoshinobu Inoue 
1225fb59c426SYoshinobu Inoue 	return 0;
1226fa046d87SRobert Watson 
1227fa046d87SRobert Watson out:
1228fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1229fa046d87SRobert Watson 	return error;
1230fb59c426SYoshinobu Inoue }
1231fb59c426SYoshinobu Inoue #endif /* INET6 */
1232fb59c426SYoshinobu Inoue 
1233cfe8b629SGarrett Wollman /*
1234b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1235b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1236b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1237b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1238b8af5dfaSRobert Watson  * from Linux.
1239b8af5dfaSRobert Watson  */
1240b8af5dfaSRobert Watson static void
1241ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1242b8af5dfaSRobert Watson {
1243b8af5dfaSRobert Watson 
12448501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1245b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1246b8af5dfaSRobert Watson 
1247b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1248b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1249b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
12503529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1251b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1252b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1253b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1254b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1255b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1256b8af5dfaSRobert Watson 	}
12571baaf834SBruce M Simpson 
125843d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
125943d94734SJohn Baldwin 	ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
12601baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
12611baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
12621baaf834SBruce M Simpson 
1263b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1264b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1265b8af5dfaSRobert Watson 
1266b8af5dfaSRobert Watson 	/*
1267b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1268b8af5dfaSRobert Watson 	 */
1269c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1270535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1271b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
12721c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1273535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
127443d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
127543d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1276535fbad6SKip Macy 	if (tp->t_flags & TF_TOE)
1277535fbad6SKip Macy 		ti->tcpi_options |= TCPI_OPT_TOE;
1278f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1279f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1280f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1281b8af5dfaSRobert Watson }
1282b8af5dfaSRobert Watson 
1283b8af5dfaSRobert Watson /*
12841e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
12851e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
12861e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
12871e8f5ffaSRobert Watson  * option.
1288cfe8b629SGarrett Wollman  */
12898501a69cSRobert Watson #define INP_WLOCK_RECHECK(inp) do {					\
12908501a69cSRobert Watson 	INP_WLOCK(inp);							\
1291ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
12928501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
12931e8f5ffaSRobert Watson 		return (ECONNRESET);					\
12941e8f5ffaSRobert Watson 	}								\
12951e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
12961e8f5ffaSRobert Watson } while(0)
12971e8f5ffaSRobert Watson 
1298df8bae1dSRodney W. Grimes int
1299ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1300df8bae1dSRodney W. Grimes {
13013f9d1ef9SRobert Watson 	int	error, opt, optval;
13029077f387SGleb Smirnoff 	u_int	ui;
1303df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1304cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
1305b8af5dfaSRobert Watson 	struct	tcp_info ti;
1306dbc42409SLawrence Stewart 	char buf[TCP_CA_NAME_MAX];
1307dbc42409SLawrence Stewart 	struct cc_algo *algo;
1308df8bae1dSRodney W. Grimes 
1309cfe8b629SGarrett Wollman 	error = 0;
1310df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1311623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
13128501a69cSRobert Watson 	INP_WLOCK(inp);
1313cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1314fb59c426SYoshinobu Inoue #ifdef INET6
13155cd54324SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6PROTO) {
13168501a69cSRobert Watson 			INP_WUNLOCK(inp);
1317fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
1318b287c6c7SBjoern A. Zeeb 		}
1319fb59c426SYoshinobu Inoue #endif /* INET6 */
1320b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1321b287c6c7SBjoern A. Zeeb 		else
1322b287c6c7SBjoern A. Zeeb #endif
1323b287c6c7SBjoern A. Zeeb #ifdef INET
1324b287c6c7SBjoern A. Zeeb 		{
13258501a69cSRobert Watson 			INP_WUNLOCK(inp);
1326cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
13271e8f5ffaSRobert Watson 		}
13281e8f5ffaSRobert Watson #endif
1329df8bae1dSRodney W. Grimes 		return (error);
1330df8bae1dSRodney W. Grimes 	}
1331ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
13328501a69cSRobert Watson 		INP_WUNLOCK(inp);
13331e8f5ffaSRobert Watson 		return (ECONNRESET);
1334623dce13SRobert Watson 	}
1335df8bae1dSRodney W. Grimes 
1336cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1337cfe8b629SGarrett Wollman 	case SOPT_SET:
1338cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
13391cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
134088f6b043SBruce M Simpson 		case TCP_MD5SIG:
13418501a69cSRobert Watson 			INP_WUNLOCK(inp);
13421cfd4b53SBruce M Simpson 			error = sooptcopyin(sopt, &optval, sizeof optval,
13431cfd4b53SBruce M Simpson 			    sizeof optval);
13441cfd4b53SBruce M Simpson 			if (error)
13451e8f5ffaSRobert Watson 				return (error);
13461cfd4b53SBruce M Simpson 
13478501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
13481cfd4b53SBruce M Simpson 			if (optval > 0)
13491cfd4b53SBruce M Simpson 				tp->t_flags |= TF_SIGNATURE;
13501cfd4b53SBruce M Simpson 			else
13511cfd4b53SBruce M Simpson 				tp->t_flags &= ~TF_SIGNATURE;
1352*09fe6320SNavdeep Parhar 			goto unlock_and_done;
13531cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
1354*09fe6320SNavdeep Parhar 
1355df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1356cfe8b629SGarrett Wollman 		case TCP_NOOPT:
13578501a69cSRobert Watson 			INP_WUNLOCK(inp);
1358cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1359cfe8b629SGarrett Wollman 			    sizeof optval);
1360cfe8b629SGarrett Wollman 			if (error)
13611e8f5ffaSRobert Watson 				return (error);
1362cfe8b629SGarrett Wollman 
13638501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1364cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1365cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1366cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1367cfe8b629SGarrett Wollman 				break;
1368cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1369cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1370cfe8b629SGarrett Wollman 				break;
1371cfe8b629SGarrett Wollman 			default:
1372cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1373cfe8b629SGarrett Wollman 				break;
1374cfe8b629SGarrett Wollman 			}
1375cfe8b629SGarrett Wollman 
1376cfe8b629SGarrett Wollman 			if (optval)
1377cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1378df8bae1dSRodney W. Grimes 			else
1379cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
1380*09fe6320SNavdeep Parhar unlock_and_done:
1381*09fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
1382*09fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
1383*09fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
1384*09fe6320SNavdeep Parhar 				    sopt->sopt_name);
1385*09fe6320SNavdeep Parhar 			}
1386*09fe6320SNavdeep Parhar #endif
13878501a69cSRobert Watson 			INP_WUNLOCK(inp);
1388df8bae1dSRodney W. Grimes 			break;
1389df8bae1dSRodney W. Grimes 
1390007581c0SJonathan Lemon 		case TCP_NOPUSH:
13918501a69cSRobert Watson 			INP_WUNLOCK(inp);
1392007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1393007581c0SJonathan Lemon 			    sizeof optval);
1394007581c0SJonathan Lemon 			if (error)
13951e8f5ffaSRobert Watson 				return (error);
1396007581c0SJonathan Lemon 
13978501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1398007581c0SJonathan Lemon 			if (optval)
1399007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1400d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
1401007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1402d28b9e89SJohn Baldwin 				if (TCPS_HAVEESTABLISHED(tp->t_state))
1403007581c0SJonathan Lemon 					error = tcp_output(tp);
1404007581c0SJonathan Lemon 			}
1405*09fe6320SNavdeep Parhar 			goto unlock_and_done;
1406007581c0SJonathan Lemon 
1407df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
14088501a69cSRobert Watson 			INP_WUNLOCK(inp);
1409cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1410cfe8b629SGarrett Wollman 			    sizeof optval);
1411cfe8b629SGarrett Wollman 			if (error)
14121e8f5ffaSRobert Watson 				return (error);
1413df8bae1dSRodney W. Grimes 
14148501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
141553369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
1416603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
1417cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1418a0292f23SGarrett Wollman 			else
1419a0292f23SGarrett Wollman 				error = EINVAL;
1420*09fe6320SNavdeep Parhar 			goto unlock_and_done;
1421a0292f23SGarrett Wollman 
1422b8af5dfaSRobert Watson 		case TCP_INFO:
14238501a69cSRobert Watson 			INP_WUNLOCK(inp);
1424b8af5dfaSRobert Watson 			error = EINVAL;
1425b8af5dfaSRobert Watson 			break;
1426b8af5dfaSRobert Watson 
1427dbc42409SLawrence Stewart 		case TCP_CONGESTION:
1428dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
1429dbc42409SLawrence Stewart 			bzero(buf, sizeof(buf));
1430dbc42409SLawrence Stewart 			error = sooptcopyin(sopt, &buf, sizeof(buf), 1);
1431dbc42409SLawrence Stewart 			if (error)
1432dbc42409SLawrence Stewart 				break;
1433dbc42409SLawrence Stewart 			INP_WLOCK_RECHECK(inp);
1434dbc42409SLawrence Stewart 			/*
1435dbc42409SLawrence Stewart 			 * Return EINVAL if we can't find the requested cc algo.
1436dbc42409SLawrence Stewart 			 */
1437dbc42409SLawrence Stewart 			error = EINVAL;
1438dbc42409SLawrence Stewart 			CC_LIST_RLOCK();
1439dbc42409SLawrence Stewart 			STAILQ_FOREACH(algo, &cc_list, entries) {
1440dbc42409SLawrence Stewart 				if (strncmp(buf, algo->name, TCP_CA_NAME_MAX)
1441dbc42409SLawrence Stewart 				    == 0) {
1442dbc42409SLawrence Stewart 					/* We've found the requested algo. */
1443dbc42409SLawrence Stewart 					error = 0;
1444dbc42409SLawrence Stewart 					/*
1445dbc42409SLawrence Stewart 					 * We hold a write lock over the tcb
1446dbc42409SLawrence Stewart 					 * so it's safe to do these things
1447dbc42409SLawrence Stewart 					 * without ordering concerns.
1448dbc42409SLawrence Stewart 					 */
1449dbc42409SLawrence Stewart 					if (CC_ALGO(tp)->cb_destroy != NULL)
1450dbc42409SLawrence Stewart 						CC_ALGO(tp)->cb_destroy(tp->ccv);
1451dbc42409SLawrence Stewart 					CC_ALGO(tp) = algo;
1452dbc42409SLawrence Stewart 					/*
1453dbc42409SLawrence Stewart 					 * If something goes pear shaped
1454dbc42409SLawrence Stewart 					 * initialising the new algo,
1455dbc42409SLawrence Stewart 					 * fall back to newreno (which
1456dbc42409SLawrence Stewart 					 * does not require initialisation).
1457dbc42409SLawrence Stewart 					 */
1458dbc42409SLawrence Stewart 					if (algo->cb_init != NULL)
1459dbc42409SLawrence Stewart 						if (algo->cb_init(tp->ccv) > 0) {
1460dbc42409SLawrence Stewart 							CC_ALGO(tp) = &newreno_cc_algo;
1461dbc42409SLawrence Stewart 							/*
1462dbc42409SLawrence Stewart 							 * The only reason init
1463dbc42409SLawrence Stewart 							 * should fail is
1464dbc42409SLawrence Stewart 							 * because of malloc.
1465dbc42409SLawrence Stewart 							 */
1466dbc42409SLawrence Stewart 							error = ENOMEM;
1467dbc42409SLawrence Stewart 						}
1468dbc42409SLawrence Stewart 					break; /* Break the STAILQ_FOREACH. */
1469dbc42409SLawrence Stewart 				}
1470dbc42409SLawrence Stewart 			}
1471dbc42409SLawrence Stewart 			CC_LIST_RUNLOCK();
1472*09fe6320SNavdeep Parhar 			goto unlock_and_done;
1473dbc42409SLawrence Stewart 
14749077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
14759077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
14769077f387SGleb Smirnoff 		case TCP_KEEPCNT:
14779077f387SGleb Smirnoff 		case TCP_KEEPINIT:
14789077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
14799077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
14809077f387SGleb Smirnoff 			if (error)
14819077f387SGleb Smirnoff 				return (error);
14829077f387SGleb Smirnoff 
14839077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
14849077f387SGleb Smirnoff 				error = EINVAL;
14859077f387SGleb Smirnoff 				break;
14869077f387SGleb Smirnoff 			}
14879077f387SGleb Smirnoff 			ui *= hz;
14889077f387SGleb Smirnoff 
14899077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
14909077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
14919077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
14929077f387SGleb Smirnoff 				tp->t_keepidle = ui;
14939077f387SGleb Smirnoff 				/*
14949077f387SGleb Smirnoff 				 * XXX: better check current remaining
14959077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
14969077f387SGleb Smirnoff 				 */
14979077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
14989077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
14999077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
15009077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
15019077f387SGleb Smirnoff 				break;
15029077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
15039077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
15049077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
15059077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
15069077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
15079077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
15089077f387SGleb Smirnoff 				break;
15099077f387SGleb Smirnoff 			case TCP_KEEPCNT:
15109077f387SGleb Smirnoff 				tp->t_keepcnt = ui;
15119077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
15129077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
15139077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
15149077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
15159077f387SGleb Smirnoff 				break;
15169077f387SGleb Smirnoff 			case TCP_KEEPINIT:
15179077f387SGleb Smirnoff 				tp->t_keepinit = ui;
15189077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
15199077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
15209077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
15219077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
15229077f387SGleb Smirnoff 				break;
15239077f387SGleb Smirnoff 			}
1524*09fe6320SNavdeep Parhar 			goto unlock_and_done;
15259077f387SGleb Smirnoff 
1526df8bae1dSRodney W. Grimes 		default:
15278501a69cSRobert Watson 			INP_WUNLOCK(inp);
1528df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1529df8bae1dSRodney W. Grimes 			break;
1530df8bae1dSRodney W. Grimes 		}
1531df8bae1dSRodney W. Grimes 		break;
1532df8bae1dSRodney W. Grimes 
1533cfe8b629SGarrett Wollman 	case SOPT_GET:
15341e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
1535cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
15361cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
153788f6b043SBruce M Simpson 		case TCP_MD5SIG:
15381cfd4b53SBruce M Simpson 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
15398501a69cSRobert Watson 			INP_WUNLOCK(inp);
1540b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
15411cfd4b53SBruce M Simpson 			break;
1542265ed012SBruce M Simpson #endif
15431e8f5ffaSRobert Watson 
1544df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1545cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
15468501a69cSRobert Watson 			INP_WUNLOCK(inp);
1547b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1548df8bae1dSRodney W. Grimes 			break;
1549df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1550cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
15518501a69cSRobert Watson 			INP_WUNLOCK(inp);
1552b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1553df8bae1dSRodney W. Grimes 			break;
1554a0292f23SGarrett Wollman 		case TCP_NOOPT:
1555cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
15568501a69cSRobert Watson 			INP_WUNLOCK(inp);
1557b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1558a0292f23SGarrett Wollman 			break;
1559a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1560cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
15618501a69cSRobert Watson 			INP_WUNLOCK(inp);
1562b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1563b8af5dfaSRobert Watson 			break;
1564b8af5dfaSRobert Watson 		case TCP_INFO:
1565b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
15668501a69cSRobert Watson 			INP_WUNLOCK(inp);
1567b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
1568a0292f23SGarrett Wollman 			break;
1569dbc42409SLawrence Stewart 		case TCP_CONGESTION:
1570dbc42409SLawrence Stewart 			bzero(buf, sizeof(buf));
1571dbc42409SLawrence Stewart 			strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1572dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
1573dbc42409SLawrence Stewart 			error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX);
1574dbc42409SLawrence Stewart 			break;
1575df8bae1dSRodney W. Grimes 		default:
15768501a69cSRobert Watson 			INP_WUNLOCK(inp);
1577df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1578df8bae1dSRodney W. Grimes 			break;
1579df8bae1dSRodney W. Grimes 		}
1580df8bae1dSRodney W. Grimes 		break;
1581df8bae1dSRodney W. Grimes 	}
1582df8bae1dSRodney W. Grimes 	return (error);
1583df8bae1dSRodney W. Grimes }
15848501a69cSRobert Watson #undef INP_WLOCK_RECHECK
1585df8bae1dSRodney W. Grimes 
158626e30fbbSDavid Greenman /*
1587df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1588df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1589df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1590df8bae1dSRodney W. Grimes  */
15910312fbe9SPoul-Henning Kamp static int
1592ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so)
1593df8bae1dSRodney W. Grimes {
1594ad3f9ab3SAndre Oppermann 	struct tcpcb *tp;
1595df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1596df8bae1dSRodney W. Grimes 	int error;
1597df8bae1dSRodney W. Grimes 
1598df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1599e233e2acSAndre Oppermann 		error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1600df8bae1dSRodney W. Grimes 		if (error)
1601df8bae1dSRodney W. Grimes 			return (error);
1602df8bae1dSRodney W. Grimes 	}
16036741ecf5SAndre Oppermann 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
16046741ecf5SAndre Oppermann 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1605603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
1606603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_tcbinfo);
1607f2de87feSRobert Watson 	if (error) {
1608603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
1609df8bae1dSRodney W. Grimes 		return (error);
1610f2de87feSRobert Watson 	}
1611df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1612fb59c426SYoshinobu Inoue #ifdef INET6
16135cd54324SBjoern A. Zeeb 	if (inp->inp_vflag & INP_IPV6PROTO) {
1614fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1615fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1616fb59c426SYoshinobu Inoue 	}
1617fb59c426SYoshinobu Inoue 	else
1618fb59c426SYoshinobu Inoue #endif
1619cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1620df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1621623dce13SRobert Watson 	if (tp == NULL) {
1622df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
16230206cdb8SBjoern A. Zeeb 		in_pcbfree(inp);
1624603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
1625df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1626df8bae1dSRodney W. Grimes 	}
1627df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
16288501a69cSRobert Watson 	INP_WUNLOCK(inp);
1629603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
1630df8bae1dSRodney W. Grimes 	return (0);
1631df8bae1dSRodney W. Grimes }
1632df8bae1dSRodney W. Grimes 
1633df8bae1dSRodney W. Grimes /*
1634df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1635df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1636df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1637df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1638df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1639df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1640df8bae1dSRodney W. Grimes  */
1641623dce13SRobert Watson static void
1642ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
1643df8bae1dSRodney W. Grimes {
1644e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
1645e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
1646e6e0b5ffSRobert Watson 
1647603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
16488501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1649df8bae1dSRodney W. Grimes 
1650623dce13SRobert Watson 	/*
1651623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1652623dce13SRobert Watson 	 * socket is still open.
1653623dce13SRobert Watson 	 */
1654623dce13SRobert Watson 	if (tp->t_state < TCPS_ESTABLISHED) {
1655df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1656623dce13SRobert Watson 		KASSERT(tp != NULL,
1657623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
1658623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1659243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
1660623dce13SRobert Watson 		KASSERT(tp != NULL,
1661623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
1662623dce13SRobert Watson 	} else {
1663df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
1664df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
1665623dce13SRobert Watson 		tcp_usrclosed(tp);
1666ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
1667*09fe6320SNavdeep Parhar 			tcp_output(tp);
1668df8bae1dSRodney W. Grimes 	}
1669df8bae1dSRodney W. Grimes }
1670df8bae1dSRodney W. Grimes 
1671df8bae1dSRodney W. Grimes /*
1672df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1673df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1674df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1675df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1676df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1677df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1678df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1679df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1680df8bae1dSRodney W. Grimes  */
1681623dce13SRobert Watson static void
1682ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
1683df8bae1dSRodney W. Grimes {
1684df8bae1dSRodney W. Grimes 
1685603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
16868501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1687e6e0b5ffSRobert Watson 
1688df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1689df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
1690*09fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
1691*09fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
1692*09fe6320SNavdeep Parhar #endif
1693bc65987aSKip Macy 		/* FALLTHROUGH */
1694bc65987aSKip Macy 	case TCPS_CLOSED:
1695df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_CLOSED;
1696df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1697623dce13SRobert Watson 		/*
1698623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
1699623dce13SRobert Watson 		 * still open.
1700623dce13SRobert Watson 		 */
1701623dce13SRobert Watson 		KASSERT(tp != NULL,
1702623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
1703df8bae1dSRodney W. Grimes 		break;
1704df8bae1dSRodney W. Grimes 
1705a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
1706df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1707a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
1708a0292f23SGarrett Wollman 		break;
1709a0292f23SGarrett Wollman 
1710df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1711df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_FIN_WAIT_1;
1712df8bae1dSRodney W. Grimes 		break;
1713df8bae1dSRodney W. Grimes 
1714df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1715df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_LAST_ACK;
1716df8bae1dSRodney W. Grimes 		break;
1717df8bae1dSRodney W. Grimes 	}
1718abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
1719df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
1720abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
17217c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
17227c72af87SMohan Srinivasan 			int timeout;
17237c72af87SMohan Srinivasan 
17247c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
17259077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
1726b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
1727b6239c4aSAndras Olah 		}
1728df8bae1dSRodney W. Grimes 	}
17297c72af87SMohan Srinivasan }
1730497057eeSRobert Watson 
1731497057eeSRobert Watson #ifdef DDB
1732497057eeSRobert Watson static void
1733497057eeSRobert Watson db_print_indent(int indent)
1734497057eeSRobert Watson {
1735497057eeSRobert Watson 	int i;
1736497057eeSRobert Watson 
1737497057eeSRobert Watson 	for (i = 0; i < indent; i++)
1738497057eeSRobert Watson 		db_printf(" ");
1739497057eeSRobert Watson }
1740497057eeSRobert Watson 
1741497057eeSRobert Watson static void
1742497057eeSRobert Watson db_print_tstate(int t_state)
1743497057eeSRobert Watson {
1744497057eeSRobert Watson 
1745497057eeSRobert Watson 	switch (t_state) {
1746497057eeSRobert Watson 	case TCPS_CLOSED:
1747497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
1748497057eeSRobert Watson 		return;
1749497057eeSRobert Watson 
1750497057eeSRobert Watson 	case TCPS_LISTEN:
1751497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
1752497057eeSRobert Watson 		return;
1753497057eeSRobert Watson 
1754497057eeSRobert Watson 	case TCPS_SYN_SENT:
1755497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
1756497057eeSRobert Watson 		return;
1757497057eeSRobert Watson 
1758497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
1759497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
1760497057eeSRobert Watson 		return;
1761497057eeSRobert Watson 
1762497057eeSRobert Watson 	case TCPS_ESTABLISHED:
1763497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
1764497057eeSRobert Watson 		return;
1765497057eeSRobert Watson 
1766497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
1767497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
1768497057eeSRobert Watson 		return;
1769497057eeSRobert Watson 
1770497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
1771497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
1772497057eeSRobert Watson 		return;
1773497057eeSRobert Watson 
1774497057eeSRobert Watson 	case TCPS_CLOSING:
1775497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
1776497057eeSRobert Watson 		return;
1777497057eeSRobert Watson 
1778497057eeSRobert Watson 	case TCPS_LAST_ACK:
1779497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
1780497057eeSRobert Watson 		return;
1781497057eeSRobert Watson 
1782497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
1783497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
1784497057eeSRobert Watson 		return;
1785497057eeSRobert Watson 
1786497057eeSRobert Watson 	case TCPS_TIME_WAIT:
1787497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
1788497057eeSRobert Watson 		return;
1789497057eeSRobert Watson 
1790497057eeSRobert Watson 	default:
1791497057eeSRobert Watson 		db_printf("unknown");
1792497057eeSRobert Watson 		return;
1793497057eeSRobert Watson 	}
1794497057eeSRobert Watson }
1795497057eeSRobert Watson 
1796497057eeSRobert Watson static void
1797497057eeSRobert Watson db_print_tflags(u_int t_flags)
1798497057eeSRobert Watson {
1799497057eeSRobert Watson 	int comma;
1800497057eeSRobert Watson 
1801497057eeSRobert Watson 	comma = 0;
1802497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
1803497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
1804497057eeSRobert Watson 		comma = 1;
1805497057eeSRobert Watson 	}
1806497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
1807497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
1808497057eeSRobert Watson 		comma = 1;
1809497057eeSRobert Watson 	}
1810497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
1811497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
1812497057eeSRobert Watson 		comma = 1;
1813497057eeSRobert Watson 	}
1814497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
1815497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
1816497057eeSRobert Watson 		comma = 1;
1817497057eeSRobert Watson 	}
1818497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
1819497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
1820497057eeSRobert Watson 		comma = 1;
1821497057eeSRobert Watson 	}
1822497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
1823497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1824497057eeSRobert Watson 		comma = 1;
1825497057eeSRobert Watson 	}
1826497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
1827497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1828497057eeSRobert Watson 		comma = 1;
1829497057eeSRobert Watson 	}
1830497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
1831497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1832497057eeSRobert Watson 		comma = 1;
1833497057eeSRobert Watson 	}
1834497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
1835497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1836497057eeSRobert Watson 		comma = 1;
1837497057eeSRobert Watson 	}
1838497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
1839497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1840497057eeSRobert Watson 		comma = 1;
1841497057eeSRobert Watson 	}
1842497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
1843497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1844497057eeSRobert Watson 		comma = 1;
1845497057eeSRobert Watson 	}
1846497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
1847497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1848497057eeSRobert Watson 		comma = 1;
1849497057eeSRobert Watson 	}
1850497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
1851497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
1852497057eeSRobert Watson 		comma = 1;
1853497057eeSRobert Watson 	}
1854497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
1855497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1856497057eeSRobert Watson 		comma = 1;
1857497057eeSRobert Watson 	}
1858497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
1859497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1860497057eeSRobert Watson 		comma = 1;
1861497057eeSRobert Watson 	}
1862497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
1863497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1864497057eeSRobert Watson 		comma = 1;
1865497057eeSRobert Watson 	}
1866497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
1867497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1868497057eeSRobert Watson 		comma = 1;
1869497057eeSRobert Watson 	}
1870497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
1871497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1872497057eeSRobert Watson 		comma = 1;
1873497057eeSRobert Watson 	}
1874dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
1875dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
1876dbc42409SLawrence Stewart 		comma = 1;
1877dbc42409SLawrence Stewart 	}
1878497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
1879497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1880497057eeSRobert Watson 		comma = 1;
1881497057eeSRobert Watson 	}
1882497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
1883497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1884497057eeSRobert Watson 		comma = 1;
1885497057eeSRobert Watson 	}
1886497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
1887497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1888497057eeSRobert Watson 		comma = 1;
1889497057eeSRobert Watson 	}
1890497057eeSRobert Watson 	if (t_flags & TF_TSO) {
1891497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
1892497057eeSRobert Watson 		comma = 1;
1893497057eeSRobert Watson 	}
1894f2512ba1SRui Paulo 	if (t_flags & TF_ECN_PERMIT) {
1895f2512ba1SRui Paulo 		db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
1896f2512ba1SRui Paulo 		comma = 1;
1897f2512ba1SRui Paulo 	}
1898497057eeSRobert Watson }
1899497057eeSRobert Watson 
1900497057eeSRobert Watson static void
1901497057eeSRobert Watson db_print_toobflags(char t_oobflags)
1902497057eeSRobert Watson {
1903497057eeSRobert Watson 	int comma;
1904497057eeSRobert Watson 
1905497057eeSRobert Watson 	comma = 0;
1906497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
1907497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1908497057eeSRobert Watson 		comma = 1;
1909497057eeSRobert Watson 	}
1910497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
1911497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1912497057eeSRobert Watson 		comma = 1;
1913497057eeSRobert Watson 	}
1914497057eeSRobert Watson }
1915497057eeSRobert Watson 
1916497057eeSRobert Watson static void
1917497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1918497057eeSRobert Watson {
1919497057eeSRobert Watson 
1920497057eeSRobert Watson 	db_print_indent(indent);
1921497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
1922497057eeSRobert Watson 
1923497057eeSRobert Watson 	indent += 2;
1924497057eeSRobert Watson 
1925497057eeSRobert Watson 	db_print_indent(indent);
1926497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
1927497057eeSRobert Watson 	   LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
1928497057eeSRobert Watson 
1929497057eeSRobert Watson 	db_print_indent(indent);
193085d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
1931e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
1932497057eeSRobert Watson 
1933497057eeSRobert Watson 	db_print_indent(indent);
1934e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
1935e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
1936497057eeSRobert Watson 
1937497057eeSRobert Watson 	db_print_indent(indent);
1938497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
1939497057eeSRobert Watson 	db_print_tstate(tp->t_state);
1940497057eeSRobert Watson 	db_printf(")\n");
1941497057eeSRobert Watson 
1942497057eeSRobert Watson 	db_print_indent(indent);
1943497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
1944497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
1945497057eeSRobert Watson 	db_printf(")\n");
1946497057eeSRobert Watson 
1947497057eeSRobert Watson 	db_print_indent(indent);
1948497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
1949497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
1950497057eeSRobert Watson 
1951497057eeSRobert Watson 	db_print_indent(indent);
1952497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
1953497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
1954497057eeSRobert Watson 
1955497057eeSRobert Watson 	db_print_indent(indent);
1956497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
1957497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
1958497057eeSRobert Watson 
1959497057eeSRobert Watson 	db_print_indent(indent);
1960497057eeSRobert Watson 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %lu   rcv_up: 0x%08x\n",
1961497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
1962497057eeSRobert Watson 
1963497057eeSRobert Watson 	db_print_indent(indent);
19641c18314dSAndre Oppermann 	db_printf("snd_wnd: %lu   snd_cwnd: %lu\n",
19651c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
1966497057eeSRobert Watson 
1967497057eeSRobert Watson 	db_print_indent(indent);
19681c18314dSAndre Oppermann 	db_printf("snd_ssthresh: %lu   snd_recover: "
19691c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
1970497057eeSRobert Watson 
1971497057eeSRobert Watson 	db_print_indent(indent);
19729f78a87aSJohn Baldwin 	db_printf("t_maxopd: %u   t_rcvtime: %u   t_startime: %u\n",
1973497057eeSRobert Watson 	    tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
1974497057eeSRobert Watson 
1975497057eeSRobert Watson 	db_print_indent(indent);
19761c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
19771c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
1978497057eeSRobert Watson 
1979497057eeSRobert Watson 	db_print_indent(indent);
19801c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
19811c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
1982497057eeSRobert Watson 
1983497057eeSRobert Watson 	db_print_indent(indent);
1984497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
1985497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
1986497057eeSRobert Watson 	    tp->t_rttbest);
1987497057eeSRobert Watson 
1988497057eeSRobert Watson 	db_print_indent(indent);
1989497057eeSRobert Watson 	db_printf("t_rttupdated: %lu   max_sndwnd: %lu   t_softerror: %d\n",
1990497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
1991497057eeSRobert Watson 
1992497057eeSRobert Watson 	db_print_indent(indent);
1993497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
1994497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
1995497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
1996497057eeSRobert Watson 
1997497057eeSRobert Watson 	db_print_indent(indent);
1998497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
1999497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2000497057eeSRobert Watson 
2001497057eeSRobert Watson 	db_print_indent(indent);
20029f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
20031a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
2004497057eeSRobert Watson 
2005497057eeSRobert Watson 	db_print_indent(indent);
2006497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
2007497057eeSRobert Watson 	    "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2008497057eeSRobert Watson 
2009497057eeSRobert Watson 	db_print_indent(indent);
2010497057eeSRobert Watson 	db_printf("snd_ssthresh_prev: %lu   snd_recover_prev: 0x%08x   "
20119f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2012497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
2013497057eeSRobert Watson 
2014497057eeSRobert Watson 	db_print_indent(indent);
20153529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
20163529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2017497057eeSRobert Watson 
2018497057eeSRobert Watson 	db_print_indent(indent);
2019497057eeSRobert Watson 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
2020497057eeSRobert Watson 	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2021497057eeSRobert Watson 
2022497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
2023497057eeSRobert Watson 
2024497057eeSRobert Watson 	db_print_indent(indent);
2025497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
2026497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2027497057eeSRobert Watson }
2028497057eeSRobert Watson 
2029497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2030497057eeSRobert Watson {
2031497057eeSRobert Watson 	struct tcpcb *tp;
2032497057eeSRobert Watson 
2033497057eeSRobert Watson 	if (!have_addr) {
2034497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
2035497057eeSRobert Watson 		return;
2036497057eeSRobert Watson 	}
2037497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
2038497057eeSRobert Watson 
2039497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
2040497057eeSRobert Watson }
2041497057eeSRobert Watson #endif
2042