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