tcp_usrreq.c revision 185370
1/*-
2 * Copyright (c) 1982, 1986, 1988, 1993
3 *	The Regents of the University of California.
4 * Copyright (c) 2006-2007 Robert N. M. Watson
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: head/sys/netinet/tcp_usrreq.c 185370 2008-11-27 12:04:35Z bz $");
36
37#include "opt_ddb.h"
38#include "opt_inet.h"
39#include "opt_inet6.h"
40#include "opt_tcpdebug.h"
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/malloc.h>
45#include <sys/kernel.h>
46#include <sys/sysctl.h>
47#include <sys/mbuf.h>
48#ifdef INET6
49#include <sys/domain.h>
50#endif /* INET6 */
51#include <sys/socket.h>
52#include <sys/socketvar.h>
53#include <sys/protosw.h>
54#include <sys/proc.h>
55#include <sys/jail.h>
56#include <sys/vimage.h>
57
58#ifdef DDB
59#include <ddb/ddb.h>
60#endif
61
62#include <net/if.h>
63#include <net/route.h>
64
65#include <netinet/in.h>
66#include <netinet/in_systm.h>
67#ifdef INET6
68#include <netinet/ip6.h>
69#endif
70#include <netinet/in_pcb.h>
71#ifdef INET6
72#include <netinet6/in6_pcb.h>
73#endif
74#include <netinet/in_var.h>
75#include <netinet/ip_var.h>
76#ifdef INET6
77#include <netinet6/ip6_var.h>
78#include <netinet6/scope6_var.h>
79#endif
80#include <netinet/tcp.h>
81#include <netinet/tcp_fsm.h>
82#include <netinet/tcp_seq.h>
83#include <netinet/tcp_timer.h>
84#include <netinet/tcp_var.h>
85#include <netinet/tcpip.h>
86#ifdef TCPDEBUG
87#include <netinet/tcp_debug.h>
88#endif
89#include <netinet/tcp_offload.h>
90
91/*
92 * TCP protocol interface to socket abstraction.
93 */
94static int	tcp_attach(struct socket *);
95static int	tcp_connect(struct tcpcb *, struct sockaddr *,
96		    struct thread *td);
97#ifdef INET6
98static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
99		    struct thread *td);
100#endif /* INET6 */
101static void	tcp_disconnect(struct tcpcb *);
102static void	tcp_usrclosed(struct tcpcb *);
103static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
104
105#ifdef TCPDEBUG
106#define	TCPDEBUG0	int ostate = 0
107#define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
108#define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
109				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
110#else
111#define	TCPDEBUG0
112#define	TCPDEBUG1()
113#define	TCPDEBUG2(req)
114#endif
115
116/*
117 * TCP attaches to socket via pru_attach(), reserving space,
118 * and an internet control block.
119 */
120static int
121tcp_usr_attach(struct socket *so, int proto, struct thread *td)
122{
123	struct inpcb *inp;
124	struct tcpcb *tp = NULL;
125	int error;
126	TCPDEBUG0;
127
128	inp = sotoinpcb(so);
129	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
130	TCPDEBUG1();
131
132	error = tcp_attach(so);
133	if (error)
134		goto out;
135
136	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
137		so->so_linger = TCP_LINGERTIME;
138
139	inp = sotoinpcb(so);
140	tp = intotcpcb(inp);
141out:
142	TCPDEBUG2(PRU_ATTACH);
143	return error;
144}
145
146/*
147 * tcp_detach is called when the socket layer loses its final reference
148 * to the socket, be it a file descriptor reference, a reference from TCP,
149 * etc.  At this point, there is only one case in which we will keep around
150 * inpcb state: time wait.
151 *
152 * This function can probably be re-absorbed back into tcp_usr_detach() now
153 * that there is a single detach path.
154 */
155static void
156tcp_detach(struct socket *so, struct inpcb *inp)
157{
158	struct tcpcb *tp;
159#ifdef INVARIANTS
160	INIT_VNET_INET(so->so_vnet);
161#endif
162
163	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
164	INP_WLOCK_ASSERT(inp);
165
166	KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
167	KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
168
169	tp = intotcpcb(inp);
170
171	if (inp->inp_vflag & INP_TIMEWAIT) {
172		/*
173		 * There are two cases to handle: one in which the time wait
174		 * state is being discarded (INP_DROPPED), and one in which
175		 * this connection will remain in timewait.  In the former,
176		 * it is time to discard all state (except tcptw, which has
177		 * already been discarded by the timewait close code, which
178		 * should be further up the call stack somewhere).  In the
179		 * latter case, we detach from the socket, but leave the pcb
180		 * present until timewait ends.
181		 *
182		 * XXXRW: Would it be cleaner to free the tcptw here?
183		 */
184		if (inp->inp_vflag & INP_DROPPED) {
185			KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
186			    "INP_DROPPED && tp != NULL"));
187			in_pcbdetach(inp);
188			in_pcbfree(inp);
189		} else {
190			in_pcbdetach(inp);
191			INP_WUNLOCK(inp);
192		}
193	} else {
194		/*
195		 * If the connection is not in timewait, we consider two
196		 * two conditions: one in which no further processing is
197		 * necessary (dropped || embryonic), and one in which TCP is
198		 * not yet done, but no longer requires the socket, so the
199		 * pcb will persist for the time being.
200		 *
201		 * XXXRW: Does the second case still occur?
202		 */
203		if (inp->inp_vflag & INP_DROPPED ||
204		    tp->t_state < TCPS_SYN_SENT) {
205			tcp_discardcb(tp);
206			in_pcbdetach(inp);
207			in_pcbfree(inp);
208		} else
209			in_pcbdetach(inp);
210	}
211}
212
213/*
214 * pru_detach() detaches the TCP protocol from the socket.
215 * If the protocol state is non-embryonic, then can't
216 * do this directly: have to initiate a pru_disconnect(),
217 * which may finish later; embryonic TCB's can just
218 * be discarded here.
219 */
220static void
221tcp_usr_detach(struct socket *so)
222{
223	INIT_VNET_INET(so->so_vnet);
224	struct inpcb *inp;
225
226	inp = sotoinpcb(so);
227	KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
228	INP_INFO_WLOCK(&V_tcbinfo);
229	INP_WLOCK(inp);
230	KASSERT(inp->inp_socket != NULL,
231	    ("tcp_usr_detach: inp_socket == NULL"));
232	tcp_detach(so, inp);
233	INP_INFO_WUNLOCK(&V_tcbinfo);
234}
235
236/*
237 * Give the socket an address.
238 */
239static int
240tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
241{
242	INIT_VNET_INET(so->so_vnet);
243	int error = 0;
244	struct inpcb *inp;
245	struct tcpcb *tp = NULL;
246	struct sockaddr_in *sinp;
247
248	sinp = (struct sockaddr_in *)nam;
249	if (nam->sa_len != sizeof (*sinp))
250		return (EINVAL);
251	/*
252	 * Must check for multicast addresses and disallow binding
253	 * to them.
254	 */
255	if (sinp->sin_family == AF_INET &&
256	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
257		return (EAFNOSUPPORT);
258
259	TCPDEBUG0;
260	INP_INFO_WLOCK(&V_tcbinfo);
261	inp = sotoinpcb(so);
262	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
263	INP_WLOCK(inp);
264	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
265		error = EINVAL;
266		goto out;
267	}
268	tp = intotcpcb(inp);
269	TCPDEBUG1();
270	error = in_pcbbind(inp, nam, td->td_ucred);
271out:
272	TCPDEBUG2(PRU_BIND);
273	INP_WUNLOCK(inp);
274	INP_INFO_WUNLOCK(&V_tcbinfo);
275
276	return (error);
277}
278
279#ifdef INET6
280static int
281tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
282{
283	INIT_VNET_INET(so->so_vnet);
284	int error = 0;
285	struct inpcb *inp;
286	struct tcpcb *tp = NULL;
287	struct sockaddr_in6 *sin6p;
288
289	sin6p = (struct sockaddr_in6 *)nam;
290	if (nam->sa_len != sizeof (*sin6p))
291		return (EINVAL);
292	/*
293	 * Must check for multicast addresses and disallow binding
294	 * to them.
295	 */
296	if (sin6p->sin6_family == AF_INET6 &&
297	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
298		return (EAFNOSUPPORT);
299
300	TCPDEBUG0;
301	INP_INFO_WLOCK(&V_tcbinfo);
302	inp = sotoinpcb(so);
303	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
304	INP_WLOCK(inp);
305	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
306		error = EINVAL;
307		goto out;
308	}
309	tp = intotcpcb(inp);
310	TCPDEBUG1();
311	inp->inp_vflag &= ~INP_IPV4;
312	inp->inp_vflag |= INP_IPV6;
313	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
314		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
315			inp->inp_vflag |= INP_IPV4;
316		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
317			struct sockaddr_in sin;
318
319			in6_sin6_2_sin(&sin, sin6p);
320			inp->inp_vflag |= INP_IPV4;
321			inp->inp_vflag &= ~INP_IPV6;
322			error = in_pcbbind(inp, (struct sockaddr *)&sin,
323			    td->td_ucred);
324			goto out;
325		}
326	}
327	error = in6_pcbbind(inp, nam, td->td_ucred);
328out:
329	TCPDEBUG2(PRU_BIND);
330	INP_WUNLOCK(inp);
331	INP_INFO_WUNLOCK(&V_tcbinfo);
332	return (error);
333}
334#endif /* INET6 */
335
336/*
337 * Prepare to accept connections.
338 */
339static int
340tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
341{
342	INIT_VNET_INET(so->so_vnet);
343	int error = 0;
344	struct inpcb *inp;
345	struct tcpcb *tp = NULL;
346
347	TCPDEBUG0;
348	INP_INFO_WLOCK(&V_tcbinfo);
349	inp = sotoinpcb(so);
350	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
351	INP_WLOCK(inp);
352	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
353		error = EINVAL;
354		goto out;
355	}
356	tp = intotcpcb(inp);
357	TCPDEBUG1();
358	SOCK_LOCK(so);
359	error = solisten_proto_check(so);
360	if (error == 0 && inp->inp_lport == 0)
361		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
362	if (error == 0) {
363		tp->t_state = TCPS_LISTEN;
364		solisten_proto(so, backlog);
365		tcp_offload_listen_open(tp);
366	}
367	SOCK_UNLOCK(so);
368
369out:
370	TCPDEBUG2(PRU_LISTEN);
371	INP_WUNLOCK(inp);
372	INP_INFO_WUNLOCK(&V_tcbinfo);
373	return (error);
374}
375
376#ifdef INET6
377static int
378tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
379{
380	INIT_VNET_INET(so->so_vnet);
381	int error = 0;
382	struct inpcb *inp;
383	struct tcpcb *tp = NULL;
384
385	TCPDEBUG0;
386	INP_INFO_WLOCK(&V_tcbinfo);
387	inp = sotoinpcb(so);
388	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
389	INP_WLOCK(inp);
390	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
391		error = EINVAL;
392		goto out;
393	}
394	tp = intotcpcb(inp);
395	TCPDEBUG1();
396	SOCK_LOCK(so);
397	error = solisten_proto_check(so);
398	if (error == 0 && inp->inp_lport == 0) {
399		inp->inp_vflag &= ~INP_IPV4;
400		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
401			inp->inp_vflag |= INP_IPV4;
402		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
403	}
404	if (error == 0) {
405		tp->t_state = TCPS_LISTEN;
406		solisten_proto(so, backlog);
407	}
408	SOCK_UNLOCK(so);
409
410out:
411	TCPDEBUG2(PRU_LISTEN);
412	INP_WUNLOCK(inp);
413	INP_INFO_WUNLOCK(&V_tcbinfo);
414	return (error);
415}
416#endif /* INET6 */
417
418/*
419 * Initiate connection to peer.
420 * Create a template for use in transmissions on this connection.
421 * Enter SYN_SENT state, and mark socket as connecting.
422 * Start keep-alive timer, and seed output sequence space.
423 * Send initial segment on connection.
424 */
425static int
426tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
427{
428	INIT_VNET_INET(so->so_vnet);
429	int error = 0;
430	struct inpcb *inp;
431	struct tcpcb *tp = NULL;
432	struct sockaddr_in *sinp;
433
434	sinp = (struct sockaddr_in *)nam;
435	if (nam->sa_len != sizeof (*sinp))
436		return (EINVAL);
437	/*
438	 * Must disallow TCP ``connections'' to multicast addresses.
439	 */
440	if (sinp->sin_family == AF_INET
441	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
442		return (EAFNOSUPPORT);
443	if (jailed(td->td_ucred))
444		prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
445
446	TCPDEBUG0;
447	INP_INFO_WLOCK(&V_tcbinfo);
448	inp = sotoinpcb(so);
449	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
450	INP_WLOCK(inp);
451	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
452		error = EINVAL;
453		goto out;
454	}
455	tp = intotcpcb(inp);
456	TCPDEBUG1();
457	if ((error = tcp_connect(tp, nam, td)) != 0)
458		goto out;
459	error = tcp_output_connect(so, nam);
460out:
461	TCPDEBUG2(PRU_CONNECT);
462	INP_WUNLOCK(inp);
463	INP_INFO_WUNLOCK(&V_tcbinfo);
464	return (error);
465}
466
467#ifdef INET6
468static int
469tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
470{
471	INIT_VNET_INET(so->so_vnet);
472	int error = 0;
473	struct inpcb *inp;
474	struct tcpcb *tp = NULL;
475	struct sockaddr_in6 *sin6p;
476
477	TCPDEBUG0;
478
479	sin6p = (struct sockaddr_in6 *)nam;
480	if (nam->sa_len != sizeof (*sin6p))
481		return (EINVAL);
482	/*
483	 * Must disallow TCP ``connections'' to multicast addresses.
484	 */
485	if (sin6p->sin6_family == AF_INET6
486	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
487		return (EAFNOSUPPORT);
488
489	INP_INFO_WLOCK(&V_tcbinfo);
490	inp = sotoinpcb(so);
491	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
492	INP_WLOCK(inp);
493	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
494		error = EINVAL;
495		goto out;
496	}
497	tp = intotcpcb(inp);
498	TCPDEBUG1();
499	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
500		struct sockaddr_in sin;
501
502		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
503			error = EINVAL;
504			goto out;
505		}
506
507		in6_sin6_2_sin(&sin, sin6p);
508		inp->inp_vflag |= INP_IPV4;
509		inp->inp_vflag &= ~INP_IPV6;
510		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
511			goto out;
512		error = tcp_output_connect(so, nam);
513		goto out;
514	}
515	inp->inp_vflag &= ~INP_IPV4;
516	inp->inp_vflag |= INP_IPV6;
517	inp->inp_inc.inc_isipv6 = 1;
518	if ((error = tcp6_connect(tp, nam, td)) != 0)
519		goto out;
520	error = tcp_output_connect(so, nam);
521
522out:
523	TCPDEBUG2(PRU_CONNECT);
524	INP_WUNLOCK(inp);
525	INP_INFO_WUNLOCK(&V_tcbinfo);
526	return (error);
527}
528#endif /* INET6 */
529
530/*
531 * Initiate disconnect from peer.
532 * If connection never passed embryonic stage, just drop;
533 * else if don't need to let data drain, then can just drop anyways,
534 * else have to begin TCP shutdown process: mark socket disconnecting,
535 * drain unread data, state switch to reflect user close, and
536 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
537 * when peer sends FIN and acks ours.
538 *
539 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
540 */
541static int
542tcp_usr_disconnect(struct socket *so)
543{
544	INIT_VNET_INET(so->so_vnet);
545	struct inpcb *inp;
546	struct tcpcb *tp = NULL;
547	int error = 0;
548
549	TCPDEBUG0;
550	INP_INFO_WLOCK(&V_tcbinfo);
551	inp = sotoinpcb(so);
552	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
553	INP_WLOCK(inp);
554	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
555		error = ECONNRESET;
556		goto out;
557	}
558	tp = intotcpcb(inp);
559	TCPDEBUG1();
560	tcp_disconnect(tp);
561out:
562	TCPDEBUG2(PRU_DISCONNECT);
563	INP_WUNLOCK(inp);
564	INP_INFO_WUNLOCK(&V_tcbinfo);
565	return (error);
566}
567
568/*
569 * Accept a connection.  Essentially all the work is
570 * done at higher levels; just return the address
571 * of the peer, storing through addr.
572 */
573static int
574tcp_usr_accept(struct socket *so, struct sockaddr **nam)
575{
576	INIT_VNET_INET(so->so_vnet);
577	int error = 0;
578	struct inpcb *inp = NULL;
579	struct tcpcb *tp = NULL;
580	struct in_addr addr;
581	in_port_t port = 0;
582	TCPDEBUG0;
583
584	if (so->so_state & SS_ISDISCONNECTED)
585		return (ECONNABORTED);
586
587	inp = sotoinpcb(so);
588	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
589	INP_INFO_RLOCK(&V_tcbinfo);
590	INP_WLOCK(inp);
591	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
592		error = ECONNABORTED;
593		goto out;
594	}
595	tp = intotcpcb(inp);
596	TCPDEBUG1();
597
598	/*
599	 * We inline in_getpeeraddr and COMMON_END here, so that we can
600	 * copy the data of interest and defer the malloc until after we
601	 * release the lock.
602	 */
603	port = inp->inp_fport;
604	addr = inp->inp_faddr;
605
606out:
607	TCPDEBUG2(PRU_ACCEPT);
608	INP_WUNLOCK(inp);
609	INP_INFO_RUNLOCK(&V_tcbinfo);
610	if (error == 0)
611		*nam = in_sockaddr(port, &addr);
612	return error;
613}
614
615#ifdef INET6
616static int
617tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
618{
619	struct inpcb *inp = NULL;
620	int error = 0;
621	struct tcpcb *tp = NULL;
622	struct in_addr addr;
623	struct in6_addr addr6;
624	in_port_t port = 0;
625	int v4 = 0;
626	TCPDEBUG0;
627
628	if (so->so_state & SS_ISDISCONNECTED)
629		return (ECONNABORTED);
630
631	inp = sotoinpcb(so);
632	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
633	INP_WLOCK(inp);
634	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
635		error = ECONNABORTED;
636		goto out;
637	}
638	tp = intotcpcb(inp);
639	TCPDEBUG1();
640
641	/*
642	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
643	 * copy the data of interest and defer the malloc until after we
644	 * release the lock.
645	 */
646	if (inp->inp_vflag & INP_IPV4) {
647		v4 = 1;
648		port = inp->inp_fport;
649		addr = inp->inp_faddr;
650	} else {
651		port = inp->inp_fport;
652		addr6 = inp->in6p_faddr;
653	}
654
655out:
656	TCPDEBUG2(PRU_ACCEPT);
657	INP_WUNLOCK(inp);
658	if (error == 0) {
659		if (v4)
660			*nam = in6_v4mapsin6_sockaddr(port, &addr);
661		else
662			*nam = in6_sockaddr(port, &addr6);
663	}
664	return error;
665}
666#endif /* INET6 */
667
668/*
669 * Mark the connection as being incapable of further output.
670 */
671static int
672tcp_usr_shutdown(struct socket *so)
673{
674	INIT_VNET_INET(so->so_vnet);
675	int error = 0;
676	struct inpcb *inp;
677	struct tcpcb *tp = NULL;
678
679	TCPDEBUG0;
680	INP_INFO_WLOCK(&V_tcbinfo);
681	inp = sotoinpcb(so);
682	KASSERT(inp != NULL, ("inp == NULL"));
683	INP_WLOCK(inp);
684	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
685		error = ECONNRESET;
686		goto out;
687	}
688	tp = intotcpcb(inp);
689	TCPDEBUG1();
690	socantsendmore(so);
691	tcp_usrclosed(tp);
692	error = tcp_output_disconnect(tp);
693
694out:
695	TCPDEBUG2(PRU_SHUTDOWN);
696	INP_WUNLOCK(inp);
697	INP_INFO_WUNLOCK(&V_tcbinfo);
698
699	return (error);
700}
701
702/*
703 * After a receive, possibly send window update to peer.
704 */
705static int
706tcp_usr_rcvd(struct socket *so, int flags)
707{
708	struct inpcb *inp;
709	struct tcpcb *tp = NULL;
710	int error = 0;
711
712	TCPDEBUG0;
713	inp = sotoinpcb(so);
714	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
715	INP_WLOCK(inp);
716	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
717		error = ECONNRESET;
718		goto out;
719	}
720	tp = intotcpcb(inp);
721	TCPDEBUG1();
722	tcp_output_rcvd(tp);
723
724out:
725	TCPDEBUG2(PRU_RCVD);
726	INP_WUNLOCK(inp);
727	return (error);
728}
729
730/*
731 * Do a send by putting data in output queue and updating urgent
732 * marker if URG set.  Possibly send more data.  Unlike the other
733 * pru_*() routines, the mbuf chains are our responsibility.  We
734 * must either enqueue them or free them.  The other pru_* routines
735 * generally are caller-frees.
736 */
737static int
738tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
739    struct sockaddr *nam, struct mbuf *control, struct thread *td)
740{
741	INIT_VNET_INET(so->so_vnet);
742	int error = 0;
743	struct inpcb *inp;
744	struct tcpcb *tp = NULL;
745	int headlocked = 0;
746#ifdef INET6
747	int isipv6;
748#endif
749	TCPDEBUG0;
750
751	/*
752	 * We require the pcbinfo lock in two cases:
753	 *
754	 * (1) An implied connect is taking place, which can result in
755	 *     binding IPs and ports and hence modification of the pcb hash
756	 *     chains.
757	 *
758	 * (2) PRUS_EOF is set, resulting in explicit close on the send.
759	 */
760	if ((nam != NULL) || (flags & PRUS_EOF)) {
761		INP_INFO_WLOCK(&V_tcbinfo);
762		headlocked = 1;
763	}
764	inp = sotoinpcb(so);
765	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
766	INP_WLOCK(inp);
767	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
768		if (control)
769			m_freem(control);
770		if (m)
771			m_freem(m);
772		error = ECONNRESET;
773		goto out;
774	}
775#ifdef INET6
776	isipv6 = nam && nam->sa_family == AF_INET6;
777#endif /* INET6 */
778	tp = intotcpcb(inp);
779	TCPDEBUG1();
780	if (control) {
781		/* TCP doesn't do control messages (rights, creds, etc) */
782		if (control->m_len) {
783			m_freem(control);
784			if (m)
785				m_freem(m);
786			error = EINVAL;
787			goto out;
788		}
789		m_freem(control);	/* empty control, just free it */
790	}
791	if (!(flags & PRUS_OOB)) {
792		sbappendstream(&so->so_snd, m);
793		if (nam && tp->t_state < TCPS_SYN_SENT) {
794			/*
795			 * Do implied connect if not yet connected,
796			 * initialize window to default value, and
797			 * initialize maxseg/maxopd using peer's cached
798			 * MSS.
799			 */
800			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
801#ifdef INET6
802			if (isipv6)
803				error = tcp6_connect(tp, nam, td);
804			else
805#endif /* INET6 */
806			error = tcp_connect(tp, nam, td);
807			if (error)
808				goto out;
809			tp->snd_wnd = TTCP_CLIENT_SND_WND;
810			tcp_mss(tp, -1);
811		}
812		if (flags & PRUS_EOF) {
813			/*
814			 * Close the send side of the connection after
815			 * the data is sent.
816			 */
817			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
818			socantsendmore(so);
819			tcp_usrclosed(tp);
820		}
821		if (headlocked) {
822			INP_INFO_WUNLOCK(&V_tcbinfo);
823			headlocked = 0;
824		}
825		if (tp != NULL) {
826			if (flags & PRUS_MORETOCOME)
827				tp->t_flags |= TF_MORETOCOME;
828			error = tcp_output_send(tp);
829			if (flags & PRUS_MORETOCOME)
830				tp->t_flags &= ~TF_MORETOCOME;
831		}
832	} else {
833		/*
834		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
835		 */
836		SOCKBUF_LOCK(&so->so_snd);
837		if (sbspace(&so->so_snd) < -512) {
838			SOCKBUF_UNLOCK(&so->so_snd);
839			m_freem(m);
840			error = ENOBUFS;
841			goto out;
842		}
843		/*
844		 * According to RFC961 (Assigned Protocols),
845		 * the urgent pointer points to the last octet
846		 * of urgent data.  We continue, however,
847		 * to consider it to indicate the first octet
848		 * of data past the urgent section.
849		 * Otherwise, snd_up should be one lower.
850		 */
851		sbappendstream_locked(&so->so_snd, m);
852		SOCKBUF_UNLOCK(&so->so_snd);
853		if (nam && tp->t_state < TCPS_SYN_SENT) {
854			/*
855			 * Do implied connect if not yet connected,
856			 * initialize window to default value, and
857			 * initialize maxseg/maxopd using peer's cached
858			 * MSS.
859			 */
860			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
861#ifdef INET6
862			if (isipv6)
863				error = tcp6_connect(tp, nam, td);
864			else
865#endif /* INET6 */
866			error = tcp_connect(tp, nam, td);
867			if (error)
868				goto out;
869			tp->snd_wnd = TTCP_CLIENT_SND_WND;
870			tcp_mss(tp, -1);
871			INP_INFO_WUNLOCK(&V_tcbinfo);
872			headlocked = 0;
873		} else if (nam) {
874			INP_INFO_WUNLOCK(&V_tcbinfo);
875			headlocked = 0;
876		}
877		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
878		tp->t_flags |= TF_FORCEDATA;
879		error = tcp_output_send(tp);
880		tp->t_flags &= ~TF_FORCEDATA;
881	}
882out:
883	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
884		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
885	INP_WUNLOCK(inp);
886	if (headlocked)
887		INP_INFO_WUNLOCK(&V_tcbinfo);
888	return (error);
889}
890
891/*
892 * Abort the TCP.  Drop the connection abruptly.
893 */
894static void
895tcp_usr_abort(struct socket *so)
896{
897	INIT_VNET_INET(so->so_vnet);
898	struct inpcb *inp;
899	struct tcpcb *tp = NULL;
900	TCPDEBUG0;
901
902	inp = sotoinpcb(so);
903	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
904
905	INP_INFO_WLOCK(&V_tcbinfo);
906	INP_WLOCK(inp);
907	KASSERT(inp->inp_socket != NULL,
908	    ("tcp_usr_abort: inp_socket == NULL"));
909
910	/*
911	 * If we still have full TCP state, and we're not dropped, drop.
912	 */
913	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
914	    !(inp->inp_vflag & INP_DROPPED)) {
915		tp = intotcpcb(inp);
916		TCPDEBUG1();
917		tcp_drop(tp, ECONNABORTED);
918		TCPDEBUG2(PRU_ABORT);
919	}
920	if (!(inp->inp_vflag & INP_DROPPED)) {
921		SOCK_LOCK(so);
922		so->so_state |= SS_PROTOREF;
923		SOCK_UNLOCK(so);
924		inp->inp_vflag |= INP_SOCKREF;
925	}
926	INP_WUNLOCK(inp);
927	INP_INFO_WUNLOCK(&V_tcbinfo);
928}
929
930/*
931 * TCP socket is closed.  Start friendly disconnect.
932 */
933static void
934tcp_usr_close(struct socket *so)
935{
936	INIT_VNET_INET(so->so_vnet);
937	struct inpcb *inp;
938	struct tcpcb *tp = NULL;
939	TCPDEBUG0;
940
941	inp = sotoinpcb(so);
942	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
943
944	INP_INFO_WLOCK(&V_tcbinfo);
945	INP_WLOCK(inp);
946	KASSERT(inp->inp_socket != NULL,
947	    ("tcp_usr_close: inp_socket == NULL"));
948
949	/*
950	 * If we still have full TCP state, and we're not dropped, initiate
951	 * a disconnect.
952	 */
953	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
954	    !(inp->inp_vflag & INP_DROPPED)) {
955		tp = intotcpcb(inp);
956		TCPDEBUG1();
957		tcp_disconnect(tp);
958		TCPDEBUG2(PRU_CLOSE);
959	}
960	if (!(inp->inp_vflag & INP_DROPPED)) {
961		SOCK_LOCK(so);
962		so->so_state |= SS_PROTOREF;
963		SOCK_UNLOCK(so);
964		inp->inp_vflag |= INP_SOCKREF;
965	}
966	INP_WUNLOCK(inp);
967	INP_INFO_WUNLOCK(&V_tcbinfo);
968}
969
970/*
971 * Receive out-of-band data.
972 */
973static int
974tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
975{
976	int error = 0;
977	struct inpcb *inp;
978	struct tcpcb *tp = NULL;
979
980	TCPDEBUG0;
981	inp = sotoinpcb(so);
982	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
983	INP_WLOCK(inp);
984	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
985		error = ECONNRESET;
986		goto out;
987	}
988	tp = intotcpcb(inp);
989	TCPDEBUG1();
990	if ((so->so_oobmark == 0 &&
991	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
992	    so->so_options & SO_OOBINLINE ||
993	    tp->t_oobflags & TCPOOB_HADDATA) {
994		error = EINVAL;
995		goto out;
996	}
997	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
998		error = EWOULDBLOCK;
999		goto out;
1000	}
1001	m->m_len = 1;
1002	*mtod(m, caddr_t) = tp->t_iobc;
1003	if ((flags & MSG_PEEK) == 0)
1004		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1005
1006out:
1007	TCPDEBUG2(PRU_RCVOOB);
1008	INP_WUNLOCK(inp);
1009	return (error);
1010}
1011
1012struct pr_usrreqs tcp_usrreqs = {
1013	.pru_abort =		tcp_usr_abort,
1014	.pru_accept =		tcp_usr_accept,
1015	.pru_attach =		tcp_usr_attach,
1016	.pru_bind =		tcp_usr_bind,
1017	.pru_connect =		tcp_usr_connect,
1018	.pru_control =		in_control,
1019	.pru_detach =		tcp_usr_detach,
1020	.pru_disconnect =	tcp_usr_disconnect,
1021	.pru_listen =		tcp_usr_listen,
1022	.pru_peeraddr =		in_getpeeraddr,
1023	.pru_rcvd =		tcp_usr_rcvd,
1024	.pru_rcvoob =		tcp_usr_rcvoob,
1025	.pru_send =		tcp_usr_send,
1026	.pru_shutdown =		tcp_usr_shutdown,
1027	.pru_sockaddr =		in_getsockaddr,
1028	.pru_sosetlabel =	in_pcbsosetlabel,
1029	.pru_close =		tcp_usr_close,
1030};
1031
1032#ifdef INET6
1033struct pr_usrreqs tcp6_usrreqs = {
1034	.pru_abort =		tcp_usr_abort,
1035	.pru_accept =		tcp6_usr_accept,
1036	.pru_attach =		tcp_usr_attach,
1037	.pru_bind =		tcp6_usr_bind,
1038	.pru_connect =		tcp6_usr_connect,
1039	.pru_control =		in6_control,
1040	.pru_detach =		tcp_usr_detach,
1041	.pru_disconnect =	tcp_usr_disconnect,
1042	.pru_listen =		tcp6_usr_listen,
1043	.pru_peeraddr =		in6_mapped_peeraddr,
1044	.pru_rcvd =		tcp_usr_rcvd,
1045	.pru_rcvoob =		tcp_usr_rcvoob,
1046	.pru_send =		tcp_usr_send,
1047	.pru_shutdown =		tcp_usr_shutdown,
1048	.pru_sockaddr =		in6_mapped_sockaddr,
1049 	.pru_sosetlabel =	in_pcbsosetlabel,
1050	.pru_close =		tcp_usr_close,
1051};
1052#endif /* INET6 */
1053
1054/*
1055 * Common subroutine to open a TCP connection to remote host specified
1056 * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
1057 * port number if needed.  Call in_pcbconnect_setup to do the routing and
1058 * to choose a local host address (interface).  If there is an existing
1059 * incarnation of the same connection in TIME-WAIT state and if the remote
1060 * host was sending CC options and if the connection duration was < MSL, then
1061 * truncate the previous TIME-WAIT state and proceed.
1062 * Initialize connection parameters and enter SYN-SENT state.
1063 */
1064static int
1065tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1066{
1067	struct inpcb *inp = tp->t_inpcb, *oinp;
1068	struct socket *so = inp->inp_socket;
1069	INIT_VNET_INET(so->so_vnet);
1070	struct in_addr laddr;
1071	u_short lport;
1072	int error;
1073
1074	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1075	INP_WLOCK_ASSERT(inp);
1076
1077	if (inp->inp_lport == 0) {
1078		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1079		if (error)
1080			return error;
1081	}
1082
1083	/*
1084	 * Cannot simply call in_pcbconnect, because there might be an
1085	 * earlier incarnation of this same connection still in
1086	 * TIME_WAIT state, creating an ADDRINUSE error.
1087	 */
1088	laddr = inp->inp_laddr;
1089	lport = inp->inp_lport;
1090	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1091	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1092	if (error && oinp == NULL)
1093		return error;
1094	if (oinp)
1095		return EADDRINUSE;
1096	inp->inp_laddr = laddr;
1097	in_pcbrehash(inp);
1098
1099	/*
1100	 * Compute window scaling to request:
1101	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1102	 * XXX: This should move to tcp_output().
1103	 */
1104	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1105	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1106		tp->request_r_scale++;
1107
1108	soisconnecting(so);
1109	V_tcpstat.tcps_connattempt++;
1110	tp->t_state = TCPS_SYN_SENT;
1111	tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1112	tp->iss = tcp_new_isn(tp);
1113	tp->t_bw_rtseq = tp->iss;
1114	tcp_sendseqinit(tp);
1115
1116	return 0;
1117}
1118
1119#ifdef INET6
1120static int
1121tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1122{
1123	struct inpcb *inp = tp->t_inpcb, *oinp;
1124	struct socket *so = inp->inp_socket;
1125	INIT_VNET_INET(so->so_vnet);
1126	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1127	struct in6_addr *addr6;
1128	int error;
1129
1130	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1131	INP_WLOCK_ASSERT(inp);
1132
1133	if (inp->inp_lport == 0) {
1134		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1135		if (error)
1136			return error;
1137	}
1138
1139	/*
1140	 * Cannot simply call in_pcbconnect, because there might be an
1141	 * earlier incarnation of this same connection still in
1142	 * TIME_WAIT state, creating an ADDRINUSE error.
1143	 * in6_pcbladdr() also handles scope zone IDs.
1144	 */
1145	error = in6_pcbladdr(inp, nam, &addr6);
1146	if (error)
1147		return error;
1148	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1149				  &sin6->sin6_addr, sin6->sin6_port,
1150				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1151				  ? addr6
1152				  : &inp->in6p_laddr,
1153				  inp->inp_lport,  0, NULL);
1154	if (oinp)
1155		return EADDRINUSE;
1156	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1157		inp->in6p_laddr = *addr6;
1158	inp->in6p_faddr = sin6->sin6_addr;
1159	inp->inp_fport = sin6->sin6_port;
1160	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
1161	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
1162	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
1163		inp->in6p_flowinfo |=
1164		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1165	in_pcbrehash(inp);
1166
1167	/* Compute window scaling to request.  */
1168	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1169	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1170		tp->request_r_scale++;
1171
1172	soisconnecting(so);
1173	V_tcpstat.tcps_connattempt++;
1174	tp->t_state = TCPS_SYN_SENT;
1175	tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1176	tp->iss = tcp_new_isn(tp);
1177	tp->t_bw_rtseq = tp->iss;
1178	tcp_sendseqinit(tp);
1179
1180	return 0;
1181}
1182#endif /* INET6 */
1183
1184/*
1185 * Export TCP internal state information via a struct tcp_info, based on the
1186 * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1187 * (TCP state machine, etc).  We export all information using FreeBSD-native
1188 * constants -- for example, the numeric values for tcpi_state will differ
1189 * from Linux.
1190 */
1191static void
1192tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1193{
1194
1195	INP_WLOCK_ASSERT(tp->t_inpcb);
1196	bzero(ti, sizeof(*ti));
1197
1198	ti->tcpi_state = tp->t_state;
1199	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1200		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1201	if (tp->t_flags & TF_SACK_PERMIT)
1202		ti->tcpi_options |= TCPI_OPT_SACK;
1203	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1204		ti->tcpi_options |= TCPI_OPT_WSCALE;
1205		ti->tcpi_snd_wscale = tp->snd_scale;
1206		ti->tcpi_rcv_wscale = tp->rcv_scale;
1207	}
1208
1209	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1210	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1211
1212	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1213	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1214
1215	/*
1216	 * FreeBSD-specific extension fields for tcp_info.
1217	 */
1218	ti->tcpi_rcv_space = tp->rcv_wnd;
1219	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1220	ti->tcpi_snd_wnd = tp->snd_wnd;
1221	ti->tcpi_snd_bwnd = tp->snd_bwnd;
1222	ti->tcpi_snd_nxt = tp->snd_nxt;
1223	ti->__tcpi_snd_mss = tp->t_maxseg;
1224	ti->__tcpi_rcv_mss = tp->t_maxseg;
1225	if (tp->t_flags & TF_TOE)
1226		ti->tcpi_options |= TCPI_OPT_TOE;
1227}
1228
1229/*
1230 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1231 * socket option arguments.  When it re-acquires the lock after the copy, it
1232 * has to revalidate that the connection is still valid for the socket
1233 * option.
1234 */
1235#define INP_WLOCK_RECHECK(inp) do {					\
1236	INP_WLOCK(inp);							\
1237	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {		\
1238		INP_WUNLOCK(inp);					\
1239		return (ECONNRESET);					\
1240	}								\
1241	tp = intotcpcb(inp);						\
1242} while(0)
1243
1244int
1245tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1246{
1247	INIT_VNET_INET(so->so_vnet);
1248	int	error, opt, optval;
1249	struct	inpcb *inp;
1250	struct	tcpcb *tp;
1251	struct	tcp_info ti;
1252
1253	error = 0;
1254	inp = sotoinpcb(so);
1255	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1256	INP_WLOCK(inp);
1257	if (sopt->sopt_level != IPPROTO_TCP) {
1258#ifdef INET6
1259		if (INP_CHECK_SOCKAF(so, AF_INET6)) {
1260			INP_WUNLOCK(inp);
1261			error = ip6_ctloutput(so, sopt);
1262		} else {
1263#endif /* INET6 */
1264			INP_WUNLOCK(inp);
1265			error = ip_ctloutput(so, sopt);
1266#ifdef INET6
1267		}
1268#endif
1269		return (error);
1270	}
1271	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
1272		INP_WUNLOCK(inp);
1273		return (ECONNRESET);
1274	}
1275
1276	switch (sopt->sopt_dir) {
1277	case SOPT_SET:
1278		switch (sopt->sopt_name) {
1279#ifdef TCP_SIGNATURE
1280		case TCP_MD5SIG:
1281			INP_WUNLOCK(inp);
1282			error = sooptcopyin(sopt, &optval, sizeof optval,
1283			    sizeof optval);
1284			if (error)
1285				return (error);
1286
1287			INP_WLOCK_RECHECK(inp);
1288			if (optval > 0)
1289				tp->t_flags |= TF_SIGNATURE;
1290			else
1291				tp->t_flags &= ~TF_SIGNATURE;
1292			INP_WUNLOCK(inp);
1293			break;
1294#endif /* TCP_SIGNATURE */
1295		case TCP_NODELAY:
1296		case TCP_NOOPT:
1297			INP_WUNLOCK(inp);
1298			error = sooptcopyin(sopt, &optval, sizeof optval,
1299			    sizeof optval);
1300			if (error)
1301				return (error);
1302
1303			INP_WLOCK_RECHECK(inp);
1304			switch (sopt->sopt_name) {
1305			case TCP_NODELAY:
1306				opt = TF_NODELAY;
1307				break;
1308			case TCP_NOOPT:
1309				opt = TF_NOOPT;
1310				break;
1311			default:
1312				opt = 0; /* dead code to fool gcc */
1313				break;
1314			}
1315
1316			if (optval)
1317				tp->t_flags |= opt;
1318			else
1319				tp->t_flags &= ~opt;
1320			INP_WUNLOCK(inp);
1321			break;
1322
1323		case TCP_NOPUSH:
1324			INP_WUNLOCK(inp);
1325			error = sooptcopyin(sopt, &optval, sizeof optval,
1326			    sizeof optval);
1327			if (error)
1328				return (error);
1329
1330			INP_WLOCK_RECHECK(inp);
1331			if (optval)
1332				tp->t_flags |= TF_NOPUSH;
1333			else {
1334				tp->t_flags &= ~TF_NOPUSH;
1335				error = tcp_output(tp);
1336			}
1337			INP_WUNLOCK(inp);
1338			break;
1339
1340		case TCP_MAXSEG:
1341			INP_WUNLOCK(inp);
1342			error = sooptcopyin(sopt, &optval, sizeof optval,
1343			    sizeof optval);
1344			if (error)
1345				return (error);
1346
1347			INP_WLOCK_RECHECK(inp);
1348			if (optval > 0 && optval <= tp->t_maxseg &&
1349			    optval + 40 >= V_tcp_minmss)
1350				tp->t_maxseg = optval;
1351			else
1352				error = EINVAL;
1353			INP_WUNLOCK(inp);
1354			break;
1355
1356		case TCP_INFO:
1357			INP_WUNLOCK(inp);
1358			error = EINVAL;
1359			break;
1360
1361		default:
1362			INP_WUNLOCK(inp);
1363			error = ENOPROTOOPT;
1364			break;
1365		}
1366		break;
1367
1368	case SOPT_GET:
1369		tp = intotcpcb(inp);
1370		switch (sopt->sopt_name) {
1371#ifdef TCP_SIGNATURE
1372		case TCP_MD5SIG:
1373			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1374			INP_WUNLOCK(inp);
1375			error = sooptcopyout(sopt, &optval, sizeof optval);
1376			break;
1377#endif
1378
1379		case TCP_NODELAY:
1380			optval = tp->t_flags & TF_NODELAY;
1381			INP_WUNLOCK(inp);
1382			error = sooptcopyout(sopt, &optval, sizeof optval);
1383			break;
1384		case TCP_MAXSEG:
1385			optval = tp->t_maxseg;
1386			INP_WUNLOCK(inp);
1387			error = sooptcopyout(sopt, &optval, sizeof optval);
1388			break;
1389		case TCP_NOOPT:
1390			optval = tp->t_flags & TF_NOOPT;
1391			INP_WUNLOCK(inp);
1392			error = sooptcopyout(sopt, &optval, sizeof optval);
1393			break;
1394		case TCP_NOPUSH:
1395			optval = tp->t_flags & TF_NOPUSH;
1396			INP_WUNLOCK(inp);
1397			error = sooptcopyout(sopt, &optval, sizeof optval);
1398			break;
1399		case TCP_INFO:
1400			tcp_fill_info(tp, &ti);
1401			INP_WUNLOCK(inp);
1402			error = sooptcopyout(sopt, &ti, sizeof ti);
1403			break;
1404		default:
1405			INP_WUNLOCK(inp);
1406			error = ENOPROTOOPT;
1407			break;
1408		}
1409		break;
1410	}
1411	return (error);
1412}
1413#undef INP_WLOCK_RECHECK
1414
1415/*
1416 * tcp_sendspace and tcp_recvspace are the default send and receive window
1417 * sizes, respectively.  These are obsolescent (this information should
1418 * be set by the route).
1419 */
1420u_long	tcp_sendspace = 1024*32;
1421SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1422    &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1423u_long	tcp_recvspace = 1024*64;
1424SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1425    &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1426
1427/*
1428 * Attach TCP protocol to socket, allocating
1429 * internet protocol control block, tcp control block,
1430 * bufer space, and entering LISTEN state if to accept connections.
1431 */
1432static int
1433tcp_attach(struct socket *so)
1434{
1435	INIT_VNET_INET(so->so_vnet);
1436	struct tcpcb *tp;
1437	struct inpcb *inp;
1438	int error;
1439#ifdef INET6
1440	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1441#endif
1442
1443	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1444		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1445		if (error)
1446			return (error);
1447	}
1448	so->so_rcv.sb_flags |= SB_AUTOSIZE;
1449	so->so_snd.sb_flags |= SB_AUTOSIZE;
1450	INP_INFO_WLOCK(&V_tcbinfo);
1451	error = in_pcballoc(so, &V_tcbinfo);
1452	if (error) {
1453		INP_INFO_WUNLOCK(&V_tcbinfo);
1454		return (error);
1455	}
1456	inp = sotoinpcb(so);
1457#ifdef INET6
1458	if (isipv6) {
1459		inp->inp_vflag |= INP_IPV6;
1460		inp->in6p_hops = -1;	/* use kernel default */
1461	}
1462	else
1463#endif
1464	inp->inp_vflag |= INP_IPV4;
1465	tp = tcp_newtcpcb(inp);
1466	if (tp == NULL) {
1467		in_pcbdetach(inp);
1468		in_pcbfree(inp);
1469		INP_INFO_WUNLOCK(&V_tcbinfo);
1470		return (ENOBUFS);
1471	}
1472	tp->t_state = TCPS_CLOSED;
1473	INP_WUNLOCK(inp);
1474	INP_INFO_WUNLOCK(&V_tcbinfo);
1475	return (0);
1476}
1477
1478/*
1479 * Initiate (or continue) disconnect.
1480 * If embryonic state, just send reset (once).
1481 * If in ``let data drain'' option and linger null, just drop.
1482 * Otherwise (hard), mark socket disconnecting and drop
1483 * current input data; switch states based on user close, and
1484 * send segment to peer (with FIN).
1485 */
1486static void
1487tcp_disconnect(struct tcpcb *tp)
1488{
1489	struct inpcb *inp = tp->t_inpcb;
1490	struct socket *so = inp->inp_socket;
1491#ifdef INVARIANTS
1492	INIT_VNET_INET(so->so_vnet);
1493#endif
1494
1495	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1496	INP_WLOCK_ASSERT(inp);
1497
1498	/*
1499	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1500	 * socket is still open.
1501	 */
1502	if (tp->t_state < TCPS_ESTABLISHED) {
1503		tp = tcp_close(tp);
1504		KASSERT(tp != NULL,
1505		    ("tcp_disconnect: tcp_close() returned NULL"));
1506	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1507		tp = tcp_drop(tp, 0);
1508		KASSERT(tp != NULL,
1509		    ("tcp_disconnect: tcp_drop() returned NULL"));
1510	} else {
1511		soisdisconnecting(so);
1512		sbflush(&so->so_rcv);
1513		tcp_usrclosed(tp);
1514		if (!(inp->inp_vflag & INP_DROPPED))
1515			tcp_output_disconnect(tp);
1516	}
1517}
1518
1519/*
1520 * User issued close, and wish to trail through shutdown states:
1521 * if never received SYN, just forget it.  If got a SYN from peer,
1522 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1523 * If already got a FIN from peer, then almost done; go to LAST_ACK
1524 * state.  In all other cases, have already sent FIN to peer (e.g.
1525 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1526 * for peer to send FIN or not respond to keep-alives, etc.
1527 * We can let the user exit from the close as soon as the FIN is acked.
1528 */
1529static void
1530tcp_usrclosed(struct tcpcb *tp)
1531{
1532#ifdef INVARIANTS
1533	INIT_VNET_INET(tp->t_inpcb->inp_vnet);
1534#endif
1535
1536	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1537	INP_WLOCK_ASSERT(tp->t_inpcb);
1538
1539	switch (tp->t_state) {
1540	case TCPS_LISTEN:
1541		tcp_offload_listen_close(tp);
1542		/* FALLTHROUGH */
1543	case TCPS_CLOSED:
1544		tp->t_state = TCPS_CLOSED;
1545		tp = tcp_close(tp);
1546		/*
1547		 * tcp_close() should never return NULL here as the socket is
1548		 * still open.
1549		 */
1550		KASSERT(tp != NULL,
1551		    ("tcp_usrclosed: tcp_close() returned NULL"));
1552		break;
1553
1554	case TCPS_SYN_SENT:
1555	case TCPS_SYN_RECEIVED:
1556		tp->t_flags |= TF_NEEDFIN;
1557		break;
1558
1559	case TCPS_ESTABLISHED:
1560		tp->t_state = TCPS_FIN_WAIT_1;
1561		break;
1562
1563	case TCPS_CLOSE_WAIT:
1564		tp->t_state = TCPS_LAST_ACK;
1565		break;
1566	}
1567	if (tp->t_state >= TCPS_FIN_WAIT_2) {
1568		soisdisconnected(tp->t_inpcb->inp_socket);
1569		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
1570		if (tp->t_state == TCPS_FIN_WAIT_2) {
1571			int timeout;
1572
1573			timeout = (tcp_fast_finwait2_recycle) ?
1574			    tcp_finwait2_timeout : tcp_maxidle;
1575			tcp_timer_activate(tp, TT_2MSL, timeout);
1576		}
1577	}
1578}
1579
1580#ifdef DDB
1581static void
1582db_print_indent(int indent)
1583{
1584	int i;
1585
1586	for (i = 0; i < indent; i++)
1587		db_printf(" ");
1588}
1589
1590static void
1591db_print_tstate(int t_state)
1592{
1593
1594	switch (t_state) {
1595	case TCPS_CLOSED:
1596		db_printf("TCPS_CLOSED");
1597		return;
1598
1599	case TCPS_LISTEN:
1600		db_printf("TCPS_LISTEN");
1601		return;
1602
1603	case TCPS_SYN_SENT:
1604		db_printf("TCPS_SYN_SENT");
1605		return;
1606
1607	case TCPS_SYN_RECEIVED:
1608		db_printf("TCPS_SYN_RECEIVED");
1609		return;
1610
1611	case TCPS_ESTABLISHED:
1612		db_printf("TCPS_ESTABLISHED");
1613		return;
1614
1615	case TCPS_CLOSE_WAIT:
1616		db_printf("TCPS_CLOSE_WAIT");
1617		return;
1618
1619	case TCPS_FIN_WAIT_1:
1620		db_printf("TCPS_FIN_WAIT_1");
1621		return;
1622
1623	case TCPS_CLOSING:
1624		db_printf("TCPS_CLOSING");
1625		return;
1626
1627	case TCPS_LAST_ACK:
1628		db_printf("TCPS_LAST_ACK");
1629		return;
1630
1631	case TCPS_FIN_WAIT_2:
1632		db_printf("TCPS_FIN_WAIT_2");
1633		return;
1634
1635	case TCPS_TIME_WAIT:
1636		db_printf("TCPS_TIME_WAIT");
1637		return;
1638
1639	default:
1640		db_printf("unknown");
1641		return;
1642	}
1643}
1644
1645static void
1646db_print_tflags(u_int t_flags)
1647{
1648	int comma;
1649
1650	comma = 0;
1651	if (t_flags & TF_ACKNOW) {
1652		db_printf("%sTF_ACKNOW", comma ? ", " : "");
1653		comma = 1;
1654	}
1655	if (t_flags & TF_DELACK) {
1656		db_printf("%sTF_DELACK", comma ? ", " : "");
1657		comma = 1;
1658	}
1659	if (t_flags & TF_NODELAY) {
1660		db_printf("%sTF_NODELAY", comma ? ", " : "");
1661		comma = 1;
1662	}
1663	if (t_flags & TF_NOOPT) {
1664		db_printf("%sTF_NOOPT", comma ? ", " : "");
1665		comma = 1;
1666	}
1667	if (t_flags & TF_SENTFIN) {
1668		db_printf("%sTF_SENTFIN", comma ? ", " : "");
1669		comma = 1;
1670	}
1671	if (t_flags & TF_REQ_SCALE) {
1672		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1673		comma = 1;
1674	}
1675	if (t_flags & TF_RCVD_SCALE) {
1676		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1677		comma = 1;
1678	}
1679	if (t_flags & TF_REQ_TSTMP) {
1680		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1681		comma = 1;
1682	}
1683	if (t_flags & TF_RCVD_TSTMP) {
1684		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1685		comma = 1;
1686	}
1687	if (t_flags & TF_SACK_PERMIT) {
1688		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1689		comma = 1;
1690	}
1691	if (t_flags & TF_NEEDSYN) {
1692		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1693		comma = 1;
1694	}
1695	if (t_flags & TF_NEEDFIN) {
1696		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1697		comma = 1;
1698	}
1699	if (t_flags & TF_NOPUSH) {
1700		db_printf("%sTF_NOPUSH", comma ? ", " : "");
1701		comma = 1;
1702	}
1703	if (t_flags & TF_NOPUSH) {
1704		db_printf("%sTF_NOPUSH", comma ? ", " : "");
1705		comma = 1;
1706	}
1707	if (t_flags & TF_MORETOCOME) {
1708		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1709		comma = 1;
1710	}
1711	if (t_flags & TF_LQ_OVERFLOW) {
1712		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1713		comma = 1;
1714	}
1715	if (t_flags & TF_LASTIDLE) {
1716		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1717		comma = 1;
1718	}
1719	if (t_flags & TF_RXWIN0SENT) {
1720		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1721		comma = 1;
1722	}
1723	if (t_flags & TF_FASTRECOVERY) {
1724		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1725		comma = 1;
1726	}
1727	if (t_flags & TF_WASFRECOVERY) {
1728		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1729		comma = 1;
1730	}
1731	if (t_flags & TF_SIGNATURE) {
1732		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1733		comma = 1;
1734	}
1735	if (t_flags & TF_FORCEDATA) {
1736		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1737		comma = 1;
1738	}
1739	if (t_flags & TF_TSO) {
1740		db_printf("%sTF_TSO", comma ? ", " : "");
1741		comma = 1;
1742	}
1743	if (t_flags & TF_ECN_PERMIT) {
1744		db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
1745		comma = 1;
1746	}
1747}
1748
1749static void
1750db_print_toobflags(char t_oobflags)
1751{
1752	int comma;
1753
1754	comma = 0;
1755	if (t_oobflags & TCPOOB_HAVEDATA) {
1756		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1757		comma = 1;
1758	}
1759	if (t_oobflags & TCPOOB_HADDATA) {
1760		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1761		comma = 1;
1762	}
1763}
1764
1765static void
1766db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1767{
1768
1769	db_print_indent(indent);
1770	db_printf("%s at %p\n", name, tp);
1771
1772	indent += 2;
1773
1774	db_print_indent(indent);
1775	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
1776	   LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
1777
1778	db_print_indent(indent);
1779	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
1780	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
1781
1782	db_print_indent(indent);
1783	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
1784	    &tp->t_timers->tt_delack, tp->t_inpcb);
1785
1786	db_print_indent(indent);
1787	db_printf("t_state: %d (", tp->t_state);
1788	db_print_tstate(tp->t_state);
1789	db_printf(")\n");
1790
1791	db_print_indent(indent);
1792	db_printf("t_flags: 0x%x (", tp->t_flags);
1793	db_print_tflags(tp->t_flags);
1794	db_printf(")\n");
1795
1796	db_print_indent(indent);
1797	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
1798	    tp->snd_una, tp->snd_max, tp->snd_nxt);
1799
1800	db_print_indent(indent);
1801	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
1802	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
1803
1804	db_print_indent(indent);
1805	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
1806	    tp->iss, tp->irs, tp->rcv_nxt);
1807
1808	db_print_indent(indent);
1809	db_printf("rcv_adv: 0x%08x   rcv_wnd: %lu   rcv_up: 0x%08x\n",
1810	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
1811
1812	db_print_indent(indent);
1813	db_printf("snd_wnd: %lu   snd_cwnd: %lu   snd_bwnd: %lu\n",
1814	   tp->snd_wnd, tp->snd_cwnd, tp->snd_bwnd);
1815
1816	db_print_indent(indent);
1817	db_printf("snd_ssthresh: %lu   snd_bandwidth: %lu   snd_recover: "
1818	    "0x%08x\n", tp->snd_ssthresh, tp->snd_bandwidth,
1819	    tp->snd_recover);
1820
1821	db_print_indent(indent);
1822	db_printf("t_maxopd: %u   t_rcvtime: %lu   t_startime: %lu\n",
1823	    tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
1824
1825	db_print_indent(indent);
1826	db_printf("t_rttime: %d   t_rtsq: 0x%08x   t_bw_rtttime: %d\n",
1827	    tp->t_rtttime, tp->t_rtseq, tp->t_bw_rtttime);
1828
1829	db_print_indent(indent);
1830	db_printf("t_bw_rtseq: 0x%08x   t_rxtcur: %d   t_maxseg: %u   "
1831	    "t_srtt: %d\n", tp->t_bw_rtseq, tp->t_rxtcur, tp->t_maxseg,
1832	    tp->t_srtt);
1833
1834	db_print_indent(indent);
1835	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
1836	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
1837	    tp->t_rttbest);
1838
1839	db_print_indent(indent);
1840	db_printf("t_rttupdated: %lu   max_sndwnd: %lu   t_softerror: %d\n",
1841	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
1842
1843	db_print_indent(indent);
1844	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
1845	db_print_toobflags(tp->t_oobflags);
1846	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
1847
1848	db_print_indent(indent);
1849	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
1850	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
1851
1852	db_print_indent(indent);
1853	db_printf("ts_recent: %u   ts_recent_age: %lu\n",
1854	    tp->ts_recent, tp->ts_recent_age);
1855
1856	db_print_indent(indent);
1857	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
1858	    "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
1859
1860	db_print_indent(indent);
1861	db_printf("snd_ssthresh_prev: %lu   snd_recover_prev: 0x%08x   "
1862	    "t_badrxtwin: %lu\n", tp->snd_ssthresh_prev,
1863	    tp->snd_recover_prev, tp->t_badrxtwin);
1864
1865	db_print_indent(indent);
1866	db_printf("snd_numholes: %d  snd_holes first: %p\n",
1867	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
1868
1869	db_print_indent(indent);
1870	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
1871	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
1872
1873	/* Skip sackblks, sackhint. */
1874
1875	db_print_indent(indent);
1876	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
1877	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
1878}
1879
1880DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
1881{
1882	struct tcpcb *tp;
1883
1884	if (!have_addr) {
1885		db_printf("usage: show tcpcb <addr>\n");
1886		return;
1887	}
1888	tp = (struct tcpcb *)addr;
1889
1890	db_print_tcpcb(tp, "tcpcb", 0);
1891}
1892#endif
1893