xref: /netbsd-src/sys/netinet/tcp_usrreq.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: tcp_usrreq.c,v 1.209 2015/08/24 22:21:26 pooka Exp $	*/
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 /*
98  * TCP protocol interface to socket abstraction.
99  */
100 
101 #include <sys/cdefs.h>
102 __KERNEL_RCSID(0, "$NetBSD: tcp_usrreq.c,v 1.209 2015/08/24 22:21:26 pooka Exp $");
103 
104 #ifdef _KERNEL_OPT
105 #include "opt_inet.h"
106 #include "opt_tcp_debug.h"
107 #include "opt_mbuftrace.h"
108 #include "opt_tcp_space.h"
109 #endif
110 
111 #include <sys/param.h>
112 #include <sys/systm.h>
113 #include <sys/kernel.h>
114 #include <sys/mbuf.h>
115 #include <sys/socket.h>
116 #include <sys/socketvar.h>
117 #include <sys/protosw.h>
118 #include <sys/errno.h>
119 #include <sys/stat.h>
120 #include <sys/proc.h>
121 #include <sys/domain.h>
122 #include <sys/sysctl.h>
123 #include <sys/kauth.h>
124 #include <sys/kernel.h>
125 #include <sys/uidinfo.h>
126 
127 #include <net/if.h>
128 #include <net/route.h>
129 
130 #include <netinet/in.h>
131 #include <netinet/in_systm.h>
132 #include <netinet/in_var.h>
133 #include <netinet/ip.h>
134 #include <netinet/in_pcb.h>
135 #include <netinet/ip_var.h>
136 #include <netinet/in_offload.h>
137 
138 #ifdef INET6
139 #ifndef INET
140 #include <netinet/in.h>
141 #endif
142 #include <netinet/ip6.h>
143 #include <netinet6/in6_pcb.h>
144 #include <netinet6/ip6_var.h>
145 #include <netinet6/scope6_var.h>
146 #endif
147 
148 #include <netinet/tcp.h>
149 #include <netinet/tcp_fsm.h>
150 #include <netinet/tcp_seq.h>
151 #include <netinet/tcp_timer.h>
152 #include <netinet/tcp_var.h>
153 #include <netinet/tcp_private.h>
154 #include <netinet/tcp_congctl.h>
155 #include <netinet/tcpip.h>
156 #include <netinet/tcp_debug.h>
157 #include <netinet/tcp_vtw.h>
158 
159 static int
160 tcp_debug_capture(struct tcpcb *tp, int req)
161 {
162 #ifdef KPROF
163 	tcp_acounts[tp->t_state][req]++;
164 #endif
165 #ifdef TCP_DEBUG
166 	return tp->t_state;
167 #endif
168 	return 0;
169 }
170 
171 static inline void
172 tcp_debug_trace(struct socket *so, struct tcpcb *tp, int ostate, int req)
173 {
174 #ifdef TCP_DEBUG
175 	if (tp && (so->so_options & SO_DEBUG))
176 		tcp_trace(TA_USER, ostate, tp, NULL, req);
177 #endif
178 }
179 
180 static int
181 tcp_getpcb(struct socket *so, struct inpcb **inp,
182     struct in6pcb **in6p, struct tcpcb **tp)
183 {
184 
185 	KASSERT(solocked(so));
186 
187 	/*
188 	 * When a TCP is attached to a socket, then there will be
189 	 * a (struct inpcb) pointed at by the socket, and this
190 	 * structure will point at a subsidary (struct tcpcb).
191 	 */
192 	switch (so->so_proto->pr_domain->dom_family) {
193 #ifdef INET
194 	case PF_INET:
195 		*inp = sotoinpcb(so);
196 		if (*inp == NULL)
197 			return EINVAL;
198 		*tp = intotcpcb(*inp);
199 		break;
200 #endif
201 #ifdef INET6
202 	case PF_INET6:
203 		*in6p = sotoin6pcb(so);
204 		if (*in6p == NULL)
205 			return EINVAL;
206 		*tp = in6totcpcb(*in6p);
207 		break;
208 #endif
209 	default:
210 		return EAFNOSUPPORT;
211 	}
212 
213 	KASSERT(tp != NULL);
214 
215 	return 0;
216 }
217 
218 static void
219 change_keepalive(struct socket *so, struct tcpcb *tp)
220 {
221 	tp->t_maxidle = tp->t_keepcnt * tp->t_keepintvl;
222 	TCP_TIMER_DISARM(tp, TCPT_KEEP);
223 	TCP_TIMER_DISARM(tp, TCPT_2MSL);
224 
225 	if (tp->t_state == TCPS_SYN_RECEIVED ||
226 	    tp->t_state == TCPS_SYN_SENT) {
227 		TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepinit);
228 	} else if (so->so_options & SO_KEEPALIVE &&
229 	    tp->t_state <= TCPS_CLOSE_WAIT) {
230 		TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepintvl);
231 	} else {
232 		TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepidle);
233 	}
234 
235 	if ((tp->t_state == TCPS_FIN_WAIT_2) && (tp->t_maxidle > 0))
236 		TCP_TIMER_ARM(tp, TCPT_2MSL, tp->t_maxidle);
237 }
238 
239 /*
240  * Export TCP internal state information via a struct tcp_info, based on the
241  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
242  * (TCP state machine, etc).  We export all information using FreeBSD-native
243  * constants -- for example, the numeric values for tcpi_state will differ
244  * from Linux.
245  */
246 static void
247 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
248 {
249 
250 	bzero(ti, sizeof(*ti));
251 
252 	ti->tcpi_state = tp->t_state;
253 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
254 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
255 	if (tp->t_flags & TF_SACK_PERMIT)
256 		ti->tcpi_options |= TCPI_OPT_SACK;
257 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
258 		ti->tcpi_options |= TCPI_OPT_WSCALE;
259 		ti->tcpi_snd_wscale = tp->snd_scale;
260 		ti->tcpi_rcv_wscale = tp->rcv_scale;
261 	}
262 	if (tp->t_flags & TF_ECN_PERMIT) {
263 		ti->tcpi_options |= TCPI_OPT_ECN;
264 	}
265 
266 	ti->tcpi_rto = tp->t_rxtcur * tick;
267 	ti->tcpi_last_data_recv = (long)(hardclock_ticks -
268 					 (int)tp->t_rcvtime) * tick;
269 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
270 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
271 
272 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
273 	/* Linux API wants these in # of segments, apparently */
274 	ti->tcpi_snd_cwnd = tp->snd_cwnd / tp->t_segsz;
275 	ti->tcpi_snd_wnd = tp->snd_wnd / tp->t_segsz;
276 
277 	/*
278 	 * FreeBSD-specific extension fields for tcp_info.
279 	 */
280 	ti->tcpi_rcv_space = tp->rcv_wnd;
281 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
282 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
283 	ti->tcpi_snd_nxt = tp->snd_nxt;
284 	ti->tcpi_snd_mss = tp->t_segsz;
285 	ti->tcpi_rcv_mss = tp->t_segsz;
286 #ifdef TF_TOE
287 	if (tp->t_flags & TF_TOE)
288 		ti->tcpi_options |= TCPI_OPT_TOE;
289 #endif
290 	/* From the redundant department of redundancies... */
291 	ti->__tcpi_retransmits = ti->__tcpi_retrans =
292 		ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
293 
294 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
295 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
296 }
297 
298 int
299 tcp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
300 {
301 	int error = 0, s;
302 	struct inpcb *inp;
303 #ifdef INET6
304 	struct in6pcb *in6p;
305 #endif
306 	struct tcpcb *tp;
307 	struct tcp_info ti;
308 	u_int ui;
309 	int family;	/* family of the socket */
310 	int level, optname, optval;
311 
312 	level = sopt->sopt_level;
313 	optname = sopt->sopt_name;
314 
315 	family = so->so_proto->pr_domain->dom_family;
316 
317 	s = splsoftnet();
318 	switch (family) {
319 #ifdef INET
320 	case PF_INET:
321 		inp = sotoinpcb(so);
322 #ifdef INET6
323 		in6p = NULL;
324 #endif
325 		break;
326 #endif
327 #ifdef INET6
328 	case PF_INET6:
329 		inp = NULL;
330 		in6p = sotoin6pcb(so);
331 		break;
332 #endif
333 	default:
334 		splx(s);
335 		panic("%s: af %d", __func__, family);
336 	}
337 #ifndef INET6
338 	if (inp == NULL)
339 #else
340 	if (inp == NULL && in6p == NULL)
341 #endif
342 	{
343 		splx(s);
344 		return (ECONNRESET);
345 	}
346 	if (level != IPPROTO_TCP) {
347 		switch (family) {
348 #ifdef INET
349 		case PF_INET:
350 			error = ip_ctloutput(op, so, sopt);
351 			break;
352 #endif
353 #ifdef INET6
354 		case PF_INET6:
355 			error = ip6_ctloutput(op, so, sopt);
356 			break;
357 #endif
358 		}
359 		splx(s);
360 		return (error);
361 	}
362 	if (inp)
363 		tp = intotcpcb(inp);
364 #ifdef INET6
365 	else if (in6p)
366 		tp = in6totcpcb(in6p);
367 #endif
368 	else
369 		tp = NULL;
370 
371 	switch (op) {
372 	case PRCO_SETOPT:
373 		switch (optname) {
374 #ifdef TCP_SIGNATURE
375 		case TCP_MD5SIG:
376 			error = sockopt_getint(sopt, &optval);
377 			if (error)
378 				break;
379 			if (optval > 0)
380 				tp->t_flags |= TF_SIGNATURE;
381 			else
382 				tp->t_flags &= ~TF_SIGNATURE;
383 			break;
384 #endif /* TCP_SIGNATURE */
385 
386 		case TCP_NODELAY:
387 			error = sockopt_getint(sopt, &optval);
388 			if (error)
389 				break;
390 			if (optval)
391 				tp->t_flags |= TF_NODELAY;
392 			else
393 				tp->t_flags &= ~TF_NODELAY;
394 			break;
395 
396 		case TCP_MAXSEG:
397 			error = sockopt_getint(sopt, &optval);
398 			if (error)
399 				break;
400 			if (optval > 0 && optval <= tp->t_peermss)
401 				tp->t_peermss = optval; /* limit on send size */
402 			else
403 				error = EINVAL;
404 			break;
405 #ifdef notyet
406 		case TCP_CONGCTL:
407 			/* XXX string overflow XXX */
408 			error = tcp_congctl_select(tp, sopt->sopt_data);
409 			break;
410 #endif
411 
412 		case TCP_KEEPIDLE:
413 			error = sockopt_get(sopt, &ui, sizeof(ui));
414 			if (error)
415 				break;
416 			if (ui > 0) {
417 				tp->t_keepidle = ui;
418 				change_keepalive(so, tp);
419 			} else
420 				error = EINVAL;
421 			break;
422 
423 		case TCP_KEEPINTVL:
424 			error = sockopt_get(sopt, &ui, sizeof(ui));
425 			if (error)
426 				break;
427 			if (ui > 0) {
428 				tp->t_keepintvl = ui;
429 				change_keepalive(so, tp);
430 			} else
431 				error = EINVAL;
432 			break;
433 
434 		case TCP_KEEPCNT:
435 			error = sockopt_get(sopt, &ui, sizeof(ui));
436 			if (error)
437 				break;
438 			if (ui > 0) {
439 				tp->t_keepcnt = ui;
440 				change_keepalive(so, tp);
441 			} else
442 				error = EINVAL;
443 			break;
444 
445 		case TCP_KEEPINIT:
446 			error = sockopt_get(sopt, &ui, sizeof(ui));
447 			if (error)
448 				break;
449 			if (ui > 0) {
450 				tp->t_keepinit = ui;
451 				change_keepalive(so, tp);
452 			} else
453 				error = EINVAL;
454 			break;
455 
456 		default:
457 			error = ENOPROTOOPT;
458 			break;
459 		}
460 		break;
461 
462 	case PRCO_GETOPT:
463 		switch (optname) {
464 #ifdef TCP_SIGNATURE
465 		case TCP_MD5SIG:
466 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
467 			error = sockopt_set(sopt, &optval, sizeof(optval));
468 			break;
469 #endif
470 		case TCP_NODELAY:
471 			optval = tp->t_flags & TF_NODELAY;
472 			error = sockopt_set(sopt, &optval, sizeof(optval));
473 			break;
474 		case TCP_MAXSEG:
475 			optval = tp->t_peermss;
476 			error = sockopt_set(sopt, &optval, sizeof(optval));
477 			break;
478 		case TCP_INFO:
479 			tcp_fill_info(tp, &ti);
480 			error = sockopt_set(sopt, &ti, sizeof ti);
481 			break;
482 #ifdef notyet
483 		case TCP_CONGCTL:
484 			break;
485 #endif
486 		default:
487 			error = ENOPROTOOPT;
488 			break;
489 		}
490 		break;
491 	}
492 	splx(s);
493 	return (error);
494 }
495 
496 #ifndef TCP_SENDSPACE
497 #define	TCP_SENDSPACE	1024*32
498 #endif
499 int	tcp_sendspace = TCP_SENDSPACE;
500 #ifndef TCP_RECVSPACE
501 #define	TCP_RECVSPACE	1024*32
502 #endif
503 int	tcp_recvspace = TCP_RECVSPACE;
504 
505 /*
506  * tcp_attach: attach TCP protocol to socket, allocating internet protocol
507  * control block, TCP control block, buffer space and entering LISTEN state
508  * if to accept connections.
509  */
510 static int
511 tcp_attach(struct socket *so, int proto)
512 {
513 	struct tcpcb *tp;
514 	struct inpcb *inp;
515 #ifdef INET6
516 	struct in6pcb *in6p;
517 #endif
518 	int s, error, family;
519 
520 	/* Assign the lock (must happen even if we will error out). */
521 	s = splsoftnet();
522 	sosetlock(so);
523 	KASSERT(solocked(so));
524 
525 	family = so->so_proto->pr_domain->dom_family;
526 	switch (family) {
527 #ifdef INET
528 	case PF_INET:
529 		inp = sotoinpcb(so);
530 #ifdef INET6
531 		in6p = NULL;
532 #endif
533 		break;
534 #endif
535 #ifdef INET6
536 	case PF_INET6:
537 		inp = NULL;
538 		in6p = sotoin6pcb(so);
539 		break;
540 #endif
541 	default:
542 		error = EAFNOSUPPORT;
543 		goto out;
544 	}
545 
546 	KASSERT(inp == NULL);
547 #ifdef INET6
548 	KASSERT(in6p == NULL);
549 #endif
550 
551 #ifdef MBUFTRACE
552 	so->so_mowner = &tcp_sock_mowner;
553 	so->so_rcv.sb_mowner = &tcp_sock_rx_mowner;
554 	so->so_snd.sb_mowner = &tcp_sock_tx_mowner;
555 #endif
556 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
557 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
558 		if (error)
559 			goto out;
560 	}
561 
562 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
563 	so->so_snd.sb_flags |= SB_AUTOSIZE;
564 
565 	switch (family) {
566 #ifdef INET
567 	case PF_INET:
568 		error = in_pcballoc(so, &tcbtable);
569 		if (error)
570 			goto out;
571 		inp = sotoinpcb(so);
572 #ifdef INET6
573 		in6p = NULL;
574 #endif
575 		break;
576 #endif
577 #ifdef INET6
578 	case PF_INET6:
579 		error = in6_pcballoc(so, &tcbtable);
580 		if (error)
581 			goto out;
582 		inp = NULL;
583 		in6p = sotoin6pcb(so);
584 		break;
585 #endif
586 	default:
587 		error = EAFNOSUPPORT;
588 		goto out;
589 	}
590 	if (inp)
591 		tp = tcp_newtcpcb(family, (void *)inp);
592 #ifdef INET6
593 	else if (in6p)
594 		tp = tcp_newtcpcb(family, (void *)in6p);
595 #endif
596 	else
597 		tp = NULL;
598 
599 	if (tp == NULL) {
600 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
601 
602 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
603 #ifdef INET
604 		if (inp)
605 			in_pcbdetach(inp);
606 #endif
607 #ifdef INET6
608 		if (in6p)
609 			in6_pcbdetach(in6p);
610 #endif
611 		so->so_state |= nofd;
612 		error = ENOBUFS;
613 		goto out;
614 	}
615 	tp->t_state = TCPS_CLOSED;
616 	if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
617 		so->so_linger = TCP_LINGERTIME;
618 	}
619 out:
620 	KASSERT(solocked(so));
621 	splx(s);
622 	return error;
623 }
624 
625 static void
626 tcp_detach(struct socket *so)
627 {
628 	struct inpcb *inp = NULL;
629 	struct in6pcb *in6p = NULL;
630 	struct tcpcb *tp = NULL;
631 	int s;
632 
633 	if (tcp_getpcb(so, &inp, &in6p, &tp) != 0)
634 		return;
635 
636 	s = splsoftnet();
637 	(void)tcp_disconnect1(tp);
638 	splx(s);
639 }
640 
641 static int
642 tcp_accept(struct socket *so, struct sockaddr *nam)
643 {
644 	struct inpcb *inp = NULL;
645 	struct in6pcb *in6p = NULL;
646 	struct tcpcb *tp = NULL;
647 	int ostate = 0;
648 	int error = 0;
649 	int s;
650 
651 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
652 		return error;
653 
654 	ostate = tcp_debug_capture(tp, PRU_ACCEPT);
655 
656 	/*
657 	 * Accept a connection.  Essentially all the work is
658 	 * done at higher levels; just return the address
659 	 * of the peer, storing through addr.
660 	 */
661 	s = splsoftnet();
662 #ifdef INET
663 	if (inp) {
664 		in_setpeeraddr(inp, (struct sockaddr_in *)nam);
665 	}
666 #endif
667 #ifdef INET6
668 	if (in6p) {
669 		in6_setpeeraddr(in6p, (struct sockaddr_in6 *)nam);
670 	}
671 #endif
672 	tcp_debug_trace(so, tp, ostate, PRU_ACCEPT);
673 	splx(s);
674 
675 	return 0;
676 }
677 
678 static int
679 tcp_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
680 {
681 	struct inpcb *inp = NULL;
682 	struct in6pcb *in6p = NULL;
683 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
684 #ifdef INET6
685 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
686 #endif /* INET6 */
687 	struct tcpcb *tp = NULL;
688 	int s;
689 	int error = 0;
690 	int ostate = 0;
691 
692 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
693 		return error;
694 
695 	ostate = tcp_debug_capture(tp, PRU_BIND);
696 
697 	/*
698 	 * Give the socket an address.
699 	 */
700 	s = splsoftnet();
701 	switch (so->so_proto->pr_domain->dom_family) {
702 #ifdef INET
703 	case PF_INET:
704 		error = in_pcbbind(inp, sin, l);
705 		break;
706 #endif
707 #ifdef INET6
708 	case PF_INET6:
709 		error = in6_pcbbind(in6p, sin6, l);
710 		if (!error) {
711 			/* mapped addr case */
712 			if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
713 				tp->t_family = AF_INET;
714 			else
715 				tp->t_family = AF_INET6;
716 		}
717 		break;
718 #endif
719 	}
720 	tcp_debug_trace(so, tp, ostate, PRU_BIND);
721 	splx(s);
722 
723 	return error;
724 }
725 
726 static int
727 tcp_listen(struct socket *so, struct lwp *l)
728 {
729 	struct inpcb *inp = NULL;
730 	struct in6pcb *in6p = NULL;
731 	struct tcpcb *tp = NULL;
732 	int error = 0;
733 	int ostate = 0;
734 	int s;
735 
736 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
737 		return error;
738 
739 	ostate = tcp_debug_capture(tp, PRU_LISTEN);
740 
741 	/*
742 	 * Prepare to accept connections.
743 	 */
744 	s = splsoftnet();
745 #ifdef INET
746 	if (inp && inp->inp_lport == 0) {
747 		error = in_pcbbind(inp, NULL, l);
748 		if (error)
749 			goto release;
750 	}
751 #endif
752 #ifdef INET6
753 	if (in6p && in6p->in6p_lport == 0) {
754 		error = in6_pcbbind(in6p, NULL, l);
755 		if (error)
756 			goto release;
757 	}
758 #endif
759 	tp->t_state = TCPS_LISTEN;
760 
761 release:
762 	tcp_debug_trace(so, tp, ostate, PRU_LISTEN);
763 	splx(s);
764 
765 	return error;
766 }
767 
768 static int
769 tcp_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
770 {
771 	struct inpcb *inp = NULL;
772 	struct in6pcb *in6p = NULL;
773 	struct tcpcb *tp = NULL;
774 	int s;
775 	int error = 0;
776 	int ostate = 0;
777 
778 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
779 		return error;
780 
781 	ostate = tcp_debug_capture(tp, PRU_CONNECT);
782 
783 	/*
784 	 * Initiate connection to peer.
785 	 * Create a template for use in transmissions on this connection.
786 	 * Enter SYN_SENT state, and mark socket as connecting.
787 	 * Start keep-alive timer, and seed output sequence space.
788 	 * Send initial segment on connection.
789 	 */
790 	s = splsoftnet();
791 #ifdef INET
792 	if (inp) {
793 		if (inp->inp_lport == 0) {
794 			error = in_pcbbind(inp, NULL, l);
795 			if (error)
796 				goto release;
797 		}
798 		error = in_pcbconnect(inp, (struct sockaddr_in *)nam, l);
799 	}
800 #endif
801 #ifdef INET6
802 	if (in6p) {
803 		if (in6p->in6p_lport == 0) {
804 			error = in6_pcbbind(in6p, NULL, l);
805 			if (error)
806 				goto release;
807 		}
808 		error = in6_pcbconnect(in6p, (struct sockaddr_in6 *)nam, l);
809 		if (!error) {
810 			/* mapped addr case */
811 			if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
812 				tp->t_family = AF_INET;
813 			else
814 				tp->t_family = AF_INET6;
815 		}
816 	}
817 #endif
818 	if (error)
819 		goto release;
820 	tp->t_template = tcp_template(tp);
821 	if (tp->t_template == 0) {
822 #ifdef INET
823 		if (inp)
824 			in_pcbdisconnect(inp);
825 #endif
826 #ifdef INET6
827 		if (in6p)
828 			in6_pcbdisconnect(in6p);
829 #endif
830 		error = ENOBUFS;
831 		goto release;
832 	}
833 	/*
834 	 * Compute window scaling to request.
835 	 * XXX: This should be moved to tcp_output().
836 	 */
837 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
838 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
839 		tp->request_r_scale++;
840 	soisconnecting(so);
841 	TCP_STATINC(TCP_STAT_CONNATTEMPT);
842 	tp->t_state = TCPS_SYN_SENT;
843 	TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepinit);
844 	tp->iss = tcp_new_iss(tp, 0);
845 	tcp_sendseqinit(tp);
846 	error = tcp_output(tp);
847 
848 release:
849 	tcp_debug_trace(so, tp, ostate, PRU_CONNECT);
850 	splx(s);
851 
852 	return error;
853 }
854 
855 static int
856 tcp_connect2(struct socket *so, struct socket *so2)
857 {
858 	struct inpcb *inp = NULL;
859 	struct in6pcb *in6p = NULL;
860 	struct tcpcb *tp = NULL;
861 	int error = 0;
862 	int ostate = 0;
863 
864 	KASSERT(solocked(so));
865 
866 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
867 		return error;
868 
869 	ostate = tcp_debug_capture(tp, PRU_CONNECT2);
870 
871 	tcp_debug_trace(so, tp, ostate, PRU_CONNECT2);
872 
873 	return EOPNOTSUPP;
874 }
875 
876 static int
877 tcp_disconnect(struct socket *so)
878 {
879 	struct inpcb *inp = NULL;
880 	struct in6pcb *in6p = NULL;
881 	struct tcpcb *tp = NULL;
882 	int error = 0;
883 	int ostate = 0;
884 	int s;
885 
886 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
887 		return error;
888 
889 	ostate = tcp_debug_capture(tp, PRU_DISCONNECT);
890 
891 	/*
892 	 * Initiate disconnect from peer.
893 	 * If connection never passed embryonic stage, just drop;
894 	 * else if don't need to let data drain, then can just drop anyways,
895 	 * else have to begin TCP shutdown process: mark socket disconnecting,
896 	 * drain unread data, state switch to reflect user close, and
897 	 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
898 	 * when peer sends FIN and acks ours.
899 	 *
900 	 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
901 	 */
902 	s = splsoftnet();
903 	tp = tcp_disconnect1(tp);
904 	tcp_debug_trace(so, tp, ostate, PRU_DISCONNECT);
905 	splx(s);
906 
907 	return error;
908 }
909 
910 static int
911 tcp_shutdown(struct socket *so)
912 {
913 	struct inpcb *inp = NULL;
914 	struct in6pcb *in6p = NULL;
915 	struct tcpcb *tp = NULL;
916 	int error = 0;
917 	int ostate = 0;
918 	int s;
919 
920 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
921 		return error;
922 
923 	ostate = tcp_debug_capture(tp, PRU_SHUTDOWN);
924 	/*
925 	 * Mark the connection as being incapable of further output.
926 	 */
927 	s = splsoftnet();
928 	socantsendmore(so);
929 	tp = tcp_usrclosed(tp);
930 	if (tp)
931 		error = tcp_output(tp);
932 	tcp_debug_trace(so, tp, ostate, PRU_SHUTDOWN);
933 	splx(s);
934 
935 	return error;
936 }
937 
938 static int
939 tcp_abort(struct socket *so)
940 {
941 	struct inpcb *inp = NULL;
942 	struct in6pcb *in6p = NULL;
943 	struct tcpcb *tp = NULL;
944 	int error = 0;
945 	int ostate = 0;
946 	int s;
947 
948 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
949 		return error;
950 
951 	ostate = tcp_debug_capture(tp, PRU_ABORT);
952 
953 	/*
954 	 * Abort the TCP.
955 	 */
956 	s = splsoftnet();
957 	tp = tcp_drop(tp, ECONNABORTED);
958 	tcp_debug_trace(so, tp, ostate, PRU_ABORT);
959 	splx(s);
960 
961 	return error;
962 }
963 
964 static int
965 tcp_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
966 {
967 	switch (so->so_proto->pr_domain->dom_family) {
968 #ifdef INET
969 	case PF_INET:
970 		return in_control(so, cmd, nam, ifp);
971 #endif
972 #ifdef INET6
973 	case PF_INET6:
974 		return in6_control(so, cmd, nam, ifp);
975 #endif
976 	default:
977 		return EAFNOSUPPORT;
978 	}
979 }
980 
981 static int
982 tcp_stat(struct socket *so, struct stat *ub)
983 {
984 	KASSERT(solocked(so));
985 
986 	/* stat: don't bother with a blocksize.  */
987 	return 0;
988 }
989 
990 static int
991 tcp_peeraddr(struct socket *so, struct sockaddr *nam)
992 {
993 	struct inpcb *inp = NULL;
994 	struct in6pcb *in6p = NULL;
995 	struct tcpcb *tp = NULL;
996 	int ostate = 0;
997 	int error = 0;
998 	int s;
999 
1000 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
1001 		return error;
1002 
1003 	ostate = tcp_debug_capture(tp, PRU_PEERADDR);
1004 
1005 	s = splsoftnet();
1006 #ifdef INET
1007 	if (inp) {
1008 		in_setpeeraddr(inp, (struct sockaddr_in *)nam);
1009 	}
1010 #endif
1011 #ifdef INET6
1012 	if (in6p) {
1013 		in6_setpeeraddr(in6p, (struct sockaddr_in6 *)nam);
1014 	}
1015 #endif
1016 	tcp_debug_trace(so, tp, ostate, PRU_PEERADDR);
1017 	splx(s);
1018 
1019 	return 0;
1020 }
1021 
1022 static int
1023 tcp_sockaddr(struct socket *so, struct sockaddr *nam)
1024 {
1025 	struct inpcb *inp = NULL;
1026 	struct in6pcb *in6p = NULL;
1027 	struct tcpcb *tp = NULL;
1028 	int ostate = 0;
1029 	int error = 0;
1030 	int s;
1031 
1032 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
1033 		return error;
1034 
1035 	ostate = tcp_debug_capture(tp, PRU_SOCKADDR);
1036 
1037 	s = splsoftnet();
1038 #ifdef INET
1039 	if (inp) {
1040 		in_setsockaddr(inp, (struct sockaddr_in *)nam);
1041 	}
1042 #endif
1043 #ifdef INET6
1044 	if (in6p) {
1045 		in6_setsockaddr(in6p, (struct sockaddr_in6 *)nam);
1046 	}
1047 #endif
1048 	tcp_debug_trace(so, tp, ostate, PRU_SOCKADDR);
1049 	splx(s);
1050 
1051 	return 0;
1052 }
1053 
1054 static int
1055 tcp_rcvd(struct socket *so, int flags, struct lwp *l)
1056 {
1057 	struct inpcb *inp = NULL;
1058 	struct in6pcb *in6p = NULL;
1059 	struct tcpcb *tp = NULL;
1060 	int ostate = 0;
1061 	int error = 0;
1062 	int s;
1063 
1064 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
1065 		return error;
1066 
1067 	ostate = tcp_debug_capture(tp, PRU_RCVD);
1068 
1069 	/*
1070 	 * After a receive, possibly send window update to peer.
1071 	 *
1072 	 * soreceive() calls this function when a user receives
1073 	 * ancillary data on a listening socket. We don't call
1074 	 * tcp_output in such a case, since there is no header
1075 	 * template for a listening socket and hence the kernel
1076 	 * will panic.
1077 	 */
1078 	s = splsoftnet();
1079 	if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) != 0)
1080 		(void) tcp_output(tp);
1081 	splx(s);
1082 
1083 	tcp_debug_trace(so, tp, ostate, PRU_RCVD);
1084 
1085 	return 0;
1086 }
1087 
1088 static int
1089 tcp_recvoob(struct socket *so, struct mbuf *m, int flags)
1090 {
1091 	struct inpcb *inp = NULL;
1092 	struct in6pcb *in6p = NULL;
1093 	struct tcpcb *tp = NULL;
1094 	int ostate = 0;
1095 	int error = 0;
1096 	int s;
1097 
1098 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
1099 		return error;
1100 
1101 	ostate = tcp_debug_capture(tp, PRU_RCVOOB);
1102 
1103 	s = splsoftnet();
1104 	if ((so->so_oobmark == 0 &&
1105 	    (so->so_state & SS_RCVATMARK) == 0) ||
1106 	    so->so_options & SO_OOBINLINE ||
1107 	    tp->t_oobflags & TCPOOB_HADDATA) {
1108 		splx(s);
1109 		return EINVAL;
1110 	}
1111 
1112 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1113 		splx(s);
1114 		return EWOULDBLOCK;
1115 	}
1116 
1117 	m->m_len = 1;
1118 	*mtod(m, char *) = tp->t_iobc;
1119 	if ((flags & MSG_PEEK) == 0)
1120 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1121 
1122 	tcp_debug_trace(so, tp, ostate, PRU_RCVOOB);
1123 	splx(s);
1124 
1125 	return 0;
1126 }
1127 
1128 static int
1129 tcp_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
1130     struct mbuf *control, struct lwp *l)
1131 {
1132 	struct inpcb *inp = NULL;
1133 	struct in6pcb *in6p = NULL;
1134 	struct tcpcb *tp = NULL;
1135 	int ostate = 0;
1136 	int error = 0;
1137 	int s;
1138 
1139 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
1140 		return error;
1141 
1142 	ostate = tcp_debug_capture(tp, PRU_SEND);
1143 
1144 	/*
1145 	 * Do a send by putting data in output queue and updating urgent
1146 	 * marker if URG set.  Possibly send more data.
1147 	 */
1148 	s = splsoftnet();
1149 	if (control && control->m_len) {
1150 		m_freem(control);
1151 		m_freem(m);
1152 		tcp_debug_trace(so, tp, ostate, PRU_SEND);
1153 		splx(s);
1154 		return EINVAL;
1155 	}
1156 
1157 	sbappendstream(&so->so_snd, m);
1158 	error = tcp_output(tp);
1159 	tcp_debug_trace(so, tp, ostate, PRU_SEND);
1160 	splx(s);
1161 
1162 	return error;
1163 }
1164 
1165 static int
1166 tcp_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
1167 {
1168 	struct inpcb *inp = NULL;
1169 	struct in6pcb *in6p = NULL;
1170 	struct tcpcb *tp = NULL;
1171 	int ostate = 0;
1172 	int error = 0;
1173 	int s;
1174 
1175 	if ((error = tcp_getpcb(so, &inp, &in6p, &tp)) != 0)
1176 		return error;
1177 
1178 	ostate = tcp_debug_capture(tp, PRU_SENDOOB);
1179 
1180 	s = splsoftnet();
1181 	if (sbspace(&so->so_snd) < -512) {
1182 		m_freem(m);
1183 		splx(s);
1184 		return ENOBUFS;
1185 	}
1186 	/*
1187 	 * According to RFC961 (Assigned Protocols),
1188 	 * the urgent pointer points to the last octet
1189 	 * of urgent data.  We continue, however,
1190 	 * to consider it to indicate the first octet
1191 	 * of data past the urgent section.
1192 	 * Otherwise, snd_up should be one lower.
1193 	 */
1194 	sbappendstream(&so->so_snd, m);
1195 	tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
1196 	tp->t_force = 1;
1197 	error = tcp_output(tp);
1198 	tp->t_force = 0;
1199 	tcp_debug_trace(so, tp, ostate, PRU_SENDOOB);
1200 	splx(s);
1201 
1202 	return error;
1203 }
1204 
1205 static int
1206 tcp_purgeif(struct socket *so, struct ifnet *ifp)
1207 {
1208 	int s;
1209 
1210 	s = splsoftnet();
1211 	mutex_enter(softnet_lock);
1212 	switch (so->so_proto->pr_domain->dom_family) {
1213 #ifdef INET
1214 	case PF_INET:
1215 		in_pcbpurgeif0(&tcbtable, ifp);
1216 		in_purgeif(ifp);
1217 		in_pcbpurgeif(&tcbtable, ifp);
1218 		break;
1219 #endif
1220 #ifdef INET6
1221 	case PF_INET6:
1222 		in6_pcbpurgeif0(&tcbtable, ifp);
1223 		in6_purgeif(ifp);
1224 		in6_pcbpurgeif(&tcbtable, ifp);
1225 		break;
1226 #endif
1227 	default:
1228 		mutex_exit(softnet_lock);
1229 		splx(s);
1230 		return EAFNOSUPPORT;
1231 	}
1232 	mutex_exit(softnet_lock);
1233 	splx(s);
1234 
1235 	return 0;
1236 }
1237 
1238 /*
1239  * Initiate (or continue) disconnect.
1240  * If embryonic state, just send reset (once).
1241  * If in ``let data drain'' option and linger null, just drop.
1242  * Otherwise (hard), mark socket disconnecting and drop
1243  * current input data; switch states based on user close, and
1244  * send segment to peer (with FIN).
1245  */
1246 struct tcpcb *
1247 tcp_disconnect1(struct tcpcb *tp)
1248 {
1249 	struct socket *so;
1250 
1251 	if (tp->t_inpcb)
1252 		so = tp->t_inpcb->inp_socket;
1253 #ifdef INET6
1254 	else if (tp->t_in6pcb)
1255 		so = tp->t_in6pcb->in6p_socket;
1256 #endif
1257 	else
1258 		so = NULL;
1259 
1260 	if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
1261 		tp = tcp_close(tp);
1262 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1263 		tp = tcp_drop(tp, 0);
1264 	else {
1265 		soisdisconnecting(so);
1266 		sbflush(&so->so_rcv);
1267 		tp = tcp_usrclosed(tp);
1268 		if (tp)
1269 			(void) tcp_output(tp);
1270 	}
1271 	return (tp);
1272 }
1273 
1274 /*
1275  * User issued close, and wish to trail through shutdown states:
1276  * if never received SYN, just forget it.  If got a SYN from peer,
1277  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1278  * If already got a FIN from peer, then almost done; go to LAST_ACK
1279  * state.  In all other cases, have already sent FIN to peer (e.g.
1280  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1281  * for peer to send FIN or not respond to keep-alives, etc.
1282  * We can let the user exit from the close as soon as the FIN is acked.
1283  */
1284 struct tcpcb *
1285 tcp_usrclosed(struct tcpcb *tp)
1286 {
1287 
1288 	switch (tp->t_state) {
1289 
1290 	case TCPS_CLOSED:
1291 	case TCPS_LISTEN:
1292 	case TCPS_SYN_SENT:
1293 		tp->t_state = TCPS_CLOSED;
1294 		tp = tcp_close(tp);
1295 		break;
1296 
1297 	case TCPS_SYN_RECEIVED:
1298 	case TCPS_ESTABLISHED:
1299 		tp->t_state = TCPS_FIN_WAIT_1;
1300 		break;
1301 
1302 	case TCPS_CLOSE_WAIT:
1303 		tp->t_state = TCPS_LAST_ACK;
1304 		break;
1305 	}
1306 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1307 		struct socket *so;
1308 		if (tp->t_inpcb)
1309 			so = tp->t_inpcb->inp_socket;
1310 #ifdef INET6
1311 		else if (tp->t_in6pcb)
1312 			so = tp->t_in6pcb->in6p_socket;
1313 #endif
1314 		else
1315 			so = NULL;
1316 		if (so)
1317 			soisdisconnected(so);
1318 		/*
1319 		 * If we are in FIN_WAIT_2, we arrived here because the
1320 		 * application did a shutdown of the send side.  Like the
1321 		 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after
1322 		 * a full close, we start a timer to make sure sockets are
1323 		 * not left in FIN_WAIT_2 forever.
1324 		 */
1325 		if ((tp->t_state == TCPS_FIN_WAIT_2) && (tp->t_maxidle > 0))
1326 			TCP_TIMER_ARM(tp, TCPT_2MSL, tp->t_maxidle);
1327 		else if (tp->t_state == TCPS_TIME_WAIT
1328 			 && ((tp->t_inpcb
1329 			      && (tcp4_vtw_enable & 1)
1330 			      && vtw_add(AF_INET, tp))
1331 			     ||
1332 			     (tp->t_in6pcb
1333 			      && (tcp6_vtw_enable & 1)
1334 			      && vtw_add(AF_INET6, tp)))) {
1335 			tp = 0;
1336 		}
1337 	}
1338 	return (tp);
1339 }
1340 
1341 /*
1342  * sysctl helper routine for net.inet.ip.mssdflt.  it can't be less
1343  * than 32.
1344  */
1345 static int
1346 sysctl_net_inet_tcp_mssdflt(SYSCTLFN_ARGS)
1347 {
1348 	int error, mssdflt;
1349 	struct sysctlnode node;
1350 
1351 	mssdflt = tcp_mssdflt;
1352 	node = *rnode;
1353 	node.sysctl_data = &mssdflt;
1354 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1355 	if (error || newp == NULL)
1356 		return (error);
1357 
1358 	if (mssdflt < 32)
1359 		return (EINVAL);
1360 	tcp_mssdflt = mssdflt;
1361 
1362 	mutex_enter(softnet_lock);
1363 	tcp_tcpcb_template();
1364 	mutex_exit(softnet_lock);
1365 
1366 	return (0);
1367 }
1368 
1369 /*
1370  * sysctl helper for TCP CB template update
1371  */
1372 static int
1373 sysctl_update_tcpcb_template(SYSCTLFN_ARGS)
1374 {
1375 	int t, error;
1376 	struct sysctlnode node;
1377 
1378 	/* follow procedures in sysctl(9) manpage */
1379 	t = *(int *)rnode->sysctl_data;
1380 	node = *rnode;
1381 	node.sysctl_data = &t;
1382 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1383 	if (error || newp == NULL)
1384 		return error;
1385 
1386 	if (t < 0)
1387 		return EINVAL;
1388 
1389 	*(int *)rnode->sysctl_data = t;
1390 
1391 	mutex_enter(softnet_lock);
1392 	tcp_tcpcb_template();
1393 	mutex_exit(softnet_lock);
1394 
1395 	return 0;
1396 }
1397 
1398 /*
1399  * sysctl helper routine for setting port related values under
1400  * net.inet.ip and net.inet6.ip6.  does basic range checking and does
1401  * additional checks for each type.  this code has placed in
1402  * tcp_input.c since INET and INET6 both use the same tcp code.
1403  *
1404  * this helper is not static so that both inet and inet6 can use it.
1405  */
1406 int
1407 sysctl_net_inet_ip_ports(SYSCTLFN_ARGS)
1408 {
1409 	int error, tmp;
1410 	int apmin, apmax;
1411 #ifndef IPNOPRIVPORTS
1412 	int lpmin, lpmax;
1413 #endif /* IPNOPRIVPORTS */
1414 	struct sysctlnode node;
1415 
1416 	if (namelen != 0)
1417 		return (EINVAL);
1418 
1419 	switch (name[-3]) {
1420 #ifdef INET
1421 	    case PF_INET:
1422 		apmin = anonportmin;
1423 		apmax = anonportmax;
1424 #ifndef IPNOPRIVPORTS
1425 		lpmin = lowportmin;
1426 		lpmax = lowportmax;
1427 #endif /* IPNOPRIVPORTS */
1428 		break;
1429 #endif /* INET */
1430 #ifdef INET6
1431 	    case PF_INET6:
1432 		apmin = ip6_anonportmin;
1433 		apmax = ip6_anonportmax;
1434 #ifndef IPNOPRIVPORTS
1435 		lpmin = ip6_lowportmin;
1436 		lpmax = ip6_lowportmax;
1437 #endif /* IPNOPRIVPORTS */
1438 		break;
1439 #endif /* INET6 */
1440 	    default:
1441 		return (EINVAL);
1442 	}
1443 
1444 	/*
1445 	 * insert temporary copy into node, perform lookup on
1446 	 * temporary, then restore pointer
1447 	 */
1448 	node = *rnode;
1449 	tmp = *(int*)rnode->sysctl_data;
1450 	node.sysctl_data = &tmp;
1451 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1452 	if (error || newp == NULL)
1453 		return (error);
1454 
1455 	/*
1456 	 * simple port range check
1457 	 */
1458 	if (tmp < 0 || tmp > 65535)
1459 		return (EINVAL);
1460 
1461 	/*
1462 	 * per-node range checks
1463 	 */
1464 	switch (rnode->sysctl_num) {
1465 	case IPCTL_ANONPORTMIN:
1466 	case IPV6CTL_ANONPORTMIN:
1467 		if (tmp >= apmax)
1468 			return (EINVAL);
1469 #ifndef IPNOPRIVPORTS
1470 		if (tmp < IPPORT_RESERVED)
1471                         return (EINVAL);
1472 #endif /* IPNOPRIVPORTS */
1473 		break;
1474 
1475 	case IPCTL_ANONPORTMAX:
1476 	case IPV6CTL_ANONPORTMAX:
1477                 if (apmin >= tmp)
1478 			return (EINVAL);
1479 #ifndef IPNOPRIVPORTS
1480 		if (tmp < IPPORT_RESERVED)
1481                         return (EINVAL);
1482 #endif /* IPNOPRIVPORTS */
1483 		break;
1484 
1485 #ifndef IPNOPRIVPORTS
1486 	case IPCTL_LOWPORTMIN:
1487 	case IPV6CTL_LOWPORTMIN:
1488 		if (tmp >= lpmax ||
1489 		    tmp > IPPORT_RESERVEDMAX ||
1490 		    tmp < IPPORT_RESERVEDMIN)
1491 			return (EINVAL);
1492 		break;
1493 
1494 	case IPCTL_LOWPORTMAX:
1495 	case IPV6CTL_LOWPORTMAX:
1496 		if (lpmin >= tmp ||
1497 		    tmp > IPPORT_RESERVEDMAX ||
1498 		    tmp < IPPORT_RESERVEDMIN)
1499 			return (EINVAL);
1500 		break;
1501 #endif /* IPNOPRIVPORTS */
1502 
1503 	default:
1504 		return (EINVAL);
1505 	}
1506 
1507 	*(int*)rnode->sysctl_data = tmp;
1508 
1509 	return (0);
1510 }
1511 
1512 static inline int
1513 copyout_uid(struct socket *sockp, void *oldp, size_t *oldlenp)
1514 {
1515 	if (oldp) {
1516 		size_t sz;
1517 		uid_t uid;
1518 		int error;
1519 
1520 		if (sockp->so_cred == NULL)
1521 			return EPERM;
1522 
1523 		uid = kauth_cred_geteuid(sockp->so_cred);
1524 		sz = MIN(sizeof(uid), *oldlenp);
1525 		if ((error = copyout(&uid, oldp, sz)) != 0)
1526 			return error;
1527 	}
1528 	*oldlenp = sizeof(uid_t);
1529 	return 0;
1530 }
1531 
1532 static inline int
1533 inet4_ident_core(struct in_addr raddr, u_int rport,
1534     struct in_addr laddr, u_int lport,
1535     void *oldp, size_t *oldlenp,
1536     struct lwp *l, int dodrop)
1537 {
1538 	struct inpcb *inp;
1539 	struct socket *sockp;
1540 
1541 	inp = in_pcblookup_connect(&tcbtable, raddr, rport, laddr, lport, 0);
1542 
1543 	if (inp == NULL || (sockp = inp->inp_socket) == NULL)
1544 		return ESRCH;
1545 
1546 	if (dodrop) {
1547 		struct tcpcb *tp;
1548 		int error;
1549 
1550 		if (inp == NULL || (tp = intotcpcb(inp)) == NULL ||
1551 		    (inp->inp_socket->so_options & SO_ACCEPTCONN) != 0)
1552 			return ESRCH;
1553 
1554 		error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
1555 		    KAUTH_REQ_NETWORK_SOCKET_DROP, inp->inp_socket, tp, NULL);
1556 		if (error)
1557 			return (error);
1558 
1559 		(void)tcp_drop(tp, ECONNABORTED);
1560 		return 0;
1561 	}
1562 	else
1563 		return copyout_uid(sockp, oldp, oldlenp);
1564 }
1565 
1566 #ifdef INET6
1567 static inline int
1568 inet6_ident_core(struct in6_addr *raddr, u_int rport,
1569     struct in6_addr *laddr, u_int lport,
1570     void *oldp, size_t *oldlenp,
1571     struct lwp *l, int dodrop)
1572 {
1573 	struct in6pcb *in6p;
1574 	struct socket *sockp;
1575 
1576 	in6p = in6_pcblookup_connect(&tcbtable, raddr, rport, laddr, lport, 0, 0);
1577 
1578 	if (in6p == NULL || (sockp = in6p->in6p_socket) == NULL)
1579 		return ESRCH;
1580 
1581 	if (dodrop) {
1582 		struct tcpcb *tp;
1583 		int error;
1584 
1585 		if (in6p == NULL || (tp = in6totcpcb(in6p)) == NULL ||
1586 		    (in6p->in6p_socket->so_options & SO_ACCEPTCONN) != 0)
1587 			return ESRCH;
1588 
1589 		error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
1590 		    KAUTH_REQ_NETWORK_SOCKET_DROP, in6p->in6p_socket, tp, NULL);
1591 		if (error)
1592 			return (error);
1593 
1594 		(void)tcp_drop(tp, ECONNABORTED);
1595 		return 0;
1596 	}
1597 	else
1598 		return copyout_uid(sockp, oldp, oldlenp);
1599 }
1600 #endif
1601 
1602 /*
1603  * sysctl helper routine for the net.inet.tcp.drop and
1604  * net.inet6.tcp6.drop nodes.
1605  */
1606 #define sysctl_net_inet_tcp_drop sysctl_net_inet_tcp_ident
1607 
1608 /*
1609  * sysctl helper routine for the net.inet.tcp.ident and
1610  * net.inet6.tcp6.ident nodes.  contains backwards compat code for the
1611  * old way of looking up the ident information for ipv4 which involves
1612  * stuffing the port/addr pairs into the mib lookup.
1613  */
1614 static int
1615 sysctl_net_inet_tcp_ident(SYSCTLFN_ARGS)
1616 {
1617 #ifdef INET
1618 	struct sockaddr_in *si4[2];
1619 #endif /* INET */
1620 #ifdef INET6
1621 	struct sockaddr_in6 *si6[2];
1622 #endif /* INET6 */
1623 	struct sockaddr_storage sa[2];
1624 	int error, pf, dodrop;
1625 
1626 	dodrop = name[-1] == TCPCTL_DROP;
1627 	if (dodrop) {
1628 		if (oldp != NULL || *oldlenp != 0)
1629 			return EINVAL;
1630 		if (newp == NULL)
1631 			return EPERM;
1632 		if (newlen < sizeof(sa))
1633 			return ENOMEM;
1634 	}
1635 	if (namelen != 4 && namelen != 0)
1636 		return EINVAL;
1637 	if (name[-2] != IPPROTO_TCP)
1638 		return EINVAL;
1639 	pf = name[-3];
1640 
1641 	/* old style lookup, ipv4 only */
1642 	if (namelen == 4) {
1643 #ifdef INET
1644 		struct in_addr laddr, raddr;
1645 		u_int lport, rport;
1646 
1647 		if (pf != PF_INET)
1648 			return EPROTONOSUPPORT;
1649 		raddr.s_addr = (uint32_t)name[0];
1650 		rport = (u_int)name[1];
1651 		laddr.s_addr = (uint32_t)name[2];
1652 		lport = (u_int)name[3];
1653 
1654 		mutex_enter(softnet_lock);
1655 		error = inet4_ident_core(raddr, rport, laddr, lport,
1656 		    oldp, oldlenp, l, dodrop);
1657 		mutex_exit(softnet_lock);
1658 		return error;
1659 #else /* INET */
1660 		return EINVAL;
1661 #endif /* INET */
1662 	}
1663 
1664 	if (newp == NULL || newlen != sizeof(sa))
1665 		return EINVAL;
1666 	error = copyin(newp, &sa, newlen);
1667 	if (error)
1668 		return error;
1669 
1670 	/*
1671 	 * requested families must match
1672 	 */
1673 	if (pf != sa[0].ss_family || sa[0].ss_family != sa[1].ss_family)
1674 		return EINVAL;
1675 
1676 	switch (pf) {
1677 #ifdef INET6
1678 	case PF_INET6:
1679 		si6[0] = (struct sockaddr_in6*)&sa[0];
1680 		si6[1] = (struct sockaddr_in6*)&sa[1];
1681 		if (si6[0]->sin6_len != sizeof(*si6[0]) ||
1682 		    si6[1]->sin6_len != sizeof(*si6[1]))
1683 			return EINVAL;
1684 
1685 		if (!IN6_IS_ADDR_V4MAPPED(&si6[0]->sin6_addr) &&
1686 		    !IN6_IS_ADDR_V4MAPPED(&si6[1]->sin6_addr)) {
1687 			error = sa6_embedscope(si6[0], ip6_use_defzone);
1688 			if (error)
1689 				return error;
1690 			error = sa6_embedscope(si6[1], ip6_use_defzone);
1691 			if (error)
1692 				return error;
1693 
1694 			mutex_enter(softnet_lock);
1695 			error = inet6_ident_core(&si6[0]->sin6_addr,
1696 			    si6[0]->sin6_port, &si6[1]->sin6_addr,
1697 			    si6[1]->sin6_port, oldp, oldlenp, l, dodrop);
1698 			mutex_exit(softnet_lock);
1699 			return error;
1700 		}
1701 
1702 		if (IN6_IS_ADDR_V4MAPPED(&si6[0]->sin6_addr) !=
1703 		    IN6_IS_ADDR_V4MAPPED(&si6[1]->sin6_addr))
1704 			return EINVAL;
1705 
1706 		in6_sin6_2_sin_in_sock((struct sockaddr *)&sa[0]);
1707 		in6_sin6_2_sin_in_sock((struct sockaddr *)&sa[1]);
1708 		/*FALLTHROUGH*/
1709 #endif /* INET6 */
1710 #ifdef INET
1711 	case PF_INET:
1712 		si4[0] = (struct sockaddr_in*)&sa[0];
1713 		si4[1] = (struct sockaddr_in*)&sa[1];
1714 		if (si4[0]->sin_len != sizeof(*si4[0]) ||
1715 		    si4[0]->sin_len != sizeof(*si4[1]))
1716 			return EINVAL;
1717 
1718 		mutex_enter(softnet_lock);
1719 		error = inet4_ident_core(si4[0]->sin_addr, si4[0]->sin_port,
1720 		    si4[1]->sin_addr, si4[1]->sin_port,
1721 		    oldp, oldlenp, l, dodrop);
1722 		mutex_exit(softnet_lock);
1723 		return error;
1724 #endif /* INET */
1725 	default:
1726 		return EPROTONOSUPPORT;
1727 	}
1728 }
1729 
1730 /*
1731  * sysctl helper for the inet and inet6 pcblists.  handles tcp/udp and
1732  * inet/inet6, as well as raw pcbs for each.  specifically not
1733  * declared static so that raw sockets and udp/udp6 can use it as
1734  * well.
1735  */
1736 int
1737 sysctl_inpcblist(SYSCTLFN_ARGS)
1738 {
1739 #ifdef INET
1740 	struct sockaddr_in *in;
1741 	const struct inpcb *inp;
1742 #endif
1743 #ifdef INET6
1744 	struct sockaddr_in6 *in6;
1745 	const struct in6pcb *in6p;
1746 #endif
1747 	struct inpcbtable *pcbtbl = __UNCONST(rnode->sysctl_data);
1748 	const struct inpcb_hdr *inph;
1749 	struct tcpcb *tp;
1750 	struct kinfo_pcb pcb;
1751 	char *dp;
1752 	size_t len, needed, elem_size, out_size;
1753 	int error, elem_count, pf, proto, pf2;
1754 
1755 	if (namelen != 4)
1756 		return (EINVAL);
1757 
1758 	if (oldp != NULL) {
1759 		    len = *oldlenp;
1760 		    elem_size = name[2];
1761 		    elem_count = name[3];
1762 		    if (elem_size != sizeof(pcb))
1763 			    return EINVAL;
1764 	} else {
1765 		    len = 0;
1766 		    elem_count = INT_MAX;
1767 		    elem_size = sizeof(pcb);
1768 	}
1769 	error = 0;
1770 	dp = oldp;
1771 	out_size = elem_size;
1772 	needed = 0;
1773 
1774 	if (namelen == 1 && name[0] == CTL_QUERY)
1775 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
1776 
1777 	if (name - oname != 4)
1778 		return (EINVAL);
1779 
1780 	pf = oname[1];
1781 	proto = oname[2];
1782 	pf2 = (oldp != NULL) ? pf : 0;
1783 
1784 	mutex_enter(softnet_lock);
1785 
1786 	TAILQ_FOREACH(inph, &pcbtbl->inpt_queue, inph_queue) {
1787 #ifdef INET
1788 		inp = (const struct inpcb *)inph;
1789 #endif
1790 #ifdef INET6
1791 		in6p = (const struct in6pcb *)inph;
1792 #endif
1793 
1794 		if (inph->inph_af != pf)
1795 			continue;
1796 
1797 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
1798 		    KAUTH_REQ_NETWORK_SOCKET_CANSEE, inph->inph_socket, NULL,
1799 		    NULL) != 0)
1800 			continue;
1801 
1802 		memset(&pcb, 0, sizeof(pcb));
1803 
1804 		pcb.ki_family = pf;
1805 		pcb.ki_type = proto;
1806 
1807 		switch (pf2) {
1808 		case 0:
1809 			/* just probing for size */
1810 			break;
1811 #ifdef INET
1812 		case PF_INET:
1813 			pcb.ki_family = inp->inp_socket->so_proto->
1814 			    pr_domain->dom_family;
1815 			pcb.ki_type = inp->inp_socket->so_proto->
1816 			    pr_type;
1817 			pcb.ki_protocol = inp->inp_socket->so_proto->
1818 			    pr_protocol;
1819 			pcb.ki_pflags = inp->inp_flags;
1820 
1821 			pcb.ki_sostate = inp->inp_socket->so_state;
1822 			pcb.ki_prstate = inp->inp_state;
1823 			if (proto == IPPROTO_TCP) {
1824 				tp = intotcpcb(inp);
1825 				pcb.ki_tstate = tp->t_state;
1826 				pcb.ki_tflags = tp->t_flags;
1827 			}
1828 
1829 			pcb.ki_pcbaddr = PTRTOUINT64(inp);
1830 			pcb.ki_ppcbaddr = PTRTOUINT64(inp->inp_ppcb);
1831 			pcb.ki_sockaddr = PTRTOUINT64(inp->inp_socket);
1832 
1833 			pcb.ki_rcvq = inp->inp_socket->so_rcv.sb_cc;
1834 			pcb.ki_sndq = inp->inp_socket->so_snd.sb_cc;
1835 
1836 			in = satosin(&pcb.ki_src);
1837 			in->sin_len = sizeof(*in);
1838 			in->sin_family = pf;
1839 			in->sin_port = inp->inp_lport;
1840 			in->sin_addr = inp->inp_laddr;
1841 			if (pcb.ki_prstate >= INP_CONNECTED) {
1842 				in = satosin(&pcb.ki_dst);
1843 				in->sin_len = sizeof(*in);
1844 				in->sin_family = pf;
1845 				in->sin_port = inp->inp_fport;
1846 				in->sin_addr = inp->inp_faddr;
1847 			}
1848 			break;
1849 #endif
1850 #ifdef INET6
1851 		case PF_INET6:
1852 			pcb.ki_family = in6p->in6p_socket->so_proto->
1853 			    pr_domain->dom_family;
1854 			pcb.ki_type = in6p->in6p_socket->so_proto->pr_type;
1855 			pcb.ki_protocol = in6p->in6p_socket->so_proto->
1856 			    pr_protocol;
1857 			pcb.ki_pflags = in6p->in6p_flags;
1858 
1859 			pcb.ki_sostate = in6p->in6p_socket->so_state;
1860 			pcb.ki_prstate = in6p->in6p_state;
1861 			if (proto == IPPROTO_TCP) {
1862 				tp = in6totcpcb(in6p);
1863 				pcb.ki_tstate = tp->t_state;
1864 				pcb.ki_tflags = tp->t_flags;
1865 			}
1866 
1867 			pcb.ki_pcbaddr = PTRTOUINT64(in6p);
1868 			pcb.ki_ppcbaddr = PTRTOUINT64(in6p->in6p_ppcb);
1869 			pcb.ki_sockaddr = PTRTOUINT64(in6p->in6p_socket);
1870 
1871 			pcb.ki_rcvq = in6p->in6p_socket->so_rcv.sb_cc;
1872 			pcb.ki_sndq = in6p->in6p_socket->so_snd.sb_cc;
1873 
1874 			in6 = satosin6(&pcb.ki_src);
1875 			in6->sin6_len = sizeof(*in6);
1876 			in6->sin6_family = pf;
1877 			in6->sin6_port = in6p->in6p_lport;
1878 			in6->sin6_flowinfo = in6p->in6p_flowinfo;
1879 			in6->sin6_addr = in6p->in6p_laddr;
1880 			in6->sin6_scope_id = 0; /* XXX? */
1881 
1882 			if (pcb.ki_prstate >= IN6P_CONNECTED) {
1883 				in6 = satosin6(&pcb.ki_dst);
1884 				in6->sin6_len = sizeof(*in6);
1885 				in6->sin6_family = pf;
1886 				in6->sin6_port = in6p->in6p_fport;
1887 				in6->sin6_flowinfo = in6p->in6p_flowinfo;
1888 				in6->sin6_addr = in6p->in6p_faddr;
1889 				in6->sin6_scope_id = 0; /* XXX? */
1890 			}
1891 			break;
1892 #endif
1893 		}
1894 
1895 		if (len >= elem_size && elem_count > 0) {
1896 			error = copyout(&pcb, dp, out_size);
1897 			if (error) {
1898 				mutex_exit(softnet_lock);
1899 				return (error);
1900 			}
1901 			dp += elem_size;
1902 			len -= elem_size;
1903 		}
1904 		needed += elem_size;
1905 		if (elem_count > 0 && elem_count != INT_MAX)
1906 			elem_count--;
1907 	}
1908 
1909 	*oldlenp = needed;
1910 	if (oldp == NULL)
1911 		*oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
1912 
1913 	mutex_exit(softnet_lock);
1914 
1915 	return (error);
1916 }
1917 
1918 static int
1919 sysctl_tcp_congctl(SYSCTLFN_ARGS)
1920 {
1921 	struct sysctlnode node;
1922 	int error;
1923 	char newname[TCPCC_MAXLEN];
1924 
1925 	strlcpy(newname, tcp_congctl_global_name, sizeof(newname) - 1);
1926 
1927 	node = *rnode;
1928 	node.sysctl_data = newname;
1929 	node.sysctl_size = sizeof(newname);
1930 
1931 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1932 
1933 	if (error ||
1934 	    newp == NULL ||
1935 	    strncmp(newname, tcp_congctl_global_name, sizeof(newname)) == 0)
1936 		return error;
1937 
1938 	mutex_enter(softnet_lock);
1939 	error = tcp_congctl_select(NULL, newname);
1940 	mutex_exit(softnet_lock);
1941 
1942 	return error;
1943 }
1944 
1945 static int
1946 sysctl_tcp_init_win(SYSCTLFN_ARGS)
1947 {
1948 	int error;
1949 	u_int iw;
1950 	struct sysctlnode node;
1951 
1952 	iw = *(u_int *)rnode->sysctl_data;
1953 	node = *rnode;
1954 	node.sysctl_data = &iw;
1955 	node.sysctl_size = sizeof(iw);
1956 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1957 	if (error || newp == NULL)
1958 		return error;
1959 
1960 	if (iw >= __arraycount(tcp_init_win_max))
1961 		return EINVAL;
1962 	*(u_int *)rnode->sysctl_data = iw;
1963 	return 0;
1964 }
1965 
1966 static int
1967 sysctl_tcp_keep(SYSCTLFN_ARGS)
1968 {
1969 	int error;
1970 	u_int tmp;
1971 	struct sysctlnode node;
1972 
1973 	node = *rnode;
1974 	tmp = *(u_int *)rnode->sysctl_data;
1975 	node.sysctl_data = &tmp;
1976 
1977 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1978 	if (error || newp == NULL)
1979 		return error;
1980 
1981 	mutex_enter(softnet_lock);
1982 
1983 	*(u_int *)rnode->sysctl_data = tmp;
1984 	tcp_tcpcb_template();	/* update the template */
1985 
1986 	mutex_exit(softnet_lock);
1987 	return 0;
1988 }
1989 
1990 static int
1991 sysctl_net_inet_tcp_stats(SYSCTLFN_ARGS)
1992 {
1993 
1994 	return (NETSTAT_SYSCTL(tcpstat_percpu, TCP_NSTATS));
1995 }
1996 
1997 /*
1998  * this (second stage) setup routine is a replacement for tcp_sysctl()
1999  * (which is currently used for ipv4 and ipv6)
2000  */
2001 static void
2002 sysctl_net_inet_tcp_setup2(struct sysctllog **clog, int pf, const char *pfname,
2003 			   const char *tcpname)
2004 {
2005 	const struct sysctlnode *sack_node;
2006 	const struct sysctlnode *abc_node;
2007 	const struct sysctlnode *ecn_node;
2008 	const struct sysctlnode *congctl_node;
2009 	const struct sysctlnode *mslt_node;
2010 	const struct sysctlnode *vtw_node;
2011 #ifdef TCP_DEBUG
2012 	extern struct tcp_debug tcp_debug[TCP_NDEBUG];
2013 	extern int tcp_debx;
2014 #endif
2015 
2016 	sysctl_createv(clog, 0, NULL, NULL,
2017 		       CTLFLAG_PERMANENT,
2018 		       CTLTYPE_NODE, pfname, NULL,
2019 		       NULL, 0, NULL, 0,
2020 		       CTL_NET, pf, CTL_EOL);
2021 	sysctl_createv(clog, 0, NULL, NULL,
2022 		       CTLFLAG_PERMANENT,
2023 		       CTLTYPE_NODE, tcpname,
2024 		       SYSCTL_DESCR("TCP related settings"),
2025 		       NULL, 0, NULL, 0,
2026 		       CTL_NET, pf, IPPROTO_TCP, CTL_EOL);
2027 
2028 	sysctl_createv(clog, 0, NULL, NULL,
2029 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2030 		       CTLTYPE_INT, "rfc1323",
2031 		       SYSCTL_DESCR("Enable RFC1323 TCP extensions"),
2032 		       sysctl_update_tcpcb_template, 0, &tcp_do_rfc1323, 0,
2033 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RFC1323, CTL_EOL);
2034 	sysctl_createv(clog, 0, NULL, NULL,
2035 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2036 		       CTLTYPE_INT, "sendspace",
2037 		       SYSCTL_DESCR("Default TCP send buffer size"),
2038 		       NULL, 0, &tcp_sendspace, 0,
2039 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SENDSPACE, CTL_EOL);
2040 	sysctl_createv(clog, 0, NULL, NULL,
2041 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2042 		       CTLTYPE_INT, "recvspace",
2043 		       SYSCTL_DESCR("Default TCP receive buffer size"),
2044 		       NULL, 0, &tcp_recvspace, 0,
2045 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RECVSPACE, CTL_EOL);
2046 	sysctl_createv(clog, 0, NULL, NULL,
2047 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2048 		       CTLTYPE_INT, "mssdflt",
2049 		       SYSCTL_DESCR("Default maximum segment size"),
2050 		       sysctl_net_inet_tcp_mssdflt, 0, &tcp_mssdflt, 0,
2051 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSSDFLT, CTL_EOL);
2052 	sysctl_createv(clog, 0, NULL, NULL,
2053 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2054 		       CTLTYPE_INT, "minmss",
2055 		       SYSCTL_DESCR("Lower limit for TCP maximum segment size"),
2056 		       NULL, 0, &tcp_minmss, 0,
2057 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2058 	sysctl_createv(clog, 0, NULL, NULL,
2059 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2060 		       CTLTYPE_INT, "msl",
2061 		       SYSCTL_DESCR("Maximum Segment Life"),
2062 		       NULL, 0, &tcp_msl, 0,
2063 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSL, CTL_EOL);
2064 	sysctl_createv(clog, 0, NULL, NULL,
2065 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2066 		       CTLTYPE_INT, "syn_cache_limit",
2067 		       SYSCTL_DESCR("Maximum number of entries in the TCP "
2068 				    "compressed state engine"),
2069 		       NULL, 0, &tcp_syn_cache_limit, 0,
2070 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_LIMIT,
2071 		       CTL_EOL);
2072 	sysctl_createv(clog, 0, NULL, NULL,
2073 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2074 		       CTLTYPE_INT, "syn_bucket_limit",
2075 		       SYSCTL_DESCR("Maximum number of entries per hash "
2076 				    "bucket in the TCP compressed state "
2077 				    "engine"),
2078 		       NULL, 0, &tcp_syn_bucket_limit, 0,
2079 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_BUCKET_LIMIT,
2080 		       CTL_EOL);
2081 #if 0 /* obsoleted */
2082 	sysctl_createv(clog, 0, NULL, NULL,
2083 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2084 		       CTLTYPE_INT, "syn_cache_interval",
2085 		       SYSCTL_DESCR("TCP compressed state engine's timer interval"),
2086 		       NULL, 0, &tcp_syn_cache_interval, 0,
2087 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_INTER,
2088 		       CTL_EOL);
2089 #endif
2090 	sysctl_createv(clog, 0, NULL, NULL,
2091 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2092 		       CTLTYPE_INT, "init_win",
2093 		       SYSCTL_DESCR("Initial TCP congestion window"),
2094 		       sysctl_tcp_init_win, 0, &tcp_init_win, 0,
2095 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN, CTL_EOL);
2096 	sysctl_createv(clog, 0, NULL, NULL,
2097 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2098 		       CTLTYPE_INT, "mss_ifmtu",
2099 		       SYSCTL_DESCR("Use interface MTU for calculating MSS"),
2100 		       NULL, 0, &tcp_mss_ifmtu, 0,
2101 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSS_IFMTU, CTL_EOL);
2102 	sysctl_createv(clog, 0, NULL, &sack_node,
2103 		       CTLFLAG_PERMANENT,
2104 		       CTLTYPE_NODE, "sack",
2105 		       SYSCTL_DESCR("RFC2018 Selective ACKnowledgement tunables"),
2106 		       NULL, 0, NULL, 0,
2107 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_EOL);
2108 
2109 	/* Congctl subtree */
2110 	sysctl_createv(clog, 0, NULL, &congctl_node,
2111 		       CTLFLAG_PERMANENT,
2112 		       CTLTYPE_NODE, "congctl",
2113 		       SYSCTL_DESCR("TCP Congestion Control"),
2114 	    	       NULL, 0, NULL, 0,
2115 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2116 	sysctl_createv(clog, 0, &congctl_node, NULL,
2117 		       CTLFLAG_PERMANENT,
2118 		       CTLTYPE_STRING, "available",
2119 		       SYSCTL_DESCR("Available Congestion Control Mechanisms"),
2120 		       NULL, 0, tcp_congctl_avail, 0, CTL_CREATE, CTL_EOL);
2121 	sysctl_createv(clog, 0, &congctl_node, NULL,
2122 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2123 		       CTLTYPE_STRING, "selected",
2124 		       SYSCTL_DESCR("Selected Congestion Control Mechanism"),
2125 		       sysctl_tcp_congctl, 0, NULL, TCPCC_MAXLEN,
2126 		       CTL_CREATE, CTL_EOL);
2127 
2128 	sysctl_createv(clog, 0, NULL, NULL,
2129 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2130 		       CTLTYPE_INT, "win_scale",
2131 		       SYSCTL_DESCR("Use RFC1323 window scale options"),
2132 		       sysctl_update_tcpcb_template, 0, &tcp_do_win_scale, 0,
2133 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_WSCALE, CTL_EOL);
2134 	sysctl_createv(clog, 0, NULL, NULL,
2135 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2136 		       CTLTYPE_INT, "timestamps",
2137 		       SYSCTL_DESCR("Use RFC1323 time stamp options"),
2138 		       sysctl_update_tcpcb_template, 0, &tcp_do_timestamps, 0,
2139 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_TSTAMP, CTL_EOL);
2140 	sysctl_createv(clog, 0, NULL, NULL,
2141 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2142 		       CTLTYPE_INT, "compat_42",
2143 		       SYSCTL_DESCR("Enable workarounds for 4.2BSD TCP bugs"),
2144 		       NULL, 0, &tcp_compat_42, 0,
2145 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_COMPAT_42, CTL_EOL);
2146 	sysctl_createv(clog, 0, NULL, NULL,
2147 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2148 		       CTLTYPE_INT, "cwm",
2149 		       SYSCTL_DESCR("Hughes/Touch/Heidemann Congestion Window "
2150 				    "Monitoring"),
2151 		       NULL, 0, &tcp_cwm, 0,
2152 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM, CTL_EOL);
2153 	sysctl_createv(clog, 0, NULL, NULL,
2154 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2155 		       CTLTYPE_INT, "cwm_burstsize",
2156 		       SYSCTL_DESCR("Congestion Window Monitoring allowed "
2157 				    "burst count in packets"),
2158 		       NULL, 0, &tcp_cwm_burstsize, 0,
2159 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM_BURSTSIZE,
2160 		       CTL_EOL);
2161 	sysctl_createv(clog, 0, NULL, NULL,
2162 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2163 		       CTLTYPE_INT, "ack_on_push",
2164 		       SYSCTL_DESCR("Immediately return ACK when PSH is "
2165 				    "received"),
2166 		       NULL, 0, &tcp_ack_on_push, 0,
2167 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_ACK_ON_PUSH, CTL_EOL);
2168 	sysctl_createv(clog, 0, NULL, NULL,
2169 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2170 		       CTLTYPE_INT, "keepidle",
2171 		       SYSCTL_DESCR("Allowed connection idle ticks before a "
2172 				    "keepalive probe is sent"),
2173 		       sysctl_tcp_keep, 0, &tcp_keepidle, 0,
2174 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPIDLE, CTL_EOL);
2175 	sysctl_createv(clog, 0, NULL, NULL,
2176 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2177 		       CTLTYPE_INT, "keepintvl",
2178 		       SYSCTL_DESCR("Ticks before next keepalive probe is sent"),
2179 		       sysctl_tcp_keep, 0, &tcp_keepintvl, 0,
2180 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPINTVL, CTL_EOL);
2181 	sysctl_createv(clog, 0, NULL, NULL,
2182 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2183 		       CTLTYPE_INT, "keepcnt",
2184 		       SYSCTL_DESCR("Number of keepalive probes to send"),
2185 		       sysctl_tcp_keep, 0, &tcp_keepcnt, 0,
2186 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPCNT, CTL_EOL);
2187 	sysctl_createv(clog, 0, NULL, NULL,
2188 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
2189 		       CTLTYPE_INT, "slowhz",
2190 		       SYSCTL_DESCR("Keepalive ticks per second"),
2191 		       NULL, PR_SLOWHZ, NULL, 0,
2192 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SLOWHZ, CTL_EOL);
2193 	sysctl_createv(clog, 0, NULL, NULL,
2194 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2195 		       CTLTYPE_INT, "log_refused",
2196 		       SYSCTL_DESCR("Log refused TCP connections"),
2197 		       NULL, 0, &tcp_log_refused, 0,
2198 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOG_REFUSED, CTL_EOL);
2199 #if 0 /* obsoleted */
2200 	sysctl_createv(clog, 0, NULL, NULL,
2201 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2202 		       CTLTYPE_INT, "rstratelimit", NULL,
2203 		       NULL, 0, &tcp_rst_ratelim, 0,
2204 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTRATELIMIT, CTL_EOL);
2205 #endif
2206 	sysctl_createv(clog, 0, NULL, NULL,
2207 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2208 		       CTLTYPE_INT, "rstppslimit",
2209 		       SYSCTL_DESCR("Maximum number of RST packets to send "
2210 				    "per second"),
2211 		       NULL, 0, &tcp_rst_ppslim, 0,
2212 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTPPSLIMIT, CTL_EOL);
2213 	sysctl_createv(clog, 0, NULL, NULL,
2214 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2215 		       CTLTYPE_INT, "delack_ticks",
2216 		       SYSCTL_DESCR("Number of ticks to delay sending an ACK"),
2217 		       NULL, 0, &tcp_delack_ticks, 0,
2218 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DELACK_TICKS, CTL_EOL);
2219 	sysctl_createv(clog, 0, NULL, NULL,
2220 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2221 		       CTLTYPE_INT, "init_win_local",
2222 		       SYSCTL_DESCR("Initial TCP window size (in segments)"),
2223 		       sysctl_tcp_init_win, 0, &tcp_init_win_local, 0,
2224 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN_LOCAL,
2225 		       CTL_EOL);
2226 	sysctl_createv(clog, 0, NULL, NULL,
2227 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2228 		       CTLTYPE_STRUCT, "ident",
2229 		       SYSCTL_DESCR("RFC1413 Identification Protocol lookups"),
2230 		       sysctl_net_inet_tcp_ident, 0, NULL, sizeof(uid_t),
2231 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_IDENT, CTL_EOL);
2232 	sysctl_createv(clog, 0, NULL, NULL,
2233 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2234 		       CTLTYPE_INT, "do_loopback_cksum",
2235 		       SYSCTL_DESCR("Perform TCP checksum on loopback"),
2236 		       NULL, 0, &tcp_do_loopback_cksum, 0,
2237 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOOPBACKCKSUM,
2238 		       CTL_EOL);
2239 	sysctl_createv(clog, 0, NULL, NULL,
2240 		       CTLFLAG_PERMANENT,
2241 		       CTLTYPE_STRUCT, "pcblist",
2242 		       SYSCTL_DESCR("TCP protocol control block list"),
2243 		       sysctl_inpcblist, 0, &tcbtable, 0,
2244 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE,
2245 		       CTL_EOL);
2246 	sysctl_createv(clog, 0, NULL, NULL,
2247 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2248 		       CTLTYPE_INT, "keepinit",
2249 		       SYSCTL_DESCR("Ticks before initial tcp connection times out"),
2250 		       sysctl_tcp_keep, 0, &tcp_keepinit, 0,
2251 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2252 
2253 	/* TCP socket buffers auto-sizing nodes */
2254 	sysctl_createv(clog, 0, NULL, NULL,
2255 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2256 		       CTLTYPE_INT, "recvbuf_auto",
2257 		       SYSCTL_DESCR("Enable automatic receive "
2258 		           "buffer sizing (experimental)"),
2259 		       NULL, 0, &tcp_do_autorcvbuf, 0,
2260 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2261 	sysctl_createv(clog, 0, NULL, NULL,
2262 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2263 		       CTLTYPE_INT, "recvbuf_inc",
2264 		       SYSCTL_DESCR("Incrementor step size of "
2265 		           "automatic receive buffer"),
2266 		       NULL, 0, &tcp_autorcvbuf_inc, 0,
2267 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2268 	sysctl_createv(clog, 0, NULL, NULL,
2269 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2270 		       CTLTYPE_INT, "recvbuf_max",
2271 		       SYSCTL_DESCR("Max size of automatic receive buffer"),
2272 		       NULL, 0, &tcp_autorcvbuf_max, 0,
2273 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2274 
2275 	sysctl_createv(clog, 0, NULL, NULL,
2276 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2277 		       CTLTYPE_INT, "sendbuf_auto",
2278 		       SYSCTL_DESCR("Enable automatic send "
2279 		           "buffer sizing (experimental)"),
2280 		       NULL, 0, &tcp_do_autosndbuf, 0,
2281 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2282 	sysctl_createv(clog, 0, NULL, NULL,
2283 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2284 		       CTLTYPE_INT, "sendbuf_inc",
2285 		       SYSCTL_DESCR("Incrementor step size of "
2286 		           "automatic send buffer"),
2287 		       NULL, 0, &tcp_autosndbuf_inc, 0,
2288 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2289 	sysctl_createv(clog, 0, NULL, NULL,
2290 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2291 		       CTLTYPE_INT, "sendbuf_max",
2292 		       SYSCTL_DESCR("Max size of automatic send buffer"),
2293 		       NULL, 0, &tcp_autosndbuf_max, 0,
2294 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2295 
2296 	/* ECN subtree */
2297 	sysctl_createv(clog, 0, NULL, &ecn_node,
2298 	    	       CTLFLAG_PERMANENT,
2299 		       CTLTYPE_NODE, "ecn",
2300 	    	       SYSCTL_DESCR("RFC3168 Explicit Congestion Notification"),
2301 	    	       NULL, 0, NULL, 0,
2302 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2303 	sysctl_createv(clog, 0, &ecn_node, NULL,
2304 	    	       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2305 		       CTLTYPE_INT, "enable",
2306 		       SYSCTL_DESCR("Enable TCP Explicit Congestion "
2307 			   "Notification"),
2308 	    	       NULL, 0, &tcp_do_ecn, 0, CTL_CREATE, CTL_EOL);
2309 	sysctl_createv(clog, 0, &ecn_node, NULL,
2310 	    	       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2311 		       CTLTYPE_INT, "maxretries",
2312 		       SYSCTL_DESCR("Number of times to retry ECN setup "
2313 			       "before disabling ECN on the connection"),
2314 	    	       NULL, 0, &tcp_ecn_maxretries, 0, CTL_CREATE, CTL_EOL);
2315 
2316 	/* SACK gets its own little subtree. */
2317 	sysctl_createv(clog, 0, NULL, &sack_node,
2318 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2319 		       CTLTYPE_INT, "enable",
2320 		       SYSCTL_DESCR("Enable RFC2018 Selective ACKnowledgement"),
2321 		       NULL, 0, &tcp_do_sack, 0,
2322 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
2323 	sysctl_createv(clog, 0, NULL, &sack_node,
2324 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2325 		       CTLTYPE_INT, "maxholes",
2326 		       SYSCTL_DESCR("Maximum number of TCP SACK holes allowed per connection"),
2327 		       NULL, 0, &tcp_sack_tp_maxholes, 0,
2328 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
2329 	sysctl_createv(clog, 0, NULL, &sack_node,
2330 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2331 		       CTLTYPE_INT, "globalmaxholes",
2332 		       SYSCTL_DESCR("Global maximum number of TCP SACK holes"),
2333 		       NULL, 0, &tcp_sack_globalmaxholes, 0,
2334 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
2335 	sysctl_createv(clog, 0, NULL, &sack_node,
2336 		       CTLFLAG_PERMANENT,
2337 		       CTLTYPE_INT, "globalholes",
2338 		       SYSCTL_DESCR("Global number of TCP SACK holes"),
2339 		       NULL, 0, &tcp_sack_globalholes, 0,
2340 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
2341 
2342 	sysctl_createv(clog, 0, NULL, NULL,
2343 		       CTLFLAG_PERMANENT,
2344 		       CTLTYPE_STRUCT, "stats",
2345 		       SYSCTL_DESCR("TCP statistics"),
2346 		       sysctl_net_inet_tcp_stats, 0, NULL, 0,
2347 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_STATS,
2348 		       CTL_EOL);
2349         sysctl_createv(clog, 0, NULL, NULL,
2350                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2351                        CTLTYPE_INT, "local_by_rtt",
2352                        SYSCTL_DESCR("Use RTT estimator to decide which hosts "
2353 				    "are local"),
2354 		       NULL, 0, &tcp_rttlocal, 0,
2355 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2356 #ifdef TCP_DEBUG
2357 	sysctl_createv(clog, 0, NULL, NULL,
2358 		       CTLFLAG_PERMANENT,
2359 		       CTLTYPE_STRUCT, "debug",
2360 		       SYSCTL_DESCR("TCP sockets debug information"),
2361 		       NULL, 0, &tcp_debug, sizeof(tcp_debug),
2362 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBUG,
2363 		       CTL_EOL);
2364 	sysctl_createv(clog, 0, NULL, NULL,
2365 		       CTLFLAG_PERMANENT,
2366 		       CTLTYPE_INT, "debx",
2367 		       SYSCTL_DESCR("Number of TCP debug sockets messages"),
2368 		       NULL, 0, &tcp_debx, sizeof(tcp_debx),
2369 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBX,
2370 		       CTL_EOL);
2371 #endif
2372 	sysctl_createv(clog, 0, NULL, NULL,
2373 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2374 		       CTLTYPE_STRUCT, "drop",
2375 		       SYSCTL_DESCR("TCP drop connection"),
2376 		       sysctl_net_inet_tcp_drop, 0, NULL, 0,
2377 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DROP, CTL_EOL);
2378 	sysctl_createv(clog, 0, NULL, NULL,
2379 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2380 		       CTLTYPE_INT, "iss_hash",
2381 		       SYSCTL_DESCR("Enable RFC 1948 ISS by cryptographic "
2382 				    "hash computation"),
2383 		       NULL, 0, &tcp_do_rfc1948, sizeof(tcp_do_rfc1948),
2384 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE,
2385 		       CTL_EOL);
2386 
2387 	/* ABC subtree */
2388 
2389 	sysctl_createv(clog, 0, NULL, &abc_node,
2390 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "abc",
2391 		       SYSCTL_DESCR("RFC3465 Appropriate Byte Counting (ABC)"),
2392 		       NULL, 0, NULL, 0,
2393 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2394 	sysctl_createv(clog, 0, &abc_node, NULL,
2395 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2396 		       CTLTYPE_INT, "enable",
2397 		       SYSCTL_DESCR("Enable RFC3465 Appropriate Byte Counting"),
2398 		       NULL, 0, &tcp_do_abc, 0, CTL_CREATE, CTL_EOL);
2399 	sysctl_createv(clog, 0, &abc_node, NULL,
2400 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2401 		       CTLTYPE_INT, "aggressive",
2402 		       SYSCTL_DESCR("1: L=2*SMSS 0: L=1*SMSS"),
2403 		       NULL, 0, &tcp_abc_aggressive, 0, CTL_CREATE, CTL_EOL);
2404 
2405 	/* MSL tuning subtree */
2406 
2407 	sysctl_createv(clog, 0, NULL, &mslt_node,
2408 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "mslt",
2409 		       SYSCTL_DESCR("MSL Tuning for TIME_WAIT truncation"),
2410 		       NULL, 0, NULL, 0,
2411 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2412 	sysctl_createv(clog, 0, &mslt_node, NULL,
2413 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2414 		       CTLTYPE_INT, "enable",
2415 		       SYSCTL_DESCR("Enable TIME_WAIT truncation"),
2416 		       NULL, 0, &tcp_msl_enable, 0, CTL_CREATE, CTL_EOL);
2417 	sysctl_createv(clog, 0, &mslt_node, NULL,
2418 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2419 		       CTLTYPE_INT, "loopback",
2420 		       SYSCTL_DESCR("MSL value to use for loopback connections"),
2421 		       NULL, 0, &tcp_msl_loop, 0, CTL_CREATE, CTL_EOL);
2422 	sysctl_createv(clog, 0, &mslt_node, NULL,
2423 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2424 		       CTLTYPE_INT, "local",
2425 		       SYSCTL_DESCR("MSL value to use for local connections"),
2426 		       NULL, 0, &tcp_msl_local, 0, CTL_CREATE, CTL_EOL);
2427 	sysctl_createv(clog, 0, &mslt_node, NULL,
2428 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2429 		       CTLTYPE_INT, "remote",
2430 		       SYSCTL_DESCR("MSL value to use for remote connections"),
2431 		       NULL, 0, &tcp_msl_remote, 0, CTL_CREATE, CTL_EOL);
2432 	sysctl_createv(clog, 0, &mslt_node, NULL,
2433 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2434 		       CTLTYPE_INT, "remote_threshold",
2435 		       SYSCTL_DESCR("RTT estimate value to promote local to remote"),
2436 		       NULL, 0, &tcp_msl_remote_threshold, 0, CTL_CREATE, CTL_EOL);
2437 
2438 	/* vestigial TIME_WAIT tuning subtree */
2439 
2440 	sysctl_createv(clog, 0, NULL, &vtw_node,
2441 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "vtw",
2442 		       SYSCTL_DESCR("Tuning for Vestigial TIME_WAIT"),
2443 		       NULL, 0, NULL, 0,
2444 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2445 	sysctl_createv(clog, 0, &vtw_node, NULL,
2446 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2447 		       CTLTYPE_INT, "enable",
2448 		       SYSCTL_DESCR("Enable Vestigial TIME_WAIT"),
2449 		       sysctl_tcp_vtw_enable, 0,
2450 	               (pf == AF_INET) ? &tcp4_vtw_enable : &tcp6_vtw_enable,
2451 		       0, CTL_CREATE, CTL_EOL);
2452 	sysctl_createv(clog, 0, &vtw_node, NULL,
2453 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
2454 		       CTLTYPE_INT, "entries",
2455 		       SYSCTL_DESCR("Maximum number of vestigial TIME_WAIT entries"),
2456 		       NULL, 0, &tcp_vtw_entries, 0, CTL_CREATE, CTL_EOL);
2457 }
2458 
2459 void
2460 tcp_usrreq_init(void)
2461 {
2462 
2463 #ifdef INET
2464 	sysctl_net_inet_tcp_setup2(NULL, PF_INET, "inet", "tcp");
2465 #endif
2466 #ifdef INET6
2467 	sysctl_net_inet_tcp_setup2(NULL, PF_INET6, "inet6", "tcp6");
2468 #endif
2469 }
2470 
2471 PR_WRAP_USRREQS(tcp)
2472 #define	tcp_attach	tcp_attach_wrapper
2473 #define	tcp_detach	tcp_detach_wrapper
2474 #define	tcp_accept	tcp_accept_wrapper
2475 #define	tcp_bind	tcp_bind_wrapper
2476 #define	tcp_listen	tcp_listen_wrapper
2477 #define	tcp_connect	tcp_connect_wrapper
2478 #define	tcp_connect2	tcp_connect2_wrapper
2479 #define	tcp_disconnect	tcp_disconnect_wrapper
2480 #define	tcp_shutdown	tcp_shutdown_wrapper
2481 #define	tcp_abort	tcp_abort_wrapper
2482 #define	tcp_ioctl	tcp_ioctl_wrapper
2483 #define	tcp_stat	tcp_stat_wrapper
2484 #define	tcp_peeraddr	tcp_peeraddr_wrapper
2485 #define	tcp_sockaddr	tcp_sockaddr_wrapper
2486 #define	tcp_rcvd	tcp_rcvd_wrapper
2487 #define	tcp_recvoob	tcp_recvoob_wrapper
2488 #define	tcp_send	tcp_send_wrapper
2489 #define	tcp_sendoob	tcp_sendoob_wrapper
2490 #define	tcp_purgeif	tcp_purgeif_wrapper
2491 
2492 const struct pr_usrreqs tcp_usrreqs = {
2493 	.pr_attach	= tcp_attach,
2494 	.pr_detach	= tcp_detach,
2495 	.pr_accept	= tcp_accept,
2496 	.pr_bind	= tcp_bind,
2497 	.pr_listen	= tcp_listen,
2498 	.pr_connect	= tcp_connect,
2499 	.pr_connect2	= tcp_connect2,
2500 	.pr_disconnect	= tcp_disconnect,
2501 	.pr_shutdown	= tcp_shutdown,
2502 	.pr_abort	= tcp_abort,
2503 	.pr_ioctl	= tcp_ioctl,
2504 	.pr_stat	= tcp_stat,
2505 	.pr_peeraddr	= tcp_peeraddr,
2506 	.pr_sockaddr	= tcp_sockaddr,
2507 	.pr_rcvd	= tcp_rcvd,
2508 	.pr_recvoob	= tcp_recvoob,
2509 	.pr_send	= tcp_send,
2510 	.pr_sendoob	= tcp_sendoob,
2511 	.pr_purgeif	= tcp_purgeif,
2512 };
2513