xref: /freebsd/sys/netinet/tcp_usrreq.c (revision d0ba1baed3f6e4936a0c1b89c25f6c59168ef6de)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1988, 1993
5  *	The Regents of the University of California.
6  * Copyright (c) 2006-2007 Robert N. M. Watson
7  * Copyright (c) 2010-2011 Juniper Networks, Inc.
8  * All rights reserved.
9  *
10  * Portions of this software were developed by Robert N. M. Watson under
11  * contract to Juniper Networks, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
38  */
39 
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 #include "opt_ddb.h"
44 #include "opt_inet.h"
45 #include "opt_inet6.h"
46 #include "opt_ipsec.h"
47 #include "opt_tcpdebug.h"
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/limits.h>
52 #include <sys/malloc.h>
53 #include <sys/refcount.h>
54 #include <sys/kernel.h>
55 #include <sys/sysctl.h>
56 #include <sys/mbuf.h>
57 #ifdef INET6
58 #include <sys/domain.h>
59 #endif /* INET6 */
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/protosw.h>
63 #include <sys/proc.h>
64 #include <sys/jail.h>
65 #include <sys/syslog.h>
66 
67 #ifdef DDB
68 #include <ddb/ddb.h>
69 #endif
70 
71 #include <net/if.h>
72 #include <net/if_var.h>
73 #include <net/route.h>
74 #include <net/vnet.h>
75 
76 #include <netinet/in.h>
77 #include <netinet/in_kdtrace.h>
78 #include <netinet/in_pcb.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/in_var.h>
81 #include <netinet/ip_var.h>
82 #ifdef INET6
83 #include <netinet/ip6.h>
84 #include <netinet6/in6_pcb.h>
85 #include <netinet6/ip6_var.h>
86 #include <netinet6/scope6_var.h>
87 #endif
88 #include <netinet/tcp.h>
89 #include <netinet/tcp_fsm.h>
90 #include <netinet/tcp_seq.h>
91 #include <netinet/tcp_timer.h>
92 #include <netinet/tcp_var.h>
93 #include <netinet/tcp_log_buf.h>
94 #include <netinet/tcpip.h>
95 #include <netinet/cc/cc.h>
96 #include <netinet/tcp_fastopen.h>
97 #include <netinet/tcp_hpts.h>
98 #ifdef TCPPCAP
99 #include <netinet/tcp_pcap.h>
100 #endif
101 #ifdef TCPDEBUG
102 #include <netinet/tcp_debug.h>
103 #endif
104 #ifdef TCP_OFFLOAD
105 #include <netinet/tcp_offload.h>
106 #endif
107 #include <netipsec/ipsec_support.h>
108 
109 /*
110  * TCP protocol interface to socket abstraction.
111  */
112 static int	tcp_attach(struct socket *);
113 #ifdef INET
114 static int	tcp_connect(struct tcpcb *, struct sockaddr *,
115 		    struct thread *td);
116 #endif /* INET */
117 #ifdef INET6
118 static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
119 		    struct thread *td);
120 #endif /* INET6 */
121 static void	tcp_disconnect(struct tcpcb *);
122 static void	tcp_usrclosed(struct tcpcb *);
123 static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
124 
125 #ifdef TCPDEBUG
126 #define	TCPDEBUG0	int ostate = 0
127 #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
128 #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
129 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
130 #else
131 #define	TCPDEBUG0
132 #define	TCPDEBUG1()
133 #define	TCPDEBUG2(req)
134 #endif
135 
136 /*
137  * TCP attaches to socket via pru_attach(), reserving space,
138  * and an internet control block.
139  */
140 static int
141 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
142 {
143 	struct inpcb *inp;
144 	struct tcpcb *tp = NULL;
145 	int error;
146 	TCPDEBUG0;
147 
148 	inp = sotoinpcb(so);
149 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
150 	TCPDEBUG1();
151 
152 	error = tcp_attach(so);
153 	if (error)
154 		goto out;
155 
156 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
157 		so->so_linger = TCP_LINGERTIME;
158 
159 	inp = sotoinpcb(so);
160 	tp = intotcpcb(inp);
161 out:
162 	TCPDEBUG2(PRU_ATTACH);
163 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
164 	return error;
165 }
166 
167 /*
168  * tcp_detach is called when the socket layer loses its final reference
169  * to the socket, be it a file descriptor reference, a reference from TCP,
170  * etc.  At this point, there is only one case in which we will keep around
171  * inpcb state: time wait.
172  *
173  * This function can probably be re-absorbed back into tcp_usr_detach() now
174  * that there is a single detach path.
175  */
176 static void
177 tcp_detach(struct socket *so, struct inpcb *inp)
178 {
179 	struct tcpcb *tp;
180 
181 	INP_INFO_LOCK_ASSERT(&V_tcbinfo);
182 	INP_WLOCK_ASSERT(inp);
183 
184 	KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
185 	KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
186 
187 	tp = intotcpcb(inp);
188 
189 	if (inp->inp_flags & INP_TIMEWAIT) {
190 		/*
191 		 * There are two cases to handle: one in which the time wait
192 		 * state is being discarded (INP_DROPPED), and one in which
193 		 * this connection will remain in timewait.  In the former,
194 		 * it is time to discard all state (except tcptw, which has
195 		 * already been discarded by the timewait close code, which
196 		 * should be further up the call stack somewhere).  In the
197 		 * latter case, we detach from the socket, but leave the pcb
198 		 * present until timewait ends.
199 		 *
200 		 * XXXRW: Would it be cleaner to free the tcptw here?
201 		 *
202 		 * Astute question indeed, from twtcp perspective there are
203 		 * four cases to consider:
204 		 *
205 		 * #1 tcp_detach is called at tcptw creation time by
206 		 *  tcp_twstart, then do not discard the newly created tcptw
207 		 *  and leave inpcb present until timewait ends
208 		 * #2 tcp_detach is called at tcptw creation time by
209 		 *  tcp_twstart, but connection is local and tw will be
210 		 *  discarded immediately
211 		 * #3 tcp_detach is called at timewait end (or reuse) by
212 		 *  tcp_twclose, then the tcptw has already been discarded
213 		 *  (or reused) and inpcb is freed here
214 		 * #4 tcp_detach is called() after timewait ends (or reuse)
215 		 *  (e.g. by soclose), then tcptw has already been discarded
216 		 *  (or reused) and inpcb is freed here
217 		 *
218 		 *  In all three cases the tcptw should not be freed here.
219 		 */
220 		if (inp->inp_flags & INP_DROPPED) {
221 			in_pcbdetach(inp);
222 			if (__predict_true(tp == NULL)) {
223 				in_pcbfree(inp);
224 			} else {
225 				/*
226 				 * This case should not happen as in TIMEWAIT
227 				 * state the inp should not be destroyed before
228 				 * its tcptw.  If INVARIANTS is defined, panic.
229 				 */
230 #ifdef INVARIANTS
231 				panic("%s: Panic before an inp double-free: "
232 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
233 				    , __func__);
234 #else
235 				log(LOG_ERR, "%s: Avoid an inp double-free: "
236 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
237 				    , __func__);
238 #endif
239 				INP_WUNLOCK(inp);
240 			}
241 		} else {
242 			in_pcbdetach(inp);
243 			INP_WUNLOCK(inp);
244 		}
245 	} else {
246 		/*
247 		 * If the connection is not in timewait, we consider two
248 		 * two conditions: one in which no further processing is
249 		 * necessary (dropped || embryonic), and one in which TCP is
250 		 * not yet done, but no longer requires the socket, so the
251 		 * pcb will persist for the time being.
252 		 *
253 		 * XXXRW: Does the second case still occur?
254 		 */
255 		if (inp->inp_flags & INP_DROPPED ||
256 		    tp->t_state < TCPS_SYN_SENT) {
257 			tcp_discardcb(tp);
258 			in_pcbdetach(inp);
259 			in_pcbfree(inp);
260 		} else {
261 			in_pcbdetach(inp);
262 			INP_WUNLOCK(inp);
263 		}
264 	}
265 }
266 
267 /*
268  * pru_detach() detaches the TCP protocol from the socket.
269  * If the protocol state is non-embryonic, then can't
270  * do this directly: have to initiate a pru_disconnect(),
271  * which may finish later; embryonic TCB's can just
272  * be discarded here.
273  */
274 static void
275 tcp_usr_detach(struct socket *so)
276 {
277 	struct inpcb *inp;
278 	int rlock = 0;
279 
280 	inp = sotoinpcb(so);
281 	KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
282 	if (!INP_INFO_WLOCKED(&V_tcbinfo)) {
283 		INP_INFO_RLOCK(&V_tcbinfo);
284 		rlock = 1;
285 	}
286 	INP_WLOCK(inp);
287 	KASSERT(inp->inp_socket != NULL,
288 	    ("tcp_usr_detach: inp_socket == NULL"));
289 	tcp_detach(so, inp);
290 	if (rlock)
291 		INP_INFO_RUNLOCK(&V_tcbinfo);
292 }
293 
294 #ifdef INET
295 /*
296  * Give the socket an address.
297  */
298 static int
299 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
300 {
301 	int error = 0;
302 	struct inpcb *inp;
303 	struct tcpcb *tp = NULL;
304 	struct sockaddr_in *sinp;
305 
306 	sinp = (struct sockaddr_in *)nam;
307 	if (nam->sa_len != sizeof (*sinp))
308 		return (EINVAL);
309 	/*
310 	 * Must check for multicast addresses and disallow binding
311 	 * to them.
312 	 */
313 	if (sinp->sin_family == AF_INET &&
314 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
315 		return (EAFNOSUPPORT);
316 
317 	TCPDEBUG0;
318 	inp = sotoinpcb(so);
319 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
320 	INP_WLOCK(inp);
321 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
322 		error = EINVAL;
323 		goto out;
324 	}
325 	tp = intotcpcb(inp);
326 	TCPDEBUG1();
327 	INP_HASH_WLOCK(&V_tcbinfo);
328 	error = in_pcbbind(inp, nam, td->td_ucred);
329 	INP_HASH_WUNLOCK(&V_tcbinfo);
330 out:
331 	TCPDEBUG2(PRU_BIND);
332 	TCP_PROBE2(debug__user, tp, PRU_BIND);
333 	INP_WUNLOCK(inp);
334 
335 	return (error);
336 }
337 #endif /* INET */
338 
339 #ifdef INET6
340 static int
341 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
342 {
343 	int error = 0;
344 	struct inpcb *inp;
345 	struct tcpcb *tp = NULL;
346 	struct sockaddr_in6 *sin6p;
347 
348 	sin6p = (struct sockaddr_in6 *)nam;
349 	if (nam->sa_len != sizeof (*sin6p))
350 		return (EINVAL);
351 	/*
352 	 * Must check for multicast addresses and disallow binding
353 	 * to them.
354 	 */
355 	if (sin6p->sin6_family == AF_INET6 &&
356 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
357 		return (EAFNOSUPPORT);
358 
359 	TCPDEBUG0;
360 	inp = sotoinpcb(so);
361 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
362 	INP_WLOCK(inp);
363 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
364 		error = EINVAL;
365 		goto out;
366 	}
367 	tp = intotcpcb(inp);
368 	TCPDEBUG1();
369 	INP_HASH_WLOCK(&V_tcbinfo);
370 	inp->inp_vflag &= ~INP_IPV4;
371 	inp->inp_vflag |= INP_IPV6;
372 #ifdef INET
373 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
374 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
375 			inp->inp_vflag |= INP_IPV4;
376 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
377 			struct sockaddr_in sin;
378 
379 			in6_sin6_2_sin(&sin, sin6p);
380 			inp->inp_vflag |= INP_IPV4;
381 			inp->inp_vflag &= ~INP_IPV6;
382 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
383 			    td->td_ucred);
384 			INP_HASH_WUNLOCK(&V_tcbinfo);
385 			goto out;
386 		}
387 	}
388 #endif
389 	error = in6_pcbbind(inp, nam, td->td_ucred);
390 	INP_HASH_WUNLOCK(&V_tcbinfo);
391 out:
392 	TCPDEBUG2(PRU_BIND);
393 	TCP_PROBE2(debug__user, tp, PRU_BIND);
394 	INP_WUNLOCK(inp);
395 	return (error);
396 }
397 #endif /* INET6 */
398 
399 #ifdef INET
400 /*
401  * Prepare to accept connections.
402  */
403 static int
404 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
405 {
406 	int error = 0;
407 	struct inpcb *inp;
408 	struct tcpcb *tp = NULL;
409 
410 	TCPDEBUG0;
411 	inp = sotoinpcb(so);
412 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
413 	INP_WLOCK(inp);
414 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
415 		error = EINVAL;
416 		goto out;
417 	}
418 	tp = intotcpcb(inp);
419 	TCPDEBUG1();
420 	SOCK_LOCK(so);
421 	error = solisten_proto_check(so);
422 	INP_HASH_WLOCK(&V_tcbinfo);
423 	if (error == 0 && inp->inp_lport == 0)
424 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
425 	INP_HASH_WUNLOCK(&V_tcbinfo);
426 	if (error == 0) {
427 		tcp_state_change(tp, TCPS_LISTEN);
428 		solisten_proto(so, backlog);
429 #ifdef TCP_OFFLOAD
430 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
431 			tcp_offload_listen_start(tp);
432 #endif
433 	}
434 	SOCK_UNLOCK(so);
435 
436 	if (IS_FASTOPEN(tp->t_flags))
437 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
438 
439 out:
440 	TCPDEBUG2(PRU_LISTEN);
441 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
442 	INP_WUNLOCK(inp);
443 	return (error);
444 }
445 #endif /* INET */
446 
447 #ifdef INET6
448 static int
449 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
450 {
451 	int error = 0;
452 	struct inpcb *inp;
453 	struct tcpcb *tp = NULL;
454 
455 	TCPDEBUG0;
456 	inp = sotoinpcb(so);
457 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
458 	INP_WLOCK(inp);
459 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
460 		error = EINVAL;
461 		goto out;
462 	}
463 	tp = intotcpcb(inp);
464 	TCPDEBUG1();
465 	SOCK_LOCK(so);
466 	error = solisten_proto_check(so);
467 	INP_HASH_WLOCK(&V_tcbinfo);
468 	if (error == 0 && inp->inp_lport == 0) {
469 		inp->inp_vflag &= ~INP_IPV4;
470 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
471 			inp->inp_vflag |= INP_IPV4;
472 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
473 	}
474 	INP_HASH_WUNLOCK(&V_tcbinfo);
475 	if (error == 0) {
476 		tcp_state_change(tp, TCPS_LISTEN);
477 		solisten_proto(so, backlog);
478 #ifdef TCP_OFFLOAD
479 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
480 			tcp_offload_listen_start(tp);
481 #endif
482 	}
483 	SOCK_UNLOCK(so);
484 
485 	if (IS_FASTOPEN(tp->t_flags))
486 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
487 
488 out:
489 	TCPDEBUG2(PRU_LISTEN);
490 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
491 	INP_WUNLOCK(inp);
492 	return (error);
493 }
494 #endif /* INET6 */
495 
496 #ifdef INET
497 /*
498  * Initiate connection to peer.
499  * Create a template for use in transmissions on this connection.
500  * Enter SYN_SENT state, and mark socket as connecting.
501  * Start keep-alive timer, and seed output sequence space.
502  * Send initial segment on connection.
503  */
504 static int
505 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
506 {
507 	int error = 0;
508 	struct inpcb *inp;
509 	struct tcpcb *tp = NULL;
510 	struct sockaddr_in *sinp;
511 
512 	sinp = (struct sockaddr_in *)nam;
513 	if (nam->sa_len != sizeof (*sinp))
514 		return (EINVAL);
515 	/*
516 	 * Must disallow TCP ``connections'' to multicast addresses.
517 	 */
518 	if (sinp->sin_family == AF_INET
519 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
520 		return (EAFNOSUPPORT);
521 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
522 		return (error);
523 
524 	TCPDEBUG0;
525 	inp = sotoinpcb(so);
526 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
527 	INP_WLOCK(inp);
528 	if (inp->inp_flags & INP_TIMEWAIT) {
529 		error = EADDRINUSE;
530 		goto out;
531 	}
532 	if (inp->inp_flags & INP_DROPPED) {
533 		error = ECONNREFUSED;
534 		goto out;
535 	}
536 	tp = intotcpcb(inp);
537 	TCPDEBUG1();
538 	if ((error = tcp_connect(tp, nam, td)) != 0)
539 		goto out;
540 #ifdef TCP_OFFLOAD
541 	if (registered_toedevs > 0 &&
542 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
543 	    (error = tcp_offload_connect(so, nam)) == 0)
544 		goto out;
545 #endif
546 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
547 	error = tp->t_fb->tfb_tcp_output(tp);
548 out:
549 	TCPDEBUG2(PRU_CONNECT);
550 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
551 	INP_WUNLOCK(inp);
552 	return (error);
553 }
554 #endif /* INET */
555 
556 #ifdef INET6
557 static int
558 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
559 {
560 	int error = 0;
561 	struct inpcb *inp;
562 	struct tcpcb *tp = NULL;
563 	struct sockaddr_in6 *sin6p;
564 
565 	TCPDEBUG0;
566 
567 	sin6p = (struct sockaddr_in6 *)nam;
568 	if (nam->sa_len != sizeof (*sin6p))
569 		return (EINVAL);
570 	/*
571 	 * Must disallow TCP ``connections'' to multicast addresses.
572 	 */
573 	if (sin6p->sin6_family == AF_INET6
574 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
575 		return (EAFNOSUPPORT);
576 
577 	inp = sotoinpcb(so);
578 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
579 	INP_WLOCK(inp);
580 	if (inp->inp_flags & INP_TIMEWAIT) {
581 		error = EADDRINUSE;
582 		goto out;
583 	}
584 	if (inp->inp_flags & INP_DROPPED) {
585 		error = ECONNREFUSED;
586 		goto out;
587 	}
588 	tp = intotcpcb(inp);
589 	TCPDEBUG1();
590 #ifdef INET
591 	/*
592 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
593 	 * therefore probably require the hash lock, which isn't held here.
594 	 * Is this a significant problem?
595 	 */
596 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
597 		struct sockaddr_in sin;
598 
599 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
600 			error = EINVAL;
601 			goto out;
602 		}
603 		if ((inp->inp_vflag & INP_IPV4) == 0) {
604 			error = EAFNOSUPPORT;
605 			goto out;
606 		}
607 
608 		in6_sin6_2_sin(&sin, sin6p);
609 		inp->inp_vflag |= INP_IPV4;
610 		inp->inp_vflag &= ~INP_IPV6;
611 		if ((error = prison_remote_ip4(td->td_ucred,
612 		    &sin.sin_addr)) != 0)
613 			goto out;
614 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
615 			goto out;
616 #ifdef TCP_OFFLOAD
617 		if (registered_toedevs > 0 &&
618 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
619 		    (error = tcp_offload_connect(so, nam)) == 0)
620 			goto out;
621 #endif
622 		error = tp->t_fb->tfb_tcp_output(tp);
623 		goto out;
624 	} else {
625 		if ((inp->inp_vflag & INP_IPV6) == 0) {
626 			error = EAFNOSUPPORT;
627 			goto out;
628 		}
629 	}
630 #endif
631 	inp->inp_vflag &= ~INP_IPV4;
632 	inp->inp_vflag |= INP_IPV6;
633 	inp->inp_inc.inc_flags |= INC_ISIPV6;
634 	if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
635 		goto out;
636 	if ((error = tcp6_connect(tp, nam, td)) != 0)
637 		goto out;
638 #ifdef TCP_OFFLOAD
639 	if (registered_toedevs > 0 &&
640 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
641 	    (error = tcp_offload_connect(so, nam)) == 0)
642 		goto out;
643 #endif
644 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
645 	error = tp->t_fb->tfb_tcp_output(tp);
646 
647 out:
648 	TCPDEBUG2(PRU_CONNECT);
649 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
650 	INP_WUNLOCK(inp);
651 	return (error);
652 }
653 #endif /* INET6 */
654 
655 /*
656  * Initiate disconnect from peer.
657  * If connection never passed embryonic stage, just drop;
658  * else if don't need to let data drain, then can just drop anyways,
659  * else have to begin TCP shutdown process: mark socket disconnecting,
660  * drain unread data, state switch to reflect user close, and
661  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
662  * when peer sends FIN and acks ours.
663  *
664  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
665  */
666 static int
667 tcp_usr_disconnect(struct socket *so)
668 {
669 	struct inpcb *inp;
670 	struct tcpcb *tp = NULL;
671 	int error = 0;
672 
673 	TCPDEBUG0;
674 	INP_INFO_RLOCK(&V_tcbinfo);
675 	inp = sotoinpcb(so);
676 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
677 	INP_WLOCK(inp);
678 	if (inp->inp_flags & INP_TIMEWAIT)
679 		goto out;
680 	if (inp->inp_flags & INP_DROPPED) {
681 		error = ECONNRESET;
682 		goto out;
683 	}
684 	tp = intotcpcb(inp);
685 	TCPDEBUG1();
686 	tcp_disconnect(tp);
687 out:
688 	TCPDEBUG2(PRU_DISCONNECT);
689 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
690 	INP_WUNLOCK(inp);
691 	INP_INFO_RUNLOCK(&V_tcbinfo);
692 	return (error);
693 }
694 
695 #ifdef INET
696 /*
697  * Accept a connection.  Essentially all the work is done at higher levels;
698  * just return the address of the peer, storing through addr.
699  */
700 static int
701 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
702 {
703 	int error = 0;
704 	struct inpcb *inp = NULL;
705 	struct tcpcb *tp = NULL;
706 	struct in_addr addr;
707 	in_port_t port = 0;
708 	TCPDEBUG0;
709 
710 	if (so->so_state & SS_ISDISCONNECTED)
711 		return (ECONNABORTED);
712 
713 	inp = sotoinpcb(so);
714 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
715 	INP_WLOCK(inp);
716 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
717 		error = ECONNABORTED;
718 		goto out;
719 	}
720 	tp = intotcpcb(inp);
721 	TCPDEBUG1();
722 
723 	/*
724 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
725 	 * copy the data of interest and defer the malloc until after we
726 	 * release the lock.
727 	 */
728 	port = inp->inp_fport;
729 	addr = inp->inp_faddr;
730 
731 out:
732 	TCPDEBUG2(PRU_ACCEPT);
733 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
734 	INP_WUNLOCK(inp);
735 	if (error == 0)
736 		*nam = in_sockaddr(port, &addr);
737 	return error;
738 }
739 #endif /* INET */
740 
741 #ifdef INET6
742 static int
743 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
744 {
745 	struct inpcb *inp = NULL;
746 	int error = 0;
747 	struct tcpcb *tp = NULL;
748 	struct in_addr addr;
749 	struct in6_addr addr6;
750 	in_port_t port = 0;
751 	int v4 = 0;
752 	TCPDEBUG0;
753 
754 	if (so->so_state & SS_ISDISCONNECTED)
755 		return (ECONNABORTED);
756 
757 	inp = sotoinpcb(so);
758 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
759 	INP_INFO_RLOCK(&V_tcbinfo);
760 	INP_WLOCK(inp);
761 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
762 		error = ECONNABORTED;
763 		goto out;
764 	}
765 	tp = intotcpcb(inp);
766 	TCPDEBUG1();
767 
768 	/*
769 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
770 	 * copy the data of interest and defer the malloc until after we
771 	 * release the lock.
772 	 */
773 	if (inp->inp_vflag & INP_IPV4) {
774 		v4 = 1;
775 		port = inp->inp_fport;
776 		addr = inp->inp_faddr;
777 	} else {
778 		port = inp->inp_fport;
779 		addr6 = inp->in6p_faddr;
780 	}
781 
782 out:
783 	TCPDEBUG2(PRU_ACCEPT);
784 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
785 	INP_WUNLOCK(inp);
786 	INP_INFO_RUNLOCK(&V_tcbinfo);
787 	if (error == 0) {
788 		if (v4)
789 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
790 		else
791 			*nam = in6_sockaddr(port, &addr6);
792 	}
793 	return error;
794 }
795 #endif /* INET6 */
796 
797 /*
798  * Mark the connection as being incapable of further output.
799  */
800 static int
801 tcp_usr_shutdown(struct socket *so)
802 {
803 	int error = 0;
804 	struct inpcb *inp;
805 	struct tcpcb *tp = NULL;
806 
807 	TCPDEBUG0;
808 	INP_INFO_RLOCK(&V_tcbinfo);
809 	inp = sotoinpcb(so);
810 	KASSERT(inp != NULL, ("inp == NULL"));
811 	INP_WLOCK(inp);
812 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
813 		error = ECONNRESET;
814 		goto out;
815 	}
816 	tp = intotcpcb(inp);
817 	TCPDEBUG1();
818 	socantsendmore(so);
819 	tcp_usrclosed(tp);
820 	if (!(inp->inp_flags & INP_DROPPED))
821 		error = tp->t_fb->tfb_tcp_output(tp);
822 
823 out:
824 	TCPDEBUG2(PRU_SHUTDOWN);
825 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
826 	INP_WUNLOCK(inp);
827 	INP_INFO_RUNLOCK(&V_tcbinfo);
828 
829 	return (error);
830 }
831 
832 /*
833  * After a receive, possibly send window update to peer.
834  */
835 static int
836 tcp_usr_rcvd(struct socket *so, int flags)
837 {
838 	struct inpcb *inp;
839 	struct tcpcb *tp = NULL;
840 	int error = 0;
841 
842 	TCPDEBUG0;
843 	inp = sotoinpcb(so);
844 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
845 	INP_WLOCK(inp);
846 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
847 		error = ECONNRESET;
848 		goto out;
849 	}
850 	tp = intotcpcb(inp);
851 	TCPDEBUG1();
852 	/*
853 	 * For passively-created TFO connections, don't attempt a window
854 	 * update while still in SYN_RECEIVED as this may trigger an early
855 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
856 	 * application response data, or failing that, when the DELACK timer
857 	 * expires.
858 	 */
859 	if (IS_FASTOPEN(tp->t_flags) &&
860 	    (tp->t_state == TCPS_SYN_RECEIVED))
861 		goto out;
862 #ifdef TCP_OFFLOAD
863 	if (tp->t_flags & TF_TOE)
864 		tcp_offload_rcvd(tp);
865 	else
866 #endif
867 	tp->t_fb->tfb_tcp_output(tp);
868 
869 out:
870 	TCPDEBUG2(PRU_RCVD);
871 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
872 	INP_WUNLOCK(inp);
873 	return (error);
874 }
875 
876 /*
877  * Do a send by putting data in output queue and updating urgent
878  * marker if URG set.  Possibly send more data.  Unlike the other
879  * pru_*() routines, the mbuf chains are our responsibility.  We
880  * must either enqueue them or free them.  The other pru_* routines
881  * generally are caller-frees.
882  */
883 static int
884 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
885     struct sockaddr *nam, struct mbuf *control, struct thread *td)
886 {
887 	int error = 0;
888 	struct inpcb *inp;
889 	struct tcpcb *tp = NULL;
890 #ifdef INET6
891 	int isipv6;
892 #endif
893 	TCPDEBUG0;
894 
895 	/*
896 	 * We require the pcbinfo lock if we will close the socket as part of
897 	 * this call.
898 	 */
899 	if (flags & PRUS_EOF)
900 		INP_INFO_RLOCK(&V_tcbinfo);
901 	inp = sotoinpcb(so);
902 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
903 	INP_WLOCK(inp);
904 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
905 		if (control)
906 			m_freem(control);
907 		/*
908 		 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
909 		 * for freeing memory.
910 		 */
911 		if (m && (flags & PRUS_NOTREADY) == 0)
912 			m_freem(m);
913 		error = ECONNRESET;
914 		goto out;
915 	}
916 #ifdef INET6
917 	isipv6 = nam && nam->sa_family == AF_INET6;
918 #endif /* INET6 */
919 	tp = intotcpcb(inp);
920 	TCPDEBUG1();
921 	if (control) {
922 		/* TCP doesn't do control messages (rights, creds, etc) */
923 		if (control->m_len) {
924 			m_freem(control);
925 			if (m)
926 				m_freem(m);
927 			error = EINVAL;
928 			goto out;
929 		}
930 		m_freem(control);	/* empty control, just free it */
931 	}
932 	if (!(flags & PRUS_OOB)) {
933 		sbappendstream(&so->so_snd, m, flags);
934 		if (nam && tp->t_state < TCPS_SYN_SENT) {
935 			/*
936 			 * Do implied connect if not yet connected,
937 			 * initialize window to default value, and
938 			 * initialize maxseg using peer's cached MSS.
939 			 */
940 #ifdef INET6
941 			if (isipv6)
942 				error = tcp6_connect(tp, nam, td);
943 #endif /* INET6 */
944 #if defined(INET6) && defined(INET)
945 			else
946 #endif
947 #ifdef INET
948 				error = tcp_connect(tp, nam, td);
949 #endif
950 			if (error)
951 				goto out;
952 			if (IS_FASTOPEN(tp->t_flags))
953 				tcp_fastopen_connect(tp);
954 			else {
955 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
956 				tcp_mss(tp, -1);
957 			}
958 		}
959 		if (flags & PRUS_EOF) {
960 			/*
961 			 * Close the send side of the connection after
962 			 * the data is sent.
963 			 */
964 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
965 			socantsendmore(so);
966 			tcp_usrclosed(tp);
967 		}
968 		if (!(inp->inp_flags & INP_DROPPED) &&
969 		    !(flags & PRUS_NOTREADY)) {
970 			if (flags & PRUS_MORETOCOME)
971 				tp->t_flags |= TF_MORETOCOME;
972 			error = tp->t_fb->tfb_tcp_output(tp);
973 			if (flags & PRUS_MORETOCOME)
974 				tp->t_flags &= ~TF_MORETOCOME;
975 		}
976 	} else {
977 		/*
978 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
979 		 */
980 		SOCKBUF_LOCK(&so->so_snd);
981 		if (sbspace(&so->so_snd) < -512) {
982 			SOCKBUF_UNLOCK(&so->so_snd);
983 			m_freem(m);
984 			error = ENOBUFS;
985 			goto out;
986 		}
987 		/*
988 		 * According to RFC961 (Assigned Protocols),
989 		 * the urgent pointer points to the last octet
990 		 * of urgent data.  We continue, however,
991 		 * to consider it to indicate the first octet
992 		 * of data past the urgent section.
993 		 * Otherwise, snd_up should be one lower.
994 		 */
995 		sbappendstream_locked(&so->so_snd, m, flags);
996 		SOCKBUF_UNLOCK(&so->so_snd);
997 		if (nam && tp->t_state < TCPS_SYN_SENT) {
998 			/*
999 			 * Do implied connect if not yet connected,
1000 			 * initialize window to default value, and
1001 			 * initialize maxseg using peer's cached MSS.
1002 			 */
1003 
1004 			/*
1005 			 * Not going to contemplate SYN|URG
1006 			 */
1007 			if (IS_FASTOPEN(tp->t_flags))
1008 				tp->t_flags &= ~TF_FASTOPEN;
1009 #ifdef INET6
1010 			if (isipv6)
1011 				error = tcp6_connect(tp, nam, td);
1012 #endif /* INET6 */
1013 #if defined(INET6) && defined(INET)
1014 			else
1015 #endif
1016 #ifdef INET
1017 				error = tcp_connect(tp, nam, td);
1018 #endif
1019 			if (error)
1020 				goto out;
1021 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1022 			tcp_mss(tp, -1);
1023 		}
1024 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1025 		if (!(flags & PRUS_NOTREADY)) {
1026 			tp->t_flags |= TF_FORCEDATA;
1027 			error = tp->t_fb->tfb_tcp_output(tp);
1028 			tp->t_flags &= ~TF_FORCEDATA;
1029 		}
1030 	}
1031 	TCP_LOG_EVENT(tp, NULL,
1032 	    &inp->inp_socket->so_rcv,
1033 	    &inp->inp_socket->so_snd,
1034 	    TCP_LOG_USERSEND, error,
1035 	    0, NULL, false);
1036 out:
1037 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
1038 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1039 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1040 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1041 	INP_WUNLOCK(inp);
1042 	if (flags & PRUS_EOF)
1043 		INP_INFO_RUNLOCK(&V_tcbinfo);
1044 	return (error);
1045 }
1046 
1047 static int
1048 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1049 {
1050 	struct inpcb *inp;
1051 	struct tcpcb *tp;
1052 	int error;
1053 
1054 	inp = sotoinpcb(so);
1055 	INP_WLOCK(inp);
1056 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1057 		INP_WUNLOCK(inp);
1058 		for (int i = 0; i < count; i++)
1059 			m = m_free(m);
1060 		return (ECONNRESET);
1061 	}
1062 	tp = intotcpcb(inp);
1063 
1064 	SOCKBUF_LOCK(&so->so_snd);
1065 	error = sbready(&so->so_snd, m, count);
1066 	SOCKBUF_UNLOCK(&so->so_snd);
1067 	if (error == 0)
1068 		error = tp->t_fb->tfb_tcp_output(tp);
1069 	INP_WUNLOCK(inp);
1070 
1071 	return (error);
1072 }
1073 
1074 /*
1075  * Abort the TCP.  Drop the connection abruptly.
1076  */
1077 static void
1078 tcp_usr_abort(struct socket *so)
1079 {
1080 	struct inpcb *inp;
1081 	struct tcpcb *tp = NULL;
1082 	TCPDEBUG0;
1083 
1084 	inp = sotoinpcb(so);
1085 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1086 
1087 	INP_INFO_RLOCK(&V_tcbinfo);
1088 	INP_WLOCK(inp);
1089 	KASSERT(inp->inp_socket != NULL,
1090 	    ("tcp_usr_abort: inp_socket == NULL"));
1091 
1092 	/*
1093 	 * If we still have full TCP state, and we're not dropped, drop.
1094 	 */
1095 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1096 	    !(inp->inp_flags & INP_DROPPED)) {
1097 		tp = intotcpcb(inp);
1098 		TCPDEBUG1();
1099 		tp = tcp_drop(tp, ECONNABORTED);
1100 		if (tp == NULL)
1101 			goto dropped;
1102 		TCPDEBUG2(PRU_ABORT);
1103 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1104 	}
1105 	if (!(inp->inp_flags & INP_DROPPED)) {
1106 		SOCK_LOCK(so);
1107 		so->so_state |= SS_PROTOREF;
1108 		SOCK_UNLOCK(so);
1109 		inp->inp_flags |= INP_SOCKREF;
1110 	}
1111 	INP_WUNLOCK(inp);
1112 dropped:
1113 	INP_INFO_RUNLOCK(&V_tcbinfo);
1114 }
1115 
1116 /*
1117  * TCP socket is closed.  Start friendly disconnect.
1118  */
1119 static void
1120 tcp_usr_close(struct socket *so)
1121 {
1122 	struct inpcb *inp;
1123 	struct tcpcb *tp = NULL;
1124 	TCPDEBUG0;
1125 
1126 	inp = sotoinpcb(so);
1127 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1128 
1129 	INP_INFO_RLOCK(&V_tcbinfo);
1130 	INP_WLOCK(inp);
1131 	KASSERT(inp->inp_socket != NULL,
1132 	    ("tcp_usr_close: inp_socket == NULL"));
1133 
1134 	/*
1135 	 * If we still have full TCP state, and we're not dropped, initiate
1136 	 * a disconnect.
1137 	 */
1138 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1139 	    !(inp->inp_flags & INP_DROPPED)) {
1140 		tp = intotcpcb(inp);
1141 		TCPDEBUG1();
1142 		tcp_disconnect(tp);
1143 		TCPDEBUG2(PRU_CLOSE);
1144 		TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1145 	}
1146 	if (!(inp->inp_flags & INP_DROPPED)) {
1147 		SOCK_LOCK(so);
1148 		so->so_state |= SS_PROTOREF;
1149 		SOCK_UNLOCK(so);
1150 		inp->inp_flags |= INP_SOCKREF;
1151 	}
1152 	INP_WUNLOCK(inp);
1153 	INP_INFO_RUNLOCK(&V_tcbinfo);
1154 }
1155 
1156 /*
1157  * Receive out-of-band data.
1158  */
1159 static int
1160 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1161 {
1162 	int error = 0;
1163 	struct inpcb *inp;
1164 	struct tcpcb *tp = NULL;
1165 
1166 	TCPDEBUG0;
1167 	inp = sotoinpcb(so);
1168 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1169 	INP_WLOCK(inp);
1170 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1171 		error = ECONNRESET;
1172 		goto out;
1173 	}
1174 	tp = intotcpcb(inp);
1175 	TCPDEBUG1();
1176 	if ((so->so_oobmark == 0 &&
1177 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1178 	    so->so_options & SO_OOBINLINE ||
1179 	    tp->t_oobflags & TCPOOB_HADDATA) {
1180 		error = EINVAL;
1181 		goto out;
1182 	}
1183 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1184 		error = EWOULDBLOCK;
1185 		goto out;
1186 	}
1187 	m->m_len = 1;
1188 	*mtod(m, caddr_t) = tp->t_iobc;
1189 	if ((flags & MSG_PEEK) == 0)
1190 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1191 
1192 out:
1193 	TCPDEBUG2(PRU_RCVOOB);
1194 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1195 	INP_WUNLOCK(inp);
1196 	return (error);
1197 }
1198 
1199 #ifdef INET
1200 struct pr_usrreqs tcp_usrreqs = {
1201 	.pru_abort =		tcp_usr_abort,
1202 	.pru_accept =		tcp_usr_accept,
1203 	.pru_attach =		tcp_usr_attach,
1204 	.pru_bind =		tcp_usr_bind,
1205 	.pru_connect =		tcp_usr_connect,
1206 	.pru_control =		in_control,
1207 	.pru_detach =		tcp_usr_detach,
1208 	.pru_disconnect =	tcp_usr_disconnect,
1209 	.pru_listen =		tcp_usr_listen,
1210 	.pru_peeraddr =		in_getpeeraddr,
1211 	.pru_rcvd =		tcp_usr_rcvd,
1212 	.pru_rcvoob =		tcp_usr_rcvoob,
1213 	.pru_send =		tcp_usr_send,
1214 	.pru_ready =		tcp_usr_ready,
1215 	.pru_shutdown =		tcp_usr_shutdown,
1216 	.pru_sockaddr =		in_getsockaddr,
1217 	.pru_sosetlabel =	in_pcbsosetlabel,
1218 	.pru_close =		tcp_usr_close,
1219 };
1220 #endif /* INET */
1221 
1222 #ifdef INET6
1223 struct pr_usrreqs tcp6_usrreqs = {
1224 	.pru_abort =		tcp_usr_abort,
1225 	.pru_accept =		tcp6_usr_accept,
1226 	.pru_attach =		tcp_usr_attach,
1227 	.pru_bind =		tcp6_usr_bind,
1228 	.pru_connect =		tcp6_usr_connect,
1229 	.pru_control =		in6_control,
1230 	.pru_detach =		tcp_usr_detach,
1231 	.pru_disconnect =	tcp_usr_disconnect,
1232 	.pru_listen =		tcp6_usr_listen,
1233 	.pru_peeraddr =		in6_mapped_peeraddr,
1234 	.pru_rcvd =		tcp_usr_rcvd,
1235 	.pru_rcvoob =		tcp_usr_rcvoob,
1236 	.pru_send =		tcp_usr_send,
1237 	.pru_ready =		tcp_usr_ready,
1238 	.pru_shutdown =		tcp_usr_shutdown,
1239 	.pru_sockaddr =		in6_mapped_sockaddr,
1240 	.pru_sosetlabel =	in_pcbsosetlabel,
1241 	.pru_close =		tcp_usr_close,
1242 };
1243 #endif /* INET6 */
1244 
1245 #ifdef INET
1246 /*
1247  * Common subroutine to open a TCP connection to remote host specified
1248  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
1249  * port number if needed.  Call in_pcbconnect_setup to do the routing and
1250  * to choose a local host address (interface).  If there is an existing
1251  * incarnation of the same connection in TIME-WAIT state and if the remote
1252  * host was sending CC options and if the connection duration was < MSL, then
1253  * truncate the previous TIME-WAIT state and proceed.
1254  * Initialize connection parameters and enter SYN-SENT state.
1255  */
1256 static int
1257 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1258 {
1259 	struct inpcb *inp = tp->t_inpcb, *oinp;
1260 	struct socket *so = inp->inp_socket;
1261 	struct in_addr laddr;
1262 	u_short lport;
1263 	int error;
1264 
1265 	INP_WLOCK_ASSERT(inp);
1266 	INP_HASH_WLOCK(&V_tcbinfo);
1267 
1268 	if (inp->inp_lport == 0) {
1269 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1270 		if (error)
1271 			goto out;
1272 	}
1273 
1274 	/*
1275 	 * Cannot simply call in_pcbconnect, because there might be an
1276 	 * earlier incarnation of this same connection still in
1277 	 * TIME_WAIT state, creating an ADDRINUSE error.
1278 	 */
1279 	laddr = inp->inp_laddr;
1280 	lport = inp->inp_lport;
1281 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1282 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1283 	if (error && oinp == NULL)
1284 		goto out;
1285 	if (oinp) {
1286 		error = EADDRINUSE;
1287 		goto out;
1288 	}
1289 	inp->inp_laddr = laddr;
1290 	in_pcbrehash(inp);
1291 	INP_HASH_WUNLOCK(&V_tcbinfo);
1292 
1293 	/*
1294 	 * Compute window scaling to request:
1295 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1296 	 * XXX: This should move to tcp_output().
1297 	 */
1298 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1299 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1300 		tp->request_r_scale++;
1301 
1302 	soisconnecting(so);
1303 	TCPSTAT_INC(tcps_connattempt);
1304 	tcp_state_change(tp, TCPS_SYN_SENT);
1305 	tp->iss = tcp_new_isn(tp);
1306 	tcp_sendseqinit(tp);
1307 
1308 	return 0;
1309 
1310 out:
1311 	INP_HASH_WUNLOCK(&V_tcbinfo);
1312 	return (error);
1313 }
1314 #endif /* INET */
1315 
1316 #ifdef INET6
1317 static int
1318 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1319 {
1320 	struct inpcb *inp = tp->t_inpcb;
1321 	int error;
1322 
1323 	INP_WLOCK_ASSERT(inp);
1324 	INP_HASH_WLOCK(&V_tcbinfo);
1325 
1326 	if (inp->inp_lport == 0) {
1327 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1328 		if (error)
1329 			goto out;
1330 	}
1331 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1332 	if (error != 0)
1333 		goto out;
1334 	INP_HASH_WUNLOCK(&V_tcbinfo);
1335 
1336 	/* Compute window scaling to request.  */
1337 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1338 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1339 		tp->request_r_scale++;
1340 
1341 	soisconnecting(inp->inp_socket);
1342 	TCPSTAT_INC(tcps_connattempt);
1343 	tcp_state_change(tp, TCPS_SYN_SENT);
1344 	tp->iss = tcp_new_isn(tp);
1345 	tcp_sendseqinit(tp);
1346 
1347 	return 0;
1348 
1349 out:
1350 	INP_HASH_WUNLOCK(&V_tcbinfo);
1351 	return error;
1352 }
1353 #endif /* INET6 */
1354 
1355 /*
1356  * Export TCP internal state information via a struct tcp_info, based on the
1357  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1358  * (TCP state machine, etc).  We export all information using FreeBSD-native
1359  * constants -- for example, the numeric values for tcpi_state will differ
1360  * from Linux.
1361  */
1362 static void
1363 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1364 {
1365 
1366 	INP_WLOCK_ASSERT(tp->t_inpcb);
1367 	bzero(ti, sizeof(*ti));
1368 
1369 	ti->tcpi_state = tp->t_state;
1370 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1371 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1372 	if (tp->t_flags & TF_SACK_PERMIT)
1373 		ti->tcpi_options |= TCPI_OPT_SACK;
1374 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1375 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1376 		ti->tcpi_snd_wscale = tp->snd_scale;
1377 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1378 	}
1379 	if (tp->t_flags & TF_ECN_PERMIT)
1380 		ti->tcpi_options |= TCPI_OPT_ECN;
1381 
1382 	ti->tcpi_rto = tp->t_rxtcur * tick;
1383 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1384 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1385 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1386 
1387 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1388 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1389 
1390 	/*
1391 	 * FreeBSD-specific extension fields for tcp_info.
1392 	 */
1393 	ti->tcpi_rcv_space = tp->rcv_wnd;
1394 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1395 	ti->tcpi_snd_wnd = tp->snd_wnd;
1396 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1397 	ti->tcpi_snd_nxt = tp->snd_nxt;
1398 	ti->tcpi_snd_mss = tp->t_maxseg;
1399 	ti->tcpi_rcv_mss = tp->t_maxseg;
1400 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1401 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1402 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1403 #ifdef TCP_OFFLOAD
1404 	if (tp->t_flags & TF_TOE) {
1405 		ti->tcpi_options |= TCPI_OPT_TOE;
1406 		tcp_offload_tcp_info(tp, ti);
1407 	}
1408 #endif
1409 }
1410 
1411 /*
1412  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1413  * socket option arguments.  When it re-acquires the lock after the copy, it
1414  * has to revalidate that the connection is still valid for the socket
1415  * option.
1416  */
1417 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
1418 	INP_WLOCK(inp);							\
1419 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
1420 		INP_WUNLOCK(inp);					\
1421 		cleanup;						\
1422 		return (ECONNRESET);					\
1423 	}								\
1424 	tp = intotcpcb(inp);						\
1425 } while(0)
1426 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1427 
1428 int
1429 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1430 {
1431 	int	error;
1432 	struct	inpcb *inp;
1433 	struct	tcpcb *tp;
1434 	struct tcp_function_block *blk;
1435 	struct tcp_function_set fsn;
1436 
1437 	error = 0;
1438 	inp = sotoinpcb(so);
1439 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1440 	INP_WLOCK(inp);
1441 	if (sopt->sopt_level != IPPROTO_TCP) {
1442 #ifdef INET6
1443 		if (inp->inp_vflag & INP_IPV6PROTO) {
1444 			INP_WUNLOCK(inp);
1445 			error = ip6_ctloutput(so, sopt);
1446 		}
1447 #endif /* INET6 */
1448 #if defined(INET6) && defined(INET)
1449 		else
1450 #endif
1451 #ifdef INET
1452 		{
1453 			INP_WUNLOCK(inp);
1454 			error = ip_ctloutput(so, sopt);
1455 		}
1456 #endif
1457 		return (error);
1458 	}
1459 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1460 		INP_WUNLOCK(inp);
1461 		return (ECONNRESET);
1462 	}
1463 	tp = intotcpcb(inp);
1464 	/*
1465 	 * Protect the TCP option TCP_FUNCTION_BLK so
1466 	 * that a sub-function can *never* overwrite this.
1467 	 */
1468 	if ((sopt->sopt_dir == SOPT_SET) &&
1469 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1470 		INP_WUNLOCK(inp);
1471 		error = sooptcopyin(sopt, &fsn, sizeof fsn,
1472 		    sizeof fsn);
1473 		if (error)
1474 			return (error);
1475 		INP_WLOCK_RECHECK(inp);
1476 		blk = find_and_ref_tcp_functions(&fsn);
1477 		if (blk == NULL) {
1478 			INP_WUNLOCK(inp);
1479 			return (ENOENT);
1480 		}
1481 		if (tp->t_fb == blk) {
1482 			/* You already have this */
1483 			refcount_release(&blk->tfb_refcnt);
1484 			INP_WUNLOCK(inp);
1485 			return (0);
1486 		}
1487 		if (tp->t_state != TCPS_CLOSED) {
1488 			int error=EINVAL;
1489 			/*
1490 			 * The user has advanced the state
1491 			 * past the initial point, we may not
1492 			 * be able to switch.
1493 			 */
1494 			if (blk->tfb_tcp_handoff_ok != NULL) {
1495 				/*
1496 				 * Does the stack provide a
1497 				 * query mechanism, if so it may
1498 				 * still be possible?
1499 				 */
1500 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1501 			}
1502 			if (error) {
1503 				refcount_release(&blk->tfb_refcnt);
1504 				INP_WUNLOCK(inp);
1505 				return(error);
1506 			}
1507 		}
1508 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1509 			refcount_release(&blk->tfb_refcnt);
1510 			INP_WUNLOCK(inp);
1511 			return (ENOENT);
1512 		}
1513 		/*
1514 		 * Release the old refcnt, the
1515 		 * lookup acquired a ref on the
1516 		 * new one already.
1517 		 */
1518 		if (tp->t_fb->tfb_tcp_fb_fini) {
1519 			/*
1520 			 * Tell the stack to cleanup with 0 i.e.
1521 			 * the tcb is not going away.
1522 			 */
1523 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1524 		}
1525 #ifdef TCPHPTS
1526 		/* Assure that we are not on any hpts */
1527 		tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
1528 #endif
1529 		if (blk->tfb_tcp_fb_init) {
1530 			error = (*blk->tfb_tcp_fb_init)(tp);
1531 			if (error) {
1532 				refcount_release(&blk->tfb_refcnt);
1533 				if (tp->t_fb->tfb_tcp_fb_init) {
1534 					if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0)  {
1535 						/* Fall back failed, drop the connection */
1536 						INP_WUNLOCK(inp);
1537 						soabort(so);
1538 						return(error);
1539 					}
1540 				}
1541 				goto err_out;
1542 			}
1543 		}
1544 		refcount_release(&tp->t_fb->tfb_refcnt);
1545 		tp->t_fb = blk;
1546 #ifdef TCP_OFFLOAD
1547 		if (tp->t_flags & TF_TOE) {
1548 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
1549 			     sopt->sopt_name);
1550 		}
1551 #endif
1552 err_out:
1553 		INP_WUNLOCK(inp);
1554 		return (error);
1555 	} else if ((sopt->sopt_dir == SOPT_GET) &&
1556 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1557 		strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1558 		    TCP_FUNCTION_NAME_LEN_MAX);
1559 		fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1560 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1561 		INP_WUNLOCK(inp);
1562 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
1563 		return (error);
1564 	}
1565 	/* Pass in the INP locked, called must unlock it */
1566 	return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
1567 }
1568 
1569 /*
1570  * If this assert becomes untrue, we need to change the size of the buf
1571  * variable in tcp_default_ctloutput().
1572  */
1573 #ifdef CTASSERT
1574 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1575 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1576 #endif
1577 
1578 int
1579 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
1580 {
1581 	int	error, opt, optval;
1582 	u_int	ui;
1583 	struct	tcp_info ti;
1584 	struct cc_algo *algo;
1585 	char	*pbuf, buf[TCP_LOG_ID_LEN];
1586 	size_t	len;
1587 
1588 	/*
1589 	 * For TCP_CCALGOOPT forward the control to CC module, for both
1590 	 * SOPT_SET and SOPT_GET.
1591 	 */
1592 	switch (sopt->sopt_name) {
1593 	case TCP_CCALGOOPT:
1594 		INP_WUNLOCK(inp);
1595 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1596 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1597 		    sopt->sopt_valsize);
1598 		if (error) {
1599 			free(pbuf, M_TEMP);
1600 			return (error);
1601 		}
1602 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1603 		if (CC_ALGO(tp)->ctl_output != NULL)
1604 			error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1605 		else
1606 			error = ENOENT;
1607 		INP_WUNLOCK(inp);
1608 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
1609 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1610 		free(pbuf, M_TEMP);
1611 		return (error);
1612 	}
1613 
1614 	switch (sopt->sopt_dir) {
1615 	case SOPT_SET:
1616 		switch (sopt->sopt_name) {
1617 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1618 		case TCP_MD5SIG:
1619 			if (!TCPMD5_ENABLED()) {
1620 				INP_WUNLOCK(inp);
1621 				return (ENOPROTOOPT);
1622 			}
1623 			error = TCPMD5_PCBCTL(inp, sopt);
1624 			if (error)
1625 				return (error);
1626 			goto unlock_and_done;
1627 #endif /* IPSEC */
1628 
1629 		case TCP_NODELAY:
1630 		case TCP_NOOPT:
1631 			INP_WUNLOCK(inp);
1632 			error = sooptcopyin(sopt, &optval, sizeof optval,
1633 			    sizeof optval);
1634 			if (error)
1635 				return (error);
1636 
1637 			INP_WLOCK_RECHECK(inp);
1638 			switch (sopt->sopt_name) {
1639 			case TCP_NODELAY:
1640 				opt = TF_NODELAY;
1641 				break;
1642 			case TCP_NOOPT:
1643 				opt = TF_NOOPT;
1644 				break;
1645 			default:
1646 				opt = 0; /* dead code to fool gcc */
1647 				break;
1648 			}
1649 
1650 			if (optval)
1651 				tp->t_flags |= opt;
1652 			else
1653 				tp->t_flags &= ~opt;
1654 unlock_and_done:
1655 #ifdef TCP_OFFLOAD
1656 			if (tp->t_flags & TF_TOE) {
1657 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
1658 				    sopt->sopt_name);
1659 			}
1660 #endif
1661 			INP_WUNLOCK(inp);
1662 			break;
1663 
1664 		case TCP_NOPUSH:
1665 			INP_WUNLOCK(inp);
1666 			error = sooptcopyin(sopt, &optval, sizeof optval,
1667 			    sizeof optval);
1668 			if (error)
1669 				return (error);
1670 
1671 			INP_WLOCK_RECHECK(inp);
1672 			if (optval)
1673 				tp->t_flags |= TF_NOPUSH;
1674 			else if (tp->t_flags & TF_NOPUSH) {
1675 				tp->t_flags &= ~TF_NOPUSH;
1676 				if (TCPS_HAVEESTABLISHED(tp->t_state))
1677 					error = tp->t_fb->tfb_tcp_output(tp);
1678 			}
1679 			goto unlock_and_done;
1680 
1681 		case TCP_MAXSEG:
1682 			INP_WUNLOCK(inp);
1683 			error = sooptcopyin(sopt, &optval, sizeof optval,
1684 			    sizeof optval);
1685 			if (error)
1686 				return (error);
1687 
1688 			INP_WLOCK_RECHECK(inp);
1689 			if (optval > 0 && optval <= tp->t_maxseg &&
1690 			    optval + 40 >= V_tcp_minmss)
1691 				tp->t_maxseg = optval;
1692 			else
1693 				error = EINVAL;
1694 			goto unlock_and_done;
1695 
1696 		case TCP_INFO:
1697 			INP_WUNLOCK(inp);
1698 			error = EINVAL;
1699 			break;
1700 
1701 		case TCP_CONGESTION:
1702 			INP_WUNLOCK(inp);
1703 			error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1704 			if (error)
1705 				break;
1706 			buf[sopt->sopt_valsize] = '\0';
1707 			INP_WLOCK_RECHECK(inp);
1708 			CC_LIST_RLOCK();
1709 			STAILQ_FOREACH(algo, &cc_list, entries)
1710 				if (strncmp(buf, algo->name,
1711 				    TCP_CA_NAME_MAX) == 0)
1712 					break;
1713 			CC_LIST_RUNLOCK();
1714 			if (algo == NULL) {
1715 				INP_WUNLOCK(inp);
1716 				error = EINVAL;
1717 				break;
1718 			}
1719 			/*
1720 			 * We hold a write lock over the tcb so it's safe to
1721 			 * do these things without ordering concerns.
1722 			 */
1723 			if (CC_ALGO(tp)->cb_destroy != NULL)
1724 				CC_ALGO(tp)->cb_destroy(tp->ccv);
1725 			CC_ALGO(tp) = algo;
1726 			/*
1727 			 * If something goes pear shaped initialising the new
1728 			 * algo, fall back to newreno (which does not
1729 			 * require initialisation).
1730 			 */
1731 			if (algo->cb_init != NULL &&
1732 			    algo->cb_init(tp->ccv) != 0) {
1733 				CC_ALGO(tp) = &newreno_cc_algo;
1734 				/*
1735 				 * The only reason init should fail is
1736 				 * because of malloc.
1737 				 */
1738 				error = ENOMEM;
1739 			}
1740 			INP_WUNLOCK(inp);
1741 			break;
1742 
1743 		case TCP_KEEPIDLE:
1744 		case TCP_KEEPINTVL:
1745 		case TCP_KEEPINIT:
1746 			INP_WUNLOCK(inp);
1747 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1748 			if (error)
1749 				return (error);
1750 
1751 			if (ui > (UINT_MAX / hz)) {
1752 				error = EINVAL;
1753 				break;
1754 			}
1755 			ui *= hz;
1756 
1757 			INP_WLOCK_RECHECK(inp);
1758 			switch (sopt->sopt_name) {
1759 			case TCP_KEEPIDLE:
1760 				tp->t_keepidle = ui;
1761 				/*
1762 				 * XXX: better check current remaining
1763 				 * timeout and "merge" it with new value.
1764 				 */
1765 				if ((tp->t_state > TCPS_LISTEN) &&
1766 				    (tp->t_state <= TCPS_CLOSING))
1767 					tcp_timer_activate(tp, TT_KEEP,
1768 					    TP_KEEPIDLE(tp));
1769 				break;
1770 			case TCP_KEEPINTVL:
1771 				tp->t_keepintvl = ui;
1772 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1773 				    (TP_MAXIDLE(tp) > 0))
1774 					tcp_timer_activate(tp, TT_2MSL,
1775 					    TP_MAXIDLE(tp));
1776 				break;
1777 			case TCP_KEEPINIT:
1778 				tp->t_keepinit = ui;
1779 				if (tp->t_state == TCPS_SYN_RECEIVED ||
1780 				    tp->t_state == TCPS_SYN_SENT)
1781 					tcp_timer_activate(tp, TT_KEEP,
1782 					    TP_KEEPINIT(tp));
1783 				break;
1784 			}
1785 			goto unlock_and_done;
1786 
1787 		case TCP_KEEPCNT:
1788 			INP_WUNLOCK(inp);
1789 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1790 			if (error)
1791 				return (error);
1792 
1793 			INP_WLOCK_RECHECK(inp);
1794 			tp->t_keepcnt = ui;
1795 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1796 			    (TP_MAXIDLE(tp) > 0))
1797 				tcp_timer_activate(tp, TT_2MSL,
1798 				    TP_MAXIDLE(tp));
1799 			goto unlock_and_done;
1800 
1801 #ifdef TCPPCAP
1802 		case TCP_PCAP_OUT:
1803 		case TCP_PCAP_IN:
1804 			INP_WUNLOCK(inp);
1805 			error = sooptcopyin(sopt, &optval, sizeof optval,
1806 			    sizeof optval);
1807 			if (error)
1808 				return (error);
1809 
1810 			INP_WLOCK_RECHECK(inp);
1811 			if (optval >= 0)
1812 				tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
1813 					&(tp->t_outpkts) : &(tp->t_inpkts),
1814 					optval);
1815 			else
1816 				error = EINVAL;
1817 			goto unlock_and_done;
1818 #endif
1819 
1820 		case TCP_FASTOPEN: {
1821 			struct tcp_fastopen tfo_optval;
1822 
1823 			INP_WUNLOCK(inp);
1824 			if (!V_tcp_fastopen_client_enable &&
1825 			    !V_tcp_fastopen_server_enable)
1826 				return (EPERM);
1827 
1828 			error = sooptcopyin(sopt, &tfo_optval,
1829 				    sizeof(tfo_optval), sizeof(int));
1830 			if (error)
1831 				return (error);
1832 
1833 			INP_WLOCK_RECHECK(inp);
1834 			if (tfo_optval.enable) {
1835 				if (tp->t_state == TCPS_LISTEN) {
1836 					if (!V_tcp_fastopen_server_enable) {
1837 						error = EPERM;
1838 						goto unlock_and_done;
1839 					}
1840 
1841 					tp->t_flags |= TF_FASTOPEN;
1842 					if (tp->t_tfo_pending == NULL)
1843 						tp->t_tfo_pending =
1844 						    tcp_fastopen_alloc_counter();
1845 				} else {
1846 					/*
1847 					 * If a pre-shared key was provided,
1848 					 * stash it in the client cookie
1849 					 * field of the tcpcb for use during
1850 					 * connect.
1851 					 */
1852 					if (sopt->sopt_valsize ==
1853 					    sizeof(tfo_optval)) {
1854 						memcpy(tp->t_tfo_cookie.client,
1855 						       tfo_optval.psk,
1856 						       TCP_FASTOPEN_PSK_LEN);
1857 						tp->t_tfo_client_cookie_len =
1858 						    TCP_FASTOPEN_PSK_LEN;
1859 					}
1860 					tp->t_flags |= TF_FASTOPEN;
1861 				}
1862 			} else
1863 				tp->t_flags &= ~TF_FASTOPEN;
1864 			goto unlock_and_done;
1865 		}
1866 
1867 #ifdef TCP_BLACKBOX
1868 		case TCP_LOG:
1869 			INP_WUNLOCK(inp);
1870 			error = sooptcopyin(sopt, &optval, sizeof optval,
1871 			    sizeof optval);
1872 			if (error)
1873 				return (error);
1874 
1875 			INP_WLOCK_RECHECK(inp);
1876 			error = tcp_log_state_change(tp, optval);
1877 			goto unlock_and_done;
1878 
1879 		case TCP_LOGBUF:
1880 			INP_WUNLOCK(inp);
1881 			error = EINVAL;
1882 			break;
1883 
1884 		case TCP_LOGID:
1885 			INP_WUNLOCK(inp);
1886 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
1887 			if (error)
1888 				break;
1889 			buf[sopt->sopt_valsize] = '\0';
1890 			INP_WLOCK_RECHECK(inp);
1891 			error = tcp_log_set_id(tp, buf);
1892 			/* tcp_log_set_id() unlocks the INP. */
1893 			break;
1894 
1895 		case TCP_LOGDUMP:
1896 		case TCP_LOGDUMPID:
1897 			INP_WUNLOCK(inp);
1898 			error =
1899 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
1900 			if (error)
1901 				break;
1902 			buf[sopt->sopt_valsize] = '\0';
1903 			INP_WLOCK_RECHECK(inp);
1904 			if (sopt->sopt_name == TCP_LOGDUMP) {
1905 				error = tcp_log_dump_tp_logbuf(tp, buf,
1906 				    M_WAITOK, true);
1907 				INP_WUNLOCK(inp);
1908 			} else {
1909 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
1910 				/*
1911 				 * tcp_log_dump_tp_bucket_logbufs() drops the
1912 				 * INP lock.
1913 				 */
1914 			}
1915 			break;
1916 #endif
1917 
1918 		default:
1919 			INP_WUNLOCK(inp);
1920 			error = ENOPROTOOPT;
1921 			break;
1922 		}
1923 		break;
1924 
1925 	case SOPT_GET:
1926 		tp = intotcpcb(inp);
1927 		switch (sopt->sopt_name) {
1928 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1929 		case TCP_MD5SIG:
1930 			if (!TCPMD5_ENABLED()) {
1931 				INP_WUNLOCK(inp);
1932 				return (ENOPROTOOPT);
1933 			}
1934 			error = TCPMD5_PCBCTL(inp, sopt);
1935 			break;
1936 #endif
1937 
1938 		case TCP_NODELAY:
1939 			optval = tp->t_flags & TF_NODELAY;
1940 			INP_WUNLOCK(inp);
1941 			error = sooptcopyout(sopt, &optval, sizeof optval);
1942 			break;
1943 		case TCP_MAXSEG:
1944 			optval = tp->t_maxseg;
1945 			INP_WUNLOCK(inp);
1946 			error = sooptcopyout(sopt, &optval, sizeof optval);
1947 			break;
1948 		case TCP_NOOPT:
1949 			optval = tp->t_flags & TF_NOOPT;
1950 			INP_WUNLOCK(inp);
1951 			error = sooptcopyout(sopt, &optval, sizeof optval);
1952 			break;
1953 		case TCP_NOPUSH:
1954 			optval = tp->t_flags & TF_NOPUSH;
1955 			INP_WUNLOCK(inp);
1956 			error = sooptcopyout(sopt, &optval, sizeof optval);
1957 			break;
1958 		case TCP_INFO:
1959 			tcp_fill_info(tp, &ti);
1960 			INP_WUNLOCK(inp);
1961 			error = sooptcopyout(sopt, &ti, sizeof ti);
1962 			break;
1963 		case TCP_CONGESTION:
1964 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1965 			INP_WUNLOCK(inp);
1966 			error = sooptcopyout(sopt, buf, len + 1);
1967 			break;
1968 		case TCP_KEEPIDLE:
1969 		case TCP_KEEPINTVL:
1970 		case TCP_KEEPINIT:
1971 		case TCP_KEEPCNT:
1972 			switch (sopt->sopt_name) {
1973 			case TCP_KEEPIDLE:
1974 				ui = TP_KEEPIDLE(tp) / hz;
1975 				break;
1976 			case TCP_KEEPINTVL:
1977 				ui = TP_KEEPINTVL(tp) / hz;
1978 				break;
1979 			case TCP_KEEPINIT:
1980 				ui = TP_KEEPINIT(tp) / hz;
1981 				break;
1982 			case TCP_KEEPCNT:
1983 				ui = TP_KEEPCNT(tp);
1984 				break;
1985 			}
1986 			INP_WUNLOCK(inp);
1987 			error = sooptcopyout(sopt, &ui, sizeof(ui));
1988 			break;
1989 #ifdef TCPPCAP
1990 		case TCP_PCAP_OUT:
1991 		case TCP_PCAP_IN:
1992 			optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
1993 					&(tp->t_outpkts) : &(tp->t_inpkts));
1994 			INP_WUNLOCK(inp);
1995 			error = sooptcopyout(sopt, &optval, sizeof optval);
1996 			break;
1997 #endif
1998 		case TCP_FASTOPEN:
1999 			optval = tp->t_flags & TF_FASTOPEN;
2000 			INP_WUNLOCK(inp);
2001 			error = sooptcopyout(sopt, &optval, sizeof optval);
2002 			break;
2003 #ifdef TCP_BLACKBOX
2004 		case TCP_LOG:
2005 			optval = tp->t_logstate;
2006 			INP_WUNLOCK(inp);
2007 			error = sooptcopyout(sopt, &optval, sizeof(optval));
2008 			break;
2009 		case TCP_LOGBUF:
2010 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2011 			error = tcp_log_getlogbuf(sopt, tp);
2012 			break;
2013 		case TCP_LOGID:
2014 			len = tcp_log_get_id(tp, buf);
2015 			INP_WUNLOCK(inp);
2016 			error = sooptcopyout(sopt, buf, len + 1);
2017 			break;
2018 		case TCP_LOGDUMP:
2019 		case TCP_LOGDUMPID:
2020 			INP_WUNLOCK(inp);
2021 			error = EINVAL;
2022 			break;
2023 #endif
2024 		default:
2025 			INP_WUNLOCK(inp);
2026 			error = ENOPROTOOPT;
2027 			break;
2028 		}
2029 		break;
2030 	}
2031 	return (error);
2032 }
2033 #undef INP_WLOCK_RECHECK
2034 #undef INP_WLOCK_RECHECK_CLEANUP
2035 
2036 /*
2037  * Attach TCP protocol to socket, allocating
2038  * internet protocol control block, tcp control block,
2039  * bufer space, and entering LISTEN state if to accept connections.
2040  */
2041 static int
2042 tcp_attach(struct socket *so)
2043 {
2044 	struct tcpcb *tp;
2045 	struct inpcb *inp;
2046 	int error;
2047 
2048 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
2049 		error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
2050 		if (error)
2051 			return (error);
2052 	}
2053 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
2054 	so->so_snd.sb_flags |= SB_AUTOSIZE;
2055 	INP_INFO_RLOCK(&V_tcbinfo);
2056 	error = in_pcballoc(so, &V_tcbinfo);
2057 	if (error) {
2058 		INP_INFO_RUNLOCK(&V_tcbinfo);
2059 		return (error);
2060 	}
2061 	inp = sotoinpcb(so);
2062 #ifdef INET6
2063 	if (inp->inp_vflag & INP_IPV6PROTO) {
2064 		inp->inp_vflag |= INP_IPV6;
2065 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
2066 			inp->inp_vflag |= INP_IPV4;
2067 		inp->in6p_hops = -1;	/* use kernel default */
2068 	}
2069 	else
2070 #endif
2071 	inp->inp_vflag |= INP_IPV4;
2072 	tp = tcp_newtcpcb(inp);
2073 	if (tp == NULL) {
2074 		in_pcbdetach(inp);
2075 		in_pcbfree(inp);
2076 		INP_INFO_RUNLOCK(&V_tcbinfo);
2077 		return (ENOBUFS);
2078 	}
2079 	tp->t_state = TCPS_CLOSED;
2080 	INP_WUNLOCK(inp);
2081 	INP_INFO_RUNLOCK(&V_tcbinfo);
2082 	TCPSTATES_INC(TCPS_CLOSED);
2083 	return (0);
2084 }
2085 
2086 /*
2087  * Initiate (or continue) disconnect.
2088  * If embryonic state, just send reset (once).
2089  * If in ``let data drain'' option and linger null, just drop.
2090  * Otherwise (hard), mark socket disconnecting and drop
2091  * current input data; switch states based on user close, and
2092  * send segment to peer (with FIN).
2093  */
2094 static void
2095 tcp_disconnect(struct tcpcb *tp)
2096 {
2097 	struct inpcb *inp = tp->t_inpcb;
2098 	struct socket *so = inp->inp_socket;
2099 
2100 	INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2101 	INP_WLOCK_ASSERT(inp);
2102 
2103 	/*
2104 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2105 	 * socket is still open.
2106 	 */
2107 	if (tp->t_state < TCPS_ESTABLISHED) {
2108 		tp = tcp_close(tp);
2109 		KASSERT(tp != NULL,
2110 		    ("tcp_disconnect: tcp_close() returned NULL"));
2111 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2112 		tp = tcp_drop(tp, 0);
2113 		KASSERT(tp != NULL,
2114 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2115 	} else {
2116 		soisdisconnecting(so);
2117 		sbflush(&so->so_rcv);
2118 		tcp_usrclosed(tp);
2119 		if (!(inp->inp_flags & INP_DROPPED))
2120 			tp->t_fb->tfb_tcp_output(tp);
2121 	}
2122 }
2123 
2124 /*
2125  * User issued close, and wish to trail through shutdown states:
2126  * if never received SYN, just forget it.  If got a SYN from peer,
2127  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2128  * If already got a FIN from peer, then almost done; go to LAST_ACK
2129  * state.  In all other cases, have already sent FIN to peer (e.g.
2130  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2131  * for peer to send FIN or not respond to keep-alives, etc.
2132  * We can let the user exit from the close as soon as the FIN is acked.
2133  */
2134 static void
2135 tcp_usrclosed(struct tcpcb *tp)
2136 {
2137 
2138 	INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2139 	INP_WLOCK_ASSERT(tp->t_inpcb);
2140 
2141 	switch (tp->t_state) {
2142 	case TCPS_LISTEN:
2143 #ifdef TCP_OFFLOAD
2144 		tcp_offload_listen_stop(tp);
2145 #endif
2146 		tcp_state_change(tp, TCPS_CLOSED);
2147 		/* FALLTHROUGH */
2148 	case TCPS_CLOSED:
2149 		tp = tcp_close(tp);
2150 		/*
2151 		 * tcp_close() should never return NULL here as the socket is
2152 		 * still open.
2153 		 */
2154 		KASSERT(tp != NULL,
2155 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2156 		break;
2157 
2158 	case TCPS_SYN_SENT:
2159 	case TCPS_SYN_RECEIVED:
2160 		tp->t_flags |= TF_NEEDFIN;
2161 		break;
2162 
2163 	case TCPS_ESTABLISHED:
2164 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2165 		break;
2166 
2167 	case TCPS_CLOSE_WAIT:
2168 		tcp_state_change(tp, TCPS_LAST_ACK);
2169 		break;
2170 	}
2171 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
2172 		soisdisconnected(tp->t_inpcb->inp_socket);
2173 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
2174 		if (tp->t_state == TCPS_FIN_WAIT_2) {
2175 			int timeout;
2176 
2177 			timeout = (tcp_fast_finwait2_recycle) ?
2178 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2179 			tcp_timer_activate(tp, TT_2MSL, timeout);
2180 		}
2181 	}
2182 }
2183 
2184 #ifdef DDB
2185 static void
2186 db_print_indent(int indent)
2187 {
2188 	int i;
2189 
2190 	for (i = 0; i < indent; i++)
2191 		db_printf(" ");
2192 }
2193 
2194 static void
2195 db_print_tstate(int t_state)
2196 {
2197 
2198 	switch (t_state) {
2199 	case TCPS_CLOSED:
2200 		db_printf("TCPS_CLOSED");
2201 		return;
2202 
2203 	case TCPS_LISTEN:
2204 		db_printf("TCPS_LISTEN");
2205 		return;
2206 
2207 	case TCPS_SYN_SENT:
2208 		db_printf("TCPS_SYN_SENT");
2209 		return;
2210 
2211 	case TCPS_SYN_RECEIVED:
2212 		db_printf("TCPS_SYN_RECEIVED");
2213 		return;
2214 
2215 	case TCPS_ESTABLISHED:
2216 		db_printf("TCPS_ESTABLISHED");
2217 		return;
2218 
2219 	case TCPS_CLOSE_WAIT:
2220 		db_printf("TCPS_CLOSE_WAIT");
2221 		return;
2222 
2223 	case TCPS_FIN_WAIT_1:
2224 		db_printf("TCPS_FIN_WAIT_1");
2225 		return;
2226 
2227 	case TCPS_CLOSING:
2228 		db_printf("TCPS_CLOSING");
2229 		return;
2230 
2231 	case TCPS_LAST_ACK:
2232 		db_printf("TCPS_LAST_ACK");
2233 		return;
2234 
2235 	case TCPS_FIN_WAIT_2:
2236 		db_printf("TCPS_FIN_WAIT_2");
2237 		return;
2238 
2239 	case TCPS_TIME_WAIT:
2240 		db_printf("TCPS_TIME_WAIT");
2241 		return;
2242 
2243 	default:
2244 		db_printf("unknown");
2245 		return;
2246 	}
2247 }
2248 
2249 static void
2250 db_print_tflags(u_int t_flags)
2251 {
2252 	int comma;
2253 
2254 	comma = 0;
2255 	if (t_flags & TF_ACKNOW) {
2256 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2257 		comma = 1;
2258 	}
2259 	if (t_flags & TF_DELACK) {
2260 		db_printf("%sTF_DELACK", comma ? ", " : "");
2261 		comma = 1;
2262 	}
2263 	if (t_flags & TF_NODELAY) {
2264 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2265 		comma = 1;
2266 	}
2267 	if (t_flags & TF_NOOPT) {
2268 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2269 		comma = 1;
2270 	}
2271 	if (t_flags & TF_SENTFIN) {
2272 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2273 		comma = 1;
2274 	}
2275 	if (t_flags & TF_REQ_SCALE) {
2276 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2277 		comma = 1;
2278 	}
2279 	if (t_flags & TF_RCVD_SCALE) {
2280 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2281 		comma = 1;
2282 	}
2283 	if (t_flags & TF_REQ_TSTMP) {
2284 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2285 		comma = 1;
2286 	}
2287 	if (t_flags & TF_RCVD_TSTMP) {
2288 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2289 		comma = 1;
2290 	}
2291 	if (t_flags & TF_SACK_PERMIT) {
2292 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2293 		comma = 1;
2294 	}
2295 	if (t_flags & TF_NEEDSYN) {
2296 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2297 		comma = 1;
2298 	}
2299 	if (t_flags & TF_NEEDFIN) {
2300 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2301 		comma = 1;
2302 	}
2303 	if (t_flags & TF_NOPUSH) {
2304 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2305 		comma = 1;
2306 	}
2307 	if (t_flags & TF_MORETOCOME) {
2308 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2309 		comma = 1;
2310 	}
2311 	if (t_flags & TF_LQ_OVERFLOW) {
2312 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2313 		comma = 1;
2314 	}
2315 	if (t_flags & TF_LASTIDLE) {
2316 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2317 		comma = 1;
2318 	}
2319 	if (t_flags & TF_RXWIN0SENT) {
2320 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2321 		comma = 1;
2322 	}
2323 	if (t_flags & TF_FASTRECOVERY) {
2324 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2325 		comma = 1;
2326 	}
2327 	if (t_flags & TF_CONGRECOVERY) {
2328 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2329 		comma = 1;
2330 	}
2331 	if (t_flags & TF_WASFRECOVERY) {
2332 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2333 		comma = 1;
2334 	}
2335 	if (t_flags & TF_SIGNATURE) {
2336 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2337 		comma = 1;
2338 	}
2339 	if (t_flags & TF_FORCEDATA) {
2340 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2341 		comma = 1;
2342 	}
2343 	if (t_flags & TF_TSO) {
2344 		db_printf("%sTF_TSO", comma ? ", " : "");
2345 		comma = 1;
2346 	}
2347 	if (t_flags & TF_ECN_PERMIT) {
2348 		db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2349 		comma = 1;
2350 	}
2351 	if (t_flags & TF_FASTOPEN) {
2352 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2353 		comma = 1;
2354 	}
2355 }
2356 
2357 static void
2358 db_print_toobflags(char t_oobflags)
2359 {
2360 	int comma;
2361 
2362 	comma = 0;
2363 	if (t_oobflags & TCPOOB_HAVEDATA) {
2364 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2365 		comma = 1;
2366 	}
2367 	if (t_oobflags & TCPOOB_HADDATA) {
2368 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2369 		comma = 1;
2370 	}
2371 }
2372 
2373 static void
2374 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2375 {
2376 
2377 	db_print_indent(indent);
2378 	db_printf("%s at %p\n", name, tp);
2379 
2380 	indent += 2;
2381 
2382 	db_print_indent(indent);
2383 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
2384 	   LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2385 
2386 	db_print_indent(indent);
2387 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
2388 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2389 
2390 	db_print_indent(indent);
2391 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2392 	    &tp->t_timers->tt_delack, tp->t_inpcb);
2393 
2394 	db_print_indent(indent);
2395 	db_printf("t_state: %d (", tp->t_state);
2396 	db_print_tstate(tp->t_state);
2397 	db_printf(")\n");
2398 
2399 	db_print_indent(indent);
2400 	db_printf("t_flags: 0x%x (", tp->t_flags);
2401 	db_print_tflags(tp->t_flags);
2402 	db_printf(")\n");
2403 
2404 	db_print_indent(indent);
2405 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
2406 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
2407 
2408 	db_print_indent(indent);
2409 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
2410 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2411 
2412 	db_print_indent(indent);
2413 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
2414 	    tp->iss, tp->irs, tp->rcv_nxt);
2415 
2416 	db_print_indent(indent);
2417 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
2418 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2419 
2420 	db_print_indent(indent);
2421 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
2422 	   tp->snd_wnd, tp->snd_cwnd);
2423 
2424 	db_print_indent(indent);
2425 	db_printf("snd_ssthresh: %u   snd_recover: "
2426 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2427 
2428 	db_print_indent(indent);
2429 	db_printf("t_rcvtime: %u   t_startime: %u\n",
2430 	    tp->t_rcvtime, tp->t_starttime);
2431 
2432 	db_print_indent(indent);
2433 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
2434 	    tp->t_rtttime, tp->t_rtseq);
2435 
2436 	db_print_indent(indent);
2437 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
2438 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2439 
2440 	db_print_indent(indent);
2441 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
2442 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2443 	    tp->t_rttbest);
2444 
2445 	db_print_indent(indent);
2446 	db_printf("t_rttupdated: %lu   max_sndwnd: %u   t_softerror: %d\n",
2447 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2448 
2449 	db_print_indent(indent);
2450 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2451 	db_print_toobflags(tp->t_oobflags);
2452 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
2453 
2454 	db_print_indent(indent);
2455 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
2456 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2457 
2458 	db_print_indent(indent);
2459 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
2460 	    tp->ts_recent, tp->ts_recent_age);
2461 
2462 	db_print_indent(indent);
2463 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
2464 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2465 
2466 	db_print_indent(indent);
2467 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
2468 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2469 	    tp->snd_recover_prev, tp->t_badrxtwin);
2470 
2471 	db_print_indent(indent);
2472 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
2473 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2474 
2475 	db_print_indent(indent);
2476 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
2477 	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2478 
2479 	/* Skip sackblks, sackhint. */
2480 
2481 	db_print_indent(indent);
2482 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
2483 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2484 }
2485 
2486 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2487 {
2488 	struct tcpcb *tp;
2489 
2490 	if (!have_addr) {
2491 		db_printf("usage: show tcpcb <addr>\n");
2492 		return;
2493 	}
2494 	tp = (struct tcpcb *)addr;
2495 
2496 	db_print_tcpcb(tp, "tcpcb", 0);
2497 }
2498 #endif
2499