xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 6356dba0b403daa023dec24559ab1f8e602e4f14)
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1993
3  *	The Regents of the University of California.
4  * Copyright (c) 2006-2007 Robert N. M. Watson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 4. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include "opt_ddb.h"
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40 #include "opt_tcpdebug.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 #include <sys/mbuf.h>
48 #ifdef INET6
49 #include <sys/domain.h>
50 #endif /* INET6 */
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/protosw.h>
54 #include <sys/proc.h>
55 #include <sys/jail.h>
56 #include <sys/vimage.h>
57 
58 #ifdef DDB
59 #include <ddb/ddb.h>
60 #endif
61 
62 #include <net/if.h>
63 #include <net/route.h>
64 
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #ifdef INET6
68 #include <netinet/ip6.h>
69 #endif
70 #include <netinet/in_pcb.h>
71 #ifdef INET6
72 #include <netinet6/in6_pcb.h>
73 #endif
74 #include <netinet/in_var.h>
75 #include <netinet/ip_var.h>
76 #ifdef INET6
77 #include <netinet6/ip6_var.h>
78 #include <netinet6/scope6_var.h>
79 #endif
80 #include <netinet/tcp.h>
81 #include <netinet/tcp_fsm.h>
82 #include <netinet/tcp_seq.h>
83 #include <netinet/tcp_timer.h>
84 #include <netinet/tcp_var.h>
85 #include <netinet/tcpip.h>
86 #ifdef TCPDEBUG
87 #include <netinet/tcp_debug.h>
88 #endif
89 #include <netinet/tcp_offload.h>
90 
91 /*
92  * TCP protocol interface to socket abstraction.
93  */
94 static int	tcp_attach(struct socket *);
95 static int	tcp_connect(struct tcpcb *, struct sockaddr *,
96 		    struct thread *td);
97 #ifdef INET6
98 static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
99 		    struct thread *td);
100 #endif /* INET6 */
101 static void	tcp_disconnect(struct tcpcb *);
102 static void	tcp_usrclosed(struct tcpcb *);
103 static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
104 
105 #ifdef TCPDEBUG
106 #define	TCPDEBUG0	int ostate = 0
107 #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
108 #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
109 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
110 #else
111 #define	TCPDEBUG0
112 #define	TCPDEBUG1()
113 #define	TCPDEBUG2(req)
114 #endif
115 
116 /*
117  * TCP attaches to socket via pru_attach(), reserving space,
118  * and an internet control block.
119  */
120 static int
121 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
122 {
123 	struct inpcb *inp;
124 	struct tcpcb *tp = NULL;
125 	int error;
126 	TCPDEBUG0;
127 
128 	inp = sotoinpcb(so);
129 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
130 	TCPDEBUG1();
131 
132 	error = tcp_attach(so);
133 	if (error)
134 		goto out;
135 
136 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
137 		so->so_linger = TCP_LINGERTIME;
138 
139 	inp = sotoinpcb(so);
140 	tp = intotcpcb(inp);
141 out:
142 	TCPDEBUG2(PRU_ATTACH);
143 	return error;
144 }
145 
146 /*
147  * tcp_detach is called when the socket layer loses its final reference
148  * to the socket, be it a file descriptor reference, a reference from TCP,
149  * etc.  At this point, there is only one case in which we will keep around
150  * inpcb state: time wait.
151  *
152  * This function can probably be re-absorbed back into tcp_usr_detach() now
153  * that there is a single detach path.
154  */
155 static void
156 tcp_detach(struct socket *so, struct inpcb *inp)
157 {
158 	struct tcpcb *tp;
159 #ifdef INET6
160 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
161 #endif
162 
163 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
164 	INP_WLOCK_ASSERT(inp);
165 
166 	KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
167 	KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
168 
169 	tp = intotcpcb(inp);
170 
171 	if (inp->inp_vflag & INP_TIMEWAIT) {
172 		/*
173 		 * There are two cases to handle: one in which the time wait
174 		 * state is being discarded (INP_DROPPED), and one in which
175 		 * this connection will remain in timewait.  In the former,
176 		 * it is time to discard all state (except tcptw, which has
177 		 * already been discarded by the timewait close code, which
178 		 * should be further up the call stack somewhere).  In the
179 		 * latter case, we detach from the socket, but leave the pcb
180 		 * present until timewait ends.
181 		 *
182 		 * XXXRW: Would it be cleaner to free the tcptw here?
183 		 */
184 		if (inp->inp_vflag & INP_DROPPED) {
185 			KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
186 			    "INP_DROPPED && tp != NULL"));
187 #ifdef INET6
188 			if (isipv6) {
189 				in6_pcbdetach(inp);
190 				in6_pcbfree(inp);
191 			} else {
192 #endif
193 				in_pcbdetach(inp);
194 				in_pcbfree(inp);
195 #ifdef INET6
196 			}
197 #endif
198 		} else {
199 #ifdef INET6
200 			if (isipv6)
201 				in6_pcbdetach(inp);
202 			else
203 #endif
204 				in_pcbdetach(inp);
205 			INP_WUNLOCK(inp);
206 		}
207 	} else {
208 		/*
209 		 * If the connection is not in timewait, we consider two
210 		 * two conditions: one in which no further processing is
211 		 * necessary (dropped || embryonic), and one in which TCP is
212 		 * not yet done, but no longer requires the socket, so the
213 		 * pcb will persist for the time being.
214 		 *
215 		 * XXXRW: Does the second case still occur?
216 		 */
217 		if (inp->inp_vflag & INP_DROPPED ||
218 		    tp->t_state < TCPS_SYN_SENT) {
219 			tcp_discardcb(tp);
220 #ifdef INET6
221 			if (isipv6) {
222 				in6_pcbdetach(inp);
223 				in6_pcbfree(inp);
224 			} else {
225 #endif
226 				in_pcbdetach(inp);
227 				in_pcbfree(inp);
228 #ifdef INET6
229 			}
230 #endif
231 		} else {
232 #ifdef INET6
233 			if (isipv6)
234 				in6_pcbdetach(inp);
235 			else
236 #endif
237 				in_pcbdetach(inp);
238 		}
239 	}
240 }
241 
242 /*
243  * pru_detach() detaches the TCP protocol from the socket.
244  * If the protocol state is non-embryonic, then can't
245  * do this directly: have to initiate a pru_disconnect(),
246  * which may finish later; embryonic TCB's can just
247  * be discarded here.
248  */
249 static void
250 tcp_usr_detach(struct socket *so)
251 {
252 	struct inpcb *inp;
253 
254 	inp = sotoinpcb(so);
255 	KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
256 	INP_INFO_WLOCK(&V_tcbinfo);
257 	INP_WLOCK(inp);
258 	KASSERT(inp->inp_socket != NULL,
259 	    ("tcp_usr_detach: inp_socket == NULL"));
260 	tcp_detach(so, inp);
261 	INP_INFO_WUNLOCK(&V_tcbinfo);
262 }
263 
264 /*
265  * Give the socket an address.
266  */
267 static int
268 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
269 {
270 	int error = 0;
271 	struct inpcb *inp;
272 	struct tcpcb *tp = NULL;
273 	struct sockaddr_in *sinp;
274 
275 	sinp = (struct sockaddr_in *)nam;
276 	if (nam->sa_len != sizeof (*sinp))
277 		return (EINVAL);
278 	/*
279 	 * Must check for multicast addresses and disallow binding
280 	 * to them.
281 	 */
282 	if (sinp->sin_family == AF_INET &&
283 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
284 		return (EAFNOSUPPORT);
285 
286 	TCPDEBUG0;
287 	INP_INFO_WLOCK(&V_tcbinfo);
288 	inp = sotoinpcb(so);
289 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
290 	INP_WLOCK(inp);
291 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
292 		error = EINVAL;
293 		goto out;
294 	}
295 	tp = intotcpcb(inp);
296 	TCPDEBUG1();
297 	error = in_pcbbind(inp, nam, td->td_ucred);
298 out:
299 	TCPDEBUG2(PRU_BIND);
300 	INP_WUNLOCK(inp);
301 	INP_INFO_WUNLOCK(&V_tcbinfo);
302 
303 	return (error);
304 }
305 
306 #ifdef INET6
307 static int
308 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
309 {
310 	int error = 0;
311 	struct inpcb *inp;
312 	struct tcpcb *tp = NULL;
313 	struct sockaddr_in6 *sin6p;
314 
315 	sin6p = (struct sockaddr_in6 *)nam;
316 	if (nam->sa_len != sizeof (*sin6p))
317 		return (EINVAL);
318 	/*
319 	 * Must check for multicast addresses and disallow binding
320 	 * to them.
321 	 */
322 	if (sin6p->sin6_family == AF_INET6 &&
323 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
324 		return (EAFNOSUPPORT);
325 
326 	TCPDEBUG0;
327 	INP_INFO_WLOCK(&V_tcbinfo);
328 	inp = sotoinpcb(so);
329 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
330 	INP_WLOCK(inp);
331 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
332 		error = EINVAL;
333 		goto out;
334 	}
335 	tp = intotcpcb(inp);
336 	TCPDEBUG1();
337 	inp->inp_vflag &= ~INP_IPV4;
338 	inp->inp_vflag |= INP_IPV6;
339 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
340 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
341 			inp->inp_vflag |= INP_IPV4;
342 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
343 			struct sockaddr_in sin;
344 
345 			in6_sin6_2_sin(&sin, sin6p);
346 			inp->inp_vflag |= INP_IPV4;
347 			inp->inp_vflag &= ~INP_IPV6;
348 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
349 			    td->td_ucred);
350 			goto out;
351 		}
352 	}
353 	error = in6_pcbbind(inp, nam, td->td_ucred);
354 out:
355 	TCPDEBUG2(PRU_BIND);
356 	INP_WUNLOCK(inp);
357 	INP_INFO_WUNLOCK(&V_tcbinfo);
358 	return (error);
359 }
360 #endif /* INET6 */
361 
362 /*
363  * Prepare to accept connections.
364  */
365 static int
366 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
367 {
368 	int error = 0;
369 	struct inpcb *inp;
370 	struct tcpcb *tp = NULL;
371 
372 	TCPDEBUG0;
373 	INP_INFO_WLOCK(&V_tcbinfo);
374 	inp = sotoinpcb(so);
375 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
376 	INP_WLOCK(inp);
377 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
378 		error = EINVAL;
379 		goto out;
380 	}
381 	tp = intotcpcb(inp);
382 	TCPDEBUG1();
383 	SOCK_LOCK(so);
384 	error = solisten_proto_check(so);
385 	if (error == 0 && inp->inp_lport == 0)
386 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
387 	if (error == 0) {
388 		tp->t_state = TCPS_LISTEN;
389 		solisten_proto(so, backlog);
390 		tcp_offload_listen_open(tp);
391 	}
392 	SOCK_UNLOCK(so);
393 
394 out:
395 	TCPDEBUG2(PRU_LISTEN);
396 	INP_WUNLOCK(inp);
397 	INP_INFO_WUNLOCK(&V_tcbinfo);
398 	return (error);
399 }
400 
401 #ifdef INET6
402 static int
403 tcp6_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_INFO_WLOCK(&V_tcbinfo);
411 	inp = sotoinpcb(so);
412 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
413 	INP_WLOCK(inp);
414 	if (inp->inp_vflag & (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 	if (error == 0 && inp->inp_lport == 0) {
423 		inp->inp_vflag &= ~INP_IPV4;
424 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
425 			inp->inp_vflag |= INP_IPV4;
426 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
427 	}
428 	if (error == 0) {
429 		tp->t_state = TCPS_LISTEN;
430 		solisten_proto(so, backlog);
431 	}
432 	SOCK_UNLOCK(so);
433 
434 out:
435 	TCPDEBUG2(PRU_LISTEN);
436 	INP_WUNLOCK(inp);
437 	INP_INFO_WUNLOCK(&V_tcbinfo);
438 	return (error);
439 }
440 #endif /* INET6 */
441 
442 /*
443  * Initiate connection to peer.
444  * Create a template for use in transmissions on this connection.
445  * Enter SYN_SENT state, and mark socket as connecting.
446  * Start keep-alive timer, and seed output sequence space.
447  * Send initial segment on connection.
448  */
449 static int
450 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
451 {
452 	int error = 0;
453 	struct inpcb *inp;
454 	struct tcpcb *tp = NULL;
455 	struct sockaddr_in *sinp;
456 
457 	sinp = (struct sockaddr_in *)nam;
458 	if (nam->sa_len != sizeof (*sinp))
459 		return (EINVAL);
460 	/*
461 	 * Must disallow TCP ``connections'' to multicast addresses.
462 	 */
463 	if (sinp->sin_family == AF_INET
464 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
465 		return (EAFNOSUPPORT);
466 	if (jailed(td->td_ucred))
467 		prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
468 
469 	TCPDEBUG0;
470 	INP_INFO_WLOCK(&V_tcbinfo);
471 	inp = sotoinpcb(so);
472 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
473 	INP_WLOCK(inp);
474 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
475 		error = EINVAL;
476 		goto out;
477 	}
478 	tp = intotcpcb(inp);
479 	TCPDEBUG1();
480 	if ((error = tcp_connect(tp, nam, td)) != 0)
481 		goto out;
482 	error = tcp_output_connect(so, nam);
483 out:
484 	TCPDEBUG2(PRU_CONNECT);
485 	INP_WUNLOCK(inp);
486 	INP_INFO_WUNLOCK(&V_tcbinfo);
487 	return (error);
488 }
489 
490 #ifdef INET6
491 static int
492 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
493 {
494 	int error = 0;
495 	struct inpcb *inp;
496 	struct tcpcb *tp = NULL;
497 	struct sockaddr_in6 *sin6p;
498 
499 	TCPDEBUG0;
500 
501 	sin6p = (struct sockaddr_in6 *)nam;
502 	if (nam->sa_len != sizeof (*sin6p))
503 		return (EINVAL);
504 	/*
505 	 * Must disallow TCP ``connections'' to multicast addresses.
506 	 */
507 	if (sin6p->sin6_family == AF_INET6
508 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
509 		return (EAFNOSUPPORT);
510 
511 	INP_INFO_WLOCK(&V_tcbinfo);
512 	inp = sotoinpcb(so);
513 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
514 	INP_WLOCK(inp);
515 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
516 		error = EINVAL;
517 		goto out;
518 	}
519 	tp = intotcpcb(inp);
520 	TCPDEBUG1();
521 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
522 		struct sockaddr_in sin;
523 
524 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
525 			error = EINVAL;
526 			goto out;
527 		}
528 
529 		in6_sin6_2_sin(&sin, sin6p);
530 		inp->inp_vflag |= INP_IPV4;
531 		inp->inp_vflag &= ~INP_IPV6;
532 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
533 			goto out;
534 		error = tcp_output_connect(so, nam);
535 		goto out;
536 	}
537 	inp->inp_vflag &= ~INP_IPV4;
538 	inp->inp_vflag |= INP_IPV6;
539 	inp->inp_inc.inc_isipv6 = 1;
540 	if ((error = tcp6_connect(tp, nam, td)) != 0)
541 		goto out;
542 	error = tcp_output_connect(so, nam);
543 
544 out:
545 	TCPDEBUG2(PRU_CONNECT);
546 	INP_WUNLOCK(inp);
547 	INP_INFO_WUNLOCK(&V_tcbinfo);
548 	return (error);
549 }
550 #endif /* INET6 */
551 
552 /*
553  * Initiate disconnect from peer.
554  * If connection never passed embryonic stage, just drop;
555  * else if don't need to let data drain, then can just drop anyways,
556  * else have to begin TCP shutdown process: mark socket disconnecting,
557  * drain unread data, state switch to reflect user close, and
558  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
559  * when peer sends FIN and acks ours.
560  *
561  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
562  */
563 static int
564 tcp_usr_disconnect(struct socket *so)
565 {
566 	struct inpcb *inp;
567 	struct tcpcb *tp = NULL;
568 	int error = 0;
569 
570 	TCPDEBUG0;
571 	INP_INFO_WLOCK(&V_tcbinfo);
572 	inp = sotoinpcb(so);
573 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
574 	INP_WLOCK(inp);
575 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
576 		error = ECONNRESET;
577 		goto out;
578 	}
579 	tp = intotcpcb(inp);
580 	TCPDEBUG1();
581 	tcp_disconnect(tp);
582 out:
583 	TCPDEBUG2(PRU_DISCONNECT);
584 	INP_WUNLOCK(inp);
585 	INP_INFO_WUNLOCK(&V_tcbinfo);
586 	return (error);
587 }
588 
589 /*
590  * Accept a connection.  Essentially all the work is
591  * done at higher levels; just return the address
592  * of the peer, storing through addr.
593  */
594 static int
595 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
596 {
597 	int error = 0;
598 	struct inpcb *inp = NULL;
599 	struct tcpcb *tp = NULL;
600 	struct in_addr addr;
601 	in_port_t port = 0;
602 	TCPDEBUG0;
603 
604 	if (so->so_state & SS_ISDISCONNECTED)
605 		return (ECONNABORTED);
606 
607 	inp = sotoinpcb(so);
608 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
609 	INP_INFO_RLOCK(&V_tcbinfo);
610 	INP_WLOCK(inp);
611 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
612 		error = ECONNABORTED;
613 		goto out;
614 	}
615 	tp = intotcpcb(inp);
616 	TCPDEBUG1();
617 
618 	/*
619 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
620 	 * copy the data of interest and defer the malloc until after we
621 	 * release the lock.
622 	 */
623 	port = inp->inp_fport;
624 	addr = inp->inp_faddr;
625 
626 out:
627 	TCPDEBUG2(PRU_ACCEPT);
628 	INP_WUNLOCK(inp);
629 	INP_INFO_RUNLOCK(&V_tcbinfo);
630 	if (error == 0)
631 		*nam = in_sockaddr(port, &addr);
632 	return error;
633 }
634 
635 #ifdef INET6
636 static int
637 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
638 {
639 	struct inpcb *inp = NULL;
640 	int error = 0;
641 	struct tcpcb *tp = NULL;
642 	struct in_addr addr;
643 	struct in6_addr addr6;
644 	in_port_t port = 0;
645 	int v4 = 0;
646 	TCPDEBUG0;
647 
648 	if (so->so_state & SS_ISDISCONNECTED)
649 		return (ECONNABORTED);
650 
651 	inp = sotoinpcb(so);
652 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
653 	INP_WLOCK(inp);
654 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
655 		error = ECONNABORTED;
656 		goto out;
657 	}
658 	tp = intotcpcb(inp);
659 	TCPDEBUG1();
660 
661 	/*
662 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
663 	 * copy the data of interest and defer the malloc until after we
664 	 * release the lock.
665 	 */
666 	if (inp->inp_vflag & INP_IPV4) {
667 		v4 = 1;
668 		port = inp->inp_fport;
669 		addr = inp->inp_faddr;
670 	} else {
671 		port = inp->inp_fport;
672 		addr6 = inp->in6p_faddr;
673 	}
674 
675 out:
676 	TCPDEBUG2(PRU_ACCEPT);
677 	INP_WUNLOCK(inp);
678 	if (error == 0) {
679 		if (v4)
680 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
681 		else
682 			*nam = in6_sockaddr(port, &addr6);
683 	}
684 	return error;
685 }
686 #endif /* INET6 */
687 
688 /*
689  * Mark the connection as being incapable of further output.
690  */
691 static int
692 tcp_usr_shutdown(struct socket *so)
693 {
694 	int error = 0;
695 	struct inpcb *inp;
696 	struct tcpcb *tp = NULL;
697 
698 	TCPDEBUG0;
699 	INP_INFO_WLOCK(&V_tcbinfo);
700 	inp = sotoinpcb(so);
701 	KASSERT(inp != NULL, ("inp == NULL"));
702 	INP_WLOCK(inp);
703 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
704 		error = ECONNRESET;
705 		goto out;
706 	}
707 	tp = intotcpcb(inp);
708 	TCPDEBUG1();
709 	socantsendmore(so);
710 	tcp_usrclosed(tp);
711 	error = tcp_output_disconnect(tp);
712 
713 out:
714 	TCPDEBUG2(PRU_SHUTDOWN);
715 	INP_WUNLOCK(inp);
716 	INP_INFO_WUNLOCK(&V_tcbinfo);
717 
718 	return (error);
719 }
720 
721 /*
722  * After a receive, possibly send window update to peer.
723  */
724 static int
725 tcp_usr_rcvd(struct socket *so, int flags)
726 {
727 	struct inpcb *inp;
728 	struct tcpcb *tp = NULL;
729 	int error = 0;
730 
731 	TCPDEBUG0;
732 	inp = sotoinpcb(so);
733 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
734 	INP_WLOCK(inp);
735 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
736 		error = ECONNRESET;
737 		goto out;
738 	}
739 	tp = intotcpcb(inp);
740 	TCPDEBUG1();
741 	tcp_output_rcvd(tp);
742 
743 out:
744 	TCPDEBUG2(PRU_RCVD);
745 	INP_WUNLOCK(inp);
746 	return (error);
747 }
748 
749 /*
750  * Do a send by putting data in output queue and updating urgent
751  * marker if URG set.  Possibly send more data.  Unlike the other
752  * pru_*() routines, the mbuf chains are our responsibility.  We
753  * must either enqueue them or free them.  The other pru_* routines
754  * generally are caller-frees.
755  */
756 static int
757 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
758     struct sockaddr *nam, struct mbuf *control, struct thread *td)
759 {
760 	int error = 0;
761 	struct inpcb *inp;
762 	struct tcpcb *tp = NULL;
763 	int headlocked = 0;
764 #ifdef INET6
765 	int isipv6;
766 #endif
767 	TCPDEBUG0;
768 
769 	/*
770 	 * We require the pcbinfo lock in two cases:
771 	 *
772 	 * (1) An implied connect is taking place, which can result in
773 	 *     binding IPs and ports and hence modification of the pcb hash
774 	 *     chains.
775 	 *
776 	 * (2) PRUS_EOF is set, resulting in explicit close on the send.
777 	 */
778 	if ((nam != NULL) || (flags & PRUS_EOF)) {
779 		INP_INFO_WLOCK(&V_tcbinfo);
780 		headlocked = 1;
781 	}
782 	inp = sotoinpcb(so);
783 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
784 	INP_WLOCK(inp);
785 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
786 		if (control)
787 			m_freem(control);
788 		if (m)
789 			m_freem(m);
790 		error = ECONNRESET;
791 		goto out;
792 	}
793 #ifdef INET6
794 	isipv6 = nam && nam->sa_family == AF_INET6;
795 #endif /* INET6 */
796 	tp = intotcpcb(inp);
797 	TCPDEBUG1();
798 	if (control) {
799 		/* TCP doesn't do control messages (rights, creds, etc) */
800 		if (control->m_len) {
801 			m_freem(control);
802 			if (m)
803 				m_freem(m);
804 			error = EINVAL;
805 			goto out;
806 		}
807 		m_freem(control);	/* empty control, just free it */
808 	}
809 	if (!(flags & PRUS_OOB)) {
810 		sbappendstream(&so->so_snd, m);
811 		if (nam && tp->t_state < TCPS_SYN_SENT) {
812 			/*
813 			 * Do implied connect if not yet connected,
814 			 * initialize window to default value, and
815 			 * initialize maxseg/maxopd using peer's cached
816 			 * MSS.
817 			 */
818 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
819 #ifdef INET6
820 			if (isipv6)
821 				error = tcp6_connect(tp, nam, td);
822 			else
823 #endif /* INET6 */
824 			error = tcp_connect(tp, nam, td);
825 			if (error)
826 				goto out;
827 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
828 			tcp_mss(tp, -1);
829 		}
830 		if (flags & PRUS_EOF) {
831 			/*
832 			 * Close the send side of the connection after
833 			 * the data is sent.
834 			 */
835 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
836 			socantsendmore(so);
837 			tcp_usrclosed(tp);
838 		}
839 		if (headlocked) {
840 			INP_INFO_WUNLOCK(&V_tcbinfo);
841 			headlocked = 0;
842 		}
843 		if (tp != NULL) {
844 			if (flags & PRUS_MORETOCOME)
845 				tp->t_flags |= TF_MORETOCOME;
846 			error = tcp_output_send(tp);
847 			if (flags & PRUS_MORETOCOME)
848 				tp->t_flags &= ~TF_MORETOCOME;
849 		}
850 	} else {
851 		/*
852 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
853 		 */
854 		SOCKBUF_LOCK(&so->so_snd);
855 		if (sbspace(&so->so_snd) < -512) {
856 			SOCKBUF_UNLOCK(&so->so_snd);
857 			m_freem(m);
858 			error = ENOBUFS;
859 			goto out;
860 		}
861 		/*
862 		 * According to RFC961 (Assigned Protocols),
863 		 * the urgent pointer points to the last octet
864 		 * of urgent data.  We continue, however,
865 		 * to consider it to indicate the first octet
866 		 * of data past the urgent section.
867 		 * Otherwise, snd_up should be one lower.
868 		 */
869 		sbappendstream_locked(&so->so_snd, m);
870 		SOCKBUF_UNLOCK(&so->so_snd);
871 		if (nam && tp->t_state < TCPS_SYN_SENT) {
872 			/*
873 			 * Do implied connect if not yet connected,
874 			 * initialize window to default value, and
875 			 * initialize maxseg/maxopd using peer's cached
876 			 * MSS.
877 			 */
878 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
879 #ifdef INET6
880 			if (isipv6)
881 				error = tcp6_connect(tp, nam, td);
882 			else
883 #endif /* INET6 */
884 			error = tcp_connect(tp, nam, td);
885 			if (error)
886 				goto out;
887 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
888 			tcp_mss(tp, -1);
889 			INP_INFO_WUNLOCK(&V_tcbinfo);
890 			headlocked = 0;
891 		} else if (nam) {
892 			INP_INFO_WUNLOCK(&V_tcbinfo);
893 			headlocked = 0;
894 		}
895 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
896 		tp->t_flags |= TF_FORCEDATA;
897 		error = tcp_output_send(tp);
898 		tp->t_flags &= ~TF_FORCEDATA;
899 	}
900 out:
901 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
902 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
903 	INP_WUNLOCK(inp);
904 	if (headlocked)
905 		INP_INFO_WUNLOCK(&V_tcbinfo);
906 	return (error);
907 }
908 
909 /*
910  * Abort the TCP.  Drop the connection abruptly.
911  */
912 static void
913 tcp_usr_abort(struct socket *so)
914 {
915 	struct inpcb *inp;
916 	struct tcpcb *tp = NULL;
917 	TCPDEBUG0;
918 
919 	inp = sotoinpcb(so);
920 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
921 
922 	INP_INFO_WLOCK(&V_tcbinfo);
923 	INP_WLOCK(inp);
924 	KASSERT(inp->inp_socket != NULL,
925 	    ("tcp_usr_abort: inp_socket == NULL"));
926 
927 	/*
928 	 * If we still have full TCP state, and we're not dropped, drop.
929 	 */
930 	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
931 	    !(inp->inp_vflag & INP_DROPPED)) {
932 		tp = intotcpcb(inp);
933 		TCPDEBUG1();
934 		tcp_drop(tp, ECONNABORTED);
935 		TCPDEBUG2(PRU_ABORT);
936 	}
937 	if (!(inp->inp_vflag & INP_DROPPED)) {
938 		SOCK_LOCK(so);
939 		so->so_state |= SS_PROTOREF;
940 		SOCK_UNLOCK(so);
941 		inp->inp_vflag |= INP_SOCKREF;
942 	}
943 	INP_WUNLOCK(inp);
944 	INP_INFO_WUNLOCK(&V_tcbinfo);
945 }
946 
947 /*
948  * TCP socket is closed.  Start friendly disconnect.
949  */
950 static void
951 tcp_usr_close(struct socket *so)
952 {
953 	struct inpcb *inp;
954 	struct tcpcb *tp = NULL;
955 	TCPDEBUG0;
956 
957 	inp = sotoinpcb(so);
958 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
959 
960 	INP_INFO_WLOCK(&V_tcbinfo);
961 	INP_WLOCK(inp);
962 	KASSERT(inp->inp_socket != NULL,
963 	    ("tcp_usr_close: inp_socket == NULL"));
964 
965 	/*
966 	 * If we still have full TCP state, and we're not dropped, initiate
967 	 * a disconnect.
968 	 */
969 	if (!(inp->inp_vflag & INP_TIMEWAIT) &&
970 	    !(inp->inp_vflag & INP_DROPPED)) {
971 		tp = intotcpcb(inp);
972 		TCPDEBUG1();
973 		tcp_disconnect(tp);
974 		TCPDEBUG2(PRU_CLOSE);
975 	}
976 	if (!(inp->inp_vflag & INP_DROPPED)) {
977 		SOCK_LOCK(so);
978 		so->so_state |= SS_PROTOREF;
979 		SOCK_UNLOCK(so);
980 		inp->inp_vflag |= INP_SOCKREF;
981 	}
982 	INP_WUNLOCK(inp);
983 	INP_INFO_WUNLOCK(&V_tcbinfo);
984 }
985 
986 /*
987  * Receive out-of-band data.
988  */
989 static int
990 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
991 {
992 	int error = 0;
993 	struct inpcb *inp;
994 	struct tcpcb *tp = NULL;
995 
996 	TCPDEBUG0;
997 	inp = sotoinpcb(so);
998 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
999 	INP_WLOCK(inp);
1000 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
1001 		error = ECONNRESET;
1002 		goto out;
1003 	}
1004 	tp = intotcpcb(inp);
1005 	TCPDEBUG1();
1006 	if ((so->so_oobmark == 0 &&
1007 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1008 	    so->so_options & SO_OOBINLINE ||
1009 	    tp->t_oobflags & TCPOOB_HADDATA) {
1010 		error = EINVAL;
1011 		goto out;
1012 	}
1013 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1014 		error = EWOULDBLOCK;
1015 		goto out;
1016 	}
1017 	m->m_len = 1;
1018 	*mtod(m, caddr_t) = tp->t_iobc;
1019 	if ((flags & MSG_PEEK) == 0)
1020 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1021 
1022 out:
1023 	TCPDEBUG2(PRU_RCVOOB);
1024 	INP_WUNLOCK(inp);
1025 	return (error);
1026 }
1027 
1028 struct pr_usrreqs tcp_usrreqs = {
1029 	.pru_abort =		tcp_usr_abort,
1030 	.pru_accept =		tcp_usr_accept,
1031 	.pru_attach =		tcp_usr_attach,
1032 	.pru_bind =		tcp_usr_bind,
1033 	.pru_connect =		tcp_usr_connect,
1034 	.pru_control =		in_control,
1035 	.pru_detach =		tcp_usr_detach,
1036 	.pru_disconnect =	tcp_usr_disconnect,
1037 	.pru_listen =		tcp_usr_listen,
1038 	.pru_peeraddr =		in_getpeeraddr,
1039 	.pru_rcvd =		tcp_usr_rcvd,
1040 	.pru_rcvoob =		tcp_usr_rcvoob,
1041 	.pru_send =		tcp_usr_send,
1042 	.pru_shutdown =		tcp_usr_shutdown,
1043 	.pru_sockaddr =		in_getsockaddr,
1044 	.pru_sosetlabel =	in_pcbsosetlabel,
1045 	.pru_close =		tcp_usr_close,
1046 };
1047 
1048 #ifdef INET6
1049 struct pr_usrreqs tcp6_usrreqs = {
1050 	.pru_abort =		tcp_usr_abort,
1051 	.pru_accept =		tcp6_usr_accept,
1052 	.pru_attach =		tcp_usr_attach,
1053 	.pru_bind =		tcp6_usr_bind,
1054 	.pru_connect =		tcp6_usr_connect,
1055 	.pru_control =		in6_control,
1056 	.pru_detach =		tcp_usr_detach,
1057 	.pru_disconnect =	tcp_usr_disconnect,
1058 	.pru_listen =		tcp6_usr_listen,
1059 	.pru_peeraddr =		in6_mapped_peeraddr,
1060 	.pru_rcvd =		tcp_usr_rcvd,
1061 	.pru_rcvoob =		tcp_usr_rcvoob,
1062 	.pru_send =		tcp_usr_send,
1063 	.pru_shutdown =		tcp_usr_shutdown,
1064 	.pru_sockaddr =		in6_mapped_sockaddr,
1065  	.pru_sosetlabel =	in_pcbsosetlabel,
1066 	.pru_close =		tcp_usr_close,
1067 };
1068 #endif /* INET6 */
1069 
1070 /*
1071  * Common subroutine to open a TCP connection to remote host specified
1072  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
1073  * port number if needed.  Call in_pcbconnect_setup to do the routing and
1074  * to choose a local host address (interface).  If there is an existing
1075  * incarnation of the same connection in TIME-WAIT state and if the remote
1076  * host was sending CC options and if the connection duration was < MSL, then
1077  * truncate the previous TIME-WAIT state and proceed.
1078  * Initialize connection parameters and enter SYN-SENT state.
1079  */
1080 static int
1081 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1082 {
1083 	struct inpcb *inp = tp->t_inpcb, *oinp;
1084 	struct socket *so = inp->inp_socket;
1085 	struct in_addr laddr;
1086 	u_short lport;
1087 	int error;
1088 
1089 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1090 	INP_WLOCK_ASSERT(inp);
1091 
1092 	if (inp->inp_lport == 0) {
1093 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1094 		if (error)
1095 			return error;
1096 	}
1097 
1098 	/*
1099 	 * Cannot simply call in_pcbconnect, because there might be an
1100 	 * earlier incarnation of this same connection still in
1101 	 * TIME_WAIT state, creating an ADDRINUSE error.
1102 	 */
1103 	laddr = inp->inp_laddr;
1104 	lport = inp->inp_lport;
1105 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1106 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1107 	if (error && oinp == NULL)
1108 		return error;
1109 	if (oinp)
1110 		return EADDRINUSE;
1111 	inp->inp_laddr = laddr;
1112 	in_pcbrehash(inp);
1113 
1114 	/*
1115 	 * Compute window scaling to request:
1116 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1117 	 * XXX: This should move to tcp_output().
1118 	 */
1119 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1120 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1121 		tp->request_r_scale++;
1122 
1123 	soisconnecting(so);
1124 	V_tcpstat.tcps_connattempt++;
1125 	tp->t_state = TCPS_SYN_SENT;
1126 	tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1127 	tp->iss = tcp_new_isn(tp);
1128 	tp->t_bw_rtseq = tp->iss;
1129 	tcp_sendseqinit(tp);
1130 
1131 	return 0;
1132 }
1133 
1134 #ifdef INET6
1135 static int
1136 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1137 {
1138 	struct inpcb *inp = tp->t_inpcb, *oinp;
1139 	struct socket *so = inp->inp_socket;
1140 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1141 	struct in6_addr *addr6;
1142 	int error;
1143 
1144 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1145 	INP_WLOCK_ASSERT(inp);
1146 
1147 	if (inp->inp_lport == 0) {
1148 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1149 		if (error)
1150 			return error;
1151 	}
1152 
1153 	/*
1154 	 * Cannot simply call in_pcbconnect, because there might be an
1155 	 * earlier incarnation of this same connection still in
1156 	 * TIME_WAIT state, creating an ADDRINUSE error.
1157 	 * in6_pcbladdr() also handles scope zone IDs.
1158 	 */
1159 	error = in6_pcbladdr(inp, nam, &addr6);
1160 	if (error)
1161 		return error;
1162 	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1163 				  &sin6->sin6_addr, sin6->sin6_port,
1164 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1165 				  ? addr6
1166 				  : &inp->in6p_laddr,
1167 				  inp->inp_lport,  0, NULL);
1168 	if (oinp)
1169 		return EADDRINUSE;
1170 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1171 		inp->in6p_laddr = *addr6;
1172 	inp->in6p_faddr = sin6->sin6_addr;
1173 	inp->inp_fport = sin6->sin6_port;
1174 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
1175 	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
1176 	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
1177 		inp->in6p_flowinfo |=
1178 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1179 	in_pcbrehash(inp);
1180 
1181 	/* Compute window scaling to request.  */
1182 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1183 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1184 		tp->request_r_scale++;
1185 
1186 	soisconnecting(so);
1187 	V_tcpstat.tcps_connattempt++;
1188 	tp->t_state = TCPS_SYN_SENT;
1189 	tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1190 	tp->iss = tcp_new_isn(tp);
1191 	tp->t_bw_rtseq = tp->iss;
1192 	tcp_sendseqinit(tp);
1193 
1194 	return 0;
1195 }
1196 #endif /* INET6 */
1197 
1198 /*
1199  * Export TCP internal state information via a struct tcp_info, based on the
1200  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1201  * (TCP state machine, etc).  We export all information using FreeBSD-native
1202  * constants -- for example, the numeric values for tcpi_state will differ
1203  * from Linux.
1204  */
1205 static void
1206 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1207 {
1208 
1209 	INP_WLOCK_ASSERT(tp->t_inpcb);
1210 	bzero(ti, sizeof(*ti));
1211 
1212 	ti->tcpi_state = tp->t_state;
1213 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1214 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1215 	if (tp->t_flags & TF_SACK_PERMIT)
1216 		ti->tcpi_options |= TCPI_OPT_SACK;
1217 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1218 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1219 		ti->tcpi_snd_wscale = tp->snd_scale;
1220 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1221 	}
1222 
1223 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1224 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1225 
1226 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1227 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1228 
1229 	/*
1230 	 * FreeBSD-specific extension fields for tcp_info.
1231 	 */
1232 	ti->tcpi_rcv_space = tp->rcv_wnd;
1233 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1234 	ti->tcpi_snd_wnd = tp->snd_wnd;
1235 	ti->tcpi_snd_bwnd = tp->snd_bwnd;
1236 	ti->tcpi_snd_nxt = tp->snd_nxt;
1237 	ti->__tcpi_snd_mss = tp->t_maxseg;
1238 	ti->__tcpi_rcv_mss = tp->t_maxseg;
1239 	if (tp->t_flags & TF_TOE)
1240 		ti->tcpi_options |= TCPI_OPT_TOE;
1241 }
1242 
1243 /*
1244  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1245  * socket option arguments.  When it re-acquires the lock after the copy, it
1246  * has to revalidate that the connection is still valid for the socket
1247  * option.
1248  */
1249 #define INP_WLOCK_RECHECK(inp) do {					\
1250 	INP_WLOCK(inp);							\
1251 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {		\
1252 		INP_WUNLOCK(inp);					\
1253 		return (ECONNRESET);					\
1254 	}								\
1255 	tp = intotcpcb(inp);						\
1256 } while(0)
1257 
1258 int
1259 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1260 {
1261 	int	error, opt, optval;
1262 	struct	inpcb *inp;
1263 	struct	tcpcb *tp;
1264 	struct	tcp_info ti;
1265 
1266 	error = 0;
1267 	inp = sotoinpcb(so);
1268 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1269 	INP_WLOCK(inp);
1270 	if (sopt->sopt_level != IPPROTO_TCP) {
1271 #ifdef INET6
1272 		if (INP_CHECK_SOCKAF(so, AF_INET6)) {
1273 			INP_WUNLOCK(inp);
1274 			error = ip6_ctloutput(so, sopt);
1275 		} else {
1276 #endif /* INET6 */
1277 			INP_WUNLOCK(inp);
1278 			error = ip_ctloutput(so, sopt);
1279 #ifdef INET6
1280 		}
1281 #endif
1282 		return (error);
1283 	}
1284 	if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) {
1285 		INP_WUNLOCK(inp);
1286 		return (ECONNRESET);
1287 	}
1288 
1289 	switch (sopt->sopt_dir) {
1290 	case SOPT_SET:
1291 		switch (sopt->sopt_name) {
1292 #ifdef TCP_SIGNATURE
1293 		case TCP_MD5SIG:
1294 			INP_WUNLOCK(inp);
1295 			error = sooptcopyin(sopt, &optval, sizeof optval,
1296 			    sizeof optval);
1297 			if (error)
1298 				return (error);
1299 
1300 			INP_WLOCK_RECHECK(inp);
1301 			if (optval > 0)
1302 				tp->t_flags |= TF_SIGNATURE;
1303 			else
1304 				tp->t_flags &= ~TF_SIGNATURE;
1305 			INP_WUNLOCK(inp);
1306 			break;
1307 #endif /* TCP_SIGNATURE */
1308 		case TCP_NODELAY:
1309 		case TCP_NOOPT:
1310 			INP_WUNLOCK(inp);
1311 			error = sooptcopyin(sopt, &optval, sizeof optval,
1312 			    sizeof optval);
1313 			if (error)
1314 				return (error);
1315 
1316 			INP_WLOCK_RECHECK(inp);
1317 			switch (sopt->sopt_name) {
1318 			case TCP_NODELAY:
1319 				opt = TF_NODELAY;
1320 				break;
1321 			case TCP_NOOPT:
1322 				opt = TF_NOOPT;
1323 				break;
1324 			default:
1325 				opt = 0; /* dead code to fool gcc */
1326 				break;
1327 			}
1328 
1329 			if (optval)
1330 				tp->t_flags |= opt;
1331 			else
1332 				tp->t_flags &= ~opt;
1333 			INP_WUNLOCK(inp);
1334 			break;
1335 
1336 		case TCP_NOPUSH:
1337 			INP_WUNLOCK(inp);
1338 			error = sooptcopyin(sopt, &optval, sizeof optval,
1339 			    sizeof optval);
1340 			if (error)
1341 				return (error);
1342 
1343 			INP_WLOCK_RECHECK(inp);
1344 			if (optval)
1345 				tp->t_flags |= TF_NOPUSH;
1346 			else {
1347 				tp->t_flags &= ~TF_NOPUSH;
1348 				error = tcp_output(tp);
1349 			}
1350 			INP_WUNLOCK(inp);
1351 			break;
1352 
1353 		case TCP_MAXSEG:
1354 			INP_WUNLOCK(inp);
1355 			error = sooptcopyin(sopt, &optval, sizeof optval,
1356 			    sizeof optval);
1357 			if (error)
1358 				return (error);
1359 
1360 			INP_WLOCK_RECHECK(inp);
1361 			if (optval > 0 && optval <= tp->t_maxseg &&
1362 			    optval + 40 >= V_tcp_minmss)
1363 				tp->t_maxseg = optval;
1364 			else
1365 				error = EINVAL;
1366 			INP_WUNLOCK(inp);
1367 			break;
1368 
1369 		case TCP_INFO:
1370 			INP_WUNLOCK(inp);
1371 			error = EINVAL;
1372 			break;
1373 
1374 		default:
1375 			INP_WUNLOCK(inp);
1376 			error = ENOPROTOOPT;
1377 			break;
1378 		}
1379 		break;
1380 
1381 	case SOPT_GET:
1382 		tp = intotcpcb(inp);
1383 		switch (sopt->sopt_name) {
1384 #ifdef TCP_SIGNATURE
1385 		case TCP_MD5SIG:
1386 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1387 			INP_WUNLOCK(inp);
1388 			error = sooptcopyout(sopt, &optval, sizeof optval);
1389 			break;
1390 #endif
1391 
1392 		case TCP_NODELAY:
1393 			optval = tp->t_flags & TF_NODELAY;
1394 			INP_WUNLOCK(inp);
1395 			error = sooptcopyout(sopt, &optval, sizeof optval);
1396 			break;
1397 		case TCP_MAXSEG:
1398 			optval = tp->t_maxseg;
1399 			INP_WUNLOCK(inp);
1400 			error = sooptcopyout(sopt, &optval, sizeof optval);
1401 			break;
1402 		case TCP_NOOPT:
1403 			optval = tp->t_flags & TF_NOOPT;
1404 			INP_WUNLOCK(inp);
1405 			error = sooptcopyout(sopt, &optval, sizeof optval);
1406 			break;
1407 		case TCP_NOPUSH:
1408 			optval = tp->t_flags & TF_NOPUSH;
1409 			INP_WUNLOCK(inp);
1410 			error = sooptcopyout(sopt, &optval, sizeof optval);
1411 			break;
1412 		case TCP_INFO:
1413 			tcp_fill_info(tp, &ti);
1414 			INP_WUNLOCK(inp);
1415 			error = sooptcopyout(sopt, &ti, sizeof ti);
1416 			break;
1417 		default:
1418 			INP_WUNLOCK(inp);
1419 			error = ENOPROTOOPT;
1420 			break;
1421 		}
1422 		break;
1423 	}
1424 	return (error);
1425 }
1426 #undef INP_WLOCK_RECHECK
1427 
1428 /*
1429  * tcp_sendspace and tcp_recvspace are the default send and receive window
1430  * sizes, respectively.  These are obsolescent (this information should
1431  * be set by the route).
1432  */
1433 u_long	tcp_sendspace = 1024*32;
1434 SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1435     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1436 u_long	tcp_recvspace = 1024*64;
1437 SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1438     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1439 
1440 /*
1441  * Attach TCP protocol to socket, allocating
1442  * internet protocol control block, tcp control block,
1443  * bufer space, and entering LISTEN state if to accept connections.
1444  */
1445 static int
1446 tcp_attach(struct socket *so)
1447 {
1448 	struct tcpcb *tp;
1449 	struct inpcb *inp;
1450 	int error;
1451 #ifdef INET6
1452 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1453 #endif
1454 
1455 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1456 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1457 		if (error)
1458 			return (error);
1459 	}
1460 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
1461 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1462 	INP_INFO_WLOCK(&V_tcbinfo);
1463 	error = in_pcballoc(so, &V_tcbinfo);
1464 	if (error) {
1465 		INP_INFO_WUNLOCK(&V_tcbinfo);
1466 		return (error);
1467 	}
1468 	inp = sotoinpcb(so);
1469 #ifdef INET6
1470 	if (isipv6) {
1471 		inp->inp_vflag |= INP_IPV6;
1472 		inp->in6p_hops = -1;	/* use kernel default */
1473 	}
1474 	else
1475 #endif
1476 	inp->inp_vflag |= INP_IPV4;
1477 	tp = tcp_newtcpcb(inp);
1478 	if (tp == NULL) {
1479 #ifdef INET6
1480 		if (isipv6) {
1481 			in6_pcbdetach(inp);
1482 			in6_pcbfree(inp);
1483 		} else {
1484 #endif
1485 			in_pcbdetach(inp);
1486 			in_pcbfree(inp);
1487 #ifdef INET6
1488 		}
1489 #endif
1490 		INP_INFO_WUNLOCK(&V_tcbinfo);
1491 		return (ENOBUFS);
1492 	}
1493 	tp->t_state = TCPS_CLOSED;
1494 	INP_WUNLOCK(inp);
1495 	INP_INFO_WUNLOCK(&V_tcbinfo);
1496 	return (0);
1497 }
1498 
1499 /*
1500  * Initiate (or continue) disconnect.
1501  * If embryonic state, just send reset (once).
1502  * If in ``let data drain'' option and linger null, just drop.
1503  * Otherwise (hard), mark socket disconnecting and drop
1504  * current input data; switch states based on user close, and
1505  * send segment to peer (with FIN).
1506  */
1507 static void
1508 tcp_disconnect(struct tcpcb *tp)
1509 {
1510 	struct inpcb *inp = tp->t_inpcb;
1511 	struct socket *so = inp->inp_socket;
1512 
1513 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1514 	INP_WLOCK_ASSERT(inp);
1515 
1516 	/*
1517 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1518 	 * socket is still open.
1519 	 */
1520 	if (tp->t_state < TCPS_ESTABLISHED) {
1521 		tp = tcp_close(tp);
1522 		KASSERT(tp != NULL,
1523 		    ("tcp_disconnect: tcp_close() returned NULL"));
1524 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1525 		tp = tcp_drop(tp, 0);
1526 		KASSERT(tp != NULL,
1527 		    ("tcp_disconnect: tcp_drop() returned NULL"));
1528 	} else {
1529 		soisdisconnecting(so);
1530 		sbflush(&so->so_rcv);
1531 		tcp_usrclosed(tp);
1532 		if (!(inp->inp_vflag & INP_DROPPED))
1533 			tcp_output_disconnect(tp);
1534 	}
1535 }
1536 
1537 /*
1538  * User issued close, and wish to trail through shutdown states:
1539  * if never received SYN, just forget it.  If got a SYN from peer,
1540  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1541  * If already got a FIN from peer, then almost done; go to LAST_ACK
1542  * state.  In all other cases, have already sent FIN to peer (e.g.
1543  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1544  * for peer to send FIN or not respond to keep-alives, etc.
1545  * We can let the user exit from the close as soon as the FIN is acked.
1546  */
1547 static void
1548 tcp_usrclosed(struct tcpcb *tp)
1549 {
1550 
1551 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1552 	INP_WLOCK_ASSERT(tp->t_inpcb);
1553 
1554 	switch (tp->t_state) {
1555 	case TCPS_LISTEN:
1556 		tcp_offload_listen_close(tp);
1557 		/* FALLTHROUGH */
1558 	case TCPS_CLOSED:
1559 		tp->t_state = TCPS_CLOSED;
1560 		tp = tcp_close(tp);
1561 		/*
1562 		 * tcp_close() should never return NULL here as the socket is
1563 		 * still open.
1564 		 */
1565 		KASSERT(tp != NULL,
1566 		    ("tcp_usrclosed: tcp_close() returned NULL"));
1567 		break;
1568 
1569 	case TCPS_SYN_SENT:
1570 	case TCPS_SYN_RECEIVED:
1571 		tp->t_flags |= TF_NEEDFIN;
1572 		break;
1573 
1574 	case TCPS_ESTABLISHED:
1575 		tp->t_state = TCPS_FIN_WAIT_1;
1576 		break;
1577 
1578 	case TCPS_CLOSE_WAIT:
1579 		tp->t_state = TCPS_LAST_ACK;
1580 		break;
1581 	}
1582 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
1583 		soisdisconnected(tp->t_inpcb->inp_socket);
1584 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
1585 		if (tp->t_state == TCPS_FIN_WAIT_2) {
1586 			int timeout;
1587 
1588 			timeout = (tcp_fast_finwait2_recycle) ?
1589 			    tcp_finwait2_timeout : tcp_maxidle;
1590 			tcp_timer_activate(tp, TT_2MSL, timeout);
1591 		}
1592 	}
1593 }
1594 
1595 #ifdef DDB
1596 static void
1597 db_print_indent(int indent)
1598 {
1599 	int i;
1600 
1601 	for (i = 0; i < indent; i++)
1602 		db_printf(" ");
1603 }
1604 
1605 static void
1606 db_print_tstate(int t_state)
1607 {
1608 
1609 	switch (t_state) {
1610 	case TCPS_CLOSED:
1611 		db_printf("TCPS_CLOSED");
1612 		return;
1613 
1614 	case TCPS_LISTEN:
1615 		db_printf("TCPS_LISTEN");
1616 		return;
1617 
1618 	case TCPS_SYN_SENT:
1619 		db_printf("TCPS_SYN_SENT");
1620 		return;
1621 
1622 	case TCPS_SYN_RECEIVED:
1623 		db_printf("TCPS_SYN_RECEIVED");
1624 		return;
1625 
1626 	case TCPS_ESTABLISHED:
1627 		db_printf("TCPS_ESTABLISHED");
1628 		return;
1629 
1630 	case TCPS_CLOSE_WAIT:
1631 		db_printf("TCPS_CLOSE_WAIT");
1632 		return;
1633 
1634 	case TCPS_FIN_WAIT_1:
1635 		db_printf("TCPS_FIN_WAIT_1");
1636 		return;
1637 
1638 	case TCPS_CLOSING:
1639 		db_printf("TCPS_CLOSING");
1640 		return;
1641 
1642 	case TCPS_LAST_ACK:
1643 		db_printf("TCPS_LAST_ACK");
1644 		return;
1645 
1646 	case TCPS_FIN_WAIT_2:
1647 		db_printf("TCPS_FIN_WAIT_2");
1648 		return;
1649 
1650 	case TCPS_TIME_WAIT:
1651 		db_printf("TCPS_TIME_WAIT");
1652 		return;
1653 
1654 	default:
1655 		db_printf("unknown");
1656 		return;
1657 	}
1658 }
1659 
1660 static void
1661 db_print_tflags(u_int t_flags)
1662 {
1663 	int comma;
1664 
1665 	comma = 0;
1666 	if (t_flags & TF_ACKNOW) {
1667 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
1668 		comma = 1;
1669 	}
1670 	if (t_flags & TF_DELACK) {
1671 		db_printf("%sTF_DELACK", comma ? ", " : "");
1672 		comma = 1;
1673 	}
1674 	if (t_flags & TF_NODELAY) {
1675 		db_printf("%sTF_NODELAY", comma ? ", " : "");
1676 		comma = 1;
1677 	}
1678 	if (t_flags & TF_NOOPT) {
1679 		db_printf("%sTF_NOOPT", comma ? ", " : "");
1680 		comma = 1;
1681 	}
1682 	if (t_flags & TF_SENTFIN) {
1683 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
1684 		comma = 1;
1685 	}
1686 	if (t_flags & TF_REQ_SCALE) {
1687 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1688 		comma = 1;
1689 	}
1690 	if (t_flags & TF_RCVD_SCALE) {
1691 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1692 		comma = 1;
1693 	}
1694 	if (t_flags & TF_REQ_TSTMP) {
1695 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1696 		comma = 1;
1697 	}
1698 	if (t_flags & TF_RCVD_TSTMP) {
1699 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1700 		comma = 1;
1701 	}
1702 	if (t_flags & TF_SACK_PERMIT) {
1703 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1704 		comma = 1;
1705 	}
1706 	if (t_flags & TF_NEEDSYN) {
1707 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1708 		comma = 1;
1709 	}
1710 	if (t_flags & TF_NEEDFIN) {
1711 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1712 		comma = 1;
1713 	}
1714 	if (t_flags & TF_NOPUSH) {
1715 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
1716 		comma = 1;
1717 	}
1718 	if (t_flags & TF_NOPUSH) {
1719 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
1720 		comma = 1;
1721 	}
1722 	if (t_flags & TF_MORETOCOME) {
1723 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1724 		comma = 1;
1725 	}
1726 	if (t_flags & TF_LQ_OVERFLOW) {
1727 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1728 		comma = 1;
1729 	}
1730 	if (t_flags & TF_LASTIDLE) {
1731 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1732 		comma = 1;
1733 	}
1734 	if (t_flags & TF_RXWIN0SENT) {
1735 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1736 		comma = 1;
1737 	}
1738 	if (t_flags & TF_FASTRECOVERY) {
1739 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1740 		comma = 1;
1741 	}
1742 	if (t_flags & TF_WASFRECOVERY) {
1743 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1744 		comma = 1;
1745 	}
1746 	if (t_flags & TF_SIGNATURE) {
1747 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1748 		comma = 1;
1749 	}
1750 	if (t_flags & TF_FORCEDATA) {
1751 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1752 		comma = 1;
1753 	}
1754 	if (t_flags & TF_TSO) {
1755 		db_printf("%sTF_TSO", comma ? ", " : "");
1756 		comma = 1;
1757 	}
1758 	if (t_flags & TF_ECN_PERMIT) {
1759 		db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
1760 		comma = 1;
1761 	}
1762 }
1763 
1764 static void
1765 db_print_toobflags(char t_oobflags)
1766 {
1767 	int comma;
1768 
1769 	comma = 0;
1770 	if (t_oobflags & TCPOOB_HAVEDATA) {
1771 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1772 		comma = 1;
1773 	}
1774 	if (t_oobflags & TCPOOB_HADDATA) {
1775 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1776 		comma = 1;
1777 	}
1778 }
1779 
1780 static void
1781 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1782 {
1783 
1784 	db_print_indent(indent);
1785 	db_printf("%s at %p\n", name, tp);
1786 
1787 	indent += 2;
1788 
1789 	db_print_indent(indent);
1790 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
1791 	   LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
1792 
1793 	db_print_indent(indent);
1794 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
1795 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
1796 
1797 	db_print_indent(indent);
1798 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
1799 	    &tp->t_timers->tt_delack, tp->t_inpcb);
1800 
1801 	db_print_indent(indent);
1802 	db_printf("t_state: %d (", tp->t_state);
1803 	db_print_tstate(tp->t_state);
1804 	db_printf(")\n");
1805 
1806 	db_print_indent(indent);
1807 	db_printf("t_flags: 0x%x (", tp->t_flags);
1808 	db_print_tflags(tp->t_flags);
1809 	db_printf(")\n");
1810 
1811 	db_print_indent(indent);
1812 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
1813 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
1814 
1815 	db_print_indent(indent);
1816 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
1817 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
1818 
1819 	db_print_indent(indent);
1820 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
1821 	    tp->iss, tp->irs, tp->rcv_nxt);
1822 
1823 	db_print_indent(indent);
1824 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %lu   rcv_up: 0x%08x\n",
1825 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
1826 
1827 	db_print_indent(indent);
1828 	db_printf("snd_wnd: %lu   snd_cwnd: %lu   snd_bwnd: %lu\n",
1829 	   tp->snd_wnd, tp->snd_cwnd, tp->snd_bwnd);
1830 
1831 	db_print_indent(indent);
1832 	db_printf("snd_ssthresh: %lu   snd_bandwidth: %lu   snd_recover: "
1833 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_bandwidth,
1834 	    tp->snd_recover);
1835 
1836 	db_print_indent(indent);
1837 	db_printf("t_maxopd: %u   t_rcvtime: %lu   t_startime: %lu\n",
1838 	    tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
1839 
1840 	db_print_indent(indent);
1841 	db_printf("t_rttime: %d   t_rtsq: 0x%08x   t_bw_rtttime: %d\n",
1842 	    tp->t_rtttime, tp->t_rtseq, tp->t_bw_rtttime);
1843 
1844 	db_print_indent(indent);
1845 	db_printf("t_bw_rtseq: 0x%08x   t_rxtcur: %d   t_maxseg: %u   "
1846 	    "t_srtt: %d\n", tp->t_bw_rtseq, tp->t_rxtcur, tp->t_maxseg,
1847 	    tp->t_srtt);
1848 
1849 	db_print_indent(indent);
1850 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
1851 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
1852 	    tp->t_rttbest);
1853 
1854 	db_print_indent(indent);
1855 	db_printf("t_rttupdated: %lu   max_sndwnd: %lu   t_softerror: %d\n",
1856 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
1857 
1858 	db_print_indent(indent);
1859 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
1860 	db_print_toobflags(tp->t_oobflags);
1861 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
1862 
1863 	db_print_indent(indent);
1864 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
1865 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
1866 
1867 	db_print_indent(indent);
1868 	db_printf("ts_recent: %u   ts_recent_age: %lu\n",
1869 	    tp->ts_recent, tp->ts_recent_age);
1870 
1871 	db_print_indent(indent);
1872 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
1873 	    "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
1874 
1875 	db_print_indent(indent);
1876 	db_printf("snd_ssthresh_prev: %lu   snd_recover_prev: 0x%08x   "
1877 	    "t_badrxtwin: %lu\n", tp->snd_ssthresh_prev,
1878 	    tp->snd_recover_prev, tp->t_badrxtwin);
1879 
1880 	db_print_indent(indent);
1881 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
1882 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
1883 
1884 	db_print_indent(indent);
1885 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
1886 	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
1887 
1888 	/* Skip sackblks, sackhint. */
1889 
1890 	db_print_indent(indent);
1891 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
1892 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
1893 }
1894 
1895 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
1896 {
1897 	struct tcpcb *tp;
1898 
1899 	if (!have_addr) {
1900 		db_printf("usage: show tcpcb <addr>\n");
1901 		return;
1902 	}
1903 	tp = (struct tcpcb *)addr;
1904 
1905 	db_print_tcpcb(tp, "tcpcb", 0);
1906 }
1907 #endif
1908