xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 0572ccaa4543b0abef8ef81e384c1d04de9f3da1)
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 | INP_DROPPED)) {
596 		error = ECONNRESET;
597 		goto out;
598 	}
599 	tp = intotcpcb(inp);
600 	TCPDEBUG1();
601 	tcp_disconnect(tp);
602 out:
603 	TCPDEBUG2(PRU_DISCONNECT);
604 	INP_WUNLOCK(inp);
605 	INP_INFO_WUNLOCK(&V_tcbinfo);
606 	return (error);
607 }
608 
609 #ifdef INET
610 /*
611  * Accept a connection.  Essentially all the work is done at higher levels;
612  * just return the address of the peer, storing through addr.
613  */
614 static int
615 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
616 {
617 	int error = 0;
618 	struct inpcb *inp = NULL;
619 	struct tcpcb *tp = NULL;
620 	struct in_addr addr;
621 	in_port_t port = 0;
622 	TCPDEBUG0;
623 
624 	if (so->so_state & SS_ISDISCONNECTED)
625 		return (ECONNABORTED);
626 
627 	inp = sotoinpcb(so);
628 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
629 	INP_WLOCK(inp);
630 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
631 		error = ECONNABORTED;
632 		goto out;
633 	}
634 	tp = intotcpcb(inp);
635 	TCPDEBUG1();
636 
637 	/*
638 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
639 	 * copy the data of interest and defer the malloc until after we
640 	 * release the lock.
641 	 */
642 	port = inp->inp_fport;
643 	addr = inp->inp_faddr;
644 
645 out:
646 	TCPDEBUG2(PRU_ACCEPT);
647 	INP_WUNLOCK(inp);
648 	if (error == 0)
649 		*nam = in_sockaddr(port, &addr);
650 	return error;
651 }
652 #endif /* INET */
653 
654 #ifdef INET6
655 static int
656 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
657 {
658 	struct inpcb *inp = NULL;
659 	int error = 0;
660 	struct tcpcb *tp = NULL;
661 	struct in_addr addr;
662 	struct in6_addr addr6;
663 	in_port_t port = 0;
664 	int v4 = 0;
665 	TCPDEBUG0;
666 
667 	if (so->so_state & SS_ISDISCONNECTED)
668 		return (ECONNABORTED);
669 
670 	inp = sotoinpcb(so);
671 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
672 	INP_INFO_RLOCK(&V_tcbinfo);
673 	INP_WLOCK(inp);
674 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
675 		error = ECONNABORTED;
676 		goto out;
677 	}
678 	tp = intotcpcb(inp);
679 	TCPDEBUG1();
680 
681 	/*
682 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
683 	 * copy the data of interest and defer the malloc until after we
684 	 * release the lock.
685 	 */
686 	if (inp->inp_vflag & INP_IPV4) {
687 		v4 = 1;
688 		port = inp->inp_fport;
689 		addr = inp->inp_faddr;
690 	} else {
691 		port = inp->inp_fport;
692 		addr6 = inp->in6p_faddr;
693 	}
694 
695 out:
696 	TCPDEBUG2(PRU_ACCEPT);
697 	INP_WUNLOCK(inp);
698 	INP_INFO_RUNLOCK(&V_tcbinfo);
699 	if (error == 0) {
700 		if (v4)
701 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
702 		else
703 			*nam = in6_sockaddr(port, &addr6);
704 	}
705 	return error;
706 }
707 #endif /* INET6 */
708 
709 /*
710  * Mark the connection as being incapable of further output.
711  */
712 static int
713 tcp_usr_shutdown(struct socket *so)
714 {
715 	int error = 0;
716 	struct inpcb *inp;
717 	struct tcpcb *tp = NULL;
718 
719 	TCPDEBUG0;
720 	INP_INFO_WLOCK(&V_tcbinfo);
721 	inp = sotoinpcb(so);
722 	KASSERT(inp != NULL, ("inp == NULL"));
723 	INP_WLOCK(inp);
724 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
725 		error = ECONNRESET;
726 		goto out;
727 	}
728 	tp = intotcpcb(inp);
729 	TCPDEBUG1();
730 	socantsendmore(so);
731 	tcp_usrclosed(tp);
732 	if (!(inp->inp_flags & INP_DROPPED))
733 		error = tcp_output(tp);
734 
735 out:
736 	TCPDEBUG2(PRU_SHUTDOWN);
737 	INP_WUNLOCK(inp);
738 	INP_INFO_WUNLOCK(&V_tcbinfo);
739 
740 	return (error);
741 }
742 
743 /*
744  * After a receive, possibly send window update to peer.
745  */
746 static int
747 tcp_usr_rcvd(struct socket *so, int flags)
748 {
749 	struct inpcb *inp;
750 	struct tcpcb *tp = NULL;
751 	int error = 0;
752 
753 	TCPDEBUG0;
754 	inp = sotoinpcb(so);
755 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
756 	INP_WLOCK(inp);
757 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
758 		error = ECONNRESET;
759 		goto out;
760 	}
761 	tp = intotcpcb(inp);
762 	TCPDEBUG1();
763 #ifdef TCP_OFFLOAD
764 	if (tp->t_flags & TF_TOE)
765 		tcp_offload_rcvd(tp);
766 	else
767 #endif
768 	tcp_output(tp);
769 
770 out:
771 	TCPDEBUG2(PRU_RCVD);
772 	INP_WUNLOCK(inp);
773 	return (error);
774 }
775 
776 /*
777  * Do a send by putting data in output queue and updating urgent
778  * marker if URG set.  Possibly send more data.  Unlike the other
779  * pru_*() routines, the mbuf chains are our responsibility.  We
780  * must either enqueue them or free them.  The other pru_* routines
781  * generally are caller-frees.
782  */
783 static int
784 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
785     struct sockaddr *nam, struct mbuf *control, struct thread *td)
786 {
787 	int error = 0;
788 	struct inpcb *inp;
789 	struct tcpcb *tp = NULL;
790 #ifdef INET6
791 	int isipv6;
792 #endif
793 	TCPDEBUG0;
794 
795 	/*
796 	 * We require the pcbinfo lock if we will close the socket as part of
797 	 * this call.
798 	 */
799 	if (flags & PRUS_EOF)
800 		INP_INFO_WLOCK(&V_tcbinfo);
801 	inp = sotoinpcb(so);
802 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
803 	INP_WLOCK(inp);
804 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
805 		if (control)
806 			m_freem(control);
807 		if (m)
808 			m_freem(m);
809 		error = ECONNRESET;
810 		goto out;
811 	}
812 #ifdef INET6
813 	isipv6 = nam && nam->sa_family == AF_INET6;
814 #endif /* INET6 */
815 	tp = intotcpcb(inp);
816 	TCPDEBUG1();
817 	if (control) {
818 		/* TCP doesn't do control messages (rights, creds, etc) */
819 		if (control->m_len) {
820 			m_freem(control);
821 			if (m)
822 				m_freem(m);
823 			error = EINVAL;
824 			goto out;
825 		}
826 		m_freem(control);	/* empty control, just free it */
827 	}
828 	if (!(flags & PRUS_OOB)) {
829 		sbappendstream(&so->so_snd, m);
830 		if (nam && tp->t_state < TCPS_SYN_SENT) {
831 			/*
832 			 * Do implied connect if not yet connected,
833 			 * initialize window to default value, and
834 			 * initialize maxseg/maxopd using peer's cached
835 			 * MSS.
836 			 */
837 #ifdef INET6
838 			if (isipv6)
839 				error = tcp6_connect(tp, nam, td);
840 #endif /* INET6 */
841 #if defined(INET6) && defined(INET)
842 			else
843 #endif
844 #ifdef INET
845 				error = tcp_connect(tp, nam, td);
846 #endif
847 			if (error)
848 				goto out;
849 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
850 			tcp_mss(tp, -1);
851 		}
852 		if (flags & PRUS_EOF) {
853 			/*
854 			 * Close the send side of the connection after
855 			 * the data is sent.
856 			 */
857 			INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
858 			socantsendmore(so);
859 			tcp_usrclosed(tp);
860 		}
861 		if (!(inp->inp_flags & INP_DROPPED)) {
862 			if (flags & PRUS_MORETOCOME)
863 				tp->t_flags |= TF_MORETOCOME;
864 			error = tcp_output(tp);
865 			if (flags & PRUS_MORETOCOME)
866 				tp->t_flags &= ~TF_MORETOCOME;
867 		}
868 	} else {
869 		/*
870 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
871 		 */
872 		SOCKBUF_LOCK(&so->so_snd);
873 		if (sbspace(&so->so_snd) < -512) {
874 			SOCKBUF_UNLOCK(&so->so_snd);
875 			m_freem(m);
876 			error = ENOBUFS;
877 			goto out;
878 		}
879 		/*
880 		 * According to RFC961 (Assigned Protocols),
881 		 * the urgent pointer points to the last octet
882 		 * of urgent data.  We continue, however,
883 		 * to consider it to indicate the first octet
884 		 * of data past the urgent section.
885 		 * Otherwise, snd_up should be one lower.
886 		 */
887 		sbappendstream_locked(&so->so_snd, m);
888 		SOCKBUF_UNLOCK(&so->so_snd);
889 		if (nam && tp->t_state < TCPS_SYN_SENT) {
890 			/*
891 			 * Do implied connect if not yet connected,
892 			 * initialize window to default value, and
893 			 * initialize maxseg/maxopd using peer's cached
894 			 * MSS.
895 			 */
896 #ifdef INET6
897 			if (isipv6)
898 				error = tcp6_connect(tp, nam, td);
899 #endif /* INET6 */
900 #if defined(INET6) && defined(INET)
901 			else
902 #endif
903 #ifdef INET
904 				error = tcp_connect(tp, nam, td);
905 #endif
906 			if (error)
907 				goto out;
908 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
909 			tcp_mss(tp, -1);
910 		}
911 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
912 		tp->t_flags |= TF_FORCEDATA;
913 		error = tcp_output(tp);
914 		tp->t_flags &= ~TF_FORCEDATA;
915 	}
916 out:
917 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
918 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
919 	INP_WUNLOCK(inp);
920 	if (flags & PRUS_EOF)
921 		INP_INFO_WUNLOCK(&V_tcbinfo);
922 	return (error);
923 }
924 
925 /*
926  * Abort the TCP.  Drop the connection abruptly.
927  */
928 static void
929 tcp_usr_abort(struct socket *so)
930 {
931 	struct inpcb *inp;
932 	struct tcpcb *tp = NULL;
933 	TCPDEBUG0;
934 
935 	inp = sotoinpcb(so);
936 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
937 
938 	INP_INFO_WLOCK(&V_tcbinfo);
939 	INP_WLOCK(inp);
940 	KASSERT(inp->inp_socket != NULL,
941 	    ("tcp_usr_abort: inp_socket == NULL"));
942 
943 	/*
944 	 * If we still have full TCP state, and we're not dropped, drop.
945 	 */
946 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
947 	    !(inp->inp_flags & INP_DROPPED)) {
948 		tp = intotcpcb(inp);
949 		TCPDEBUG1();
950 		tcp_drop(tp, ECONNABORTED);
951 		TCPDEBUG2(PRU_ABORT);
952 	}
953 	if (!(inp->inp_flags & INP_DROPPED)) {
954 		SOCK_LOCK(so);
955 		so->so_state |= SS_PROTOREF;
956 		SOCK_UNLOCK(so);
957 		inp->inp_flags |= INP_SOCKREF;
958 	}
959 	INP_WUNLOCK(inp);
960 	INP_INFO_WUNLOCK(&V_tcbinfo);
961 }
962 
963 /*
964  * TCP socket is closed.  Start friendly disconnect.
965  */
966 static void
967 tcp_usr_close(struct socket *so)
968 {
969 	struct inpcb *inp;
970 	struct tcpcb *tp = NULL;
971 	TCPDEBUG0;
972 
973 	inp = sotoinpcb(so);
974 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
975 
976 	INP_INFO_WLOCK(&V_tcbinfo);
977 	INP_WLOCK(inp);
978 	KASSERT(inp->inp_socket != NULL,
979 	    ("tcp_usr_close: inp_socket == NULL"));
980 
981 	/*
982 	 * If we still have full TCP state, and we're not dropped, initiate
983 	 * a disconnect.
984 	 */
985 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
986 	    !(inp->inp_flags & INP_DROPPED)) {
987 		tp = intotcpcb(inp);
988 		TCPDEBUG1();
989 		tcp_disconnect(tp);
990 		TCPDEBUG2(PRU_CLOSE);
991 	}
992 	if (!(inp->inp_flags & INP_DROPPED)) {
993 		SOCK_LOCK(so);
994 		so->so_state |= SS_PROTOREF;
995 		SOCK_UNLOCK(so);
996 		inp->inp_flags |= INP_SOCKREF;
997 	}
998 	INP_WUNLOCK(inp);
999 	INP_INFO_WUNLOCK(&V_tcbinfo);
1000 }
1001 
1002 /*
1003  * Receive out-of-band data.
1004  */
1005 static int
1006 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1007 {
1008 	int error = 0;
1009 	struct inpcb *inp;
1010 	struct tcpcb *tp = NULL;
1011 
1012 	TCPDEBUG0;
1013 	inp = sotoinpcb(so);
1014 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1015 	INP_WLOCK(inp);
1016 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1017 		error = ECONNRESET;
1018 		goto out;
1019 	}
1020 	tp = intotcpcb(inp);
1021 	TCPDEBUG1();
1022 	if ((so->so_oobmark == 0 &&
1023 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1024 	    so->so_options & SO_OOBINLINE ||
1025 	    tp->t_oobflags & TCPOOB_HADDATA) {
1026 		error = EINVAL;
1027 		goto out;
1028 	}
1029 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1030 		error = EWOULDBLOCK;
1031 		goto out;
1032 	}
1033 	m->m_len = 1;
1034 	*mtod(m, caddr_t) = tp->t_iobc;
1035 	if ((flags & MSG_PEEK) == 0)
1036 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1037 
1038 out:
1039 	TCPDEBUG2(PRU_RCVOOB);
1040 	INP_WUNLOCK(inp);
1041 	return (error);
1042 }
1043 
1044 #ifdef INET
1045 struct pr_usrreqs tcp_usrreqs = {
1046 	.pru_abort =		tcp_usr_abort,
1047 	.pru_accept =		tcp_usr_accept,
1048 	.pru_attach =		tcp_usr_attach,
1049 	.pru_bind =		tcp_usr_bind,
1050 	.pru_connect =		tcp_usr_connect,
1051 	.pru_control =		in_control,
1052 	.pru_detach =		tcp_usr_detach,
1053 	.pru_disconnect =	tcp_usr_disconnect,
1054 	.pru_listen =		tcp_usr_listen,
1055 	.pru_peeraddr =		in_getpeeraddr,
1056 	.pru_rcvd =		tcp_usr_rcvd,
1057 	.pru_rcvoob =		tcp_usr_rcvoob,
1058 	.pru_send =		tcp_usr_send,
1059 	.pru_shutdown =		tcp_usr_shutdown,
1060 	.pru_sockaddr =		in_getsockaddr,
1061 	.pru_sosetlabel =	in_pcbsosetlabel,
1062 	.pru_close =		tcp_usr_close,
1063 };
1064 #endif /* INET */
1065 
1066 #ifdef INET6
1067 struct pr_usrreqs tcp6_usrreqs = {
1068 	.pru_abort =		tcp_usr_abort,
1069 	.pru_accept =		tcp6_usr_accept,
1070 	.pru_attach =		tcp_usr_attach,
1071 	.pru_bind =		tcp6_usr_bind,
1072 	.pru_connect =		tcp6_usr_connect,
1073 	.pru_control =		in6_control,
1074 	.pru_detach =		tcp_usr_detach,
1075 	.pru_disconnect =	tcp_usr_disconnect,
1076 	.pru_listen =		tcp6_usr_listen,
1077 	.pru_peeraddr =		in6_mapped_peeraddr,
1078 	.pru_rcvd =		tcp_usr_rcvd,
1079 	.pru_rcvoob =		tcp_usr_rcvoob,
1080 	.pru_send =		tcp_usr_send,
1081 	.pru_shutdown =		tcp_usr_shutdown,
1082 	.pru_sockaddr =		in6_mapped_sockaddr,
1083 	.pru_sosetlabel =	in_pcbsosetlabel,
1084 	.pru_close =		tcp_usr_close,
1085 };
1086 #endif /* INET6 */
1087 
1088 #ifdef INET
1089 /*
1090  * Common subroutine to open a TCP connection to remote host specified
1091  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
1092  * port number if needed.  Call in_pcbconnect_setup to do the routing and
1093  * to choose a local host address (interface).  If there is an existing
1094  * incarnation of the same connection in TIME-WAIT state and if the remote
1095  * host was sending CC options and if the connection duration was < MSL, then
1096  * truncate the previous TIME-WAIT state and proceed.
1097  * Initialize connection parameters and enter SYN-SENT state.
1098  */
1099 static int
1100 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1101 {
1102 	struct inpcb *inp = tp->t_inpcb, *oinp;
1103 	struct socket *so = inp->inp_socket;
1104 	struct in_addr laddr;
1105 	u_short lport;
1106 	int error;
1107 
1108 	INP_WLOCK_ASSERT(inp);
1109 	INP_HASH_WLOCK(&V_tcbinfo);
1110 
1111 	if (inp->inp_lport == 0) {
1112 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1113 		if (error)
1114 			goto out;
1115 	}
1116 
1117 	/*
1118 	 * Cannot simply call in_pcbconnect, because there might be an
1119 	 * earlier incarnation of this same connection still in
1120 	 * TIME_WAIT state, creating an ADDRINUSE error.
1121 	 */
1122 	laddr = inp->inp_laddr;
1123 	lport = inp->inp_lport;
1124 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1125 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1126 	if (error && oinp == NULL)
1127 		goto out;
1128 	if (oinp) {
1129 		error = EADDRINUSE;
1130 		goto out;
1131 	}
1132 	inp->inp_laddr = laddr;
1133 	in_pcbrehash(inp);
1134 	INP_HASH_WUNLOCK(&V_tcbinfo);
1135 
1136 	/*
1137 	 * Compute window scaling to request:
1138 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1139 	 * XXX: This should move to tcp_output().
1140 	 */
1141 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1142 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1143 		tp->request_r_scale++;
1144 
1145 	soisconnecting(so);
1146 	TCPSTAT_INC(tcps_connattempt);
1147 	tcp_state_change(tp, TCPS_SYN_SENT);
1148 	tp->iss = tcp_new_isn(tp);
1149 	tcp_sendseqinit(tp);
1150 
1151 	return 0;
1152 
1153 out:
1154 	INP_HASH_WUNLOCK(&V_tcbinfo);
1155 	return (error);
1156 }
1157 #endif /* INET */
1158 
1159 #ifdef INET6
1160 static int
1161 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1162 {
1163 	struct inpcb *inp = tp->t_inpcb, *oinp;
1164 	struct socket *so = inp->inp_socket;
1165 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1166 	struct in6_addr addr6;
1167 	int error;
1168 
1169 	INP_WLOCK_ASSERT(inp);
1170 	INP_HASH_WLOCK(&V_tcbinfo);
1171 
1172 	if (inp->inp_lport == 0) {
1173 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1174 		if (error)
1175 			goto out;
1176 	}
1177 
1178 	/*
1179 	 * Cannot simply call in_pcbconnect, because there might be an
1180 	 * earlier incarnation of this same connection still in
1181 	 * TIME_WAIT state, creating an ADDRINUSE error.
1182 	 * in6_pcbladdr() also handles scope zone IDs.
1183 	 *
1184 	 * XXXRW: We wouldn't need to expose in6_pcblookup_hash_locked()
1185 	 * outside of in6_pcb.c if there were an in6_pcbconnect_setup().
1186 	 */
1187 	error = in6_pcbladdr(inp, nam, &addr6);
1188 	if (error)
1189 		goto out;
1190 	oinp = in6_pcblookup_hash_locked(inp->inp_pcbinfo,
1191 				  &sin6->sin6_addr, sin6->sin6_port,
1192 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1193 				  ? &addr6
1194 				  : &inp->in6p_laddr,
1195 				  inp->inp_lport,  0, NULL);
1196 	if (oinp) {
1197 		error = EADDRINUSE;
1198 		goto out;
1199 	}
1200 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1201 		inp->in6p_laddr = addr6;
1202 	inp->in6p_faddr = sin6->sin6_addr;
1203 	inp->inp_fport = sin6->sin6_port;
1204 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
1205 	inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
1206 	if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
1207 		inp->inp_flow |=
1208 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1209 	in_pcbrehash(inp);
1210 	INP_HASH_WUNLOCK(&V_tcbinfo);
1211 
1212 	/* Compute window scaling to request.  */
1213 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1214 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1215 		tp->request_r_scale++;
1216 
1217 	soisconnecting(so);
1218 	TCPSTAT_INC(tcps_connattempt);
1219 	tcp_state_change(tp, TCPS_SYN_SENT);
1220 	tp->iss = tcp_new_isn(tp);
1221 	tcp_sendseqinit(tp);
1222 
1223 	return 0;
1224 
1225 out:
1226 	INP_HASH_WUNLOCK(&V_tcbinfo);
1227 	return error;
1228 }
1229 #endif /* INET6 */
1230 
1231 /*
1232  * Export TCP internal state information via a struct tcp_info, based on the
1233  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1234  * (TCP state machine, etc).  We export all information using FreeBSD-native
1235  * constants -- for example, the numeric values for tcpi_state will differ
1236  * from Linux.
1237  */
1238 static void
1239 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1240 {
1241 
1242 	INP_WLOCK_ASSERT(tp->t_inpcb);
1243 	bzero(ti, sizeof(*ti));
1244 
1245 	ti->tcpi_state = tp->t_state;
1246 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1247 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1248 	if (tp->t_flags & TF_SACK_PERMIT)
1249 		ti->tcpi_options |= TCPI_OPT_SACK;
1250 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1251 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1252 		ti->tcpi_snd_wscale = tp->snd_scale;
1253 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1254 	}
1255 
1256 	ti->tcpi_rto = tp->t_rxtcur * tick;
1257 	ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
1258 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1259 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1260 
1261 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1262 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1263 
1264 	/*
1265 	 * FreeBSD-specific extension fields for tcp_info.
1266 	 */
1267 	ti->tcpi_rcv_space = tp->rcv_wnd;
1268 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1269 	ti->tcpi_snd_wnd = tp->snd_wnd;
1270 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1271 	ti->tcpi_snd_nxt = tp->snd_nxt;
1272 	ti->tcpi_snd_mss = tp->t_maxseg;
1273 	ti->tcpi_rcv_mss = tp->t_maxseg;
1274 	if (tp->t_flags & TF_TOE)
1275 		ti->tcpi_options |= TCPI_OPT_TOE;
1276 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1277 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1278 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1279 }
1280 
1281 /*
1282  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1283  * socket option arguments.  When it re-acquires the lock after the copy, it
1284  * has to revalidate that the connection is still valid for the socket
1285  * option.
1286  */
1287 #define INP_WLOCK_RECHECK(inp) do {					\
1288 	INP_WLOCK(inp);							\
1289 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
1290 		INP_WUNLOCK(inp);					\
1291 		return (ECONNRESET);					\
1292 	}								\
1293 	tp = intotcpcb(inp);						\
1294 } while(0)
1295 
1296 int
1297 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1298 {
1299 	int	error, opt, optval;
1300 	u_int	ui;
1301 	struct	inpcb *inp;
1302 	struct	tcpcb *tp;
1303 	struct	tcp_info ti;
1304 	char buf[TCP_CA_NAME_MAX];
1305 	struct cc_algo *algo;
1306 
1307 	error = 0;
1308 	inp = sotoinpcb(so);
1309 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1310 	INP_WLOCK(inp);
1311 	if (sopt->sopt_level != IPPROTO_TCP) {
1312 #ifdef INET6
1313 		if (inp->inp_vflag & INP_IPV6PROTO) {
1314 			INP_WUNLOCK(inp);
1315 			error = ip6_ctloutput(so, sopt);
1316 		}
1317 #endif /* INET6 */
1318 #if defined(INET6) && defined(INET)
1319 		else
1320 #endif
1321 #ifdef INET
1322 		{
1323 			INP_WUNLOCK(inp);
1324 			error = ip_ctloutput(so, sopt);
1325 		}
1326 #endif
1327 		return (error);
1328 	}
1329 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1330 		INP_WUNLOCK(inp);
1331 		return (ECONNRESET);
1332 	}
1333 
1334 	switch (sopt->sopt_dir) {
1335 	case SOPT_SET:
1336 		switch (sopt->sopt_name) {
1337 #ifdef TCP_SIGNATURE
1338 		case TCP_MD5SIG:
1339 			INP_WUNLOCK(inp);
1340 			error = sooptcopyin(sopt, &optval, sizeof optval,
1341 			    sizeof optval);
1342 			if (error)
1343 				return (error);
1344 
1345 			INP_WLOCK_RECHECK(inp);
1346 			if (optval > 0)
1347 				tp->t_flags |= TF_SIGNATURE;
1348 			else
1349 				tp->t_flags &= ~TF_SIGNATURE;
1350 			goto unlock_and_done;
1351 #endif /* TCP_SIGNATURE */
1352 
1353 		case TCP_NODELAY:
1354 		case TCP_NOOPT:
1355 			INP_WUNLOCK(inp);
1356 			error = sooptcopyin(sopt, &optval, sizeof optval,
1357 			    sizeof optval);
1358 			if (error)
1359 				return (error);
1360 
1361 			INP_WLOCK_RECHECK(inp);
1362 			switch (sopt->sopt_name) {
1363 			case TCP_NODELAY:
1364 				opt = TF_NODELAY;
1365 				break;
1366 			case TCP_NOOPT:
1367 				opt = TF_NOOPT;
1368 				break;
1369 			default:
1370 				opt = 0; /* dead code to fool gcc */
1371 				break;
1372 			}
1373 
1374 			if (optval)
1375 				tp->t_flags |= opt;
1376 			else
1377 				tp->t_flags &= ~opt;
1378 unlock_and_done:
1379 #ifdef TCP_OFFLOAD
1380 			if (tp->t_flags & TF_TOE) {
1381 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
1382 				    sopt->sopt_name);
1383 			}
1384 #endif
1385 			INP_WUNLOCK(inp);
1386 			break;
1387 
1388 		case TCP_NOPUSH:
1389 			INP_WUNLOCK(inp);
1390 			error = sooptcopyin(sopt, &optval, sizeof optval,
1391 			    sizeof optval);
1392 			if (error)
1393 				return (error);
1394 
1395 			INP_WLOCK_RECHECK(inp);
1396 			if (optval)
1397 				tp->t_flags |= TF_NOPUSH;
1398 			else if (tp->t_flags & TF_NOPUSH) {
1399 				tp->t_flags &= ~TF_NOPUSH;
1400 				if (TCPS_HAVEESTABLISHED(tp->t_state))
1401 					error = tcp_output(tp);
1402 			}
1403 			goto unlock_and_done;
1404 
1405 		case TCP_MAXSEG:
1406 			INP_WUNLOCK(inp);
1407 			error = sooptcopyin(sopt, &optval, sizeof optval,
1408 			    sizeof optval);
1409 			if (error)
1410 				return (error);
1411 
1412 			INP_WLOCK_RECHECK(inp);
1413 			if (optval > 0 && optval <= tp->t_maxseg &&
1414 			    optval + 40 >= V_tcp_minmss)
1415 				tp->t_maxseg = optval;
1416 			else
1417 				error = EINVAL;
1418 			goto unlock_and_done;
1419 
1420 		case TCP_INFO:
1421 			INP_WUNLOCK(inp);
1422 			error = EINVAL;
1423 			break;
1424 
1425 		case TCP_CONGESTION:
1426 			INP_WUNLOCK(inp);
1427 			bzero(buf, sizeof(buf));
1428 			error = sooptcopyin(sopt, &buf, sizeof(buf), 1);
1429 			if (error)
1430 				break;
1431 			INP_WLOCK_RECHECK(inp);
1432 			/*
1433 			 * Return EINVAL if we can't find the requested cc algo.
1434 			 */
1435 			error = EINVAL;
1436 			CC_LIST_RLOCK();
1437 			STAILQ_FOREACH(algo, &cc_list, entries) {
1438 				if (strncmp(buf, algo->name, TCP_CA_NAME_MAX)
1439 				    == 0) {
1440 					/* We've found the requested algo. */
1441 					error = 0;
1442 					/*
1443 					 * We hold a write lock over the tcb
1444 					 * so it's safe to do these things
1445 					 * without ordering concerns.
1446 					 */
1447 					if (CC_ALGO(tp)->cb_destroy != NULL)
1448 						CC_ALGO(tp)->cb_destroy(tp->ccv);
1449 					CC_ALGO(tp) = algo;
1450 					/*
1451 					 * If something goes pear shaped
1452 					 * initialising the new algo,
1453 					 * fall back to newreno (which
1454 					 * does not require initialisation).
1455 					 */
1456 					if (algo->cb_init != NULL)
1457 						if (algo->cb_init(tp->ccv) > 0) {
1458 							CC_ALGO(tp) = &newreno_cc_algo;
1459 							/*
1460 							 * The only reason init
1461 							 * should fail is
1462 							 * because of malloc.
1463 							 */
1464 							error = ENOMEM;
1465 						}
1466 					break; /* Break the STAILQ_FOREACH. */
1467 				}
1468 			}
1469 			CC_LIST_RUNLOCK();
1470 			goto unlock_and_done;
1471 
1472 		case TCP_KEEPIDLE:
1473 		case TCP_KEEPINTVL:
1474 		case TCP_KEEPINIT:
1475 			INP_WUNLOCK(inp);
1476 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1477 			if (error)
1478 				return (error);
1479 
1480 			if (ui > (UINT_MAX / hz)) {
1481 				error = EINVAL;
1482 				break;
1483 			}
1484 			ui *= hz;
1485 
1486 			INP_WLOCK_RECHECK(inp);
1487 			switch (sopt->sopt_name) {
1488 			case TCP_KEEPIDLE:
1489 				tp->t_keepidle = ui;
1490 				/*
1491 				 * XXX: better check current remaining
1492 				 * timeout and "merge" it with new value.
1493 				 */
1494 				if ((tp->t_state > TCPS_LISTEN) &&
1495 				    (tp->t_state <= TCPS_CLOSING))
1496 					tcp_timer_activate(tp, TT_KEEP,
1497 					    TP_KEEPIDLE(tp));
1498 				break;
1499 			case TCP_KEEPINTVL:
1500 				tp->t_keepintvl = ui;
1501 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1502 				    (TP_MAXIDLE(tp) > 0))
1503 					tcp_timer_activate(tp, TT_2MSL,
1504 					    TP_MAXIDLE(tp));
1505 				break;
1506 			case TCP_KEEPINIT:
1507 				tp->t_keepinit = ui;
1508 				if (tp->t_state == TCPS_SYN_RECEIVED ||
1509 				    tp->t_state == TCPS_SYN_SENT)
1510 					tcp_timer_activate(tp, TT_KEEP,
1511 					    TP_KEEPINIT(tp));
1512 				break;
1513 			}
1514 			goto unlock_and_done;
1515 
1516 		case TCP_KEEPCNT:
1517 			INP_WUNLOCK(inp);
1518 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1519 			if (error)
1520 				return (error);
1521 
1522 			INP_WLOCK_RECHECK(inp);
1523 			tp->t_keepcnt = ui;
1524 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1525 			    (TP_MAXIDLE(tp) > 0))
1526 				tcp_timer_activate(tp, TT_2MSL,
1527 				    TP_MAXIDLE(tp));
1528 			goto unlock_and_done;
1529 
1530 		default:
1531 			INP_WUNLOCK(inp);
1532 			error = ENOPROTOOPT;
1533 			break;
1534 		}
1535 		break;
1536 
1537 	case SOPT_GET:
1538 		tp = intotcpcb(inp);
1539 		switch (sopt->sopt_name) {
1540 #ifdef TCP_SIGNATURE
1541 		case TCP_MD5SIG:
1542 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1543 			INP_WUNLOCK(inp);
1544 			error = sooptcopyout(sopt, &optval, sizeof optval);
1545 			break;
1546 #endif
1547 
1548 		case TCP_NODELAY:
1549 			optval = tp->t_flags & TF_NODELAY;
1550 			INP_WUNLOCK(inp);
1551 			error = sooptcopyout(sopt, &optval, sizeof optval);
1552 			break;
1553 		case TCP_MAXSEG:
1554 			optval = tp->t_maxseg;
1555 			INP_WUNLOCK(inp);
1556 			error = sooptcopyout(sopt, &optval, sizeof optval);
1557 			break;
1558 		case TCP_NOOPT:
1559 			optval = tp->t_flags & TF_NOOPT;
1560 			INP_WUNLOCK(inp);
1561 			error = sooptcopyout(sopt, &optval, sizeof optval);
1562 			break;
1563 		case TCP_NOPUSH:
1564 			optval = tp->t_flags & TF_NOPUSH;
1565 			INP_WUNLOCK(inp);
1566 			error = sooptcopyout(sopt, &optval, sizeof optval);
1567 			break;
1568 		case TCP_INFO:
1569 			tcp_fill_info(tp, &ti);
1570 			INP_WUNLOCK(inp);
1571 			error = sooptcopyout(sopt, &ti, sizeof ti);
1572 			break;
1573 		case TCP_CONGESTION:
1574 			bzero(buf, sizeof(buf));
1575 			strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1576 			INP_WUNLOCK(inp);
1577 			error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX);
1578 			break;
1579 		case TCP_KEEPIDLE:
1580 		case TCP_KEEPINTVL:
1581 		case TCP_KEEPINIT:
1582 		case TCP_KEEPCNT:
1583 			switch (sopt->sopt_name) {
1584 			case TCP_KEEPIDLE:
1585 				ui = tp->t_keepidle / hz;
1586 				break;
1587 			case TCP_KEEPINTVL:
1588 				ui = tp->t_keepintvl / hz;
1589 				break;
1590 			case TCP_KEEPINIT:
1591 				ui = tp->t_keepinit / hz;
1592 				break;
1593 			case TCP_KEEPCNT:
1594 				ui = tp->t_keepcnt;
1595 				break;
1596 			}
1597 			INP_WUNLOCK(inp);
1598 			error = sooptcopyout(sopt, &ui, sizeof(ui));
1599 			break;
1600 		default:
1601 			INP_WUNLOCK(inp);
1602 			error = ENOPROTOOPT;
1603 			break;
1604 		}
1605 		break;
1606 	}
1607 	return (error);
1608 }
1609 #undef INP_WLOCK_RECHECK
1610 
1611 /*
1612  * Attach TCP protocol to socket, allocating
1613  * internet protocol control block, tcp control block,
1614  * bufer space, and entering LISTEN state if to accept connections.
1615  */
1616 static int
1617 tcp_attach(struct socket *so)
1618 {
1619 	struct tcpcb *tp;
1620 	struct inpcb *inp;
1621 	int error;
1622 
1623 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1624 		error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1625 		if (error)
1626 			return (error);
1627 	}
1628 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
1629 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1630 	INP_INFO_WLOCK(&V_tcbinfo);
1631 	error = in_pcballoc(so, &V_tcbinfo);
1632 	if (error) {
1633 		INP_INFO_WUNLOCK(&V_tcbinfo);
1634 		return (error);
1635 	}
1636 	inp = sotoinpcb(so);
1637 #ifdef INET6
1638 	if (inp->inp_vflag & INP_IPV6PROTO) {
1639 		inp->inp_vflag |= INP_IPV6;
1640 		inp->in6p_hops = -1;	/* use kernel default */
1641 	}
1642 	else
1643 #endif
1644 	inp->inp_vflag |= INP_IPV4;
1645 	tp = tcp_newtcpcb(inp);
1646 	if (tp == NULL) {
1647 		in_pcbdetach(inp);
1648 		in_pcbfree(inp);
1649 		INP_INFO_WUNLOCK(&V_tcbinfo);
1650 		return (ENOBUFS);
1651 	}
1652 	tp->t_state = TCPS_CLOSED;
1653 	INP_WUNLOCK(inp);
1654 	INP_INFO_WUNLOCK(&V_tcbinfo);
1655 	return (0);
1656 }
1657 
1658 /*
1659  * Initiate (or continue) disconnect.
1660  * If embryonic state, just send reset (once).
1661  * If in ``let data drain'' option and linger null, just drop.
1662  * Otherwise (hard), mark socket disconnecting and drop
1663  * current input data; switch states based on user close, and
1664  * send segment to peer (with FIN).
1665  */
1666 static void
1667 tcp_disconnect(struct tcpcb *tp)
1668 {
1669 	struct inpcb *inp = tp->t_inpcb;
1670 	struct socket *so = inp->inp_socket;
1671 
1672 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1673 	INP_WLOCK_ASSERT(inp);
1674 
1675 	/*
1676 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1677 	 * socket is still open.
1678 	 */
1679 	if (tp->t_state < TCPS_ESTABLISHED) {
1680 		tp = tcp_close(tp);
1681 		KASSERT(tp != NULL,
1682 		    ("tcp_disconnect: tcp_close() returned NULL"));
1683 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1684 		tp = tcp_drop(tp, 0);
1685 		KASSERT(tp != NULL,
1686 		    ("tcp_disconnect: tcp_drop() returned NULL"));
1687 	} else {
1688 		soisdisconnecting(so);
1689 		sbflush(&so->so_rcv);
1690 		tcp_usrclosed(tp);
1691 		if (!(inp->inp_flags & INP_DROPPED))
1692 			tcp_output(tp);
1693 	}
1694 }
1695 
1696 /*
1697  * User issued close, and wish to trail through shutdown states:
1698  * if never received SYN, just forget it.  If got a SYN from peer,
1699  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1700  * If already got a FIN from peer, then almost done; go to LAST_ACK
1701  * state.  In all other cases, have already sent FIN to peer (e.g.
1702  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1703  * for peer to send FIN or not respond to keep-alives, etc.
1704  * We can let the user exit from the close as soon as the FIN is acked.
1705  */
1706 static void
1707 tcp_usrclosed(struct tcpcb *tp)
1708 {
1709 
1710 	INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1711 	INP_WLOCK_ASSERT(tp->t_inpcb);
1712 
1713 	switch (tp->t_state) {
1714 	case TCPS_LISTEN:
1715 #ifdef TCP_OFFLOAD
1716 		tcp_offload_listen_stop(tp);
1717 #endif
1718 		/* FALLTHROUGH */
1719 	case TCPS_CLOSED:
1720 		tcp_state_change(tp, TCPS_CLOSED);
1721 		tp = tcp_close(tp);
1722 		/*
1723 		 * tcp_close() should never return NULL here as the socket is
1724 		 * still open.
1725 		 */
1726 		KASSERT(tp != NULL,
1727 		    ("tcp_usrclosed: tcp_close() returned NULL"));
1728 		break;
1729 
1730 	case TCPS_SYN_SENT:
1731 	case TCPS_SYN_RECEIVED:
1732 		tp->t_flags |= TF_NEEDFIN;
1733 		break;
1734 
1735 	case TCPS_ESTABLISHED:
1736 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
1737 		break;
1738 
1739 	case TCPS_CLOSE_WAIT:
1740 		tcp_state_change(tp, TCPS_LAST_ACK);
1741 		break;
1742 	}
1743 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
1744 		soisdisconnected(tp->t_inpcb->inp_socket);
1745 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
1746 		if (tp->t_state == TCPS_FIN_WAIT_2) {
1747 			int timeout;
1748 
1749 			timeout = (tcp_fast_finwait2_recycle) ?
1750 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
1751 			tcp_timer_activate(tp, TT_2MSL, timeout);
1752 		}
1753 	}
1754 }
1755 
1756 #ifdef DDB
1757 static void
1758 db_print_indent(int indent)
1759 {
1760 	int i;
1761 
1762 	for (i = 0; i < indent; i++)
1763 		db_printf(" ");
1764 }
1765 
1766 static void
1767 db_print_tstate(int t_state)
1768 {
1769 
1770 	switch (t_state) {
1771 	case TCPS_CLOSED:
1772 		db_printf("TCPS_CLOSED");
1773 		return;
1774 
1775 	case TCPS_LISTEN:
1776 		db_printf("TCPS_LISTEN");
1777 		return;
1778 
1779 	case TCPS_SYN_SENT:
1780 		db_printf("TCPS_SYN_SENT");
1781 		return;
1782 
1783 	case TCPS_SYN_RECEIVED:
1784 		db_printf("TCPS_SYN_RECEIVED");
1785 		return;
1786 
1787 	case TCPS_ESTABLISHED:
1788 		db_printf("TCPS_ESTABLISHED");
1789 		return;
1790 
1791 	case TCPS_CLOSE_WAIT:
1792 		db_printf("TCPS_CLOSE_WAIT");
1793 		return;
1794 
1795 	case TCPS_FIN_WAIT_1:
1796 		db_printf("TCPS_FIN_WAIT_1");
1797 		return;
1798 
1799 	case TCPS_CLOSING:
1800 		db_printf("TCPS_CLOSING");
1801 		return;
1802 
1803 	case TCPS_LAST_ACK:
1804 		db_printf("TCPS_LAST_ACK");
1805 		return;
1806 
1807 	case TCPS_FIN_WAIT_2:
1808 		db_printf("TCPS_FIN_WAIT_2");
1809 		return;
1810 
1811 	case TCPS_TIME_WAIT:
1812 		db_printf("TCPS_TIME_WAIT");
1813 		return;
1814 
1815 	default:
1816 		db_printf("unknown");
1817 		return;
1818 	}
1819 }
1820 
1821 static void
1822 db_print_tflags(u_int t_flags)
1823 {
1824 	int comma;
1825 
1826 	comma = 0;
1827 	if (t_flags & TF_ACKNOW) {
1828 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
1829 		comma = 1;
1830 	}
1831 	if (t_flags & TF_DELACK) {
1832 		db_printf("%sTF_DELACK", comma ? ", " : "");
1833 		comma = 1;
1834 	}
1835 	if (t_flags & TF_NODELAY) {
1836 		db_printf("%sTF_NODELAY", comma ? ", " : "");
1837 		comma = 1;
1838 	}
1839 	if (t_flags & TF_NOOPT) {
1840 		db_printf("%sTF_NOOPT", comma ? ", " : "");
1841 		comma = 1;
1842 	}
1843 	if (t_flags & TF_SENTFIN) {
1844 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
1845 		comma = 1;
1846 	}
1847 	if (t_flags & TF_REQ_SCALE) {
1848 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1849 		comma = 1;
1850 	}
1851 	if (t_flags & TF_RCVD_SCALE) {
1852 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1853 		comma = 1;
1854 	}
1855 	if (t_flags & TF_REQ_TSTMP) {
1856 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1857 		comma = 1;
1858 	}
1859 	if (t_flags & TF_RCVD_TSTMP) {
1860 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1861 		comma = 1;
1862 	}
1863 	if (t_flags & TF_SACK_PERMIT) {
1864 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1865 		comma = 1;
1866 	}
1867 	if (t_flags & TF_NEEDSYN) {
1868 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1869 		comma = 1;
1870 	}
1871 	if (t_flags & TF_NEEDFIN) {
1872 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1873 		comma = 1;
1874 	}
1875 	if (t_flags & TF_NOPUSH) {
1876 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
1877 		comma = 1;
1878 	}
1879 	if (t_flags & TF_MORETOCOME) {
1880 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1881 		comma = 1;
1882 	}
1883 	if (t_flags & TF_LQ_OVERFLOW) {
1884 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1885 		comma = 1;
1886 	}
1887 	if (t_flags & TF_LASTIDLE) {
1888 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1889 		comma = 1;
1890 	}
1891 	if (t_flags & TF_RXWIN0SENT) {
1892 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1893 		comma = 1;
1894 	}
1895 	if (t_flags & TF_FASTRECOVERY) {
1896 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1897 		comma = 1;
1898 	}
1899 	if (t_flags & TF_CONGRECOVERY) {
1900 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
1901 		comma = 1;
1902 	}
1903 	if (t_flags & TF_WASFRECOVERY) {
1904 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1905 		comma = 1;
1906 	}
1907 	if (t_flags & TF_SIGNATURE) {
1908 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1909 		comma = 1;
1910 	}
1911 	if (t_flags & TF_FORCEDATA) {
1912 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1913 		comma = 1;
1914 	}
1915 	if (t_flags & TF_TSO) {
1916 		db_printf("%sTF_TSO", comma ? ", " : "");
1917 		comma = 1;
1918 	}
1919 	if (t_flags & TF_ECN_PERMIT) {
1920 		db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
1921 		comma = 1;
1922 	}
1923 }
1924 
1925 static void
1926 db_print_toobflags(char t_oobflags)
1927 {
1928 	int comma;
1929 
1930 	comma = 0;
1931 	if (t_oobflags & TCPOOB_HAVEDATA) {
1932 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1933 		comma = 1;
1934 	}
1935 	if (t_oobflags & TCPOOB_HADDATA) {
1936 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1937 		comma = 1;
1938 	}
1939 }
1940 
1941 static void
1942 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1943 {
1944 
1945 	db_print_indent(indent);
1946 	db_printf("%s at %p\n", name, tp);
1947 
1948 	indent += 2;
1949 
1950 	db_print_indent(indent);
1951 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
1952 	   tp->t_segq, tp->t_segqlen, tp->t_dupacks);
1953 
1954 	db_print_indent(indent);
1955 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
1956 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
1957 
1958 	db_print_indent(indent);
1959 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
1960 	    &tp->t_timers->tt_delack, tp->t_inpcb);
1961 
1962 	db_print_indent(indent);
1963 	db_printf("t_state: %d (", tp->t_state);
1964 	db_print_tstate(tp->t_state);
1965 	db_printf(")\n");
1966 
1967 	db_print_indent(indent);
1968 	db_printf("t_flags: 0x%x (", tp->t_flags);
1969 	db_print_tflags(tp->t_flags);
1970 	db_printf(")\n");
1971 
1972 	db_print_indent(indent);
1973 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
1974 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
1975 
1976 	db_print_indent(indent);
1977 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
1978 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
1979 
1980 	db_print_indent(indent);
1981 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
1982 	    tp->iss, tp->irs, tp->rcv_nxt);
1983 
1984 	db_print_indent(indent);
1985 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %lu   rcv_up: 0x%08x\n",
1986 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
1987 
1988 	db_print_indent(indent);
1989 	db_printf("snd_wnd: %lu   snd_cwnd: %lu\n",
1990 	   tp->snd_wnd, tp->snd_cwnd);
1991 
1992 	db_print_indent(indent);
1993 	db_printf("snd_ssthresh: %lu   snd_recover: "
1994 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
1995 
1996 	db_print_indent(indent);
1997 	db_printf("t_maxopd: %u   t_rcvtime: %u   t_startime: %u\n",
1998 	    tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
1999 
2000 	db_print_indent(indent);
2001 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
2002 	    tp->t_rtttime, tp->t_rtseq);
2003 
2004 	db_print_indent(indent);
2005 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
2006 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2007 
2008 	db_print_indent(indent);
2009 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
2010 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2011 	    tp->t_rttbest);
2012 
2013 	db_print_indent(indent);
2014 	db_printf("t_rttupdated: %lu   max_sndwnd: %lu   t_softerror: %d\n",
2015 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2016 
2017 	db_print_indent(indent);
2018 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2019 	db_print_toobflags(tp->t_oobflags);
2020 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
2021 
2022 	db_print_indent(indent);
2023 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
2024 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2025 
2026 	db_print_indent(indent);
2027 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
2028 	    tp->ts_recent, tp->ts_recent_age);
2029 
2030 	db_print_indent(indent);
2031 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
2032 	    "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2033 
2034 	db_print_indent(indent);
2035 	db_printf("snd_ssthresh_prev: %lu   snd_recover_prev: 0x%08x   "
2036 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2037 	    tp->snd_recover_prev, tp->t_badrxtwin);
2038 
2039 	db_print_indent(indent);
2040 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
2041 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2042 
2043 	db_print_indent(indent);
2044 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
2045 	    "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2046 
2047 	/* Skip sackblks, sackhint. */
2048 
2049 	db_print_indent(indent);
2050 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
2051 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2052 }
2053 
2054 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2055 {
2056 	struct tcpcb *tp;
2057 
2058 	if (!have_addr) {
2059 		db_printf("usage: show tcpcb <addr>\n");
2060 		return;
2061 	}
2062 	tp = (struct tcpcb *)addr;
2063 
2064 	db_print_tcpcb(tp, "tcpcb", 0);
2065 }
2066 #endif
2067