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