xref: /netbsd-src/sys/netinet/tcp_usrreq.c (revision c71562d660be5e4ad22016bce45e96f08af190cc)
1 /*	$NetBSD: tcp_usrreq.c,v 1.116 2006/04/15 00:29:25 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.116 2006/04/15 00:29:25 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 		if (so)
922 			soisdisconnected(so);
923 		/*
924 		 * If we are in FIN_WAIT_2, we arrived here because the
925 		 * application did a shutdown of the send side.  Like the
926 		 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after
927 		 * a full close, we start a timer to make sure sockets are
928 		 * not left in FIN_WAIT_2 forever.
929 		 */
930 		if ((tp->t_state == TCPS_FIN_WAIT_2) && (tcp_maxidle > 0))
931 			TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_maxidle);
932 	}
933 	return (tp);
934 }
935 
936 /*
937  * sysctl helper routine for net.inet.ip.mssdflt.  it can't be less
938  * than 32.
939  */
940 static int
941 sysctl_net_inet_tcp_mssdflt(SYSCTLFN_ARGS)
942 {
943 	int error, mssdflt;
944 	struct sysctlnode node;
945 
946 	mssdflt = tcp_mssdflt;
947 	node = *rnode;
948 	node.sysctl_data = &mssdflt;
949 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
950 	if (error || newp == NULL)
951 		return (error);
952 
953 	if (mssdflt < 32)
954 		return (EINVAL);
955 	tcp_mssdflt = mssdflt;
956 
957 	return (0);
958 }
959 
960 /*
961  * sysctl helper routine for setting port related values under
962  * net.inet.ip and net.inet6.ip6.  does basic range checking and does
963  * additional checks for each type.  this code has placed in
964  * tcp_input.c since INET and INET6 both use the same tcp code.
965  *
966  * this helper is not static so that both inet and inet6 can use it.
967  */
968 int
969 sysctl_net_inet_ip_ports(SYSCTLFN_ARGS)
970 {
971 	int error, tmp;
972 	int apmin, apmax;
973 #ifndef IPNOPRIVPORTS
974 	int lpmin, lpmax;
975 #endif /* IPNOPRIVPORTS */
976 	struct sysctlnode node;
977 
978 	if (namelen != 0)
979 		return (EINVAL);
980 
981 	switch (name[-3]) {
982 #ifdef INET
983 	    case PF_INET:
984 		apmin = anonportmin;
985 		apmax = anonportmax;
986 #ifndef IPNOPRIVPORTS
987 		lpmin = lowportmin;
988 		lpmax = lowportmax;
989 #endif /* IPNOPRIVPORTS */
990 		break;
991 #endif /* INET */
992 #ifdef INET6
993 	    case PF_INET6:
994 		apmin = ip6_anonportmin;
995 		apmax = ip6_anonportmax;
996 #ifndef IPNOPRIVPORTS
997 		lpmin = ip6_lowportmin;
998 		lpmax = ip6_lowportmax;
999 #endif /* IPNOPRIVPORTS */
1000 		break;
1001 #endif /* INET6 */
1002 	    default:
1003 		return (EINVAL);
1004 	}
1005 
1006 	/*
1007 	 * insert temporary copy into node, perform lookup on
1008 	 * temporary, then restore pointer
1009 	 */
1010 	node = *rnode;
1011 	tmp = *(int*)rnode->sysctl_data;
1012 	node.sysctl_data = &tmp;
1013 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1014 	if (error || newp == NULL)
1015 		return (error);
1016 
1017 	/*
1018 	 * simple port range check
1019 	 */
1020 	if (tmp < 0 || tmp > 65535)
1021 		return (EINVAL);
1022 
1023 	/*
1024 	 * per-node range checks
1025 	 */
1026 	switch (rnode->sysctl_num) {
1027 	case IPCTL_ANONPORTMIN:
1028 		if (tmp >= apmax)
1029 			return (EINVAL);
1030 #ifndef IPNOPRIVPORTS
1031 		if (tmp < IPPORT_RESERVED)
1032                         return (EINVAL);
1033 #endif /* IPNOPRIVPORTS */
1034 		break;
1035 
1036 	case IPCTL_ANONPORTMAX:
1037                 if (apmin >= tmp)
1038 			return (EINVAL);
1039 #ifndef IPNOPRIVPORTS
1040 		if (tmp < IPPORT_RESERVED)
1041                         return (EINVAL);
1042 #endif /* IPNOPRIVPORTS */
1043 		break;
1044 
1045 #ifndef IPNOPRIVPORTS
1046 	case IPCTL_LOWPORTMIN:
1047 		if (tmp >= lpmax ||
1048 		    tmp > IPPORT_RESERVEDMAX ||
1049 		    tmp < IPPORT_RESERVEDMIN)
1050 			return (EINVAL);
1051 		break;
1052 
1053 	case IPCTL_LOWPORTMAX:
1054 		if (lpmin >= tmp ||
1055 		    tmp > IPPORT_RESERVEDMAX ||
1056 		    tmp < IPPORT_RESERVEDMIN)
1057 			return (EINVAL);
1058 		break;
1059 #endif /* IPNOPRIVPORTS */
1060 
1061 	default:
1062 		return (EINVAL);
1063 	}
1064 
1065 	*(int*)rnode->sysctl_data = tmp;
1066 
1067 	return (0);
1068 }
1069 
1070 /*
1071  * sysctl helper routine for the net.inet.tcp.ident and
1072  * net.inet6.tcp6.ident nodes.  contains backwards compat code for the
1073  * old way of looking up the ident information for ipv4 which involves
1074  * stuffing the port/addr pairs into the mib lookup.
1075  */
1076 static int
1077 sysctl_net_inet_tcp_ident(SYSCTLFN_ARGS)
1078 {
1079 #ifdef INET
1080 	struct inpcb *inb;
1081 	struct sockaddr_in *si4[2];
1082 #endif /* INET */
1083 #ifdef INET6
1084 	struct in6pcb *in6b;
1085 	struct sockaddr_in6 *si6[2];
1086 #endif /* INET6 */
1087 	struct sockaddr_storage sa[2];
1088 	struct socket *sockp;
1089 	size_t sz;
1090 	uid_t uid;
1091 	int error, pf;
1092 
1093 	if (namelen != 4 && namelen != 0)
1094 		return (EINVAL);
1095 	if (name[-2] != IPPROTO_TCP)
1096 		return (EINVAL);
1097 	pf = name[-3];
1098 
1099 	/* old style lookup, ipv4 only */
1100 	if (namelen == 4) {
1101 #ifdef INET
1102 		struct in_addr laddr, raddr;
1103 		u_int lport, rport;
1104 
1105 		if (pf != PF_INET)
1106 			return (EPROTONOSUPPORT);
1107 		raddr.s_addr = (uint32_t)name[0];
1108 		rport = (u_int)name[1];
1109 		laddr.s_addr = (uint32_t)name[2];
1110 		lport = (u_int)name[3];
1111 		inb = in_pcblookup_connect(&tcbtable, raddr, rport,
1112 					   laddr, lport);
1113 		if (inb == NULL || (sockp = inb->inp_socket) == NULL)
1114 			return (ESRCH);
1115 		uid = sockp->so_uidinfo->ui_uid;
1116 		if (oldp) {
1117 			sz = MIN(sizeof(uid), *oldlenp);
1118 			error = copyout(&uid, oldp, sz);
1119 			if (error)
1120 				return (error);
1121 		}
1122 		*oldlenp = sizeof(uid);
1123 		return (0);
1124 #else /* INET */
1125 		return (EINVAL);
1126 #endif /* INET */
1127 	}
1128 
1129 	if (newp == NULL || newlen != sizeof(sa))
1130 		return (EINVAL);
1131 	error = copyin(newp, &sa, newlen);
1132 	if (error)
1133 		return (error);
1134 
1135 	/*
1136 	 * requested families must match
1137 	 */
1138 	if (pf != sa[0].ss_family || sa[0].ss_family != sa[1].ss_family)
1139 		return (EINVAL);
1140 
1141 	switch (pf) {
1142 #ifdef INET
1143 	    case PF_INET:
1144 		si4[0] = (struct sockaddr_in*)&sa[0];
1145 		si4[1] = (struct sockaddr_in*)&sa[1];
1146 		if (si4[0]->sin_len != sizeof(*si4[0]) ||
1147 		    si4[0]->sin_len != si4[1]->sin_len)
1148 			return (EINVAL);
1149 		inb = in_pcblookup_connect(&tcbtable,
1150 		    si4[0]->sin_addr, si4[0]->sin_port,
1151 		    si4[1]->sin_addr, si4[1]->sin_port);
1152 		if (inb == NULL || (sockp = inb->inp_socket) == NULL)
1153 			return (ESRCH);
1154 		break;
1155 #endif /* INET */
1156 #ifdef INET6
1157 	    case PF_INET6:
1158 		si6[0] = (struct sockaddr_in6*)&sa[0];
1159 		si6[1] = (struct sockaddr_in6*)&sa[1];
1160 		if (si6[0]->sin6_len != sizeof(*si6[0]) ||
1161 		    si6[0]->sin6_len != si6[1]->sin6_len)
1162 			return (EINVAL);
1163 		in6b = in6_pcblookup_connect(&tcbtable,
1164 		    &si6[0]->sin6_addr, si6[0]->sin6_port,
1165 		    &si6[1]->sin6_addr, si6[1]->sin6_port, 0);
1166 		if (in6b == NULL || (sockp = in6b->in6p_socket) == NULL)
1167 			return (ESRCH);
1168 		break;
1169 #endif /* INET6 */
1170 	    default:
1171 		return (EPROTONOSUPPORT);
1172 	}
1173 	*oldlenp = sizeof(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 
1184 	return (0);
1185 }
1186 
1187 /*
1188  * sysctl helper for the inet and inet6 pcblists.  handles tcp/udp and
1189  * inet/inet6, as well as raw pcbs for each.  specifically not
1190  * declared static so that raw sockets and udp/udp6 can use it as
1191  * well.
1192  */
1193 int
1194 sysctl_inpcblist(SYSCTLFN_ARGS)
1195 {
1196 #ifdef INET
1197 	struct sockaddr_in *in;
1198 	const struct inpcb *inp;
1199 #endif
1200 #ifdef INET6
1201 	struct sockaddr_in6 *in6;
1202 	const struct in6pcb *in6p;
1203 #endif
1204 	/*
1205 	 * sysctl_data is const, but CIRCLEQ_FOREACH can't use a const
1206 	 * struct inpcbtable pointer, so we have to discard const.  :-/
1207 	 */
1208 	struct inpcbtable *pcbtbl = __UNCONST(rnode->sysctl_data);
1209 	const struct inpcb_hdr *inph;
1210 	struct tcpcb *tp;
1211 	struct kinfo_pcb pcb;
1212 	char *dp;
1213 	u_int op, arg;
1214 	size_t len, needed, elem_size, out_size;
1215 	int error, elem_count, pf, proto, pf2;
1216 
1217 	if (namelen != 4)
1218 		return (EINVAL);
1219 
1220 	if (oldp != NULL) {
1221 		    len = *oldlenp;
1222 		    elem_size = name[2];
1223 		    elem_count = name[3];
1224 		    if (elem_size != sizeof(pcb))
1225 			    return EINVAL;
1226 	} else {
1227 		    len = 0;
1228 		    elem_count = INT_MAX;
1229 		    elem_size = sizeof(pcb);
1230 	}
1231 	error = 0;
1232 	dp = oldp;
1233 	op = name[0];
1234 	arg = name[1];
1235 	out_size = elem_size;
1236 	needed = 0;
1237 
1238 	if (namelen == 1 && name[0] == CTL_QUERY)
1239 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
1240 
1241 	if (name - oname != 4)
1242 		return (EINVAL);
1243 
1244 	pf = oname[1];
1245 	proto = oname[2];
1246 	pf2 = (oldp != NULL) ? pf : 0;
1247 
1248 	CIRCLEQ_FOREACH(inph, &pcbtbl->inpt_queue, inph_queue) {
1249 #ifdef INET
1250 		inp = (const struct inpcb *)inph;
1251 #endif
1252 #ifdef INET6
1253 		in6p = (const struct in6pcb *)inph;
1254 #endif
1255 
1256 		if (inph->inph_af != pf)
1257 			continue;
1258 
1259 		if (CURTAIN(l->l_proc->p_ucred->cr_uid,
1260 			    inph->inph_socket->so_uidinfo->ui_uid))
1261 			continue;
1262 
1263 		memset(&pcb, 0, sizeof(pcb));
1264 
1265 		pcb.ki_family = pf;
1266 		pcb.ki_type = proto;
1267 
1268 		switch (pf2) {
1269 		case 0:
1270 			/* just probing for size */
1271 			break;
1272 #ifdef INET
1273 		case PF_INET:
1274 			pcb.ki_family = inp->inp_socket->so_proto->
1275 			    pr_domain->dom_family;
1276 			pcb.ki_type = inp->inp_socket->so_proto->
1277 			    pr_type;
1278 			pcb.ki_protocol = inp->inp_socket->so_proto->
1279 			    pr_protocol;
1280 			pcb.ki_pflags = inp->inp_flags;
1281 
1282 			pcb.ki_sostate = inp->inp_socket->so_state;
1283 			pcb.ki_prstate = inp->inp_state;
1284 			if (proto == IPPROTO_TCP) {
1285 				tp = intotcpcb(inp);
1286 				pcb.ki_tstate = tp->t_state;
1287 				pcb.ki_tflags = tp->t_flags;
1288 			}
1289 
1290 			pcb.ki_pcbaddr = PTRTOUINT64(inp);
1291 			pcb.ki_ppcbaddr = PTRTOUINT64(inp->inp_ppcb);
1292 			pcb.ki_sockaddr = PTRTOUINT64(inp->inp_socket);
1293 
1294 			pcb.ki_rcvq = inp->inp_socket->so_rcv.sb_cc;
1295 			pcb.ki_sndq = inp->inp_socket->so_snd.sb_cc;
1296 
1297 			in = satosin(&pcb.ki_src);
1298 			in->sin_len = sizeof(*in);
1299 			in->sin_family = pf;
1300 			in->sin_port = inp->inp_lport;
1301 			in->sin_addr = inp->inp_laddr;
1302 			if (pcb.ki_prstate >= INP_CONNECTED) {
1303 				in = satosin(&pcb.ki_dst);
1304 				in->sin_len = sizeof(*in);
1305 				in->sin_family = pf;
1306 				in->sin_port = inp->inp_fport;
1307 				in->sin_addr = inp->inp_faddr;
1308 			}
1309 			break;
1310 #endif
1311 #ifdef INET6
1312 		case PF_INET6:
1313 			pcb.ki_family = in6p->in6p_socket->so_proto->
1314 			    pr_domain->dom_family;
1315 			pcb.ki_type = in6p->in6p_socket->so_proto->pr_type;
1316 			pcb.ki_protocol = in6p->in6p_socket->so_proto->
1317 			    pr_protocol;
1318 			pcb.ki_pflags = in6p->in6p_flags;
1319 
1320 			pcb.ki_sostate = in6p->in6p_socket->so_state;
1321 			pcb.ki_prstate = in6p->in6p_state;
1322 			if (proto == IPPROTO_TCP) {
1323 				tp = in6totcpcb(in6p);
1324 				pcb.ki_tstate = tp->t_state;
1325 				pcb.ki_tflags = tp->t_flags;
1326 			}
1327 
1328 			pcb.ki_pcbaddr = PTRTOUINT64(in6p);
1329 			pcb.ki_ppcbaddr = PTRTOUINT64(in6p->in6p_ppcb);
1330 			pcb.ki_sockaddr = PTRTOUINT64(in6p->in6p_socket);
1331 
1332 			pcb.ki_rcvq = in6p->in6p_socket->so_rcv.sb_cc;
1333 			pcb.ki_sndq = in6p->in6p_socket->so_snd.sb_cc;
1334 
1335 			in6 = satosin6(&pcb.ki_src);
1336 			in6->sin6_len = sizeof(*in6);
1337 			in6->sin6_family = pf;
1338 			in6->sin6_port = in6p->in6p_lport;
1339 			in6->sin6_flowinfo = in6p->in6p_flowinfo;
1340 			in6->sin6_addr = in6p->in6p_laddr;
1341 			in6->sin6_scope_id = 0; /* XXX? */
1342 
1343 			if (pcb.ki_prstate >= IN6P_CONNECTED) {
1344 				in6 = satosin6(&pcb.ki_dst);
1345 				in6->sin6_len = sizeof(*in6);
1346 				in6->sin6_family = pf;
1347 				in6->sin6_port = in6p->in6p_fport;
1348 				in6->sin6_flowinfo = in6p->in6p_flowinfo;
1349 				in6->sin6_addr = in6p->in6p_faddr;
1350 				in6->sin6_scope_id = 0; /* XXX? */
1351 			}
1352 			break;
1353 #endif
1354 		}
1355 
1356 		if (len >= elem_size && elem_count > 0) {
1357 			error = copyout(&pcb, dp, out_size);
1358 			if (error)
1359 				return (error);
1360 			dp += elem_size;
1361 			len -= elem_size;
1362 		}
1363 		if (elem_count > 0) {
1364 			needed += elem_size;
1365 			if (elem_count != INT_MAX)
1366 				elem_count--;
1367 		}
1368 	}
1369 
1370 	*oldlenp = needed;
1371 	if (oldp == NULL)
1372 		*oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb);
1373 
1374 	return (error);
1375 }
1376 
1377 /*
1378  * this (second stage) setup routine is a replacement for tcp_sysctl()
1379  * (which is currently used for ipv4 and ipv6)
1380  */
1381 static void
1382 sysctl_net_inet_tcp_setup2(struct sysctllog **clog, int pf, const char *pfname,
1383 			   const char *tcpname)
1384 {
1385 	const struct sysctlnode *sack_node;
1386 #ifdef TCP_DEBUG
1387 	extern struct tcp_debug tcp_debug[TCP_NDEBUG];
1388 	extern int tcp_debx;
1389 #endif
1390 
1391 	sysctl_createv(clog, 0, NULL, NULL,
1392 		       CTLFLAG_PERMANENT,
1393 		       CTLTYPE_NODE, "net", NULL,
1394 		       NULL, 0, NULL, 0,
1395 		       CTL_NET, CTL_EOL);
1396 	sysctl_createv(clog, 0, NULL, NULL,
1397 		       CTLFLAG_PERMANENT,
1398 		       CTLTYPE_NODE, pfname, NULL,
1399 		       NULL, 0, NULL, 0,
1400 		       CTL_NET, pf, CTL_EOL);
1401 	sysctl_createv(clog, 0, NULL, NULL,
1402 		       CTLFLAG_PERMANENT,
1403 		       CTLTYPE_NODE, tcpname,
1404 		       SYSCTL_DESCR("TCP related settings"),
1405 		       NULL, 0, NULL, 0,
1406 		       CTL_NET, pf, IPPROTO_TCP, CTL_EOL);
1407 
1408 	sysctl_createv(clog, 0, NULL, NULL,
1409 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1410 		       CTLTYPE_INT, "rfc1323",
1411 		       SYSCTL_DESCR("Enable RFC1323 TCP extensions"),
1412 		       NULL, 0, &tcp_do_rfc1323, 0,
1413 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RFC1323, CTL_EOL);
1414 	sysctl_createv(clog, 0, NULL, NULL,
1415 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1416 		       CTLTYPE_INT, "sendspace",
1417 		       SYSCTL_DESCR("Default TCP send buffer size"),
1418 		       NULL, 0, &tcp_sendspace, 0,
1419 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SENDSPACE, CTL_EOL);
1420 	sysctl_createv(clog, 0, NULL, NULL,
1421 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1422 		       CTLTYPE_INT, "recvspace",
1423 		       SYSCTL_DESCR("Default TCP receive buffer size"),
1424 		       NULL, 0, &tcp_recvspace, 0,
1425 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RECVSPACE, CTL_EOL);
1426 	sysctl_createv(clog, 0, NULL, NULL,
1427 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1428 		       CTLTYPE_INT, "mssdflt",
1429 		       SYSCTL_DESCR("Default maximum segment size"),
1430 		       sysctl_net_inet_tcp_mssdflt, 0, &tcp_mssdflt, 0,
1431 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSSDFLT, CTL_EOL);
1432 	sysctl_createv(clog, 0, NULL, NULL,
1433 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1434 		       CTLTYPE_INT, "syn_cache_limit",
1435 		       SYSCTL_DESCR("Maximum number of entries in the TCP "
1436 				    "compressed state engine"),
1437 		       NULL, 0, &tcp_syn_cache_limit, 0,
1438 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_LIMIT,
1439 		       CTL_EOL);
1440 	sysctl_createv(clog, 0, NULL, NULL,
1441 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1442 		       CTLTYPE_INT, "syn_bucket_limit",
1443 		       SYSCTL_DESCR("Maximum number of entries per hash "
1444 				    "bucket in the TCP compressed state "
1445 				    "engine"),
1446 		       NULL, 0, &tcp_syn_bucket_limit, 0,
1447 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_BUCKET_LIMIT,
1448 		       CTL_EOL);
1449 #if 0 /* obsoleted */
1450 	sysctl_createv(clog, 0, NULL, NULL,
1451 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1452 		       CTLTYPE_INT, "syn_cache_interval",
1453 		       SYSCTL_DESCR("TCP compressed state engine's timer interval"),
1454 		       NULL, 0, &tcp_syn_cache_interval, 0,
1455 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_INTER,
1456 		       CTL_EOL);
1457 #endif
1458 	sysctl_createv(clog, 0, NULL, NULL,
1459 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1460 		       CTLTYPE_INT, "init_win",
1461 		       SYSCTL_DESCR("Initial TCP congestion window"),
1462 		       NULL, 0, &tcp_init_win, 0,
1463 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN, CTL_EOL);
1464 	sysctl_createv(clog, 0, NULL, NULL,
1465 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1466 		       CTLTYPE_INT, "mss_ifmtu",
1467 		       SYSCTL_DESCR("Use interface MTU for calculating MSS"),
1468 		       NULL, 0, &tcp_mss_ifmtu, 0,
1469 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSS_IFMTU, CTL_EOL);
1470 	sysctl_createv(clog, 0, NULL, &sack_node,
1471 		       CTLFLAG_PERMANENT,
1472 		       CTLTYPE_NODE, "sack",
1473 		       SYSCTL_DESCR("RFC2018 Selective ACKnowledgement tunables"),
1474 		       NULL, 0, NULL, 0,
1475 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_EOL);
1476 	sysctl_createv(clog, 0, NULL, NULL,
1477 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1478 		       CTLTYPE_INT, "win_scale",
1479 		       SYSCTL_DESCR("Use RFC1323 window scale options"),
1480 		       NULL, 0, &tcp_do_win_scale, 0,
1481 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_WSCALE, CTL_EOL);
1482 	sysctl_createv(clog, 0, NULL, NULL,
1483 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1484 		       CTLTYPE_INT, "timestamps",
1485 		       SYSCTL_DESCR("Use RFC1323 time stamp options"),
1486 		       NULL, 0, &tcp_do_timestamps, 0,
1487 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_TSTAMP, CTL_EOL);
1488 	sysctl_createv(clog, 0, NULL, NULL,
1489 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1490 		       CTLTYPE_INT, "compat_42",
1491 		       SYSCTL_DESCR("Enable workarounds for 4.2BSD TCP bugs"),
1492 		       NULL, 0, &tcp_compat_42, 0,
1493 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_COMPAT_42, CTL_EOL);
1494 	sysctl_createv(clog, 0, NULL, NULL,
1495 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1496 		       CTLTYPE_INT, "cwm",
1497 		       SYSCTL_DESCR("Hughes/Touch/Heidemann Congestion Window "
1498 				    "Monitoring"),
1499 		       NULL, 0, &tcp_cwm, 0,
1500 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM, CTL_EOL);
1501 	sysctl_createv(clog, 0, NULL, NULL,
1502 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1503 		       CTLTYPE_INT, "cwm_burstsize",
1504 		       SYSCTL_DESCR("Congestion Window Monitoring allowed "
1505 				    "burst count in packets"),
1506 		       NULL, 0, &tcp_cwm_burstsize, 0,
1507 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM_BURSTSIZE,
1508 		       CTL_EOL);
1509 	sysctl_createv(clog, 0, NULL, NULL,
1510 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1511 		       CTLTYPE_INT, "ack_on_push",
1512 		       SYSCTL_DESCR("Immediately return ACK when PSH is "
1513 				    "received"),
1514 		       NULL, 0, &tcp_ack_on_push, 0,
1515 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_ACK_ON_PUSH, CTL_EOL);
1516 	sysctl_createv(clog, 0, NULL, NULL,
1517 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1518 		       CTLTYPE_INT, "keepidle",
1519 		       SYSCTL_DESCR("Allowed connection idle ticks before a "
1520 				    "keepalive probe is sent"),
1521 		       NULL, 0, &tcp_keepidle, 0,
1522 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPIDLE, CTL_EOL);
1523 	sysctl_createv(clog, 0, NULL, NULL,
1524 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1525 		       CTLTYPE_INT, "keepintvl",
1526 		       SYSCTL_DESCR("Ticks before next keepalive probe is sent"),
1527 		       NULL, 0, &tcp_keepintvl, 0,
1528 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPINTVL, CTL_EOL);
1529 	sysctl_createv(clog, 0, NULL, NULL,
1530 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1531 		       CTLTYPE_INT, "keepcnt",
1532 		       SYSCTL_DESCR("Number of keepalive probes to send"),
1533 		       NULL, 0, &tcp_keepcnt, 0,
1534 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPCNT, CTL_EOL);
1535 	sysctl_createv(clog, 0, NULL, NULL,
1536 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
1537 		       CTLTYPE_INT, "slowhz",
1538 		       SYSCTL_DESCR("Keepalive ticks per second"),
1539 		       NULL, PR_SLOWHZ, NULL, 0,
1540 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SLOWHZ, CTL_EOL);
1541 	sysctl_createv(clog, 0, NULL, NULL,
1542 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1543 		       CTLTYPE_INT, "newreno",
1544 		       SYSCTL_DESCR("NewReno congestion control algorithm"),
1545 		       NULL, 0, &tcp_do_newreno, 0,
1546 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_NEWRENO, CTL_EOL);
1547 	sysctl_createv(clog, 0, NULL, NULL,
1548 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1549 		       CTLTYPE_INT, "log_refused",
1550 		       SYSCTL_DESCR("Log refused TCP connections"),
1551 		       NULL, 0, &tcp_log_refused, 0,
1552 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOG_REFUSED, CTL_EOL);
1553 #if 0 /* obsoleted */
1554 	sysctl_createv(clog, 0, NULL, NULL,
1555 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1556 		       CTLTYPE_INT, "rstratelimit", NULL,
1557 		       NULL, 0, &tcp_rst_ratelim, 0,
1558 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTRATELIMIT, CTL_EOL);
1559 #endif
1560 	sysctl_createv(clog, 0, NULL, NULL,
1561 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1562 		       CTLTYPE_INT, "rstppslimit",
1563 		       SYSCTL_DESCR("Maximum number of RST packets to send "
1564 				    "per second"),
1565 		       NULL, 0, &tcp_rst_ppslim, 0,
1566 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTPPSLIMIT, CTL_EOL);
1567 	sysctl_createv(clog, 0, NULL, NULL,
1568 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1569 		       CTLTYPE_INT, "delack_ticks",
1570 		       SYSCTL_DESCR("Number of ticks to delay sending an ACK"),
1571 		       NULL, 0, &tcp_delack_ticks, 0,
1572 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DELACK_TICKS, CTL_EOL);
1573 	sysctl_createv(clog, 0, NULL, NULL,
1574 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1575 		       CTLTYPE_INT, "init_win_local",
1576 		       SYSCTL_DESCR("Initial TCP window size (in segments)"),
1577 		       NULL, 0, &tcp_init_win_local, 0,
1578 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN_LOCAL,
1579 		       CTL_EOL);
1580 	sysctl_createv(clog, 0, NULL, NULL,
1581 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1582 		       CTLTYPE_STRUCT, "ident",
1583 		       SYSCTL_DESCR("RFC1413 Identification Protocol lookups"),
1584 		       sysctl_net_inet_tcp_ident, 0, NULL, sizeof(uid_t),
1585 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_IDENT, CTL_EOL);
1586 	sysctl_createv(clog, 0, NULL, NULL,
1587 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1588 		       CTLTYPE_INT, "do_loopback_cksum",
1589 		       SYSCTL_DESCR("Perform TCP checksum on loopback"),
1590 		       NULL, 0, &tcp_do_loopback_cksum, 0,
1591 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOOPBACKCKSUM,
1592 		       CTL_EOL);
1593 	sysctl_createv(clog, 0, NULL, NULL,
1594 		       CTLFLAG_PERMANENT,
1595 		       CTLTYPE_STRUCT, "pcblist",
1596 		       SYSCTL_DESCR("TCP protocol control block list"),
1597 		       sysctl_inpcblist, 0, &tcbtable, 0,
1598 		       CTL_NET, pf, IPPROTO_TCP, CTL_CREATE,
1599 		       CTL_EOL);
1600 
1601 	/* SACK gets it's own little subtree. */
1602 	sysctl_createv(clog, 0, NULL, &sack_node,
1603 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1604 		       CTLTYPE_INT, "enable",
1605 		       SYSCTL_DESCR("Enable RFC2018 Selective ACKnowledgement"),
1606 		       NULL, 0, &tcp_do_sack, 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, "maxholes",
1611 		       SYSCTL_DESCR("Maximum number of TCP SACK holes allowed per connection"),
1612 		       NULL, 0, &tcp_sack_tp_maxholes, 0,
1613 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1614 	sysctl_createv(clog, 0, NULL, &sack_node,
1615 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1616 		       CTLTYPE_INT, "globalmaxholes",
1617 		       SYSCTL_DESCR("Global maximum number of TCP SACK holes"),
1618 		       NULL, 0, &tcp_sack_globalmaxholes, 0,
1619 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1620 	sysctl_createv(clog, 0, NULL, &sack_node,
1621 		       CTLFLAG_PERMANENT,
1622 		       CTLTYPE_INT, "globalholes",
1623 		       SYSCTL_DESCR("Global number of TCP SACK holes"),
1624 		       NULL, 0, &tcp_sack_globalholes, 0,
1625 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL);
1626 
1627 	sysctl_createv(clog, 0, NULL, NULL,
1628 		       CTLFLAG_PERMANENT,
1629 		       CTLTYPE_STRUCT, "stats",
1630 		       SYSCTL_DESCR("TCP statistics"),
1631 		       NULL, 0, &tcpstat, sizeof(tcpstat),
1632 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_STATS,
1633 		       CTL_EOL);
1634 #ifdef TCP_DEBUG
1635 	sysctl_createv(clog, 0, NULL, NULL,
1636 		       CTLFLAG_PERMANENT,
1637 		       CTLTYPE_STRUCT, "debug",
1638 		       SYSCTL_DESCR("TCP sockets debug information"),
1639 		       NULL, 0, &tcp_debug, sizeof(tcp_debug),
1640 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBUG,
1641 		       CTL_EOL);
1642 	sysctl_createv(clog, 0, NULL, NULL,
1643 		       CTLFLAG_PERMANENT,
1644 		       CTLTYPE_INT, "debx",
1645 		       SYSCTL_DESCR("Number of TCP debug sockets messages"),
1646 		       NULL, 0, &tcp_debx, sizeof(tcp_debx),
1647 		       CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBX,
1648 		       CTL_EOL);
1649 #endif
1650 
1651 }
1652 
1653 /*
1654  * Sysctl for tcp variables.
1655  */
1656 #ifdef INET
1657 SYSCTL_SETUP(sysctl_net_inet_tcp_setup, "sysctl net.inet.tcp subtree setup")
1658 {
1659 
1660 	sysctl_net_inet_tcp_setup2(clog, PF_INET, "inet", "tcp");
1661 }
1662 #endif /* INET */
1663 
1664 #ifdef INET6
1665 SYSCTL_SETUP(sysctl_net_inet6_tcp6_setup, "sysctl net.inet6.tcp6 subtree setup")
1666 {
1667 
1668 	sysctl_net_inet_tcp_setup2(clog, PF_INET6, "inet6", "tcp6");
1669 }
1670 #endif /* INET6 */
1671