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