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