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