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