xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 2cbcd3c19841b6b025d9d9a27c09681bd83ae156)
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
5fa046d87SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
6623dce13SRobert Watson  * All rights reserved.
7df8bae1dSRodney W. Grimes  *
8fa046d87SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
9fa046d87SRobert Watson  * contract to Juniper Networks, Inc.
10fa046d87SRobert Watson  *
11df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
12df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
13df8bae1dSRodney W. Grimes  * are met:
14df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
15df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
16df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
18df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
20df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
21df8bae1dSRodney W. Grimes  *    without specific prior written permission.
22df8bae1dSRodney W. Grimes  *
23df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
34df8bae1dSRodney W. Grimes  *
351fdbc7aeSGarrett Wollman  *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
36df8bae1dSRodney W. Grimes  */
37df8bae1dSRodney W. Grimes 
384b421e2dSMike Silbersack #include <sys/cdefs.h>
394b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
404b421e2dSMike Silbersack 
41497057eeSRobert Watson #include "opt_ddb.h"
421cfd4b53SBruce M Simpson #include "opt_inet.h"
43fb59c426SYoshinobu Inoue #include "opt_inet6.h"
440cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
450cc12cc5SJoerg Wunsch 
46df8bae1dSRodney W. Grimes #include <sys/param.h>
47df8bae1dSRodney W. Grimes #include <sys/systm.h>
489077f387SGleb Smirnoff #include <sys/limits.h>
49f76fcf6dSJeffrey Hsu #include <sys/malloc.h>
50c7a82f90SGarrett Wollman #include <sys/kernel.h>
5198163b98SPoul-Henning Kamp #include <sys/sysctl.h>
52df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
53fb59c426SYoshinobu Inoue #ifdef INET6
54fb59c426SYoshinobu Inoue #include <sys/domain.h>
55fb59c426SYoshinobu Inoue #endif /* INET6 */
56df8bae1dSRodney W. Grimes #include <sys/socket.h>
57df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
58df8bae1dSRodney W. Grimes #include <sys/protosw.h>
5991421ba2SRobert Watson #include <sys/proc.h>
6091421ba2SRobert Watson #include <sys/jail.h>
61df8bae1dSRodney W. Grimes 
62497057eeSRobert Watson #ifdef DDB
63497057eeSRobert Watson #include <ddb/ddb.h>
64497057eeSRobert Watson #endif
65497057eeSRobert Watson 
66df8bae1dSRodney W. Grimes #include <net/if.h>
6776039bc8SGleb Smirnoff #include <net/if_var.h>
68df8bae1dSRodney W. Grimes #include <net/route.h>
69530c0060SRobert Watson #include <net/vnet.h>
70df8bae1dSRodney W. Grimes 
71dbc42409SLawrence Stewart #include <netinet/cc.h>
72df8bae1dSRodney W. Grimes #include <netinet/in.h>
73df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
74b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h>
75b5e8ce9fSBruce Evans #include <netinet/in_var.h>
76df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
77fb59c426SYoshinobu Inoue #ifdef INET6
78b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h>
79b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
80fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
81a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
82fb59c426SYoshinobu Inoue #endif
83df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
84df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
85df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
86df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
87df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
88610ee2f9SDavid Greenman #ifdef TCPDEBUG
89df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
90610ee2f9SDavid Greenman #endif
9109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
92bc65987aSKip Macy #include <netinet/tcp_offload.h>
9309fe6320SNavdeep Parhar #endif
94df8bae1dSRodney W. Grimes 
95df8bae1dSRodney W. Grimes /*
96df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
97df8bae1dSRodney W. Grimes  */
9856dc72c3SPawel Jakub Dawidek static int	tcp_attach(struct socket *);
99b287c6c7SBjoern A. Zeeb #ifdef INET
1004d77a549SAlfred Perlstein static int	tcp_connect(struct tcpcb *, struct sockaddr *,
1014d77a549SAlfred Perlstein 		    struct thread *td);
102b287c6c7SBjoern A. Zeeb #endif /* INET */
103fb59c426SYoshinobu Inoue #ifdef INET6
1044d77a549SAlfred Perlstein static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
1054d77a549SAlfred Perlstein 		    struct thread *td);
106fb59c426SYoshinobu Inoue #endif /* INET6 */
107623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
108623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
109b8af5dfaSRobert Watson static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
1102c37256eSGarrett Wollman 
1112c37256eSGarrett Wollman #ifdef TCPDEBUG
1121db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1132c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
1144cc20ab1SSeigo Tanimura #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
1154cc20ab1SSeigo Tanimura 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1162c37256eSGarrett Wollman #else
1172c37256eSGarrett Wollman #define	TCPDEBUG0
1182c37256eSGarrett Wollman #define	TCPDEBUG1()
1192c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1202c37256eSGarrett Wollman #endif
1212c37256eSGarrett Wollman 
1222c37256eSGarrett Wollman /*
1232c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1242c37256eSGarrett Wollman  * and an internet control block.
1252c37256eSGarrett Wollman  */
1262c37256eSGarrett Wollman static int
127b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1282c37256eSGarrett Wollman {
129f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
130623dce13SRobert Watson 	struct tcpcb *tp = NULL;
131623dce13SRobert Watson 	int error;
1322c37256eSGarrett Wollman 	TCPDEBUG0;
1332c37256eSGarrett Wollman 
134623dce13SRobert Watson 	inp = sotoinpcb(so);
135623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1362c37256eSGarrett Wollman 	TCPDEBUG1();
1372c37256eSGarrett Wollman 
13856dc72c3SPawel Jakub Dawidek 	error = tcp_attach(so);
1392c37256eSGarrett Wollman 	if (error)
1402c37256eSGarrett Wollman 		goto out;
1412c37256eSGarrett Wollman 
1422c37256eSGarrett Wollman 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1433879597fSAndrey A. Chernov 		so->so_linger = TCP_LINGERTIME;
144f76fcf6dSJeffrey Hsu 
145f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
146f76fcf6dSJeffrey Hsu 	tp = intotcpcb(inp);
1472c37256eSGarrett Wollman out:
1482c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
1492c37256eSGarrett Wollman 	return error;
1502c37256eSGarrett Wollman }
1512c37256eSGarrett Wollman 
1522c37256eSGarrett Wollman /*
153a152f8a3SRobert Watson  * tcp_detach is called when the socket layer loses its final reference
154a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
155a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
156a152f8a3SRobert Watson  * inpcb state: time wait.
157c78cbc7bSRobert Watson  *
158a152f8a3SRobert Watson  * This function can probably be re-absorbed back into tcp_usr_detach() now
159a152f8a3SRobert Watson  * that there is a single detach path.
1602c37256eSGarrett Wollman  */
161bc725eafSRobert Watson static void
162c78cbc7bSRobert Watson tcp_detach(struct socket *so, struct inpcb *inp)
1632c37256eSGarrett Wollman {
1642c37256eSGarrett Wollman 	struct tcpcb *tp;
1652c37256eSGarrett Wollman 
166603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1678501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
168623dce13SRobert Watson 
169c78cbc7bSRobert Watson 	KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
170c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
171953b5606SRobert Watson 
172a152f8a3SRobert Watson 	tp = intotcpcb(inp);
173a152f8a3SRobert Watson 
174ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
175623dce13SRobert Watson 		/*
176a152f8a3SRobert Watson 		 * There are two cases to handle: one in which the time wait
177a152f8a3SRobert Watson 		 * state is being discarded (INP_DROPPED), and one in which
178a152f8a3SRobert Watson 		 * this connection will remain in timewait.  In the former,
179a152f8a3SRobert Watson 		 * it is time to discard all state (except tcptw, which has
180a152f8a3SRobert Watson 		 * already been discarded by the timewait close code, which
181a152f8a3SRobert Watson 		 * should be further up the call stack somewhere).  In the
182a152f8a3SRobert Watson 		 * latter case, we detach from the socket, but leave the pcb
183a152f8a3SRobert Watson 		 * present until timewait ends.
184623dce13SRobert Watson 		 *
185a152f8a3SRobert Watson 		 * XXXRW: Would it be cleaner to free the tcptw here?
186cea40c48SJulien Charbon 		 *
187cea40c48SJulien Charbon 		 * Astute question indeed, from twtcp perspective there are
188cea40c48SJulien Charbon 		 * three cases to consider:
189cea40c48SJulien Charbon 		 *
190cea40c48SJulien Charbon 		 * #1 tcp_detach is called at tcptw creation time by
191cea40c48SJulien Charbon 		 *  tcp_twstart, then do not discard the newly created tcptw
192cea40c48SJulien Charbon 		 *  and leave inpcb present until timewait ends
193cea40c48SJulien Charbon 		 * #2 tcp_detach is called at timewait end (or reuse) by
194cea40c48SJulien Charbon 		 *  tcp_twclose, then the tcptw has already been discarded
195cea40c48SJulien Charbon 		 *  and inpcb is freed here
196cea40c48SJulien Charbon 		 * #3 tcp_detach is called() after timewait ends (or reuse)
197cea40c48SJulien Charbon 		 *  (e.g. by soclose), then tcptw has already been discarded
198cea40c48SJulien Charbon 		 *  and inpcb is freed here
199cea40c48SJulien Charbon 		 *
200cea40c48SJulien Charbon 		 *  In all three cases the tcptw should not be freed here.
201623dce13SRobert Watson 		 */
202ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED) {
203a152f8a3SRobert Watson 			KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
204a152f8a3SRobert Watson 			    "INP_DROPPED && tp != NULL"));
205623dce13SRobert Watson 			in_pcbdetach(inp);
2060206cdb8SBjoern A. Zeeb 			in_pcbfree(inp);
2070206cdb8SBjoern A. Zeeb 		} else {
208623dce13SRobert Watson 			in_pcbdetach(inp);
2098501a69cSRobert Watson 			INP_WUNLOCK(inp);
210623dce13SRobert Watson 		}
211623dce13SRobert Watson 	} else {
212e6e65783SRobert Watson 		/*
213a152f8a3SRobert Watson 		 * If the connection is not in timewait, we consider two
214a152f8a3SRobert Watson 		 * two conditions: one in which no further processing is
215a152f8a3SRobert Watson 		 * necessary (dropped || embryonic), and one in which TCP is
216a152f8a3SRobert Watson 		 * not yet done, but no longer requires the socket, so the
217a152f8a3SRobert Watson 		 * pcb will persist for the time being.
218a152f8a3SRobert Watson 		 *
219a152f8a3SRobert Watson 		 * XXXRW: Does the second case still occur?
220e6e65783SRobert Watson 		 */
221ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED ||
222623dce13SRobert Watson 		    tp->t_state < TCPS_SYN_SENT) {
223623dce13SRobert Watson 			tcp_discardcb(tp);
224623dce13SRobert Watson 			in_pcbdetach(inp);
2250206cdb8SBjoern A. Zeeb 			in_pcbfree(inp);
226db3cee51SNavdeep Parhar 		} else {
227a152f8a3SRobert Watson 			in_pcbdetach(inp);
228db3cee51SNavdeep Parhar 			INP_WUNLOCK(inp);
229db3cee51SNavdeep Parhar 		}
230623dce13SRobert Watson 	}
231623dce13SRobert Watson }
232c78cbc7bSRobert Watson 
233c78cbc7bSRobert Watson /*
234c78cbc7bSRobert Watson  * pru_detach() detaches the TCP protocol from the socket.
235c78cbc7bSRobert Watson  * If the protocol state is non-embryonic, then can't
236c78cbc7bSRobert Watson  * do this directly: have to initiate a pru_disconnect(),
237c78cbc7bSRobert Watson  * which may finish later; embryonic TCB's can just
238c78cbc7bSRobert Watson  * be discarded here.
239c78cbc7bSRobert Watson  */
240c78cbc7bSRobert Watson static void
241c78cbc7bSRobert Watson tcp_usr_detach(struct socket *so)
242c78cbc7bSRobert Watson {
243c78cbc7bSRobert Watson 	struct inpcb *inp;
244c78cbc7bSRobert Watson 
245c78cbc7bSRobert Watson 	inp = sotoinpcb(so);
246c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
247603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
2488501a69cSRobert Watson 	INP_WLOCK(inp);
249c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
250c78cbc7bSRobert Watson 	    ("tcp_usr_detach: inp_socket == NULL"));
251c78cbc7bSRobert Watson 	tcp_detach(so, inp);
252603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
2532c37256eSGarrett Wollman }
2542c37256eSGarrett Wollman 
255b287c6c7SBjoern A. Zeeb #ifdef INET
2562c37256eSGarrett Wollman /*
2572c37256eSGarrett Wollman  * Give the socket an address.
2582c37256eSGarrett Wollman  */
2592c37256eSGarrett Wollman static int
260b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2612c37256eSGarrett Wollman {
2622c37256eSGarrett Wollman 	int error = 0;
263f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
264623dce13SRobert Watson 	struct tcpcb *tp = NULL;
2652c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
2662c37256eSGarrett Wollman 
26752710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
26852710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sinp))
26952710de1SPawel Jakub Dawidek 		return (EINVAL);
2702c37256eSGarrett Wollman 	/*
2712c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2722c37256eSGarrett Wollman 	 * to them.
2732c37256eSGarrett Wollman 	 */
2742c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
27552710de1SPawel Jakub Dawidek 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
27652710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
27752710de1SPawel Jakub Dawidek 
278623dce13SRobert Watson 	TCPDEBUG0;
279623dce13SRobert Watson 	inp = sotoinpcb(so);
280623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
2818501a69cSRobert Watson 	INP_WLOCK(inp);
282ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
283623dce13SRobert Watson 		error = EINVAL;
2842c37256eSGarrett Wollman 		goto out;
285623dce13SRobert Watson 	}
286623dce13SRobert Watson 	tp = intotcpcb(inp);
287623dce13SRobert Watson 	TCPDEBUG1();
288fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
289623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
290fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
291623dce13SRobert Watson out:
292623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
2938501a69cSRobert Watson 	INP_WUNLOCK(inp);
294623dce13SRobert Watson 
295623dce13SRobert Watson 	return (error);
2962c37256eSGarrett Wollman }
297b287c6c7SBjoern A. Zeeb #endif /* INET */
2982c37256eSGarrett Wollman 
299fb59c426SYoshinobu Inoue #ifdef INET6
300fb59c426SYoshinobu Inoue static int
301b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
302fb59c426SYoshinobu Inoue {
303fb59c426SYoshinobu Inoue 	int error = 0;
304f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
305623dce13SRobert Watson 	struct tcpcb *tp = NULL;
306fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
307fb59c426SYoshinobu Inoue 
30852710de1SPawel Jakub Dawidek 	sin6p = (struct sockaddr_in6 *)nam;
30952710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sin6p))
31052710de1SPawel Jakub Dawidek 		return (EINVAL);
311fb59c426SYoshinobu Inoue 	/*
312fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
313fb59c426SYoshinobu Inoue 	 * to them.
314fb59c426SYoshinobu Inoue 	 */
315fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6 &&
31652710de1SPawel Jakub Dawidek 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
31752710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
31852710de1SPawel Jakub Dawidek 
319623dce13SRobert Watson 	TCPDEBUG0;
320623dce13SRobert Watson 	inp = sotoinpcb(so);
321623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
3228501a69cSRobert Watson 	INP_WLOCK(inp);
323ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
324623dce13SRobert Watson 		error = EINVAL;
325623dce13SRobert Watson 		goto out;
326623dce13SRobert Watson 	}
327623dce13SRobert Watson 	tp = intotcpcb(inp);
328623dce13SRobert Watson 	TCPDEBUG1();
329fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
330fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
331fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
332b287c6c7SBjoern A. Zeeb #ifdef INET
33366ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
334fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
335fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
336fb59c426SYoshinobu Inoue 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
337fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
338fb59c426SYoshinobu Inoue 
339fb59c426SYoshinobu Inoue 			in6_sin6_2_sin(&sin, sin6p);
340fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
341fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
342b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
343b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
344fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
345fb59c426SYoshinobu Inoue 			goto out;
346fb59c426SYoshinobu Inoue 		}
347fb59c426SYoshinobu Inoue 	}
348b287c6c7SBjoern A. Zeeb #endif
349b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
350fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
351623dce13SRobert Watson out:
352623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3538501a69cSRobert Watson 	INP_WUNLOCK(inp);
354623dce13SRobert Watson 	return (error);
355fb59c426SYoshinobu Inoue }
356fb59c426SYoshinobu Inoue #endif /* INET6 */
357fb59c426SYoshinobu Inoue 
358b287c6c7SBjoern A. Zeeb #ifdef INET
3592c37256eSGarrett Wollman /*
3602c37256eSGarrett Wollman  * Prepare to accept connections.
3612c37256eSGarrett Wollman  */
3622c37256eSGarrett Wollman static int
363d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
3642c37256eSGarrett Wollman {
3652c37256eSGarrett Wollman 	int error = 0;
366f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
367623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3682c37256eSGarrett Wollman 
369623dce13SRobert Watson 	TCPDEBUG0;
370623dce13SRobert Watson 	inp = sotoinpcb(so);
371623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
3728501a69cSRobert Watson 	INP_WLOCK(inp);
373ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
374623dce13SRobert Watson 		error = EINVAL;
375623dce13SRobert Watson 		goto out;
376623dce13SRobert Watson 	}
377623dce13SRobert Watson 	tp = intotcpcb(inp);
378623dce13SRobert Watson 	TCPDEBUG1();
3790daccb9cSRobert Watson 	SOCK_LOCK(so);
3800daccb9cSRobert Watson 	error = solisten_proto_check(so);
381fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
3820daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0)
383b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
384fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
3850daccb9cSRobert Watson 	if (error == 0) {
38657f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
387d374e81eSRobert Watson 		solisten_proto(so, backlog);
38809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
38937cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
39009fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
39109fe6320SNavdeep Parhar #endif
3920daccb9cSRobert Watson 	}
3930daccb9cSRobert Watson 	SOCK_UNLOCK(so);
394623dce13SRobert Watson 
395623dce13SRobert Watson out:
396623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
3978501a69cSRobert Watson 	INP_WUNLOCK(inp);
398623dce13SRobert Watson 	return (error);
3992c37256eSGarrett Wollman }
400b287c6c7SBjoern A. Zeeb #endif /* INET */
4012c37256eSGarrett Wollman 
402fb59c426SYoshinobu Inoue #ifdef INET6
403fb59c426SYoshinobu Inoue static int
404d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
405fb59c426SYoshinobu Inoue {
406fb59c426SYoshinobu Inoue 	int error = 0;
407f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
408623dce13SRobert Watson 	struct tcpcb *tp = NULL;
409fb59c426SYoshinobu Inoue 
410623dce13SRobert Watson 	TCPDEBUG0;
411623dce13SRobert Watson 	inp = sotoinpcb(so);
412623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4138501a69cSRobert Watson 	INP_WLOCK(inp);
414ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
415623dce13SRobert Watson 		error = EINVAL;
416623dce13SRobert Watson 		goto out;
417623dce13SRobert Watson 	}
418623dce13SRobert Watson 	tp = intotcpcb(inp);
419623dce13SRobert Watson 	TCPDEBUG1();
4200daccb9cSRobert Watson 	SOCK_LOCK(so);
4210daccb9cSRobert Watson 	error = solisten_proto_check(so);
422fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
4230daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0) {
424fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
42566ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
426fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
427b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
428fb59c426SYoshinobu Inoue 	}
429fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4300daccb9cSRobert Watson 	if (error == 0) {
43157f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
432d374e81eSRobert Watson 		solisten_proto(so, backlog);
43309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
43437cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
43509fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
43609fe6320SNavdeep Parhar #endif
4370daccb9cSRobert Watson 	}
4380daccb9cSRobert Watson 	SOCK_UNLOCK(so);
439623dce13SRobert Watson 
440623dce13SRobert Watson out:
441623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4428501a69cSRobert Watson 	INP_WUNLOCK(inp);
443623dce13SRobert Watson 	return (error);
444fb59c426SYoshinobu Inoue }
445fb59c426SYoshinobu Inoue #endif /* INET6 */
446fb59c426SYoshinobu Inoue 
447b287c6c7SBjoern A. Zeeb #ifdef INET
4482c37256eSGarrett Wollman /*
4492c37256eSGarrett Wollman  * Initiate connection to peer.
4502c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
4512c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
4522c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
4532c37256eSGarrett Wollman  * Send initial segment on connection.
4542c37256eSGarrett Wollman  */
4552c37256eSGarrett Wollman static int
456b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
4572c37256eSGarrett Wollman {
4582c37256eSGarrett Wollman 	int error = 0;
459f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
460623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4612c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
4622c37256eSGarrett Wollman 
46357bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
464e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
465e29ef13fSDon Lewis 		return (EINVAL);
46652710de1SPawel Jakub Dawidek 	/*
46752710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
46852710de1SPawel Jakub Dawidek 	 */
4692c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
47052710de1SPawel Jakub Dawidek 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
47152710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
472b89e82ddSJamie Gritton 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
473b89e82ddSJamie Gritton 		return (error);
47475c13541SPoul-Henning Kamp 
475623dce13SRobert Watson 	TCPDEBUG0;
476623dce13SRobert Watson 	inp = sotoinpcb(so);
477623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
4788501a69cSRobert Watson 	INP_WLOCK(inp);
479ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
480623dce13SRobert Watson 		error = EINVAL;
481623dce13SRobert Watson 		goto out;
482623dce13SRobert Watson 	}
483623dce13SRobert Watson 	tp = intotcpcb(inp);
484623dce13SRobert Watson 	TCPDEBUG1();
485b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
4862c37256eSGarrett Wollman 		goto out;
48709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
48809fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
48937cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
49009fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
49109fe6320SNavdeep Parhar 		goto out;
49209fe6320SNavdeep Parhar #endif
49309fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
49409fe6320SNavdeep Parhar 	error = tcp_output(tp);
495623dce13SRobert Watson out:
496623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
4978501a69cSRobert Watson 	INP_WUNLOCK(inp);
498623dce13SRobert Watson 	return (error);
4992c37256eSGarrett Wollman }
500b287c6c7SBjoern A. Zeeb #endif /* INET */
5012c37256eSGarrett Wollman 
502fb59c426SYoshinobu Inoue #ifdef INET6
503fb59c426SYoshinobu Inoue static int
504b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
505fb59c426SYoshinobu Inoue {
506fb59c426SYoshinobu Inoue 	int error = 0;
507f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
508623dce13SRobert Watson 	struct tcpcb *tp = NULL;
509fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
510623dce13SRobert Watson 
511623dce13SRobert Watson 	TCPDEBUG0;
512fb59c426SYoshinobu Inoue 
513fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
514e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sin6p))
515e29ef13fSDon Lewis 		return (EINVAL);
51652710de1SPawel Jakub Dawidek 	/*
51752710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
51852710de1SPawel Jakub Dawidek 	 */
519fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6
52052710de1SPawel Jakub Dawidek 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
52152710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
522fb59c426SYoshinobu Inoue 
523623dce13SRobert Watson 	inp = sotoinpcb(so);
524623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
5258501a69cSRobert Watson 	INP_WLOCK(inp);
526ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
527623dce13SRobert Watson 		error = EINVAL;
528623dce13SRobert Watson 		goto out;
529623dce13SRobert Watson 	}
530623dce13SRobert Watson 	tp = intotcpcb(inp);
531623dce13SRobert Watson 	TCPDEBUG1();
532b287c6c7SBjoern A. Zeeb #ifdef INET
533fa046d87SRobert Watson 	/*
534fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
535fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
536fa046d87SRobert Watson 	 * Is this a significant problem?
537fa046d87SRobert Watson 	 */
53833841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
539fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
540fb59c426SYoshinobu Inoue 
541d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
542d46a5312SMaxim Konovalov 			error = EINVAL;
543d46a5312SMaxim Konovalov 			goto out;
544d46a5312SMaxim Konovalov 		}
54533841545SHajimu UMEMOTO 
546fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
547fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
548fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
549b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
550b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
551413628a7SBjoern A. Zeeb 			goto out;
552b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
553fb59c426SYoshinobu Inoue 			goto out;
55409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
55509fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
556adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
55709fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
55809fe6320SNavdeep Parhar 			goto out;
55909fe6320SNavdeep Parhar #endif
56009fe6320SNavdeep Parhar 		error = tcp_output(tp);
561fb59c426SYoshinobu Inoue 		goto out;
562fb59c426SYoshinobu Inoue 	}
563b287c6c7SBjoern A. Zeeb #endif
564fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
565fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
566dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
567b89e82ddSJamie Gritton 	if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
568413628a7SBjoern A. Zeeb 		goto out;
569b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
570fb59c426SYoshinobu Inoue 		goto out;
57109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
57209fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
573adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
57409fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
57509fe6320SNavdeep Parhar 		goto out;
57609fe6320SNavdeep Parhar #endif
57709fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
57809fe6320SNavdeep Parhar 	error = tcp_output(tp);
579623dce13SRobert Watson 
580623dce13SRobert Watson out:
581623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
5828501a69cSRobert Watson 	INP_WUNLOCK(inp);
583623dce13SRobert Watson 	return (error);
584fb59c426SYoshinobu Inoue }
585fb59c426SYoshinobu Inoue #endif /* INET6 */
586fb59c426SYoshinobu Inoue 
5872c37256eSGarrett Wollman /*
5882c37256eSGarrett Wollman  * Initiate disconnect from peer.
5892c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
5902c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
5912c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
5922c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
5932c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
5942c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
5952c37256eSGarrett Wollman  *
5962c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
5972c37256eSGarrett Wollman  */
5982c37256eSGarrett Wollman static int
5992c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
6002c37256eSGarrett Wollman {
601f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
602623dce13SRobert Watson 	struct tcpcb *tp = NULL;
603623dce13SRobert Watson 	int error = 0;
6042c37256eSGarrett Wollman 
605623dce13SRobert Watson 	TCPDEBUG0;
606603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
607623dce13SRobert Watson 	inp = sotoinpcb(so);
608623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
6098501a69cSRobert Watson 	INP_WLOCK(inp);
610489dcc92SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT)
611489dcc92SJulien Charbon 		goto out;
612489dcc92SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
61321367f63SSam Leffler 		error = ECONNRESET;
614623dce13SRobert Watson 		goto out;
615623dce13SRobert Watson 	}
616623dce13SRobert Watson 	tp = intotcpcb(inp);
617623dce13SRobert Watson 	TCPDEBUG1();
618623dce13SRobert Watson 	tcp_disconnect(tp);
619623dce13SRobert Watson out:
620623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
6218501a69cSRobert Watson 	INP_WUNLOCK(inp);
622603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
623623dce13SRobert Watson 	return (error);
6242c37256eSGarrett Wollman }
6252c37256eSGarrett Wollman 
626b287c6c7SBjoern A. Zeeb #ifdef INET
6272c37256eSGarrett Wollman /*
6288296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
6298296cddfSRobert Watson  * just return the address of the peer, storing through addr.
6302c37256eSGarrett Wollman  */
6312c37256eSGarrett Wollman static int
63257bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
6332c37256eSGarrett Wollman {
6342c37256eSGarrett Wollman 	int error = 0;
635f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
6361db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
63726ef6ac4SDon Lewis 	struct in_addr addr;
63826ef6ac4SDon Lewis 	in_port_t port = 0;
6391db24ffbSJonathan Lemon 	TCPDEBUG0;
6402c37256eSGarrett Wollman 
6413d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
6423d2d3ef4SRobert Watson 		return (ECONNABORTED);
643f76fcf6dSJeffrey Hsu 
644f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
645623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
6468501a69cSRobert Watson 	INP_WLOCK(inp);
647ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
6483d2d3ef4SRobert Watson 		error = ECONNABORTED;
649623dce13SRobert Watson 		goto out;
650623dce13SRobert Watson 	}
6511db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
6521db24ffbSJonathan Lemon 	TCPDEBUG1();
653f76fcf6dSJeffrey Hsu 
654f76fcf6dSJeffrey Hsu 	/*
65554d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
65626ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
65726ef6ac4SDon Lewis 	 * release the lock.
658f76fcf6dSJeffrey Hsu 	 */
65926ef6ac4SDon Lewis 	port = inp->inp_fport;
66026ef6ac4SDon Lewis 	addr = inp->inp_faddr;
661f76fcf6dSJeffrey Hsu 
662623dce13SRobert Watson out:
663623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
6648501a69cSRobert Watson 	INP_WUNLOCK(inp);
66526ef6ac4SDon Lewis 	if (error == 0)
66626ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
66726ef6ac4SDon Lewis 	return error;
6682c37256eSGarrett Wollman }
669b287c6c7SBjoern A. Zeeb #endif /* INET */
6702c37256eSGarrett Wollman 
671fb59c426SYoshinobu Inoue #ifdef INET6
672fb59c426SYoshinobu Inoue static int
673fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
674fb59c426SYoshinobu Inoue {
675f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
676fb59c426SYoshinobu Inoue 	int error = 0;
6771db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
67826ef6ac4SDon Lewis 	struct in_addr addr;
67926ef6ac4SDon Lewis 	struct in6_addr addr6;
68026ef6ac4SDon Lewis 	in_port_t port = 0;
68126ef6ac4SDon Lewis 	int v4 = 0;
6821db24ffbSJonathan Lemon 	TCPDEBUG0;
683fb59c426SYoshinobu Inoue 
684b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
685b4470c16SRobert Watson 		return (ECONNABORTED);
686f76fcf6dSJeffrey Hsu 
687f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
688623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
689fa046d87SRobert Watson 	INP_INFO_RLOCK(&V_tcbinfo);
6908501a69cSRobert Watson 	INP_WLOCK(inp);
691ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
69221367f63SSam Leffler 		error = ECONNABORTED;
693623dce13SRobert Watson 		goto out;
694623dce13SRobert Watson 	}
6951db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
6961db24ffbSJonathan Lemon 	TCPDEBUG1();
697623dce13SRobert Watson 
69826ef6ac4SDon Lewis 	/*
69926ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
70026ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
70126ef6ac4SDon Lewis 	 * release the lock.
70226ef6ac4SDon Lewis 	 */
70326ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
70426ef6ac4SDon Lewis 		v4 = 1;
70526ef6ac4SDon Lewis 		port = inp->inp_fport;
70626ef6ac4SDon Lewis 		addr = inp->inp_faddr;
70726ef6ac4SDon Lewis 	} else {
70826ef6ac4SDon Lewis 		port = inp->inp_fport;
70926ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
71026ef6ac4SDon Lewis 	}
71126ef6ac4SDon Lewis 
712623dce13SRobert Watson out:
713623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
7148501a69cSRobert Watson 	INP_WUNLOCK(inp);
715fa046d87SRobert Watson 	INP_INFO_RUNLOCK(&V_tcbinfo);
71626ef6ac4SDon Lewis 	if (error == 0) {
71726ef6ac4SDon Lewis 		if (v4)
71826ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
71926ef6ac4SDon Lewis 		else
72026ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
72126ef6ac4SDon Lewis 	}
72226ef6ac4SDon Lewis 	return error;
723fb59c426SYoshinobu Inoue }
724fb59c426SYoshinobu Inoue #endif /* INET6 */
725f76fcf6dSJeffrey Hsu 
726f76fcf6dSJeffrey Hsu /*
7272c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
7282c37256eSGarrett Wollman  */
7292c37256eSGarrett Wollman static int
7302c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
7312c37256eSGarrett Wollman {
7322c37256eSGarrett Wollman 	int error = 0;
733f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
734623dce13SRobert Watson 	struct tcpcb *tp = NULL;
7352c37256eSGarrett Wollman 
736623dce13SRobert Watson 	TCPDEBUG0;
737603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
738623dce13SRobert Watson 	inp = sotoinpcb(so);
739623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
7408501a69cSRobert Watson 	INP_WLOCK(inp);
741ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
74221367f63SSam Leffler 		error = ECONNRESET;
743623dce13SRobert Watson 		goto out;
744623dce13SRobert Watson 	}
745623dce13SRobert Watson 	tp = intotcpcb(inp);
746623dce13SRobert Watson 	TCPDEBUG1();
7472c37256eSGarrett Wollman 	socantsendmore(so);
748623dce13SRobert Watson 	tcp_usrclosed(tp);
749ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
75009fe6320SNavdeep Parhar 		error = tcp_output(tp);
751623dce13SRobert Watson 
752623dce13SRobert Watson out:
753623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
7548501a69cSRobert Watson 	INP_WUNLOCK(inp);
755603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
756623dce13SRobert Watson 
757623dce13SRobert Watson 	return (error);
7582c37256eSGarrett Wollman }
7592c37256eSGarrett Wollman 
7602c37256eSGarrett Wollman /*
7612c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
7622c37256eSGarrett Wollman  */
7632c37256eSGarrett Wollman static int
7642c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
7652c37256eSGarrett Wollman {
766f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
767623dce13SRobert Watson 	struct tcpcb *tp = NULL;
768623dce13SRobert Watson 	int error = 0;
7692c37256eSGarrett Wollman 
770623dce13SRobert Watson 	TCPDEBUG0;
771623dce13SRobert Watson 	inp = sotoinpcb(so);
772623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
7738501a69cSRobert Watson 	INP_WLOCK(inp);
774ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
77521367f63SSam Leffler 		error = ECONNRESET;
776623dce13SRobert Watson 		goto out;
777623dce13SRobert Watson 	}
778623dce13SRobert Watson 	tp = intotcpcb(inp);
779623dce13SRobert Watson 	TCPDEBUG1();
78009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
78109fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
78209fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
783460cf046SNavdeep Parhar 	else
78409fe6320SNavdeep Parhar #endif
78509fe6320SNavdeep Parhar 	tcp_output(tp);
786623dce13SRobert Watson 
787623dce13SRobert Watson out:
788623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
7898501a69cSRobert Watson 	INP_WUNLOCK(inp);
790623dce13SRobert Watson 	return (error);
7912c37256eSGarrett Wollman }
7922c37256eSGarrett Wollman 
7932c37256eSGarrett Wollman /*
7942c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
7959c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
7969c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
7979c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
7989c9906e9SPeter Wemm  * generally are caller-frees.
7992c37256eSGarrett Wollman  */
8002c37256eSGarrett Wollman static int
80157bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
802b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
8032c37256eSGarrett Wollman {
8042c37256eSGarrett Wollman 	int error = 0;
805f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
806623dce13SRobert Watson 	struct tcpcb *tp = NULL;
807fb59c426SYoshinobu Inoue #ifdef INET6
808fb59c426SYoshinobu Inoue 	int isipv6;
809fb59c426SYoshinobu Inoue #endif
8109c9906e9SPeter Wemm 	TCPDEBUG0;
8112c37256eSGarrett Wollman 
812f76fcf6dSJeffrey Hsu 	/*
813fa046d87SRobert Watson 	 * We require the pcbinfo lock if we will close the socket as part of
814fa046d87SRobert Watson 	 * this call.
815f76fcf6dSJeffrey Hsu 	 */
816fa046d87SRobert Watson 	if (flags & PRUS_EOF)
817603724d3SBjoern A. Zeeb 		INP_INFO_WLOCK(&V_tcbinfo);
818f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
819623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
8208501a69cSRobert Watson 	INP_WLOCK(inp);
821ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
8227ff0b850SAndre Oppermann 		if (control)
8237ff0b850SAndre Oppermann 			m_freem(control);
824*2cbcd3c1SGleb Smirnoff 		/*
825*2cbcd3c1SGleb Smirnoff 		 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
826*2cbcd3c1SGleb Smirnoff 		 * for freeing memory.
827*2cbcd3c1SGleb Smirnoff 		 */
828*2cbcd3c1SGleb Smirnoff 		if (m && (flags & PRUS_NOTREADY) == 0)
8297ff0b850SAndre Oppermann 			m_freem(m);
83021367f63SSam Leffler 		error = ECONNRESET;
8319c9906e9SPeter Wemm 		goto out;
8329c9906e9SPeter Wemm 	}
833fb59c426SYoshinobu Inoue #ifdef INET6
834fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
835fb59c426SYoshinobu Inoue #endif /* INET6 */
8369c9906e9SPeter Wemm 	tp = intotcpcb(inp);
8379c9906e9SPeter Wemm 	TCPDEBUG1();
8389c9906e9SPeter Wemm 	if (control) {
8399c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
8409c9906e9SPeter Wemm 		if (control->m_len) {
8419c9906e9SPeter Wemm 			m_freem(control);
8422c37256eSGarrett Wollman 			if (m)
8432c37256eSGarrett Wollman 				m_freem(m);
844744f87eaSDavid Greenman 			error = EINVAL;
845744f87eaSDavid Greenman 			goto out;
8462c37256eSGarrett Wollman 		}
8479c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
8489c9906e9SPeter Wemm 	}
8492c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
850651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
8512c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
8522c37256eSGarrett Wollman 			/*
8532c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
8542c37256eSGarrett Wollman 			 * initialize window to default value, and
8552c37256eSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
8562c37256eSGarrett Wollman 			 * MSS.
8572c37256eSGarrett Wollman 			 */
858fb59c426SYoshinobu Inoue #ifdef INET6
859fb59c426SYoshinobu Inoue 			if (isipv6)
860b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
861fb59c426SYoshinobu Inoue #endif /* INET6 */
862b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
863b287c6c7SBjoern A. Zeeb 			else
864b287c6c7SBjoern A. Zeeb #endif
865b287c6c7SBjoern A. Zeeb #ifdef INET
866b40ce416SJulian Elischer 				error = tcp_connect(tp, nam, td);
867b287c6c7SBjoern A. Zeeb #endif
8682c37256eSGarrett Wollman 			if (error)
8692c37256eSGarrett Wollman 				goto out;
8702c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
8712c37256eSGarrett Wollman 			tcp_mss(tp, -1);
8722c37256eSGarrett Wollman 		}
8732c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
8742c37256eSGarrett Wollman 			/*
8752c37256eSGarrett Wollman 			 * Close the send side of the connection after
8762c37256eSGarrett Wollman 			 * the data is sent.
8772c37256eSGarrett Wollman 			 */
878603724d3SBjoern A. Zeeb 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
8792c37256eSGarrett Wollman 			socantsendmore(so);
880623dce13SRobert Watson 			tcp_usrclosed(tp);
8812c37256eSGarrett Wollman 		}
882*2cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
883*2cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
884b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
885b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
88609fe6320SNavdeep Parhar 			error = tcp_output(tp);
887b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
888b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
889b0acefa8SBill Fenner 		}
8902c37256eSGarrett Wollman 	} else {
891623dce13SRobert Watson 		/*
892623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
893623dce13SRobert Watson 		 */
894d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
8952c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
896d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
8972c37256eSGarrett Wollman 			m_freem(m);
8982c37256eSGarrett Wollman 			error = ENOBUFS;
8992c37256eSGarrett Wollman 			goto out;
9002c37256eSGarrett Wollman 		}
9012c37256eSGarrett Wollman 		/*
9022c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
9032c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
9042c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
9052c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
9062c37256eSGarrett Wollman 		 * of data past the urgent section.
9072c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
9082c37256eSGarrett Wollman 		 */
909651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
910d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
911ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
912ef53690bSGarrett Wollman 			/*
913ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
914ef53690bSGarrett Wollman 			 * initialize window to default value, and
915ef53690bSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
916ef53690bSGarrett Wollman 			 * MSS.
917ef53690bSGarrett Wollman 			 */
918fb59c426SYoshinobu Inoue #ifdef INET6
919fb59c426SYoshinobu Inoue 			if (isipv6)
920b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
921fb59c426SYoshinobu Inoue #endif /* INET6 */
922b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
923b287c6c7SBjoern A. Zeeb 			else
924b287c6c7SBjoern A. Zeeb #endif
925b287c6c7SBjoern A. Zeeb #ifdef INET
926b40ce416SJulian Elischer 				error = tcp_connect(tp, nam, td);
927b287c6c7SBjoern A. Zeeb #endif
928ef53690bSGarrett Wollman 			if (error)
929ef53690bSGarrett Wollman 				goto out;
930ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
931ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
932623dce13SRobert Watson 		}
933300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
934*2cbcd3c1SGleb Smirnoff 		if (!(flags & PRUS_NOTREADY)) {
9352cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
93609fe6320SNavdeep Parhar 			error = tcp_output(tp);
9372cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
9382c37256eSGarrett Wollman 		}
939*2cbcd3c1SGleb Smirnoff 	}
940d1401c90SRobert Watson out:
941d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
9422c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
9438501a69cSRobert Watson 	INP_WUNLOCK(inp);
944fa046d87SRobert Watson 	if (flags & PRUS_EOF)
945603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
94673fddedaSPeter Grehan 	return (error);
9472c37256eSGarrett Wollman }
9482c37256eSGarrett Wollman 
949*2cbcd3c1SGleb Smirnoff static int
950*2cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
951*2cbcd3c1SGleb Smirnoff {
952*2cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
953*2cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
954*2cbcd3c1SGleb Smirnoff 	int error;
955*2cbcd3c1SGleb Smirnoff 
956*2cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
957*2cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
958*2cbcd3c1SGleb Smirnoff 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
959*2cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
960*2cbcd3c1SGleb Smirnoff 		for (int i = 0; i < count; i++)
961*2cbcd3c1SGleb Smirnoff 			m = m_free(m);
962*2cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
963*2cbcd3c1SGleb Smirnoff 	}
964*2cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
965*2cbcd3c1SGleb Smirnoff 
966*2cbcd3c1SGleb Smirnoff 	SOCKBUF_LOCK(&so->so_snd);
967*2cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
968*2cbcd3c1SGleb Smirnoff 	SOCKBUF_UNLOCK(&so->so_snd);
969*2cbcd3c1SGleb Smirnoff 	if (error == 0)
970*2cbcd3c1SGleb Smirnoff 		error = tcp_output(tp);
971*2cbcd3c1SGleb Smirnoff 	INP_WUNLOCK(inp);
972*2cbcd3c1SGleb Smirnoff 
973*2cbcd3c1SGleb Smirnoff 	return (error);
974*2cbcd3c1SGleb Smirnoff }
975*2cbcd3c1SGleb Smirnoff 
9762c37256eSGarrett Wollman /*
977a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
9782c37256eSGarrett Wollman  */
979ac45e92fSRobert Watson static void
9802c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
9812c37256eSGarrett Wollman {
982f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
983a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
984623dce13SRobert Watson 	TCPDEBUG0;
985c78cbc7bSRobert Watson 
986ac45e92fSRobert Watson 	inp = sotoinpcb(so);
987c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
988c78cbc7bSRobert Watson 
989603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
9908501a69cSRobert Watson 	INP_WLOCK(inp);
991c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
992c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
993c78cbc7bSRobert Watson 
994c78cbc7bSRobert Watson 	/*
995a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
996c78cbc7bSRobert Watson 	 */
997ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
998ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
999c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
1000a152f8a3SRobert Watson 		TCPDEBUG1();
1001c78cbc7bSRobert Watson 		tcp_drop(tp, ECONNABORTED);
1002a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
1003c78cbc7bSRobert Watson 	}
1004ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1005a152f8a3SRobert Watson 		SOCK_LOCK(so);
1006a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1007a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1008ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1009a152f8a3SRobert Watson 	}
10108501a69cSRobert Watson 	INP_WUNLOCK(inp);
1011603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
1012a152f8a3SRobert Watson }
1013a152f8a3SRobert Watson 
1014a152f8a3SRobert Watson /*
1015a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1016a152f8a3SRobert Watson  */
1017a152f8a3SRobert Watson static void
1018a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1019a152f8a3SRobert Watson {
1020a152f8a3SRobert Watson 	struct inpcb *inp;
1021a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
1022a152f8a3SRobert Watson 	TCPDEBUG0;
1023a152f8a3SRobert Watson 
1024a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1025a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1026a152f8a3SRobert Watson 
1027603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
10288501a69cSRobert Watson 	INP_WLOCK(inp);
1029a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1030a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1031a152f8a3SRobert Watson 
1032a152f8a3SRobert Watson 	/*
1033a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
1034a152f8a3SRobert Watson 	 * a disconnect.
1035a152f8a3SRobert Watson 	 */
1036ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1037ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1038a152f8a3SRobert Watson 		tp = intotcpcb(inp);
1039a152f8a3SRobert Watson 		TCPDEBUG1();
1040a152f8a3SRobert Watson 		tcp_disconnect(tp);
1041a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
1042a152f8a3SRobert Watson 	}
1043ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1044a152f8a3SRobert Watson 		SOCK_LOCK(so);
1045a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1046a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1047ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1048a152f8a3SRobert Watson 	}
10498501a69cSRobert Watson 	INP_WUNLOCK(inp);
1050603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
10512c37256eSGarrett Wollman }
10522c37256eSGarrett Wollman 
10532c37256eSGarrett Wollman /*
10542c37256eSGarrett Wollman  * Receive out-of-band data.
10552c37256eSGarrett Wollman  */
10562c37256eSGarrett Wollman static int
10572c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
10582c37256eSGarrett Wollman {
10592c37256eSGarrett Wollman 	int error = 0;
1060f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1061623dce13SRobert Watson 	struct tcpcb *tp = NULL;
10622c37256eSGarrett Wollman 
1063623dce13SRobert Watson 	TCPDEBUG0;
1064623dce13SRobert Watson 	inp = sotoinpcb(so);
1065623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
10668501a69cSRobert Watson 	INP_WLOCK(inp);
1067ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
106821367f63SSam Leffler 		error = ECONNRESET;
1069623dce13SRobert Watson 		goto out;
1070623dce13SRobert Watson 	}
1071623dce13SRobert Watson 	tp = intotcpcb(inp);
1072623dce13SRobert Watson 	TCPDEBUG1();
10732c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1074c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
10754cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
10764cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
10772c37256eSGarrett Wollman 		error = EINVAL;
10782c37256eSGarrett Wollman 		goto out;
10792c37256eSGarrett Wollman 	}
10802c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
10812c37256eSGarrett Wollman 		error = EWOULDBLOCK;
10822c37256eSGarrett Wollman 		goto out;
10832c37256eSGarrett Wollman 	}
10842c37256eSGarrett Wollman 	m->m_len = 1;
10852c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
10862c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
10872c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1088623dce13SRobert Watson 
1089623dce13SRobert Watson out:
1090623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
10918501a69cSRobert Watson 	INP_WUNLOCK(inp);
1092623dce13SRobert Watson 	return (error);
10932c37256eSGarrett Wollman }
10942c37256eSGarrett Wollman 
1095b287c6c7SBjoern A. Zeeb #ifdef INET
10962c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1097756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1098756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1099756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1100756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1101756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1102756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1103756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1104756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1105756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
110654d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1107756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1108756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1109756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
1110*2cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1111756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
111254d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1113a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1114a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
11152c37256eSGarrett Wollman };
1116b287c6c7SBjoern A. Zeeb #endif /* INET */
1117df8bae1dSRodney W. Grimes 
1118fb59c426SYoshinobu Inoue #ifdef INET6
1119fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1120756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1121756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1122756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1123756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1124756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1125756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1126756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1127756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1128756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1129756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1130756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1131756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1132756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
1133*2cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1134756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1135756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1136a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1137a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1138fb59c426SYoshinobu Inoue };
1139fb59c426SYoshinobu Inoue #endif /* INET6 */
1140fb59c426SYoshinobu Inoue 
1141b287c6c7SBjoern A. Zeeb #ifdef INET
1142a0292f23SGarrett Wollman /*
1143a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1144a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
11455200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
11465200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
11475200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
11485200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1149a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1150a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1151a0292f23SGarrett Wollman  */
11520312fbe9SPoul-Henning Kamp static int
1153ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1154a0292f23SGarrett Wollman {
1155a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1156a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
11575200e00eSIan Dowse 	struct in_addr laddr;
11585200e00eSIan Dowse 	u_short lport;
1159c3229e05SDavid Greenman 	int error;
1160a0292f23SGarrett Wollman 
11618501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1162fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1163623dce13SRobert Watson 
1164a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
1165b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1166a0292f23SGarrett Wollman 		if (error)
1167fa046d87SRobert Watson 			goto out;
1168a0292f23SGarrett Wollman 	}
1169a0292f23SGarrett Wollman 
1170a0292f23SGarrett Wollman 	/*
1171a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1172a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1173a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1174a0292f23SGarrett Wollman 	 */
11755200e00eSIan Dowse 	laddr = inp->inp_laddr;
11765200e00eSIan Dowse 	lport = inp->inp_lport;
11775200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1178b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
11795200e00eSIan Dowse 	if (error && oinp == NULL)
1180fa046d87SRobert Watson 		goto out;
1181fa046d87SRobert Watson 	if (oinp) {
1182fa046d87SRobert Watson 		error = EADDRINUSE;
1183fa046d87SRobert Watson 		goto out;
1184fa046d87SRobert Watson 	}
11855200e00eSIan Dowse 	inp->inp_laddr = laddr;
118615bd2b43SDavid Greenman 	in_pcbrehash(inp);
1187fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1188a0292f23SGarrett Wollman 
1189087b55eaSAndre Oppermann 	/*
1190087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1191087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1192087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1193087b55eaSAndre Oppermann 	 */
1194a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
11959b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1196a0292f23SGarrett Wollman 		tp->request_r_scale++;
1197a0292f23SGarrett Wollman 
1198a0292f23SGarrett Wollman 	soisconnecting(so);
119978b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
120057f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
1201b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
1202a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1203a45d2726SAndras Olah 
1204a0292f23SGarrett Wollman 	return 0;
1205fa046d87SRobert Watson 
1206fa046d87SRobert Watson out:
1207fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1208fa046d87SRobert Watson 	return (error);
1209a0292f23SGarrett Wollman }
1210b287c6c7SBjoern A. Zeeb #endif /* INET */
1211a0292f23SGarrett Wollman 
1212fb59c426SYoshinobu Inoue #ifdef INET6
1213fb59c426SYoshinobu Inoue static int
1214ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1215fb59c426SYoshinobu Inoue {
1216a7e201bbSAndrey V. Elsukov 	struct inpcb *inp = tp->t_inpcb;
1217fb59c426SYoshinobu Inoue 	int error;
1218fb59c426SYoshinobu Inoue 
12198501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1220fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1221623dce13SRobert Watson 
1222fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
1223b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1224fb59c426SYoshinobu Inoue 		if (error)
1225fa046d87SRobert Watson 			goto out;
1226fb59c426SYoshinobu Inoue 	}
1227a7e201bbSAndrey V. Elsukov 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1228a7e201bbSAndrey V. Elsukov 	if (error != 0)
1229b598155aSRobert Watson 		goto out;
1230fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1231fb59c426SYoshinobu Inoue 
1232fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1233fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1234970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1235fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1236fb59c426SYoshinobu Inoue 
1237a7e201bbSAndrey V. Elsukov 	soisconnecting(inp->inp_socket);
123878b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
123957f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
1240b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
1241fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1242fb59c426SYoshinobu Inoue 
1243fb59c426SYoshinobu Inoue 	return 0;
1244fa046d87SRobert Watson 
1245fa046d87SRobert Watson out:
1246fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1247fa046d87SRobert Watson 	return error;
1248fb59c426SYoshinobu Inoue }
1249fb59c426SYoshinobu Inoue #endif /* INET6 */
1250fb59c426SYoshinobu Inoue 
1251cfe8b629SGarrett Wollman /*
1252b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1253b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1254b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1255b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1256b8af5dfaSRobert Watson  * from Linux.
1257b8af5dfaSRobert Watson  */
1258b8af5dfaSRobert Watson static void
1259ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1260b8af5dfaSRobert Watson {
1261b8af5dfaSRobert Watson 
12628501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1263b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1264b8af5dfaSRobert Watson 
1265b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1266b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1267b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
12683529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1269b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1270b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1271b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1272b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1273b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1274b8af5dfaSRobert Watson 	}
12751baaf834SBruce M Simpson 
127643d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
127743d94734SJohn Baldwin 	ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
12781baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
12791baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
12801baaf834SBruce M Simpson 
1281b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1282b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1283b8af5dfaSRobert Watson 
1284b8af5dfaSRobert Watson 	/*
1285b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1286b8af5dfaSRobert Watson 	 */
1287c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1288535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1289b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
12901c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1291535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
129243d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
129343d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1294535fbad6SKip Macy 	if (tp->t_flags & TF_TOE)
1295535fbad6SKip Macy 		ti->tcpi_options |= TCPI_OPT_TOE;
1296f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1297f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1298f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1299b8af5dfaSRobert Watson }
1300b8af5dfaSRobert Watson 
1301b8af5dfaSRobert Watson /*
13021e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
13031e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
13041e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
13051e8f5ffaSRobert Watson  * option.
1306cfe8b629SGarrett Wollman  */
13078501a69cSRobert Watson #define INP_WLOCK_RECHECK(inp) do {					\
13088501a69cSRobert Watson 	INP_WLOCK(inp);							\
1309ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
13108501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
13111e8f5ffaSRobert Watson 		return (ECONNRESET);					\
13121e8f5ffaSRobert Watson 	}								\
13131e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
13141e8f5ffaSRobert Watson } while(0)
13151e8f5ffaSRobert Watson 
1316df8bae1dSRodney W. Grimes int
1317ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1318df8bae1dSRodney W. Grimes {
13193f9d1ef9SRobert Watson 	int	error, opt, optval;
13209077f387SGleb Smirnoff 	u_int	ui;
1321df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1322cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
1323b8af5dfaSRobert Watson 	struct	tcp_info ti;
1324dbc42409SLawrence Stewart 	char buf[TCP_CA_NAME_MAX];
1325dbc42409SLawrence Stewart 	struct cc_algo *algo;
1326df8bae1dSRodney W. Grimes 
1327cfe8b629SGarrett Wollman 	error = 0;
1328df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1329623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
13308501a69cSRobert Watson 	INP_WLOCK(inp);
1331cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1332fb59c426SYoshinobu Inoue #ifdef INET6
13335cd54324SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6PROTO) {
13348501a69cSRobert Watson 			INP_WUNLOCK(inp);
1335fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
1336b287c6c7SBjoern A. Zeeb 		}
1337fb59c426SYoshinobu Inoue #endif /* INET6 */
1338b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1339b287c6c7SBjoern A. Zeeb 		else
1340b287c6c7SBjoern A. Zeeb #endif
1341b287c6c7SBjoern A. Zeeb #ifdef INET
1342b287c6c7SBjoern A. Zeeb 		{
13438501a69cSRobert Watson 			INP_WUNLOCK(inp);
1344cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
13451e8f5ffaSRobert Watson 		}
13461e8f5ffaSRobert Watson #endif
1347df8bae1dSRodney W. Grimes 		return (error);
1348df8bae1dSRodney W. Grimes 	}
1349ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
13508501a69cSRobert Watson 		INP_WUNLOCK(inp);
13511e8f5ffaSRobert Watson 		return (ECONNRESET);
1352623dce13SRobert Watson 	}
1353df8bae1dSRodney W. Grimes 
1354cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1355cfe8b629SGarrett Wollman 	case SOPT_SET:
1356cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
13571cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
135888f6b043SBruce M Simpson 		case TCP_MD5SIG:
13598501a69cSRobert Watson 			INP_WUNLOCK(inp);
13601cfd4b53SBruce M Simpson 			error = sooptcopyin(sopt, &optval, sizeof optval,
13611cfd4b53SBruce M Simpson 			    sizeof optval);
13621cfd4b53SBruce M Simpson 			if (error)
13631e8f5ffaSRobert Watson 				return (error);
13641cfd4b53SBruce M Simpson 
13658501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
13661cfd4b53SBruce M Simpson 			if (optval > 0)
13671cfd4b53SBruce M Simpson 				tp->t_flags |= TF_SIGNATURE;
13681cfd4b53SBruce M Simpson 			else
13691cfd4b53SBruce M Simpson 				tp->t_flags &= ~TF_SIGNATURE;
137009fe6320SNavdeep Parhar 			goto unlock_and_done;
13711cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
137209fe6320SNavdeep Parhar 
1373df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1374cfe8b629SGarrett Wollman 		case TCP_NOOPT:
13758501a69cSRobert Watson 			INP_WUNLOCK(inp);
1376cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1377cfe8b629SGarrett Wollman 			    sizeof optval);
1378cfe8b629SGarrett Wollman 			if (error)
13791e8f5ffaSRobert Watson 				return (error);
1380cfe8b629SGarrett Wollman 
13818501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1382cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1383cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1384cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1385cfe8b629SGarrett Wollman 				break;
1386cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1387cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1388cfe8b629SGarrett Wollman 				break;
1389cfe8b629SGarrett Wollman 			default:
1390cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1391cfe8b629SGarrett Wollman 				break;
1392cfe8b629SGarrett Wollman 			}
1393cfe8b629SGarrett Wollman 
1394cfe8b629SGarrett Wollman 			if (optval)
1395cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1396df8bae1dSRodney W. Grimes 			else
1397cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
139809fe6320SNavdeep Parhar unlock_and_done:
139909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
140009fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
140109fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
140209fe6320SNavdeep Parhar 				    sopt->sopt_name);
140309fe6320SNavdeep Parhar 			}
140409fe6320SNavdeep Parhar #endif
14058501a69cSRobert Watson 			INP_WUNLOCK(inp);
1406df8bae1dSRodney W. Grimes 			break;
1407df8bae1dSRodney W. Grimes 
1408007581c0SJonathan Lemon 		case TCP_NOPUSH:
14098501a69cSRobert Watson 			INP_WUNLOCK(inp);
1410007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1411007581c0SJonathan Lemon 			    sizeof optval);
1412007581c0SJonathan Lemon 			if (error)
14131e8f5ffaSRobert Watson 				return (error);
1414007581c0SJonathan Lemon 
14158501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1416007581c0SJonathan Lemon 			if (optval)
1417007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1418d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
1419007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1420d28b9e89SJohn Baldwin 				if (TCPS_HAVEESTABLISHED(tp->t_state))
1421007581c0SJonathan Lemon 					error = tcp_output(tp);
1422007581c0SJonathan Lemon 			}
142309fe6320SNavdeep Parhar 			goto unlock_and_done;
1424007581c0SJonathan Lemon 
1425df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
14268501a69cSRobert Watson 			INP_WUNLOCK(inp);
1427cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1428cfe8b629SGarrett Wollman 			    sizeof optval);
1429cfe8b629SGarrett Wollman 			if (error)
14301e8f5ffaSRobert Watson 				return (error);
1431df8bae1dSRodney W. Grimes 
14328501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
143353369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
1434603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
1435cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1436a0292f23SGarrett Wollman 			else
1437a0292f23SGarrett Wollman 				error = EINVAL;
143809fe6320SNavdeep Parhar 			goto unlock_and_done;
1439a0292f23SGarrett Wollman 
1440b8af5dfaSRobert Watson 		case TCP_INFO:
14418501a69cSRobert Watson 			INP_WUNLOCK(inp);
1442b8af5dfaSRobert Watson 			error = EINVAL;
1443b8af5dfaSRobert Watson 			break;
1444b8af5dfaSRobert Watson 
1445dbc42409SLawrence Stewart 		case TCP_CONGESTION:
1446dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
1447dbc42409SLawrence Stewart 			bzero(buf, sizeof(buf));
1448dbc42409SLawrence Stewart 			error = sooptcopyin(sopt, &buf, sizeof(buf), 1);
1449dbc42409SLawrence Stewart 			if (error)
1450dbc42409SLawrence Stewart 				break;
1451dbc42409SLawrence Stewart 			INP_WLOCK_RECHECK(inp);
1452dbc42409SLawrence Stewart 			/*
1453dbc42409SLawrence Stewart 			 * Return EINVAL if we can't find the requested cc algo.
1454dbc42409SLawrence Stewart 			 */
1455dbc42409SLawrence Stewart 			error = EINVAL;
1456dbc42409SLawrence Stewart 			CC_LIST_RLOCK();
1457dbc42409SLawrence Stewart 			STAILQ_FOREACH(algo, &cc_list, entries) {
1458dbc42409SLawrence Stewart 				if (strncmp(buf, algo->name, TCP_CA_NAME_MAX)
1459dbc42409SLawrence Stewart 				    == 0) {
1460dbc42409SLawrence Stewart 					/* We've found the requested algo. */
1461dbc42409SLawrence Stewart 					error = 0;
1462dbc42409SLawrence Stewart 					/*
1463dbc42409SLawrence Stewart 					 * We hold a write lock over the tcb
1464dbc42409SLawrence Stewart 					 * so it's safe to do these things
1465dbc42409SLawrence Stewart 					 * without ordering concerns.
1466dbc42409SLawrence Stewart 					 */
1467dbc42409SLawrence Stewart 					if (CC_ALGO(tp)->cb_destroy != NULL)
1468dbc42409SLawrence Stewart 						CC_ALGO(tp)->cb_destroy(tp->ccv);
1469dbc42409SLawrence Stewart 					CC_ALGO(tp) = algo;
1470dbc42409SLawrence Stewart 					/*
1471dbc42409SLawrence Stewart 					 * If something goes pear shaped
1472dbc42409SLawrence Stewart 					 * initialising the new algo,
1473dbc42409SLawrence Stewart 					 * fall back to newreno (which
1474dbc42409SLawrence Stewart 					 * does not require initialisation).
1475dbc42409SLawrence Stewart 					 */
1476dbc42409SLawrence Stewart 					if (algo->cb_init != NULL)
1477dbc42409SLawrence Stewart 						if (algo->cb_init(tp->ccv) > 0) {
1478dbc42409SLawrence Stewart 							CC_ALGO(tp) = &newreno_cc_algo;
1479dbc42409SLawrence Stewart 							/*
1480dbc42409SLawrence Stewart 							 * The only reason init
1481dbc42409SLawrence Stewart 							 * should fail is
1482dbc42409SLawrence Stewart 							 * because of malloc.
1483dbc42409SLawrence Stewart 							 */
1484dbc42409SLawrence Stewart 							error = ENOMEM;
1485dbc42409SLawrence Stewart 						}
1486dbc42409SLawrence Stewart 					break; /* Break the STAILQ_FOREACH. */
1487dbc42409SLawrence Stewart 				}
1488dbc42409SLawrence Stewart 			}
1489dbc42409SLawrence Stewart 			CC_LIST_RUNLOCK();
149009fe6320SNavdeep Parhar 			goto unlock_and_done;
1491dbc42409SLawrence Stewart 
14929077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
14939077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
14949077f387SGleb Smirnoff 		case TCP_KEEPINIT:
14959077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
14969077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
14979077f387SGleb Smirnoff 			if (error)
14989077f387SGleb Smirnoff 				return (error);
14999077f387SGleb Smirnoff 
15009077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
15019077f387SGleb Smirnoff 				error = EINVAL;
15029077f387SGleb Smirnoff 				break;
15039077f387SGleb Smirnoff 			}
15049077f387SGleb Smirnoff 			ui *= hz;
15059077f387SGleb Smirnoff 
15069077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
15079077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
15089077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
15099077f387SGleb Smirnoff 				tp->t_keepidle = ui;
15109077f387SGleb Smirnoff 				/*
15119077f387SGleb Smirnoff 				 * XXX: better check current remaining
15129077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
15139077f387SGleb Smirnoff 				 */
15149077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
15159077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
15169077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
15179077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
15189077f387SGleb Smirnoff 				break;
15199077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
15209077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
15219077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
15229077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
15239077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
15249077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
15259077f387SGleb Smirnoff 				break;
15269077f387SGleb Smirnoff 			case TCP_KEEPINIT:
15279077f387SGleb Smirnoff 				tp->t_keepinit = ui;
15289077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
15299077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
15309077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
15319077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
15329077f387SGleb Smirnoff 				break;
15339077f387SGleb Smirnoff 			}
153409fe6320SNavdeep Parhar 			goto unlock_and_done;
15359077f387SGleb Smirnoff 
153685c05144SGleb Smirnoff 		case TCP_KEEPCNT:
153785c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
153885c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
153985c05144SGleb Smirnoff 			if (error)
154085c05144SGleb Smirnoff 				return (error);
154185c05144SGleb Smirnoff 
154285c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
154385c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
154485c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
154585c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
154685c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
154785c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
154885c05144SGleb Smirnoff 			goto unlock_and_done;
154985c05144SGleb Smirnoff 
1550df8bae1dSRodney W. Grimes 		default:
15518501a69cSRobert Watson 			INP_WUNLOCK(inp);
1552df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1553df8bae1dSRodney W. Grimes 			break;
1554df8bae1dSRodney W. Grimes 		}
1555df8bae1dSRodney W. Grimes 		break;
1556df8bae1dSRodney W. Grimes 
1557cfe8b629SGarrett Wollman 	case SOPT_GET:
15581e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
1559cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
15601cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
156188f6b043SBruce M Simpson 		case TCP_MD5SIG:
15621cfd4b53SBruce M Simpson 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
15638501a69cSRobert Watson 			INP_WUNLOCK(inp);
1564b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
15651cfd4b53SBruce M Simpson 			break;
1566265ed012SBruce M Simpson #endif
15671e8f5ffaSRobert Watson 
1568df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1569cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
15708501a69cSRobert Watson 			INP_WUNLOCK(inp);
1571b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1572df8bae1dSRodney W. Grimes 			break;
1573df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1574cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
15758501a69cSRobert Watson 			INP_WUNLOCK(inp);
1576b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1577df8bae1dSRodney W. Grimes 			break;
1578a0292f23SGarrett Wollman 		case TCP_NOOPT:
1579cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
15808501a69cSRobert Watson 			INP_WUNLOCK(inp);
1581b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1582a0292f23SGarrett Wollman 			break;
1583a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1584cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
15858501a69cSRobert Watson 			INP_WUNLOCK(inp);
1586b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1587b8af5dfaSRobert Watson 			break;
1588b8af5dfaSRobert Watson 		case TCP_INFO:
1589b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
15908501a69cSRobert Watson 			INP_WUNLOCK(inp);
1591b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
1592a0292f23SGarrett Wollman 			break;
1593dbc42409SLawrence Stewart 		case TCP_CONGESTION:
1594dbc42409SLawrence Stewart 			bzero(buf, sizeof(buf));
1595dbc42409SLawrence Stewart 			strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1596dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
1597dbc42409SLawrence Stewart 			error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX);
1598dbc42409SLawrence Stewart 			break;
15992f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
16002f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
16012f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
16022f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
16032f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
16042f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
16052f3eb7f4SGleb Smirnoff 				ui = tp->t_keepidle / hz;
16062f3eb7f4SGleb Smirnoff 				break;
16072f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
16082f3eb7f4SGleb Smirnoff 				ui = tp->t_keepintvl / hz;
16092f3eb7f4SGleb Smirnoff 				break;
16102f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
16112f3eb7f4SGleb Smirnoff 				ui = tp->t_keepinit / hz;
16122f3eb7f4SGleb Smirnoff 				break;
16132f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
16142f3eb7f4SGleb Smirnoff 				ui = tp->t_keepcnt;
16152f3eb7f4SGleb Smirnoff 				break;
16162f3eb7f4SGleb Smirnoff 			}
16172f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
16182f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
16192f3eb7f4SGleb Smirnoff 			break;
1620df8bae1dSRodney W. Grimes 		default:
16218501a69cSRobert Watson 			INP_WUNLOCK(inp);
1622df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1623df8bae1dSRodney W. Grimes 			break;
1624df8bae1dSRodney W. Grimes 		}
1625df8bae1dSRodney W. Grimes 		break;
1626df8bae1dSRodney W. Grimes 	}
1627df8bae1dSRodney W. Grimes 	return (error);
1628df8bae1dSRodney W. Grimes }
16298501a69cSRobert Watson #undef INP_WLOCK_RECHECK
1630df8bae1dSRodney W. Grimes 
163126e30fbbSDavid Greenman /*
1632df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1633df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1634df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1635df8bae1dSRodney W. Grimes  */
16360312fbe9SPoul-Henning Kamp static int
1637ad3f9ab3SAndre Oppermann tcp_attach(struct socket *so)
1638df8bae1dSRodney W. Grimes {
1639ad3f9ab3SAndre Oppermann 	struct tcpcb *tp;
1640df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1641df8bae1dSRodney W. Grimes 	int error;
1642df8bae1dSRodney W. Grimes 
1643df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1644e233e2acSAndre Oppermann 		error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1645df8bae1dSRodney W. Grimes 		if (error)
1646df8bae1dSRodney W. Grimes 			return (error);
1647df8bae1dSRodney W. Grimes 	}
16486741ecf5SAndre Oppermann 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
16496741ecf5SAndre Oppermann 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1650603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_tcbinfo);
1651603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_tcbinfo);
1652f2de87feSRobert Watson 	if (error) {
1653603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
1654df8bae1dSRodney W. Grimes 		return (error);
1655f2de87feSRobert Watson 	}
1656df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1657fb59c426SYoshinobu Inoue #ifdef INET6
16585cd54324SBjoern A. Zeeb 	if (inp->inp_vflag & INP_IPV6PROTO) {
1659fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1660fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1661fb59c426SYoshinobu Inoue 	}
1662fb59c426SYoshinobu Inoue 	else
1663fb59c426SYoshinobu Inoue #endif
1664cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1665df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1666623dce13SRobert Watson 	if (tp == NULL) {
1667df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
16680206cdb8SBjoern A. Zeeb 		in_pcbfree(inp);
1669603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_tcbinfo);
1670df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1671df8bae1dSRodney W. Grimes 	}
1672df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
16738501a69cSRobert Watson 	INP_WUNLOCK(inp);
1674603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_tcbinfo);
1675df8bae1dSRodney W. Grimes 	return (0);
1676df8bae1dSRodney W. Grimes }
1677df8bae1dSRodney W. Grimes 
1678df8bae1dSRodney W. Grimes /*
1679df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1680df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1681df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1682df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1683df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1684df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1685df8bae1dSRodney W. Grimes  */
1686623dce13SRobert Watson static void
1687ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
1688df8bae1dSRodney W. Grimes {
1689e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
1690e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
1691e6e0b5ffSRobert Watson 
1692603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
16938501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1694df8bae1dSRodney W. Grimes 
1695623dce13SRobert Watson 	/*
1696623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1697623dce13SRobert Watson 	 * socket is still open.
1698623dce13SRobert Watson 	 */
1699623dce13SRobert Watson 	if (tp->t_state < TCPS_ESTABLISHED) {
1700df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1701623dce13SRobert Watson 		KASSERT(tp != NULL,
1702623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
1703623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1704243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
1705623dce13SRobert Watson 		KASSERT(tp != NULL,
1706623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
1707623dce13SRobert Watson 	} else {
1708df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
1709df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
1710623dce13SRobert Watson 		tcp_usrclosed(tp);
1711ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
171209fe6320SNavdeep Parhar 			tcp_output(tp);
1713df8bae1dSRodney W. Grimes 	}
1714df8bae1dSRodney W. Grimes }
1715df8bae1dSRodney W. Grimes 
1716df8bae1dSRodney W. Grimes /*
1717df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1718df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1719df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1720df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1721df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1722df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1723df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1724df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1725df8bae1dSRodney W. Grimes  */
1726623dce13SRobert Watson static void
1727ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
1728df8bae1dSRodney W. Grimes {
1729df8bae1dSRodney W. Grimes 
1730603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
17318501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1732e6e0b5ffSRobert Watson 
1733df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1734df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
173509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
173609fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
173709fe6320SNavdeep Parhar #endif
1738bc65987aSKip Macy 		/* FALLTHROUGH */
1739bc65987aSKip Macy 	case TCPS_CLOSED:
174057f60867SMark Johnston 		tcp_state_change(tp, TCPS_CLOSED);
1741df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1742623dce13SRobert Watson 		/*
1743623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
1744623dce13SRobert Watson 		 * still open.
1745623dce13SRobert Watson 		 */
1746623dce13SRobert Watson 		KASSERT(tp != NULL,
1747623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
1748df8bae1dSRodney W. Grimes 		break;
1749df8bae1dSRodney W. Grimes 
1750a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
1751df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1752a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
1753a0292f23SGarrett Wollman 		break;
1754a0292f23SGarrett Wollman 
1755df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
175657f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
1757df8bae1dSRodney W. Grimes 		break;
1758df8bae1dSRodney W. Grimes 
1759df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
176057f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
1761df8bae1dSRodney W. Grimes 		break;
1762df8bae1dSRodney W. Grimes 	}
1763abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
1764df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
1765abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
17667c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
17677c72af87SMohan Srinivasan 			int timeout;
17687c72af87SMohan Srinivasan 
17697c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
17709077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
1771b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
1772b6239c4aSAndras Olah 		}
1773df8bae1dSRodney W. Grimes 	}
17747c72af87SMohan Srinivasan }
1775497057eeSRobert Watson 
1776497057eeSRobert Watson #ifdef DDB
1777497057eeSRobert Watson static void
1778497057eeSRobert Watson db_print_indent(int indent)
1779497057eeSRobert Watson {
1780497057eeSRobert Watson 	int i;
1781497057eeSRobert Watson 
1782497057eeSRobert Watson 	for (i = 0; i < indent; i++)
1783497057eeSRobert Watson 		db_printf(" ");
1784497057eeSRobert Watson }
1785497057eeSRobert Watson 
1786497057eeSRobert Watson static void
1787497057eeSRobert Watson db_print_tstate(int t_state)
1788497057eeSRobert Watson {
1789497057eeSRobert Watson 
1790497057eeSRobert Watson 	switch (t_state) {
1791497057eeSRobert Watson 	case TCPS_CLOSED:
1792497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
1793497057eeSRobert Watson 		return;
1794497057eeSRobert Watson 
1795497057eeSRobert Watson 	case TCPS_LISTEN:
1796497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
1797497057eeSRobert Watson 		return;
1798497057eeSRobert Watson 
1799497057eeSRobert Watson 	case TCPS_SYN_SENT:
1800497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
1801497057eeSRobert Watson 		return;
1802497057eeSRobert Watson 
1803497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
1804497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
1805497057eeSRobert Watson 		return;
1806497057eeSRobert Watson 
1807497057eeSRobert Watson 	case TCPS_ESTABLISHED:
1808497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
1809497057eeSRobert Watson 		return;
1810497057eeSRobert Watson 
1811497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
1812497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
1813497057eeSRobert Watson 		return;
1814497057eeSRobert Watson 
1815497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
1816497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
1817497057eeSRobert Watson 		return;
1818497057eeSRobert Watson 
1819497057eeSRobert Watson 	case TCPS_CLOSING:
1820497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
1821497057eeSRobert Watson 		return;
1822497057eeSRobert Watson 
1823497057eeSRobert Watson 	case TCPS_LAST_ACK:
1824497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
1825497057eeSRobert Watson 		return;
1826497057eeSRobert Watson 
1827497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
1828497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
1829497057eeSRobert Watson 		return;
1830497057eeSRobert Watson 
1831497057eeSRobert Watson 	case TCPS_TIME_WAIT:
1832497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
1833497057eeSRobert Watson 		return;
1834497057eeSRobert Watson 
1835497057eeSRobert Watson 	default:
1836497057eeSRobert Watson 		db_printf("unknown");
1837497057eeSRobert Watson 		return;
1838497057eeSRobert Watson 	}
1839497057eeSRobert Watson }
1840497057eeSRobert Watson 
1841497057eeSRobert Watson static void
1842497057eeSRobert Watson db_print_tflags(u_int t_flags)
1843497057eeSRobert Watson {
1844497057eeSRobert Watson 	int comma;
1845497057eeSRobert Watson 
1846497057eeSRobert Watson 	comma = 0;
1847497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
1848497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
1849497057eeSRobert Watson 		comma = 1;
1850497057eeSRobert Watson 	}
1851497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
1852497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
1853497057eeSRobert Watson 		comma = 1;
1854497057eeSRobert Watson 	}
1855497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
1856497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
1857497057eeSRobert Watson 		comma = 1;
1858497057eeSRobert Watson 	}
1859497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
1860497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
1861497057eeSRobert Watson 		comma = 1;
1862497057eeSRobert Watson 	}
1863497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
1864497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
1865497057eeSRobert Watson 		comma = 1;
1866497057eeSRobert Watson 	}
1867497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
1868497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1869497057eeSRobert Watson 		comma = 1;
1870497057eeSRobert Watson 	}
1871497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
1872497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1873497057eeSRobert Watson 		comma = 1;
1874497057eeSRobert Watson 	}
1875497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
1876497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1877497057eeSRobert Watson 		comma = 1;
1878497057eeSRobert Watson 	}
1879497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
1880497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1881497057eeSRobert Watson 		comma = 1;
1882497057eeSRobert Watson 	}
1883497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
1884497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1885497057eeSRobert Watson 		comma = 1;
1886497057eeSRobert Watson 	}
1887497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
1888497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1889497057eeSRobert Watson 		comma = 1;
1890497057eeSRobert Watson 	}
1891497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
1892497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1893497057eeSRobert Watson 		comma = 1;
1894497057eeSRobert Watson 	}
1895497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
1896497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
1897497057eeSRobert Watson 		comma = 1;
1898497057eeSRobert Watson 	}
1899497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
1900497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1901497057eeSRobert Watson 		comma = 1;
1902497057eeSRobert Watson 	}
1903497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
1904497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1905497057eeSRobert Watson 		comma = 1;
1906497057eeSRobert Watson 	}
1907497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
1908497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1909497057eeSRobert Watson 		comma = 1;
1910497057eeSRobert Watson 	}
1911497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
1912497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1913497057eeSRobert Watson 		comma = 1;
1914497057eeSRobert Watson 	}
1915497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
1916497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1917497057eeSRobert Watson 		comma = 1;
1918497057eeSRobert Watson 	}
1919dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
1920dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
1921dbc42409SLawrence Stewart 		comma = 1;
1922dbc42409SLawrence Stewart 	}
1923497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
1924497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1925497057eeSRobert Watson 		comma = 1;
1926497057eeSRobert Watson 	}
1927497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
1928497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1929497057eeSRobert Watson 		comma = 1;
1930497057eeSRobert Watson 	}
1931497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
1932497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1933497057eeSRobert Watson 		comma = 1;
1934497057eeSRobert Watson 	}
1935497057eeSRobert Watson 	if (t_flags & TF_TSO) {
1936497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
1937497057eeSRobert Watson 		comma = 1;
1938497057eeSRobert Watson 	}
1939f2512ba1SRui Paulo 	if (t_flags & TF_ECN_PERMIT) {
1940f2512ba1SRui Paulo 		db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
1941f2512ba1SRui Paulo 		comma = 1;
1942f2512ba1SRui Paulo 	}
1943497057eeSRobert Watson }
1944497057eeSRobert Watson 
1945497057eeSRobert Watson static void
1946497057eeSRobert Watson db_print_toobflags(char t_oobflags)
1947497057eeSRobert Watson {
1948497057eeSRobert Watson 	int comma;
1949497057eeSRobert Watson 
1950497057eeSRobert Watson 	comma = 0;
1951497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
1952497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1953497057eeSRobert Watson 		comma = 1;
1954497057eeSRobert Watson 	}
1955497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
1956497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1957497057eeSRobert Watson 		comma = 1;
1958497057eeSRobert Watson 	}
1959497057eeSRobert Watson }
1960497057eeSRobert Watson 
1961497057eeSRobert Watson static void
1962497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1963497057eeSRobert Watson {
1964497057eeSRobert Watson 
1965497057eeSRobert Watson 	db_print_indent(indent);
1966497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
1967497057eeSRobert Watson 
1968497057eeSRobert Watson 	indent += 2;
1969497057eeSRobert Watson 
1970497057eeSRobert Watson 	db_print_indent(indent);
1971497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
1972e407b67bSGleb Smirnoff 	   tp->t_segq, tp->t_segqlen, tp->t_dupacks);
1973497057eeSRobert Watson 
1974497057eeSRobert Watson 	db_print_indent(indent);
197585d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
1976e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
1977497057eeSRobert Watson 
1978497057eeSRobert Watson 	db_print_indent(indent);
1979e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
1980e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
1981497057eeSRobert Watson 
1982497057eeSRobert Watson 	db_print_indent(indent);
1983497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
1984497057eeSRobert Watson 	db_print_tstate(tp->t_state);
1985497057eeSRobert Watson 	db_printf(")\n");
1986497057eeSRobert Watson 
1987497057eeSRobert Watson 	db_print_indent(indent);
1988497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
1989497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
1990497057eeSRobert Watson 	db_printf(")\n");
1991497057eeSRobert Watson 
1992497057eeSRobert Watson 	db_print_indent(indent);
1993497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
1994497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
1995497057eeSRobert Watson 
1996497057eeSRobert Watson 	db_print_indent(indent);
1997497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
1998497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
1999497057eeSRobert Watson 
2000497057eeSRobert Watson 	db_print_indent(indent);
2001497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
2002497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
2003497057eeSRobert Watson 
2004497057eeSRobert Watson 	db_print_indent(indent);
2005497057eeSRobert Watson 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %lu   rcv_up: 0x%08x\n",
2006497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2007497057eeSRobert Watson 
2008497057eeSRobert Watson 	db_print_indent(indent);
20091c18314dSAndre Oppermann 	db_printf("snd_wnd: %lu   snd_cwnd: %lu\n",
20101c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
2011497057eeSRobert Watson 
2012497057eeSRobert Watson 	db_print_indent(indent);
20131c18314dSAndre Oppermann 	db_printf("snd_ssthresh: %lu   snd_recover: "
20141c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2015497057eeSRobert Watson 
2016497057eeSRobert Watson 	db_print_indent(indent);
20179f78a87aSJohn Baldwin 	db_printf("t_maxopd: %u   t_rcvtime: %u   t_startime: %u\n",
2018497057eeSRobert Watson 	    tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
2019497057eeSRobert Watson 
2020497057eeSRobert Watson 	db_print_indent(indent);
20211c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
20221c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
2023497057eeSRobert Watson 
2024497057eeSRobert Watson 	db_print_indent(indent);
20251c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
20261c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2027497057eeSRobert Watson 
2028497057eeSRobert Watson 	db_print_indent(indent);
2029497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
2030497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2031497057eeSRobert Watson 	    tp->t_rttbest);
2032497057eeSRobert Watson 
2033497057eeSRobert Watson 	db_print_indent(indent);
2034497057eeSRobert Watson 	db_printf("t_rttupdated: %lu   max_sndwnd: %lu   t_softerror: %d\n",
2035497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2036497057eeSRobert Watson 
2037497057eeSRobert Watson 	db_print_indent(indent);
2038497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2039497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
2040497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
2041497057eeSRobert Watson 
2042497057eeSRobert Watson 	db_print_indent(indent);
2043497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
2044497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2045497057eeSRobert Watson 
2046497057eeSRobert Watson 	db_print_indent(indent);
20479f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
20481a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
2049497057eeSRobert Watson 
2050497057eeSRobert Watson 	db_print_indent(indent);
2051497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
2052497057eeSRobert Watson 	    "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2053497057eeSRobert Watson 
2054497057eeSRobert Watson 	db_print_indent(indent);
2055497057eeSRobert Watson 	db_printf("snd_ssthresh_prev: %lu   snd_recover_prev: 0x%08x   "
20569f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2057497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
2058497057eeSRobert Watson 
2059497057eeSRobert Watson 	db_print_indent(indent);
20603529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
20613529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2062497057eeSRobert Watson 
2063497057eeSRobert Watson 	db_print_indent(indent);
2064497057eeSRobert Watson 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
2065497057eeSRobert Watson 	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2066497057eeSRobert Watson 
2067497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
2068497057eeSRobert Watson 
2069497057eeSRobert Watson 	db_print_indent(indent);
2070497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
2071497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2072497057eeSRobert Watson }
2073497057eeSRobert Watson 
2074497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2075497057eeSRobert Watson {
2076497057eeSRobert Watson 	struct tcpcb *tp;
2077497057eeSRobert Watson 
2078497057eeSRobert Watson 	if (!have_addr) {
2079497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
2080497057eeSRobert Watson 		return;
2081497057eeSRobert Watson 	}
2082497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
2083497057eeSRobert Watson 
2084497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
2085497057eeSRobert Watson }
2086497057eeSRobert Watson #endif
2087