xref: /netbsd/sys/netinet/tcp_usrreq.c (revision 6550d01e)
1 /*	$NetBSD: tcp_usrreq.c,v 1.158 2009/12/30 06:59:32 elad 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.158 2009/12/30 06:59:32 elad 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, l);
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, l);
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 	case IPV6CTL_ANONPORTMIN:
1112 		if (tmp >= apmax)
1113 			return (EINVAL);
1114 #ifndef IPNOPRIVPORTS
1115 		if (tmp < IPPORT_RESERVED)
1116                         return (EINVAL);
1117 #endif /* IPNOPRIVPORTS */
1118 		break;
1119 
1120 	case IPCTL_ANONPORTMAX:
1121 	case IPV6CTL_ANONPORTMAX:
1122                 if (apmin >= tmp)
1123 			return (EINVAL);
1124 #ifndef IPNOPRIVPORTS
1125 		if (tmp < IPPORT_RESERVED)
1126                         return (EINVAL);
1127 #endif /* IPNOPRIVPORTS */
1128 		break;
1129 
1130 #ifndef IPNOPRIVPORTS
1131 	case IPCTL_LOWPORTMIN:
1132 	case IPV6CTL_LOWPORTMIN:
1133 		if (tmp >= lpmax ||
1134 		    tmp > IPPORT_RESERVEDMAX ||
1135 		    tmp < IPPORT_RESERVEDMIN)
1136 			return (EINVAL);
1137 		break;
1138 
1139 	case IPCTL_LOWPORTMAX:
1140 	case IPV6CTL_LOWPORTMAX:
1141 		if (lpmin >= tmp ||
1142 		    tmp > IPPORT_RESERVEDMAX ||
1143 		    tmp < IPPORT_RESERVEDMIN)
1144 			return (EINVAL);
1145 		break;
1146 #endif /* IPNOPRIVPORTS */
1147 
1148 	default:
1149 		return (EINVAL);
1150 	}
1151 
1152 	*(int*)rnode->sysctl_data = tmp;
1153 
1154 	return (0);
1155 }
1156 
1157 static inline int
1158 copyout_uid(struct socket *sockp, void *oldp, size_t *oldlenp)
1159 {
1160 	size_t sz;
1161 	int error;
1162 	uid_t uid;
1163 
1164 	uid = kauth_cred_geteuid(sockp->so_cred);
1165 	if (oldp) {
1166 		sz = MIN(sizeof(uid), *oldlenp);
1167 		error = copyout(&uid, oldp, sz);
1168 		if (error)
1169 			return error;
1170 	}
1171 	*oldlenp = sizeof(uid);
1172 	return 0;
1173 }
1174 
1175 static inline int
1176 inet4_ident_core(struct in_addr raddr, u_int rport,
1177     struct in_addr laddr, u_int lport,
1178     void *oldp, size_t *oldlenp,
1179     struct lwp *l, int dodrop)
1180 {
1181 	struct inpcb *inp;
1182 	struct socket *sockp;
1183 
1184 	inp = in_pcblookup_connect(&tcbtable, raddr, rport, laddr, lport);
1185 
1186 	if (inp == NULL || (sockp = inp->inp_socket) == NULL)
1187 		return ESRCH;
1188 
1189 	if (dodrop) {
1190 		struct tcpcb *tp;
1191 		int error;
1192 
1193 		if (inp == NULL || (tp = intotcpcb(inp)) == NULL ||
1194 		    (inp->inp_socket->so_options & SO_ACCEPTCONN) != 0)
1195 			return ESRCH;
1196 
1197 		error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
1198 		    KAUTH_REQ_NETWORK_SOCKET_DROP, inp->inp_socket, tp, NULL);
1199 		if (error)
1200 			return (error);
1201 
1202 		(void)tcp_drop(tp, ECONNABORTED);
1203 		return 0;
1204 	}
1205 	else
1206 		return copyout_uid(sockp, oldp, oldlenp);
1207 }
1208 
1209 #ifdef INET6
1210 static inline int
1211 inet6_ident_core(struct in6_addr *raddr, u_int rport,
1212     struct in6_addr *laddr, u_int lport,
1213     void *oldp, size_t *oldlenp,
1214     struct lwp *l, int dodrop)
1215 {
1216 	struct in6pcb *in6p;
1217 	struct socket *sockp;
1218 
1219 	in6p = in6_pcblookup_connect(&tcbtable, raddr, rport, laddr, lport, 0);
1220 
1221 	if (in6p == NULL || (sockp = in6p->in6p_socket) == NULL)
1222 		return ESRCH;
1223 
1224 	if (dodrop) {
1225 		struct tcpcb *tp;
1226 		int error;
1227 
1228 		if (in6p == NULL || (tp = in6totcpcb(in6p)) == NULL ||
1229 		    (in6p->in6p_socket->so_options & SO_ACCEPTCONN) != 0)
1230 			return ESRCH;
1231 
1232 		error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
1233 		    KAUTH_REQ_NETWORK_SOCKET_DROP, in6p->in6p_socket, tp, NULL);
1234 		if (error)
1235 			return (error);
1236 
1237 		(void)tcp_drop(tp, ECONNABORTED);
1238 		return 0;
1239 	}
1240 	else
1241 		return copyout_uid(sockp, oldp, oldlenp);
1242 }
1243 #endif
1244 
1245 /*
1246  * sysctl helper routine for the net.inet.tcp.drop and
1247  * net.inet6.tcp6.drop nodes.
1248  */
1249 #define sysctl_net_inet_tcp_drop sysctl_net_inet_tcp_ident
1250 
1251 /*
1252  * sysctl helper routine for the net.inet.tcp.ident and
1253  * net.inet6.tcp6.ident nodes.  contains backwards compat code for the
1254  * old way of looking up the ident information for ipv4 which involves
1255  * stuffing the port/addr pairs into the mib lookup.
1256  */
1257 static int
1258 sysctl_net_inet_tcp_ident(SYSCTLFN_ARGS)
1259 {
1260 #ifdef INET
1261 	struct sockaddr_in *si4[2];
1262 #endif /* INET */
1263 #ifdef INET6
1264 	struct sockaddr_in6 *si6[2];
1265 #endif /* INET6 */
1266 	struct sockaddr_storage sa[2];
1267 	int error, pf, dodrop;
1268 
1269 	dodrop = name[-1] == TCPCTL_DROP;
1270 	if (dodrop) {
1271 		if (oldp != NULL || *oldlenp != 0)
1272 			return EINVAL;
1273 		if (newp == NULL)
1274 			return EPERM;
1275 		if (newlen < sizeof(sa))
1276 			return ENOMEM;
1277 	}
1278 	if (namelen != 4 && namelen != 0)
1279 		return EINVAL;
1280 	if (name[-2] != IPPROTO_TCP)
1281 		return EINVAL;
1282 	pf = name[-3];
1283 
1284 	/* old style lookup, ipv4 only */
1285 	if (namelen == 4) {
1286 #ifdef INET
1287 		struct in_addr laddr, raddr;
1288 		u_int lport, rport;
1289 
1290 		if (pf != PF_INET)
1291 			return EPROTONOSUPPORT;
1292 		raddr.s_addr = (uint32_t)name[0];
1293 		rport = (u_int)name[1];
1294 		laddr.s_addr = (uint32_t)name[2];
1295 		lport = (u_int)name[3];
1296 
1297 		mutex_enter(softnet_lock);
1298 		error = inet4_ident_core(raddr, rport, laddr, lport,
1299 		    oldp, oldlenp, l, dodrop);
1300 		mutex_exit(softnet_lock);
1301 		return error;
1302 #else /* INET */
1303 		return EINVAL;
1304 #endif /* INET */
1305 	}
1306 
1307 	if (newp == NULL || newlen != sizeof(sa))
1308 		return EINVAL;
1309 	error = copyin(newp, &sa, newlen);
1310 	if (error)
1311 		return error;
1312 
1313 	/*
1314 	 * requested families must match
1315 	 */
1316 	if (pf != sa[0].ss_family || sa[0].ss_family != sa[1].ss_family)
1317 		return EINVAL;
1318 
1319 	switch (pf) {
1320 #ifdef INET6
1321 	case PF_INET6:
1322 		si6[0] = (struct sockaddr_in6*)&sa[0];
1323 		si6[1] = (struct sockaddr_in6*)&sa[1];
1324 		if (si6[0]->sin6_len != sizeof(*si6[0]) ||
1325 		    si6[1]->sin6_len != sizeof(*si6[1]))
1326 			return EINVAL;
1327 
1328 		if (!IN6_IS_ADDR_V4MAPPED(&si6[0]->sin6_addr) &&
1329 		    !IN6_IS_ADDR_V4MAPPED(&si6[1]->sin6_addr)) {
1330 			error = sa6_embedscope(si6[0], ip6_use_defzone);
1331 			if (error)
1332 				return error;
1333 			error = sa6_embedscope(si6[1], ip6_use_defzone);
1334 			if (error)
1335 				return error;
1336 
1337 			mutex_enter(softnet_lock);
1338 			error = inet6_ident_core(&si6[0]->sin6_addr,
1339 			    si6[0]->sin6_port, &si6[1]->sin6_addr,
1340 			    si6[1]->sin6_port, oldp, oldlenp, l, dodrop);
1341 			mutex_exit(softnet_lock);
1342 			return error;
1343 		}
1344 
1345 		if (IN6_IS_ADDR_V4MAPPED(&si6[0]->sin6_addr) !=
1346 		    IN6_IS_ADDR_V4MAPPED(&si6[1]->sin6_addr))
1347 			return EINVAL;
1348 
1349 		in6_sin6_2_sin_in_sock((struct sockaddr *)&sa[0]);
1350 		in6_sin6_2_sin_in_sock((struct sockaddr *)&sa[1]);
1351 		/*FALLTHROUGH*/
1352 #endif /* INET6 */
1353 #ifdef INET
1354 	case PF_INET:
1355 		si4[0] = (struct sockaddr_in*)&sa[0];
1356 		si4[1] = (struct sockaddr_in*)&sa[1];
1357 		if (si4[0]->sin_len != sizeof(*si4[0]) ||
1358 		    si4[0]->sin_len != sizeof(*si4[1]))
1359 			return EINVAL;
1360 
1361 		mutex_enter(softnet_lock);
1362 		error = inet4_ident_core(si4[0]->sin_addr, si4[0]->sin_port,
1363 		    si4[1]->sin_addr, si4[1]->sin_port,
1364 		    oldp, oldlenp, l, dodrop);
1365 		mutex_exit(softnet_lock);
1366 		return error;
1367 #endif /* INET */
1368 	default:
1369 		return EPROTONOSUPPORT;
1370 	}
1371 }
1372 
1373 /*
1374  * sysctl helper for the inet and inet6 pcblists.  handles tcp/udp and
1375  * inet/inet6, as well as raw pcbs for each.  specifically not
1376  * declared static so that raw sockets and udp/udp6 can use it as
1377  * well.
1378  */
1379 int
1380 sysctl_inpcblist(SYSCTLFN_ARGS)
1381 {
1382 #ifdef INET
1383 	struct sockaddr_in *in;
1384 	const struct inpcb *inp;
1385 #endif
1386 #ifdef INET6
1387 	struct sockaddr_in6 *in6;
1388 	const struct in6pcb *in6p;
1389 #endif
1390 	/*
1391 	 * sysctl_data is const, but CIRCLEQ_FOREACH can't use a const
1392 	 * struct inpcbtable pointer, so we have to discard const.  :-/
1393 	 */
1394 	struct inpcbtable *pcbtbl = __UNCONST(rnode->sysctl_data);
1395 	const struct inpcb_hdr *inph;
1396 	struct tcpcb *tp;
1397 	struct kinfo_pcb pcb;
1398 	char *dp;
1399 	u_int op, arg;
1400 	size_t len, needed, elem_size, out_size;
1401 	int error, elem_count, pf, proto, pf2;
1402 
1403 	if (namelen != 4)
1404 		return (EINVAL);
1405 
1406 	if (oldp != NULL) {
1407 		    len = *oldlenp;
1408 		    elem_size = name[2];
1409 		    elem_count = name[3];
1410 		    if (elem_size != sizeof(pcb))
1411 			    return EINVAL;
1412 	} else {
1413 		    len = 0;
1414 		    elem_count = INT_MAX;
1415 		    elem_size = sizeof(pcb);
1416 	}
1417 	error = 0;
1418 	dp = oldp;
1419 	op = name[0];
1420 	arg = name[1];
1421 	out_size = elem_size;
1422 	needed = 0;
1423 
1424 	if (namelen == 1 && name[0] == CTL_QUERY)
1425 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
1426 
1427 	if (name - oname != 4)
1428 		return (EINVAL);
1429 
1430 	pf = oname[1];
1431 	proto = oname[2];
1432 	pf2 = (oldp != NULL) ? pf : 0;
1433 
1434 	mutex_enter(softnet_lock);
1435 
1436 	CIRCLEQ_FOREACH(inph, &pcbtbl->inpt_queue, inph_queue) {
1437 #ifdef INET
1438 		inp = (const struct inpcb *)inph;
1439 #endif
1440 #ifdef INET6
1441 		in6p = (const struct in6pcb *)inph;
1442 #endif
1443 
1444 		if (inph->inph_af != pf)
1445 			continue;
1446 
1447 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
1448 		    KAUTH_REQ_NETWORK_SOCKET_CANSEE, inph->inph_socket, NULL,
1449 		    NULL) != 0)
1450 			continue;
1451 
1452 		memset(&pcb, 0, sizeof(pcb));
1453 
1454 		pcb.ki_family = pf;
1455 		pcb.ki_type = proto;
1456 
1457 		switch (pf2) {
1458 		case 0:
1459 			/* just probing for size */
1460 			break;
1461 #ifdef INET
1462 		case PF_INET:
1463 			pcb.ki_family = inp->inp_socket->so_proto->
1464 			    pr_domain->dom_family;
1465 			pcb.ki_type = inp->inp_socket->so_proto->
1466 			    pr_type;
1467 			pcb.ki_protocol = inp->inp_socket->so_proto->
1468 			    pr_protocol;
1469 			pcb.ki_pflags = inp->inp_flags;
1470 
1471 			pcb.ki_sostate = inp->inp_socket->so_state;
1472 			pcb.ki_prstate = inp->inp_state;
1473 			if (proto == IPPROTO_TCP) {
1474 				tp = intotcpcb(inp);
1475 				pcb.ki_tstate = tp->t_state;
1476 				pcb.ki_tflags = tp->t_flags;
1477 			}
1478 
1479 			pcb.ki_pcbaddr = PTRTOUINT64(inp);
1480 			pcb.ki_ppcbaddr = PTRTOUINT64(inp->inp_ppcb);
1481 			pcb.ki_sockaddr = PTRTOUINT64(inp->inp_socket);
1482 
1483 			pcb.ki_rcvq = inp->inp_socket->so_rcv.sb_cc;
1484 			pcb.ki_sndq = inp->inp_socket->so_snd.sb_cc;
1485 
1486 			in = satosin(&pcb.ki_src);
1487 			in->sin_len = sizeof(*in);
1488 			in->sin_family = pf;
1489 			in->sin_port = inp->inp_lport;
1490 			in->sin_addr = inp->inp_laddr;
1491 			if (pcb.ki_prstate >= INP_CONNECTED) {
1492 				in = satosin(&pcb.ki_dst);
1493 				in->sin_len = sizeof(*in);
1494 				in->sin_family = pf;
1495 				in->sin_port = inp->inp_fport;
1496 				in->sin_addr = inp->inp_faddr;
1497 			}
1498 			break;
1499 #endif
1500 #ifdef INET6
1501 		case PF_INET6:
1502 			pcb.ki_family = in6p->in6p_socket->so_proto->
1503 			    pr_domain->dom_family;
1504 			pcb.ki_type = in6p->in6p_socket->so_proto->pr_type;
1505 			pcb.ki_protocol = in6p->in6p_socket->so_proto->
1506 			    pr_protocol;
1507 			pcb.ki_pflags = in6p->in6p_flags;
1508 
1509 			pcb.ki_sostate = in6p->in6p_socket->so_state;
1510 			pcb.ki_prstate = in6p->in6p_state;
1511 			if (proto == IPPROTO_TCP) {
1512 				tp = in6totcpcb(in6p);
1513 				pcb.ki_tstate = tp->t_state;
1514 				pcb.ki_tflags = tp->t_flags;
1515 			}
1516 
1517 			pcb.ki_pcbaddr = PTRTOUINT64(in6p);
1518 			pcb.ki_ppcbaddr = PTRTOUINT64(in6p->in6p_ppcb);
1519 			pcb.ki_sockaddr = PTRTOUINT64(in6p->in6p_socket);
1520 
1521 			pcb.ki_rcvq = in6p->in6p_socket->so_rcv.sb_cc;
1522 			pcb.ki_sndq = in6p->in6p_socket->so_snd.sb_cc;
1523 
1524 			in6 = satosin6(&pcb.ki_src);
1525 			in6->sin6_len = sizeof(*in6);
1526 			in6->sin6_family = pf;
1527 			in6->sin6_port = in6p->in6p_lport;
1528 			in6->sin6_flowinfo = in6p->in6p_flowinfo;
1529 			in6->sin6_addr = in6p->in6p_laddr;
1530 			in6->sin6_scope_id = 0; /* XXX? */
1531 
1532 			if (pcb.ki_prstate >= IN6P_CONNECTED) {
1533 				in6 = satosin6(&pcb.ki_dst);
1534 				in6->sin6_len = sizeof(*in6);
1535 				in6->sin6_family = pf;
1536 				in6->sin6_port = in6p->in6p_fport;
1537 				in6->sin6_flowinfo = in6p->in6p_flowinfo;
1538 				in6->sin6_addr = in6p->in6p_faddr;
1539 				in6->sin6_scope_id = 0; /* XXX? */
1540 			}
1541 			break;
1542 #endif
1543 		}
1544 
1545 		if (len >= elem_size && elem_count > 0) {
1546 			error = copyout(&pcb, dp, out_size);
1547 			if (error) {
1548 				mutex_exit(softnet_lock);
1549 				return (error);
1550 			}
1551 			dp += elem_size;
1552 			len -= elem_size;
1553 		}
1554 		needed += elem_size;
1555 		if (elem_count > 0 && elem_count != INT_MAX)
1556 			elem_count--;
1557 	}
1558 
1559 	*oldlenp = needed;
1560 	if (oldp == NULL)
1561 		*oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
1562 
1563 	mutex_exit(softnet_lock);
1564 
1565 	return (error);
1566 }
1567 
1568 static int
1569 sysctl_tcp_congctl(SYSCTLFN_ARGS)
1570 {
1571 	struct sysctlnode node;
1572 	int error;
1573 	char newname[TCPCC_MAXLEN];
1574 
1575 	strlcpy(newname, tcp_congctl_global_name, sizeof(newname) - 1);
1576 
1577 	node = *rnode;
1578 	node.sysctl_data = newname;
1579 	node.sysctl_size = sizeof(newname);
1580 
1581 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1582 
1583 	if (error ||
1584 	    newp == NULL ||
1585 	    strncmp(newname, tcp_congctl_global_name, sizeof(newname)) == 0)
1586 		return error;
1587 
1588 	mutex_enter(softnet_lock);
1589 	error = tcp_congctl_select(NULL, newname);
1590 	mutex_exit(softnet_lock);
1591 
1592 	return error;
1593 }
1594 
1595 static int
1596 sysctl_tcp_keep(SYSCTLFN_ARGS)
1597 {
1598 	int error;
1599 	u_int tmp;
1600 	struct sysctlnode node;
1601 
1602 	node = *rnode;
1603 	tmp = *(u_int *)rnode->sysctl_data;
1604 	node.sysctl_data = &tmp;
1605 
1606 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1607 	if (error || newp == NULL)
1608 		return error;
1609 
1610 	mutex_enter(softnet_lock);
1611 
1612 	*(u_int *)rnode->sysctl_data = tmp;
1613 	tcp_tcpcb_template();	/* update the template */
1614 
1615 	mutex_exit(softnet_lock);
1616 	return 0;
1617 }
1618 
1619 static int
1620 sysctl_net_inet_tcp_stats(SYSCTLFN_ARGS)
1621 {
1622 
1623 	return (NETSTAT_SYSCTL(tcpstat_percpu, TCP_NSTATS));
1624 }
1625 
1626 /*
1627  * this (second stage) setup routine is a replacement for tcp_sysctl()
1628  * (which is currently used for ipv4 and ipv6)
1629  */
1630 static void
1631 sysctl_net_inet_tcp_setup2(struct sysctllog **clog, int pf, const char *pfname,
1632 			   const char *tcpname)
1633 {
1634 	const struct sysctlnode *sack_node;
1635 	const struct sysctlnode *abc_node;
1636 	const struct sysctlnode *ecn_node;
1637 	const struct sysctlnode *congctl_node;
1638 #ifdef TCP_DEBUG
1639 	extern struct tcp_debug tcp_debug[TCP_NDEBUG];
1640 	extern int tcp_debx;
1641 #endif
1642 
1643 	sysctl_createv(clog, 0, NULL, NULL,
1644 		       CTLFLAG_PERMANENT,
1645 		       CTLTYPE_NODE, "net", NULL,
1646 		       NULL, 0, NULL, 0,
1647 		       CTL_NET, CTL_EOL);
1648 	sysctl_createv(clog, 0, NULL, NULL,
1649 		       CTLFLAG_PERMANENT,
1650 		       CTLTYPE_NODE, pfname, NULL,
1651 		       NULL, 0, NULL, 0,
1652 		       CTL_NET, pf, CTL_EOL);
1653 	sysctl_createv(clog, 0, NULL, NULL,
1654 		       CTLFLAG_PERMANENT,
1655 		       CTLTYPE_NODE, tcpname,
1656 		       SYSCTL_DESCR("TCP related settings"),
1657 		       NULL, 0, NULL, 0,
1658 		       CTL_NET, pf, IPPROTO_TCP, CTL_EOL);
1659 
1660 	sysctl_createv(clog, 0, NULL, NULL,
1661 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1662 		       CTLTYPE_INT, "rfc1323",
1663 		       SYSCTL_DESCR("Enable RFC1323 TCP extensions"),
1664 		       NULL, 0, &tcp_do_rfc1323, 0,
1665 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RFC1323, CTL_EOL);
1666 	sysctl_createv(clog, 0, NULL, NULL,
1667 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1668 		       CTLTYPE_INT, "sendspace",
1669 		       SYSCTL_DESCR("Default TCP send buffer size"),
1670 		       NULL, 0, &tcp_sendspace, 0,
1671 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SENDSPACE, CTL_EOL);
1672 	sysctl_createv(clog, 0, NULL, NULL,
1673 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1674 		       CTLTYPE_INT, "recvspace",
1675 		       SYSCTL_DESCR("Default TCP receive buffer size"),
1676 		       NULL, 0, &tcp_recvspace, 0,
1677 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RECVSPACE, CTL_EOL);
1678 	sysctl_createv(clog, 0, NULL, NULL,
1679 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1680 		       CTLTYPE_INT, "mssdflt",
1681 		       SYSCTL_DESCR("Default maximum segment size"),
1682 		       sysctl_net_inet_tcp_mssdflt, 0, &tcp_mssdflt, 0,
1683 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSSDFLT, CTL_EOL);
1684 	sysctl_createv(clog, 0, NULL, NULL,
1685 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1686 		       CTLTYPE_INT, "minmss",
1687 		       SYSCTL_DESCR("Lower limit for TCP maximum segment size"),
1688 		       NULL, 0, &tcp_minmss, 0,
1689 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1690 	sysctl_createv(clog, 0, NULL, NULL,
1691 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1692 		       CTLTYPE_INT, "msl",
1693 		       SYSCTL_DESCR("Maximum Segment Life"),
1694 		       NULL, 0, &tcp_msl, 0,
1695 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSL, CTL_EOL);
1696 	sysctl_createv(clog, 0, NULL, NULL,
1697 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1698 		       CTLTYPE_INT, "syn_cache_limit",
1699 		       SYSCTL_DESCR("Maximum number of entries in the TCP "
1700 				    "compressed state engine"),
1701 		       NULL, 0, &tcp_syn_cache_limit, 0,
1702 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_LIMIT,
1703 		       CTL_EOL);
1704 	sysctl_createv(clog, 0, NULL, NULL,
1705 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1706 		       CTLTYPE_INT, "syn_bucket_limit",
1707 		       SYSCTL_DESCR("Maximum number of entries per hash "
1708 				    "bucket in the TCP compressed state "
1709 				    "engine"),
1710 		       NULL, 0, &tcp_syn_bucket_limit, 0,
1711 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_BUCKET_LIMIT,
1712 		       CTL_EOL);
1713 #if 0 /* obsoleted */
1714 	sysctl_createv(clog, 0, NULL, NULL,
1715 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1716 		       CTLTYPE_INT, "syn_cache_interval",
1717 		       SYSCTL_DESCR("TCP compressed state engine's timer interval"),
1718 		       NULL, 0, &tcp_syn_cache_interval, 0,
1719 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_INTER,
1720 		       CTL_EOL);
1721 #endif
1722 	sysctl_createv(clog, 0, NULL, NULL,
1723 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1724 		       CTLTYPE_INT, "init_win",
1725 		       SYSCTL_DESCR("Initial TCP congestion window"),
1726 		       NULL, 0, &tcp_init_win, 0,
1727 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN, CTL_EOL);
1728 	sysctl_createv(clog, 0, NULL, NULL,
1729 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1730 		       CTLTYPE_INT, "mss_ifmtu",
1731 		       SYSCTL_DESCR("Use interface MTU for calculating MSS"),
1732 		       NULL, 0, &tcp_mss_ifmtu, 0,
1733 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSS_IFMTU, CTL_EOL);
1734 	sysctl_createv(clog, 0, NULL, &sack_node,
1735 		       CTLFLAG_PERMANENT,
1736 		       CTLTYPE_NODE, "sack",
1737 		       SYSCTL_DESCR("RFC2018 Selective ACKnowledgement tunables"),
1738 		       NULL, 0, NULL, 0,
1739 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_EOL);
1740 
1741 	/* Congctl subtree */
1742 	sysctl_createv(clog, 0, NULL, &congctl_node,
1743 		       CTLFLAG_PERMANENT,
1744 		       CTLTYPE_NODE, "congctl",
1745 		       SYSCTL_DESCR("TCP Congestion Control"),
1746 	    	       NULL, 0, NULL, 0,
1747 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1748 	sysctl_createv(clog, 0, &congctl_node, NULL,
1749 		       CTLFLAG_PERMANENT,
1750 		       CTLTYPE_STRING, "available",
1751 		       SYSCTL_DESCR("Available Congestion Control Mechanisms"),
1752 		       NULL, 0, &tcp_congctl_avail, 0, CTL_CREATE, CTL_EOL);
1753 	sysctl_createv(clog, 0, &congctl_node, NULL,
1754 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1755 		       CTLTYPE_STRING, "selected",
1756 		       SYSCTL_DESCR("Selected Congestion Control Mechanism"),
1757 		       sysctl_tcp_congctl, 0, NULL, TCPCC_MAXLEN,
1758 		       CTL_CREATE, CTL_EOL);
1759 
1760 	sysctl_createv(clog, 0, NULL, NULL,
1761 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1762 		       CTLTYPE_INT, "win_scale",
1763 		       SYSCTL_DESCR("Use RFC1323 window scale options"),
1764 		       NULL, 0, &tcp_do_win_scale, 0,
1765 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_WSCALE, CTL_EOL);
1766 	sysctl_createv(clog, 0, NULL, NULL,
1767 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1768 		       CTLTYPE_INT, "timestamps",
1769 		       SYSCTL_DESCR("Use RFC1323 time stamp options"),
1770 		       NULL, 0, &tcp_do_timestamps, 0,
1771 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_TSTAMP, CTL_EOL);
1772 	sysctl_createv(clog, 0, NULL, NULL,
1773 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1774 		       CTLTYPE_INT, "compat_42",
1775 		       SYSCTL_DESCR("Enable workarounds for 4.2BSD TCP bugs"),
1776 		       NULL, 0, &tcp_compat_42, 0,
1777 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_COMPAT_42, CTL_EOL);
1778 	sysctl_createv(clog, 0, NULL, NULL,
1779 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1780 		       CTLTYPE_INT, "cwm",
1781 		       SYSCTL_DESCR("Hughes/Touch/Heidemann Congestion Window "
1782 				    "Monitoring"),
1783 		       NULL, 0, &tcp_cwm, 0,
1784 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM, CTL_EOL);
1785 	sysctl_createv(clog, 0, NULL, NULL,
1786 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1787 		       CTLTYPE_INT, "cwm_burstsize",
1788 		       SYSCTL_DESCR("Congestion Window Monitoring allowed "
1789 				    "burst count in packets"),
1790 		       NULL, 0, &tcp_cwm_burstsize, 0,
1791 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM_BURSTSIZE,
1792 		       CTL_EOL);
1793 	sysctl_createv(clog, 0, NULL, NULL,
1794 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1795 		       CTLTYPE_INT, "ack_on_push",
1796 		       SYSCTL_DESCR("Immediately return ACK when PSH is "
1797 				    "received"),
1798 		       NULL, 0, &tcp_ack_on_push, 0,
1799 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_ACK_ON_PUSH, CTL_EOL);
1800 	sysctl_createv(clog, 0, NULL, NULL,
1801 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1802 		       CTLTYPE_INT, "keepidle",
1803 		       SYSCTL_DESCR("Allowed connection idle ticks before a "
1804 				    "keepalive probe is sent"),
1805 		       sysctl_tcp_keep, 0, &tcp_keepidle, 0,
1806 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPIDLE, CTL_EOL);
1807 	sysctl_createv(clog, 0, NULL, NULL,
1808 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1809 		       CTLTYPE_INT, "keepintvl",
1810 		       SYSCTL_DESCR("Ticks before next keepalive probe is sent"),
1811 		       sysctl_tcp_keep, 0, &tcp_keepintvl, 0,
1812 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPINTVL, CTL_EOL);
1813 	sysctl_createv(clog, 0, NULL, NULL,
1814 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1815 		       CTLTYPE_INT, "keepcnt",
1816 		       SYSCTL_DESCR("Number of keepalive probes to send"),
1817 		       sysctl_tcp_keep, 0, &tcp_keepcnt, 0,
1818 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPCNT, CTL_EOL);
1819 	sysctl_createv(clog, 0, NULL, NULL,
1820 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
1821 		       CTLTYPE_INT, "slowhz",
1822 		       SYSCTL_DESCR("Keepalive ticks per second"),
1823 		       NULL, PR_SLOWHZ, NULL, 0,
1824 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SLOWHZ, CTL_EOL);
1825 	sysctl_createv(clog, 0, NULL, NULL,
1826 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1827 		       CTLTYPE_INT, "log_refused",
1828 		       SYSCTL_DESCR("Log refused TCP connections"),
1829 		       NULL, 0, &tcp_log_refused, 0,
1830 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOG_REFUSED, CTL_EOL);
1831 #if 0 /* obsoleted */
1832 	sysctl_createv(clog, 0, NULL, NULL,
1833 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1834 		       CTLTYPE_INT, "rstratelimit", NULL,
1835 		       NULL, 0, &tcp_rst_ratelim, 0,
1836 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTRATELIMIT, CTL_EOL);
1837 #endif
1838 	sysctl_createv(clog, 0, NULL, NULL,
1839 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1840 		       CTLTYPE_INT, "rstppslimit",
1841 		       SYSCTL_DESCR("Maximum number of RST packets to send "
1842 				    "per second"),
1843 		       NULL, 0, &tcp_rst_ppslim, 0,
1844 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTPPSLIMIT, CTL_EOL);
1845 	sysctl_createv(clog, 0, NULL, NULL,
1846 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1847 		       CTLTYPE_INT, "delack_ticks",
1848 		       SYSCTL_DESCR("Number of ticks to delay sending an ACK"),
1849 		       NULL, 0, &tcp_delack_ticks, 0,
1850 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DELACK_TICKS, CTL_EOL);
1851 	sysctl_createv(clog, 0, NULL, NULL,
1852 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1853 		       CTLTYPE_INT, "init_win_local",
1854 		       SYSCTL_DESCR("Initial TCP window size (in segments)"),
1855 		       NULL, 0, &tcp_init_win_local, 0,
1856 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN_LOCAL,
1857 		       CTL_EOL);
1858 	sysctl_createv(clog, 0, NULL, NULL,
1859 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1860 		       CTLTYPE_STRUCT, "ident",
1861 		       SYSCTL_DESCR("RFC1413 Identification Protocol lookups"),
1862 		       sysctl_net_inet_tcp_ident, 0, NULL, sizeof(uid_t),
1863 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_IDENT, CTL_EOL);
1864 	sysctl_createv(clog, 0, NULL, NULL,
1865 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1866 		       CTLTYPE_INT, "do_loopback_cksum",
1867 		       SYSCTL_DESCR("Perform TCP checksum on loopback"),
1868 		       NULL, 0, &tcp_do_loopback_cksum, 0,
1869 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOOPBACKCKSUM,
1870 		       CTL_EOL);
1871 	sysctl_createv(clog, 0, NULL, NULL,
1872 		       CTLFLAG_PERMANENT,
1873 		       CTLTYPE_STRUCT, "pcblist",
1874 		       SYSCTL_DESCR("TCP protocol control block list"),
1875 		       sysctl_inpcblist, 0, &tcbtable, 0,
1876 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE,
1877 		       CTL_EOL);
1878 	sysctl_createv(clog, 0, NULL, NULL,
1879 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1880 		       CTLTYPE_INT, "keepinit",
1881 		       SYSCTL_DESCR("Ticks before initial tcp connection times out"),
1882 		       sysctl_tcp_keep, 0, &tcp_keepinit, 0,
1883 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1884 
1885 	/* TCP socket buffers auto-sizing nodes */
1886 	sysctl_createv(clog, 0, NULL, NULL,
1887 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1888 		       CTLTYPE_INT, "recvbuf_auto",
1889 		       SYSCTL_DESCR("Enable automatic receive "
1890 		           "buffer sizing (experimental)"),
1891 		       NULL, 0, &tcp_do_autorcvbuf, 0,
1892 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1893 	sysctl_createv(clog, 0, NULL, NULL,
1894 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1895 		       CTLTYPE_INT, "recvbuf_inc",
1896 		       SYSCTL_DESCR("Incrementor step size of "
1897 		           "automatic receive buffer"),
1898 		       NULL, 0, &tcp_autorcvbuf_inc, 0,
1899 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1900 	sysctl_createv(clog, 0, NULL, NULL,
1901 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1902 		       CTLTYPE_INT, "recvbuf_max",
1903 		       SYSCTL_DESCR("Max size of automatic receive buffer"),
1904 		       NULL, 0, &tcp_autorcvbuf_max, 0,
1905 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1906 
1907 	sysctl_createv(clog, 0, NULL, NULL,
1908 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1909 		       CTLTYPE_INT, "sendbuf_auto",
1910 		       SYSCTL_DESCR("Enable automatic send "
1911 		           "buffer sizing (experimental)"),
1912 		       NULL, 0, &tcp_do_autosndbuf, 0,
1913 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1914 	sysctl_createv(clog, 0, NULL, NULL,
1915 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1916 		       CTLTYPE_INT, "sendbuf_inc",
1917 		       SYSCTL_DESCR("Incrementor step size of "
1918 		           "automatic send buffer"),
1919 		       NULL, 0, &tcp_autosndbuf_inc, 0,
1920 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1921 	sysctl_createv(clog, 0, NULL, NULL,
1922 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1923 		       CTLTYPE_INT, "sendbuf_max",
1924 		       SYSCTL_DESCR("Max size of automatic send buffer"),
1925 		       NULL, 0, &tcp_autosndbuf_max, 0,
1926 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1927 
1928 	/* ECN subtree */
1929 	sysctl_createv(clog, 0, NULL, &ecn_node,
1930 	    	       CTLFLAG_PERMANENT,
1931 		       CTLTYPE_NODE, "ecn",
1932 	    	       SYSCTL_DESCR("RFC3168 Explicit Congestion Notification"),
1933 	    	       NULL, 0, NULL, 0,
1934 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
1935 	sysctl_createv(clog, 0, &ecn_node, NULL,
1936 	    	       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1937 		       CTLTYPE_INT, "enable",
1938 		       SYSCTL_DESCR("Enable TCP Explicit Congestion "
1939 			   "Notification"),
1940 	    	       NULL, 0, &tcp_do_ecn, 0, CTL_CREATE, CTL_EOL);
1941 	sysctl_createv(clog, 0, &ecn_node, NULL,
1942 	    	       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1943 		       CTLTYPE_INT, "maxretries",
1944 		       SYSCTL_DESCR("Number of times to retry ECN setup "
1945 			       "before disabling ECN on the connection"),
1946 	    	       NULL, 0, &tcp_ecn_maxretries, 0, CTL_CREATE, CTL_EOL);
1947 
1948 	/* SACK gets it's own little subtree. */
1949 	sysctl_createv(clog, 0, NULL, &sack_node,
1950 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1951 		       CTLTYPE_INT, "enable",
1952 		       SYSCTL_DESCR("Enable RFC2018 Selective ACKnowledgement"),
1953 		       NULL, 0, &tcp_do_sack, 0,
1954 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1955 	sysctl_createv(clog, 0, NULL, &sack_node,
1956 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1957 		       CTLTYPE_INT, "maxholes",
1958 		       SYSCTL_DESCR("Maximum number of TCP SACK holes allowed per connection"),
1959 		       NULL, 0, &tcp_sack_tp_maxholes, 0,
1960 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1961 	sysctl_createv(clog, 0, NULL, &sack_node,
1962 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1963 		       CTLTYPE_INT, "globalmaxholes",
1964 		       SYSCTL_DESCR("Global maximum number of TCP SACK holes"),
1965 		       NULL, 0, &tcp_sack_globalmaxholes, 0,
1966 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1967 	sysctl_createv(clog, 0, NULL, &sack_node,
1968 		       CTLFLAG_PERMANENT,
1969 		       CTLTYPE_INT, "globalholes",
1970 		       SYSCTL_DESCR("Global number of TCP SACK holes"),
1971 		       NULL, 0, &tcp_sack_globalholes, 0,
1972 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1973 
1974 	sysctl_createv(clog, 0, NULL, NULL,
1975 		       CTLFLAG_PERMANENT,
1976 		       CTLTYPE_STRUCT, "stats",
1977 		       SYSCTL_DESCR("TCP statistics"),
1978 		       sysctl_net_inet_tcp_stats, 0, NULL, 0,
1979 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_STATS,
1980 		       CTL_EOL);
1981 #ifdef TCP_DEBUG
1982 	sysctl_createv(clog, 0, NULL, NULL,
1983 		       CTLFLAG_PERMANENT,
1984 		       CTLTYPE_STRUCT, "debug",
1985 		       SYSCTL_DESCR("TCP sockets debug information"),
1986 		       NULL, 0, &tcp_debug, sizeof(tcp_debug),
1987 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBUG,
1988 		       CTL_EOL);
1989 	sysctl_createv(clog, 0, NULL, NULL,
1990 		       CTLFLAG_PERMANENT,
1991 		       CTLTYPE_INT, "debx",
1992 		       SYSCTL_DESCR("Number of TCP debug sockets messages"),
1993 		       NULL, 0, &tcp_debx, sizeof(tcp_debx),
1994 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBX,
1995 		       CTL_EOL);
1996 #endif
1997 	sysctl_createv(clog, 0, NULL, NULL,
1998 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1999 		       CTLTYPE_STRUCT, "drop",
2000 		       SYSCTL_DESCR("TCP drop connection"),
2001 		       sysctl_net_inet_tcp_drop, 0, NULL, 0,
2002 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DROP, CTL_EOL);
2003 #if NRND > 0
2004 	sysctl_createv(clog, 0, NULL, NULL,
2005 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2006 		       CTLTYPE_INT, "iss_hash",
2007 		       SYSCTL_DESCR("Enable RFC 1948 ISS by cryptographic "
2008 				    "hash computation"),
2009 		       NULL, 0, &tcp_do_rfc1948, sizeof(tcp_do_rfc1948),
2010 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE,
2011 		       CTL_EOL);
2012 #endif
2013 
2014 	/* ABC subtree */
2015 
2016 	sysctl_createv(clog, 0, NULL, &abc_node,
2017 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "abc",
2018 		       SYSCTL_DESCR("RFC3465 Appropriate Byte Counting (ABC)"),
2019 		       NULL, 0, NULL, 0,
2020 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL);
2021 	sysctl_createv(clog, 0, &abc_node, NULL,
2022 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2023 		       CTLTYPE_INT, "enable",
2024 		       SYSCTL_DESCR("Enable RFC3465 Appropriate Byte Counting"),
2025 		       NULL, 0, &tcp_do_abc, 0, CTL_CREATE, CTL_EOL);
2026 	sysctl_createv(clog, 0, &abc_node, NULL,
2027 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2028 		       CTLTYPE_INT, "aggressive",
2029 		       SYSCTL_DESCR("1: L=2*SMSS 0: L=1*SMSS"),
2030 		       NULL, 0, &tcp_abc_aggressive, 0, CTL_CREATE, CTL_EOL);
2031 }
2032 
2033 void
2034 tcp_usrreq_init(void)
2035 {
2036 
2037 #ifdef INET
2038 	sysctl_net_inet_tcp_setup2(NULL, PF_INET, "inet", "tcp");
2039 #endif
2040 #ifdef INET6
2041 	sysctl_net_inet_tcp_setup2(NULL, PF_INET6, "inet6", "tcp6");
2042 #endif
2043 }
2044