xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 535fbad68f67bd603548cc82077cda9bbacc6fcd)
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);
1638501a69cSRobert Watson 	INP_WLOCK_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);
2048501a69cSRobert Watson 			INP_WUNLOCK(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);
2568501a69cSRobert Watson 	INP_WLOCK(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"));
2898501a69cSRobert Watson 	INP_WLOCK(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);
2998501a69cSRobert Watson 	INP_WUNLOCK(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"));
3298501a69cSRobert Watson 	INP_WLOCK(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);
3558501a69cSRobert Watson 	INP_WUNLOCK(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"));
3758501a69cSRobert Watson 	INP_WLOCK(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);
3958501a69cSRobert Watson 	INP_WUNLOCK(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"));
4128501a69cSRobert Watson 	INP_WLOCK(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);
4358501a69cSRobert Watson 	INP_WUNLOCK(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"));
4728501a69cSRobert Watson 	INP_WLOCK(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);
4848501a69cSRobert Watson 	INP_WUNLOCK(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"));
5138501a69cSRobert Watson 	INP_WLOCK(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);
5458501a69cSRobert Watson 	INP_WUNLOCK(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"));
5738501a69cSRobert Watson 	INP_WLOCK(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);
5838501a69cSRobert Watson 	INP_WUNLOCK(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"));
608109058b0SRobert Watson 	INP_INFO_RLOCK(&tcbinfo);
6098501a69cSRobert Watson 	INP_WLOCK(inp);
610623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
6113d2d3ef4SRobert Watson 		error = ECONNABORTED;
612623dce13SRobert Watson 		goto out;
613623dce13SRobert Watson 	}
6141db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
6151db24ffbSJonathan Lemon 	TCPDEBUG1();
616f76fcf6dSJeffrey Hsu 
617f76fcf6dSJeffrey Hsu 	/*
61854d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
61926ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
62026ef6ac4SDon Lewis 	 * release the lock.
621f76fcf6dSJeffrey Hsu 	 */
62226ef6ac4SDon Lewis 	port = inp->inp_fport;
62326ef6ac4SDon Lewis 	addr = inp->inp_faddr;
624f76fcf6dSJeffrey Hsu 
625623dce13SRobert Watson out:
626623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
6278501a69cSRobert Watson 	INP_WUNLOCK(inp);
628109058b0SRobert Watson 	INP_INFO_RUNLOCK(&tcbinfo);
62926ef6ac4SDon Lewis 	if (error == 0)
63026ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
63126ef6ac4SDon Lewis 	return error;
6322c37256eSGarrett Wollman }
6332c37256eSGarrett Wollman 
634fb59c426SYoshinobu Inoue #ifdef INET6
635fb59c426SYoshinobu Inoue static int
636fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
637fb59c426SYoshinobu Inoue {
638f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
639fb59c426SYoshinobu Inoue 	int error = 0;
6401db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
64126ef6ac4SDon Lewis 	struct in_addr addr;
64226ef6ac4SDon Lewis 	struct in6_addr addr6;
64326ef6ac4SDon Lewis 	in_port_t port = 0;
64426ef6ac4SDon Lewis 	int v4 = 0;
6451db24ffbSJonathan Lemon 	TCPDEBUG0;
646fb59c426SYoshinobu Inoue 
647b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
648b4470c16SRobert Watson 		return (ECONNABORTED);
649f76fcf6dSJeffrey Hsu 
650f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
651623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
6528501a69cSRobert Watson 	INP_WLOCK(inp);
653623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
65421367f63SSam Leffler 		error = ECONNABORTED;
655623dce13SRobert Watson 		goto out;
656623dce13SRobert Watson 	}
6571db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
6581db24ffbSJonathan Lemon 	TCPDEBUG1();
659623dce13SRobert Watson 
66026ef6ac4SDon Lewis 	/*
66126ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
66226ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
66326ef6ac4SDon Lewis 	 * release the lock.
66426ef6ac4SDon Lewis 	 */
66526ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
66626ef6ac4SDon Lewis 		v4 = 1;
66726ef6ac4SDon Lewis 		port = inp->inp_fport;
66826ef6ac4SDon Lewis 		addr = inp->inp_faddr;
66926ef6ac4SDon Lewis 	} else {
67026ef6ac4SDon Lewis 		port = inp->inp_fport;
67126ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
67226ef6ac4SDon Lewis 	}
67326ef6ac4SDon Lewis 
674623dce13SRobert Watson out:
675623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
6768501a69cSRobert Watson 	INP_WUNLOCK(inp);
67726ef6ac4SDon Lewis 	if (error == 0) {
67826ef6ac4SDon Lewis 		if (v4)
67926ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
68026ef6ac4SDon Lewis 		else
68126ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
68226ef6ac4SDon Lewis 	}
68326ef6ac4SDon Lewis 	return error;
684fb59c426SYoshinobu Inoue }
685fb59c426SYoshinobu Inoue #endif /* INET6 */
686f76fcf6dSJeffrey Hsu 
687f76fcf6dSJeffrey Hsu /*
6882c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
6892c37256eSGarrett Wollman  */
6902c37256eSGarrett Wollman static int
6912c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
6922c37256eSGarrett Wollman {
6932c37256eSGarrett Wollman 	int error = 0;
694f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
695623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6962c37256eSGarrett Wollman 
697623dce13SRobert Watson 	TCPDEBUG0;
698623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
699623dce13SRobert Watson 	inp = sotoinpcb(so);
700623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
7018501a69cSRobert Watson 	INP_WLOCK(inp);
702623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
70321367f63SSam Leffler 		error = ECONNRESET;
704623dce13SRobert Watson 		goto out;
705623dce13SRobert Watson 	}
706623dce13SRobert Watson 	tp = intotcpcb(inp);
707623dce13SRobert Watson 	TCPDEBUG1();
7082c37256eSGarrett Wollman 	socantsendmore(so);
709623dce13SRobert Watson 	tcp_usrclosed(tp);
710bc65987aSKip Macy 	error = tcp_output_disconnect(tp);
711623dce13SRobert Watson 
712623dce13SRobert Watson out:
713623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
7148501a69cSRobert Watson 	INP_WUNLOCK(inp);
715623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
716623dce13SRobert Watson 
717623dce13SRobert Watson 	return (error);
7182c37256eSGarrett Wollman }
7192c37256eSGarrett Wollman 
7202c37256eSGarrett Wollman /*
7212c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
7222c37256eSGarrett Wollman  */
7232c37256eSGarrett Wollman static int
7242c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
7252c37256eSGarrett Wollman {
726f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
727623dce13SRobert Watson 	struct tcpcb *tp = NULL;
728623dce13SRobert Watson 	int error = 0;
7292c37256eSGarrett Wollman 
730623dce13SRobert Watson 	TCPDEBUG0;
731623dce13SRobert Watson 	inp = sotoinpcb(so);
732623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
7338501a69cSRobert Watson 	INP_WLOCK(inp);
734623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
73521367f63SSam Leffler 		error = ECONNRESET;
736623dce13SRobert Watson 		goto out;
737623dce13SRobert Watson 	}
738623dce13SRobert Watson 	tp = intotcpcb(inp);
739623dce13SRobert Watson 	TCPDEBUG1();
740bc65987aSKip Macy 	tcp_output_rcvd(tp);
741623dce13SRobert Watson 
742623dce13SRobert Watson out:
743623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
7448501a69cSRobert Watson 	INP_WUNLOCK(inp);
745623dce13SRobert Watson 	return (error);
7462c37256eSGarrett Wollman }
7472c37256eSGarrett Wollman 
7482c37256eSGarrett Wollman /*
7492c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
7509c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
7519c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
7529c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
7539c9906e9SPeter Wemm  * generally are caller-frees.
7542c37256eSGarrett Wollman  */
7552c37256eSGarrett Wollman static int
75657bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
757b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
7582c37256eSGarrett Wollman {
7592c37256eSGarrett Wollman 	int error = 0;
760f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
761623dce13SRobert Watson 	struct tcpcb *tp = NULL;
762623dce13SRobert Watson 	int headlocked = 0;
763fb59c426SYoshinobu Inoue #ifdef INET6
764fb59c426SYoshinobu Inoue 	int isipv6;
765fb59c426SYoshinobu Inoue #endif
7669c9906e9SPeter Wemm 	TCPDEBUG0;
7672c37256eSGarrett Wollman 
768f76fcf6dSJeffrey Hsu 	/*
769623dce13SRobert Watson 	 * We require the pcbinfo lock in two cases:
770623dce13SRobert Watson 	 *
771623dce13SRobert Watson 	 * (1) An implied connect is taking place, which can result in
772623dce13SRobert Watson 	 *     binding IPs and ports and hence modification of the pcb hash
773623dce13SRobert Watson 	 *     chains.
774623dce13SRobert Watson 	 *
775623dce13SRobert Watson 	 * (2) PRUS_EOF is set, resulting in explicit close on the send.
776f76fcf6dSJeffrey Hsu 	 */
777623dce13SRobert Watson 	if ((nam != NULL) || (flags & PRUS_EOF)) {
778f76fcf6dSJeffrey Hsu 		INP_INFO_WLOCK(&tcbinfo);
779623dce13SRobert Watson 		headlocked = 1;
780623dce13SRobert Watson 	}
781f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
782623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
7838501a69cSRobert Watson 	INP_WLOCK(inp);
784623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
7857ff0b850SAndre Oppermann 		if (control)
7867ff0b850SAndre Oppermann 			m_freem(control);
7877ff0b850SAndre Oppermann 		if (m)
7887ff0b850SAndre Oppermann 			m_freem(m);
78921367f63SSam Leffler 		error = ECONNRESET;
7909c9906e9SPeter Wemm 		goto out;
7919c9906e9SPeter Wemm 	}
792fb59c426SYoshinobu Inoue #ifdef INET6
793fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
794fb59c426SYoshinobu Inoue #endif /* INET6 */
7959c9906e9SPeter Wemm 	tp = intotcpcb(inp);
7969c9906e9SPeter Wemm 	TCPDEBUG1();
7979c9906e9SPeter Wemm 	if (control) {
7989c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
7999c9906e9SPeter Wemm 		if (control->m_len) {
8009c9906e9SPeter Wemm 			m_freem(control);
8012c37256eSGarrett Wollman 			if (m)
8022c37256eSGarrett Wollman 				m_freem(m);
803744f87eaSDavid Greenman 			error = EINVAL;
804744f87eaSDavid Greenman 			goto out;
8052c37256eSGarrett Wollman 		}
8069c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
8079c9906e9SPeter Wemm 	}
8082c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
809395bb186SSam Leffler 		sbappendstream(&so->so_snd, m);
8102c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
8112c37256eSGarrett Wollman 			/*
8122c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
8132c37256eSGarrett Wollman 			 * initialize window to default value, and
8142c37256eSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
8152c37256eSGarrett Wollman 			 * MSS.
8162c37256eSGarrett Wollman 			 */
817623dce13SRobert Watson 			INP_INFO_WLOCK_ASSERT(&tcbinfo);
818fb59c426SYoshinobu Inoue #ifdef INET6
819fb59c426SYoshinobu Inoue 			if (isipv6)
820b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
821fb59c426SYoshinobu Inoue 			else
822fb59c426SYoshinobu Inoue #endif /* INET6 */
823b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
8242c37256eSGarrett Wollman 			if (error)
8252c37256eSGarrett Wollman 				goto out;
8262c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
8272c37256eSGarrett Wollman 			tcp_mss(tp, -1);
8282c37256eSGarrett Wollman 		}
8292c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
8302c37256eSGarrett Wollman 			/*
8312c37256eSGarrett Wollman 			 * Close the send side of the connection after
8322c37256eSGarrett Wollman 			 * the data is sent.
8332c37256eSGarrett Wollman 			 */
834623dce13SRobert Watson 			INP_INFO_WLOCK_ASSERT(&tcbinfo);
8352c37256eSGarrett Wollman 			socantsendmore(so);
836623dce13SRobert Watson 			tcp_usrclosed(tp);
8372c37256eSGarrett Wollman 		}
838623dce13SRobert Watson 		if (headlocked) {
839d1401c90SRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
840623dce13SRobert Watson 			headlocked = 0;
841623dce13SRobert Watson 		}
842b0acefa8SBill Fenner 		if (tp != NULL) {
843b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
844b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
845bc65987aSKip Macy 			error = tcp_output_send(tp);
846b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
847b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
848b0acefa8SBill Fenner 		}
8492c37256eSGarrett Wollman 	} else {
850623dce13SRobert Watson 		/*
851623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
852623dce13SRobert Watson 		 */
853d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
8542c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
855d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
8562c37256eSGarrett Wollman 			m_freem(m);
8572c37256eSGarrett Wollman 			error = ENOBUFS;
8582c37256eSGarrett Wollman 			goto out;
8592c37256eSGarrett Wollman 		}
8602c37256eSGarrett Wollman 		/*
8612c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
8622c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
8632c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
8642c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
8652c37256eSGarrett Wollman 		 * of data past the urgent section.
8662c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
8672c37256eSGarrett Wollman 		 */
868d2bc35abSRobert Watson 		sbappendstream_locked(&so->so_snd, m);
869d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
870ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
871ef53690bSGarrett Wollman 			/*
872ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
873ef53690bSGarrett Wollman 			 * initialize window to default value, and
874ef53690bSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
875ef53690bSGarrett Wollman 			 * MSS.
876ef53690bSGarrett Wollman 			 */
877623dce13SRobert Watson 			INP_INFO_WLOCK_ASSERT(&tcbinfo);
878fb59c426SYoshinobu Inoue #ifdef INET6
879fb59c426SYoshinobu Inoue 			if (isipv6)
880b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
881fb59c426SYoshinobu Inoue 			else
882fb59c426SYoshinobu Inoue #endif /* INET6 */
883b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
884ef53690bSGarrett Wollman 			if (error)
885ef53690bSGarrett Wollman 				goto out;
886ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
887ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
888d1401c90SRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
889623dce13SRobert Watson 			headlocked = 0;
890623dce13SRobert Watson 		} else if (nam) {
891623dce13SRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
892623dce13SRobert Watson 			headlocked = 0;
893623dce13SRobert Watson 		}
8942c37256eSGarrett Wollman 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
8952cdbfa66SPaul Saab 		tp->t_flags |= TF_FORCEDATA;
896bc65987aSKip Macy 		error = tcp_output_send(tp);
8972cdbfa66SPaul Saab 		tp->t_flags &= ~TF_FORCEDATA;
8982c37256eSGarrett Wollman 	}
899d1401c90SRobert Watson out:
900d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
9012c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
9028501a69cSRobert Watson 	INP_WUNLOCK(inp);
903623dce13SRobert Watson 	if (headlocked)
904d1401c90SRobert Watson 		INP_INFO_WUNLOCK(&tcbinfo);
90573fddedaSPeter Grehan 	return (error);
9062c37256eSGarrett Wollman }
9072c37256eSGarrett Wollman 
9082c37256eSGarrett Wollman /*
909a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
9102c37256eSGarrett Wollman  */
911ac45e92fSRobert Watson static void
9122c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
9132c37256eSGarrett Wollman {
914f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
915a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
916623dce13SRobert Watson 	TCPDEBUG0;
917c78cbc7bSRobert Watson 
918ac45e92fSRobert Watson 	inp = sotoinpcb(so);
919c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
920c78cbc7bSRobert Watson 
921c78cbc7bSRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
9228501a69cSRobert Watson 	INP_WLOCK(inp);
923c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
924c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
925c78cbc7bSRobert Watson 
926c78cbc7bSRobert Watson 	/*
927a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
928c78cbc7bSRobert Watson 	 */
929c78cbc7bSRobert Watson 	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
930c78cbc7bSRobert Watson 	    !(inp->inp_vflag & INP_DROPPED)) {
931c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
932a152f8a3SRobert Watson 		TCPDEBUG1();
933c78cbc7bSRobert Watson 		tcp_drop(tp, ECONNABORTED);
934a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
935c78cbc7bSRobert Watson 	}
936a152f8a3SRobert Watson 	if (!(inp->inp_vflag & INP_DROPPED)) {
937a152f8a3SRobert Watson 		SOCK_LOCK(so);
938a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
939a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
940a152f8a3SRobert Watson 		inp->inp_vflag |= INP_SOCKREF;
941a152f8a3SRobert Watson 	}
9428501a69cSRobert Watson 	INP_WUNLOCK(inp);
943a152f8a3SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
944a152f8a3SRobert Watson }
945a152f8a3SRobert Watson 
946a152f8a3SRobert Watson /*
947a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
948a152f8a3SRobert Watson  */
949a152f8a3SRobert Watson static void
950a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
951a152f8a3SRobert Watson {
952a152f8a3SRobert Watson 	struct inpcb *inp;
953a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
954a152f8a3SRobert Watson 	TCPDEBUG0;
955a152f8a3SRobert Watson 
956a152f8a3SRobert Watson 	inp = sotoinpcb(so);
957a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
958a152f8a3SRobert Watson 
959a152f8a3SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
9608501a69cSRobert Watson 	INP_WLOCK(inp);
961a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
962a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
963a152f8a3SRobert Watson 
964a152f8a3SRobert Watson 	/*
965a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
966a152f8a3SRobert Watson 	 * a disconnect.
967a152f8a3SRobert Watson 	 */
968a152f8a3SRobert Watson 	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
969a152f8a3SRobert Watson 	    !(inp->inp_vflag & INP_DROPPED)) {
970a152f8a3SRobert Watson 		tp = intotcpcb(inp);
971a152f8a3SRobert Watson 		TCPDEBUG1();
972a152f8a3SRobert Watson 		tcp_disconnect(tp);
973a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
974a152f8a3SRobert Watson 	}
975a152f8a3SRobert Watson 	if (!(inp->inp_vflag & INP_DROPPED)) {
976a152f8a3SRobert Watson 		SOCK_LOCK(so);
977a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
978a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
979a152f8a3SRobert Watson 		inp->inp_vflag |= INP_SOCKREF;
980a152f8a3SRobert Watson 	}
9818501a69cSRobert Watson 	INP_WUNLOCK(inp);
982ac45e92fSRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
9832c37256eSGarrett Wollman }
9842c37256eSGarrett Wollman 
9852c37256eSGarrett Wollman /*
9862c37256eSGarrett Wollman  * Receive out-of-band data.
9872c37256eSGarrett Wollman  */
9882c37256eSGarrett Wollman static int
9892c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
9902c37256eSGarrett Wollman {
9912c37256eSGarrett Wollman 	int error = 0;
992f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
993623dce13SRobert Watson 	struct tcpcb *tp = NULL;
9942c37256eSGarrett Wollman 
995623dce13SRobert Watson 	TCPDEBUG0;
996623dce13SRobert Watson 	inp = sotoinpcb(so);
997623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
9988501a69cSRobert Watson 	INP_WLOCK(inp);
999623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
100021367f63SSam Leffler 		error = ECONNRESET;
1001623dce13SRobert Watson 		goto out;
1002623dce13SRobert Watson 	}
1003623dce13SRobert Watson 	tp = intotcpcb(inp);
1004623dce13SRobert Watson 	TCPDEBUG1();
10052c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1006c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
10074cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
10084cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
10092c37256eSGarrett Wollman 		error = EINVAL;
10102c37256eSGarrett Wollman 		goto out;
10112c37256eSGarrett Wollman 	}
10122c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
10132c37256eSGarrett Wollman 		error = EWOULDBLOCK;
10142c37256eSGarrett Wollman 		goto out;
10152c37256eSGarrett Wollman 	}
10162c37256eSGarrett Wollman 	m->m_len = 1;
10172c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
10182c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
10192c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1020623dce13SRobert Watson 
1021623dce13SRobert Watson out:
1022623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
10238501a69cSRobert Watson 	INP_WUNLOCK(inp);
1024623dce13SRobert Watson 	return (error);
10252c37256eSGarrett Wollman }
10262c37256eSGarrett Wollman 
10272c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1028756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1029756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1030756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1031756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1032756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1033756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1034756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1035756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1036756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
103754d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1038756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1039756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1040756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
1041756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
104254d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1043a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1044a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
10452c37256eSGarrett Wollman };
1046df8bae1dSRodney W. Grimes 
1047fb59c426SYoshinobu Inoue #ifdef INET6
1048fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1049756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1050756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1051756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1052756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1053756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1054756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1055756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1056756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1057756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1058756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1059756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1060756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1061756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
1062756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1063756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1064a152f8a3SRobert Watson  	.pru_sosetlabel =	in_pcbsosetlabel,
1065a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1066fb59c426SYoshinobu Inoue };
1067fb59c426SYoshinobu Inoue #endif /* INET6 */
1068fb59c426SYoshinobu Inoue 
1069a0292f23SGarrett Wollman /*
1070a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1071a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
10725200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
10735200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
10745200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
10755200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1076a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1077a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1078a0292f23SGarrett Wollman  */
10790312fbe9SPoul-Henning Kamp static int
1080ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1081a0292f23SGarrett Wollman {
1082a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1083a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
10845200e00eSIan Dowse 	struct in_addr laddr;
10855200e00eSIan Dowse 	u_short lport;
1086c3229e05SDavid Greenman 	int error;
1087a0292f23SGarrett Wollman 
1088623dce13SRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
10898501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1090623dce13SRobert Watson 
1091a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
1092b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1093a0292f23SGarrett Wollman 		if (error)
1094a0292f23SGarrett Wollman 			return error;
1095a0292f23SGarrett Wollman 	}
1096a0292f23SGarrett Wollman 
1097a0292f23SGarrett Wollman 	/*
1098a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1099a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1100a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1101a0292f23SGarrett Wollman 	 */
11025200e00eSIan Dowse 	laddr = inp->inp_laddr;
11035200e00eSIan Dowse 	lport = inp->inp_lport;
11045200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1105b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
11065200e00eSIan Dowse 	if (error && oinp == NULL)
1107d3628763SRodney W. Grimes 		return error;
1108c94c54e4SAndre Oppermann 	if (oinp)
1109a0292f23SGarrett Wollman 		return EADDRINUSE;
11105200e00eSIan Dowse 	inp->inp_laddr = laddr;
111115bd2b43SDavid Greenman 	in_pcbrehash(inp);
1112a0292f23SGarrett Wollman 
1113087b55eaSAndre Oppermann 	/*
1114087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1115087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1116087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1117087b55eaSAndre Oppermann 	 */
1118a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
11199b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1120a0292f23SGarrett Wollman 		tp->request_r_scale++;
1121a0292f23SGarrett Wollman 
1122a0292f23SGarrett Wollman 	soisconnecting(so);
1123a0292f23SGarrett Wollman 	tcpstat.tcps_connattempt++;
1124a0292f23SGarrett Wollman 	tp->t_state = TCPS_SYN_SENT;
1125b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1126b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
11271fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
1128a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1129a45d2726SAndras Olah 
1130a0292f23SGarrett Wollman 	return 0;
1131a0292f23SGarrett Wollman }
1132a0292f23SGarrett Wollman 
1133fb59c426SYoshinobu Inoue #ifdef INET6
1134fb59c426SYoshinobu Inoue static int
1135ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1136fb59c426SYoshinobu Inoue {
1137fb59c426SYoshinobu Inoue 	struct inpcb *inp = tp->t_inpcb, *oinp;
1138fb59c426SYoshinobu Inoue 	struct socket *so = inp->inp_socket;
1139fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1140fb59c426SYoshinobu Inoue 	struct in6_addr *addr6;
1141fb59c426SYoshinobu Inoue 	int error;
1142fb59c426SYoshinobu Inoue 
1143623dce13SRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
11448501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1145623dce13SRobert Watson 
1146fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
1147b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1148fb59c426SYoshinobu Inoue 		if (error)
1149fb59c426SYoshinobu Inoue 			return error;
1150fb59c426SYoshinobu Inoue 	}
1151fb59c426SYoshinobu Inoue 
1152fb59c426SYoshinobu Inoue 	/*
1153fb59c426SYoshinobu Inoue 	 * Cannot simply call in_pcbconnect, because there might be an
1154fb59c426SYoshinobu Inoue 	 * earlier incarnation of this same connection still in
1155fb59c426SYoshinobu Inoue 	 * TIME_WAIT state, creating an ADDRINUSE error.
1156a1f7e5f8SHajimu UMEMOTO 	 * in6_pcbladdr() also handles scope zone IDs.
1157fb59c426SYoshinobu Inoue 	 */
1158fb59c426SYoshinobu Inoue 	error = in6_pcbladdr(inp, nam, &addr6);
1159fb59c426SYoshinobu Inoue 	if (error)
1160fb59c426SYoshinobu Inoue 		return error;
1161fb59c426SYoshinobu Inoue 	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1162fb59c426SYoshinobu Inoue 				  &sin6->sin6_addr, sin6->sin6_port,
1163fb59c426SYoshinobu Inoue 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1164fb59c426SYoshinobu Inoue 				  ? addr6
1165fb59c426SYoshinobu Inoue 				  : &inp->in6p_laddr,
1166fb59c426SYoshinobu Inoue 				  inp->inp_lport,  0, NULL);
1167c94c54e4SAndre Oppermann 	if (oinp)
1168fb59c426SYoshinobu Inoue 		return EADDRINUSE;
1169fb59c426SYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1170fb59c426SYoshinobu Inoue 		inp->in6p_laddr = *addr6;
1171fb59c426SYoshinobu Inoue 	inp->in6p_faddr = sin6->sin6_addr;
1172fb59c426SYoshinobu Inoue 	inp->inp_fport = sin6->sin6_port;
11738a59da30SHajimu UMEMOTO 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
11748a59da30SHajimu UMEMOTO 	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
11758a59da30SHajimu UMEMOTO 	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
11768a59da30SHajimu UMEMOTO 		inp->in6p_flowinfo |=
11778a59da30SHajimu UMEMOTO 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1178fb59c426SYoshinobu Inoue 	in_pcbrehash(inp);
1179fb59c426SYoshinobu Inoue 
1180fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1181fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1182fb59c426SYoshinobu Inoue 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1183fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1184fb59c426SYoshinobu Inoue 
1185fb59c426SYoshinobu Inoue 	soisconnecting(so);
1186fb59c426SYoshinobu Inoue 	tcpstat.tcps_connattempt++;
1187fb59c426SYoshinobu Inoue 	tp->t_state = TCPS_SYN_SENT;
1188b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1189b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
11901fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
1191fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1192fb59c426SYoshinobu Inoue 
1193fb59c426SYoshinobu Inoue 	return 0;
1194fb59c426SYoshinobu Inoue }
1195fb59c426SYoshinobu Inoue #endif /* INET6 */
1196fb59c426SYoshinobu Inoue 
1197cfe8b629SGarrett Wollman /*
1198b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1199b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1200b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1201b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1202b8af5dfaSRobert Watson  * from Linux.
1203b8af5dfaSRobert Watson  */
1204b8af5dfaSRobert Watson static void
1205ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1206b8af5dfaSRobert Watson {
1207b8af5dfaSRobert Watson 
12088501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1209b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1210b8af5dfaSRobert Watson 
1211b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1212b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1213b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
12143529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1215b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1216b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1217b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1218b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1219b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1220b8af5dfaSRobert Watson 	}
12211baaf834SBruce M Simpson 
12221baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
12231baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
12241baaf834SBruce M Simpson 
1225b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1226b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1227b8af5dfaSRobert Watson 
1228b8af5dfaSRobert Watson 	/*
1229b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1230b8af5dfaSRobert Watson 	 */
1231c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1232535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1233b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
1234b8af5dfaSRobert Watson 	ti->tcpi_snd_bwnd = tp->snd_bwnd;
1235535fbad6SKip Macy         ti->tcpi_snd_nxt = tp->snd_nxt;
1236535fbad6SKip Macy         ti->__tcpi_snd_mss = tp->t_maxseg;
1237535fbad6SKip Macy         ti->__tcpi_rcv_mss = tp->t_maxseg;
1238535fbad6SKip Macy         if (tp->t_flags & TF_TOE)
1239535fbad6SKip Macy                 ti->tcpi_options |= TCPI_OPT_TOE;
1240b8af5dfaSRobert Watson }
1241b8af5dfaSRobert Watson 
1242b8af5dfaSRobert Watson /*
12431e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
12441e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
12451e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
12461e8f5ffaSRobert Watson  * option.
1247cfe8b629SGarrett Wollman  */
12488501a69cSRobert Watson #define INP_WLOCK_RECHECK(inp) do {					\
12498501a69cSRobert Watson 	INP_WLOCK(inp);							\
12501e8f5ffaSRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {		\
12518501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
12521e8f5ffaSRobert Watson 		return (ECONNRESET);					\
12531e8f5ffaSRobert Watson 	}								\
12541e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
12551e8f5ffaSRobert Watson } while(0)
12561e8f5ffaSRobert Watson 
1257df8bae1dSRodney W. Grimes int
1258ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1259df8bae1dSRodney W. Grimes {
12603f9d1ef9SRobert Watson 	int	error, opt, optval;
1261df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1262cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
1263b8af5dfaSRobert Watson 	struct	tcp_info ti;
1264df8bae1dSRodney W. Grimes 
1265cfe8b629SGarrett Wollman 	error = 0;
1266df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1267623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
12688501a69cSRobert Watson 	INP_WLOCK(inp);
1269cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1270fb59c426SYoshinobu Inoue #ifdef INET6
12711e8f5ffaSRobert Watson 		if (INP_CHECK_SOCKAF(so, AF_INET6)) {
12728501a69cSRobert Watson 			INP_WUNLOCK(inp);
1273fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
12741e8f5ffaSRobert Watson 		} else {
1275fb59c426SYoshinobu Inoue #endif /* INET6 */
12768501a69cSRobert Watson 			INP_WUNLOCK(inp);
1277cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
12781e8f5ffaSRobert Watson #ifdef INET6
12791e8f5ffaSRobert Watson 		}
12801e8f5ffaSRobert Watson #endif
1281df8bae1dSRodney W. Grimes 		return (error);
1282df8bae1dSRodney W. Grimes 	}
1283623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
12848501a69cSRobert Watson 		INP_WUNLOCK(inp);
12851e8f5ffaSRobert Watson 		return (ECONNRESET);
1286623dce13SRobert Watson 	}
1287df8bae1dSRodney W. Grimes 
1288cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1289cfe8b629SGarrett Wollman 	case SOPT_SET:
1290cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
12911cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
129288f6b043SBruce M Simpson 		case TCP_MD5SIG:
12938501a69cSRobert Watson 			INP_WUNLOCK(inp);
12941cfd4b53SBruce M Simpson 			error = sooptcopyin(sopt, &optval, sizeof optval,
12951cfd4b53SBruce M Simpson 			    sizeof optval);
12961cfd4b53SBruce M Simpson 			if (error)
12971e8f5ffaSRobert Watson 				return (error);
12981cfd4b53SBruce M Simpson 
12998501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
13001cfd4b53SBruce M Simpson 			if (optval > 0)
13011cfd4b53SBruce M Simpson 				tp->t_flags |= TF_SIGNATURE;
13021cfd4b53SBruce M Simpson 			else
13031cfd4b53SBruce M Simpson 				tp->t_flags &= ~TF_SIGNATURE;
13048501a69cSRobert Watson 			INP_WUNLOCK(inp);
13051cfd4b53SBruce M Simpson 			break;
13061cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
1307df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1308cfe8b629SGarrett Wollman 		case TCP_NOOPT:
13098501a69cSRobert Watson 			INP_WUNLOCK(inp);
1310cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1311cfe8b629SGarrett Wollman 			    sizeof optval);
1312cfe8b629SGarrett Wollman 			if (error)
13131e8f5ffaSRobert Watson 				return (error);
1314cfe8b629SGarrett Wollman 
13158501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1316cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1317cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1318cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1319cfe8b629SGarrett Wollman 				break;
1320cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1321cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1322cfe8b629SGarrett Wollman 				break;
1323cfe8b629SGarrett Wollman 			default:
1324cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1325cfe8b629SGarrett Wollman 				break;
1326cfe8b629SGarrett Wollman 			}
1327cfe8b629SGarrett Wollman 
1328cfe8b629SGarrett Wollman 			if (optval)
1329cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1330df8bae1dSRodney W. Grimes 			else
1331cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
13328501a69cSRobert Watson 			INP_WUNLOCK(inp);
1333df8bae1dSRodney W. Grimes 			break;
1334df8bae1dSRodney W. Grimes 
1335007581c0SJonathan Lemon 		case TCP_NOPUSH:
13368501a69cSRobert Watson 			INP_WUNLOCK(inp);
1337007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1338007581c0SJonathan Lemon 			    sizeof optval);
1339007581c0SJonathan Lemon 			if (error)
13401e8f5ffaSRobert Watson 				return (error);
1341007581c0SJonathan Lemon 
13428501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1343007581c0SJonathan Lemon 			if (optval)
1344007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1345007581c0SJonathan Lemon 			else {
1346007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1347007581c0SJonathan Lemon 				error = tcp_output(tp);
1348007581c0SJonathan Lemon 			}
13498501a69cSRobert Watson 			INP_WUNLOCK(inp);
1350007581c0SJonathan Lemon 			break;
1351007581c0SJonathan Lemon 
1352df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
13538501a69cSRobert Watson 			INP_WUNLOCK(inp);
1354cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1355cfe8b629SGarrett Wollman 			    sizeof optval);
1356cfe8b629SGarrett Wollman 			if (error)
13571e8f5ffaSRobert Watson 				return (error);
1358df8bae1dSRodney W. Grimes 
13598501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
136053369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
136153369ac9SAndre Oppermann 			    optval + 40 >= tcp_minmss)
1362cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1363a0292f23SGarrett Wollman 			else
1364a0292f23SGarrett Wollman 				error = EINVAL;
13658501a69cSRobert Watson 			INP_WUNLOCK(inp);
1366a0292f23SGarrett Wollman 			break;
1367a0292f23SGarrett Wollman 
1368b8af5dfaSRobert Watson 		case TCP_INFO:
13698501a69cSRobert Watson 			INP_WUNLOCK(inp);
1370b8af5dfaSRobert Watson 			error = EINVAL;
1371b8af5dfaSRobert Watson 			break;
1372b8af5dfaSRobert Watson 
1373df8bae1dSRodney W. Grimes 		default:
13748501a69cSRobert Watson 			INP_WUNLOCK(inp);
1375df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1376df8bae1dSRodney W. Grimes 			break;
1377df8bae1dSRodney W. Grimes 		}
1378df8bae1dSRodney W. Grimes 		break;
1379df8bae1dSRodney W. Grimes 
1380cfe8b629SGarrett Wollman 	case SOPT_GET:
13811e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
1382cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
13831cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
138488f6b043SBruce M Simpson 		case TCP_MD5SIG:
13851cfd4b53SBruce M Simpson 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
13868501a69cSRobert Watson 			INP_WUNLOCK(inp);
1387b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
13881cfd4b53SBruce M Simpson 			break;
1389265ed012SBruce M Simpson #endif
13901e8f5ffaSRobert Watson 
1391df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1392cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
13938501a69cSRobert Watson 			INP_WUNLOCK(inp);
1394b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1395df8bae1dSRodney W. Grimes 			break;
1396df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1397cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
13988501a69cSRobert Watson 			INP_WUNLOCK(inp);
1399b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1400df8bae1dSRodney W. Grimes 			break;
1401a0292f23SGarrett Wollman 		case TCP_NOOPT:
1402cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
14038501a69cSRobert Watson 			INP_WUNLOCK(inp);
1404b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1405a0292f23SGarrett Wollman 			break;
1406a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1407cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
14088501a69cSRobert Watson 			INP_WUNLOCK(inp);
1409b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1410b8af5dfaSRobert Watson 			break;
1411b8af5dfaSRobert Watson 		case TCP_INFO:
1412b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
14138501a69cSRobert Watson 			INP_WUNLOCK(inp);
1414b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
1415a0292f23SGarrett Wollman 			break;
1416df8bae1dSRodney W. Grimes 		default:
14178501a69cSRobert Watson 			INP_WUNLOCK(inp);
1418df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1419df8bae1dSRodney W. Grimes 			break;
1420df8bae1dSRodney W. Grimes 		}
1421df8bae1dSRodney W. Grimes 		break;
1422df8bae1dSRodney W. Grimes 	}
1423df8bae1dSRodney W. Grimes 	return (error);
1424df8bae1dSRodney W. Grimes }
14258501a69cSRobert Watson #undef INP_WLOCK_RECHECK
1426df8bae1dSRodney W. Grimes 
142726e30fbbSDavid Greenman /*
142826e30fbbSDavid Greenman  * tcp_sendspace and tcp_recvspace are the default send and receive window
142926e30fbbSDavid Greenman  * sizes, respectively.  These are obsolescent (this information should
143026e30fbbSDavid Greenman  * be set by the route).
143126e30fbbSDavid Greenman  */
143281e561cdSDavid E. O'Brien u_long	tcp_sendspace = 1024*32;
1433e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
14343d177f46SBill Fumerola     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
143581e561cdSDavid E. O'Brien u_long	tcp_recvspace = 1024*64;
1436e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
14373d177f46SBill Fumerola     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1438df8bae1dSRodney W. Grimes 
1439df8bae1dSRodney W. Grimes /*
1440df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1441df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1442df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1443df8bae1dSRodney W. Grimes  */
14440312fbe9SPoul-Henning Kamp static int
1445ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so)
1446df8bae1dSRodney W. Grimes {
1447ad3f9ab3SAndre Oppermann 	struct tcpcb *tp;
1448df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1449df8bae1dSRodney W. Grimes 	int error;
1450fb59c426SYoshinobu Inoue #ifdef INET6
14514a6a94d8SArchie Cobbs 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1452fb59c426SYoshinobu Inoue #endif
1453df8bae1dSRodney W. Grimes 
1454df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1455df8bae1dSRodney W. Grimes 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1456df8bae1dSRodney W. Grimes 		if (error)
1457df8bae1dSRodney W. Grimes 			return (error);
1458df8bae1dSRodney W. Grimes 	}
14596741ecf5SAndre Oppermann 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
14606741ecf5SAndre Oppermann 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1461f2de87feSRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
1462d915b280SStephan Uphoff 	error = in_pcballoc(so, &tcbinfo);
1463f2de87feSRobert Watson 	if (error) {
1464f2de87feSRobert Watson 		INP_INFO_WUNLOCK(&tcbinfo);
1465df8bae1dSRodney W. Grimes 		return (error);
1466f2de87feSRobert Watson 	}
1467df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1468fb59c426SYoshinobu Inoue #ifdef INET6
1469fb59c426SYoshinobu Inoue 	if (isipv6) {
1470fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1471fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1472fb59c426SYoshinobu Inoue 	}
1473fb59c426SYoshinobu Inoue 	else
1474fb59c426SYoshinobu Inoue #endif
1475cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1476df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1477623dce13SRobert Watson 	if (tp == NULL) {
1478fb59c426SYoshinobu Inoue #ifdef INET6
1479623dce13SRobert Watson 		if (isipv6) {
1480fb59c426SYoshinobu Inoue 			in6_pcbdetach(inp);
1481623dce13SRobert Watson 			in6_pcbfree(inp);
1482623dce13SRobert Watson 		} else {
1483fb59c426SYoshinobu Inoue #endif
1484df8bae1dSRodney W. Grimes 			in_pcbdetach(inp);
1485623dce13SRobert Watson 			in_pcbfree(inp);
1486623dce13SRobert Watson #ifdef INET6
1487623dce13SRobert Watson 		}
1488623dce13SRobert Watson #endif
1489f2de87feSRobert Watson 		INP_INFO_WUNLOCK(&tcbinfo);
1490df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1491df8bae1dSRodney W. Grimes 	}
1492df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
14938501a69cSRobert Watson 	INP_WUNLOCK(inp);
1494f2de87feSRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
1495df8bae1dSRodney W. Grimes 	return (0);
1496df8bae1dSRodney W. Grimes }
1497df8bae1dSRodney W. Grimes 
1498df8bae1dSRodney W. Grimes /*
1499df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1500df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1501df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1502df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1503df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1504df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1505df8bae1dSRodney W. Grimes  */
1506623dce13SRobert Watson static void
1507ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
1508df8bae1dSRodney W. Grimes {
1509e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
1510e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
1511e6e0b5ffSRobert Watson 
1512e6e0b5ffSRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
15138501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1514df8bae1dSRodney W. Grimes 
1515623dce13SRobert Watson 	/*
1516623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1517623dce13SRobert Watson 	 * socket is still open.
1518623dce13SRobert Watson 	 */
1519623dce13SRobert Watson 	if (tp->t_state < TCPS_ESTABLISHED) {
1520df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1521623dce13SRobert Watson 		KASSERT(tp != NULL,
1522623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
1523623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1524243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
1525623dce13SRobert Watson 		KASSERT(tp != NULL,
1526623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
1527623dce13SRobert Watson 	} else {
1528df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
1529df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
1530623dce13SRobert Watson 		tcp_usrclosed(tp);
1531953b5606SRobert Watson 		if (!(inp->inp_vflag & INP_DROPPED))
1532bc65987aSKip Macy 			tcp_output_disconnect(tp);
1533df8bae1dSRodney W. Grimes 	}
1534df8bae1dSRodney W. Grimes }
1535df8bae1dSRodney W. Grimes 
1536df8bae1dSRodney W. Grimes /*
1537df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1538df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1539df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1540df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1541df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1542df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1543df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1544df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1545df8bae1dSRodney W. Grimes  */
1546623dce13SRobert Watson static void
1547ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
1548df8bae1dSRodney W. Grimes {
1549df8bae1dSRodney W. Grimes 
1550e6e0b5ffSRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
15518501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1552e6e0b5ffSRobert Watson 
1553df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1554df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
1555bc65987aSKip Macy 		tcp_offload_listen_close(tp);
1556bc65987aSKip Macy 		/* FALLTHROUGH */
1557bc65987aSKip Macy 	case TCPS_CLOSED:
1558df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_CLOSED;
1559df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1560623dce13SRobert Watson 		/*
1561623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
1562623dce13SRobert Watson 		 * still open.
1563623dce13SRobert Watson 		 */
1564623dce13SRobert Watson 		KASSERT(tp != NULL,
1565623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
1566df8bae1dSRodney W. Grimes 		break;
1567df8bae1dSRodney W. Grimes 
1568a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
1569df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1570a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
1571a0292f23SGarrett Wollman 		break;
1572a0292f23SGarrett Wollman 
1573df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1574df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_FIN_WAIT_1;
1575df8bae1dSRodney W. Grimes 		break;
1576df8bae1dSRodney W. Grimes 
1577df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1578df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_LAST_ACK;
1579df8bae1dSRodney W. Grimes 		break;
1580df8bae1dSRodney W. Grimes 	}
1581abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
1582df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
1583abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
15847c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
15857c72af87SMohan Srinivasan 			int timeout;
15867c72af87SMohan Srinivasan 
15877c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
15887c72af87SMohan Srinivasan 			    tcp_finwait2_timeout : tcp_maxidle;
1589b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
1590b6239c4aSAndras Olah 		}
1591df8bae1dSRodney W. Grimes 	}
15927c72af87SMohan Srinivasan }
1593497057eeSRobert Watson 
1594497057eeSRobert Watson #ifdef DDB
1595497057eeSRobert Watson static void
1596497057eeSRobert Watson db_print_indent(int indent)
1597497057eeSRobert Watson {
1598497057eeSRobert Watson 	int i;
1599497057eeSRobert Watson 
1600497057eeSRobert Watson 	for (i = 0; i < indent; i++)
1601497057eeSRobert Watson 		db_printf(" ");
1602497057eeSRobert Watson }
1603497057eeSRobert Watson 
1604497057eeSRobert Watson static void
1605497057eeSRobert Watson db_print_tstate(int t_state)
1606497057eeSRobert Watson {
1607497057eeSRobert Watson 
1608497057eeSRobert Watson 	switch (t_state) {
1609497057eeSRobert Watson 	case TCPS_CLOSED:
1610497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
1611497057eeSRobert Watson 		return;
1612497057eeSRobert Watson 
1613497057eeSRobert Watson 	case TCPS_LISTEN:
1614497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
1615497057eeSRobert Watson 		return;
1616497057eeSRobert Watson 
1617497057eeSRobert Watson 	case TCPS_SYN_SENT:
1618497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
1619497057eeSRobert Watson 		return;
1620497057eeSRobert Watson 
1621497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
1622497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
1623497057eeSRobert Watson 		return;
1624497057eeSRobert Watson 
1625497057eeSRobert Watson 	case TCPS_ESTABLISHED:
1626497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
1627497057eeSRobert Watson 		return;
1628497057eeSRobert Watson 
1629497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
1630497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
1631497057eeSRobert Watson 		return;
1632497057eeSRobert Watson 
1633497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
1634497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
1635497057eeSRobert Watson 		return;
1636497057eeSRobert Watson 
1637497057eeSRobert Watson 	case TCPS_CLOSING:
1638497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
1639497057eeSRobert Watson 		return;
1640497057eeSRobert Watson 
1641497057eeSRobert Watson 	case TCPS_LAST_ACK:
1642497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
1643497057eeSRobert Watson 		return;
1644497057eeSRobert Watson 
1645497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
1646497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
1647497057eeSRobert Watson 		return;
1648497057eeSRobert Watson 
1649497057eeSRobert Watson 	case TCPS_TIME_WAIT:
1650497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
1651497057eeSRobert Watson 		return;
1652497057eeSRobert Watson 
1653497057eeSRobert Watson 	default:
1654497057eeSRobert Watson 		db_printf("unknown");
1655497057eeSRobert Watson 		return;
1656497057eeSRobert Watson 	}
1657497057eeSRobert Watson }
1658497057eeSRobert Watson 
1659497057eeSRobert Watson static void
1660497057eeSRobert Watson db_print_tflags(u_int t_flags)
1661497057eeSRobert Watson {
1662497057eeSRobert Watson 	int comma;
1663497057eeSRobert Watson 
1664497057eeSRobert Watson 	comma = 0;
1665497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
1666497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
1667497057eeSRobert Watson 		comma = 1;
1668497057eeSRobert Watson 	}
1669497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
1670497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
1671497057eeSRobert Watson 		comma = 1;
1672497057eeSRobert Watson 	}
1673497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
1674497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
1675497057eeSRobert Watson 		comma = 1;
1676497057eeSRobert Watson 	}
1677497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
1678497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
1679497057eeSRobert Watson 		comma = 1;
1680497057eeSRobert Watson 	}
1681497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
1682497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
1683497057eeSRobert Watson 		comma = 1;
1684497057eeSRobert Watson 	}
1685497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
1686497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1687497057eeSRobert Watson 		comma = 1;
1688497057eeSRobert Watson 	}
1689497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
1690497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1691497057eeSRobert Watson 		comma = 1;
1692497057eeSRobert Watson 	}
1693497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
1694497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1695497057eeSRobert Watson 		comma = 1;
1696497057eeSRobert Watson 	}
1697497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
1698497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1699497057eeSRobert Watson 		comma = 1;
1700497057eeSRobert Watson 	}
1701497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
1702497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1703497057eeSRobert Watson 		comma = 1;
1704497057eeSRobert Watson 	}
1705497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
1706497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1707497057eeSRobert Watson 		comma = 1;
1708497057eeSRobert Watson 	}
1709497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
1710497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1711497057eeSRobert Watson 		comma = 1;
1712497057eeSRobert Watson 	}
1713497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
1714497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
1715497057eeSRobert Watson 		comma = 1;
1716497057eeSRobert Watson 	}
1717497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
1718497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
1719497057eeSRobert Watson 		comma = 1;
1720497057eeSRobert Watson 	}
1721497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
1722497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1723497057eeSRobert Watson 		comma = 1;
1724497057eeSRobert Watson 	}
1725497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
1726497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1727497057eeSRobert Watson 		comma = 1;
1728497057eeSRobert Watson 	}
1729497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
1730497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1731497057eeSRobert Watson 		comma = 1;
1732497057eeSRobert Watson 	}
1733497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
1734497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1735497057eeSRobert Watson 		comma = 1;
1736497057eeSRobert Watson 	}
1737497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
1738497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1739497057eeSRobert Watson 		comma = 1;
1740497057eeSRobert Watson 	}
1741497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
1742497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1743497057eeSRobert Watson 		comma = 1;
1744497057eeSRobert Watson 	}
1745497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
1746497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1747497057eeSRobert Watson 		comma = 1;
1748497057eeSRobert Watson 	}
1749497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
1750497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1751497057eeSRobert Watson 		comma = 1;
1752497057eeSRobert Watson 	}
1753497057eeSRobert Watson 	if (t_flags & TF_TSO) {
1754497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
1755497057eeSRobert Watson 		comma = 1;
1756497057eeSRobert Watson 	}
1757497057eeSRobert Watson }
1758497057eeSRobert Watson 
1759497057eeSRobert Watson static void
1760497057eeSRobert Watson db_print_toobflags(char t_oobflags)
1761497057eeSRobert Watson {
1762497057eeSRobert Watson 	int comma;
1763497057eeSRobert Watson 
1764497057eeSRobert Watson 	comma = 0;
1765497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
1766497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1767497057eeSRobert Watson 		comma = 1;
1768497057eeSRobert Watson 	}
1769497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
1770497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1771497057eeSRobert Watson 		comma = 1;
1772497057eeSRobert Watson 	}
1773497057eeSRobert Watson }
1774497057eeSRobert Watson 
1775497057eeSRobert Watson static void
1776497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1777497057eeSRobert Watson {
1778497057eeSRobert Watson 
1779497057eeSRobert Watson 	db_print_indent(indent);
1780497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
1781497057eeSRobert Watson 
1782497057eeSRobert Watson 	indent += 2;
1783497057eeSRobert Watson 
1784497057eeSRobert Watson 	db_print_indent(indent);
1785497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
1786497057eeSRobert Watson 	   LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
1787497057eeSRobert Watson 
1788497057eeSRobert Watson 	db_print_indent(indent);
178985d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
1790e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
1791497057eeSRobert Watson 
1792497057eeSRobert Watson 	db_print_indent(indent);
1793e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
1794e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
1795497057eeSRobert Watson 
1796497057eeSRobert Watson 	db_print_indent(indent);
1797497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
1798497057eeSRobert Watson 	db_print_tstate(tp->t_state);
1799497057eeSRobert Watson 	db_printf(")\n");
1800497057eeSRobert Watson 
1801497057eeSRobert Watson 	db_print_indent(indent);
1802497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
1803497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
1804497057eeSRobert Watson 	db_printf(")\n");
1805497057eeSRobert Watson 
1806497057eeSRobert Watson 	db_print_indent(indent);
1807497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
1808497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
1809497057eeSRobert Watson 
1810497057eeSRobert Watson 	db_print_indent(indent);
1811497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
1812497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
1813497057eeSRobert Watson 
1814497057eeSRobert Watson 	db_print_indent(indent);
1815497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
1816497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
1817497057eeSRobert Watson 
1818497057eeSRobert Watson 	db_print_indent(indent);
1819497057eeSRobert Watson 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %lu   rcv_up: 0x%08x\n",
1820497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
1821497057eeSRobert Watson 
1822497057eeSRobert Watson 	db_print_indent(indent);
1823497057eeSRobert Watson 	db_printf("snd_wnd: %lu   snd_cwnd: %lu   snd_bwnd: %lu\n",
1824497057eeSRobert Watson 	   tp->snd_wnd, tp->snd_cwnd, tp->snd_bwnd);
1825497057eeSRobert Watson 
1826497057eeSRobert Watson 	db_print_indent(indent);
1827497057eeSRobert Watson 	db_printf("snd_ssthresh: %lu   snd_bandwidth: %lu   snd_recover: "
1828497057eeSRobert Watson 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_bandwidth,
1829497057eeSRobert Watson 	    tp->snd_recover);
1830497057eeSRobert Watson 
1831497057eeSRobert Watson 	db_print_indent(indent);
1832497057eeSRobert Watson 	db_printf("t_maxopd: %u   t_rcvtime: %lu   t_startime: %lu\n",
1833497057eeSRobert Watson 	    tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
1834497057eeSRobert Watson 
1835497057eeSRobert Watson 	db_print_indent(indent);
1836497057eeSRobert Watson 	db_printf("t_rttime: %d   t_rtsq: 0x%08x   t_bw_rtttime: %d\n",
1837497057eeSRobert Watson 	    tp->t_rtttime, tp->t_rtseq, tp->t_bw_rtttime);
1838497057eeSRobert Watson 
1839497057eeSRobert Watson 	db_print_indent(indent);
1840497057eeSRobert Watson 	db_printf("t_bw_rtseq: 0x%08x   t_rxtcur: %d   t_maxseg: %u   "
1841497057eeSRobert Watson 	    "t_srtt: %d\n", tp->t_bw_rtseq, tp->t_rxtcur, tp->t_maxseg,
1842497057eeSRobert Watson 	    tp->t_srtt);
1843497057eeSRobert Watson 
1844497057eeSRobert Watson 	db_print_indent(indent);
1845497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
1846497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
1847497057eeSRobert Watson 	    tp->t_rttbest);
1848497057eeSRobert Watson 
1849497057eeSRobert Watson 	db_print_indent(indent);
1850497057eeSRobert Watson 	db_printf("t_rttupdated: %lu   max_sndwnd: %lu   t_softerror: %d\n",
1851497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
1852497057eeSRobert Watson 
1853497057eeSRobert Watson 	db_print_indent(indent);
1854497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
1855497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
1856497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
1857497057eeSRobert Watson 
1858497057eeSRobert Watson 	db_print_indent(indent);
1859497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
1860497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
1861497057eeSRobert Watson 
1862497057eeSRobert Watson 	db_print_indent(indent);
18631a553740SAndre Oppermann 	db_printf("ts_recent: %u   ts_recent_age: %lu\n",
18641a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
1865497057eeSRobert Watson 
1866497057eeSRobert Watson 	db_print_indent(indent);
1867497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
1868497057eeSRobert Watson 	    "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
1869497057eeSRobert Watson 
1870497057eeSRobert Watson 	db_print_indent(indent);
1871497057eeSRobert Watson 	db_printf("snd_ssthresh_prev: %lu   snd_recover_prev: 0x%08x   "
1872497057eeSRobert Watson 	    "t_badrxtwin: %lu\n", tp->snd_ssthresh_prev,
1873497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
1874497057eeSRobert Watson 
1875497057eeSRobert Watson 	db_print_indent(indent);
18763529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
18773529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
1878497057eeSRobert Watson 
1879497057eeSRobert Watson 	db_print_indent(indent);
1880497057eeSRobert Watson 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
1881497057eeSRobert Watson 	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
1882497057eeSRobert Watson 
1883497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
1884497057eeSRobert Watson 
1885497057eeSRobert Watson 	db_print_indent(indent);
1886497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
1887497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
1888497057eeSRobert Watson }
1889497057eeSRobert Watson 
1890497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
1891497057eeSRobert Watson {
1892497057eeSRobert Watson 	struct tcpcb *tp;
1893497057eeSRobert Watson 
1894497057eeSRobert Watson 	if (!have_addr) {
1895497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
1896497057eeSRobert Watson 		return;
1897497057eeSRobert Watson 	}
1898497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
1899497057eeSRobert Watson 
1900497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
1901497057eeSRobert Watson }
1902497057eeSRobert Watson #endif
1903