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