xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 0206cdb8461ff06a42eafe71aa9163523ca3940e)
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>
90df8bae1dSRodney W. Grimes 
91df8bae1dSRodney W. Grimes /*
92df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
93df8bae1dSRodney W. Grimes  */
9456dc72c3SPawel Jakub Dawidek static int	tcp_attach(struct socket *);
954d77a549SAlfred Perlstein static int	tcp_connect(struct tcpcb *, struct sockaddr *,
964d77a549SAlfred Perlstein 		    struct thread *td);
97fb59c426SYoshinobu Inoue #ifdef INET6
984d77a549SAlfred Perlstein static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
994d77a549SAlfred Perlstein 		    struct thread *td);
100fb59c426SYoshinobu Inoue #endif /* INET6 */
101623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
102623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
103b8af5dfaSRobert Watson static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
1042c37256eSGarrett Wollman 
1052c37256eSGarrett Wollman #ifdef TCPDEBUG
1061db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1072c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
1084cc20ab1SSeigo Tanimura #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
1094cc20ab1SSeigo Tanimura 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1102c37256eSGarrett Wollman #else
1112c37256eSGarrett Wollman #define	TCPDEBUG0
1122c37256eSGarrett Wollman #define	TCPDEBUG1()
1132c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1142c37256eSGarrett Wollman #endif
1152c37256eSGarrett Wollman 
1162c37256eSGarrett Wollman /*
1172c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1182c37256eSGarrett Wollman  * and an internet control block.
1192c37256eSGarrett Wollman  */
1202c37256eSGarrett Wollman static int
121b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1222c37256eSGarrett Wollman {
123f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
124623dce13SRobert Watson 	struct tcpcb *tp = NULL;
125623dce13SRobert Watson 	int error;
1262c37256eSGarrett Wollman 	TCPDEBUG0;
1272c37256eSGarrett Wollman 
128623dce13SRobert Watson 	inp = sotoinpcb(so);
129623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1302c37256eSGarrett Wollman 	TCPDEBUG1();
1312c37256eSGarrett Wollman 
13256dc72c3SPawel Jakub Dawidek 	error = tcp_attach(so);
1332c37256eSGarrett Wollman 	if (error)
1342c37256eSGarrett Wollman 		goto out;
1352c37256eSGarrett Wollman 
1362c37256eSGarrett Wollman 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1373879597fSAndrey A. Chernov 		so->so_linger = TCP_LINGERTIME;
138f76fcf6dSJeffrey Hsu 
139f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
140f76fcf6dSJeffrey Hsu 	tp = intotcpcb(inp);
1412c37256eSGarrett Wollman out:
1422c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
1432c37256eSGarrett Wollman 	return error;
1442c37256eSGarrett Wollman }
1452c37256eSGarrett Wollman 
1462c37256eSGarrett Wollman /*
147a152f8a3SRobert Watson  * tcp_detach is called when the socket layer loses its final reference
148a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
149a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
150a152f8a3SRobert Watson  * inpcb state: time wait.
151c78cbc7bSRobert Watson  *
152a152f8a3SRobert Watson  * This function can probably be re-absorbed back into tcp_usr_detach() now
153a152f8a3SRobert Watson  * that there is a single detach path.
1542c37256eSGarrett Wollman  */
155bc725eafSRobert Watson static void
156c78cbc7bSRobert Watson tcp_detach(struct socket *so, struct inpcb *inp)
1572c37256eSGarrett Wollman {
1582c37256eSGarrett Wollman 	struct tcpcb *tp;
159623dce13SRobert Watson #ifdef INET6
160623dce13SRobert Watson 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
161623dce13SRobert Watson #endif
1628b615593SMarko Zec #ifdef INVARIANTS
1638b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
1648b615593SMarko Zec #endif
1652c37256eSGarrett Wollman 
166603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1678501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
168623dce13SRobert Watson 
169c78cbc7bSRobert Watson 	KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
170c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
171953b5606SRobert Watson 
172a152f8a3SRobert Watson 	tp = intotcpcb(inp);
173a152f8a3SRobert Watson 
174623dce13SRobert Watson 	if (inp->inp_vflag & INP_TIMEWAIT) {
175623dce13SRobert Watson 		/*
176a152f8a3SRobert Watson 		 * There are two cases to handle: one in which the time wait
177a152f8a3SRobert Watson 		 * state is being discarded (INP_DROPPED), and one in which
178a152f8a3SRobert Watson 		 * this connection will remain in timewait.  In the former,
179a152f8a3SRobert Watson 		 * it is time to discard all state (except tcptw, which has
180a152f8a3SRobert Watson 		 * already been discarded by the timewait close code, which
181a152f8a3SRobert Watson 		 * should be further up the call stack somewhere).  In the
182a152f8a3SRobert Watson 		 * latter case, we detach from the socket, but leave the pcb
183a152f8a3SRobert Watson 		 * present until timewait ends.
184623dce13SRobert Watson 		 *
185a152f8a3SRobert Watson 		 * XXXRW: Would it be cleaner to free the tcptw here?
186623dce13SRobert Watson 		 */
187a152f8a3SRobert Watson 		if (inp->inp_vflag & INP_DROPPED) {
188a152f8a3SRobert Watson 			KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
189a152f8a3SRobert Watson 			    "INP_DROPPED && tp != NULL"));
190623dce13SRobert Watson 			in_pcbdetach(inp);
191623dce13SRobert Watson #ifdef INET6
192623dce13SRobert Watson 			if (isipv6)
1930206cdb8SBjoern A. Zeeb 				in6_pcbfree(inp);
194623dce13SRobert Watson 			else
195623dce13SRobert Watson #endif
1960206cdb8SBjoern A. Zeeb 				in_pcbfree(inp);
1970206cdb8SBjoern A. Zeeb 		} else {
198623dce13SRobert Watson 			in_pcbdetach(inp);
1998501a69cSRobert Watson 			INP_WUNLOCK(inp);
200623dce13SRobert Watson 		}
201623dce13SRobert Watson 	} else {
202e6e65783SRobert Watson 		/*
203a152f8a3SRobert Watson 		 * If the connection is not in timewait, we consider two
204a152f8a3SRobert Watson 		 * two conditions: one in which no further processing is
205a152f8a3SRobert Watson 		 * necessary (dropped || embryonic), and one in which TCP is
206a152f8a3SRobert Watson 		 * not yet done, but no longer requires the socket, so the
207a152f8a3SRobert Watson 		 * pcb will persist for the time being.
208a152f8a3SRobert Watson 		 *
209a152f8a3SRobert Watson 		 * XXXRW: Does the second case still occur?
210e6e65783SRobert Watson 		 */
211623dce13SRobert Watson 		if (inp->inp_vflag & INP_DROPPED ||
212623dce13SRobert Watson 		    tp->t_state < TCPS_SYN_SENT) {
213623dce13SRobert Watson 			tcp_discardcb(tp);
214623dce13SRobert Watson 			in_pcbdetach(inp);
215a152f8a3SRobert Watson #ifdef INET6
216a152f8a3SRobert Watson 			if (isipv6)
2170206cdb8SBjoern A. Zeeb 				in6_pcbfree(inp);
218a152f8a3SRobert Watson 			else
219a152f8a3SRobert Watson #endif
2200206cdb8SBjoern A. Zeeb 				in_pcbfree(inp);
2210206cdb8SBjoern A. Zeeb 		} else {
222a152f8a3SRobert Watson 				in_pcbdetach(inp);
223623dce13SRobert Watson 		}
224623dce13SRobert Watson 	}
225c78cbc7bSRobert Watson }
226c78cbc7bSRobert Watson 
227c78cbc7bSRobert Watson /*
228c78cbc7bSRobert Watson  * pru_detach() detaches the TCP protocol from the socket.
229c78cbc7bSRobert Watson  * If the protocol state is non-embryonic, then can't
230c78cbc7bSRobert Watson  * do this directly: have to initiate a pru_disconnect(),
231c78cbc7bSRobert Watson  * which may finish later; embryonic TCB's can just
232c78cbc7bSRobert Watson  * be discarded here.
233c78cbc7bSRobert Watson  */
234c78cbc7bSRobert Watson static void
235c78cbc7bSRobert Watson tcp_usr_detach(struct socket *so)
236c78cbc7bSRobert Watson {
2378b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
238c78cbc7bSRobert Watson 	struct inpcb *inp;
239c78cbc7bSRobert Watson 
240c78cbc7bSRobert Watson 	inp = sotoinpcb(so);
241c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
242603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
2438501a69cSRobert Watson 	INP_WLOCK(inp);
244c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
245c78cbc7bSRobert Watson 	    ("tcp_usr_detach: inp_socket == NULL"));
246c78cbc7bSRobert Watson 	tcp_detach(so, inp);
247603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
2482c37256eSGarrett Wollman }
2492c37256eSGarrett Wollman 
2502c37256eSGarrett Wollman /*
2512c37256eSGarrett Wollman  * Give the socket an address.
2522c37256eSGarrett Wollman  */
2532c37256eSGarrett Wollman static int
254b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2552c37256eSGarrett Wollman {
2568b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
2572c37256eSGarrett Wollman 	int error = 0;
258f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
259623dce13SRobert Watson 	struct tcpcb *tp = NULL;
2602c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
2612c37256eSGarrett Wollman 
26252710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
26352710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sinp))
26452710de1SPawel Jakub Dawidek 		return (EINVAL);
2652c37256eSGarrett Wollman 	/*
2662c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2672c37256eSGarrett Wollman 	 * to them.
2682c37256eSGarrett Wollman 	 */
2692c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
27052710de1SPawel Jakub Dawidek 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
27152710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
27252710de1SPawel Jakub Dawidek 
273623dce13SRobert Watson 	TCPDEBUG0;
274603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
275623dce13SRobert Watson 	inp = sotoinpcb(so);
276623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
2778501a69cSRobert Watson 	INP_WLOCK(inp);
278623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
279623dce13SRobert Watson 		error = EINVAL;
2802c37256eSGarrett Wollman 		goto out;
281623dce13SRobert Watson 	}
282623dce13SRobert Watson 	tp = intotcpcb(inp);
283623dce13SRobert Watson 	TCPDEBUG1();
284623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
285623dce13SRobert Watson out:
286623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
2878501a69cSRobert Watson 	INP_WUNLOCK(inp);
288603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
289623dce13SRobert Watson 
290623dce13SRobert Watson 	return (error);
2912c37256eSGarrett Wollman }
2922c37256eSGarrett Wollman 
293fb59c426SYoshinobu Inoue #ifdef INET6
294fb59c426SYoshinobu Inoue static int
295b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
296fb59c426SYoshinobu Inoue {
2978b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
298fb59c426SYoshinobu Inoue 	int error = 0;
299f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
300623dce13SRobert Watson 	struct tcpcb *tp = NULL;
301fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
302fb59c426SYoshinobu Inoue 
30352710de1SPawel Jakub Dawidek 	sin6p = (struct sockaddr_in6 *)nam;
30452710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sin6p))
30552710de1SPawel Jakub Dawidek 		return (EINVAL);
306fb59c426SYoshinobu Inoue 	/*
307fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
308fb59c426SYoshinobu Inoue 	 * to them.
309fb59c426SYoshinobu Inoue 	 */
310fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6 &&
31152710de1SPawel Jakub Dawidek 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
31252710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
31352710de1SPawel Jakub Dawidek 
314623dce13SRobert Watson 	TCPDEBUG0;
315603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
316623dce13SRobert Watson 	inp = sotoinpcb(so);
317623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
3188501a69cSRobert Watson 	INP_WLOCK(inp);
319623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
320623dce13SRobert Watson 		error = EINVAL;
321623dce13SRobert Watson 		goto out;
322623dce13SRobert Watson 	}
323623dce13SRobert Watson 	tp = intotcpcb(inp);
324623dce13SRobert Watson 	TCPDEBUG1();
325fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
326fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
32766ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
328fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
329fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
330fb59c426SYoshinobu Inoue 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
331fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
332fb59c426SYoshinobu Inoue 
333fb59c426SYoshinobu Inoue 			in6_sin6_2_sin(&sin, sin6p);
334fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
335fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
336b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
337b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
338fb59c426SYoshinobu Inoue 			goto out;
339fb59c426SYoshinobu Inoue 		}
340fb59c426SYoshinobu Inoue 	}
341b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
342623dce13SRobert Watson out:
343623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3448501a69cSRobert Watson 	INP_WUNLOCK(inp);
345603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
346623dce13SRobert Watson 	return (error);
347fb59c426SYoshinobu Inoue }
348fb59c426SYoshinobu Inoue #endif /* INET6 */
349fb59c426SYoshinobu Inoue 
3502c37256eSGarrett Wollman /*
3512c37256eSGarrett Wollman  * Prepare to accept connections.
3522c37256eSGarrett Wollman  */
3532c37256eSGarrett Wollman static int
354d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
3552c37256eSGarrett Wollman {
3568b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
3572c37256eSGarrett Wollman 	int error = 0;
358f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
359623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3602c37256eSGarrett Wollman 
361623dce13SRobert Watson 	TCPDEBUG0;
362603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
363623dce13SRobert Watson 	inp = sotoinpcb(so);
364623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
3658501a69cSRobert Watson 	INP_WLOCK(inp);
366623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
367623dce13SRobert Watson 		error = EINVAL;
368623dce13SRobert Watson 		goto out;
369623dce13SRobert Watson 	}
370623dce13SRobert Watson 	tp = intotcpcb(inp);
371623dce13SRobert Watson 	TCPDEBUG1();
3720daccb9cSRobert Watson 	SOCK_LOCK(so);
3730daccb9cSRobert Watson 	error = solisten_proto_check(so);
3740daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0)
375b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
3760daccb9cSRobert Watson 	if (error == 0) {
3772c37256eSGarrett Wollman 		tp->t_state = TCPS_LISTEN;
378d374e81eSRobert Watson 		solisten_proto(so, backlog);
379bc65987aSKip Macy 		tcp_offload_listen_open(tp);
3800daccb9cSRobert Watson 	}
3810daccb9cSRobert Watson 	SOCK_UNLOCK(so);
382623dce13SRobert Watson 
383623dce13SRobert Watson out:
384623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
3858501a69cSRobert Watson 	INP_WUNLOCK(inp);
386603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
387623dce13SRobert Watson 	return (error);
3882c37256eSGarrett Wollman }
3892c37256eSGarrett Wollman 
390fb59c426SYoshinobu Inoue #ifdef INET6
391fb59c426SYoshinobu Inoue static int
392d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
393fb59c426SYoshinobu Inoue {
3948b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
395fb59c426SYoshinobu Inoue 	int error = 0;
396f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
397623dce13SRobert Watson 	struct tcpcb *tp = NULL;
398fb59c426SYoshinobu Inoue 
399623dce13SRobert Watson 	TCPDEBUG0;
400603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
401623dce13SRobert Watson 	inp = sotoinpcb(so);
402623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4038501a69cSRobert Watson 	INP_WLOCK(inp);
404623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
405623dce13SRobert Watson 		error = EINVAL;
406623dce13SRobert Watson 		goto out;
407623dce13SRobert Watson 	}
408623dce13SRobert Watson 	tp = intotcpcb(inp);
409623dce13SRobert Watson 	TCPDEBUG1();
4100daccb9cSRobert Watson 	SOCK_LOCK(so);
4110daccb9cSRobert Watson 	error = solisten_proto_check(so);
4120daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0) {
413fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
41466ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
415fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
416b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
417fb59c426SYoshinobu Inoue 	}
4180daccb9cSRobert Watson 	if (error == 0) {
419fb59c426SYoshinobu Inoue 		tp->t_state = TCPS_LISTEN;
420d374e81eSRobert Watson 		solisten_proto(so, backlog);
4210daccb9cSRobert Watson 	}
4220daccb9cSRobert Watson 	SOCK_UNLOCK(so);
423623dce13SRobert Watson 
424623dce13SRobert Watson out:
425623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4268501a69cSRobert Watson 	INP_WUNLOCK(inp);
427603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
428623dce13SRobert Watson 	return (error);
429fb59c426SYoshinobu Inoue }
430fb59c426SYoshinobu Inoue #endif /* INET6 */
431fb59c426SYoshinobu Inoue 
4322c37256eSGarrett Wollman /*
4332c37256eSGarrett Wollman  * Initiate connection to peer.
4342c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
4352c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
4362c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
4372c37256eSGarrett Wollman  * Send initial segment on connection.
4382c37256eSGarrett Wollman  */
4392c37256eSGarrett Wollman static int
440b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
4412c37256eSGarrett Wollman {
4428b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
4432c37256eSGarrett Wollman 	int error = 0;
444f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
445623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4462c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
4472c37256eSGarrett Wollman 
44857bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
449e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
450e29ef13fSDon Lewis 		return (EINVAL);
45152710de1SPawel Jakub Dawidek 	/*
45252710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
45352710de1SPawel Jakub Dawidek 	 */
4542c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
45552710de1SPawel Jakub Dawidek 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
45652710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
457812d8653SSam Leffler 	if (jailed(td->td_ucred))
458a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
45975c13541SPoul-Henning Kamp 
460623dce13SRobert Watson 	TCPDEBUG0;
461603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
462623dce13SRobert Watson 	inp = sotoinpcb(so);
463623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
4648501a69cSRobert Watson 	INP_WLOCK(inp);
465623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
466623dce13SRobert Watson 		error = EINVAL;
467623dce13SRobert Watson 		goto out;
468623dce13SRobert Watson 	}
469623dce13SRobert Watson 	tp = intotcpcb(inp);
470623dce13SRobert Watson 	TCPDEBUG1();
471b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
4722c37256eSGarrett Wollman 		goto out;
473bc65987aSKip Macy 	error = tcp_output_connect(so, nam);
474623dce13SRobert Watson out:
475623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
4768501a69cSRobert Watson 	INP_WUNLOCK(inp);
477603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
478623dce13SRobert Watson 	return (error);
4792c37256eSGarrett Wollman }
4802c37256eSGarrett Wollman 
481fb59c426SYoshinobu Inoue #ifdef INET6
482fb59c426SYoshinobu Inoue static int
483b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
484fb59c426SYoshinobu Inoue {
4858b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
486fb59c426SYoshinobu Inoue 	int error = 0;
487f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
488623dce13SRobert Watson 	struct tcpcb *tp = NULL;
489fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
490623dce13SRobert Watson 
491623dce13SRobert Watson 	TCPDEBUG0;
492fb59c426SYoshinobu Inoue 
493fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
494e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sin6p))
495e29ef13fSDon Lewis 		return (EINVAL);
49652710de1SPawel Jakub Dawidek 	/*
49752710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
49852710de1SPawel Jakub Dawidek 	 */
499fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6
50052710de1SPawel Jakub Dawidek 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
50152710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
502fb59c426SYoshinobu Inoue 
503603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
504623dce13SRobert Watson 	inp = sotoinpcb(so);
505623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
5068501a69cSRobert Watson 	INP_WLOCK(inp);
507623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
508623dce13SRobert Watson 		error = EINVAL;
509623dce13SRobert Watson 		goto out;
510623dce13SRobert Watson 	}
511623dce13SRobert Watson 	tp = intotcpcb(inp);
512623dce13SRobert Watson 	TCPDEBUG1();
51333841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
514fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
515fb59c426SYoshinobu Inoue 
516d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
517d46a5312SMaxim Konovalov 			error = EINVAL;
518d46a5312SMaxim Konovalov 			goto out;
519d46a5312SMaxim Konovalov 		}
52033841545SHajimu UMEMOTO 
521fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
522fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
523fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
524b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
525fb59c426SYoshinobu Inoue 			goto out;
526bc65987aSKip Macy 		error = tcp_output_connect(so, nam);
527fb59c426SYoshinobu Inoue 		goto out;
528fb59c426SYoshinobu Inoue 	}
529fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
530fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
531b7d6d952SHajimu UMEMOTO 	inp->inp_inc.inc_isipv6 = 1;
532b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
533fb59c426SYoshinobu Inoue 		goto out;
534bc65987aSKip Macy 	error = tcp_output_connect(so, nam);
535623dce13SRobert Watson 
536623dce13SRobert Watson out:
537623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
5388501a69cSRobert Watson 	INP_WUNLOCK(inp);
539603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
540623dce13SRobert Watson 	return (error);
541fb59c426SYoshinobu Inoue }
542fb59c426SYoshinobu Inoue #endif /* INET6 */
543fb59c426SYoshinobu Inoue 
5442c37256eSGarrett Wollman /*
5452c37256eSGarrett Wollman  * Initiate disconnect from peer.
5462c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
5472c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
5482c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
5492c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
5502c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
5512c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
5522c37256eSGarrett Wollman  *
5532c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
5542c37256eSGarrett Wollman  */
5552c37256eSGarrett Wollman static int
5562c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
5572c37256eSGarrett Wollman {
5588b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
559f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
560623dce13SRobert Watson 	struct tcpcb *tp = NULL;
561623dce13SRobert Watson 	int error = 0;
5622c37256eSGarrett Wollman 
563623dce13SRobert Watson 	TCPDEBUG0;
564603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
565623dce13SRobert Watson 	inp = sotoinpcb(so);
566623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
5678501a69cSRobert Watson 	INP_WLOCK(inp);
568623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
56921367f63SSam Leffler 		error = ECONNRESET;
570623dce13SRobert Watson 		goto out;
571623dce13SRobert Watson 	}
572623dce13SRobert Watson 	tp = intotcpcb(inp);
573623dce13SRobert Watson 	TCPDEBUG1();
574623dce13SRobert Watson 	tcp_disconnect(tp);
575623dce13SRobert Watson out:
576623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
5778501a69cSRobert Watson 	INP_WUNLOCK(inp);
578603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
579623dce13SRobert Watson 	return (error);
5802c37256eSGarrett Wollman }
5812c37256eSGarrett Wollman 
5822c37256eSGarrett Wollman /*
5832c37256eSGarrett Wollman  * Accept a connection.  Essentially all the work is
5842c37256eSGarrett Wollman  * done at higher levels; just return the address
5852c37256eSGarrett Wollman  * of the peer, storing through addr.
5862c37256eSGarrett Wollman  */
5872c37256eSGarrett Wollman static int
58857bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
5892c37256eSGarrett Wollman {
5908b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
5912c37256eSGarrett Wollman 	int error = 0;
592f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
5931db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
59426ef6ac4SDon Lewis 	struct in_addr addr;
59526ef6ac4SDon Lewis 	in_port_t port = 0;
5961db24ffbSJonathan Lemon 	TCPDEBUG0;
5972c37256eSGarrett Wollman 
5983d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
5993d2d3ef4SRobert Watson 		return (ECONNABORTED);
600f76fcf6dSJeffrey Hsu 
601f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
602623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
603603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_tcbinfo);
6048501a69cSRobert Watson 	INP_WLOCK(inp);
605623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
6063d2d3ef4SRobert Watson 		error = ECONNABORTED;
607623dce13SRobert Watson 		goto out;
608623dce13SRobert Watson 	}
6091db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
6101db24ffbSJonathan Lemon 	TCPDEBUG1();
611f76fcf6dSJeffrey Hsu 
612f76fcf6dSJeffrey Hsu 	/*
61354d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
61426ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
61526ef6ac4SDon Lewis 	 * release the lock.
616f76fcf6dSJeffrey Hsu 	 */
61726ef6ac4SDon Lewis 	port = inp->inp_fport;
61826ef6ac4SDon Lewis 	addr = inp->inp_faddr;
619f76fcf6dSJeffrey Hsu 
620623dce13SRobert Watson out:
621623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
6228501a69cSRobert Watson 	INP_WUNLOCK(inp);
623603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_tcbinfo);
62426ef6ac4SDon Lewis 	if (error == 0)
62526ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
62626ef6ac4SDon Lewis 	return error;
6272c37256eSGarrett Wollman }
6282c37256eSGarrett Wollman 
629fb59c426SYoshinobu Inoue #ifdef INET6
630fb59c426SYoshinobu Inoue static int
631fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
632fb59c426SYoshinobu Inoue {
633f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
634fb59c426SYoshinobu Inoue 	int error = 0;
6351db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
63626ef6ac4SDon Lewis 	struct in_addr addr;
63726ef6ac4SDon Lewis 	struct in6_addr addr6;
63826ef6ac4SDon Lewis 	in_port_t port = 0;
63926ef6ac4SDon Lewis 	int v4 = 0;
6401db24ffbSJonathan Lemon 	TCPDEBUG0;
641fb59c426SYoshinobu Inoue 
642b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
643b4470c16SRobert Watson 		return (ECONNABORTED);
644f76fcf6dSJeffrey Hsu 
645f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
646623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
6478501a69cSRobert Watson 	INP_WLOCK(inp);
648623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
64921367f63SSam Leffler 		error = ECONNABORTED;
650623dce13SRobert Watson 		goto out;
651623dce13SRobert Watson 	}
6521db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
6531db24ffbSJonathan Lemon 	TCPDEBUG1();
654623dce13SRobert Watson 
65526ef6ac4SDon Lewis 	/*
65626ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
65726ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
65826ef6ac4SDon Lewis 	 * release the lock.
65926ef6ac4SDon Lewis 	 */
66026ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
66126ef6ac4SDon Lewis 		v4 = 1;
66226ef6ac4SDon Lewis 		port = inp->inp_fport;
66326ef6ac4SDon Lewis 		addr = inp->inp_faddr;
66426ef6ac4SDon Lewis 	} else {
66526ef6ac4SDon Lewis 		port = inp->inp_fport;
66626ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
66726ef6ac4SDon Lewis 	}
66826ef6ac4SDon Lewis 
669623dce13SRobert Watson out:
670623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
6718501a69cSRobert Watson 	INP_WUNLOCK(inp);
67226ef6ac4SDon Lewis 	if (error == 0) {
67326ef6ac4SDon Lewis 		if (v4)
67426ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
67526ef6ac4SDon Lewis 		else
67626ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
67726ef6ac4SDon Lewis 	}
67826ef6ac4SDon Lewis 	return error;
679fb59c426SYoshinobu Inoue }
680fb59c426SYoshinobu Inoue #endif /* INET6 */
681f76fcf6dSJeffrey Hsu 
682f76fcf6dSJeffrey Hsu /*
6832c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
6842c37256eSGarrett Wollman  */
6852c37256eSGarrett Wollman static int
6862c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
6872c37256eSGarrett Wollman {
6888b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
6892c37256eSGarrett Wollman 	int error = 0;
690f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
691623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6922c37256eSGarrett Wollman 
693623dce13SRobert Watson 	TCPDEBUG0;
694603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
695623dce13SRobert Watson 	inp = sotoinpcb(so);
696623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
6978501a69cSRobert Watson 	INP_WLOCK(inp);
698623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
69921367f63SSam Leffler 		error = ECONNRESET;
700623dce13SRobert Watson 		goto out;
701623dce13SRobert Watson 	}
702623dce13SRobert Watson 	tp = intotcpcb(inp);
703623dce13SRobert Watson 	TCPDEBUG1();
7042c37256eSGarrett Wollman 	socantsendmore(so);
705623dce13SRobert Watson 	tcp_usrclosed(tp);
706bc65987aSKip Macy 	error = tcp_output_disconnect(tp);
707623dce13SRobert Watson 
708623dce13SRobert Watson out:
709623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
7108501a69cSRobert Watson 	INP_WUNLOCK(inp);
711603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
712623dce13SRobert Watson 
713623dce13SRobert Watson 	return (error);
7142c37256eSGarrett Wollman }
7152c37256eSGarrett Wollman 
7162c37256eSGarrett Wollman /*
7172c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
7182c37256eSGarrett Wollman  */
7192c37256eSGarrett Wollman static int
7202c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
7212c37256eSGarrett Wollman {
722f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
723623dce13SRobert Watson 	struct tcpcb *tp = NULL;
724623dce13SRobert Watson 	int error = 0;
7252c37256eSGarrett Wollman 
726623dce13SRobert Watson 	TCPDEBUG0;
727623dce13SRobert Watson 	inp = sotoinpcb(so);
728623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
7298501a69cSRobert Watson 	INP_WLOCK(inp);
730623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
73121367f63SSam Leffler 		error = ECONNRESET;
732623dce13SRobert Watson 		goto out;
733623dce13SRobert Watson 	}
734623dce13SRobert Watson 	tp = intotcpcb(inp);
735623dce13SRobert Watson 	TCPDEBUG1();
736bc65987aSKip Macy 	tcp_output_rcvd(tp);
737623dce13SRobert Watson 
738623dce13SRobert Watson out:
739623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
7408501a69cSRobert Watson 	INP_WUNLOCK(inp);
741623dce13SRobert Watson 	return (error);
7422c37256eSGarrett Wollman }
7432c37256eSGarrett Wollman 
7442c37256eSGarrett Wollman /*
7452c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
7469c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
7479c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
7489c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
7499c9906e9SPeter Wemm  * generally are caller-frees.
7502c37256eSGarrett Wollman  */
7512c37256eSGarrett Wollman static int
75257bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
753b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
7542c37256eSGarrett Wollman {
7558b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
7562c37256eSGarrett Wollman 	int error = 0;
757f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
758623dce13SRobert Watson 	struct tcpcb *tp = NULL;
759623dce13SRobert Watson 	int headlocked = 0;
760fb59c426SYoshinobu Inoue #ifdef INET6
761fb59c426SYoshinobu Inoue 	int isipv6;
762fb59c426SYoshinobu Inoue #endif
7639c9906e9SPeter Wemm 	TCPDEBUG0;
7642c37256eSGarrett Wollman 
765f76fcf6dSJeffrey Hsu 	/*
766623dce13SRobert Watson 	 * We require the pcbinfo lock in two cases:
767623dce13SRobert Watson 	 *
768623dce13SRobert Watson 	 * (1) An implied connect is taking place, which can result in
769623dce13SRobert Watson 	 *     binding IPs and ports and hence modification of the pcb hash
770623dce13SRobert Watson 	 *     chains.
771623dce13SRobert Watson 	 *
772623dce13SRobert Watson 	 * (2) PRUS_EOF is set, resulting in explicit close on the send.
773f76fcf6dSJeffrey Hsu 	 */
774623dce13SRobert Watson 	if ((nam != NULL) || (flags & PRUS_EOF)) {
775603724d3SBjoern A. Zeeb 		INP_INFO_WLOCK(&V_tcbinfo);
776623dce13SRobert Watson 		headlocked = 1;
777623dce13SRobert Watson 	}
778f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
779623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
7808501a69cSRobert Watson 	INP_WLOCK(inp);
781623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
7827ff0b850SAndre Oppermann 		if (control)
7837ff0b850SAndre Oppermann 			m_freem(control);
7847ff0b850SAndre Oppermann 		if (m)
7857ff0b850SAndre Oppermann 			m_freem(m);
78621367f63SSam Leffler 		error = ECONNRESET;
7879c9906e9SPeter Wemm 		goto out;
7889c9906e9SPeter Wemm 	}
789fb59c426SYoshinobu Inoue #ifdef INET6
790fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
791fb59c426SYoshinobu Inoue #endif /* INET6 */
7929c9906e9SPeter Wemm 	tp = intotcpcb(inp);
7939c9906e9SPeter Wemm 	TCPDEBUG1();
7949c9906e9SPeter Wemm 	if (control) {
7959c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
7969c9906e9SPeter Wemm 		if (control->m_len) {
7979c9906e9SPeter Wemm 			m_freem(control);
7982c37256eSGarrett Wollman 			if (m)
7992c37256eSGarrett Wollman 				m_freem(m);
800744f87eaSDavid Greenman 			error = EINVAL;
801744f87eaSDavid Greenman 			goto out;
8022c37256eSGarrett Wollman 		}
8039c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
8049c9906e9SPeter Wemm 	}
8052c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
806395bb186SSam Leffler 		sbappendstream(&so->so_snd, m);
8072c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
8082c37256eSGarrett Wollman 			/*
8092c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
8102c37256eSGarrett Wollman 			 * initialize window to default value, and
8112c37256eSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
8122c37256eSGarrett Wollman 			 * MSS.
8132c37256eSGarrett Wollman 			 */
814603724d3SBjoern A. Zeeb 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
815fb59c426SYoshinobu Inoue #ifdef INET6
816fb59c426SYoshinobu Inoue 			if (isipv6)
817b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
818fb59c426SYoshinobu Inoue 			else
819fb59c426SYoshinobu Inoue #endif /* INET6 */
820b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
8212c37256eSGarrett Wollman 			if (error)
8222c37256eSGarrett Wollman 				goto out;
8232c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
8242c37256eSGarrett Wollman 			tcp_mss(tp, -1);
8252c37256eSGarrett Wollman 		}
8262c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
8272c37256eSGarrett Wollman 			/*
8282c37256eSGarrett Wollman 			 * Close the send side of the connection after
8292c37256eSGarrett Wollman 			 * the data is sent.
8302c37256eSGarrett Wollman 			 */
831603724d3SBjoern A. Zeeb 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
8322c37256eSGarrett Wollman 			socantsendmore(so);
833623dce13SRobert Watson 			tcp_usrclosed(tp);
8342c37256eSGarrett Wollman 		}
835623dce13SRobert Watson 		if (headlocked) {
836603724d3SBjoern A. Zeeb 			INP_INFO_WUNLOCK(&V_tcbinfo);
837623dce13SRobert Watson 			headlocked = 0;
838623dce13SRobert Watson 		}
839b0acefa8SBill Fenner 		if (tp != NULL) {
840b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
841b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
842bc65987aSKip Macy 			error = tcp_output_send(tp);
843b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
844b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
845b0acefa8SBill Fenner 		}
8462c37256eSGarrett Wollman 	} else {
847623dce13SRobert Watson 		/*
848623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
849623dce13SRobert Watson 		 */
850d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
8512c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
852d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
8532c37256eSGarrett Wollman 			m_freem(m);
8542c37256eSGarrett Wollman 			error = ENOBUFS;
8552c37256eSGarrett Wollman 			goto out;
8562c37256eSGarrett Wollman 		}
8572c37256eSGarrett Wollman 		/*
8582c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
8592c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
8602c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
8612c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
8622c37256eSGarrett Wollman 		 * of data past the urgent section.
8632c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
8642c37256eSGarrett Wollman 		 */
865d2bc35abSRobert Watson 		sbappendstream_locked(&so->so_snd, m);
866d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
867ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
868ef53690bSGarrett Wollman 			/*
869ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
870ef53690bSGarrett Wollman 			 * initialize window to default value, and
871ef53690bSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
872ef53690bSGarrett Wollman 			 * MSS.
873ef53690bSGarrett Wollman 			 */
874603724d3SBjoern A. Zeeb 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
875fb59c426SYoshinobu Inoue #ifdef INET6
876fb59c426SYoshinobu Inoue 			if (isipv6)
877b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
878fb59c426SYoshinobu Inoue 			else
879fb59c426SYoshinobu Inoue #endif /* INET6 */
880b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
881ef53690bSGarrett Wollman 			if (error)
882ef53690bSGarrett Wollman 				goto out;
883ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
884ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
885603724d3SBjoern A. Zeeb 			INP_INFO_WUNLOCK(&V_tcbinfo);
886623dce13SRobert Watson 			headlocked = 0;
887623dce13SRobert Watson 		} else if (nam) {
888603724d3SBjoern A. Zeeb 			INP_INFO_WUNLOCK(&V_tcbinfo);
889623dce13SRobert Watson 			headlocked = 0;
890623dce13SRobert Watson 		}
8912c37256eSGarrett Wollman 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
8922cdbfa66SPaul Saab 		tp->t_flags |= TF_FORCEDATA;
893bc65987aSKip Macy 		error = tcp_output_send(tp);
8942cdbfa66SPaul Saab 		tp->t_flags &= ~TF_FORCEDATA;
8952c37256eSGarrett Wollman 	}
896d1401c90SRobert Watson out:
897d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
8982c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
8998501a69cSRobert Watson 	INP_WUNLOCK(inp);
900623dce13SRobert Watson 	if (headlocked)
901603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
90273fddedaSPeter Grehan 	return (error);
9032c37256eSGarrett Wollman }
9042c37256eSGarrett Wollman 
9052c37256eSGarrett Wollman /*
906a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
9072c37256eSGarrett Wollman  */
908ac45e92fSRobert Watson static void
9092c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
9102c37256eSGarrett Wollman {
9118b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
912f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
913a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
914623dce13SRobert Watson 	TCPDEBUG0;
915c78cbc7bSRobert Watson 
916ac45e92fSRobert Watson 	inp = sotoinpcb(so);
917c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
918c78cbc7bSRobert Watson 
919603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
9208501a69cSRobert Watson 	INP_WLOCK(inp);
921c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
922c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
923c78cbc7bSRobert Watson 
924c78cbc7bSRobert Watson 	/*
925a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
926c78cbc7bSRobert Watson 	 */
927c78cbc7bSRobert Watson 	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
928c78cbc7bSRobert Watson 	    !(inp->inp_vflag & INP_DROPPED)) {
929c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
930a152f8a3SRobert Watson 		TCPDEBUG1();
931c78cbc7bSRobert Watson 		tcp_drop(tp, ECONNABORTED);
932a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
933c78cbc7bSRobert Watson 	}
934a152f8a3SRobert Watson 	if (!(inp->inp_vflag & INP_DROPPED)) {
935a152f8a3SRobert Watson 		SOCK_LOCK(so);
936a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
937a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
938a152f8a3SRobert Watson 		inp->inp_vflag |= INP_SOCKREF;
939a152f8a3SRobert Watson 	}
9408501a69cSRobert Watson 	INP_WUNLOCK(inp);
941603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
942a152f8a3SRobert Watson }
943a152f8a3SRobert Watson 
944a152f8a3SRobert Watson /*
945a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
946a152f8a3SRobert Watson  */
947a152f8a3SRobert Watson static void
948a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
949a152f8a3SRobert Watson {
9508b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
951a152f8a3SRobert Watson 	struct inpcb *inp;
952a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
953a152f8a3SRobert Watson 	TCPDEBUG0;
954a152f8a3SRobert Watson 
955a152f8a3SRobert Watson 	inp = sotoinpcb(so);
956a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
957a152f8a3SRobert Watson 
958603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
9598501a69cSRobert Watson 	INP_WLOCK(inp);
960a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
961a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
962a152f8a3SRobert Watson 
963a152f8a3SRobert Watson 	/*
964a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
965a152f8a3SRobert Watson 	 * a disconnect.
966a152f8a3SRobert Watson 	 */
967a152f8a3SRobert Watson 	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
968a152f8a3SRobert Watson 	    !(inp->inp_vflag & INP_DROPPED)) {
969a152f8a3SRobert Watson 		tp = intotcpcb(inp);
970a152f8a3SRobert Watson 		TCPDEBUG1();
971a152f8a3SRobert Watson 		tcp_disconnect(tp);
972a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
973a152f8a3SRobert Watson 	}
974a152f8a3SRobert Watson 	if (!(inp->inp_vflag & INP_DROPPED)) {
975a152f8a3SRobert Watson 		SOCK_LOCK(so);
976a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
977a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
978a152f8a3SRobert Watson 		inp->inp_vflag |= INP_SOCKREF;
979a152f8a3SRobert Watson 	}
9808501a69cSRobert Watson 	INP_WUNLOCK(inp);
981603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
9822c37256eSGarrett Wollman }
9832c37256eSGarrett Wollman 
9842c37256eSGarrett Wollman /*
9852c37256eSGarrett Wollman  * Receive out-of-band data.
9862c37256eSGarrett Wollman  */
9872c37256eSGarrett Wollman static int
9882c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
9892c37256eSGarrett Wollman {
9902c37256eSGarrett Wollman 	int error = 0;
991f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
992623dce13SRobert Watson 	struct tcpcb *tp = NULL;
9932c37256eSGarrett Wollman 
994623dce13SRobert Watson 	TCPDEBUG0;
995623dce13SRobert Watson 	inp = sotoinpcb(so);
996623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
9978501a69cSRobert Watson 	INP_WLOCK(inp);
998623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
99921367f63SSam Leffler 		error = ECONNRESET;
1000623dce13SRobert Watson 		goto out;
1001623dce13SRobert Watson 	}
1002623dce13SRobert Watson 	tp = intotcpcb(inp);
1003623dce13SRobert Watson 	TCPDEBUG1();
10042c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1005c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
10064cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
10074cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
10082c37256eSGarrett Wollman 		error = EINVAL;
10092c37256eSGarrett Wollman 		goto out;
10102c37256eSGarrett Wollman 	}
10112c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
10122c37256eSGarrett Wollman 		error = EWOULDBLOCK;
10132c37256eSGarrett Wollman 		goto out;
10142c37256eSGarrett Wollman 	}
10152c37256eSGarrett Wollman 	m->m_len = 1;
10162c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
10172c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
10182c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1019623dce13SRobert Watson 
1020623dce13SRobert Watson out:
1021623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
10228501a69cSRobert Watson 	INP_WUNLOCK(inp);
1023623dce13SRobert Watson 	return (error);
10242c37256eSGarrett Wollman }
10252c37256eSGarrett Wollman 
10262c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1027756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1028756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1029756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1030756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1031756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1032756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1033756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1034756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1035756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
103654d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1037756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1038756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1039756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
1040756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
104154d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1042a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1043a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
10442c37256eSGarrett Wollman };
1045df8bae1dSRodney W. Grimes 
1046fb59c426SYoshinobu Inoue #ifdef INET6
1047fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1048756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1049756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1050756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1051756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1052756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1053756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1054756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1055756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1056756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1057756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1058756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1059756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1060756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
1061756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1062756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1063a152f8a3SRobert Watson  	.pru_sosetlabel =	in_pcbsosetlabel,
1064a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1065fb59c426SYoshinobu Inoue };
1066fb59c426SYoshinobu Inoue #endif /* INET6 */
1067fb59c426SYoshinobu Inoue 
1068a0292f23SGarrett Wollman /*
1069a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1070a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
10715200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
10725200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
10735200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
10745200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1075a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1076a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1077a0292f23SGarrett Wollman  */
10780312fbe9SPoul-Henning Kamp static int
1079ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1080a0292f23SGarrett Wollman {
1081a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1082a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
10838b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
10845200e00eSIan Dowse 	struct in_addr laddr;
10855200e00eSIan Dowse 	u_short lport;
1086c3229e05SDavid Greenman 	int error;
1087a0292f23SGarrett Wollman 
1088603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
10898501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1090623dce13SRobert Watson 
1091a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
1092b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1093a0292f23SGarrett Wollman 		if (error)
1094a0292f23SGarrett Wollman 			return error;
1095a0292f23SGarrett Wollman 	}
1096a0292f23SGarrett Wollman 
1097a0292f23SGarrett Wollman 	/*
1098a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1099a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1100a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1101a0292f23SGarrett Wollman 	 */
11025200e00eSIan Dowse 	laddr = inp->inp_laddr;
11035200e00eSIan Dowse 	lport = inp->inp_lport;
11045200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1105b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
11065200e00eSIan Dowse 	if (error && oinp == NULL)
1107d3628763SRodney W. Grimes 		return error;
1108c94c54e4SAndre Oppermann 	if (oinp)
1109a0292f23SGarrett Wollman 		return EADDRINUSE;
11105200e00eSIan Dowse 	inp->inp_laddr = laddr;
111115bd2b43SDavid Greenman 	in_pcbrehash(inp);
1112a0292f23SGarrett Wollman 
1113087b55eaSAndre Oppermann 	/*
1114087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1115087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1116087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1117087b55eaSAndre Oppermann 	 */
1118a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
11199b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1120a0292f23SGarrett Wollman 		tp->request_r_scale++;
1121a0292f23SGarrett Wollman 
1122a0292f23SGarrett Wollman 	soisconnecting(so);
1123603724d3SBjoern A. Zeeb 	V_tcpstat.tcps_connattempt++;
1124a0292f23SGarrett Wollman 	tp->t_state = TCPS_SYN_SENT;
1125b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1126b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
11271fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
1128a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1129a45d2726SAndras Olah 
1130a0292f23SGarrett Wollman 	return 0;
1131a0292f23SGarrett Wollman }
1132a0292f23SGarrett Wollman 
1133fb59c426SYoshinobu Inoue #ifdef INET6
1134fb59c426SYoshinobu Inoue static int
1135ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1136fb59c426SYoshinobu Inoue {
1137fb59c426SYoshinobu Inoue 	struct inpcb *inp = tp->t_inpcb, *oinp;
1138fb59c426SYoshinobu Inoue 	struct socket *so = inp->inp_socket;
11398b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
1140fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1141fb59c426SYoshinobu Inoue 	struct in6_addr *addr6;
1142fb59c426SYoshinobu Inoue 	int error;
1143fb59c426SYoshinobu Inoue 
1144603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
11458501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1146623dce13SRobert Watson 
1147fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
1148b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1149fb59c426SYoshinobu Inoue 		if (error)
1150fb59c426SYoshinobu Inoue 			return error;
1151fb59c426SYoshinobu Inoue 	}
1152fb59c426SYoshinobu Inoue 
1153fb59c426SYoshinobu Inoue 	/*
1154fb59c426SYoshinobu Inoue 	 * Cannot simply call in_pcbconnect, because there might be an
1155fb59c426SYoshinobu Inoue 	 * earlier incarnation of this same connection still in
1156fb59c426SYoshinobu Inoue 	 * TIME_WAIT state, creating an ADDRINUSE error.
1157a1f7e5f8SHajimu UMEMOTO 	 * in6_pcbladdr() also handles scope zone IDs.
1158fb59c426SYoshinobu Inoue 	 */
1159fb59c426SYoshinobu Inoue 	error = in6_pcbladdr(inp, nam, &addr6);
1160fb59c426SYoshinobu Inoue 	if (error)
1161fb59c426SYoshinobu Inoue 		return error;
1162fb59c426SYoshinobu Inoue 	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1163fb59c426SYoshinobu Inoue 				  &sin6->sin6_addr, sin6->sin6_port,
1164fb59c426SYoshinobu Inoue 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1165fb59c426SYoshinobu Inoue 				  ? addr6
1166fb59c426SYoshinobu Inoue 				  : &inp->in6p_laddr,
1167fb59c426SYoshinobu Inoue 				  inp->inp_lport,  0, NULL);
1168c94c54e4SAndre Oppermann 	if (oinp)
1169fb59c426SYoshinobu Inoue 		return EADDRINUSE;
1170fb59c426SYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1171fb59c426SYoshinobu Inoue 		inp->in6p_laddr = *addr6;
1172fb59c426SYoshinobu Inoue 	inp->in6p_faddr = sin6->sin6_addr;
1173fb59c426SYoshinobu Inoue 	inp->inp_fport = sin6->sin6_port;
11748a59da30SHajimu UMEMOTO 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
11758a59da30SHajimu UMEMOTO 	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
11768a59da30SHajimu UMEMOTO 	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
11778a59da30SHajimu UMEMOTO 		inp->in6p_flowinfo |=
11788a59da30SHajimu UMEMOTO 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1179fb59c426SYoshinobu Inoue 	in_pcbrehash(inp);
1180fb59c426SYoshinobu Inoue 
1181fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1182fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1183fb59c426SYoshinobu Inoue 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1184fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1185fb59c426SYoshinobu Inoue 
1186fb59c426SYoshinobu Inoue 	soisconnecting(so);
1187603724d3SBjoern A. Zeeb 	V_tcpstat.tcps_connattempt++;
1188fb59c426SYoshinobu Inoue 	tp->t_state = TCPS_SYN_SENT;
1189b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1190b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
11911fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
1192fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1193fb59c426SYoshinobu Inoue 
1194fb59c426SYoshinobu Inoue 	return 0;
1195fb59c426SYoshinobu Inoue }
1196fb59c426SYoshinobu Inoue #endif /* INET6 */
1197fb59c426SYoshinobu Inoue 
1198cfe8b629SGarrett Wollman /*
1199b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1200b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1201b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1202b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1203b8af5dfaSRobert Watson  * from Linux.
1204b8af5dfaSRobert Watson  */
1205b8af5dfaSRobert Watson static void
1206ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1207b8af5dfaSRobert Watson {
1208b8af5dfaSRobert Watson 
12098501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1210b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1211b8af5dfaSRobert Watson 
1212b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1213b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1214b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
12153529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1216b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1217b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1218b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1219b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1220b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1221b8af5dfaSRobert Watson 	}
12221baaf834SBruce M Simpson 
12231baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
12241baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
12251baaf834SBruce M Simpson 
1226b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1227b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1228b8af5dfaSRobert Watson 
1229b8af5dfaSRobert Watson 	/*
1230b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1231b8af5dfaSRobert Watson 	 */
1232c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1233535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1234b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
1235b8af5dfaSRobert Watson 	ti->tcpi_snd_bwnd = tp->snd_bwnd;
1236535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
1237535fbad6SKip Macy 	ti->__tcpi_snd_mss = tp->t_maxseg;
1238535fbad6SKip Macy 	ti->__tcpi_rcv_mss = tp->t_maxseg;
1239535fbad6SKip Macy 	if (tp->t_flags & TF_TOE)
1240535fbad6SKip Macy 		ti->tcpi_options |= TCPI_OPT_TOE;
1241b8af5dfaSRobert Watson }
1242b8af5dfaSRobert Watson 
1243b8af5dfaSRobert Watson /*
12441e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
12451e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
12461e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
12471e8f5ffaSRobert Watson  * option.
1248cfe8b629SGarrett Wollman  */
12498501a69cSRobert Watson #define INP_WLOCK_RECHECK(inp) do {					\
12508501a69cSRobert Watson 	INP_WLOCK(inp);							\
12511e8f5ffaSRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {		\
12528501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
12531e8f5ffaSRobert Watson 		return (ECONNRESET);					\
12541e8f5ffaSRobert Watson 	}								\
12551e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
12561e8f5ffaSRobert Watson } while(0)
12571e8f5ffaSRobert Watson 
1258df8bae1dSRodney W. Grimes int
1259ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1260df8bae1dSRodney W. Grimes {
12618b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
12623f9d1ef9SRobert Watson 	int	error, opt, optval;
1263df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1264cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
1265b8af5dfaSRobert Watson 	struct	tcp_info ti;
1266df8bae1dSRodney W. Grimes 
1267cfe8b629SGarrett Wollman 	error = 0;
1268df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1269623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
12708501a69cSRobert Watson 	INP_WLOCK(inp);
1271cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1272fb59c426SYoshinobu Inoue #ifdef INET6
12731e8f5ffaSRobert Watson 		if (INP_CHECK_SOCKAF(so, AF_INET6)) {
12748501a69cSRobert Watson 			INP_WUNLOCK(inp);
1275fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
12761e8f5ffaSRobert Watson 		} else {
1277fb59c426SYoshinobu Inoue #endif /* INET6 */
12788501a69cSRobert Watson 			INP_WUNLOCK(inp);
1279cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
12801e8f5ffaSRobert Watson #ifdef INET6
12811e8f5ffaSRobert Watson 		}
12821e8f5ffaSRobert Watson #endif
1283df8bae1dSRodney W. Grimes 		return (error);
1284df8bae1dSRodney W. Grimes 	}
1285623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
12868501a69cSRobert Watson 		INP_WUNLOCK(inp);
12871e8f5ffaSRobert Watson 		return (ECONNRESET);
1288623dce13SRobert Watson 	}
1289df8bae1dSRodney W. Grimes 
1290cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1291cfe8b629SGarrett Wollman 	case SOPT_SET:
1292cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
12931cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
129488f6b043SBruce M Simpson 		case TCP_MD5SIG:
12958501a69cSRobert Watson 			INP_WUNLOCK(inp);
12961cfd4b53SBruce M Simpson 			error = sooptcopyin(sopt, &optval, sizeof optval,
12971cfd4b53SBruce M Simpson 			    sizeof optval);
12981cfd4b53SBruce M Simpson 			if (error)
12991e8f5ffaSRobert Watson 				return (error);
13001cfd4b53SBruce M Simpson 
13018501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
13021cfd4b53SBruce M Simpson 			if (optval > 0)
13031cfd4b53SBruce M Simpson 				tp->t_flags |= TF_SIGNATURE;
13041cfd4b53SBruce M Simpson 			else
13051cfd4b53SBruce M Simpson 				tp->t_flags &= ~TF_SIGNATURE;
13068501a69cSRobert Watson 			INP_WUNLOCK(inp);
13071cfd4b53SBruce M Simpson 			break;
13081cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
1309df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1310cfe8b629SGarrett Wollman 		case TCP_NOOPT:
13118501a69cSRobert Watson 			INP_WUNLOCK(inp);
1312cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1313cfe8b629SGarrett Wollman 			    sizeof optval);
1314cfe8b629SGarrett Wollman 			if (error)
13151e8f5ffaSRobert Watson 				return (error);
1316cfe8b629SGarrett Wollman 
13178501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1318cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1319cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1320cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1321cfe8b629SGarrett Wollman 				break;
1322cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1323cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1324cfe8b629SGarrett Wollman 				break;
1325cfe8b629SGarrett Wollman 			default:
1326cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1327cfe8b629SGarrett Wollman 				break;
1328cfe8b629SGarrett Wollman 			}
1329cfe8b629SGarrett Wollman 
1330cfe8b629SGarrett Wollman 			if (optval)
1331cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1332df8bae1dSRodney W. Grimes 			else
1333cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
13348501a69cSRobert Watson 			INP_WUNLOCK(inp);
1335df8bae1dSRodney W. Grimes 			break;
1336df8bae1dSRodney W. Grimes 
1337007581c0SJonathan Lemon 		case TCP_NOPUSH:
13388501a69cSRobert Watson 			INP_WUNLOCK(inp);
1339007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1340007581c0SJonathan Lemon 			    sizeof optval);
1341007581c0SJonathan Lemon 			if (error)
13421e8f5ffaSRobert Watson 				return (error);
1343007581c0SJonathan Lemon 
13448501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1345007581c0SJonathan Lemon 			if (optval)
1346007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1347007581c0SJonathan Lemon 			else {
1348007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1349007581c0SJonathan Lemon 				error = tcp_output(tp);
1350007581c0SJonathan Lemon 			}
13518501a69cSRobert Watson 			INP_WUNLOCK(inp);
1352007581c0SJonathan Lemon 			break;
1353007581c0SJonathan Lemon 
1354df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
13558501a69cSRobert Watson 			INP_WUNLOCK(inp);
1356cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1357cfe8b629SGarrett Wollman 			    sizeof optval);
1358cfe8b629SGarrett Wollman 			if (error)
13591e8f5ffaSRobert Watson 				return (error);
1360df8bae1dSRodney W. Grimes 
13618501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
136253369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
1363603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
1364cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1365a0292f23SGarrett Wollman 			else
1366a0292f23SGarrett Wollman 				error = EINVAL;
13678501a69cSRobert Watson 			INP_WUNLOCK(inp);
1368a0292f23SGarrett Wollman 			break;
1369a0292f23SGarrett Wollman 
1370b8af5dfaSRobert Watson 		case TCP_INFO:
13718501a69cSRobert Watson 			INP_WUNLOCK(inp);
1372b8af5dfaSRobert Watson 			error = EINVAL;
1373b8af5dfaSRobert Watson 			break;
1374b8af5dfaSRobert Watson 
1375df8bae1dSRodney W. Grimes 		default:
13768501a69cSRobert Watson 			INP_WUNLOCK(inp);
1377df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1378df8bae1dSRodney W. Grimes 			break;
1379df8bae1dSRodney W. Grimes 		}
1380df8bae1dSRodney W. Grimes 		break;
1381df8bae1dSRodney W. Grimes 
1382cfe8b629SGarrett Wollman 	case SOPT_GET:
13831e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
1384cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
13851cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
138688f6b043SBruce M Simpson 		case TCP_MD5SIG:
13871cfd4b53SBruce M Simpson 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
13888501a69cSRobert Watson 			INP_WUNLOCK(inp);
1389b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
13901cfd4b53SBruce M Simpson 			break;
1391265ed012SBruce M Simpson #endif
13921e8f5ffaSRobert Watson 
1393df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1394cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
13958501a69cSRobert Watson 			INP_WUNLOCK(inp);
1396b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1397df8bae1dSRodney W. Grimes 			break;
1398df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1399cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
14008501a69cSRobert Watson 			INP_WUNLOCK(inp);
1401b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1402df8bae1dSRodney W. Grimes 			break;
1403a0292f23SGarrett Wollman 		case TCP_NOOPT:
1404cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
14058501a69cSRobert Watson 			INP_WUNLOCK(inp);
1406b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1407a0292f23SGarrett Wollman 			break;
1408a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1409cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
14108501a69cSRobert Watson 			INP_WUNLOCK(inp);
1411b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1412b8af5dfaSRobert Watson 			break;
1413b8af5dfaSRobert Watson 		case TCP_INFO:
1414b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
14158501a69cSRobert Watson 			INP_WUNLOCK(inp);
1416b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
1417a0292f23SGarrett Wollman 			break;
1418df8bae1dSRodney W. Grimes 		default:
14198501a69cSRobert Watson 			INP_WUNLOCK(inp);
1420df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1421df8bae1dSRodney W. Grimes 			break;
1422df8bae1dSRodney W. Grimes 		}
1423df8bae1dSRodney W. Grimes 		break;
1424df8bae1dSRodney W. Grimes 	}
1425df8bae1dSRodney W. Grimes 	return (error);
1426df8bae1dSRodney W. Grimes }
14278501a69cSRobert Watson #undef INP_WLOCK_RECHECK
1428df8bae1dSRodney W. Grimes 
142926e30fbbSDavid Greenman /*
143026e30fbbSDavid Greenman  * tcp_sendspace and tcp_recvspace are the default send and receive window
143126e30fbbSDavid Greenman  * sizes, respectively.  These are obsolescent (this information should
143226e30fbbSDavid Greenman  * be set by the route).
143326e30fbbSDavid Greenman  */
143481e561cdSDavid E. O'Brien u_long	tcp_sendspace = 1024*32;
1435e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
14363d177f46SBill Fumerola     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
143781e561cdSDavid E. O'Brien u_long	tcp_recvspace = 1024*64;
1438e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
14393d177f46SBill Fumerola     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1440df8bae1dSRodney W. Grimes 
1441df8bae1dSRodney W. Grimes /*
1442df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1443df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1444df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1445df8bae1dSRodney W. Grimes  */
14460312fbe9SPoul-Henning Kamp static int
1447ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so)
1448df8bae1dSRodney W. Grimes {
14498b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
1450ad3f9ab3SAndre Oppermann 	struct tcpcb *tp;
1451df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1452df8bae1dSRodney W. Grimes 	int error;
1453fb59c426SYoshinobu Inoue #ifdef INET6
14544a6a94d8SArchie Cobbs 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1455fb59c426SYoshinobu Inoue #endif
1456df8bae1dSRodney W. Grimes 
1457df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1458df8bae1dSRodney W. Grimes 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1459df8bae1dSRodney W. Grimes 		if (error)
1460df8bae1dSRodney W. Grimes 			return (error);
1461df8bae1dSRodney W. Grimes 	}
14626741ecf5SAndre Oppermann 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
14636741ecf5SAndre Oppermann 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1464603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
1465603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_tcbinfo);
1466f2de87feSRobert Watson 	if (error) {
1467603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
1468df8bae1dSRodney W. Grimes 		return (error);
1469f2de87feSRobert Watson 	}
1470df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1471fb59c426SYoshinobu Inoue #ifdef INET6
1472fb59c426SYoshinobu Inoue 	if (isipv6) {
1473fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1474fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1475fb59c426SYoshinobu Inoue 	}
1476fb59c426SYoshinobu Inoue 	else
1477fb59c426SYoshinobu Inoue #endif
1478cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1479df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1480623dce13SRobert Watson 	if (tp == NULL) {
1481df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
1482623dce13SRobert Watson #ifdef INET6
14830206cdb8SBjoern A. Zeeb 		if (isipv6)
14840206cdb8SBjoern A. Zeeb 			in6_pcbfree(inp);
14850206cdb8SBjoern A. Zeeb 		else
1486623dce13SRobert Watson #endif
14870206cdb8SBjoern A. Zeeb 			in_pcbfree(inp);
1488603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
1489df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1490df8bae1dSRodney W. Grimes 	}
1491df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
14928501a69cSRobert Watson 	INP_WUNLOCK(inp);
1493603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
1494df8bae1dSRodney W. Grimes 	return (0);
1495df8bae1dSRodney W. Grimes }
1496df8bae1dSRodney W. Grimes 
1497df8bae1dSRodney W. Grimes /*
1498df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1499df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1500df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1501df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1502df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1503df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1504df8bae1dSRodney W. Grimes  */
1505623dce13SRobert Watson static void
1506ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
1507df8bae1dSRodney W. Grimes {
1508e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
1509e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
15108b615593SMarko Zec #ifdef INVARIANTS
15118b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
15128b615593SMarko Zec #endif
1513e6e0b5ffSRobert Watson 
1514603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
15158501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1516df8bae1dSRodney W. Grimes 
1517623dce13SRobert Watson 	/*
1518623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1519623dce13SRobert Watson 	 * socket is still open.
1520623dce13SRobert Watson 	 */
1521623dce13SRobert Watson 	if (tp->t_state < TCPS_ESTABLISHED) {
1522df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1523623dce13SRobert Watson 		KASSERT(tp != NULL,
1524623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
1525623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1526243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
1527623dce13SRobert Watson 		KASSERT(tp != NULL,
1528623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
1529623dce13SRobert Watson 	} else {
1530df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
1531df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
1532623dce13SRobert Watson 		tcp_usrclosed(tp);
1533953b5606SRobert Watson 		if (!(inp->inp_vflag & INP_DROPPED))
1534bc65987aSKip Macy 			tcp_output_disconnect(tp);
1535df8bae1dSRodney W. Grimes 	}
1536df8bae1dSRodney W. Grimes }
1537df8bae1dSRodney W. Grimes 
1538df8bae1dSRodney W. Grimes /*
1539df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1540df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1541df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1542df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1543df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1544df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1545df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1546df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1547df8bae1dSRodney W. Grimes  */
1548623dce13SRobert Watson static void
1549ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
1550df8bae1dSRodney W. Grimes {
15518b615593SMarko Zec #ifdef INVARIANTS
15528b615593SMarko Zec 	INIT_VNET_INET(tp->t_inpcb->inp_vnet);
15538b615593SMarko Zec #endif
1554df8bae1dSRodney W. Grimes 
1555603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
15568501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1557e6e0b5ffSRobert Watson 
1558df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1559df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
1560bc65987aSKip Macy 		tcp_offload_listen_close(tp);
1561bc65987aSKip Macy 		/* FALLTHROUGH */
1562bc65987aSKip Macy 	case TCPS_CLOSED:
1563df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_CLOSED;
1564df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1565623dce13SRobert Watson 		/*
1566623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
1567623dce13SRobert Watson 		 * still open.
1568623dce13SRobert Watson 		 */
1569623dce13SRobert Watson 		KASSERT(tp != NULL,
1570623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
1571df8bae1dSRodney W. Grimes 		break;
1572df8bae1dSRodney W. Grimes 
1573a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
1574df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1575a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
1576a0292f23SGarrett Wollman 		break;
1577a0292f23SGarrett Wollman 
1578df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1579df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_FIN_WAIT_1;
1580df8bae1dSRodney W. Grimes 		break;
1581df8bae1dSRodney W. Grimes 
1582df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1583df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_LAST_ACK;
1584df8bae1dSRodney W. Grimes 		break;
1585df8bae1dSRodney W. Grimes 	}
1586abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
1587df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
1588abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
15897c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
15907c72af87SMohan Srinivasan 			int timeout;
15917c72af87SMohan Srinivasan 
15927c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
15937c72af87SMohan Srinivasan 			    tcp_finwait2_timeout : tcp_maxidle;
1594b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
1595b6239c4aSAndras Olah 		}
1596df8bae1dSRodney W. Grimes 	}
15977c72af87SMohan Srinivasan }
1598497057eeSRobert Watson 
1599497057eeSRobert Watson #ifdef DDB
1600497057eeSRobert Watson static void
1601497057eeSRobert Watson db_print_indent(int indent)
1602497057eeSRobert Watson {
1603497057eeSRobert Watson 	int i;
1604497057eeSRobert Watson 
1605497057eeSRobert Watson 	for (i = 0; i < indent; i++)
1606497057eeSRobert Watson 		db_printf(" ");
1607497057eeSRobert Watson }
1608497057eeSRobert Watson 
1609497057eeSRobert Watson static void
1610497057eeSRobert Watson db_print_tstate(int t_state)
1611497057eeSRobert Watson {
1612497057eeSRobert Watson 
1613497057eeSRobert Watson 	switch (t_state) {
1614497057eeSRobert Watson 	case TCPS_CLOSED:
1615497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
1616497057eeSRobert Watson 		return;
1617497057eeSRobert Watson 
1618497057eeSRobert Watson 	case TCPS_LISTEN:
1619497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
1620497057eeSRobert Watson 		return;
1621497057eeSRobert Watson 
1622497057eeSRobert Watson 	case TCPS_SYN_SENT:
1623497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
1624497057eeSRobert Watson 		return;
1625497057eeSRobert Watson 
1626497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
1627497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
1628497057eeSRobert Watson 		return;
1629497057eeSRobert Watson 
1630497057eeSRobert Watson 	case TCPS_ESTABLISHED:
1631497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
1632497057eeSRobert Watson 		return;
1633497057eeSRobert Watson 
1634497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
1635497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
1636497057eeSRobert Watson 		return;
1637497057eeSRobert Watson 
1638497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
1639497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
1640497057eeSRobert Watson 		return;
1641497057eeSRobert Watson 
1642497057eeSRobert Watson 	case TCPS_CLOSING:
1643497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
1644497057eeSRobert Watson 		return;
1645497057eeSRobert Watson 
1646497057eeSRobert Watson 	case TCPS_LAST_ACK:
1647497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
1648497057eeSRobert Watson 		return;
1649497057eeSRobert Watson 
1650497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
1651497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
1652497057eeSRobert Watson 		return;
1653497057eeSRobert Watson 
1654497057eeSRobert Watson 	case TCPS_TIME_WAIT:
1655497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
1656497057eeSRobert Watson 		return;
1657497057eeSRobert Watson 
1658497057eeSRobert Watson 	default:
1659497057eeSRobert Watson 		db_printf("unknown");
1660497057eeSRobert Watson 		return;
1661497057eeSRobert Watson 	}
1662497057eeSRobert Watson }
1663497057eeSRobert Watson 
1664497057eeSRobert Watson static void
1665497057eeSRobert Watson db_print_tflags(u_int t_flags)
1666497057eeSRobert Watson {
1667497057eeSRobert Watson 	int comma;
1668497057eeSRobert Watson 
1669497057eeSRobert Watson 	comma = 0;
1670497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
1671497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
1672497057eeSRobert Watson 		comma = 1;
1673497057eeSRobert Watson 	}
1674497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
1675497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
1676497057eeSRobert Watson 		comma = 1;
1677497057eeSRobert Watson 	}
1678497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
1679497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
1680497057eeSRobert Watson 		comma = 1;
1681497057eeSRobert Watson 	}
1682497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
1683497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
1684497057eeSRobert Watson 		comma = 1;
1685497057eeSRobert Watson 	}
1686497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
1687497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
1688497057eeSRobert Watson 		comma = 1;
1689497057eeSRobert Watson 	}
1690497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
1691497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1692497057eeSRobert Watson 		comma = 1;
1693497057eeSRobert Watson 	}
1694497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
1695497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1696497057eeSRobert Watson 		comma = 1;
1697497057eeSRobert Watson 	}
1698497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
1699497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1700497057eeSRobert Watson 		comma = 1;
1701497057eeSRobert Watson 	}
1702497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
1703497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1704497057eeSRobert Watson 		comma = 1;
1705497057eeSRobert Watson 	}
1706497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
1707497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1708497057eeSRobert Watson 		comma = 1;
1709497057eeSRobert Watson 	}
1710497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
1711497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1712497057eeSRobert Watson 		comma = 1;
1713497057eeSRobert Watson 	}
1714497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
1715497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1716497057eeSRobert Watson 		comma = 1;
1717497057eeSRobert Watson 	}
1718497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
1719497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
1720497057eeSRobert Watson 		comma = 1;
1721497057eeSRobert Watson 	}
1722497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
1723497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
1724497057eeSRobert Watson 		comma = 1;
1725497057eeSRobert Watson 	}
1726497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
1727497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1728497057eeSRobert Watson 		comma = 1;
1729497057eeSRobert Watson 	}
1730497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
1731497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1732497057eeSRobert Watson 		comma = 1;
1733497057eeSRobert Watson 	}
1734497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
1735497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1736497057eeSRobert Watson 		comma = 1;
1737497057eeSRobert Watson 	}
1738497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
1739497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1740497057eeSRobert Watson 		comma = 1;
1741497057eeSRobert Watson 	}
1742497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
1743497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1744497057eeSRobert Watson 		comma = 1;
1745497057eeSRobert Watson 	}
1746497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
1747497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1748497057eeSRobert Watson 		comma = 1;
1749497057eeSRobert Watson 	}
1750497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
1751497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1752497057eeSRobert Watson 		comma = 1;
1753497057eeSRobert Watson 	}
1754497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
1755497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1756497057eeSRobert Watson 		comma = 1;
1757497057eeSRobert Watson 	}
1758497057eeSRobert Watson 	if (t_flags & TF_TSO) {
1759497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
1760497057eeSRobert Watson 		comma = 1;
1761497057eeSRobert Watson 	}
1762f2512ba1SRui Paulo 	if (t_flags & TF_ECN_PERMIT) {
1763f2512ba1SRui Paulo 		db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
1764f2512ba1SRui Paulo 		comma = 1;
1765f2512ba1SRui Paulo 	}
1766497057eeSRobert Watson }
1767497057eeSRobert Watson 
1768497057eeSRobert Watson static void
1769497057eeSRobert Watson db_print_toobflags(char t_oobflags)
1770497057eeSRobert Watson {
1771497057eeSRobert Watson 	int comma;
1772497057eeSRobert Watson 
1773497057eeSRobert Watson 	comma = 0;
1774497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
1775497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1776497057eeSRobert Watson 		comma = 1;
1777497057eeSRobert Watson 	}
1778497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
1779497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1780497057eeSRobert Watson 		comma = 1;
1781497057eeSRobert Watson 	}
1782497057eeSRobert Watson }
1783497057eeSRobert Watson 
1784497057eeSRobert Watson static void
1785497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1786497057eeSRobert Watson {
1787497057eeSRobert Watson 
1788497057eeSRobert Watson 	db_print_indent(indent);
1789497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
1790497057eeSRobert Watson 
1791497057eeSRobert Watson 	indent += 2;
1792497057eeSRobert Watson 
1793497057eeSRobert Watson 	db_print_indent(indent);
1794497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
1795497057eeSRobert Watson 	   LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
1796497057eeSRobert Watson 
1797497057eeSRobert Watson 	db_print_indent(indent);
179885d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
1799e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
1800497057eeSRobert Watson 
1801497057eeSRobert Watson 	db_print_indent(indent);
1802e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
1803e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
1804497057eeSRobert Watson 
1805497057eeSRobert Watson 	db_print_indent(indent);
1806497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
1807497057eeSRobert Watson 	db_print_tstate(tp->t_state);
1808497057eeSRobert Watson 	db_printf(")\n");
1809497057eeSRobert Watson 
1810497057eeSRobert Watson 	db_print_indent(indent);
1811497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
1812497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
1813497057eeSRobert Watson 	db_printf(")\n");
1814497057eeSRobert Watson 
1815497057eeSRobert Watson 	db_print_indent(indent);
1816497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
1817497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
1818497057eeSRobert Watson 
1819497057eeSRobert Watson 	db_print_indent(indent);
1820497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
1821497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
1822497057eeSRobert Watson 
1823497057eeSRobert Watson 	db_print_indent(indent);
1824497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
1825497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
1826497057eeSRobert Watson 
1827497057eeSRobert Watson 	db_print_indent(indent);
1828497057eeSRobert Watson 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %lu   rcv_up: 0x%08x\n",
1829497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
1830497057eeSRobert Watson 
1831497057eeSRobert Watson 	db_print_indent(indent);
1832497057eeSRobert Watson 	db_printf("snd_wnd: %lu   snd_cwnd: %lu   snd_bwnd: %lu\n",
1833497057eeSRobert Watson 	   tp->snd_wnd, tp->snd_cwnd, tp->snd_bwnd);
1834497057eeSRobert Watson 
1835497057eeSRobert Watson 	db_print_indent(indent);
1836497057eeSRobert Watson 	db_printf("snd_ssthresh: %lu   snd_bandwidth: %lu   snd_recover: "
1837497057eeSRobert Watson 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_bandwidth,
1838497057eeSRobert Watson 	    tp->snd_recover);
1839497057eeSRobert Watson 
1840497057eeSRobert Watson 	db_print_indent(indent);
1841497057eeSRobert Watson 	db_printf("t_maxopd: %u   t_rcvtime: %lu   t_startime: %lu\n",
1842497057eeSRobert Watson 	    tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
1843497057eeSRobert Watson 
1844497057eeSRobert Watson 	db_print_indent(indent);
1845497057eeSRobert Watson 	db_printf("t_rttime: %d   t_rtsq: 0x%08x   t_bw_rtttime: %d\n",
1846497057eeSRobert Watson 	    tp->t_rtttime, tp->t_rtseq, tp->t_bw_rtttime);
1847497057eeSRobert Watson 
1848497057eeSRobert Watson 	db_print_indent(indent);
1849497057eeSRobert Watson 	db_printf("t_bw_rtseq: 0x%08x   t_rxtcur: %d   t_maxseg: %u   "
1850497057eeSRobert Watson 	    "t_srtt: %d\n", tp->t_bw_rtseq, tp->t_rxtcur, tp->t_maxseg,
1851497057eeSRobert Watson 	    tp->t_srtt);
1852497057eeSRobert Watson 
1853497057eeSRobert Watson 	db_print_indent(indent);
1854497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
1855497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
1856497057eeSRobert Watson 	    tp->t_rttbest);
1857497057eeSRobert Watson 
1858497057eeSRobert Watson 	db_print_indent(indent);
1859497057eeSRobert Watson 	db_printf("t_rttupdated: %lu   max_sndwnd: %lu   t_softerror: %d\n",
1860497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
1861497057eeSRobert Watson 
1862497057eeSRobert Watson 	db_print_indent(indent);
1863497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
1864497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
1865497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
1866497057eeSRobert Watson 
1867497057eeSRobert Watson 	db_print_indent(indent);
1868497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
1869497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
1870497057eeSRobert Watson 
1871497057eeSRobert Watson 	db_print_indent(indent);
18721a553740SAndre Oppermann 	db_printf("ts_recent: %u   ts_recent_age: %lu\n",
18731a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
1874497057eeSRobert Watson 
1875497057eeSRobert Watson 	db_print_indent(indent);
1876497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
1877497057eeSRobert Watson 	    "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
1878497057eeSRobert Watson 
1879497057eeSRobert Watson 	db_print_indent(indent);
1880497057eeSRobert Watson 	db_printf("snd_ssthresh_prev: %lu   snd_recover_prev: 0x%08x   "
1881497057eeSRobert Watson 	    "t_badrxtwin: %lu\n", tp->snd_ssthresh_prev,
1882497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
1883497057eeSRobert Watson 
1884497057eeSRobert Watson 	db_print_indent(indent);
18853529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
18863529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
1887497057eeSRobert Watson 
1888497057eeSRobert Watson 	db_print_indent(indent);
1889497057eeSRobert Watson 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
1890497057eeSRobert Watson 	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
1891497057eeSRobert Watson 
1892497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
1893497057eeSRobert Watson 
1894497057eeSRobert Watson 	db_print_indent(indent);
1895497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
1896497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
1897497057eeSRobert Watson }
1898497057eeSRobert Watson 
1899497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
1900497057eeSRobert Watson {
1901497057eeSRobert Watson 	struct tcpcb *tp;
1902497057eeSRobert Watson 
1903497057eeSRobert Watson 	if (!have_addr) {
1904497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
1905497057eeSRobert Watson 		return;
1906497057eeSRobert Watson 	}
1907497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
1908497057eeSRobert Watson 
1909497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
1910497057eeSRobert Watson }
1911497057eeSRobert Watson #endif
1912