xref: /freebsd/sys/netinet/tcp_usrreq.c (revision abc7d91030f4e3b050970d9049846302c1d015e4)
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
32c3aac50fSPeter Wemm  * $FreeBSD$
33df8bae1dSRodney W. Grimes  */
34df8bae1dSRodney W. Grimes 
35497057eeSRobert Watson #include "opt_ddb.h"
361cfd4b53SBruce M Simpson #include "opt_inet.h"
37fb59c426SYoshinobu Inoue #include "opt_inet6.h"
380cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
390cc12cc5SJoerg Wunsch 
40df8bae1dSRodney W. Grimes #include <sys/param.h>
41df8bae1dSRodney W. Grimes #include <sys/systm.h>
42f76fcf6dSJeffrey Hsu #include <sys/malloc.h>
43c7a82f90SGarrett Wollman #include <sys/kernel.h>
4498163b98SPoul-Henning Kamp #include <sys/sysctl.h>
45df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
46fb59c426SYoshinobu Inoue #ifdef INET6
47fb59c426SYoshinobu Inoue #include <sys/domain.h>
48fb59c426SYoshinobu Inoue #endif /* INET6 */
49df8bae1dSRodney W. Grimes #include <sys/socket.h>
50df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
51df8bae1dSRodney W. Grimes #include <sys/protosw.h>
5291421ba2SRobert Watson #include <sys/proc.h>
5391421ba2SRobert Watson #include <sys/jail.h>
54df8bae1dSRodney W. Grimes 
55497057eeSRobert Watson #ifdef DDB
56497057eeSRobert Watson #include <ddb/ddb.h>
57497057eeSRobert Watson #endif
58497057eeSRobert Watson 
59df8bae1dSRodney W. Grimes #include <net/if.h>
60df8bae1dSRodney W. Grimes #include <net/route.h>
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes #include <netinet/in.h>
63df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
64fb59c426SYoshinobu Inoue #ifdef INET6
65fb59c426SYoshinobu Inoue #include <netinet/ip6.h>
66fb59c426SYoshinobu Inoue #endif
67df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
68fb59c426SYoshinobu Inoue #ifdef INET6
69fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h>
70fb59c426SYoshinobu Inoue #endif
71b5e8ce9fSBruce Evans #include <netinet/in_var.h>
72df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
73fb59c426SYoshinobu Inoue #ifdef INET6
74fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
75a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
76fb59c426SYoshinobu Inoue #endif
77df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
78df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
79df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
80df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
81df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
82df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
83610ee2f9SDavid Greenman #ifdef TCPDEBUG
84df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
85610ee2f9SDavid Greenman #endif
86df8bae1dSRodney W. Grimes 
87df8bae1dSRodney W. Grimes /*
88df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
89df8bae1dSRodney W. Grimes  */
90117bcae7SGarrett Wollman extern	char *tcpstates[];	/* XXX ??? */
91df8bae1dSRodney W. Grimes 
9256dc72c3SPawel Jakub Dawidek static int	tcp_attach(struct socket *);
934d77a549SAlfred Perlstein static int	tcp_connect(struct tcpcb *, struct sockaddr *,
944d77a549SAlfred Perlstein 		    struct thread *td);
95fb59c426SYoshinobu Inoue #ifdef INET6
964d77a549SAlfred Perlstein static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
974d77a549SAlfred Perlstein 		    struct thread *td);
98fb59c426SYoshinobu Inoue #endif /* INET6 */
99623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
100623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
101b8af5dfaSRobert Watson static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
1022c37256eSGarrett Wollman 
1032c37256eSGarrett Wollman #ifdef TCPDEBUG
1041db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1052c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
1064cc20ab1SSeigo Tanimura #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
1074cc20ab1SSeigo Tanimura 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1082c37256eSGarrett Wollman #else
1092c37256eSGarrett Wollman #define	TCPDEBUG0
1102c37256eSGarrett Wollman #define	TCPDEBUG1()
1112c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1122c37256eSGarrett Wollman #endif
1132c37256eSGarrett Wollman 
1142c37256eSGarrett Wollman /*
1152c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1162c37256eSGarrett Wollman  * and an internet control block.
1172c37256eSGarrett Wollman  */
1182c37256eSGarrett Wollman static int
119b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1202c37256eSGarrett Wollman {
121f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
122623dce13SRobert Watson 	struct tcpcb *tp = NULL;
123623dce13SRobert Watson 	int error;
1242c37256eSGarrett Wollman 	TCPDEBUG0;
1252c37256eSGarrett Wollman 
126623dce13SRobert Watson 	inp = sotoinpcb(so);
127623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1282c37256eSGarrett Wollman 	TCPDEBUG1();
1292c37256eSGarrett Wollman 
13056dc72c3SPawel Jakub Dawidek 	error = tcp_attach(so);
1312c37256eSGarrett Wollman 	if (error)
1322c37256eSGarrett Wollman 		goto out;
1332c37256eSGarrett Wollman 
1342c37256eSGarrett Wollman 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1353879597fSAndrey A. Chernov 		so->so_linger = TCP_LINGERTIME;
136f76fcf6dSJeffrey Hsu 
137f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
138f76fcf6dSJeffrey Hsu 	tp = intotcpcb(inp);
1392c37256eSGarrett Wollman out:
1402c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
1412c37256eSGarrett Wollman 	return error;
1422c37256eSGarrett Wollman }
1432c37256eSGarrett Wollman 
1442c37256eSGarrett Wollman /*
145a152f8a3SRobert Watson  * tcp_detach is called when the socket layer loses its final reference
146a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
147a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
148a152f8a3SRobert Watson  * inpcb state: time wait.
149c78cbc7bSRobert Watson  *
150a152f8a3SRobert Watson  * This function can probably be re-absorbed back into tcp_usr_detach() now
151a152f8a3SRobert Watson  * that there is a single detach path.
1522c37256eSGarrett Wollman  */
153bc725eafSRobert Watson static void
154c78cbc7bSRobert Watson tcp_detach(struct socket *so, struct inpcb *inp)
1552c37256eSGarrett Wollman {
1562c37256eSGarrett Wollman 	struct tcpcb *tp;
157623dce13SRobert Watson #ifdef INET6
158623dce13SRobert Watson 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
159623dce13SRobert Watson #endif
1602c37256eSGarrett Wollman 
161c78cbc7bSRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
162c78cbc7bSRobert Watson 	INP_LOCK_ASSERT(inp);
163623dce13SRobert Watson 
164c78cbc7bSRobert Watson 	KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
165c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
166953b5606SRobert Watson 
167a152f8a3SRobert Watson 	tp = intotcpcb(inp);
168a152f8a3SRobert Watson 
169623dce13SRobert Watson 	if (inp->inp_vflag & INP_TIMEWAIT) {
170623dce13SRobert Watson 		/*
171a152f8a3SRobert Watson 		 * There are two cases to handle: one in which the time wait
172a152f8a3SRobert Watson 		 * state is being discarded (INP_DROPPED), and one in which
173a152f8a3SRobert Watson 		 * this connection will remain in timewait.  In the former,
174a152f8a3SRobert Watson 		 * it is time to discard all state (except tcptw, which has
175a152f8a3SRobert Watson 		 * already been discarded by the timewait close code, which
176a152f8a3SRobert Watson 		 * should be further up the call stack somewhere).  In the
177a152f8a3SRobert Watson 		 * latter case, we detach from the socket, but leave the pcb
178a152f8a3SRobert Watson 		 * present until timewait ends.
179623dce13SRobert Watson 		 *
180a152f8a3SRobert Watson 		 * XXXRW: Would it be cleaner to free the tcptw here?
181623dce13SRobert Watson 		 */
182a152f8a3SRobert Watson 		if (inp->inp_vflag & INP_DROPPED) {
183a152f8a3SRobert Watson 			KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
184a152f8a3SRobert Watson 			    "INP_DROPPED && tp != NULL"));
185623dce13SRobert Watson #ifdef INET6
186623dce13SRobert Watson 			if (isipv6) {
187623dce13SRobert Watson 				in6_pcbdetach(inp);
188623dce13SRobert Watson 				in6_pcbfree(inp);
189623dce13SRobert Watson 			} else {
190623dce13SRobert Watson #endif
191623dce13SRobert Watson 				in_pcbdetach(inp);
192623dce13SRobert Watson 				in_pcbfree(inp);
193623dce13SRobert Watson #ifdef INET6
194623dce13SRobert Watson 			}
195623dce13SRobert Watson #endif
196623dce13SRobert Watson 		} else {
197623dce13SRobert Watson #ifdef INET6
198623dce13SRobert Watson 			if (isipv6)
199623dce13SRobert Watson 				in6_pcbdetach(inp);
200623dce13SRobert Watson 			else
201623dce13SRobert Watson #endif
202623dce13SRobert Watson 				in_pcbdetach(inp);
203f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
204623dce13SRobert Watson 		}
205623dce13SRobert Watson 	} else {
206e6e65783SRobert Watson 		/*
207a152f8a3SRobert Watson 		 * If the connection is not in timewait, we consider two
208a152f8a3SRobert Watson 		 * two conditions: one in which no further processing is
209a152f8a3SRobert Watson 		 * necessary (dropped || embryonic), and one in which TCP is
210a152f8a3SRobert Watson 		 * not yet done, but no longer requires the socket, so the
211a152f8a3SRobert Watson 		 * pcb will persist for the time being.
212a152f8a3SRobert Watson 		 *
213a152f8a3SRobert Watson 		 * XXXRW: Does the second case still occur?
214e6e65783SRobert Watson 		 */
215623dce13SRobert Watson 		if (inp->inp_vflag & INP_DROPPED ||
216623dce13SRobert Watson 		    tp->t_state < TCPS_SYN_SENT) {
217623dce13SRobert Watson 			tcp_discardcb(tp);
218623dce13SRobert Watson #ifdef INET6
219623dce13SRobert Watson 			if (isipv6) {
220a152f8a3SRobert Watson 				in6_pcbdetach(inp);
221a152f8a3SRobert Watson 				in6_pcbfree(inp);
222623dce13SRobert Watson 			} else {
223623dce13SRobert Watson #endif
224623dce13SRobert Watson 				in_pcbdetach(inp);
225623dce13SRobert Watson 				in_pcbfree(inp);
226623dce13SRobert Watson #ifdef INET6
227623dce13SRobert Watson 			}
228623dce13SRobert Watson #endif
229623dce13SRobert Watson 		} else {
230a152f8a3SRobert Watson #ifdef INET6
231a152f8a3SRobert Watson 			if (isipv6)
232a152f8a3SRobert Watson 				in6_pcbdetach(inp);
233a152f8a3SRobert Watson 			else
234a152f8a3SRobert Watson #endif
235a152f8a3SRobert Watson 				in_pcbdetach(inp);
236623dce13SRobert Watson 		}
237623dce13SRobert Watson 	}
238c78cbc7bSRobert Watson }
239c78cbc7bSRobert Watson 
240c78cbc7bSRobert Watson /*
241c78cbc7bSRobert Watson  * pru_detach() detaches the TCP protocol from the socket.
242c78cbc7bSRobert Watson  * If the protocol state is non-embryonic, then can't
243c78cbc7bSRobert Watson  * do this directly: have to initiate a pru_disconnect(),
244c78cbc7bSRobert Watson  * which may finish later; embryonic TCB's can just
245c78cbc7bSRobert Watson  * be discarded here.
246c78cbc7bSRobert Watson  */
247c78cbc7bSRobert Watson static void
248c78cbc7bSRobert Watson tcp_usr_detach(struct socket *so)
249c78cbc7bSRobert Watson {
250c78cbc7bSRobert Watson 	struct inpcb *inp;
251c78cbc7bSRobert Watson 	struct tcpcb *tp;
252c78cbc7bSRobert Watson 	TCPDEBUG0;
253c78cbc7bSRobert Watson 
254c78cbc7bSRobert Watson 	inp = sotoinpcb(so);
255c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
256c78cbc7bSRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
257c78cbc7bSRobert Watson 	INP_LOCK(inp);
258c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
259c78cbc7bSRobert Watson 	    ("tcp_usr_detach: inp_socket == NULL"));
260c78cbc7bSRobert Watson 	TCPDEBUG1();
261c78cbc7bSRobert Watson 
262c78cbc7bSRobert Watson 	tcp_detach(so, inp);
263623dce13SRobert Watson 	tp = NULL;
264623dce13SRobert Watson 	TCPDEBUG2(PRU_DETACH);
265f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&tcbinfo);
2662c37256eSGarrett Wollman }
2672c37256eSGarrett Wollman 
2682c37256eSGarrett Wollman /*
2692c37256eSGarrett Wollman  * Give the socket an address.
2702c37256eSGarrett Wollman  */
2712c37256eSGarrett Wollman static int
272b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2732c37256eSGarrett Wollman {
2742c37256eSGarrett Wollman 	int error = 0;
275f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
276623dce13SRobert Watson 	struct tcpcb *tp = NULL;
2772c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
2782c37256eSGarrett Wollman 
27952710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
28052710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sinp))
28152710de1SPawel Jakub Dawidek 		return (EINVAL);
2822c37256eSGarrett Wollman 	/*
2832c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2842c37256eSGarrett Wollman 	 * to them.
2852c37256eSGarrett Wollman 	 */
2862c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
28752710de1SPawel Jakub Dawidek 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
28852710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
28952710de1SPawel Jakub Dawidek 
290623dce13SRobert Watson 	TCPDEBUG0;
291623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
292623dce13SRobert Watson 	inp = sotoinpcb(so);
293623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
294623dce13SRobert Watson 	INP_LOCK(inp);
295623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
296623dce13SRobert Watson 		error = EINVAL;
2972c37256eSGarrett Wollman 		goto out;
298623dce13SRobert Watson 	}
299623dce13SRobert Watson 	tp = intotcpcb(inp);
300623dce13SRobert Watson 	TCPDEBUG1();
301623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
302623dce13SRobert Watson out:
303623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
304623dce13SRobert Watson 	INP_UNLOCK(inp);
305623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
306623dce13SRobert Watson 
307623dce13SRobert Watson 	return (error);
3082c37256eSGarrett Wollman }
3092c37256eSGarrett Wollman 
310fb59c426SYoshinobu Inoue #ifdef INET6
311fb59c426SYoshinobu Inoue static int
312b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
313fb59c426SYoshinobu Inoue {
314fb59c426SYoshinobu Inoue 	int error = 0;
315f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
316623dce13SRobert Watson 	struct tcpcb *tp = NULL;
317fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
318fb59c426SYoshinobu Inoue 
31952710de1SPawel Jakub Dawidek 	sin6p = (struct sockaddr_in6 *)nam;
32052710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sin6p))
32152710de1SPawel Jakub Dawidek 		return (EINVAL);
322fb59c426SYoshinobu Inoue 	/*
323fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
324fb59c426SYoshinobu Inoue 	 * to them.
325fb59c426SYoshinobu Inoue 	 */
326fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6 &&
32752710de1SPawel Jakub Dawidek 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
32852710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
32952710de1SPawel Jakub Dawidek 
330623dce13SRobert Watson 	TCPDEBUG0;
331623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
332623dce13SRobert Watson 	inp = sotoinpcb(so);
333623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
334623dce13SRobert Watson 	INP_LOCK(inp);
335623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
336623dce13SRobert Watson 		error = EINVAL;
337623dce13SRobert Watson 		goto out;
338623dce13SRobert Watson 	}
339623dce13SRobert Watson 	tp = intotcpcb(inp);
340623dce13SRobert Watson 	TCPDEBUG1();
341fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
342fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
34366ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
344fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
345fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
346fb59c426SYoshinobu Inoue 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
347fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
348fb59c426SYoshinobu Inoue 
349fb59c426SYoshinobu Inoue 			in6_sin6_2_sin(&sin, sin6p);
350fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
351fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
352b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
353b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
354fb59c426SYoshinobu Inoue 			goto out;
355fb59c426SYoshinobu Inoue 		}
356fb59c426SYoshinobu Inoue 	}
357b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
358623dce13SRobert Watson out:
359623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
360623dce13SRobert Watson 	INP_UNLOCK(inp);
361623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
362623dce13SRobert Watson 	return (error);
363fb59c426SYoshinobu Inoue }
364fb59c426SYoshinobu Inoue #endif /* INET6 */
365fb59c426SYoshinobu Inoue 
3662c37256eSGarrett Wollman /*
3672c37256eSGarrett Wollman  * Prepare to accept connections.
3682c37256eSGarrett Wollman  */
3692c37256eSGarrett Wollman static int
370d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
3712c37256eSGarrett Wollman {
3722c37256eSGarrett Wollman 	int error = 0;
373f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
374623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3752c37256eSGarrett Wollman 
376623dce13SRobert Watson 	TCPDEBUG0;
377623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
378623dce13SRobert Watson 	inp = sotoinpcb(so);
379623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
380623dce13SRobert Watson 	INP_LOCK(inp);
381623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
382623dce13SRobert Watson 		error = EINVAL;
383623dce13SRobert Watson 		goto out;
384623dce13SRobert Watson 	}
385623dce13SRobert Watson 	tp = intotcpcb(inp);
386623dce13SRobert Watson 	TCPDEBUG1();
3870daccb9cSRobert Watson 	SOCK_LOCK(so);
3880daccb9cSRobert Watson 	error = solisten_proto_check(so);
3890daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0)
390b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
3910daccb9cSRobert Watson 	if (error == 0) {
3922c37256eSGarrett Wollman 		tp->t_state = TCPS_LISTEN;
393d374e81eSRobert Watson 		solisten_proto(so, backlog);
3940daccb9cSRobert Watson 	}
3950daccb9cSRobert Watson 	SOCK_UNLOCK(so);
396623dce13SRobert Watson 
397623dce13SRobert Watson out:
398623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
399623dce13SRobert Watson 	INP_UNLOCK(inp);
400623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
401623dce13SRobert Watson 	return (error);
4022c37256eSGarrett Wollman }
4032c37256eSGarrett Wollman 
404fb59c426SYoshinobu Inoue #ifdef INET6
405fb59c426SYoshinobu Inoue static int
406d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
407fb59c426SYoshinobu Inoue {
408fb59c426SYoshinobu Inoue 	int error = 0;
409f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
410623dce13SRobert Watson 	struct tcpcb *tp = NULL;
411fb59c426SYoshinobu Inoue 
412623dce13SRobert Watson 	TCPDEBUG0;
413623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
414623dce13SRobert Watson 	inp = sotoinpcb(so);
415623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
416623dce13SRobert Watson 	INP_LOCK(inp);
417623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
418623dce13SRobert Watson 		error = EINVAL;
419623dce13SRobert Watson 		goto out;
420623dce13SRobert Watson 	}
421623dce13SRobert Watson 	tp = intotcpcb(inp);
422623dce13SRobert Watson 	TCPDEBUG1();
4230daccb9cSRobert Watson 	SOCK_LOCK(so);
4240daccb9cSRobert Watson 	error = solisten_proto_check(so);
4250daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0) {
426fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
42766ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
428fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
429b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
430fb59c426SYoshinobu Inoue 	}
4310daccb9cSRobert Watson 	if (error == 0) {
432fb59c426SYoshinobu Inoue 		tp->t_state = TCPS_LISTEN;
433d374e81eSRobert Watson 		solisten_proto(so, backlog);
4340daccb9cSRobert Watson 	}
4350daccb9cSRobert Watson 	SOCK_UNLOCK(so);
436623dce13SRobert Watson 
437623dce13SRobert Watson out:
438623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
439623dce13SRobert Watson 	INP_UNLOCK(inp);
440623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
441623dce13SRobert Watson 	return (error);
442fb59c426SYoshinobu Inoue }
443fb59c426SYoshinobu Inoue #endif /* INET6 */
444fb59c426SYoshinobu Inoue 
4452c37256eSGarrett Wollman /*
4462c37256eSGarrett Wollman  * Initiate connection to peer.
4472c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
4482c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
4492c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
4502c37256eSGarrett Wollman  * Send initial segment on connection.
4512c37256eSGarrett Wollman  */
4522c37256eSGarrett Wollman static int
453b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
4542c37256eSGarrett Wollman {
4552c37256eSGarrett Wollman 	int error = 0;
456f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
457623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4582c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
4592c37256eSGarrett Wollman 
46057bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
461e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
462e29ef13fSDon Lewis 		return (EINVAL);
46352710de1SPawel Jakub Dawidek 	/*
46452710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
46552710de1SPawel Jakub Dawidek 	 */
4662c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
46752710de1SPawel Jakub Dawidek 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
46852710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
469812d8653SSam Leffler 	if (jailed(td->td_ucred))
470a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
47175c13541SPoul-Henning Kamp 
472623dce13SRobert Watson 	TCPDEBUG0;
473623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
474623dce13SRobert Watson 	inp = sotoinpcb(so);
475623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
476623dce13SRobert Watson 	INP_LOCK(inp);
477623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
478623dce13SRobert Watson 		error = EINVAL;
479623dce13SRobert Watson 		goto out;
480623dce13SRobert Watson 	}
481623dce13SRobert Watson 	tp = intotcpcb(inp);
482623dce13SRobert Watson 	TCPDEBUG1();
483b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
4842c37256eSGarrett Wollman 		goto out;
4852c37256eSGarrett Wollman 	error = tcp_output(tp);
486623dce13SRobert Watson out:
487623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
488623dce13SRobert Watson 	INP_UNLOCK(inp);
489623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
490623dce13SRobert Watson 	return (error);
4912c37256eSGarrett Wollman }
4922c37256eSGarrett Wollman 
493fb59c426SYoshinobu Inoue #ifdef INET6
494fb59c426SYoshinobu Inoue static int
495b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
496fb59c426SYoshinobu Inoue {
497fb59c426SYoshinobu Inoue 	int error = 0;
498f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
499623dce13SRobert Watson 	struct tcpcb *tp = NULL;
500fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
501623dce13SRobert Watson 
502623dce13SRobert Watson 	TCPDEBUG0;
503fb59c426SYoshinobu Inoue 
504fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
505e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sin6p))
506e29ef13fSDon Lewis 		return (EINVAL);
50752710de1SPawel Jakub Dawidek 	/*
50852710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
50952710de1SPawel Jakub Dawidek 	 */
510fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6
51152710de1SPawel Jakub Dawidek 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
51252710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
513fb59c426SYoshinobu Inoue 
514623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
515623dce13SRobert Watson 	inp = sotoinpcb(so);
516623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
517623dce13SRobert Watson 	INP_LOCK(inp);
518623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
519623dce13SRobert Watson 		error = EINVAL;
520623dce13SRobert Watson 		goto out;
521623dce13SRobert Watson 	}
522623dce13SRobert Watson 	tp = intotcpcb(inp);
523623dce13SRobert Watson 	TCPDEBUG1();
52433841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
525fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
526fb59c426SYoshinobu Inoue 
527d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
528d46a5312SMaxim Konovalov 			error = EINVAL;
529d46a5312SMaxim Konovalov 			goto out;
530d46a5312SMaxim Konovalov 		}
53133841545SHajimu UMEMOTO 
532fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
533fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
534fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
535b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
536fb59c426SYoshinobu Inoue 			goto out;
537fb59c426SYoshinobu Inoue 		error = tcp_output(tp);
538fb59c426SYoshinobu Inoue 		goto out;
539fb59c426SYoshinobu Inoue 	}
540fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
541fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
542b7d6d952SHajimu UMEMOTO 	inp->inp_inc.inc_isipv6 = 1;
543b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
544fb59c426SYoshinobu Inoue 		goto out;
545fb59c426SYoshinobu Inoue 	error = tcp_output(tp);
546623dce13SRobert Watson 
547623dce13SRobert Watson out:
548623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
549623dce13SRobert Watson 	INP_UNLOCK(inp);
550623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
551623dce13SRobert Watson 	return (error);
552fb59c426SYoshinobu Inoue }
553fb59c426SYoshinobu Inoue #endif /* INET6 */
554fb59c426SYoshinobu Inoue 
5552c37256eSGarrett Wollman /*
5562c37256eSGarrett Wollman  * Initiate disconnect from peer.
5572c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
5582c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
5592c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
5602c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
5612c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
5622c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
5632c37256eSGarrett Wollman  *
5642c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
5652c37256eSGarrett Wollman  */
5662c37256eSGarrett Wollman static int
5672c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
5682c37256eSGarrett Wollman {
569f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
570623dce13SRobert Watson 	struct tcpcb *tp = NULL;
571623dce13SRobert Watson 	int error = 0;
5722c37256eSGarrett Wollman 
573623dce13SRobert Watson 	TCPDEBUG0;
574623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
575623dce13SRobert Watson 	inp = sotoinpcb(so);
576623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
577623dce13SRobert Watson 	INP_LOCK(inp);
578623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
57921367f63SSam Leffler 		error = ECONNRESET;
580623dce13SRobert Watson 		goto out;
581623dce13SRobert Watson 	}
582623dce13SRobert Watson 	tp = intotcpcb(inp);
583623dce13SRobert Watson 	TCPDEBUG1();
584623dce13SRobert Watson 	tcp_disconnect(tp);
585623dce13SRobert Watson out:
586623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
587623dce13SRobert Watson 	INP_UNLOCK(inp);
588623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
589623dce13SRobert Watson 	return (error);
5902c37256eSGarrett Wollman }
5912c37256eSGarrett Wollman 
5922c37256eSGarrett Wollman /*
5932c37256eSGarrett Wollman  * Accept a connection.  Essentially all the work is
5942c37256eSGarrett Wollman  * done at higher levels; just return the address
5952c37256eSGarrett Wollman  * of the peer, storing through addr.
5962c37256eSGarrett Wollman  */
5972c37256eSGarrett Wollman static int
59857bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
5992c37256eSGarrett Wollman {
6002c37256eSGarrett Wollman 	int error = 0;
601f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
6021db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
60326ef6ac4SDon Lewis 	struct in_addr addr;
60426ef6ac4SDon Lewis 	in_port_t port = 0;
6051db24ffbSJonathan Lemon 	TCPDEBUG0;
6062c37256eSGarrett Wollman 
6073d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
6083d2d3ef4SRobert Watson 		return (ECONNABORTED);
609f76fcf6dSJeffrey Hsu 
610f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
611623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
612f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
613623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
6143d2d3ef4SRobert Watson 		error = ECONNABORTED;
615623dce13SRobert Watson 		goto out;
616623dce13SRobert Watson 	}
6171db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
6181db24ffbSJonathan Lemon 	TCPDEBUG1();
619f76fcf6dSJeffrey Hsu 
620f76fcf6dSJeffrey Hsu 	/*
62154d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
62226ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
62326ef6ac4SDon Lewis 	 * release the lock.
624f76fcf6dSJeffrey Hsu 	 */
62526ef6ac4SDon Lewis 	port = inp->inp_fport;
62626ef6ac4SDon Lewis 	addr = inp->inp_faddr;
627f76fcf6dSJeffrey Hsu 
628623dce13SRobert Watson out:
629623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
63026ef6ac4SDon Lewis 	INP_UNLOCK(inp);
63126ef6ac4SDon Lewis 	if (error == 0)
63226ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
63326ef6ac4SDon Lewis 	return error;
6342c37256eSGarrett Wollman }
6352c37256eSGarrett Wollman 
636fb59c426SYoshinobu Inoue #ifdef INET6
637fb59c426SYoshinobu Inoue static int
638fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
639fb59c426SYoshinobu Inoue {
640f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
641fb59c426SYoshinobu Inoue 	int error = 0;
6421db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
64326ef6ac4SDon Lewis 	struct in_addr addr;
64426ef6ac4SDon Lewis 	struct in6_addr addr6;
64526ef6ac4SDon Lewis 	in_port_t port = 0;
64626ef6ac4SDon Lewis 	int v4 = 0;
6471db24ffbSJonathan Lemon 	TCPDEBUG0;
648fb59c426SYoshinobu Inoue 
649b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
650b4470c16SRobert Watson 		return (ECONNABORTED);
651f76fcf6dSJeffrey Hsu 
652f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
653623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
654f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
655623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
65621367f63SSam Leffler 		error = ECONNABORTED;
657623dce13SRobert Watson 		goto out;
658623dce13SRobert Watson 	}
6591db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
6601db24ffbSJonathan Lemon 	TCPDEBUG1();
661623dce13SRobert Watson 
66226ef6ac4SDon Lewis 	/*
66326ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
66426ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
66526ef6ac4SDon Lewis 	 * release the lock.
66626ef6ac4SDon Lewis 	 */
66726ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
66826ef6ac4SDon Lewis 		v4 = 1;
66926ef6ac4SDon Lewis 		port = inp->inp_fport;
67026ef6ac4SDon Lewis 		addr = inp->inp_faddr;
67126ef6ac4SDon Lewis 	} else {
67226ef6ac4SDon Lewis 		port = inp->inp_fport;
67326ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
67426ef6ac4SDon Lewis 	}
67526ef6ac4SDon Lewis 
676623dce13SRobert Watson out:
677623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
67826ef6ac4SDon Lewis 	INP_UNLOCK(inp);
67926ef6ac4SDon Lewis 	if (error == 0) {
68026ef6ac4SDon Lewis 		if (v4)
68126ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
68226ef6ac4SDon Lewis 		else
68326ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
68426ef6ac4SDon Lewis 	}
68526ef6ac4SDon Lewis 	return error;
686fb59c426SYoshinobu Inoue }
687fb59c426SYoshinobu Inoue #endif /* INET6 */
688f76fcf6dSJeffrey Hsu 
689f76fcf6dSJeffrey Hsu /*
6902c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
6912c37256eSGarrett Wollman  */
6922c37256eSGarrett Wollman static int
6932c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
6942c37256eSGarrett Wollman {
6952c37256eSGarrett Wollman 	int error = 0;
696f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
697623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6982c37256eSGarrett Wollman 
699623dce13SRobert Watson 	TCPDEBUG0;
700623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
701623dce13SRobert Watson 	inp = sotoinpcb(so);
702623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
703623dce13SRobert Watson 	INP_LOCK(inp);
704623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
70521367f63SSam Leffler 		error = ECONNRESET;
706623dce13SRobert Watson 		goto out;
707623dce13SRobert Watson 	}
708623dce13SRobert Watson 	tp = intotcpcb(inp);
709623dce13SRobert Watson 	TCPDEBUG1();
7102c37256eSGarrett Wollman 	socantsendmore(so);
711623dce13SRobert Watson 	tcp_usrclosed(tp);
7122c37256eSGarrett Wollman 	error = tcp_output(tp);
713623dce13SRobert Watson 
714623dce13SRobert Watson out:
715623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
716623dce13SRobert Watson 	INP_UNLOCK(inp);
717623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
718623dce13SRobert Watson 
719623dce13SRobert Watson 	return (error);
7202c37256eSGarrett Wollman }
7212c37256eSGarrett Wollman 
7222c37256eSGarrett Wollman /*
7232c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
7242c37256eSGarrett Wollman  */
7252c37256eSGarrett Wollman static int
7262c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
7272c37256eSGarrett Wollman {
728f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
729623dce13SRobert Watson 	struct tcpcb *tp = NULL;
730623dce13SRobert Watson 	int error = 0;
7312c37256eSGarrett Wollman 
732623dce13SRobert Watson 	TCPDEBUG0;
733623dce13SRobert Watson 	inp = sotoinpcb(so);
734623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
735623dce13SRobert Watson 	INP_LOCK(inp);
736623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
73721367f63SSam Leffler 		error = ECONNRESET;
738623dce13SRobert Watson 		goto out;
739623dce13SRobert Watson 	}
740623dce13SRobert Watson 	tp = intotcpcb(inp);
741623dce13SRobert Watson 	TCPDEBUG1();
7422c37256eSGarrett Wollman 	tcp_output(tp);
743623dce13SRobert Watson 
744623dce13SRobert Watson out:
745623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
746623dce13SRobert Watson 	INP_UNLOCK(inp);
747623dce13SRobert Watson 	return (error);
7482c37256eSGarrett Wollman }
7492c37256eSGarrett Wollman 
7502c37256eSGarrett Wollman /*
7512c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
7529c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
7539c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
7549c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
7559c9906e9SPeter Wemm  * generally are caller-frees.
7562c37256eSGarrett Wollman  */
7572c37256eSGarrett Wollman static int
75857bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
759b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
7602c37256eSGarrett Wollman {
7612c37256eSGarrett Wollman 	int error = 0;
762f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
763623dce13SRobert Watson 	struct tcpcb *tp = NULL;
764623dce13SRobert Watson 	int headlocked = 0;
765fb59c426SYoshinobu Inoue #ifdef INET6
766fb59c426SYoshinobu Inoue 	int isipv6;
767fb59c426SYoshinobu Inoue #endif
7689c9906e9SPeter Wemm 	TCPDEBUG0;
7692c37256eSGarrett Wollman 
770f76fcf6dSJeffrey Hsu 	/*
771623dce13SRobert Watson 	 * We require the pcbinfo lock in two cases:
772623dce13SRobert Watson 	 *
773623dce13SRobert Watson 	 * (1) An implied connect is taking place, which can result in
774623dce13SRobert Watson 	 *     binding IPs and ports and hence modification of the pcb hash
775623dce13SRobert Watson 	 *     chains.
776623dce13SRobert Watson 	 *
777623dce13SRobert Watson 	 * (2) PRUS_EOF is set, resulting in explicit close on the send.
778f76fcf6dSJeffrey Hsu 	 */
779623dce13SRobert Watson 	if ((nam != NULL) || (flags & PRUS_EOF)) {
780f76fcf6dSJeffrey Hsu 		INP_INFO_WLOCK(&tcbinfo);
781623dce13SRobert Watson 		headlocked = 1;
782623dce13SRobert Watson 	}
783f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
784623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
785623dce13SRobert Watson 	INP_LOCK(inp);
786623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
7877ff0b850SAndre Oppermann 		if (control)
7887ff0b850SAndre Oppermann 			m_freem(control);
7897ff0b850SAndre Oppermann 		if (m)
7907ff0b850SAndre Oppermann 			m_freem(m);
79121367f63SSam Leffler 		error = ECONNRESET;
7929c9906e9SPeter Wemm 		goto out;
7939c9906e9SPeter Wemm 	}
794fb59c426SYoshinobu Inoue #ifdef INET6
795fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
796fb59c426SYoshinobu Inoue #endif /* INET6 */
7979c9906e9SPeter Wemm 	tp = intotcpcb(inp);
7989c9906e9SPeter Wemm 	TCPDEBUG1();
7999c9906e9SPeter Wemm 	if (control) {
8009c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
8019c9906e9SPeter Wemm 		if (control->m_len) {
8029c9906e9SPeter Wemm 			m_freem(control);
8032c37256eSGarrett Wollman 			if (m)
8042c37256eSGarrett Wollman 				m_freem(m);
805744f87eaSDavid Greenman 			error = EINVAL;
806744f87eaSDavid Greenman 			goto out;
8072c37256eSGarrett Wollman 		}
8089c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
8099c9906e9SPeter Wemm 	}
8102c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
811395bb186SSam Leffler 		sbappendstream(&so->so_snd, m);
8122c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
8132c37256eSGarrett Wollman 			/*
8142c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
8152c37256eSGarrett Wollman 			 * initialize window to default value, and
8162c37256eSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
8172c37256eSGarrett Wollman 			 * MSS.
8182c37256eSGarrett Wollman 			 */
819623dce13SRobert Watson 			INP_INFO_WLOCK_ASSERT(&tcbinfo);
820fb59c426SYoshinobu Inoue #ifdef INET6
821fb59c426SYoshinobu Inoue 			if (isipv6)
822b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
823fb59c426SYoshinobu Inoue 			else
824fb59c426SYoshinobu Inoue #endif /* INET6 */
825b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
8262c37256eSGarrett Wollman 			if (error)
8272c37256eSGarrett Wollman 				goto out;
8282c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
8292c37256eSGarrett Wollman 			tcp_mss(tp, -1);
8302c37256eSGarrett Wollman 		}
8312c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
8322c37256eSGarrett Wollman 			/*
8332c37256eSGarrett Wollman 			 * Close the send side of the connection after
8342c37256eSGarrett Wollman 			 * the data is sent.
8352c37256eSGarrett Wollman 			 */
836623dce13SRobert Watson 			INP_INFO_WLOCK_ASSERT(&tcbinfo);
8372c37256eSGarrett Wollman 			socantsendmore(so);
838623dce13SRobert Watson 			tcp_usrclosed(tp);
8392c37256eSGarrett Wollman 		}
840623dce13SRobert Watson 		if (headlocked) {
841d1401c90SRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
842623dce13SRobert Watson 			headlocked = 0;
843623dce13SRobert Watson 		}
844b0acefa8SBill Fenner 		if (tp != NULL) {
845b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
846b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
8472c37256eSGarrett Wollman 			error = tcp_output(tp);
848b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
849b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
850b0acefa8SBill Fenner 		}
8512c37256eSGarrett Wollman 	} else {
852623dce13SRobert Watson 		/*
853623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
854623dce13SRobert Watson 		 */
855d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
8562c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
857d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
8582c37256eSGarrett Wollman 			m_freem(m);
8592c37256eSGarrett Wollman 			error = ENOBUFS;
8602c37256eSGarrett Wollman 			goto out;
8612c37256eSGarrett Wollman 		}
8622c37256eSGarrett Wollman 		/*
8632c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
8642c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
8652c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
8662c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
8672c37256eSGarrett Wollman 		 * of data past the urgent section.
8682c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
8692c37256eSGarrett Wollman 		 */
870d2bc35abSRobert Watson 		sbappendstream_locked(&so->so_snd, m);
871d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
872ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
873ef53690bSGarrett Wollman 			/*
874ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
875ef53690bSGarrett Wollman 			 * initialize window to default value, and
876ef53690bSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
877ef53690bSGarrett Wollman 			 * MSS.
878ef53690bSGarrett Wollman 			 */
879623dce13SRobert Watson 			INP_INFO_WLOCK_ASSERT(&tcbinfo);
880fb59c426SYoshinobu Inoue #ifdef INET6
881fb59c426SYoshinobu Inoue 			if (isipv6)
882b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
883fb59c426SYoshinobu Inoue 			else
884fb59c426SYoshinobu Inoue #endif /* INET6 */
885b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
886ef53690bSGarrett Wollman 			if (error)
887ef53690bSGarrett Wollman 				goto out;
888ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
889ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
890d1401c90SRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
891623dce13SRobert Watson 			headlocked = 0;
892623dce13SRobert Watson 		} else if (nam) {
893623dce13SRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
894623dce13SRobert Watson 			headlocked = 0;
895623dce13SRobert Watson 		}
8962c37256eSGarrett Wollman 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
8972cdbfa66SPaul Saab 		tp->t_flags |= TF_FORCEDATA;
8982c37256eSGarrett Wollman 		error = tcp_output(tp);
8992cdbfa66SPaul Saab 		tp->t_flags &= ~TF_FORCEDATA;
9002c37256eSGarrett Wollman 	}
901d1401c90SRobert Watson out:
902d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
9032c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
904d1401c90SRobert Watson 	INP_UNLOCK(inp);
905623dce13SRobert Watson 	if (headlocked)
906d1401c90SRobert Watson 		INP_INFO_WUNLOCK(&tcbinfo);
90773fddedaSPeter Grehan 	return (error);
9082c37256eSGarrett Wollman }
9092c37256eSGarrett Wollman 
9102c37256eSGarrett Wollman /*
911a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
9122c37256eSGarrett Wollman  */
913ac45e92fSRobert Watson static void
9142c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
9152c37256eSGarrett Wollman {
916f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
917a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
918623dce13SRobert Watson 	TCPDEBUG0;
919c78cbc7bSRobert Watson 
920ac45e92fSRobert Watson 	inp = sotoinpcb(so);
921c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
922c78cbc7bSRobert Watson 
923c78cbc7bSRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
924ac45e92fSRobert Watson 	INP_LOCK(inp);
925c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
926c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
927c78cbc7bSRobert Watson 
928c78cbc7bSRobert Watson 	/*
929a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
930c78cbc7bSRobert Watson 	 */
931c78cbc7bSRobert Watson 	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
932c78cbc7bSRobert Watson 	    !(inp->inp_vflag & INP_DROPPED)) {
933c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
934a152f8a3SRobert Watson 		TCPDEBUG1();
935c78cbc7bSRobert Watson 		tcp_drop(tp, ECONNABORTED);
936a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
937c78cbc7bSRobert Watson 	}
938a152f8a3SRobert Watson 	if (!(inp->inp_vflag & INP_DROPPED)) {
939a152f8a3SRobert Watson 		SOCK_LOCK(so);
940a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
941a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
942a152f8a3SRobert Watson 		inp->inp_vflag |= INP_SOCKREF;
943a152f8a3SRobert Watson 	}
944a152f8a3SRobert Watson 	INP_UNLOCK(inp);
945a152f8a3SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
946a152f8a3SRobert Watson }
947a152f8a3SRobert Watson 
948a152f8a3SRobert Watson /*
949a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
950a152f8a3SRobert Watson  */
951a152f8a3SRobert Watson static void
952a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
953a152f8a3SRobert Watson {
954a152f8a3SRobert Watson 	struct inpcb *inp;
955a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
956a152f8a3SRobert Watson 	TCPDEBUG0;
957a152f8a3SRobert Watson 
958a152f8a3SRobert Watson 	inp = sotoinpcb(so);
959a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
960a152f8a3SRobert Watson 
961a152f8a3SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
962a152f8a3SRobert Watson 	INP_LOCK(inp);
963a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
964a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
965a152f8a3SRobert Watson 
966a152f8a3SRobert Watson 	/*
967a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
968a152f8a3SRobert Watson 	 * a disconnect.
969a152f8a3SRobert Watson 	 */
970a152f8a3SRobert Watson 	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
971a152f8a3SRobert Watson 	    !(inp->inp_vflag & INP_DROPPED)) {
972a152f8a3SRobert Watson 		tp = intotcpcb(inp);
973a152f8a3SRobert Watson 		TCPDEBUG1();
974a152f8a3SRobert Watson 		tcp_disconnect(tp);
975a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
976a152f8a3SRobert Watson 	}
977a152f8a3SRobert Watson 	if (!(inp->inp_vflag & INP_DROPPED)) {
978a152f8a3SRobert Watson 		SOCK_LOCK(so);
979a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
980a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
981a152f8a3SRobert Watson 		inp->inp_vflag |= INP_SOCKREF;
982a152f8a3SRobert Watson 	}
983a152f8a3SRobert Watson 	INP_UNLOCK(inp);
984ac45e92fSRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
9852c37256eSGarrett Wollman }
9862c37256eSGarrett Wollman 
9872c37256eSGarrett Wollman /*
9882c37256eSGarrett Wollman  * Receive out-of-band data.
9892c37256eSGarrett Wollman  */
9902c37256eSGarrett Wollman static int
9912c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
9922c37256eSGarrett Wollman {
9932c37256eSGarrett Wollman 	int error = 0;
994f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
995623dce13SRobert Watson 	struct tcpcb *tp = NULL;
9962c37256eSGarrett Wollman 
997623dce13SRobert Watson 	TCPDEBUG0;
998623dce13SRobert Watson 	inp = sotoinpcb(so);
999623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1000623dce13SRobert Watson 	INP_LOCK(inp);
1001623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
100221367f63SSam Leffler 		error = ECONNRESET;
1003623dce13SRobert Watson 		goto out;
1004623dce13SRobert Watson 	}
1005623dce13SRobert Watson 	tp = intotcpcb(inp);
1006623dce13SRobert Watson 	TCPDEBUG1();
10072c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1008c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
10094cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
10104cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
10112c37256eSGarrett Wollman 		error = EINVAL;
10122c37256eSGarrett Wollman 		goto out;
10132c37256eSGarrett Wollman 	}
10142c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
10152c37256eSGarrett Wollman 		error = EWOULDBLOCK;
10162c37256eSGarrett Wollman 		goto out;
10172c37256eSGarrett Wollman 	}
10182c37256eSGarrett Wollman 	m->m_len = 1;
10192c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
10202c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
10212c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1022623dce13SRobert Watson 
1023623dce13SRobert Watson out:
1024623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
1025623dce13SRobert Watson 	INP_UNLOCK(inp);
1026623dce13SRobert Watson 	return (error);
10272c37256eSGarrett Wollman }
10282c37256eSGarrett Wollman 
10292c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1030756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1031756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1032756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1033756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1034756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1035756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1036756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1037756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1038756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
103954d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1040756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1041756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1042756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
1043756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
104454d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1045a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1046a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
10472c37256eSGarrett Wollman };
1048df8bae1dSRodney W. Grimes 
1049fb59c426SYoshinobu Inoue #ifdef INET6
1050fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1051756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1052756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1053756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1054756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1055756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1056756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1057756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1058756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1059756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1060756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1061756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1062756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1063756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
1064756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1065756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1066a152f8a3SRobert Watson  	.pru_sosetlabel =	in_pcbsosetlabel,
1067a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1068fb59c426SYoshinobu Inoue };
1069fb59c426SYoshinobu Inoue #endif /* INET6 */
1070fb59c426SYoshinobu Inoue 
1071a0292f23SGarrett Wollman /*
1072a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1073a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
10745200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
10755200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
10765200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
10775200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1078a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1079a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1080a0292f23SGarrett Wollman  */
10810312fbe9SPoul-Henning Kamp static int
1082ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1083a0292f23SGarrett Wollman {
1084a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1085a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
10865200e00eSIan Dowse 	struct in_addr laddr;
10875200e00eSIan Dowse 	u_short lport;
1088c3229e05SDavid Greenman 	int error;
1089a0292f23SGarrett Wollman 
1090623dce13SRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1091623dce13SRobert Watson 	INP_LOCK_ASSERT(inp);
1092623dce13SRobert Watson 
1093a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
1094b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1095a0292f23SGarrett Wollman 		if (error)
1096a0292f23SGarrett Wollman 			return error;
1097a0292f23SGarrett Wollman 	}
1098a0292f23SGarrett Wollman 
1099a0292f23SGarrett Wollman 	/*
1100a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1101a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1102a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1103a0292f23SGarrett Wollman 	 */
11045200e00eSIan Dowse 	laddr = inp->inp_laddr;
11055200e00eSIan Dowse 	lport = inp->inp_lport;
11065200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1107b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
11085200e00eSIan Dowse 	if (error && oinp == NULL)
1109d3628763SRodney W. Grimes 		return error;
1110c94c54e4SAndre Oppermann 	if (oinp)
1111a0292f23SGarrett Wollman 		return EADDRINUSE;
11125200e00eSIan Dowse 	inp->inp_laddr = laddr;
111315bd2b43SDavid Greenman 	in_pcbrehash(inp);
1114a0292f23SGarrett Wollman 
1115087b55eaSAndre Oppermann 	/*
1116087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1117087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1118087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1119087b55eaSAndre Oppermann 	 * XXX: This should be based on the actual MSS.
1120087b55eaSAndre Oppermann 	 */
1121a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1122087b55eaSAndre Oppermann 	    (0x1 << tp->request_r_scale) < tcp_minmss)
1123a0292f23SGarrett Wollman 		tp->request_r_scale++;
1124a0292f23SGarrett Wollman 
1125a0292f23SGarrett Wollman 	soisconnecting(so);
1126a0292f23SGarrett Wollman 	tcpstat.tcps_connattempt++;
1127a0292f23SGarrett Wollman 	tp->t_state = TCPS_SYN_SENT;
1128b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1129b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
11301fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
1131a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1132a45d2726SAndras Olah 
1133a0292f23SGarrett Wollman 	return 0;
1134a0292f23SGarrett Wollman }
1135a0292f23SGarrett Wollman 
1136fb59c426SYoshinobu Inoue #ifdef INET6
1137fb59c426SYoshinobu Inoue static int
1138ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1139fb59c426SYoshinobu Inoue {
1140fb59c426SYoshinobu Inoue 	struct inpcb *inp = tp->t_inpcb, *oinp;
1141fb59c426SYoshinobu Inoue 	struct socket *so = inp->inp_socket;
1142fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1143fb59c426SYoshinobu Inoue 	struct in6_addr *addr6;
1144fb59c426SYoshinobu Inoue 	int error;
1145fb59c426SYoshinobu Inoue 
1146623dce13SRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1147623dce13SRobert Watson 	INP_LOCK_ASSERT(inp);
1148623dce13SRobert Watson 
1149fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
1150b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1151fb59c426SYoshinobu Inoue 		if (error)
1152fb59c426SYoshinobu Inoue 			return error;
1153fb59c426SYoshinobu Inoue 	}
1154fb59c426SYoshinobu Inoue 
1155fb59c426SYoshinobu Inoue 	/*
1156fb59c426SYoshinobu Inoue 	 * Cannot simply call in_pcbconnect, because there might be an
1157fb59c426SYoshinobu Inoue 	 * earlier incarnation of this same connection still in
1158fb59c426SYoshinobu Inoue 	 * TIME_WAIT state, creating an ADDRINUSE error.
1159a1f7e5f8SHajimu UMEMOTO 	 * in6_pcbladdr() also handles scope zone IDs.
1160fb59c426SYoshinobu Inoue 	 */
1161fb59c426SYoshinobu Inoue 	error = in6_pcbladdr(inp, nam, &addr6);
1162fb59c426SYoshinobu Inoue 	if (error)
1163fb59c426SYoshinobu Inoue 		return error;
1164fb59c426SYoshinobu Inoue 	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1165fb59c426SYoshinobu Inoue 				  &sin6->sin6_addr, sin6->sin6_port,
1166fb59c426SYoshinobu Inoue 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1167fb59c426SYoshinobu Inoue 				  ? addr6
1168fb59c426SYoshinobu Inoue 				  : &inp->in6p_laddr,
1169fb59c426SYoshinobu Inoue 				  inp->inp_lport,  0, NULL);
1170c94c54e4SAndre Oppermann 	if (oinp)
1171fb59c426SYoshinobu Inoue 		return EADDRINUSE;
1172fb59c426SYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1173fb59c426SYoshinobu Inoue 		inp->in6p_laddr = *addr6;
1174fb59c426SYoshinobu Inoue 	inp->in6p_faddr = sin6->sin6_addr;
1175fb59c426SYoshinobu Inoue 	inp->inp_fport = sin6->sin6_port;
11768a59da30SHajimu UMEMOTO 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
11778a59da30SHajimu UMEMOTO 	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
11788a59da30SHajimu UMEMOTO 	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
11798a59da30SHajimu UMEMOTO 		inp->in6p_flowinfo |=
11808a59da30SHajimu UMEMOTO 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1181fb59c426SYoshinobu Inoue 	in_pcbrehash(inp);
1182fb59c426SYoshinobu Inoue 
1183fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1184fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1185fb59c426SYoshinobu Inoue 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1186fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1187fb59c426SYoshinobu Inoue 
1188fb59c426SYoshinobu Inoue 	soisconnecting(so);
1189fb59c426SYoshinobu Inoue 	tcpstat.tcps_connattempt++;
1190fb59c426SYoshinobu Inoue 	tp->t_state = TCPS_SYN_SENT;
1191b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1192b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
11931fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
1194fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1195fb59c426SYoshinobu Inoue 
1196fb59c426SYoshinobu Inoue 	return 0;
1197fb59c426SYoshinobu Inoue }
1198fb59c426SYoshinobu Inoue #endif /* INET6 */
1199fb59c426SYoshinobu Inoue 
1200cfe8b629SGarrett Wollman /*
1201b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1202b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1203b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1204b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1205b8af5dfaSRobert Watson  * from Linux.
1206b8af5dfaSRobert Watson  */
1207b8af5dfaSRobert Watson static void
1208ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1209b8af5dfaSRobert Watson {
1210b8af5dfaSRobert Watson 
1211b8af5dfaSRobert Watson 	INP_LOCK_ASSERT(tp->t_inpcb);
1212b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1213b8af5dfaSRobert Watson 
1214b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1215b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1216b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
12173529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1218b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1219b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1220b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1221b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1222b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1223b8af5dfaSRobert Watson 	}
12241baaf834SBruce M Simpson 
12251baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
12261baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
12271baaf834SBruce M Simpson 
1228b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1229b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1230b8af5dfaSRobert Watson 
1231b8af5dfaSRobert Watson 	/*
1232b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1233b8af5dfaSRobert Watson 	 */
1234c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1235b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
1236b8af5dfaSRobert Watson 	ti->tcpi_snd_bwnd = tp->snd_bwnd;
1237b8af5dfaSRobert Watson }
1238b8af5dfaSRobert Watson 
1239b8af5dfaSRobert Watson /*
1240cfe8b629SGarrett Wollman  * The new sockopt interface makes it possible for us to block in the
1241cfe8b629SGarrett Wollman  * copyin/out step (if we take a page fault).  Taking a page fault at
1242cfe8b629SGarrett Wollman  * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
1243cfe8b629SGarrett Wollman  * use TSM, there probably isn't any need for this function to run at
1244cfe8b629SGarrett Wollman  * splnet() any more.  This needs more examination.)
1245b8af5dfaSRobert Watson  *
1246b8af5dfaSRobert Watson  * XXXRW: The locking here is wrong; we may take a page fault while holding
1247b8af5dfaSRobert Watson  * the inpcb lock.
1248cfe8b629SGarrett Wollman  */
1249df8bae1dSRodney W. Grimes int
1250ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1251df8bae1dSRodney W. Grimes {
12523f9d1ef9SRobert Watson 	int	error, opt, optval;
1253df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1254cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
1255b8af5dfaSRobert Watson 	struct	tcp_info ti;
1256df8bae1dSRodney W. Grimes 
1257cfe8b629SGarrett Wollman 	error = 0;
1258df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1259623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1260f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1261cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
12624e397bc5SRobert Watson 		INP_UNLOCK(inp);
1263fb59c426SYoshinobu Inoue #ifdef INET6
1264fb59c426SYoshinobu Inoue 		if (INP_CHECK_SOCKAF(so, AF_INET6))
1265fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
1266fb59c426SYoshinobu Inoue 		else
1267fb59c426SYoshinobu Inoue #endif /* INET6 */
1268cfe8b629SGarrett Wollman 		error = ip_ctloutput(so, sopt);
1269df8bae1dSRodney W. Grimes 		return (error);
1270df8bae1dSRodney W. Grimes 	}
1271623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
1272623dce13SRobert Watson 		error = ECONNRESET;
1273623dce13SRobert Watson 		goto out;
1274623dce13SRobert Watson 	}
1275df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
1276df8bae1dSRodney W. Grimes 
1277cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1278cfe8b629SGarrett Wollman 	case SOPT_SET:
1279cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
12801cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
128188f6b043SBruce M Simpson 		case TCP_MD5SIG:
12821cfd4b53SBruce M Simpson 			error = sooptcopyin(sopt, &optval, sizeof optval,
12831cfd4b53SBruce M Simpson 					    sizeof optval);
12841cfd4b53SBruce M Simpson 			if (error)
12851cfd4b53SBruce M Simpson 				break;
12861cfd4b53SBruce M Simpson 
12871cfd4b53SBruce M Simpson 			if (optval > 0)
12881cfd4b53SBruce M Simpson 				tp->t_flags |= TF_SIGNATURE;
12891cfd4b53SBruce M Simpson 			else
12901cfd4b53SBruce M Simpson 				tp->t_flags &= ~TF_SIGNATURE;
12911cfd4b53SBruce M Simpson 			break;
12921cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
1293df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1294cfe8b629SGarrett Wollman 		case TCP_NOOPT:
1295cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1296cfe8b629SGarrett Wollman 					    sizeof optval);
1297cfe8b629SGarrett Wollman 			if (error)
1298cfe8b629SGarrett Wollman 				break;
1299cfe8b629SGarrett Wollman 
1300cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1301cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1302cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1303cfe8b629SGarrett Wollman 				break;
1304cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1305cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1306cfe8b629SGarrett Wollman 				break;
1307cfe8b629SGarrett Wollman 			default:
1308cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1309cfe8b629SGarrett Wollman 				break;
1310cfe8b629SGarrett Wollman 			}
1311cfe8b629SGarrett Wollman 
1312cfe8b629SGarrett Wollman 			if (optval)
1313cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1314df8bae1dSRodney W. Grimes 			else
1315cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
1316df8bae1dSRodney W. Grimes 			break;
1317df8bae1dSRodney W. Grimes 
1318007581c0SJonathan Lemon 		case TCP_NOPUSH:
1319007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1320007581c0SJonathan Lemon 					    sizeof optval);
1321007581c0SJonathan Lemon 			if (error)
1322007581c0SJonathan Lemon 				break;
1323007581c0SJonathan Lemon 
1324007581c0SJonathan Lemon 			if (optval)
1325007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1326007581c0SJonathan Lemon 			else {
1327007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1328007581c0SJonathan Lemon 				error = tcp_output(tp);
1329007581c0SJonathan Lemon 			}
1330007581c0SJonathan Lemon 			break;
1331007581c0SJonathan Lemon 
1332df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1333cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1334cfe8b629SGarrett Wollman 					    sizeof optval);
1335cfe8b629SGarrett Wollman 			if (error)
1336df8bae1dSRodney W. Grimes 				break;
1337df8bae1dSRodney W. Grimes 
133853369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
133953369ac9SAndre Oppermann 			    optval + 40 >= tcp_minmss)
1340cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1341a0292f23SGarrett Wollman 			else
1342a0292f23SGarrett Wollman 				error = EINVAL;
1343a0292f23SGarrett Wollman 			break;
1344a0292f23SGarrett Wollman 
1345b8af5dfaSRobert Watson 		case TCP_INFO:
1346b8af5dfaSRobert Watson 			error = EINVAL;
1347b8af5dfaSRobert Watson 			break;
1348b8af5dfaSRobert Watson 
1349df8bae1dSRodney W. Grimes 		default:
1350df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1351df8bae1dSRodney W. Grimes 			break;
1352df8bae1dSRodney W. Grimes 		}
1353df8bae1dSRodney W. Grimes 		break;
1354df8bae1dSRodney W. Grimes 
1355cfe8b629SGarrett Wollman 	case SOPT_GET:
1356cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
13571cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
135888f6b043SBruce M Simpson 		case TCP_MD5SIG:
13591cfd4b53SBruce M Simpson 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1360b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
13611cfd4b53SBruce M Simpson 			break;
1362265ed012SBruce M Simpson #endif
1363df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1364cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
1365b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1366df8bae1dSRodney W. Grimes 			break;
1367df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1368cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
1369b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1370df8bae1dSRodney W. Grimes 			break;
1371a0292f23SGarrett Wollman 		case TCP_NOOPT:
1372cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
1373b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1374a0292f23SGarrett Wollman 			break;
1375a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1376cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
1377b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1378b8af5dfaSRobert Watson 			break;
1379b8af5dfaSRobert Watson 		case TCP_INFO:
1380b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
1381b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
1382a0292f23SGarrett Wollman 			break;
1383df8bae1dSRodney W. Grimes 		default:
1384df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1385df8bae1dSRodney W. Grimes 			break;
1386df8bae1dSRodney W. Grimes 		}
1387df8bae1dSRodney W. Grimes 		break;
1388df8bae1dSRodney W. Grimes 	}
1389623dce13SRobert Watson out:
1390f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1391df8bae1dSRodney W. Grimes 	return (error);
1392df8bae1dSRodney W. Grimes }
1393df8bae1dSRodney W. Grimes 
139426e30fbbSDavid Greenman /*
139526e30fbbSDavid Greenman  * tcp_sendspace and tcp_recvspace are the default send and receive window
139626e30fbbSDavid Greenman  * sizes, respectively.  These are obsolescent (this information should
139726e30fbbSDavid Greenman  * be set by the route).
139826e30fbbSDavid Greenman  */
139981e561cdSDavid E. O'Brien u_long	tcp_sendspace = 1024*32;
1400e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
14013d177f46SBill Fumerola     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
140281e561cdSDavid E. O'Brien u_long	tcp_recvspace = 1024*64;
1403e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
14043d177f46SBill Fumerola     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1405df8bae1dSRodney W. Grimes 
1406df8bae1dSRodney W. Grimes /*
1407df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1408df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1409df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1410df8bae1dSRodney W. Grimes  */
14110312fbe9SPoul-Henning Kamp static int
1412ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so)
1413df8bae1dSRodney W. Grimes {
1414ad3f9ab3SAndre Oppermann 	struct tcpcb *tp;
1415df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1416df8bae1dSRodney W. Grimes 	int error;
1417fb59c426SYoshinobu Inoue #ifdef INET6
14184a6a94d8SArchie Cobbs 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1419fb59c426SYoshinobu Inoue #endif
1420df8bae1dSRodney W. Grimes 
1421df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1422df8bae1dSRodney W. Grimes 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1423df8bae1dSRodney W. Grimes 		if (error)
1424df8bae1dSRodney W. Grimes 			return (error);
1425df8bae1dSRodney W. Grimes 	}
14266741ecf5SAndre Oppermann 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
14276741ecf5SAndre Oppermann 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1428f2de87feSRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
1429d915b280SStephan Uphoff 	error = in_pcballoc(so, &tcbinfo);
1430f2de87feSRobert Watson 	if (error) {
1431f2de87feSRobert Watson 		INP_INFO_WUNLOCK(&tcbinfo);
1432df8bae1dSRodney W. Grimes 		return (error);
1433f2de87feSRobert Watson 	}
1434df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1435fb59c426SYoshinobu Inoue #ifdef INET6
1436fb59c426SYoshinobu Inoue 	if (isipv6) {
1437fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1438fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1439fb59c426SYoshinobu Inoue 	}
1440fb59c426SYoshinobu Inoue 	else
1441fb59c426SYoshinobu Inoue #endif
1442cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1443df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1444623dce13SRobert Watson 	if (tp == NULL) {
1445fb59c426SYoshinobu Inoue #ifdef INET6
1446623dce13SRobert Watson 		if (isipv6) {
1447fb59c426SYoshinobu Inoue 			in6_pcbdetach(inp);
1448623dce13SRobert Watson 			in6_pcbfree(inp);
1449623dce13SRobert Watson 		} else {
1450fb59c426SYoshinobu Inoue #endif
1451df8bae1dSRodney W. Grimes 			in_pcbdetach(inp);
1452623dce13SRobert Watson 			in_pcbfree(inp);
1453623dce13SRobert Watson #ifdef INET6
1454623dce13SRobert Watson 		}
1455623dce13SRobert Watson #endif
1456f2de87feSRobert Watson 		INP_INFO_WUNLOCK(&tcbinfo);
1457df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1458df8bae1dSRodney W. Grimes 	}
1459df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
1460f2de87feSRobert Watson 	INP_UNLOCK(inp);
1461f2de87feSRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
1462df8bae1dSRodney W. Grimes 	return (0);
1463df8bae1dSRodney W. Grimes }
1464df8bae1dSRodney W. Grimes 
1465df8bae1dSRodney W. Grimes /*
1466df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1467df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1468df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1469df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1470df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1471df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1472df8bae1dSRodney W. Grimes  */
1473623dce13SRobert Watson static void
1474ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
1475df8bae1dSRodney W. Grimes {
1476e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
1477e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
1478e6e0b5ffSRobert Watson 
1479e6e0b5ffSRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1480e6e0b5ffSRobert Watson 	INP_LOCK_ASSERT(inp);
1481df8bae1dSRodney W. Grimes 
1482623dce13SRobert Watson 	/*
1483623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1484623dce13SRobert Watson 	 * socket is still open.
1485623dce13SRobert Watson 	 */
1486623dce13SRobert Watson 	if (tp->t_state < TCPS_ESTABLISHED) {
1487df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1488623dce13SRobert Watson 		KASSERT(tp != NULL,
1489623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
1490623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1491243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
1492623dce13SRobert Watson 		KASSERT(tp != NULL,
1493623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
1494623dce13SRobert Watson 	} else {
1495df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
1496df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
1497623dce13SRobert Watson 		tcp_usrclosed(tp);
1498953b5606SRobert Watson 		if (!(inp->inp_vflag & INP_DROPPED))
1499623dce13SRobert Watson 			tcp_output(tp);
1500df8bae1dSRodney W. Grimes 	}
1501df8bae1dSRodney W. Grimes }
1502df8bae1dSRodney W. Grimes 
1503df8bae1dSRodney W. Grimes /*
1504df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1505df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1506df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1507df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1508df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1509df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1510df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1511df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1512df8bae1dSRodney W. Grimes  */
1513623dce13SRobert Watson static void
1514ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
1515df8bae1dSRodney W. Grimes {
1516df8bae1dSRodney W. Grimes 
1517e6e0b5ffSRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1518e6e0b5ffSRobert Watson 	INP_LOCK_ASSERT(tp->t_inpcb);
1519e6e0b5ffSRobert Watson 
1520df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1521df8bae1dSRodney W. Grimes 	case TCPS_CLOSED:
1522df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
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 	}
1722497057eeSRobert Watson }
1723497057eeSRobert Watson 
1724497057eeSRobert Watson static void
1725497057eeSRobert Watson db_print_toobflags(char t_oobflags)
1726497057eeSRobert Watson {
1727497057eeSRobert Watson 	int comma;
1728497057eeSRobert Watson 
1729497057eeSRobert Watson 	comma = 0;
1730497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
1731497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1732497057eeSRobert Watson 		comma = 1;
1733497057eeSRobert Watson 	}
1734497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
1735497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1736497057eeSRobert Watson 		comma = 1;
1737497057eeSRobert Watson 	}
1738497057eeSRobert Watson }
1739497057eeSRobert Watson 
1740497057eeSRobert Watson static void
1741497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1742497057eeSRobert Watson {
1743497057eeSRobert Watson 
1744497057eeSRobert Watson 	db_print_indent(indent);
1745497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
1746497057eeSRobert Watson 
1747497057eeSRobert Watson 	indent += 2;
1748497057eeSRobert Watson 
1749497057eeSRobert Watson 	db_print_indent(indent);
1750497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
1751497057eeSRobert Watson 	   LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
1752497057eeSRobert Watson 
1753497057eeSRobert Watson 	db_print_indent(indent);
1754b8152ba7SAndre Oppermann 	db_printf("t_inpcb: %p   t_timers: %p   tt_active: %x\n",
1755b8152ba7SAndre Oppermann 	    tp->t_inpcb, tp->t_timers, tp->t_timers->tt_active);
1756497057eeSRobert Watson 
1757497057eeSRobert Watson 	db_print_indent(indent);
1758b8152ba7SAndre Oppermann 	db_printf("tt_delack: %i   tt_rexmt: %i   tt_keep: %i   "
1759b8152ba7SAndre Oppermann 	    "tt_persist: %i   tt_2msl: %i\n",
1760b8152ba7SAndre Oppermann 	    tp->t_timers->tt_delack, tp->t_timers->tt_rexmt,
1761b8152ba7SAndre Oppermann 	    tp->t_timers->tt_keep, tp->t_timers->tt_persist,
1762b8152ba7SAndre Oppermann 	    tp->t_timers->tt_2msl);
1763497057eeSRobert Watson 
1764497057eeSRobert Watson 	db_print_indent(indent);
1765497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
1766497057eeSRobert Watson 	db_print_tstate(tp->t_state);
1767497057eeSRobert Watson 	db_printf(")\n");
1768497057eeSRobert Watson 
1769497057eeSRobert Watson 	db_print_indent(indent);
1770497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
1771497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
1772497057eeSRobert Watson 	db_printf(")\n");
1773497057eeSRobert Watson 
1774497057eeSRobert Watson 	db_print_indent(indent);
1775497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
1776497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
1777497057eeSRobert Watson 
1778497057eeSRobert Watson 	db_print_indent(indent);
1779497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
1780497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
1781497057eeSRobert Watson 
1782497057eeSRobert Watson 	db_print_indent(indent);
1783497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
1784497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
1785497057eeSRobert Watson 
1786497057eeSRobert Watson 	db_print_indent(indent);
1787497057eeSRobert Watson 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %lu   rcv_up: 0x%08x\n",
1788497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
1789497057eeSRobert Watson 
1790497057eeSRobert Watson 	db_print_indent(indent);
1791497057eeSRobert Watson 	db_printf("snd_wnd: %lu   snd_cwnd: %lu   snd_bwnd: %lu\n",
1792497057eeSRobert Watson 	   tp->snd_wnd, tp->snd_cwnd, tp->snd_bwnd);
1793497057eeSRobert Watson 
1794497057eeSRobert Watson 	db_print_indent(indent);
1795497057eeSRobert Watson 	db_printf("snd_ssthresh: %lu   snd_bandwidth: %lu   snd_recover: "
1796497057eeSRobert Watson 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_bandwidth,
1797497057eeSRobert Watson 	    tp->snd_recover);
1798497057eeSRobert Watson 
1799497057eeSRobert Watson 	db_print_indent(indent);
1800497057eeSRobert Watson 	db_printf("t_maxopd: %u   t_rcvtime: %lu   t_startime: %lu\n",
1801497057eeSRobert Watson 	    tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
1802497057eeSRobert Watson 
1803497057eeSRobert Watson 	db_print_indent(indent);
1804497057eeSRobert Watson 	db_printf("t_rttime: %d   t_rtsq: 0x%08x   t_bw_rtttime: %d\n",
1805497057eeSRobert Watson 	    tp->t_rtttime, tp->t_rtseq, tp->t_bw_rtttime);
1806497057eeSRobert Watson 
1807497057eeSRobert Watson 	db_print_indent(indent);
1808497057eeSRobert Watson 	db_printf("t_bw_rtseq: 0x%08x   t_rxtcur: %d   t_maxseg: %u   "
1809497057eeSRobert Watson 	    "t_srtt: %d\n", tp->t_bw_rtseq, tp->t_rxtcur, tp->t_maxseg,
1810497057eeSRobert Watson 	    tp->t_srtt);
1811497057eeSRobert Watson 
1812497057eeSRobert Watson 	db_print_indent(indent);
1813497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
1814497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
1815497057eeSRobert Watson 	    tp->t_rttbest);
1816497057eeSRobert Watson 
1817497057eeSRobert Watson 	db_print_indent(indent);
1818497057eeSRobert Watson 	db_printf("t_rttupdated: %lu   max_sndwnd: %lu   t_softerror: %d\n",
1819497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
1820497057eeSRobert Watson 
1821497057eeSRobert Watson 	db_print_indent(indent);
1822497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
1823497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
1824497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
1825497057eeSRobert Watson 
1826497057eeSRobert Watson 	db_print_indent(indent);
1827497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
1828497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
1829497057eeSRobert Watson 
1830497057eeSRobert Watson 	db_print_indent(indent);
18311a553740SAndre Oppermann 	db_printf("ts_recent: %u   ts_recent_age: %lu\n",
18321a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
1833497057eeSRobert Watson 
1834497057eeSRobert Watson 	db_print_indent(indent);
1835497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
1836497057eeSRobert Watson 	    "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
1837497057eeSRobert Watson 
1838497057eeSRobert Watson 	db_print_indent(indent);
1839497057eeSRobert Watson 	db_printf("snd_ssthresh_prev: %lu   snd_recover_prev: 0x%08x   "
1840497057eeSRobert Watson 	    "t_badrxtwin: %lu\n", tp->snd_ssthresh_prev,
1841497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
1842497057eeSRobert Watson 
1843497057eeSRobert Watson 	db_print_indent(indent);
18443529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
18453529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
1846497057eeSRobert Watson 
1847497057eeSRobert Watson 	db_print_indent(indent);
1848497057eeSRobert Watson 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
1849497057eeSRobert Watson 	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
1850497057eeSRobert Watson 
1851497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
1852497057eeSRobert Watson 
1853497057eeSRobert Watson 	db_print_indent(indent);
1854497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
1855497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
1856497057eeSRobert Watson }
1857497057eeSRobert Watson 
1858497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
1859497057eeSRobert Watson {
1860497057eeSRobert Watson 	struct tcpcb *tp;
1861497057eeSRobert Watson 
1862497057eeSRobert Watson 	if (!have_addr) {
1863497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
1864497057eeSRobert Watson 		return;
1865497057eeSRobert Watson 	}
1866497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
1867497057eeSRobert Watson 
1868497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
1869497057eeSRobert Watson }
1870497057eeSRobert Watson #endif
1871