xref: /f-stack/freebsd/netinet/tcp_usrreq.c (revision 22ce4aff)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1988, 1993
5  *	The Regents of the University of California.
6  * Copyright (c) 2006-2007 Robert N. M. Watson
7  * Copyright (c) 2010-2011 Juniper Networks, Inc.
8  * All rights reserved.
9  *
10  * Portions of this software were developed by Robert N. M. Watson under
11  * contract to Juniper Networks, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
38  */
39 
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 #include "opt_ddb.h"
44 #include "opt_inet.h"
45 #include "opt_inet6.h"
46 #include "opt_ipsec.h"
47 #include "opt_kern_tls.h"
48 #include "opt_tcpdebug.h"
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/arb.h>
53 #include <sys/limits.h>
54 #include <sys/malloc.h>
55 #include <sys/refcount.h>
56 #include <sys/kernel.h>
57 #include <sys/ktls.h>
58 #include <sys/qmath.h>
59 #include <sys/sysctl.h>
60 #include <sys/mbuf.h>
61 #ifdef INET6
62 #include <sys/domain.h>
63 #endif /* INET6 */
64 #include <sys/socket.h>
65 #include <sys/socketvar.h>
66 #include <sys/protosw.h>
67 #include <sys/proc.h>
68 #include <sys/jail.h>
69 #include <sys/syslog.h>
70 #include <sys/stats.h>
71 
72 #ifdef DDB
73 #include <ddb/ddb.h>
74 #endif
75 
76 #include <net/if.h>
77 #include <net/if_var.h>
78 #include <net/route.h>
79 #include <net/vnet.h>
80 
81 #include <netinet/in.h>
82 #include <netinet/in_kdtrace.h>
83 #include <netinet/in_pcb.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/in_var.h>
86 #include <netinet/ip_var.h>
87 #ifdef INET6
88 #include <netinet/ip6.h>
89 #include <netinet6/in6_pcb.h>
90 #include <netinet6/ip6_var.h>
91 #include <netinet6/scope6_var.h>
92 #endif
93 #include <netinet/tcp.h>
94 #include <netinet/tcp_fsm.h>
95 #include <netinet/tcp_seq.h>
96 #include <netinet/tcp_timer.h>
97 #include <netinet/tcp_var.h>
98 #include <netinet/tcp_log_buf.h>
99 #include <netinet/tcpip.h>
100 #include <netinet/cc/cc.h>
101 #include <netinet/tcp_fastopen.h>
102 #include <netinet/tcp_hpts.h>
103 #ifdef TCPPCAP
104 #include <netinet/tcp_pcap.h>
105 #endif
106 #ifdef TCPDEBUG
107 #include <netinet/tcp_debug.h>
108 #endif
109 #ifdef TCP_OFFLOAD
110 #include <netinet/tcp_offload.h>
111 #endif
112 #include <netipsec/ipsec_support.h>
113 
114 #include <vm/vm.h>
115 #include <vm/vm_param.h>
116 #include <vm/pmap.h>
117 #include <vm/vm_extern.h>
118 #include <vm/vm_map.h>
119 #include <vm/vm_page.h>
120 
121 /*
122  * TCP protocol interface to socket abstraction.
123  */
124 #ifdef INET
125 static int	tcp_connect(struct tcpcb *, struct sockaddr *,
126 		    struct thread *td);
127 #endif /* INET */
128 #ifdef INET6
129 static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
130 		    struct thread *td);
131 #endif /* INET6 */
132 static void	tcp_disconnect(struct tcpcb *);
133 static void	tcp_usrclosed(struct tcpcb *);
134 static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
135 
136 static int	tcp_pru_options_support(struct tcpcb *tp, int flags);
137 
138 #ifdef TCPDEBUG
139 #define	TCPDEBUG0	int ostate = 0
140 #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
141 #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
142 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
143 #else
144 #define	TCPDEBUG0
145 #define	TCPDEBUG1()
146 #define	TCPDEBUG2(req)
147 #endif
148 
149 /*
150  * tcp_require_unique port requires a globally-unique source port for each
151  * outgoing connection.  The default is to require the 4-tuple to be unique.
152  */
153 VNET_DEFINE(int, tcp_require_unique_port) = 0;
154 SYSCTL_INT(_net_inet_tcp, OID_AUTO, require_unique_port,
155     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_require_unique_port), 0,
156     "Require globally-unique ephemeral port for outgoing connections");
157 #define	V_tcp_require_unique_port	VNET(tcp_require_unique_port)
158 
159 /*
160  * TCP attaches to socket via pru_attach(), reserving space,
161  * and an internet control block.
162  */
163 static int
tcp_usr_attach(struct socket * so,int proto,struct thread * td)164 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
165 {
166 	struct inpcb *inp;
167 	struct tcpcb *tp = NULL;
168 	int error;
169 	TCPDEBUG0;
170 
171 	inp = sotoinpcb(so);
172 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
173 	TCPDEBUG1();
174 
175 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
176 		error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
177 		if (error)
178 			goto out;
179 	}
180 
181 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
182 	so->so_snd.sb_flags |= SB_AUTOSIZE;
183 	error = in_pcballoc(so, &V_tcbinfo);
184 	if (error)
185 		goto out;
186 	inp = sotoinpcb(so);
187 #ifdef INET6
188 	if (inp->inp_vflag & INP_IPV6PROTO) {
189 		inp->inp_vflag |= INP_IPV6;
190 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
191 			inp->inp_vflag |= INP_IPV4;
192 		inp->in6p_hops = -1;	/* use kernel default */
193 	}
194 	else
195 #endif
196 		inp->inp_vflag |= INP_IPV4;
197 	tp = tcp_newtcpcb(inp);
198 	if (tp == NULL) {
199 		error = ENOBUFS;
200 		in_pcbdetach(inp);
201 		in_pcbfree(inp);
202 		goto out;
203 	}
204 	tp->t_state = TCPS_CLOSED;
205 	INP_WUNLOCK(inp);
206 	TCPSTATES_INC(TCPS_CLOSED);
207 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
208 		so->so_linger = TCP_LINGERTIME;
209 out:
210 	TCPDEBUG2(PRU_ATTACH);
211 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
212 	return (error);
213 }
214 
215 /*
216  * tcp_usr_detach is called when the socket layer loses its final reference
217  * to the socket, be it a file descriptor reference, a reference from TCP,
218  * etc.  At this point, there is only one case in which we will keep around
219  * inpcb state: time wait.
220  */
221 static void
tcp_usr_detach(struct socket * so)222 tcp_usr_detach(struct socket *so)
223 {
224 	struct inpcb *inp;
225 	struct tcpcb *tp;
226 
227 	inp = sotoinpcb(so);
228 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
229 	INP_WLOCK(inp);
230 	KASSERT(so->so_pcb == inp && inp->inp_socket == so,
231 		("%s: socket %p inp %p mismatch", __func__, so, inp));
232 
233 	tp = intotcpcb(inp);
234 
235 	if (inp->inp_flags & INP_TIMEWAIT) {
236 		/*
237 		 * There are two cases to handle: one in which the time wait
238 		 * state is being discarded (INP_DROPPED), and one in which
239 		 * this connection will remain in timewait.  In the former,
240 		 * it is time to discard all state (except tcptw, which has
241 		 * already been discarded by the timewait close code, which
242 		 * should be further up the call stack somewhere).  In the
243 		 * latter case, we detach from the socket, but leave the pcb
244 		 * present until timewait ends.
245 		 *
246 		 * XXXRW: Would it be cleaner to free the tcptw here?
247 		 *
248 		 * Astute question indeed, from twtcp perspective there are
249 		 * four cases to consider:
250 		 *
251 		 * #1 tcp_usr_detach is called at tcptw creation time by
252 		 *  tcp_twstart, then do not discard the newly created tcptw
253 		 *  and leave inpcb present until timewait ends
254 		 * #2 tcp_usr_detach is called at tcptw creation time by
255 		 *  tcp_twstart, but connection is local and tw will be
256 		 *  discarded immediately
257 		 * #3 tcp_usr_detach is called at timewait end (or reuse) by
258 		 *  tcp_twclose, then the tcptw has already been discarded
259 		 *  (or reused) and inpcb is freed here
260 		 * #4 tcp_usr_detach is called() after timewait ends (or reuse)
261 		 *  (e.g. by soclose), then tcptw has already been discarded
262 		 *  (or reused) and inpcb is freed here
263 		 *
264 		 *  In all three cases the tcptw should not be freed here.
265 		 */
266 		if (inp->inp_flags & INP_DROPPED) {
267 			in_pcbdetach(inp);
268 			if (__predict_true(tp == NULL)) {
269 				in_pcbfree(inp);
270 			} else {
271 				/*
272 				 * This case should not happen as in TIMEWAIT
273 				 * state the inp should not be destroyed before
274 				 * its tcptw.  If INVARIANTS is defined, panic.
275 				 */
276 #ifdef INVARIANTS
277 				panic("%s: Panic before an inp double-free: "
278 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
279 				    , __func__);
280 #else
281 				log(LOG_ERR, "%s: Avoid an inp double-free: "
282 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
283 				    , __func__);
284 #endif
285 				INP_WUNLOCK(inp);
286 			}
287 		} else {
288 			in_pcbdetach(inp);
289 			INP_WUNLOCK(inp);
290 		}
291 	} else {
292 		/*
293 		 * If the connection is not in timewait, we consider two
294 		 * two conditions: one in which no further processing is
295 		 * necessary (dropped || embryonic), and one in which TCP is
296 		 * not yet done, but no longer requires the socket, so the
297 		 * pcb will persist for the time being.
298 		 *
299 		 * XXXRW: Does the second case still occur?
300 		 */
301 		if (inp->inp_flags & INP_DROPPED ||
302 		    tp->t_state < TCPS_SYN_SENT) {
303 			tcp_discardcb(tp);
304 			in_pcbdetach(inp);
305 			in_pcbfree(inp);
306 		} else {
307 			in_pcbdetach(inp);
308 			INP_WUNLOCK(inp);
309 		}
310 	}
311 }
312 
313 #ifdef LVS_TCPOPT_TOA
314 
315 #ifndef TCPOPT_TOA
316 #define TCPOPT_TOA 254
317 #define TCPOLEN_TOA  8
318 #endif
319 
320 struct toa_data {
321     uint8_t opcode;
322     uint8_t opsize;
323     uint16_t port;
324     uint32_t ip;
325 };
326 
327 static int
toa_getpeeraddr(struct socket * so,struct sockaddr ** nam)328 toa_getpeeraddr(struct socket *so, struct sockaddr **nam)
329 {
330     int ret;
331     struct toa_data *toa;
332     struct sockaddr_in *sin;
333 
334     ret = in_getpeeraddr(so, nam);
335     if (ret) {
336         return ret;
337     }
338 
339     toa = (struct toa_data *)so->so_toa;
340     if (toa->opcode == TCPOPT_TOA && toa->opsize == TCPOLEN_TOA) {
341         sin = (struct sockaddr_in *)(*nam);
342 
343         sin->sin_addr.s_addr = toa->ip;
344         sin->sin_port = toa->port;
345     }
346 
347     return 0;
348 }
349 #endif
350 
351 #ifdef INET
352 /*
353  * Give the socket an address.
354  */
355 static int
tcp_usr_bind(struct socket * so,struct sockaddr * nam,struct thread * td)356 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
357 {
358 	int error = 0;
359 	struct inpcb *inp;
360 	struct tcpcb *tp = NULL;
361 	struct sockaddr_in *sinp;
362 
363 	sinp = (struct sockaddr_in *)nam;
364 	if (nam->sa_len != sizeof (*sinp))
365 		return (EINVAL);
366 	/*
367 	 * Must check for multicast addresses and disallow binding
368 	 * to them.
369 	 */
370 	if (sinp->sin_family == AF_INET &&
371 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
372 		return (EAFNOSUPPORT);
373 
374 	TCPDEBUG0;
375 	inp = sotoinpcb(so);
376 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
377 	INP_WLOCK(inp);
378 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
379 		error = EINVAL;
380 		goto out;
381 	}
382 	tp = intotcpcb(inp);
383 	TCPDEBUG1();
384 	INP_HASH_WLOCK(&V_tcbinfo);
385 	error = in_pcbbind(inp, nam, td->td_ucred);
386 	INP_HASH_WUNLOCK(&V_tcbinfo);
387 out:
388 	TCPDEBUG2(PRU_BIND);
389 	TCP_PROBE2(debug__user, tp, PRU_BIND);
390 	INP_WUNLOCK(inp);
391 
392 	return (error);
393 }
394 #endif /* INET */
395 
396 #ifdef INET6
397 static int
tcp6_usr_bind(struct socket * so,struct sockaddr * nam,struct thread * td)398 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
399 {
400 	int error = 0;
401 	struct inpcb *inp;
402 	struct tcpcb *tp = NULL;
403 	struct sockaddr_in6 *sin6;
404 	u_char vflagsav;
405 
406 	sin6 = (struct sockaddr_in6 *)nam;
407 	if (nam->sa_len != sizeof (*sin6))
408 		return (EINVAL);
409 	/*
410 	 * Must check for multicast addresses and disallow binding
411 	 * to them.
412 	 */
413 	if (sin6->sin6_family == AF_INET6 &&
414 	    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
415 		return (EAFNOSUPPORT);
416 
417 	TCPDEBUG0;
418 	inp = sotoinpcb(so);
419 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
420 	INP_WLOCK(inp);
421 	vflagsav = inp->inp_vflag;
422 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
423 		error = EINVAL;
424 		goto out;
425 	}
426 	tp = intotcpcb(inp);
427 	TCPDEBUG1();
428 	INP_HASH_WLOCK(&V_tcbinfo);
429 	inp->inp_vflag &= ~INP_IPV4;
430 	inp->inp_vflag |= INP_IPV6;
431 #ifdef INET
432 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
433 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
434 			inp->inp_vflag |= INP_IPV4;
435 		else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
436 			struct sockaddr_in sin;
437 
438 			in6_sin6_2_sin(&sin, sin6);
439 			if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
440 				error = EAFNOSUPPORT;
441 				INP_HASH_WUNLOCK(&V_tcbinfo);
442 				goto out;
443 			}
444 			inp->inp_vflag |= INP_IPV4;
445 			inp->inp_vflag &= ~INP_IPV6;
446 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
447 			    td->td_ucred);
448 			INP_HASH_WUNLOCK(&V_tcbinfo);
449 			goto out;
450 		}
451 	}
452 #endif
453 	error = in6_pcbbind(inp, nam, td->td_ucred);
454 	INP_HASH_WUNLOCK(&V_tcbinfo);
455 out:
456 	if (error != 0)
457 		inp->inp_vflag = vflagsav;
458 	TCPDEBUG2(PRU_BIND);
459 	TCP_PROBE2(debug__user, tp, PRU_BIND);
460 	INP_WUNLOCK(inp);
461 	return (error);
462 }
463 #endif /* INET6 */
464 
465 #ifdef INET
466 /*
467  * Prepare to accept connections.
468  */
469 static int
tcp_usr_listen(struct socket * so,int backlog,struct thread * td)470 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
471 {
472 	int error = 0;
473 	struct inpcb *inp;
474 	struct tcpcb *tp = NULL;
475 
476 	TCPDEBUG0;
477 	inp = sotoinpcb(so);
478 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
479 	INP_WLOCK(inp);
480 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
481 		error = EINVAL;
482 		goto out;
483 	}
484 	tp = intotcpcb(inp);
485 	TCPDEBUG1();
486 	SOCK_LOCK(so);
487 	error = solisten_proto_check(so);
488 	INP_HASH_WLOCK(&V_tcbinfo);
489 	if (error == 0 && inp->inp_lport == 0)
490 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
491 	INP_HASH_WUNLOCK(&V_tcbinfo);
492 	if (error == 0) {
493 		tcp_state_change(tp, TCPS_LISTEN);
494 		solisten_proto(so, backlog);
495 #ifdef TCP_OFFLOAD
496 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
497 			tcp_offload_listen_start(tp);
498 #endif
499 	}
500 	SOCK_UNLOCK(so);
501 
502 	if (IS_FASTOPEN(tp->t_flags))
503 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
504 
505 out:
506 	TCPDEBUG2(PRU_LISTEN);
507 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
508 	INP_WUNLOCK(inp);
509 	return (error);
510 }
511 #endif /* INET */
512 
513 #ifdef INET6
514 static int
tcp6_usr_listen(struct socket * so,int backlog,struct thread * td)515 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
516 {
517 	int error = 0;
518 	struct inpcb *inp;
519 	struct tcpcb *tp = NULL;
520 	u_char vflagsav;
521 
522 	TCPDEBUG0;
523 	inp = sotoinpcb(so);
524 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
525 	INP_WLOCK(inp);
526 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
527 		error = EINVAL;
528 		goto out;
529 	}
530 	vflagsav = inp->inp_vflag;
531 	tp = intotcpcb(inp);
532 	TCPDEBUG1();
533 	SOCK_LOCK(so);
534 	error = solisten_proto_check(so);
535 	INP_HASH_WLOCK(&V_tcbinfo);
536 	if (error == 0 && inp->inp_lport == 0) {
537 		inp->inp_vflag &= ~INP_IPV4;
538 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
539 			inp->inp_vflag |= INP_IPV4;
540 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
541 	}
542 	INP_HASH_WUNLOCK(&V_tcbinfo);
543 	if (error == 0) {
544 		tcp_state_change(tp, TCPS_LISTEN);
545 		solisten_proto(so, backlog);
546 #ifdef TCP_OFFLOAD
547 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
548 			tcp_offload_listen_start(tp);
549 #endif
550 	}
551 	SOCK_UNLOCK(so);
552 
553 	if (IS_FASTOPEN(tp->t_flags))
554 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
555 
556 	if (error != 0)
557 		inp->inp_vflag = vflagsav;
558 
559 out:
560 	TCPDEBUG2(PRU_LISTEN);
561 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
562 	INP_WUNLOCK(inp);
563 	return (error);
564 }
565 #endif /* INET6 */
566 
567 #ifdef INET
568 /*
569  * Initiate connection to peer.
570  * Create a template for use in transmissions on this connection.
571  * Enter SYN_SENT state, and mark socket as connecting.
572  * Start keep-alive timer, and seed output sequence space.
573  * Send initial segment on connection.
574  */
575 static int
tcp_usr_connect(struct socket * so,struct sockaddr * nam,struct thread * td)576 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
577 {
578 	struct epoch_tracker et;
579 	int error = 0;
580 	struct inpcb *inp;
581 	struct tcpcb *tp = NULL;
582 	struct sockaddr_in *sinp;
583 
584 	sinp = (struct sockaddr_in *)nam;
585 	if (nam->sa_len != sizeof (*sinp))
586 		return (EINVAL);
587 	/*
588 	 * Must disallow TCP ``connections'' to multicast addresses.
589 	 */
590 	if (sinp->sin_family == AF_INET
591 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
592 		return (EAFNOSUPPORT);
593 	if ((sinp->sin_family == AF_INET) &&
594 	    (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST))
595 		return (EACCES);
596 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
597 		return (error);
598 
599 	TCPDEBUG0;
600 	inp = sotoinpcb(so);
601 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
602 	INP_WLOCK(inp);
603 	if (inp->inp_flags & INP_TIMEWAIT) {
604 		error = EADDRINUSE;
605 		goto out;
606 	}
607 	if (inp->inp_flags & INP_DROPPED) {
608 		error = ECONNREFUSED;
609 		goto out;
610 	}
611 	tp = intotcpcb(inp);
612 	TCPDEBUG1();
613 	NET_EPOCH_ENTER(et);
614 	if ((error = tcp_connect(tp, nam, td)) != 0)
615 		goto out_in_epoch;
616 #ifdef TCP_OFFLOAD
617 	if (registered_toedevs > 0 &&
618 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
619 	    (error = tcp_offload_connect(so, nam)) == 0)
620 		goto out_in_epoch;
621 #endif
622 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
623 	error = tp->t_fb->tfb_tcp_output(tp);
624 out_in_epoch:
625 	NET_EPOCH_EXIT(et);
626 out:
627 	TCPDEBUG2(PRU_CONNECT);
628 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
629 	INP_WUNLOCK(inp);
630 	return (error);
631 }
632 #endif /* INET */
633 
634 #ifdef INET6
635 static int
tcp6_usr_connect(struct socket * so,struct sockaddr * nam,struct thread * td)636 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
637 {
638 	struct epoch_tracker et;
639 	int error = 0;
640 	struct inpcb *inp;
641 	struct tcpcb *tp = NULL;
642 	struct sockaddr_in6 *sin6;
643 	u_int8_t incflagsav;
644 	u_char vflagsav;
645 
646 	TCPDEBUG0;
647 
648 	sin6 = (struct sockaddr_in6 *)nam;
649 	if (nam->sa_len != sizeof (*sin6))
650 		return (EINVAL);
651 	/*
652 	 * Must disallow TCP ``connections'' to multicast addresses.
653 	 */
654 	if (sin6->sin6_family == AF_INET6
655 	    && IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
656 		return (EAFNOSUPPORT);
657 
658 	inp = sotoinpcb(so);
659 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
660 	INP_WLOCK(inp);
661 	vflagsav = inp->inp_vflag;
662 	incflagsav = inp->inp_inc.inc_flags;
663 	if (inp->inp_flags & INP_TIMEWAIT) {
664 		error = EADDRINUSE;
665 		goto out;
666 	}
667 	if (inp->inp_flags & INP_DROPPED) {
668 		error = ECONNREFUSED;
669 		goto out;
670 	}
671 	tp = intotcpcb(inp);
672 	TCPDEBUG1();
673 #ifdef INET
674 	/*
675 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
676 	 * therefore probably require the hash lock, which isn't held here.
677 	 * Is this a significant problem?
678 	 */
679 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
680 		struct sockaddr_in sin;
681 
682 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
683 			error = EINVAL;
684 			goto out;
685 		}
686 		if ((inp->inp_vflag & INP_IPV4) == 0) {
687 			error = EAFNOSUPPORT;
688 			goto out;
689 		}
690 
691 		in6_sin6_2_sin(&sin, sin6);
692 		if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
693 			error = EAFNOSUPPORT;
694 			goto out;
695 		}
696 		if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
697 			error = EACCES;
698 			goto out;
699 		}
700 		if ((error = prison_remote_ip4(td->td_ucred,
701 		    &sin.sin_addr)) != 0)
702 			goto out;
703 		inp->inp_vflag |= INP_IPV4;
704 		inp->inp_vflag &= ~INP_IPV6;
705 		NET_EPOCH_ENTER(et);
706 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
707 			goto out_in_epoch;
708 #ifdef TCP_OFFLOAD
709 		if (registered_toedevs > 0 &&
710 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
711 		    (error = tcp_offload_connect(so, nam)) == 0)
712 			goto out_in_epoch;
713 #endif
714 		error = tp->t_fb->tfb_tcp_output(tp);
715 		goto out_in_epoch;
716 	} else {
717 		if ((inp->inp_vflag & INP_IPV6) == 0) {
718 			error = EAFNOSUPPORT;
719 			goto out;
720 		}
721 	}
722 #endif
723 	if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
724 		goto out;
725 	inp->inp_vflag &= ~INP_IPV4;
726 	inp->inp_vflag |= INP_IPV6;
727 	inp->inp_inc.inc_flags |= INC_ISIPV6;
728 	if ((error = tcp6_connect(tp, nam, td)) != 0)
729 		goto out;
730 #ifdef TCP_OFFLOAD
731 	if (registered_toedevs > 0 &&
732 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
733 	    (error = tcp_offload_connect(so, nam)) == 0)
734 		goto out;
735 #endif
736 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
737 	NET_EPOCH_ENTER(et);
738 	error = tp->t_fb->tfb_tcp_output(tp);
739 #ifdef INET
740 out_in_epoch:
741 #endif
742 	NET_EPOCH_EXIT(et);
743 out:
744 	/*
745 	 * If the implicit bind in the connect call fails, restore
746 	 * the flags we modified.
747 	 */
748 	if (error != 0 && inp->inp_lport == 0) {
749 		inp->inp_vflag = vflagsav;
750 		inp->inp_inc.inc_flags = incflagsav;
751 	}
752 
753 	TCPDEBUG2(PRU_CONNECT);
754 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
755 	INP_WUNLOCK(inp);
756 	return (error);
757 }
758 #endif /* INET6 */
759 
760 /*
761  * Initiate disconnect from peer.
762  * If connection never passed embryonic stage, just drop;
763  * else if don't need to let data drain, then can just drop anyways,
764  * else have to begin TCP shutdown process: mark socket disconnecting,
765  * drain unread data, state switch to reflect user close, and
766  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
767  * when peer sends FIN and acks ours.
768  *
769  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
770  */
771 static int
tcp_usr_disconnect(struct socket * so)772 tcp_usr_disconnect(struct socket *so)
773 {
774 	struct inpcb *inp;
775 	struct tcpcb *tp = NULL;
776 	struct epoch_tracker et;
777 	int error = 0;
778 
779 	TCPDEBUG0;
780 	NET_EPOCH_ENTER(et);
781 	inp = sotoinpcb(so);
782 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
783 	INP_WLOCK(inp);
784 	if (inp->inp_flags & INP_TIMEWAIT)
785 		goto out;
786 	if (inp->inp_flags & INP_DROPPED) {
787 		error = ECONNRESET;
788 		goto out;
789 	}
790 	tp = intotcpcb(inp);
791 	TCPDEBUG1();
792 	tcp_disconnect(tp);
793 out:
794 	TCPDEBUG2(PRU_DISCONNECT);
795 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
796 	INP_WUNLOCK(inp);
797 	NET_EPOCH_EXIT(et);
798 	return (error);
799 }
800 
801 #ifdef INET
802 /*
803  * Accept a connection.  Essentially all the work is done at higher levels;
804  * just return the address of the peer, storing through addr.
805  */
806 static int
tcp_usr_accept(struct socket * so,struct sockaddr ** nam)807 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
808 {
809 	int error = 0;
810 	struct inpcb *inp = NULL;
811 	struct tcpcb *tp = NULL;
812 	struct in_addr addr;
813 	in_port_t port = 0;
814 	TCPDEBUG0;
815 
816 	if (so->so_state & SS_ISDISCONNECTED)
817 		return (ECONNABORTED);
818 
819 	inp = sotoinpcb(so);
820 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
821 	INP_WLOCK(inp);
822 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
823 		error = ECONNABORTED;
824 		goto out;
825 	}
826 	tp = intotcpcb(inp);
827 	TCPDEBUG1();
828 
829 	/*
830 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
831 	 * copy the data of interest and defer the malloc until after we
832 	 * release the lock.
833 	 */
834 	port = inp->inp_fport;
835 	addr = inp->inp_faddr;
836 
837 #ifdef LVS_TCPOPT_TOA
838 	{
839 		struct toa_data *toa = (struct toa_data *)so->so_toa;
840 		if (toa->opcode == TCPOPT_TOA && toa->opsize == TCPOLEN_TOA) {
841 			addr.s_addr = toa->ip;
842 			port = toa->port;
843 		}
844 	}
845 #endif
846 
847 out:
848 	TCPDEBUG2(PRU_ACCEPT);
849 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
850 	INP_WUNLOCK(inp);
851 	if (error == 0)
852 		*nam = in_sockaddr(port, &addr);
853 	return error;
854 }
855 #endif /* INET */
856 
857 #ifdef INET6
858 static int
tcp6_usr_accept(struct socket * so,struct sockaddr ** nam)859 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
860 {
861 	struct inpcb *inp = NULL;
862 	int error = 0;
863 	struct tcpcb *tp = NULL;
864 	struct in_addr addr;
865 	struct in6_addr addr6;
866 	struct epoch_tracker et;
867 	in_port_t port = 0;
868 	int v4 = 0;
869 	TCPDEBUG0;
870 
871 	if (so->so_state & SS_ISDISCONNECTED)
872 		return (ECONNABORTED);
873 
874 	inp = sotoinpcb(so);
875 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
876 	NET_EPOCH_ENTER(et);
877 	INP_WLOCK(inp);
878 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
879 		error = ECONNABORTED;
880 		goto out;
881 	}
882 	tp = intotcpcb(inp);
883 	TCPDEBUG1();
884 
885 	/*
886 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
887 	 * copy the data of interest and defer the malloc until after we
888 	 * release the lock.
889 	 */
890 	if (inp->inp_vflag & INP_IPV4) {
891 		v4 = 1;
892 		port = inp->inp_fport;
893 		addr = inp->inp_faddr;
894 	} else {
895 		port = inp->inp_fport;
896 		addr6 = inp->in6p_faddr;
897 	}
898 
899 out:
900 	TCPDEBUG2(PRU_ACCEPT);
901 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
902 	INP_WUNLOCK(inp);
903 	NET_EPOCH_EXIT(et);
904 	if (error == 0) {
905 		if (v4)
906 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
907 		else
908 			*nam = in6_sockaddr(port, &addr6);
909 	}
910 	return error;
911 }
912 #endif /* INET6 */
913 
914 /*
915  * Mark the connection as being incapable of further output.
916  */
917 static int
tcp_usr_shutdown(struct socket * so)918 tcp_usr_shutdown(struct socket *so)
919 {
920 	int error = 0;
921 	struct inpcb *inp;
922 	struct tcpcb *tp = NULL;
923 	struct epoch_tracker et;
924 
925 	TCPDEBUG0;
926 	NET_EPOCH_ENTER(et);
927 	inp = sotoinpcb(so);
928 	KASSERT(inp != NULL, ("inp == NULL"));
929 	INP_WLOCK(inp);
930 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
931 		error = ECONNRESET;
932 		goto out;
933 	}
934 	tp = intotcpcb(inp);
935 	TCPDEBUG1();
936 	socantsendmore(so);
937 	tcp_usrclosed(tp);
938 	if (!(inp->inp_flags & INP_DROPPED))
939 		error = tp->t_fb->tfb_tcp_output(tp);
940 
941 out:
942 	TCPDEBUG2(PRU_SHUTDOWN);
943 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
944 	INP_WUNLOCK(inp);
945 	NET_EPOCH_EXIT(et);
946 
947 	return (error);
948 }
949 
950 /*
951  * After a receive, possibly send window update to peer.
952  */
953 static int
tcp_usr_rcvd(struct socket * so,int flags)954 tcp_usr_rcvd(struct socket *so, int flags)
955 {
956 	struct epoch_tracker et;
957 	struct inpcb *inp;
958 	struct tcpcb *tp = NULL;
959 	int error = 0;
960 
961 	TCPDEBUG0;
962 	inp = sotoinpcb(so);
963 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
964 	INP_WLOCK(inp);
965 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
966 		error = ECONNRESET;
967 		goto out;
968 	}
969 	tp = intotcpcb(inp);
970 	TCPDEBUG1();
971 	/*
972 	 * For passively-created TFO connections, don't attempt a window
973 	 * update while still in SYN_RECEIVED as this may trigger an early
974 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
975 	 * application response data, or failing that, when the DELACK timer
976 	 * expires.
977 	 */
978 	if (IS_FASTOPEN(tp->t_flags) &&
979 	    (tp->t_state == TCPS_SYN_RECEIVED))
980 		goto out;
981 	NET_EPOCH_ENTER(et);
982 #ifdef TCP_OFFLOAD
983 	if (tp->t_flags & TF_TOE)
984 		tcp_offload_rcvd(tp);
985 	else
986 #endif
987 	tp->t_fb->tfb_tcp_output(tp);
988 	NET_EPOCH_EXIT(et);
989 out:
990 	TCPDEBUG2(PRU_RCVD);
991 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
992 	INP_WUNLOCK(inp);
993 	return (error);
994 }
995 
996 /*
997  * Do a send by putting data in output queue and updating urgent
998  * marker if URG set.  Possibly send more data.  Unlike the other
999  * pru_*() routines, the mbuf chains are our responsibility.  We
1000  * must either enqueue them or free them.  The other pru_* routines
1001  * generally are caller-frees.
1002  */
1003 static int
tcp_usr_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct thread * td)1004 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
1005     struct sockaddr *nam, struct mbuf *control, struct thread *td)
1006 {
1007 	struct epoch_tracker et;
1008 	int error = 0;
1009 	struct inpcb *inp;
1010 	struct tcpcb *tp = NULL;
1011 #ifdef INET
1012 #ifdef INET6
1013 	struct sockaddr_in sin;
1014 #endif
1015 	struct sockaddr_in *sinp;
1016 #endif
1017 #ifdef INET6
1018 	int isipv6;
1019 #endif
1020 	u_int8_t incflagsav;
1021 	u_char vflagsav;
1022 	bool restoreflags;
1023 	TCPDEBUG0;
1024 
1025 	/*
1026 	 * We require the pcbinfo "read lock" if we will close the socket
1027 	 * as part of this call.
1028 	 */
1029 	NET_EPOCH_ENTER(et);
1030 	inp = sotoinpcb(so);
1031 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
1032 	INP_WLOCK(inp);
1033 	vflagsav = inp->inp_vflag;
1034 	incflagsav = inp->inp_inc.inc_flags;
1035 	restoreflags = false;
1036 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1037 		if (control)
1038 			m_freem(control);
1039 		/*
1040 		 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
1041 		 * for freeing memory.
1042 		 */
1043 		if (m && (flags & PRUS_NOTREADY) == 0)
1044 			m_freem(m);
1045 		error = ECONNRESET;
1046 		goto out;
1047 	}
1048 	tp = intotcpcb(inp);
1049 	if (flags & PRUS_OOB) {
1050 		if ((error = tcp_pru_options_support(tp, PRUS_OOB)) != 0) {
1051 			if (control)
1052 				m_freem(control);
1053 			if (m && (flags & PRUS_NOTREADY) == 0)
1054 				m_freem(m);
1055 			goto out;
1056 		}
1057 	}
1058 	TCPDEBUG1();
1059 	if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
1060 		switch (nam->sa_family) {
1061 #ifdef INET
1062 		case AF_INET:
1063 			sinp = (struct sockaddr_in *)nam;
1064 			if (sinp->sin_len != sizeof(struct sockaddr_in)) {
1065 				if (m)
1066 					m_freem(m);
1067 				error = EINVAL;
1068 				goto out;
1069 			}
1070 			if ((inp->inp_vflag & INP_IPV6) != 0) {
1071 				if (m)
1072 					m_freem(m);
1073 				error = EAFNOSUPPORT;
1074 				goto out;
1075 			}
1076 			if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
1077 				if (m)
1078 					m_freem(m);
1079 				error = EAFNOSUPPORT;
1080 				goto out;
1081 			}
1082 			if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
1083 				if (m)
1084 					m_freem(m);
1085 				error = EACCES;
1086 				goto out;
1087 			}
1088 			if ((error = prison_remote_ip4(td->td_ucred,
1089 			    &sinp->sin_addr))) {
1090 				if (m)
1091 					m_freem(m);
1092 				goto out;
1093 			}
1094 #ifdef INET6
1095 			isipv6 = 0;
1096 #endif
1097 			break;
1098 #endif /* INET */
1099 #ifdef INET6
1100 		case AF_INET6:
1101 		{
1102 			struct sockaddr_in6 *sin6;
1103 
1104 			sin6 = (struct sockaddr_in6 *)nam;
1105 			if (sin6->sin6_len != sizeof(*sin6)) {
1106 				if (m)
1107 					m_freem(m);
1108 				error = EINVAL;
1109 				goto out;
1110 			}
1111 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
1112 				if (m != NULL)
1113 					m_freem(m);
1114 				error = EAFNOSUPPORT;
1115 				goto out;
1116 			}
1117 			if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1118 				if (m)
1119 					m_freem(m);
1120 				error = EAFNOSUPPORT;
1121 				goto out;
1122 			}
1123 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1124 #ifdef INET
1125 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1126 					error = EINVAL;
1127 					if (m)
1128 						m_freem(m);
1129 					goto out;
1130 				}
1131 				if ((inp->inp_vflag & INP_IPV4) == 0) {
1132 					error = EAFNOSUPPORT;
1133 					if (m)
1134 						m_freem(m);
1135 					goto out;
1136 				}
1137 				restoreflags = true;
1138 				inp->inp_vflag &= ~INP_IPV6;
1139 				sinp = &sin;
1140 				in6_sin6_2_sin(sinp, sin6);
1141 				if (IN_MULTICAST(
1142 				    ntohl(sinp->sin_addr.s_addr))) {
1143 					error = EAFNOSUPPORT;
1144 					if (m)
1145 						m_freem(m);
1146 					goto out;
1147 				}
1148 				if ((error = prison_remote_ip4(td->td_ucred,
1149 				    &sinp->sin_addr))) {
1150 					if (m)
1151 						m_freem(m);
1152 					goto out;
1153 				}
1154 				isipv6 = 0;
1155 #else /* !INET */
1156 				error = EAFNOSUPPORT;
1157 				if (m)
1158 					m_freem(m);
1159 				goto out;
1160 #endif /* INET */
1161 			} else {
1162 				if ((inp->inp_vflag & INP_IPV6) == 0) {
1163 					if (m)
1164 						m_freem(m);
1165 					error = EAFNOSUPPORT;
1166 					goto out;
1167 				}
1168 				restoreflags = true;
1169 				inp->inp_vflag &= ~INP_IPV4;
1170 				inp->inp_inc.inc_flags |= INC_ISIPV6;
1171 				if ((error = prison_remote_ip6(td->td_ucred,
1172 				    &sin6->sin6_addr))) {
1173 					if (m)
1174 						m_freem(m);
1175 					goto out;
1176 				}
1177 				isipv6 = 1;
1178 			}
1179 			break;
1180 		}
1181 #endif /* INET6 */
1182 		default:
1183 			if (m)
1184 				m_freem(m);
1185 			error = EAFNOSUPPORT;
1186 			goto out;
1187 		}
1188 	}
1189 	if (control) {
1190 		/* TCP doesn't do control messages (rights, creds, etc) */
1191 		if (control->m_len) {
1192 			m_freem(control);
1193 			if (m)
1194 				m_freem(m);
1195 			error = EINVAL;
1196 			goto out;
1197 		}
1198 		m_freem(control);	/* empty control, just free it */
1199 	}
1200 	if (!(flags & PRUS_OOB)) {
1201 		sbappendstream(&so->so_snd, m, flags);
1202 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1203 			/*
1204 			 * Do implied connect if not yet connected,
1205 			 * initialize window to default value, and
1206 			 * initialize maxseg using peer's cached MSS.
1207 			 */
1208 #ifdef INET6
1209 			if (isipv6)
1210 				error = tcp6_connect(tp, nam, td);
1211 #endif /* INET6 */
1212 #if defined(INET6) && defined(INET)
1213 			else
1214 #endif
1215 #ifdef INET
1216 				error = tcp_connect(tp,
1217 				    (struct sockaddr *)sinp, td);
1218 #endif
1219 			/*
1220 			 * The bind operation in tcp_connect succeeded. We
1221 			 * no longer want to restore the flags if later
1222 			 * operations fail.
1223 			 */
1224 			if (error == 0 || inp->inp_lport != 0)
1225 				restoreflags = false;
1226 
1227 			if (error)
1228 				goto out;
1229 			if (IS_FASTOPEN(tp->t_flags))
1230 				tcp_fastopen_connect(tp);
1231 			else {
1232 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
1233 				tcp_mss(tp, -1);
1234 			}
1235 		}
1236 		if (flags & PRUS_EOF) {
1237 			/*
1238 			 * Close the send side of the connection after
1239 			 * the data is sent.
1240 			 */
1241 			socantsendmore(so);
1242 			tcp_usrclosed(tp);
1243 		}
1244 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1245 		    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1246 		    (tp->t_fbyte_out == 0) &&
1247 		    (so->so_snd.sb_ccc > 0)) {
1248 			tp->t_fbyte_out = ticks;
1249 			if (tp->t_fbyte_out == 0)
1250 				tp->t_fbyte_out = 1;
1251 			if (tp->t_fbyte_out && tp->t_fbyte_in)
1252 				tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1253 		}
1254 		if (!(inp->inp_flags & INP_DROPPED) &&
1255 		    !(flags & PRUS_NOTREADY)) {
1256 			if (flags & PRUS_MORETOCOME)
1257 				tp->t_flags |= TF_MORETOCOME;
1258 			error = tp->t_fb->tfb_tcp_output(tp);
1259 			if (flags & PRUS_MORETOCOME)
1260 				tp->t_flags &= ~TF_MORETOCOME;
1261 		}
1262 	} else {
1263 		/*
1264 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1265 		 */
1266 		SOCKBUF_LOCK(&so->so_snd);
1267 		if (sbspace(&so->so_snd) < -512) {
1268 			SOCKBUF_UNLOCK(&so->so_snd);
1269 			m_freem(m);
1270 			error = ENOBUFS;
1271 			goto out;
1272 		}
1273 		/*
1274 		 * According to RFC961 (Assigned Protocols),
1275 		 * the urgent pointer points to the last octet
1276 		 * of urgent data.  We continue, however,
1277 		 * to consider it to indicate the first octet
1278 		 * of data past the urgent section.
1279 		 * Otherwise, snd_up should be one lower.
1280 		 */
1281 		sbappendstream_locked(&so->so_snd, m, flags);
1282 		SOCKBUF_UNLOCK(&so->so_snd);
1283 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1284 			/*
1285 			 * Do implied connect if not yet connected,
1286 			 * initialize window to default value, and
1287 			 * initialize maxseg using peer's cached MSS.
1288 			 */
1289 
1290 			/*
1291 			 * Not going to contemplate SYN|URG
1292 			 */
1293 			if (IS_FASTOPEN(tp->t_flags))
1294 				tp->t_flags &= ~TF_FASTOPEN;
1295 #ifdef INET6
1296 			if (isipv6)
1297 				error = tcp6_connect(tp, nam, td);
1298 #endif /* INET6 */
1299 #if defined(INET6) && defined(INET)
1300 			else
1301 #endif
1302 #ifdef INET
1303 				error = tcp_connect(tp,
1304 				    (struct sockaddr *)sinp, td);
1305 #endif
1306 			/*
1307 			 * The bind operation in tcp_connect succeeded. We
1308 			 * no longer want to restore the flags if later
1309 			 * operations fail.
1310 			 */
1311 			if (error == 0 || inp->inp_lport != 0)
1312 				restoreflags = false;
1313 
1314 			if (error)
1315 				goto out;
1316 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1317 			tcp_mss(tp, -1);
1318 		}
1319 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1320 		if (!(flags & PRUS_NOTREADY)) {
1321 			tp->t_flags |= TF_FORCEDATA;
1322 			error = tp->t_fb->tfb_tcp_output(tp);
1323 			tp->t_flags &= ~TF_FORCEDATA;
1324 		}
1325 	}
1326 	TCP_LOG_EVENT(tp, NULL,
1327 	    &inp->inp_socket->so_rcv,
1328 	    &inp->inp_socket->so_snd,
1329 	    TCP_LOG_USERSEND, error,
1330 	    0, NULL, false);
1331 out:
1332 	/*
1333 	 * If the request was unsuccessful and we changed flags,
1334 	 * restore the original flags.
1335 	 */
1336 	if (error != 0 && restoreflags) {
1337 		inp->inp_vflag = vflagsav;
1338 		inp->inp_inc.inc_flags = incflagsav;
1339 	}
1340 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
1341 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1342 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1343 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1344 	INP_WUNLOCK(inp);
1345 	NET_EPOCH_EXIT(et);
1346 	return (error);
1347 }
1348 
1349 static int
tcp_usr_ready(struct socket * so,struct mbuf * m,int count)1350 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1351 {
1352 	struct epoch_tracker et;
1353 	struct inpcb *inp;
1354 	struct tcpcb *tp;
1355 	int error;
1356 
1357 	inp = sotoinpcb(so);
1358 	INP_WLOCK(inp);
1359 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1360 		INP_WUNLOCK(inp);
1361 		mb_free_notready(m, count);
1362 		return (ECONNRESET);
1363 	}
1364 	tp = intotcpcb(inp);
1365 
1366 	SOCKBUF_LOCK(&so->so_snd);
1367 	error = sbready(&so->so_snd, m, count);
1368 	SOCKBUF_UNLOCK(&so->so_snd);
1369 	if (error == 0) {
1370 		NET_EPOCH_ENTER(et);
1371 		error = tp->t_fb->tfb_tcp_output(tp);
1372 		NET_EPOCH_EXIT(et);
1373 	}
1374 	INP_WUNLOCK(inp);
1375 
1376 	return (error);
1377 }
1378 
1379 /*
1380  * Abort the TCP.  Drop the connection abruptly.
1381  */
1382 static void
tcp_usr_abort(struct socket * so)1383 tcp_usr_abort(struct socket *so)
1384 {
1385 	struct inpcb *inp;
1386 	struct tcpcb *tp = NULL;
1387 	struct epoch_tracker et;
1388 	TCPDEBUG0;
1389 
1390 	inp = sotoinpcb(so);
1391 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1392 
1393 	NET_EPOCH_ENTER(et);
1394 	INP_WLOCK(inp);
1395 	KASSERT(inp->inp_socket != NULL,
1396 	    ("tcp_usr_abort: inp_socket == NULL"));
1397 
1398 	/*
1399 	 * If we still have full TCP state, and we're not dropped, drop.
1400 	 */
1401 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1402 	    !(inp->inp_flags & INP_DROPPED)) {
1403 		tp = intotcpcb(inp);
1404 		TCPDEBUG1();
1405 		tp = tcp_drop(tp, ECONNABORTED);
1406 		if (tp == NULL)
1407 			goto dropped;
1408 		TCPDEBUG2(PRU_ABORT);
1409 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1410 	}
1411 	if (!(inp->inp_flags & INP_DROPPED)) {
1412 		SOCK_LOCK(so);
1413 		so->so_state |= SS_PROTOREF;
1414 		SOCK_UNLOCK(so);
1415 		inp->inp_flags |= INP_SOCKREF;
1416 	}
1417 	INP_WUNLOCK(inp);
1418 dropped:
1419 	NET_EPOCH_EXIT(et);
1420 }
1421 
1422 /*
1423  * TCP socket is closed.  Start friendly disconnect.
1424  */
1425 static void
tcp_usr_close(struct socket * so)1426 tcp_usr_close(struct socket *so)
1427 {
1428 	struct inpcb *inp;
1429 	struct tcpcb *tp = NULL;
1430 	struct epoch_tracker et;
1431 	TCPDEBUG0;
1432 
1433 	inp = sotoinpcb(so);
1434 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1435 
1436 	NET_EPOCH_ENTER(et);
1437 	INP_WLOCK(inp);
1438 	KASSERT(inp->inp_socket != NULL,
1439 	    ("tcp_usr_close: inp_socket == NULL"));
1440 
1441 	/*
1442 	 * If we still have full TCP state, and we're not dropped, initiate
1443 	 * a disconnect.
1444 	 */
1445 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1446 	    !(inp->inp_flags & INP_DROPPED)) {
1447 		tp = intotcpcb(inp);
1448 		TCPDEBUG1();
1449 		tcp_disconnect(tp);
1450 		TCPDEBUG2(PRU_CLOSE);
1451 		TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1452 	}
1453 	if (!(inp->inp_flags & INP_DROPPED)) {
1454 		SOCK_LOCK(so);
1455 		so->so_state |= SS_PROTOREF;
1456 		SOCK_UNLOCK(so);
1457 		inp->inp_flags |= INP_SOCKREF;
1458 	}
1459 	INP_WUNLOCK(inp);
1460 	NET_EPOCH_EXIT(et);
1461 }
1462 
1463 static int
tcp_pru_options_support(struct tcpcb * tp,int flags)1464 tcp_pru_options_support(struct tcpcb *tp, int flags)
1465 {
1466 	/*
1467 	 * If the specific TCP stack has a pru_options
1468 	 * specified then it does not always support
1469 	 * all the PRU_XX options and we must ask it.
1470 	 * If the function is not specified then all
1471 	 * of the PRU_XX options are supported.
1472 	 */
1473 	int ret = 0;
1474 
1475 	if (tp->t_fb->tfb_pru_options) {
1476 		ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1477 	}
1478 	return (ret);
1479 }
1480 
1481 /*
1482  * Receive out-of-band data.
1483  */
1484 static int
tcp_usr_rcvoob(struct socket * so,struct mbuf * m,int flags)1485 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1486 {
1487 	int error = 0;
1488 	struct inpcb *inp;
1489 	struct tcpcb *tp = NULL;
1490 
1491 	TCPDEBUG0;
1492 	inp = sotoinpcb(so);
1493 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1494 	INP_WLOCK(inp);
1495 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1496 		error = ECONNRESET;
1497 		goto out;
1498 	}
1499 	tp = intotcpcb(inp);
1500 	error = tcp_pru_options_support(tp, PRUS_OOB);
1501 	if (error) {
1502 		goto out;
1503 	}
1504 	TCPDEBUG1();
1505 	if ((so->so_oobmark == 0 &&
1506 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1507 	    so->so_options & SO_OOBINLINE ||
1508 	    tp->t_oobflags & TCPOOB_HADDATA) {
1509 		error = EINVAL;
1510 		goto out;
1511 	}
1512 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1513 		error = EWOULDBLOCK;
1514 		goto out;
1515 	}
1516 	m->m_len = 1;
1517 	*mtod(m, caddr_t) = tp->t_iobc;
1518 	if ((flags & MSG_PEEK) == 0)
1519 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1520 
1521 out:
1522 	TCPDEBUG2(PRU_RCVOOB);
1523 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1524 	INP_WUNLOCK(inp);
1525 	return (error);
1526 }
1527 
1528 #ifdef INET
1529 struct pr_usrreqs tcp_usrreqs = {
1530 	.pru_abort =		tcp_usr_abort,
1531 	.pru_accept =		tcp_usr_accept,
1532 	.pru_attach =		tcp_usr_attach,
1533 	.pru_bind =		tcp_usr_bind,
1534 	.pru_connect =		tcp_usr_connect,
1535 	.pru_control =		in_control,
1536 	.pru_detach =		tcp_usr_detach,
1537 	.pru_disconnect =	tcp_usr_disconnect,
1538 	.pru_listen =		tcp_usr_listen,
1539 #ifdef LVS_TCPOPT_TOA
1540 	.pru_peeraddr =     toa_getpeeraddr,
1541 #else
1542 	.pru_peeraddr =		in_getpeeraddr,
1543 #endif
1544 	.pru_rcvd =		tcp_usr_rcvd,
1545 	.pru_rcvoob =		tcp_usr_rcvoob,
1546 	.pru_send =		tcp_usr_send,
1547 	.pru_ready =		tcp_usr_ready,
1548 	.pru_shutdown =		tcp_usr_shutdown,
1549 	.pru_sockaddr =		in_getsockaddr,
1550 	.pru_sosetlabel =	in_pcbsosetlabel,
1551 	.pru_close =		tcp_usr_close,
1552 };
1553 #endif /* INET */
1554 
1555 #ifdef INET6
1556 struct pr_usrreqs tcp6_usrreqs = {
1557 	.pru_abort =		tcp_usr_abort,
1558 	.pru_accept =		tcp6_usr_accept,
1559 	.pru_attach =		tcp_usr_attach,
1560 	.pru_bind =		tcp6_usr_bind,
1561 	.pru_connect =		tcp6_usr_connect,
1562 	.pru_control =		in6_control,
1563 	.pru_detach =		tcp_usr_detach,
1564 	.pru_disconnect =	tcp_usr_disconnect,
1565 	.pru_listen =		tcp6_usr_listen,
1566 	.pru_peeraddr =		in6_mapped_peeraddr,
1567 	.pru_rcvd =		tcp_usr_rcvd,
1568 	.pru_rcvoob =		tcp_usr_rcvoob,
1569 	.pru_send =		tcp_usr_send,
1570 	.pru_ready =		tcp_usr_ready,
1571 	.pru_shutdown =		tcp_usr_shutdown,
1572 	.pru_sockaddr =		in6_mapped_sockaddr,
1573 	.pru_sosetlabel =	in_pcbsosetlabel,
1574 	.pru_close =		tcp_usr_close,
1575 };
1576 #endif /* INET6 */
1577 
1578 #ifdef INET
1579 /*
1580  * Common subroutine to open a TCP connection to remote host specified
1581  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
1582  * port number if needed.  Call in_pcbconnect_setup to do the routing and
1583  * to choose a local host address (interface).  If there is an existing
1584  * incarnation of the same connection in TIME-WAIT state and if the remote
1585  * host was sending CC options and if the connection duration was < MSL, then
1586  * truncate the previous TIME-WAIT state and proceed.
1587  * Initialize connection parameters and enter SYN-SENT state.
1588  */
1589 static int
tcp_connect(struct tcpcb * tp,struct sockaddr * nam,struct thread * td)1590 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1591 {
1592 	struct inpcb *inp = tp->t_inpcb, *oinp;
1593 	struct socket *so = inp->inp_socket;
1594 	struct in_addr laddr;
1595 	u_short lport;
1596 	int error;
1597 
1598 	NET_EPOCH_ASSERT();
1599 	INP_WLOCK_ASSERT(inp);
1600 	INP_HASH_WLOCK(&V_tcbinfo);
1601 
1602 #ifndef FSTACK
1603 	if (V_tcp_require_unique_port && inp->inp_lport == 0) {
1604 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1605 		if (error)
1606 			goto out;
1607 	}
1608 
1609 	/*
1610 	 * Cannot simply call in_pcbconnect, because there might be an
1611 	 * earlier incarnation of this same connection still in
1612 	 * TIME_WAIT state, creating an ADDRINUSE error.
1613 	 */
1614 	laddr = inp->inp_laddr;
1615 	lport = inp->inp_lport;
1616 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1617 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1618 	if (error && oinp == NULL)
1619 		goto out;
1620 	if (oinp) {
1621 		error = EADDRINUSE;
1622 		goto out;
1623 	}
1624 	/* Handle initial bind if it hadn't been done in advance. */
1625 	if (inp->inp_lport == 0) {
1626 		inp->inp_lport = lport;
1627 		if (in_pcbinshash(inp) != 0) {
1628 			inp->inp_lport = 0;
1629 			error = EAGAIN;
1630 			goto out;
1631 		}
1632 	}
1633 	inp->inp_laddr = laddr;
1634 	in_pcbrehash(inp);
1635 #else
1636 	int anonport = 0;
1637 	if (inp->inp_lport == 0) {
1638 		anonport = 1;
1639 	}
1640 
1641 	laddr = inp->inp_laddr;
1642 	lport = inp->inp_lport;
1643 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1644 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1645 	if (error && oinp == NULL)
1646 		goto out;
1647 	if (oinp) {
1648 		error = EADDRINUSE;
1649 		goto out;
1650 	}
1651 
1652 	inp->inp_laddr = laddr;
1653 
1654 	if (inp->inp_lport != lport) {
1655 		inp->inp_lport = lport;
1656 		oinp = in_pcblookup(inp->inp_pcbinfo, inp->inp_faddr,
1657 		    inp->inp_fport, laddr, lport, 0, NULL);
1658 		if (oinp != NULL) {
1659 			error = EADDRINUSE;
1660 			goto out;
1661 		}
1662 
1663 		// inp->inp_lport != lport means in_pcbconnect_setup selected new port to inp->inp_lport.
1664 		// inp will inhash.
1665 		if (in_pcbinshash(inp) != 0) {
1666 			inp->inp_laddr.s_addr = INADDR_ANY;
1667 			inp->inp_lport = 0;
1668 			return (EAGAIN);
1669 		}
1670 	}
1671 	else
1672 	{
1673 		// app call bind() and connect(), lport is set when bind, and the inp is inhashed in bind() function.
1674 		// in_pcbconnect_setup() update inp->inp_faddr/inp->inp_fport, so inp should be rehashed.
1675 		in_pcbrehash(inp);
1676 	}
1677 
1678 	if (anonport) {
1679 		inp->inp_flags |= INP_ANONPORT;
1680 	}
1681 #endif
1682 
1683 	INP_HASH_WUNLOCK(&V_tcbinfo);
1684 
1685 	/*
1686 	 * Compute window scaling to request:
1687 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1688 	 * XXX: This should move to tcp_output().
1689 	 */
1690 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1691 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1692 		tp->request_r_scale++;
1693 
1694 	soisconnecting(so);
1695 	TCPSTAT_INC(tcps_connattempt);
1696 	tcp_state_change(tp, TCPS_SYN_SENT);
1697 	tp->iss = tcp_new_isn(&inp->inp_inc);
1698 	if (tp->t_flags & TF_REQ_TSTMP)
1699 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1700 	tcp_sendseqinit(tp);
1701 
1702 	return 0;
1703 
1704 out:
1705 	INP_HASH_WUNLOCK(&V_tcbinfo);
1706 	return (error);
1707 }
1708 #endif /* INET */
1709 
1710 #ifdef INET6
1711 static int
tcp6_connect(struct tcpcb * tp,struct sockaddr * nam,struct thread * td)1712 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1713 {
1714 	struct inpcb *inp = tp->t_inpcb;
1715 	int error;
1716 
1717 	INP_WLOCK_ASSERT(inp);
1718 	INP_HASH_WLOCK(&V_tcbinfo);
1719 
1720 	if (V_tcp_require_unique_port && inp->inp_lport == 0) {
1721 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1722 		if (error)
1723 			goto out;
1724 	}
1725 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1726 	if (error != 0)
1727 		goto out;
1728 	INP_HASH_WUNLOCK(&V_tcbinfo);
1729 
1730 	/* Compute window scaling to request.  */
1731 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1732 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1733 		tp->request_r_scale++;
1734 
1735 	soisconnecting(inp->inp_socket);
1736 	TCPSTAT_INC(tcps_connattempt);
1737 	tcp_state_change(tp, TCPS_SYN_SENT);
1738 	tp->iss = tcp_new_isn(&inp->inp_inc);
1739 	if (tp->t_flags & TF_REQ_TSTMP)
1740 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1741 	tcp_sendseqinit(tp);
1742 
1743 	return 0;
1744 
1745 out:
1746 	INP_HASH_WUNLOCK(&V_tcbinfo);
1747 	return error;
1748 }
1749 #endif /* INET6 */
1750 
1751 /*
1752  * Export TCP internal state information via a struct tcp_info, based on the
1753  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1754  * (TCP state machine, etc).  We export all information using FreeBSD-native
1755  * constants -- for example, the numeric values for tcpi_state will differ
1756  * from Linux.
1757  */
1758 static void
tcp_fill_info(struct tcpcb * tp,struct tcp_info * ti)1759 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1760 {
1761 
1762 	INP_WLOCK_ASSERT(tp->t_inpcb);
1763 	bzero(ti, sizeof(*ti));
1764 
1765 	ti->tcpi_state = tp->t_state;
1766 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1767 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1768 	if (tp->t_flags & TF_SACK_PERMIT)
1769 		ti->tcpi_options |= TCPI_OPT_SACK;
1770 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1771 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1772 		ti->tcpi_snd_wscale = tp->snd_scale;
1773 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1774 	}
1775 	if (tp->t_flags2 & TF2_ECN_PERMIT)
1776 		ti->tcpi_options |= TCPI_OPT_ECN;
1777 
1778 	ti->tcpi_rto = tp->t_rxtcur * tick;
1779 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1780 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1781 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1782 
1783 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1784 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1785 
1786 	/*
1787 	 * FreeBSD-specific extension fields for tcp_info.
1788 	 */
1789 	ti->tcpi_rcv_space = tp->rcv_wnd;
1790 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1791 	ti->tcpi_snd_wnd = tp->snd_wnd;
1792 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1793 	ti->tcpi_snd_nxt = tp->snd_nxt;
1794 	ti->tcpi_snd_mss = tp->t_maxseg;
1795 	ti->tcpi_rcv_mss = tp->t_maxseg;
1796 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1797 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1798 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1799 #ifdef TCP_OFFLOAD
1800 	if (tp->t_flags & TF_TOE) {
1801 		ti->tcpi_options |= TCPI_OPT_TOE;
1802 		tcp_offload_tcp_info(tp, ti);
1803 	}
1804 #endif
1805 }
1806 
1807 /*
1808  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1809  * socket option arguments.  When it re-acquires the lock after the copy, it
1810  * has to revalidate that the connection is still valid for the socket
1811  * option.
1812  */
1813 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
1814 	INP_WLOCK(inp);							\
1815 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
1816 		INP_WUNLOCK(inp);					\
1817 		cleanup;						\
1818 		return (ECONNRESET);					\
1819 	}								\
1820 	tp = intotcpcb(inp);						\
1821 } while(0)
1822 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1823 
1824 int
tcp_ctloutput(struct socket * so,struct sockopt * sopt)1825 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1826 {
1827 	int	error;
1828 	struct	inpcb *inp;
1829 	struct	tcpcb *tp;
1830 	struct tcp_function_block *blk;
1831 	struct tcp_function_set fsn;
1832 
1833 	error = 0;
1834 	inp = sotoinpcb(so);
1835 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1836 	if (sopt->sopt_level != IPPROTO_TCP) {
1837 #ifdef INET6
1838 		if (inp->inp_vflag & INP_IPV6PROTO) {
1839 			error = ip6_ctloutput(so, sopt);
1840 			/*
1841 			 * In case of the IPV6_USE_MIN_MTU socket option,
1842 			 * the INC_IPV6MINMTU flag to announce a corresponding
1843 			 * MSS during the initial handshake.
1844 			 * If the TCP connection is not in the front states,
1845 			 * just reduce the MSS being used.
1846 			 * This avoids the sending of TCP segments which will
1847 			 * be fragmented at the IPv6 layer.
1848 			 */
1849 			if ((error == 0) &&
1850 			    (sopt->sopt_dir == SOPT_SET) &&
1851 			    (sopt->sopt_level == IPPROTO_IPV6) &&
1852 			    (sopt->sopt_name == IPV6_USE_MIN_MTU)) {
1853 				INP_WLOCK(inp);
1854 				if ((inp->inp_flags &
1855 				    (INP_TIMEWAIT | INP_DROPPED))) {
1856 					INP_WUNLOCK(inp);
1857 					return (ECONNRESET);
1858 				}
1859 				inp->inp_inc.inc_flags |= INC_IPV6MINMTU;
1860 				tp = intotcpcb(inp);
1861 				if ((tp->t_state >= TCPS_SYN_SENT) &&
1862 				    (inp->inp_inc.inc_flags & INC_ISIPV6)) {
1863 					struct ip6_pktopts *opt;
1864 
1865 					opt = inp->in6p_outputopts;
1866 					if ((opt != NULL) &&
1867 					    (opt->ip6po_minmtu ==
1868 					    IP6PO_MINMTU_ALL)) {
1869 						if (tp->t_maxseg > TCP6_MSS) {
1870 							tp->t_maxseg = TCP6_MSS;
1871 						}
1872 					}
1873 				}
1874 				INP_WUNLOCK(inp);
1875 			}
1876 		}
1877 #endif /* INET6 */
1878 #if defined(INET6) && defined(INET)
1879 		else
1880 #endif
1881 #ifdef INET
1882 		{
1883 			error = ip_ctloutput(so, sopt);
1884 		}
1885 #endif
1886 		return (error);
1887 	}
1888 	INP_WLOCK(inp);
1889 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1890 		INP_WUNLOCK(inp);
1891 		return (ECONNRESET);
1892 	}
1893 	tp = intotcpcb(inp);
1894 	/*
1895 	 * Protect the TCP option TCP_FUNCTION_BLK so
1896 	 * that a sub-function can *never* overwrite this.
1897 	 */
1898 	if ((sopt->sopt_dir == SOPT_SET) &&
1899 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1900 		INP_WUNLOCK(inp);
1901 		error = sooptcopyin(sopt, &fsn, sizeof fsn,
1902 		    sizeof fsn);
1903 		if (error)
1904 			return (error);
1905 		INP_WLOCK_RECHECK(inp);
1906 		blk = find_and_ref_tcp_functions(&fsn);
1907 		if (blk == NULL) {
1908 			INP_WUNLOCK(inp);
1909 			return (ENOENT);
1910 		}
1911 		if (tp->t_fb == blk) {
1912 			/* You already have this */
1913 			refcount_release(&blk->tfb_refcnt);
1914 			INP_WUNLOCK(inp);
1915 			return (0);
1916 		}
1917 		if (tp->t_state != TCPS_CLOSED) {
1918 			/*
1919 			 * The user has advanced the state
1920 			 * past the initial point, we may not
1921 			 * be able to switch.
1922 			 */
1923 			if (blk->tfb_tcp_handoff_ok != NULL) {
1924 				/*
1925 				 * Does the stack provide a
1926 				 * query mechanism, if so it may
1927 				 * still be possible?
1928 				 */
1929 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1930 			} else
1931 				error = EINVAL;
1932 			if (error) {
1933 				refcount_release(&blk->tfb_refcnt);
1934 				INP_WUNLOCK(inp);
1935 				return(error);
1936 			}
1937 		}
1938 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1939 			refcount_release(&blk->tfb_refcnt);
1940 			INP_WUNLOCK(inp);
1941 			return (ENOENT);
1942 		}
1943 		/*
1944 		 * Release the old refcnt, the
1945 		 * lookup acquired a ref on the
1946 		 * new one already.
1947 		 */
1948 		if (tp->t_fb->tfb_tcp_fb_fini) {
1949 			/*
1950 			 * Tell the stack to cleanup with 0 i.e.
1951 			 * the tcb is not going away.
1952 			 */
1953 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1954 		}
1955 #ifdef TCPHPTS
1956 		/* Assure that we are not on any hpts */
1957 		tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
1958 #endif
1959 		if (blk->tfb_tcp_fb_init) {
1960 			error = (*blk->tfb_tcp_fb_init)(tp);
1961 			if (error) {
1962 				refcount_release(&blk->tfb_refcnt);
1963 				if (tp->t_fb->tfb_tcp_fb_init) {
1964 					if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0)  {
1965 						/* Fall back failed, drop the connection */
1966 						INP_WUNLOCK(inp);
1967 						soabort(so);
1968 						return(error);
1969 					}
1970 				}
1971 				goto err_out;
1972 			}
1973 		}
1974 		refcount_release(&tp->t_fb->tfb_refcnt);
1975 		tp->t_fb = blk;
1976 #ifdef TCP_OFFLOAD
1977 		if (tp->t_flags & TF_TOE) {
1978 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
1979 			     sopt->sopt_name);
1980 		}
1981 #endif
1982 err_out:
1983 		INP_WUNLOCK(inp);
1984 		return (error);
1985 	} else if ((sopt->sopt_dir == SOPT_GET) &&
1986 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1987 		strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1988 		    TCP_FUNCTION_NAME_LEN_MAX);
1989 		fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1990 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1991 		INP_WUNLOCK(inp);
1992 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
1993 		return (error);
1994 	}
1995 	/* Pass in the INP locked, called must unlock it */
1996 	return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
1997 }
1998 
1999 /*
2000  * If this assert becomes untrue, we need to change the size of the buf
2001  * variable in tcp_default_ctloutput().
2002  */
2003 #ifdef CTASSERT
2004 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
2005 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
2006 #endif
2007 
2008 #ifdef KERN_TLS
2009 static int
copyin_tls_enable(struct sockopt * sopt,struct tls_enable * tls)2010 copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
2011 {
2012 	struct tls_enable_v0 tls_v0;
2013 	int error;
2014 
2015 	if (sopt->sopt_valsize == sizeof(tls_v0)) {
2016 		error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
2017 		    sizeof(tls_v0));
2018 		if (error)
2019 			return (error);
2020 		memset(tls, 0, sizeof(*tls));
2021 		tls->cipher_key = tls_v0.cipher_key;
2022 		tls->iv = tls_v0.iv;
2023 		tls->auth_key = tls_v0.auth_key;
2024 		tls->cipher_algorithm = tls_v0.cipher_algorithm;
2025 		tls->cipher_key_len = tls_v0.cipher_key_len;
2026 		tls->iv_len = tls_v0.iv_len;
2027 		tls->auth_algorithm = tls_v0.auth_algorithm;
2028 		tls->auth_key_len = tls_v0.auth_key_len;
2029 		tls->flags = tls_v0.flags;
2030 		tls->tls_vmajor = tls_v0.tls_vmajor;
2031 		tls->tls_vminor = tls_v0.tls_vminor;
2032 		return (0);
2033 	}
2034 
2035 	return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
2036 }
2037 #endif
2038 
2039 int
tcp_default_ctloutput(struct socket * so,struct sockopt * sopt,struct inpcb * inp,struct tcpcb * tp)2040 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
2041 {
2042 	int	error, opt, optval;
2043 	u_int	ui;
2044 	struct	tcp_info ti;
2045 #ifdef KERN_TLS
2046 	struct tls_enable tls;
2047 #endif
2048 	struct cc_algo *algo;
2049 	char	*pbuf, buf[TCP_LOG_ID_LEN];
2050 #ifdef STATS
2051 	struct statsblob *sbp;
2052 #endif
2053 	size_t	len;
2054 
2055 	/*
2056 	 * For TCP_CCALGOOPT forward the control to CC module, for both
2057 	 * SOPT_SET and SOPT_GET.
2058 	 */
2059 	switch (sopt->sopt_name) {
2060 	case TCP_CCALGOOPT:
2061 		INP_WUNLOCK(inp);
2062 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2063 			return (EINVAL);
2064 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2065 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2066 		    sopt->sopt_valsize);
2067 		if (error) {
2068 			free(pbuf, M_TEMP);
2069 			return (error);
2070 		}
2071 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2072 		if (CC_ALGO(tp)->ctl_output != NULL)
2073 			error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
2074 		else
2075 			error = ENOENT;
2076 		INP_WUNLOCK(inp);
2077 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
2078 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2079 		free(pbuf, M_TEMP);
2080 		return (error);
2081 	}
2082 
2083 	switch (sopt->sopt_dir) {
2084 	case SOPT_SET:
2085 		switch (sopt->sopt_name) {
2086 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2087 		case TCP_MD5SIG:
2088 			if (!TCPMD5_ENABLED()) {
2089 				INP_WUNLOCK(inp);
2090 				return (ENOPROTOOPT);
2091 			}
2092 			error = TCPMD5_PCBCTL(inp, sopt);
2093 			if (error)
2094 				return (error);
2095 			goto unlock_and_done;
2096 #endif /* IPSEC */
2097 
2098 		case TCP_NODELAY:
2099 		case TCP_NOOPT:
2100 			INP_WUNLOCK(inp);
2101 			error = sooptcopyin(sopt, &optval, sizeof optval,
2102 			    sizeof optval);
2103 			if (error)
2104 				return (error);
2105 
2106 			INP_WLOCK_RECHECK(inp);
2107 			switch (sopt->sopt_name) {
2108 			case TCP_NODELAY:
2109 				opt = TF_NODELAY;
2110 				break;
2111 			case TCP_NOOPT:
2112 				opt = TF_NOOPT;
2113 				break;
2114 			default:
2115 				opt = 0; /* dead code to fool gcc */
2116 				break;
2117 			}
2118 
2119 			if (optval)
2120 				tp->t_flags |= opt;
2121 			else
2122 				tp->t_flags &= ~opt;
2123 unlock_and_done:
2124 #ifdef TCP_OFFLOAD
2125 			if (tp->t_flags & TF_TOE) {
2126 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
2127 				    sopt->sopt_name);
2128 			}
2129 #endif
2130 			INP_WUNLOCK(inp);
2131 			break;
2132 
2133 		case TCP_NOPUSH:
2134 			INP_WUNLOCK(inp);
2135 			error = sooptcopyin(sopt, &optval, sizeof optval,
2136 			    sizeof optval);
2137 			if (error)
2138 				return (error);
2139 
2140 			INP_WLOCK_RECHECK(inp);
2141 			if (optval)
2142 				tp->t_flags |= TF_NOPUSH;
2143 			else if (tp->t_flags & TF_NOPUSH) {
2144 				tp->t_flags &= ~TF_NOPUSH;
2145 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2146 					struct epoch_tracker et;
2147 
2148 					NET_EPOCH_ENTER(et);
2149 					error = tp->t_fb->tfb_tcp_output(tp);
2150 					NET_EPOCH_EXIT(et);
2151 				}
2152 			}
2153 			goto unlock_and_done;
2154 
2155 		case TCP_MAXSEG:
2156 			INP_WUNLOCK(inp);
2157 			error = sooptcopyin(sopt, &optval, sizeof optval,
2158 			    sizeof optval);
2159 			if (error)
2160 				return (error);
2161 
2162 			INP_WLOCK_RECHECK(inp);
2163 			if (optval > 0 && optval <= tp->t_maxseg &&
2164 			    optval + 40 >= V_tcp_minmss)
2165 				tp->t_maxseg = optval;
2166 			else
2167 				error = EINVAL;
2168 			goto unlock_and_done;
2169 
2170 		case TCP_INFO:
2171 			INP_WUNLOCK(inp);
2172 			error = EINVAL;
2173 			break;
2174 
2175 		case TCP_STATS:
2176 			INP_WUNLOCK(inp);
2177 #ifdef STATS
2178 			error = sooptcopyin(sopt, &optval, sizeof optval,
2179 			    sizeof optval);
2180 			if (error)
2181 				return (error);
2182 
2183 			if (optval > 0)
2184 				sbp = stats_blob_alloc(
2185 				    V_tcp_perconn_stats_dflt_tpl, 0);
2186 			else
2187 				sbp = NULL;
2188 
2189 			INP_WLOCK_RECHECK(inp);
2190 			if ((tp->t_stats != NULL && sbp == NULL) ||
2191 			    (tp->t_stats == NULL && sbp != NULL)) {
2192 				struct statsblob *t = tp->t_stats;
2193 				tp->t_stats = sbp;
2194 				sbp = t;
2195 			}
2196 			INP_WUNLOCK(inp);
2197 
2198 			stats_blob_destroy(sbp);
2199 #else
2200 			return (EOPNOTSUPP);
2201 #endif /* !STATS */
2202 			break;
2203 
2204 		case TCP_CONGESTION:
2205 			INP_WUNLOCK(inp);
2206 			error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
2207 			if (error)
2208 				break;
2209 			buf[sopt->sopt_valsize] = '\0';
2210 			INP_WLOCK_RECHECK(inp);
2211 			CC_LIST_RLOCK();
2212 			STAILQ_FOREACH(algo, &cc_list, entries)
2213 				if (strncmp(buf, algo->name,
2214 				    TCP_CA_NAME_MAX) == 0)
2215 					break;
2216 			CC_LIST_RUNLOCK();
2217 			if (algo == NULL) {
2218 				INP_WUNLOCK(inp);
2219 				error = EINVAL;
2220 				break;
2221 			}
2222 			/*
2223 			 * We hold a write lock over the tcb so it's safe to
2224 			 * do these things without ordering concerns.
2225 			 */
2226 			if (CC_ALGO(tp)->cb_destroy != NULL)
2227 				CC_ALGO(tp)->cb_destroy(tp->ccv);
2228 			CC_DATA(tp) = NULL;
2229 			CC_ALGO(tp) = algo;
2230 			/*
2231 			 * If something goes pear shaped initialising the new
2232 			 * algo, fall back to newreno (which does not
2233 			 * require initialisation).
2234 			 */
2235 			if (algo->cb_init != NULL &&
2236 			    algo->cb_init(tp->ccv) != 0) {
2237 				CC_ALGO(tp) = &newreno_cc_algo;
2238 				/*
2239 				 * The only reason init should fail is
2240 				 * because of malloc.
2241 				 */
2242 				error = ENOMEM;
2243 			}
2244 			INP_WUNLOCK(inp);
2245 			break;
2246 
2247 		case TCP_REUSPORT_LB_NUMA:
2248 			INP_WUNLOCK(inp);
2249 			error = sooptcopyin(sopt, &optval, sizeof(optval),
2250 			    sizeof(optval));
2251 			INP_WLOCK_RECHECK(inp);
2252 			if (!error)
2253 				error = in_pcblbgroup_numa(inp, optval);
2254 			INP_WUNLOCK(inp);
2255 			break;
2256 
2257 #ifdef KERN_TLS
2258 		case TCP_TXTLS_ENABLE:
2259 			INP_WUNLOCK(inp);
2260 			error = copyin_tls_enable(sopt, &tls);
2261 			if (error)
2262 				break;
2263 			error = ktls_enable_tx(so, &tls);
2264 			break;
2265 		case TCP_TXTLS_MODE:
2266 			INP_WUNLOCK(inp);
2267 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2268 			if (error)
2269 				return (error);
2270 
2271 			INP_WLOCK_RECHECK(inp);
2272 			error = ktls_set_tx_mode(so, ui);
2273 			INP_WUNLOCK(inp);
2274 			break;
2275 		case TCP_RXTLS_ENABLE:
2276 			INP_WUNLOCK(inp);
2277 			error = sooptcopyin(sopt, &tls, sizeof(tls),
2278 			    sizeof(tls));
2279 			if (error)
2280 				break;
2281 			error = ktls_enable_rx(so, &tls);
2282 			break;
2283 #endif
2284 
2285 		case TCP_KEEPIDLE:
2286 		case TCP_KEEPINTVL:
2287 		case TCP_KEEPINIT:
2288 			INP_WUNLOCK(inp);
2289 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2290 			if (error)
2291 				return (error);
2292 
2293 			if (ui > (UINT_MAX / hz)) {
2294 				error = EINVAL;
2295 				break;
2296 			}
2297 			ui *= hz;
2298 
2299 			INP_WLOCK_RECHECK(inp);
2300 			switch (sopt->sopt_name) {
2301 			case TCP_KEEPIDLE:
2302 				tp->t_keepidle = ui;
2303 				/*
2304 				 * XXX: better check current remaining
2305 				 * timeout and "merge" it with new value.
2306 				 */
2307 				if ((tp->t_state > TCPS_LISTEN) &&
2308 				    (tp->t_state <= TCPS_CLOSING))
2309 					tcp_timer_activate(tp, TT_KEEP,
2310 					    TP_KEEPIDLE(tp));
2311 				break;
2312 			case TCP_KEEPINTVL:
2313 				tp->t_keepintvl = ui;
2314 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2315 				    (TP_MAXIDLE(tp) > 0))
2316 					tcp_timer_activate(tp, TT_2MSL,
2317 					    TP_MAXIDLE(tp));
2318 				break;
2319 			case TCP_KEEPINIT:
2320 				tp->t_keepinit = ui;
2321 				if (tp->t_state == TCPS_SYN_RECEIVED ||
2322 				    tp->t_state == TCPS_SYN_SENT)
2323 					tcp_timer_activate(tp, TT_KEEP,
2324 					    TP_KEEPINIT(tp));
2325 				break;
2326 			}
2327 			goto unlock_and_done;
2328 
2329 		case TCP_KEEPCNT:
2330 			INP_WUNLOCK(inp);
2331 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2332 			if (error)
2333 				return (error);
2334 
2335 			INP_WLOCK_RECHECK(inp);
2336 			tp->t_keepcnt = ui;
2337 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
2338 			    (TP_MAXIDLE(tp) > 0))
2339 				tcp_timer_activate(tp, TT_2MSL,
2340 				    TP_MAXIDLE(tp));
2341 			goto unlock_and_done;
2342 
2343 #ifdef TCPPCAP
2344 		case TCP_PCAP_OUT:
2345 		case TCP_PCAP_IN:
2346 			INP_WUNLOCK(inp);
2347 			error = sooptcopyin(sopt, &optval, sizeof optval,
2348 			    sizeof optval);
2349 			if (error)
2350 				return (error);
2351 
2352 			INP_WLOCK_RECHECK(inp);
2353 			if (optval >= 0)
2354 				tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
2355 					&(tp->t_outpkts) : &(tp->t_inpkts),
2356 					optval);
2357 			else
2358 				error = EINVAL;
2359 			goto unlock_and_done;
2360 #endif
2361 
2362 		case TCP_FASTOPEN: {
2363 			struct tcp_fastopen tfo_optval;
2364 
2365 			INP_WUNLOCK(inp);
2366 			if (!V_tcp_fastopen_client_enable &&
2367 			    !V_tcp_fastopen_server_enable)
2368 				return (EPERM);
2369 
2370 			error = sooptcopyin(sopt, &tfo_optval,
2371 				    sizeof(tfo_optval), sizeof(int));
2372 			if (error)
2373 				return (error);
2374 
2375 			INP_WLOCK_RECHECK(inp);
2376 			if ((tp->t_state != TCPS_CLOSED) &&
2377 			    (tp->t_state != TCPS_LISTEN)) {
2378 				error = EINVAL;
2379 				goto unlock_and_done;
2380 			}
2381 			if (tfo_optval.enable) {
2382 				if (tp->t_state == TCPS_LISTEN) {
2383 					if (!V_tcp_fastopen_server_enable) {
2384 						error = EPERM;
2385 						goto unlock_and_done;
2386 					}
2387 
2388 					if (tp->t_tfo_pending == NULL)
2389 						tp->t_tfo_pending =
2390 						    tcp_fastopen_alloc_counter();
2391 				} else {
2392 					/*
2393 					 * If a pre-shared key was provided,
2394 					 * stash it in the client cookie
2395 					 * field of the tcpcb for use during
2396 					 * connect.
2397 					 */
2398 					if (sopt->sopt_valsize ==
2399 					    sizeof(tfo_optval)) {
2400 						memcpy(tp->t_tfo_cookie.client,
2401 						       tfo_optval.psk,
2402 						       TCP_FASTOPEN_PSK_LEN);
2403 						tp->t_tfo_client_cookie_len =
2404 						    TCP_FASTOPEN_PSK_LEN;
2405 					}
2406 				}
2407 				tp->t_flags |= TF_FASTOPEN;
2408 			} else
2409 				tp->t_flags &= ~TF_FASTOPEN;
2410 			goto unlock_and_done;
2411 		}
2412 
2413 #ifdef TCP_BLACKBOX
2414 		case TCP_LOG:
2415 			INP_WUNLOCK(inp);
2416 			error = sooptcopyin(sopt, &optval, sizeof optval,
2417 			    sizeof optval);
2418 			if (error)
2419 				return (error);
2420 
2421 			INP_WLOCK_RECHECK(inp);
2422 			error = tcp_log_state_change(tp, optval);
2423 			goto unlock_and_done;
2424 
2425 		case TCP_LOGBUF:
2426 			INP_WUNLOCK(inp);
2427 			error = EINVAL;
2428 			break;
2429 
2430 		case TCP_LOGID:
2431 			INP_WUNLOCK(inp);
2432 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
2433 			if (error)
2434 				break;
2435 			buf[sopt->sopt_valsize] = '\0';
2436 			INP_WLOCK_RECHECK(inp);
2437 			error = tcp_log_set_id(tp, buf);
2438 			/* tcp_log_set_id() unlocks the INP. */
2439 			break;
2440 
2441 		case TCP_LOGDUMP:
2442 		case TCP_LOGDUMPID:
2443 			INP_WUNLOCK(inp);
2444 			error =
2445 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
2446 			if (error)
2447 				break;
2448 			buf[sopt->sopt_valsize] = '\0';
2449 			INP_WLOCK_RECHECK(inp);
2450 			if (sopt->sopt_name == TCP_LOGDUMP) {
2451 				error = tcp_log_dump_tp_logbuf(tp, buf,
2452 				    M_WAITOK, true);
2453 				INP_WUNLOCK(inp);
2454 			} else {
2455 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
2456 				/*
2457 				 * tcp_log_dump_tp_bucket_logbufs() drops the
2458 				 * INP lock.
2459 				 */
2460 			}
2461 			break;
2462 #endif
2463 
2464 		default:
2465 			INP_WUNLOCK(inp);
2466 			error = ENOPROTOOPT;
2467 			break;
2468 		}
2469 		break;
2470 
2471 	case SOPT_GET:
2472 		tp = intotcpcb(inp);
2473 		switch (sopt->sopt_name) {
2474 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
2475 		case TCP_MD5SIG:
2476 			if (!TCPMD5_ENABLED()) {
2477 				INP_WUNLOCK(inp);
2478 				return (ENOPROTOOPT);
2479 			}
2480 			error = TCPMD5_PCBCTL(inp, sopt);
2481 			break;
2482 #endif
2483 
2484 		case TCP_NODELAY:
2485 			optval = tp->t_flags & TF_NODELAY;
2486 			INP_WUNLOCK(inp);
2487 			error = sooptcopyout(sopt, &optval, sizeof optval);
2488 			break;
2489 		case TCP_MAXSEG:
2490 			optval = tp->t_maxseg;
2491 			INP_WUNLOCK(inp);
2492 			error = sooptcopyout(sopt, &optval, sizeof optval);
2493 			break;
2494 		case TCP_NOOPT:
2495 			optval = tp->t_flags & TF_NOOPT;
2496 			INP_WUNLOCK(inp);
2497 			error = sooptcopyout(sopt, &optval, sizeof optval);
2498 			break;
2499 		case TCP_NOPUSH:
2500 			optval = tp->t_flags & TF_NOPUSH;
2501 			INP_WUNLOCK(inp);
2502 			error = sooptcopyout(sopt, &optval, sizeof optval);
2503 			break;
2504 		case TCP_INFO:
2505 			tcp_fill_info(tp, &ti);
2506 			INP_WUNLOCK(inp);
2507 			error = sooptcopyout(sopt, &ti, sizeof ti);
2508 			break;
2509 		case TCP_STATS:
2510 			{
2511 #ifdef STATS
2512 			int nheld;
2513 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2514 
2515 			error = 0;
2516 			socklen_t outsbsz = sopt->sopt_valsize;
2517 			if (tp->t_stats == NULL)
2518 				error = ENOENT;
2519 			else if (outsbsz >= tp->t_stats->cursz)
2520 				outsbsz = tp->t_stats->cursz;
2521 			else if (outsbsz >= sizeof(struct statsblob))
2522 				outsbsz = sizeof(struct statsblob);
2523 			else
2524 				error = EINVAL;
2525 			INP_WUNLOCK(inp);
2526 			if (error)
2527 				break;
2528 
2529 			sbp = sopt->sopt_val;
2530 			nheld = atop(round_page(((vm_offset_t)sbp) +
2531 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2532 			vm_page_t ma[nheld];
2533 			if (vm_fault_quick_hold_pages(
2534 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2535 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2536 			    nheld) < 0) {
2537 				error = EFAULT;
2538 				break;
2539 			}
2540 
2541 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2542 			    SIZEOF_MEMBER(struct statsblob, flags))))
2543 				goto unhold;
2544 
2545 			INP_WLOCK_RECHECK(inp);
2546 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2547 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2548 			INP_WUNLOCK(inp);
2549 			sopt->sopt_valsize = outsbsz;
2550 unhold:
2551 			vm_page_unhold_pages(ma, nheld);
2552 #else
2553 			INP_WUNLOCK(inp);
2554 			error = EOPNOTSUPP;
2555 #endif /* !STATS */
2556 			break;
2557 			}
2558 		case TCP_CONGESTION:
2559 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2560 			INP_WUNLOCK(inp);
2561 			error = sooptcopyout(sopt, buf, len + 1);
2562 			break;
2563 		case TCP_KEEPIDLE:
2564 		case TCP_KEEPINTVL:
2565 		case TCP_KEEPINIT:
2566 		case TCP_KEEPCNT:
2567 			switch (sopt->sopt_name) {
2568 			case TCP_KEEPIDLE:
2569 				ui = TP_KEEPIDLE(tp) / hz;
2570 				break;
2571 			case TCP_KEEPINTVL:
2572 				ui = TP_KEEPINTVL(tp) / hz;
2573 				break;
2574 			case TCP_KEEPINIT:
2575 				ui = TP_KEEPINIT(tp) / hz;
2576 				break;
2577 			case TCP_KEEPCNT:
2578 				ui = TP_KEEPCNT(tp);
2579 				break;
2580 			}
2581 			INP_WUNLOCK(inp);
2582 			error = sooptcopyout(sopt, &ui, sizeof(ui));
2583 			break;
2584 #ifdef TCPPCAP
2585 		case TCP_PCAP_OUT:
2586 		case TCP_PCAP_IN:
2587 			optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
2588 					&(tp->t_outpkts) : &(tp->t_inpkts));
2589 			INP_WUNLOCK(inp);
2590 			error = sooptcopyout(sopt, &optval, sizeof optval);
2591 			break;
2592 #endif
2593 		case TCP_FASTOPEN:
2594 			optval = tp->t_flags & TF_FASTOPEN;
2595 			INP_WUNLOCK(inp);
2596 			error = sooptcopyout(sopt, &optval, sizeof optval);
2597 			break;
2598 #ifdef TCP_BLACKBOX
2599 		case TCP_LOG:
2600 			optval = tp->t_logstate;
2601 			INP_WUNLOCK(inp);
2602 			error = sooptcopyout(sopt, &optval, sizeof(optval));
2603 			break;
2604 		case TCP_LOGBUF:
2605 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2606 			error = tcp_log_getlogbuf(sopt, tp);
2607 			break;
2608 		case TCP_LOGID:
2609 			len = tcp_log_get_id(tp, buf);
2610 			INP_WUNLOCK(inp);
2611 			error = sooptcopyout(sopt, buf, len + 1);
2612 			break;
2613 		case TCP_LOGDUMP:
2614 		case TCP_LOGDUMPID:
2615 			INP_WUNLOCK(inp);
2616 			error = EINVAL;
2617 			break;
2618 #endif
2619 #ifdef KERN_TLS
2620 		case TCP_TXTLS_MODE:
2621 			optval = ktls_get_tx_mode(so);
2622 			INP_WUNLOCK(inp);
2623 			error = sooptcopyout(sopt, &optval, sizeof(optval));
2624 			break;
2625 		case TCP_RXTLS_MODE:
2626 			optval = ktls_get_rx_mode(so);
2627 			INP_WUNLOCK(inp);
2628 			error = sooptcopyout(sopt, &optval, sizeof(optval));
2629 			break;
2630 #endif
2631 		default:
2632 			INP_WUNLOCK(inp);
2633 			error = ENOPROTOOPT;
2634 			break;
2635 		}
2636 		break;
2637 	}
2638 	return (error);
2639 }
2640 #undef INP_WLOCK_RECHECK
2641 #undef INP_WLOCK_RECHECK_CLEANUP
2642 
2643 /*
2644  * Initiate (or continue) disconnect.
2645  * If embryonic state, just send reset (once).
2646  * If in ``let data drain'' option and linger null, just drop.
2647  * Otherwise (hard), mark socket disconnecting and drop
2648  * current input data; switch states based on user close, and
2649  * send segment to peer (with FIN).
2650  */
2651 static void
tcp_disconnect(struct tcpcb * tp)2652 tcp_disconnect(struct tcpcb *tp)
2653 {
2654 	struct inpcb *inp = tp->t_inpcb;
2655 	struct socket *so = inp->inp_socket;
2656 
2657 	NET_EPOCH_ASSERT();
2658 	INP_WLOCK_ASSERT(inp);
2659 
2660 	/*
2661 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2662 	 * socket is still open.
2663 	 */
2664 	if (tp->t_state < TCPS_ESTABLISHED &&
2665 	    !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2666 		tp = tcp_close(tp);
2667 		KASSERT(tp != NULL,
2668 		    ("tcp_disconnect: tcp_close() returned NULL"));
2669 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2670 		tp = tcp_drop(tp, 0);
2671 		KASSERT(tp != NULL,
2672 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2673 	} else {
2674 		soisdisconnecting(so);
2675 		sbflush(&so->so_rcv);
2676 		tcp_usrclosed(tp);
2677 		if (!(inp->inp_flags & INP_DROPPED))
2678 			tp->t_fb->tfb_tcp_output(tp);
2679 	}
2680 }
2681 
2682 /*
2683  * User issued close, and wish to trail through shutdown states:
2684  * if never received SYN, just forget it.  If got a SYN from peer,
2685  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2686  * If already got a FIN from peer, then almost done; go to LAST_ACK
2687  * state.  In all other cases, have already sent FIN to peer (e.g.
2688  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2689  * for peer to send FIN or not respond to keep-alives, etc.
2690  * We can let the user exit from the close as soon as the FIN is acked.
2691  */
2692 static void
tcp_usrclosed(struct tcpcb * tp)2693 tcp_usrclosed(struct tcpcb *tp)
2694 {
2695 
2696 	NET_EPOCH_ASSERT();
2697 	INP_WLOCK_ASSERT(tp->t_inpcb);
2698 
2699 	switch (tp->t_state) {
2700 	case TCPS_LISTEN:
2701 #ifdef TCP_OFFLOAD
2702 		tcp_offload_listen_stop(tp);
2703 #endif
2704 		tcp_state_change(tp, TCPS_CLOSED);
2705 		/* FALLTHROUGH */
2706 	case TCPS_CLOSED:
2707 		tp = tcp_close(tp);
2708 		/*
2709 		 * tcp_close() should never return NULL here as the socket is
2710 		 * still open.
2711 		 */
2712 		KASSERT(tp != NULL,
2713 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2714 		break;
2715 
2716 	case TCPS_SYN_SENT:
2717 	case TCPS_SYN_RECEIVED:
2718 		tp->t_flags |= TF_NEEDFIN;
2719 		break;
2720 
2721 	case TCPS_ESTABLISHED:
2722 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2723 		break;
2724 
2725 	case TCPS_CLOSE_WAIT:
2726 		tcp_state_change(tp, TCPS_LAST_ACK);
2727 		break;
2728 	}
2729 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
2730 		soisdisconnected(tp->t_inpcb->inp_socket);
2731 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
2732 		if (tp->t_state == TCPS_FIN_WAIT_2) {
2733 			int timeout;
2734 
2735 			timeout = (tcp_fast_finwait2_recycle) ?
2736 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2737 			tcp_timer_activate(tp, TT_2MSL, timeout);
2738 		}
2739 	}
2740 }
2741 
2742 #ifdef DDB
2743 static void
db_print_indent(int indent)2744 db_print_indent(int indent)
2745 {
2746 	int i;
2747 
2748 	for (i = 0; i < indent; i++)
2749 		db_printf(" ");
2750 }
2751 
2752 static void
db_print_tstate(int t_state)2753 db_print_tstate(int t_state)
2754 {
2755 
2756 	switch (t_state) {
2757 	case TCPS_CLOSED:
2758 		db_printf("TCPS_CLOSED");
2759 		return;
2760 
2761 	case TCPS_LISTEN:
2762 		db_printf("TCPS_LISTEN");
2763 		return;
2764 
2765 	case TCPS_SYN_SENT:
2766 		db_printf("TCPS_SYN_SENT");
2767 		return;
2768 
2769 	case TCPS_SYN_RECEIVED:
2770 		db_printf("TCPS_SYN_RECEIVED");
2771 		return;
2772 
2773 	case TCPS_ESTABLISHED:
2774 		db_printf("TCPS_ESTABLISHED");
2775 		return;
2776 
2777 	case TCPS_CLOSE_WAIT:
2778 		db_printf("TCPS_CLOSE_WAIT");
2779 		return;
2780 
2781 	case TCPS_FIN_WAIT_1:
2782 		db_printf("TCPS_FIN_WAIT_1");
2783 		return;
2784 
2785 	case TCPS_CLOSING:
2786 		db_printf("TCPS_CLOSING");
2787 		return;
2788 
2789 	case TCPS_LAST_ACK:
2790 		db_printf("TCPS_LAST_ACK");
2791 		return;
2792 
2793 	case TCPS_FIN_WAIT_2:
2794 		db_printf("TCPS_FIN_WAIT_2");
2795 		return;
2796 
2797 	case TCPS_TIME_WAIT:
2798 		db_printf("TCPS_TIME_WAIT");
2799 		return;
2800 
2801 	default:
2802 		db_printf("unknown");
2803 		return;
2804 	}
2805 }
2806 
2807 static void
db_print_tflags(u_int t_flags)2808 db_print_tflags(u_int t_flags)
2809 {
2810 	int comma;
2811 
2812 	comma = 0;
2813 	if (t_flags & TF_ACKNOW) {
2814 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2815 		comma = 1;
2816 	}
2817 	if (t_flags & TF_DELACK) {
2818 		db_printf("%sTF_DELACK", comma ? ", " : "");
2819 		comma = 1;
2820 	}
2821 	if (t_flags & TF_NODELAY) {
2822 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2823 		comma = 1;
2824 	}
2825 	if (t_flags & TF_NOOPT) {
2826 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2827 		comma = 1;
2828 	}
2829 	if (t_flags & TF_SENTFIN) {
2830 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2831 		comma = 1;
2832 	}
2833 	if (t_flags & TF_REQ_SCALE) {
2834 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2835 		comma = 1;
2836 	}
2837 	if (t_flags & TF_RCVD_SCALE) {
2838 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2839 		comma = 1;
2840 	}
2841 	if (t_flags & TF_REQ_TSTMP) {
2842 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2843 		comma = 1;
2844 	}
2845 	if (t_flags & TF_RCVD_TSTMP) {
2846 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2847 		comma = 1;
2848 	}
2849 	if (t_flags & TF_SACK_PERMIT) {
2850 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2851 		comma = 1;
2852 	}
2853 	if (t_flags & TF_NEEDSYN) {
2854 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2855 		comma = 1;
2856 	}
2857 	if (t_flags & TF_NEEDFIN) {
2858 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2859 		comma = 1;
2860 	}
2861 	if (t_flags & TF_NOPUSH) {
2862 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2863 		comma = 1;
2864 	}
2865 	if (t_flags & TF_MORETOCOME) {
2866 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2867 		comma = 1;
2868 	}
2869 	if (t_flags & TF_LQ_OVERFLOW) {
2870 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2871 		comma = 1;
2872 	}
2873 	if (t_flags & TF_LASTIDLE) {
2874 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2875 		comma = 1;
2876 	}
2877 	if (t_flags & TF_RXWIN0SENT) {
2878 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2879 		comma = 1;
2880 	}
2881 	if (t_flags & TF_FASTRECOVERY) {
2882 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2883 		comma = 1;
2884 	}
2885 	if (t_flags & TF_CONGRECOVERY) {
2886 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2887 		comma = 1;
2888 	}
2889 	if (t_flags & TF_WASFRECOVERY) {
2890 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2891 		comma = 1;
2892 	}
2893 	if (t_flags & TF_SIGNATURE) {
2894 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2895 		comma = 1;
2896 	}
2897 	if (t_flags & TF_FORCEDATA) {
2898 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2899 		comma = 1;
2900 	}
2901 	if (t_flags & TF_TSO) {
2902 		db_printf("%sTF_TSO", comma ? ", " : "");
2903 		comma = 1;
2904 	}
2905 	if (t_flags & TF_FASTOPEN) {
2906 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2907 		comma = 1;
2908 	}
2909 }
2910 
2911 static void
db_print_tflags2(u_int t_flags2)2912 db_print_tflags2(u_int t_flags2)
2913 {
2914 	int comma;
2915 
2916 	comma = 0;
2917 	if (t_flags2 & TF2_ECN_PERMIT) {
2918 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
2919 		comma = 1;
2920 	}
2921 }
2922 
2923 static void
db_print_toobflags(char t_oobflags)2924 db_print_toobflags(char t_oobflags)
2925 {
2926 	int comma;
2927 
2928 	comma = 0;
2929 	if (t_oobflags & TCPOOB_HAVEDATA) {
2930 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2931 		comma = 1;
2932 	}
2933 	if (t_oobflags & TCPOOB_HADDATA) {
2934 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2935 		comma = 1;
2936 	}
2937 }
2938 
2939 static void
db_print_tcpcb(struct tcpcb * tp,const char * name,int indent)2940 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2941 {
2942 
2943 	db_print_indent(indent);
2944 	db_printf("%s at %p\n", name, tp);
2945 
2946 	indent += 2;
2947 
2948 	db_print_indent(indent);
2949 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
2950 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2951 
2952 	db_print_indent(indent);
2953 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
2954 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2955 
2956 	db_print_indent(indent);
2957 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2958 	    &tp->t_timers->tt_delack, tp->t_inpcb);
2959 
2960 	db_print_indent(indent);
2961 	db_printf("t_state: %d (", tp->t_state);
2962 	db_print_tstate(tp->t_state);
2963 	db_printf(")\n");
2964 
2965 	db_print_indent(indent);
2966 	db_printf("t_flags: 0x%x (", tp->t_flags);
2967 	db_print_tflags(tp->t_flags);
2968 	db_printf(")\n");
2969 
2970 	db_print_indent(indent);
2971 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
2972 	db_print_tflags2(tp->t_flags2);
2973 	db_printf(")\n");
2974 
2975 	db_print_indent(indent);
2976 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
2977 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
2978 
2979 	db_print_indent(indent);
2980 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
2981 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2982 
2983 	db_print_indent(indent);
2984 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
2985 	    tp->iss, tp->irs, tp->rcv_nxt);
2986 
2987 	db_print_indent(indent);
2988 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
2989 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2990 
2991 	db_print_indent(indent);
2992 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
2993 	   tp->snd_wnd, tp->snd_cwnd);
2994 
2995 	db_print_indent(indent);
2996 	db_printf("snd_ssthresh: %u   snd_recover: "
2997 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2998 
2999 	db_print_indent(indent);
3000 	db_printf("t_rcvtime: %u   t_startime: %u\n",
3001 	    tp->t_rcvtime, tp->t_starttime);
3002 
3003 	db_print_indent(indent);
3004 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
3005 	    tp->t_rtttime, tp->t_rtseq);
3006 
3007 	db_print_indent(indent);
3008 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
3009 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3010 
3011 	db_print_indent(indent);
3012 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
3013 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
3014 	    tp->t_rttbest);
3015 
3016 	db_print_indent(indent);
3017 	db_printf("t_rttupdated: %lu   max_sndwnd: %u   t_softerror: %d\n",
3018 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3019 
3020 	db_print_indent(indent);
3021 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3022 	db_print_toobflags(tp->t_oobflags);
3023 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
3024 
3025 	db_print_indent(indent);
3026 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
3027 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3028 
3029 	db_print_indent(indent);
3030 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
3031 	    tp->ts_recent, tp->ts_recent_age);
3032 
3033 	db_print_indent(indent);
3034 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
3035 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3036 
3037 	db_print_indent(indent);
3038 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
3039 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3040 	    tp->snd_recover_prev, tp->t_badrxtwin);
3041 
3042 	db_print_indent(indent);
3043 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
3044 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3045 
3046 	db_print_indent(indent);
3047 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
3048 	    tp->snd_fack, tp->rcv_numsacks);
3049 
3050 	/* Skip sackblks, sackhint. */
3051 
3052 	db_print_indent(indent);
3053 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
3054 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3055 }
3056 
DB_SHOW_COMMAND(tcpcb,db_show_tcpcb)3057 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3058 {
3059 	struct tcpcb *tp;
3060 
3061 	if (!have_addr) {
3062 		db_printf("usage: show tcpcb <addr>\n");
3063 		return;
3064 	}
3065 	tp = (struct tcpcb *)addr;
3066 
3067 	db_print_tcpcb(tp, "tcpcb", 0);
3068 }
3069 #endif
3070