xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 43d94734991b48232bd5c724c6ffd9a5e1aa0708)
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>
56df8bae1dSRodney W. Grimes 
57497057eeSRobert Watson #ifdef DDB
58497057eeSRobert Watson #include <ddb/ddb.h>
59497057eeSRobert Watson #endif
60497057eeSRobert Watson 
61df8bae1dSRodney W. Grimes #include <net/if.h>
62df8bae1dSRodney W. Grimes #include <net/route.h>
63530c0060SRobert Watson #include <net/vnet.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;
1592c37256eSGarrett Wollman 
160603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1618501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
162623dce13SRobert Watson 
163c78cbc7bSRobert Watson 	KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
164c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
165953b5606SRobert Watson 
166a152f8a3SRobert Watson 	tp = intotcpcb(inp);
167a152f8a3SRobert Watson 
168ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
169623dce13SRobert Watson 		/*
170a152f8a3SRobert Watson 		 * There are two cases to handle: one in which the time wait
171a152f8a3SRobert Watson 		 * state is being discarded (INP_DROPPED), and one in which
172a152f8a3SRobert Watson 		 * this connection will remain in timewait.  In the former,
173a152f8a3SRobert Watson 		 * it is time to discard all state (except tcptw, which has
174a152f8a3SRobert Watson 		 * already been discarded by the timewait close code, which
175a152f8a3SRobert Watson 		 * should be further up the call stack somewhere).  In the
176a152f8a3SRobert Watson 		 * latter case, we detach from the socket, but leave the pcb
177a152f8a3SRobert Watson 		 * present until timewait ends.
178623dce13SRobert Watson 		 *
179a152f8a3SRobert Watson 		 * XXXRW: Would it be cleaner to free the tcptw here?
180623dce13SRobert Watson 		 */
181ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED) {
182a152f8a3SRobert Watson 			KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
183a152f8a3SRobert Watson 			    "INP_DROPPED && tp != NULL"));
184623dce13SRobert Watson 			in_pcbdetach(inp);
1850206cdb8SBjoern A. Zeeb 			in_pcbfree(inp);
1860206cdb8SBjoern A. Zeeb 		} else {
187623dce13SRobert Watson 			in_pcbdetach(inp);
1888501a69cSRobert Watson 			INP_WUNLOCK(inp);
189623dce13SRobert Watson 		}
190623dce13SRobert Watson 	} else {
191e6e65783SRobert Watson 		/*
192a152f8a3SRobert Watson 		 * If the connection is not in timewait, we consider two
193a152f8a3SRobert Watson 		 * two conditions: one in which no further processing is
194a152f8a3SRobert Watson 		 * necessary (dropped || embryonic), and one in which TCP is
195a152f8a3SRobert Watson 		 * not yet done, but no longer requires the socket, so the
196a152f8a3SRobert Watson 		 * pcb will persist for the time being.
197a152f8a3SRobert Watson 		 *
198a152f8a3SRobert Watson 		 * XXXRW: Does the second case still occur?
199e6e65783SRobert Watson 		 */
200ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED ||
201623dce13SRobert Watson 		    tp->t_state < TCPS_SYN_SENT) {
202623dce13SRobert Watson 			tcp_discardcb(tp);
203623dce13SRobert Watson 			in_pcbdetach(inp);
2040206cdb8SBjoern A. Zeeb 			in_pcbfree(inp);
2056aee2fc5SBjoern A. Zeeb 		} else
206a152f8a3SRobert Watson 			in_pcbdetach(inp);
207623dce13SRobert Watson 	}
208623dce13SRobert Watson }
209c78cbc7bSRobert Watson 
210c78cbc7bSRobert Watson /*
211c78cbc7bSRobert Watson  * pru_detach() detaches the TCP protocol from the socket.
212c78cbc7bSRobert Watson  * If the protocol state is non-embryonic, then can't
213c78cbc7bSRobert Watson  * do this directly: have to initiate a pru_disconnect(),
214c78cbc7bSRobert Watson  * which may finish later; embryonic TCB's can just
215c78cbc7bSRobert Watson  * be discarded here.
216c78cbc7bSRobert Watson  */
217c78cbc7bSRobert Watson static void
218c78cbc7bSRobert Watson tcp_usr_detach(struct socket *so)
219c78cbc7bSRobert Watson {
220c78cbc7bSRobert Watson 	struct inpcb *inp;
221c78cbc7bSRobert Watson 
222c78cbc7bSRobert Watson 	inp = sotoinpcb(so);
223c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
224603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
2258501a69cSRobert Watson 	INP_WLOCK(inp);
226c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
227c78cbc7bSRobert Watson 	    ("tcp_usr_detach: inp_socket == NULL"));
228c78cbc7bSRobert Watson 	tcp_detach(so, inp);
229603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
2302c37256eSGarrett Wollman }
2312c37256eSGarrett Wollman 
2322c37256eSGarrett Wollman /*
2332c37256eSGarrett Wollman  * Give the socket an address.
2342c37256eSGarrett Wollman  */
2352c37256eSGarrett Wollman static int
236b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2372c37256eSGarrett Wollman {
2382c37256eSGarrett Wollman 	int error = 0;
239f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
240623dce13SRobert Watson 	struct tcpcb *tp = NULL;
2412c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
2422c37256eSGarrett Wollman 
24352710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
24452710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sinp))
24552710de1SPawel Jakub Dawidek 		return (EINVAL);
2462c37256eSGarrett Wollman 	/*
2472c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2482c37256eSGarrett Wollman 	 * to them.
2492c37256eSGarrett Wollman 	 */
2502c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
25152710de1SPawel Jakub Dawidek 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
25252710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
25352710de1SPawel Jakub Dawidek 
254623dce13SRobert Watson 	TCPDEBUG0;
255603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
256623dce13SRobert Watson 	inp = sotoinpcb(so);
257623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
2588501a69cSRobert Watson 	INP_WLOCK(inp);
259ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
260623dce13SRobert Watson 		error = EINVAL;
2612c37256eSGarrett Wollman 		goto out;
262623dce13SRobert Watson 	}
263623dce13SRobert Watson 	tp = intotcpcb(inp);
264623dce13SRobert Watson 	TCPDEBUG1();
265623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
266623dce13SRobert Watson out:
267623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
2688501a69cSRobert Watson 	INP_WUNLOCK(inp);
269603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
270623dce13SRobert Watson 
271623dce13SRobert Watson 	return (error);
2722c37256eSGarrett Wollman }
2732c37256eSGarrett Wollman 
274fb59c426SYoshinobu Inoue #ifdef INET6
275fb59c426SYoshinobu Inoue static int
276b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
277fb59c426SYoshinobu Inoue {
278fb59c426SYoshinobu Inoue 	int error = 0;
279f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
280623dce13SRobert Watson 	struct tcpcb *tp = NULL;
281fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
282fb59c426SYoshinobu Inoue 
28352710de1SPawel Jakub Dawidek 	sin6p = (struct sockaddr_in6 *)nam;
28452710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sin6p))
28552710de1SPawel Jakub Dawidek 		return (EINVAL);
286fb59c426SYoshinobu Inoue 	/*
287fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
288fb59c426SYoshinobu Inoue 	 * to them.
289fb59c426SYoshinobu Inoue 	 */
290fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6 &&
29152710de1SPawel Jakub Dawidek 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
29252710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
29352710de1SPawel Jakub Dawidek 
294623dce13SRobert Watson 	TCPDEBUG0;
295603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
296623dce13SRobert Watson 	inp = sotoinpcb(so);
297623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
2988501a69cSRobert Watson 	INP_WLOCK(inp);
299ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
300623dce13SRobert Watson 		error = EINVAL;
301623dce13SRobert Watson 		goto out;
302623dce13SRobert Watson 	}
303623dce13SRobert Watson 	tp = intotcpcb(inp);
304623dce13SRobert Watson 	TCPDEBUG1();
305fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
306fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
30766ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
308fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
309fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
310fb59c426SYoshinobu Inoue 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
311fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
312fb59c426SYoshinobu Inoue 
313fb59c426SYoshinobu Inoue 			in6_sin6_2_sin(&sin, sin6p);
314fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
315fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
316b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
317b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
318fb59c426SYoshinobu Inoue 			goto out;
319fb59c426SYoshinobu Inoue 		}
320fb59c426SYoshinobu Inoue 	}
321b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
322623dce13SRobert Watson out:
323623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3248501a69cSRobert Watson 	INP_WUNLOCK(inp);
325603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
326623dce13SRobert Watson 	return (error);
327fb59c426SYoshinobu Inoue }
328fb59c426SYoshinobu Inoue #endif /* INET6 */
329fb59c426SYoshinobu Inoue 
3302c37256eSGarrett Wollman /*
3312c37256eSGarrett Wollman  * Prepare to accept connections.
3322c37256eSGarrett Wollman  */
3332c37256eSGarrett Wollman static int
334d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
3352c37256eSGarrett Wollman {
3362c37256eSGarrett Wollman 	int error = 0;
337f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
338623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3392c37256eSGarrett Wollman 
340623dce13SRobert Watson 	TCPDEBUG0;
341603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
342623dce13SRobert Watson 	inp = sotoinpcb(so);
343623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
3448501a69cSRobert Watson 	INP_WLOCK(inp);
345ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
346623dce13SRobert Watson 		error = EINVAL;
347623dce13SRobert Watson 		goto out;
348623dce13SRobert Watson 	}
349623dce13SRobert Watson 	tp = intotcpcb(inp);
350623dce13SRobert Watson 	TCPDEBUG1();
3510daccb9cSRobert Watson 	SOCK_LOCK(so);
3520daccb9cSRobert Watson 	error = solisten_proto_check(so);
3530daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0)
354b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
3550daccb9cSRobert Watson 	if (error == 0) {
3562c37256eSGarrett Wollman 		tp->t_state = TCPS_LISTEN;
357d374e81eSRobert Watson 		solisten_proto(so, backlog);
358bc65987aSKip Macy 		tcp_offload_listen_open(tp);
3590daccb9cSRobert Watson 	}
3600daccb9cSRobert Watson 	SOCK_UNLOCK(so);
361623dce13SRobert Watson 
362623dce13SRobert Watson out:
363623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
3648501a69cSRobert Watson 	INP_WUNLOCK(inp);
365603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
366623dce13SRobert Watson 	return (error);
3672c37256eSGarrett Wollman }
3682c37256eSGarrett Wollman 
369fb59c426SYoshinobu Inoue #ifdef INET6
370fb59c426SYoshinobu Inoue static int
371d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
372fb59c426SYoshinobu Inoue {
373fb59c426SYoshinobu Inoue 	int error = 0;
374f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
375623dce13SRobert Watson 	struct tcpcb *tp = NULL;
376fb59c426SYoshinobu Inoue 
377623dce13SRobert Watson 	TCPDEBUG0;
378603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
379623dce13SRobert Watson 	inp = sotoinpcb(so);
380623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
3818501a69cSRobert Watson 	INP_WLOCK(inp);
382ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
383623dce13SRobert Watson 		error = EINVAL;
384623dce13SRobert Watson 		goto out;
385623dce13SRobert Watson 	}
386623dce13SRobert Watson 	tp = intotcpcb(inp);
387623dce13SRobert Watson 	TCPDEBUG1();
3880daccb9cSRobert Watson 	SOCK_LOCK(so);
3890daccb9cSRobert Watson 	error = solisten_proto_check(so);
3900daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0) {
391fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
39266ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
393fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
394b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
395fb59c426SYoshinobu Inoue 	}
3960daccb9cSRobert Watson 	if (error == 0) {
397fb59c426SYoshinobu Inoue 		tp->t_state = TCPS_LISTEN;
398d374e81eSRobert Watson 		solisten_proto(so, backlog);
3990daccb9cSRobert Watson 	}
4000daccb9cSRobert Watson 	SOCK_UNLOCK(so);
401623dce13SRobert Watson 
402623dce13SRobert Watson out:
403623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4048501a69cSRobert Watson 	INP_WUNLOCK(inp);
405603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
406623dce13SRobert Watson 	return (error);
407fb59c426SYoshinobu Inoue }
408fb59c426SYoshinobu Inoue #endif /* INET6 */
409fb59c426SYoshinobu Inoue 
4102c37256eSGarrett Wollman /*
4112c37256eSGarrett Wollman  * Initiate connection to peer.
4122c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
4132c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
4142c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
4152c37256eSGarrett Wollman  * Send initial segment on connection.
4162c37256eSGarrett Wollman  */
4172c37256eSGarrett Wollman static int
418b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
4192c37256eSGarrett Wollman {
4202c37256eSGarrett Wollman 	int error = 0;
421f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
422623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4232c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
4242c37256eSGarrett Wollman 
42557bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
426e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
427e29ef13fSDon Lewis 		return (EINVAL);
42852710de1SPawel Jakub Dawidek 	/*
42952710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
43052710de1SPawel Jakub Dawidek 	 */
4312c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
43252710de1SPawel Jakub Dawidek 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
43352710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
434b89e82ddSJamie Gritton 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
435b89e82ddSJamie Gritton 		return (error);
43675c13541SPoul-Henning Kamp 
437623dce13SRobert Watson 	TCPDEBUG0;
438603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
439623dce13SRobert Watson 	inp = sotoinpcb(so);
440623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
4418501a69cSRobert Watson 	INP_WLOCK(inp);
442ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
443623dce13SRobert Watson 		error = EINVAL;
444623dce13SRobert Watson 		goto out;
445623dce13SRobert Watson 	}
446623dce13SRobert Watson 	tp = intotcpcb(inp);
447623dce13SRobert Watson 	TCPDEBUG1();
448b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
4492c37256eSGarrett Wollman 		goto out;
450bc65987aSKip Macy 	error = tcp_output_connect(so, nam);
451623dce13SRobert Watson out:
452623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
4538501a69cSRobert Watson 	INP_WUNLOCK(inp);
454603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
455623dce13SRobert Watson 	return (error);
4562c37256eSGarrett Wollman }
4572c37256eSGarrett Wollman 
458fb59c426SYoshinobu Inoue #ifdef INET6
459fb59c426SYoshinobu Inoue static int
460b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
461fb59c426SYoshinobu Inoue {
462fb59c426SYoshinobu Inoue 	int error = 0;
463f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
464623dce13SRobert Watson 	struct tcpcb *tp = NULL;
465fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
466623dce13SRobert Watson 
467623dce13SRobert Watson 	TCPDEBUG0;
468fb59c426SYoshinobu Inoue 
469fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
470e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sin6p))
471e29ef13fSDon Lewis 		return (EINVAL);
47252710de1SPawel Jakub Dawidek 	/*
47352710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
47452710de1SPawel Jakub Dawidek 	 */
475fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6
47652710de1SPawel Jakub Dawidek 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
47752710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
478fb59c426SYoshinobu Inoue 
479603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
480623dce13SRobert Watson 	inp = sotoinpcb(so);
481623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
4828501a69cSRobert Watson 	INP_WLOCK(inp);
483ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
484623dce13SRobert Watson 		error = EINVAL;
485623dce13SRobert Watson 		goto out;
486623dce13SRobert Watson 	}
487623dce13SRobert Watson 	tp = intotcpcb(inp);
488623dce13SRobert Watson 	TCPDEBUG1();
48933841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
490fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
491fb59c426SYoshinobu Inoue 
492d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
493d46a5312SMaxim Konovalov 			error = EINVAL;
494d46a5312SMaxim Konovalov 			goto out;
495d46a5312SMaxim Konovalov 		}
49633841545SHajimu UMEMOTO 
497fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
498fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
499fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
500b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
501b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
502413628a7SBjoern A. Zeeb 			goto out;
503b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
504fb59c426SYoshinobu Inoue 			goto out;
505bc65987aSKip Macy 		error = tcp_output_connect(so, nam);
506fb59c426SYoshinobu Inoue 		goto out;
507fb59c426SYoshinobu Inoue 	}
508fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
509fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
510dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
511b89e82ddSJamie Gritton 	if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
512413628a7SBjoern A. Zeeb 		goto out;
513b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
514fb59c426SYoshinobu Inoue 		goto out;
515bc65987aSKip Macy 	error = tcp_output_connect(so, nam);
516623dce13SRobert Watson 
517623dce13SRobert Watson out:
518623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
5198501a69cSRobert Watson 	INP_WUNLOCK(inp);
520603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
521623dce13SRobert Watson 	return (error);
522fb59c426SYoshinobu Inoue }
523fb59c426SYoshinobu Inoue #endif /* INET6 */
524fb59c426SYoshinobu Inoue 
5252c37256eSGarrett Wollman /*
5262c37256eSGarrett Wollman  * Initiate disconnect from peer.
5272c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
5282c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
5292c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
5302c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
5312c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
5322c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
5332c37256eSGarrett Wollman  *
5342c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
5352c37256eSGarrett Wollman  */
5362c37256eSGarrett Wollman static int
5372c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
5382c37256eSGarrett Wollman {
539f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
540623dce13SRobert Watson 	struct tcpcb *tp = NULL;
541623dce13SRobert Watson 	int error = 0;
5422c37256eSGarrett Wollman 
543623dce13SRobert Watson 	TCPDEBUG0;
544603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
545623dce13SRobert Watson 	inp = sotoinpcb(so);
546623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
5478501a69cSRobert Watson 	INP_WLOCK(inp);
548ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
54921367f63SSam Leffler 		error = ECONNRESET;
550623dce13SRobert Watson 		goto out;
551623dce13SRobert Watson 	}
552623dce13SRobert Watson 	tp = intotcpcb(inp);
553623dce13SRobert Watson 	TCPDEBUG1();
554623dce13SRobert Watson 	tcp_disconnect(tp);
555623dce13SRobert Watson out:
556623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
5578501a69cSRobert Watson 	INP_WUNLOCK(inp);
558603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
559623dce13SRobert Watson 	return (error);
5602c37256eSGarrett Wollman }
5612c37256eSGarrett Wollman 
5622c37256eSGarrett Wollman /*
5632c37256eSGarrett Wollman  * Accept a connection.  Essentially all the work is
5642c37256eSGarrett Wollman  * done at higher levels; just return the address
5652c37256eSGarrett Wollman  * of the peer, storing through addr.
5662c37256eSGarrett Wollman  */
5672c37256eSGarrett Wollman static int
56857bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
5692c37256eSGarrett Wollman {
5702c37256eSGarrett Wollman 	int error = 0;
571f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
5721db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
57326ef6ac4SDon Lewis 	struct in_addr addr;
57426ef6ac4SDon Lewis 	in_port_t port = 0;
5751db24ffbSJonathan Lemon 	TCPDEBUG0;
5762c37256eSGarrett Wollman 
5773d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
5783d2d3ef4SRobert Watson 		return (ECONNABORTED);
579f76fcf6dSJeffrey Hsu 
580f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
581623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
582603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_tcbinfo);
5838501a69cSRobert Watson 	INP_WLOCK(inp);
584ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
5853d2d3ef4SRobert Watson 		error = ECONNABORTED;
586623dce13SRobert Watson 		goto out;
587623dce13SRobert Watson 	}
5881db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
5891db24ffbSJonathan Lemon 	TCPDEBUG1();
590f76fcf6dSJeffrey Hsu 
591f76fcf6dSJeffrey Hsu 	/*
59254d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
59326ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
59426ef6ac4SDon Lewis 	 * release the lock.
595f76fcf6dSJeffrey Hsu 	 */
59626ef6ac4SDon Lewis 	port = inp->inp_fport;
59726ef6ac4SDon Lewis 	addr = inp->inp_faddr;
598f76fcf6dSJeffrey Hsu 
599623dce13SRobert Watson out:
600623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
6018501a69cSRobert Watson 	INP_WUNLOCK(inp);
602603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_tcbinfo);
60326ef6ac4SDon Lewis 	if (error == 0)
60426ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
60526ef6ac4SDon Lewis 	return error;
6062c37256eSGarrett Wollman }
6072c37256eSGarrett Wollman 
608fb59c426SYoshinobu Inoue #ifdef INET6
609fb59c426SYoshinobu Inoue static int
610fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
611fb59c426SYoshinobu Inoue {
612f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
613fb59c426SYoshinobu Inoue 	int error = 0;
6141db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
61526ef6ac4SDon Lewis 	struct in_addr addr;
61626ef6ac4SDon Lewis 	struct in6_addr addr6;
61726ef6ac4SDon Lewis 	in_port_t port = 0;
61826ef6ac4SDon Lewis 	int v4 = 0;
6191db24ffbSJonathan Lemon 	TCPDEBUG0;
620fb59c426SYoshinobu Inoue 
621b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
622b4470c16SRobert Watson 		return (ECONNABORTED);
623f76fcf6dSJeffrey Hsu 
624f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
625623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
6268501a69cSRobert Watson 	INP_WLOCK(inp);
627ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
62821367f63SSam Leffler 		error = ECONNABORTED;
629623dce13SRobert Watson 		goto out;
630623dce13SRobert Watson 	}
6311db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
6321db24ffbSJonathan Lemon 	TCPDEBUG1();
633623dce13SRobert Watson 
63426ef6ac4SDon Lewis 	/*
63526ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
63626ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
63726ef6ac4SDon Lewis 	 * release the lock.
63826ef6ac4SDon Lewis 	 */
63926ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
64026ef6ac4SDon Lewis 		v4 = 1;
64126ef6ac4SDon Lewis 		port = inp->inp_fport;
64226ef6ac4SDon Lewis 		addr = inp->inp_faddr;
64326ef6ac4SDon Lewis 	} else {
64426ef6ac4SDon Lewis 		port = inp->inp_fport;
64526ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
64626ef6ac4SDon Lewis 	}
64726ef6ac4SDon Lewis 
648623dce13SRobert Watson out:
649623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
6508501a69cSRobert Watson 	INP_WUNLOCK(inp);
65126ef6ac4SDon Lewis 	if (error == 0) {
65226ef6ac4SDon Lewis 		if (v4)
65326ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
65426ef6ac4SDon Lewis 		else
65526ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
65626ef6ac4SDon Lewis 	}
65726ef6ac4SDon Lewis 	return error;
658fb59c426SYoshinobu Inoue }
659fb59c426SYoshinobu Inoue #endif /* INET6 */
660f76fcf6dSJeffrey Hsu 
661f76fcf6dSJeffrey Hsu /*
6622c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
6632c37256eSGarrett Wollman  */
6642c37256eSGarrett Wollman static int
6652c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
6662c37256eSGarrett Wollman {
6672c37256eSGarrett Wollman 	int error = 0;
668f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
669623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6702c37256eSGarrett Wollman 
671623dce13SRobert Watson 	TCPDEBUG0;
672603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
673623dce13SRobert Watson 	inp = sotoinpcb(so);
674623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
6758501a69cSRobert Watson 	INP_WLOCK(inp);
676ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
67721367f63SSam Leffler 		error = ECONNRESET;
678623dce13SRobert Watson 		goto out;
679623dce13SRobert Watson 	}
680623dce13SRobert Watson 	tp = intotcpcb(inp);
681623dce13SRobert Watson 	TCPDEBUG1();
6822c37256eSGarrett Wollman 	socantsendmore(so);
683623dce13SRobert Watson 	tcp_usrclosed(tp);
684ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
685bc65987aSKip Macy 		error = tcp_output_disconnect(tp);
686623dce13SRobert Watson 
687623dce13SRobert Watson out:
688623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
6898501a69cSRobert Watson 	INP_WUNLOCK(inp);
690603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
691623dce13SRobert Watson 
692623dce13SRobert Watson 	return (error);
6932c37256eSGarrett Wollman }
6942c37256eSGarrett Wollman 
6952c37256eSGarrett Wollman /*
6962c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
6972c37256eSGarrett Wollman  */
6982c37256eSGarrett Wollman static int
6992c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
7002c37256eSGarrett Wollman {
701f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
702623dce13SRobert Watson 	struct tcpcb *tp = NULL;
703623dce13SRobert Watson 	int error = 0;
7042c37256eSGarrett Wollman 
705623dce13SRobert Watson 	TCPDEBUG0;
706623dce13SRobert Watson 	inp = sotoinpcb(so);
707623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
7088501a69cSRobert Watson 	INP_WLOCK(inp);
709ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
71021367f63SSam Leffler 		error = ECONNRESET;
711623dce13SRobert Watson 		goto out;
712623dce13SRobert Watson 	}
713623dce13SRobert Watson 	tp = intotcpcb(inp);
714623dce13SRobert Watson 	TCPDEBUG1();
715bc65987aSKip Macy 	tcp_output_rcvd(tp);
716623dce13SRobert Watson 
717623dce13SRobert Watson out:
718623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
7198501a69cSRobert Watson 	INP_WUNLOCK(inp);
720623dce13SRobert Watson 	return (error);
7212c37256eSGarrett Wollman }
7222c37256eSGarrett Wollman 
7232c37256eSGarrett Wollman /*
7242c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
7259c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
7269c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
7279c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
7289c9906e9SPeter Wemm  * generally are caller-frees.
7292c37256eSGarrett Wollman  */
7302c37256eSGarrett Wollman static int
73157bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
732b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
7332c37256eSGarrett Wollman {
7342c37256eSGarrett Wollman 	int error = 0;
735f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
736623dce13SRobert Watson 	struct tcpcb *tp = NULL;
737623dce13SRobert Watson 	int headlocked = 0;
738fb59c426SYoshinobu Inoue #ifdef INET6
739fb59c426SYoshinobu Inoue 	int isipv6;
740fb59c426SYoshinobu Inoue #endif
7419c9906e9SPeter Wemm 	TCPDEBUG0;
7422c37256eSGarrett Wollman 
743f76fcf6dSJeffrey Hsu 	/*
744623dce13SRobert Watson 	 * We require the pcbinfo lock in two cases:
745623dce13SRobert Watson 	 *
746623dce13SRobert Watson 	 * (1) An implied connect is taking place, which can result in
747623dce13SRobert Watson 	 *     binding IPs and ports and hence modification of the pcb hash
748623dce13SRobert Watson 	 *     chains.
749623dce13SRobert Watson 	 *
750623dce13SRobert Watson 	 * (2) PRUS_EOF is set, resulting in explicit close on the send.
751f76fcf6dSJeffrey Hsu 	 */
752623dce13SRobert Watson 	if ((nam != NULL) || (flags & PRUS_EOF)) {
753603724d3SBjoern A. Zeeb 		INP_INFO_WLOCK(&V_tcbinfo);
754623dce13SRobert Watson 		headlocked = 1;
755623dce13SRobert Watson 	}
756f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
757623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
7588501a69cSRobert Watson 	INP_WLOCK(inp);
759ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
7607ff0b850SAndre Oppermann 		if (control)
7617ff0b850SAndre Oppermann 			m_freem(control);
7627ff0b850SAndre Oppermann 		if (m)
7637ff0b850SAndre Oppermann 			m_freem(m);
76421367f63SSam Leffler 		error = ECONNRESET;
7659c9906e9SPeter Wemm 		goto out;
7669c9906e9SPeter Wemm 	}
767fb59c426SYoshinobu Inoue #ifdef INET6
768fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
769fb59c426SYoshinobu Inoue #endif /* INET6 */
7709c9906e9SPeter Wemm 	tp = intotcpcb(inp);
7719c9906e9SPeter Wemm 	TCPDEBUG1();
7729c9906e9SPeter Wemm 	if (control) {
7739c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
7749c9906e9SPeter Wemm 		if (control->m_len) {
7759c9906e9SPeter Wemm 			m_freem(control);
7762c37256eSGarrett Wollman 			if (m)
7772c37256eSGarrett Wollman 				m_freem(m);
778744f87eaSDavid Greenman 			error = EINVAL;
779744f87eaSDavid Greenman 			goto out;
7802c37256eSGarrett Wollman 		}
7819c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
7829c9906e9SPeter Wemm 	}
7832c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
784395bb186SSam Leffler 		sbappendstream(&so->so_snd, m);
7852c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
7862c37256eSGarrett Wollman 			/*
7872c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
7882c37256eSGarrett Wollman 			 * initialize window to default value, and
7892c37256eSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
7902c37256eSGarrett Wollman 			 * MSS.
7912c37256eSGarrett Wollman 			 */
792603724d3SBjoern A. Zeeb 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
793fb59c426SYoshinobu Inoue #ifdef INET6
794fb59c426SYoshinobu Inoue 			if (isipv6)
795b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
796fb59c426SYoshinobu Inoue 			else
797fb59c426SYoshinobu Inoue #endif /* INET6 */
798b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
7992c37256eSGarrett Wollman 			if (error)
8002c37256eSGarrett Wollman 				goto out;
8012c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
8022c37256eSGarrett Wollman 			tcp_mss(tp, -1);
8032c37256eSGarrett Wollman 		}
8042c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
8052c37256eSGarrett Wollman 			/*
8062c37256eSGarrett Wollman 			 * Close the send side of the connection after
8072c37256eSGarrett Wollman 			 * the data is sent.
8082c37256eSGarrett Wollman 			 */
809603724d3SBjoern A. Zeeb 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
8102c37256eSGarrett Wollman 			socantsendmore(so);
811623dce13SRobert Watson 			tcp_usrclosed(tp);
8122c37256eSGarrett Wollman 		}
813623dce13SRobert Watson 		if (headlocked) {
814603724d3SBjoern A. Zeeb 			INP_INFO_WUNLOCK(&V_tcbinfo);
815623dce13SRobert Watson 			headlocked = 0;
816623dce13SRobert Watson 		}
817ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED)) {
818b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
819b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
820bc65987aSKip Macy 			error = tcp_output_send(tp);
821b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
822b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
823b0acefa8SBill Fenner 		}
8242c37256eSGarrett Wollman 	} else {
825623dce13SRobert Watson 		/*
826623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
827623dce13SRobert Watson 		 */
828d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
8292c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
830d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
8312c37256eSGarrett Wollman 			m_freem(m);
8322c37256eSGarrett Wollman 			error = ENOBUFS;
8332c37256eSGarrett Wollman 			goto out;
8342c37256eSGarrett Wollman 		}
8352c37256eSGarrett Wollman 		/*
8362c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
8372c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
8382c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
8392c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
8402c37256eSGarrett Wollman 		 * of data past the urgent section.
8412c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
8422c37256eSGarrett Wollman 		 */
843d2bc35abSRobert Watson 		sbappendstream_locked(&so->so_snd, m);
844d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
845ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
846ef53690bSGarrett Wollman 			/*
847ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
848ef53690bSGarrett Wollman 			 * initialize window to default value, and
849ef53690bSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
850ef53690bSGarrett Wollman 			 * MSS.
851ef53690bSGarrett Wollman 			 */
852603724d3SBjoern A. Zeeb 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
853fb59c426SYoshinobu Inoue #ifdef INET6
854fb59c426SYoshinobu Inoue 			if (isipv6)
855b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
856fb59c426SYoshinobu Inoue 			else
857fb59c426SYoshinobu Inoue #endif /* INET6 */
858b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
859ef53690bSGarrett Wollman 			if (error)
860ef53690bSGarrett Wollman 				goto out;
861ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
862ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
863603724d3SBjoern A. Zeeb 			INP_INFO_WUNLOCK(&V_tcbinfo);
864623dce13SRobert Watson 			headlocked = 0;
865623dce13SRobert Watson 		} else if (nam) {
866603724d3SBjoern A. Zeeb 			INP_INFO_WUNLOCK(&V_tcbinfo);
867623dce13SRobert Watson 			headlocked = 0;
868623dce13SRobert Watson 		}
8692c37256eSGarrett Wollman 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
8702cdbfa66SPaul Saab 		tp->t_flags |= TF_FORCEDATA;
871bc65987aSKip Macy 		error = tcp_output_send(tp);
8722cdbfa66SPaul Saab 		tp->t_flags &= ~TF_FORCEDATA;
8732c37256eSGarrett Wollman 	}
874d1401c90SRobert Watson out:
875d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
8762c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
8778501a69cSRobert Watson 	INP_WUNLOCK(inp);
878623dce13SRobert Watson 	if (headlocked)
879603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
88073fddedaSPeter Grehan 	return (error);
8812c37256eSGarrett Wollman }
8822c37256eSGarrett Wollman 
8832c37256eSGarrett Wollman /*
884a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
8852c37256eSGarrett Wollman  */
886ac45e92fSRobert Watson static void
8872c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
8882c37256eSGarrett Wollman {
889f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
890a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
891623dce13SRobert Watson 	TCPDEBUG0;
892c78cbc7bSRobert Watson 
893ac45e92fSRobert Watson 	inp = sotoinpcb(so);
894c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
895c78cbc7bSRobert Watson 
896603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
8978501a69cSRobert Watson 	INP_WLOCK(inp);
898c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
899c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
900c78cbc7bSRobert Watson 
901c78cbc7bSRobert Watson 	/*
902a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
903c78cbc7bSRobert Watson 	 */
904ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
905ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
906c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
907a152f8a3SRobert Watson 		TCPDEBUG1();
908c78cbc7bSRobert Watson 		tcp_drop(tp, ECONNABORTED);
909a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
910c78cbc7bSRobert Watson 	}
911ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
912a152f8a3SRobert Watson 		SOCK_LOCK(so);
913a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
914a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
915ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
916a152f8a3SRobert Watson 	}
9178501a69cSRobert Watson 	INP_WUNLOCK(inp);
918603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
919a152f8a3SRobert Watson }
920a152f8a3SRobert Watson 
921a152f8a3SRobert Watson /*
922a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
923a152f8a3SRobert Watson  */
924a152f8a3SRobert Watson static void
925a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
926a152f8a3SRobert Watson {
927a152f8a3SRobert Watson 	struct inpcb *inp;
928a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
929a152f8a3SRobert Watson 	TCPDEBUG0;
930a152f8a3SRobert Watson 
931a152f8a3SRobert Watson 	inp = sotoinpcb(so);
932a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
933a152f8a3SRobert Watson 
934603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
9358501a69cSRobert Watson 	INP_WLOCK(inp);
936a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
937a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
938a152f8a3SRobert Watson 
939a152f8a3SRobert Watson 	/*
940a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
941a152f8a3SRobert Watson 	 * a disconnect.
942a152f8a3SRobert Watson 	 */
943ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
944ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
945a152f8a3SRobert Watson 		tp = intotcpcb(inp);
946a152f8a3SRobert Watson 		TCPDEBUG1();
947a152f8a3SRobert Watson 		tcp_disconnect(tp);
948a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
949a152f8a3SRobert Watson 	}
950ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
951a152f8a3SRobert Watson 		SOCK_LOCK(so);
952a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
953a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
954ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
955a152f8a3SRobert Watson 	}
9568501a69cSRobert Watson 	INP_WUNLOCK(inp);
957603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
9582c37256eSGarrett Wollman }
9592c37256eSGarrett Wollman 
9602c37256eSGarrett Wollman /*
9612c37256eSGarrett Wollman  * Receive out-of-band data.
9622c37256eSGarrett Wollman  */
9632c37256eSGarrett Wollman static int
9642c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
9652c37256eSGarrett Wollman {
9662c37256eSGarrett Wollman 	int error = 0;
967f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
968623dce13SRobert Watson 	struct tcpcb *tp = NULL;
9692c37256eSGarrett Wollman 
970623dce13SRobert Watson 	TCPDEBUG0;
971623dce13SRobert Watson 	inp = sotoinpcb(so);
972623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
9738501a69cSRobert Watson 	INP_WLOCK(inp);
974ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
97521367f63SSam Leffler 		error = ECONNRESET;
976623dce13SRobert Watson 		goto out;
977623dce13SRobert Watson 	}
978623dce13SRobert Watson 	tp = intotcpcb(inp);
979623dce13SRobert Watson 	TCPDEBUG1();
9802c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
981c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
9824cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
9834cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
9842c37256eSGarrett Wollman 		error = EINVAL;
9852c37256eSGarrett Wollman 		goto out;
9862c37256eSGarrett Wollman 	}
9872c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
9882c37256eSGarrett Wollman 		error = EWOULDBLOCK;
9892c37256eSGarrett Wollman 		goto out;
9902c37256eSGarrett Wollman 	}
9912c37256eSGarrett Wollman 	m->m_len = 1;
9922c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
9932c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
9942c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
995623dce13SRobert Watson 
996623dce13SRobert Watson out:
997623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
9988501a69cSRobert Watson 	INP_WUNLOCK(inp);
999623dce13SRobert Watson 	return (error);
10002c37256eSGarrett Wollman }
10012c37256eSGarrett Wollman 
10022c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1003756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1004756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1005756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1006756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1007756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1008756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1009756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1010756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1011756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
101254d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1013756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1014756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1015756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
1016756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
101754d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1018a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1019a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
10202c37256eSGarrett Wollman };
1021df8bae1dSRodney W. Grimes 
1022fb59c426SYoshinobu Inoue #ifdef INET6
1023fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1024756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1025756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1026756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1027756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1028756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1029756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1030756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1031756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1032756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1033756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1034756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1035756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1036756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
1037756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1038756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1039a152f8a3SRobert Watson  	.pru_sosetlabel =	in_pcbsosetlabel,
1040a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1041fb59c426SYoshinobu Inoue };
1042fb59c426SYoshinobu Inoue #endif /* INET6 */
1043fb59c426SYoshinobu Inoue 
1044a0292f23SGarrett Wollman /*
1045a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1046a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
10475200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
10485200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
10495200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
10505200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1051a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1052a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1053a0292f23SGarrett Wollman  */
10540312fbe9SPoul-Henning Kamp static int
1055ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1056a0292f23SGarrett Wollman {
1057a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1058a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
10595200e00eSIan Dowse 	struct in_addr laddr;
10605200e00eSIan Dowse 	u_short lport;
1061c3229e05SDavid Greenman 	int error;
1062a0292f23SGarrett Wollman 
1063603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
10648501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1065623dce13SRobert Watson 
1066a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
1067b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1068a0292f23SGarrett Wollman 		if (error)
1069a0292f23SGarrett Wollman 			return error;
1070a0292f23SGarrett Wollman 	}
1071a0292f23SGarrett Wollman 
1072a0292f23SGarrett Wollman 	/*
1073a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1074a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1075a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1076a0292f23SGarrett Wollman 	 */
10775200e00eSIan Dowse 	laddr = inp->inp_laddr;
10785200e00eSIan Dowse 	lport = inp->inp_lport;
10795200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1080b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
10815200e00eSIan Dowse 	if (error && oinp == NULL)
1082d3628763SRodney W. Grimes 		return error;
1083c94c54e4SAndre Oppermann 	if (oinp)
1084a0292f23SGarrett Wollman 		return EADDRINUSE;
10855200e00eSIan Dowse 	inp->inp_laddr = laddr;
108615bd2b43SDavid Greenman 	in_pcbrehash(inp);
1087a0292f23SGarrett Wollman 
1088087b55eaSAndre Oppermann 	/*
1089087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1090087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1091087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1092087b55eaSAndre Oppermann 	 */
1093a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
10949b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1095a0292f23SGarrett Wollman 		tp->request_r_scale++;
1096a0292f23SGarrett Wollman 
1097a0292f23SGarrett Wollman 	soisconnecting(so);
109878b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
1099a0292f23SGarrett Wollman 	tp->t_state = TCPS_SYN_SENT;
1100b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1101b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
11021fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
1103a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1104a45d2726SAndras Olah 
1105a0292f23SGarrett Wollman 	return 0;
1106a0292f23SGarrett Wollman }
1107a0292f23SGarrett Wollman 
1108fb59c426SYoshinobu Inoue #ifdef INET6
1109fb59c426SYoshinobu Inoue static int
1110ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1111fb59c426SYoshinobu Inoue {
1112fb59c426SYoshinobu Inoue 	struct inpcb *inp = tp->t_inpcb, *oinp;
1113fb59c426SYoshinobu Inoue 	struct socket *so = inp->inp_socket;
1114fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
111588d166bfSBjoern A. Zeeb 	struct in6_addr addr6;
1116fb59c426SYoshinobu Inoue 	int error;
1117fb59c426SYoshinobu Inoue 
1118603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
11198501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1120623dce13SRobert Watson 
1121fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
1122b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1123fb59c426SYoshinobu Inoue 		if (error)
1124fb59c426SYoshinobu Inoue 			return error;
1125fb59c426SYoshinobu Inoue 	}
1126fb59c426SYoshinobu Inoue 
1127fb59c426SYoshinobu Inoue 	/*
1128fb59c426SYoshinobu Inoue 	 * Cannot simply call in_pcbconnect, because there might be an
1129fb59c426SYoshinobu Inoue 	 * earlier incarnation of this same connection still in
1130fb59c426SYoshinobu Inoue 	 * TIME_WAIT state, creating an ADDRINUSE error.
1131a1f7e5f8SHajimu UMEMOTO 	 * in6_pcbladdr() also handles scope zone IDs.
1132fb59c426SYoshinobu Inoue 	 */
1133fb59c426SYoshinobu Inoue 	error = in6_pcbladdr(inp, nam, &addr6);
1134fb59c426SYoshinobu Inoue 	if (error)
1135fb59c426SYoshinobu Inoue 		return error;
1136fb59c426SYoshinobu Inoue 	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1137fb59c426SYoshinobu Inoue 				  &sin6->sin6_addr, sin6->sin6_port,
1138fb59c426SYoshinobu Inoue 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
113988d166bfSBjoern A. Zeeb 				  ? &addr6
1140fb59c426SYoshinobu Inoue 				  : &inp->in6p_laddr,
1141fb59c426SYoshinobu Inoue 				  inp->inp_lport,  0, NULL);
1142c94c54e4SAndre Oppermann 	if (oinp)
1143fb59c426SYoshinobu Inoue 		return EADDRINUSE;
1144fb59c426SYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
114588d166bfSBjoern A. Zeeb 		inp->in6p_laddr = addr6;
1146fb59c426SYoshinobu Inoue 	inp->in6p_faddr = sin6->sin6_addr;
1147fb59c426SYoshinobu Inoue 	inp->inp_fport = sin6->sin6_port;
11488a59da30SHajimu UMEMOTO 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
1149fc384fa5SBjoern A. Zeeb 	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
1150fc384fa5SBjoern A. Zeeb 	if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
1151fc384fa5SBjoern A. Zeeb 		inp->inp_flow |=
11528a59da30SHajimu UMEMOTO 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1153fb59c426SYoshinobu Inoue 	in_pcbrehash(inp);
1154fb59c426SYoshinobu Inoue 
1155fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1156fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1157970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1158fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1159fb59c426SYoshinobu Inoue 
1160fb59c426SYoshinobu Inoue 	soisconnecting(so);
116178b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
1162fb59c426SYoshinobu Inoue 	tp->t_state = TCPS_SYN_SENT;
1163b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1164b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
11651fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
1166fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1167fb59c426SYoshinobu Inoue 
1168fb59c426SYoshinobu Inoue 	return 0;
1169fb59c426SYoshinobu Inoue }
1170fb59c426SYoshinobu Inoue #endif /* INET6 */
1171fb59c426SYoshinobu Inoue 
1172cfe8b629SGarrett Wollman /*
1173b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1174b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1175b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1176b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1177b8af5dfaSRobert Watson  * from Linux.
1178b8af5dfaSRobert Watson  */
1179b8af5dfaSRobert Watson static void
1180ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1181b8af5dfaSRobert Watson {
1182b8af5dfaSRobert Watson 
11838501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1184b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1185b8af5dfaSRobert Watson 
1186b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1187b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1188b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
11893529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1190b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1191b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1192b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1193b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1194b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1195b8af5dfaSRobert Watson 	}
11961baaf834SBruce M Simpson 
119743d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
119843d94734SJohn Baldwin 	ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
11991baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
12001baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
12011baaf834SBruce M Simpson 
1202b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1203b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1204b8af5dfaSRobert Watson 
1205b8af5dfaSRobert Watson 	/*
1206b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1207b8af5dfaSRobert Watson 	 */
1208c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1209535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1210b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
1211b8af5dfaSRobert Watson 	ti->tcpi_snd_bwnd = tp->snd_bwnd;
1212535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
121343d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
121443d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1215535fbad6SKip Macy 	if (tp->t_flags & TF_TOE)
1216535fbad6SKip Macy 		ti->tcpi_options |= TCPI_OPT_TOE;
1217b8af5dfaSRobert Watson }
1218b8af5dfaSRobert Watson 
1219b8af5dfaSRobert Watson /*
12201e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
12211e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
12221e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
12231e8f5ffaSRobert Watson  * option.
1224cfe8b629SGarrett Wollman  */
12258501a69cSRobert Watson #define INP_WLOCK_RECHECK(inp) do {					\
12268501a69cSRobert Watson 	INP_WLOCK(inp);							\
1227ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
12288501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
12291e8f5ffaSRobert Watson 		return (ECONNRESET);					\
12301e8f5ffaSRobert Watson 	}								\
12311e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
12321e8f5ffaSRobert Watson } while(0)
12331e8f5ffaSRobert Watson 
1234df8bae1dSRodney W. Grimes int
1235ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1236df8bae1dSRodney W. Grimes {
12373f9d1ef9SRobert Watson 	int	error, opt, optval;
1238df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1239cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
1240b8af5dfaSRobert Watson 	struct	tcp_info ti;
1241df8bae1dSRodney W. Grimes 
1242cfe8b629SGarrett Wollman 	error = 0;
1243df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1244623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
12458501a69cSRobert Watson 	INP_WLOCK(inp);
1246cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1247fb59c426SYoshinobu Inoue #ifdef INET6
12485cd54324SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6PROTO) {
12498501a69cSRobert Watson 			INP_WUNLOCK(inp);
1250fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
12511e8f5ffaSRobert Watson 		} else {
1252fb59c426SYoshinobu Inoue #endif /* INET6 */
12538501a69cSRobert Watson 			INP_WUNLOCK(inp);
1254cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
12551e8f5ffaSRobert Watson #ifdef INET6
12561e8f5ffaSRobert Watson 		}
12571e8f5ffaSRobert Watson #endif
1258df8bae1dSRodney W. Grimes 		return (error);
1259df8bae1dSRodney W. Grimes 	}
1260ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
12618501a69cSRobert Watson 		INP_WUNLOCK(inp);
12621e8f5ffaSRobert Watson 		return (ECONNRESET);
1263623dce13SRobert Watson 	}
1264df8bae1dSRodney W. Grimes 
1265cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1266cfe8b629SGarrett Wollman 	case SOPT_SET:
1267cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
12681cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
126988f6b043SBruce M Simpson 		case TCP_MD5SIG:
12708501a69cSRobert Watson 			INP_WUNLOCK(inp);
12711cfd4b53SBruce M Simpson 			error = sooptcopyin(sopt, &optval, sizeof optval,
12721cfd4b53SBruce M Simpson 			    sizeof optval);
12731cfd4b53SBruce M Simpson 			if (error)
12741e8f5ffaSRobert Watson 				return (error);
12751cfd4b53SBruce M Simpson 
12768501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
12771cfd4b53SBruce M Simpson 			if (optval > 0)
12781cfd4b53SBruce M Simpson 				tp->t_flags |= TF_SIGNATURE;
12791cfd4b53SBruce M Simpson 			else
12801cfd4b53SBruce M Simpson 				tp->t_flags &= ~TF_SIGNATURE;
12818501a69cSRobert Watson 			INP_WUNLOCK(inp);
12821cfd4b53SBruce M Simpson 			break;
12831cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
1284df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1285cfe8b629SGarrett Wollman 		case TCP_NOOPT:
12868501a69cSRobert Watson 			INP_WUNLOCK(inp);
1287cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1288cfe8b629SGarrett Wollman 			    sizeof optval);
1289cfe8b629SGarrett Wollman 			if (error)
12901e8f5ffaSRobert Watson 				return (error);
1291cfe8b629SGarrett Wollman 
12928501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1293cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1294cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1295cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1296cfe8b629SGarrett Wollman 				break;
1297cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1298cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1299cfe8b629SGarrett Wollman 				break;
1300cfe8b629SGarrett Wollman 			default:
1301cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1302cfe8b629SGarrett Wollman 				break;
1303cfe8b629SGarrett Wollman 			}
1304cfe8b629SGarrett Wollman 
1305cfe8b629SGarrett Wollman 			if (optval)
1306cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1307df8bae1dSRodney W. Grimes 			else
1308cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
13098501a69cSRobert Watson 			INP_WUNLOCK(inp);
1310df8bae1dSRodney W. Grimes 			break;
1311df8bae1dSRodney W. Grimes 
1312007581c0SJonathan Lemon 		case TCP_NOPUSH:
13138501a69cSRobert Watson 			INP_WUNLOCK(inp);
1314007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1315007581c0SJonathan Lemon 			    sizeof optval);
1316007581c0SJonathan Lemon 			if (error)
13171e8f5ffaSRobert Watson 				return (error);
1318007581c0SJonathan Lemon 
13198501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1320007581c0SJonathan Lemon 			if (optval)
1321007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1322007581c0SJonathan Lemon 			else {
1323007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1324007581c0SJonathan Lemon 				error = tcp_output(tp);
1325007581c0SJonathan Lemon 			}
13268501a69cSRobert Watson 			INP_WUNLOCK(inp);
1327007581c0SJonathan Lemon 			break;
1328007581c0SJonathan Lemon 
1329df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
13308501a69cSRobert Watson 			INP_WUNLOCK(inp);
1331cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1332cfe8b629SGarrett Wollman 			    sizeof optval);
1333cfe8b629SGarrett Wollman 			if (error)
13341e8f5ffaSRobert Watson 				return (error);
1335df8bae1dSRodney W. Grimes 
13368501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
133753369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
1338603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
1339cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1340a0292f23SGarrett Wollman 			else
1341a0292f23SGarrett Wollman 				error = EINVAL;
13428501a69cSRobert Watson 			INP_WUNLOCK(inp);
1343a0292f23SGarrett Wollman 			break;
1344a0292f23SGarrett Wollman 
1345b8af5dfaSRobert Watson 		case TCP_INFO:
13468501a69cSRobert Watson 			INP_WUNLOCK(inp);
1347b8af5dfaSRobert Watson 			error = EINVAL;
1348b8af5dfaSRobert Watson 			break;
1349b8af5dfaSRobert Watson 
1350df8bae1dSRodney W. Grimes 		default:
13518501a69cSRobert Watson 			INP_WUNLOCK(inp);
1352df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1353df8bae1dSRodney W. Grimes 			break;
1354df8bae1dSRodney W. Grimes 		}
1355df8bae1dSRodney W. Grimes 		break;
1356df8bae1dSRodney W. Grimes 
1357cfe8b629SGarrett Wollman 	case SOPT_GET:
13581e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
1359cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
13601cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
136188f6b043SBruce M Simpson 		case TCP_MD5SIG:
13621cfd4b53SBruce M Simpson 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
13638501a69cSRobert Watson 			INP_WUNLOCK(inp);
1364b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
13651cfd4b53SBruce M Simpson 			break;
1366265ed012SBruce M Simpson #endif
13671e8f5ffaSRobert Watson 
1368df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1369cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
13708501a69cSRobert Watson 			INP_WUNLOCK(inp);
1371b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1372df8bae1dSRodney W. Grimes 			break;
1373df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1374cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
13758501a69cSRobert Watson 			INP_WUNLOCK(inp);
1376b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1377df8bae1dSRodney W. Grimes 			break;
1378a0292f23SGarrett Wollman 		case TCP_NOOPT:
1379cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
13808501a69cSRobert Watson 			INP_WUNLOCK(inp);
1381b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1382a0292f23SGarrett Wollman 			break;
1383a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1384cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
13858501a69cSRobert Watson 			INP_WUNLOCK(inp);
1386b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1387b8af5dfaSRobert Watson 			break;
1388b8af5dfaSRobert Watson 		case TCP_INFO:
1389b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
13908501a69cSRobert Watson 			INP_WUNLOCK(inp);
1391b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
1392a0292f23SGarrett Wollman 			break;
1393df8bae1dSRodney W. Grimes 		default:
13948501a69cSRobert Watson 			INP_WUNLOCK(inp);
1395df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1396df8bae1dSRodney W. Grimes 			break;
1397df8bae1dSRodney W. Grimes 		}
1398df8bae1dSRodney W. Grimes 		break;
1399df8bae1dSRodney W. Grimes 	}
1400df8bae1dSRodney W. Grimes 	return (error);
1401df8bae1dSRodney W. Grimes }
14028501a69cSRobert Watson #undef INP_WLOCK_RECHECK
1403df8bae1dSRodney W. Grimes 
140426e30fbbSDavid Greenman /*
140526e30fbbSDavid Greenman  * tcp_sendspace and tcp_recvspace are the default send and receive window
140626e30fbbSDavid Greenman  * sizes, respectively.  These are obsolescent (this information should
140726e30fbbSDavid Greenman  * be set by the route).
140826e30fbbSDavid Greenman  */
140981e561cdSDavid E. O'Brien u_long	tcp_sendspace = 1024*32;
1410e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
14113d177f46SBill Fumerola     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
141281e561cdSDavid E. O'Brien u_long	tcp_recvspace = 1024*64;
1413e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
14143d177f46SBill Fumerola     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1415df8bae1dSRodney W. Grimes 
1416df8bae1dSRodney W. Grimes /*
1417df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1418df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1419df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1420df8bae1dSRodney W. Grimes  */
14210312fbe9SPoul-Henning Kamp static int
1422ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so)
1423df8bae1dSRodney W. Grimes {
1424ad3f9ab3SAndre Oppermann 	struct tcpcb *tp;
1425df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1426df8bae1dSRodney W. Grimes 	int error;
1427df8bae1dSRodney W. Grimes 
1428df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1429df8bae1dSRodney W. Grimes 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1430df8bae1dSRodney W. Grimes 		if (error)
1431df8bae1dSRodney W. Grimes 			return (error);
1432df8bae1dSRodney W. Grimes 	}
14336741ecf5SAndre Oppermann 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
14346741ecf5SAndre Oppermann 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1435603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
1436603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_tcbinfo);
1437f2de87feSRobert Watson 	if (error) {
1438603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
1439df8bae1dSRodney W. Grimes 		return (error);
1440f2de87feSRobert Watson 	}
1441df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1442fb59c426SYoshinobu Inoue #ifdef INET6
14435cd54324SBjoern A. Zeeb 	if (inp->inp_vflag & INP_IPV6PROTO) {
1444fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1445fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1446fb59c426SYoshinobu Inoue 	}
1447fb59c426SYoshinobu Inoue 	else
1448fb59c426SYoshinobu Inoue #endif
1449cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1450df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1451623dce13SRobert Watson 	if (tp == NULL) {
1452df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
14530206cdb8SBjoern A. Zeeb 		in_pcbfree(inp);
1454603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
1455df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1456df8bae1dSRodney W. Grimes 	}
1457df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
14588501a69cSRobert Watson 	INP_WUNLOCK(inp);
1459603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
1460df8bae1dSRodney W. Grimes 	return (0);
1461df8bae1dSRodney W. Grimes }
1462df8bae1dSRodney W. Grimes 
1463df8bae1dSRodney W. Grimes /*
1464df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1465df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1466df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1467df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1468df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1469df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1470df8bae1dSRodney W. Grimes  */
1471623dce13SRobert Watson static void
1472ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
1473df8bae1dSRodney W. Grimes {
1474e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
1475e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
1476e6e0b5ffSRobert Watson 
1477603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
14788501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1479df8bae1dSRodney W. Grimes 
1480623dce13SRobert Watson 	/*
1481623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1482623dce13SRobert Watson 	 * socket is still open.
1483623dce13SRobert Watson 	 */
1484623dce13SRobert Watson 	if (tp->t_state < TCPS_ESTABLISHED) {
1485df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1486623dce13SRobert Watson 		KASSERT(tp != NULL,
1487623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
1488623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1489243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
1490623dce13SRobert Watson 		KASSERT(tp != NULL,
1491623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
1492623dce13SRobert Watson 	} else {
1493df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
1494df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
1495623dce13SRobert Watson 		tcp_usrclosed(tp);
1496ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
1497bc65987aSKip Macy 			tcp_output_disconnect(tp);
1498df8bae1dSRodney W. Grimes 	}
1499df8bae1dSRodney W. Grimes }
1500df8bae1dSRodney W. Grimes 
1501df8bae1dSRodney W. Grimes /*
1502df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1503df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1504df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1505df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1506df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1507df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1508df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1509df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1510df8bae1dSRodney W. Grimes  */
1511623dce13SRobert Watson static void
1512ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
1513df8bae1dSRodney W. Grimes {
1514df8bae1dSRodney W. Grimes 
1515603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
15168501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1517e6e0b5ffSRobert Watson 
1518df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1519df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
1520bc65987aSKip Macy 		tcp_offload_listen_close(tp);
1521bc65987aSKip Macy 		/* FALLTHROUGH */
1522bc65987aSKip Macy 	case TCPS_CLOSED:
1523df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_CLOSED;
1524df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1525623dce13SRobert Watson 		/*
1526623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
1527623dce13SRobert Watson 		 * still open.
1528623dce13SRobert Watson 		 */
1529623dce13SRobert Watson 		KASSERT(tp != NULL,
1530623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
1531df8bae1dSRodney W. Grimes 		break;
1532df8bae1dSRodney W. Grimes 
1533a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
1534df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1535a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
1536a0292f23SGarrett Wollman 		break;
1537a0292f23SGarrett Wollman 
1538df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1539df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_FIN_WAIT_1;
1540df8bae1dSRodney W. Grimes 		break;
1541df8bae1dSRodney W. Grimes 
1542df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1543df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_LAST_ACK;
1544df8bae1dSRodney W. Grimes 		break;
1545df8bae1dSRodney W. Grimes 	}
1546abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
1547df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
1548abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
15497c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
15507c72af87SMohan Srinivasan 			int timeout;
15517c72af87SMohan Srinivasan 
15527c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
15537c72af87SMohan Srinivasan 			    tcp_finwait2_timeout : tcp_maxidle;
1554b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
1555b6239c4aSAndras Olah 		}
1556df8bae1dSRodney W. Grimes 	}
15577c72af87SMohan Srinivasan }
1558497057eeSRobert Watson 
1559497057eeSRobert Watson #ifdef DDB
1560497057eeSRobert Watson static void
1561497057eeSRobert Watson db_print_indent(int indent)
1562497057eeSRobert Watson {
1563497057eeSRobert Watson 	int i;
1564497057eeSRobert Watson 
1565497057eeSRobert Watson 	for (i = 0; i < indent; i++)
1566497057eeSRobert Watson 		db_printf(" ");
1567497057eeSRobert Watson }
1568497057eeSRobert Watson 
1569497057eeSRobert Watson static void
1570497057eeSRobert Watson db_print_tstate(int t_state)
1571497057eeSRobert Watson {
1572497057eeSRobert Watson 
1573497057eeSRobert Watson 	switch (t_state) {
1574497057eeSRobert Watson 	case TCPS_CLOSED:
1575497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
1576497057eeSRobert Watson 		return;
1577497057eeSRobert Watson 
1578497057eeSRobert Watson 	case TCPS_LISTEN:
1579497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
1580497057eeSRobert Watson 		return;
1581497057eeSRobert Watson 
1582497057eeSRobert Watson 	case TCPS_SYN_SENT:
1583497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
1584497057eeSRobert Watson 		return;
1585497057eeSRobert Watson 
1586497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
1587497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
1588497057eeSRobert Watson 		return;
1589497057eeSRobert Watson 
1590497057eeSRobert Watson 	case TCPS_ESTABLISHED:
1591497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
1592497057eeSRobert Watson 		return;
1593497057eeSRobert Watson 
1594497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
1595497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
1596497057eeSRobert Watson 		return;
1597497057eeSRobert Watson 
1598497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
1599497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
1600497057eeSRobert Watson 		return;
1601497057eeSRobert Watson 
1602497057eeSRobert Watson 	case TCPS_CLOSING:
1603497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
1604497057eeSRobert Watson 		return;
1605497057eeSRobert Watson 
1606497057eeSRobert Watson 	case TCPS_LAST_ACK:
1607497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
1608497057eeSRobert Watson 		return;
1609497057eeSRobert Watson 
1610497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
1611497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
1612497057eeSRobert Watson 		return;
1613497057eeSRobert Watson 
1614497057eeSRobert Watson 	case TCPS_TIME_WAIT:
1615497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
1616497057eeSRobert Watson 		return;
1617497057eeSRobert Watson 
1618497057eeSRobert Watson 	default:
1619497057eeSRobert Watson 		db_printf("unknown");
1620497057eeSRobert Watson 		return;
1621497057eeSRobert Watson 	}
1622497057eeSRobert Watson }
1623497057eeSRobert Watson 
1624497057eeSRobert Watson static void
1625497057eeSRobert Watson db_print_tflags(u_int t_flags)
1626497057eeSRobert Watson {
1627497057eeSRobert Watson 	int comma;
1628497057eeSRobert Watson 
1629497057eeSRobert Watson 	comma = 0;
1630497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
1631497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
1632497057eeSRobert Watson 		comma = 1;
1633497057eeSRobert Watson 	}
1634497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
1635497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
1636497057eeSRobert Watson 		comma = 1;
1637497057eeSRobert Watson 	}
1638497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
1639497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
1640497057eeSRobert Watson 		comma = 1;
1641497057eeSRobert Watson 	}
1642497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
1643497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
1644497057eeSRobert Watson 		comma = 1;
1645497057eeSRobert Watson 	}
1646497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
1647497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
1648497057eeSRobert Watson 		comma = 1;
1649497057eeSRobert Watson 	}
1650497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
1651497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1652497057eeSRobert Watson 		comma = 1;
1653497057eeSRobert Watson 	}
1654497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
1655497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1656497057eeSRobert Watson 		comma = 1;
1657497057eeSRobert Watson 	}
1658497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
1659497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1660497057eeSRobert Watson 		comma = 1;
1661497057eeSRobert Watson 	}
1662497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
1663497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1664497057eeSRobert Watson 		comma = 1;
1665497057eeSRobert Watson 	}
1666497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
1667497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1668497057eeSRobert Watson 		comma = 1;
1669497057eeSRobert Watson 	}
1670497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
1671497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1672497057eeSRobert Watson 		comma = 1;
1673497057eeSRobert Watson 	}
1674497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
1675497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1676497057eeSRobert Watson 		comma = 1;
1677497057eeSRobert Watson 	}
1678497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
1679497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
1680497057eeSRobert Watson 		comma = 1;
1681497057eeSRobert Watson 	}
1682497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
1683497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
1684497057eeSRobert Watson 		comma = 1;
1685497057eeSRobert Watson 	}
1686497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
1687497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1688497057eeSRobert Watson 		comma = 1;
1689497057eeSRobert Watson 	}
1690497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
1691497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1692497057eeSRobert Watson 		comma = 1;
1693497057eeSRobert Watson 	}
1694497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
1695497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1696497057eeSRobert Watson 		comma = 1;
1697497057eeSRobert Watson 	}
1698497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
1699497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1700497057eeSRobert Watson 		comma = 1;
1701497057eeSRobert Watson 	}
1702497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
1703497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1704497057eeSRobert Watson 		comma = 1;
1705497057eeSRobert Watson 	}
1706497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
1707497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1708497057eeSRobert Watson 		comma = 1;
1709497057eeSRobert Watson 	}
1710497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
1711497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1712497057eeSRobert Watson 		comma = 1;
1713497057eeSRobert Watson 	}
1714497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
1715497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1716497057eeSRobert Watson 		comma = 1;
1717497057eeSRobert Watson 	}
1718497057eeSRobert Watson 	if (t_flags & TF_TSO) {
1719497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
1720497057eeSRobert Watson 		comma = 1;
1721497057eeSRobert Watson 	}
1722f2512ba1SRui Paulo 	if (t_flags & TF_ECN_PERMIT) {
1723f2512ba1SRui Paulo 		db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
1724f2512ba1SRui Paulo 		comma = 1;
1725f2512ba1SRui Paulo 	}
1726497057eeSRobert Watson }
1727497057eeSRobert Watson 
1728497057eeSRobert Watson static void
1729497057eeSRobert Watson db_print_toobflags(char t_oobflags)
1730497057eeSRobert Watson {
1731497057eeSRobert Watson 	int comma;
1732497057eeSRobert Watson 
1733497057eeSRobert Watson 	comma = 0;
1734497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
1735497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1736497057eeSRobert Watson 		comma = 1;
1737497057eeSRobert Watson 	}
1738497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
1739497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1740497057eeSRobert Watson 		comma = 1;
1741497057eeSRobert Watson 	}
1742497057eeSRobert Watson }
1743497057eeSRobert Watson 
1744497057eeSRobert Watson static void
1745497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1746497057eeSRobert Watson {
1747497057eeSRobert Watson 
1748497057eeSRobert Watson 	db_print_indent(indent);
1749497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
1750497057eeSRobert Watson 
1751497057eeSRobert Watson 	indent += 2;
1752497057eeSRobert Watson 
1753497057eeSRobert Watson 	db_print_indent(indent);
1754497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
1755497057eeSRobert Watson 	   LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
1756497057eeSRobert Watson 
1757497057eeSRobert Watson 	db_print_indent(indent);
175885d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
1759e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
1760497057eeSRobert Watson 
1761497057eeSRobert Watson 	db_print_indent(indent);
1762e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
1763e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
1764497057eeSRobert Watson 
1765497057eeSRobert Watson 	db_print_indent(indent);
1766497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
1767497057eeSRobert Watson 	db_print_tstate(tp->t_state);
1768497057eeSRobert Watson 	db_printf(")\n");
1769497057eeSRobert Watson 
1770497057eeSRobert Watson 	db_print_indent(indent);
1771497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
1772497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
1773497057eeSRobert Watson 	db_printf(")\n");
1774497057eeSRobert Watson 
1775497057eeSRobert Watson 	db_print_indent(indent);
1776497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
1777497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
1778497057eeSRobert Watson 
1779497057eeSRobert Watson 	db_print_indent(indent);
1780497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
1781497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
1782497057eeSRobert Watson 
1783497057eeSRobert Watson 	db_print_indent(indent);
1784497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
1785497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
1786497057eeSRobert Watson 
1787497057eeSRobert Watson 	db_print_indent(indent);
1788497057eeSRobert Watson 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %lu   rcv_up: 0x%08x\n",
1789497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
1790497057eeSRobert Watson 
1791497057eeSRobert Watson 	db_print_indent(indent);
1792497057eeSRobert Watson 	db_printf("snd_wnd: %lu   snd_cwnd: %lu   snd_bwnd: %lu\n",
1793497057eeSRobert Watson 	   tp->snd_wnd, tp->snd_cwnd, tp->snd_bwnd);
1794497057eeSRobert Watson 
1795497057eeSRobert Watson 	db_print_indent(indent);
1796497057eeSRobert Watson 	db_printf("snd_ssthresh: %lu   snd_bandwidth: %lu   snd_recover: "
1797497057eeSRobert Watson 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_bandwidth,
1798497057eeSRobert Watson 	    tp->snd_recover);
1799497057eeSRobert Watson 
1800497057eeSRobert Watson 	db_print_indent(indent);
18019f78a87aSJohn Baldwin 	db_printf("t_maxopd: %u   t_rcvtime: %u   t_startime: %u\n",
1802497057eeSRobert Watson 	    tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
1803497057eeSRobert Watson 
1804497057eeSRobert Watson 	db_print_indent(indent);
18059f78a87aSJohn Baldwin 	db_printf("t_rttime: %u   t_rtsq: 0x%08x   t_bw_rtttime: %u\n",
1806497057eeSRobert Watson 	    tp->t_rtttime, tp->t_rtseq, tp->t_bw_rtttime);
1807497057eeSRobert Watson 
1808497057eeSRobert Watson 	db_print_indent(indent);
1809497057eeSRobert Watson 	db_printf("t_bw_rtseq: 0x%08x   t_rxtcur: %d   t_maxseg: %u   "
1810497057eeSRobert Watson 	    "t_srtt: %d\n", tp->t_bw_rtseq, tp->t_rxtcur, tp->t_maxseg,
1811497057eeSRobert Watson 	    tp->t_srtt);
1812497057eeSRobert Watson 
1813497057eeSRobert Watson 	db_print_indent(indent);
1814497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
1815497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
1816497057eeSRobert Watson 	    tp->t_rttbest);
1817497057eeSRobert Watson 
1818497057eeSRobert Watson 	db_print_indent(indent);
1819497057eeSRobert Watson 	db_printf("t_rttupdated: %lu   max_sndwnd: %lu   t_softerror: %d\n",
1820497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
1821497057eeSRobert Watson 
1822497057eeSRobert Watson 	db_print_indent(indent);
1823497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
1824497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
1825497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
1826497057eeSRobert Watson 
1827497057eeSRobert Watson 	db_print_indent(indent);
1828497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
1829497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
1830497057eeSRobert Watson 
1831497057eeSRobert Watson 	db_print_indent(indent);
18329f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
18331a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
1834497057eeSRobert Watson 
1835497057eeSRobert Watson 	db_print_indent(indent);
1836497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
1837497057eeSRobert Watson 	    "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
1838497057eeSRobert Watson 
1839497057eeSRobert Watson 	db_print_indent(indent);
1840497057eeSRobert Watson 	db_printf("snd_ssthresh_prev: %lu   snd_recover_prev: 0x%08x   "
18419f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
1842497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
1843497057eeSRobert Watson 
1844497057eeSRobert Watson 	db_print_indent(indent);
18453529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
18463529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
1847497057eeSRobert Watson 
1848497057eeSRobert Watson 	db_print_indent(indent);
1849497057eeSRobert Watson 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
1850497057eeSRobert Watson 	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
1851497057eeSRobert Watson 
1852497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
1853497057eeSRobert Watson 
1854497057eeSRobert Watson 	db_print_indent(indent);
1855497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
1856497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
1857497057eeSRobert Watson }
1858497057eeSRobert Watson 
1859497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
1860497057eeSRobert Watson {
1861497057eeSRobert Watson 	struct tcpcb *tp;
1862497057eeSRobert Watson 
1863497057eeSRobert Watson 	if (!have_addr) {
1864497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
1865497057eeSRobert Watson 		return;
1866497057eeSRobert Watson 	}
1867497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
1868497057eeSRobert Watson 
1869497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
1870497057eeSRobert Watson }
1871497057eeSRobert Watson #endif
1872