xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 78b5071407e7f59724b2c3d3a4bd9f5cca1f0794)
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
5623dce13SRobert Watson  * All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
311fdbc7aeSGarrett Wollman  *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
32df8bae1dSRodney W. Grimes  */
33df8bae1dSRodney W. Grimes 
344b421e2dSMike Silbersack #include <sys/cdefs.h>
354b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
364b421e2dSMike Silbersack 
37497057eeSRobert Watson #include "opt_ddb.h"
381cfd4b53SBruce M Simpson #include "opt_inet.h"
39fb59c426SYoshinobu Inoue #include "opt_inet6.h"
400cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
410cc12cc5SJoerg Wunsch 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43df8bae1dSRodney W. Grimes #include <sys/systm.h>
44f76fcf6dSJeffrey Hsu #include <sys/malloc.h>
45c7a82f90SGarrett Wollman #include <sys/kernel.h>
4698163b98SPoul-Henning Kamp #include <sys/sysctl.h>
47df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
48fb59c426SYoshinobu Inoue #ifdef INET6
49fb59c426SYoshinobu Inoue #include <sys/domain.h>
50fb59c426SYoshinobu Inoue #endif /* INET6 */
51df8bae1dSRodney W. Grimes #include <sys/socket.h>
52df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
53df8bae1dSRodney W. Grimes #include <sys/protosw.h>
5491421ba2SRobert Watson #include <sys/proc.h>
5591421ba2SRobert Watson #include <sys/jail.h>
56603724d3SBjoern A. Zeeb #include <sys/vimage.h>
57df8bae1dSRodney W. Grimes 
58497057eeSRobert Watson #ifdef DDB
59497057eeSRobert Watson #include <ddb/ddb.h>
60497057eeSRobert Watson #endif
61497057eeSRobert Watson 
62df8bae1dSRodney W. Grimes #include <net/if.h>
63df8bae1dSRodney W. Grimes #include <net/route.h>
64df8bae1dSRodney W. Grimes 
65df8bae1dSRodney W. Grimes #include <netinet/in.h>
66df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
67fb59c426SYoshinobu Inoue #ifdef INET6
68fb59c426SYoshinobu Inoue #include <netinet/ip6.h>
69fb59c426SYoshinobu Inoue #endif
70df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
71fb59c426SYoshinobu Inoue #ifdef INET6
72fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h>
73fb59c426SYoshinobu Inoue #endif
74b5e8ce9fSBruce Evans #include <netinet/in_var.h>
75df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
76fb59c426SYoshinobu Inoue #ifdef INET6
77fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
78a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
79fb59c426SYoshinobu Inoue #endif
80df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
81df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
82df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
83df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
84df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
85df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
86610ee2f9SDavid Greenman #ifdef TCPDEBUG
87df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
88610ee2f9SDavid Greenman #endif
89bc65987aSKip Macy #include <netinet/tcp_offload.h>
904b79449eSBjoern A. Zeeb #include <netinet/vinet.h>
91df8bae1dSRodney W. Grimes 
92df8bae1dSRodney W. Grimes /*
93df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
94df8bae1dSRodney W. Grimes  */
9556dc72c3SPawel Jakub Dawidek static int	tcp_attach(struct socket *);
964d77a549SAlfred Perlstein static int	tcp_connect(struct tcpcb *, struct sockaddr *,
974d77a549SAlfred Perlstein 		    struct thread *td);
98fb59c426SYoshinobu Inoue #ifdef INET6
994d77a549SAlfred Perlstein static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
1004d77a549SAlfred Perlstein 		    struct thread *td);
101fb59c426SYoshinobu Inoue #endif /* INET6 */
102623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
103623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
104b8af5dfaSRobert Watson static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
1052c37256eSGarrett Wollman 
1062c37256eSGarrett Wollman #ifdef TCPDEBUG
1071db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1082c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
1094cc20ab1SSeigo Tanimura #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
1104cc20ab1SSeigo Tanimura 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1112c37256eSGarrett Wollman #else
1122c37256eSGarrett Wollman #define	TCPDEBUG0
1132c37256eSGarrett Wollman #define	TCPDEBUG1()
1142c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1152c37256eSGarrett Wollman #endif
1162c37256eSGarrett Wollman 
1172c37256eSGarrett Wollman /*
1182c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1192c37256eSGarrett Wollman  * and an internet control block.
1202c37256eSGarrett Wollman  */
1212c37256eSGarrett Wollman static int
122b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1232c37256eSGarrett Wollman {
124f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
125623dce13SRobert Watson 	struct tcpcb *tp = NULL;
126623dce13SRobert Watson 	int error;
1272c37256eSGarrett Wollman 	TCPDEBUG0;
1282c37256eSGarrett Wollman 
129623dce13SRobert Watson 	inp = sotoinpcb(so);
130623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1312c37256eSGarrett Wollman 	TCPDEBUG1();
1322c37256eSGarrett Wollman 
13356dc72c3SPawel Jakub Dawidek 	error = tcp_attach(so);
1342c37256eSGarrett Wollman 	if (error)
1352c37256eSGarrett Wollman 		goto out;
1362c37256eSGarrett Wollman 
1372c37256eSGarrett Wollman 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1383879597fSAndrey A. Chernov 		so->so_linger = TCP_LINGERTIME;
139f76fcf6dSJeffrey Hsu 
140f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
141f76fcf6dSJeffrey Hsu 	tp = intotcpcb(inp);
1422c37256eSGarrett Wollman out:
1432c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
1442c37256eSGarrett Wollman 	return error;
1452c37256eSGarrett Wollman }
1462c37256eSGarrett Wollman 
1472c37256eSGarrett Wollman /*
148a152f8a3SRobert Watson  * tcp_detach is called when the socket layer loses its final reference
149a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
150a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
151a152f8a3SRobert Watson  * inpcb state: time wait.
152c78cbc7bSRobert Watson  *
153a152f8a3SRobert Watson  * This function can probably be re-absorbed back into tcp_usr_detach() now
154a152f8a3SRobert Watson  * that there is a single detach path.
1552c37256eSGarrett Wollman  */
156bc725eafSRobert Watson static void
157c78cbc7bSRobert Watson tcp_detach(struct socket *so, struct inpcb *inp)
1582c37256eSGarrett Wollman {
1592c37256eSGarrett Wollman 	struct tcpcb *tp;
1608b615593SMarko Zec #ifdef INVARIANTS
1618b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
1628b615593SMarko Zec #endif
1632c37256eSGarrett Wollman 
164603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1658501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
166623dce13SRobert Watson 
167c78cbc7bSRobert Watson 	KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
168c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
169953b5606SRobert Watson 
170a152f8a3SRobert Watson 	tp = intotcpcb(inp);
171a152f8a3SRobert Watson 
172ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
173623dce13SRobert Watson 		/*
174a152f8a3SRobert Watson 		 * There are two cases to handle: one in which the time wait
175a152f8a3SRobert Watson 		 * state is being discarded (INP_DROPPED), and one in which
176a152f8a3SRobert Watson 		 * this connection will remain in timewait.  In the former,
177a152f8a3SRobert Watson 		 * it is time to discard all state (except tcptw, which has
178a152f8a3SRobert Watson 		 * already been discarded by the timewait close code, which
179a152f8a3SRobert Watson 		 * should be further up the call stack somewhere).  In the
180a152f8a3SRobert Watson 		 * latter case, we detach from the socket, but leave the pcb
181a152f8a3SRobert Watson 		 * present until timewait ends.
182623dce13SRobert Watson 		 *
183a152f8a3SRobert Watson 		 * XXXRW: Would it be cleaner to free the tcptw here?
184623dce13SRobert Watson 		 */
185ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED) {
186a152f8a3SRobert Watson 			KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
187a152f8a3SRobert Watson 			    "INP_DROPPED && tp != NULL"));
188623dce13SRobert Watson 			in_pcbdetach(inp);
1890206cdb8SBjoern A. Zeeb 			in_pcbfree(inp);
1900206cdb8SBjoern A. Zeeb 		} else {
191623dce13SRobert Watson 			in_pcbdetach(inp);
1928501a69cSRobert Watson 			INP_WUNLOCK(inp);
193623dce13SRobert Watson 		}
194623dce13SRobert Watson 	} else {
195e6e65783SRobert Watson 		/*
196a152f8a3SRobert Watson 		 * If the connection is not in timewait, we consider two
197a152f8a3SRobert Watson 		 * two conditions: one in which no further processing is
198a152f8a3SRobert Watson 		 * necessary (dropped || embryonic), and one in which TCP is
199a152f8a3SRobert Watson 		 * not yet done, but no longer requires the socket, so the
200a152f8a3SRobert Watson 		 * pcb will persist for the time being.
201a152f8a3SRobert Watson 		 *
202a152f8a3SRobert Watson 		 * XXXRW: Does the second case still occur?
203e6e65783SRobert Watson 		 */
204ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED ||
205623dce13SRobert Watson 		    tp->t_state < TCPS_SYN_SENT) {
206623dce13SRobert Watson 			tcp_discardcb(tp);
207623dce13SRobert Watson 			in_pcbdetach(inp);
2080206cdb8SBjoern A. Zeeb 			in_pcbfree(inp);
2096aee2fc5SBjoern A. Zeeb 		} else
210a152f8a3SRobert Watson 			in_pcbdetach(inp);
211623dce13SRobert Watson 	}
212623dce13SRobert Watson }
213c78cbc7bSRobert Watson 
214c78cbc7bSRobert Watson /*
215c78cbc7bSRobert Watson  * pru_detach() detaches the TCP protocol from the socket.
216c78cbc7bSRobert Watson  * If the protocol state is non-embryonic, then can't
217c78cbc7bSRobert Watson  * do this directly: have to initiate a pru_disconnect(),
218c78cbc7bSRobert Watson  * which may finish later; embryonic TCB's can just
219c78cbc7bSRobert Watson  * be discarded here.
220c78cbc7bSRobert Watson  */
221c78cbc7bSRobert Watson static void
222c78cbc7bSRobert Watson tcp_usr_detach(struct socket *so)
223c78cbc7bSRobert Watson {
2248b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
225c78cbc7bSRobert Watson 	struct inpcb *inp;
226c78cbc7bSRobert Watson 
227c78cbc7bSRobert Watson 	inp = sotoinpcb(so);
228c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
229603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
2308501a69cSRobert Watson 	INP_WLOCK(inp);
231c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
232c78cbc7bSRobert Watson 	    ("tcp_usr_detach: inp_socket == NULL"));
233c78cbc7bSRobert Watson 	tcp_detach(so, inp);
234603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
2352c37256eSGarrett Wollman }
2362c37256eSGarrett Wollman 
2372c37256eSGarrett Wollman /*
2382c37256eSGarrett Wollman  * Give the socket an address.
2392c37256eSGarrett Wollman  */
2402c37256eSGarrett Wollman static int
241b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2422c37256eSGarrett Wollman {
2438b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
2442c37256eSGarrett Wollman 	int error = 0;
245f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
246623dce13SRobert Watson 	struct tcpcb *tp = NULL;
2472c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
2482c37256eSGarrett Wollman 
24952710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
25052710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sinp))
25152710de1SPawel Jakub Dawidek 		return (EINVAL);
2522c37256eSGarrett Wollman 	/*
2532c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2542c37256eSGarrett Wollman 	 * to them.
2552c37256eSGarrett Wollman 	 */
2562c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
25752710de1SPawel Jakub Dawidek 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
25852710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
25952710de1SPawel Jakub Dawidek 
260623dce13SRobert Watson 	TCPDEBUG0;
261603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
262623dce13SRobert Watson 	inp = sotoinpcb(so);
263623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
2648501a69cSRobert Watson 	INP_WLOCK(inp);
265ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
266623dce13SRobert Watson 		error = EINVAL;
2672c37256eSGarrett Wollman 		goto out;
268623dce13SRobert Watson 	}
269623dce13SRobert Watson 	tp = intotcpcb(inp);
270623dce13SRobert Watson 	TCPDEBUG1();
271623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
272623dce13SRobert Watson out:
273623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
2748501a69cSRobert Watson 	INP_WUNLOCK(inp);
275603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
276623dce13SRobert Watson 
277623dce13SRobert Watson 	return (error);
2782c37256eSGarrett Wollman }
2792c37256eSGarrett Wollman 
280fb59c426SYoshinobu Inoue #ifdef INET6
281fb59c426SYoshinobu Inoue static int
282b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
283fb59c426SYoshinobu Inoue {
2848b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
285fb59c426SYoshinobu Inoue 	int error = 0;
286f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
287623dce13SRobert Watson 	struct tcpcb *tp = NULL;
288fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
289fb59c426SYoshinobu Inoue 
29052710de1SPawel Jakub Dawidek 	sin6p = (struct sockaddr_in6 *)nam;
29152710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sin6p))
29252710de1SPawel Jakub Dawidek 		return (EINVAL);
293fb59c426SYoshinobu Inoue 	/*
294fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
295fb59c426SYoshinobu Inoue 	 * to them.
296fb59c426SYoshinobu Inoue 	 */
297fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6 &&
29852710de1SPawel Jakub Dawidek 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
29952710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
30052710de1SPawel Jakub Dawidek 
301623dce13SRobert Watson 	TCPDEBUG0;
302603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
303623dce13SRobert Watson 	inp = sotoinpcb(so);
304623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
3058501a69cSRobert Watson 	INP_WLOCK(inp);
306ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
307623dce13SRobert Watson 		error = EINVAL;
308623dce13SRobert Watson 		goto out;
309623dce13SRobert Watson 	}
310623dce13SRobert Watson 	tp = intotcpcb(inp);
311623dce13SRobert Watson 	TCPDEBUG1();
312fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
313fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
31466ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
315fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
316fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
317fb59c426SYoshinobu Inoue 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
318fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
319fb59c426SYoshinobu Inoue 
320fb59c426SYoshinobu Inoue 			in6_sin6_2_sin(&sin, sin6p);
321fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
322fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
323b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
324b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
325fb59c426SYoshinobu Inoue 			goto out;
326fb59c426SYoshinobu Inoue 		}
327fb59c426SYoshinobu Inoue 	}
328b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
329623dce13SRobert Watson out:
330623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3318501a69cSRobert Watson 	INP_WUNLOCK(inp);
332603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
333623dce13SRobert Watson 	return (error);
334fb59c426SYoshinobu Inoue }
335fb59c426SYoshinobu Inoue #endif /* INET6 */
336fb59c426SYoshinobu Inoue 
3372c37256eSGarrett Wollman /*
3382c37256eSGarrett Wollman  * Prepare to accept connections.
3392c37256eSGarrett Wollman  */
3402c37256eSGarrett Wollman static int
341d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
3422c37256eSGarrett Wollman {
3438b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
3442c37256eSGarrett Wollman 	int error = 0;
345f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
346623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3472c37256eSGarrett Wollman 
348623dce13SRobert Watson 	TCPDEBUG0;
349603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
350623dce13SRobert Watson 	inp = sotoinpcb(so);
351623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
3528501a69cSRobert Watson 	INP_WLOCK(inp);
353ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
354623dce13SRobert Watson 		error = EINVAL;
355623dce13SRobert Watson 		goto out;
356623dce13SRobert Watson 	}
357623dce13SRobert Watson 	tp = intotcpcb(inp);
358623dce13SRobert Watson 	TCPDEBUG1();
3590daccb9cSRobert Watson 	SOCK_LOCK(so);
3600daccb9cSRobert Watson 	error = solisten_proto_check(so);
3610daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0)
362b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
3630daccb9cSRobert Watson 	if (error == 0) {
3642c37256eSGarrett Wollman 		tp->t_state = TCPS_LISTEN;
365d374e81eSRobert Watson 		solisten_proto(so, backlog);
366bc65987aSKip Macy 		tcp_offload_listen_open(tp);
3670daccb9cSRobert Watson 	}
3680daccb9cSRobert Watson 	SOCK_UNLOCK(so);
369623dce13SRobert Watson 
370623dce13SRobert Watson out:
371623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
3728501a69cSRobert Watson 	INP_WUNLOCK(inp);
373603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
374623dce13SRobert Watson 	return (error);
3752c37256eSGarrett Wollman }
3762c37256eSGarrett Wollman 
377fb59c426SYoshinobu Inoue #ifdef INET6
378fb59c426SYoshinobu Inoue static int
379d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
380fb59c426SYoshinobu Inoue {
3818b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
382fb59c426SYoshinobu Inoue 	int error = 0;
383f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
384623dce13SRobert Watson 	struct tcpcb *tp = NULL;
385fb59c426SYoshinobu Inoue 
386623dce13SRobert Watson 	TCPDEBUG0;
387603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
388623dce13SRobert Watson 	inp = sotoinpcb(so);
389623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
3908501a69cSRobert Watson 	INP_WLOCK(inp);
391ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
392623dce13SRobert Watson 		error = EINVAL;
393623dce13SRobert Watson 		goto out;
394623dce13SRobert Watson 	}
395623dce13SRobert Watson 	tp = intotcpcb(inp);
396623dce13SRobert Watson 	TCPDEBUG1();
3970daccb9cSRobert Watson 	SOCK_LOCK(so);
3980daccb9cSRobert Watson 	error = solisten_proto_check(so);
3990daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0) {
400fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
40166ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
402fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
403b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
404fb59c426SYoshinobu Inoue 	}
4050daccb9cSRobert Watson 	if (error == 0) {
406fb59c426SYoshinobu Inoue 		tp->t_state = TCPS_LISTEN;
407d374e81eSRobert Watson 		solisten_proto(so, backlog);
4080daccb9cSRobert Watson 	}
4090daccb9cSRobert Watson 	SOCK_UNLOCK(so);
410623dce13SRobert Watson 
411623dce13SRobert Watson out:
412623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4138501a69cSRobert Watson 	INP_WUNLOCK(inp);
414603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
415623dce13SRobert Watson 	return (error);
416fb59c426SYoshinobu Inoue }
417fb59c426SYoshinobu Inoue #endif /* INET6 */
418fb59c426SYoshinobu Inoue 
4192c37256eSGarrett Wollman /*
4202c37256eSGarrett Wollman  * Initiate connection to peer.
4212c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
4222c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
4232c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
4242c37256eSGarrett Wollman  * Send initial segment on connection.
4252c37256eSGarrett Wollman  */
4262c37256eSGarrett Wollman static int
427b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
4282c37256eSGarrett Wollman {
4298b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
4302c37256eSGarrett Wollman 	int error = 0;
431f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
432623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4332c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
4342c37256eSGarrett Wollman 
43557bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
436e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
437e29ef13fSDon Lewis 		return (EINVAL);
43852710de1SPawel Jakub Dawidek 	/*
43952710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
44052710de1SPawel Jakub Dawidek 	 */
4412c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
44252710de1SPawel Jakub Dawidek 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
44352710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
444b89e82ddSJamie Gritton 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
445b89e82ddSJamie Gritton 		return (error);
44675c13541SPoul-Henning Kamp 
447623dce13SRobert Watson 	TCPDEBUG0;
448603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
449623dce13SRobert Watson 	inp = sotoinpcb(so);
450623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
4518501a69cSRobert Watson 	INP_WLOCK(inp);
452ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
453623dce13SRobert Watson 		error = EINVAL;
454623dce13SRobert Watson 		goto out;
455623dce13SRobert Watson 	}
456623dce13SRobert Watson 	tp = intotcpcb(inp);
457623dce13SRobert Watson 	TCPDEBUG1();
458b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
4592c37256eSGarrett Wollman 		goto out;
460bc65987aSKip Macy 	error = tcp_output_connect(so, nam);
461623dce13SRobert Watson out:
462623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
4638501a69cSRobert Watson 	INP_WUNLOCK(inp);
464603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
465623dce13SRobert Watson 	return (error);
4662c37256eSGarrett Wollman }
4672c37256eSGarrett Wollman 
468fb59c426SYoshinobu Inoue #ifdef INET6
469fb59c426SYoshinobu Inoue static int
470b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
471fb59c426SYoshinobu Inoue {
4728b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
473fb59c426SYoshinobu Inoue 	int error = 0;
474f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
475623dce13SRobert Watson 	struct tcpcb *tp = NULL;
476fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
477623dce13SRobert Watson 
478623dce13SRobert Watson 	TCPDEBUG0;
479fb59c426SYoshinobu Inoue 
480fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
481e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sin6p))
482e29ef13fSDon Lewis 		return (EINVAL);
48352710de1SPawel Jakub Dawidek 	/*
48452710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
48552710de1SPawel Jakub Dawidek 	 */
486fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6
48752710de1SPawel Jakub Dawidek 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
48852710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
489fb59c426SYoshinobu Inoue 
490603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
491623dce13SRobert Watson 	inp = sotoinpcb(so);
492623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
4938501a69cSRobert Watson 	INP_WLOCK(inp);
494ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
495623dce13SRobert Watson 		error = EINVAL;
496623dce13SRobert Watson 		goto out;
497623dce13SRobert Watson 	}
498623dce13SRobert Watson 	tp = intotcpcb(inp);
499623dce13SRobert Watson 	TCPDEBUG1();
50033841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
501fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
502fb59c426SYoshinobu Inoue 
503d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
504d46a5312SMaxim Konovalov 			error = EINVAL;
505d46a5312SMaxim Konovalov 			goto out;
506d46a5312SMaxim Konovalov 		}
50733841545SHajimu UMEMOTO 
508fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
509fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
510fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
511b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
512b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
513413628a7SBjoern A. Zeeb 			goto out;
514b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
515fb59c426SYoshinobu Inoue 			goto out;
516bc65987aSKip Macy 		error = tcp_output_connect(so, nam);
517fb59c426SYoshinobu Inoue 		goto out;
518fb59c426SYoshinobu Inoue 	}
519fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
520fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
521dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
522b89e82ddSJamie Gritton 	if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
523413628a7SBjoern A. Zeeb 		goto out;
524b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
525fb59c426SYoshinobu Inoue 		goto out;
526bc65987aSKip Macy 	error = tcp_output_connect(so, nam);
527623dce13SRobert Watson 
528623dce13SRobert Watson out:
529623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
5308501a69cSRobert Watson 	INP_WUNLOCK(inp);
531603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
532623dce13SRobert Watson 	return (error);
533fb59c426SYoshinobu Inoue }
534fb59c426SYoshinobu Inoue #endif /* INET6 */
535fb59c426SYoshinobu Inoue 
5362c37256eSGarrett Wollman /*
5372c37256eSGarrett Wollman  * Initiate disconnect from peer.
5382c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
5392c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
5402c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
5412c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
5422c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
5432c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
5442c37256eSGarrett Wollman  *
5452c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
5462c37256eSGarrett Wollman  */
5472c37256eSGarrett Wollman static int
5482c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
5492c37256eSGarrett Wollman {
5508b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
551f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
552623dce13SRobert Watson 	struct tcpcb *tp = NULL;
553623dce13SRobert Watson 	int error = 0;
5542c37256eSGarrett Wollman 
555623dce13SRobert Watson 	TCPDEBUG0;
556603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
557623dce13SRobert Watson 	inp = sotoinpcb(so);
558623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
5598501a69cSRobert Watson 	INP_WLOCK(inp);
560ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
56121367f63SSam Leffler 		error = ECONNRESET;
562623dce13SRobert Watson 		goto out;
563623dce13SRobert Watson 	}
564623dce13SRobert Watson 	tp = intotcpcb(inp);
565623dce13SRobert Watson 	TCPDEBUG1();
566623dce13SRobert Watson 	tcp_disconnect(tp);
567623dce13SRobert Watson out:
568623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
5698501a69cSRobert Watson 	INP_WUNLOCK(inp);
570603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
571623dce13SRobert Watson 	return (error);
5722c37256eSGarrett Wollman }
5732c37256eSGarrett Wollman 
5742c37256eSGarrett Wollman /*
5752c37256eSGarrett Wollman  * Accept a connection.  Essentially all the work is
5762c37256eSGarrett Wollman  * done at higher levels; just return the address
5772c37256eSGarrett Wollman  * of the peer, storing through addr.
5782c37256eSGarrett Wollman  */
5792c37256eSGarrett Wollman static int
58057bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
5812c37256eSGarrett Wollman {
5828b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
5832c37256eSGarrett Wollman 	int error = 0;
584f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
5851db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
58626ef6ac4SDon Lewis 	struct in_addr addr;
58726ef6ac4SDon Lewis 	in_port_t port = 0;
5881db24ffbSJonathan Lemon 	TCPDEBUG0;
5892c37256eSGarrett Wollman 
5903d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
5913d2d3ef4SRobert Watson 		return (ECONNABORTED);
592f76fcf6dSJeffrey Hsu 
593f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
594623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
595603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_tcbinfo);
5968501a69cSRobert Watson 	INP_WLOCK(inp);
597ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
5983d2d3ef4SRobert Watson 		error = ECONNABORTED;
599623dce13SRobert Watson 		goto out;
600623dce13SRobert Watson 	}
6011db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
6021db24ffbSJonathan Lemon 	TCPDEBUG1();
603f76fcf6dSJeffrey Hsu 
604f76fcf6dSJeffrey Hsu 	/*
60554d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
60626ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
60726ef6ac4SDon Lewis 	 * release the lock.
608f76fcf6dSJeffrey Hsu 	 */
60926ef6ac4SDon Lewis 	port = inp->inp_fport;
61026ef6ac4SDon Lewis 	addr = inp->inp_faddr;
611f76fcf6dSJeffrey Hsu 
612623dce13SRobert Watson out:
613623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
6148501a69cSRobert Watson 	INP_WUNLOCK(inp);
615603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_tcbinfo);
61626ef6ac4SDon Lewis 	if (error == 0)
61726ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
61826ef6ac4SDon Lewis 	return error;
6192c37256eSGarrett Wollman }
6202c37256eSGarrett Wollman 
621fb59c426SYoshinobu Inoue #ifdef INET6
622fb59c426SYoshinobu Inoue static int
623fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
624fb59c426SYoshinobu Inoue {
625f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
626fb59c426SYoshinobu Inoue 	int error = 0;
6271db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
62826ef6ac4SDon Lewis 	struct in_addr addr;
62926ef6ac4SDon Lewis 	struct in6_addr addr6;
63026ef6ac4SDon Lewis 	in_port_t port = 0;
63126ef6ac4SDon Lewis 	int v4 = 0;
6321db24ffbSJonathan Lemon 	TCPDEBUG0;
633fb59c426SYoshinobu Inoue 
634b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
635b4470c16SRobert Watson 		return (ECONNABORTED);
636f76fcf6dSJeffrey Hsu 
637f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
638623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
6398501a69cSRobert Watson 	INP_WLOCK(inp);
640ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
64121367f63SSam Leffler 		error = ECONNABORTED;
642623dce13SRobert Watson 		goto out;
643623dce13SRobert Watson 	}
6441db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
6451db24ffbSJonathan Lemon 	TCPDEBUG1();
646623dce13SRobert Watson 
64726ef6ac4SDon Lewis 	/*
64826ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
64926ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
65026ef6ac4SDon Lewis 	 * release the lock.
65126ef6ac4SDon Lewis 	 */
65226ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
65326ef6ac4SDon Lewis 		v4 = 1;
65426ef6ac4SDon Lewis 		port = inp->inp_fport;
65526ef6ac4SDon Lewis 		addr = inp->inp_faddr;
65626ef6ac4SDon Lewis 	} else {
65726ef6ac4SDon Lewis 		port = inp->inp_fport;
65826ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
65926ef6ac4SDon Lewis 	}
66026ef6ac4SDon Lewis 
661623dce13SRobert Watson out:
662623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
6638501a69cSRobert Watson 	INP_WUNLOCK(inp);
66426ef6ac4SDon Lewis 	if (error == 0) {
66526ef6ac4SDon Lewis 		if (v4)
66626ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
66726ef6ac4SDon Lewis 		else
66826ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
66926ef6ac4SDon Lewis 	}
67026ef6ac4SDon Lewis 	return error;
671fb59c426SYoshinobu Inoue }
672fb59c426SYoshinobu Inoue #endif /* INET6 */
673f76fcf6dSJeffrey Hsu 
674f76fcf6dSJeffrey Hsu /*
6752c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
6762c37256eSGarrett Wollman  */
6772c37256eSGarrett Wollman static int
6782c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
6792c37256eSGarrett Wollman {
6808b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
6812c37256eSGarrett Wollman 	int error = 0;
682f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
683623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6842c37256eSGarrett Wollman 
685623dce13SRobert Watson 	TCPDEBUG0;
686603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
687623dce13SRobert Watson 	inp = sotoinpcb(so);
688623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
6898501a69cSRobert Watson 	INP_WLOCK(inp);
690ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
69121367f63SSam Leffler 		error = ECONNRESET;
692623dce13SRobert Watson 		goto out;
693623dce13SRobert Watson 	}
694623dce13SRobert Watson 	tp = intotcpcb(inp);
695623dce13SRobert Watson 	TCPDEBUG1();
6962c37256eSGarrett Wollman 	socantsendmore(so);
697623dce13SRobert Watson 	tcp_usrclosed(tp);
698ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
699bc65987aSKip Macy 		error = tcp_output_disconnect(tp);
700623dce13SRobert Watson 
701623dce13SRobert Watson out:
702623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
7038501a69cSRobert Watson 	INP_WUNLOCK(inp);
704603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
705623dce13SRobert Watson 
706623dce13SRobert Watson 	return (error);
7072c37256eSGarrett Wollman }
7082c37256eSGarrett Wollman 
7092c37256eSGarrett Wollman /*
7102c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
7112c37256eSGarrett Wollman  */
7122c37256eSGarrett Wollman static int
7132c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
7142c37256eSGarrett Wollman {
715f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
716623dce13SRobert Watson 	struct tcpcb *tp = NULL;
717623dce13SRobert Watson 	int error = 0;
7182c37256eSGarrett Wollman 
719623dce13SRobert Watson 	TCPDEBUG0;
720623dce13SRobert Watson 	inp = sotoinpcb(so);
721623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
7228501a69cSRobert Watson 	INP_WLOCK(inp);
723ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
72421367f63SSam Leffler 		error = ECONNRESET;
725623dce13SRobert Watson 		goto out;
726623dce13SRobert Watson 	}
727623dce13SRobert Watson 	tp = intotcpcb(inp);
728623dce13SRobert Watson 	TCPDEBUG1();
729bc65987aSKip Macy 	tcp_output_rcvd(tp);
730623dce13SRobert Watson 
731623dce13SRobert Watson out:
732623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
7338501a69cSRobert Watson 	INP_WUNLOCK(inp);
734623dce13SRobert Watson 	return (error);
7352c37256eSGarrett Wollman }
7362c37256eSGarrett Wollman 
7372c37256eSGarrett Wollman /*
7382c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
7399c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
7409c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
7419c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
7429c9906e9SPeter Wemm  * generally are caller-frees.
7432c37256eSGarrett Wollman  */
7442c37256eSGarrett Wollman static int
74557bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
746b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
7472c37256eSGarrett Wollman {
7488b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
7492c37256eSGarrett Wollman 	int error = 0;
750f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
751623dce13SRobert Watson 	struct tcpcb *tp = NULL;
752623dce13SRobert Watson 	int headlocked = 0;
753fb59c426SYoshinobu Inoue #ifdef INET6
754fb59c426SYoshinobu Inoue 	int isipv6;
755fb59c426SYoshinobu Inoue #endif
7569c9906e9SPeter Wemm 	TCPDEBUG0;
7572c37256eSGarrett Wollman 
758f76fcf6dSJeffrey Hsu 	/*
759623dce13SRobert Watson 	 * We require the pcbinfo lock in two cases:
760623dce13SRobert Watson 	 *
761623dce13SRobert Watson 	 * (1) An implied connect is taking place, which can result in
762623dce13SRobert Watson 	 *     binding IPs and ports and hence modification of the pcb hash
763623dce13SRobert Watson 	 *     chains.
764623dce13SRobert Watson 	 *
765623dce13SRobert Watson 	 * (2) PRUS_EOF is set, resulting in explicit close on the send.
766f76fcf6dSJeffrey Hsu 	 */
767623dce13SRobert Watson 	if ((nam != NULL) || (flags & PRUS_EOF)) {
768603724d3SBjoern A. Zeeb 		INP_INFO_WLOCK(&V_tcbinfo);
769623dce13SRobert Watson 		headlocked = 1;
770623dce13SRobert Watson 	}
771f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
772623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
7738501a69cSRobert Watson 	INP_WLOCK(inp);
774ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
7757ff0b850SAndre Oppermann 		if (control)
7767ff0b850SAndre Oppermann 			m_freem(control);
7777ff0b850SAndre Oppermann 		if (m)
7787ff0b850SAndre Oppermann 			m_freem(m);
77921367f63SSam Leffler 		error = ECONNRESET;
7809c9906e9SPeter Wemm 		goto out;
7819c9906e9SPeter Wemm 	}
782fb59c426SYoshinobu Inoue #ifdef INET6
783fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
784fb59c426SYoshinobu Inoue #endif /* INET6 */
7859c9906e9SPeter Wemm 	tp = intotcpcb(inp);
7869c9906e9SPeter Wemm 	TCPDEBUG1();
7879c9906e9SPeter Wemm 	if (control) {
7889c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
7899c9906e9SPeter Wemm 		if (control->m_len) {
7909c9906e9SPeter Wemm 			m_freem(control);
7912c37256eSGarrett Wollman 			if (m)
7922c37256eSGarrett Wollman 				m_freem(m);
793744f87eaSDavid Greenman 			error = EINVAL;
794744f87eaSDavid Greenman 			goto out;
7952c37256eSGarrett Wollman 		}
7969c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
7979c9906e9SPeter Wemm 	}
7982c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
799395bb186SSam Leffler 		sbappendstream(&so->so_snd, m);
8002c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
8012c37256eSGarrett Wollman 			/*
8022c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
8032c37256eSGarrett Wollman 			 * initialize window to default value, and
8042c37256eSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
8052c37256eSGarrett Wollman 			 * MSS.
8062c37256eSGarrett Wollman 			 */
807603724d3SBjoern A. Zeeb 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
808fb59c426SYoshinobu Inoue #ifdef INET6
809fb59c426SYoshinobu Inoue 			if (isipv6)
810b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
811fb59c426SYoshinobu Inoue 			else
812fb59c426SYoshinobu Inoue #endif /* INET6 */
813b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
8142c37256eSGarrett Wollman 			if (error)
8152c37256eSGarrett Wollman 				goto out;
8162c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
8172c37256eSGarrett Wollman 			tcp_mss(tp, -1);
8182c37256eSGarrett Wollman 		}
8192c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
8202c37256eSGarrett Wollman 			/*
8212c37256eSGarrett Wollman 			 * Close the send side of the connection after
8222c37256eSGarrett Wollman 			 * the data is sent.
8232c37256eSGarrett Wollman 			 */
824603724d3SBjoern A. Zeeb 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
8252c37256eSGarrett Wollman 			socantsendmore(so);
826623dce13SRobert Watson 			tcp_usrclosed(tp);
8272c37256eSGarrett Wollman 		}
828623dce13SRobert Watson 		if (headlocked) {
829603724d3SBjoern A. Zeeb 			INP_INFO_WUNLOCK(&V_tcbinfo);
830623dce13SRobert Watson 			headlocked = 0;
831623dce13SRobert Watson 		}
832ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED)) {
833b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
834b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
835bc65987aSKip Macy 			error = tcp_output_send(tp);
836b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
837b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
838b0acefa8SBill Fenner 		}
8392c37256eSGarrett Wollman 	} else {
840623dce13SRobert Watson 		/*
841623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
842623dce13SRobert Watson 		 */
843d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
8442c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
845d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
8462c37256eSGarrett Wollman 			m_freem(m);
8472c37256eSGarrett Wollman 			error = ENOBUFS;
8482c37256eSGarrett Wollman 			goto out;
8492c37256eSGarrett Wollman 		}
8502c37256eSGarrett Wollman 		/*
8512c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
8522c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
8532c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
8542c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
8552c37256eSGarrett Wollman 		 * of data past the urgent section.
8562c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
8572c37256eSGarrett Wollman 		 */
858d2bc35abSRobert Watson 		sbappendstream_locked(&so->so_snd, m);
859d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
860ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
861ef53690bSGarrett Wollman 			/*
862ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
863ef53690bSGarrett Wollman 			 * initialize window to default value, and
864ef53690bSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
865ef53690bSGarrett Wollman 			 * MSS.
866ef53690bSGarrett Wollman 			 */
867603724d3SBjoern A. Zeeb 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
868fb59c426SYoshinobu Inoue #ifdef INET6
869fb59c426SYoshinobu Inoue 			if (isipv6)
870b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
871fb59c426SYoshinobu Inoue 			else
872fb59c426SYoshinobu Inoue #endif /* INET6 */
873b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
874ef53690bSGarrett Wollman 			if (error)
875ef53690bSGarrett Wollman 				goto out;
876ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
877ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
878603724d3SBjoern A. Zeeb 			INP_INFO_WUNLOCK(&V_tcbinfo);
879623dce13SRobert Watson 			headlocked = 0;
880623dce13SRobert Watson 		} else if (nam) {
881603724d3SBjoern A. Zeeb 			INP_INFO_WUNLOCK(&V_tcbinfo);
882623dce13SRobert Watson 			headlocked = 0;
883623dce13SRobert Watson 		}
8842c37256eSGarrett Wollman 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
8852cdbfa66SPaul Saab 		tp->t_flags |= TF_FORCEDATA;
886bc65987aSKip Macy 		error = tcp_output_send(tp);
8872cdbfa66SPaul Saab 		tp->t_flags &= ~TF_FORCEDATA;
8882c37256eSGarrett Wollman 	}
889d1401c90SRobert Watson out:
890d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
8912c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
8928501a69cSRobert Watson 	INP_WUNLOCK(inp);
893623dce13SRobert Watson 	if (headlocked)
894603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
89573fddedaSPeter Grehan 	return (error);
8962c37256eSGarrett Wollman }
8972c37256eSGarrett Wollman 
8982c37256eSGarrett Wollman /*
899a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
9002c37256eSGarrett Wollman  */
901ac45e92fSRobert Watson static void
9022c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
9032c37256eSGarrett Wollman {
9048b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
905f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
906a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
907623dce13SRobert Watson 	TCPDEBUG0;
908c78cbc7bSRobert Watson 
909ac45e92fSRobert Watson 	inp = sotoinpcb(so);
910c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
911c78cbc7bSRobert Watson 
912603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
9138501a69cSRobert Watson 	INP_WLOCK(inp);
914c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
915c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
916c78cbc7bSRobert Watson 
917c78cbc7bSRobert Watson 	/*
918a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
919c78cbc7bSRobert Watson 	 */
920ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
921ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
922c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
923a152f8a3SRobert Watson 		TCPDEBUG1();
924c78cbc7bSRobert Watson 		tcp_drop(tp, ECONNABORTED);
925a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
926c78cbc7bSRobert Watson 	}
927ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
928a152f8a3SRobert Watson 		SOCK_LOCK(so);
929a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
930a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
931ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
932a152f8a3SRobert Watson 	}
9338501a69cSRobert Watson 	INP_WUNLOCK(inp);
934603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
935a152f8a3SRobert Watson }
936a152f8a3SRobert Watson 
937a152f8a3SRobert Watson /*
938a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
939a152f8a3SRobert Watson  */
940a152f8a3SRobert Watson static void
941a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
942a152f8a3SRobert Watson {
9438b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
944a152f8a3SRobert Watson 	struct inpcb *inp;
945a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
946a152f8a3SRobert Watson 	TCPDEBUG0;
947a152f8a3SRobert Watson 
948a152f8a3SRobert Watson 	inp = sotoinpcb(so);
949a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
950a152f8a3SRobert Watson 
951603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
9528501a69cSRobert Watson 	INP_WLOCK(inp);
953a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
954a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
955a152f8a3SRobert Watson 
956a152f8a3SRobert Watson 	/*
957a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
958a152f8a3SRobert Watson 	 * a disconnect.
959a152f8a3SRobert Watson 	 */
960ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
961ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
962a152f8a3SRobert Watson 		tp = intotcpcb(inp);
963a152f8a3SRobert Watson 		TCPDEBUG1();
964a152f8a3SRobert Watson 		tcp_disconnect(tp);
965a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
966a152f8a3SRobert Watson 	}
967ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
968a152f8a3SRobert Watson 		SOCK_LOCK(so);
969a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
970a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
971ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
972a152f8a3SRobert Watson 	}
9738501a69cSRobert Watson 	INP_WUNLOCK(inp);
974603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
9752c37256eSGarrett Wollman }
9762c37256eSGarrett Wollman 
9772c37256eSGarrett Wollman /*
9782c37256eSGarrett Wollman  * Receive out-of-band data.
9792c37256eSGarrett Wollman  */
9802c37256eSGarrett Wollman static int
9812c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
9822c37256eSGarrett Wollman {
9832c37256eSGarrett Wollman 	int error = 0;
984f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
985623dce13SRobert Watson 	struct tcpcb *tp = NULL;
9862c37256eSGarrett Wollman 
987623dce13SRobert Watson 	TCPDEBUG0;
988623dce13SRobert Watson 	inp = sotoinpcb(so);
989623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
9908501a69cSRobert Watson 	INP_WLOCK(inp);
991ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
99221367f63SSam Leffler 		error = ECONNRESET;
993623dce13SRobert Watson 		goto out;
994623dce13SRobert Watson 	}
995623dce13SRobert Watson 	tp = intotcpcb(inp);
996623dce13SRobert Watson 	TCPDEBUG1();
9972c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
998c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
9994cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
10004cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
10012c37256eSGarrett Wollman 		error = EINVAL;
10022c37256eSGarrett Wollman 		goto out;
10032c37256eSGarrett Wollman 	}
10042c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
10052c37256eSGarrett Wollman 		error = EWOULDBLOCK;
10062c37256eSGarrett Wollman 		goto out;
10072c37256eSGarrett Wollman 	}
10082c37256eSGarrett Wollman 	m->m_len = 1;
10092c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
10102c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
10112c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1012623dce13SRobert Watson 
1013623dce13SRobert Watson out:
1014623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
10158501a69cSRobert Watson 	INP_WUNLOCK(inp);
1016623dce13SRobert Watson 	return (error);
10172c37256eSGarrett Wollman }
10182c37256eSGarrett Wollman 
10192c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1020756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1021756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1022756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1023756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1024756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1025756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1026756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1027756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1028756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
102954d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1030756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1031756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1032756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
1033756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
103454d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1035a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1036a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
10372c37256eSGarrett Wollman };
1038df8bae1dSRodney W. Grimes 
1039fb59c426SYoshinobu Inoue #ifdef INET6
1040fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1041756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1042756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1043756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1044756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1045756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1046756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1047756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1048756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1049756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1050756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1051756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1052756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1053756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
1054756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1055756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1056a152f8a3SRobert Watson  	.pru_sosetlabel =	in_pcbsosetlabel,
1057a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1058fb59c426SYoshinobu Inoue };
1059fb59c426SYoshinobu Inoue #endif /* INET6 */
1060fb59c426SYoshinobu Inoue 
1061a0292f23SGarrett Wollman /*
1062a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1063a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
10645200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
10655200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
10665200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
10675200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1068a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1069a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1070a0292f23SGarrett Wollman  */
10710312fbe9SPoul-Henning Kamp static int
1072ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1073a0292f23SGarrett Wollman {
1074a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1075a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
10768b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
10775200e00eSIan Dowse 	struct in_addr laddr;
10785200e00eSIan Dowse 	u_short lport;
1079c3229e05SDavid Greenman 	int error;
1080a0292f23SGarrett Wollman 
1081603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
10828501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1083623dce13SRobert Watson 
1084a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
1085b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1086a0292f23SGarrett Wollman 		if (error)
1087a0292f23SGarrett Wollman 			return error;
1088a0292f23SGarrett Wollman 	}
1089a0292f23SGarrett Wollman 
1090a0292f23SGarrett Wollman 	/*
1091a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1092a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1093a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1094a0292f23SGarrett Wollman 	 */
10955200e00eSIan Dowse 	laddr = inp->inp_laddr;
10965200e00eSIan Dowse 	lport = inp->inp_lport;
10975200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1098b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
10995200e00eSIan Dowse 	if (error && oinp == NULL)
1100d3628763SRodney W. Grimes 		return error;
1101c94c54e4SAndre Oppermann 	if (oinp)
1102a0292f23SGarrett Wollman 		return EADDRINUSE;
11035200e00eSIan Dowse 	inp->inp_laddr = laddr;
110415bd2b43SDavid Greenman 	in_pcbrehash(inp);
1105a0292f23SGarrett Wollman 
1106087b55eaSAndre Oppermann 	/*
1107087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1108087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1109087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1110087b55eaSAndre Oppermann 	 */
1111a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
11129b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1113a0292f23SGarrett Wollman 		tp->request_r_scale++;
1114a0292f23SGarrett Wollman 
1115a0292f23SGarrett Wollman 	soisconnecting(so);
111678b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
1117a0292f23SGarrett Wollman 	tp->t_state = TCPS_SYN_SENT;
1118b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1119b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
11201fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
1121a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1122a45d2726SAndras Olah 
1123a0292f23SGarrett Wollman 	return 0;
1124a0292f23SGarrett Wollman }
1125a0292f23SGarrett Wollman 
1126fb59c426SYoshinobu Inoue #ifdef INET6
1127fb59c426SYoshinobu Inoue static int
1128ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1129fb59c426SYoshinobu Inoue {
1130fb59c426SYoshinobu Inoue 	struct inpcb *inp = tp->t_inpcb, *oinp;
1131fb59c426SYoshinobu Inoue 	struct socket *so = inp->inp_socket;
11328b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
1133fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1134fb59c426SYoshinobu Inoue 	struct in6_addr *addr6;
1135fb59c426SYoshinobu Inoue 	int error;
1136fb59c426SYoshinobu Inoue 
1137603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
11388501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1139623dce13SRobert Watson 
1140fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
1141b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1142fb59c426SYoshinobu Inoue 		if (error)
1143fb59c426SYoshinobu Inoue 			return error;
1144fb59c426SYoshinobu Inoue 	}
1145fb59c426SYoshinobu Inoue 
1146fb59c426SYoshinobu Inoue 	/*
1147fb59c426SYoshinobu Inoue 	 * Cannot simply call in_pcbconnect, because there might be an
1148fb59c426SYoshinobu Inoue 	 * earlier incarnation of this same connection still in
1149fb59c426SYoshinobu Inoue 	 * TIME_WAIT state, creating an ADDRINUSE error.
1150a1f7e5f8SHajimu UMEMOTO 	 * in6_pcbladdr() also handles scope zone IDs.
1151fb59c426SYoshinobu Inoue 	 */
1152fb59c426SYoshinobu Inoue 	error = in6_pcbladdr(inp, nam, &addr6);
1153fb59c426SYoshinobu Inoue 	if (error)
1154fb59c426SYoshinobu Inoue 		return error;
1155fb59c426SYoshinobu Inoue 	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1156fb59c426SYoshinobu Inoue 				  &sin6->sin6_addr, sin6->sin6_port,
1157fb59c426SYoshinobu Inoue 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1158fb59c426SYoshinobu Inoue 				  ? addr6
1159fb59c426SYoshinobu Inoue 				  : &inp->in6p_laddr,
1160fb59c426SYoshinobu Inoue 				  inp->inp_lport,  0, NULL);
1161c94c54e4SAndre Oppermann 	if (oinp)
1162fb59c426SYoshinobu Inoue 		return EADDRINUSE;
1163fb59c426SYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1164fb59c426SYoshinobu Inoue 		inp->in6p_laddr = *addr6;
1165fb59c426SYoshinobu Inoue 	inp->in6p_faddr = sin6->sin6_addr;
1166fb59c426SYoshinobu Inoue 	inp->inp_fport = sin6->sin6_port;
11678a59da30SHajimu UMEMOTO 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
1168fc384fa5SBjoern A. Zeeb 	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
1169fc384fa5SBjoern A. Zeeb 	if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
1170fc384fa5SBjoern A. Zeeb 		inp->inp_flow |=
11718a59da30SHajimu UMEMOTO 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1172fb59c426SYoshinobu Inoue 	in_pcbrehash(inp);
1173fb59c426SYoshinobu Inoue 
1174fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1175fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1176970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1177fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1178fb59c426SYoshinobu Inoue 
1179fb59c426SYoshinobu Inoue 	soisconnecting(so);
118078b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
1181fb59c426SYoshinobu Inoue 	tp->t_state = TCPS_SYN_SENT;
1182b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1183b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
11841fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
1185fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1186fb59c426SYoshinobu Inoue 
1187fb59c426SYoshinobu Inoue 	return 0;
1188fb59c426SYoshinobu Inoue }
1189fb59c426SYoshinobu Inoue #endif /* INET6 */
1190fb59c426SYoshinobu Inoue 
1191cfe8b629SGarrett Wollman /*
1192b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1193b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1194b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1195b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1196b8af5dfaSRobert Watson  * from Linux.
1197b8af5dfaSRobert Watson  */
1198b8af5dfaSRobert Watson static void
1199ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1200b8af5dfaSRobert Watson {
1201b8af5dfaSRobert Watson 
12028501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1203b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1204b8af5dfaSRobert Watson 
1205b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1206b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1207b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
12083529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1209b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1210b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1211b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1212b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1213b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1214b8af5dfaSRobert Watson 	}
12151baaf834SBruce M Simpson 
12161baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
12171baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
12181baaf834SBruce M Simpson 
1219b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1220b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1221b8af5dfaSRobert Watson 
1222b8af5dfaSRobert Watson 	/*
1223b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1224b8af5dfaSRobert Watson 	 */
1225c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1226535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1227b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
1228b8af5dfaSRobert Watson 	ti->tcpi_snd_bwnd = tp->snd_bwnd;
1229535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
1230535fbad6SKip Macy 	ti->__tcpi_snd_mss = tp->t_maxseg;
1231535fbad6SKip Macy 	ti->__tcpi_rcv_mss = tp->t_maxseg;
1232535fbad6SKip Macy 	if (tp->t_flags & TF_TOE)
1233535fbad6SKip Macy 		ti->tcpi_options |= TCPI_OPT_TOE;
1234b8af5dfaSRobert Watson }
1235b8af5dfaSRobert Watson 
1236b8af5dfaSRobert Watson /*
12371e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
12381e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
12391e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
12401e8f5ffaSRobert Watson  * option.
1241cfe8b629SGarrett Wollman  */
12428501a69cSRobert Watson #define INP_WLOCK_RECHECK(inp) do {					\
12438501a69cSRobert Watson 	INP_WLOCK(inp);							\
1244ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
12458501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
12461e8f5ffaSRobert Watson 		return (ECONNRESET);					\
12471e8f5ffaSRobert Watson 	}								\
12481e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
12491e8f5ffaSRobert Watson } while(0)
12501e8f5ffaSRobert Watson 
1251df8bae1dSRodney W. Grimes int
1252ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1253df8bae1dSRodney W. Grimes {
12548b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
12553f9d1ef9SRobert Watson 	int	error, opt, optval;
1256df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1257cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
1258b8af5dfaSRobert Watson 	struct	tcp_info ti;
1259df8bae1dSRodney W. Grimes 
1260cfe8b629SGarrett Wollman 	error = 0;
1261df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1262623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
12638501a69cSRobert Watson 	INP_WLOCK(inp);
1264cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1265fb59c426SYoshinobu Inoue #ifdef INET6
12665cd54324SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6PROTO) {
12678501a69cSRobert Watson 			INP_WUNLOCK(inp);
1268fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
12691e8f5ffaSRobert Watson 		} else {
1270fb59c426SYoshinobu Inoue #endif /* INET6 */
12718501a69cSRobert Watson 			INP_WUNLOCK(inp);
1272cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
12731e8f5ffaSRobert Watson #ifdef INET6
12741e8f5ffaSRobert Watson 		}
12751e8f5ffaSRobert Watson #endif
1276df8bae1dSRodney W. Grimes 		return (error);
1277df8bae1dSRodney W. Grimes 	}
1278ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
12798501a69cSRobert Watson 		INP_WUNLOCK(inp);
12801e8f5ffaSRobert Watson 		return (ECONNRESET);
1281623dce13SRobert Watson 	}
1282df8bae1dSRodney W. Grimes 
1283cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1284cfe8b629SGarrett Wollman 	case SOPT_SET:
1285cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
12861cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
128788f6b043SBruce M Simpson 		case TCP_MD5SIG:
12888501a69cSRobert Watson 			INP_WUNLOCK(inp);
12891cfd4b53SBruce M Simpson 			error = sooptcopyin(sopt, &optval, sizeof optval,
12901cfd4b53SBruce M Simpson 			    sizeof optval);
12911cfd4b53SBruce M Simpson 			if (error)
12921e8f5ffaSRobert Watson 				return (error);
12931cfd4b53SBruce M Simpson 
12948501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
12951cfd4b53SBruce M Simpson 			if (optval > 0)
12961cfd4b53SBruce M Simpson 				tp->t_flags |= TF_SIGNATURE;
12971cfd4b53SBruce M Simpson 			else
12981cfd4b53SBruce M Simpson 				tp->t_flags &= ~TF_SIGNATURE;
12998501a69cSRobert Watson 			INP_WUNLOCK(inp);
13001cfd4b53SBruce M Simpson 			break;
13011cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
1302df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1303cfe8b629SGarrett Wollman 		case TCP_NOOPT:
13048501a69cSRobert Watson 			INP_WUNLOCK(inp);
1305cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1306cfe8b629SGarrett Wollman 			    sizeof optval);
1307cfe8b629SGarrett Wollman 			if (error)
13081e8f5ffaSRobert Watson 				return (error);
1309cfe8b629SGarrett Wollman 
13108501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1311cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1312cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1313cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1314cfe8b629SGarrett Wollman 				break;
1315cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1316cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1317cfe8b629SGarrett Wollman 				break;
1318cfe8b629SGarrett Wollman 			default:
1319cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1320cfe8b629SGarrett Wollman 				break;
1321cfe8b629SGarrett Wollman 			}
1322cfe8b629SGarrett Wollman 
1323cfe8b629SGarrett Wollman 			if (optval)
1324cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1325df8bae1dSRodney W. Grimes 			else
1326cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
13278501a69cSRobert Watson 			INP_WUNLOCK(inp);
1328df8bae1dSRodney W. Grimes 			break;
1329df8bae1dSRodney W. Grimes 
1330007581c0SJonathan Lemon 		case TCP_NOPUSH:
13318501a69cSRobert Watson 			INP_WUNLOCK(inp);
1332007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1333007581c0SJonathan Lemon 			    sizeof optval);
1334007581c0SJonathan Lemon 			if (error)
13351e8f5ffaSRobert Watson 				return (error);
1336007581c0SJonathan Lemon 
13378501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1338007581c0SJonathan Lemon 			if (optval)
1339007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1340007581c0SJonathan Lemon 			else {
1341007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1342007581c0SJonathan Lemon 				error = tcp_output(tp);
1343007581c0SJonathan Lemon 			}
13448501a69cSRobert Watson 			INP_WUNLOCK(inp);
1345007581c0SJonathan Lemon 			break;
1346007581c0SJonathan Lemon 
1347df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
13488501a69cSRobert Watson 			INP_WUNLOCK(inp);
1349cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1350cfe8b629SGarrett Wollman 			    sizeof optval);
1351cfe8b629SGarrett Wollman 			if (error)
13521e8f5ffaSRobert Watson 				return (error);
1353df8bae1dSRodney W. Grimes 
13548501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
135553369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
1356603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
1357cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1358a0292f23SGarrett Wollman 			else
1359a0292f23SGarrett Wollman 				error = EINVAL;
13608501a69cSRobert Watson 			INP_WUNLOCK(inp);
1361a0292f23SGarrett Wollman 			break;
1362a0292f23SGarrett Wollman 
1363b8af5dfaSRobert Watson 		case TCP_INFO:
13648501a69cSRobert Watson 			INP_WUNLOCK(inp);
1365b8af5dfaSRobert Watson 			error = EINVAL;
1366b8af5dfaSRobert Watson 			break;
1367b8af5dfaSRobert Watson 
1368df8bae1dSRodney W. Grimes 		default:
13698501a69cSRobert Watson 			INP_WUNLOCK(inp);
1370df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1371df8bae1dSRodney W. Grimes 			break;
1372df8bae1dSRodney W. Grimes 		}
1373df8bae1dSRodney W. Grimes 		break;
1374df8bae1dSRodney W. Grimes 
1375cfe8b629SGarrett Wollman 	case SOPT_GET:
13761e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
1377cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
13781cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
137988f6b043SBruce M Simpson 		case TCP_MD5SIG:
13801cfd4b53SBruce M Simpson 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
13818501a69cSRobert Watson 			INP_WUNLOCK(inp);
1382b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
13831cfd4b53SBruce M Simpson 			break;
1384265ed012SBruce M Simpson #endif
13851e8f5ffaSRobert Watson 
1386df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1387cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
13888501a69cSRobert Watson 			INP_WUNLOCK(inp);
1389b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1390df8bae1dSRodney W. Grimes 			break;
1391df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1392cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
13938501a69cSRobert Watson 			INP_WUNLOCK(inp);
1394b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1395df8bae1dSRodney W. Grimes 			break;
1396a0292f23SGarrett Wollman 		case TCP_NOOPT:
1397cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
13988501a69cSRobert Watson 			INP_WUNLOCK(inp);
1399b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1400a0292f23SGarrett Wollman 			break;
1401a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1402cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
14038501a69cSRobert Watson 			INP_WUNLOCK(inp);
1404b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1405b8af5dfaSRobert Watson 			break;
1406b8af5dfaSRobert Watson 		case TCP_INFO:
1407b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
14088501a69cSRobert Watson 			INP_WUNLOCK(inp);
1409b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
1410a0292f23SGarrett Wollman 			break;
1411df8bae1dSRodney W. Grimes 		default:
14128501a69cSRobert Watson 			INP_WUNLOCK(inp);
1413df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1414df8bae1dSRodney W. Grimes 			break;
1415df8bae1dSRodney W. Grimes 		}
1416df8bae1dSRodney W. Grimes 		break;
1417df8bae1dSRodney W. Grimes 	}
1418df8bae1dSRodney W. Grimes 	return (error);
1419df8bae1dSRodney W. Grimes }
14208501a69cSRobert Watson #undef INP_WLOCK_RECHECK
1421df8bae1dSRodney W. Grimes 
142226e30fbbSDavid Greenman /*
142326e30fbbSDavid Greenman  * tcp_sendspace and tcp_recvspace are the default send and receive window
142426e30fbbSDavid Greenman  * sizes, respectively.  These are obsolescent (this information should
142526e30fbbSDavid Greenman  * be set by the route).
142626e30fbbSDavid Greenman  */
142781e561cdSDavid E. O'Brien u_long	tcp_sendspace = 1024*32;
1428e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
14293d177f46SBill Fumerola     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
143081e561cdSDavid E. O'Brien u_long	tcp_recvspace = 1024*64;
1431e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
14323d177f46SBill Fumerola     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1433df8bae1dSRodney W. Grimes 
1434df8bae1dSRodney W. Grimes /*
1435df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1436df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1437df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1438df8bae1dSRodney W. Grimes  */
14390312fbe9SPoul-Henning Kamp static int
1440ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so)
1441df8bae1dSRodney W. Grimes {
14428b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
1443ad3f9ab3SAndre Oppermann 	struct tcpcb *tp;
1444df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1445df8bae1dSRodney W. Grimes 	int error;
1446df8bae1dSRodney W. Grimes 
1447df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1448df8bae1dSRodney W. Grimes 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1449df8bae1dSRodney W. Grimes 		if (error)
1450df8bae1dSRodney W. Grimes 			return (error);
1451df8bae1dSRodney W. Grimes 	}
14526741ecf5SAndre Oppermann 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
14536741ecf5SAndre Oppermann 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1454603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
1455603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_tcbinfo);
1456f2de87feSRobert Watson 	if (error) {
1457603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
1458df8bae1dSRodney W. Grimes 		return (error);
1459f2de87feSRobert Watson 	}
1460df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1461fb59c426SYoshinobu Inoue #ifdef INET6
14625cd54324SBjoern A. Zeeb 	if (inp->inp_vflag & INP_IPV6PROTO) {
1463fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1464fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1465fb59c426SYoshinobu Inoue 	}
1466fb59c426SYoshinobu Inoue 	else
1467fb59c426SYoshinobu Inoue #endif
1468cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1469df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1470623dce13SRobert Watson 	if (tp == NULL) {
1471df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
14720206cdb8SBjoern A. Zeeb 		in_pcbfree(inp);
1473603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
1474df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1475df8bae1dSRodney W. Grimes 	}
1476df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
14778501a69cSRobert Watson 	INP_WUNLOCK(inp);
1478603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
1479df8bae1dSRodney W. Grimes 	return (0);
1480df8bae1dSRodney W. Grimes }
1481df8bae1dSRodney W. Grimes 
1482df8bae1dSRodney W. Grimes /*
1483df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1484df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1485df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1486df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1487df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1488df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1489df8bae1dSRodney W. Grimes  */
1490623dce13SRobert Watson static void
1491ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
1492df8bae1dSRodney W. Grimes {
1493e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
1494e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
14958b615593SMarko Zec #ifdef INVARIANTS
14968b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
14978b615593SMarko Zec #endif
1498e6e0b5ffSRobert Watson 
1499603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
15008501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1501df8bae1dSRodney W. Grimes 
1502623dce13SRobert Watson 	/*
1503623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1504623dce13SRobert Watson 	 * socket is still open.
1505623dce13SRobert Watson 	 */
1506623dce13SRobert Watson 	if (tp->t_state < TCPS_ESTABLISHED) {
1507df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1508623dce13SRobert Watson 		KASSERT(tp != NULL,
1509623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
1510623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1511243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
1512623dce13SRobert Watson 		KASSERT(tp != NULL,
1513623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
1514623dce13SRobert Watson 	} else {
1515df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
1516df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
1517623dce13SRobert Watson 		tcp_usrclosed(tp);
1518ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
1519bc65987aSKip Macy 			tcp_output_disconnect(tp);
1520df8bae1dSRodney W. Grimes 	}
1521df8bae1dSRodney W. Grimes }
1522df8bae1dSRodney W. Grimes 
1523df8bae1dSRodney W. Grimes /*
1524df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1525df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1526df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1527df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1528df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1529df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1530df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1531df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1532df8bae1dSRodney W. Grimes  */
1533623dce13SRobert Watson static void
1534ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
1535df8bae1dSRodney W. Grimes {
15368b615593SMarko Zec #ifdef INVARIANTS
15378b615593SMarko Zec 	INIT_VNET_INET(tp->t_inpcb->inp_vnet);
15388b615593SMarko Zec #endif
1539df8bae1dSRodney W. Grimes 
1540603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
15418501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1542e6e0b5ffSRobert Watson 
1543df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1544df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
1545bc65987aSKip Macy 		tcp_offload_listen_close(tp);
1546bc65987aSKip Macy 		/* FALLTHROUGH */
1547bc65987aSKip Macy 	case TCPS_CLOSED:
1548df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_CLOSED;
1549df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1550623dce13SRobert Watson 		/*
1551623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
1552623dce13SRobert Watson 		 * still open.
1553623dce13SRobert Watson 		 */
1554623dce13SRobert Watson 		KASSERT(tp != NULL,
1555623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
1556df8bae1dSRodney W. Grimes 		break;
1557df8bae1dSRodney W. Grimes 
1558a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
1559df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1560a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
1561a0292f23SGarrett Wollman 		break;
1562a0292f23SGarrett Wollman 
1563df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1564df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_FIN_WAIT_1;
1565df8bae1dSRodney W. Grimes 		break;
1566df8bae1dSRodney W. Grimes 
1567df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1568df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_LAST_ACK;
1569df8bae1dSRodney W. Grimes 		break;
1570df8bae1dSRodney W. Grimes 	}
1571abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
1572df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
1573abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
15747c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
15757c72af87SMohan Srinivasan 			int timeout;
15767c72af87SMohan Srinivasan 
15777c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
15787c72af87SMohan Srinivasan 			    tcp_finwait2_timeout : tcp_maxidle;
1579b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
1580b6239c4aSAndras Olah 		}
1581df8bae1dSRodney W. Grimes 	}
15827c72af87SMohan Srinivasan }
1583497057eeSRobert Watson 
1584497057eeSRobert Watson #ifdef DDB
1585497057eeSRobert Watson static void
1586497057eeSRobert Watson db_print_indent(int indent)
1587497057eeSRobert Watson {
1588497057eeSRobert Watson 	int i;
1589497057eeSRobert Watson 
1590497057eeSRobert Watson 	for (i = 0; i < indent; i++)
1591497057eeSRobert Watson 		db_printf(" ");
1592497057eeSRobert Watson }
1593497057eeSRobert Watson 
1594497057eeSRobert Watson static void
1595497057eeSRobert Watson db_print_tstate(int t_state)
1596497057eeSRobert Watson {
1597497057eeSRobert Watson 
1598497057eeSRobert Watson 	switch (t_state) {
1599497057eeSRobert Watson 	case TCPS_CLOSED:
1600497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
1601497057eeSRobert Watson 		return;
1602497057eeSRobert Watson 
1603497057eeSRobert Watson 	case TCPS_LISTEN:
1604497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
1605497057eeSRobert Watson 		return;
1606497057eeSRobert Watson 
1607497057eeSRobert Watson 	case TCPS_SYN_SENT:
1608497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
1609497057eeSRobert Watson 		return;
1610497057eeSRobert Watson 
1611497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
1612497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
1613497057eeSRobert Watson 		return;
1614497057eeSRobert Watson 
1615497057eeSRobert Watson 	case TCPS_ESTABLISHED:
1616497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
1617497057eeSRobert Watson 		return;
1618497057eeSRobert Watson 
1619497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
1620497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
1621497057eeSRobert Watson 		return;
1622497057eeSRobert Watson 
1623497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
1624497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
1625497057eeSRobert Watson 		return;
1626497057eeSRobert Watson 
1627497057eeSRobert Watson 	case TCPS_CLOSING:
1628497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
1629497057eeSRobert Watson 		return;
1630497057eeSRobert Watson 
1631497057eeSRobert Watson 	case TCPS_LAST_ACK:
1632497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
1633497057eeSRobert Watson 		return;
1634497057eeSRobert Watson 
1635497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
1636497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
1637497057eeSRobert Watson 		return;
1638497057eeSRobert Watson 
1639497057eeSRobert Watson 	case TCPS_TIME_WAIT:
1640497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
1641497057eeSRobert Watson 		return;
1642497057eeSRobert Watson 
1643497057eeSRobert Watson 	default:
1644497057eeSRobert Watson 		db_printf("unknown");
1645497057eeSRobert Watson 		return;
1646497057eeSRobert Watson 	}
1647497057eeSRobert Watson }
1648497057eeSRobert Watson 
1649497057eeSRobert Watson static void
1650497057eeSRobert Watson db_print_tflags(u_int t_flags)
1651497057eeSRobert Watson {
1652497057eeSRobert Watson 	int comma;
1653497057eeSRobert Watson 
1654497057eeSRobert Watson 	comma = 0;
1655497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
1656497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
1657497057eeSRobert Watson 		comma = 1;
1658497057eeSRobert Watson 	}
1659497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
1660497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
1661497057eeSRobert Watson 		comma = 1;
1662497057eeSRobert Watson 	}
1663497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
1664497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
1665497057eeSRobert Watson 		comma = 1;
1666497057eeSRobert Watson 	}
1667497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
1668497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
1669497057eeSRobert Watson 		comma = 1;
1670497057eeSRobert Watson 	}
1671497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
1672497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
1673497057eeSRobert Watson 		comma = 1;
1674497057eeSRobert Watson 	}
1675497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
1676497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1677497057eeSRobert Watson 		comma = 1;
1678497057eeSRobert Watson 	}
1679497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
1680497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1681497057eeSRobert Watson 		comma = 1;
1682497057eeSRobert Watson 	}
1683497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
1684497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1685497057eeSRobert Watson 		comma = 1;
1686497057eeSRobert Watson 	}
1687497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
1688497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1689497057eeSRobert Watson 		comma = 1;
1690497057eeSRobert Watson 	}
1691497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
1692497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1693497057eeSRobert Watson 		comma = 1;
1694497057eeSRobert Watson 	}
1695497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
1696497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1697497057eeSRobert Watson 		comma = 1;
1698497057eeSRobert Watson 	}
1699497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
1700497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1701497057eeSRobert Watson 		comma = 1;
1702497057eeSRobert Watson 	}
1703497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
1704497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
1705497057eeSRobert Watson 		comma = 1;
1706497057eeSRobert Watson 	}
1707497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
1708497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
1709497057eeSRobert Watson 		comma = 1;
1710497057eeSRobert Watson 	}
1711497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
1712497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1713497057eeSRobert Watson 		comma = 1;
1714497057eeSRobert Watson 	}
1715497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
1716497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1717497057eeSRobert Watson 		comma = 1;
1718497057eeSRobert Watson 	}
1719497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
1720497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1721497057eeSRobert Watson 		comma = 1;
1722497057eeSRobert Watson 	}
1723497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
1724497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1725497057eeSRobert Watson 		comma = 1;
1726497057eeSRobert Watson 	}
1727497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
1728497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1729497057eeSRobert Watson 		comma = 1;
1730497057eeSRobert Watson 	}
1731497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
1732497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1733497057eeSRobert Watson 		comma = 1;
1734497057eeSRobert Watson 	}
1735497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
1736497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1737497057eeSRobert Watson 		comma = 1;
1738497057eeSRobert Watson 	}
1739497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
1740497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1741497057eeSRobert Watson 		comma = 1;
1742497057eeSRobert Watson 	}
1743497057eeSRobert Watson 	if (t_flags & TF_TSO) {
1744497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
1745497057eeSRobert Watson 		comma = 1;
1746497057eeSRobert Watson 	}
1747f2512ba1SRui Paulo 	if (t_flags & TF_ECN_PERMIT) {
1748f2512ba1SRui Paulo 		db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
1749f2512ba1SRui Paulo 		comma = 1;
1750f2512ba1SRui Paulo 	}
1751497057eeSRobert Watson }
1752497057eeSRobert Watson 
1753497057eeSRobert Watson static void
1754497057eeSRobert Watson db_print_toobflags(char t_oobflags)
1755497057eeSRobert Watson {
1756497057eeSRobert Watson 	int comma;
1757497057eeSRobert Watson 
1758497057eeSRobert Watson 	comma = 0;
1759497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
1760497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1761497057eeSRobert Watson 		comma = 1;
1762497057eeSRobert Watson 	}
1763497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
1764497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1765497057eeSRobert Watson 		comma = 1;
1766497057eeSRobert Watson 	}
1767497057eeSRobert Watson }
1768497057eeSRobert Watson 
1769497057eeSRobert Watson static void
1770497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1771497057eeSRobert Watson {
1772497057eeSRobert Watson 
1773497057eeSRobert Watson 	db_print_indent(indent);
1774497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
1775497057eeSRobert Watson 
1776497057eeSRobert Watson 	indent += 2;
1777497057eeSRobert Watson 
1778497057eeSRobert Watson 	db_print_indent(indent);
1779497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
1780497057eeSRobert Watson 	   LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
1781497057eeSRobert Watson 
1782497057eeSRobert Watson 	db_print_indent(indent);
178385d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
1784e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
1785497057eeSRobert Watson 
1786497057eeSRobert Watson 	db_print_indent(indent);
1787e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
1788e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
1789497057eeSRobert Watson 
1790497057eeSRobert Watson 	db_print_indent(indent);
1791497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
1792497057eeSRobert Watson 	db_print_tstate(tp->t_state);
1793497057eeSRobert Watson 	db_printf(")\n");
1794497057eeSRobert Watson 
1795497057eeSRobert Watson 	db_print_indent(indent);
1796497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
1797497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
1798497057eeSRobert Watson 	db_printf(")\n");
1799497057eeSRobert Watson 
1800497057eeSRobert Watson 	db_print_indent(indent);
1801497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
1802497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
1803497057eeSRobert Watson 
1804497057eeSRobert Watson 	db_print_indent(indent);
1805497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
1806497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
1807497057eeSRobert Watson 
1808497057eeSRobert Watson 	db_print_indent(indent);
1809497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
1810497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
1811497057eeSRobert Watson 
1812497057eeSRobert Watson 	db_print_indent(indent);
1813497057eeSRobert Watson 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %lu   rcv_up: 0x%08x\n",
1814497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
1815497057eeSRobert Watson 
1816497057eeSRobert Watson 	db_print_indent(indent);
1817497057eeSRobert Watson 	db_printf("snd_wnd: %lu   snd_cwnd: %lu   snd_bwnd: %lu\n",
1818497057eeSRobert Watson 	   tp->snd_wnd, tp->snd_cwnd, tp->snd_bwnd);
1819497057eeSRobert Watson 
1820497057eeSRobert Watson 	db_print_indent(indent);
1821497057eeSRobert Watson 	db_printf("snd_ssthresh: %lu   snd_bandwidth: %lu   snd_recover: "
1822497057eeSRobert Watson 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_bandwidth,
1823497057eeSRobert Watson 	    tp->snd_recover);
1824497057eeSRobert Watson 
1825497057eeSRobert Watson 	db_print_indent(indent);
1826497057eeSRobert Watson 	db_printf("t_maxopd: %u   t_rcvtime: %lu   t_startime: %lu\n",
1827497057eeSRobert Watson 	    tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
1828497057eeSRobert Watson 
1829497057eeSRobert Watson 	db_print_indent(indent);
1830497057eeSRobert Watson 	db_printf("t_rttime: %d   t_rtsq: 0x%08x   t_bw_rtttime: %d\n",
1831497057eeSRobert Watson 	    tp->t_rtttime, tp->t_rtseq, tp->t_bw_rtttime);
1832497057eeSRobert Watson 
1833497057eeSRobert Watson 	db_print_indent(indent);
1834497057eeSRobert Watson 	db_printf("t_bw_rtseq: 0x%08x   t_rxtcur: %d   t_maxseg: %u   "
1835497057eeSRobert Watson 	    "t_srtt: %d\n", tp->t_bw_rtseq, tp->t_rxtcur, tp->t_maxseg,
1836497057eeSRobert Watson 	    tp->t_srtt);
1837497057eeSRobert Watson 
1838497057eeSRobert Watson 	db_print_indent(indent);
1839497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
1840497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
1841497057eeSRobert Watson 	    tp->t_rttbest);
1842497057eeSRobert Watson 
1843497057eeSRobert Watson 	db_print_indent(indent);
1844497057eeSRobert Watson 	db_printf("t_rttupdated: %lu   max_sndwnd: %lu   t_softerror: %d\n",
1845497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
1846497057eeSRobert Watson 
1847497057eeSRobert Watson 	db_print_indent(indent);
1848497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
1849497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
1850497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
1851497057eeSRobert Watson 
1852497057eeSRobert Watson 	db_print_indent(indent);
1853497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
1854497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
1855497057eeSRobert Watson 
1856497057eeSRobert Watson 	db_print_indent(indent);
18571a553740SAndre Oppermann 	db_printf("ts_recent: %u   ts_recent_age: %lu\n",
18581a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
1859497057eeSRobert Watson 
1860497057eeSRobert Watson 	db_print_indent(indent);
1861497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
1862497057eeSRobert Watson 	    "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
1863497057eeSRobert Watson 
1864497057eeSRobert Watson 	db_print_indent(indent);
1865497057eeSRobert Watson 	db_printf("snd_ssthresh_prev: %lu   snd_recover_prev: 0x%08x   "
1866497057eeSRobert Watson 	    "t_badrxtwin: %lu\n", tp->snd_ssthresh_prev,
1867497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
1868497057eeSRobert Watson 
1869497057eeSRobert Watson 	db_print_indent(indent);
18703529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
18713529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
1872497057eeSRobert Watson 
1873497057eeSRobert Watson 	db_print_indent(indent);
1874497057eeSRobert Watson 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
1875497057eeSRobert Watson 	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
1876497057eeSRobert Watson 
1877497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
1878497057eeSRobert Watson 
1879497057eeSRobert Watson 	db_print_indent(indent);
1880497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
1881497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
1882497057eeSRobert Watson }
1883497057eeSRobert Watson 
1884497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
1885497057eeSRobert Watson {
1886497057eeSRobert Watson 	struct tcpcb *tp;
1887497057eeSRobert Watson 
1888497057eeSRobert Watson 	if (!have_addr) {
1889497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
1890497057eeSRobert Watson 		return;
1891497057eeSRobert Watson 	}
1892497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
1893497057eeSRobert Watson 
1894497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
1895497057eeSRobert Watson }
1896497057eeSRobert Watson #endif
1897