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