xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 1baaf8347c4456bf08be6411d56cea565b1e03e7)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
3623dce13SRobert Watson  *	The Regents of the University of California.
4623dce13SRobert Watson  * Copyright (c) 2006 Robert N. M. Watson
5623dce13SRobert Watson  * All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
311fdbc7aeSGarrett Wollman  *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
32c3aac50fSPeter Wemm  * $FreeBSD$
33df8bae1dSRodney W. Grimes  */
34df8bae1dSRodney W. Grimes 
351cfd4b53SBruce M Simpson #include "opt_inet.h"
36fb59c426SYoshinobu Inoue #include "opt_inet6.h"
370cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
380cc12cc5SJoerg Wunsch 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
40df8bae1dSRodney W. Grimes #include <sys/systm.h>
41f76fcf6dSJeffrey Hsu #include <sys/malloc.h>
42c7a82f90SGarrett Wollman #include <sys/kernel.h>
4398163b98SPoul-Henning Kamp #include <sys/sysctl.h>
44df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
45fb59c426SYoshinobu Inoue #ifdef INET6
46fb59c426SYoshinobu Inoue #include <sys/domain.h>
47fb59c426SYoshinobu Inoue #endif /* INET6 */
48df8bae1dSRodney W. Grimes #include <sys/socket.h>
49df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
50df8bae1dSRodney W. Grimes #include <sys/protosw.h>
5191421ba2SRobert Watson #include <sys/proc.h>
5291421ba2SRobert Watson #include <sys/jail.h>
53df8bae1dSRodney W. Grimes 
54df8bae1dSRodney W. Grimes #include <net/if.h>
55df8bae1dSRodney W. Grimes #include <net/route.h>
56df8bae1dSRodney W. Grimes 
57df8bae1dSRodney W. Grimes #include <netinet/in.h>
58df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
59fb59c426SYoshinobu Inoue #ifdef INET6
60fb59c426SYoshinobu Inoue #include <netinet/ip6.h>
61fb59c426SYoshinobu Inoue #endif
62df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
63fb59c426SYoshinobu Inoue #ifdef INET6
64fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h>
65fb59c426SYoshinobu Inoue #endif
66b5e8ce9fSBruce Evans #include <netinet/in_var.h>
67df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
68fb59c426SYoshinobu Inoue #ifdef INET6
69fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
70a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
71fb59c426SYoshinobu Inoue #endif
72df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
73df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
74df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
75df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
76df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
77df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
78610ee2f9SDavid Greenman #ifdef TCPDEBUG
79df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
80610ee2f9SDavid Greenman #endif
81df8bae1dSRodney W. Grimes 
82df8bae1dSRodney W. Grimes /*
83df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
84df8bae1dSRodney W. Grimes  */
85117bcae7SGarrett Wollman extern	char *tcpstates[];	/* XXX ??? */
86df8bae1dSRodney W. Grimes 
8756dc72c3SPawel Jakub Dawidek static int	tcp_attach(struct socket *);
884d77a549SAlfred Perlstein static int	tcp_connect(struct tcpcb *, struct sockaddr *,
894d77a549SAlfred Perlstein 		    struct thread *td);
90fb59c426SYoshinobu Inoue #ifdef INET6
914d77a549SAlfred Perlstein static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
924d77a549SAlfred Perlstein 		    struct thread *td);
93fb59c426SYoshinobu Inoue #endif /* INET6 */
94623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
95623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
96b8af5dfaSRobert Watson static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
972c37256eSGarrett Wollman 
982c37256eSGarrett Wollman #ifdef TCPDEBUG
991db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1002c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
1014cc20ab1SSeigo Tanimura #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
1024cc20ab1SSeigo Tanimura 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1032c37256eSGarrett Wollman #else
1042c37256eSGarrett Wollman #define	TCPDEBUG0
1052c37256eSGarrett Wollman #define	TCPDEBUG1()
1062c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1072c37256eSGarrett Wollman #endif
1082c37256eSGarrett Wollman 
1092c37256eSGarrett Wollman /*
1102c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1112c37256eSGarrett Wollman  * and an internet control block.
1122c37256eSGarrett Wollman  */
1132c37256eSGarrett Wollman static int
114b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1152c37256eSGarrett Wollman {
116f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
117623dce13SRobert Watson 	struct tcpcb *tp = NULL;
118623dce13SRobert Watson 	int error;
1192c37256eSGarrett Wollman 	TCPDEBUG0;
1202c37256eSGarrett Wollman 
121623dce13SRobert Watson 	inp = sotoinpcb(so);
122623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1232c37256eSGarrett Wollman 	TCPDEBUG1();
1242c37256eSGarrett Wollman 
12556dc72c3SPawel Jakub Dawidek 	error = tcp_attach(so);
1262c37256eSGarrett Wollman 	if (error)
1272c37256eSGarrett Wollman 		goto out;
1282c37256eSGarrett Wollman 
1292c37256eSGarrett Wollman 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1303879597fSAndrey A. Chernov 		so->so_linger = TCP_LINGERTIME;
131f76fcf6dSJeffrey Hsu 
132f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
133f76fcf6dSJeffrey Hsu 	tp = intotcpcb(inp);
1342c37256eSGarrett Wollman out:
1352c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
1362c37256eSGarrett Wollman 	return error;
1372c37256eSGarrett Wollman }
1382c37256eSGarrett Wollman 
1392c37256eSGarrett Wollman /*
140a152f8a3SRobert Watson  * tcp_detach is called when the socket layer loses its final reference
141a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
142a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
143a152f8a3SRobert Watson  * inpcb state: time wait.
144c78cbc7bSRobert Watson  *
145a152f8a3SRobert Watson  * This function can probably be re-absorbed back into tcp_usr_detach() now
146a152f8a3SRobert Watson  * that there is a single detach path.
1472c37256eSGarrett Wollman  */
148bc725eafSRobert Watson static void
149c78cbc7bSRobert Watson tcp_detach(struct socket *so, struct inpcb *inp)
1502c37256eSGarrett Wollman {
1512c37256eSGarrett Wollman 	struct tcpcb *tp;
152623dce13SRobert Watson #ifdef INET6
153623dce13SRobert Watson 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
154623dce13SRobert Watson #endif
1552c37256eSGarrett Wollman 
156c78cbc7bSRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
157c78cbc7bSRobert Watson 	INP_LOCK_ASSERT(inp);
158623dce13SRobert Watson 
159c78cbc7bSRobert Watson 	KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
160c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
161953b5606SRobert Watson 
162a152f8a3SRobert Watson 	tp = intotcpcb(inp);
163a152f8a3SRobert Watson 
164623dce13SRobert Watson 	if (inp->inp_vflag & INP_TIMEWAIT) {
165623dce13SRobert Watson 		/*
166a152f8a3SRobert Watson 		 * There are two cases to handle: one in which the time wait
167a152f8a3SRobert Watson 		 * state is being discarded (INP_DROPPED), and one in which
168a152f8a3SRobert Watson 		 * this connection will remain in timewait.  In the former,
169a152f8a3SRobert Watson 		 * it is time to discard all state (except tcptw, which has
170a152f8a3SRobert Watson 		 * already been discarded by the timewait close code, which
171a152f8a3SRobert Watson 		 * should be further up the call stack somewhere).  In the
172a152f8a3SRobert Watson 		 * latter case, we detach from the socket, but leave the pcb
173a152f8a3SRobert Watson 		 * present until timewait ends.
174623dce13SRobert Watson 		 *
175a152f8a3SRobert Watson 		 * XXXRW: Would it be cleaner to free the tcptw here?
176623dce13SRobert Watson 		 */
177a152f8a3SRobert Watson 		if (inp->inp_vflag & INP_DROPPED) {
178a152f8a3SRobert Watson 			KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
179a152f8a3SRobert Watson 			    "INP_DROPPED && tp != NULL"));
180623dce13SRobert Watson #ifdef INET6
181623dce13SRobert Watson 			if (isipv6) {
182623dce13SRobert Watson 				in6_pcbdetach(inp);
183623dce13SRobert Watson 				in6_pcbfree(inp);
184623dce13SRobert Watson 			} else {
185623dce13SRobert Watson #endif
186623dce13SRobert Watson 				in_pcbdetach(inp);
187623dce13SRobert Watson 				in_pcbfree(inp);
188623dce13SRobert Watson #ifdef INET6
189623dce13SRobert Watson 			}
190623dce13SRobert Watson #endif
191623dce13SRobert Watson 		} else {
192623dce13SRobert Watson #ifdef INET6
193623dce13SRobert Watson 			if (isipv6)
194623dce13SRobert Watson 				in6_pcbdetach(inp);
195623dce13SRobert Watson 			else
196623dce13SRobert Watson #endif
197623dce13SRobert Watson 				in_pcbdetach(inp);
198f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
199623dce13SRobert Watson 		}
200623dce13SRobert Watson 	} else {
201e6e65783SRobert Watson 		/*
202a152f8a3SRobert Watson 		 * If the connection is not in timewait, we consider two
203a152f8a3SRobert Watson 		 * two conditions: one in which no further processing is
204a152f8a3SRobert Watson 		 * necessary (dropped || embryonic), and one in which TCP is
205a152f8a3SRobert Watson 		 * not yet done, but no longer requires the socket, so the
206a152f8a3SRobert Watson 		 * pcb will persist for the time being.
207a152f8a3SRobert Watson 		 *
208a152f8a3SRobert Watson 		 * XXXRW: Does the second case still occur?
209e6e65783SRobert Watson 		 */
210623dce13SRobert Watson 		if (inp->inp_vflag & INP_DROPPED ||
211623dce13SRobert Watson 		    tp->t_state < TCPS_SYN_SENT) {
212623dce13SRobert Watson 			tcp_discardcb(tp);
213623dce13SRobert Watson #ifdef INET6
214623dce13SRobert Watson 			if (isipv6) {
215a152f8a3SRobert Watson 				in6_pcbdetach(inp);
216a152f8a3SRobert Watson 				in6_pcbfree(inp);
217623dce13SRobert Watson 			} else {
218623dce13SRobert Watson #endif
219623dce13SRobert Watson 				in_pcbdetach(inp);
220623dce13SRobert Watson 				in_pcbfree(inp);
221623dce13SRobert Watson #ifdef INET6
222623dce13SRobert Watson 			}
223623dce13SRobert Watson #endif
224623dce13SRobert Watson 		} else {
225a152f8a3SRobert Watson #ifdef INET6
226a152f8a3SRobert Watson 			if (isipv6)
227a152f8a3SRobert Watson 				in6_pcbdetach(inp);
228a152f8a3SRobert Watson 			else
229a152f8a3SRobert Watson #endif
230a152f8a3SRobert Watson 				in_pcbdetach(inp);
231623dce13SRobert Watson 		}
232623dce13SRobert Watson 	}
233c78cbc7bSRobert Watson }
234c78cbc7bSRobert Watson 
235c78cbc7bSRobert Watson /*
236c78cbc7bSRobert Watson  * pru_detach() detaches the TCP protocol from the socket.
237c78cbc7bSRobert Watson  * If the protocol state is non-embryonic, then can't
238c78cbc7bSRobert Watson  * do this directly: have to initiate a pru_disconnect(),
239c78cbc7bSRobert Watson  * which may finish later; embryonic TCB's can just
240c78cbc7bSRobert Watson  * be discarded here.
241c78cbc7bSRobert Watson  */
242c78cbc7bSRobert Watson static void
243c78cbc7bSRobert Watson tcp_usr_detach(struct socket *so)
244c78cbc7bSRobert Watson {
245c78cbc7bSRobert Watson 	struct inpcb *inp;
246c78cbc7bSRobert Watson 	struct tcpcb *tp;
247c78cbc7bSRobert Watson 	TCPDEBUG0;
248c78cbc7bSRobert Watson 
249c78cbc7bSRobert Watson 	inp = sotoinpcb(so);
250c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
251c78cbc7bSRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
252c78cbc7bSRobert Watson 	INP_LOCK(inp);
253c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
254c78cbc7bSRobert Watson 	    ("tcp_usr_detach: inp_socket == NULL"));
255c78cbc7bSRobert Watson 	TCPDEBUG1();
256c78cbc7bSRobert Watson 
257c78cbc7bSRobert Watson 	tcp_detach(so, inp);
258623dce13SRobert Watson 	tp = NULL;
259623dce13SRobert Watson 	TCPDEBUG2(PRU_DETACH);
260f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&tcbinfo);
2612c37256eSGarrett Wollman }
2622c37256eSGarrett Wollman 
2632c37256eSGarrett Wollman /*
2642c37256eSGarrett Wollman  * Give the socket an address.
2652c37256eSGarrett Wollman  */
2662c37256eSGarrett Wollman static int
267b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2682c37256eSGarrett Wollman {
2692c37256eSGarrett Wollman 	int error = 0;
270f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
271623dce13SRobert Watson 	struct tcpcb *tp = NULL;
2722c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
2732c37256eSGarrett Wollman 
27452710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
27552710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sinp))
27652710de1SPawel Jakub Dawidek 		return (EINVAL);
2772c37256eSGarrett Wollman 	/*
2782c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2792c37256eSGarrett Wollman 	 * to them.
2802c37256eSGarrett Wollman 	 */
2812c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
28252710de1SPawel Jakub Dawidek 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
28352710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
28452710de1SPawel Jakub Dawidek 
285623dce13SRobert Watson 	TCPDEBUG0;
286623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
287623dce13SRobert Watson 	inp = sotoinpcb(so);
288623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
289623dce13SRobert Watson 	INP_LOCK(inp);
290623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
291623dce13SRobert Watson 		error = EINVAL;
2922c37256eSGarrett Wollman 		goto out;
293623dce13SRobert Watson 	}
294623dce13SRobert Watson 	tp = intotcpcb(inp);
295623dce13SRobert Watson 	TCPDEBUG1();
296623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
297623dce13SRobert Watson out:
298623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
299623dce13SRobert Watson 	INP_UNLOCK(inp);
300623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
301623dce13SRobert Watson 
302623dce13SRobert Watson 	return (error);
3032c37256eSGarrett Wollman }
3042c37256eSGarrett Wollman 
305fb59c426SYoshinobu Inoue #ifdef INET6
306fb59c426SYoshinobu Inoue static int
307b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
308fb59c426SYoshinobu Inoue {
309fb59c426SYoshinobu Inoue 	int error = 0;
310f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
311623dce13SRobert Watson 	struct tcpcb *tp = NULL;
312fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
313fb59c426SYoshinobu Inoue 
31452710de1SPawel Jakub Dawidek 	sin6p = (struct sockaddr_in6 *)nam;
31552710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sin6p))
31652710de1SPawel Jakub Dawidek 		return (EINVAL);
317fb59c426SYoshinobu Inoue 	/*
318fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
319fb59c426SYoshinobu Inoue 	 * to them.
320fb59c426SYoshinobu Inoue 	 */
321fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6 &&
32252710de1SPawel Jakub Dawidek 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
32352710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
32452710de1SPawel Jakub Dawidek 
325623dce13SRobert Watson 	TCPDEBUG0;
326623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
327623dce13SRobert Watson 	inp = sotoinpcb(so);
328623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
329623dce13SRobert Watson 	INP_LOCK(inp);
330623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
331623dce13SRobert Watson 		error = EINVAL;
332623dce13SRobert Watson 		goto out;
333623dce13SRobert Watson 	}
334623dce13SRobert Watson 	tp = intotcpcb(inp);
335623dce13SRobert Watson 	TCPDEBUG1();
336fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
337fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
33866ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
339fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
340fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
341fb59c426SYoshinobu Inoue 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
342fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
343fb59c426SYoshinobu Inoue 
344fb59c426SYoshinobu Inoue 			in6_sin6_2_sin(&sin, sin6p);
345fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
346fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
347b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
348b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
349fb59c426SYoshinobu Inoue 			goto out;
350fb59c426SYoshinobu Inoue 		}
351fb59c426SYoshinobu Inoue 	}
352b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
353623dce13SRobert Watson out:
354623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
355623dce13SRobert Watson 	INP_UNLOCK(inp);
356623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
357623dce13SRobert Watson 	return (error);
358fb59c426SYoshinobu Inoue }
359fb59c426SYoshinobu Inoue #endif /* INET6 */
360fb59c426SYoshinobu Inoue 
3612c37256eSGarrett Wollman /*
3622c37256eSGarrett Wollman  * Prepare to accept connections.
3632c37256eSGarrett Wollman  */
3642c37256eSGarrett Wollman static int
365d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
3662c37256eSGarrett Wollman {
3672c37256eSGarrett Wollman 	int error = 0;
368f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
369623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3702c37256eSGarrett Wollman 
371623dce13SRobert Watson 	TCPDEBUG0;
372623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
373623dce13SRobert Watson 	inp = sotoinpcb(so);
374623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
375623dce13SRobert Watson 	INP_LOCK(inp);
376623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
377623dce13SRobert Watson 		error = EINVAL;
378623dce13SRobert Watson 		goto out;
379623dce13SRobert Watson 	}
380623dce13SRobert Watson 	tp = intotcpcb(inp);
381623dce13SRobert Watson 	TCPDEBUG1();
3820daccb9cSRobert Watson 	SOCK_LOCK(so);
3830daccb9cSRobert Watson 	error = solisten_proto_check(so);
3840daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0)
385b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
3860daccb9cSRobert Watson 	if (error == 0) {
3872c37256eSGarrett Wollman 		tp->t_state = TCPS_LISTEN;
388d374e81eSRobert Watson 		solisten_proto(so, backlog);
3890daccb9cSRobert Watson 	}
3900daccb9cSRobert Watson 	SOCK_UNLOCK(so);
391623dce13SRobert Watson 
392623dce13SRobert Watson out:
393623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
394623dce13SRobert Watson 	INP_UNLOCK(inp);
395623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
396623dce13SRobert Watson 	return (error);
3972c37256eSGarrett Wollman }
3982c37256eSGarrett Wollman 
399fb59c426SYoshinobu Inoue #ifdef INET6
400fb59c426SYoshinobu Inoue static int
401d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
402fb59c426SYoshinobu Inoue {
403fb59c426SYoshinobu Inoue 	int error = 0;
404f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
405623dce13SRobert Watson 	struct tcpcb *tp = NULL;
406fb59c426SYoshinobu Inoue 
407623dce13SRobert Watson 	TCPDEBUG0;
408623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
409623dce13SRobert Watson 	inp = sotoinpcb(so);
410623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
411623dce13SRobert Watson 	INP_LOCK(inp);
412623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
413623dce13SRobert Watson 		error = EINVAL;
414623dce13SRobert Watson 		goto out;
415623dce13SRobert Watson 	}
416623dce13SRobert Watson 	tp = intotcpcb(inp);
417623dce13SRobert Watson 	TCPDEBUG1();
4180daccb9cSRobert Watson 	SOCK_LOCK(so);
4190daccb9cSRobert Watson 	error = solisten_proto_check(so);
4200daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0) {
421fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
42266ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
423fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
424b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
425fb59c426SYoshinobu Inoue 	}
4260daccb9cSRobert Watson 	if (error == 0) {
427fb59c426SYoshinobu Inoue 		tp->t_state = TCPS_LISTEN;
428d374e81eSRobert Watson 		solisten_proto(so, backlog);
4290daccb9cSRobert Watson 	}
4300daccb9cSRobert Watson 	SOCK_UNLOCK(so);
431623dce13SRobert Watson 
432623dce13SRobert Watson out:
433623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
434623dce13SRobert Watson 	INP_UNLOCK(inp);
435623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
436623dce13SRobert Watson 	return (error);
437fb59c426SYoshinobu Inoue }
438fb59c426SYoshinobu Inoue #endif /* INET6 */
439fb59c426SYoshinobu Inoue 
4402c37256eSGarrett Wollman /*
4412c37256eSGarrett Wollman  * Initiate connection to peer.
4422c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
4432c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
4442c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
4452c37256eSGarrett Wollman  * Send initial segment on connection.
4462c37256eSGarrett Wollman  */
4472c37256eSGarrett Wollman static int
448b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
4492c37256eSGarrett Wollman {
4502c37256eSGarrett Wollman 	int error = 0;
451f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
452623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4532c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
4542c37256eSGarrett Wollman 
45557bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
456e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
457e29ef13fSDon Lewis 		return (EINVAL);
45852710de1SPawel Jakub Dawidek 	/*
45952710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
46052710de1SPawel Jakub Dawidek 	 */
4612c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
46252710de1SPawel Jakub Dawidek 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
46352710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
464812d8653SSam Leffler 	if (jailed(td->td_ucred))
465a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
46675c13541SPoul-Henning Kamp 
467623dce13SRobert Watson 	TCPDEBUG0;
468623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
469623dce13SRobert Watson 	inp = sotoinpcb(so);
470623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
471623dce13SRobert Watson 	INP_LOCK(inp);
472623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
473623dce13SRobert Watson 		error = EINVAL;
474623dce13SRobert Watson 		goto out;
475623dce13SRobert Watson 	}
476623dce13SRobert Watson 	tp = intotcpcb(inp);
477623dce13SRobert Watson 	TCPDEBUG1();
478b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
4792c37256eSGarrett Wollman 		goto out;
4802c37256eSGarrett Wollman 	error = tcp_output(tp);
481623dce13SRobert Watson out:
482623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
483623dce13SRobert Watson 	INP_UNLOCK(inp);
484623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
485623dce13SRobert Watson 	return (error);
4862c37256eSGarrett Wollman }
4872c37256eSGarrett Wollman 
488fb59c426SYoshinobu Inoue #ifdef INET6
489fb59c426SYoshinobu Inoue static int
490b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
491fb59c426SYoshinobu Inoue {
492fb59c426SYoshinobu Inoue 	int error = 0;
493f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
494623dce13SRobert Watson 	struct tcpcb *tp = NULL;
495fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
496623dce13SRobert Watson 
497623dce13SRobert Watson 	TCPDEBUG0;
498fb59c426SYoshinobu Inoue 
499fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
500e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sin6p))
501e29ef13fSDon Lewis 		return (EINVAL);
50252710de1SPawel Jakub Dawidek 	/*
50352710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
50452710de1SPawel Jakub Dawidek 	 */
505fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6
50652710de1SPawel Jakub Dawidek 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
50752710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
508fb59c426SYoshinobu Inoue 
509623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
510623dce13SRobert Watson 	inp = sotoinpcb(so);
511623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
512623dce13SRobert Watson 	INP_LOCK(inp);
513623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
514623dce13SRobert Watson 		error = EINVAL;
515623dce13SRobert Watson 		goto out;
516623dce13SRobert Watson 	}
517623dce13SRobert Watson 	tp = intotcpcb(inp);
518623dce13SRobert Watson 	TCPDEBUG1();
51933841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
520fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
521fb59c426SYoshinobu Inoue 
522d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
523d46a5312SMaxim Konovalov 			error = EINVAL;
524d46a5312SMaxim Konovalov 			goto out;
525d46a5312SMaxim Konovalov 		}
52633841545SHajimu UMEMOTO 
527fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
528fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
529fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
530b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
531fb59c426SYoshinobu Inoue 			goto out;
532fb59c426SYoshinobu Inoue 		error = tcp_output(tp);
533fb59c426SYoshinobu Inoue 		goto out;
534fb59c426SYoshinobu Inoue 	}
535fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
536fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
537b7d6d952SHajimu UMEMOTO 	inp->inp_inc.inc_isipv6 = 1;
538b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
539fb59c426SYoshinobu Inoue 		goto out;
540fb59c426SYoshinobu Inoue 	error = tcp_output(tp);
541623dce13SRobert Watson 
542623dce13SRobert Watson out:
543623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
544623dce13SRobert Watson 	INP_UNLOCK(inp);
545623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
546623dce13SRobert Watson 	return (error);
547fb59c426SYoshinobu Inoue }
548fb59c426SYoshinobu Inoue #endif /* INET6 */
549fb59c426SYoshinobu Inoue 
5502c37256eSGarrett Wollman /*
5512c37256eSGarrett Wollman  * Initiate disconnect from peer.
5522c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
5532c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
5542c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
5552c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
5562c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
5572c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
5582c37256eSGarrett Wollman  *
5592c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
5602c37256eSGarrett Wollman  */
5612c37256eSGarrett Wollman static int
5622c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
5632c37256eSGarrett Wollman {
564f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
565623dce13SRobert Watson 	struct tcpcb *tp = NULL;
566623dce13SRobert Watson 	int error = 0;
5672c37256eSGarrett Wollman 
568623dce13SRobert Watson 	TCPDEBUG0;
569623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
570623dce13SRobert Watson 	inp = sotoinpcb(so);
571623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
572623dce13SRobert Watson 	INP_LOCK(inp);
573623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
57421367f63SSam Leffler 		error = ECONNRESET;
575623dce13SRobert Watson 		goto out;
576623dce13SRobert Watson 	}
577623dce13SRobert Watson 	tp = intotcpcb(inp);
578623dce13SRobert Watson 	TCPDEBUG1();
579623dce13SRobert Watson 	tcp_disconnect(tp);
580623dce13SRobert Watson out:
581623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
582623dce13SRobert Watson 	INP_UNLOCK(inp);
583623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
584623dce13SRobert Watson 	return (error);
5852c37256eSGarrett Wollman }
5862c37256eSGarrett Wollman 
5872c37256eSGarrett Wollman /*
5882c37256eSGarrett Wollman  * Accept a connection.  Essentially all the work is
5892c37256eSGarrett Wollman  * done at higher levels; just return the address
5902c37256eSGarrett Wollman  * of the peer, storing through addr.
5912c37256eSGarrett Wollman  */
5922c37256eSGarrett Wollman static int
59357bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
5942c37256eSGarrett Wollman {
5952c37256eSGarrett Wollman 	int error = 0;
596f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
5971db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
59826ef6ac4SDon Lewis 	struct in_addr addr;
59926ef6ac4SDon Lewis 	in_port_t port = 0;
6001db24ffbSJonathan Lemon 	TCPDEBUG0;
6012c37256eSGarrett Wollman 
6023d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
6033d2d3ef4SRobert Watson 		return (ECONNABORTED);
604f76fcf6dSJeffrey Hsu 
605f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
606623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
607f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
608623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
6093d2d3ef4SRobert Watson 		error = ECONNABORTED;
610623dce13SRobert Watson 		goto out;
611623dce13SRobert Watson 	}
6121db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
6131db24ffbSJonathan Lemon 	TCPDEBUG1();
614f76fcf6dSJeffrey Hsu 
615f76fcf6dSJeffrey Hsu 	/*
61626ef6ac4SDon Lewis 	 * We inline in_setpeeraddr and COMMON_END here, so that we can
61726ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
61826ef6ac4SDon Lewis 	 * release the lock.
619f76fcf6dSJeffrey Hsu 	 */
62026ef6ac4SDon Lewis 	port = inp->inp_fport;
62126ef6ac4SDon Lewis 	addr = inp->inp_faddr;
622f76fcf6dSJeffrey Hsu 
623623dce13SRobert Watson out:
624623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
62526ef6ac4SDon Lewis 	INP_UNLOCK(inp);
62626ef6ac4SDon Lewis 	if (error == 0)
62726ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
62826ef6ac4SDon Lewis 	return error;
6292c37256eSGarrett Wollman }
6302c37256eSGarrett Wollman 
631fb59c426SYoshinobu Inoue #ifdef INET6
632fb59c426SYoshinobu Inoue static int
633fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
634fb59c426SYoshinobu Inoue {
635f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
636fb59c426SYoshinobu Inoue 	int error = 0;
6371db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
63826ef6ac4SDon Lewis 	struct in_addr addr;
63926ef6ac4SDon Lewis 	struct in6_addr addr6;
64026ef6ac4SDon Lewis 	in_port_t port = 0;
64126ef6ac4SDon Lewis 	int v4 = 0;
6421db24ffbSJonathan Lemon 	TCPDEBUG0;
643fb59c426SYoshinobu Inoue 
644b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
645b4470c16SRobert Watson 		return (ECONNABORTED);
646f76fcf6dSJeffrey Hsu 
647f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
648623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
649f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
650623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
65121367f63SSam Leffler 		error = ECONNABORTED;
652623dce13SRobert Watson 		goto out;
653623dce13SRobert Watson 	}
6541db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
6551db24ffbSJonathan Lemon 	TCPDEBUG1();
656623dce13SRobert Watson 
65726ef6ac4SDon Lewis 	/*
65826ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
65926ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
66026ef6ac4SDon Lewis 	 * release the lock.
66126ef6ac4SDon Lewis 	 */
66226ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
66326ef6ac4SDon Lewis 		v4 = 1;
66426ef6ac4SDon Lewis 		port = inp->inp_fport;
66526ef6ac4SDon Lewis 		addr = inp->inp_faddr;
66626ef6ac4SDon Lewis 	} else {
66726ef6ac4SDon Lewis 		port = inp->inp_fport;
66826ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
66926ef6ac4SDon Lewis 	}
67026ef6ac4SDon Lewis 
671623dce13SRobert Watson out:
672623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
67326ef6ac4SDon Lewis 	INP_UNLOCK(inp);
67426ef6ac4SDon Lewis 	if (error == 0) {
67526ef6ac4SDon Lewis 		if (v4)
67626ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
67726ef6ac4SDon Lewis 		else
67826ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
67926ef6ac4SDon Lewis 	}
68026ef6ac4SDon Lewis 	return error;
681fb59c426SYoshinobu Inoue }
682fb59c426SYoshinobu Inoue #endif /* INET6 */
683f76fcf6dSJeffrey Hsu 
684f76fcf6dSJeffrey Hsu /*
685f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setsockaddr. We just pass down
686f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
687f76fcf6dSJeffrey Hsu  * here because in_setsockaddr will call malloc and can block.
688f76fcf6dSJeffrey Hsu  */
689f76fcf6dSJeffrey Hsu static int
690f76fcf6dSJeffrey Hsu tcp_sockaddr(struct socket *so, struct sockaddr **nam)
691f76fcf6dSJeffrey Hsu {
692f76fcf6dSJeffrey Hsu 	return (in_setsockaddr(so, nam, &tcbinfo));
693f76fcf6dSJeffrey Hsu }
694f76fcf6dSJeffrey Hsu 
695f76fcf6dSJeffrey Hsu /*
696f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setpeeraddr. We just pass down
697f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setpeeraddr to lock.
698f76fcf6dSJeffrey Hsu  */
699f76fcf6dSJeffrey Hsu static int
700f76fcf6dSJeffrey Hsu tcp_peeraddr(struct socket *so, struct sockaddr **nam)
701f76fcf6dSJeffrey Hsu {
702f76fcf6dSJeffrey Hsu 	return (in_setpeeraddr(so, nam, &tcbinfo));
703f76fcf6dSJeffrey Hsu }
704f76fcf6dSJeffrey Hsu 
7052c37256eSGarrett Wollman /*
7062c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
7072c37256eSGarrett Wollman  */
7082c37256eSGarrett Wollman static int
7092c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
7102c37256eSGarrett Wollman {
7112c37256eSGarrett Wollman 	int error = 0;
712f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
713623dce13SRobert Watson 	struct tcpcb *tp = NULL;
7142c37256eSGarrett Wollman 
715623dce13SRobert Watson 	TCPDEBUG0;
716623dce13SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
717623dce13SRobert Watson 	inp = sotoinpcb(so);
718623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
719623dce13SRobert Watson 	INP_LOCK(inp);
720623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
72121367f63SSam Leffler 		error = ECONNRESET;
722623dce13SRobert Watson 		goto out;
723623dce13SRobert Watson 	}
724623dce13SRobert Watson 	tp = intotcpcb(inp);
725623dce13SRobert Watson 	TCPDEBUG1();
7262c37256eSGarrett Wollman 	socantsendmore(so);
727623dce13SRobert Watson 	tcp_usrclosed(tp);
7282c37256eSGarrett Wollman 	error = tcp_output(tp);
729623dce13SRobert Watson 
730623dce13SRobert Watson out:
731623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
732623dce13SRobert Watson 	INP_UNLOCK(inp);
733623dce13SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
734623dce13SRobert Watson 
735623dce13SRobert Watson 	return (error);
7362c37256eSGarrett Wollman }
7372c37256eSGarrett Wollman 
7382c37256eSGarrett Wollman /*
7392c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
7402c37256eSGarrett Wollman  */
7412c37256eSGarrett Wollman static int
7422c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
7432c37256eSGarrett Wollman {
744f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
745623dce13SRobert Watson 	struct tcpcb *tp = NULL;
746623dce13SRobert Watson 	int error = 0;
7472c37256eSGarrett Wollman 
748623dce13SRobert Watson 	TCPDEBUG0;
749623dce13SRobert Watson 	inp = sotoinpcb(so);
750623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
751623dce13SRobert Watson 	INP_LOCK(inp);
752623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
75321367f63SSam Leffler 		error = ECONNRESET;
754623dce13SRobert Watson 		goto out;
755623dce13SRobert Watson 	}
756623dce13SRobert Watson 	tp = intotcpcb(inp);
757623dce13SRobert Watson 	TCPDEBUG1();
7582c37256eSGarrett Wollman 	tcp_output(tp);
759623dce13SRobert Watson 
760623dce13SRobert Watson out:
761623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
762623dce13SRobert Watson 	INP_UNLOCK(inp);
763623dce13SRobert Watson 	return (error);
7642c37256eSGarrett Wollman }
7652c37256eSGarrett Wollman 
7662c37256eSGarrett Wollman /*
7672c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
7689c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
7699c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
7709c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
7719c9906e9SPeter Wemm  * generally are caller-frees.
7722c37256eSGarrett Wollman  */
7732c37256eSGarrett Wollman static int
77457bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
775b40ce416SJulian Elischer 	     struct sockaddr *nam, struct mbuf *control, struct thread *td)
7762c37256eSGarrett Wollman {
7772c37256eSGarrett Wollman 	int error = 0;
778f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
779623dce13SRobert Watson 	struct tcpcb *tp = NULL;
780623dce13SRobert Watson 	int headlocked = 0;
781fb59c426SYoshinobu Inoue #ifdef INET6
782fb59c426SYoshinobu Inoue 	int isipv6;
783fb59c426SYoshinobu Inoue #endif
7849c9906e9SPeter Wemm 	TCPDEBUG0;
7852c37256eSGarrett Wollman 
786f76fcf6dSJeffrey Hsu 	/*
787623dce13SRobert Watson 	 * We require the pcbinfo lock in two cases:
788623dce13SRobert Watson 	 *
789623dce13SRobert Watson 	 * (1) An implied connect is taking place, which can result in
790623dce13SRobert Watson 	 *     binding IPs and ports and hence modification of the pcb hash
791623dce13SRobert Watson 	 *     chains.
792623dce13SRobert Watson 	 *
793623dce13SRobert Watson 	 * (2) PRUS_EOF is set, resulting in explicit close on the send.
794f76fcf6dSJeffrey Hsu 	 */
795623dce13SRobert Watson 	if ((nam != NULL) || (flags & PRUS_EOF)) {
796f76fcf6dSJeffrey Hsu 		INP_INFO_WLOCK(&tcbinfo);
797623dce13SRobert Watson 		headlocked = 1;
798623dce13SRobert Watson 	}
799f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
800623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
801623dce13SRobert Watson 	INP_LOCK(inp);
802623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
8037ff0b850SAndre Oppermann 		if (control)
8047ff0b850SAndre Oppermann 			m_freem(control);
8057ff0b850SAndre Oppermann 		if (m)
8067ff0b850SAndre Oppermann 			m_freem(m);
80721367f63SSam Leffler 		error = ECONNRESET;
8089c9906e9SPeter Wemm 		goto out;
8099c9906e9SPeter Wemm 	}
810fb59c426SYoshinobu Inoue #ifdef INET6
811fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
812fb59c426SYoshinobu Inoue #endif /* INET6 */
8139c9906e9SPeter Wemm 	tp = intotcpcb(inp);
8149c9906e9SPeter Wemm 	TCPDEBUG1();
8159c9906e9SPeter Wemm 	if (control) {
8169c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
8179c9906e9SPeter Wemm 		if (control->m_len) {
8189c9906e9SPeter Wemm 			m_freem(control);
8192c37256eSGarrett Wollman 			if (m)
8202c37256eSGarrett Wollman 				m_freem(m);
821744f87eaSDavid Greenman 			error = EINVAL;
822744f87eaSDavid Greenman 			goto out;
8232c37256eSGarrett Wollman 		}
8249c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
8259c9906e9SPeter Wemm 	}
8262c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
827395bb186SSam Leffler 		sbappendstream(&so->so_snd, m);
8282c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
8292c37256eSGarrett Wollman 			/*
8302c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
8312c37256eSGarrett Wollman 			 * initialize window to default value, and
8322c37256eSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
8332c37256eSGarrett Wollman 			 * MSS.
8342c37256eSGarrett Wollman 			 */
835623dce13SRobert Watson 			INP_INFO_WLOCK_ASSERT(&tcbinfo);
836fb59c426SYoshinobu Inoue #ifdef INET6
837fb59c426SYoshinobu Inoue 			if (isipv6)
838b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
839fb59c426SYoshinobu Inoue 			else
840fb59c426SYoshinobu Inoue #endif /* INET6 */
841b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
8422c37256eSGarrett Wollman 			if (error)
8432c37256eSGarrett Wollman 				goto out;
8442c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
8452c37256eSGarrett Wollman 			tcp_mss(tp, -1);
8462c37256eSGarrett Wollman 		}
8472c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
8482c37256eSGarrett Wollman 			/*
8492c37256eSGarrett Wollman 			 * Close the send side of the connection after
8502c37256eSGarrett Wollman 			 * the data is sent.
8512c37256eSGarrett Wollman 			 */
852623dce13SRobert Watson 			INP_INFO_WLOCK_ASSERT(&tcbinfo);
8532c37256eSGarrett Wollman 			socantsendmore(so);
854623dce13SRobert Watson 			tcp_usrclosed(tp);
8552c37256eSGarrett Wollman 		}
856623dce13SRobert Watson 		if (headlocked) {
857d1401c90SRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
858623dce13SRobert Watson 			headlocked = 0;
859623dce13SRobert Watson 		}
860b0acefa8SBill Fenner 		if (tp != NULL) {
861b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
862b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
8632c37256eSGarrett Wollman 			error = tcp_output(tp);
864b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
865b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
866b0acefa8SBill Fenner 		}
8672c37256eSGarrett Wollman 	} else {
868623dce13SRobert Watson 		/*
869623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
870623dce13SRobert Watson 		 */
871d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
8722c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
873d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
8742c37256eSGarrett Wollman 			m_freem(m);
8752c37256eSGarrett Wollman 			error = ENOBUFS;
8762c37256eSGarrett Wollman 			goto out;
8772c37256eSGarrett Wollman 		}
8782c37256eSGarrett Wollman 		/*
8792c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
8802c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
8812c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
8822c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
8832c37256eSGarrett Wollman 		 * of data past the urgent section.
8842c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
8852c37256eSGarrett Wollman 		 */
886d2bc35abSRobert Watson 		sbappendstream_locked(&so->so_snd, m);
887d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
888ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
889ef53690bSGarrett Wollman 			/*
890ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
891ef53690bSGarrett Wollman 			 * initialize window to default value, and
892ef53690bSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
893ef53690bSGarrett Wollman 			 * MSS.
894ef53690bSGarrett Wollman 			 */
895623dce13SRobert Watson 			INP_INFO_WLOCK_ASSERT(&tcbinfo);
896fb59c426SYoshinobu Inoue #ifdef INET6
897fb59c426SYoshinobu Inoue 			if (isipv6)
898b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
899fb59c426SYoshinobu Inoue 			else
900fb59c426SYoshinobu Inoue #endif /* INET6 */
901b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
902ef53690bSGarrett Wollman 			if (error)
903ef53690bSGarrett Wollman 				goto out;
904ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
905ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
906d1401c90SRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
907623dce13SRobert Watson 			headlocked = 0;
908623dce13SRobert Watson 		} else if (nam) {
909623dce13SRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
910623dce13SRobert Watson 			headlocked = 0;
911623dce13SRobert Watson 		}
9122c37256eSGarrett Wollman 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
9132cdbfa66SPaul Saab 		tp->t_flags |= TF_FORCEDATA;
9142c37256eSGarrett Wollman 		error = tcp_output(tp);
9152cdbfa66SPaul Saab 		tp->t_flags &= ~TF_FORCEDATA;
9162c37256eSGarrett Wollman 	}
917d1401c90SRobert Watson out:
918d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
9192c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
920d1401c90SRobert Watson 	INP_UNLOCK(inp);
921623dce13SRobert Watson 	if (headlocked)
922d1401c90SRobert Watson 		INP_INFO_WUNLOCK(&tcbinfo);
92373fddedaSPeter Grehan 	return (error);
9242c37256eSGarrett Wollman }
9252c37256eSGarrett Wollman 
9262c37256eSGarrett Wollman /*
927a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
9282c37256eSGarrett Wollman  */
929ac45e92fSRobert Watson static void
9302c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
9312c37256eSGarrett Wollman {
932f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
933a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
934623dce13SRobert Watson 	TCPDEBUG0;
935c78cbc7bSRobert Watson 
936ac45e92fSRobert Watson 	inp = sotoinpcb(so);
937c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
938c78cbc7bSRobert Watson 
939c78cbc7bSRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
940ac45e92fSRobert Watson 	INP_LOCK(inp);
941c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
942c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
943c78cbc7bSRobert Watson 
944c78cbc7bSRobert Watson 	/*
945a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
946c78cbc7bSRobert Watson 	 */
947c78cbc7bSRobert Watson 	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
948c78cbc7bSRobert Watson 	    !(inp->inp_vflag & INP_DROPPED)) {
949c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
950a152f8a3SRobert Watson 		TCPDEBUG1();
951c78cbc7bSRobert Watson 		tcp_drop(tp, ECONNABORTED);
952a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
953c78cbc7bSRobert Watson 	}
954a152f8a3SRobert Watson 	if (!(inp->inp_vflag & INP_DROPPED)) {
955a152f8a3SRobert Watson 		SOCK_LOCK(so);
956a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
957a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
958a152f8a3SRobert Watson 		inp->inp_vflag |= INP_SOCKREF;
959a152f8a3SRobert Watson 	}
960a152f8a3SRobert Watson 	INP_UNLOCK(inp);
961a152f8a3SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
962a152f8a3SRobert Watson }
963a152f8a3SRobert Watson 
964a152f8a3SRobert Watson /*
965a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
966a152f8a3SRobert Watson  */
967a152f8a3SRobert Watson static void
968a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
969a152f8a3SRobert Watson {
970a152f8a3SRobert Watson 	struct inpcb *inp;
971a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
972a152f8a3SRobert Watson 	TCPDEBUG0;
973a152f8a3SRobert Watson 
974a152f8a3SRobert Watson 	inp = sotoinpcb(so);
975a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
976a152f8a3SRobert Watson 
977a152f8a3SRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
978a152f8a3SRobert Watson 	INP_LOCK(inp);
979a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
980a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
981a152f8a3SRobert Watson 
982a152f8a3SRobert Watson 	/*
983a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
984a152f8a3SRobert Watson 	 * a disconnect.
985a152f8a3SRobert Watson 	 */
986a152f8a3SRobert Watson 	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
987a152f8a3SRobert Watson 	    !(inp->inp_vflag & INP_DROPPED)) {
988a152f8a3SRobert Watson 		tp = intotcpcb(inp);
989a152f8a3SRobert Watson 		TCPDEBUG1();
990a152f8a3SRobert Watson 		tcp_disconnect(tp);
991a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
992a152f8a3SRobert Watson 	}
993a152f8a3SRobert Watson 	if (!(inp->inp_vflag & INP_DROPPED)) {
994a152f8a3SRobert Watson 		SOCK_LOCK(so);
995a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
996a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
997a152f8a3SRobert Watson 		inp->inp_vflag |= INP_SOCKREF;
998a152f8a3SRobert Watson 	}
999a152f8a3SRobert Watson 	INP_UNLOCK(inp);
1000ac45e92fSRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
10012c37256eSGarrett Wollman }
10022c37256eSGarrett Wollman 
10032c37256eSGarrett Wollman /*
10042c37256eSGarrett Wollman  * Receive out-of-band data.
10052c37256eSGarrett Wollman  */
10062c37256eSGarrett Wollman static int
10072c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
10082c37256eSGarrett Wollman {
10092c37256eSGarrett Wollman 	int error = 0;
1010f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1011623dce13SRobert Watson 	struct tcpcb *tp = NULL;
10122c37256eSGarrett Wollman 
1013623dce13SRobert Watson 	TCPDEBUG0;
1014623dce13SRobert Watson 	inp = sotoinpcb(so);
1015623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1016623dce13SRobert Watson 	INP_LOCK(inp);
1017623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
101821367f63SSam Leffler 		error = ECONNRESET;
1019623dce13SRobert Watson 		goto out;
1020623dce13SRobert Watson 	}
1021623dce13SRobert Watson 	tp = intotcpcb(inp);
1022623dce13SRobert Watson 	TCPDEBUG1();
10232c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1024c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
10254cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
10264cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
10272c37256eSGarrett Wollman 		error = EINVAL;
10282c37256eSGarrett Wollman 		goto out;
10292c37256eSGarrett Wollman 	}
10302c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
10312c37256eSGarrett Wollman 		error = EWOULDBLOCK;
10322c37256eSGarrett Wollman 		goto out;
10332c37256eSGarrett Wollman 	}
10342c37256eSGarrett Wollman 	m->m_len = 1;
10352c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
10362c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
10372c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1038623dce13SRobert Watson 
1039623dce13SRobert Watson out:
1040623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
1041623dce13SRobert Watson 	INP_UNLOCK(inp);
1042623dce13SRobert Watson 	return (error);
10432c37256eSGarrett Wollman }
10442c37256eSGarrett Wollman 
10452c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1046756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1047756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1048756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1049756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1050756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1051756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1052756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1053756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1054756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
1055756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		tcp_peeraddr,
1056756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1057756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1058756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
1059756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1060756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		tcp_sockaddr,
1061a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1062a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
10632c37256eSGarrett Wollman };
1064df8bae1dSRodney W. Grimes 
1065fb59c426SYoshinobu Inoue #ifdef INET6
1066fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1067756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1068756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1069756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1070756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1071756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1072756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1073756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1074756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1075756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1076756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1077756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1078756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1079756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
1080756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1081756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1082a152f8a3SRobert Watson  	.pru_sosetlabel =	in_pcbsosetlabel,
1083a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1084fb59c426SYoshinobu Inoue };
1085fb59c426SYoshinobu Inoue #endif /* INET6 */
1086fb59c426SYoshinobu Inoue 
1087a0292f23SGarrett Wollman /*
1088a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1089a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
10905200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
10915200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
10925200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
10935200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1094a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1095a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1096a0292f23SGarrett Wollman  */
10970312fbe9SPoul-Henning Kamp static int
1098b40ce416SJulian Elischer tcp_connect(tp, nam, td)
1099a0292f23SGarrett Wollman 	register struct tcpcb *tp;
110057bf258eSGarrett Wollman 	struct sockaddr *nam;
1101b40ce416SJulian Elischer 	struct thread *td;
1102a0292f23SGarrett Wollman {
1103a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1104a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
11055200e00eSIan Dowse 	struct in_addr laddr;
11065200e00eSIan Dowse 	u_short lport;
1107c3229e05SDavid Greenman 	int error;
1108a0292f23SGarrett Wollman 
1109623dce13SRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1110623dce13SRobert Watson 	INP_LOCK_ASSERT(inp);
1111623dce13SRobert Watson 
1112a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
1113b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1114a0292f23SGarrett Wollman 		if (error)
1115a0292f23SGarrett Wollman 			return error;
1116a0292f23SGarrett Wollman 	}
1117a0292f23SGarrett Wollman 
1118a0292f23SGarrett Wollman 	/*
1119a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1120a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1121a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1122a0292f23SGarrett Wollman 	 */
11235200e00eSIan Dowse 	laddr = inp->inp_laddr;
11245200e00eSIan Dowse 	lport = inp->inp_lport;
11255200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1126b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
11275200e00eSIan Dowse 	if (error && oinp == NULL)
1128d3628763SRodney W. Grimes 		return error;
1129c94c54e4SAndre Oppermann 	if (oinp)
1130a0292f23SGarrett Wollman 		return EADDRINUSE;
11315200e00eSIan Dowse 	inp->inp_laddr = laddr;
113215bd2b43SDavid Greenman 	in_pcbrehash(inp);
1133a0292f23SGarrett Wollman 
1134087b55eaSAndre Oppermann 	/*
1135087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1136087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1137087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1138087b55eaSAndre Oppermann 	 * XXX: This should be based on the actual MSS.
1139087b55eaSAndre Oppermann 	 */
1140a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1141087b55eaSAndre Oppermann 	    (0x1 << tp->request_r_scale) < tcp_minmss)
1142a0292f23SGarrett Wollman 		tp->request_r_scale++;
1143a0292f23SGarrett Wollman 
1144a0292f23SGarrett Wollman 	soisconnecting(so);
1145a0292f23SGarrett Wollman 	tcpstat.tcps_connattempt++;
1146a0292f23SGarrett Wollman 	tp->t_state = TCPS_SYN_SENT;
11479b8b58e0SJonathan Lemon 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
1148b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
11491fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
1150a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1151a45d2726SAndras Olah 
1152a0292f23SGarrett Wollman 	return 0;
1153a0292f23SGarrett Wollman }
1154a0292f23SGarrett Wollman 
1155fb59c426SYoshinobu Inoue #ifdef INET6
1156fb59c426SYoshinobu Inoue static int
1157b40ce416SJulian Elischer tcp6_connect(tp, nam, td)
1158fb59c426SYoshinobu Inoue 	register struct tcpcb *tp;
1159fb59c426SYoshinobu Inoue 	struct sockaddr *nam;
1160b40ce416SJulian Elischer 	struct thread *td;
1161fb59c426SYoshinobu Inoue {
1162fb59c426SYoshinobu Inoue 	struct inpcb *inp = tp->t_inpcb, *oinp;
1163fb59c426SYoshinobu Inoue 	struct socket *so = inp->inp_socket;
1164fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1165fb59c426SYoshinobu Inoue 	struct in6_addr *addr6;
1166fb59c426SYoshinobu Inoue 	int error;
1167fb59c426SYoshinobu Inoue 
1168623dce13SRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1169623dce13SRobert Watson 	INP_LOCK_ASSERT(inp);
1170623dce13SRobert Watson 
1171fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
1172b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1173fb59c426SYoshinobu Inoue 		if (error)
1174fb59c426SYoshinobu Inoue 			return error;
1175fb59c426SYoshinobu Inoue 	}
1176fb59c426SYoshinobu Inoue 
1177fb59c426SYoshinobu Inoue 	/*
1178fb59c426SYoshinobu Inoue 	 * Cannot simply call in_pcbconnect, because there might be an
1179fb59c426SYoshinobu Inoue 	 * earlier incarnation of this same connection still in
1180fb59c426SYoshinobu Inoue 	 * TIME_WAIT state, creating an ADDRINUSE error.
1181a1f7e5f8SHajimu UMEMOTO 	 * in6_pcbladdr() also handles scope zone IDs.
1182fb59c426SYoshinobu Inoue 	 */
1183fb59c426SYoshinobu Inoue 	error = in6_pcbladdr(inp, nam, &addr6);
1184fb59c426SYoshinobu Inoue 	if (error)
1185fb59c426SYoshinobu Inoue 		return error;
1186fb59c426SYoshinobu Inoue 	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1187fb59c426SYoshinobu Inoue 				  &sin6->sin6_addr, sin6->sin6_port,
1188fb59c426SYoshinobu Inoue 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1189fb59c426SYoshinobu Inoue 				  ? addr6
1190fb59c426SYoshinobu Inoue 				  : &inp->in6p_laddr,
1191fb59c426SYoshinobu Inoue 				  inp->inp_lport,  0, NULL);
1192c94c54e4SAndre Oppermann 	if (oinp)
1193fb59c426SYoshinobu Inoue 		return EADDRINUSE;
1194fb59c426SYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1195fb59c426SYoshinobu Inoue 		inp->in6p_laddr = *addr6;
1196fb59c426SYoshinobu Inoue 	inp->in6p_faddr = sin6->sin6_addr;
1197fb59c426SYoshinobu Inoue 	inp->inp_fport = sin6->sin6_port;
11988a59da30SHajimu UMEMOTO 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
11998a59da30SHajimu UMEMOTO 	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
12008a59da30SHajimu UMEMOTO 	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
12018a59da30SHajimu UMEMOTO 		inp->in6p_flowinfo |=
12028a59da30SHajimu UMEMOTO 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1203fb59c426SYoshinobu Inoue 	in_pcbrehash(inp);
1204fb59c426SYoshinobu Inoue 
1205fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1206fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1207fb59c426SYoshinobu Inoue 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1208fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1209fb59c426SYoshinobu Inoue 
1210fb59c426SYoshinobu Inoue 	soisconnecting(so);
1211fb59c426SYoshinobu Inoue 	tcpstat.tcps_connattempt++;
1212fb59c426SYoshinobu Inoue 	tp->t_state = TCPS_SYN_SENT;
1213fb59c426SYoshinobu Inoue 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
1214b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
12151fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
1216fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1217fb59c426SYoshinobu Inoue 
1218fb59c426SYoshinobu Inoue 	return 0;
1219fb59c426SYoshinobu Inoue }
1220fb59c426SYoshinobu Inoue #endif /* INET6 */
1221fb59c426SYoshinobu Inoue 
1222cfe8b629SGarrett Wollman /*
1223b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1224b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1225b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1226b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1227b8af5dfaSRobert Watson  * from Linux.
1228b8af5dfaSRobert Watson  */
1229b8af5dfaSRobert Watson static void
1230b8af5dfaSRobert Watson tcp_fill_info(tp, ti)
1231b8af5dfaSRobert Watson 	struct tcpcb *tp;
1232b8af5dfaSRobert Watson 	struct tcp_info *ti;
1233b8af5dfaSRobert Watson {
1234b8af5dfaSRobert Watson 
1235b8af5dfaSRobert Watson 	INP_LOCK_ASSERT(tp->t_inpcb);
1236b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1237b8af5dfaSRobert Watson 
1238b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1239b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1240b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1241b8af5dfaSRobert Watson 	if (tp->sack_enable)
1242b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1243b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1244b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1245b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1246b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1247b8af5dfaSRobert Watson 	}
12481baaf834SBruce M Simpson 
12491baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
12501baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
12511baaf834SBruce M Simpson 
1252b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1253b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1254b8af5dfaSRobert Watson 
1255b8af5dfaSRobert Watson 	/*
1256b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1257b8af5dfaSRobert Watson 	 */
1258c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1259b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
1260b8af5dfaSRobert Watson 	ti->tcpi_snd_bwnd = tp->snd_bwnd;
1261b8af5dfaSRobert Watson }
1262b8af5dfaSRobert Watson 
1263b8af5dfaSRobert Watson /*
1264cfe8b629SGarrett Wollman  * The new sockopt interface makes it possible for us to block in the
1265cfe8b629SGarrett Wollman  * copyin/out step (if we take a page fault).  Taking a page fault at
1266cfe8b629SGarrett Wollman  * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
1267cfe8b629SGarrett Wollman  * use TSM, there probably isn't any need for this function to run at
1268cfe8b629SGarrett Wollman  * splnet() any more.  This needs more examination.)
1269b8af5dfaSRobert Watson  *
1270b8af5dfaSRobert Watson  * XXXRW: The locking here is wrong; we may take a page fault while holding
1271b8af5dfaSRobert Watson  * the inpcb lock.
1272cfe8b629SGarrett Wollman  */
1273df8bae1dSRodney W. Grimes int
1274cfe8b629SGarrett Wollman tcp_ctloutput(so, sopt)
1275df8bae1dSRodney W. Grimes 	struct socket *so;
1276cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1277df8bae1dSRodney W. Grimes {
12783f9d1ef9SRobert Watson 	int	error, opt, optval;
1279df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1280cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
1281b8af5dfaSRobert Watson 	struct	tcp_info ti;
1282df8bae1dSRodney W. Grimes 
1283cfe8b629SGarrett Wollman 	error = 0;
1284df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1285623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1286f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1287cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
12884e397bc5SRobert Watson 		INP_UNLOCK(inp);
1289fb59c426SYoshinobu Inoue #ifdef INET6
1290fb59c426SYoshinobu Inoue 		if (INP_CHECK_SOCKAF(so, AF_INET6))
1291fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
1292fb59c426SYoshinobu Inoue 		else
1293fb59c426SYoshinobu Inoue #endif /* INET6 */
1294cfe8b629SGarrett Wollman 		error = ip_ctloutput(so, sopt);
1295df8bae1dSRodney W. Grimes 		return (error);
1296df8bae1dSRodney W. Grimes 	}
1297623dce13SRobert Watson 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
1298623dce13SRobert Watson 		error = ECONNRESET;
1299623dce13SRobert Watson 		goto out;
1300623dce13SRobert Watson 	}
1301df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
1302df8bae1dSRodney W. Grimes 
1303cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1304cfe8b629SGarrett Wollman 	case SOPT_SET:
1305cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
13061cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
130788f6b043SBruce M Simpson 		case TCP_MD5SIG:
13081cfd4b53SBruce M Simpson 			error = sooptcopyin(sopt, &optval, sizeof optval,
13091cfd4b53SBruce M Simpson 					    sizeof optval);
13101cfd4b53SBruce M Simpson 			if (error)
13111cfd4b53SBruce M Simpson 				break;
13121cfd4b53SBruce M Simpson 
13131cfd4b53SBruce M Simpson 			if (optval > 0)
13141cfd4b53SBruce M Simpson 				tp->t_flags |= TF_SIGNATURE;
13151cfd4b53SBruce M Simpson 			else
13161cfd4b53SBruce M Simpson 				tp->t_flags &= ~TF_SIGNATURE;
13171cfd4b53SBruce M Simpson 			break;
13181cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
1319df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1320cfe8b629SGarrett Wollman 		case TCP_NOOPT:
1321cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1322cfe8b629SGarrett Wollman 					    sizeof optval);
1323cfe8b629SGarrett Wollman 			if (error)
1324cfe8b629SGarrett Wollman 				break;
1325cfe8b629SGarrett Wollman 
1326cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1327cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1328cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1329cfe8b629SGarrett Wollman 				break;
1330cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1331cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1332cfe8b629SGarrett Wollman 				break;
1333cfe8b629SGarrett Wollman 			default:
1334cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1335cfe8b629SGarrett Wollman 				break;
1336cfe8b629SGarrett Wollman 			}
1337cfe8b629SGarrett Wollman 
1338cfe8b629SGarrett Wollman 			if (optval)
1339cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1340df8bae1dSRodney W. Grimes 			else
1341cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
1342df8bae1dSRodney W. Grimes 			break;
1343df8bae1dSRodney W. Grimes 
1344007581c0SJonathan Lemon 		case TCP_NOPUSH:
1345007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1346007581c0SJonathan Lemon 					    sizeof optval);
1347007581c0SJonathan Lemon 			if (error)
1348007581c0SJonathan Lemon 				break;
1349007581c0SJonathan Lemon 
1350007581c0SJonathan Lemon 			if (optval)
1351007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1352007581c0SJonathan Lemon 			else {
1353007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1354007581c0SJonathan Lemon 				error = tcp_output(tp);
1355007581c0SJonathan Lemon 			}
1356007581c0SJonathan Lemon 			break;
1357007581c0SJonathan Lemon 
1358df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1359cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1360cfe8b629SGarrett Wollman 					    sizeof optval);
1361cfe8b629SGarrett Wollman 			if (error)
1362df8bae1dSRodney W. Grimes 				break;
1363df8bae1dSRodney W. Grimes 
136453369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
136553369ac9SAndre Oppermann 			    optval + 40 >= tcp_minmss)
1366cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1367a0292f23SGarrett Wollman 			else
1368a0292f23SGarrett Wollman 				error = EINVAL;
1369a0292f23SGarrett Wollman 			break;
1370a0292f23SGarrett Wollman 
1371b8af5dfaSRobert Watson 		case TCP_INFO:
1372b8af5dfaSRobert Watson 			error = EINVAL;
1373b8af5dfaSRobert Watson 			break;
1374b8af5dfaSRobert Watson 
1375df8bae1dSRodney W. Grimes 		default:
1376df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1377df8bae1dSRodney W. Grimes 			break;
1378df8bae1dSRodney W. Grimes 		}
1379df8bae1dSRodney W. Grimes 		break;
1380df8bae1dSRodney W. Grimes 
1381cfe8b629SGarrett Wollman 	case SOPT_GET:
1382cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
13831cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
138488f6b043SBruce M Simpson 		case TCP_MD5SIG:
13851cfd4b53SBruce M Simpson 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1386b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
13871cfd4b53SBruce M Simpson 			break;
1388265ed012SBruce M Simpson #endif
1389df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1390cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
1391b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1392df8bae1dSRodney W. Grimes 			break;
1393df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1394cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
1395b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1396df8bae1dSRodney W. Grimes 			break;
1397a0292f23SGarrett Wollman 		case TCP_NOOPT:
1398cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
1399b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1400a0292f23SGarrett Wollman 			break;
1401a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1402cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
1403b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1404b8af5dfaSRobert Watson 			break;
1405b8af5dfaSRobert Watson 		case TCP_INFO:
1406b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
1407b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
1408a0292f23SGarrett Wollman 			break;
1409df8bae1dSRodney W. Grimes 		default:
1410df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1411df8bae1dSRodney W. Grimes 			break;
1412df8bae1dSRodney W. Grimes 		}
1413df8bae1dSRodney W. Grimes 		break;
1414df8bae1dSRodney W. Grimes 	}
1415623dce13SRobert Watson out:
1416f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1417df8bae1dSRodney W. Grimes 	return (error);
1418df8bae1dSRodney W. Grimes }
1419df8bae1dSRodney W. Grimes 
142026e30fbbSDavid Greenman /*
142126e30fbbSDavid Greenman  * tcp_sendspace and tcp_recvspace are the default send and receive window
142226e30fbbSDavid Greenman  * sizes, respectively.  These are obsolescent (this information should
142326e30fbbSDavid Greenman  * be set by the route).
142426e30fbbSDavid Greenman  */
142581e561cdSDavid E. O'Brien u_long	tcp_sendspace = 1024*32;
1426e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
14273d177f46SBill Fumerola     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
142881e561cdSDavid E. O'Brien u_long	tcp_recvspace = 1024*64;
1429e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
14303d177f46SBill Fumerola     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1431df8bae1dSRodney W. Grimes 
1432df8bae1dSRodney W. Grimes /*
1433df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1434df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1435df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1436df8bae1dSRodney W. Grimes  */
14370312fbe9SPoul-Henning Kamp static int
143856dc72c3SPawel Jakub Dawidek tcp_attach(so)
1439df8bae1dSRodney W. Grimes 	struct socket *so;
1440df8bae1dSRodney W. Grimes {
1441df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1442df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1443df8bae1dSRodney W. Grimes 	int error;
1444fb59c426SYoshinobu Inoue #ifdef INET6
14454a6a94d8SArchie Cobbs 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1446fb59c426SYoshinobu Inoue #endif
1447df8bae1dSRodney W. Grimes 
1448df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1449df8bae1dSRodney W. Grimes 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1450df8bae1dSRodney W. Grimes 		if (error)
1451df8bae1dSRodney W. Grimes 			return (error);
1452df8bae1dSRodney W. Grimes 	}
14536741ecf5SAndre Oppermann 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
14546741ecf5SAndre Oppermann 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1455f2de87feSRobert Watson 	INP_INFO_WLOCK(&tcbinfo);
1456d915b280SStephan Uphoff 	error = in_pcballoc(so, &tcbinfo);
1457f2de87feSRobert Watson 	if (error) {
1458f2de87feSRobert Watson 		INP_INFO_WUNLOCK(&tcbinfo);
1459df8bae1dSRodney W. Grimes 		return (error);
1460f2de87feSRobert Watson 	}
1461df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1462fb59c426SYoshinobu Inoue #ifdef INET6
1463fb59c426SYoshinobu Inoue 	if (isipv6) {
1464fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1465fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1466fb59c426SYoshinobu Inoue 	}
1467fb59c426SYoshinobu Inoue 	else
1468fb59c426SYoshinobu Inoue #endif
1469cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1470df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1471623dce13SRobert Watson 	if (tp == NULL) {
1472fb59c426SYoshinobu Inoue #ifdef INET6
1473623dce13SRobert Watson 		if (isipv6) {
1474fb59c426SYoshinobu Inoue 			in6_pcbdetach(inp);
1475623dce13SRobert Watson 			in6_pcbfree(inp);
1476623dce13SRobert Watson 		} else {
1477fb59c426SYoshinobu Inoue #endif
1478df8bae1dSRodney W. Grimes 			in_pcbdetach(inp);
1479623dce13SRobert Watson 			in_pcbfree(inp);
1480623dce13SRobert Watson #ifdef INET6
1481623dce13SRobert Watson 		}
1482623dce13SRobert Watson #endif
1483f2de87feSRobert Watson 		INP_INFO_WUNLOCK(&tcbinfo);
1484df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1485df8bae1dSRodney W. Grimes 	}
1486df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
1487f2de87feSRobert Watson 	INP_UNLOCK(inp);
1488f2de87feSRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
1489df8bae1dSRodney W. Grimes 	return (0);
1490df8bae1dSRodney W. Grimes }
1491df8bae1dSRodney W. Grimes 
1492df8bae1dSRodney W. Grimes /*
1493df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1494df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1495df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1496df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1497df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1498df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1499df8bae1dSRodney W. Grimes  */
1500623dce13SRobert Watson static void
1501df8bae1dSRodney W. Grimes tcp_disconnect(tp)
1502df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1503df8bae1dSRodney W. Grimes {
1504e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
1505e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
1506e6e0b5ffSRobert Watson 
1507e6e0b5ffSRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1508e6e0b5ffSRobert Watson 	INP_LOCK_ASSERT(inp);
1509df8bae1dSRodney W. Grimes 
1510623dce13SRobert Watson 	/*
1511623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1512623dce13SRobert Watson 	 * socket is still open.
1513623dce13SRobert Watson 	 */
1514623dce13SRobert Watson 	if (tp->t_state < TCPS_ESTABLISHED) {
1515df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1516623dce13SRobert Watson 		KASSERT(tp != NULL,
1517623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
1518623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1519243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
1520623dce13SRobert Watson 		KASSERT(tp != NULL,
1521623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
1522623dce13SRobert Watson 	} else {
1523df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
1524df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
1525623dce13SRobert Watson 		tcp_usrclosed(tp);
1526953b5606SRobert Watson 		if (!(inp->inp_vflag & INP_DROPPED))
1527623dce13SRobert Watson 			tcp_output(tp);
1528df8bae1dSRodney W. Grimes 	}
1529df8bae1dSRodney W. Grimes }
1530df8bae1dSRodney W. Grimes 
1531df8bae1dSRodney W. Grimes /*
1532df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1533df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1534df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1535df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1536df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1537df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1538df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1539df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1540df8bae1dSRodney W. Grimes  */
1541623dce13SRobert Watson static void
1542df8bae1dSRodney W. Grimes tcp_usrclosed(tp)
1543df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1544df8bae1dSRodney W. Grimes {
1545df8bae1dSRodney W. Grimes 
1546e6e0b5ffSRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1547e6e0b5ffSRobert Watson 	INP_LOCK_ASSERT(tp->t_inpcb);
1548e6e0b5ffSRobert Watson 
1549df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1550df8bae1dSRodney W. Grimes 
1551df8bae1dSRodney W. Grimes 	case TCPS_CLOSED:
1552df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
1553df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_CLOSED;
1554df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1555623dce13SRobert Watson 		/*
1556623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
1557623dce13SRobert Watson 		 * still open.
1558623dce13SRobert Watson 		 */
1559623dce13SRobert Watson 		KASSERT(tp != NULL,
1560623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
1561df8bae1dSRodney W. Grimes 		break;
1562df8bae1dSRodney W. Grimes 
1563a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
1564df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1565a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
1566a0292f23SGarrett Wollman 		break;
1567a0292f23SGarrett Wollman 
1568df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1569df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_FIN_WAIT_1;
1570df8bae1dSRodney W. Grimes 		break;
1571df8bae1dSRodney W. Grimes 
1572df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1573df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_LAST_ACK;
1574df8bae1dSRodney W. Grimes 		break;
1575df8bae1dSRodney W. Grimes 	}
1576b6239c4aSAndras Olah 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1577df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
1578b6239c4aSAndras Olah 		/* To prevent the connection hanging in FIN_WAIT_2 forever. */
1579b6239c4aSAndras Olah 		if (tp->t_state == TCPS_FIN_WAIT_2)
15809b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, tcp_maxidle,
15819b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
1582b6239c4aSAndras Olah 	}
1583df8bae1dSRodney W. Grimes }
1584