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