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