1/*	$NetBSD$	*/
2
3/*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
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 * 3. Neither the name of the project 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 PROJECT 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 PROJECT 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
32/*-
33 * Copyright (c) 1997, 1998, 2005, 2006 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
38 * Facility, NASA Ames Research Center.
39 * This code is derived from software contributed to The NetBSD Foundation
40 * by Charles M. Hannum.
41 * This code is derived from software contributed to The NetBSD Foundation
42 * by Rui Paulo.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 *    notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 *    notice, this list of conditions and the following disclaimer in the
51 *    documentation and/or other materials provided with the distribution.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
54 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
57 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63 * POSSIBILITY OF SUCH DAMAGE.
64 */
65
66/*
67 * Copyright (c) 1982, 1986, 1988, 1993, 1995
68 *	The Regents of the University of California.  All rights reserved.
69 *
70 * Redistribution and use in source and binary forms, with or without
71 * modification, are permitted provided that the following conditions
72 * are met:
73 * 1. Redistributions of source code must retain the above copyright
74 *    notice, this list of conditions and the following disclaimer.
75 * 2. Redistributions in binary form must reproduce the above copyright
76 *    notice, this list of conditions and the following disclaimer in the
77 *    documentation and/or other materials provided with the distribution.
78 * 3. Neither the name of the University nor the names of its contributors
79 *    may be used to endorse or promote products derived from this software
80 *    without specific prior written permission.
81 *
82 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
83 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
84 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
85 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
86 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
87 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
88 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
89 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
90 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
91 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
92 * SUCH DAMAGE.
93 *
94 *	@(#)tcp_usrreq.c	8.5 (Berkeley) 6/21/95
95 */
96
97#include <sys/cdefs.h>
98__KERNEL_RCSID(0, "$NetBSD$");
99
100#include "opt_inet.h"
101#include "opt_ipsec.h"
102#include "opt_tcp_debug.h"
103#include "opt_mbuftrace.h"
104
105
106#include <sys/param.h>
107#include <sys/systm.h>
108#include <sys/kernel.h>
109#include <sys/malloc.h>
110#include <sys/mbuf.h>
111#include <sys/socket.h>
112#include <sys/socketvar.h>
113#include <sys/protosw.h>
114#include <sys/errno.h>
115#include <sys/stat.h>
116#include <sys/proc.h>
117#include <sys/domain.h>
118#include <sys/sysctl.h>
119#include <sys/kauth.h>
120#include <sys/uidinfo.h>
121
122#include <net/if.h>
123#include <net/route.h>
124
125#include <netinet/in.h>
126#include <netinet/in_systm.h>
127#include <netinet/in_var.h>
128#include <netinet/ip.h>
129#include <netinet/in_pcb.h>
130#include <netinet/ip_var.h>
131#include <netinet/in_offload.h>
132
133#ifdef INET6
134#ifndef INET
135#include <netinet/in.h>
136#endif
137#include <netinet/ip6.h>
138#include <netinet6/in6_pcb.h>
139#include <netinet6/ip6_var.h>
140#include <netinet6/scope6_var.h>
141#endif
142
143#include <netinet/tcp.h>
144#include <netinet/tcp_fsm.h>
145#include <netinet/tcp_seq.h>
146#include <netinet/tcp_timer.h>
147#include <netinet/tcp_var.h>
148#include <netinet/tcp_private.h>
149#include <netinet/tcp_congctl.h>
150#include <netinet/tcpip.h>
151#include <netinet/tcp_debug.h>
152#include <netinet/tcp_vtw.h>
153
154#include "opt_tcp_space.h"
155
156#ifdef KAME_IPSEC
157#include <netinet6/ipsec.h>
158#endif /*KAME_IPSEC*/
159
160/*
161 * TCP protocol interface to socket abstraction.
162 */
163
164/*
165 * Process a TCP user request for TCP tb.  If this is a send request
166 * then m is the mbuf chain of send data.  If this is a timer expiration
167 * (called from the software clock routine), then timertype tells which timer.
168 */
169/*ARGSUSED*/
170int
171tcp_usrreq(struct socket *so, int req,
172    struct mbuf *m, struct mbuf *nam, struct mbuf *control, struct lwp *l)
173{
174	struct inpcb *inp;
175#ifdef INET6
176	struct in6pcb *in6p;
177#endif
178	struct tcpcb *tp = NULL;
179	int s;
180	int error = 0;
181#ifdef TCP_DEBUG
182	int ostate = 0;
183#endif
184	int family;	/* family of the socket */
185
186	family = so->so_proto->pr_domain->dom_family;
187
188	if (req == PRU_CONTROL) {
189		switch (family) {
190#ifdef INET
191		case PF_INET:
192			return (in_control(so, (long)m, (void *)nam,
193			    (struct ifnet *)control, l));
194#endif
195#ifdef INET6
196		case PF_INET6:
197			return (in6_control(so, (long)m, (void *)nam,
198			    (struct ifnet *)control, l));
199#endif
200		default:
201			return EAFNOSUPPORT;
202		}
203	}
204
205	s = splsoftnet();
206
207	if (req == PRU_PURGEIF) {
208		mutex_enter(softnet_lock);
209		switch (family) {
210#ifdef INET
211		case PF_INET:
212			in_pcbpurgeif0(&tcbtable, (struct ifnet *)control);
213			in_purgeif((struct ifnet *)control);
214			in_pcbpurgeif(&tcbtable, (struct ifnet *)control);
215			break;
216#endif
217#ifdef INET6
218		case PF_INET6:
219			in6_pcbpurgeif0(&tcbtable, (struct ifnet *)control);
220			in6_purgeif((struct ifnet *)control);
221			in6_pcbpurgeif(&tcbtable, (struct ifnet *)control);
222			break;
223#endif
224		default:
225			mutex_exit(softnet_lock);
226			splx(s);
227			return (EAFNOSUPPORT);
228		}
229		mutex_exit(softnet_lock);
230		splx(s);
231		return (0);
232	}
233
234	if (req == PRU_ATTACH)
235		sosetlock(so);
236
237	switch (family) {
238#ifdef INET
239	case PF_INET:
240		inp = sotoinpcb(so);
241#ifdef INET6
242		in6p = NULL;
243#endif
244		break;
245#endif
246#ifdef INET6
247	case PF_INET6:
248		inp = NULL;
249		in6p = sotoin6pcb(so);
250		break;
251#endif
252	default:
253		splx(s);
254		return EAFNOSUPPORT;
255	}
256
257#ifdef DIAGNOSTIC
258#ifdef INET6
259	if (inp && in6p)
260		panic("tcp_usrreq: both inp and in6p set to non-NULL");
261#endif
262	if (req != PRU_SEND && req != PRU_SENDOOB && control)
263		panic("tcp_usrreq: unexpected control mbuf");
264#endif
265	/*
266	 * When a TCP is attached to a socket, then there will be
267	 * a (struct inpcb) pointed at by the socket, and this
268	 * structure will point at a subsidary (struct tcpcb).
269	 */
270	if ((inp == 0
271#ifdef INET6
272	    && in6p == 0
273#endif
274	    ) && (req != PRU_ATTACH && req != PRU_SENSE))
275	{
276		error = EINVAL;
277		goto release;
278	}
279#ifdef INET
280	if (inp) {
281		tp = intotcpcb(inp);
282		/* WHAT IF TP IS 0? */
283#ifdef KPROF
284		tcp_acounts[tp->t_state][req]++;
285#endif
286#ifdef TCP_DEBUG
287		ostate = tp->t_state;
288#endif
289	}
290#endif
291#ifdef INET6
292	if (in6p) {
293		tp = in6totcpcb(in6p);
294		/* WHAT IF TP IS 0? */
295#ifdef KPROF
296		tcp_acounts[tp->t_state][req]++;
297#endif
298#ifdef TCP_DEBUG
299		ostate = tp->t_state;
300#endif
301	}
302#endif
303
304	switch (req) {
305
306	/*
307	 * TCP attaches to socket via PRU_ATTACH, reserving space,
308	 * and an internet control block.
309	 */
310	case PRU_ATTACH:
311#ifndef INET6
312		if (inp != 0)
313#else
314		if (inp != 0 || in6p != 0)
315#endif
316		{
317			error = EISCONN;
318			break;
319		}
320		error = tcp_attach(so);
321		if (error)
322			break;
323		if ((so->so_options & SO_LINGER) && so->so_linger == 0)
324			so->so_linger = TCP_LINGERTIME;
325		tp = sototcpcb(so);
326		break;
327
328	/*
329	 * PRU_DETACH detaches the TCP protocol from the socket.
330	 */
331	case PRU_DETACH:
332		tp = tcp_disconnect(tp);
333		break;
334
335	/*
336	 * Give the socket an address.
337	 */
338	case PRU_BIND:
339		switch (family) {
340#ifdef INET
341		case PF_INET:
342			error = in_pcbbind(inp, nam, l);
343			break;
344#endif
345#ifdef INET6
346		case PF_INET6:
347			error = in6_pcbbind(in6p, nam, l);
348			if (!error) {
349				/* mapped addr case */
350				if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
351					tp->t_family = AF_INET;
352				else
353					tp->t_family = AF_INET6;
354			}
355			break;
356#endif
357		}
358		break;
359
360	/*
361	 * Prepare to accept connections.
362	 */
363	case PRU_LISTEN:
364#ifdef INET
365		if (inp && inp->inp_lport == 0) {
366			error = in_pcbbind(inp, NULL, l);
367			if (error)
368				break;
369		}
370#endif
371#ifdef INET6
372		if (in6p && in6p->in6p_lport == 0) {
373			error = in6_pcbbind(in6p, NULL, l);
374			if (error)
375				break;
376		}
377#endif
378		tp->t_state = TCPS_LISTEN;
379		break;
380
381	/*
382	 * Initiate connection to peer.
383	 * Create a template for use in transmissions on this connection.
384	 * Enter SYN_SENT state, and mark socket as connecting.
385	 * Start keep-alive timer, and seed output sequence space.
386	 * Send initial segment on connection.
387	 */
388	case PRU_CONNECT:
389#ifdef INET
390		if (inp) {
391			if (inp->inp_lport == 0) {
392				error = in_pcbbind(inp, NULL, l);
393				if (error)
394					break;
395			}
396			error = in_pcbconnect(inp, nam, l);
397		}
398#endif
399#ifdef INET6
400		if (in6p) {
401			if (in6p->in6p_lport == 0) {
402				error = in6_pcbbind(in6p, NULL, l);
403				if (error)
404					break;
405			}
406			error = in6_pcbconnect(in6p, nam, l);
407			if (!error) {
408				/* mapped addr case */
409				if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
410					tp->t_family = AF_INET;
411				else
412					tp->t_family = AF_INET6;
413			}
414		}
415#endif
416		if (error)
417			break;
418		tp->t_template = tcp_template(tp);
419		if (tp->t_template == 0) {
420#ifdef INET
421			if (inp)
422				in_pcbdisconnect(inp);
423#endif
424#ifdef INET6
425			if (in6p)
426				in6_pcbdisconnect(in6p);
427#endif
428			error = ENOBUFS;
429			break;
430		}
431		/*
432		 * Compute window scaling to request.
433		 * XXX: This should be moved to tcp_output().
434		 */
435		while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
436		    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
437			tp->request_r_scale++;
438		soisconnecting(so);
439		TCP_STATINC(TCP_STAT_CONNATTEMPT);
440		tp->t_state = TCPS_SYN_SENT;
441		TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepinit);
442		tp->iss = tcp_new_iss(tp, 0);
443		tcp_sendseqinit(tp);
444		error = tcp_output(tp);
445		break;
446
447	/*
448	 * Create a TCP connection between two sockets.
449	 */
450	case PRU_CONNECT2:
451		error = EOPNOTSUPP;
452		break;
453
454	/*
455	 * Initiate disconnect from peer.
456	 * If connection never passed embryonic stage, just drop;
457	 * else if don't need to let data drain, then can just drop anyways,
458	 * else have to begin TCP shutdown process: mark socket disconnecting,
459	 * drain unread data, state switch to reflect user close, and
460	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
461	 * when peer sends FIN and acks ours.
462	 *
463	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
464	 */
465	case PRU_DISCONNECT:
466		tp = tcp_disconnect(tp);
467		break;
468
469	/*
470	 * Accept a connection.  Essentially all the work is
471	 * done at higher levels; just return the address
472	 * of the peer, storing through addr.
473	 */
474	case PRU_ACCEPT:
475#ifdef INET
476		if (inp)
477			in_setpeeraddr(inp, nam);
478#endif
479#ifdef INET6
480		if (in6p)
481			in6_setpeeraddr(in6p, nam);
482#endif
483		break;
484
485	/*
486	 * Mark the connection as being incapable of further output.
487	 */
488	case PRU_SHUTDOWN:
489		socantsendmore(so);
490		tp = tcp_usrclosed(tp);
491		if (tp)
492			error = tcp_output(tp);
493		break;
494
495	/*
496	 * After a receive, possibly send window update to peer.
497	 */
498	case PRU_RCVD:
499		/*
500		 * soreceive() calls this function when a user receives
501		 * ancillary data on a listening socket. We don't call
502		 * tcp_output in such a case, since there is no header
503		 * template for a listening socket and hence the kernel
504		 * will panic.
505		 */
506		if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) != 0)
507			(void) tcp_output(tp);
508		break;
509
510	/*
511	 * Do a send by putting data in output queue and updating urgent
512	 * marker if URG set.  Possibly send more data.
513	 */
514	case PRU_SEND:
515		if (control && control->m_len) {
516			m_freem(control);
517			m_freem(m);
518			error = EINVAL;
519			break;
520		}
521		sbappendstream(&so->so_snd, m);
522		error = tcp_output(tp);
523		break;
524
525	/*
526	 * Abort the TCP.
527	 */
528	case PRU_ABORT:
529		tp = tcp_drop(tp, ECONNABORTED);
530		break;
531
532	case PRU_SENSE:
533		/*
534		 * stat: don't bother with a blocksize.
535		 */
536		splx(s);
537		return (0);
538
539	case PRU_RCVOOB:
540		if (control && control->m_len) {
541			m_freem(control);
542			m_freem(m);
543			error = EINVAL;
544			break;
545		}
546		if ((so->so_oobmark == 0 &&
547		    (so->so_state & SS_RCVATMARK) == 0) ||
548		    so->so_options & SO_OOBINLINE ||
549		    tp->t_oobflags & TCPOOB_HADDATA) {
550			error = EINVAL;
551			break;
552		}
553		if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
554			error = EWOULDBLOCK;
555			break;
556		}
557		m->m_len = 1;
558		*mtod(m, char *) = tp->t_iobc;
559		if (((long)nam & MSG_PEEK) == 0)
560			tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
561		break;
562
563	case PRU_SENDOOB:
564		if (sbspace(&so->so_snd) < -512) {
565			m_freem(m);
566			error = ENOBUFS;
567			break;
568		}
569		/*
570		 * According to RFC961 (Assigned Protocols),
571		 * the urgent pointer points to the last octet
572		 * of urgent data.  We continue, however,
573		 * to consider it to indicate the first octet
574		 * of data past the urgent section.
575		 * Otherwise, snd_up should be one lower.
576		 */
577		sbappendstream(&so->so_snd, m);
578		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
579		tp->t_force = 1;
580		error = tcp_output(tp);
581		tp->t_force = 0;
582		break;
583
584	case PRU_SOCKADDR:
585#ifdef INET
586		if (inp)
587			in_setsockaddr(inp, nam);
588#endif
589#ifdef INET6
590		if (in6p)
591			in6_setsockaddr(in6p, nam);
592#endif
593		break;
594
595	case PRU_PEERADDR:
596#ifdef INET
597		if (inp)
598			in_setpeeraddr(inp, nam);
599#endif
600#ifdef INET6
601		if (in6p)
602			in6_setpeeraddr(in6p, nam);
603#endif
604		break;
605
606	default:
607		panic("tcp_usrreq");
608	}
609#ifdef TCP_DEBUG
610	if (tp && (so->so_options & SO_DEBUG))
611		tcp_trace(TA_USER, ostate, tp, NULL, req);
612#endif
613
614release:
615	splx(s);
616	return (error);
617}
618
619static void
620change_keepalive(struct socket *so, struct tcpcb *tp)
621{
622	tp->t_maxidle = tp->t_keepcnt * tp->t_keepintvl;
623	TCP_TIMER_DISARM(tp, TCPT_KEEP);
624	TCP_TIMER_DISARM(tp, TCPT_2MSL);
625
626	if (tp->t_state == TCPS_SYN_RECEIVED ||
627	    tp->t_state == TCPS_SYN_SENT) {
628		TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepinit);
629	} else if (so->so_options & SO_KEEPALIVE &&
630	    tp->t_state <= TCPS_CLOSE_WAIT) {
631		TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepintvl);
632	} else {
633		TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepidle);
634	}
635
636	if ((tp->t_state == TCPS_FIN_WAIT_2) && (tp->t_maxidle > 0))
637		TCP_TIMER_ARM(tp, TCPT_2MSL, tp->t_maxidle);
638}
639
640
641int
642tcp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
643{
644	int error = 0, s;
645	struct inpcb *inp;
646#ifdef INET6
647	struct in6pcb *in6p;
648#endif
649	struct tcpcb *tp;
650	u_int ui;
651	int family;	/* family of the socket */
652	int level, optname, optval;
653
654	level = sopt->sopt_level;
655	optname = sopt->sopt_name;
656
657	family = so->so_proto->pr_domain->dom_family;
658
659	s = splsoftnet();
660	switch (family) {
661#ifdef INET
662	case PF_INET:
663		inp = sotoinpcb(so);
664#ifdef INET6
665		in6p = NULL;
666#endif
667		break;
668#endif
669#ifdef INET6
670	case PF_INET6:
671		inp = NULL;
672		in6p = sotoin6pcb(so);
673		break;
674#endif
675	default:
676		splx(s);
677		panic("%s: af %d", __func__, family);
678	}
679#ifndef INET6
680	if (inp == NULL)
681#else
682	if (inp == NULL && in6p == NULL)
683#endif
684	{
685		splx(s);
686		return (ECONNRESET);
687	}
688	if (level != IPPROTO_TCP) {
689		switch (family) {
690#ifdef INET
691		case PF_INET:
692			error = ip_ctloutput(op, so, sopt);
693			break;
694#endif
695#ifdef INET6
696		case PF_INET6:
697			error = ip6_ctloutput(op, so, sopt);
698			break;
699#endif
700		}
701		splx(s);
702		return (error);
703	}
704	if (inp)
705		tp = intotcpcb(inp);
706#ifdef INET6
707	else if (in6p)
708		tp = in6totcpcb(in6p);
709#endif
710	else
711		tp = NULL;
712
713	switch (op) {
714	case PRCO_SETOPT:
715		switch (optname) {
716#ifdef TCP_SIGNATURE
717		case TCP_MD5SIG:
718			error = sockopt_getint(sopt, &optval);
719			if (error)
720				break;
721			if (optval > 0)
722				tp->t_flags |= TF_SIGNATURE;
723			else
724				tp->t_flags &= ~TF_SIGNATURE;
725			break;
726#endif /* TCP_SIGNATURE */
727
728		case TCP_NODELAY:
729			error = sockopt_getint(sopt, &optval);
730			if (error)
731				break;
732			if (optval)
733				tp->t_flags |= TF_NODELAY;
734			else
735				tp->t_flags &= ~TF_NODELAY;
736			break;
737
738		case TCP_MAXSEG:
739			error = sockopt_getint(sopt, &optval);
740			if (error)
741				break;
742			if (optval > 0 && optval <= tp->t_peermss)
743				tp->t_peermss = optval; /* limit on send size */
744			else
745				error = EINVAL;
746			break;
747#ifdef notyet
748		case TCP_CONGCTL:
749			/* XXX string overflow XXX */
750			error = tcp_congctl_select(tp, sopt->sopt_data);
751			break;
752#endif
753
754		case TCP_KEEPIDLE:
755			error = sockopt_get(sopt, &ui, sizeof(ui));
756			if (error)
757				break;
758			if (ui > 0) {
759				tp->t_keepidle = ui;
760				change_keepalive(so, tp);
761			} else
762				error = EINVAL;
763			break;
764
765		case TCP_KEEPINTVL:
766			error = sockopt_get(sopt, &ui, sizeof(ui));
767			if (error)
768				break;
769			if (ui > 0) {
770				tp->t_keepintvl = ui;
771				change_keepalive(so, tp);
772			} else
773				error = EINVAL;
774			break;
775
776		case TCP_KEEPCNT:
777			error = sockopt_get(sopt, &ui, sizeof(ui));
778			if (error)
779				break;
780			if (ui > 0) {
781				tp->t_keepcnt = ui;
782				change_keepalive(so, tp);
783			} else
784				error = EINVAL;
785			break;
786
787		case TCP_KEEPINIT:
788			error = sockopt_get(sopt, &ui, sizeof(ui));
789			if (error)
790				break;
791			if (ui > 0) {
792				tp->t_keepinit = ui;
793				change_keepalive(so, tp);
794			} else
795				error = EINVAL;
796			break;
797
798		default:
799			error = ENOPROTOOPT;
800			break;
801		}
802		break;
803
804	case PRCO_GETOPT:
805		switch (optname) {
806#ifdef TCP_SIGNATURE
807		case TCP_MD5SIG:
808			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
809			error = sockopt_set(sopt, &optval, sizeof(optval));
810			break;
811#endif
812		case TCP_NODELAY:
813			optval = tp->t_flags & TF_NODELAY;
814			error = sockopt_set(sopt, &optval, sizeof(optval));
815			break;
816		case TCP_MAXSEG:
817			optval = tp->t_peermss;
818			error = sockopt_set(sopt, &optval, sizeof(optval));
819			break;
820#ifdef notyet
821		case TCP_CONGCTL:
822			break;
823#endif
824		default:
825			error = ENOPROTOOPT;
826			break;
827		}
828		break;
829	}
830	splx(s);
831	return (error);
832}
833
834#ifndef TCP_SENDSPACE
835#define	TCP_SENDSPACE	1024*32
836#endif
837int	tcp_sendspace = TCP_SENDSPACE;
838#ifndef TCP_RECVSPACE
839#define	TCP_RECVSPACE	1024*32
840#endif
841int	tcp_recvspace = TCP_RECVSPACE;
842
843/*
844 * Attach TCP protocol to socket, allocating
845 * internet protocol control block, tcp control block,
846 * bufer space, and entering LISTEN state if to accept connections.
847 */
848int
849tcp_attach(struct socket *so)
850{
851	struct tcpcb *tp;
852	struct inpcb *inp;
853#ifdef INET6
854	struct in6pcb *in6p;
855#endif
856	int error;
857	int family;	/* family of the socket */
858
859	family = so->so_proto->pr_domain->dom_family;
860
861#ifdef MBUFTRACE
862	so->so_mowner = &tcp_sock_mowner;
863	so->so_rcv.sb_mowner = &tcp_sock_rx_mowner;
864	so->so_snd.sb_mowner = &tcp_sock_tx_mowner;
865#endif
866	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
867		error = soreserve(so, tcp_sendspace, tcp_recvspace);
868		if (error)
869			return (error);
870	}
871
872	so->so_rcv.sb_flags |= SB_AUTOSIZE;
873	so->so_snd.sb_flags |= SB_AUTOSIZE;
874
875	switch (family) {
876#ifdef INET
877	case PF_INET:
878		error = in_pcballoc(so, &tcbtable);
879		if (error)
880			return (error);
881		inp = sotoinpcb(so);
882#ifdef INET6
883		in6p = NULL;
884#endif
885		break;
886#endif
887#ifdef INET6
888	case PF_INET6:
889		error = in6_pcballoc(so, &tcbtable);
890		if (error)
891			return (error);
892		inp = NULL;
893		in6p = sotoin6pcb(so);
894		break;
895#endif
896	default:
897		return EAFNOSUPPORT;
898	}
899	if (inp)
900		tp = tcp_newtcpcb(family, (void *)inp);
901#ifdef INET6
902	else if (in6p)
903		tp = tcp_newtcpcb(family, (void *)in6p);
904#endif
905	else
906		tp = NULL;
907
908	if (tp == 0) {
909		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
910
911		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
912#ifdef INET
913		if (inp)
914			in_pcbdetach(inp);
915#endif
916#ifdef INET6
917		if (in6p)
918			in6_pcbdetach(in6p);
919#endif
920		so->so_state |= nofd;
921		return (ENOBUFS);
922	}
923	tp->t_state = TCPS_CLOSED;
924	return (0);
925}
926
927/*
928 * Initiate (or continue) disconnect.
929 * If embryonic state, just send reset (once).
930 * If in ``let data drain'' option and linger null, just drop.
931 * Otherwise (hard), mark socket disconnecting and drop
932 * current input data; switch states based on user close, and
933 * send segment to peer (with FIN).
934 */
935struct tcpcb *
936tcp_disconnect(struct tcpcb *tp)
937{
938	struct socket *so;
939
940	if (tp->t_inpcb)
941		so = tp->t_inpcb->inp_socket;
942#ifdef INET6
943	else if (tp->t_in6pcb)
944		so = tp->t_in6pcb->in6p_socket;
945#endif
946	else
947		so = NULL;
948
949	if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
950		tp = tcp_close(tp);
951	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
952		tp = tcp_drop(tp, 0);
953	else {
954		soisdisconnecting(so);
955		sbflush(&so->so_rcv);
956		tp = tcp_usrclosed(tp);
957		if (tp)
958			(void) tcp_output(tp);
959	}
960	return (tp);
961}
962
963/*
964 * User issued close, and wish to trail through shutdown states:
965 * if never received SYN, just forget it.  If got a SYN from peer,
966 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
967 * If already got a FIN from peer, then almost done; go to LAST_ACK
968 * state.  In all other cases, have already sent FIN to peer (e.g.
969 * after PRU_SHUTDOWN), and just have to play tedious game waiting
970 * for peer to send FIN or not respond to keep-alives, etc.
971 * We can let the user exit from the close as soon as the FIN is acked.
972 */
973struct tcpcb *
974tcp_usrclosed(struct tcpcb *tp)
975{
976
977	switch (tp->t_state) {
978
979	case TCPS_CLOSED:
980	case TCPS_LISTEN:
981	case TCPS_SYN_SENT:
982		tp->t_state = TCPS_CLOSED;
983		tp = tcp_close(tp);
984		break;
985
986	case TCPS_SYN_RECEIVED:
987	case TCPS_ESTABLISHED:
988		tp->t_state = TCPS_FIN_WAIT_1;
989		break;
990
991	case TCPS_CLOSE_WAIT:
992		tp->t_state = TCPS_LAST_ACK;
993		break;
994	}
995	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
996		struct socket *so;
997		if (tp->t_inpcb)
998			so = tp->t_inpcb->inp_socket;
999#ifdef INET6
1000		else if (tp->t_in6pcb)
1001			so = tp->t_in6pcb->in6p_socket;
1002#endif
1003		else
1004			so = NULL;
1005		if (so)
1006			soisdisconnected(so);
1007		/*
1008		 * If we are in FIN_WAIT_2, we arrived here because the
1009		 * application did a shutdown of the send side.  Like the
1010		 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after
1011		 * a full close, we start a timer to make sure sockets are
1012		 * not left in FIN_WAIT_2 forever.
1013		 */
1014		if ((tp->t_state == TCPS_FIN_WAIT_2) && (tp->t_maxidle > 0))
1015			TCP_TIMER_ARM(tp, TCPT_2MSL, tp->t_maxidle);
1016		else if (tp->t_state == TCPS_TIME_WAIT
1017			 && ((tp->t_inpcb
1018			      && (tcp4_vtw_enable & 1)
1019			      && vtw_add(AF_INET, tp))
1020			     ||
1021			     (tp->t_in6pcb
1022			      && (tcp6_vtw_enable & 1)
1023			      && vtw_add(AF_INET6, tp)))) {
1024			tp = 0;
1025		}
1026	}
1027	return (tp);
1028}
1029
1030/*
1031 * sysctl helper routine for net.inet.ip.mssdflt.  it can't be less
1032 * than 32.
1033 */
1034static int
1035sysctl_net_inet_tcp_mssdflt(SYSCTLFN_ARGS)
1036{
1037	int error, mssdflt;
1038	struct sysctlnode node;
1039
1040	mssdflt = tcp_mssdflt;
1041	node = *rnode;
1042	node.sysctl_data = &mssdflt;
1043	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1044	if (error || newp == NULL)
1045		return (error);
1046
1047	if (mssdflt < 32)
1048		return (EINVAL);
1049	tcp_mssdflt = mssdflt;
1050
1051	return (0);
1052}
1053
1054/*
1055 * sysctl helper routine for setting port related values under
1056 * net.inet.ip and net.inet6.ip6.  does basic range checking and does
1057 * additional checks for each type.  this code has placed in
1058 * tcp_input.c since INET and INET6 both use the same tcp code.
1059 *
1060 * this helper is not static so that both inet and inet6 can use it.
1061 */
1062int
1063sysctl_net_inet_ip_ports(SYSCTLFN_ARGS)
1064{
1065	int error, tmp;
1066	int apmin, apmax;
1067#ifndef IPNOPRIVPORTS
1068	int lpmin, lpmax;
1069#endif /* IPNOPRIVPORTS */
1070	struct sysctlnode node;
1071
1072	if (namelen != 0)
1073		return (EINVAL);
1074
1075	switch (name[-3]) {
1076#ifdef INET
1077	    case PF_INET:
1078		apmin = anonportmin;
1079		apmax = anonportmax;
1080#ifndef IPNOPRIVPORTS
1081		lpmin = lowportmin;
1082		lpmax = lowportmax;
1083#endif /* IPNOPRIVPORTS */
1084		break;
1085#endif /* INET */
1086#ifdef INET6
1087	    case PF_INET6:
1088		apmin = ip6_anonportmin;
1089		apmax = ip6_anonportmax;
1090#ifndef IPNOPRIVPORTS
1091		lpmin = ip6_lowportmin;
1092		lpmax = ip6_lowportmax;
1093#endif /* IPNOPRIVPORTS */
1094		break;
1095#endif /* INET6 */
1096	    default:
1097		return (EINVAL);
1098	}
1099
1100	/*
1101	 * insert temporary copy into node, perform lookup on
1102	 * temporary, then restore pointer
1103	 */
1104	node = *rnode;
1105	tmp = *(int*)rnode->sysctl_data;
1106	node.sysctl_data = &tmp;
1107	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1108	if (error || newp == NULL)
1109		return (error);
1110
1111	/*
1112	 * simple port range check
1113	 */
1114	if (tmp < 0 || tmp > 65535)
1115		return (EINVAL);
1116
1117	/*
1118	 * per-node range checks
1119	 */
1120	switch (rnode->sysctl_num) {
1121	case IPCTL_ANONPORTMIN:
1122	case IPV6CTL_ANONPORTMIN:
1123		if (tmp >= apmax)
1124			return (EINVAL);
1125#ifndef IPNOPRIVPORTS
1126		if (tmp < IPPORT_RESERVED)
1127                        return (EINVAL);
1128#endif /* IPNOPRIVPORTS */
1129		break;
1130
1131	case IPCTL_ANONPORTMAX:
1132	case IPV6CTL_ANONPORTMAX:
1133                if (apmin >= tmp)
1134			return (EINVAL);
1135#ifndef IPNOPRIVPORTS
1136		if (tmp < IPPORT_RESERVED)
1137                        return (EINVAL);
1138#endif /* IPNOPRIVPORTS */
1139		break;
1140
1141#ifndef IPNOPRIVPORTS
1142	case IPCTL_LOWPORTMIN:
1143	case IPV6CTL_LOWPORTMIN:
1144		if (tmp >= lpmax ||
1145		    tmp > IPPORT_RESERVEDMAX ||
1146		    tmp < IPPORT_RESERVEDMIN)
1147			return (EINVAL);
1148		break;
1149
1150	case IPCTL_LOWPORTMAX:
1151	case IPV6CTL_LOWPORTMAX:
1152		if (lpmin >= tmp ||
1153		    tmp > IPPORT_RESERVEDMAX ||
1154		    tmp < IPPORT_RESERVEDMIN)
1155			return (EINVAL);
1156		break;
1157#endif /* IPNOPRIVPORTS */
1158
1159	default:
1160		return (EINVAL);
1161	}
1162
1163	*(int*)rnode->sysctl_data = tmp;
1164
1165	return (0);
1166}
1167
1168static inline int
1169copyout_uid(struct socket *sockp, void *oldp, size_t *oldlenp)
1170{
1171	if (oldp) {
1172		size_t sz;
1173		uid_t uid;
1174		int error;
1175
1176		if (sockp->so_cred == NULL)
1177			return EPERM;
1178
1179		uid = kauth_cred_geteuid(sockp->so_cred);
1180		sz = MIN(sizeof(uid), *oldlenp);
1181		if ((error = copyout(&uid, oldp, sz)) != 0)
1182			return error;
1183	}
1184	*oldlenp = sizeof(uid_t);
1185	return 0;
1186}
1187
1188static inline int
1189inet4_ident_core(struct in_addr raddr, u_int rport,
1190    struct in_addr laddr, u_int lport,
1191    void *oldp, size_t *oldlenp,
1192    struct lwp *l, int dodrop)
1193{
1194	struct inpcb *inp;
1195	struct socket *sockp;
1196
1197	inp = in_pcblookup_connect(&tcbtable, raddr, rport, laddr, lport, 0);
1198
1199	if (inp == NULL || (sockp = inp->inp_socket) == NULL)
1200		return ESRCH;
1201
1202	if (dodrop) {
1203		struct tcpcb *tp;
1204		int error;
1205
1206		if (inp == NULL || (tp = intotcpcb(inp)) == NULL ||
1207		    (inp->inp_socket->so_options & SO_ACCEPTCONN) != 0)
1208			return ESRCH;
1209
1210		error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
1211		    KAUTH_REQ_NETWORK_SOCKET_DROP, inp->inp_socket, tp, NULL);
1212		if (error)
1213			return (error);
1214
1215		(void)tcp_drop(tp, ECONNABORTED);
1216		return 0;
1217	}
1218	else
1219		return copyout_uid(sockp, oldp, oldlenp);
1220}
1221
1222#ifdef INET6
1223static inline int
1224inet6_ident_core(struct in6_addr *raddr, u_int rport,
1225    struct in6_addr *laddr, u_int lport,
1226    void *oldp, size_t *oldlenp,
1227    struct lwp *l, int dodrop)
1228{
1229	struct in6pcb *in6p;
1230	struct socket *sockp;
1231
1232	in6p = in6_pcblookup_connect(&tcbtable, raddr, rport, laddr, lport, 0, 0);
1233
1234	if (in6p == NULL || (sockp = in6p->in6p_socket) == NULL)
1235		return ESRCH;
1236
1237	if (dodrop) {
1238		struct tcpcb *tp;
1239		int error;
1240
1241		if (in6p == NULL || (tp = in6totcpcb(in6p)) == NULL ||
1242		    (in6p->in6p_socket->so_options & SO_ACCEPTCONN) != 0)
1243			return ESRCH;
1244
1245		error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
1246		    KAUTH_REQ_NETWORK_SOCKET_DROP, in6p->in6p_socket, tp, NULL);
1247		if (error)
1248			return (error);
1249
1250		(void)tcp_drop(tp, ECONNABORTED);
1251		return 0;
1252	}
1253	else
1254		return copyout_uid(sockp, oldp, oldlenp);
1255}
1256#endif
1257
1258/*
1259 * sysctl helper routine for the net.inet.tcp.drop and
1260 * net.inet6.tcp6.drop nodes.
1261 */
1262#define sysctl_net_inet_tcp_drop sysctl_net_inet_tcp_ident
1263
1264/*
1265 * sysctl helper routine for the net.inet.tcp.ident and
1266 * net.inet6.tcp6.ident nodes.  contains backwards compat code for the
1267 * old way of looking up the ident information for ipv4 which involves
1268 * stuffing the port/addr pairs into the mib lookup.
1269 */
1270static int
1271sysctl_net_inet_tcp_ident(SYSCTLFN_ARGS)
1272{
1273#ifdef INET
1274	struct sockaddr_in *si4[2];
1275#endif /* INET */
1276#ifdef INET6
1277	struct sockaddr_in6 *si6[2];
1278#endif /* INET6 */
1279	struct sockaddr_storage sa[2];
1280	int error, pf, dodrop;
1281
1282	dodrop = name[-1] == TCPCTL_DROP;
1283	if (dodrop) {
1284		if (oldp != NULL || *oldlenp != 0)
1285			return EINVAL;
1286		if (newp == NULL)
1287			return EPERM;
1288		if (newlen < sizeof(sa))
1289			return ENOMEM;
1290	}
1291	if (namelen != 4 && namelen != 0)
1292		return EINVAL;
1293	if (name[-2] != IPPROTO_TCP)
1294		return EINVAL;
1295	pf = name[-3];
1296
1297	/* old style lookup, ipv4 only */
1298	if (namelen == 4) {
1299#ifdef INET
1300		struct in_addr laddr, raddr;
1301		u_int lport, rport;
1302
1303		if (pf != PF_INET)
1304			return EPROTONOSUPPORT;
1305		raddr.s_addr = (uint32_t)name[0];
1306		rport = (u_int)name[1];
1307		laddr.s_addr = (uint32_t)name[2];
1308		lport = (u_int)name[3];
1309
1310		mutex_enter(softnet_lock);
1311		error = inet4_ident_core(raddr, rport, laddr, lport,
1312		    oldp, oldlenp, l, dodrop);
1313		mutex_exit(softnet_lock);
1314		return error;
1315#else /* INET */
1316		return EINVAL;
1317#endif /* INET */
1318	}
1319
1320	if (newp == NULL || newlen != sizeof(sa))
1321		return EINVAL;
1322	error = copyin(newp, &sa, newlen);
1323	if (error)
1324		return error;
1325
1326	/*
1327	 * requested families must match
1328	 */
1329	if (pf != sa[0].ss_family || sa[0].ss_family != sa[1].ss_family)
1330		return EINVAL;
1331
1332	switch (pf) {
1333#ifdef INET6
1334	case PF_INET6:
1335		si6[0] = (struct sockaddr_in6*)&sa[0];
1336		si6[1] = (struct sockaddr_in6*)&sa[1];
1337		if (si6[0]->sin6_len != sizeof(*si6[0]) ||
1338		    si6[1]->sin6_len != sizeof(*si6[1]))
1339			return EINVAL;
1340
1341		if (!IN6_IS_ADDR_V4MAPPED(&si6[0]->sin6_addr) &&
1342		    !IN6_IS_ADDR_V4MAPPED(&si6[1]->sin6_addr)) {
1343			error = sa6_embedscope(si6[0], ip6_use_defzone);
1344			if (error)
1345				return error;
1346			error = sa6_embedscope(si6[1], ip6_use_defzone);
1347			if (error)
1348				return error;
1349
1350			mutex_enter(softnet_lock);
1351			error = inet6_ident_core(&si6[0]->sin6_addr,
1352			    si6[0]->sin6_port, &si6[1]->sin6_addr,
1353			    si6[1]->sin6_port, oldp, oldlenp, l, dodrop);
1354			mutex_exit(softnet_lock);
1355			return error;
1356		}
1357
1358		if (IN6_IS_ADDR_V4MAPPED(&si6[0]->sin6_addr) !=
1359		    IN6_IS_ADDR_V4MAPPED(&si6[1]->sin6_addr))
1360			return EINVAL;
1361
1362		in6_sin6_2_sin_in_sock((struct sockaddr *)&sa[0]);
1363		in6_sin6_2_sin_in_sock((struct sockaddr *)&sa[1]);
1364		/*FALLTHROUGH*/
1365#endif /* INET6 */
1366#ifdef INET
1367	case PF_INET:
1368		si4[0] = (struct sockaddr_in*)&sa[0];
1369		si4[1] = (struct sockaddr_in*)&sa[1];
1370		if (si4[0]->sin_len != sizeof(*si4[0]) ||
1371		    si4[0]->sin_len != sizeof(*si4[1]))
1372			return EINVAL;
1373
1374		mutex_enter(softnet_lock);
1375		error = inet4_ident_core(si4[0]->sin_addr, si4[0]->sin_port,
1376		    si4[1]->sin_addr, si4[1]->sin_port,
1377		    oldp, oldlenp, l, dodrop);
1378		mutex_exit(softnet_lock);
1379		return error;
1380#endif /* INET */
1381	default:
1382		return EPROTONOSUPPORT;
1383	}
1384}
1385
1386/*
1387 * sysctl helper for the inet and inet6 pcblists.  handles tcp/udp and
1388 * inet/inet6, as well as raw pcbs for each.  specifically not
1389 * declared static so that raw sockets and udp/udp6 can use it as
1390 * well.
1391 */
1392int
1393sysctl_inpcblist(SYSCTLFN_ARGS)
1394{
1395#ifdef INET
1396	struct sockaddr_in *in;
1397	const struct inpcb *inp;
1398#endif
1399#ifdef INET6
1400	struct sockaddr_in6 *in6;
1401	const struct in6pcb *in6p;
1402#endif
1403	/*
1404	 * sysctl_data is const, but CIRCLEQ_FOREACH can't use a const
1405	 * struct inpcbtable pointer, so we have to discard const.  :-/
1406	 */
1407	struct inpcbtable *pcbtbl = __UNCONST(rnode->sysctl_data);
1408	const struct inpcb_hdr *inph;
1409	struct tcpcb *tp;
1410	struct kinfo_pcb pcb;
1411	char *dp;
1412	u_int op, arg;
1413	size_t len, needed, elem_size, out_size;
1414	int error, elem_count, pf, proto, pf2;
1415
1416	if (namelen != 4)
1417		return (EINVAL);
1418
1419	if (oldp != NULL) {
1420		    len = *oldlenp;
1421		    elem_size = name[2];
1422		    elem_count = name[3];
1423		    if (elem_size != sizeof(pcb))
1424			    return EINVAL;
1425	} else {
1426		    len = 0;
1427		    elem_count = INT_MAX;
1428		    elem_size = sizeof(pcb);
1429	}
1430	error = 0;
1431	dp = oldp;
1432	op = name[0];
1433	arg = name[1];
1434	out_size = elem_size;
1435	needed = 0;
1436
1437	if (namelen == 1 && name[0] == CTL_QUERY)
1438		return (sysctl_query(SYSCTLFN_CALL(rnode)));
1439
1440	if (name - oname != 4)
1441		return (EINVAL);
1442
1443	pf = oname[1];
1444	proto = oname[2];
1445	pf2 = (oldp != NULL) ? pf : 0;
1446
1447	mutex_enter(softnet_lock);
1448
1449	CIRCLEQ_FOREACH(inph, &pcbtbl->inpt_queue, inph_queue) {
1450#ifdef INET
1451		inp = (const struct inpcb *)inph;
1452#endif
1453#ifdef INET6
1454		in6p = (const struct in6pcb *)inph;
1455#endif
1456
1457		if (inph->inph_af != pf)
1458			continue;
1459
1460		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
1461		    KAUTH_REQ_NETWORK_SOCKET_CANSEE, inph->inph_socket, NULL,
1462		    NULL) != 0)
1463			continue;
1464
1465		memset(&pcb, 0, sizeof(pcb));
1466
1467		pcb.ki_family = pf;
1468		pcb.ki_type = proto;
1469
1470		switch (pf2) {
1471		case 0:
1472			/* just probing for size */
1473			break;
1474#ifdef INET
1475		case PF_INET:
1476			pcb.ki_family = inp->inp_socket->so_proto->
1477			    pr_domain->dom_family;
1478			pcb.ki_type = inp->inp_socket->so_proto->
1479			    pr_type;
1480			pcb.ki_protocol = inp->inp_socket->so_proto->
1481			    pr_protocol;
1482			pcb.ki_pflags = inp->inp_flags;
1483
1484			pcb.ki_sostate = inp->inp_socket->so_state;
1485			pcb.ki_prstate = inp->inp_state;
1486			if (proto == IPPROTO_TCP) {
1487				tp = intotcpcb(inp);
1488				pcb.ki_tstate = tp->t_state;
1489				pcb.ki_tflags = tp->t_flags;
1490			}
1491
1492			pcb.ki_pcbaddr = PTRTOUINT64(inp);
1493			pcb.ki_ppcbaddr = PTRTOUINT64(inp->inp_ppcb);
1494			pcb.ki_sockaddr = PTRTOUINT64(inp->inp_socket);
1495
1496			pcb.ki_rcvq = inp->inp_socket->so_rcv.sb_cc;
1497			pcb.ki_sndq = inp->inp_socket->so_snd.sb_cc;
1498
1499			in = satosin(&pcb.ki_src);
1500			in->sin_len = sizeof(*in);
1501			in->sin_family = pf;
1502			in->sin_port = inp->inp_lport;
1503			in->sin_addr = inp->inp_laddr;
1504			if (pcb.ki_prstate >= INP_CONNECTED) {
1505				in = satosin(&pcb.ki_dst);
1506				in->sin_len = sizeof(*in);
1507				in->sin_family = pf;
1508				in->sin_port = inp->inp_fport;
1509				in->sin_addr = inp->inp_faddr;
1510			}
1511			break;
1512#endif
1513#ifdef INET6
1514		case PF_INET6:
1515			pcb.ki_family = in6p->in6p_socket->so_proto->
1516			    pr_domain->dom_family;
1517			pcb.ki_type = in6p->in6p_socket->so_proto->pr_type;
1518			pcb.ki_protocol = in6p->in6p_socket->so_proto->
1519			    pr_protocol;
1520			pcb.ki_pflags = in6p->in6p_flags;
1521
1522			pcb.ki_sostate = in6p->in6p_socket->so_state;
1523			pcb.ki_prstate = in6p->in6p_state;
1524			if (proto == IPPROTO_TCP) {
1525				tp = in6totcpcb(in6p);
1526				pcb.ki_tstate = tp->t_state;
1527				pcb.ki_tflags = tp->t_flags;
1528			}
1529
1530			pcb.ki_pcbaddr = PTRTOUINT64(in6p);
1531			pcb.ki_ppcbaddr = PTRTOUINT64(in6p->in6p_ppcb);
1532			pcb.ki_sockaddr = PTRTOUINT64(in6p->in6p_socket);
1533
1534			pcb.ki_rcvq = in6p->in6p_socket->so_rcv.sb_cc;
1535			pcb.ki_sndq = in6p->in6p_socket->so_snd.sb_cc;
1536
1537			in6 = satosin6(&pcb.ki_src);
1538			in6->sin6_len = sizeof(*in6);
1539			in6->sin6_family = pf;
1540			in6->sin6_port = in6p->in6p_lport;
1541			in6->sin6_flowinfo = in6p->in6p_flowinfo;
1542			in6->sin6_addr = in6p->in6p_laddr;
1543			in6->sin6_scope_id = 0; /* XXX? */
1544
1545			if (pcb.ki_prstate >= IN6P_CONNECTED) {
1546				in6 = satosin6(&pcb.ki_dst);
1547				in6->sin6_len = sizeof(*in6);
1548				in6->sin6_family = pf;
1549				in6->sin6_port = in6p->in6p_fport;
1550				in6->sin6_flowinfo = in6p->in6p_flowinfo;
1551				in6->sin6_addr = in6p->in6p_faddr;
1552				in6->sin6_scope_id = 0; /* XXX? */
1553			}
1554			break;
1555#endif
1556		}
1557
1558		if (len >= elem_size && elem_count > 0) {
1559			error = copyout(&pcb, dp, out_size);
1560			if (error) {
1561				mutex_exit(softnet_lock);
1562				return (error);
1563			}
1564			dp += elem_size;
1565			len -= elem_size;
1566		}
1567		needed += elem_size;
1568		if (elem_count > 0 && elem_count != INT_MAX)
1569			elem_count--;
1570	}
1571
1572	*oldlenp = needed;
1573	if (oldp == NULL)
1574		*oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
1575
1576	mutex_exit(softnet_lock);
1577
1578	return (error);
1579}
1580
1581static int
1582sysctl_tcp_congctl(SYSCTLFN_ARGS)
1583{
1584	struct sysctlnode node;
1585	int error;
1586	char newname[TCPCC_MAXLEN];
1587
1588	strlcpy(newname, tcp_congctl_global_name, sizeof(newname) - 1);
1589
1590	node = *rnode;
1591	node.sysctl_data = newname;
1592	node.sysctl_size = sizeof(newname);
1593
1594	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1595
1596	if (error ||
1597	    newp == NULL ||
1598	    strncmp(newname, tcp_congctl_global_name, sizeof(newname)) == 0)
1599		return error;
1600
1601	mutex_enter(softnet_lock);
1602	error = tcp_congctl_select(NULL, newname);
1603	mutex_exit(softnet_lock);
1604
1605	return error;
1606}
1607
1608static int
1609sysctl_tcp_keep(SYSCTLFN_ARGS)
1610{
1611	int error;
1612	u_int tmp;
1613	struct sysctlnode node;
1614
1615	node = *rnode;
1616	tmp = *(u_int *)rnode->sysctl_data;
1617	node.sysctl_data = &tmp;
1618
1619	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1620	if (error || newp == NULL)
1621		return error;
1622
1623	mutex_enter(softnet_lock);
1624
1625	*(u_int *)rnode->sysctl_data = tmp;
1626	tcp_tcpcb_template();	/* update the template */
1627
1628	mutex_exit(softnet_lock);
1629	return 0;
1630}
1631
1632static int
1633sysctl_net_inet_tcp_stats(SYSCTLFN_ARGS)
1634{
1635
1636	return (NETSTAT_SYSCTL(tcpstat_percpu, TCP_NSTATS));
1637}
1638
1639/*
1640 * this (second stage) setup routine is a replacement for tcp_sysctl()
1641 * (which is currently used for ipv4 and ipv6)
1642 */
1643static void
1644sysctl_net_inet_tcp_setup2(struct sysctllog **clog, int pf, const char *pfname,
1645			   const char *tcpname)
1646{
1647	const struct sysctlnode *sack_node;
1648	const struct sysctlnode *abc_node;
1649	const struct sysctlnode *ecn_node;
1650	const struct sysctlnode *congctl_node;
1651	const struct sysctlnode *mslt_node;
1652	const struct sysctlnode *vtw_node;
1653#ifdef TCP_DEBUG
1654	extern struct tcp_debug tcp_debug[TCP_NDEBUG];
1655	extern int tcp_debx;
1656#endif
1657
1658	sysctl_createv(clog, 0, NULL, NULL,
1659		       CTLFLAG_PERMANENT,
1660		       CTLTYPE_NODE, "net", NULL,
1661		       NULL, 0, NULL, 0,
1662		       CTL_NET, CTL_EOL);
1663	sysctl_createv(clog, 0, NULL, NULL,
1664		       CTLFLAG_PERMANENT,
1665		       CTLTYPE_NODE, pfname, NULL,
1666		       NULL, 0, NULL, 0,
1667		       CTL_NET, pf, CTL_EOL);
1668	sysctl_createv(clog, 0, NULL, NULL,
1669		       CTLFLAG_PERMANENT,
1670		       CTLTYPE_NODE, tcpname,
1671		       SYSCTL_DESCR("TCP related settings"),
1672		       NULL, 0, NULL, 0,
1673		       CTL_NET, pf, IPPROTO_TCP, CTL_EOL);
1674
1675	sysctl_createv(clog, 0, NULL, NULL,
1676		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1677		       CTLTYPE_INT, "rfc1323",
1678		       SYSCTL_DESCR("Enable RFC1323 TCP extensions"),
1679		       NULL, 0, &tcp_do_rfc1323, 0,
1680		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RFC1323, CTL_EOL);
1681	sysctl_createv(clog, 0, NULL, NULL,
1682		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1683		       CTLTYPE_INT, "sendspace",
1684		       SYSCTL_DESCR("Default TCP send buffer size"),
1685		       NULL, 0, &tcp_sendspace, 0,
1686		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SENDSPACE, CTL_EOL);
1687	sysctl_createv(clog, 0, NULL, NULL,
1688		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1689		       CTLTYPE_INT, "recvspace",
1690		       SYSCTL_DESCR("Default TCP receive buffer size"),
1691		       NULL, 0, &tcp_recvspace, 0,
1692		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RECVSPACE, CTL_EOL);
1693	sysctl_createv(clog, 0, NULL, NULL,
1694		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1695		       CTLTYPE_INT, "mssdflt",
1696		       SYSCTL_DESCR("Default maximum segment size"),
1697		       sysctl_net_inet_tcp_mssdflt, 0, &tcp_mssdflt, 0,
1698		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSSDFLT, CTL_EOL);
1699	sysctl_createv(clog, 0, NULL, NULL,
1700		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1701		       CTLTYPE_INT, "minmss",
1702		       SYSCTL_DESCR("Lower limit for TCP maximum segment size"),
1703		       NULL, 0, &tcp_minmss, 0,
1704		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1705	sysctl_createv(clog, 0, NULL, NULL,
1706		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1707		       CTLTYPE_INT, "msl",
1708		       SYSCTL_DESCR("Maximum Segment Life"),
1709		       NULL, 0, &tcp_msl, 0,
1710		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSL, CTL_EOL);
1711	sysctl_createv(clog, 0, NULL, NULL,
1712		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1713		       CTLTYPE_INT, "syn_cache_limit",
1714		       SYSCTL_DESCR("Maximum number of entries in the TCP "
1715				    "compressed state engine"),
1716		       NULL, 0, &tcp_syn_cache_limit, 0,
1717		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_LIMIT,
1718		       CTL_EOL);
1719	sysctl_createv(clog, 0, NULL, NULL,
1720		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1721		       CTLTYPE_INT, "syn_bucket_limit",
1722		       SYSCTL_DESCR("Maximum number of entries per hash "
1723				    "bucket in the TCP compressed state "
1724				    "engine"),
1725		       NULL, 0, &tcp_syn_bucket_limit, 0,
1726		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_BUCKET_LIMIT,
1727		       CTL_EOL);
1728#if 0 /* obsoleted */
1729	sysctl_createv(clog, 0, NULL, NULL,
1730		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1731		       CTLTYPE_INT, "syn_cache_interval",
1732		       SYSCTL_DESCR("TCP compressed state engine's timer interval"),
1733		       NULL, 0, &tcp_syn_cache_interval, 0,
1734		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_INTER,
1735		       CTL_EOL);
1736#endif
1737	sysctl_createv(clog, 0, NULL, NULL,
1738		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1739		       CTLTYPE_INT, "init_win",
1740		       SYSCTL_DESCR("Initial TCP congestion window"),
1741		       NULL, 0, &tcp_init_win, 0,
1742		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN, CTL_EOL);
1743	sysctl_createv(clog, 0, NULL, NULL,
1744		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1745		       CTLTYPE_INT, "mss_ifmtu",
1746		       SYSCTL_DESCR("Use interface MTU for calculating MSS"),
1747		       NULL, 0, &tcp_mss_ifmtu, 0,
1748		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSS_IFMTU, CTL_EOL);
1749	sysctl_createv(clog, 0, NULL, &sack_node,
1750		       CTLFLAG_PERMANENT,
1751		       CTLTYPE_NODE, "sack",
1752		       SYSCTL_DESCR("RFC2018 Selective ACKnowledgement tunables"),
1753		       NULL, 0, NULL, 0,
1754		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_EOL);
1755
1756	/* Congctl subtree */
1757	sysctl_createv(clog, 0, NULL, &congctl_node,
1758		       CTLFLAG_PERMANENT,
1759		       CTLTYPE_NODE, "congctl",
1760		       SYSCTL_DESCR("TCP Congestion Control"),
1761	    	       NULL, 0, NULL, 0,
1762		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1763	sysctl_createv(clog, 0, &congctl_node, NULL,
1764		       CTLFLAG_PERMANENT,
1765		       CTLTYPE_STRING, "available",
1766		       SYSCTL_DESCR("Available Congestion Control Mechanisms"),
1767		       NULL, 0, &tcp_congctl_avail, 0, CTL_CREATE, CTL_EOL);
1768	sysctl_createv(clog, 0, &congctl_node, NULL,
1769		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1770		       CTLTYPE_STRING, "selected",
1771		       SYSCTL_DESCR("Selected Congestion Control Mechanism"),
1772		       sysctl_tcp_congctl, 0, NULL, TCPCC_MAXLEN,
1773		       CTL_CREATE, CTL_EOL);
1774
1775	sysctl_createv(clog, 0, NULL, NULL,
1776		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1777		       CTLTYPE_INT, "win_scale",
1778		       SYSCTL_DESCR("Use RFC1323 window scale options"),
1779		       NULL, 0, &tcp_do_win_scale, 0,
1780		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_WSCALE, CTL_EOL);
1781	sysctl_createv(clog, 0, NULL, NULL,
1782		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1783		       CTLTYPE_INT, "timestamps",
1784		       SYSCTL_DESCR("Use RFC1323 time stamp options"),
1785		       NULL, 0, &tcp_do_timestamps, 0,
1786		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_TSTAMP, CTL_EOL);
1787	sysctl_createv(clog, 0, NULL, NULL,
1788		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1789		       CTLTYPE_INT, "compat_42",
1790		       SYSCTL_DESCR("Enable workarounds for 4.2BSD TCP bugs"),
1791		       NULL, 0, &tcp_compat_42, 0,
1792		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_COMPAT_42, CTL_EOL);
1793	sysctl_createv(clog, 0, NULL, NULL,
1794		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1795		       CTLTYPE_INT, "cwm",
1796		       SYSCTL_DESCR("Hughes/Touch/Heidemann Congestion Window "
1797				    "Monitoring"),
1798		       NULL, 0, &tcp_cwm, 0,
1799		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM, CTL_EOL);
1800	sysctl_createv(clog, 0, NULL, NULL,
1801		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1802		       CTLTYPE_INT, "cwm_burstsize",
1803		       SYSCTL_DESCR("Congestion Window Monitoring allowed "
1804				    "burst count in packets"),
1805		       NULL, 0, &tcp_cwm_burstsize, 0,
1806		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM_BURSTSIZE,
1807		       CTL_EOL);
1808	sysctl_createv(clog, 0, NULL, NULL,
1809		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1810		       CTLTYPE_INT, "ack_on_push",
1811		       SYSCTL_DESCR("Immediately return ACK when PSH is "
1812				    "received"),
1813		       NULL, 0, &tcp_ack_on_push, 0,
1814		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_ACK_ON_PUSH, CTL_EOL);
1815	sysctl_createv(clog, 0, NULL, NULL,
1816		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1817		       CTLTYPE_INT, "keepidle",
1818		       SYSCTL_DESCR("Allowed connection idle ticks before a "
1819				    "keepalive probe is sent"),
1820		       sysctl_tcp_keep, 0, &tcp_keepidle, 0,
1821		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPIDLE, CTL_EOL);
1822	sysctl_createv(clog, 0, NULL, NULL,
1823		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1824		       CTLTYPE_INT, "keepintvl",
1825		       SYSCTL_DESCR("Ticks before next keepalive probe is sent"),
1826		       sysctl_tcp_keep, 0, &tcp_keepintvl, 0,
1827		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPINTVL, CTL_EOL);
1828	sysctl_createv(clog, 0, NULL, NULL,
1829		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1830		       CTLTYPE_INT, "keepcnt",
1831		       SYSCTL_DESCR("Number of keepalive probes to send"),
1832		       sysctl_tcp_keep, 0, &tcp_keepcnt, 0,
1833		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPCNT, CTL_EOL);
1834	sysctl_createv(clog, 0, NULL, NULL,
1835		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
1836		       CTLTYPE_INT, "slowhz",
1837		       SYSCTL_DESCR("Keepalive ticks per second"),
1838		       NULL, PR_SLOWHZ, NULL, 0,
1839		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SLOWHZ, CTL_EOL);
1840	sysctl_createv(clog, 0, NULL, NULL,
1841		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1842		       CTLTYPE_INT, "log_refused",
1843		       SYSCTL_DESCR("Log refused TCP connections"),
1844		       NULL, 0, &tcp_log_refused, 0,
1845		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOG_REFUSED, CTL_EOL);
1846#if 0 /* obsoleted */
1847	sysctl_createv(clog, 0, NULL, NULL,
1848		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1849		       CTLTYPE_INT, "rstratelimit", NULL,
1850		       NULL, 0, &tcp_rst_ratelim, 0,
1851		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTRATELIMIT, CTL_EOL);
1852#endif
1853	sysctl_createv(clog, 0, NULL, NULL,
1854		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1855		       CTLTYPE_INT, "rstppslimit",
1856		       SYSCTL_DESCR("Maximum number of RST packets to send "
1857				    "per second"),
1858		       NULL, 0, &tcp_rst_ppslim, 0,
1859		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTPPSLIMIT, CTL_EOL);
1860	sysctl_createv(clog, 0, NULL, NULL,
1861		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1862		       CTLTYPE_INT, "delack_ticks",
1863		       SYSCTL_DESCR("Number of ticks to delay sending an ACK"),
1864		       NULL, 0, &tcp_delack_ticks, 0,
1865		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DELACK_TICKS, CTL_EOL);
1866	sysctl_createv(clog, 0, NULL, NULL,
1867		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1868		       CTLTYPE_INT, "init_win_local",
1869		       SYSCTL_DESCR("Initial TCP window size (in segments)"),
1870		       NULL, 0, &tcp_init_win_local, 0,
1871		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN_LOCAL,
1872		       CTL_EOL);
1873	sysctl_createv(clog, 0, NULL, NULL,
1874		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1875		       CTLTYPE_STRUCT, "ident",
1876		       SYSCTL_DESCR("RFC1413 Identification Protocol lookups"),
1877		       sysctl_net_inet_tcp_ident, 0, NULL, sizeof(uid_t),
1878		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_IDENT, CTL_EOL);
1879	sysctl_createv(clog, 0, NULL, NULL,
1880		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1881		       CTLTYPE_INT, "do_loopback_cksum",
1882		       SYSCTL_DESCR("Perform TCP checksum on loopback"),
1883		       NULL, 0, &tcp_do_loopback_cksum, 0,
1884		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOOPBACKCKSUM,
1885		       CTL_EOL);
1886	sysctl_createv(clog, 0, NULL, NULL,
1887		       CTLFLAG_PERMANENT,
1888		       CTLTYPE_STRUCT, "pcblist",
1889		       SYSCTL_DESCR("TCP protocol control block list"),
1890		       sysctl_inpcblist, 0, &tcbtable, 0,
1891		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE,
1892		       CTL_EOL);
1893	sysctl_createv(clog, 0, NULL, NULL,
1894		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1895		       CTLTYPE_INT, "keepinit",
1896		       SYSCTL_DESCR("Ticks before initial tcp connection times out"),
1897		       sysctl_tcp_keep, 0, &tcp_keepinit, 0,
1898		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1899
1900	/* TCP socket buffers auto-sizing nodes */
1901	sysctl_createv(clog, 0, NULL, NULL,
1902		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1903		       CTLTYPE_INT, "recvbuf_auto",
1904		       SYSCTL_DESCR("Enable automatic receive "
1905		           "buffer sizing (experimental)"),
1906		       NULL, 0, &tcp_do_autorcvbuf, 0,
1907		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1908	sysctl_createv(clog, 0, NULL, NULL,
1909		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1910		       CTLTYPE_INT, "recvbuf_inc",
1911		       SYSCTL_DESCR("Incrementor step size of "
1912		           "automatic receive buffer"),
1913		       NULL, 0, &tcp_autorcvbuf_inc, 0,
1914		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1915	sysctl_createv(clog, 0, NULL, NULL,
1916		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1917		       CTLTYPE_INT, "recvbuf_max",
1918		       SYSCTL_DESCR("Max size of automatic receive buffer"),
1919		       NULL, 0, &tcp_autorcvbuf_max, 0,
1920		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1921
1922	sysctl_createv(clog, 0, NULL, NULL,
1923		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1924		       CTLTYPE_INT, "sendbuf_auto",
1925		       SYSCTL_DESCR("Enable automatic send "
1926		           "buffer sizing (experimental)"),
1927		       NULL, 0, &tcp_do_autosndbuf, 0,
1928		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1929	sysctl_createv(clog, 0, NULL, NULL,
1930		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1931		       CTLTYPE_INT, "sendbuf_inc",
1932		       SYSCTL_DESCR("Incrementor step size of "
1933		           "automatic send buffer"),
1934		       NULL, 0, &tcp_autosndbuf_inc, 0,
1935		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1936	sysctl_createv(clog, 0, NULL, NULL,
1937		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1938		       CTLTYPE_INT, "sendbuf_max",
1939		       SYSCTL_DESCR("Max size of automatic send buffer"),
1940		       NULL, 0, &tcp_autosndbuf_max, 0,
1941		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1942
1943	/* ECN subtree */
1944	sysctl_createv(clog, 0, NULL, &ecn_node,
1945	    	       CTLFLAG_PERMANENT,
1946		       CTLTYPE_NODE, "ecn",
1947	    	       SYSCTL_DESCR("RFC3168 Explicit Congestion Notification"),
1948	    	       NULL, 0, NULL, 0,
1949		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1950	sysctl_createv(clog, 0, &ecn_node, NULL,
1951	    	       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1952		       CTLTYPE_INT, "enable",
1953		       SYSCTL_DESCR("Enable TCP Explicit Congestion "
1954			   "Notification"),
1955	    	       NULL, 0, &tcp_do_ecn, 0, CTL_CREATE, CTL_EOL);
1956	sysctl_createv(clog, 0, &ecn_node, NULL,
1957	    	       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1958		       CTLTYPE_INT, "maxretries",
1959		       SYSCTL_DESCR("Number of times to retry ECN setup "
1960			       "before disabling ECN on the connection"),
1961	    	       NULL, 0, &tcp_ecn_maxretries, 0, CTL_CREATE, CTL_EOL);
1962
1963	/* SACK gets it's own little subtree. */
1964	sysctl_createv(clog, 0, NULL, &sack_node,
1965		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1966		       CTLTYPE_INT, "enable",
1967		       SYSCTL_DESCR("Enable RFC2018 Selective ACKnowledgement"),
1968		       NULL, 0, &tcp_do_sack, 0,
1969		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1970	sysctl_createv(clog, 0, NULL, &sack_node,
1971		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1972		       CTLTYPE_INT, "maxholes",
1973		       SYSCTL_DESCR("Maximum number of TCP SACK holes allowed per connection"),
1974		       NULL, 0, &tcp_sack_tp_maxholes, 0,
1975		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1976	sysctl_createv(clog, 0, NULL, &sack_node,
1977		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1978		       CTLTYPE_INT, "globalmaxholes",
1979		       SYSCTL_DESCR("Global maximum number of TCP SACK holes"),
1980		       NULL, 0, &tcp_sack_globalmaxholes, 0,
1981		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1982	sysctl_createv(clog, 0, NULL, &sack_node,
1983		       CTLFLAG_PERMANENT,
1984		       CTLTYPE_INT, "globalholes",
1985		       SYSCTL_DESCR("Global number of TCP SACK holes"),
1986		       NULL, 0, &tcp_sack_globalholes, 0,
1987		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1988
1989	sysctl_createv(clog, 0, NULL, NULL,
1990		       CTLFLAG_PERMANENT,
1991		       CTLTYPE_STRUCT, "stats",
1992		       SYSCTL_DESCR("TCP statistics"),
1993		       sysctl_net_inet_tcp_stats, 0, NULL, 0,
1994		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_STATS,
1995		       CTL_EOL);
1996        sysctl_createv(clog, 0, NULL, NULL,
1997                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1998                       CTLTYPE_INT, "local_by_rtt",
1999                       SYSCTL_DESCR("Use RTT estimator to decide which hosts "
2000				    "are local"),
2001		       NULL, 0, &tcp_rttlocal, 0,
2002		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2003#ifdef TCP_DEBUG
2004	sysctl_createv(clog, 0, NULL, NULL,
2005		       CTLFLAG_PERMANENT,
2006		       CTLTYPE_STRUCT, "debug",
2007		       SYSCTL_DESCR("TCP sockets debug information"),
2008		       NULL, 0, &tcp_debug, sizeof(tcp_debug),
2009		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBUG,
2010		       CTL_EOL);
2011	sysctl_createv(clog, 0, NULL, NULL,
2012		       CTLFLAG_PERMANENT,
2013		       CTLTYPE_INT, "debx",
2014		       SYSCTL_DESCR("Number of TCP debug sockets messages"),
2015		       NULL, 0, &tcp_debx, sizeof(tcp_debx),
2016		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBX,
2017		       CTL_EOL);
2018#endif
2019	sysctl_createv(clog, 0, NULL, NULL,
2020		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2021		       CTLTYPE_STRUCT, "drop",
2022		       SYSCTL_DESCR("TCP drop connection"),
2023		       sysctl_net_inet_tcp_drop, 0, NULL, 0,
2024		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DROP, CTL_EOL);
2025	sysctl_createv(clog, 0, NULL, NULL,
2026		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2027		       CTLTYPE_INT, "iss_hash",
2028		       SYSCTL_DESCR("Enable RFC 1948 ISS by cryptographic "
2029				    "hash computation"),
2030		       NULL, 0, &tcp_do_rfc1948, sizeof(tcp_do_rfc1948),
2031		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE,
2032		       CTL_EOL);
2033
2034	/* ABC subtree */
2035
2036	sysctl_createv(clog, 0, NULL, &abc_node,
2037		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "abc",
2038		       SYSCTL_DESCR("RFC3465 Appropriate Byte Counting (ABC)"),
2039		       NULL, 0, NULL, 0,
2040		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2041	sysctl_createv(clog, 0, &abc_node, NULL,
2042		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2043		       CTLTYPE_INT, "enable",
2044		       SYSCTL_DESCR("Enable RFC3465 Appropriate Byte Counting"),
2045		       NULL, 0, &tcp_do_abc, 0, CTL_CREATE, CTL_EOL);
2046	sysctl_createv(clog, 0, &abc_node, NULL,
2047		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2048		       CTLTYPE_INT, "aggressive",
2049		       SYSCTL_DESCR("1: L=2*SMSS 0: L=1*SMSS"),
2050		       NULL, 0, &tcp_abc_aggressive, 0, CTL_CREATE, CTL_EOL);
2051
2052	/* MSL tuning subtree */
2053
2054	sysctl_createv(clog, 0, NULL, &mslt_node,
2055		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "mslt",
2056		       SYSCTL_DESCR("MSL Tuning for TIME_WAIT truncation"),
2057		       NULL, 0, NULL, 0,
2058		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2059	sysctl_createv(clog, 0, &mslt_node, NULL,
2060		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2061		       CTLTYPE_INT, "enable",
2062		       SYSCTL_DESCR("Enable TIME_WAIT truncation"),
2063		       NULL, 0, &tcp_msl_enable, 0, CTL_CREATE, CTL_EOL);
2064	sysctl_createv(clog, 0, &mslt_node, NULL,
2065		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2066		       CTLTYPE_INT, "loopback",
2067		       SYSCTL_DESCR("MSL value to use for loopback connections"),
2068		       NULL, 0, &tcp_msl_loop, 0, CTL_CREATE, CTL_EOL);
2069	sysctl_createv(clog, 0, &mslt_node, NULL,
2070		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2071		       CTLTYPE_INT, "local",
2072		       SYSCTL_DESCR("MSL value to use for local connections"),
2073		       NULL, 0, &tcp_msl_local, 0, CTL_CREATE, CTL_EOL);
2074	sysctl_createv(clog, 0, &mslt_node, NULL,
2075		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2076		       CTLTYPE_INT, "remote",
2077		       SYSCTL_DESCR("MSL value to use for remote connections"),
2078		       NULL, 0, &tcp_msl_remote, 0, CTL_CREATE, CTL_EOL);
2079	sysctl_createv(clog, 0, &mslt_node, NULL,
2080		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2081		       CTLTYPE_INT, "remote_threshold",
2082		       SYSCTL_DESCR("RTT estimate value to promote local to remote"),
2083		       NULL, 0, &tcp_msl_remote_threshold, 0, CTL_CREATE, CTL_EOL);
2084
2085	/* vestigial TIME_WAIT tuning subtree */
2086
2087	sysctl_createv(clog, 0, NULL, &vtw_node,
2088		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "vtw",
2089		       SYSCTL_DESCR("Tuning for Vestigial TIME_WAIT"),
2090		       NULL, 0, NULL, 0,
2091		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2092	sysctl_createv(clog, 0, &vtw_node, NULL,
2093		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2094		       CTLTYPE_INT, "enable",
2095		       SYSCTL_DESCR("Enable Vestigial TIME_WAIT"),
2096		       sysctl_tcp_vtw_enable, 0,
2097	               (pf == AF_INET) ? &tcp4_vtw_enable : &tcp6_vtw_enable,
2098		       0, CTL_CREATE, CTL_EOL);
2099	sysctl_createv(clog, 0, &vtw_node, NULL,
2100		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
2101		       CTLTYPE_INT, "entries",
2102		       SYSCTL_DESCR("Maximum number of vestigial TIME_WAIT entries"),
2103		       NULL, 0, &tcp_vtw_entries, 0, CTL_CREATE, CTL_EOL);
2104}
2105
2106void
2107tcp_usrreq_init(void)
2108{
2109
2110#ifdef INET
2111	sysctl_net_inet_tcp_setup2(NULL, PF_INET, "inet", "tcp");
2112#endif
2113#ifdef INET6
2114	sysctl_net_inet_tcp_setup2(NULL, PF_INET6, "inet6", "tcp6");
2115#endif
2116}
2117