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