xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 9b3bc6bf838187e25f95ac7efc9d1a3f063f9ba8)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
3623dce13SRobert Watson  *	The Regents of the University of California.
4497057eeSRobert Watson  * Copyright (c) 2006-2007 Robert N. M. Watson
5623dce13SRobert Watson  * All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
311fdbc7aeSGarrett Wollman  *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
32df8bae1dSRodney W. Grimes  */
33df8bae1dSRodney W. Grimes 
344b421e2dSMike Silbersack #include <sys/cdefs.h>
354b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
364b421e2dSMike Silbersack 
37497057eeSRobert Watson #include "opt_ddb.h"
381cfd4b53SBruce M Simpson #include "opt_inet.h"
39fb59c426SYoshinobu Inoue #include "opt_inet6.h"
400cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
410cc12cc5SJoerg Wunsch 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43df8bae1dSRodney W. Grimes #include <sys/systm.h>
44f76fcf6dSJeffrey Hsu #include <sys/malloc.h>
45c7a82f90SGarrett Wollman #include <sys/kernel.h>
4698163b98SPoul-Henning Kamp #include <sys/sysctl.h>
47df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
48fb59c426SYoshinobu Inoue #ifdef INET6
49fb59c426SYoshinobu Inoue #include <sys/domain.h>
50fb59c426SYoshinobu Inoue #endif /* INET6 */
51df8bae1dSRodney W. Grimes #include <sys/socket.h>
52df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
53df8bae1dSRodney W. Grimes #include <sys/protosw.h>
5491421ba2SRobert Watson #include <sys/proc.h>
5591421ba2SRobert Watson #include <sys/jail.h>
56df8bae1dSRodney W. Grimes 
57497057eeSRobert Watson #ifdef DDB
58497057eeSRobert Watson #include <ddb/ddb.h>
59497057eeSRobert Watson #endif
60497057eeSRobert Watson 
61df8bae1dSRodney W. Grimes #include <net/if.h>
62df8bae1dSRodney W. Grimes #include <net/route.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <netinet/in.h>
65df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
66fb59c426SYoshinobu Inoue #ifdef INET6
67fb59c426SYoshinobu Inoue #include <netinet/ip6.h>
68fb59c426SYoshinobu Inoue #endif
69df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
70fb59c426SYoshinobu Inoue #ifdef INET6
71fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h>
72fb59c426SYoshinobu Inoue #endif
73b5e8ce9fSBruce Evans #include <netinet/in_var.h>
74df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
75fb59c426SYoshinobu Inoue #ifdef INET6
76fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
77a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
78fb59c426SYoshinobu Inoue #endif
79df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
80df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
81df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
82df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
83df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
84df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
85610ee2f9SDavid Greenman #ifdef TCPDEBUG
86df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
87610ee2f9SDavid Greenman #endif
88df8bae1dSRodney W. Grimes 
89df8bae1dSRodney W. Grimes /*
90df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
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 
252c78cbc7bSRobert Watson 	inp = sotoinpcb(so);
253c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
254c78cbc7bSRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
255c78cbc7bSRobert Watson 	INP_LOCK(inp);
256c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
257c78cbc7bSRobert Watson 	    ("tcp_usr_detach: inp_socket == NULL"));
258c78cbc7bSRobert Watson 	tcp_detach(so, inp);
259f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&tcbinfo);
2602c37256eSGarrett Wollman }
2612c37256eSGarrett Wollman 
2622c37256eSGarrett Wollman /*
2632c37256eSGarrett Wollman  * Give the socket an address.
2642c37256eSGarrett Wollman  */
2652c37256eSGarrett Wollman static int
266b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2672c37256eSGarrett Wollman {
2682c37256eSGarrett Wollman 	int error = 0;
269f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
270623dce13SRobert Watson 	struct tcpcb *tp = NULL;
2712c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
2722c37256eSGarrett Wollman 
27352710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
27452710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sinp))
27552710de1SPawel Jakub Dawidek 		return (EINVAL);
2762c37256eSGarrett Wollman 	/*
2772c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2782c37256eSGarrett Wollman 	 * to them.
2792c37256eSGarrett Wollman 	 */
2802c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
28152710de1SPawel Jakub Dawidek 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
28252710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
28352710de1SPawel Jakub Dawidek 
284623dce13SRobert Watson 	TCPDEBUG0;
285623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
286623dce13SRobert Watson 	inp = sotoinpcb(so);
287623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
288623dce13SRobert Watson 	INP_LOCK(inp);
289623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
290623dce13SRobert Watson 		error = EINVAL;
2912c37256eSGarrett Wollman 		goto out;
292623dce13SRobert Watson 	}
293623dce13SRobert Watson 	tp = intotcpcb(inp);
294623dce13SRobert Watson 	TCPDEBUG1();
295623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
296623dce13SRobert Watson out:
297623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
298623dce13SRobert Watson 	INP_UNLOCK(inp);
299623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
300623dce13SRobert Watson 
301623dce13SRobert Watson 	return (error);
3022c37256eSGarrett Wollman }
3032c37256eSGarrett Wollman 
304fb59c426SYoshinobu Inoue #ifdef INET6
305fb59c426SYoshinobu Inoue static int
306b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
307fb59c426SYoshinobu Inoue {
308fb59c426SYoshinobu Inoue 	int error = 0;
309f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
310623dce13SRobert Watson 	struct tcpcb *tp = NULL;
311fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
312fb59c426SYoshinobu Inoue 
31352710de1SPawel Jakub Dawidek 	sin6p = (struct sockaddr_in6 *)nam;
31452710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sin6p))
31552710de1SPawel Jakub Dawidek 		return (EINVAL);
316fb59c426SYoshinobu Inoue 	/*
317fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
318fb59c426SYoshinobu Inoue 	 * to them.
319fb59c426SYoshinobu Inoue 	 */
320fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6 &&
32152710de1SPawel Jakub Dawidek 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
32252710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
32352710de1SPawel Jakub Dawidek 
324623dce13SRobert Watson 	TCPDEBUG0;
325623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
326623dce13SRobert Watson 	inp = sotoinpcb(so);
327623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
328623dce13SRobert Watson 	INP_LOCK(inp);
329623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
330623dce13SRobert Watson 		error = EINVAL;
331623dce13SRobert Watson 		goto out;
332623dce13SRobert Watson 	}
333623dce13SRobert Watson 	tp = intotcpcb(inp);
334623dce13SRobert Watson 	TCPDEBUG1();
335fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
336fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
33766ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
338fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
339fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
340fb59c426SYoshinobu Inoue 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
341fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
342fb59c426SYoshinobu Inoue 
343fb59c426SYoshinobu Inoue 			in6_sin6_2_sin(&sin, sin6p);
344fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
345fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
346b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
347b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
348fb59c426SYoshinobu Inoue 			goto out;
349fb59c426SYoshinobu Inoue 		}
350fb59c426SYoshinobu Inoue 	}
351b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
352623dce13SRobert Watson out:
353623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
354623dce13SRobert Watson 	INP_UNLOCK(inp);
355623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
356623dce13SRobert Watson 	return (error);
357fb59c426SYoshinobu Inoue }
358fb59c426SYoshinobu Inoue #endif /* INET6 */
359fb59c426SYoshinobu Inoue 
3602c37256eSGarrett Wollman /*
3612c37256eSGarrett Wollman  * Prepare to accept connections.
3622c37256eSGarrett Wollman  */
3632c37256eSGarrett Wollman static int
364d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
3652c37256eSGarrett Wollman {
3662c37256eSGarrett Wollman 	int error = 0;
367f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
368623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3692c37256eSGarrett Wollman 
370623dce13SRobert Watson 	TCPDEBUG0;
371623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
372623dce13SRobert Watson 	inp = sotoinpcb(so);
373623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
374623dce13SRobert Watson 	INP_LOCK(inp);
375623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
376623dce13SRobert Watson 		error = EINVAL;
377623dce13SRobert Watson 		goto out;
378623dce13SRobert Watson 	}
379623dce13SRobert Watson 	tp = intotcpcb(inp);
380623dce13SRobert Watson 	TCPDEBUG1();
3810daccb9cSRobert Watson 	SOCK_LOCK(so);
3820daccb9cSRobert Watson 	error = solisten_proto_check(so);
3830daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0)
384b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
3850daccb9cSRobert Watson 	if (error == 0) {
3862c37256eSGarrett Wollman 		tp->t_state = TCPS_LISTEN;
387d374e81eSRobert Watson 		solisten_proto(so, backlog);
3880daccb9cSRobert Watson 	}
3890daccb9cSRobert Watson 	SOCK_UNLOCK(so);
390623dce13SRobert Watson 
391623dce13SRobert Watson out:
392623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
393623dce13SRobert Watson 	INP_UNLOCK(inp);
394623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
395623dce13SRobert Watson 	return (error);
3962c37256eSGarrett Wollman }
3972c37256eSGarrett Wollman 
398fb59c426SYoshinobu Inoue #ifdef INET6
399fb59c426SYoshinobu Inoue static int
400d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
401fb59c426SYoshinobu Inoue {
402fb59c426SYoshinobu Inoue 	int error = 0;
403f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
404623dce13SRobert Watson 	struct tcpcb *tp = NULL;
405fb59c426SYoshinobu Inoue 
406623dce13SRobert Watson 	TCPDEBUG0;
407623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
408623dce13SRobert Watson 	inp = sotoinpcb(so);
409623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
410623dce13SRobert Watson 	INP_LOCK(inp);
411623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
412623dce13SRobert Watson 		error = EINVAL;
413623dce13SRobert Watson 		goto out;
414623dce13SRobert Watson 	}
415623dce13SRobert Watson 	tp = intotcpcb(inp);
416623dce13SRobert Watson 	TCPDEBUG1();
4170daccb9cSRobert Watson 	SOCK_LOCK(so);
4180daccb9cSRobert Watson 	error = solisten_proto_check(so);
4190daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0) {
420fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
42166ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
422fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
423b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
424fb59c426SYoshinobu Inoue 	}
4250daccb9cSRobert Watson 	if (error == 0) {
426fb59c426SYoshinobu Inoue 		tp->t_state = TCPS_LISTEN;
427d374e81eSRobert Watson 		solisten_proto(so, backlog);
4280daccb9cSRobert Watson 	}
4290daccb9cSRobert Watson 	SOCK_UNLOCK(so);
430623dce13SRobert Watson 
431623dce13SRobert Watson out:
432623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
433623dce13SRobert Watson 	INP_UNLOCK(inp);
434623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
435623dce13SRobert Watson 	return (error);
436fb59c426SYoshinobu Inoue }
437fb59c426SYoshinobu Inoue #endif /* INET6 */
438fb59c426SYoshinobu Inoue 
4392c37256eSGarrett Wollman /*
4402c37256eSGarrett Wollman  * Initiate connection to peer.
4412c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
4422c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
4432c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
4442c37256eSGarrett Wollman  * Send initial segment on connection.
4452c37256eSGarrett Wollman  */
4462c37256eSGarrett Wollman static int
447b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
4482c37256eSGarrett Wollman {
4492c37256eSGarrett Wollman 	int error = 0;
450f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
451623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4522c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
4532c37256eSGarrett Wollman 
45457bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
455e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
456e29ef13fSDon Lewis 		return (EINVAL);
45752710de1SPawel Jakub Dawidek 	/*
45852710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
45952710de1SPawel Jakub Dawidek 	 */
4602c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
46152710de1SPawel Jakub Dawidek 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
46252710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
463812d8653SSam Leffler 	if (jailed(td->td_ucred))
464a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
46575c13541SPoul-Henning Kamp 
466623dce13SRobert Watson 	TCPDEBUG0;
467623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
468623dce13SRobert Watson 	inp = sotoinpcb(so);
469623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
470623dce13SRobert Watson 	INP_LOCK(inp);
471623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
472623dce13SRobert Watson 		error = EINVAL;
473623dce13SRobert Watson 		goto out;
474623dce13SRobert Watson 	}
475623dce13SRobert Watson 	tp = intotcpcb(inp);
476623dce13SRobert Watson 	TCPDEBUG1();
477b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
4782c37256eSGarrett Wollman 		goto out;
4792c37256eSGarrett Wollman 	error = tcp_output(tp);
480623dce13SRobert Watson out:
481623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
482623dce13SRobert Watson 	INP_UNLOCK(inp);
483623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
484623dce13SRobert Watson 	return (error);
4852c37256eSGarrett Wollman }
4862c37256eSGarrett Wollman 
487fb59c426SYoshinobu Inoue #ifdef INET6
488fb59c426SYoshinobu Inoue static int
489b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
490fb59c426SYoshinobu Inoue {
491fb59c426SYoshinobu Inoue 	int error = 0;
492f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
493623dce13SRobert Watson 	struct tcpcb *tp = NULL;
494fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
495623dce13SRobert Watson 
496623dce13SRobert Watson 	TCPDEBUG0;
497fb59c426SYoshinobu Inoue 
498fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
499e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sin6p))
500e29ef13fSDon Lewis 		return (EINVAL);
50152710de1SPawel Jakub Dawidek 	/*
50252710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
50352710de1SPawel Jakub Dawidek 	 */
504fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6
50552710de1SPawel Jakub Dawidek 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
50652710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
507fb59c426SYoshinobu Inoue 
508623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
509623dce13SRobert Watson 	inp = sotoinpcb(so);
510623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
511623dce13SRobert Watson 	INP_LOCK(inp);
512623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
513623dce13SRobert Watson 		error = EINVAL;
514623dce13SRobert Watson 		goto out;
515623dce13SRobert Watson 	}
516623dce13SRobert Watson 	tp = intotcpcb(inp);
517623dce13SRobert Watson 	TCPDEBUG1();
51833841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
519fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
520fb59c426SYoshinobu Inoue 
521d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
522d46a5312SMaxim Konovalov 			error = EINVAL;
523d46a5312SMaxim Konovalov 			goto out;
524d46a5312SMaxim Konovalov 		}
52533841545SHajimu UMEMOTO 
526fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
527fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
528fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
529b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
530fb59c426SYoshinobu Inoue 			goto out;
531fb59c426SYoshinobu Inoue 		error = tcp_output(tp);
532fb59c426SYoshinobu Inoue 		goto out;
533fb59c426SYoshinobu Inoue 	}
534fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
535fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
536b7d6d952SHajimu UMEMOTO 	inp->inp_inc.inc_isipv6 = 1;
537b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
538fb59c426SYoshinobu Inoue 		goto out;
539fb59c426SYoshinobu Inoue 	error = tcp_output(tp);
540623dce13SRobert Watson 
541623dce13SRobert Watson out:
542623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
543623dce13SRobert Watson 	INP_UNLOCK(inp);
544623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
545623dce13SRobert Watson 	return (error);
546fb59c426SYoshinobu Inoue }
547fb59c426SYoshinobu Inoue #endif /* INET6 */
548fb59c426SYoshinobu Inoue 
5492c37256eSGarrett Wollman /*
5502c37256eSGarrett Wollman  * Initiate disconnect from peer.
5512c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
5522c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
5532c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
5542c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
5552c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
5562c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
5572c37256eSGarrett Wollman  *
5582c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
5592c37256eSGarrett Wollman  */
5602c37256eSGarrett Wollman static int
5612c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
5622c37256eSGarrett Wollman {
563f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
564623dce13SRobert Watson 	struct tcpcb *tp = NULL;
565623dce13SRobert Watson 	int error = 0;
5662c37256eSGarrett Wollman 
567623dce13SRobert Watson 	TCPDEBUG0;
568623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
569623dce13SRobert Watson 	inp = sotoinpcb(so);
570623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
571623dce13SRobert Watson 	INP_LOCK(inp);
572623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
57321367f63SSam Leffler 		error = ECONNRESET;
574623dce13SRobert Watson 		goto out;
575623dce13SRobert Watson 	}
576623dce13SRobert Watson 	tp = intotcpcb(inp);
577623dce13SRobert Watson 	TCPDEBUG1();
578623dce13SRobert Watson 	tcp_disconnect(tp);
579623dce13SRobert Watson out:
580623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
581623dce13SRobert Watson 	INP_UNLOCK(inp);
582623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
583623dce13SRobert Watson 	return (error);
5842c37256eSGarrett Wollman }
5852c37256eSGarrett Wollman 
5862c37256eSGarrett Wollman /*
5872c37256eSGarrett Wollman  * Accept a connection.  Essentially all the work is
5882c37256eSGarrett Wollman  * done at higher levels; just return the address
5892c37256eSGarrett Wollman  * of the peer, storing through addr.
5902c37256eSGarrett Wollman  */
5912c37256eSGarrett Wollman static int
59257bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
5932c37256eSGarrett Wollman {
5942c37256eSGarrett Wollman 	int error = 0;
595f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
5961db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
59726ef6ac4SDon Lewis 	struct in_addr addr;
59826ef6ac4SDon Lewis 	in_port_t port = 0;
5991db24ffbSJonathan Lemon 	TCPDEBUG0;
6002c37256eSGarrett Wollman 
6013d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
6023d2d3ef4SRobert Watson 		return (ECONNABORTED);
603f76fcf6dSJeffrey Hsu 
604f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
605623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
606f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
607623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
6083d2d3ef4SRobert Watson 		error = ECONNABORTED;
609623dce13SRobert Watson 		goto out;
610623dce13SRobert Watson 	}
6111db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
6121db24ffbSJonathan Lemon 	TCPDEBUG1();
613f76fcf6dSJeffrey Hsu 
614f76fcf6dSJeffrey Hsu 	/*
61554d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
61626ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
61726ef6ac4SDon Lewis 	 * release the lock.
618f76fcf6dSJeffrey Hsu 	 */
61926ef6ac4SDon Lewis 	port = inp->inp_fport;
62026ef6ac4SDon Lewis 	addr = inp->inp_faddr;
621f76fcf6dSJeffrey Hsu 
622623dce13SRobert Watson out:
623623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
62426ef6ac4SDon Lewis 	INP_UNLOCK(inp);
62526ef6ac4SDon Lewis 	if (error == 0)
62626ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
62726ef6ac4SDon Lewis 	return error;
6282c37256eSGarrett Wollman }
6292c37256eSGarrett Wollman 
630fb59c426SYoshinobu Inoue #ifdef INET6
631fb59c426SYoshinobu Inoue static int
632fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
633fb59c426SYoshinobu Inoue {
634f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
635fb59c426SYoshinobu Inoue 	int error = 0;
6361db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
63726ef6ac4SDon Lewis 	struct in_addr addr;
63826ef6ac4SDon Lewis 	struct in6_addr addr6;
63926ef6ac4SDon Lewis 	in_port_t port = 0;
64026ef6ac4SDon Lewis 	int v4 = 0;
6411db24ffbSJonathan Lemon 	TCPDEBUG0;
642fb59c426SYoshinobu Inoue 
643b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
644b4470c16SRobert Watson 		return (ECONNABORTED);
645f76fcf6dSJeffrey Hsu 
646f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
647623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
648f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
649623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
65021367f63SSam Leffler 		error = ECONNABORTED;
651623dce13SRobert Watson 		goto out;
652623dce13SRobert Watson 	}
6531db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
6541db24ffbSJonathan Lemon 	TCPDEBUG1();
655623dce13SRobert Watson 
65626ef6ac4SDon Lewis 	/*
65726ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
65826ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
65926ef6ac4SDon Lewis 	 * release the lock.
66026ef6ac4SDon Lewis 	 */
66126ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
66226ef6ac4SDon Lewis 		v4 = 1;
66326ef6ac4SDon Lewis 		port = inp->inp_fport;
66426ef6ac4SDon Lewis 		addr = inp->inp_faddr;
66526ef6ac4SDon Lewis 	} else {
66626ef6ac4SDon Lewis 		port = inp->inp_fport;
66726ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
66826ef6ac4SDon Lewis 	}
66926ef6ac4SDon Lewis 
670623dce13SRobert Watson out:
671623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
67226ef6ac4SDon Lewis 	INP_UNLOCK(inp);
67326ef6ac4SDon Lewis 	if (error == 0) {
67426ef6ac4SDon Lewis 		if (v4)
67526ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
67626ef6ac4SDon Lewis 		else
67726ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
67826ef6ac4SDon Lewis 	}
67926ef6ac4SDon Lewis 	return error;
680fb59c426SYoshinobu Inoue }
681fb59c426SYoshinobu Inoue #endif /* INET6 */
682f76fcf6dSJeffrey Hsu 
683f76fcf6dSJeffrey Hsu /*
6842c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
6852c37256eSGarrett Wollman  */
6862c37256eSGarrett Wollman static int
6872c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
6882c37256eSGarrett Wollman {
6892c37256eSGarrett Wollman 	int error = 0;
690f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
691623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6922c37256eSGarrett Wollman 
693623dce13SRobert Watson 	TCPDEBUG0;
694623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
695623dce13SRobert Watson 	inp = sotoinpcb(so);
696623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
697623dce13SRobert Watson 	INP_LOCK(inp);
698623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
69921367f63SSam Leffler 		error = ECONNRESET;
700623dce13SRobert Watson 		goto out;
701623dce13SRobert Watson 	}
702623dce13SRobert Watson 	tp = intotcpcb(inp);
703623dce13SRobert Watson 	TCPDEBUG1();
7042c37256eSGarrett Wollman 	socantsendmore(so);
705623dce13SRobert Watson 	tcp_usrclosed(tp);
7062c37256eSGarrett Wollman 	error = tcp_output(tp);
707623dce13SRobert Watson 
708623dce13SRobert Watson out:
709623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
710623dce13SRobert Watson 	INP_UNLOCK(inp);
711623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
712623dce13SRobert Watson 
713623dce13SRobert Watson 	return (error);
7142c37256eSGarrett Wollman }
7152c37256eSGarrett Wollman 
7162c37256eSGarrett Wollman /*
7172c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
7182c37256eSGarrett Wollman  */
7192c37256eSGarrett Wollman static int
7202c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
7212c37256eSGarrett Wollman {
722f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
723623dce13SRobert Watson 	struct tcpcb *tp = NULL;
724623dce13SRobert Watson 	int error = 0;
7252c37256eSGarrett Wollman 
726623dce13SRobert Watson 	TCPDEBUG0;
727623dce13SRobert Watson 	inp = sotoinpcb(so);
728623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
729623dce13SRobert Watson 	INP_LOCK(inp);
730623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
73121367f63SSam Leffler 		error = ECONNRESET;
732623dce13SRobert Watson 		goto out;
733623dce13SRobert Watson 	}
734623dce13SRobert Watson 	tp = intotcpcb(inp);
735623dce13SRobert Watson 	TCPDEBUG1();
7362c37256eSGarrett Wollman 	tcp_output(tp);
737623dce13SRobert Watson 
738623dce13SRobert Watson out:
739623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
740623dce13SRobert Watson 	INP_UNLOCK(inp);
741623dce13SRobert Watson 	return (error);
7422c37256eSGarrett Wollman }
7432c37256eSGarrett Wollman 
7442c37256eSGarrett Wollman /*
7452c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
7469c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
7479c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
7489c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
7499c9906e9SPeter Wemm  * generally are caller-frees.
7502c37256eSGarrett Wollman  */
7512c37256eSGarrett Wollman static int
75257bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
753b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
7542c37256eSGarrett Wollman {
7552c37256eSGarrett Wollman 	int error = 0;
756f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
757623dce13SRobert Watson 	struct tcpcb *tp = NULL;
758623dce13SRobert Watson 	int headlocked = 0;
759fb59c426SYoshinobu Inoue #ifdef INET6
760fb59c426SYoshinobu Inoue 	int isipv6;
761fb59c426SYoshinobu Inoue #endif
7629c9906e9SPeter Wemm 	TCPDEBUG0;
7632c37256eSGarrett Wollman 
764f76fcf6dSJeffrey Hsu 	/*
765623dce13SRobert Watson 	 * We require the pcbinfo lock in two cases:
766623dce13SRobert Watson 	 *
767623dce13SRobert Watson 	 * (1) An implied connect is taking place, which can result in
768623dce13SRobert Watson 	 *     binding IPs and ports and hence modification of the pcb hash
769623dce13SRobert Watson 	 *     chains.
770623dce13SRobert Watson 	 *
771623dce13SRobert Watson 	 * (2) PRUS_EOF is set, resulting in explicit close on the send.
772f76fcf6dSJeffrey Hsu 	 */
773623dce13SRobert Watson 	if ((nam != NULL) || (flags & PRUS_EOF)) {
774f76fcf6dSJeffrey Hsu 		INP_INFO_WLOCK(&tcbinfo);
775623dce13SRobert Watson 		headlocked = 1;
776623dce13SRobert Watson 	}
777f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
778623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
779623dce13SRobert Watson 	INP_LOCK(inp);
780623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
7817ff0b850SAndre Oppermann 		if (control)
7827ff0b850SAndre Oppermann 			m_freem(control);
7837ff0b850SAndre Oppermann 		if (m)
7847ff0b850SAndre Oppermann 			m_freem(m);
78521367f63SSam Leffler 		error = ECONNRESET;
7869c9906e9SPeter Wemm 		goto out;
7879c9906e9SPeter Wemm 	}
788fb59c426SYoshinobu Inoue #ifdef INET6
789fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
790fb59c426SYoshinobu Inoue #endif /* INET6 */
7919c9906e9SPeter Wemm 	tp = intotcpcb(inp);
7929c9906e9SPeter Wemm 	TCPDEBUG1();
7939c9906e9SPeter Wemm 	if (control) {
7949c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
7959c9906e9SPeter Wemm 		if (control->m_len) {
7969c9906e9SPeter Wemm 			m_freem(control);
7972c37256eSGarrett Wollman 			if (m)
7982c37256eSGarrett Wollman 				m_freem(m);
799744f87eaSDavid Greenman 			error = EINVAL;
800744f87eaSDavid Greenman 			goto out;
8012c37256eSGarrett Wollman 		}
8029c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
8039c9906e9SPeter Wemm 	}
8042c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
805395bb186SSam Leffler 		sbappendstream(&so->so_snd, m);
8062c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
8072c37256eSGarrett Wollman 			/*
8082c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
8092c37256eSGarrett Wollman 			 * initialize window to default value, and
8102c37256eSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
8112c37256eSGarrett Wollman 			 * MSS.
8122c37256eSGarrett Wollman 			 */
813623dce13SRobert Watson 			INP_INFO_WLOCK_ASSERT(&tcbinfo);
814fb59c426SYoshinobu Inoue #ifdef INET6
815fb59c426SYoshinobu Inoue 			if (isipv6)
816b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
817fb59c426SYoshinobu Inoue 			else
818fb59c426SYoshinobu Inoue #endif /* INET6 */
819b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
8202c37256eSGarrett Wollman 			if (error)
8212c37256eSGarrett Wollman 				goto out;
8222c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
8232c37256eSGarrett Wollman 			tcp_mss(tp, -1);
8242c37256eSGarrett Wollman 		}
8252c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
8262c37256eSGarrett Wollman 			/*
8272c37256eSGarrett Wollman 			 * Close the send side of the connection after
8282c37256eSGarrett Wollman 			 * the data is sent.
8292c37256eSGarrett Wollman 			 */
830623dce13SRobert Watson 			INP_INFO_WLOCK_ASSERT(&tcbinfo);
8312c37256eSGarrett Wollman 			socantsendmore(so);
832623dce13SRobert Watson 			tcp_usrclosed(tp);
8332c37256eSGarrett Wollman 		}
834623dce13SRobert Watson 		if (headlocked) {
835d1401c90SRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
836623dce13SRobert Watson 			headlocked = 0;
837623dce13SRobert Watson 		}
838b0acefa8SBill Fenner 		if (tp != NULL) {
839b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
840b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
8412c37256eSGarrett Wollman 			error = tcp_output(tp);
842b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
843b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
844b0acefa8SBill Fenner 		}
8452c37256eSGarrett Wollman 	} else {
846623dce13SRobert Watson 		/*
847623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
848623dce13SRobert Watson 		 */
849d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
8502c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
851d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
8522c37256eSGarrett Wollman 			m_freem(m);
8532c37256eSGarrett Wollman 			error = ENOBUFS;
8542c37256eSGarrett Wollman 			goto out;
8552c37256eSGarrett Wollman 		}
8562c37256eSGarrett Wollman 		/*
8572c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
8582c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
8592c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
8602c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
8612c37256eSGarrett Wollman 		 * of data past the urgent section.
8622c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
8632c37256eSGarrett Wollman 		 */
864d2bc35abSRobert Watson 		sbappendstream_locked(&so->so_snd, m);
865d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
866ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
867ef53690bSGarrett Wollman 			/*
868ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
869ef53690bSGarrett Wollman 			 * initialize window to default value, and
870ef53690bSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
871ef53690bSGarrett Wollman 			 * MSS.
872ef53690bSGarrett Wollman 			 */
873623dce13SRobert Watson 			INP_INFO_WLOCK_ASSERT(&tcbinfo);
874fb59c426SYoshinobu Inoue #ifdef INET6
875fb59c426SYoshinobu Inoue 			if (isipv6)
876b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
877fb59c426SYoshinobu Inoue 			else
878fb59c426SYoshinobu Inoue #endif /* INET6 */
879b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
880ef53690bSGarrett Wollman 			if (error)
881ef53690bSGarrett Wollman 				goto out;
882ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
883ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
884d1401c90SRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
885623dce13SRobert Watson 			headlocked = 0;
886623dce13SRobert Watson 		} else if (nam) {
887623dce13SRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
888623dce13SRobert Watson 			headlocked = 0;
889623dce13SRobert Watson 		}
8902c37256eSGarrett Wollman 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
8912cdbfa66SPaul Saab 		tp->t_flags |= TF_FORCEDATA;
8922c37256eSGarrett Wollman 		error = tcp_output(tp);
8932cdbfa66SPaul Saab 		tp->t_flags &= ~TF_FORCEDATA;
8942c37256eSGarrett Wollman 	}
895d1401c90SRobert Watson out:
896d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
8972c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
898d1401c90SRobert Watson 	INP_UNLOCK(inp);
899623dce13SRobert Watson 	if (headlocked)
900d1401c90SRobert Watson 		INP_INFO_WUNLOCK(&tcbinfo);
90173fddedaSPeter Grehan 	return (error);
9022c37256eSGarrett Wollman }
9032c37256eSGarrett Wollman 
9042c37256eSGarrett Wollman /*
905a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
9062c37256eSGarrett Wollman  */
907ac45e92fSRobert Watson static void
9082c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
9092c37256eSGarrett Wollman {
910f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
911a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
912623dce13SRobert Watson 	TCPDEBUG0;
913c78cbc7bSRobert Watson 
914ac45e92fSRobert Watson 	inp = sotoinpcb(so);
915c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
916c78cbc7bSRobert Watson 
917c78cbc7bSRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
918ac45e92fSRobert Watson 	INP_LOCK(inp);
919c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
920c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
921c78cbc7bSRobert Watson 
922c78cbc7bSRobert Watson 	/*
923a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
924c78cbc7bSRobert Watson 	 */
925c78cbc7bSRobert Watson 	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
926c78cbc7bSRobert Watson 	    !(inp->inp_vflag & INP_DROPPED)) {
927c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
928a152f8a3SRobert Watson 		TCPDEBUG1();
929c78cbc7bSRobert Watson 		tcp_drop(tp, ECONNABORTED);
930a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
931c78cbc7bSRobert Watson 	}
932a152f8a3SRobert Watson 	if (!(inp->inp_vflag & INP_DROPPED)) {
933a152f8a3SRobert Watson 		SOCK_LOCK(so);
934a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
935a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
936a152f8a3SRobert Watson 		inp->inp_vflag |= INP_SOCKREF;
937a152f8a3SRobert Watson 	}
938a152f8a3SRobert Watson 	INP_UNLOCK(inp);
939a152f8a3SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
940a152f8a3SRobert Watson }
941a152f8a3SRobert Watson 
942a152f8a3SRobert Watson /*
943a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
944a152f8a3SRobert Watson  */
945a152f8a3SRobert Watson static void
946a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
947a152f8a3SRobert Watson {
948a152f8a3SRobert Watson 	struct inpcb *inp;
949a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
950a152f8a3SRobert Watson 	TCPDEBUG0;
951a152f8a3SRobert Watson 
952a152f8a3SRobert Watson 	inp = sotoinpcb(so);
953a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
954a152f8a3SRobert Watson 
955a152f8a3SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
956a152f8a3SRobert Watson 	INP_LOCK(inp);
957a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
958a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
959a152f8a3SRobert Watson 
960a152f8a3SRobert Watson 	/*
961a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
962a152f8a3SRobert Watson 	 * a disconnect.
963a152f8a3SRobert Watson 	 */
964a152f8a3SRobert Watson 	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
965a152f8a3SRobert Watson 	    !(inp->inp_vflag & INP_DROPPED)) {
966a152f8a3SRobert Watson 		tp = intotcpcb(inp);
967a152f8a3SRobert Watson 		TCPDEBUG1();
968a152f8a3SRobert Watson 		tcp_disconnect(tp);
969a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
970a152f8a3SRobert Watson 	}
971a152f8a3SRobert Watson 	if (!(inp->inp_vflag & INP_DROPPED)) {
972a152f8a3SRobert Watson 		SOCK_LOCK(so);
973a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
974a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
975a152f8a3SRobert Watson 		inp->inp_vflag |= INP_SOCKREF;
976a152f8a3SRobert Watson 	}
977a152f8a3SRobert Watson 	INP_UNLOCK(inp);
978ac45e92fSRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
9792c37256eSGarrett Wollman }
9802c37256eSGarrett Wollman 
9812c37256eSGarrett Wollman /*
9822c37256eSGarrett Wollman  * Receive out-of-band data.
9832c37256eSGarrett Wollman  */
9842c37256eSGarrett Wollman static int
9852c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
9862c37256eSGarrett Wollman {
9872c37256eSGarrett Wollman 	int error = 0;
988f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
989623dce13SRobert Watson 	struct tcpcb *tp = NULL;
9902c37256eSGarrett Wollman 
991623dce13SRobert Watson 	TCPDEBUG0;
992623dce13SRobert Watson 	inp = sotoinpcb(so);
993623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
994623dce13SRobert Watson 	INP_LOCK(inp);
995623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
99621367f63SSam Leffler 		error = ECONNRESET;
997623dce13SRobert Watson 		goto out;
998623dce13SRobert Watson 	}
999623dce13SRobert Watson 	tp = intotcpcb(inp);
1000623dce13SRobert Watson 	TCPDEBUG1();
10012c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1002c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
10034cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
10044cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
10052c37256eSGarrett Wollman 		error = EINVAL;
10062c37256eSGarrett Wollman 		goto out;
10072c37256eSGarrett Wollman 	}
10082c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
10092c37256eSGarrett Wollman 		error = EWOULDBLOCK;
10102c37256eSGarrett Wollman 		goto out;
10112c37256eSGarrett Wollman 	}
10122c37256eSGarrett Wollman 	m->m_len = 1;
10132c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
10142c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
10152c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1016623dce13SRobert Watson 
1017623dce13SRobert Watson out:
1018623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
1019623dce13SRobert Watson 	INP_UNLOCK(inp);
1020623dce13SRobert Watson 	return (error);
10212c37256eSGarrett Wollman }
10222c37256eSGarrett Wollman 
10232c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1024756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1025756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1026756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1027756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1028756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1029756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1030756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1031756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1032756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
103354d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1034756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1035756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1036756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
1037756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
103854d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1039a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1040a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
10412c37256eSGarrett Wollman };
1042df8bae1dSRodney W. Grimes 
1043fb59c426SYoshinobu Inoue #ifdef INET6
1044fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1045756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1046756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1047756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1048756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1049756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1050756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1051756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1052756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1053756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1054756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1055756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1056756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1057756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
1058756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1059756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1060a152f8a3SRobert Watson  	.pru_sosetlabel =	in_pcbsosetlabel,
1061a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1062fb59c426SYoshinobu Inoue };
1063fb59c426SYoshinobu Inoue #endif /* INET6 */
1064fb59c426SYoshinobu Inoue 
1065a0292f23SGarrett Wollman /*
1066a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1067a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
10685200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
10695200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
10705200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
10715200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1072a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1073a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1074a0292f23SGarrett Wollman  */
10750312fbe9SPoul-Henning Kamp static int
1076ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1077a0292f23SGarrett Wollman {
1078a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1079a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
10805200e00eSIan Dowse 	struct in_addr laddr;
10815200e00eSIan Dowse 	u_short lport;
1082c3229e05SDavid Greenman 	int error;
1083a0292f23SGarrett Wollman 
1084623dce13SRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1085623dce13SRobert Watson 	INP_LOCK_ASSERT(inp);
1086623dce13SRobert Watson 
1087a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
1088b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1089a0292f23SGarrett Wollman 		if (error)
1090a0292f23SGarrett Wollman 			return error;
1091a0292f23SGarrett Wollman 	}
1092a0292f23SGarrett Wollman 
1093a0292f23SGarrett Wollman 	/*
1094a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1095a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1096a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1097a0292f23SGarrett Wollman 	 */
10985200e00eSIan Dowse 	laddr = inp->inp_laddr;
10995200e00eSIan Dowse 	lport = inp->inp_lport;
11005200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1101b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
11025200e00eSIan Dowse 	if (error && oinp == NULL)
1103d3628763SRodney W. Grimes 		return error;
1104c94c54e4SAndre Oppermann 	if (oinp)
1105a0292f23SGarrett Wollman 		return EADDRINUSE;
11065200e00eSIan Dowse 	inp->inp_laddr = laddr;
110715bd2b43SDavid Greenman 	in_pcbrehash(inp);
1108a0292f23SGarrett Wollman 
1109087b55eaSAndre Oppermann 	/*
1110087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1111087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1112087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1113087b55eaSAndre Oppermann 	 */
1114a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
11159b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1116a0292f23SGarrett Wollman 		tp->request_r_scale++;
1117a0292f23SGarrett Wollman 
1118a0292f23SGarrett Wollman 	soisconnecting(so);
1119a0292f23SGarrett Wollman 	tcpstat.tcps_connattempt++;
1120a0292f23SGarrett Wollman 	tp->t_state = TCPS_SYN_SENT;
1121b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1122b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
11231fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
1124a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1125a45d2726SAndras Olah 
1126a0292f23SGarrett Wollman 	return 0;
1127a0292f23SGarrett Wollman }
1128a0292f23SGarrett Wollman 
1129fb59c426SYoshinobu Inoue #ifdef INET6
1130fb59c426SYoshinobu Inoue static int
1131ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1132fb59c426SYoshinobu Inoue {
1133fb59c426SYoshinobu Inoue 	struct inpcb *inp = tp->t_inpcb, *oinp;
1134fb59c426SYoshinobu Inoue 	struct socket *so = inp->inp_socket;
1135fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1136fb59c426SYoshinobu Inoue 	struct in6_addr *addr6;
1137fb59c426SYoshinobu Inoue 	int error;
1138fb59c426SYoshinobu Inoue 
1139623dce13SRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1140623dce13SRobert Watson 	INP_LOCK_ASSERT(inp);
1141623dce13SRobert Watson 
1142fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
1143b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1144fb59c426SYoshinobu Inoue 		if (error)
1145fb59c426SYoshinobu Inoue 			return error;
1146fb59c426SYoshinobu Inoue 	}
1147fb59c426SYoshinobu Inoue 
1148fb59c426SYoshinobu Inoue 	/*
1149fb59c426SYoshinobu Inoue 	 * Cannot simply call in_pcbconnect, because there might be an
1150fb59c426SYoshinobu Inoue 	 * earlier incarnation of this same connection still in
1151fb59c426SYoshinobu Inoue 	 * TIME_WAIT state, creating an ADDRINUSE error.
1152a1f7e5f8SHajimu UMEMOTO 	 * in6_pcbladdr() also handles scope zone IDs.
1153fb59c426SYoshinobu Inoue 	 */
1154fb59c426SYoshinobu Inoue 	error = in6_pcbladdr(inp, nam, &addr6);
1155fb59c426SYoshinobu Inoue 	if (error)
1156fb59c426SYoshinobu Inoue 		return error;
1157fb59c426SYoshinobu Inoue 	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1158fb59c426SYoshinobu Inoue 				  &sin6->sin6_addr, sin6->sin6_port,
1159fb59c426SYoshinobu Inoue 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1160fb59c426SYoshinobu Inoue 				  ? addr6
1161fb59c426SYoshinobu Inoue 				  : &inp->in6p_laddr,
1162fb59c426SYoshinobu Inoue 				  inp->inp_lport,  0, NULL);
1163c94c54e4SAndre Oppermann 	if (oinp)
1164fb59c426SYoshinobu Inoue 		return EADDRINUSE;
1165fb59c426SYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1166fb59c426SYoshinobu Inoue 		inp->in6p_laddr = *addr6;
1167fb59c426SYoshinobu Inoue 	inp->in6p_faddr = sin6->sin6_addr;
1168fb59c426SYoshinobu Inoue 	inp->inp_fport = sin6->sin6_port;
11698a59da30SHajimu UMEMOTO 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
11708a59da30SHajimu UMEMOTO 	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
11718a59da30SHajimu UMEMOTO 	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
11728a59da30SHajimu UMEMOTO 		inp->in6p_flowinfo |=
11738a59da30SHajimu UMEMOTO 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1174fb59c426SYoshinobu Inoue 	in_pcbrehash(inp);
1175fb59c426SYoshinobu Inoue 
1176fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1177fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1178fb59c426SYoshinobu Inoue 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1179fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1180fb59c426SYoshinobu Inoue 
1181fb59c426SYoshinobu Inoue 	soisconnecting(so);
1182fb59c426SYoshinobu Inoue 	tcpstat.tcps_connattempt++;
1183fb59c426SYoshinobu Inoue 	tp->t_state = TCPS_SYN_SENT;
1184b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1185b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
11861fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
1187fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1188fb59c426SYoshinobu Inoue 
1189fb59c426SYoshinobu Inoue 	return 0;
1190fb59c426SYoshinobu Inoue }
1191fb59c426SYoshinobu Inoue #endif /* INET6 */
1192fb59c426SYoshinobu Inoue 
1193cfe8b629SGarrett Wollman /*
1194b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1195b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1196b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1197b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1198b8af5dfaSRobert Watson  * from Linux.
1199b8af5dfaSRobert Watson  */
1200b8af5dfaSRobert Watson static void
1201ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1202b8af5dfaSRobert Watson {
1203b8af5dfaSRobert Watson 
1204b8af5dfaSRobert Watson 	INP_LOCK_ASSERT(tp->t_inpcb);
1205b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1206b8af5dfaSRobert Watson 
1207b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1208b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1209b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
12103529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1211b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1212b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1213b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1214b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1215b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1216b8af5dfaSRobert Watson 	}
12171baaf834SBruce M Simpson 
12181baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
12191baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
12201baaf834SBruce M Simpson 
1221b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1222b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1223b8af5dfaSRobert Watson 
1224b8af5dfaSRobert Watson 	/*
1225b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1226b8af5dfaSRobert Watson 	 */
1227c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1228b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
1229b8af5dfaSRobert Watson 	ti->tcpi_snd_bwnd = tp->snd_bwnd;
1230b8af5dfaSRobert Watson }
1231b8af5dfaSRobert Watson 
1232b8af5dfaSRobert Watson /*
1233cfe8b629SGarrett Wollman  * The new sockopt interface makes it possible for us to block in the
1234cfe8b629SGarrett Wollman  * copyin/out step (if we take a page fault).  Taking a page fault at
1235cfe8b629SGarrett Wollman  * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
1236cfe8b629SGarrett Wollman  * use TSM, there probably isn't any need for this function to run at
1237cfe8b629SGarrett Wollman  * splnet() any more.  This needs more examination.)
1238b8af5dfaSRobert Watson  *
1239b8af5dfaSRobert Watson  * XXXRW: The locking here is wrong; we may take a page fault while holding
1240b8af5dfaSRobert Watson  * the inpcb lock.
1241cfe8b629SGarrett Wollman  */
1242df8bae1dSRodney W. Grimes int
1243ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1244df8bae1dSRodney W. Grimes {
12453f9d1ef9SRobert Watson 	int	error, opt, optval;
1246df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1247cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
1248b8af5dfaSRobert Watson 	struct	tcp_info ti;
1249df8bae1dSRodney W. Grimes 
1250cfe8b629SGarrett Wollman 	error = 0;
1251df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1252623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1253f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1254cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
12554e397bc5SRobert Watson 		INP_UNLOCK(inp);
1256fb59c426SYoshinobu Inoue #ifdef INET6
1257fb59c426SYoshinobu Inoue 		if (INP_CHECK_SOCKAF(so, AF_INET6))
1258fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
1259fb59c426SYoshinobu Inoue 		else
1260fb59c426SYoshinobu Inoue #endif /* INET6 */
1261cfe8b629SGarrett Wollman 		error = ip_ctloutput(so, sopt);
1262df8bae1dSRodney W. Grimes 		return (error);
1263df8bae1dSRodney W. Grimes 	}
1264623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
1265623dce13SRobert Watson 		error = ECONNRESET;
1266623dce13SRobert Watson 		goto out;
1267623dce13SRobert Watson 	}
1268df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
1269df8bae1dSRodney W. Grimes 
1270cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1271cfe8b629SGarrett Wollman 	case SOPT_SET:
1272cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
12731cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
127488f6b043SBruce M Simpson 		case TCP_MD5SIG:
12751cfd4b53SBruce M Simpson 			error = sooptcopyin(sopt, &optval, sizeof optval,
12761cfd4b53SBruce M Simpson 					    sizeof optval);
12771cfd4b53SBruce M Simpson 			if (error)
12781cfd4b53SBruce M Simpson 				break;
12791cfd4b53SBruce M Simpson 
12801cfd4b53SBruce M Simpson 			if (optval > 0)
12811cfd4b53SBruce M Simpson 				tp->t_flags |= TF_SIGNATURE;
12821cfd4b53SBruce M Simpson 			else
12831cfd4b53SBruce M Simpson 				tp->t_flags &= ~TF_SIGNATURE;
12841cfd4b53SBruce M Simpson 			break;
12851cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
1286df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1287cfe8b629SGarrett Wollman 		case TCP_NOOPT:
1288cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1289cfe8b629SGarrett Wollman 					    sizeof optval);
1290cfe8b629SGarrett Wollman 			if (error)
1291cfe8b629SGarrett Wollman 				break;
1292cfe8b629SGarrett Wollman 
1293cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1294cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1295cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1296cfe8b629SGarrett Wollman 				break;
1297cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1298cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1299cfe8b629SGarrett Wollman 				break;
1300cfe8b629SGarrett Wollman 			default:
1301cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1302cfe8b629SGarrett Wollman 				break;
1303cfe8b629SGarrett Wollman 			}
1304cfe8b629SGarrett Wollman 
1305cfe8b629SGarrett Wollman 			if (optval)
1306cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1307df8bae1dSRodney W. Grimes 			else
1308cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
1309df8bae1dSRodney W. Grimes 			break;
1310df8bae1dSRodney W. Grimes 
1311007581c0SJonathan Lemon 		case TCP_NOPUSH:
1312007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1313007581c0SJonathan Lemon 					    sizeof optval);
1314007581c0SJonathan Lemon 			if (error)
1315007581c0SJonathan Lemon 				break;
1316007581c0SJonathan Lemon 
1317007581c0SJonathan Lemon 			if (optval)
1318007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1319007581c0SJonathan Lemon 			else {
1320007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1321007581c0SJonathan Lemon 				error = tcp_output(tp);
1322007581c0SJonathan Lemon 			}
1323007581c0SJonathan Lemon 			break;
1324007581c0SJonathan Lemon 
1325df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1326cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1327cfe8b629SGarrett Wollman 					    sizeof optval);
1328cfe8b629SGarrett Wollman 			if (error)
1329df8bae1dSRodney W. Grimes 				break;
1330df8bae1dSRodney W. Grimes 
133153369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
133253369ac9SAndre Oppermann 			    optval + 40 >= tcp_minmss)
1333cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1334a0292f23SGarrett Wollman 			else
1335a0292f23SGarrett Wollman 				error = EINVAL;
1336a0292f23SGarrett Wollman 			break;
1337a0292f23SGarrett Wollman 
1338b8af5dfaSRobert Watson 		case TCP_INFO:
1339b8af5dfaSRobert Watson 			error = EINVAL;
1340b8af5dfaSRobert Watson 			break;
1341b8af5dfaSRobert Watson 
1342df8bae1dSRodney W. Grimes 		default:
1343df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1344df8bae1dSRodney W. Grimes 			break;
1345df8bae1dSRodney W. Grimes 		}
1346df8bae1dSRodney W. Grimes 		break;
1347df8bae1dSRodney W. Grimes 
1348cfe8b629SGarrett Wollman 	case SOPT_GET:
1349cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
13501cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
135188f6b043SBruce M Simpson 		case TCP_MD5SIG:
13521cfd4b53SBruce M Simpson 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1353b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
13541cfd4b53SBruce M Simpson 			break;
1355265ed012SBruce M Simpson #endif
1356df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1357cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
1358b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1359df8bae1dSRodney W. Grimes 			break;
1360df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1361cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
1362b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1363df8bae1dSRodney W. Grimes 			break;
1364a0292f23SGarrett Wollman 		case TCP_NOOPT:
1365cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
1366b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1367a0292f23SGarrett Wollman 			break;
1368a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1369cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
1370b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1371b8af5dfaSRobert Watson 			break;
1372b8af5dfaSRobert Watson 		case TCP_INFO:
1373b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
1374b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
1375a0292f23SGarrett Wollman 			break;
1376df8bae1dSRodney W. Grimes 		default:
1377df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1378df8bae1dSRodney W. Grimes 			break;
1379df8bae1dSRodney W. Grimes 		}
1380df8bae1dSRodney W. Grimes 		break;
1381df8bae1dSRodney W. Grimes 	}
1382623dce13SRobert Watson out:
1383f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1384df8bae1dSRodney W. Grimes 	return (error);
1385df8bae1dSRodney W. Grimes }
1386df8bae1dSRodney W. Grimes 
138726e30fbbSDavid Greenman /*
138826e30fbbSDavid Greenman  * tcp_sendspace and tcp_recvspace are the default send and receive window
138926e30fbbSDavid Greenman  * sizes, respectively.  These are obsolescent (this information should
139026e30fbbSDavid Greenman  * be set by the route).
139126e30fbbSDavid Greenman  */
139281e561cdSDavid E. O'Brien u_long	tcp_sendspace = 1024*32;
1393e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
13943d177f46SBill Fumerola     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
139581e561cdSDavid E. O'Brien u_long	tcp_recvspace = 1024*64;
1396e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
13973d177f46SBill Fumerola     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1398df8bae1dSRodney W. Grimes 
1399df8bae1dSRodney W. Grimes /*
1400df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1401df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1402df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1403df8bae1dSRodney W. Grimes  */
14040312fbe9SPoul-Henning Kamp static int
1405ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so)
1406df8bae1dSRodney W. Grimes {
1407ad3f9ab3SAndre Oppermann 	struct tcpcb *tp;
1408df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1409df8bae1dSRodney W. Grimes 	int error;
1410fb59c426SYoshinobu Inoue #ifdef INET6
14114a6a94d8SArchie Cobbs 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1412fb59c426SYoshinobu Inoue #endif
1413df8bae1dSRodney W. Grimes 
1414df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1415df8bae1dSRodney W. Grimes 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1416df8bae1dSRodney W. Grimes 		if (error)
1417df8bae1dSRodney W. Grimes 			return (error);
1418df8bae1dSRodney W. Grimes 	}
14196741ecf5SAndre Oppermann 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
14206741ecf5SAndre Oppermann 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1421f2de87feSRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
1422d915b280SStephan Uphoff 	error = in_pcballoc(so, &tcbinfo);
1423f2de87feSRobert Watson 	if (error) {
1424f2de87feSRobert Watson 		INP_INFO_WUNLOCK(&tcbinfo);
1425df8bae1dSRodney W. Grimes 		return (error);
1426f2de87feSRobert Watson 	}
1427df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1428fb59c426SYoshinobu Inoue #ifdef INET6
1429fb59c426SYoshinobu Inoue 	if (isipv6) {
1430fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1431fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1432fb59c426SYoshinobu Inoue 	}
1433fb59c426SYoshinobu Inoue 	else
1434fb59c426SYoshinobu Inoue #endif
1435cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1436df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1437623dce13SRobert Watson 	if (tp == NULL) {
1438fb59c426SYoshinobu Inoue #ifdef INET6
1439623dce13SRobert Watson 		if (isipv6) {
1440fb59c426SYoshinobu Inoue 			in6_pcbdetach(inp);
1441623dce13SRobert Watson 			in6_pcbfree(inp);
1442623dce13SRobert Watson 		} else {
1443fb59c426SYoshinobu Inoue #endif
1444df8bae1dSRodney W. Grimes 			in_pcbdetach(inp);
1445623dce13SRobert Watson 			in_pcbfree(inp);
1446623dce13SRobert Watson #ifdef INET6
1447623dce13SRobert Watson 		}
1448623dce13SRobert Watson #endif
1449f2de87feSRobert Watson 		INP_INFO_WUNLOCK(&tcbinfo);
1450df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1451df8bae1dSRodney W. Grimes 	}
1452df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
1453f2de87feSRobert Watson 	INP_UNLOCK(inp);
1454f2de87feSRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
1455df8bae1dSRodney W. Grimes 	return (0);
1456df8bae1dSRodney W. Grimes }
1457df8bae1dSRodney W. Grimes 
1458df8bae1dSRodney W. Grimes /*
1459df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1460df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1461df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1462df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1463df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1464df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1465df8bae1dSRodney W. Grimes  */
1466623dce13SRobert Watson static void
1467ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
1468df8bae1dSRodney W. Grimes {
1469e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
1470e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
1471e6e0b5ffSRobert Watson 
1472e6e0b5ffSRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1473e6e0b5ffSRobert Watson 	INP_LOCK_ASSERT(inp);
1474df8bae1dSRodney W. Grimes 
1475623dce13SRobert Watson 	/*
1476623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1477623dce13SRobert Watson 	 * socket is still open.
1478623dce13SRobert Watson 	 */
1479623dce13SRobert Watson 	if (tp->t_state < TCPS_ESTABLISHED) {
1480df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1481623dce13SRobert Watson 		KASSERT(tp != NULL,
1482623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
1483623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1484243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
1485623dce13SRobert Watson 		KASSERT(tp != NULL,
1486623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
1487623dce13SRobert Watson 	} else {
1488df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
1489df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
1490623dce13SRobert Watson 		tcp_usrclosed(tp);
1491953b5606SRobert Watson 		if (!(inp->inp_vflag & INP_DROPPED))
1492623dce13SRobert Watson 			tcp_output(tp);
1493df8bae1dSRodney W. Grimes 	}
1494df8bae1dSRodney W. Grimes }
1495df8bae1dSRodney W. Grimes 
1496df8bae1dSRodney W. Grimes /*
1497df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1498df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1499df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1500df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1501df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1502df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1503df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1504df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1505df8bae1dSRodney W. Grimes  */
1506623dce13SRobert Watson static void
1507ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
1508df8bae1dSRodney W. Grimes {
1509df8bae1dSRodney W. Grimes 
1510e6e0b5ffSRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1511e6e0b5ffSRobert Watson 	INP_LOCK_ASSERT(tp->t_inpcb);
1512e6e0b5ffSRobert Watson 
1513df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1514df8bae1dSRodney W. Grimes 	case TCPS_CLOSED:
1515df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
1516df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_CLOSED;
1517df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1518623dce13SRobert Watson 		/*
1519623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
1520623dce13SRobert Watson 		 * still open.
1521623dce13SRobert Watson 		 */
1522623dce13SRobert Watson 		KASSERT(tp != NULL,
1523623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
1524df8bae1dSRodney W. Grimes 		break;
1525df8bae1dSRodney W. Grimes 
1526a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
1527df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1528a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
1529a0292f23SGarrett Wollman 		break;
1530a0292f23SGarrett Wollman 
1531df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1532df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_FIN_WAIT_1;
1533df8bae1dSRodney W. Grimes 		break;
1534df8bae1dSRodney W. Grimes 
1535df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1536df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_LAST_ACK;
1537df8bae1dSRodney W. Grimes 		break;
1538df8bae1dSRodney W. Grimes 	}
1539abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
1540df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
1541abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
15427c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
15437c72af87SMohan Srinivasan 			int timeout;
15447c72af87SMohan Srinivasan 
15457c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
15467c72af87SMohan Srinivasan 			    tcp_finwait2_timeout : tcp_maxidle;
1547b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
1548b6239c4aSAndras Olah 		}
1549df8bae1dSRodney W. Grimes 	}
15507c72af87SMohan Srinivasan }
1551497057eeSRobert Watson 
1552497057eeSRobert Watson #ifdef DDB
1553497057eeSRobert Watson static void
1554497057eeSRobert Watson db_print_indent(int indent)
1555497057eeSRobert Watson {
1556497057eeSRobert Watson 	int i;
1557497057eeSRobert Watson 
1558497057eeSRobert Watson 	for (i = 0; i < indent; i++)
1559497057eeSRobert Watson 		db_printf(" ");
1560497057eeSRobert Watson }
1561497057eeSRobert Watson 
1562497057eeSRobert Watson static void
1563497057eeSRobert Watson db_print_tstate(int t_state)
1564497057eeSRobert Watson {
1565497057eeSRobert Watson 
1566497057eeSRobert Watson 	switch (t_state) {
1567497057eeSRobert Watson 	case TCPS_CLOSED:
1568497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
1569497057eeSRobert Watson 		return;
1570497057eeSRobert Watson 
1571497057eeSRobert Watson 	case TCPS_LISTEN:
1572497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
1573497057eeSRobert Watson 		return;
1574497057eeSRobert Watson 
1575497057eeSRobert Watson 	case TCPS_SYN_SENT:
1576497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
1577497057eeSRobert Watson 		return;
1578497057eeSRobert Watson 
1579497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
1580497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
1581497057eeSRobert Watson 		return;
1582497057eeSRobert Watson 
1583497057eeSRobert Watson 	case TCPS_ESTABLISHED:
1584497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
1585497057eeSRobert Watson 		return;
1586497057eeSRobert Watson 
1587497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
1588497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
1589497057eeSRobert Watson 		return;
1590497057eeSRobert Watson 
1591497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
1592497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
1593497057eeSRobert Watson 		return;
1594497057eeSRobert Watson 
1595497057eeSRobert Watson 	case TCPS_CLOSING:
1596497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
1597497057eeSRobert Watson 		return;
1598497057eeSRobert Watson 
1599497057eeSRobert Watson 	case TCPS_LAST_ACK:
1600497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
1601497057eeSRobert Watson 		return;
1602497057eeSRobert Watson 
1603497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
1604497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
1605497057eeSRobert Watson 		return;
1606497057eeSRobert Watson 
1607497057eeSRobert Watson 	case TCPS_TIME_WAIT:
1608497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
1609497057eeSRobert Watson 		return;
1610497057eeSRobert Watson 
1611497057eeSRobert Watson 	default:
1612497057eeSRobert Watson 		db_printf("unknown");
1613497057eeSRobert Watson 		return;
1614497057eeSRobert Watson 	}
1615497057eeSRobert Watson }
1616497057eeSRobert Watson 
1617497057eeSRobert Watson static void
1618497057eeSRobert Watson db_print_tflags(u_int t_flags)
1619497057eeSRobert Watson {
1620497057eeSRobert Watson 	int comma;
1621497057eeSRobert Watson 
1622497057eeSRobert Watson 	comma = 0;
1623497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
1624497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
1625497057eeSRobert Watson 		comma = 1;
1626497057eeSRobert Watson 	}
1627497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
1628497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
1629497057eeSRobert Watson 		comma = 1;
1630497057eeSRobert Watson 	}
1631497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
1632497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
1633497057eeSRobert Watson 		comma = 1;
1634497057eeSRobert Watson 	}
1635497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
1636497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
1637497057eeSRobert Watson 		comma = 1;
1638497057eeSRobert Watson 	}
1639497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
1640497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
1641497057eeSRobert Watson 		comma = 1;
1642497057eeSRobert Watson 	}
1643497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
1644497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1645497057eeSRobert Watson 		comma = 1;
1646497057eeSRobert Watson 	}
1647497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
1648497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1649497057eeSRobert Watson 		comma = 1;
1650497057eeSRobert Watson 	}
1651497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
1652497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1653497057eeSRobert Watson 		comma = 1;
1654497057eeSRobert Watson 	}
1655497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
1656497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1657497057eeSRobert Watson 		comma = 1;
1658497057eeSRobert Watson 	}
1659497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
1660497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1661497057eeSRobert Watson 		comma = 1;
1662497057eeSRobert Watson 	}
1663497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
1664497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1665497057eeSRobert Watson 		comma = 1;
1666497057eeSRobert Watson 	}
1667497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
1668497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1669497057eeSRobert Watson 		comma = 1;
1670497057eeSRobert Watson 	}
1671497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
1672497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
1673497057eeSRobert Watson 		comma = 1;
1674497057eeSRobert Watson 	}
1675497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
1676497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
1677497057eeSRobert Watson 		comma = 1;
1678497057eeSRobert Watson 	}
1679497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
1680497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1681497057eeSRobert Watson 		comma = 1;
1682497057eeSRobert Watson 	}
1683497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
1684497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1685497057eeSRobert Watson 		comma = 1;
1686497057eeSRobert Watson 	}
1687497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
1688497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1689497057eeSRobert Watson 		comma = 1;
1690497057eeSRobert Watson 	}
1691497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
1692497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1693497057eeSRobert Watson 		comma = 1;
1694497057eeSRobert Watson 	}
1695497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
1696497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1697497057eeSRobert Watson 		comma = 1;
1698497057eeSRobert Watson 	}
1699497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
1700497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1701497057eeSRobert Watson 		comma = 1;
1702497057eeSRobert Watson 	}
1703497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
1704497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1705497057eeSRobert Watson 		comma = 1;
1706497057eeSRobert Watson 	}
1707497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
1708497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1709497057eeSRobert Watson 		comma = 1;
1710497057eeSRobert Watson 	}
1711497057eeSRobert Watson 	if (t_flags & TF_TSO) {
1712497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
1713497057eeSRobert Watson 		comma = 1;
1714497057eeSRobert Watson 	}
1715497057eeSRobert Watson }
1716497057eeSRobert Watson 
1717497057eeSRobert Watson static void
1718497057eeSRobert Watson db_print_toobflags(char t_oobflags)
1719497057eeSRobert Watson {
1720497057eeSRobert Watson 	int comma;
1721497057eeSRobert Watson 
1722497057eeSRobert Watson 	comma = 0;
1723497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
1724497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1725497057eeSRobert Watson 		comma = 1;
1726497057eeSRobert Watson 	}
1727497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
1728497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1729497057eeSRobert Watson 		comma = 1;
1730497057eeSRobert Watson 	}
1731497057eeSRobert Watson }
1732497057eeSRobert Watson 
1733497057eeSRobert Watson static void
1734497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1735497057eeSRobert Watson {
1736497057eeSRobert Watson 
1737497057eeSRobert Watson 	db_print_indent(indent);
1738497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
1739497057eeSRobert Watson 
1740497057eeSRobert Watson 	indent += 2;
1741497057eeSRobert Watson 
1742497057eeSRobert Watson 	db_print_indent(indent);
1743497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
1744497057eeSRobert Watson 	   LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
1745497057eeSRobert Watson 
1746497057eeSRobert Watson 	db_print_indent(indent);
174785d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
1748e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
1749497057eeSRobert Watson 
1750497057eeSRobert Watson 	db_print_indent(indent);
1751e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
1752e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
1753497057eeSRobert Watson 
1754497057eeSRobert Watson 	db_print_indent(indent);
1755497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
1756497057eeSRobert Watson 	db_print_tstate(tp->t_state);
1757497057eeSRobert Watson 	db_printf(")\n");
1758497057eeSRobert Watson 
1759497057eeSRobert Watson 	db_print_indent(indent);
1760497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
1761497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
1762497057eeSRobert Watson 	db_printf(")\n");
1763497057eeSRobert Watson 
1764497057eeSRobert Watson 	db_print_indent(indent);
1765497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
1766497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
1767497057eeSRobert Watson 
1768497057eeSRobert Watson 	db_print_indent(indent);
1769497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
1770497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
1771497057eeSRobert Watson 
1772497057eeSRobert Watson 	db_print_indent(indent);
1773497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
1774497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
1775497057eeSRobert Watson 
1776497057eeSRobert Watson 	db_print_indent(indent);
1777497057eeSRobert Watson 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %lu   rcv_up: 0x%08x\n",
1778497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
1779497057eeSRobert Watson 
1780497057eeSRobert Watson 	db_print_indent(indent);
1781497057eeSRobert Watson 	db_printf("snd_wnd: %lu   snd_cwnd: %lu   snd_bwnd: %lu\n",
1782497057eeSRobert Watson 	   tp->snd_wnd, tp->snd_cwnd, tp->snd_bwnd);
1783497057eeSRobert Watson 
1784497057eeSRobert Watson 	db_print_indent(indent);
1785497057eeSRobert Watson 	db_printf("snd_ssthresh: %lu   snd_bandwidth: %lu   snd_recover: "
1786497057eeSRobert Watson 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_bandwidth,
1787497057eeSRobert Watson 	    tp->snd_recover);
1788497057eeSRobert Watson 
1789497057eeSRobert Watson 	db_print_indent(indent);
1790497057eeSRobert Watson 	db_printf("t_maxopd: %u   t_rcvtime: %lu   t_startime: %lu\n",
1791497057eeSRobert Watson 	    tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
1792497057eeSRobert Watson 
1793497057eeSRobert Watson 	db_print_indent(indent);
1794497057eeSRobert Watson 	db_printf("t_rttime: %d   t_rtsq: 0x%08x   t_bw_rtttime: %d\n",
1795497057eeSRobert Watson 	    tp->t_rtttime, tp->t_rtseq, tp->t_bw_rtttime);
1796497057eeSRobert Watson 
1797497057eeSRobert Watson 	db_print_indent(indent);
1798497057eeSRobert Watson 	db_printf("t_bw_rtseq: 0x%08x   t_rxtcur: %d   t_maxseg: %u   "
1799497057eeSRobert Watson 	    "t_srtt: %d\n", tp->t_bw_rtseq, tp->t_rxtcur, tp->t_maxseg,
1800497057eeSRobert Watson 	    tp->t_srtt);
1801497057eeSRobert Watson 
1802497057eeSRobert Watson 	db_print_indent(indent);
1803497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
1804497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
1805497057eeSRobert Watson 	    tp->t_rttbest);
1806497057eeSRobert Watson 
1807497057eeSRobert Watson 	db_print_indent(indent);
1808497057eeSRobert Watson 	db_printf("t_rttupdated: %lu   max_sndwnd: %lu   t_softerror: %d\n",
1809497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
1810497057eeSRobert Watson 
1811497057eeSRobert Watson 	db_print_indent(indent);
1812497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
1813497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
1814497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
1815497057eeSRobert Watson 
1816497057eeSRobert Watson 	db_print_indent(indent);
1817497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
1818497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
1819497057eeSRobert Watson 
1820497057eeSRobert Watson 	db_print_indent(indent);
18211a553740SAndre Oppermann 	db_printf("ts_recent: %u   ts_recent_age: %lu\n",
18221a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
1823497057eeSRobert Watson 
1824497057eeSRobert Watson 	db_print_indent(indent);
1825497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
1826497057eeSRobert Watson 	    "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
1827497057eeSRobert Watson 
1828497057eeSRobert Watson 	db_print_indent(indent);
1829497057eeSRobert Watson 	db_printf("snd_ssthresh_prev: %lu   snd_recover_prev: 0x%08x   "
1830497057eeSRobert Watson 	    "t_badrxtwin: %lu\n", tp->snd_ssthresh_prev,
1831497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
1832497057eeSRobert Watson 
1833497057eeSRobert Watson 	db_print_indent(indent);
18343529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
18353529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
1836497057eeSRobert Watson 
1837497057eeSRobert Watson 	db_print_indent(indent);
1838497057eeSRobert Watson 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
1839497057eeSRobert Watson 	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
1840497057eeSRobert Watson 
1841497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
1842497057eeSRobert Watson 
1843497057eeSRobert Watson 	db_print_indent(indent);
1844497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
1845497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
1846497057eeSRobert Watson }
1847497057eeSRobert Watson 
1848497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
1849497057eeSRobert Watson {
1850497057eeSRobert Watson 	struct tcpcb *tp;
1851497057eeSRobert Watson 
1852497057eeSRobert Watson 	if (!have_addr) {
1853497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
1854497057eeSRobert Watson 		return;
1855497057eeSRobert Watson 	}
1856497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
1857497057eeSRobert Watson 
1858497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
1859497057eeSRobert Watson }
1860497057eeSRobert Watson #endif
1861