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