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