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