xref: /freebsd/sys/netinet/tcp_usrreq.c (revision b8152ba79343a5a011bd5f4c856e29fb9ee7a9fe)
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 	/*
62126ef6ac4SDon Lewis 	 * We inline in_setpeeraddr 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 /*
690f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setsockaddr. We just pass down
691f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
692f76fcf6dSJeffrey Hsu  * here because in_setsockaddr will call malloc and can block.
693f76fcf6dSJeffrey Hsu  */
694f76fcf6dSJeffrey Hsu static int
695f76fcf6dSJeffrey Hsu tcp_sockaddr(struct socket *so, struct sockaddr **nam)
696f76fcf6dSJeffrey Hsu {
697f76fcf6dSJeffrey Hsu 	return (in_setsockaddr(so, nam, &tcbinfo));
698f76fcf6dSJeffrey Hsu }
699f76fcf6dSJeffrey Hsu 
700f76fcf6dSJeffrey Hsu /*
701f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setpeeraddr. We just pass down
702f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setpeeraddr to lock.
703f76fcf6dSJeffrey Hsu  */
704f76fcf6dSJeffrey Hsu static int
705f76fcf6dSJeffrey Hsu tcp_peeraddr(struct socket *so, struct sockaddr **nam)
706f76fcf6dSJeffrey Hsu {
707f76fcf6dSJeffrey Hsu 	return (in_setpeeraddr(so, nam, &tcbinfo));
708f76fcf6dSJeffrey Hsu }
709f76fcf6dSJeffrey Hsu 
7102c37256eSGarrett Wollman /*
7112c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
7122c37256eSGarrett Wollman  */
7132c37256eSGarrett Wollman static int
7142c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
7152c37256eSGarrett Wollman {
7162c37256eSGarrett Wollman 	int error = 0;
717f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
718623dce13SRobert Watson 	struct tcpcb *tp = NULL;
7192c37256eSGarrett Wollman 
720623dce13SRobert Watson 	TCPDEBUG0;
721623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
722623dce13SRobert Watson 	inp = sotoinpcb(so);
723623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
724623dce13SRobert Watson 	INP_LOCK(inp);
725623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
72621367f63SSam Leffler 		error = ECONNRESET;
727623dce13SRobert Watson 		goto out;
728623dce13SRobert Watson 	}
729623dce13SRobert Watson 	tp = intotcpcb(inp);
730623dce13SRobert Watson 	TCPDEBUG1();
7312c37256eSGarrett Wollman 	socantsendmore(so);
732623dce13SRobert Watson 	tcp_usrclosed(tp);
7332c37256eSGarrett Wollman 	error = tcp_output(tp);
734623dce13SRobert Watson 
735623dce13SRobert Watson out:
736623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
737623dce13SRobert Watson 	INP_UNLOCK(inp);
738623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
739623dce13SRobert Watson 
740623dce13SRobert Watson 	return (error);
7412c37256eSGarrett Wollman }
7422c37256eSGarrett Wollman 
7432c37256eSGarrett Wollman /*
7442c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
7452c37256eSGarrett Wollman  */
7462c37256eSGarrett Wollman static int
7472c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
7482c37256eSGarrett Wollman {
749f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
750623dce13SRobert Watson 	struct tcpcb *tp = NULL;
751623dce13SRobert Watson 	int error = 0;
7522c37256eSGarrett Wollman 
753623dce13SRobert Watson 	TCPDEBUG0;
754623dce13SRobert Watson 	inp = sotoinpcb(so);
755623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
756623dce13SRobert Watson 	INP_LOCK(inp);
757623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
75821367f63SSam Leffler 		error = ECONNRESET;
759623dce13SRobert Watson 		goto out;
760623dce13SRobert Watson 	}
761623dce13SRobert Watson 	tp = intotcpcb(inp);
762623dce13SRobert Watson 	TCPDEBUG1();
7632c37256eSGarrett Wollman 	tcp_output(tp);
764623dce13SRobert Watson 
765623dce13SRobert Watson out:
766623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
767623dce13SRobert Watson 	INP_UNLOCK(inp);
768623dce13SRobert Watson 	return (error);
7692c37256eSGarrett Wollman }
7702c37256eSGarrett Wollman 
7712c37256eSGarrett Wollman /*
7722c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
7739c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
7749c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
7759c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
7769c9906e9SPeter Wemm  * generally are caller-frees.
7772c37256eSGarrett Wollman  */
7782c37256eSGarrett Wollman static int
77957bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
780b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
7812c37256eSGarrett Wollman {
7822c37256eSGarrett Wollman 	int error = 0;
783f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
784623dce13SRobert Watson 	struct tcpcb *tp = NULL;
785623dce13SRobert Watson 	int headlocked = 0;
786fb59c426SYoshinobu Inoue #ifdef INET6
787fb59c426SYoshinobu Inoue 	int isipv6;
788fb59c426SYoshinobu Inoue #endif
7899c9906e9SPeter Wemm 	TCPDEBUG0;
7902c37256eSGarrett Wollman 
791f76fcf6dSJeffrey Hsu 	/*
792623dce13SRobert Watson 	 * We require the pcbinfo lock in two cases:
793623dce13SRobert Watson 	 *
794623dce13SRobert Watson 	 * (1) An implied connect is taking place, which can result in
795623dce13SRobert Watson 	 *     binding IPs and ports and hence modification of the pcb hash
796623dce13SRobert Watson 	 *     chains.
797623dce13SRobert Watson 	 *
798623dce13SRobert Watson 	 * (2) PRUS_EOF is set, resulting in explicit close on the send.
799f76fcf6dSJeffrey Hsu 	 */
800623dce13SRobert Watson 	if ((nam != NULL) || (flags & PRUS_EOF)) {
801f76fcf6dSJeffrey Hsu 		INP_INFO_WLOCK(&tcbinfo);
802623dce13SRobert Watson 		headlocked = 1;
803623dce13SRobert Watson 	}
804f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
805623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
806623dce13SRobert Watson 	INP_LOCK(inp);
807623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
8087ff0b850SAndre Oppermann 		if (control)
8097ff0b850SAndre Oppermann 			m_freem(control);
8107ff0b850SAndre Oppermann 		if (m)
8117ff0b850SAndre Oppermann 			m_freem(m);
81221367f63SSam Leffler 		error = ECONNRESET;
8139c9906e9SPeter Wemm 		goto out;
8149c9906e9SPeter Wemm 	}
815fb59c426SYoshinobu Inoue #ifdef INET6
816fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
817fb59c426SYoshinobu Inoue #endif /* INET6 */
8189c9906e9SPeter Wemm 	tp = intotcpcb(inp);
8199c9906e9SPeter Wemm 	TCPDEBUG1();
8209c9906e9SPeter Wemm 	if (control) {
8219c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
8229c9906e9SPeter Wemm 		if (control->m_len) {
8239c9906e9SPeter Wemm 			m_freem(control);
8242c37256eSGarrett Wollman 			if (m)
8252c37256eSGarrett Wollman 				m_freem(m);
826744f87eaSDavid Greenman 			error = EINVAL;
827744f87eaSDavid Greenman 			goto out;
8282c37256eSGarrett Wollman 		}
8299c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
8309c9906e9SPeter Wemm 	}
8312c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
832395bb186SSam Leffler 		sbappendstream(&so->so_snd, m);
8332c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
8342c37256eSGarrett Wollman 			/*
8352c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
8362c37256eSGarrett Wollman 			 * initialize window to default value, and
8372c37256eSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
8382c37256eSGarrett Wollman 			 * MSS.
8392c37256eSGarrett Wollman 			 */
840623dce13SRobert Watson 			INP_INFO_WLOCK_ASSERT(&tcbinfo);
841fb59c426SYoshinobu Inoue #ifdef INET6
842fb59c426SYoshinobu Inoue 			if (isipv6)
843b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
844fb59c426SYoshinobu Inoue 			else
845fb59c426SYoshinobu Inoue #endif /* INET6 */
846b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
8472c37256eSGarrett Wollman 			if (error)
8482c37256eSGarrett Wollman 				goto out;
8492c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
8502c37256eSGarrett Wollman 			tcp_mss(tp, -1);
8512c37256eSGarrett Wollman 		}
8522c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
8532c37256eSGarrett Wollman 			/*
8542c37256eSGarrett Wollman 			 * Close the send side of the connection after
8552c37256eSGarrett Wollman 			 * the data is sent.
8562c37256eSGarrett Wollman 			 */
857623dce13SRobert Watson 			INP_INFO_WLOCK_ASSERT(&tcbinfo);
8582c37256eSGarrett Wollman 			socantsendmore(so);
859623dce13SRobert Watson 			tcp_usrclosed(tp);
8602c37256eSGarrett Wollman 		}
861623dce13SRobert Watson 		if (headlocked) {
862d1401c90SRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
863623dce13SRobert Watson 			headlocked = 0;
864623dce13SRobert Watson 		}
865b0acefa8SBill Fenner 		if (tp != NULL) {
866b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
867b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
8682c37256eSGarrett Wollman 			error = tcp_output(tp);
869b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
870b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
871b0acefa8SBill Fenner 		}
8722c37256eSGarrett Wollman 	} else {
873623dce13SRobert Watson 		/*
874623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
875623dce13SRobert Watson 		 */
876d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
8772c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
878d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
8792c37256eSGarrett Wollman 			m_freem(m);
8802c37256eSGarrett Wollman 			error = ENOBUFS;
8812c37256eSGarrett Wollman 			goto out;
8822c37256eSGarrett Wollman 		}
8832c37256eSGarrett Wollman 		/*
8842c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
8852c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
8862c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
8872c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
8882c37256eSGarrett Wollman 		 * of data past the urgent section.
8892c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
8902c37256eSGarrett Wollman 		 */
891d2bc35abSRobert Watson 		sbappendstream_locked(&so->so_snd, m);
892d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
893ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
894ef53690bSGarrett Wollman 			/*
895ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
896ef53690bSGarrett Wollman 			 * initialize window to default value, and
897ef53690bSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
898ef53690bSGarrett Wollman 			 * MSS.
899ef53690bSGarrett Wollman 			 */
900623dce13SRobert Watson 			INP_INFO_WLOCK_ASSERT(&tcbinfo);
901fb59c426SYoshinobu Inoue #ifdef INET6
902fb59c426SYoshinobu Inoue 			if (isipv6)
903b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
904fb59c426SYoshinobu Inoue 			else
905fb59c426SYoshinobu Inoue #endif /* INET6 */
906b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
907ef53690bSGarrett Wollman 			if (error)
908ef53690bSGarrett Wollman 				goto out;
909ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
910ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
911d1401c90SRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
912623dce13SRobert Watson 			headlocked = 0;
913623dce13SRobert Watson 		} else if (nam) {
914623dce13SRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
915623dce13SRobert Watson 			headlocked = 0;
916623dce13SRobert Watson 		}
9172c37256eSGarrett Wollman 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
9182cdbfa66SPaul Saab 		tp->t_flags |= TF_FORCEDATA;
9192c37256eSGarrett Wollman 		error = tcp_output(tp);
9202cdbfa66SPaul Saab 		tp->t_flags &= ~TF_FORCEDATA;
9212c37256eSGarrett Wollman 	}
922d1401c90SRobert Watson out:
923d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
9242c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
925d1401c90SRobert Watson 	INP_UNLOCK(inp);
926623dce13SRobert Watson 	if (headlocked)
927d1401c90SRobert Watson 		INP_INFO_WUNLOCK(&tcbinfo);
92873fddedaSPeter Grehan 	return (error);
9292c37256eSGarrett Wollman }
9302c37256eSGarrett Wollman 
9312c37256eSGarrett Wollman /*
932a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
9332c37256eSGarrett Wollman  */
934ac45e92fSRobert Watson static void
9352c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
9362c37256eSGarrett Wollman {
937f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
938a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
939623dce13SRobert Watson 	TCPDEBUG0;
940c78cbc7bSRobert Watson 
941ac45e92fSRobert Watson 	inp = sotoinpcb(so);
942c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
943c78cbc7bSRobert Watson 
944c78cbc7bSRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
945ac45e92fSRobert Watson 	INP_LOCK(inp);
946c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
947c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
948c78cbc7bSRobert Watson 
949c78cbc7bSRobert Watson 	/*
950a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
951c78cbc7bSRobert Watson 	 */
952c78cbc7bSRobert Watson 	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
953c78cbc7bSRobert Watson 	    !(inp->inp_vflag & INP_DROPPED)) {
954c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
955a152f8a3SRobert Watson 		TCPDEBUG1();
956c78cbc7bSRobert Watson 		tcp_drop(tp, ECONNABORTED);
957a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
958c78cbc7bSRobert Watson 	}
959a152f8a3SRobert Watson 	if (!(inp->inp_vflag & INP_DROPPED)) {
960a152f8a3SRobert Watson 		SOCK_LOCK(so);
961a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
962a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
963a152f8a3SRobert Watson 		inp->inp_vflag |= INP_SOCKREF;
964a152f8a3SRobert Watson 	}
965a152f8a3SRobert Watson 	INP_UNLOCK(inp);
966a152f8a3SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
967a152f8a3SRobert Watson }
968a152f8a3SRobert Watson 
969a152f8a3SRobert Watson /*
970a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
971a152f8a3SRobert Watson  */
972a152f8a3SRobert Watson static void
973a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
974a152f8a3SRobert Watson {
975a152f8a3SRobert Watson 	struct inpcb *inp;
976a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
977a152f8a3SRobert Watson 	TCPDEBUG0;
978a152f8a3SRobert Watson 
979a152f8a3SRobert Watson 	inp = sotoinpcb(so);
980a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
981a152f8a3SRobert Watson 
982a152f8a3SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
983a152f8a3SRobert Watson 	INP_LOCK(inp);
984a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
985a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
986a152f8a3SRobert Watson 
987a152f8a3SRobert Watson 	/*
988a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
989a152f8a3SRobert Watson 	 * a disconnect.
990a152f8a3SRobert Watson 	 */
991a152f8a3SRobert Watson 	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
992a152f8a3SRobert Watson 	    !(inp->inp_vflag & INP_DROPPED)) {
993a152f8a3SRobert Watson 		tp = intotcpcb(inp);
994a152f8a3SRobert Watson 		TCPDEBUG1();
995a152f8a3SRobert Watson 		tcp_disconnect(tp);
996a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
997a152f8a3SRobert Watson 	}
998a152f8a3SRobert Watson 	if (!(inp->inp_vflag & INP_DROPPED)) {
999a152f8a3SRobert Watson 		SOCK_LOCK(so);
1000a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1001a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1002a152f8a3SRobert Watson 		inp->inp_vflag |= INP_SOCKREF;
1003a152f8a3SRobert Watson 	}
1004a152f8a3SRobert Watson 	INP_UNLOCK(inp);
1005ac45e92fSRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
10062c37256eSGarrett Wollman }
10072c37256eSGarrett Wollman 
10082c37256eSGarrett Wollman /*
10092c37256eSGarrett Wollman  * Receive out-of-band data.
10102c37256eSGarrett Wollman  */
10112c37256eSGarrett Wollman static int
10122c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
10132c37256eSGarrett Wollman {
10142c37256eSGarrett Wollman 	int error = 0;
1015f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1016623dce13SRobert Watson 	struct tcpcb *tp = NULL;
10172c37256eSGarrett Wollman 
1018623dce13SRobert Watson 	TCPDEBUG0;
1019623dce13SRobert Watson 	inp = sotoinpcb(so);
1020623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1021623dce13SRobert Watson 	INP_LOCK(inp);
1022623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
102321367f63SSam Leffler 		error = ECONNRESET;
1024623dce13SRobert Watson 		goto out;
1025623dce13SRobert Watson 	}
1026623dce13SRobert Watson 	tp = intotcpcb(inp);
1027623dce13SRobert Watson 	TCPDEBUG1();
10282c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1029c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
10304cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
10314cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
10322c37256eSGarrett Wollman 		error = EINVAL;
10332c37256eSGarrett Wollman 		goto out;
10342c37256eSGarrett Wollman 	}
10352c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
10362c37256eSGarrett Wollman 		error = EWOULDBLOCK;
10372c37256eSGarrett Wollman 		goto out;
10382c37256eSGarrett Wollman 	}
10392c37256eSGarrett Wollman 	m->m_len = 1;
10402c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
10412c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
10422c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1043623dce13SRobert Watson 
1044623dce13SRobert Watson out:
1045623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
1046623dce13SRobert Watson 	INP_UNLOCK(inp);
1047623dce13SRobert Watson 	return (error);
10482c37256eSGarrett Wollman }
10492c37256eSGarrett Wollman 
10502c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1051756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1052756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1053756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1054756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1055756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1056756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1057756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1058756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1059756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
1060756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		tcp_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 =		tcp_sockaddr,
1066a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1067a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
10682c37256eSGarrett Wollman };
1069df8bae1dSRodney W. Grimes 
1070fb59c426SYoshinobu Inoue #ifdef INET6
1071fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1072756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1073756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1074756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1075756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1076756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1077756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1078756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1079756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1080756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1081756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1082756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1083756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1084756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
1085756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1086756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1087a152f8a3SRobert Watson  	.pru_sosetlabel =	in_pcbsosetlabel,
1088a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1089fb59c426SYoshinobu Inoue };
1090fb59c426SYoshinobu Inoue #endif /* INET6 */
1091fb59c426SYoshinobu Inoue 
1092a0292f23SGarrett Wollman /*
1093a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1094a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
10955200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
10965200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
10975200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
10985200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1099a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1100a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1101a0292f23SGarrett Wollman  */
11020312fbe9SPoul-Henning Kamp static int
1103ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1104a0292f23SGarrett Wollman {
1105a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1106a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
11075200e00eSIan Dowse 	struct in_addr laddr;
11085200e00eSIan Dowse 	u_short lport;
1109c3229e05SDavid Greenman 	int error;
1110a0292f23SGarrett Wollman 
1111623dce13SRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1112623dce13SRobert Watson 	INP_LOCK_ASSERT(inp);
1113623dce13SRobert Watson 
1114a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
1115b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1116a0292f23SGarrett Wollman 		if (error)
1117a0292f23SGarrett Wollman 			return error;
1118a0292f23SGarrett Wollman 	}
1119a0292f23SGarrett Wollman 
1120a0292f23SGarrett Wollman 	/*
1121a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1122a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1123a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1124a0292f23SGarrett Wollman 	 */
11255200e00eSIan Dowse 	laddr = inp->inp_laddr;
11265200e00eSIan Dowse 	lport = inp->inp_lport;
11275200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1128b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
11295200e00eSIan Dowse 	if (error && oinp == NULL)
1130d3628763SRodney W. Grimes 		return error;
1131c94c54e4SAndre Oppermann 	if (oinp)
1132a0292f23SGarrett Wollman 		return EADDRINUSE;
11335200e00eSIan Dowse 	inp->inp_laddr = laddr;
113415bd2b43SDavid Greenman 	in_pcbrehash(inp);
1135a0292f23SGarrett Wollman 
1136087b55eaSAndre Oppermann 	/*
1137087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1138087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1139087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1140087b55eaSAndre Oppermann 	 * XXX: This should be based on the actual MSS.
1141087b55eaSAndre Oppermann 	 */
1142a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1143087b55eaSAndre Oppermann 	    (0x1 << tp->request_r_scale) < tcp_minmss)
1144a0292f23SGarrett Wollman 		tp->request_r_scale++;
1145a0292f23SGarrett Wollman 
1146a0292f23SGarrett Wollman 	soisconnecting(so);
1147a0292f23SGarrett Wollman 	tcpstat.tcps_connattempt++;
1148a0292f23SGarrett Wollman 	tp->t_state = TCPS_SYN_SENT;
1149b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1150b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
11511fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
1152a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1153a45d2726SAndras Olah 
1154a0292f23SGarrett Wollman 	return 0;
1155a0292f23SGarrett Wollman }
1156a0292f23SGarrett Wollman 
1157fb59c426SYoshinobu Inoue #ifdef INET6
1158fb59c426SYoshinobu Inoue static int
1159ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1160fb59c426SYoshinobu Inoue {
1161fb59c426SYoshinobu Inoue 	struct inpcb *inp = tp->t_inpcb, *oinp;
1162fb59c426SYoshinobu Inoue 	struct socket *so = inp->inp_socket;
1163fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1164fb59c426SYoshinobu Inoue 	struct in6_addr *addr6;
1165fb59c426SYoshinobu Inoue 	int error;
1166fb59c426SYoshinobu Inoue 
1167623dce13SRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1168623dce13SRobert Watson 	INP_LOCK_ASSERT(inp);
1169623dce13SRobert Watson 
1170fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
1171b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1172fb59c426SYoshinobu Inoue 		if (error)
1173fb59c426SYoshinobu Inoue 			return error;
1174fb59c426SYoshinobu Inoue 	}
1175fb59c426SYoshinobu Inoue 
1176fb59c426SYoshinobu Inoue 	/*
1177fb59c426SYoshinobu Inoue 	 * Cannot simply call in_pcbconnect, because there might be an
1178fb59c426SYoshinobu Inoue 	 * earlier incarnation of this same connection still in
1179fb59c426SYoshinobu Inoue 	 * TIME_WAIT state, creating an ADDRINUSE error.
1180a1f7e5f8SHajimu UMEMOTO 	 * in6_pcbladdr() also handles scope zone IDs.
1181fb59c426SYoshinobu Inoue 	 */
1182fb59c426SYoshinobu Inoue 	error = in6_pcbladdr(inp, nam, &addr6);
1183fb59c426SYoshinobu Inoue 	if (error)
1184fb59c426SYoshinobu Inoue 		return error;
1185fb59c426SYoshinobu Inoue 	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1186fb59c426SYoshinobu Inoue 				  &sin6->sin6_addr, sin6->sin6_port,
1187fb59c426SYoshinobu Inoue 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1188fb59c426SYoshinobu Inoue 				  ? addr6
1189fb59c426SYoshinobu Inoue 				  : &inp->in6p_laddr,
1190fb59c426SYoshinobu Inoue 				  inp->inp_lport,  0, NULL);
1191c94c54e4SAndre Oppermann 	if (oinp)
1192fb59c426SYoshinobu Inoue 		return EADDRINUSE;
1193fb59c426SYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1194fb59c426SYoshinobu Inoue 		inp->in6p_laddr = *addr6;
1195fb59c426SYoshinobu Inoue 	inp->in6p_faddr = sin6->sin6_addr;
1196fb59c426SYoshinobu Inoue 	inp->inp_fport = sin6->sin6_port;
11978a59da30SHajimu UMEMOTO 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
11988a59da30SHajimu UMEMOTO 	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
11998a59da30SHajimu UMEMOTO 	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
12008a59da30SHajimu UMEMOTO 		inp->in6p_flowinfo |=
12018a59da30SHajimu UMEMOTO 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1202fb59c426SYoshinobu Inoue 	in_pcbrehash(inp);
1203fb59c426SYoshinobu Inoue 
1204fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1205fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1206fb59c426SYoshinobu Inoue 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1207fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1208fb59c426SYoshinobu Inoue 
1209fb59c426SYoshinobu Inoue 	soisconnecting(so);
1210fb59c426SYoshinobu Inoue 	tcpstat.tcps_connattempt++;
1211fb59c426SYoshinobu Inoue 	tp->t_state = TCPS_SYN_SENT;
1212b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1213b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
12141fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
1215fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1216fb59c426SYoshinobu Inoue 
1217fb59c426SYoshinobu Inoue 	return 0;
1218fb59c426SYoshinobu Inoue }
1219fb59c426SYoshinobu Inoue #endif /* INET6 */
1220fb59c426SYoshinobu Inoue 
1221cfe8b629SGarrett Wollman /*
1222b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1223b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1224b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1225b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1226b8af5dfaSRobert Watson  * from Linux.
1227b8af5dfaSRobert Watson  */
1228b8af5dfaSRobert Watson static void
1229ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1230b8af5dfaSRobert Watson {
1231b8af5dfaSRobert Watson 
1232b8af5dfaSRobert Watson 	INP_LOCK_ASSERT(tp->t_inpcb);
1233b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1234b8af5dfaSRobert Watson 
1235b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1236b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1237b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1238b8af5dfaSRobert Watson 	if (tp->sack_enable)
1239b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1240b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1241b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1242b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1243b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1244b8af5dfaSRobert Watson 	}
12451baaf834SBruce M Simpson 
12461baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
12471baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
12481baaf834SBruce M Simpson 
1249b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1250b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1251b8af5dfaSRobert Watson 
1252b8af5dfaSRobert Watson 	/*
1253b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1254b8af5dfaSRobert Watson 	 */
1255c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1256b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
1257b8af5dfaSRobert Watson 	ti->tcpi_snd_bwnd = tp->snd_bwnd;
1258b8af5dfaSRobert Watson }
1259b8af5dfaSRobert Watson 
1260b8af5dfaSRobert Watson /*
1261cfe8b629SGarrett Wollman  * The new sockopt interface makes it possible for us to block in the
1262cfe8b629SGarrett Wollman  * copyin/out step (if we take a page fault).  Taking a page fault at
1263cfe8b629SGarrett Wollman  * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
1264cfe8b629SGarrett Wollman  * use TSM, there probably isn't any need for this function to run at
1265cfe8b629SGarrett Wollman  * splnet() any more.  This needs more examination.)
1266b8af5dfaSRobert Watson  *
1267b8af5dfaSRobert Watson  * XXXRW: The locking here is wrong; we may take a page fault while holding
1268b8af5dfaSRobert Watson  * the inpcb lock.
1269cfe8b629SGarrett Wollman  */
1270df8bae1dSRodney W. Grimes int
1271ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1272df8bae1dSRodney W. Grimes {
12733f9d1ef9SRobert Watson 	int	error, opt, optval;
1274df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1275cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
1276b8af5dfaSRobert Watson 	struct	tcp_info ti;
1277df8bae1dSRodney W. Grimes 
1278cfe8b629SGarrett Wollman 	error = 0;
1279df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1280623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1281f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1282cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
12834e397bc5SRobert Watson 		INP_UNLOCK(inp);
1284fb59c426SYoshinobu Inoue #ifdef INET6
1285fb59c426SYoshinobu Inoue 		if (INP_CHECK_SOCKAF(so, AF_INET6))
1286fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
1287fb59c426SYoshinobu Inoue 		else
1288fb59c426SYoshinobu Inoue #endif /* INET6 */
1289cfe8b629SGarrett Wollman 		error = ip_ctloutput(so, sopt);
1290df8bae1dSRodney W. Grimes 		return (error);
1291df8bae1dSRodney W. Grimes 	}
1292623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
1293623dce13SRobert Watson 		error = ECONNRESET;
1294623dce13SRobert Watson 		goto out;
1295623dce13SRobert Watson 	}
1296df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
1297df8bae1dSRodney W. Grimes 
1298cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1299cfe8b629SGarrett Wollman 	case SOPT_SET:
1300cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
13011cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
130288f6b043SBruce M Simpson 		case TCP_MD5SIG:
13031cfd4b53SBruce M Simpson 			error = sooptcopyin(sopt, &optval, sizeof optval,
13041cfd4b53SBruce M Simpson 					    sizeof optval);
13051cfd4b53SBruce M Simpson 			if (error)
13061cfd4b53SBruce M Simpson 				break;
13071cfd4b53SBruce M Simpson 
13081cfd4b53SBruce M Simpson 			if (optval > 0)
13091cfd4b53SBruce M Simpson 				tp->t_flags |= TF_SIGNATURE;
13101cfd4b53SBruce M Simpson 			else
13111cfd4b53SBruce M Simpson 				tp->t_flags &= ~TF_SIGNATURE;
13121cfd4b53SBruce M Simpson 			break;
13131cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
1314df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1315cfe8b629SGarrett Wollman 		case TCP_NOOPT:
1316cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1317cfe8b629SGarrett Wollman 					    sizeof optval);
1318cfe8b629SGarrett Wollman 			if (error)
1319cfe8b629SGarrett Wollman 				break;
1320cfe8b629SGarrett Wollman 
1321cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1322cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1323cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1324cfe8b629SGarrett Wollman 				break;
1325cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1326cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1327cfe8b629SGarrett Wollman 				break;
1328cfe8b629SGarrett Wollman 			default:
1329cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1330cfe8b629SGarrett Wollman 				break;
1331cfe8b629SGarrett Wollman 			}
1332cfe8b629SGarrett Wollman 
1333cfe8b629SGarrett Wollman 			if (optval)
1334cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1335df8bae1dSRodney W. Grimes 			else
1336cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
1337df8bae1dSRodney W. Grimes 			break;
1338df8bae1dSRodney W. Grimes 
1339007581c0SJonathan Lemon 		case TCP_NOPUSH:
1340007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1341007581c0SJonathan Lemon 					    sizeof optval);
1342007581c0SJonathan Lemon 			if (error)
1343007581c0SJonathan Lemon 				break;
1344007581c0SJonathan Lemon 
1345007581c0SJonathan Lemon 			if (optval)
1346007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1347007581c0SJonathan Lemon 			else {
1348007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1349007581c0SJonathan Lemon 				error = tcp_output(tp);
1350007581c0SJonathan Lemon 			}
1351007581c0SJonathan Lemon 			break;
1352007581c0SJonathan Lemon 
1353df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1354cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1355cfe8b629SGarrett Wollman 					    sizeof optval);
1356cfe8b629SGarrett Wollman 			if (error)
1357df8bae1dSRodney W. Grimes 				break;
1358df8bae1dSRodney W. Grimes 
135953369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
136053369ac9SAndre Oppermann 			    optval + 40 >= tcp_minmss)
1361cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1362a0292f23SGarrett Wollman 			else
1363a0292f23SGarrett Wollman 				error = EINVAL;
1364a0292f23SGarrett Wollman 			break;
1365a0292f23SGarrett Wollman 
1366b8af5dfaSRobert Watson 		case TCP_INFO:
1367b8af5dfaSRobert Watson 			error = EINVAL;
1368b8af5dfaSRobert Watson 			break;
1369b8af5dfaSRobert Watson 
1370df8bae1dSRodney W. Grimes 		default:
1371df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1372df8bae1dSRodney W. Grimes 			break;
1373df8bae1dSRodney W. Grimes 		}
1374df8bae1dSRodney W. Grimes 		break;
1375df8bae1dSRodney W. Grimes 
1376cfe8b629SGarrett Wollman 	case SOPT_GET:
1377cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
13781cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
137988f6b043SBruce M Simpson 		case TCP_MD5SIG:
13801cfd4b53SBruce M Simpson 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1381b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
13821cfd4b53SBruce M Simpson 			break;
1383265ed012SBruce M Simpson #endif
1384df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1385cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
1386b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1387df8bae1dSRodney W. Grimes 			break;
1388df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1389cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
1390b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1391df8bae1dSRodney W. Grimes 			break;
1392a0292f23SGarrett Wollman 		case TCP_NOOPT:
1393cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
1394b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1395a0292f23SGarrett Wollman 			break;
1396a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1397cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
1398b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1399b8af5dfaSRobert Watson 			break;
1400b8af5dfaSRobert Watson 		case TCP_INFO:
1401b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
1402b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
1403a0292f23SGarrett Wollman 			break;
1404df8bae1dSRodney W. Grimes 		default:
1405df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1406df8bae1dSRodney W. Grimes 			break;
1407df8bae1dSRodney W. Grimes 		}
1408df8bae1dSRodney W. Grimes 		break;
1409df8bae1dSRodney W. Grimes 	}
1410623dce13SRobert Watson out:
1411f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1412df8bae1dSRodney W. Grimes 	return (error);
1413df8bae1dSRodney W. Grimes }
1414df8bae1dSRodney W. Grimes 
141526e30fbbSDavid Greenman /*
141626e30fbbSDavid Greenman  * tcp_sendspace and tcp_recvspace are the default send and receive window
141726e30fbbSDavid Greenman  * sizes, respectively.  These are obsolescent (this information should
141826e30fbbSDavid Greenman  * be set by the route).
141926e30fbbSDavid Greenman  */
142081e561cdSDavid E. O'Brien u_long	tcp_sendspace = 1024*32;
1421e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
14223d177f46SBill Fumerola     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
142381e561cdSDavid E. O'Brien u_long	tcp_recvspace = 1024*64;
1424e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
14253d177f46SBill Fumerola     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1426df8bae1dSRodney W. Grimes 
1427df8bae1dSRodney W. Grimes /*
1428df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1429df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1430df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1431df8bae1dSRodney W. Grimes  */
14320312fbe9SPoul-Henning Kamp static int
1433ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so)
1434df8bae1dSRodney W. Grimes {
1435ad3f9ab3SAndre Oppermann 	struct tcpcb *tp;
1436df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1437df8bae1dSRodney W. Grimes 	int error;
1438fb59c426SYoshinobu Inoue #ifdef INET6
14394a6a94d8SArchie Cobbs 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1440fb59c426SYoshinobu Inoue #endif
1441df8bae1dSRodney W. Grimes 
1442df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1443df8bae1dSRodney W. Grimes 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1444df8bae1dSRodney W. Grimes 		if (error)
1445df8bae1dSRodney W. Grimes 			return (error);
1446df8bae1dSRodney W. Grimes 	}
14476741ecf5SAndre Oppermann 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
14486741ecf5SAndre Oppermann 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1449f2de87feSRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
1450d915b280SStephan Uphoff 	error = in_pcballoc(so, &tcbinfo);
1451f2de87feSRobert Watson 	if (error) {
1452f2de87feSRobert Watson 		INP_INFO_WUNLOCK(&tcbinfo);
1453df8bae1dSRodney W. Grimes 		return (error);
1454f2de87feSRobert Watson 	}
1455df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1456fb59c426SYoshinobu Inoue #ifdef INET6
1457fb59c426SYoshinobu Inoue 	if (isipv6) {
1458fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1459fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1460fb59c426SYoshinobu Inoue 	}
1461fb59c426SYoshinobu Inoue 	else
1462fb59c426SYoshinobu Inoue #endif
1463cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1464df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1465623dce13SRobert Watson 	if (tp == NULL) {
1466fb59c426SYoshinobu Inoue #ifdef INET6
1467623dce13SRobert Watson 		if (isipv6) {
1468fb59c426SYoshinobu Inoue 			in6_pcbdetach(inp);
1469623dce13SRobert Watson 			in6_pcbfree(inp);
1470623dce13SRobert Watson 		} else {
1471fb59c426SYoshinobu Inoue #endif
1472df8bae1dSRodney W. Grimes 			in_pcbdetach(inp);
1473623dce13SRobert Watson 			in_pcbfree(inp);
1474623dce13SRobert Watson #ifdef INET6
1475623dce13SRobert Watson 		}
1476623dce13SRobert Watson #endif
1477f2de87feSRobert Watson 		INP_INFO_WUNLOCK(&tcbinfo);
1478df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1479df8bae1dSRodney W. Grimes 	}
1480df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
1481f2de87feSRobert Watson 	INP_UNLOCK(inp);
1482f2de87feSRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
1483df8bae1dSRodney W. Grimes 	return (0);
1484df8bae1dSRodney W. Grimes }
1485df8bae1dSRodney W. Grimes 
1486df8bae1dSRodney W. Grimes /*
1487df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1488df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1489df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1490df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1491df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1492df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1493df8bae1dSRodney W. Grimes  */
1494623dce13SRobert Watson static void
1495ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
1496df8bae1dSRodney W. Grimes {
1497e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
1498e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
1499e6e0b5ffSRobert Watson 
1500e6e0b5ffSRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1501e6e0b5ffSRobert Watson 	INP_LOCK_ASSERT(inp);
1502df8bae1dSRodney W. Grimes 
1503623dce13SRobert Watson 	/*
1504623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1505623dce13SRobert Watson 	 * socket is still open.
1506623dce13SRobert Watson 	 */
1507623dce13SRobert Watson 	if (tp->t_state < TCPS_ESTABLISHED) {
1508df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1509623dce13SRobert Watson 		KASSERT(tp != NULL,
1510623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
1511623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1512243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
1513623dce13SRobert Watson 		KASSERT(tp != NULL,
1514623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
1515623dce13SRobert Watson 	} else {
1516df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
1517df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
1518623dce13SRobert Watson 		tcp_usrclosed(tp);
1519953b5606SRobert Watson 		if (!(inp->inp_vflag & INP_DROPPED))
1520623dce13SRobert Watson 			tcp_output(tp);
1521df8bae1dSRodney W. Grimes 	}
1522df8bae1dSRodney W. Grimes }
1523df8bae1dSRodney W. Grimes 
1524df8bae1dSRodney W. Grimes /*
1525df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1526df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1527df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1528df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1529df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1530df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1531df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1532df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1533df8bae1dSRodney W. Grimes  */
1534623dce13SRobert Watson static void
1535ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
1536df8bae1dSRodney W. Grimes {
1537df8bae1dSRodney W. Grimes 
1538e6e0b5ffSRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1539e6e0b5ffSRobert Watson 	INP_LOCK_ASSERT(tp->t_inpcb);
1540e6e0b5ffSRobert Watson 
1541df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1542df8bae1dSRodney W. Grimes 
1543df8bae1dSRodney W. Grimes 	case TCPS_CLOSED:
1544df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
1545df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_CLOSED;
1546df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1547623dce13SRobert Watson 		/*
1548623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
1549623dce13SRobert Watson 		 * still open.
1550623dce13SRobert Watson 		 */
1551623dce13SRobert Watson 		KASSERT(tp != NULL,
1552623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
1553df8bae1dSRodney W. Grimes 		break;
1554df8bae1dSRodney W. Grimes 
1555a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
1556df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1557a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
1558a0292f23SGarrett Wollman 		break;
1559a0292f23SGarrett Wollman 
1560df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1561df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_FIN_WAIT_1;
1562df8bae1dSRodney W. Grimes 		break;
1563df8bae1dSRodney W. Grimes 
1564df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1565df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_LAST_ACK;
1566df8bae1dSRodney W. Grimes 		break;
1567df8bae1dSRodney W. Grimes 	}
1568b6239c4aSAndras Olah 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1569df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
1570b6239c4aSAndras Olah 		/* To prevent the connection hanging in FIN_WAIT_2 forever. */
15717c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
15727c72af87SMohan Srinivasan 			int timeout;
15737c72af87SMohan Srinivasan 
15747c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
15757c72af87SMohan Srinivasan 				tcp_finwait2_timeout : tcp_maxidle;
1576b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
1577b6239c4aSAndras Olah 		}
1578df8bae1dSRodney W. Grimes 	}
15797c72af87SMohan Srinivasan }
1580497057eeSRobert Watson 
1581497057eeSRobert Watson #ifdef DDB
1582497057eeSRobert Watson static void
1583497057eeSRobert Watson db_print_indent(int indent)
1584497057eeSRobert Watson {
1585497057eeSRobert Watson 	int i;
1586497057eeSRobert Watson 
1587497057eeSRobert Watson 	for (i = 0; i < indent; i++)
1588497057eeSRobert Watson 		db_printf(" ");
1589497057eeSRobert Watson }
1590497057eeSRobert Watson 
1591497057eeSRobert Watson static void
1592497057eeSRobert Watson db_print_tstate(int t_state)
1593497057eeSRobert Watson {
1594497057eeSRobert Watson 
1595497057eeSRobert Watson 	switch (t_state) {
1596497057eeSRobert Watson 	case TCPS_CLOSED:
1597497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
1598497057eeSRobert Watson 		return;
1599497057eeSRobert Watson 
1600497057eeSRobert Watson 	case TCPS_LISTEN:
1601497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
1602497057eeSRobert Watson 		return;
1603497057eeSRobert Watson 
1604497057eeSRobert Watson 	case TCPS_SYN_SENT:
1605497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
1606497057eeSRobert Watson 		return;
1607497057eeSRobert Watson 
1608497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
1609497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
1610497057eeSRobert Watson 		return;
1611497057eeSRobert Watson 
1612497057eeSRobert Watson 	case TCPS_ESTABLISHED:
1613497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
1614497057eeSRobert Watson 		return;
1615497057eeSRobert Watson 
1616497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
1617497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
1618497057eeSRobert Watson 		return;
1619497057eeSRobert Watson 
1620497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
1621497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
1622497057eeSRobert Watson 		return;
1623497057eeSRobert Watson 
1624497057eeSRobert Watson 	case TCPS_CLOSING:
1625497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
1626497057eeSRobert Watson 		return;
1627497057eeSRobert Watson 
1628497057eeSRobert Watson 	case TCPS_LAST_ACK:
1629497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
1630497057eeSRobert Watson 		return;
1631497057eeSRobert Watson 
1632497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
1633497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
1634497057eeSRobert Watson 		return;
1635497057eeSRobert Watson 
1636497057eeSRobert Watson 	case TCPS_TIME_WAIT:
1637497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
1638497057eeSRobert Watson 		return;
1639497057eeSRobert Watson 
1640497057eeSRobert Watson 	default:
1641497057eeSRobert Watson 		db_printf("unknown");
1642497057eeSRobert Watson 		return;
1643497057eeSRobert Watson 	}
1644497057eeSRobert Watson }
1645497057eeSRobert Watson 
1646497057eeSRobert Watson static void
1647497057eeSRobert Watson db_print_tflags(u_int t_flags)
1648497057eeSRobert Watson {
1649497057eeSRobert Watson 	int comma;
1650497057eeSRobert Watson 
1651497057eeSRobert Watson 	comma = 0;
1652497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
1653497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
1654497057eeSRobert Watson 		comma = 1;
1655497057eeSRobert Watson 	}
1656497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
1657497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
1658497057eeSRobert Watson 		comma = 1;
1659497057eeSRobert Watson 	}
1660497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
1661497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
1662497057eeSRobert Watson 		comma = 1;
1663497057eeSRobert Watson 	}
1664497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
1665497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
1666497057eeSRobert Watson 		comma = 1;
1667497057eeSRobert Watson 	}
1668497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
1669497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
1670497057eeSRobert Watson 		comma = 1;
1671497057eeSRobert Watson 	}
1672497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
1673497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1674497057eeSRobert Watson 		comma = 1;
1675497057eeSRobert Watson 	}
1676497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
1677497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1678497057eeSRobert Watson 		comma = 1;
1679497057eeSRobert Watson 	}
1680497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
1681497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1682497057eeSRobert Watson 		comma = 1;
1683497057eeSRobert Watson 	}
1684497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
1685497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1686497057eeSRobert Watson 		comma = 1;
1687497057eeSRobert Watson 	}
1688497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
1689497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1690497057eeSRobert Watson 		comma = 1;
1691497057eeSRobert Watson 	}
1692497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
1693497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1694497057eeSRobert Watson 		comma = 1;
1695497057eeSRobert Watson 	}
1696497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
1697497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1698497057eeSRobert Watson 		comma = 1;
1699497057eeSRobert Watson 	}
1700497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
1701497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
1702497057eeSRobert Watson 		comma = 1;
1703497057eeSRobert Watson 	}
1704497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
1705497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
1706497057eeSRobert Watson 		comma = 1;
1707497057eeSRobert Watson 	}
1708497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
1709497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1710497057eeSRobert Watson 		comma = 1;
1711497057eeSRobert Watson 	}
1712497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
1713497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1714497057eeSRobert Watson 		comma = 1;
1715497057eeSRobert Watson 	}
1716497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
1717497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1718497057eeSRobert Watson 		comma = 1;
1719497057eeSRobert Watson 	}
1720497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
1721497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1722497057eeSRobert Watson 		comma = 1;
1723497057eeSRobert Watson 	}
1724497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
1725497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1726497057eeSRobert Watson 		comma = 1;
1727497057eeSRobert Watson 	}
1728497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
1729497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1730497057eeSRobert Watson 		comma = 1;
1731497057eeSRobert Watson 	}
1732497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
1733497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1734497057eeSRobert Watson 		comma = 1;
1735497057eeSRobert Watson 	}
1736497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
1737497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1738497057eeSRobert Watson 		comma = 1;
1739497057eeSRobert Watson 	}
1740497057eeSRobert Watson 	if (t_flags & TF_TSO) {
1741497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
1742497057eeSRobert Watson 		comma = 1;
1743497057eeSRobert Watson 	}
1744497057eeSRobert Watson }
1745497057eeSRobert Watson 
1746497057eeSRobert Watson static void
1747497057eeSRobert Watson db_print_toobflags(char t_oobflags)
1748497057eeSRobert Watson {
1749497057eeSRobert Watson 	int comma;
1750497057eeSRobert Watson 
1751497057eeSRobert Watson 	comma = 0;
1752497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
1753497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1754497057eeSRobert Watson 		comma = 1;
1755497057eeSRobert Watson 	}
1756497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
1757497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1758497057eeSRobert Watson 		comma = 1;
1759497057eeSRobert Watson 	}
1760497057eeSRobert Watson }
1761497057eeSRobert Watson 
1762497057eeSRobert Watson static void
1763497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1764497057eeSRobert Watson {
1765497057eeSRobert Watson 
1766497057eeSRobert Watson 	db_print_indent(indent);
1767497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
1768497057eeSRobert Watson 
1769497057eeSRobert Watson 	indent += 2;
1770497057eeSRobert Watson 
1771497057eeSRobert Watson 	db_print_indent(indent);
1772497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
1773497057eeSRobert Watson 	   LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
1774497057eeSRobert Watson 
1775497057eeSRobert Watson 	db_print_indent(indent);
1776b8152ba7SAndre Oppermann 	db_printf("t_inpcb: %p   t_timers: %p   tt_active: %x\n",
1777b8152ba7SAndre Oppermann 	    tp->t_inpcb, tp->t_timers, tp->t_timers->tt_active);
1778497057eeSRobert Watson 
1779497057eeSRobert Watson 	db_print_indent(indent);
1780b8152ba7SAndre Oppermann 	db_printf("tt_delack: %i   tt_rexmt: %i   tt_keep: %i   "
1781b8152ba7SAndre Oppermann 	    "tt_persist: %i   tt_2msl: %i\n",
1782b8152ba7SAndre Oppermann 	    tp->t_timers->tt_delack, tp->t_timers->tt_rexmt,
1783b8152ba7SAndre Oppermann 	    tp->t_timers->tt_keep, tp->t_timers->tt_persist,
1784b8152ba7SAndre Oppermann 	    tp->t_timers->tt_2msl);
1785497057eeSRobert Watson 
1786497057eeSRobert Watson 	db_print_indent(indent);
1787497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
1788497057eeSRobert Watson 	db_print_tstate(tp->t_state);
1789497057eeSRobert Watson 	db_printf(")\n");
1790497057eeSRobert Watson 
1791497057eeSRobert Watson 	db_print_indent(indent);
1792497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
1793497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
1794497057eeSRobert Watson 	db_printf(")\n");
1795497057eeSRobert Watson 
1796497057eeSRobert Watson 	db_print_indent(indent);
1797497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
1798497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
1799497057eeSRobert Watson 
1800497057eeSRobert Watson 	db_print_indent(indent);
1801497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
1802497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
1803497057eeSRobert Watson 
1804497057eeSRobert Watson 	db_print_indent(indent);
1805497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
1806497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
1807497057eeSRobert Watson 
1808497057eeSRobert Watson 	db_print_indent(indent);
1809497057eeSRobert Watson 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %lu   rcv_up: 0x%08x\n",
1810497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
1811497057eeSRobert Watson 
1812497057eeSRobert Watson 	db_print_indent(indent);
1813497057eeSRobert Watson 	db_printf("snd_wnd: %lu   snd_cwnd: %lu   snd_bwnd: %lu\n",
1814497057eeSRobert Watson 	   tp->snd_wnd, tp->snd_cwnd, tp->snd_bwnd);
1815497057eeSRobert Watson 
1816497057eeSRobert Watson 	db_print_indent(indent);
1817497057eeSRobert Watson 	db_printf("snd_ssthresh: %lu   snd_bandwidth: %lu   snd_recover: "
1818497057eeSRobert Watson 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_bandwidth,
1819497057eeSRobert Watson 	    tp->snd_recover);
1820497057eeSRobert Watson 
1821497057eeSRobert Watson 	db_print_indent(indent);
1822497057eeSRobert Watson 	db_printf("t_maxopd: %u   t_rcvtime: %lu   t_startime: %lu\n",
1823497057eeSRobert Watson 	    tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
1824497057eeSRobert Watson 
1825497057eeSRobert Watson 	db_print_indent(indent);
1826497057eeSRobert Watson 	db_printf("t_rttime: %d   t_rtsq: 0x%08x   t_bw_rtttime: %d\n",
1827497057eeSRobert Watson 	    tp->t_rtttime, tp->t_rtseq, tp->t_bw_rtttime);
1828497057eeSRobert Watson 
1829497057eeSRobert Watson 	db_print_indent(indent);
1830497057eeSRobert Watson 	db_printf("t_bw_rtseq: 0x%08x   t_rxtcur: %d   t_maxseg: %u   "
1831497057eeSRobert Watson 	    "t_srtt: %d\n", tp->t_bw_rtseq, tp->t_rxtcur, tp->t_maxseg,
1832497057eeSRobert Watson 	    tp->t_srtt);
1833497057eeSRobert Watson 
1834497057eeSRobert Watson 	db_print_indent(indent);
1835497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
1836497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
1837497057eeSRobert Watson 	    tp->t_rttbest);
1838497057eeSRobert Watson 
1839497057eeSRobert Watson 	db_print_indent(indent);
1840497057eeSRobert Watson 	db_printf("t_rttupdated: %lu   max_sndwnd: %lu   t_softerror: %d\n",
1841497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
1842497057eeSRobert Watson 
1843497057eeSRobert Watson 	db_print_indent(indent);
1844497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
1845497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
1846497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
1847497057eeSRobert Watson 
1848497057eeSRobert Watson 	db_print_indent(indent);
1849497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
1850497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
1851497057eeSRobert Watson 
1852497057eeSRobert Watson 	db_print_indent(indent);
1853497057eeSRobert Watson 	db_printf("requested_s_scale: %u   ts_recent: %u   ts_recent_age: "
1854497057eeSRobert Watson 	    "%lu\n", tp->requested_s_scale, tp->ts_recent, tp->ts_recent_age);
1855497057eeSRobert Watson 
1856497057eeSRobert Watson 	db_print_indent(indent);
1857497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
1858497057eeSRobert Watson 	    "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
1859497057eeSRobert Watson 
1860497057eeSRobert Watson 	db_print_indent(indent);
1861497057eeSRobert Watson 	db_printf("snd_ssthresh_prev: %lu   snd_recover_prev: 0x%08x   "
1862497057eeSRobert Watson 	    "t_badrxtwin: %lu\n", tp->snd_ssthresh_prev,
1863497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
1864497057eeSRobert Watson 
1865497057eeSRobert Watson 	db_print_indent(indent);
1866497057eeSRobert Watson 	db_printf("sack_enable: %d   snd_numholes: %d  snd_holes first: %p\n",
1867497057eeSRobert Watson 	    tp->sack_enable, tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
1868497057eeSRobert Watson 
1869497057eeSRobert Watson 	db_print_indent(indent);
1870497057eeSRobert Watson 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
1871497057eeSRobert Watson 	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
1872497057eeSRobert Watson 
1873497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
1874497057eeSRobert Watson 
1875497057eeSRobert Watson 	db_print_indent(indent);
1876497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
1877497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
1878497057eeSRobert Watson }
1879497057eeSRobert Watson 
1880497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
1881497057eeSRobert Watson {
1882497057eeSRobert Watson 	struct tcpcb *tp;
1883497057eeSRobert Watson 
1884497057eeSRobert Watson 	if (!have_addr) {
1885497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
1886497057eeSRobert Watson 		return;
1887497057eeSRobert Watson 	}
1888497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
1889497057eeSRobert Watson 
1890497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
1891497057eeSRobert Watson }
1892497057eeSRobert Watson #endif
1893