xref: /netbsd-src/sys/netinet/tcp_usrreq.c (revision 2e2322c9c07009df921d11b1268f8506affbb8ba)
1 /*	$NetBSD: tcp_usrreq.c,v 1.213 2016/11/18 06:50:04 knakahara 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.213 2016/11/18 06:50:04 knakahara 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 #include "opt_net_mpsafe.h"
110 #endif
111 
112 #include <sys/param.h>
113 #include <sys/systm.h>
114 #include <sys/kernel.h>
115 #include <sys/mbuf.h>
116 #include <sys/socket.h>
117 #include <sys/socketvar.h>
118 #include <sys/protosw.h>
119 #include <sys/errno.h>
120 #include <sys/stat.h>
121 #include <sys/proc.h>
122 #include <sys/domain.h>
123 #include <sys/sysctl.h>
124 #include <sys/kauth.h>
125 #include <sys/kernel.h>
126 #include <sys/uidinfo.h>
127 
128 #include <net/if.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 	int error = 0;
1210 
1211 	s = splsoftnet();
1212 
1213 #ifndef NET_MPSAFE
1214 	mutex_enter(softnet_lock);
1215 #endif
1216 	switch (so->so_proto->pr_domain->dom_family) {
1217 #ifdef INET
1218 	case PF_INET:
1219 		in_pcbpurgeif0(&tcbtable, ifp);
1220 		in_purgeif(ifp);
1221 		in_pcbpurgeif(&tcbtable, ifp);
1222 		break;
1223 #endif
1224 #ifdef INET6
1225 	case PF_INET6:
1226 		in6_pcbpurgeif0(&tcbtable, ifp);
1227 		in6_purgeif(ifp);
1228 		in6_pcbpurgeif(&tcbtable, ifp);
1229 		break;
1230 #endif
1231 	default:
1232 		error = EAFNOSUPPORT;
1233 		break;
1234 	}
1235 #ifndef NET_MPSAFE
1236 	mutex_exit(softnet_lock);
1237 #endif
1238 	splx(s);
1239 
1240 	return error;
1241 }
1242 
1243 /*
1244  * Initiate (or continue) disconnect.
1245  * If embryonic state, just send reset (once).
1246  * If in ``let data drain'' option and linger null, just drop.
1247  * Otherwise (hard), mark socket disconnecting and drop
1248  * current input data; switch states based on user close, and
1249  * send segment to peer (with FIN).
1250  */
1251 struct tcpcb *
1252 tcp_disconnect1(struct tcpcb *tp)
1253 {
1254 	struct socket *so;
1255 
1256 	if (tp->t_inpcb)
1257 		so = tp->t_inpcb->inp_socket;
1258 #ifdef INET6
1259 	else if (tp->t_in6pcb)
1260 		so = tp->t_in6pcb->in6p_socket;
1261 #endif
1262 	else
1263 		so = NULL;
1264 
1265 	if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
1266 		tp = tcp_close(tp);
1267 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1268 		tp = tcp_drop(tp, 0);
1269 	else {
1270 		soisdisconnecting(so);
1271 		sbflush(&so->so_rcv);
1272 		tp = tcp_usrclosed(tp);
1273 		if (tp)
1274 			(void) tcp_output(tp);
1275 	}
1276 	return (tp);
1277 }
1278 
1279 /*
1280  * User issued close, and wish to trail through shutdown states:
1281  * if never received SYN, just forget it.  If got a SYN from peer,
1282  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1283  * If already got a FIN from peer, then almost done; go to LAST_ACK
1284  * state.  In all other cases, have already sent FIN to peer (e.g.
1285  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1286  * for peer to send FIN or not respond to keep-alives, etc.
1287  * We can let the user exit from the close as soon as the FIN is acked.
1288  */
1289 struct tcpcb *
1290 tcp_usrclosed(struct tcpcb *tp)
1291 {
1292 
1293 	switch (tp->t_state) {
1294 
1295 	case TCPS_CLOSED:
1296 	case TCPS_LISTEN:
1297 	case TCPS_SYN_SENT:
1298 		tp->t_state = TCPS_CLOSED;
1299 		tp = tcp_close(tp);
1300 		break;
1301 
1302 	case TCPS_SYN_RECEIVED:
1303 	case TCPS_ESTABLISHED:
1304 		tp->t_state = TCPS_FIN_WAIT_1;
1305 		break;
1306 
1307 	case TCPS_CLOSE_WAIT:
1308 		tp->t_state = TCPS_LAST_ACK;
1309 		break;
1310 	}
1311 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1312 		struct socket *so;
1313 		if (tp->t_inpcb)
1314 			so = tp->t_inpcb->inp_socket;
1315 #ifdef INET6
1316 		else if (tp->t_in6pcb)
1317 			so = tp->t_in6pcb->in6p_socket;
1318 #endif
1319 		else
1320 			so = NULL;
1321 		if (so)
1322 			soisdisconnected(so);
1323 		/*
1324 		 * If we are in FIN_WAIT_2, we arrived here because the
1325 		 * application did a shutdown of the send side.  Like the
1326 		 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after
1327 		 * a full close, we start a timer to make sure sockets are
1328 		 * not left in FIN_WAIT_2 forever.
1329 		 */
1330 		if ((tp->t_state == TCPS_FIN_WAIT_2) && (tp->t_maxidle > 0))
1331 			TCP_TIMER_ARM(tp, TCPT_2MSL, tp->t_maxidle);
1332 		else if (tp->t_state == TCPS_TIME_WAIT
1333 			 && ((tp->t_inpcb
1334 			      && (tcp4_vtw_enable & 1)
1335 			      && vtw_add(AF_INET, tp))
1336 			     ||
1337 			     (tp->t_in6pcb
1338 			      && (tcp6_vtw_enable & 1)
1339 			      && vtw_add(AF_INET6, tp)))) {
1340 			tp = 0;
1341 		}
1342 	}
1343 	return (tp);
1344 }
1345 
1346 /*
1347  * sysctl helper routine for net.inet.ip.mssdflt.  it can't be less
1348  * than 32.
1349  */
1350 static int
1351 sysctl_net_inet_tcp_mssdflt(SYSCTLFN_ARGS)
1352 {
1353 	int error, mssdflt;
1354 	struct sysctlnode node;
1355 
1356 	mssdflt = tcp_mssdflt;
1357 	node = *rnode;
1358 	node.sysctl_data = &mssdflt;
1359 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1360 	if (error || newp == NULL)
1361 		return (error);
1362 
1363 	if (mssdflt < 32)
1364 		return (EINVAL);
1365 	tcp_mssdflt = mssdflt;
1366 
1367 	mutex_enter(softnet_lock);
1368 	tcp_tcpcb_template();
1369 	mutex_exit(softnet_lock);
1370 
1371 	return (0);
1372 }
1373 
1374 /*
1375  * sysctl helper for TCP CB template update
1376  */
1377 static int
1378 sysctl_update_tcpcb_template(SYSCTLFN_ARGS)
1379 {
1380 	int t, error;
1381 	struct sysctlnode node;
1382 
1383 	/* follow procedures in sysctl(9) manpage */
1384 	t = *(int *)rnode->sysctl_data;
1385 	node = *rnode;
1386 	node.sysctl_data = &t;
1387 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1388 	if (error || newp == NULL)
1389 		return error;
1390 
1391 	if (t < 0)
1392 		return EINVAL;
1393 
1394 	*(int *)rnode->sysctl_data = t;
1395 
1396 	mutex_enter(softnet_lock);
1397 	tcp_tcpcb_template();
1398 	mutex_exit(softnet_lock);
1399 
1400 	return 0;
1401 }
1402 
1403 /*
1404  * sysctl helper routine for setting port related values under
1405  * net.inet.ip and net.inet6.ip6.  does basic range checking and does
1406  * additional checks for each type.  this code has placed in
1407  * tcp_input.c since INET and INET6 both use the same tcp code.
1408  *
1409  * this helper is not static so that both inet and inet6 can use it.
1410  */
1411 int
1412 sysctl_net_inet_ip_ports(SYSCTLFN_ARGS)
1413 {
1414 	int error, tmp;
1415 	int apmin, apmax;
1416 #ifndef IPNOPRIVPORTS
1417 	int lpmin, lpmax;
1418 #endif /* IPNOPRIVPORTS */
1419 	struct sysctlnode node;
1420 
1421 	if (namelen != 0)
1422 		return (EINVAL);
1423 
1424 	switch (name[-3]) {
1425 #ifdef INET
1426 	    case PF_INET:
1427 		apmin = anonportmin;
1428 		apmax = anonportmax;
1429 #ifndef IPNOPRIVPORTS
1430 		lpmin = lowportmin;
1431 		lpmax = lowportmax;
1432 #endif /* IPNOPRIVPORTS */
1433 		break;
1434 #endif /* INET */
1435 #ifdef INET6
1436 	    case PF_INET6:
1437 		apmin = ip6_anonportmin;
1438 		apmax = ip6_anonportmax;
1439 #ifndef IPNOPRIVPORTS
1440 		lpmin = ip6_lowportmin;
1441 		lpmax = ip6_lowportmax;
1442 #endif /* IPNOPRIVPORTS */
1443 		break;
1444 #endif /* INET6 */
1445 	    default:
1446 		return (EINVAL);
1447 	}
1448 
1449 	/*
1450 	 * insert temporary copy into node, perform lookup on
1451 	 * temporary, then restore pointer
1452 	 */
1453 	node = *rnode;
1454 	tmp = *(int*)rnode->sysctl_data;
1455 	node.sysctl_data = &tmp;
1456 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1457 	if (error || newp == NULL)
1458 		return (error);
1459 
1460 	/*
1461 	 * simple port range check
1462 	 */
1463 	if (tmp < 0 || tmp > 65535)
1464 		return (EINVAL);
1465 
1466 	/*
1467 	 * per-node range checks
1468 	 */
1469 	switch (rnode->sysctl_num) {
1470 	case IPCTL_ANONPORTMIN:
1471 	case IPV6CTL_ANONPORTMIN:
1472 		if (tmp >= apmax)
1473 			return (EINVAL);
1474 #ifndef IPNOPRIVPORTS
1475 		if (tmp < IPPORT_RESERVED)
1476                         return (EINVAL);
1477 #endif /* IPNOPRIVPORTS */
1478 		break;
1479 
1480 	case IPCTL_ANONPORTMAX:
1481 	case IPV6CTL_ANONPORTMAX:
1482                 if (apmin >= tmp)
1483 			return (EINVAL);
1484 #ifndef IPNOPRIVPORTS
1485 		if (tmp < IPPORT_RESERVED)
1486                         return (EINVAL);
1487 #endif /* IPNOPRIVPORTS */
1488 		break;
1489 
1490 #ifndef IPNOPRIVPORTS
1491 	case IPCTL_LOWPORTMIN:
1492 	case IPV6CTL_LOWPORTMIN:
1493 		if (tmp >= lpmax ||
1494 		    tmp > IPPORT_RESERVEDMAX ||
1495 		    tmp < IPPORT_RESERVEDMIN)
1496 			return (EINVAL);
1497 		break;
1498 
1499 	case IPCTL_LOWPORTMAX:
1500 	case IPV6CTL_LOWPORTMAX:
1501 		if (lpmin >= tmp ||
1502 		    tmp > IPPORT_RESERVEDMAX ||
1503 		    tmp < IPPORT_RESERVEDMIN)
1504 			return (EINVAL);
1505 		break;
1506 #endif /* IPNOPRIVPORTS */
1507 
1508 	default:
1509 		return (EINVAL);
1510 	}
1511 
1512 	*(int*)rnode->sysctl_data = tmp;
1513 
1514 	return (0);
1515 }
1516 
1517 static inline int
1518 copyout_uid(struct socket *sockp, void *oldp, size_t *oldlenp)
1519 {
1520 	if (oldp) {
1521 		size_t sz;
1522 		uid_t uid;
1523 		int error;
1524 
1525 		if (sockp->so_cred == NULL)
1526 			return EPERM;
1527 
1528 		uid = kauth_cred_geteuid(sockp->so_cred);
1529 		sz = MIN(sizeof(uid), *oldlenp);
1530 		if ((error = copyout(&uid, oldp, sz)) != 0)
1531 			return error;
1532 	}
1533 	*oldlenp = sizeof(uid_t);
1534 	return 0;
1535 }
1536 
1537 static inline int
1538 inet4_ident_core(struct in_addr raddr, u_int rport,
1539     struct in_addr laddr, u_int lport,
1540     void *oldp, size_t *oldlenp,
1541     struct lwp *l, int dodrop)
1542 {
1543 	struct inpcb *inp;
1544 	struct socket *sockp;
1545 
1546 	inp = in_pcblookup_connect(&tcbtable, raddr, rport, laddr, lport, 0);
1547 
1548 	if (inp == NULL || (sockp = inp->inp_socket) == NULL)
1549 		return ESRCH;
1550 
1551 	if (dodrop) {
1552 		struct tcpcb *tp;
1553 		int error;
1554 
1555 		if (inp == NULL || (tp = intotcpcb(inp)) == NULL ||
1556 		    (inp->inp_socket->so_options & SO_ACCEPTCONN) != 0)
1557 			return ESRCH;
1558 
1559 		error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
1560 		    KAUTH_REQ_NETWORK_SOCKET_DROP, inp->inp_socket, tp, NULL);
1561 		if (error)
1562 			return (error);
1563 
1564 		(void)tcp_drop(tp, ECONNABORTED);
1565 		return 0;
1566 	}
1567 	else
1568 		return copyout_uid(sockp, oldp, oldlenp);
1569 }
1570 
1571 #ifdef INET6
1572 static inline int
1573 inet6_ident_core(struct in6_addr *raddr, u_int rport,
1574     struct in6_addr *laddr, u_int lport,
1575     void *oldp, size_t *oldlenp,
1576     struct lwp *l, int dodrop)
1577 {
1578 	struct in6pcb *in6p;
1579 	struct socket *sockp;
1580 
1581 	in6p = in6_pcblookup_connect(&tcbtable, raddr, rport, laddr, lport, 0, 0);
1582 
1583 	if (in6p == NULL || (sockp = in6p->in6p_socket) == NULL)
1584 		return ESRCH;
1585 
1586 	if (dodrop) {
1587 		struct tcpcb *tp;
1588 		int error;
1589 
1590 		if (in6p == NULL || (tp = in6totcpcb(in6p)) == NULL ||
1591 		    (in6p->in6p_socket->so_options & SO_ACCEPTCONN) != 0)
1592 			return ESRCH;
1593 
1594 		error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
1595 		    KAUTH_REQ_NETWORK_SOCKET_DROP, in6p->in6p_socket, tp, NULL);
1596 		if (error)
1597 			return (error);
1598 
1599 		(void)tcp_drop(tp, ECONNABORTED);
1600 		return 0;
1601 	}
1602 	else
1603 		return copyout_uid(sockp, oldp, oldlenp);
1604 }
1605 #endif
1606 
1607 /*
1608  * sysctl helper routine for the net.inet.tcp.drop and
1609  * net.inet6.tcp6.drop nodes.
1610  */
1611 #define sysctl_net_inet_tcp_drop sysctl_net_inet_tcp_ident
1612 
1613 /*
1614  * sysctl helper routine for the net.inet.tcp.ident and
1615  * net.inet6.tcp6.ident nodes.  contains backwards compat code for the
1616  * old way of looking up the ident information for ipv4 which involves
1617  * stuffing the port/addr pairs into the mib lookup.
1618  */
1619 static int
1620 sysctl_net_inet_tcp_ident(SYSCTLFN_ARGS)
1621 {
1622 #ifdef INET
1623 	struct sockaddr_in *si4[2];
1624 #endif /* INET */
1625 #ifdef INET6
1626 	struct sockaddr_in6 *si6[2];
1627 #endif /* INET6 */
1628 	struct sockaddr_storage sa[2];
1629 	int error, pf, dodrop;
1630 
1631 	dodrop = name[-1] == TCPCTL_DROP;
1632 	if (dodrop) {
1633 		if (oldp != NULL || *oldlenp != 0)
1634 			return EINVAL;
1635 		if (newp == NULL)
1636 			return EPERM;
1637 		if (newlen < sizeof(sa))
1638 			return ENOMEM;
1639 	}
1640 	if (namelen != 4 && namelen != 0)
1641 		return EINVAL;
1642 	if (name[-2] != IPPROTO_TCP)
1643 		return EINVAL;
1644 	pf = name[-3];
1645 
1646 	/* old style lookup, ipv4 only */
1647 	if (namelen == 4) {
1648 #ifdef INET
1649 		struct in_addr laddr, raddr;
1650 		u_int lport, rport;
1651 
1652 		if (pf != PF_INET)
1653 			return EPROTONOSUPPORT;
1654 		raddr.s_addr = (uint32_t)name[0];
1655 		rport = (u_int)name[1];
1656 		laddr.s_addr = (uint32_t)name[2];
1657 		lport = (u_int)name[3];
1658 
1659 		mutex_enter(softnet_lock);
1660 		error = inet4_ident_core(raddr, rport, laddr, lport,
1661 		    oldp, oldlenp, l, dodrop);
1662 		mutex_exit(softnet_lock);
1663 		return error;
1664 #else /* INET */
1665 		return EINVAL;
1666 #endif /* INET */
1667 	}
1668 
1669 	if (newp == NULL || newlen != sizeof(sa))
1670 		return EINVAL;
1671 	error = copyin(newp, &sa, newlen);
1672 	if (error)
1673 		return error;
1674 
1675 	/*
1676 	 * requested families must match
1677 	 */
1678 	if (pf != sa[0].ss_family || sa[0].ss_family != sa[1].ss_family)
1679 		return EINVAL;
1680 
1681 	switch (pf) {
1682 #ifdef INET6
1683 	case PF_INET6:
1684 		si6[0] = (struct sockaddr_in6*)&sa[0];
1685 		si6[1] = (struct sockaddr_in6*)&sa[1];
1686 		if (si6[0]->sin6_len != sizeof(*si6[0]) ||
1687 		    si6[1]->sin6_len != sizeof(*si6[1]))
1688 			return EINVAL;
1689 
1690 		if (!IN6_IS_ADDR_V4MAPPED(&si6[0]->sin6_addr) &&
1691 		    !IN6_IS_ADDR_V4MAPPED(&si6[1]->sin6_addr)) {
1692 			error = sa6_embedscope(si6[0], ip6_use_defzone);
1693 			if (error)
1694 				return error;
1695 			error = sa6_embedscope(si6[1], ip6_use_defzone);
1696 			if (error)
1697 				return error;
1698 
1699 			mutex_enter(softnet_lock);
1700 			error = inet6_ident_core(&si6[0]->sin6_addr,
1701 			    si6[0]->sin6_port, &si6[1]->sin6_addr,
1702 			    si6[1]->sin6_port, oldp, oldlenp, l, dodrop);
1703 			mutex_exit(softnet_lock);
1704 			return error;
1705 		}
1706 
1707 		if (IN6_IS_ADDR_V4MAPPED(&si6[0]->sin6_addr) !=
1708 		    IN6_IS_ADDR_V4MAPPED(&si6[1]->sin6_addr))
1709 			return EINVAL;
1710 
1711 		in6_sin6_2_sin_in_sock((struct sockaddr *)&sa[0]);
1712 		in6_sin6_2_sin_in_sock((struct sockaddr *)&sa[1]);
1713 		/*FALLTHROUGH*/
1714 #endif /* INET6 */
1715 #ifdef INET
1716 	case PF_INET:
1717 		si4[0] = (struct sockaddr_in*)&sa[0];
1718 		si4[1] = (struct sockaddr_in*)&sa[1];
1719 		if (si4[0]->sin_len != sizeof(*si4[0]) ||
1720 		    si4[0]->sin_len != sizeof(*si4[1]))
1721 			return EINVAL;
1722 
1723 		mutex_enter(softnet_lock);
1724 		error = inet4_ident_core(si4[0]->sin_addr, si4[0]->sin_port,
1725 		    si4[1]->sin_addr, si4[1]->sin_port,
1726 		    oldp, oldlenp, l, dodrop);
1727 		mutex_exit(softnet_lock);
1728 		return error;
1729 #endif /* INET */
1730 	default:
1731 		return EPROTONOSUPPORT;
1732 	}
1733 }
1734 
1735 /*
1736  * sysctl helper for the inet and inet6 pcblists.  handles tcp/udp and
1737  * inet/inet6, as well as raw pcbs for each.  specifically not
1738  * declared static so that raw sockets and udp/udp6 can use it as
1739  * well.
1740  */
1741 int
1742 sysctl_inpcblist(SYSCTLFN_ARGS)
1743 {
1744 #ifdef INET
1745 	struct sockaddr_in *in;
1746 	const struct inpcb *inp;
1747 #endif
1748 #ifdef INET6
1749 	struct sockaddr_in6 *in6;
1750 	const struct in6pcb *in6p;
1751 #endif
1752 	struct inpcbtable *pcbtbl = __UNCONST(rnode->sysctl_data);
1753 	const struct inpcb_hdr *inph;
1754 	struct tcpcb *tp;
1755 	struct kinfo_pcb pcb;
1756 	char *dp;
1757 	size_t len, needed, elem_size, out_size;
1758 	int error, elem_count, pf, proto, pf2;
1759 
1760 	if (namelen != 4)
1761 		return (EINVAL);
1762 
1763 	if (oldp != NULL) {
1764 		    len = *oldlenp;
1765 		    elem_size = name[2];
1766 		    elem_count = name[3];
1767 		    if (elem_size != sizeof(pcb))
1768 			    return EINVAL;
1769 	} else {
1770 		    len = 0;
1771 		    elem_count = INT_MAX;
1772 		    elem_size = sizeof(pcb);
1773 	}
1774 	error = 0;
1775 	dp = oldp;
1776 	out_size = elem_size;
1777 	needed = 0;
1778 
1779 	if (namelen == 1 && name[0] == CTL_QUERY)
1780 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
1781 
1782 	if (name - oname != 4)
1783 		return (EINVAL);
1784 
1785 	pf = oname[1];
1786 	proto = oname[2];
1787 	pf2 = (oldp != NULL) ? pf : 0;
1788 
1789 	mutex_enter(softnet_lock);
1790 
1791 	TAILQ_FOREACH(inph, &pcbtbl->inpt_queue, inph_queue) {
1792 #ifdef INET
1793 		inp = (const struct inpcb *)inph;
1794 #endif
1795 #ifdef INET6
1796 		in6p = (const struct in6pcb *)inph;
1797 #endif
1798 
1799 		if (inph->inph_af != pf)
1800 			continue;
1801 
1802 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
1803 		    KAUTH_REQ_NETWORK_SOCKET_CANSEE, inph->inph_socket, NULL,
1804 		    NULL) != 0)
1805 			continue;
1806 
1807 		memset(&pcb, 0, sizeof(pcb));
1808 
1809 		pcb.ki_family = pf;
1810 		pcb.ki_type = proto;
1811 
1812 		switch (pf2) {
1813 		case 0:
1814 			/* just probing for size */
1815 			break;
1816 #ifdef INET
1817 		case PF_INET:
1818 			pcb.ki_family = inp->inp_socket->so_proto->
1819 			    pr_domain->dom_family;
1820 			pcb.ki_type = inp->inp_socket->so_proto->
1821 			    pr_type;
1822 			pcb.ki_protocol = inp->inp_socket->so_proto->
1823 			    pr_protocol;
1824 			pcb.ki_pflags = inp->inp_flags;
1825 
1826 			pcb.ki_sostate = inp->inp_socket->so_state;
1827 			pcb.ki_prstate = inp->inp_state;
1828 			if (proto == IPPROTO_TCP) {
1829 				tp = intotcpcb(inp);
1830 				pcb.ki_tstate = tp->t_state;
1831 				pcb.ki_tflags = tp->t_flags;
1832 			}
1833 
1834 			pcb.ki_pcbaddr = PTRTOUINT64(inp);
1835 			pcb.ki_ppcbaddr = PTRTOUINT64(inp->inp_ppcb);
1836 			pcb.ki_sockaddr = PTRTOUINT64(inp->inp_socket);
1837 
1838 			pcb.ki_rcvq = inp->inp_socket->so_rcv.sb_cc;
1839 			pcb.ki_sndq = inp->inp_socket->so_snd.sb_cc;
1840 
1841 			in = satosin(&pcb.ki_src);
1842 			in->sin_len = sizeof(*in);
1843 			in->sin_family = pf;
1844 			in->sin_port = inp->inp_lport;
1845 			in->sin_addr = inp->inp_laddr;
1846 			if (pcb.ki_prstate >= INP_CONNECTED) {
1847 				in = satosin(&pcb.ki_dst);
1848 				in->sin_len = sizeof(*in);
1849 				in->sin_family = pf;
1850 				in->sin_port = inp->inp_fport;
1851 				in->sin_addr = inp->inp_faddr;
1852 			}
1853 			break;
1854 #endif
1855 #ifdef INET6
1856 		case PF_INET6:
1857 			pcb.ki_family = in6p->in6p_socket->so_proto->
1858 			    pr_domain->dom_family;
1859 			pcb.ki_type = in6p->in6p_socket->so_proto->pr_type;
1860 			pcb.ki_protocol = in6p->in6p_socket->so_proto->
1861 			    pr_protocol;
1862 			pcb.ki_pflags = in6p->in6p_flags;
1863 
1864 			pcb.ki_sostate = in6p->in6p_socket->so_state;
1865 			pcb.ki_prstate = in6p->in6p_state;
1866 			if (proto == IPPROTO_TCP) {
1867 				tp = in6totcpcb(in6p);
1868 				pcb.ki_tstate = tp->t_state;
1869 				pcb.ki_tflags = tp->t_flags;
1870 			}
1871 
1872 			pcb.ki_pcbaddr = PTRTOUINT64(in6p);
1873 			pcb.ki_ppcbaddr = PTRTOUINT64(in6p->in6p_ppcb);
1874 			pcb.ki_sockaddr = PTRTOUINT64(in6p->in6p_socket);
1875 
1876 			pcb.ki_rcvq = in6p->in6p_socket->so_rcv.sb_cc;
1877 			pcb.ki_sndq = in6p->in6p_socket->so_snd.sb_cc;
1878 
1879 			in6 = satosin6(&pcb.ki_src);
1880 			in6->sin6_len = sizeof(*in6);
1881 			in6->sin6_family = pf;
1882 			in6->sin6_port = in6p->in6p_lport;
1883 			in6->sin6_flowinfo = in6p->in6p_flowinfo;
1884 			in6->sin6_addr = in6p->in6p_laddr;
1885 			in6->sin6_scope_id = 0; /* XXX? */
1886 
1887 			if (pcb.ki_prstate >= IN6P_CONNECTED) {
1888 				in6 = satosin6(&pcb.ki_dst);
1889 				in6->sin6_len = sizeof(*in6);
1890 				in6->sin6_family = pf;
1891 				in6->sin6_port = in6p->in6p_fport;
1892 				in6->sin6_flowinfo = in6p->in6p_flowinfo;
1893 				in6->sin6_addr = in6p->in6p_faddr;
1894 				in6->sin6_scope_id = 0; /* XXX? */
1895 			}
1896 			break;
1897 #endif
1898 		}
1899 
1900 		if (len >= elem_size && elem_count > 0) {
1901 			error = copyout(&pcb, dp, out_size);
1902 			if (error) {
1903 				mutex_exit(softnet_lock);
1904 				return (error);
1905 			}
1906 			dp += elem_size;
1907 			len -= elem_size;
1908 		}
1909 		needed += elem_size;
1910 		if (elem_count > 0 && elem_count != INT_MAX)
1911 			elem_count--;
1912 	}
1913 
1914 	*oldlenp = needed;
1915 	if (oldp == NULL)
1916 		*oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
1917 
1918 	mutex_exit(softnet_lock);
1919 
1920 	return (error);
1921 }
1922 
1923 static int
1924 sysctl_tcp_congctl(SYSCTLFN_ARGS)
1925 {
1926 	struct sysctlnode node;
1927 	int error;
1928 	char newname[TCPCC_MAXLEN];
1929 
1930 	strlcpy(newname, tcp_congctl_global_name, sizeof(newname) - 1);
1931 
1932 	node = *rnode;
1933 	node.sysctl_data = newname;
1934 	node.sysctl_size = sizeof(newname);
1935 
1936 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1937 
1938 	if (error ||
1939 	    newp == NULL ||
1940 	    strncmp(newname, tcp_congctl_global_name, sizeof(newname)) == 0)
1941 		return error;
1942 
1943 	mutex_enter(softnet_lock);
1944 	error = tcp_congctl_select(NULL, newname);
1945 	mutex_exit(softnet_lock);
1946 
1947 	return error;
1948 }
1949 
1950 static int
1951 sysctl_tcp_init_win(SYSCTLFN_ARGS)
1952 {
1953 	int error;
1954 	u_int iw;
1955 	struct sysctlnode node;
1956 
1957 	iw = *(u_int *)rnode->sysctl_data;
1958 	node = *rnode;
1959 	node.sysctl_data = &iw;
1960 	node.sysctl_size = sizeof(iw);
1961 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1962 	if (error || newp == NULL)
1963 		return error;
1964 
1965 	if (iw >= __arraycount(tcp_init_win_max))
1966 		return EINVAL;
1967 	*(u_int *)rnode->sysctl_data = iw;
1968 	return 0;
1969 }
1970 
1971 static int
1972 sysctl_tcp_keep(SYSCTLFN_ARGS)
1973 {
1974 	int error;
1975 	u_int tmp;
1976 	struct sysctlnode node;
1977 
1978 	node = *rnode;
1979 	tmp = *(u_int *)rnode->sysctl_data;
1980 	node.sysctl_data = &tmp;
1981 
1982 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1983 	if (error || newp == NULL)
1984 		return error;
1985 
1986 	mutex_enter(softnet_lock);
1987 
1988 	*(u_int *)rnode->sysctl_data = tmp;
1989 	tcp_tcpcb_template();	/* update the template */
1990 
1991 	mutex_exit(softnet_lock);
1992 	return 0;
1993 }
1994 
1995 static int
1996 sysctl_net_inet_tcp_stats(SYSCTLFN_ARGS)
1997 {
1998 
1999 	return (NETSTAT_SYSCTL(tcpstat_percpu, TCP_NSTATS));
2000 }
2001 
2002 /*
2003  * this (second stage) setup routine is a replacement for tcp_sysctl()
2004  * (which is currently used for ipv4 and ipv6)
2005  */
2006 static void
2007 sysctl_net_inet_tcp_setup2(struct sysctllog **clog, int pf, const char *pfname,
2008 			   const char *tcpname)
2009 {
2010 	const struct sysctlnode *sack_node;
2011 	const struct sysctlnode *abc_node;
2012 	const struct sysctlnode *ecn_node;
2013 	const struct sysctlnode *congctl_node;
2014 	const struct sysctlnode *mslt_node;
2015 	const struct sysctlnode *vtw_node;
2016 #ifdef TCP_DEBUG
2017 	extern struct tcp_debug tcp_debug[TCP_NDEBUG];
2018 	extern int tcp_debx;
2019 #endif
2020 
2021 	sysctl_createv(clog, 0, NULL, NULL,
2022 		       CTLFLAG_PERMANENT,
2023 		       CTLTYPE_NODE, pfname, NULL,
2024 		       NULL, 0, NULL, 0,
2025 		       CTL_NET, pf, CTL_EOL);
2026 	sysctl_createv(clog, 0, NULL, NULL,
2027 		       CTLFLAG_PERMANENT,
2028 		       CTLTYPE_NODE, tcpname,
2029 		       SYSCTL_DESCR("TCP related settings"),
2030 		       NULL, 0, NULL, 0,
2031 		       CTL_NET, pf, IPPROTO_TCP, CTL_EOL);
2032 
2033 	sysctl_createv(clog, 0, NULL, NULL,
2034 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2035 		       CTLTYPE_INT, "rfc1323",
2036 		       SYSCTL_DESCR("Enable RFC1323 TCP extensions"),
2037 		       sysctl_update_tcpcb_template, 0, &tcp_do_rfc1323, 0,
2038 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RFC1323, CTL_EOL);
2039 	sysctl_createv(clog, 0, NULL, NULL,
2040 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2041 		       CTLTYPE_INT, "sendspace",
2042 		       SYSCTL_DESCR("Default TCP send buffer size"),
2043 		       NULL, 0, &tcp_sendspace, 0,
2044 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SENDSPACE, CTL_EOL);
2045 	sysctl_createv(clog, 0, NULL, NULL,
2046 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2047 		       CTLTYPE_INT, "recvspace",
2048 		       SYSCTL_DESCR("Default TCP receive buffer size"),
2049 		       NULL, 0, &tcp_recvspace, 0,
2050 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RECVSPACE, CTL_EOL);
2051 	sysctl_createv(clog, 0, NULL, NULL,
2052 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2053 		       CTLTYPE_INT, "mssdflt",
2054 		       SYSCTL_DESCR("Default maximum segment size"),
2055 		       sysctl_net_inet_tcp_mssdflt, 0, &tcp_mssdflt, 0,
2056 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSSDFLT, CTL_EOL);
2057 	sysctl_createv(clog, 0, NULL, NULL,
2058 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2059 		       CTLTYPE_INT, "minmss",
2060 		       SYSCTL_DESCR("Lower limit for TCP maximum segment size"),
2061 		       NULL, 0, &tcp_minmss, 0,
2062 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2063 	sysctl_createv(clog, 0, NULL, NULL,
2064 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2065 		       CTLTYPE_INT, "msl",
2066 		       SYSCTL_DESCR("Maximum Segment Life"),
2067 		       NULL, 0, &tcp_msl, 0,
2068 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSL, CTL_EOL);
2069 	sysctl_createv(clog, 0, NULL, NULL,
2070 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2071 		       CTLTYPE_INT, "syn_cache_limit",
2072 		       SYSCTL_DESCR("Maximum number of entries in the TCP "
2073 				    "compressed state engine"),
2074 		       NULL, 0, &tcp_syn_cache_limit, 0,
2075 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_LIMIT,
2076 		       CTL_EOL);
2077 	sysctl_createv(clog, 0, NULL, NULL,
2078 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2079 		       CTLTYPE_INT, "syn_bucket_limit",
2080 		       SYSCTL_DESCR("Maximum number of entries per hash "
2081 				    "bucket in the TCP compressed state "
2082 				    "engine"),
2083 		       NULL, 0, &tcp_syn_bucket_limit, 0,
2084 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_BUCKET_LIMIT,
2085 		       CTL_EOL);
2086 #if 0 /* obsoleted */
2087 	sysctl_createv(clog, 0, NULL, NULL,
2088 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2089 		       CTLTYPE_INT, "syn_cache_interval",
2090 		       SYSCTL_DESCR("TCP compressed state engine's timer interval"),
2091 		       NULL, 0, &tcp_syn_cache_interval, 0,
2092 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_INTER,
2093 		       CTL_EOL);
2094 #endif
2095 	sysctl_createv(clog, 0, NULL, NULL,
2096 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2097 		       CTLTYPE_INT, "init_win",
2098 		       SYSCTL_DESCR("Initial TCP congestion window"),
2099 		       sysctl_tcp_init_win, 0, &tcp_init_win, 0,
2100 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN, CTL_EOL);
2101 	sysctl_createv(clog, 0, NULL, NULL,
2102 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2103 		       CTLTYPE_INT, "mss_ifmtu",
2104 		       SYSCTL_DESCR("Use interface MTU for calculating MSS"),
2105 		       NULL, 0, &tcp_mss_ifmtu, 0,
2106 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSS_IFMTU, CTL_EOL);
2107 	sysctl_createv(clog, 0, NULL, &sack_node,
2108 		       CTLFLAG_PERMANENT,
2109 		       CTLTYPE_NODE, "sack",
2110 		       SYSCTL_DESCR("RFC2018 Selective ACKnowledgement tunables"),
2111 		       NULL, 0, NULL, 0,
2112 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_EOL);
2113 
2114 	/* Congctl subtree */
2115 	sysctl_createv(clog, 0, NULL, &congctl_node,
2116 		       CTLFLAG_PERMANENT,
2117 		       CTLTYPE_NODE, "congctl",
2118 		       SYSCTL_DESCR("TCP Congestion Control"),
2119 	    	       NULL, 0, NULL, 0,
2120 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2121 	sysctl_createv(clog, 0, &congctl_node, NULL,
2122 		       CTLFLAG_PERMANENT,
2123 		       CTLTYPE_STRING, "available",
2124 		       SYSCTL_DESCR("Available Congestion Control Mechanisms"),
2125 		       NULL, 0, tcp_congctl_avail, 0, CTL_CREATE, CTL_EOL);
2126 	sysctl_createv(clog, 0, &congctl_node, NULL,
2127 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2128 		       CTLTYPE_STRING, "selected",
2129 		       SYSCTL_DESCR("Selected Congestion Control Mechanism"),
2130 		       sysctl_tcp_congctl, 0, NULL, TCPCC_MAXLEN,
2131 		       CTL_CREATE, CTL_EOL);
2132 
2133 	sysctl_createv(clog, 0, NULL, NULL,
2134 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2135 		       CTLTYPE_INT, "win_scale",
2136 		       SYSCTL_DESCR("Use RFC1323 window scale options"),
2137 		       sysctl_update_tcpcb_template, 0, &tcp_do_win_scale, 0,
2138 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_WSCALE, CTL_EOL);
2139 	sysctl_createv(clog, 0, NULL, NULL,
2140 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2141 		       CTLTYPE_INT, "timestamps",
2142 		       SYSCTL_DESCR("Use RFC1323 time stamp options"),
2143 		       sysctl_update_tcpcb_template, 0, &tcp_do_timestamps, 0,
2144 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_TSTAMP, CTL_EOL);
2145 	sysctl_createv(clog, 0, NULL, NULL,
2146 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2147 		       CTLTYPE_INT, "compat_42",
2148 		       SYSCTL_DESCR("Enable workarounds for 4.2BSD TCP bugs"),
2149 		       NULL, 0, &tcp_compat_42, 0,
2150 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_COMPAT_42, CTL_EOL);
2151 	sysctl_createv(clog, 0, NULL, NULL,
2152 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2153 		       CTLTYPE_INT, "cwm",
2154 		       SYSCTL_DESCR("Hughes/Touch/Heidemann Congestion Window "
2155 				    "Monitoring"),
2156 		       NULL, 0, &tcp_cwm, 0,
2157 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM, CTL_EOL);
2158 	sysctl_createv(clog, 0, NULL, NULL,
2159 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2160 		       CTLTYPE_INT, "cwm_burstsize",
2161 		       SYSCTL_DESCR("Congestion Window Monitoring allowed "
2162 				    "burst count in packets"),
2163 		       NULL, 0, &tcp_cwm_burstsize, 0,
2164 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM_BURSTSIZE,
2165 		       CTL_EOL);
2166 	sysctl_createv(clog, 0, NULL, NULL,
2167 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2168 		       CTLTYPE_INT, "ack_on_push",
2169 		       SYSCTL_DESCR("Immediately return ACK when PSH is "
2170 				    "received"),
2171 		       NULL, 0, &tcp_ack_on_push, 0,
2172 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_ACK_ON_PUSH, CTL_EOL);
2173 	sysctl_createv(clog, 0, NULL, NULL,
2174 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2175 		       CTLTYPE_INT, "keepidle",
2176 		       SYSCTL_DESCR("Allowed connection idle ticks before a "
2177 				    "keepalive probe is sent"),
2178 		       sysctl_tcp_keep, 0, &tcp_keepidle, 0,
2179 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPIDLE, CTL_EOL);
2180 	sysctl_createv(clog, 0, NULL, NULL,
2181 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2182 		       CTLTYPE_INT, "keepintvl",
2183 		       SYSCTL_DESCR("Ticks before next keepalive probe is sent"),
2184 		       sysctl_tcp_keep, 0, &tcp_keepintvl, 0,
2185 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPINTVL, CTL_EOL);
2186 	sysctl_createv(clog, 0, NULL, NULL,
2187 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2188 		       CTLTYPE_INT, "keepcnt",
2189 		       SYSCTL_DESCR("Number of keepalive probes to send"),
2190 		       sysctl_tcp_keep, 0, &tcp_keepcnt, 0,
2191 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPCNT, CTL_EOL);
2192 	sysctl_createv(clog, 0, NULL, NULL,
2193 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
2194 		       CTLTYPE_INT, "slowhz",
2195 		       SYSCTL_DESCR("Keepalive ticks per second"),
2196 		       NULL, PR_SLOWHZ, NULL, 0,
2197 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SLOWHZ, CTL_EOL);
2198 	sysctl_createv(clog, 0, NULL, NULL,
2199 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2200 		       CTLTYPE_INT, "log_refused",
2201 		       SYSCTL_DESCR("Log refused TCP connections"),
2202 		       NULL, 0, &tcp_log_refused, 0,
2203 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOG_REFUSED, CTL_EOL);
2204 #if 0 /* obsoleted */
2205 	sysctl_createv(clog, 0, NULL, NULL,
2206 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2207 		       CTLTYPE_INT, "rstratelimit", NULL,
2208 		       NULL, 0, &tcp_rst_ratelim, 0,
2209 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTRATELIMIT, CTL_EOL);
2210 #endif
2211 	sysctl_createv(clog, 0, NULL, NULL,
2212 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2213 		       CTLTYPE_INT, "rstppslimit",
2214 		       SYSCTL_DESCR("Maximum number of RST packets to send "
2215 				    "per second"),
2216 		       NULL, 0, &tcp_rst_ppslim, 0,
2217 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTPPSLIMIT, CTL_EOL);
2218 	sysctl_createv(clog, 0, NULL, NULL,
2219 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2220 		       CTLTYPE_INT, "delack_ticks",
2221 		       SYSCTL_DESCR("Number of ticks to delay sending an ACK"),
2222 		       NULL, 0, &tcp_delack_ticks, 0,
2223 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DELACK_TICKS, CTL_EOL);
2224 	sysctl_createv(clog, 0, NULL, NULL,
2225 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2226 		       CTLTYPE_INT, "init_win_local",
2227 		       SYSCTL_DESCR("Initial TCP window size (in segments)"),
2228 		       sysctl_tcp_init_win, 0, &tcp_init_win_local, 0,
2229 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN_LOCAL,
2230 		       CTL_EOL);
2231 	sysctl_createv(clog, 0, NULL, NULL,
2232 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2233 		       CTLTYPE_STRUCT, "ident",
2234 		       SYSCTL_DESCR("RFC1413 Identification Protocol lookups"),
2235 		       sysctl_net_inet_tcp_ident, 0, NULL, sizeof(uid_t),
2236 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_IDENT, CTL_EOL);
2237 	sysctl_createv(clog, 0, NULL, NULL,
2238 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2239 		       CTLTYPE_INT, "do_loopback_cksum",
2240 		       SYSCTL_DESCR("Perform TCP checksum on loopback"),
2241 		       NULL, 0, &tcp_do_loopback_cksum, 0,
2242 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOOPBACKCKSUM,
2243 		       CTL_EOL);
2244 	sysctl_createv(clog, 0, NULL, NULL,
2245 		       CTLFLAG_PERMANENT,
2246 		       CTLTYPE_STRUCT, "pcblist",
2247 		       SYSCTL_DESCR("TCP protocol control block list"),
2248 		       sysctl_inpcblist, 0, &tcbtable, 0,
2249 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE,
2250 		       CTL_EOL);
2251 	sysctl_createv(clog, 0, NULL, NULL,
2252 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2253 		       CTLTYPE_INT, "keepinit",
2254 		       SYSCTL_DESCR("Ticks before initial tcp connection times out"),
2255 		       sysctl_tcp_keep, 0, &tcp_keepinit, 0,
2256 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2257 
2258 	/* TCP socket buffers auto-sizing nodes */
2259 	sysctl_createv(clog, 0, NULL, NULL,
2260 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2261 		       CTLTYPE_INT, "recvbuf_auto",
2262 		       SYSCTL_DESCR("Enable automatic receive "
2263 		           "buffer sizing (experimental)"),
2264 		       NULL, 0, &tcp_do_autorcvbuf, 0,
2265 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2266 	sysctl_createv(clog, 0, NULL, NULL,
2267 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2268 		       CTLTYPE_INT, "recvbuf_inc",
2269 		       SYSCTL_DESCR("Incrementor step size of "
2270 		           "automatic receive buffer"),
2271 		       NULL, 0, &tcp_autorcvbuf_inc, 0,
2272 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2273 	sysctl_createv(clog, 0, NULL, NULL,
2274 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2275 		       CTLTYPE_INT, "recvbuf_max",
2276 		       SYSCTL_DESCR("Max size of automatic receive buffer"),
2277 		       NULL, 0, &tcp_autorcvbuf_max, 0,
2278 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2279 
2280 	sysctl_createv(clog, 0, NULL, NULL,
2281 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2282 		       CTLTYPE_INT, "sendbuf_auto",
2283 		       SYSCTL_DESCR("Enable automatic send "
2284 		           "buffer sizing (experimental)"),
2285 		       NULL, 0, &tcp_do_autosndbuf, 0,
2286 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2287 	sysctl_createv(clog, 0, NULL, NULL,
2288 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2289 		       CTLTYPE_INT, "sendbuf_inc",
2290 		       SYSCTL_DESCR("Incrementor step size of "
2291 		           "automatic send buffer"),
2292 		       NULL, 0, &tcp_autosndbuf_inc, 0,
2293 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2294 	sysctl_createv(clog, 0, NULL, NULL,
2295 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2296 		       CTLTYPE_INT, "sendbuf_max",
2297 		       SYSCTL_DESCR("Max size of automatic send buffer"),
2298 		       NULL, 0, &tcp_autosndbuf_max, 0,
2299 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2300 
2301 	/* ECN subtree */
2302 	sysctl_createv(clog, 0, NULL, &ecn_node,
2303 	    	       CTLFLAG_PERMANENT,
2304 		       CTLTYPE_NODE, "ecn",
2305 	    	       SYSCTL_DESCR("RFC3168 Explicit Congestion Notification"),
2306 	    	       NULL, 0, NULL, 0,
2307 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2308 	sysctl_createv(clog, 0, &ecn_node, NULL,
2309 	    	       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2310 		       CTLTYPE_INT, "enable",
2311 		       SYSCTL_DESCR("Enable TCP Explicit Congestion "
2312 			   "Notification"),
2313 	    	       NULL, 0, &tcp_do_ecn, 0, CTL_CREATE, CTL_EOL);
2314 	sysctl_createv(clog, 0, &ecn_node, NULL,
2315 	    	       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2316 		       CTLTYPE_INT, "maxretries",
2317 		       SYSCTL_DESCR("Number of times to retry ECN setup "
2318 			       "before disabling ECN on the connection"),
2319 	    	       NULL, 0, &tcp_ecn_maxretries, 0, CTL_CREATE, CTL_EOL);
2320 
2321 	/* SACK gets its own little subtree. */
2322 	sysctl_createv(clog, 0, NULL, &sack_node,
2323 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2324 		       CTLTYPE_INT, "enable",
2325 		       SYSCTL_DESCR("Enable RFC2018 Selective ACKnowledgement"),
2326 		       NULL, 0, &tcp_do_sack, 0,
2327 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
2328 	sysctl_createv(clog, 0, NULL, &sack_node,
2329 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2330 		       CTLTYPE_INT, "maxholes",
2331 		       SYSCTL_DESCR("Maximum number of TCP SACK holes allowed per connection"),
2332 		       NULL, 0, &tcp_sack_tp_maxholes, 0,
2333 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
2334 	sysctl_createv(clog, 0, NULL, &sack_node,
2335 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2336 		       CTLTYPE_INT, "globalmaxholes",
2337 		       SYSCTL_DESCR("Global maximum number of TCP SACK holes"),
2338 		       NULL, 0, &tcp_sack_globalmaxholes, 0,
2339 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
2340 	sysctl_createv(clog, 0, NULL, &sack_node,
2341 		       CTLFLAG_PERMANENT,
2342 		       CTLTYPE_INT, "globalholes",
2343 		       SYSCTL_DESCR("Global number of TCP SACK holes"),
2344 		       NULL, 0, &tcp_sack_globalholes, 0,
2345 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
2346 
2347 	sysctl_createv(clog, 0, NULL, NULL,
2348 		       CTLFLAG_PERMANENT,
2349 		       CTLTYPE_STRUCT, "stats",
2350 		       SYSCTL_DESCR("TCP statistics"),
2351 		       sysctl_net_inet_tcp_stats, 0, NULL, 0,
2352 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_STATS,
2353 		       CTL_EOL);
2354         sysctl_createv(clog, 0, NULL, NULL,
2355                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2356                        CTLTYPE_INT, "local_by_rtt",
2357                        SYSCTL_DESCR("Use RTT estimator to decide which hosts "
2358 				    "are local"),
2359 		       NULL, 0, &tcp_rttlocal, 0,
2360 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2361 #ifdef TCP_DEBUG
2362 	sysctl_createv(clog, 0, NULL, NULL,
2363 		       CTLFLAG_PERMANENT,
2364 		       CTLTYPE_STRUCT, "debug",
2365 		       SYSCTL_DESCR("TCP sockets debug information"),
2366 		       NULL, 0, &tcp_debug, sizeof(tcp_debug),
2367 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBUG,
2368 		       CTL_EOL);
2369 	sysctl_createv(clog, 0, NULL, NULL,
2370 		       CTLFLAG_PERMANENT,
2371 		       CTLTYPE_INT, "debx",
2372 		       SYSCTL_DESCR("Number of TCP debug sockets messages"),
2373 		       NULL, 0, &tcp_debx, sizeof(tcp_debx),
2374 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBX,
2375 		       CTL_EOL);
2376 #endif
2377 	sysctl_createv(clog, 0, NULL, NULL,
2378 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2379 		       CTLTYPE_STRUCT, "drop",
2380 		       SYSCTL_DESCR("TCP drop connection"),
2381 		       sysctl_net_inet_tcp_drop, 0, NULL, 0,
2382 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DROP, CTL_EOL);
2383 	sysctl_createv(clog, 0, NULL, NULL,
2384 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2385 		       CTLTYPE_INT, "iss_hash",
2386 		       SYSCTL_DESCR("Enable RFC 1948 ISS by cryptographic "
2387 				    "hash computation"),
2388 		       NULL, 0, &tcp_do_rfc1948, sizeof(tcp_do_rfc1948),
2389 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE,
2390 		       CTL_EOL);
2391 
2392 	/* ABC subtree */
2393 
2394 	sysctl_createv(clog, 0, NULL, &abc_node,
2395 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "abc",
2396 		       SYSCTL_DESCR("RFC3465 Appropriate Byte Counting (ABC)"),
2397 		       NULL, 0, NULL, 0,
2398 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2399 	sysctl_createv(clog, 0, &abc_node, NULL,
2400 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2401 		       CTLTYPE_INT, "enable",
2402 		       SYSCTL_DESCR("Enable RFC3465 Appropriate Byte Counting"),
2403 		       NULL, 0, &tcp_do_abc, 0, CTL_CREATE, CTL_EOL);
2404 	sysctl_createv(clog, 0, &abc_node, NULL,
2405 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2406 		       CTLTYPE_INT, "aggressive",
2407 		       SYSCTL_DESCR("1: L=2*SMSS 0: L=1*SMSS"),
2408 		       NULL, 0, &tcp_abc_aggressive, 0, CTL_CREATE, CTL_EOL);
2409 
2410 	/* MSL tuning subtree */
2411 
2412 	sysctl_createv(clog, 0, NULL, &mslt_node,
2413 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "mslt",
2414 		       SYSCTL_DESCR("MSL Tuning for TIME_WAIT truncation"),
2415 		       NULL, 0, NULL, 0,
2416 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2417 	sysctl_createv(clog, 0, &mslt_node, NULL,
2418 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2419 		       CTLTYPE_INT, "enable",
2420 		       SYSCTL_DESCR("Enable TIME_WAIT truncation"),
2421 		       NULL, 0, &tcp_msl_enable, 0, CTL_CREATE, CTL_EOL);
2422 	sysctl_createv(clog, 0, &mslt_node, NULL,
2423 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2424 		       CTLTYPE_INT, "loopback",
2425 		       SYSCTL_DESCR("MSL value to use for loopback connections"),
2426 		       NULL, 0, &tcp_msl_loop, 0, CTL_CREATE, CTL_EOL);
2427 	sysctl_createv(clog, 0, &mslt_node, NULL,
2428 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2429 		       CTLTYPE_INT, "local",
2430 		       SYSCTL_DESCR("MSL value to use for local connections"),
2431 		       NULL, 0, &tcp_msl_local, 0, CTL_CREATE, CTL_EOL);
2432 	sysctl_createv(clog, 0, &mslt_node, NULL,
2433 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2434 		       CTLTYPE_INT, "remote",
2435 		       SYSCTL_DESCR("MSL value to use for remote connections"),
2436 		       NULL, 0, &tcp_msl_remote, 0, CTL_CREATE, CTL_EOL);
2437 	sysctl_createv(clog, 0, &mslt_node, NULL,
2438 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2439 		       CTLTYPE_INT, "remote_threshold",
2440 		       SYSCTL_DESCR("RTT estimate value to promote local to remote"),
2441 		       NULL, 0, &tcp_msl_remote_threshold, 0, CTL_CREATE, CTL_EOL);
2442 
2443 	/* vestigial TIME_WAIT tuning subtree */
2444 
2445 	sysctl_createv(clog, 0, NULL, &vtw_node,
2446 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "vtw",
2447 		       SYSCTL_DESCR("Tuning for Vestigial TIME_WAIT"),
2448 		       NULL, 0, NULL, 0,
2449 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2450 	sysctl_createv(clog, 0, &vtw_node, NULL,
2451 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2452 		       CTLTYPE_INT, "enable",
2453 		       SYSCTL_DESCR("Enable Vestigial TIME_WAIT"),
2454 		       sysctl_tcp_vtw_enable, 0,
2455 	               (pf == AF_INET) ? &tcp4_vtw_enable : &tcp6_vtw_enable,
2456 		       0, CTL_CREATE, CTL_EOL);
2457 	sysctl_createv(clog, 0, &vtw_node, NULL,
2458 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
2459 		       CTLTYPE_INT, "entries",
2460 		       SYSCTL_DESCR("Maximum number of vestigial TIME_WAIT entries"),
2461 		       NULL, 0, &tcp_vtw_entries, 0, CTL_CREATE, CTL_EOL);
2462 }
2463 
2464 void
2465 tcp_usrreq_init(void)
2466 {
2467 
2468 #ifdef INET
2469 	sysctl_net_inet_tcp_setup2(NULL, PF_INET, "inet", "tcp");
2470 #endif
2471 #ifdef INET6
2472 	sysctl_net_inet_tcp_setup2(NULL, PF_INET6, "inet6", "tcp6");
2473 #endif
2474 }
2475 
2476 PR_WRAP_USRREQS(tcp)
2477 #define	tcp_attach	tcp_attach_wrapper
2478 #define	tcp_detach	tcp_detach_wrapper
2479 #define	tcp_accept	tcp_accept_wrapper
2480 #define	tcp_bind	tcp_bind_wrapper
2481 #define	tcp_listen	tcp_listen_wrapper
2482 #define	tcp_connect	tcp_connect_wrapper
2483 #define	tcp_connect2	tcp_connect2_wrapper
2484 #define	tcp_disconnect	tcp_disconnect_wrapper
2485 #define	tcp_shutdown	tcp_shutdown_wrapper
2486 #define	tcp_abort	tcp_abort_wrapper
2487 #define	tcp_ioctl	tcp_ioctl_wrapper
2488 #define	tcp_stat	tcp_stat_wrapper
2489 #define	tcp_peeraddr	tcp_peeraddr_wrapper
2490 #define	tcp_sockaddr	tcp_sockaddr_wrapper
2491 #define	tcp_rcvd	tcp_rcvd_wrapper
2492 #define	tcp_recvoob	tcp_recvoob_wrapper
2493 #define	tcp_send	tcp_send_wrapper
2494 #define	tcp_sendoob	tcp_sendoob_wrapper
2495 #define	tcp_purgeif	tcp_purgeif_wrapper
2496 
2497 const struct pr_usrreqs tcp_usrreqs = {
2498 	.pr_attach	= tcp_attach,
2499 	.pr_detach	= tcp_detach,
2500 	.pr_accept	= tcp_accept,
2501 	.pr_bind	= tcp_bind,
2502 	.pr_listen	= tcp_listen,
2503 	.pr_connect	= tcp_connect,
2504 	.pr_connect2	= tcp_connect2,
2505 	.pr_disconnect	= tcp_disconnect,
2506 	.pr_shutdown	= tcp_shutdown,
2507 	.pr_abort	= tcp_abort,
2508 	.pr_ioctl	= tcp_ioctl,
2509 	.pr_stat	= tcp_stat,
2510 	.pr_peeraddr	= tcp_peeraddr,
2511 	.pr_sockaddr	= tcp_sockaddr,
2512 	.pr_rcvd	= tcp_rcvd,
2513 	.pr_recvoob	= tcp_recvoob,
2514 	.pr_send	= tcp_send,
2515 	.pr_sendoob	= tcp_sendoob,
2516 	.pr_purgeif	= tcp_purgeif,
2517 };
2518