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