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