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