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