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