xref: /freebsd/sys/netinet/tcp_usrreq.c (revision bc65987ade60fb8ce3ec6c7241cda809bcdc661e)
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
88bc65987aSKip Macy #include <netinet/tcp_offload.h>
89df8bae1dSRodney W. Grimes 
90df8bae1dSRodney W. Grimes /*
91df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
92df8bae1dSRodney W. Grimes  */
9356dc72c3SPawel Jakub Dawidek static int	tcp_attach(struct socket *);
944d77a549SAlfred Perlstein static int	tcp_connect(struct tcpcb *, struct sockaddr *,
954d77a549SAlfred Perlstein 		    struct thread *td);
96fb59c426SYoshinobu Inoue #ifdef INET6
974d77a549SAlfred Perlstein static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
984d77a549SAlfred Perlstein 		    struct thread *td);
99fb59c426SYoshinobu Inoue #endif /* INET6 */
100623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
101623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
102b8af5dfaSRobert Watson static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
1032c37256eSGarrett Wollman 
1042c37256eSGarrett Wollman #ifdef TCPDEBUG
1051db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1062c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
1074cc20ab1SSeigo Tanimura #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
1084cc20ab1SSeigo Tanimura 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1092c37256eSGarrett Wollman #else
1102c37256eSGarrett Wollman #define	TCPDEBUG0
1112c37256eSGarrett Wollman #define	TCPDEBUG1()
1122c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1132c37256eSGarrett Wollman #endif
1142c37256eSGarrett Wollman 
1152c37256eSGarrett Wollman /*
1162c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1172c37256eSGarrett Wollman  * and an internet control block.
1182c37256eSGarrett Wollman  */
1192c37256eSGarrett Wollman static int
120b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1212c37256eSGarrett Wollman {
122f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
123623dce13SRobert Watson 	struct tcpcb *tp = NULL;
124623dce13SRobert Watson 	int error;
1252c37256eSGarrett Wollman 	TCPDEBUG0;
1262c37256eSGarrett Wollman 
127623dce13SRobert Watson 	inp = sotoinpcb(so);
128623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1292c37256eSGarrett Wollman 	TCPDEBUG1();
1302c37256eSGarrett Wollman 
13156dc72c3SPawel Jakub Dawidek 	error = tcp_attach(so);
1322c37256eSGarrett Wollman 	if (error)
1332c37256eSGarrett Wollman 		goto out;
1342c37256eSGarrett Wollman 
1352c37256eSGarrett Wollman 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1363879597fSAndrey A. Chernov 		so->so_linger = TCP_LINGERTIME;
137f76fcf6dSJeffrey Hsu 
138f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
139f76fcf6dSJeffrey Hsu 	tp = intotcpcb(inp);
1402c37256eSGarrett Wollman out:
1412c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
1422c37256eSGarrett Wollman 	return error;
1432c37256eSGarrett Wollman }
1442c37256eSGarrett Wollman 
1452c37256eSGarrett Wollman /*
146a152f8a3SRobert Watson  * tcp_detach is called when the socket layer loses its final reference
147a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
148a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
149a152f8a3SRobert Watson  * inpcb state: time wait.
150c78cbc7bSRobert Watson  *
151a152f8a3SRobert Watson  * This function can probably be re-absorbed back into tcp_usr_detach() now
152a152f8a3SRobert Watson  * that there is a single detach path.
1532c37256eSGarrett Wollman  */
154bc725eafSRobert Watson static void
155c78cbc7bSRobert Watson tcp_detach(struct socket *so, struct inpcb *inp)
1562c37256eSGarrett Wollman {
1572c37256eSGarrett Wollman 	struct tcpcb *tp;
158623dce13SRobert Watson #ifdef INET6
159623dce13SRobert Watson 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
160623dce13SRobert Watson #endif
1612c37256eSGarrett Wollman 
162c78cbc7bSRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
163c78cbc7bSRobert Watson 	INP_LOCK_ASSERT(inp);
164623dce13SRobert Watson 
165c78cbc7bSRobert Watson 	KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
166c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
167953b5606SRobert Watson 
168a152f8a3SRobert Watson 	tp = intotcpcb(inp);
169a152f8a3SRobert Watson 
170623dce13SRobert Watson 	if (inp->inp_vflag & INP_TIMEWAIT) {
171623dce13SRobert Watson 		/*
172a152f8a3SRobert Watson 		 * There are two cases to handle: one in which the time wait
173a152f8a3SRobert Watson 		 * state is being discarded (INP_DROPPED), and one in which
174a152f8a3SRobert Watson 		 * this connection will remain in timewait.  In the former,
175a152f8a3SRobert Watson 		 * it is time to discard all state (except tcptw, which has
176a152f8a3SRobert Watson 		 * already been discarded by the timewait close code, which
177a152f8a3SRobert Watson 		 * should be further up the call stack somewhere).  In the
178a152f8a3SRobert Watson 		 * latter case, we detach from the socket, but leave the pcb
179a152f8a3SRobert Watson 		 * present until timewait ends.
180623dce13SRobert Watson 		 *
181a152f8a3SRobert Watson 		 * XXXRW: Would it be cleaner to free the tcptw here?
182623dce13SRobert Watson 		 */
183a152f8a3SRobert Watson 		if (inp->inp_vflag & INP_DROPPED) {
184a152f8a3SRobert Watson 			KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
185a152f8a3SRobert Watson 			    "INP_DROPPED && tp != NULL"));
186623dce13SRobert Watson #ifdef INET6
187623dce13SRobert Watson 			if (isipv6) {
188623dce13SRobert Watson 				in6_pcbdetach(inp);
189623dce13SRobert Watson 				in6_pcbfree(inp);
190623dce13SRobert Watson 			} else {
191623dce13SRobert Watson #endif
192623dce13SRobert Watson 				in_pcbdetach(inp);
193623dce13SRobert Watson 				in_pcbfree(inp);
194623dce13SRobert Watson #ifdef INET6
195623dce13SRobert Watson 			}
196623dce13SRobert Watson #endif
197623dce13SRobert Watson 		} else {
198623dce13SRobert Watson #ifdef INET6
199623dce13SRobert Watson 			if (isipv6)
200623dce13SRobert Watson 				in6_pcbdetach(inp);
201623dce13SRobert Watson 			else
202623dce13SRobert Watson #endif
203623dce13SRobert Watson 				in_pcbdetach(inp);
204f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
205623dce13SRobert Watson 		}
206623dce13SRobert Watson 	} else {
207e6e65783SRobert Watson 		/*
208a152f8a3SRobert Watson 		 * If the connection is not in timewait, we consider two
209a152f8a3SRobert Watson 		 * two conditions: one in which no further processing is
210a152f8a3SRobert Watson 		 * necessary (dropped || embryonic), and one in which TCP is
211a152f8a3SRobert Watson 		 * not yet done, but no longer requires the socket, so the
212a152f8a3SRobert Watson 		 * pcb will persist for the time being.
213a152f8a3SRobert Watson 		 *
214a152f8a3SRobert Watson 		 * XXXRW: Does the second case still occur?
215e6e65783SRobert Watson 		 */
216623dce13SRobert Watson 		if (inp->inp_vflag & INP_DROPPED ||
217623dce13SRobert Watson 		    tp->t_state < TCPS_SYN_SENT) {
218623dce13SRobert Watson 			tcp_discardcb(tp);
219623dce13SRobert Watson #ifdef INET6
220623dce13SRobert Watson 			if (isipv6) {
221a152f8a3SRobert Watson 				in6_pcbdetach(inp);
222a152f8a3SRobert Watson 				in6_pcbfree(inp);
223623dce13SRobert Watson 			} else {
224623dce13SRobert Watson #endif
225623dce13SRobert Watson 				in_pcbdetach(inp);
226623dce13SRobert Watson 				in_pcbfree(inp);
227623dce13SRobert Watson #ifdef INET6
228623dce13SRobert Watson 			}
229623dce13SRobert Watson #endif
230623dce13SRobert Watson 		} else {
231a152f8a3SRobert Watson #ifdef INET6
232a152f8a3SRobert Watson 			if (isipv6)
233a152f8a3SRobert Watson 				in6_pcbdetach(inp);
234a152f8a3SRobert Watson 			else
235a152f8a3SRobert Watson #endif
236a152f8a3SRobert Watson 				in_pcbdetach(inp);
237623dce13SRobert Watson 		}
238623dce13SRobert Watson 	}
239c78cbc7bSRobert Watson }
240c78cbc7bSRobert Watson 
241c78cbc7bSRobert Watson /*
242c78cbc7bSRobert Watson  * pru_detach() detaches the TCP protocol from the socket.
243c78cbc7bSRobert Watson  * If the protocol state is non-embryonic, then can't
244c78cbc7bSRobert Watson  * do this directly: have to initiate a pru_disconnect(),
245c78cbc7bSRobert Watson  * which may finish later; embryonic TCB's can just
246c78cbc7bSRobert Watson  * be discarded here.
247c78cbc7bSRobert Watson  */
248c78cbc7bSRobert Watson static void
249c78cbc7bSRobert Watson tcp_usr_detach(struct socket *so)
250c78cbc7bSRobert Watson {
251c78cbc7bSRobert Watson 	struct inpcb *inp;
252c78cbc7bSRobert Watson 
253c78cbc7bSRobert Watson 	inp = sotoinpcb(so);
254c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
255c78cbc7bSRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
256c78cbc7bSRobert Watson 	INP_LOCK(inp);
257c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
258c78cbc7bSRobert Watson 	    ("tcp_usr_detach: inp_socket == NULL"));
259c78cbc7bSRobert Watson 	tcp_detach(so, inp);
260f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&tcbinfo);
2612c37256eSGarrett Wollman }
2622c37256eSGarrett Wollman 
2632c37256eSGarrett Wollman /*
2642c37256eSGarrett Wollman  * Give the socket an address.
2652c37256eSGarrett Wollman  */
2662c37256eSGarrett Wollman static int
267b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2682c37256eSGarrett Wollman {
2692c37256eSGarrett Wollman 	int error = 0;
270f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
271623dce13SRobert Watson 	struct tcpcb *tp = NULL;
2722c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
2732c37256eSGarrett Wollman 
27452710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
27552710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sinp))
27652710de1SPawel Jakub Dawidek 		return (EINVAL);
2772c37256eSGarrett Wollman 	/*
2782c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2792c37256eSGarrett Wollman 	 * to them.
2802c37256eSGarrett Wollman 	 */
2812c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
28252710de1SPawel Jakub Dawidek 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
28352710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
28452710de1SPawel Jakub Dawidek 
285623dce13SRobert Watson 	TCPDEBUG0;
286623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
287623dce13SRobert Watson 	inp = sotoinpcb(so);
288623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
289623dce13SRobert Watson 	INP_LOCK(inp);
290623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
291623dce13SRobert Watson 		error = EINVAL;
2922c37256eSGarrett Wollman 		goto out;
293623dce13SRobert Watson 	}
294623dce13SRobert Watson 	tp = intotcpcb(inp);
295623dce13SRobert Watson 	TCPDEBUG1();
296623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
297623dce13SRobert Watson out:
298623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
299623dce13SRobert Watson 	INP_UNLOCK(inp);
300623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
301623dce13SRobert Watson 
302623dce13SRobert Watson 	return (error);
3032c37256eSGarrett Wollman }
3042c37256eSGarrett Wollman 
305fb59c426SYoshinobu Inoue #ifdef INET6
306fb59c426SYoshinobu Inoue static int
307b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
308fb59c426SYoshinobu Inoue {
309fb59c426SYoshinobu Inoue 	int error = 0;
310f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
311623dce13SRobert Watson 	struct tcpcb *tp = NULL;
312fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
313fb59c426SYoshinobu Inoue 
31452710de1SPawel Jakub Dawidek 	sin6p = (struct sockaddr_in6 *)nam;
31552710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sin6p))
31652710de1SPawel Jakub Dawidek 		return (EINVAL);
317fb59c426SYoshinobu Inoue 	/*
318fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
319fb59c426SYoshinobu Inoue 	 * to them.
320fb59c426SYoshinobu Inoue 	 */
321fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6 &&
32252710de1SPawel Jakub Dawidek 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
32352710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
32452710de1SPawel Jakub Dawidek 
325623dce13SRobert Watson 	TCPDEBUG0;
326623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
327623dce13SRobert Watson 	inp = sotoinpcb(so);
328623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
329623dce13SRobert Watson 	INP_LOCK(inp);
330623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
331623dce13SRobert Watson 		error = EINVAL;
332623dce13SRobert Watson 		goto out;
333623dce13SRobert Watson 	}
334623dce13SRobert Watson 	tp = intotcpcb(inp);
335623dce13SRobert Watson 	TCPDEBUG1();
336fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
337fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
33866ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
339fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
340fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
341fb59c426SYoshinobu Inoue 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
342fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
343fb59c426SYoshinobu Inoue 
344fb59c426SYoshinobu Inoue 			in6_sin6_2_sin(&sin, sin6p);
345fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
346fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
347b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
348b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
349fb59c426SYoshinobu Inoue 			goto out;
350fb59c426SYoshinobu Inoue 		}
351fb59c426SYoshinobu Inoue 	}
352b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
353623dce13SRobert Watson out:
354623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
355623dce13SRobert Watson 	INP_UNLOCK(inp);
356623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
357623dce13SRobert Watson 	return (error);
358fb59c426SYoshinobu Inoue }
359fb59c426SYoshinobu Inoue #endif /* INET6 */
360fb59c426SYoshinobu Inoue 
3612c37256eSGarrett Wollman /*
3622c37256eSGarrett Wollman  * Prepare to accept connections.
3632c37256eSGarrett Wollman  */
3642c37256eSGarrett Wollman static int
365d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
3662c37256eSGarrett Wollman {
3672c37256eSGarrett Wollman 	int error = 0;
368f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
369623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3702c37256eSGarrett Wollman 
371623dce13SRobert Watson 	TCPDEBUG0;
372623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
373623dce13SRobert Watson 	inp = sotoinpcb(so);
374623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
375623dce13SRobert Watson 	INP_LOCK(inp);
376623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
377623dce13SRobert Watson 		error = EINVAL;
378623dce13SRobert Watson 		goto out;
379623dce13SRobert Watson 	}
380623dce13SRobert Watson 	tp = intotcpcb(inp);
381623dce13SRobert Watson 	TCPDEBUG1();
3820daccb9cSRobert Watson 	SOCK_LOCK(so);
3830daccb9cSRobert Watson 	error = solisten_proto_check(so);
3840daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0)
385b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
3860daccb9cSRobert Watson 	if (error == 0) {
3872c37256eSGarrett Wollman 		tp->t_state = TCPS_LISTEN;
388d374e81eSRobert Watson 		solisten_proto(so, backlog);
389bc65987aSKip Macy 		tcp_offload_listen_open(tp);
3900daccb9cSRobert Watson 	}
3910daccb9cSRobert Watson 	SOCK_UNLOCK(so);
392623dce13SRobert Watson 
393623dce13SRobert Watson out:
394623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
395623dce13SRobert Watson 	INP_UNLOCK(inp);
396623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
397623dce13SRobert Watson 	return (error);
3982c37256eSGarrett Wollman }
3992c37256eSGarrett Wollman 
400fb59c426SYoshinobu Inoue #ifdef INET6
401fb59c426SYoshinobu Inoue static int
402d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
403fb59c426SYoshinobu Inoue {
404fb59c426SYoshinobu Inoue 	int error = 0;
405f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
406623dce13SRobert Watson 	struct tcpcb *tp = NULL;
407fb59c426SYoshinobu Inoue 
408623dce13SRobert Watson 	TCPDEBUG0;
409623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
410623dce13SRobert Watson 	inp = sotoinpcb(so);
411623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
412623dce13SRobert Watson 	INP_LOCK(inp);
413623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
414623dce13SRobert Watson 		error = EINVAL;
415623dce13SRobert Watson 		goto out;
416623dce13SRobert Watson 	}
417623dce13SRobert Watson 	tp = intotcpcb(inp);
418623dce13SRobert Watson 	TCPDEBUG1();
4190daccb9cSRobert Watson 	SOCK_LOCK(so);
4200daccb9cSRobert Watson 	error = solisten_proto_check(so);
4210daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0) {
422fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
42366ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
424fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
425b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
426fb59c426SYoshinobu Inoue 	}
4270daccb9cSRobert Watson 	if (error == 0) {
428fb59c426SYoshinobu Inoue 		tp->t_state = TCPS_LISTEN;
429d374e81eSRobert Watson 		solisten_proto(so, backlog);
4300daccb9cSRobert Watson 	}
4310daccb9cSRobert Watson 	SOCK_UNLOCK(so);
432623dce13SRobert Watson 
433623dce13SRobert Watson out:
434623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
435623dce13SRobert Watson 	INP_UNLOCK(inp);
436623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
437623dce13SRobert Watson 	return (error);
438fb59c426SYoshinobu Inoue }
439fb59c426SYoshinobu Inoue #endif /* INET6 */
440fb59c426SYoshinobu Inoue 
4412c37256eSGarrett Wollman /*
4422c37256eSGarrett Wollman  * Initiate connection to peer.
4432c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
4442c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
4452c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
4462c37256eSGarrett Wollman  * Send initial segment on connection.
4472c37256eSGarrett Wollman  */
4482c37256eSGarrett Wollman static int
449b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
4502c37256eSGarrett Wollman {
4512c37256eSGarrett Wollman 	int error = 0;
452f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
453623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4542c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
4552c37256eSGarrett Wollman 
45657bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
457e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
458e29ef13fSDon Lewis 		return (EINVAL);
45952710de1SPawel Jakub Dawidek 	/*
46052710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
46152710de1SPawel Jakub Dawidek 	 */
4622c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
46352710de1SPawel Jakub Dawidek 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
46452710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
465812d8653SSam Leffler 	if (jailed(td->td_ucred))
466a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
46775c13541SPoul-Henning Kamp 
468623dce13SRobert Watson 	TCPDEBUG0;
469623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
470623dce13SRobert Watson 	inp = sotoinpcb(so);
471623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
472623dce13SRobert Watson 	INP_LOCK(inp);
473623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
474623dce13SRobert Watson 		error = EINVAL;
475623dce13SRobert Watson 		goto out;
476623dce13SRobert Watson 	}
477623dce13SRobert Watson 	tp = intotcpcb(inp);
478623dce13SRobert Watson 	TCPDEBUG1();
479b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
4802c37256eSGarrett Wollman 		goto out;
481bc65987aSKip Macy 	error = tcp_output_connect(so, nam);
482623dce13SRobert Watson out:
483623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
484623dce13SRobert Watson 	INP_UNLOCK(inp);
485623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
486623dce13SRobert Watson 	return (error);
4872c37256eSGarrett Wollman }
4882c37256eSGarrett Wollman 
489fb59c426SYoshinobu Inoue #ifdef INET6
490fb59c426SYoshinobu Inoue static int
491b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
492fb59c426SYoshinobu Inoue {
493fb59c426SYoshinobu Inoue 	int error = 0;
494f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
495623dce13SRobert Watson 	struct tcpcb *tp = NULL;
496fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
497623dce13SRobert Watson 
498623dce13SRobert Watson 	TCPDEBUG0;
499fb59c426SYoshinobu Inoue 
500fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
501e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sin6p))
502e29ef13fSDon Lewis 		return (EINVAL);
50352710de1SPawel Jakub Dawidek 	/*
50452710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
50552710de1SPawel Jakub Dawidek 	 */
506fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6
50752710de1SPawel Jakub Dawidek 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
50852710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
509fb59c426SYoshinobu Inoue 
510623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
511623dce13SRobert Watson 	inp = sotoinpcb(so);
512623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
513623dce13SRobert Watson 	INP_LOCK(inp);
514623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
515623dce13SRobert Watson 		error = EINVAL;
516623dce13SRobert Watson 		goto out;
517623dce13SRobert Watson 	}
518623dce13SRobert Watson 	tp = intotcpcb(inp);
519623dce13SRobert Watson 	TCPDEBUG1();
52033841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
521fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
522fb59c426SYoshinobu Inoue 
523d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
524d46a5312SMaxim Konovalov 			error = EINVAL;
525d46a5312SMaxim Konovalov 			goto out;
526d46a5312SMaxim Konovalov 		}
52733841545SHajimu UMEMOTO 
528fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
529fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
530fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
531b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
532fb59c426SYoshinobu Inoue 			goto out;
533bc65987aSKip Macy 		error = tcp_output_connect(so, nam);
534fb59c426SYoshinobu Inoue 		goto out;
535fb59c426SYoshinobu Inoue 	}
536fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
537fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
538b7d6d952SHajimu UMEMOTO 	inp->inp_inc.inc_isipv6 = 1;
539b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
540fb59c426SYoshinobu Inoue 		goto out;
541bc65987aSKip Macy 	error = tcp_output_connect(so, nam);
542623dce13SRobert Watson 
543623dce13SRobert Watson out:
544623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
545623dce13SRobert Watson 	INP_UNLOCK(inp);
546623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
547623dce13SRobert Watson 	return (error);
548fb59c426SYoshinobu Inoue }
549fb59c426SYoshinobu Inoue #endif /* INET6 */
550fb59c426SYoshinobu Inoue 
5512c37256eSGarrett Wollman /*
5522c37256eSGarrett Wollman  * Initiate disconnect from peer.
5532c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
5542c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
5552c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
5562c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
5572c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
5582c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
5592c37256eSGarrett Wollman  *
5602c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
5612c37256eSGarrett Wollman  */
5622c37256eSGarrett Wollman static int
5632c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
5642c37256eSGarrett Wollman {
565f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
566623dce13SRobert Watson 	struct tcpcb *tp = NULL;
567623dce13SRobert Watson 	int error = 0;
5682c37256eSGarrett Wollman 
569623dce13SRobert Watson 	TCPDEBUG0;
570623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
571623dce13SRobert Watson 	inp = sotoinpcb(so);
572623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
573623dce13SRobert Watson 	INP_LOCK(inp);
574623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
57521367f63SSam Leffler 		error = ECONNRESET;
576623dce13SRobert Watson 		goto out;
577623dce13SRobert Watson 	}
578623dce13SRobert Watson 	tp = intotcpcb(inp);
579623dce13SRobert Watson 	TCPDEBUG1();
580623dce13SRobert Watson 	tcp_disconnect(tp);
581623dce13SRobert Watson out:
582623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
583623dce13SRobert Watson 	INP_UNLOCK(inp);
584623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
585623dce13SRobert Watson 	return (error);
5862c37256eSGarrett Wollman }
5872c37256eSGarrett Wollman 
5882c37256eSGarrett Wollman /*
5892c37256eSGarrett Wollman  * Accept a connection.  Essentially all the work is
5902c37256eSGarrett Wollman  * done at higher levels; just return the address
5912c37256eSGarrett Wollman  * of the peer, storing through addr.
5922c37256eSGarrett Wollman  */
5932c37256eSGarrett Wollman static int
59457bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
5952c37256eSGarrett Wollman {
5962c37256eSGarrett Wollman 	int error = 0;
597f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
5981db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
59926ef6ac4SDon Lewis 	struct in_addr addr;
60026ef6ac4SDon Lewis 	in_port_t port = 0;
6011db24ffbSJonathan Lemon 	TCPDEBUG0;
6022c37256eSGarrett Wollman 
6033d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
6043d2d3ef4SRobert Watson 		return (ECONNABORTED);
605f76fcf6dSJeffrey Hsu 
606f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
607623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
608f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
609623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
6103d2d3ef4SRobert Watson 		error = ECONNABORTED;
611623dce13SRobert Watson 		goto out;
612623dce13SRobert Watson 	}
6131db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
6141db24ffbSJonathan Lemon 	TCPDEBUG1();
615f76fcf6dSJeffrey Hsu 
616f76fcf6dSJeffrey Hsu 	/*
61754d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
61826ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
61926ef6ac4SDon Lewis 	 * release the lock.
620f76fcf6dSJeffrey Hsu 	 */
62126ef6ac4SDon Lewis 	port = inp->inp_fport;
62226ef6ac4SDon Lewis 	addr = inp->inp_faddr;
623f76fcf6dSJeffrey Hsu 
624623dce13SRobert Watson out:
625623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
62626ef6ac4SDon Lewis 	INP_UNLOCK(inp);
62726ef6ac4SDon Lewis 	if (error == 0)
62826ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
62926ef6ac4SDon Lewis 	return error;
6302c37256eSGarrett Wollman }
6312c37256eSGarrett Wollman 
632fb59c426SYoshinobu Inoue #ifdef INET6
633fb59c426SYoshinobu Inoue static int
634fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
635fb59c426SYoshinobu Inoue {
636f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
637fb59c426SYoshinobu Inoue 	int error = 0;
6381db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
63926ef6ac4SDon Lewis 	struct in_addr addr;
64026ef6ac4SDon Lewis 	struct in6_addr addr6;
64126ef6ac4SDon Lewis 	in_port_t port = 0;
64226ef6ac4SDon Lewis 	int v4 = 0;
6431db24ffbSJonathan Lemon 	TCPDEBUG0;
644fb59c426SYoshinobu Inoue 
645b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
646b4470c16SRobert Watson 		return (ECONNABORTED);
647f76fcf6dSJeffrey Hsu 
648f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
649623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
650f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
651623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
65221367f63SSam Leffler 		error = ECONNABORTED;
653623dce13SRobert Watson 		goto out;
654623dce13SRobert Watson 	}
6551db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
6561db24ffbSJonathan Lemon 	TCPDEBUG1();
657623dce13SRobert Watson 
65826ef6ac4SDon Lewis 	/*
65926ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
66026ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
66126ef6ac4SDon Lewis 	 * release the lock.
66226ef6ac4SDon Lewis 	 */
66326ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
66426ef6ac4SDon Lewis 		v4 = 1;
66526ef6ac4SDon Lewis 		port = inp->inp_fport;
66626ef6ac4SDon Lewis 		addr = inp->inp_faddr;
66726ef6ac4SDon Lewis 	} else {
66826ef6ac4SDon Lewis 		port = inp->inp_fport;
66926ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
67026ef6ac4SDon Lewis 	}
67126ef6ac4SDon Lewis 
672623dce13SRobert Watson out:
673623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
67426ef6ac4SDon Lewis 	INP_UNLOCK(inp);
67526ef6ac4SDon Lewis 	if (error == 0) {
67626ef6ac4SDon Lewis 		if (v4)
67726ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
67826ef6ac4SDon Lewis 		else
67926ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
68026ef6ac4SDon Lewis 	}
68126ef6ac4SDon Lewis 	return error;
682fb59c426SYoshinobu Inoue }
683fb59c426SYoshinobu Inoue #endif /* INET6 */
684f76fcf6dSJeffrey Hsu 
685f76fcf6dSJeffrey Hsu /*
6862c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
6872c37256eSGarrett Wollman  */
6882c37256eSGarrett Wollman static int
6892c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
6902c37256eSGarrett Wollman {
6912c37256eSGarrett Wollman 	int error = 0;
692f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
693623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6942c37256eSGarrett Wollman 
695623dce13SRobert Watson 	TCPDEBUG0;
696623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
697623dce13SRobert Watson 	inp = sotoinpcb(so);
698623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
699623dce13SRobert Watson 	INP_LOCK(inp);
700623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
70121367f63SSam Leffler 		error = ECONNRESET;
702623dce13SRobert Watson 		goto out;
703623dce13SRobert Watson 	}
704623dce13SRobert Watson 	tp = intotcpcb(inp);
705623dce13SRobert Watson 	TCPDEBUG1();
7062c37256eSGarrett Wollman 	socantsendmore(so);
707623dce13SRobert Watson 	tcp_usrclosed(tp);
708bc65987aSKip Macy 	error = tcp_output_disconnect(tp);
709623dce13SRobert Watson 
710623dce13SRobert Watson out:
711623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
712623dce13SRobert Watson 	INP_UNLOCK(inp);
713623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
714623dce13SRobert Watson 
715623dce13SRobert Watson 	return (error);
7162c37256eSGarrett Wollman }
7172c37256eSGarrett Wollman 
7182c37256eSGarrett Wollman /*
7192c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
7202c37256eSGarrett Wollman  */
7212c37256eSGarrett Wollman static int
7222c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
7232c37256eSGarrett Wollman {
724f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
725623dce13SRobert Watson 	struct tcpcb *tp = NULL;
726623dce13SRobert Watson 	int error = 0;
7272c37256eSGarrett Wollman 
728623dce13SRobert Watson 	TCPDEBUG0;
729623dce13SRobert Watson 	inp = sotoinpcb(so);
730623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
731623dce13SRobert Watson 	INP_LOCK(inp);
732623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
73321367f63SSam Leffler 		error = ECONNRESET;
734623dce13SRobert Watson 		goto out;
735623dce13SRobert Watson 	}
736623dce13SRobert Watson 	tp = intotcpcb(inp);
737623dce13SRobert Watson 	TCPDEBUG1();
738bc65987aSKip Macy 	tcp_output_rcvd(tp);
739623dce13SRobert Watson 
740623dce13SRobert Watson out:
741623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
742623dce13SRobert Watson 	INP_UNLOCK(inp);
743623dce13SRobert Watson 	return (error);
7442c37256eSGarrett Wollman }
7452c37256eSGarrett Wollman 
7462c37256eSGarrett Wollman /*
7472c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
7489c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
7499c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
7509c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
7519c9906e9SPeter Wemm  * generally are caller-frees.
7522c37256eSGarrett Wollman  */
7532c37256eSGarrett Wollman static int
75457bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
755b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
7562c37256eSGarrett Wollman {
7572c37256eSGarrett Wollman 	int error = 0;
758f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
759623dce13SRobert Watson 	struct tcpcb *tp = NULL;
760623dce13SRobert Watson 	int headlocked = 0;
761fb59c426SYoshinobu Inoue #ifdef INET6
762fb59c426SYoshinobu Inoue 	int isipv6;
763fb59c426SYoshinobu Inoue #endif
7649c9906e9SPeter Wemm 	TCPDEBUG0;
7652c37256eSGarrett Wollman 
766f76fcf6dSJeffrey Hsu 	/*
767623dce13SRobert Watson 	 * We require the pcbinfo lock in two cases:
768623dce13SRobert Watson 	 *
769623dce13SRobert Watson 	 * (1) An implied connect is taking place, which can result in
770623dce13SRobert Watson 	 *     binding IPs and ports and hence modification of the pcb hash
771623dce13SRobert Watson 	 *     chains.
772623dce13SRobert Watson 	 *
773623dce13SRobert Watson 	 * (2) PRUS_EOF is set, resulting in explicit close on the send.
774f76fcf6dSJeffrey Hsu 	 */
775623dce13SRobert Watson 	if ((nam != NULL) || (flags & PRUS_EOF)) {
776f76fcf6dSJeffrey Hsu 		INP_INFO_WLOCK(&tcbinfo);
777623dce13SRobert Watson 		headlocked = 1;
778623dce13SRobert Watson 	}
779f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
780623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
781623dce13SRobert Watson 	INP_LOCK(inp);
782623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
7837ff0b850SAndre Oppermann 		if (control)
7847ff0b850SAndre Oppermann 			m_freem(control);
7857ff0b850SAndre Oppermann 		if (m)
7867ff0b850SAndre Oppermann 			m_freem(m);
78721367f63SSam Leffler 		error = ECONNRESET;
7889c9906e9SPeter Wemm 		goto out;
7899c9906e9SPeter Wemm 	}
790fb59c426SYoshinobu Inoue #ifdef INET6
791fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
792fb59c426SYoshinobu Inoue #endif /* INET6 */
7939c9906e9SPeter Wemm 	tp = intotcpcb(inp);
7949c9906e9SPeter Wemm 	TCPDEBUG1();
7959c9906e9SPeter Wemm 	if (control) {
7969c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
7979c9906e9SPeter Wemm 		if (control->m_len) {
7989c9906e9SPeter Wemm 			m_freem(control);
7992c37256eSGarrett Wollman 			if (m)
8002c37256eSGarrett Wollman 				m_freem(m);
801744f87eaSDavid Greenman 			error = EINVAL;
802744f87eaSDavid Greenman 			goto out;
8032c37256eSGarrett Wollman 		}
8049c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
8059c9906e9SPeter Wemm 	}
8062c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
807395bb186SSam Leffler 		sbappendstream(&so->so_snd, m);
8082c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
8092c37256eSGarrett Wollman 			/*
8102c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
8112c37256eSGarrett Wollman 			 * initialize window to default value, and
8122c37256eSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
8132c37256eSGarrett Wollman 			 * MSS.
8142c37256eSGarrett Wollman 			 */
815623dce13SRobert Watson 			INP_INFO_WLOCK_ASSERT(&tcbinfo);
816fb59c426SYoshinobu Inoue #ifdef INET6
817fb59c426SYoshinobu Inoue 			if (isipv6)
818b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
819fb59c426SYoshinobu Inoue 			else
820fb59c426SYoshinobu Inoue #endif /* INET6 */
821b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
8222c37256eSGarrett Wollman 			if (error)
8232c37256eSGarrett Wollman 				goto out;
8242c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
8252c37256eSGarrett Wollman 			tcp_mss(tp, -1);
8262c37256eSGarrett Wollman 		}
8272c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
8282c37256eSGarrett Wollman 			/*
8292c37256eSGarrett Wollman 			 * Close the send side of the connection after
8302c37256eSGarrett Wollman 			 * the data is sent.
8312c37256eSGarrett Wollman 			 */
832623dce13SRobert Watson 			INP_INFO_WLOCK_ASSERT(&tcbinfo);
8332c37256eSGarrett Wollman 			socantsendmore(so);
834623dce13SRobert Watson 			tcp_usrclosed(tp);
8352c37256eSGarrett Wollman 		}
836623dce13SRobert Watson 		if (headlocked) {
837d1401c90SRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
838623dce13SRobert Watson 			headlocked = 0;
839623dce13SRobert Watson 		}
840b0acefa8SBill Fenner 		if (tp != NULL) {
841b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
842b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
843bc65987aSKip Macy 			error = tcp_output_send(tp);
844b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
845b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
846b0acefa8SBill Fenner 		}
8472c37256eSGarrett Wollman 	} else {
848623dce13SRobert Watson 		/*
849623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
850623dce13SRobert Watson 		 */
851d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
8522c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
853d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
8542c37256eSGarrett Wollman 			m_freem(m);
8552c37256eSGarrett Wollman 			error = ENOBUFS;
8562c37256eSGarrett Wollman 			goto out;
8572c37256eSGarrett Wollman 		}
8582c37256eSGarrett Wollman 		/*
8592c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
8602c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
8612c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
8622c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
8632c37256eSGarrett Wollman 		 * of data past the urgent section.
8642c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
8652c37256eSGarrett Wollman 		 */
866d2bc35abSRobert Watson 		sbappendstream_locked(&so->so_snd, m);
867d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
868ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
869ef53690bSGarrett Wollman 			/*
870ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
871ef53690bSGarrett Wollman 			 * initialize window to default value, and
872ef53690bSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
873ef53690bSGarrett Wollman 			 * MSS.
874ef53690bSGarrett Wollman 			 */
875623dce13SRobert Watson 			INP_INFO_WLOCK_ASSERT(&tcbinfo);
876fb59c426SYoshinobu Inoue #ifdef INET6
877fb59c426SYoshinobu Inoue 			if (isipv6)
878b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
879fb59c426SYoshinobu Inoue 			else
880fb59c426SYoshinobu Inoue #endif /* INET6 */
881b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
882ef53690bSGarrett Wollman 			if (error)
883ef53690bSGarrett Wollman 				goto out;
884ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
885ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
886d1401c90SRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
887623dce13SRobert Watson 			headlocked = 0;
888623dce13SRobert Watson 		} else if (nam) {
889623dce13SRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
890623dce13SRobert Watson 			headlocked = 0;
891623dce13SRobert Watson 		}
8922c37256eSGarrett Wollman 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
8932cdbfa66SPaul Saab 		tp->t_flags |= TF_FORCEDATA;
894bc65987aSKip Macy 		error = tcp_output_send(tp);
8952cdbfa66SPaul Saab 		tp->t_flags &= ~TF_FORCEDATA;
8962c37256eSGarrett Wollman 	}
897d1401c90SRobert Watson out:
898d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
8992c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
900d1401c90SRobert Watson 	INP_UNLOCK(inp);
901623dce13SRobert Watson 	if (headlocked)
902d1401c90SRobert Watson 		INP_INFO_WUNLOCK(&tcbinfo);
90373fddedaSPeter Grehan 	return (error);
9042c37256eSGarrett Wollman }
9052c37256eSGarrett Wollman 
9062c37256eSGarrett Wollman /*
907a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
9082c37256eSGarrett Wollman  */
909ac45e92fSRobert Watson static void
9102c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
9112c37256eSGarrett Wollman {
912f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
913a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
914623dce13SRobert Watson 	TCPDEBUG0;
915c78cbc7bSRobert Watson 
916ac45e92fSRobert Watson 	inp = sotoinpcb(so);
917c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
918c78cbc7bSRobert Watson 
919c78cbc7bSRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
920ac45e92fSRobert Watson 	INP_LOCK(inp);
921c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
922c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
923c78cbc7bSRobert Watson 
924c78cbc7bSRobert Watson 	/*
925a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
926c78cbc7bSRobert Watson 	 */
927c78cbc7bSRobert Watson 	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
928c78cbc7bSRobert Watson 	    !(inp->inp_vflag & INP_DROPPED)) {
929c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
930a152f8a3SRobert Watson 		TCPDEBUG1();
931c78cbc7bSRobert Watson 		tcp_drop(tp, ECONNABORTED);
932a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
933c78cbc7bSRobert Watson 	}
934a152f8a3SRobert Watson 	if (!(inp->inp_vflag & INP_DROPPED)) {
935a152f8a3SRobert Watson 		SOCK_LOCK(so);
936a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
937a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
938a152f8a3SRobert Watson 		inp->inp_vflag |= INP_SOCKREF;
939a152f8a3SRobert Watson 	}
940a152f8a3SRobert Watson 	INP_UNLOCK(inp);
941a152f8a3SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
942a152f8a3SRobert Watson }
943a152f8a3SRobert Watson 
944a152f8a3SRobert Watson /*
945a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
946a152f8a3SRobert Watson  */
947a152f8a3SRobert Watson static void
948a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
949a152f8a3SRobert Watson {
950a152f8a3SRobert Watson 	struct inpcb *inp;
951a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
952a152f8a3SRobert Watson 	TCPDEBUG0;
953a152f8a3SRobert Watson 
954a152f8a3SRobert Watson 	inp = sotoinpcb(so);
955a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
956a152f8a3SRobert Watson 
957a152f8a3SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
958a152f8a3SRobert Watson 	INP_LOCK(inp);
959a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
960a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
961a152f8a3SRobert Watson 
962a152f8a3SRobert Watson 	/*
963a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
964a152f8a3SRobert Watson 	 * a disconnect.
965a152f8a3SRobert Watson 	 */
966a152f8a3SRobert Watson 	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
967a152f8a3SRobert Watson 	    !(inp->inp_vflag & INP_DROPPED)) {
968a152f8a3SRobert Watson 		tp = intotcpcb(inp);
969a152f8a3SRobert Watson 		TCPDEBUG1();
970a152f8a3SRobert Watson 		tcp_disconnect(tp);
971a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
972a152f8a3SRobert Watson 	}
973a152f8a3SRobert Watson 	if (!(inp->inp_vflag & INP_DROPPED)) {
974a152f8a3SRobert Watson 		SOCK_LOCK(so);
975a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
976a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
977a152f8a3SRobert Watson 		inp->inp_vflag |= INP_SOCKREF;
978a152f8a3SRobert Watson 	}
979a152f8a3SRobert Watson 	INP_UNLOCK(inp);
980ac45e92fSRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
9812c37256eSGarrett Wollman }
9822c37256eSGarrett Wollman 
9832c37256eSGarrett Wollman /*
9842c37256eSGarrett Wollman  * Receive out-of-band data.
9852c37256eSGarrett Wollman  */
9862c37256eSGarrett Wollman static int
9872c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
9882c37256eSGarrett Wollman {
9892c37256eSGarrett Wollman 	int error = 0;
990f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
991623dce13SRobert Watson 	struct tcpcb *tp = NULL;
9922c37256eSGarrett Wollman 
993623dce13SRobert Watson 	TCPDEBUG0;
994623dce13SRobert Watson 	inp = sotoinpcb(so);
995623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
996623dce13SRobert Watson 	INP_LOCK(inp);
997623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
99821367f63SSam Leffler 		error = ECONNRESET;
999623dce13SRobert Watson 		goto out;
1000623dce13SRobert Watson 	}
1001623dce13SRobert Watson 	tp = intotcpcb(inp);
1002623dce13SRobert Watson 	TCPDEBUG1();
10032c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1004c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
10054cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
10064cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
10072c37256eSGarrett Wollman 		error = EINVAL;
10082c37256eSGarrett Wollman 		goto out;
10092c37256eSGarrett Wollman 	}
10102c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
10112c37256eSGarrett Wollman 		error = EWOULDBLOCK;
10122c37256eSGarrett Wollman 		goto out;
10132c37256eSGarrett Wollman 	}
10142c37256eSGarrett Wollman 	m->m_len = 1;
10152c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
10162c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
10172c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1018623dce13SRobert Watson 
1019623dce13SRobert Watson out:
1020623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
1021623dce13SRobert Watson 	INP_UNLOCK(inp);
1022623dce13SRobert Watson 	return (error);
10232c37256eSGarrett Wollman }
10242c37256eSGarrett Wollman 
10252c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1026756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1027756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1028756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1029756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1030756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1031756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1032756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1033756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1034756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
103554d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1036756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1037756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1038756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
1039756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
104054d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1041a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1042a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
10432c37256eSGarrett Wollman };
1044df8bae1dSRodney W. Grimes 
1045fb59c426SYoshinobu Inoue #ifdef INET6
1046fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1047756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1048756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1049756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1050756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1051756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1052756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1053756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1054756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1055756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1056756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1057756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1058756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1059756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
1060756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1061756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1062a152f8a3SRobert Watson  	.pru_sosetlabel =	in_pcbsosetlabel,
1063a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1064fb59c426SYoshinobu Inoue };
1065fb59c426SYoshinobu Inoue #endif /* INET6 */
1066fb59c426SYoshinobu Inoue 
1067a0292f23SGarrett Wollman /*
1068a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1069a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
10705200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
10715200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
10725200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
10735200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1074a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1075a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1076a0292f23SGarrett Wollman  */
10770312fbe9SPoul-Henning Kamp static int
1078ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1079a0292f23SGarrett Wollman {
1080a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1081a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
10825200e00eSIan Dowse 	struct in_addr laddr;
10835200e00eSIan Dowse 	u_short lport;
1084c3229e05SDavid Greenman 	int error;
1085a0292f23SGarrett Wollman 
1086623dce13SRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1087623dce13SRobert Watson 	INP_LOCK_ASSERT(inp);
1088623dce13SRobert Watson 
1089a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
1090b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1091a0292f23SGarrett Wollman 		if (error)
1092a0292f23SGarrett Wollman 			return error;
1093a0292f23SGarrett Wollman 	}
1094a0292f23SGarrett Wollman 
1095a0292f23SGarrett Wollman 	/*
1096a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1097a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1098a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1099a0292f23SGarrett Wollman 	 */
11005200e00eSIan Dowse 	laddr = inp->inp_laddr;
11015200e00eSIan Dowse 	lport = inp->inp_lport;
11025200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1103b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
11045200e00eSIan Dowse 	if (error && oinp == NULL)
1105d3628763SRodney W. Grimes 		return error;
1106c94c54e4SAndre Oppermann 	if (oinp)
1107a0292f23SGarrett Wollman 		return EADDRINUSE;
11085200e00eSIan Dowse 	inp->inp_laddr = laddr;
110915bd2b43SDavid Greenman 	in_pcbrehash(inp);
1110a0292f23SGarrett Wollman 
1111087b55eaSAndre Oppermann 	/*
1112087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1113087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1114087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1115087b55eaSAndre Oppermann 	 */
1116a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
11179b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1118a0292f23SGarrett Wollman 		tp->request_r_scale++;
1119a0292f23SGarrett Wollman 
1120a0292f23SGarrett Wollman 	soisconnecting(so);
1121a0292f23SGarrett Wollman 	tcpstat.tcps_connattempt++;
1122a0292f23SGarrett Wollman 	tp->t_state = TCPS_SYN_SENT;
1123b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1124b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
11251fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
1126a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1127a45d2726SAndras Olah 
1128a0292f23SGarrett Wollman 	return 0;
1129a0292f23SGarrett Wollman }
1130a0292f23SGarrett Wollman 
1131fb59c426SYoshinobu Inoue #ifdef INET6
1132fb59c426SYoshinobu Inoue static int
1133ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1134fb59c426SYoshinobu Inoue {
1135fb59c426SYoshinobu Inoue 	struct inpcb *inp = tp->t_inpcb, *oinp;
1136fb59c426SYoshinobu Inoue 	struct socket *so = inp->inp_socket;
1137fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1138fb59c426SYoshinobu Inoue 	struct in6_addr *addr6;
1139fb59c426SYoshinobu Inoue 	int error;
1140fb59c426SYoshinobu Inoue 
1141623dce13SRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1142623dce13SRobert Watson 	INP_LOCK_ASSERT(inp);
1143623dce13SRobert Watson 
1144fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
1145b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1146fb59c426SYoshinobu Inoue 		if (error)
1147fb59c426SYoshinobu Inoue 			return error;
1148fb59c426SYoshinobu Inoue 	}
1149fb59c426SYoshinobu Inoue 
1150fb59c426SYoshinobu Inoue 	/*
1151fb59c426SYoshinobu Inoue 	 * Cannot simply call in_pcbconnect, because there might be an
1152fb59c426SYoshinobu Inoue 	 * earlier incarnation of this same connection still in
1153fb59c426SYoshinobu Inoue 	 * TIME_WAIT state, creating an ADDRINUSE error.
1154a1f7e5f8SHajimu UMEMOTO 	 * in6_pcbladdr() also handles scope zone IDs.
1155fb59c426SYoshinobu Inoue 	 */
1156fb59c426SYoshinobu Inoue 	error = in6_pcbladdr(inp, nam, &addr6);
1157fb59c426SYoshinobu Inoue 	if (error)
1158fb59c426SYoshinobu Inoue 		return error;
1159fb59c426SYoshinobu Inoue 	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1160fb59c426SYoshinobu Inoue 				  &sin6->sin6_addr, sin6->sin6_port,
1161fb59c426SYoshinobu Inoue 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1162fb59c426SYoshinobu Inoue 				  ? addr6
1163fb59c426SYoshinobu Inoue 				  : &inp->in6p_laddr,
1164fb59c426SYoshinobu Inoue 				  inp->inp_lport,  0, NULL);
1165c94c54e4SAndre Oppermann 	if (oinp)
1166fb59c426SYoshinobu Inoue 		return EADDRINUSE;
1167fb59c426SYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1168fb59c426SYoshinobu Inoue 		inp->in6p_laddr = *addr6;
1169fb59c426SYoshinobu Inoue 	inp->in6p_faddr = sin6->sin6_addr;
1170fb59c426SYoshinobu Inoue 	inp->inp_fport = sin6->sin6_port;
11718a59da30SHajimu UMEMOTO 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
11728a59da30SHajimu UMEMOTO 	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
11738a59da30SHajimu UMEMOTO 	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
11748a59da30SHajimu UMEMOTO 		inp->in6p_flowinfo |=
11758a59da30SHajimu UMEMOTO 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1176fb59c426SYoshinobu Inoue 	in_pcbrehash(inp);
1177fb59c426SYoshinobu Inoue 
1178fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1179fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1180fb59c426SYoshinobu Inoue 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1181fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1182fb59c426SYoshinobu Inoue 
1183fb59c426SYoshinobu Inoue 	soisconnecting(so);
1184fb59c426SYoshinobu Inoue 	tcpstat.tcps_connattempt++;
1185fb59c426SYoshinobu Inoue 	tp->t_state = TCPS_SYN_SENT;
1186b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1187b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
11881fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
1189fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1190fb59c426SYoshinobu Inoue 
1191fb59c426SYoshinobu Inoue 	return 0;
1192fb59c426SYoshinobu Inoue }
1193fb59c426SYoshinobu Inoue #endif /* INET6 */
1194fb59c426SYoshinobu Inoue 
1195cfe8b629SGarrett Wollman /*
1196b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1197b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1198b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1199b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1200b8af5dfaSRobert Watson  * from Linux.
1201b8af5dfaSRobert Watson  */
1202b8af5dfaSRobert Watson static void
1203ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1204b8af5dfaSRobert Watson {
1205b8af5dfaSRobert Watson 
1206b8af5dfaSRobert Watson 	INP_LOCK_ASSERT(tp->t_inpcb);
1207b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1208b8af5dfaSRobert Watson 
1209b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1210b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1211b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
12123529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1213b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1214b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1215b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1216b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1217b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1218b8af5dfaSRobert Watson 	}
12191baaf834SBruce M Simpson 
12201baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
12211baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
12221baaf834SBruce M Simpson 
1223b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1224b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1225b8af5dfaSRobert Watson 
1226b8af5dfaSRobert Watson 	/*
1227b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1228b8af5dfaSRobert Watson 	 */
1229c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1230b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
1231b8af5dfaSRobert Watson 	ti->tcpi_snd_bwnd = tp->snd_bwnd;
1232b8af5dfaSRobert Watson }
1233b8af5dfaSRobert Watson 
1234b8af5dfaSRobert Watson /*
1235cfe8b629SGarrett Wollman  * The new sockopt interface makes it possible for us to block in the
1236cfe8b629SGarrett Wollman  * copyin/out step (if we take a page fault).  Taking a page fault at
1237cfe8b629SGarrett Wollman  * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
1238cfe8b629SGarrett Wollman  * use TSM, there probably isn't any need for this function to run at
1239cfe8b629SGarrett Wollman  * splnet() any more.  This needs more examination.)
1240b8af5dfaSRobert Watson  *
1241b8af5dfaSRobert Watson  * XXXRW: The locking here is wrong; we may take a page fault while holding
1242b8af5dfaSRobert Watson  * the inpcb lock.
1243cfe8b629SGarrett Wollman  */
1244df8bae1dSRodney W. Grimes int
1245ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1246df8bae1dSRodney W. Grimes {
12473f9d1ef9SRobert Watson 	int	error, opt, optval;
1248df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1249cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
1250b8af5dfaSRobert Watson 	struct	tcp_info ti;
1251df8bae1dSRodney W. Grimes 
1252cfe8b629SGarrett Wollman 	error = 0;
1253df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1254623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1255f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1256cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
12574e397bc5SRobert Watson 		INP_UNLOCK(inp);
1258fb59c426SYoshinobu Inoue #ifdef INET6
1259fb59c426SYoshinobu Inoue 		if (INP_CHECK_SOCKAF(so, AF_INET6))
1260fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
1261fb59c426SYoshinobu Inoue 		else
1262fb59c426SYoshinobu Inoue #endif /* INET6 */
1263cfe8b629SGarrett Wollman 		error = ip_ctloutput(so, sopt);
1264df8bae1dSRodney W. Grimes 		return (error);
1265df8bae1dSRodney W. Grimes 	}
1266623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
1267623dce13SRobert Watson 		error = ECONNRESET;
1268623dce13SRobert Watson 		goto out;
1269623dce13SRobert Watson 	}
1270df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
1271df8bae1dSRodney W. Grimes 
1272cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1273cfe8b629SGarrett Wollman 	case SOPT_SET:
1274cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
12751cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
127688f6b043SBruce M Simpson 		case TCP_MD5SIG:
12771cfd4b53SBruce M Simpson 			error = sooptcopyin(sopt, &optval, sizeof optval,
12781cfd4b53SBruce M Simpson 					    sizeof optval);
12791cfd4b53SBruce M Simpson 			if (error)
12801cfd4b53SBruce M Simpson 				break;
12811cfd4b53SBruce M Simpson 
12821cfd4b53SBruce M Simpson 			if (optval > 0)
12831cfd4b53SBruce M Simpson 				tp->t_flags |= TF_SIGNATURE;
12841cfd4b53SBruce M Simpson 			else
12851cfd4b53SBruce M Simpson 				tp->t_flags &= ~TF_SIGNATURE;
12861cfd4b53SBruce M Simpson 			break;
12871cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
1288df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1289cfe8b629SGarrett Wollman 		case TCP_NOOPT:
1290cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1291cfe8b629SGarrett Wollman 					    sizeof optval);
1292cfe8b629SGarrett Wollman 			if (error)
1293cfe8b629SGarrett Wollman 				break;
1294cfe8b629SGarrett Wollman 
1295cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1296cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1297cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1298cfe8b629SGarrett Wollman 				break;
1299cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1300cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1301cfe8b629SGarrett Wollman 				break;
1302cfe8b629SGarrett Wollman 			default:
1303cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1304cfe8b629SGarrett Wollman 				break;
1305cfe8b629SGarrett Wollman 			}
1306cfe8b629SGarrett Wollman 
1307cfe8b629SGarrett Wollman 			if (optval)
1308cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1309df8bae1dSRodney W. Grimes 			else
1310cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
1311df8bae1dSRodney W. Grimes 			break;
1312df8bae1dSRodney W. Grimes 
1313007581c0SJonathan Lemon 		case TCP_NOPUSH:
1314007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1315007581c0SJonathan Lemon 					    sizeof optval);
1316007581c0SJonathan Lemon 			if (error)
1317007581c0SJonathan Lemon 				break;
1318007581c0SJonathan Lemon 
1319007581c0SJonathan Lemon 			if (optval)
1320007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1321007581c0SJonathan Lemon 			else {
1322007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1323007581c0SJonathan Lemon 				error = tcp_output(tp);
1324007581c0SJonathan Lemon 			}
1325007581c0SJonathan Lemon 			break;
1326007581c0SJonathan Lemon 
1327df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1328cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1329cfe8b629SGarrett Wollman 					    sizeof optval);
1330cfe8b629SGarrett Wollman 			if (error)
1331df8bae1dSRodney W. Grimes 				break;
1332df8bae1dSRodney W. Grimes 
133353369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
133453369ac9SAndre Oppermann 			    optval + 40 >= tcp_minmss)
1335cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1336a0292f23SGarrett Wollman 			else
1337a0292f23SGarrett Wollman 				error = EINVAL;
1338a0292f23SGarrett Wollman 			break;
1339a0292f23SGarrett Wollman 
1340b8af5dfaSRobert Watson 		case TCP_INFO:
1341b8af5dfaSRobert Watson 			error = EINVAL;
1342b8af5dfaSRobert Watson 			break;
1343b8af5dfaSRobert Watson 
1344df8bae1dSRodney W. Grimes 		default:
1345df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1346df8bae1dSRodney W. Grimes 			break;
1347df8bae1dSRodney W. Grimes 		}
1348df8bae1dSRodney W. Grimes 		break;
1349df8bae1dSRodney W. Grimes 
1350cfe8b629SGarrett Wollman 	case SOPT_GET:
1351cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
13521cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
135388f6b043SBruce M Simpson 		case TCP_MD5SIG:
13541cfd4b53SBruce M Simpson 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1355b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
13561cfd4b53SBruce M Simpson 			break;
1357265ed012SBruce M Simpson #endif
1358df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1359cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
1360b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1361df8bae1dSRodney W. Grimes 			break;
1362df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1363cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
1364b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1365df8bae1dSRodney W. Grimes 			break;
1366a0292f23SGarrett Wollman 		case TCP_NOOPT:
1367cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
1368b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1369a0292f23SGarrett Wollman 			break;
1370a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1371cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
1372b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1373b8af5dfaSRobert Watson 			break;
1374b8af5dfaSRobert Watson 		case TCP_INFO:
1375b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
1376b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
1377a0292f23SGarrett Wollman 			break;
1378df8bae1dSRodney W. Grimes 		default:
1379df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1380df8bae1dSRodney W. Grimes 			break;
1381df8bae1dSRodney W. Grimes 		}
1382df8bae1dSRodney W. Grimes 		break;
1383df8bae1dSRodney W. Grimes 	}
1384623dce13SRobert Watson out:
1385f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1386df8bae1dSRodney W. Grimes 	return (error);
1387df8bae1dSRodney W. Grimes }
1388df8bae1dSRodney W. Grimes 
138926e30fbbSDavid Greenman /*
139026e30fbbSDavid Greenman  * tcp_sendspace and tcp_recvspace are the default send and receive window
139126e30fbbSDavid Greenman  * sizes, respectively.  These are obsolescent (this information should
139226e30fbbSDavid Greenman  * be set by the route).
139326e30fbbSDavid Greenman  */
139481e561cdSDavid E. O'Brien u_long	tcp_sendspace = 1024*32;
1395e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
13963d177f46SBill Fumerola     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
139781e561cdSDavid E. O'Brien u_long	tcp_recvspace = 1024*64;
1398e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
13993d177f46SBill Fumerola     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1400df8bae1dSRodney W. Grimes 
1401df8bae1dSRodney W. Grimes /*
1402df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1403df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1404df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1405df8bae1dSRodney W. Grimes  */
14060312fbe9SPoul-Henning Kamp static int
1407ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so)
1408df8bae1dSRodney W. Grimes {
1409ad3f9ab3SAndre Oppermann 	struct tcpcb *tp;
1410df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1411df8bae1dSRodney W. Grimes 	int error;
1412fb59c426SYoshinobu Inoue #ifdef INET6
14134a6a94d8SArchie Cobbs 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1414fb59c426SYoshinobu Inoue #endif
1415df8bae1dSRodney W. Grimes 
1416df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1417df8bae1dSRodney W. Grimes 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1418df8bae1dSRodney W. Grimes 		if (error)
1419df8bae1dSRodney W. Grimes 			return (error);
1420df8bae1dSRodney W. Grimes 	}
14216741ecf5SAndre Oppermann 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
14226741ecf5SAndre Oppermann 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1423f2de87feSRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
1424d915b280SStephan Uphoff 	error = in_pcballoc(so, &tcbinfo);
1425f2de87feSRobert Watson 	if (error) {
1426f2de87feSRobert Watson 		INP_INFO_WUNLOCK(&tcbinfo);
1427df8bae1dSRodney W. Grimes 		return (error);
1428f2de87feSRobert Watson 	}
1429df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1430fb59c426SYoshinobu Inoue #ifdef INET6
1431fb59c426SYoshinobu Inoue 	if (isipv6) {
1432fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1433fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1434fb59c426SYoshinobu Inoue 	}
1435fb59c426SYoshinobu Inoue 	else
1436fb59c426SYoshinobu Inoue #endif
1437cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1438df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1439623dce13SRobert Watson 	if (tp == NULL) {
1440fb59c426SYoshinobu Inoue #ifdef INET6
1441623dce13SRobert Watson 		if (isipv6) {
1442fb59c426SYoshinobu Inoue 			in6_pcbdetach(inp);
1443623dce13SRobert Watson 			in6_pcbfree(inp);
1444623dce13SRobert Watson 		} else {
1445fb59c426SYoshinobu Inoue #endif
1446df8bae1dSRodney W. Grimes 			in_pcbdetach(inp);
1447623dce13SRobert Watson 			in_pcbfree(inp);
1448623dce13SRobert Watson #ifdef INET6
1449623dce13SRobert Watson 		}
1450623dce13SRobert Watson #endif
1451f2de87feSRobert Watson 		INP_INFO_WUNLOCK(&tcbinfo);
1452df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1453df8bae1dSRodney W. Grimes 	}
1454df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
1455f2de87feSRobert Watson 	INP_UNLOCK(inp);
1456f2de87feSRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
1457df8bae1dSRodney W. Grimes 	return (0);
1458df8bae1dSRodney W. Grimes }
1459df8bae1dSRodney W. Grimes 
1460df8bae1dSRodney W. Grimes /*
1461df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1462df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1463df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1464df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1465df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1466df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1467df8bae1dSRodney W. Grimes  */
1468623dce13SRobert Watson static void
1469ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
1470df8bae1dSRodney W. Grimes {
1471e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
1472e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
1473e6e0b5ffSRobert Watson 
1474e6e0b5ffSRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1475e6e0b5ffSRobert Watson 	INP_LOCK_ASSERT(inp);
1476df8bae1dSRodney W. Grimes 
1477623dce13SRobert Watson 	/*
1478623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1479623dce13SRobert Watson 	 * socket is still open.
1480623dce13SRobert Watson 	 */
1481623dce13SRobert Watson 	if (tp->t_state < TCPS_ESTABLISHED) {
1482df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1483623dce13SRobert Watson 		KASSERT(tp != NULL,
1484623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
1485623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1486243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
1487623dce13SRobert Watson 		KASSERT(tp != NULL,
1488623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
1489623dce13SRobert Watson 	} else {
1490df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
1491df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
1492623dce13SRobert Watson 		tcp_usrclosed(tp);
1493953b5606SRobert Watson 		if (!(inp->inp_vflag & INP_DROPPED))
1494bc65987aSKip Macy 			tcp_output_disconnect(tp);
1495df8bae1dSRodney W. Grimes 	}
1496df8bae1dSRodney W. Grimes }
1497df8bae1dSRodney W. Grimes 
1498df8bae1dSRodney W. Grimes /*
1499df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1500df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1501df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1502df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1503df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1504df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1505df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1506df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1507df8bae1dSRodney W. Grimes  */
1508623dce13SRobert Watson static void
1509ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
1510df8bae1dSRodney W. Grimes {
1511df8bae1dSRodney W. Grimes 
1512e6e0b5ffSRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1513e6e0b5ffSRobert Watson 	INP_LOCK_ASSERT(tp->t_inpcb);
1514e6e0b5ffSRobert Watson 
1515df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1516df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
1517bc65987aSKip Macy 		tcp_offload_listen_close(tp);
1518bc65987aSKip Macy 		/* FALLTHROUGH */
1519bc65987aSKip Macy 	case TCPS_CLOSED:
1520df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_CLOSED;
1521df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1522623dce13SRobert Watson 		/*
1523623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
1524623dce13SRobert Watson 		 * still open.
1525623dce13SRobert Watson 		 */
1526623dce13SRobert Watson 		KASSERT(tp != NULL,
1527623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
1528df8bae1dSRodney W. Grimes 		break;
1529df8bae1dSRodney W. Grimes 
1530a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
1531df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1532a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
1533a0292f23SGarrett Wollman 		break;
1534a0292f23SGarrett Wollman 
1535df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1536df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_FIN_WAIT_1;
1537df8bae1dSRodney W. Grimes 		break;
1538df8bae1dSRodney W. Grimes 
1539df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1540df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_LAST_ACK;
1541df8bae1dSRodney W. Grimes 		break;
1542df8bae1dSRodney W. Grimes 	}
1543abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
1544df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
1545abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
15467c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
15477c72af87SMohan Srinivasan 			int timeout;
15487c72af87SMohan Srinivasan 
15497c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
15507c72af87SMohan Srinivasan 			    tcp_finwait2_timeout : tcp_maxidle;
1551b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
1552b6239c4aSAndras Olah 		}
1553df8bae1dSRodney W. Grimes 	}
15547c72af87SMohan Srinivasan }
1555497057eeSRobert Watson 
1556497057eeSRobert Watson #ifdef DDB
1557497057eeSRobert Watson static void
1558497057eeSRobert Watson db_print_indent(int indent)
1559497057eeSRobert Watson {
1560497057eeSRobert Watson 	int i;
1561497057eeSRobert Watson 
1562497057eeSRobert Watson 	for (i = 0; i < indent; i++)
1563497057eeSRobert Watson 		db_printf(" ");
1564497057eeSRobert Watson }
1565497057eeSRobert Watson 
1566497057eeSRobert Watson static void
1567497057eeSRobert Watson db_print_tstate(int t_state)
1568497057eeSRobert Watson {
1569497057eeSRobert Watson 
1570497057eeSRobert Watson 	switch (t_state) {
1571497057eeSRobert Watson 	case TCPS_CLOSED:
1572497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
1573497057eeSRobert Watson 		return;
1574497057eeSRobert Watson 
1575497057eeSRobert Watson 	case TCPS_LISTEN:
1576497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
1577497057eeSRobert Watson 		return;
1578497057eeSRobert Watson 
1579497057eeSRobert Watson 	case TCPS_SYN_SENT:
1580497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
1581497057eeSRobert Watson 		return;
1582497057eeSRobert Watson 
1583497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
1584497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
1585497057eeSRobert Watson 		return;
1586497057eeSRobert Watson 
1587497057eeSRobert Watson 	case TCPS_ESTABLISHED:
1588497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
1589497057eeSRobert Watson 		return;
1590497057eeSRobert Watson 
1591497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
1592497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
1593497057eeSRobert Watson 		return;
1594497057eeSRobert Watson 
1595497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
1596497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
1597497057eeSRobert Watson 		return;
1598497057eeSRobert Watson 
1599497057eeSRobert Watson 	case TCPS_CLOSING:
1600497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
1601497057eeSRobert Watson 		return;
1602497057eeSRobert Watson 
1603497057eeSRobert Watson 	case TCPS_LAST_ACK:
1604497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
1605497057eeSRobert Watson 		return;
1606497057eeSRobert Watson 
1607497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
1608497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
1609497057eeSRobert Watson 		return;
1610497057eeSRobert Watson 
1611497057eeSRobert Watson 	case TCPS_TIME_WAIT:
1612497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
1613497057eeSRobert Watson 		return;
1614497057eeSRobert Watson 
1615497057eeSRobert Watson 	default:
1616497057eeSRobert Watson 		db_printf("unknown");
1617497057eeSRobert Watson 		return;
1618497057eeSRobert Watson 	}
1619497057eeSRobert Watson }
1620497057eeSRobert Watson 
1621497057eeSRobert Watson static void
1622497057eeSRobert Watson db_print_tflags(u_int t_flags)
1623497057eeSRobert Watson {
1624497057eeSRobert Watson 	int comma;
1625497057eeSRobert Watson 
1626497057eeSRobert Watson 	comma = 0;
1627497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
1628497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
1629497057eeSRobert Watson 		comma = 1;
1630497057eeSRobert Watson 	}
1631497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
1632497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
1633497057eeSRobert Watson 		comma = 1;
1634497057eeSRobert Watson 	}
1635497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
1636497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
1637497057eeSRobert Watson 		comma = 1;
1638497057eeSRobert Watson 	}
1639497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
1640497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
1641497057eeSRobert Watson 		comma = 1;
1642497057eeSRobert Watson 	}
1643497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
1644497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
1645497057eeSRobert Watson 		comma = 1;
1646497057eeSRobert Watson 	}
1647497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
1648497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1649497057eeSRobert Watson 		comma = 1;
1650497057eeSRobert Watson 	}
1651497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
1652497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1653497057eeSRobert Watson 		comma = 1;
1654497057eeSRobert Watson 	}
1655497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
1656497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1657497057eeSRobert Watson 		comma = 1;
1658497057eeSRobert Watson 	}
1659497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
1660497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1661497057eeSRobert Watson 		comma = 1;
1662497057eeSRobert Watson 	}
1663497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
1664497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1665497057eeSRobert Watson 		comma = 1;
1666497057eeSRobert Watson 	}
1667497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
1668497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1669497057eeSRobert Watson 		comma = 1;
1670497057eeSRobert Watson 	}
1671497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
1672497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", 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_NOPUSH) {
1680497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
1681497057eeSRobert Watson 		comma = 1;
1682497057eeSRobert Watson 	}
1683497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
1684497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1685497057eeSRobert Watson 		comma = 1;
1686497057eeSRobert Watson 	}
1687497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
1688497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1689497057eeSRobert Watson 		comma = 1;
1690497057eeSRobert Watson 	}
1691497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
1692497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1693497057eeSRobert Watson 		comma = 1;
1694497057eeSRobert Watson 	}
1695497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
1696497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1697497057eeSRobert Watson 		comma = 1;
1698497057eeSRobert Watson 	}
1699497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
1700497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1701497057eeSRobert Watson 		comma = 1;
1702497057eeSRobert Watson 	}
1703497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
1704497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1705497057eeSRobert Watson 		comma = 1;
1706497057eeSRobert Watson 	}
1707497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
1708497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1709497057eeSRobert Watson 		comma = 1;
1710497057eeSRobert Watson 	}
1711497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
1712497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1713497057eeSRobert Watson 		comma = 1;
1714497057eeSRobert Watson 	}
1715497057eeSRobert Watson 	if (t_flags & TF_TSO) {
1716497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
1717497057eeSRobert Watson 		comma = 1;
1718497057eeSRobert Watson 	}
1719497057eeSRobert Watson }
1720497057eeSRobert Watson 
1721497057eeSRobert Watson static void
1722497057eeSRobert Watson db_print_toobflags(char t_oobflags)
1723497057eeSRobert Watson {
1724497057eeSRobert Watson 	int comma;
1725497057eeSRobert Watson 
1726497057eeSRobert Watson 	comma = 0;
1727497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
1728497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1729497057eeSRobert Watson 		comma = 1;
1730497057eeSRobert Watson 	}
1731497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
1732497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1733497057eeSRobert Watson 		comma = 1;
1734497057eeSRobert Watson 	}
1735497057eeSRobert Watson }
1736497057eeSRobert Watson 
1737497057eeSRobert Watson static void
1738497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1739497057eeSRobert Watson {
1740497057eeSRobert Watson 
1741497057eeSRobert Watson 	db_print_indent(indent);
1742497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
1743497057eeSRobert Watson 
1744497057eeSRobert Watson 	indent += 2;
1745497057eeSRobert Watson 
1746497057eeSRobert Watson 	db_print_indent(indent);
1747497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
1748497057eeSRobert Watson 	   LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
1749497057eeSRobert Watson 
1750497057eeSRobert Watson 	db_print_indent(indent);
175185d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
1752e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
1753497057eeSRobert Watson 
1754497057eeSRobert Watson 	db_print_indent(indent);
1755e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
1756e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
1757497057eeSRobert Watson 
1758497057eeSRobert Watson 	db_print_indent(indent);
1759497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
1760497057eeSRobert Watson 	db_print_tstate(tp->t_state);
1761497057eeSRobert Watson 	db_printf(")\n");
1762497057eeSRobert Watson 
1763497057eeSRobert Watson 	db_print_indent(indent);
1764497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
1765497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
1766497057eeSRobert Watson 	db_printf(")\n");
1767497057eeSRobert Watson 
1768497057eeSRobert Watson 	db_print_indent(indent);
1769497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
1770497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
1771497057eeSRobert Watson 
1772497057eeSRobert Watson 	db_print_indent(indent);
1773497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
1774497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
1775497057eeSRobert Watson 
1776497057eeSRobert Watson 	db_print_indent(indent);
1777497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
1778497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
1779497057eeSRobert Watson 
1780497057eeSRobert Watson 	db_print_indent(indent);
1781497057eeSRobert Watson 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %lu   rcv_up: 0x%08x\n",
1782497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
1783497057eeSRobert Watson 
1784497057eeSRobert Watson 	db_print_indent(indent);
1785497057eeSRobert Watson 	db_printf("snd_wnd: %lu   snd_cwnd: %lu   snd_bwnd: %lu\n",
1786497057eeSRobert Watson 	   tp->snd_wnd, tp->snd_cwnd, tp->snd_bwnd);
1787497057eeSRobert Watson 
1788497057eeSRobert Watson 	db_print_indent(indent);
1789497057eeSRobert Watson 	db_printf("snd_ssthresh: %lu   snd_bandwidth: %lu   snd_recover: "
1790497057eeSRobert Watson 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_bandwidth,
1791497057eeSRobert Watson 	    tp->snd_recover);
1792497057eeSRobert Watson 
1793497057eeSRobert Watson 	db_print_indent(indent);
1794497057eeSRobert Watson 	db_printf("t_maxopd: %u   t_rcvtime: %lu   t_startime: %lu\n",
1795497057eeSRobert Watson 	    tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
1796497057eeSRobert Watson 
1797497057eeSRobert Watson 	db_print_indent(indent);
1798497057eeSRobert Watson 	db_printf("t_rttime: %d   t_rtsq: 0x%08x   t_bw_rtttime: %d\n",
1799497057eeSRobert Watson 	    tp->t_rtttime, tp->t_rtseq, tp->t_bw_rtttime);
1800497057eeSRobert Watson 
1801497057eeSRobert Watson 	db_print_indent(indent);
1802497057eeSRobert Watson 	db_printf("t_bw_rtseq: 0x%08x   t_rxtcur: %d   t_maxseg: %u   "
1803497057eeSRobert Watson 	    "t_srtt: %d\n", tp->t_bw_rtseq, tp->t_rxtcur, tp->t_maxseg,
1804497057eeSRobert Watson 	    tp->t_srtt);
1805497057eeSRobert Watson 
1806497057eeSRobert Watson 	db_print_indent(indent);
1807497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
1808497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
1809497057eeSRobert Watson 	    tp->t_rttbest);
1810497057eeSRobert Watson 
1811497057eeSRobert Watson 	db_print_indent(indent);
1812497057eeSRobert Watson 	db_printf("t_rttupdated: %lu   max_sndwnd: %lu   t_softerror: %d\n",
1813497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
1814497057eeSRobert Watson 
1815497057eeSRobert Watson 	db_print_indent(indent);
1816497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
1817497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
1818497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
1819497057eeSRobert Watson 
1820497057eeSRobert Watson 	db_print_indent(indent);
1821497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
1822497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
1823497057eeSRobert Watson 
1824497057eeSRobert Watson 	db_print_indent(indent);
18251a553740SAndre Oppermann 	db_printf("ts_recent: %u   ts_recent_age: %lu\n",
18261a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
1827497057eeSRobert Watson 
1828497057eeSRobert Watson 	db_print_indent(indent);
1829497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
1830497057eeSRobert Watson 	    "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
1831497057eeSRobert Watson 
1832497057eeSRobert Watson 	db_print_indent(indent);
1833497057eeSRobert Watson 	db_printf("snd_ssthresh_prev: %lu   snd_recover_prev: 0x%08x   "
1834497057eeSRobert Watson 	    "t_badrxtwin: %lu\n", tp->snd_ssthresh_prev,
1835497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
1836497057eeSRobert Watson 
1837497057eeSRobert Watson 	db_print_indent(indent);
18383529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
18393529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
1840497057eeSRobert Watson 
1841497057eeSRobert Watson 	db_print_indent(indent);
1842497057eeSRobert Watson 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
1843497057eeSRobert Watson 	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
1844497057eeSRobert Watson 
1845497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
1846497057eeSRobert Watson 
1847497057eeSRobert Watson 	db_print_indent(indent);
1848497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
1849497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
1850497057eeSRobert Watson }
1851497057eeSRobert Watson 
1852497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
1853497057eeSRobert Watson {
1854497057eeSRobert Watson 	struct tcpcb *tp;
1855497057eeSRobert Watson 
1856497057eeSRobert Watson 	if (!have_addr) {
1857497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
1858497057eeSRobert Watson 		return;
1859497057eeSRobert Watson 	}
1860497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
1861497057eeSRobert Watson 
1862497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
1863497057eeSRobert Watson }
1864497057eeSRobert Watson #endif
1865