tcp_usrreq.c revision 157993
1/*-
2 * Copyright (c) 1982, 1986, 1988, 1993
3 *	The Regents of the University of California.
4 * Copyright (c) 2006 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 * $FreeBSD: head/sys/netinet/tcp_usrreq.c 157993 2006-04-24 08:20:02Z rwatson $
33 */
34
35#include "opt_inet.h"
36#include "opt_inet6.h"
37#include "opt_tcpdebug.h"
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/malloc.h>
42#include <sys/kernel.h>
43#include <sys/sysctl.h>
44#include <sys/mbuf.h>
45#ifdef INET6
46#include <sys/domain.h>
47#endif /* INET6 */
48#include <sys/socket.h>
49#include <sys/socketvar.h>
50#include <sys/protosw.h>
51#include <sys/proc.h>
52#include <sys/jail.h>
53
54#include <net/if.h>
55#include <net/route.h>
56
57#include <netinet/in.h>
58#include <netinet/in_systm.h>
59#ifdef INET6
60#include <netinet/ip6.h>
61#endif
62#include <netinet/in_pcb.h>
63#ifdef INET6
64#include <netinet6/in6_pcb.h>
65#endif
66#include <netinet/in_var.h>
67#include <netinet/ip_var.h>
68#ifdef INET6
69#include <netinet6/ip6_var.h>
70#include <netinet6/scope6_var.h>
71#endif
72#include <netinet/tcp.h>
73#include <netinet/tcp_fsm.h>
74#include <netinet/tcp_seq.h>
75#include <netinet/tcp_timer.h>
76#include <netinet/tcp_var.h>
77#include <netinet/tcpip.h>
78#ifdef TCPDEBUG
79#include <netinet/tcp_debug.h>
80#endif
81
82/*
83 * TCP protocol interface to socket abstraction.
84 */
85extern	char *tcpstates[];	/* XXX ??? */
86
87static int	tcp_attach(struct socket *);
88static int	tcp_connect(struct tcpcb *, struct sockaddr *,
89		    struct thread *td);
90#ifdef INET6
91static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
92		    struct thread *td);
93#endif /* INET6 */
94static void	tcp_disconnect(struct tcpcb *);
95static void	tcp_usrclosed(struct tcpcb *);
96static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
97
98#ifdef TCPDEBUG
99#define	TCPDEBUG0	int ostate = 0
100#define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
101#define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
102				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
103#else
104#define	TCPDEBUG0
105#define	TCPDEBUG1()
106#define	TCPDEBUG2(req)
107#endif
108
109/*
110 * TCP attaches to socket via pru_attach(), reserving space,
111 * and an internet control block.
112 */
113static int
114tcp_usr_attach(struct socket *so, int proto, struct thread *td)
115{
116	struct inpcb *inp;
117	struct tcpcb *tp = NULL;
118	int error;
119	TCPDEBUG0;
120
121	inp = sotoinpcb(so);
122	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
123	INP_INFO_WLOCK(&tcbinfo);
124	TCPDEBUG1();
125
126	error = tcp_attach(so);
127	if (error)
128		goto out;
129
130	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
131		so->so_linger = TCP_LINGERTIME;
132
133	inp = sotoinpcb(so);
134	tp = intotcpcb(inp);
135out:
136	TCPDEBUG2(PRU_ATTACH);
137	INP_INFO_WUNLOCK(&tcbinfo);
138	return error;
139}
140
141/*
142 * tcp_detach() releases any protocol state that can be reasonably released
143 * when a socket shutdown is requested, and is a shared code path for
144 * tcp_usr_detach() and tcp_usr_abort(), the two socket close entry points.
145 *
146 * Accepts pcbinfo, inpcb locked, will unlock the inpcb (if needed) on
147 * return.
148 */
149static void
150tcp_detach(struct socket *so, struct inpcb *inp)
151{
152	struct tcpcb *tp;
153#ifdef INET6
154	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
155#endif
156
157	INP_INFO_WLOCK_ASSERT(&tcbinfo);
158	INP_LOCK_ASSERT(inp);
159
160	KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
161	KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
162
163	if (inp->inp_vflag & INP_TIMEWAIT) {
164		if (inp->inp_vflag & INP_DROPPED) {
165			/*
166			 * Connection was in time wait and has been dropped;
167			 * the calling path is either via tcp_twclose(), or
168			 * as a result of an eventual soclose() after
169			 * tcp_twclose() has been called.  In either case,
170			 * tcp_twclose() has detached the tcptw from the
171			 * inpcb, so we just detach and free the inpcb.
172			 *
173			 * XXXRW: Would it be cleaner to free the tcptw
174			 * here?
175			 */
176#ifdef INET6
177			if (isipv6) {
178				in6_pcbdetach(inp);
179				in6_pcbfree(inp);
180			} else {
181#endif
182				in_pcbdetach(inp);
183				in_pcbfree(inp);
184#ifdef INET6
185			}
186#endif
187		} else {
188			/*
189			 * Connection is in time wait and has not yet been
190			 * dropped; allow the socket to be discarded, but
191			 * need to keep inpcb until end of time wait.
192			 */
193#ifdef INET6
194			if (isipv6)
195				in6_pcbdetach(inp);
196			else
197#endif
198				in_pcbdetach(inp);
199			INP_UNLOCK(inp);
200		}
201	} else {
202		/*
203		 * If not in timewait, there are two possible paths.  First,
204		 * the TCP connection is either embryonic or done, in which
205		 * case we tear down all state.  Second, it may still be
206		 * active, in which case we acquire a reference to the socket
207		 * and will free it later when TCP is done.
208		 */
209		tp = intotcpcb(inp);
210		if (inp->inp_vflag & INP_DROPPED ||
211		    tp->t_state < TCPS_SYN_SENT) {
212			tcp_discardcb(tp);
213#ifdef INET6
214			if (isipv6) {
215				in_pcbdetach(inp);
216				in_pcbfree(inp);
217			} else {
218#endif
219				in_pcbdetach(inp);
220				in_pcbfree(inp);
221#ifdef INET6
222			}
223#endif
224		} else {
225			SOCK_LOCK(so);
226			so->so_state |= SS_PROTOREF;
227			SOCK_UNLOCK(so);
228			inp->inp_vflag |= INP_SOCKREF;
229			INP_UNLOCK(inp);
230		}
231	}
232}
233
234/*
235 * pru_detach() detaches the TCP protocol from the socket.
236 * If the protocol state is non-embryonic, then can't
237 * do this directly: have to initiate a pru_disconnect(),
238 * which may finish later; embryonic TCB's can just
239 * be discarded here.
240 */
241static void
242tcp_usr_detach(struct socket *so)
243{
244	struct inpcb *inp;
245	struct tcpcb *tp;
246	TCPDEBUG0;
247
248	inp = sotoinpcb(so);
249	KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
250	INP_INFO_WLOCK(&tcbinfo);
251	INP_LOCK(inp);
252	KASSERT(inp->inp_socket != NULL,
253	    ("tcp_usr_detach: inp_socket == NULL"));
254	TCPDEBUG1();
255
256	/*
257	 * First, if we still have full TCP state, and we're not dropped,
258	 * initiate a disconnect.
259	 */
260	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
261	    !(inp->inp_vflag & INP_DROPPED)) {
262		tp = intotcpcb(inp);
263		tcp_disconnect(tp);
264	}
265	tcp_detach(so, inp);
266	tp = NULL;
267	TCPDEBUG2(PRU_DETACH);
268	INP_INFO_WUNLOCK(&tcbinfo);
269}
270
271/*
272 * Give the socket an address.
273 */
274static int
275tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
276{
277	int error = 0;
278	struct inpcb *inp;
279	struct tcpcb *tp = NULL;
280	struct sockaddr_in *sinp;
281
282	sinp = (struct sockaddr_in *)nam;
283	if (nam->sa_len != sizeof (*sinp))
284		return (EINVAL);
285	/*
286	 * Must check for multicast addresses and disallow binding
287	 * to them.
288	 */
289	if (sinp->sin_family == AF_INET &&
290	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
291		return (EAFNOSUPPORT);
292
293	TCPDEBUG0;
294	INP_INFO_WLOCK(&tcbinfo);
295	inp = sotoinpcb(so);
296	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
297	INP_LOCK(inp);
298	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
299		error = EINVAL;
300		goto out;
301	}
302	tp = intotcpcb(inp);
303	TCPDEBUG1();
304	error = in_pcbbind(inp, nam, td->td_ucred);
305out:
306	TCPDEBUG2(PRU_BIND);
307	INP_UNLOCK(inp);
308	INP_INFO_WUNLOCK(&tcbinfo);
309
310	return (error);
311}
312
313#ifdef INET6
314static int
315tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
316{
317	int error = 0;
318	struct inpcb *inp;
319	struct tcpcb *tp = NULL;
320	struct sockaddr_in6 *sin6p;
321
322	sin6p = (struct sockaddr_in6 *)nam;
323	if (nam->sa_len != sizeof (*sin6p))
324		return (EINVAL);
325	/*
326	 * Must check for multicast addresses and disallow binding
327	 * to them.
328	 */
329	if (sin6p->sin6_family == AF_INET6 &&
330	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
331		return (EAFNOSUPPORT);
332
333	TCPDEBUG0;
334	INP_INFO_WLOCK(&tcbinfo);
335	inp = sotoinpcb(so);
336	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
337	INP_LOCK(inp);
338	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
339		error = EINVAL;
340		goto out;
341	}
342	tp = intotcpcb(inp);
343	TCPDEBUG1();
344	inp->inp_vflag &= ~INP_IPV4;
345	inp->inp_vflag |= INP_IPV6;
346	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
347		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
348			inp->inp_vflag |= INP_IPV4;
349		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
350			struct sockaddr_in sin;
351
352			in6_sin6_2_sin(&sin, sin6p);
353			inp->inp_vflag |= INP_IPV4;
354			inp->inp_vflag &= ~INP_IPV6;
355			error = in_pcbbind(inp, (struct sockaddr *)&sin,
356			    td->td_ucred);
357			goto out;
358		}
359	}
360	error = in6_pcbbind(inp, nam, td->td_ucred);
361out:
362	TCPDEBUG2(PRU_BIND);
363	INP_UNLOCK(inp);
364	INP_INFO_WUNLOCK(&tcbinfo);
365	return (error);
366}
367#endif /* INET6 */
368
369/*
370 * Prepare to accept connections.
371 */
372static int
373tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
374{
375	int error = 0;
376	struct inpcb *inp;
377	struct tcpcb *tp = NULL;
378
379	TCPDEBUG0;
380	INP_INFO_WLOCK(&tcbinfo);
381	inp = sotoinpcb(so);
382	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
383	INP_LOCK(inp);
384	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
385		error = EINVAL;
386		goto out;
387	}
388	tp = intotcpcb(inp);
389	TCPDEBUG1();
390	SOCK_LOCK(so);
391	error = solisten_proto_check(so);
392	if (error == 0 && inp->inp_lport == 0)
393		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
394	if (error == 0) {
395		tp->t_state = TCPS_LISTEN;
396		solisten_proto(so, backlog);
397	}
398	SOCK_UNLOCK(so);
399
400out:
401	TCPDEBUG2(PRU_LISTEN);
402	INP_UNLOCK(inp);
403	INP_INFO_WUNLOCK(&tcbinfo);
404	return (error);
405}
406
407#ifdef INET6
408static int
409tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
410{
411	int error = 0;
412	struct inpcb *inp;
413	struct tcpcb *tp = NULL;
414
415	TCPDEBUG0;
416	INP_INFO_WLOCK(&tcbinfo);
417	inp = sotoinpcb(so);
418	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
419	INP_LOCK(inp);
420	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
421		error = EINVAL;
422		goto out;
423	}
424	tp = intotcpcb(inp);
425	TCPDEBUG1();
426	SOCK_LOCK(so);
427	error = solisten_proto_check(so);
428	if (error == 0 && inp->inp_lport == 0) {
429		inp->inp_vflag &= ~INP_IPV4;
430		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
431			inp->inp_vflag |= INP_IPV4;
432		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
433	}
434	if (error == 0) {
435		tp->t_state = TCPS_LISTEN;
436		solisten_proto(so, backlog);
437	}
438	SOCK_UNLOCK(so);
439
440out:
441	TCPDEBUG2(PRU_LISTEN);
442	INP_UNLOCK(inp);
443	INP_INFO_WUNLOCK(&tcbinfo);
444	return (error);
445}
446#endif /* INET6 */
447
448/*
449 * Initiate connection to peer.
450 * Create a template for use in transmissions on this connection.
451 * Enter SYN_SENT state, and mark socket as connecting.
452 * Start keep-alive timer, and seed output sequence space.
453 * Send initial segment on connection.
454 */
455static int
456tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
457{
458	int error = 0;
459	struct inpcb *inp;
460	struct tcpcb *tp = NULL;
461	struct sockaddr_in *sinp;
462
463	sinp = (struct sockaddr_in *)nam;
464	if (nam->sa_len != sizeof (*sinp))
465		return (EINVAL);
466	/*
467	 * Must disallow TCP ``connections'' to multicast addresses.
468	 */
469	if (sinp->sin_family == AF_INET
470	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
471		return (EAFNOSUPPORT);
472	if (jailed(td->td_ucred))
473		prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
474
475	TCPDEBUG0;
476	INP_INFO_WLOCK(&tcbinfo);
477	inp = sotoinpcb(so);
478	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
479	INP_LOCK(inp);
480	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
481		error = EINVAL;
482		goto out;
483	}
484	tp = intotcpcb(inp);
485	TCPDEBUG1();
486	if ((error = tcp_connect(tp, nam, td)) != 0)
487		goto out;
488	error = tcp_output(tp);
489out:
490	TCPDEBUG2(PRU_CONNECT);
491	INP_UNLOCK(inp);
492	INP_INFO_WUNLOCK(&tcbinfo);
493	return (error);
494}
495
496#ifdef INET6
497static int
498tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
499{
500	int error = 0;
501	struct inpcb *inp;
502	struct tcpcb *tp = NULL;
503	struct sockaddr_in6 *sin6p;
504
505	TCPDEBUG0;
506
507	sin6p = (struct sockaddr_in6 *)nam;
508	if (nam->sa_len != sizeof (*sin6p))
509		return (EINVAL);
510	/*
511	 * Must disallow TCP ``connections'' to multicast addresses.
512	 */
513	if (sin6p->sin6_family == AF_INET6
514	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
515		return (EAFNOSUPPORT);
516
517	INP_INFO_WLOCK(&tcbinfo);
518	inp = sotoinpcb(so);
519	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
520	INP_LOCK(inp);
521	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
522		error = EINVAL;
523		goto out;
524	}
525	tp = intotcpcb(inp);
526	TCPDEBUG1();
527	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
528		struct sockaddr_in sin;
529
530		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
531			error = EINVAL;
532			goto out;
533		}
534
535		in6_sin6_2_sin(&sin, sin6p);
536		inp->inp_vflag |= INP_IPV4;
537		inp->inp_vflag &= ~INP_IPV6;
538		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
539			goto out;
540		error = tcp_output(tp);
541		goto out;
542	}
543	inp->inp_vflag &= ~INP_IPV4;
544	inp->inp_vflag |= INP_IPV6;
545	inp->inp_inc.inc_isipv6 = 1;
546	if ((error = tcp6_connect(tp, nam, td)) != 0)
547		goto out;
548	error = tcp_output(tp);
549
550out:
551	TCPDEBUG2(PRU_CONNECT);
552	INP_UNLOCK(inp);
553	INP_INFO_WUNLOCK(&tcbinfo);
554	return (error);
555}
556#endif /* INET6 */
557
558/*
559 * Initiate disconnect from peer.
560 * If connection never passed embryonic stage, just drop;
561 * else if don't need to let data drain, then can just drop anyways,
562 * else have to begin TCP shutdown process: mark socket disconnecting,
563 * drain unread data, state switch to reflect user close, and
564 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
565 * when peer sends FIN and acks ours.
566 *
567 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
568 */
569static int
570tcp_usr_disconnect(struct socket *so)
571{
572	struct inpcb *inp;
573	struct tcpcb *tp = NULL;
574	int error = 0;
575
576	TCPDEBUG0;
577	INP_INFO_WLOCK(&tcbinfo);
578	inp = sotoinpcb(so);
579	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
580	INP_LOCK(inp);
581	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
582		error = EINVAL;
583		goto out;
584	}
585	tp = intotcpcb(inp);
586	TCPDEBUG1();
587	tcp_disconnect(tp);
588out:
589	TCPDEBUG2(PRU_DISCONNECT);
590	INP_UNLOCK(inp);
591	INP_INFO_WUNLOCK(&tcbinfo);
592	return (error);
593}
594
595/*
596 * Accept a connection.  Essentially all the work is
597 * done at higher levels; just return the address
598 * of the peer, storing through addr.
599 */
600static int
601tcp_usr_accept(struct socket *so, struct sockaddr **nam)
602{
603	int error = 0;
604	struct inpcb *inp = NULL;
605	struct tcpcb *tp = NULL;
606	struct in_addr addr;
607	in_port_t port = 0;
608	TCPDEBUG0;
609
610	if (so->so_state & SS_ISDISCONNECTED)
611		return (ECONNABORTED);
612
613	inp = sotoinpcb(so);
614	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
615	INP_LOCK(inp);
616	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
617		error = ECONNABORTED;
618		goto out;
619	}
620	tp = intotcpcb(inp);
621	TCPDEBUG1();
622
623	/*
624	 * We inline in_setpeeraddr and COMMON_END here, so that we can
625	 * copy the data of interest and defer the malloc until after we
626	 * release the lock.
627	 */
628	port = inp->inp_fport;
629	addr = inp->inp_faddr;
630
631out:
632	TCPDEBUG2(PRU_ACCEPT);
633	INP_UNLOCK(inp);
634	if (error == 0)
635		*nam = in_sockaddr(port, &addr);
636	return error;
637}
638
639#ifdef INET6
640static int
641tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
642{
643	struct inpcb *inp = NULL;
644	int error = 0;
645	struct tcpcb *tp = NULL;
646	struct in_addr addr;
647	struct in6_addr addr6;
648	in_port_t port = 0;
649	int v4 = 0;
650	TCPDEBUG0;
651
652	if (so->so_state & SS_ISDISCONNECTED) {
653		error = ECONNABORTED;
654		goto out;
655	}
656
657	inp = sotoinpcb(so);
658	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
659	INP_LOCK(inp);
660	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
661		error = EINVAL;
662		goto out;
663	}
664	tp = intotcpcb(inp);
665	TCPDEBUG1();
666
667	/*
668	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
669	 * copy the data of interest and defer the malloc until after we
670	 * release the lock.
671	 */
672	if (inp->inp_vflag & INP_IPV4) {
673		v4 = 1;
674		port = inp->inp_fport;
675		addr = inp->inp_faddr;
676	} else {
677		port = inp->inp_fport;
678		addr6 = inp->in6p_faddr;
679	}
680
681out:
682	TCPDEBUG2(PRU_ACCEPT);
683	INP_UNLOCK(inp);
684	if (error == 0) {
685		if (v4)
686			*nam = in6_v4mapsin6_sockaddr(port, &addr);
687		else
688			*nam = in6_sockaddr(port, &addr6);
689	}
690	return error;
691}
692#endif /* INET6 */
693
694/*
695 * This is the wrapper function for in_setsockaddr. We just pass down
696 * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
697 * here because in_setsockaddr will call malloc and can block.
698 */
699static int
700tcp_sockaddr(struct socket *so, struct sockaddr **nam)
701{
702	return (in_setsockaddr(so, nam, &tcbinfo));
703}
704
705/*
706 * This is the wrapper function for in_setpeeraddr. We just pass down
707 * the pcbinfo for in_setpeeraddr to lock.
708 */
709static int
710tcp_peeraddr(struct socket *so, struct sockaddr **nam)
711{
712	return (in_setpeeraddr(so, nam, &tcbinfo));
713}
714
715/*
716 * Mark the connection as being incapable of further output.
717 */
718static int
719tcp_usr_shutdown(struct socket *so)
720{
721	int error = 0;
722	struct inpcb *inp;
723	struct tcpcb *tp = NULL;
724
725	TCPDEBUG0;
726	INP_INFO_WLOCK(&tcbinfo);
727	inp = sotoinpcb(so);
728	KASSERT(inp != NULL, ("inp == NULL"));
729	INP_LOCK(inp);
730	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
731		error = EINVAL;
732		goto out;
733	}
734	tp = intotcpcb(inp);
735	TCPDEBUG1();
736	socantsendmore(so);
737	tcp_usrclosed(tp);
738	error = tcp_output(tp);
739
740out:
741	TCPDEBUG2(PRU_SHUTDOWN);
742	INP_UNLOCK(inp);
743	INP_INFO_WUNLOCK(&tcbinfo);
744
745	return (error);
746}
747
748/*
749 * After a receive, possibly send window update to peer.
750 */
751static int
752tcp_usr_rcvd(struct socket *so, int flags)
753{
754	struct inpcb *inp;
755	struct tcpcb *tp = NULL;
756	int error = 0;
757
758	TCPDEBUG0;
759	inp = sotoinpcb(so);
760	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
761	INP_LOCK(inp);
762	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
763		error = EINVAL;
764		goto out;
765	}
766	tp = intotcpcb(inp);
767	TCPDEBUG1();
768	tcp_output(tp);
769
770out:
771	TCPDEBUG2(PRU_RCVD);
772	INP_UNLOCK(inp);
773	return (error);
774}
775
776/*
777 * Do a send by putting data in output queue and updating urgent
778 * marker if URG set.  Possibly send more data.  Unlike the other
779 * pru_*() routines, the mbuf chains are our responsibility.  We
780 * must either enqueue them or free them.  The other pru_* routines
781 * generally are caller-frees.
782 */
783static int
784tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
785	     struct sockaddr *nam, struct mbuf *control, struct thread *td)
786{
787	int error = 0;
788	struct inpcb *inp;
789	struct tcpcb *tp = NULL;
790	int headlocked = 0;
791#ifdef INET6
792	int isipv6;
793#endif
794	TCPDEBUG0;
795
796	/*
797	 * We require the pcbinfo lock in two cases:
798	 *
799	 * (1) An implied connect is taking place, which can result in
800	 *     binding IPs and ports and hence modification of the pcb hash
801	 *     chains.
802	 *
803	 * (2) PRUS_EOF is set, resulting in explicit close on the send.
804	 */
805	if ((nam != NULL) || (flags & PRUS_EOF)) {
806		INP_INFO_WLOCK(&tcbinfo);
807		headlocked = 1;
808	}
809	inp = sotoinpcb(so);
810	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
811	INP_LOCK(inp);
812	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
813		error = EINVAL;
814		goto out;
815	}
816#ifdef INET6
817	isipv6 = nam && nam->sa_family == AF_INET6;
818#endif /* INET6 */
819	tp = intotcpcb(inp);
820	TCPDEBUG1();
821	if (control) {
822		/* TCP doesn't do control messages (rights, creds, etc) */
823		if (control->m_len) {
824			m_freem(control);
825			if (m)
826				m_freem(m);
827			error = EINVAL;
828			goto out;
829		}
830		m_freem(control);	/* empty control, just free it */
831	}
832	if (!(flags & PRUS_OOB)) {
833		sbappendstream(&so->so_snd, m);
834		if (nam && tp->t_state < TCPS_SYN_SENT) {
835			/*
836			 * Do implied connect if not yet connected,
837			 * initialize window to default value, and
838			 * initialize maxseg/maxopd using peer's cached
839			 * MSS.
840			 */
841			INP_INFO_WLOCK_ASSERT(&tcbinfo);
842#ifdef INET6
843			if (isipv6)
844				error = tcp6_connect(tp, nam, td);
845			else
846#endif /* INET6 */
847			error = tcp_connect(tp, nam, td);
848			if (error)
849				goto out;
850			tp->snd_wnd = TTCP_CLIENT_SND_WND;
851			tcp_mss(tp, -1);
852		}
853		if (flags & PRUS_EOF) {
854			/*
855			 * Close the send side of the connection after
856			 * the data is sent.
857			 */
858			INP_INFO_WLOCK_ASSERT(&tcbinfo);
859			socantsendmore(so);
860			tcp_usrclosed(tp);
861		}
862		if (headlocked) {
863			INP_INFO_WUNLOCK(&tcbinfo);
864			headlocked = 0;
865		}
866		if (tp != NULL) {
867			if (flags & PRUS_MORETOCOME)
868				tp->t_flags |= TF_MORETOCOME;
869			error = tcp_output(tp);
870			if (flags & PRUS_MORETOCOME)
871				tp->t_flags &= ~TF_MORETOCOME;
872		}
873	} else {
874		/*
875		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
876		 */
877		SOCKBUF_LOCK(&so->so_snd);
878		if (sbspace(&so->so_snd) < -512) {
879			SOCKBUF_UNLOCK(&so->so_snd);
880			m_freem(m);
881			error = ENOBUFS;
882			goto out;
883		}
884		/*
885		 * According to RFC961 (Assigned Protocols),
886		 * the urgent pointer points to the last octet
887		 * of urgent data.  We continue, however,
888		 * to consider it to indicate the first octet
889		 * of data past the urgent section.
890		 * Otherwise, snd_up should be one lower.
891		 */
892		sbappendstream_locked(&so->so_snd, m);
893		SOCKBUF_UNLOCK(&so->so_snd);
894		if (nam && tp->t_state < TCPS_SYN_SENT) {
895			/*
896			 * Do implied connect if not yet connected,
897			 * initialize window to default value, and
898			 * initialize maxseg/maxopd using peer's cached
899			 * MSS.
900			 */
901			INP_INFO_WLOCK_ASSERT(&tcbinfo);
902#ifdef INET6
903			if (isipv6)
904				error = tcp6_connect(tp, nam, td);
905			else
906#endif /* INET6 */
907			error = tcp_connect(tp, nam, td);
908			if (error)
909				goto out;
910			tp->snd_wnd = TTCP_CLIENT_SND_WND;
911			tcp_mss(tp, -1);
912			INP_INFO_WUNLOCK(&tcbinfo);
913			headlocked = 0;
914		} else if (nam) {
915			INP_INFO_WUNLOCK(&tcbinfo);
916			headlocked = 0;
917		}
918		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
919		tp->t_flags |= TF_FORCEDATA;
920		error = tcp_output(tp);
921		tp->t_flags &= ~TF_FORCEDATA;
922	}
923out:
924	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
925		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
926	INP_UNLOCK(inp);
927	if (headlocked)
928		INP_INFO_WUNLOCK(&tcbinfo);
929	return (error);
930}
931
932/*
933 * Abort the TCP.
934 *
935 * First, drop the connection.  Then collect state if possible.
936 */
937static void
938tcp_usr_abort(struct socket *so)
939{
940	struct inpcb *inp;
941	struct tcpcb *tp;
942	TCPDEBUG0;
943
944	inp = sotoinpcb(so);
945	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
946
947	INP_INFO_WLOCK(&tcbinfo);
948	INP_LOCK(inp);
949	KASSERT(inp->inp_socket != NULL,
950	    ("tcp_usr_abort: inp_socket == NULL"));
951	TCPDEBUG1();
952
953	/*
954	 * First, if we still have full TCP state, and we're not dropped,
955	 * drop.
956	 */
957	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
958	    !(inp->inp_vflag & INP_DROPPED)) {
959		tp = intotcpcb(inp);
960		tcp_drop(tp, ECONNABORTED);
961	}
962	tcp_detach(so, inp);
963	tp = NULL;
964	TCPDEBUG2(PRU_DETACH);
965	INP_INFO_WUNLOCK(&tcbinfo);
966}
967
968/*
969 * Receive out-of-band data.
970 */
971static int
972tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
973{
974	int error = 0;
975	struct inpcb *inp;
976	struct tcpcb *tp = NULL;
977
978	TCPDEBUG0;
979	inp = sotoinpcb(so);
980	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
981	INP_LOCK(inp);
982	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
983		error = EINVAL;
984		goto out;
985	}
986	tp = intotcpcb(inp);
987	TCPDEBUG1();
988	if ((so->so_oobmark == 0 &&
989	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
990	    so->so_options & SO_OOBINLINE ||
991	    tp->t_oobflags & TCPOOB_HADDATA) {
992		error = EINVAL;
993		goto out;
994	}
995	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
996		error = EWOULDBLOCK;
997		goto out;
998	}
999	m->m_len = 1;
1000	*mtod(m, caddr_t) = tp->t_iobc;
1001	if ((flags & MSG_PEEK) == 0)
1002		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1003
1004out:
1005	TCPDEBUG2(PRU_RCVOOB);
1006	INP_UNLOCK(inp);
1007	return (error);
1008}
1009
1010struct pr_usrreqs tcp_usrreqs = {
1011	.pru_abort =		tcp_usr_abort,
1012	.pru_accept =		tcp_usr_accept,
1013	.pru_attach =		tcp_usr_attach,
1014	.pru_bind =		tcp_usr_bind,
1015	.pru_connect =		tcp_usr_connect,
1016	.pru_control =		in_control,
1017	.pru_detach =		tcp_usr_detach,
1018	.pru_disconnect =	tcp_usr_disconnect,
1019	.pru_listen =		tcp_usr_listen,
1020	.pru_peeraddr =		tcp_peeraddr,
1021	.pru_rcvd =		tcp_usr_rcvd,
1022	.pru_rcvoob =		tcp_usr_rcvoob,
1023	.pru_send =		tcp_usr_send,
1024	.pru_shutdown =		tcp_usr_shutdown,
1025	.pru_sockaddr =		tcp_sockaddr,
1026	.pru_sosetlabel =	in_pcbsosetlabel
1027};
1028
1029#ifdef INET6
1030struct pr_usrreqs tcp6_usrreqs = {
1031	.pru_abort =		tcp_usr_abort,
1032	.pru_accept =		tcp6_usr_accept,
1033	.pru_attach =		tcp_usr_attach,
1034	.pru_bind =		tcp6_usr_bind,
1035	.pru_connect =		tcp6_usr_connect,
1036	.pru_control =		in6_control,
1037	.pru_detach =		tcp_usr_detach,
1038	.pru_disconnect =	tcp_usr_disconnect,
1039	.pru_listen =		tcp6_usr_listen,
1040	.pru_peeraddr =		in6_mapped_peeraddr,
1041	.pru_rcvd =		tcp_usr_rcvd,
1042	.pru_rcvoob =		tcp_usr_rcvoob,
1043	.pru_send =		tcp_usr_send,
1044	.pru_shutdown =		tcp_usr_shutdown,
1045	.pru_sockaddr =		in6_mapped_sockaddr,
1046 	.pru_sosetlabel =	in_pcbsosetlabel
1047};
1048#endif /* INET6 */
1049
1050/*
1051 * Common subroutine to open a TCP connection to remote host specified
1052 * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
1053 * port number if needed.  Call in_pcbconnect_setup to do the routing and
1054 * to choose a local host address (interface).  If there is an existing
1055 * incarnation of the same connection in TIME-WAIT state and if the remote
1056 * host was sending CC options and if the connection duration was < MSL, then
1057 * truncate the previous TIME-WAIT state and proceed.
1058 * Initialize connection parameters and enter SYN-SENT state.
1059 */
1060static int
1061tcp_connect(tp, nam, td)
1062	register struct tcpcb *tp;
1063	struct sockaddr *nam;
1064	struct thread *td;
1065{
1066	struct inpcb *inp = tp->t_inpcb, *oinp;
1067	struct socket *so = inp->inp_socket;
1068	struct in_addr laddr;
1069	u_short lport;
1070	int error;
1071
1072	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1073	INP_LOCK_ASSERT(inp);
1074
1075	if (inp->inp_lport == 0) {
1076		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1077		if (error)
1078			return error;
1079	}
1080
1081	/*
1082	 * Cannot simply call in_pcbconnect, because there might be an
1083	 * earlier incarnation of this same connection still in
1084	 * TIME_WAIT state, creating an ADDRINUSE error.
1085	 */
1086	laddr = inp->inp_laddr;
1087	lport = inp->inp_lport;
1088	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1089	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1090	if (error && oinp == NULL)
1091		return error;
1092	if (oinp)
1093		return EADDRINUSE;
1094	inp->inp_laddr = laddr;
1095	in_pcbrehash(inp);
1096
1097	/* Compute window scaling to request.  */
1098	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1099	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1100		tp->request_r_scale++;
1101
1102	soisconnecting(so);
1103	tcpstat.tcps_connattempt++;
1104	tp->t_state = TCPS_SYN_SENT;
1105	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
1106	tp->iss = tcp_new_isn(tp);
1107	tp->t_bw_rtseq = tp->iss;
1108	tcp_sendseqinit(tp);
1109
1110	return 0;
1111}
1112
1113#ifdef INET6
1114static int
1115tcp6_connect(tp, nam, td)
1116	register struct tcpcb *tp;
1117	struct sockaddr *nam;
1118	struct thread *td;
1119{
1120	struct inpcb *inp = tp->t_inpcb, *oinp;
1121	struct socket *so = inp->inp_socket;
1122	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1123	struct in6_addr *addr6;
1124	int error;
1125
1126	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1127	INP_LOCK_ASSERT(inp);
1128
1129	if (inp->inp_lport == 0) {
1130		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1131		if (error)
1132			return error;
1133	}
1134
1135	/*
1136	 * Cannot simply call in_pcbconnect, because there might be an
1137	 * earlier incarnation of this same connection still in
1138	 * TIME_WAIT state, creating an ADDRINUSE error.
1139	 * in6_pcbladdr() also handles scope zone IDs.
1140	 */
1141	error = in6_pcbladdr(inp, nam, &addr6);
1142	if (error)
1143		return error;
1144	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1145				  &sin6->sin6_addr, sin6->sin6_port,
1146				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1147				  ? addr6
1148				  : &inp->in6p_laddr,
1149				  inp->inp_lport,  0, NULL);
1150	if (oinp)
1151		return EADDRINUSE;
1152	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1153		inp->in6p_laddr = *addr6;
1154	inp->in6p_faddr = sin6->sin6_addr;
1155	inp->inp_fport = sin6->sin6_port;
1156	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
1157	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
1158	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
1159		inp->in6p_flowinfo |=
1160		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1161	in_pcbrehash(inp);
1162
1163	/* Compute window scaling to request.  */
1164	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1165	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1166		tp->request_r_scale++;
1167
1168	soisconnecting(so);
1169	tcpstat.tcps_connattempt++;
1170	tp->t_state = TCPS_SYN_SENT;
1171	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
1172	tp->iss = tcp_new_isn(tp);
1173	tp->t_bw_rtseq = tp->iss;
1174	tcp_sendseqinit(tp);
1175
1176	return 0;
1177}
1178#endif /* INET6 */
1179
1180/*
1181 * Export TCP internal state information via a struct tcp_info, based on the
1182 * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1183 * (TCP state machine, etc).  We export all information using FreeBSD-native
1184 * constants -- for example, the numeric values for tcpi_state will differ
1185 * from Linux.
1186 */
1187static void
1188tcp_fill_info(tp, ti)
1189	struct tcpcb *tp;
1190	struct tcp_info *ti;
1191{
1192
1193	INP_LOCK_ASSERT(tp->t_inpcb);
1194	bzero(ti, sizeof(*ti));
1195
1196	ti->tcpi_state = tp->t_state;
1197	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1198		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1199	if (tp->sack_enable)
1200		ti->tcpi_options |= TCPI_OPT_SACK;
1201	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1202		ti->tcpi_options |= TCPI_OPT_WSCALE;
1203		ti->tcpi_snd_wscale = tp->snd_scale;
1204		ti->tcpi_rcv_wscale = tp->rcv_scale;
1205	}
1206	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1207	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1208
1209	/*
1210	 * FreeBSD-specific extension fields for tcp_info.
1211	 */
1212	ti->tcpi_rcv_space = tp->rcv_wnd;
1213	ti->tcpi_snd_wnd = tp->snd_wnd;
1214	ti->tcpi_snd_bwnd = tp->snd_bwnd;
1215}
1216
1217/*
1218 * The new sockopt interface makes it possible for us to block in the
1219 * copyin/out step (if we take a page fault).  Taking a page fault at
1220 * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
1221 * use TSM, there probably isn't any need for this function to run at
1222 * splnet() any more.  This needs more examination.)
1223 *
1224 * XXXRW: The locking here is wrong; we may take a page fault while holding
1225 * the inpcb lock.
1226 */
1227int
1228tcp_ctloutput(so, sopt)
1229	struct socket *so;
1230	struct sockopt *sopt;
1231{
1232	int	error, opt, optval;
1233	struct	inpcb *inp;
1234	struct	tcpcb *tp;
1235	struct	tcp_info ti;
1236
1237	error = 0;
1238	inp = sotoinpcb(so);
1239	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1240	INP_LOCK(inp);
1241	if (sopt->sopt_level != IPPROTO_TCP) {
1242		INP_UNLOCK(inp);
1243#ifdef INET6
1244		if (INP_CHECK_SOCKAF(so, AF_INET6))
1245			error = ip6_ctloutput(so, sopt);
1246		else
1247#endif /* INET6 */
1248		error = ip_ctloutput(so, sopt);
1249		return (error);
1250	}
1251	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
1252		error = ECONNRESET;
1253		goto out;
1254	}
1255	tp = intotcpcb(inp);
1256
1257	switch (sopt->sopt_dir) {
1258	case SOPT_SET:
1259		switch (sopt->sopt_name) {
1260#ifdef TCP_SIGNATURE
1261		case TCP_MD5SIG:
1262			error = sooptcopyin(sopt, &optval, sizeof optval,
1263					    sizeof optval);
1264			if (error)
1265				break;
1266
1267			if (optval > 0)
1268				tp->t_flags |= TF_SIGNATURE;
1269			else
1270				tp->t_flags &= ~TF_SIGNATURE;
1271			break;
1272#endif /* TCP_SIGNATURE */
1273		case TCP_NODELAY:
1274		case TCP_NOOPT:
1275			error = sooptcopyin(sopt, &optval, sizeof optval,
1276					    sizeof optval);
1277			if (error)
1278				break;
1279
1280			switch (sopt->sopt_name) {
1281			case TCP_NODELAY:
1282				opt = TF_NODELAY;
1283				break;
1284			case TCP_NOOPT:
1285				opt = TF_NOOPT;
1286				break;
1287			default:
1288				opt = 0; /* dead code to fool gcc */
1289				break;
1290			}
1291
1292			if (optval)
1293				tp->t_flags |= opt;
1294			else
1295				tp->t_flags &= ~opt;
1296			break;
1297
1298		case TCP_NOPUSH:
1299			error = sooptcopyin(sopt, &optval, sizeof optval,
1300					    sizeof optval);
1301			if (error)
1302				break;
1303
1304			if (optval)
1305				tp->t_flags |= TF_NOPUSH;
1306			else {
1307				tp->t_flags &= ~TF_NOPUSH;
1308				error = tcp_output(tp);
1309			}
1310			break;
1311
1312		case TCP_MAXSEG:
1313			error = sooptcopyin(sopt, &optval, sizeof optval,
1314					    sizeof optval);
1315			if (error)
1316				break;
1317
1318			if (optval > 0 && optval <= tp->t_maxseg &&
1319			    optval + 40 >= tcp_minmss)
1320				tp->t_maxseg = optval;
1321			else
1322				error = EINVAL;
1323			break;
1324
1325		case TCP_INFO:
1326			error = EINVAL;
1327			break;
1328
1329		default:
1330			error = ENOPROTOOPT;
1331			break;
1332		}
1333		break;
1334
1335	case SOPT_GET:
1336		switch (sopt->sopt_name) {
1337#ifdef TCP_SIGNATURE
1338		case TCP_MD5SIG:
1339			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1340			error = sooptcopyout(sopt, &optval, sizeof optval);
1341			break;
1342#endif
1343		case TCP_NODELAY:
1344			optval = tp->t_flags & TF_NODELAY;
1345			error = sooptcopyout(sopt, &optval, sizeof optval);
1346			break;
1347		case TCP_MAXSEG:
1348			optval = tp->t_maxseg;
1349			error = sooptcopyout(sopt, &optval, sizeof optval);
1350			break;
1351		case TCP_NOOPT:
1352			optval = tp->t_flags & TF_NOOPT;
1353			error = sooptcopyout(sopt, &optval, sizeof optval);
1354			break;
1355		case TCP_NOPUSH:
1356			optval = tp->t_flags & TF_NOPUSH;
1357			error = sooptcopyout(sopt, &optval, sizeof optval);
1358			break;
1359		case TCP_INFO:
1360			tcp_fill_info(tp, &ti);
1361			error = sooptcopyout(sopt, &ti, sizeof ti);
1362			break;
1363		default:
1364			error = ENOPROTOOPT;
1365			break;
1366		}
1367		break;
1368	}
1369out:
1370	INP_UNLOCK(inp);
1371	return (error);
1372}
1373
1374/*
1375 * tcp_sendspace and tcp_recvspace are the default send and receive window
1376 * sizes, respectively.  These are obsolescent (this information should
1377 * be set by the route).
1378 */
1379u_long	tcp_sendspace = 1024*32;
1380SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1381    &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1382u_long	tcp_recvspace = 1024*64;
1383SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1384    &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1385
1386/*
1387 * Attach TCP protocol to socket, allocating
1388 * internet protocol control block, tcp control block,
1389 * bufer space, and entering LISTEN state if to accept connections.
1390 */
1391static int
1392tcp_attach(so)
1393	struct socket *so;
1394{
1395	register struct tcpcb *tp;
1396	struct inpcb *inp;
1397	int error;
1398#ifdef INET6
1399	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1400#endif
1401
1402	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1403
1404	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1405		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1406		if (error)
1407			return (error);
1408	}
1409	error = in_pcballoc(so, &tcbinfo, "tcpinp");
1410	if (error)
1411		return (error);
1412	inp = sotoinpcb(so);
1413#ifdef INET6
1414	if (isipv6) {
1415		inp->inp_vflag |= INP_IPV6;
1416		inp->in6p_hops = -1;	/* use kernel default */
1417	}
1418	else
1419#endif
1420	inp->inp_vflag |= INP_IPV4;
1421	tp = tcp_newtcpcb(inp);
1422	if (tp == NULL) {
1423		INP_LOCK(inp);
1424#ifdef INET6
1425		if (isipv6) {
1426			in6_pcbdetach(inp);
1427			in6_pcbfree(inp);
1428		} else {
1429#endif
1430			in_pcbdetach(inp);
1431			in_pcbfree(inp);
1432#ifdef INET6
1433		}
1434#endif
1435		return (ENOBUFS);
1436	}
1437	tp->t_state = TCPS_CLOSED;
1438	return (0);
1439}
1440
1441/*
1442 * Initiate (or continue) disconnect.
1443 * If embryonic state, just send reset (once).
1444 * If in ``let data drain'' option and linger null, just drop.
1445 * Otherwise (hard), mark socket disconnecting and drop
1446 * current input data; switch states based on user close, and
1447 * send segment to peer (with FIN).
1448 */
1449static void
1450tcp_disconnect(tp)
1451	register struct tcpcb *tp;
1452{
1453	struct inpcb *inp = tp->t_inpcb;
1454	struct socket *so = inp->inp_socket;
1455
1456	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1457	INP_LOCK_ASSERT(inp);
1458
1459	/*
1460	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1461	 * socket is still open.
1462	 */
1463	if (tp->t_state < TCPS_ESTABLISHED) {
1464		tp = tcp_close(tp);
1465		KASSERT(tp != NULL,
1466		    ("tcp_disconnect: tcp_close() returned NULL"));
1467	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1468		tp = tcp_drop(tp, 0);
1469		KASSERT(tp != NULL,
1470		    ("tcp_disconnect: tcp_drop() returned NULL"));
1471	} else {
1472		soisdisconnecting(so);
1473		sbflush(&so->so_rcv);
1474		tcp_usrclosed(tp);
1475		if (!(inp->inp_vflag & INP_DROPPED))
1476			tcp_output(tp);
1477	}
1478}
1479
1480/*
1481 * User issued close, and wish to trail through shutdown states:
1482 * if never received SYN, just forget it.  If got a SYN from peer,
1483 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1484 * If already got a FIN from peer, then almost done; go to LAST_ACK
1485 * state.  In all other cases, have already sent FIN to peer (e.g.
1486 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1487 * for peer to send FIN or not respond to keep-alives, etc.
1488 * We can let the user exit from the close as soon as the FIN is acked.
1489 */
1490static void
1491tcp_usrclosed(tp)
1492	register struct tcpcb *tp;
1493{
1494
1495	INP_INFO_WLOCK_ASSERT(&tcbinfo);
1496	INP_LOCK_ASSERT(tp->t_inpcb);
1497
1498	switch (tp->t_state) {
1499
1500	case TCPS_CLOSED:
1501	case TCPS_LISTEN:
1502		tp->t_state = TCPS_CLOSED;
1503		tp = tcp_close(tp);
1504		/*
1505		 * tcp_close() should never return NULL here as the socket is
1506		 * still open.
1507		 */
1508		KASSERT(tp != NULL,
1509		    ("tcp_usrclosed: tcp_close() returned NULL"));
1510		break;
1511
1512	case TCPS_SYN_SENT:
1513	case TCPS_SYN_RECEIVED:
1514		tp->t_flags |= TF_NEEDFIN;
1515		break;
1516
1517	case TCPS_ESTABLISHED:
1518		tp->t_state = TCPS_FIN_WAIT_1;
1519		break;
1520
1521	case TCPS_CLOSE_WAIT:
1522		tp->t_state = TCPS_LAST_ACK;
1523		break;
1524	}
1525	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1526		soisdisconnected(tp->t_inpcb->inp_socket);
1527		/* To prevent the connection hanging in FIN_WAIT_2 forever. */
1528		if (tp->t_state == TCPS_FIN_WAIT_2)
1529			callout_reset(tp->tt_2msl, tcp_maxidle,
1530				      tcp_timer_2msl, tp);
1531	}
1532}
1533