]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/tcp_usrreq.c
Merge vendor lld/docs directory from r337145
[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_tcpdebug.h"
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/limits.h>
52 #include <sys/malloc.h>
53 #include <sys/refcount.h>
54 #include <sys/kernel.h>
55 #include <sys/sysctl.h>
56 #include <sys/mbuf.h>
57 #ifdef INET6
58 #include <sys/domain.h>
59 #endif /* INET6 */
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/protosw.h>
63 #include <sys/proc.h>
64 #include <sys/jail.h>
65 #include <sys/syslog.h>
66
67 #ifdef DDB
68 #include <ddb/ddb.h>
69 #endif
70
71 #include <net/if.h>
72 #include <net/if_var.h>
73 #include <net/route.h>
74 #include <net/vnet.h>
75
76 #include <netinet/in.h>
77 #include <netinet/in_kdtrace.h>
78 #include <netinet/in_pcb.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/in_var.h>
81 #include <netinet/ip_var.h>
82 #ifdef INET6
83 #include <netinet/ip6.h>
84 #include <netinet6/in6_pcb.h>
85 #include <netinet6/ip6_var.h>
86 #include <netinet6/scope6_var.h>
87 #endif
88 #include <netinet/tcp.h>
89 #include <netinet/tcp_fsm.h>
90 #include <netinet/tcp_seq.h>
91 #include <netinet/tcp_timer.h>
92 #include <netinet/tcp_var.h>
93 #include <netinet/tcp_log_buf.h>
94 #include <netinet/tcpip.h>
95 #include <netinet/cc/cc.h>
96 #include <netinet/tcp_fastopen.h>
97 #include <netinet/tcp_hpts.h>
98 #ifdef TCPPCAP
99 #include <netinet/tcp_pcap.h>
100 #endif
101 #ifdef TCPDEBUG
102 #include <netinet/tcp_debug.h>
103 #endif
104 #ifdef TCP_OFFLOAD
105 #include <netinet/tcp_offload.h>
106 #endif
107 #include <netipsec/ipsec_support.h>
108
109 /*
110  * TCP protocol interface to socket abstraction.
111  */
112 static int      tcp_attach(struct socket *);
113 #ifdef INET
114 static int      tcp_connect(struct tcpcb *, struct sockaddr *,
115                     struct thread *td);
116 #endif /* INET */
117 #ifdef INET6
118 static int      tcp6_connect(struct tcpcb *, struct sockaddr *,
119                     struct thread *td);
120 #endif /* INET6 */
121 static void     tcp_disconnect(struct tcpcb *);
122 static void     tcp_usrclosed(struct tcpcb *);
123 static void     tcp_fill_info(struct tcpcb *, struct tcp_info *);
124
125 #ifdef TCPDEBUG
126 #define TCPDEBUG0       int ostate = 0
127 #define TCPDEBUG1()     ostate = tp ? tp->t_state : 0
128 #define TCPDEBUG2(req)  if (tp && (so->so_options & SO_DEBUG)) \
129                                 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
130 #else
131 #define TCPDEBUG0
132 #define TCPDEBUG1()
133 #define TCPDEBUG2(req)
134 #endif
135
136 /*
137  * TCP attaches to socket via pru_attach(), reserving space,
138  * and an internet control block.
139  */
140 static int
141 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
142 {
143         struct inpcb *inp;
144         struct tcpcb *tp = NULL;
145         int error;
146         TCPDEBUG0;
147
148         inp = sotoinpcb(so);
149         KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
150         TCPDEBUG1();
151
152         error = tcp_attach(so);
153         if (error)
154                 goto out;
155
156         if ((so->so_options & SO_LINGER) && so->so_linger == 0)
157                 so->so_linger = TCP_LINGERTIME;
158
159         inp = sotoinpcb(so);
160         tp = intotcpcb(inp);
161 out:
162         TCPDEBUG2(PRU_ATTACH);
163         TCP_PROBE2(debug__user, tp, PRU_ATTACH);
164         return error;
165 }
166
167 /*
168  * tcp_detach is called when the socket layer loses its final reference
169  * to the socket, be it a file descriptor reference, a reference from TCP,
170  * etc.  At this point, there is only one case in which we will keep around
171  * inpcb state: time wait.
172  *
173  * This function can probably be re-absorbed back into tcp_usr_detach() now
174  * that there is a single detach path.
175  */
176 static void
177 tcp_detach(struct socket *so, struct inpcb *inp)
178 {
179         struct tcpcb *tp;
180
181         INP_INFO_LOCK_ASSERT(&V_tcbinfo);
182         INP_WLOCK_ASSERT(inp);
183
184         KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
185         KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
186
187         tp = intotcpcb(inp);
188
189         if (inp->inp_flags & INP_TIMEWAIT) {
190                 /*
191                  * There are two cases to handle: one in which the time wait
192                  * state is being discarded (INP_DROPPED), and one in which
193                  * this connection will remain in timewait.  In the former,
194                  * it is time to discard all state (except tcptw, which has
195                  * already been discarded by the timewait close code, which
196                  * should be further up the call stack somewhere).  In the
197                  * latter case, we detach from the socket, but leave the pcb
198                  * present until timewait ends.
199                  *
200                  * XXXRW: Would it be cleaner to free the tcptw here?
201                  *
202                  * Astute question indeed, from twtcp perspective there are
203                  * four cases to consider:
204                  *
205                  * #1 tcp_detach is called at tcptw creation time by
206                  *  tcp_twstart, then do not discard the newly created tcptw
207                  *  and leave inpcb present until timewait ends
208                  * #2 tcp_detach is called at tcptw creation time by
209                  *  tcp_twstart, but connection is local and tw will be
210                  *  discarded immediately
211                  * #3 tcp_detach is called at timewait end (or reuse) by
212                  *  tcp_twclose, then the tcptw has already been discarded
213                  *  (or reused) and inpcb is freed here
214                  * #4 tcp_detach is called() after timewait ends (or reuse)
215                  *  (e.g. by soclose), then tcptw has already been discarded
216                  *  (or reused) and inpcb is freed here
217                  *
218                  *  In all three cases the tcptw should not be freed here.
219                  */
220                 if (inp->inp_flags & INP_DROPPED) {
221                         in_pcbdetach(inp);
222                         if (__predict_true(tp == NULL)) {
223                                 in_pcbfree(inp);
224                         } else {
225                                 /*
226                                  * This case should not happen as in TIMEWAIT
227                                  * state the inp should not be destroyed before
228                                  * its tcptw.  If INVARIANTS is defined, panic.
229                                  */
230 #ifdef INVARIANTS
231                                 panic("%s: Panic before an inp double-free: "
232                                     "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
233                                     , __func__);
234 #else
235                                 log(LOG_ERR, "%s: Avoid an inp double-free: "
236                                     "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
237                                     , __func__);
238 #endif
239                                 INP_WUNLOCK(inp);
240                         }
241                 } else {
242                         in_pcbdetach(inp);
243                         INP_WUNLOCK(inp);
244                 }
245         } else {
246                 /*
247                  * If the connection is not in timewait, we consider two
248                  * two conditions: one in which no further processing is
249                  * necessary (dropped || embryonic), and one in which TCP is
250                  * not yet done, but no longer requires the socket, so the
251                  * pcb will persist for the time being.
252                  *
253                  * XXXRW: Does the second case still occur?
254                  */
255                 if (inp->inp_flags & INP_DROPPED ||
256                     tp->t_state < TCPS_SYN_SENT) {
257                         tcp_discardcb(tp);
258                         in_pcbdetach(inp);
259                         in_pcbfree(inp);
260                 } else {
261                         in_pcbdetach(inp);
262                         INP_WUNLOCK(inp);
263                 }
264         }
265 }
266
267 /*
268  * pru_detach() detaches the TCP protocol from the socket.
269  * If the protocol state is non-embryonic, then can't
270  * do this directly: have to initiate a pru_disconnect(),
271  * which may finish later; embryonic TCB's can just
272  * be discarded here.
273  */
274 static void
275 tcp_usr_detach(struct socket *so)
276 {
277         struct inpcb *inp;
278         int rlock = 0;
279         struct epoch_tracker et;
280
281         inp = sotoinpcb(so);
282         KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
283         if (!INP_INFO_WLOCKED(&V_tcbinfo)) {
284                 INP_INFO_RLOCK_ET(&V_tcbinfo, et);
285                 rlock = 1;
286         }
287         INP_WLOCK(inp);
288         KASSERT(inp->inp_socket != NULL,
289             ("tcp_usr_detach: inp_socket == NULL"));
290         tcp_detach(so, inp);
291         if (rlock)
292                 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
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         struct tcpcb *tp = NULL;
305         struct sockaddr_in *sinp;
306
307         sinp = (struct sockaddr_in *)nam;
308         if (nam->sa_len != sizeof (*sinp))
309                 return (EINVAL);
310         /*
311          * Must check for multicast addresses and disallow binding
312          * to them.
313          */
314         if (sinp->sin_family == AF_INET &&
315             IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
316                 return (EAFNOSUPPORT);
317
318         TCPDEBUG0;
319         inp = sotoinpcb(so);
320         KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
321         INP_WLOCK(inp);
322         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
323                 error = EINVAL;
324                 goto out;
325         }
326         tp = intotcpcb(inp);
327         TCPDEBUG1();
328         INP_HASH_WLOCK(&V_tcbinfo);
329         error = in_pcbbind(inp, nam, td->td_ucred);
330         INP_HASH_WUNLOCK(&V_tcbinfo);
331 out:
332         TCPDEBUG2(PRU_BIND);
333         TCP_PROBE2(debug__user, tp, PRU_BIND);
334         INP_WUNLOCK(inp);
335
336         return (error);
337 }
338 #endif /* INET */
339
340 #ifdef INET6
341 static int
342 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
343 {
344         int error = 0;
345         struct inpcb *inp;
346         struct tcpcb *tp = NULL;
347         struct sockaddr_in6 *sin6p;
348
349         sin6p = (struct sockaddr_in6 *)nam;
350         if (nam->sa_len != sizeof (*sin6p))
351                 return (EINVAL);
352         /*
353          * Must check for multicast addresses and disallow binding
354          * to them.
355          */
356         if (sin6p->sin6_family == AF_INET6 &&
357             IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
358                 return (EAFNOSUPPORT);
359
360         TCPDEBUG0;
361         inp = sotoinpcb(so);
362         KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
363         INP_WLOCK(inp);
364         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
365                 error = EINVAL;
366                 goto out;
367         }
368         tp = intotcpcb(inp);
369         TCPDEBUG1();
370         INP_HASH_WLOCK(&V_tcbinfo);
371         inp->inp_vflag &= ~INP_IPV4;
372         inp->inp_vflag |= INP_IPV6;
373 #ifdef INET
374         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
375                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
376                         inp->inp_vflag |= INP_IPV4;
377                 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
378                         struct sockaddr_in sin;
379
380                         in6_sin6_2_sin(&sin, sin6p);
381                         inp->inp_vflag |= INP_IPV4;
382                         inp->inp_vflag &= ~INP_IPV6;
383                         error = in_pcbbind(inp, (struct sockaddr *)&sin,
384                             td->td_ucred);
385                         INP_HASH_WUNLOCK(&V_tcbinfo);
386                         goto out;
387                 }
388         }
389 #endif
390         error = in6_pcbbind(inp, nam, td->td_ucred);
391         INP_HASH_WUNLOCK(&V_tcbinfo);
392 out:
393         TCPDEBUG2(PRU_BIND);
394         TCP_PROBE2(debug__user, tp, PRU_BIND);
395         INP_WUNLOCK(inp);
396         return (error);
397 }
398 #endif /* INET6 */
399
400 #ifdef INET
401 /*
402  * Prepare to accept connections.
403  */
404 static int
405 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
406 {
407         int error = 0;
408         struct inpcb *inp;
409         struct tcpcb *tp = NULL;
410
411         TCPDEBUG0;
412         inp = sotoinpcb(so);
413         KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
414         INP_WLOCK(inp);
415         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
416                 error = EINVAL;
417                 goto out;
418         }
419         tp = intotcpcb(inp);
420         TCPDEBUG1();
421         SOCK_LOCK(so);
422         error = solisten_proto_check(so);
423         INP_HASH_WLOCK(&V_tcbinfo);
424         if (error == 0 && inp->inp_lport == 0)
425                 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
426         INP_HASH_WUNLOCK(&V_tcbinfo);
427         if (error == 0) {
428                 tcp_state_change(tp, TCPS_LISTEN);
429                 solisten_proto(so, backlog);
430 #ifdef TCP_OFFLOAD
431                 if ((so->so_options & SO_NO_OFFLOAD) == 0)
432                         tcp_offload_listen_start(tp);
433 #endif
434         }
435         SOCK_UNLOCK(so);
436
437         if (IS_FASTOPEN(tp->t_flags))
438                 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
439
440 out:
441         TCPDEBUG2(PRU_LISTEN);
442         TCP_PROBE2(debug__user, tp, PRU_LISTEN);
443         INP_WUNLOCK(inp);
444         return (error);
445 }
446 #endif /* INET */
447
448 #ifdef INET6
449 static int
450 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
451 {
452         int error = 0;
453         struct inpcb *inp;
454         struct tcpcb *tp = NULL;
455
456         TCPDEBUG0;
457         inp = sotoinpcb(so);
458         KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
459         INP_WLOCK(inp);
460         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
461                 error = EINVAL;
462                 goto out;
463         }
464         tp = intotcpcb(inp);
465         TCPDEBUG1();
466         SOCK_LOCK(so);
467         error = solisten_proto_check(so);
468         INP_HASH_WLOCK(&V_tcbinfo);
469         if (error == 0 && inp->inp_lport == 0) {
470                 inp->inp_vflag &= ~INP_IPV4;
471                 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
472                         inp->inp_vflag |= INP_IPV4;
473                 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
474         }
475         INP_HASH_WUNLOCK(&V_tcbinfo);
476         if (error == 0) {
477                 tcp_state_change(tp, TCPS_LISTEN);
478                 solisten_proto(so, backlog);
479 #ifdef TCP_OFFLOAD
480                 if ((so->so_options & SO_NO_OFFLOAD) == 0)
481                         tcp_offload_listen_start(tp);
482 #endif
483         }
484         SOCK_UNLOCK(so);
485
486         if (IS_FASTOPEN(tp->t_flags))
487                 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
488
489 out:
490         TCPDEBUG2(PRU_LISTEN);
491         TCP_PROBE2(debug__user, tp, PRU_LISTEN);
492         INP_WUNLOCK(inp);
493         return (error);
494 }
495 #endif /* INET6 */
496
497 #ifdef INET
498 /*
499  * Initiate connection to peer.
500  * Create a template for use in transmissions on this connection.
501  * Enter SYN_SENT state, and mark socket as connecting.
502  * Start keep-alive timer, and seed output sequence space.
503  * Send initial segment on connection.
504  */
505 static int
506 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
507 {
508         int error = 0;
509         struct inpcb *inp;
510         struct tcpcb *tp = NULL;
511         struct sockaddr_in *sinp;
512
513         sinp = (struct sockaddr_in *)nam;
514         if (nam->sa_len != sizeof (*sinp))
515                 return (EINVAL);
516         /*
517          * Must disallow TCP ``connections'' to multicast addresses.
518          */
519         if (sinp->sin_family == AF_INET
520             && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
521                 return (EAFNOSUPPORT);
522         if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
523                 return (error);
524
525         TCPDEBUG0;
526         inp = sotoinpcb(so);
527         KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
528         INP_WLOCK(inp);
529         if (inp->inp_flags & INP_TIMEWAIT) {
530                 error = EADDRINUSE;
531                 goto out;
532         }
533         if (inp->inp_flags & INP_DROPPED) {
534                 error = ECONNREFUSED;
535                 goto out;
536         }
537         tp = intotcpcb(inp);
538         TCPDEBUG1();
539         if ((error = tcp_connect(tp, nam, td)) != 0)
540                 goto out;
541 #ifdef TCP_OFFLOAD
542         if (registered_toedevs > 0 &&
543             (so->so_options & SO_NO_OFFLOAD) == 0 &&
544             (error = tcp_offload_connect(so, nam)) == 0)
545                 goto out;
546 #endif
547         tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
548         error = tp->t_fb->tfb_tcp_output(tp);
549 out:
550         TCPDEBUG2(PRU_CONNECT);
551         TCP_PROBE2(debug__user, tp, PRU_CONNECT);
552         INP_WUNLOCK(inp);
553         return (error);
554 }
555 #endif /* INET */
556
557 #ifdef INET6
558 static int
559 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
560 {
561         int error = 0;
562         struct inpcb *inp;
563         struct tcpcb *tp = NULL;
564         struct sockaddr_in6 *sin6p;
565
566         TCPDEBUG0;
567
568         sin6p = (struct sockaddr_in6 *)nam;
569         if (nam->sa_len != sizeof (*sin6p))
570                 return (EINVAL);
571         /*
572          * Must disallow TCP ``connections'' to multicast addresses.
573          */
574         if (sin6p->sin6_family == AF_INET6
575             && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
576                 return (EAFNOSUPPORT);
577
578         inp = sotoinpcb(so);
579         KASSERT(inp != NULL, ("tcp6_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         tp = intotcpcb(inp);
590         TCPDEBUG1();
591 #ifdef INET
592         /*
593          * XXXRW: Some confusion: V4/V6 flags relate to binding, and
594          * therefore probably require the hash lock, which isn't held here.
595          * Is this a significant problem?
596          */
597         if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
598                 struct sockaddr_in sin;
599
600                 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
601                         error = EINVAL;
602                         goto out;
603                 }
604                 if ((inp->inp_vflag & INP_IPV4) == 0) {
605                         error = EAFNOSUPPORT;
606                         goto out;
607                 }
608
609                 in6_sin6_2_sin(&sin, sin6p);
610                 inp->inp_vflag |= INP_IPV4;
611                 inp->inp_vflag &= ~INP_IPV6;
612                 if ((error = prison_remote_ip4(td->td_ucred,
613                     &sin.sin_addr)) != 0)
614                         goto out;
615                 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
616                         goto out;
617 #ifdef TCP_OFFLOAD
618                 if (registered_toedevs > 0 &&
619                     (so->so_options & SO_NO_OFFLOAD) == 0 &&
620                     (error = tcp_offload_connect(so, nam)) == 0)
621                         goto out;
622 #endif
623                 error = tp->t_fb->tfb_tcp_output(tp);
624                 goto out;
625         } else {
626                 if ((inp->inp_vflag & INP_IPV6) == 0) {
627                         error = EAFNOSUPPORT;
628                         goto out;
629                 }
630         }
631 #endif
632         inp->inp_vflag &= ~INP_IPV4;
633         inp->inp_vflag |= INP_IPV6;
634         inp->inp_inc.inc_flags |= INC_ISIPV6;
635         if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
636                 goto out;
637         if ((error = tcp6_connect(tp, nam, td)) != 0)
638                 goto out;
639 #ifdef TCP_OFFLOAD
640         if (registered_toedevs > 0 &&
641             (so->so_options & SO_NO_OFFLOAD) == 0 &&
642             (error = tcp_offload_connect(so, nam)) == 0)
643                 goto out;
644 #endif
645         tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
646         error = tp->t_fb->tfb_tcp_output(tp);
647
648 out:
649         TCPDEBUG2(PRU_CONNECT);
650         TCP_PROBE2(debug__user, tp, PRU_CONNECT);
651         INP_WUNLOCK(inp);
652         return (error);
653 }
654 #endif /* INET6 */
655
656 /*
657  * Initiate disconnect from peer.
658  * If connection never passed embryonic stage, just drop;
659  * else if don't need to let data drain, then can just drop anyways,
660  * else have to begin TCP shutdown process: mark socket disconnecting,
661  * drain unread data, state switch to reflect user close, and
662  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
663  * when peer sends FIN and acks ours.
664  *
665  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
666  */
667 static int
668 tcp_usr_disconnect(struct socket *so)
669 {
670         struct inpcb *inp;
671         struct tcpcb *tp = NULL;
672         struct epoch_tracker et;
673         int error = 0;
674
675         TCPDEBUG0;
676         INP_INFO_RLOCK_ET(&V_tcbinfo, et);
677         inp = sotoinpcb(so);
678         KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
679         INP_WLOCK(inp);
680         if (inp->inp_flags & INP_TIMEWAIT)
681                 goto out;
682         if (inp->inp_flags & INP_DROPPED) {
683                 error = ECONNRESET;
684                 goto out;
685         }
686         tp = intotcpcb(inp);
687         TCPDEBUG1();
688         tcp_disconnect(tp);
689 out:
690         TCPDEBUG2(PRU_DISCONNECT);
691         TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
692         INP_WUNLOCK(inp);
693         INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
694         return (error);
695 }
696
697 #ifdef INET
698 /*
699  * Accept a connection.  Essentially all the work is done at higher levels;
700  * just return the address of the peer, storing through addr.
701  */
702 static int
703 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
704 {
705         int error = 0;
706         struct inpcb *inp = NULL;
707         struct tcpcb *tp = NULL;
708         struct in_addr addr;
709         in_port_t port = 0;
710         TCPDEBUG0;
711
712         if (so->so_state & SS_ISDISCONNECTED)
713                 return (ECONNABORTED);
714
715         inp = sotoinpcb(so);
716         KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
717         INP_WLOCK(inp);
718         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
719                 error = ECONNABORTED;
720                 goto out;
721         }
722         tp = intotcpcb(inp);
723         TCPDEBUG1();
724
725         /*
726          * We inline in_getpeeraddr and COMMON_END here, so that we can
727          * copy the data of interest and defer the malloc until after we
728          * release the lock.
729          */
730         port = inp->inp_fport;
731         addr = inp->inp_faddr;
732
733 out:
734         TCPDEBUG2(PRU_ACCEPT);
735         TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
736         INP_WUNLOCK(inp);
737         if (error == 0)
738                 *nam = in_sockaddr(port, &addr);
739         return error;
740 }
741 #endif /* INET */
742
743 #ifdef INET6
744 static int
745 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
746 {
747         struct inpcb *inp = NULL;
748         int error = 0;
749         struct tcpcb *tp = NULL;
750         struct in_addr addr;
751         struct in6_addr addr6;
752         struct epoch_tracker et;
753         in_port_t port = 0;
754         int v4 = 0;
755         TCPDEBUG0;
756
757         if (so->so_state & SS_ISDISCONNECTED)
758                 return (ECONNABORTED);
759
760         inp = sotoinpcb(so);
761         KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
762         INP_INFO_RLOCK_ET(&V_tcbinfo, et);
763         INP_WLOCK(inp);
764         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
765                 error = ECONNABORTED;
766                 goto out;
767         }
768         tp = intotcpcb(inp);
769         TCPDEBUG1();
770
771         /*
772          * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
773          * copy the data of interest and defer the malloc until after we
774          * release the lock.
775          */
776         if (inp->inp_vflag & INP_IPV4) {
777                 v4 = 1;
778                 port = inp->inp_fport;
779                 addr = inp->inp_faddr;
780         } else {
781                 port = inp->inp_fport;
782                 addr6 = inp->in6p_faddr;
783         }
784
785 out:
786         TCPDEBUG2(PRU_ACCEPT);
787         TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
788         INP_WUNLOCK(inp);
789         INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
790         if (error == 0) {
791                 if (v4)
792                         *nam = in6_v4mapsin6_sockaddr(port, &addr);
793                 else
794                         *nam = in6_sockaddr(port, &addr6);
795         }
796         return error;
797 }
798 #endif /* INET6 */
799
800 /*
801  * Mark the connection as being incapable of further output.
802  */
803 static int
804 tcp_usr_shutdown(struct socket *so)
805 {
806         int error = 0;
807         struct inpcb *inp;
808         struct tcpcb *tp = NULL;
809         struct epoch_tracker et;
810
811         TCPDEBUG0;
812         INP_INFO_RLOCK_ET(&V_tcbinfo, et);
813         inp = sotoinpcb(so);
814         KASSERT(inp != NULL, ("inp == NULL"));
815         INP_WLOCK(inp);
816         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
817                 error = ECONNRESET;
818                 goto out;
819         }
820         tp = intotcpcb(inp);
821         TCPDEBUG1();
822         socantsendmore(so);
823         tcp_usrclosed(tp);
824         if (!(inp->inp_flags & INP_DROPPED))
825                 error = tp->t_fb->tfb_tcp_output(tp);
826
827 out:
828         TCPDEBUG2(PRU_SHUTDOWN);
829         TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
830         INP_WUNLOCK(inp);
831         INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
832
833         return (error);
834 }
835
836 /*
837  * After a receive, possibly send window update to peer.
838  */
839 static int
840 tcp_usr_rcvd(struct socket *so, int flags)
841 {
842         struct inpcb *inp;
843         struct tcpcb *tp = NULL;
844         int error = 0;
845
846         TCPDEBUG0;
847         inp = sotoinpcb(so);
848         KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
849         INP_WLOCK(inp);
850         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
851                 error = ECONNRESET;
852                 goto out;
853         }
854         tp = intotcpcb(inp);
855         TCPDEBUG1();
856         /*
857          * For passively-created TFO connections, don't attempt a window
858          * update while still in SYN_RECEIVED as this may trigger an early
859          * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
860          * application response data, or failing that, when the DELACK timer
861          * expires.
862          */
863         if (IS_FASTOPEN(tp->t_flags) &&
864             (tp->t_state == TCPS_SYN_RECEIVED))
865                 goto out;
866 #ifdef TCP_OFFLOAD
867         if (tp->t_flags & TF_TOE)
868                 tcp_offload_rcvd(tp);
869         else
870 #endif
871         tp->t_fb->tfb_tcp_output(tp);
872
873 out:
874         TCPDEBUG2(PRU_RCVD);
875         TCP_PROBE2(debug__user, tp, PRU_RCVD);
876         INP_WUNLOCK(inp);
877         return (error);
878 }
879
880 /*
881  * Do a send by putting data in output queue and updating urgent
882  * marker if URG set.  Possibly send more data.  Unlike the other
883  * pru_*() routines, the mbuf chains are our responsibility.  We
884  * must either enqueue them or free them.  The other pru_* routines
885  * generally are caller-frees.
886  */
887 static int
888 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
889     struct sockaddr *nam, struct mbuf *control, struct thread *td)
890 {
891         int error = 0;
892         struct inpcb *inp;
893         struct tcpcb *tp = NULL;
894         struct epoch_tracker net_et;
895 #ifdef INET6
896         int isipv6;
897 #endif
898         TCPDEBUG0;
899
900         /*
901          * We require the pcbinfo lock if we will close the socket as part of
902          * this call.
903          */
904         if (flags & PRUS_EOF)
905                 INP_INFO_RLOCK_ET(&V_tcbinfo, net_et);
906         inp = sotoinpcb(so);
907         KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
908         INP_WLOCK(inp);
909         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
910                 if (control)
911                         m_freem(control);
912                 /*
913                  * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
914                  * for freeing memory.
915                  */
916                 if (m && (flags & PRUS_NOTREADY) == 0)
917                         m_freem(m);
918                 error = ECONNRESET;
919                 goto out;
920         }
921 #ifdef INET6
922         isipv6 = nam && nam->sa_family == AF_INET6;
923 #endif /* INET6 */
924         tp = intotcpcb(inp);
925         TCPDEBUG1();
926         if (control) {
927                 /* TCP doesn't do control messages (rights, creds, etc) */
928                 if (control->m_len) {
929                         m_freem(control);
930                         if (m)
931                                 m_freem(m);
932                         error = EINVAL;
933                         goto out;
934                 }
935                 m_freem(control);       /* empty control, just free it */
936         }
937         if (!(flags & PRUS_OOB)) {
938                 sbappendstream(&so->so_snd, m, flags);
939                 if (nam && tp->t_state < TCPS_SYN_SENT) {
940                         /*
941                          * Do implied connect if not yet connected,
942                          * initialize window to default value, and
943                          * initialize maxseg using peer's cached MSS.
944                          */
945 #ifdef INET6
946                         if (isipv6)
947                                 error = tcp6_connect(tp, nam, td);
948 #endif /* INET6 */
949 #if defined(INET6) && defined(INET)
950                         else
951 #endif
952 #ifdef INET
953                                 error = tcp_connect(tp, nam, td);
954 #endif
955                         if (error)
956                                 goto out;
957                         if (IS_FASTOPEN(tp->t_flags))
958                                 tcp_fastopen_connect(tp);
959                         else {
960                                 tp->snd_wnd = TTCP_CLIENT_SND_WND;
961                                 tcp_mss(tp, -1);
962                         }
963                 }
964                 if (flags & PRUS_EOF) {
965                         /*
966                          * Close the send side of the connection after
967                          * the data is sent.
968                          */
969                         INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
970                         socantsendmore(so);
971                         tcp_usrclosed(tp);
972                 }
973                 if (!(inp->inp_flags & INP_DROPPED) &&
974                     !(flags & PRUS_NOTREADY)) {
975                         if (flags & PRUS_MORETOCOME)
976                                 tp->t_flags |= TF_MORETOCOME;
977                         error = tp->t_fb->tfb_tcp_output(tp);
978                         if (flags & PRUS_MORETOCOME)
979                                 tp->t_flags &= ~TF_MORETOCOME;
980                 }
981         } else {
982                 /*
983                  * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
984                  */
985                 SOCKBUF_LOCK(&so->so_snd);
986                 if (sbspace(&so->so_snd) < -512) {
987                         SOCKBUF_UNLOCK(&so->so_snd);
988                         m_freem(m);
989                         error = ENOBUFS;
990                         goto out;
991                 }
992                 /*
993                  * According to RFC961 (Assigned Protocols),
994                  * the urgent pointer points to the last octet
995                  * of urgent data.  We continue, however,
996                  * to consider it to indicate the first octet
997                  * of data past the urgent section.
998                  * Otherwise, snd_up should be one lower.
999                  */
1000                 sbappendstream_locked(&so->so_snd, m, flags);
1001                 SOCKBUF_UNLOCK(&so->so_snd);
1002                 if (nam && tp->t_state < TCPS_SYN_SENT) {
1003                         /*
1004                          * Do implied connect if not yet connected,
1005                          * initialize window to default value, and
1006                          * initialize maxseg using peer's cached MSS.
1007                          */
1008
1009                         /*
1010                          * Not going to contemplate SYN|URG
1011                          */
1012                         if (IS_FASTOPEN(tp->t_flags))
1013                                 tp->t_flags &= ~TF_FASTOPEN;
1014 #ifdef INET6
1015                         if (isipv6)
1016                                 error = tcp6_connect(tp, nam, td);
1017 #endif /* INET6 */
1018 #if defined(INET6) && defined(INET)
1019                         else
1020 #endif
1021 #ifdef INET
1022                                 error = tcp_connect(tp, nam, td);
1023 #endif
1024                         if (error)
1025                                 goto out;
1026                         tp->snd_wnd = TTCP_CLIENT_SND_WND;
1027                         tcp_mss(tp, -1);
1028                 }
1029                 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
1030                 if (!(flags & PRUS_NOTREADY)) {
1031                         tp->t_flags |= TF_FORCEDATA;
1032                         error = tp->t_fb->tfb_tcp_output(tp);
1033                         tp->t_flags &= ~TF_FORCEDATA;
1034                 }
1035         }
1036         TCP_LOG_EVENT(tp, NULL,
1037             &inp->inp_socket->so_rcv,
1038             &inp->inp_socket->so_snd,
1039             TCP_LOG_USERSEND, error,
1040             0, NULL, false);
1041 out:
1042         TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
1043                   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1044         TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
1045                    ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1046         INP_WUNLOCK(inp);
1047         if (flags & PRUS_EOF)
1048                 INP_INFO_RUNLOCK_ET(&V_tcbinfo, net_et);
1049         return (error);
1050 }
1051
1052 static int
1053 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
1054 {
1055         struct inpcb *inp;
1056         struct tcpcb *tp;
1057         int error;
1058
1059         inp = sotoinpcb(so);
1060         INP_WLOCK(inp);
1061         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1062                 INP_WUNLOCK(inp);
1063                 for (int i = 0; i < count; i++)
1064                         m = m_free(m);
1065                 return (ECONNRESET);
1066         }
1067         tp = intotcpcb(inp);
1068
1069         SOCKBUF_LOCK(&so->so_snd);
1070         error = sbready(&so->so_snd, m, count);
1071         SOCKBUF_UNLOCK(&so->so_snd);
1072         if (error == 0)
1073                 error = tp->t_fb->tfb_tcp_output(tp);
1074         INP_WUNLOCK(inp);
1075
1076         return (error);
1077 }
1078
1079 /*
1080  * Abort the TCP.  Drop the connection abruptly.
1081  */
1082 static void
1083 tcp_usr_abort(struct socket *so)
1084 {
1085         struct inpcb *inp;
1086         struct tcpcb *tp = NULL;
1087         struct epoch_tracker et;
1088         TCPDEBUG0;
1089
1090         inp = sotoinpcb(so);
1091         KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1092
1093         INP_INFO_RLOCK_ET(&V_tcbinfo, et);
1094         INP_WLOCK(inp);
1095         KASSERT(inp->inp_socket != NULL,
1096             ("tcp_usr_abort: inp_socket == NULL"));
1097
1098         /*
1099          * If we still have full TCP state, and we're not dropped, drop.
1100          */
1101         if (!(inp->inp_flags & INP_TIMEWAIT) &&
1102             !(inp->inp_flags & INP_DROPPED)) {
1103                 tp = intotcpcb(inp);
1104                 TCPDEBUG1();
1105                 tp = tcp_drop(tp, ECONNABORTED);
1106                 if (tp == NULL)
1107                         goto dropped;
1108                 TCPDEBUG2(PRU_ABORT);
1109                 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1110         }
1111         if (!(inp->inp_flags & INP_DROPPED)) {
1112                 SOCK_LOCK(so);
1113                 so->so_state |= SS_PROTOREF;
1114                 SOCK_UNLOCK(so);
1115                 inp->inp_flags |= INP_SOCKREF;
1116         }
1117         INP_WUNLOCK(inp);
1118 dropped:
1119         INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
1120 }
1121
1122 /*
1123  * TCP socket is closed.  Start friendly disconnect.
1124  */
1125 static void
1126 tcp_usr_close(struct socket *so)
1127 {
1128         struct inpcb *inp;
1129         struct tcpcb *tp = NULL;
1130         struct epoch_tracker et;
1131         TCPDEBUG0;
1132
1133         inp = sotoinpcb(so);
1134         KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1135
1136         INP_INFO_RLOCK_ET(&V_tcbinfo, et);
1137         INP_WLOCK(inp);
1138         KASSERT(inp->inp_socket != NULL,
1139             ("tcp_usr_close: inp_socket == NULL"));
1140
1141         /*
1142          * If we still have full TCP state, and we're not dropped, initiate
1143          * a disconnect.
1144          */
1145         if (!(inp->inp_flags & INP_TIMEWAIT) &&
1146             !(inp->inp_flags & INP_DROPPED)) {
1147                 tp = intotcpcb(inp);
1148                 TCPDEBUG1();
1149                 tcp_disconnect(tp);
1150                 TCPDEBUG2(PRU_CLOSE);
1151                 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1152         }
1153         if (!(inp->inp_flags & INP_DROPPED)) {
1154                 SOCK_LOCK(so);
1155                 so->so_state |= SS_PROTOREF;
1156                 SOCK_UNLOCK(so);
1157                 inp->inp_flags |= INP_SOCKREF;
1158         }
1159         INP_WUNLOCK(inp);
1160         INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
1161 }
1162
1163 /*
1164  * Receive out-of-band data.
1165  */
1166 static int
1167 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1168 {
1169         int error = 0;
1170         struct inpcb *inp;
1171         struct tcpcb *tp = NULL;
1172
1173         TCPDEBUG0;
1174         inp = sotoinpcb(so);
1175         KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1176         INP_WLOCK(inp);
1177         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1178                 error = ECONNRESET;
1179                 goto out;
1180         }
1181         tp = intotcpcb(inp);
1182         TCPDEBUG1();
1183         if ((so->so_oobmark == 0 &&
1184              (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1185             so->so_options & SO_OOBINLINE ||
1186             tp->t_oobflags & TCPOOB_HADDATA) {
1187                 error = EINVAL;
1188                 goto out;
1189         }
1190         if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1191                 error = EWOULDBLOCK;
1192                 goto out;
1193         }
1194         m->m_len = 1;
1195         *mtod(m, caddr_t) = tp->t_iobc;
1196         if ((flags & MSG_PEEK) == 0)
1197                 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1198
1199 out:
1200         TCPDEBUG2(PRU_RCVOOB);
1201         TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1202         INP_WUNLOCK(inp);
1203         return (error);
1204 }
1205
1206 #ifdef INET
1207 struct pr_usrreqs tcp_usrreqs = {
1208         .pru_abort =            tcp_usr_abort,
1209         .pru_accept =           tcp_usr_accept,
1210         .pru_attach =           tcp_usr_attach,
1211         .pru_bind =             tcp_usr_bind,
1212         .pru_connect =          tcp_usr_connect,
1213         .pru_control =          in_control,
1214         .pru_detach =           tcp_usr_detach,
1215         .pru_disconnect =       tcp_usr_disconnect,
1216         .pru_listen =           tcp_usr_listen,
1217         .pru_peeraddr =         in_getpeeraddr,
1218         .pru_rcvd =             tcp_usr_rcvd,
1219         .pru_rcvoob =           tcp_usr_rcvoob,
1220         .pru_send =             tcp_usr_send,
1221         .pru_ready =            tcp_usr_ready,
1222         .pru_shutdown =         tcp_usr_shutdown,
1223         .pru_sockaddr =         in_getsockaddr,
1224         .pru_sosetlabel =       in_pcbsosetlabel,
1225         .pru_close =            tcp_usr_close,
1226 };
1227 #endif /* INET */
1228
1229 #ifdef INET6
1230 struct pr_usrreqs tcp6_usrreqs = {
1231         .pru_abort =            tcp_usr_abort,
1232         .pru_accept =           tcp6_usr_accept,
1233         .pru_attach =           tcp_usr_attach,
1234         .pru_bind =             tcp6_usr_bind,
1235         .pru_connect =          tcp6_usr_connect,
1236         .pru_control =          in6_control,
1237         .pru_detach =           tcp_usr_detach,
1238         .pru_disconnect =       tcp_usr_disconnect,
1239         .pru_listen =           tcp6_usr_listen,
1240         .pru_peeraddr =         in6_mapped_peeraddr,
1241         .pru_rcvd =             tcp_usr_rcvd,
1242         .pru_rcvoob =           tcp_usr_rcvoob,
1243         .pru_send =             tcp_usr_send,
1244         .pru_ready =            tcp_usr_ready,
1245         .pru_shutdown =         tcp_usr_shutdown,
1246         .pru_sockaddr =         in6_mapped_sockaddr,
1247         .pru_sosetlabel =       in_pcbsosetlabel,
1248         .pru_close =            tcp_usr_close,
1249 };
1250 #endif /* INET6 */
1251
1252 #ifdef INET
1253 /*
1254  * Common subroutine to open a TCP connection to remote host specified
1255  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
1256  * port number if needed.  Call in_pcbconnect_setup to do the routing and
1257  * to choose a local host address (interface).  If there is an existing
1258  * incarnation of the same connection in TIME-WAIT state and if the remote
1259  * host was sending CC options and if the connection duration was < MSL, then
1260  * truncate the previous TIME-WAIT state and proceed.
1261  * Initialize connection parameters and enter SYN-SENT state.
1262  */
1263 static int
1264 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1265 {
1266         struct inpcb *inp = tp->t_inpcb, *oinp;
1267         struct socket *so = inp->inp_socket;
1268         struct in_addr laddr;
1269         u_short lport;
1270         int error;
1271
1272         INP_WLOCK_ASSERT(inp);
1273         INP_HASH_WLOCK(&V_tcbinfo);
1274
1275         if (inp->inp_lport == 0) {
1276                 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1277                 if (error)
1278                         goto out;
1279         }
1280
1281         /*
1282          * Cannot simply call in_pcbconnect, because there might be an
1283          * earlier incarnation of this same connection still in
1284          * TIME_WAIT state, creating an ADDRINUSE error.
1285          */
1286         laddr = inp->inp_laddr;
1287         lport = inp->inp_lport;
1288         error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1289             &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1290         if (error && oinp == NULL)
1291                 goto out;
1292         if (oinp) {
1293                 error = EADDRINUSE;
1294                 goto out;
1295         }
1296         inp->inp_laddr = laddr;
1297         in_pcbrehash(inp);
1298         INP_HASH_WUNLOCK(&V_tcbinfo);
1299
1300         /*
1301          * Compute window scaling to request:
1302          * Scale to fit into sweet spot.  See tcp_syncache.c.
1303          * XXX: This should move to tcp_output().
1304          */
1305         while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1306             (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1307                 tp->request_r_scale++;
1308
1309         soisconnecting(so);
1310         TCPSTAT_INC(tcps_connattempt);
1311         tcp_state_change(tp, TCPS_SYN_SENT);
1312         tp->iss = tcp_new_isn(tp);
1313         tcp_sendseqinit(tp);
1314
1315         return 0;
1316
1317 out:
1318         INP_HASH_WUNLOCK(&V_tcbinfo);
1319         return (error);
1320 }
1321 #endif /* INET */
1322
1323 #ifdef INET6
1324 static int
1325 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1326 {
1327         struct inpcb *inp = tp->t_inpcb;
1328         int error;
1329
1330         INP_WLOCK_ASSERT(inp);
1331         INP_HASH_WLOCK(&V_tcbinfo);
1332
1333         if (inp->inp_lport == 0) {
1334                 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1335                 if (error)
1336                         goto out;
1337         }
1338         error = in6_pcbconnect(inp, nam, td->td_ucred);
1339         if (error != 0)
1340                 goto out;
1341         INP_HASH_WUNLOCK(&V_tcbinfo);
1342
1343         /* Compute window scaling to request.  */
1344         while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1345             (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1346                 tp->request_r_scale++;
1347
1348         soisconnecting(inp->inp_socket);
1349         TCPSTAT_INC(tcps_connattempt);
1350         tcp_state_change(tp, TCPS_SYN_SENT);
1351         tp->iss = tcp_new_isn(tp);
1352         tcp_sendseqinit(tp);
1353
1354         return 0;
1355
1356 out:
1357         INP_HASH_WUNLOCK(&V_tcbinfo);
1358         return error;
1359 }
1360 #endif /* INET6 */
1361
1362 /*
1363  * Export TCP internal state information via a struct tcp_info, based on the
1364  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1365  * (TCP state machine, etc).  We export all information using FreeBSD-native
1366  * constants -- for example, the numeric values for tcpi_state will differ
1367  * from Linux.
1368  */
1369 static void
1370 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1371 {
1372
1373         INP_WLOCK_ASSERT(tp->t_inpcb);
1374         bzero(ti, sizeof(*ti));
1375
1376         ti->tcpi_state = tp->t_state;
1377         if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1378                 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1379         if (tp->t_flags & TF_SACK_PERMIT)
1380                 ti->tcpi_options |= TCPI_OPT_SACK;
1381         if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1382                 ti->tcpi_options |= TCPI_OPT_WSCALE;
1383                 ti->tcpi_snd_wscale = tp->snd_scale;
1384                 ti->tcpi_rcv_wscale = tp->rcv_scale;
1385         }
1386         if (tp->t_flags & TF_ECN_PERMIT)
1387                 ti->tcpi_options |= TCPI_OPT_ECN;
1388
1389         ti->tcpi_rto = tp->t_rxtcur * tick;
1390         ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
1391         ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1392         ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1393
1394         ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1395         ti->tcpi_snd_cwnd = tp->snd_cwnd;
1396
1397         /*
1398          * FreeBSD-specific extension fields for tcp_info.
1399          */
1400         ti->tcpi_rcv_space = tp->rcv_wnd;
1401         ti->tcpi_rcv_nxt = tp->rcv_nxt;
1402         ti->tcpi_snd_wnd = tp->snd_wnd;
1403         ti->tcpi_snd_bwnd = 0;          /* Unused, kept for compat. */
1404         ti->tcpi_snd_nxt = tp->snd_nxt;
1405         ti->tcpi_snd_mss = tp->t_maxseg;
1406         ti->tcpi_rcv_mss = tp->t_maxseg;
1407         ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1408         ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1409         ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1410 #ifdef TCP_OFFLOAD
1411         if (tp->t_flags & TF_TOE) {
1412                 ti->tcpi_options |= TCPI_OPT_TOE;
1413                 tcp_offload_tcp_info(tp, ti);
1414         }
1415 #endif
1416 }
1417
1418 /*
1419  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1420  * socket option arguments.  When it re-acquires the lock after the copy, it
1421  * has to revalidate that the connection is still valid for the socket
1422  * option.
1423  */
1424 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {                    \
1425         INP_WLOCK(inp);                                                 \
1426         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {            \
1427                 INP_WUNLOCK(inp);                                       \
1428                 cleanup;                                                \
1429                 return (ECONNRESET);                                    \
1430         }                                                               \
1431         tp = intotcpcb(inp);                                            \
1432 } while(0)
1433 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
1434
1435 int
1436 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1437 {
1438         int     error;
1439         struct  inpcb *inp;
1440         struct  tcpcb *tp;
1441         struct tcp_function_block *blk;
1442         struct tcp_function_set fsn;
1443
1444         error = 0;
1445         inp = sotoinpcb(so);
1446         KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1447         INP_WLOCK(inp);
1448         if (sopt->sopt_level != IPPROTO_TCP) {
1449 #ifdef INET6
1450                 if (inp->inp_vflag & INP_IPV6PROTO) {
1451                         INP_WUNLOCK(inp);
1452                         error = ip6_ctloutput(so, sopt);
1453                 }
1454 #endif /* INET6 */
1455 #if defined(INET6) && defined(INET)
1456                 else
1457 #endif
1458 #ifdef INET
1459                 {
1460                         INP_WUNLOCK(inp);
1461                         error = ip_ctloutput(so, sopt);
1462                 }
1463 #endif
1464                 return (error);
1465         }
1466         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1467                 INP_WUNLOCK(inp);
1468                 return (ECONNRESET);
1469         }
1470         tp = intotcpcb(inp);
1471         /*
1472          * Protect the TCP option TCP_FUNCTION_BLK so
1473          * that a sub-function can *never* overwrite this.
1474          */
1475         if ((sopt->sopt_dir == SOPT_SET) && 
1476             (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1477                 INP_WUNLOCK(inp);
1478                 error = sooptcopyin(sopt, &fsn, sizeof fsn,
1479                     sizeof fsn);
1480                 if (error)
1481                         return (error);
1482                 INP_WLOCK_RECHECK(inp);
1483                 blk = find_and_ref_tcp_functions(&fsn);
1484                 if (blk == NULL) {
1485                         INP_WUNLOCK(inp);
1486                         return (ENOENT);
1487                 }
1488                 if (tp->t_fb == blk) {
1489                         /* You already have this */
1490                         refcount_release(&blk->tfb_refcnt);
1491                         INP_WUNLOCK(inp);
1492                         return (0);
1493                 }
1494                 if (tp->t_state != TCPS_CLOSED) {
1495                         int error=EINVAL;
1496                         /* 
1497                          * The user has advanced the state
1498                          * past the initial point, we may not
1499                          * be able to switch. 
1500                          */
1501                         if (blk->tfb_tcp_handoff_ok != NULL) {
1502                                 /* 
1503                                  * Does the stack provide a
1504                                  * query mechanism, if so it may
1505                                  * still be possible?
1506                                  */
1507                                 error = (*blk->tfb_tcp_handoff_ok)(tp);
1508                         }
1509                         if (error) {
1510                                 refcount_release(&blk->tfb_refcnt);
1511                                 INP_WUNLOCK(inp);
1512                                 return(error);
1513                         }
1514                 }
1515                 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
1516                         refcount_release(&blk->tfb_refcnt);
1517                         INP_WUNLOCK(inp);
1518                         return (ENOENT);
1519                 }
1520                 /* 
1521                  * Release the old refcnt, the
1522                  * lookup acquired a ref on the
1523                  * new one already.
1524                  */
1525                 if (tp->t_fb->tfb_tcp_fb_fini) {
1526                         /* 
1527                          * Tell the stack to cleanup with 0 i.e.
1528                          * the tcb is not going away.
1529                          */
1530                         (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1531                 }
1532 #ifdef TCPHPTS 
1533                 /* Assure that we are not on any hpts */
1534                 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
1535 #endif
1536                 if (blk->tfb_tcp_fb_init) {
1537                         error = (*blk->tfb_tcp_fb_init)(tp);
1538                         if (error) {
1539                                 refcount_release(&blk->tfb_refcnt);
1540                                 if (tp->t_fb->tfb_tcp_fb_init) {
1541                                         if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0)  {
1542                                                 /* Fall back failed, drop the connection */
1543                                                 INP_WUNLOCK(inp);
1544                                                 soabort(so);
1545                                                 return(error);
1546                                         }
1547                                 }
1548                                 goto err_out;
1549                         }
1550                 }
1551                 refcount_release(&tp->t_fb->tfb_refcnt);
1552                 tp->t_fb = blk;
1553 #ifdef TCP_OFFLOAD
1554                 if (tp->t_flags & TF_TOE) {
1555                         tcp_offload_ctloutput(tp, sopt->sopt_dir,
1556                              sopt->sopt_name);
1557                 }
1558 #endif
1559 err_out:
1560                 INP_WUNLOCK(inp);
1561                 return (error);
1562         } else if ((sopt->sopt_dir == SOPT_GET) && 
1563             (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1564                 strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1565                     TCP_FUNCTION_NAME_LEN_MAX);
1566                 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1567                 fsn.pcbcnt = tp->t_fb->tfb_refcnt;
1568                 INP_WUNLOCK(inp);
1569                 error = sooptcopyout(sopt, &fsn, sizeof fsn);
1570                 return (error);
1571         }
1572         /* Pass in the INP locked, called must unlock it */
1573         return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
1574 }
1575
1576 /*
1577  * If this assert becomes untrue, we need to change the size of the buf
1578  * variable in tcp_default_ctloutput().
1579  */
1580 #ifdef CTASSERT
1581 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
1582 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
1583 #endif
1584
1585 int
1586 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
1587 {
1588         int     error, opt, optval;
1589         u_int   ui;
1590         struct  tcp_info ti;
1591         struct cc_algo *algo;
1592         char    *pbuf, buf[TCP_LOG_ID_LEN];
1593         size_t  len;
1594
1595         /*
1596          * For TCP_CCALGOOPT forward the control to CC module, for both
1597          * SOPT_SET and SOPT_GET.
1598          */
1599         switch (sopt->sopt_name) {
1600         case TCP_CCALGOOPT:
1601                 INP_WUNLOCK(inp);
1602                 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1603                 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1604                     sopt->sopt_valsize);
1605                 if (error) {
1606                         free(pbuf, M_TEMP);
1607                         return (error);
1608                 }
1609                 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1610                 if (CC_ALGO(tp)->ctl_output != NULL)
1611                         error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1612                 else
1613                         error = ENOENT;
1614                 INP_WUNLOCK(inp);
1615                 if (error == 0 && sopt->sopt_dir == SOPT_GET)
1616                         error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1617                 free(pbuf, M_TEMP);
1618                 return (error);
1619         }
1620
1621         switch (sopt->sopt_dir) {
1622         case SOPT_SET:
1623                 switch (sopt->sopt_name) {
1624 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1625                 case TCP_MD5SIG:
1626                         if (!TCPMD5_ENABLED()) {
1627                                 INP_WUNLOCK(inp);
1628                                 return (ENOPROTOOPT);
1629                         }
1630                         error = TCPMD5_PCBCTL(inp, sopt);
1631                         if (error)
1632                                 return (error);
1633                         goto unlock_and_done;
1634 #endif /* IPSEC */
1635
1636                 case TCP_NODELAY:
1637                 case TCP_NOOPT:
1638                         INP_WUNLOCK(inp);
1639                         error = sooptcopyin(sopt, &optval, sizeof optval,
1640                             sizeof optval);
1641                         if (error)
1642                                 return (error);
1643
1644                         INP_WLOCK_RECHECK(inp);
1645                         switch (sopt->sopt_name) {
1646                         case TCP_NODELAY:
1647                                 opt = TF_NODELAY;
1648                                 break;
1649                         case TCP_NOOPT:
1650                                 opt = TF_NOOPT;
1651                                 break;
1652                         default:
1653                                 opt = 0; /* dead code to fool gcc */
1654                                 break;
1655                         }
1656
1657                         if (optval)
1658                                 tp->t_flags |= opt;
1659                         else
1660                                 tp->t_flags &= ~opt;
1661 unlock_and_done:
1662 #ifdef TCP_OFFLOAD
1663                         if (tp->t_flags & TF_TOE) {
1664                                 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1665                                     sopt->sopt_name);
1666                         }
1667 #endif
1668                         INP_WUNLOCK(inp);
1669                         break;
1670
1671                 case TCP_NOPUSH:
1672                         INP_WUNLOCK(inp);
1673                         error = sooptcopyin(sopt, &optval, sizeof optval,
1674                             sizeof optval);
1675                         if (error)
1676                                 return (error);
1677
1678                         INP_WLOCK_RECHECK(inp);
1679                         if (optval)
1680                                 tp->t_flags |= TF_NOPUSH;
1681                         else if (tp->t_flags & TF_NOPUSH) {
1682                                 tp->t_flags &= ~TF_NOPUSH;
1683                                 if (TCPS_HAVEESTABLISHED(tp->t_state))
1684                                         error = tp->t_fb->tfb_tcp_output(tp);
1685                         }
1686                         goto unlock_and_done;
1687
1688                 case TCP_MAXSEG:
1689                         INP_WUNLOCK(inp);
1690                         error = sooptcopyin(sopt, &optval, sizeof optval,
1691                             sizeof optval);
1692                         if (error)
1693                                 return (error);
1694
1695                         INP_WLOCK_RECHECK(inp);
1696                         if (optval > 0 && optval <= tp->t_maxseg &&
1697                             optval + 40 >= V_tcp_minmss)
1698                                 tp->t_maxseg = optval;
1699                         else
1700                                 error = EINVAL;
1701                         goto unlock_and_done;
1702
1703                 case TCP_INFO:
1704                         INP_WUNLOCK(inp);
1705                         error = EINVAL;
1706                         break;
1707
1708                 case TCP_CONGESTION:
1709                         INP_WUNLOCK(inp);
1710                         error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1711                         if (error)
1712                                 break;
1713                         buf[sopt->sopt_valsize] = '\0';
1714                         INP_WLOCK_RECHECK(inp);
1715                         CC_LIST_RLOCK();
1716                         STAILQ_FOREACH(algo, &cc_list, entries)
1717                                 if (strncmp(buf, algo->name,
1718                                     TCP_CA_NAME_MAX) == 0)
1719                                         break;
1720                         CC_LIST_RUNLOCK();
1721                         if (algo == NULL) {
1722                                 INP_WUNLOCK(inp);
1723                                 error = EINVAL;
1724                                 break;
1725                         }
1726                         /*
1727                          * We hold a write lock over the tcb so it's safe to
1728                          * do these things without ordering concerns.
1729                          */
1730                         if (CC_ALGO(tp)->cb_destroy != NULL)
1731                                 CC_ALGO(tp)->cb_destroy(tp->ccv);
1732                         CC_DATA(tp) = NULL;
1733                         CC_ALGO(tp) = algo;
1734                         /*
1735                          * If something goes pear shaped initialising the new
1736                          * algo, fall back to newreno (which does not
1737                          * require initialisation).
1738                          */
1739                         if (algo->cb_init != NULL &&
1740                             algo->cb_init(tp->ccv) != 0) {
1741                                 CC_ALGO(tp) = &newreno_cc_algo;
1742                                 /*
1743                                  * The only reason init should fail is
1744                                  * because of malloc.
1745                                  */
1746                                 error = ENOMEM;
1747                         }
1748                         INP_WUNLOCK(inp);
1749                         break;
1750
1751                 case TCP_KEEPIDLE:
1752                 case TCP_KEEPINTVL:
1753                 case TCP_KEEPINIT:
1754                         INP_WUNLOCK(inp);
1755                         error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1756                         if (error)
1757                                 return (error);
1758
1759                         if (ui > (UINT_MAX / hz)) {
1760                                 error = EINVAL;
1761                                 break;
1762                         }
1763                         ui *= hz;
1764
1765                         INP_WLOCK_RECHECK(inp);
1766                         switch (sopt->sopt_name) {
1767                         case TCP_KEEPIDLE:
1768                                 tp->t_keepidle = ui;
1769                                 /*
1770                                  * XXX: better check current remaining
1771                                  * timeout and "merge" it with new value.
1772                                  */
1773                                 if ((tp->t_state > TCPS_LISTEN) &&
1774                                     (tp->t_state <= TCPS_CLOSING))
1775                                         tcp_timer_activate(tp, TT_KEEP,
1776                                             TP_KEEPIDLE(tp));
1777                                 break;
1778                         case TCP_KEEPINTVL:
1779                                 tp->t_keepintvl = ui;
1780                                 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1781                                     (TP_MAXIDLE(tp) > 0))
1782                                         tcp_timer_activate(tp, TT_2MSL,
1783                                             TP_MAXIDLE(tp));
1784                                 break;
1785                         case TCP_KEEPINIT:
1786                                 tp->t_keepinit = ui;
1787                                 if (tp->t_state == TCPS_SYN_RECEIVED ||
1788                                     tp->t_state == TCPS_SYN_SENT)
1789                                         tcp_timer_activate(tp, TT_KEEP,
1790                                             TP_KEEPINIT(tp));
1791                                 break;
1792                         }
1793                         goto unlock_and_done;
1794
1795                 case TCP_KEEPCNT:
1796                         INP_WUNLOCK(inp);
1797                         error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1798                         if (error)
1799                                 return (error);
1800
1801                         INP_WLOCK_RECHECK(inp);
1802                         tp->t_keepcnt = ui;
1803                         if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1804                             (TP_MAXIDLE(tp) > 0))
1805                                 tcp_timer_activate(tp, TT_2MSL,
1806                                     TP_MAXIDLE(tp));
1807                         goto unlock_and_done;
1808
1809 #ifdef TCPPCAP
1810                 case TCP_PCAP_OUT:
1811                 case TCP_PCAP_IN:
1812                         INP_WUNLOCK(inp);
1813                         error = sooptcopyin(sopt, &optval, sizeof optval,
1814                             sizeof optval);
1815                         if (error)
1816                                 return (error);
1817
1818                         INP_WLOCK_RECHECK(inp);
1819                         if (optval >= 0)
1820                                 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
1821                                         &(tp->t_outpkts) : &(tp->t_inpkts),
1822                                         optval);
1823                         else
1824                                 error = EINVAL;
1825                         goto unlock_and_done;
1826 #endif
1827
1828                 case TCP_FASTOPEN: {
1829                         struct tcp_fastopen tfo_optval;
1830
1831                         INP_WUNLOCK(inp);
1832                         if (!V_tcp_fastopen_client_enable &&
1833                             !V_tcp_fastopen_server_enable)
1834                                 return (EPERM);
1835
1836                         error = sooptcopyin(sopt, &tfo_optval,
1837                                     sizeof(tfo_optval), sizeof(int));
1838                         if (error)
1839                                 return (error);
1840
1841                         INP_WLOCK_RECHECK(inp);
1842                         if (tfo_optval.enable) {
1843                                 if (tp->t_state == TCPS_LISTEN) {
1844                                         if (!V_tcp_fastopen_server_enable) {
1845                                                 error = EPERM;
1846                                                 goto unlock_and_done;
1847                                         }
1848
1849                                         tp->t_flags |= TF_FASTOPEN;
1850                                         if (tp->t_tfo_pending == NULL)
1851                                                 tp->t_tfo_pending =
1852                                                     tcp_fastopen_alloc_counter();
1853                                 } else {
1854                                         /*
1855                                          * If a pre-shared key was provided,
1856                                          * stash it in the client cookie
1857                                          * field of the tcpcb for use during
1858                                          * connect.
1859                                          */
1860                                         if (sopt->sopt_valsize ==
1861                                             sizeof(tfo_optval)) {
1862                                                 memcpy(tp->t_tfo_cookie.client,
1863                                                        tfo_optval.psk,
1864                                                        TCP_FASTOPEN_PSK_LEN);
1865                                                 tp->t_tfo_client_cookie_len =
1866                                                     TCP_FASTOPEN_PSK_LEN;
1867                                         }
1868                                         tp->t_flags |= TF_FASTOPEN;
1869                                 }
1870                         } else
1871                                 tp->t_flags &= ~TF_FASTOPEN;
1872                         goto unlock_and_done;
1873                 }
1874
1875 #ifdef TCP_BLACKBOX
1876                 case TCP_LOG:
1877                         INP_WUNLOCK(inp);
1878                         error = sooptcopyin(sopt, &optval, sizeof optval,
1879                             sizeof optval);
1880                         if (error)
1881                                 return (error);
1882
1883                         INP_WLOCK_RECHECK(inp);
1884                         error = tcp_log_state_change(tp, optval);
1885                         goto unlock_and_done;
1886
1887                 case TCP_LOGBUF:
1888                         INP_WUNLOCK(inp);
1889                         error = EINVAL;
1890                         break;
1891
1892                 case TCP_LOGID:
1893                         INP_WUNLOCK(inp);
1894                         error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
1895                         if (error)
1896                                 break;
1897                         buf[sopt->sopt_valsize] = '\0';
1898                         INP_WLOCK_RECHECK(inp);
1899                         error = tcp_log_set_id(tp, buf);
1900                         /* tcp_log_set_id() unlocks the INP. */
1901                         break;
1902
1903                 case TCP_LOGDUMP:
1904                 case TCP_LOGDUMPID:
1905                         INP_WUNLOCK(inp);
1906                         error =
1907                             sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
1908                         if (error)
1909                                 break;
1910                         buf[sopt->sopt_valsize] = '\0';
1911                         INP_WLOCK_RECHECK(inp);
1912                         if (sopt->sopt_name == TCP_LOGDUMP) {
1913                                 error = tcp_log_dump_tp_logbuf(tp, buf,
1914                                     M_WAITOK, true);
1915                                 INP_WUNLOCK(inp);
1916                         } else {
1917                                 tcp_log_dump_tp_bucket_logbufs(tp, buf);
1918                                 /*
1919                                  * tcp_log_dump_tp_bucket_logbufs() drops the
1920                                  * INP lock.
1921                                  */
1922                         }
1923                         break;
1924 #endif
1925
1926                 default:
1927                         INP_WUNLOCK(inp);
1928                         error = ENOPROTOOPT;
1929                         break;
1930                 }
1931                 break;
1932
1933         case SOPT_GET:
1934                 tp = intotcpcb(inp);
1935                 switch (sopt->sopt_name) {
1936 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1937                 case TCP_MD5SIG:
1938                         if (!TCPMD5_ENABLED()) {
1939                                 INP_WUNLOCK(inp);
1940                                 return (ENOPROTOOPT);
1941                         }
1942                         error = TCPMD5_PCBCTL(inp, sopt);
1943                         break;
1944 #endif
1945
1946                 case TCP_NODELAY:
1947                         optval = tp->t_flags & TF_NODELAY;
1948                         INP_WUNLOCK(inp);
1949                         error = sooptcopyout(sopt, &optval, sizeof optval);
1950                         break;
1951                 case TCP_MAXSEG:
1952                         optval = tp->t_maxseg;
1953                         INP_WUNLOCK(inp);
1954                         error = sooptcopyout(sopt, &optval, sizeof optval);
1955                         break;
1956                 case TCP_NOOPT:
1957                         optval = tp->t_flags & TF_NOOPT;
1958                         INP_WUNLOCK(inp);
1959                         error = sooptcopyout(sopt, &optval, sizeof optval);
1960                         break;
1961                 case TCP_NOPUSH:
1962                         optval = tp->t_flags & TF_NOPUSH;
1963                         INP_WUNLOCK(inp);
1964                         error = sooptcopyout(sopt, &optval, sizeof optval);
1965                         break;
1966                 case TCP_INFO:
1967                         tcp_fill_info(tp, &ti);
1968                         INP_WUNLOCK(inp);
1969                         error = sooptcopyout(sopt, &ti, sizeof ti);
1970                         break;
1971                 case TCP_CONGESTION:
1972                         len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1973                         INP_WUNLOCK(inp);
1974                         error = sooptcopyout(sopt, buf, len + 1);
1975                         break;
1976                 case TCP_KEEPIDLE:
1977                 case TCP_KEEPINTVL:
1978                 case TCP_KEEPINIT:
1979                 case TCP_KEEPCNT:
1980                         switch (sopt->sopt_name) {
1981                         case TCP_KEEPIDLE:
1982                                 ui = TP_KEEPIDLE(tp) / hz;
1983                                 break;
1984                         case TCP_KEEPINTVL:
1985                                 ui = TP_KEEPINTVL(tp) / hz;
1986                                 break;
1987                         case TCP_KEEPINIT:
1988                                 ui = TP_KEEPINIT(tp) / hz;
1989                                 break;
1990                         case TCP_KEEPCNT:
1991                                 ui = TP_KEEPCNT(tp);
1992                                 break;
1993                         }
1994                         INP_WUNLOCK(inp);
1995                         error = sooptcopyout(sopt, &ui, sizeof(ui));
1996                         break;
1997 #ifdef TCPPCAP
1998                 case TCP_PCAP_OUT:
1999                 case TCP_PCAP_IN:
2000                         optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
2001                                         &(tp->t_outpkts) : &(tp->t_inpkts));
2002                         INP_WUNLOCK(inp);
2003                         error = sooptcopyout(sopt, &optval, sizeof optval);
2004                         break;
2005 #endif
2006                 case TCP_FASTOPEN:
2007                         optval = tp->t_flags & TF_FASTOPEN;
2008                         INP_WUNLOCK(inp);
2009                         error = sooptcopyout(sopt, &optval, sizeof optval);
2010                         break;
2011 #ifdef TCP_BLACKBOX
2012                 case TCP_LOG:
2013                         optval = tp->t_logstate;
2014                         INP_WUNLOCK(inp);
2015                         error = sooptcopyout(sopt, &optval, sizeof(optval));
2016                         break;
2017                 case TCP_LOGBUF:
2018                         /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
2019                         error = tcp_log_getlogbuf(sopt, tp);
2020                         break;
2021                 case TCP_LOGID:
2022                         len = tcp_log_get_id(tp, buf);
2023                         INP_WUNLOCK(inp);
2024                         error = sooptcopyout(sopt, buf, len + 1);
2025                         break;
2026                 case TCP_LOGDUMP:
2027                 case TCP_LOGDUMPID:
2028                         INP_WUNLOCK(inp);
2029                         error = EINVAL;
2030                         break;
2031 #endif
2032                 default:
2033                         INP_WUNLOCK(inp);
2034                         error = ENOPROTOOPT;
2035                         break;
2036                 }
2037                 break;
2038         }
2039         return (error);
2040 }
2041 #undef INP_WLOCK_RECHECK
2042 #undef INP_WLOCK_RECHECK_CLEANUP
2043
2044 /*
2045  * Attach TCP protocol to socket, allocating
2046  * internet protocol control block, tcp control block,
2047  * bufer space, and entering LISTEN state if to accept connections.
2048  */
2049 static int
2050 tcp_attach(struct socket *so)
2051 {
2052         struct tcpcb *tp;
2053         struct inpcb *inp;
2054         struct epoch_tracker et;
2055         int error;
2056
2057         if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
2058                 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
2059                 if (error)
2060                         return (error);
2061         }
2062         so->so_rcv.sb_flags |= SB_AUTOSIZE;
2063         so->so_snd.sb_flags |= SB_AUTOSIZE;
2064         INP_INFO_RLOCK_ET(&V_tcbinfo, et);
2065         error = in_pcballoc(so, &V_tcbinfo);
2066         if (error) {
2067                 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
2068                 return (error);
2069         }
2070         inp = sotoinpcb(so);
2071 #ifdef INET6
2072         if (inp->inp_vflag & INP_IPV6PROTO) {
2073                 inp->inp_vflag |= INP_IPV6;
2074                 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
2075                         inp->inp_vflag |= INP_IPV4;
2076                 inp->in6p_hops = -1;    /* use kernel default */
2077         }
2078         else
2079 #endif
2080         inp->inp_vflag |= INP_IPV4;
2081         tp = tcp_newtcpcb(inp);
2082         if (tp == NULL) {
2083                 in_pcbdetach(inp);
2084                 in_pcbfree(inp);
2085                 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
2086                 return (ENOBUFS);
2087         }
2088         tp->t_state = TCPS_CLOSED;
2089         INP_WUNLOCK(inp);
2090         INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
2091         TCPSTATES_INC(TCPS_CLOSED);
2092         return (0);
2093 }
2094
2095 /*
2096  * Initiate (or continue) disconnect.
2097  * If embryonic state, just send reset (once).
2098  * If in ``let data drain'' option and linger null, just drop.
2099  * Otherwise (hard), mark socket disconnecting and drop
2100  * current input data; switch states based on user close, and
2101  * send segment to peer (with FIN).
2102  */
2103 static void
2104 tcp_disconnect(struct tcpcb *tp)
2105 {
2106         struct inpcb *inp = tp->t_inpcb;
2107         struct socket *so = inp->inp_socket;
2108
2109         INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2110         INP_WLOCK_ASSERT(inp);
2111
2112         /*
2113          * Neither tcp_close() nor tcp_drop() should return NULL, as the
2114          * socket is still open.
2115          */
2116         if (tp->t_state < TCPS_ESTABLISHED) {
2117                 tp = tcp_close(tp);
2118                 KASSERT(tp != NULL,
2119                     ("tcp_disconnect: tcp_close() returned NULL"));
2120         } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2121                 tp = tcp_drop(tp, 0);
2122                 KASSERT(tp != NULL,
2123                     ("tcp_disconnect: tcp_drop() returned NULL"));
2124         } else {
2125                 soisdisconnecting(so);
2126                 sbflush(&so->so_rcv);
2127                 tcp_usrclosed(tp);
2128                 if (!(inp->inp_flags & INP_DROPPED))
2129                         tp->t_fb->tfb_tcp_output(tp);
2130         }
2131 }
2132
2133 /*
2134  * User issued close, and wish to trail through shutdown states:
2135  * if never received SYN, just forget it.  If got a SYN from peer,
2136  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2137  * If already got a FIN from peer, then almost done; go to LAST_ACK
2138  * state.  In all other cases, have already sent FIN to peer (e.g.
2139  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2140  * for peer to send FIN or not respond to keep-alives, etc.
2141  * We can let the user exit from the close as soon as the FIN is acked.
2142  */
2143 static void
2144 tcp_usrclosed(struct tcpcb *tp)
2145 {
2146
2147         INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2148         INP_WLOCK_ASSERT(tp->t_inpcb);
2149
2150         switch (tp->t_state) {
2151         case TCPS_LISTEN:
2152 #ifdef TCP_OFFLOAD
2153                 tcp_offload_listen_stop(tp);
2154 #endif
2155                 tcp_state_change(tp, TCPS_CLOSED);
2156                 /* FALLTHROUGH */
2157         case TCPS_CLOSED:
2158                 tp = tcp_close(tp);
2159                 /*
2160                  * tcp_close() should never return NULL here as the socket is
2161                  * still open.
2162                  */
2163                 KASSERT(tp != NULL,
2164                     ("tcp_usrclosed: tcp_close() returned NULL"));
2165                 break;
2166
2167         case TCPS_SYN_SENT:
2168         case TCPS_SYN_RECEIVED:
2169                 tp->t_flags |= TF_NEEDFIN;
2170                 break;
2171
2172         case TCPS_ESTABLISHED:
2173                 tcp_state_change(tp, TCPS_FIN_WAIT_1);
2174                 break;
2175
2176         case TCPS_CLOSE_WAIT:
2177                 tcp_state_change(tp, TCPS_LAST_ACK);
2178                 break;
2179         }
2180         if (tp->t_state >= TCPS_FIN_WAIT_2) {
2181                 soisdisconnected(tp->t_inpcb->inp_socket);
2182                 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
2183                 if (tp->t_state == TCPS_FIN_WAIT_2) {
2184                         int timeout;
2185
2186                         timeout = (tcp_fast_finwait2_recycle) ? 
2187                             tcp_finwait2_timeout : TP_MAXIDLE(tp);
2188                         tcp_timer_activate(tp, TT_2MSL, timeout);
2189                 }
2190         }
2191 }
2192
2193 #ifdef DDB
2194 static void
2195 db_print_indent(int indent)
2196 {
2197         int i;
2198
2199         for (i = 0; i < indent; i++)
2200                 db_printf(" ");
2201 }
2202
2203 static void
2204 db_print_tstate(int t_state)
2205 {
2206
2207         switch (t_state) {
2208         case TCPS_CLOSED:
2209                 db_printf("TCPS_CLOSED");
2210                 return;
2211
2212         case TCPS_LISTEN:
2213                 db_printf("TCPS_LISTEN");
2214                 return;
2215
2216         case TCPS_SYN_SENT:
2217                 db_printf("TCPS_SYN_SENT");
2218                 return;
2219
2220         case TCPS_SYN_RECEIVED:
2221                 db_printf("TCPS_SYN_RECEIVED");
2222                 return;
2223
2224         case TCPS_ESTABLISHED:
2225                 db_printf("TCPS_ESTABLISHED");
2226                 return;
2227
2228         case TCPS_CLOSE_WAIT:
2229                 db_printf("TCPS_CLOSE_WAIT");
2230                 return;
2231
2232         case TCPS_FIN_WAIT_1:
2233                 db_printf("TCPS_FIN_WAIT_1");
2234                 return;
2235
2236         case TCPS_CLOSING:
2237                 db_printf("TCPS_CLOSING");
2238                 return;
2239
2240         case TCPS_LAST_ACK:
2241                 db_printf("TCPS_LAST_ACK");
2242                 return;
2243
2244         case TCPS_FIN_WAIT_2:
2245                 db_printf("TCPS_FIN_WAIT_2");
2246                 return;
2247
2248         case TCPS_TIME_WAIT:
2249                 db_printf("TCPS_TIME_WAIT");
2250                 return;
2251
2252         default:
2253                 db_printf("unknown");
2254                 return;
2255         }
2256 }
2257
2258 static void
2259 db_print_tflags(u_int t_flags)
2260 {
2261         int comma;
2262
2263         comma = 0;
2264         if (t_flags & TF_ACKNOW) {
2265                 db_printf("%sTF_ACKNOW", comma ? ", " : "");
2266                 comma = 1;
2267         }
2268         if (t_flags & TF_DELACK) {
2269                 db_printf("%sTF_DELACK", comma ? ", " : "");
2270                 comma = 1;
2271         }
2272         if (t_flags & TF_NODELAY) {
2273                 db_printf("%sTF_NODELAY", comma ? ", " : "");
2274                 comma = 1;
2275         }
2276         if (t_flags & TF_NOOPT) {
2277                 db_printf("%sTF_NOOPT", comma ? ", " : "");
2278                 comma = 1;
2279         }
2280         if (t_flags & TF_SENTFIN) {
2281                 db_printf("%sTF_SENTFIN", comma ? ", " : "");
2282                 comma = 1;
2283         }
2284         if (t_flags & TF_REQ_SCALE) {
2285                 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2286                 comma = 1;
2287         }
2288         if (t_flags & TF_RCVD_SCALE) {
2289                 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2290                 comma = 1;
2291         }
2292         if (t_flags & TF_REQ_TSTMP) {
2293                 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2294                 comma = 1;
2295         }
2296         if (t_flags & TF_RCVD_TSTMP) {
2297                 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2298                 comma = 1;
2299         }
2300         if (t_flags & TF_SACK_PERMIT) {
2301                 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2302                 comma = 1;
2303         }
2304         if (t_flags & TF_NEEDSYN) {
2305                 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2306                 comma = 1;
2307         }
2308         if (t_flags & TF_NEEDFIN) {
2309                 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2310                 comma = 1;
2311         }
2312         if (t_flags & TF_NOPUSH) {
2313                 db_printf("%sTF_NOPUSH", comma ? ", " : "");
2314                 comma = 1;
2315         }
2316         if (t_flags & TF_MORETOCOME) {
2317                 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2318                 comma = 1;
2319         }
2320         if (t_flags & TF_LQ_OVERFLOW) {
2321                 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2322                 comma = 1;
2323         }
2324         if (t_flags & TF_LASTIDLE) {
2325                 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2326                 comma = 1;
2327         }
2328         if (t_flags & TF_RXWIN0SENT) {
2329                 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2330                 comma = 1;
2331         }
2332         if (t_flags & TF_FASTRECOVERY) {
2333                 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2334                 comma = 1;
2335         }
2336         if (t_flags & TF_CONGRECOVERY) {
2337                 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2338                 comma = 1;
2339         }
2340         if (t_flags & TF_WASFRECOVERY) {
2341                 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2342                 comma = 1;
2343         }
2344         if (t_flags & TF_SIGNATURE) {
2345                 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2346                 comma = 1;
2347         }
2348         if (t_flags & TF_FORCEDATA) {
2349                 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2350                 comma = 1;
2351         }
2352         if (t_flags & TF_TSO) {
2353                 db_printf("%sTF_TSO", comma ? ", " : "");
2354                 comma = 1;
2355         }
2356         if (t_flags & TF_ECN_PERMIT) {
2357                 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2358                 comma = 1;
2359         }
2360         if (t_flags & TF_FASTOPEN) {
2361                 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2362                 comma = 1;
2363         }
2364 }
2365
2366 static void
2367 db_print_toobflags(char t_oobflags)
2368 {
2369         int comma;
2370
2371         comma = 0;
2372         if (t_oobflags & TCPOOB_HAVEDATA) {
2373                 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2374                 comma = 1;
2375         }
2376         if (t_oobflags & TCPOOB_HADDATA) {
2377                 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2378                 comma = 1;
2379         }
2380 }
2381
2382 static void
2383 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2384 {
2385
2386         db_print_indent(indent);
2387         db_printf("%s at %p\n", name, tp);
2388
2389         indent += 2;
2390
2391         db_print_indent(indent);
2392         db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
2393            LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2394
2395         db_print_indent(indent);
2396         db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
2397             &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2398
2399         db_print_indent(indent);
2400         db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2401             &tp->t_timers->tt_delack, tp->t_inpcb);
2402
2403         db_print_indent(indent);
2404         db_printf("t_state: %d (", tp->t_state);
2405         db_print_tstate(tp->t_state);
2406         db_printf(")\n");
2407
2408         db_print_indent(indent);
2409         db_printf("t_flags: 0x%x (", tp->t_flags);
2410         db_print_tflags(tp->t_flags);
2411         db_printf(")\n");
2412
2413         db_print_indent(indent);
2414         db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
2415             tp->snd_una, tp->snd_max, tp->snd_nxt);
2416
2417         db_print_indent(indent);
2418         db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
2419            tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2420
2421         db_print_indent(indent);
2422         db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
2423             tp->iss, tp->irs, tp->rcv_nxt);
2424
2425         db_print_indent(indent);
2426         db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
2427             tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2428
2429         db_print_indent(indent);
2430         db_printf("snd_wnd: %u   snd_cwnd: %u\n",
2431            tp->snd_wnd, tp->snd_cwnd);
2432
2433         db_print_indent(indent);
2434         db_printf("snd_ssthresh: %u   snd_recover: "
2435             "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2436
2437         db_print_indent(indent);
2438         db_printf("t_rcvtime: %u   t_startime: %u\n",
2439             tp->t_rcvtime, tp->t_starttime);
2440
2441         db_print_indent(indent);
2442         db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
2443             tp->t_rtttime, tp->t_rtseq);
2444
2445         db_print_indent(indent);
2446         db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
2447             tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2448
2449         db_print_indent(indent);
2450         db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
2451             "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2452             tp->t_rttbest);
2453
2454         db_print_indent(indent);
2455         db_printf("t_rttupdated: %lu   max_sndwnd: %u   t_softerror: %d\n",
2456             tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2457
2458         db_print_indent(indent);
2459         db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2460         db_print_toobflags(tp->t_oobflags);
2461         db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
2462
2463         db_print_indent(indent);
2464         db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
2465             tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2466
2467         db_print_indent(indent);
2468         db_printf("ts_recent: %u   ts_recent_age: %u\n",
2469             tp->ts_recent, tp->ts_recent_age);
2470
2471         db_print_indent(indent);
2472         db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
2473             "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2474
2475         db_print_indent(indent);
2476         db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
2477             "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2478             tp->snd_recover_prev, tp->t_badrxtwin);
2479
2480         db_print_indent(indent);
2481         db_printf("snd_numholes: %d  snd_holes first: %p\n",
2482             tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2483
2484         db_print_indent(indent);
2485         db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
2486             "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2487
2488         /* Skip sackblks, sackhint. */
2489
2490         db_print_indent(indent);
2491         db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
2492             tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2493 }
2494
2495 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2496 {
2497         struct tcpcb *tp;
2498
2499         if (!have_addr) {
2500                 db_printf("usage: show tcpcb <addr>\n");
2501                 return;
2502         }
2503         tp = (struct tcpcb *)addr;
2504
2505         db_print_tcpcb(tp, "tcpcb", 0);
2506 }
2507 #endif