]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/netinet/tcp_usrreq.c
MFC r286873:
[FreeBSD/stable/10.git] / sys / netinet / tcp_usrreq.c
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1993
3  *      The Regents of the University of California.
4  * Copyright (c) 2006-2007 Robert N. M. Watson
5  * Copyright (c) 2010-2011 Juniper Networks, Inc.
6  * All rights reserved.
7  *
8  * Portions of this software were developed by Robert N. M. Watson under
9  * contract to Juniper Networks, Inc.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *      From: @(#)tcp_usrreq.c  8.2 (Berkeley) 1/3/94
36  */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include "opt_ddb.h"
42 #include "opt_inet.h"
43 #include "opt_inet6.h"
44 #include "opt_tcpdebug.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/limits.h>
49 #include <sys/malloc.h>
50 #include <sys/kernel.h>
51 #include <sys/sysctl.h>
52 #include <sys/mbuf.h>
53 #ifdef INET6
54 #include <sys/domain.h>
55 #endif /* INET6 */
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/protosw.h>
59 #include <sys/proc.h>
60 #include <sys/jail.h>
61
62 #ifdef DDB
63 #include <ddb/ddb.h>
64 #endif
65
66 #include <net/if.h>
67 #include <net/route.h>
68 #include <net/vnet.h>
69
70 #include <netinet/cc.h>
71 #include <netinet/in.h>
72 #include <netinet/in_pcb.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/in_var.h>
75 #include <netinet/ip_var.h>
76 #ifdef INET6
77 #include <netinet/ip6.h>
78 #include <netinet6/in6_pcb.h>
79 #include <netinet6/ip6_var.h>
80 #include <netinet6/scope6_var.h>
81 #endif
82 #ifdef TCP_RFC7413
83 #include <netinet/tcp_fastopen.h>
84 #endif
85 #include <netinet/tcp_fsm.h>
86 #include <netinet/tcp_seq.h>
87 #include <netinet/tcp_timer.h>
88 #include <netinet/tcp_var.h>
89 #include <netinet/tcpip.h>
90 #ifdef TCPDEBUG
91 #include <netinet/tcp_debug.h>
92 #endif
93 #ifdef TCP_OFFLOAD
94 #include <netinet/tcp_offload.h>
95 #endif
96
97 /*
98  * TCP protocol interface to socket abstraction.
99  */
100 static int      tcp_attach(struct socket *);
101 #ifdef INET
102 static int      tcp_connect(struct tcpcb *, struct sockaddr *,
103                     struct thread *td);
104 #endif /* INET */
105 #ifdef INET6
106 static int      tcp6_connect(struct tcpcb *, struct sockaddr *,
107                     struct thread *td);
108 #endif /* INET6 */
109 static void     tcp_disconnect(struct tcpcb *);
110 static void     tcp_usrclosed(struct tcpcb *);
111 static void     tcp_fill_info(struct tcpcb *, struct tcp_info *);
112
113 #ifdef TCPDEBUG
114 #define TCPDEBUG0       int ostate = 0
115 #define TCPDEBUG1()     ostate = tp ? tp->t_state : 0
116 #define TCPDEBUG2(req)  if (tp && (so->so_options & SO_DEBUG)) \
117                                 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
118 #else
119 #define TCPDEBUG0
120 #define TCPDEBUG1()
121 #define TCPDEBUG2(req)
122 #endif
123
124 /*
125  * TCP attaches to socket via pru_attach(), reserving space,
126  * and an internet control block.
127  */
128 static int
129 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
130 {
131         struct inpcb *inp;
132         struct tcpcb *tp = NULL;
133         int error;
134         TCPDEBUG0;
135
136         inp = sotoinpcb(so);
137         KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
138         TCPDEBUG1();
139
140         error = tcp_attach(so);
141         if (error)
142                 goto out;
143
144         if ((so->so_options & SO_LINGER) && so->so_linger == 0)
145                 so->so_linger = TCP_LINGERTIME;
146
147         inp = sotoinpcb(so);
148         tp = intotcpcb(inp);
149 out:
150         TCPDEBUG2(PRU_ATTACH);
151         return error;
152 }
153
154 /*
155  * tcp_detach is called when the socket layer loses its final reference
156  * to the socket, be it a file descriptor reference, a reference from TCP,
157  * etc.  At this point, there is only one case in which we will keep around
158  * inpcb state: time wait.
159  *
160  * This function can probably be re-absorbed back into tcp_usr_detach() now
161  * that there is a single detach path.
162  */
163 static void
164 tcp_detach(struct socket *so, struct inpcb *inp)
165 {
166         struct tcpcb *tp;
167
168         INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
169         INP_WLOCK_ASSERT(inp);
170
171         KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
172         KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
173
174         tp = intotcpcb(inp);
175
176         if (inp->inp_flags & INP_TIMEWAIT) {
177                 /*
178                  * There are two cases to handle: one in which the time wait
179                  * state is being discarded (INP_DROPPED), and one in which
180                  * this connection will remain in timewait.  In the former,
181                  * it is time to discard all state (except tcptw, which has
182                  * already been discarded by the timewait close code, which
183                  * should be further up the call stack somewhere).  In the
184                  * latter case, we detach from the socket, but leave the pcb
185                  * present until timewait ends.
186                  *
187                  * XXXRW: Would it be cleaner to free the tcptw here?
188                  *
189                  * Astute question indeed, from twtcp perspective there are
190                  * three cases to consider:
191                  *
192                  * #1 tcp_detach is called at tcptw creation time by
193                  *  tcp_twstart, then do not discard the newly created tcptw
194                  *  and leave inpcb present until timewait ends
195                  * #2 tcp_detach is called at timewait end (or reuse) by
196                  *  tcp_twclose, then the tcptw has already been discarded
197                  *  and inpcb is freed here
198                  * #3 tcp_detach is called() after timewait ends (or reuse)
199                  *  (e.g. by soclose), then tcptw has already been discarded
200                  *  and inpcb is freed here
201                  *
202                  *  In all three cases the tcptw should not be freed here.
203                  */
204                 if (inp->inp_flags & INP_DROPPED) {
205                         KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
206                             "INP_DROPPED && tp != NULL"));
207                         in_pcbdetach(inp);
208                         in_pcbfree(inp);
209                 } else {
210                         in_pcbdetach(inp);
211                         INP_WUNLOCK(inp);
212                 }
213         } else {
214                 /*
215                  * If the connection is not in timewait, we consider two
216                  * two conditions: one in which no further processing is
217                  * necessary (dropped || embryonic), and one in which TCP is
218                  * not yet done, but no longer requires the socket, so the
219                  * pcb will persist for the time being.
220                  *
221                  * XXXRW: Does the second case still occur?
222                  */
223                 if (inp->inp_flags & INP_DROPPED ||
224                     tp->t_state < TCPS_SYN_SENT) {
225                         tcp_discardcb(tp);
226                         in_pcbdetach(inp);
227                         in_pcbfree(inp);
228                 } else {
229                         in_pcbdetach(inp);
230                         INP_WUNLOCK(inp);
231                 }
232         }
233 }
234
235 /*
236  * pru_detach() detaches the TCP protocol from the socket.
237  * If the protocol state is non-embryonic, then can't
238  * do this directly: have to initiate a pru_disconnect(),
239  * which may finish later; embryonic TCB's can just
240  * be discarded here.
241  */
242 static void
243 tcp_usr_detach(struct socket *so)
244 {
245         struct inpcb *inp;
246
247         inp = sotoinpcb(so);
248         KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
249         INP_INFO_WLOCK(&V_tcbinfo);
250         INP_WLOCK(inp);
251         KASSERT(inp->inp_socket != NULL,
252             ("tcp_usr_detach: inp_socket == NULL"));
253         tcp_detach(so, inp);
254         INP_INFO_WUNLOCK(&V_tcbinfo);
255 }
256
257 #ifdef INET
258 /*
259  * Give the socket an address.
260  */
261 static int
262 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
263 {
264         int error = 0;
265         struct inpcb *inp;
266         struct tcpcb *tp = NULL;
267         struct sockaddr_in *sinp;
268
269         sinp = (struct sockaddr_in *)nam;
270         if (nam->sa_len != sizeof (*sinp))
271                 return (EINVAL);
272         /*
273          * Must check for multicast addresses and disallow binding
274          * to them.
275          */
276         if (sinp->sin_family == AF_INET &&
277             IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
278                 return (EAFNOSUPPORT);
279
280         TCPDEBUG0;
281         inp = sotoinpcb(so);
282         KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
283         INP_WLOCK(inp);
284         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
285                 error = EINVAL;
286                 goto out;
287         }
288         tp = intotcpcb(inp);
289         TCPDEBUG1();
290         INP_HASH_WLOCK(&V_tcbinfo);
291         error = in_pcbbind(inp, nam, td->td_ucred);
292         INP_HASH_WUNLOCK(&V_tcbinfo);
293 out:
294         TCPDEBUG2(PRU_BIND);
295         INP_WUNLOCK(inp);
296
297         return (error);
298 }
299 #endif /* INET */
300
301 #ifdef INET6
302 static int
303 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
304 {
305         int error = 0;
306         struct inpcb *inp;
307         struct tcpcb *tp = NULL;
308         struct sockaddr_in6 *sin6p;
309
310         sin6p = (struct sockaddr_in6 *)nam;
311         if (nam->sa_len != sizeof (*sin6p))
312                 return (EINVAL);
313         /*
314          * Must check for multicast addresses and disallow binding
315          * to them.
316          */
317         if (sin6p->sin6_family == AF_INET6 &&
318             IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
319                 return (EAFNOSUPPORT);
320
321         TCPDEBUG0;
322         inp = sotoinpcb(so);
323         KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
324         INP_WLOCK(inp);
325         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
326                 error = EINVAL;
327                 goto out;
328         }
329         tp = intotcpcb(inp);
330         TCPDEBUG1();
331         INP_HASH_WLOCK(&V_tcbinfo);
332         inp->inp_vflag &= ~INP_IPV4;
333         inp->inp_vflag |= INP_IPV6;
334 #ifdef INET
335         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
336                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
337                         inp->inp_vflag |= INP_IPV4;
338                 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
339                         struct sockaddr_in sin;
340
341                         in6_sin6_2_sin(&sin, sin6p);
342                         inp->inp_vflag |= INP_IPV4;
343                         inp->inp_vflag &= ~INP_IPV6;
344                         error = in_pcbbind(inp, (struct sockaddr *)&sin,
345                             td->td_ucred);
346                         INP_HASH_WUNLOCK(&V_tcbinfo);
347                         goto out;
348                 }
349         }
350 #endif
351         error = in6_pcbbind(inp, nam, td->td_ucred);
352         INP_HASH_WUNLOCK(&V_tcbinfo);
353 out:
354         TCPDEBUG2(PRU_BIND);
355         INP_WUNLOCK(inp);
356         return (error);
357 }
358 #endif /* INET6 */
359
360 #ifdef INET
361 /*
362  * Prepare to accept connections.
363  */
364 static int
365 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
366 {
367         int error = 0;
368         struct inpcb *inp;
369         struct tcpcb *tp = NULL;
370
371         TCPDEBUG0;
372         inp = sotoinpcb(so);
373         KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
374         INP_WLOCK(inp);
375         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
376                 error = EINVAL;
377                 goto out;
378         }
379         tp = intotcpcb(inp);
380         TCPDEBUG1();
381         SOCK_LOCK(so);
382         error = solisten_proto_check(so);
383         INP_HASH_WLOCK(&V_tcbinfo);
384         if (error == 0 && inp->inp_lport == 0)
385                 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
386         INP_HASH_WUNLOCK(&V_tcbinfo);
387         if (error == 0) {
388                 tcp_state_change(tp, TCPS_LISTEN);
389                 solisten_proto(so, backlog);
390 #ifdef TCP_OFFLOAD
391                 if ((so->so_options & SO_NO_OFFLOAD) == 0)
392                         tcp_offload_listen_start(tp);
393 #endif
394         }
395         SOCK_UNLOCK(so);
396
397 #ifdef TCP_RFC7413
398         if (tp->t_flags & TF_FASTOPEN)
399                 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
400 #endif
401 out:
402         TCPDEBUG2(PRU_LISTEN);
403         INP_WUNLOCK(inp);
404         return (error);
405 }
406 #endif /* INET */
407
408 #ifdef INET6
409 static int
410 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
411 {
412         int error = 0;
413         struct inpcb *inp;
414         struct tcpcb *tp = NULL;
415
416         TCPDEBUG0;
417         inp = sotoinpcb(so);
418         KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
419         INP_WLOCK(inp);
420         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
421                 error = EINVAL;
422                 goto out;
423         }
424         tp = intotcpcb(inp);
425         TCPDEBUG1();
426         SOCK_LOCK(so);
427         error = solisten_proto_check(so);
428         INP_HASH_WLOCK(&V_tcbinfo);
429         if (error == 0 && inp->inp_lport == 0) {
430                 inp->inp_vflag &= ~INP_IPV4;
431                 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
432                         inp->inp_vflag |= INP_IPV4;
433                 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
434         }
435         INP_HASH_WUNLOCK(&V_tcbinfo);
436         if (error == 0) {
437                 tcp_state_change(tp, TCPS_LISTEN);
438                 solisten_proto(so, backlog);
439 #ifdef TCP_OFFLOAD
440                 if ((so->so_options & SO_NO_OFFLOAD) == 0)
441                         tcp_offload_listen_start(tp);
442 #endif
443         }
444         SOCK_UNLOCK(so);
445
446 #ifdef TCP_RFC7413
447         if (tp->t_flags & TF_FASTOPEN)
448                 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
449 #endif
450 out:
451         TCPDEBUG2(PRU_LISTEN);
452         INP_WUNLOCK(inp);
453         return (error);
454 }
455 #endif /* INET6 */
456
457 #ifdef INET
458 /*
459  * Initiate connection to peer.
460  * Create a template for use in transmissions on this connection.
461  * Enter SYN_SENT state, and mark socket as connecting.
462  * Start keep-alive timer, and seed output sequence space.
463  * Send initial segment on connection.
464  */
465 static int
466 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
467 {
468         int error = 0;
469         struct inpcb *inp;
470         struct tcpcb *tp = NULL;
471         struct sockaddr_in *sinp;
472
473         sinp = (struct sockaddr_in *)nam;
474         if (nam->sa_len != sizeof (*sinp))
475                 return (EINVAL);
476         /*
477          * Must disallow TCP ``connections'' to multicast addresses.
478          */
479         if (sinp->sin_family == AF_INET
480             && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
481                 return (EAFNOSUPPORT);
482         if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
483                 return (error);
484
485         TCPDEBUG0;
486         inp = sotoinpcb(so);
487         KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
488         INP_WLOCK(inp);
489         if (inp->inp_flags & INP_TIMEWAIT) {
490                 error = EADDRINUSE;
491                 goto out;
492         }
493         if (inp->inp_flags & INP_DROPPED) {
494                 error = ECONNREFUSED;
495                 goto out;
496         }
497         tp = intotcpcb(inp);
498         TCPDEBUG1();
499         if ((error = tcp_connect(tp, nam, td)) != 0)
500                 goto out;
501 #ifdef TCP_OFFLOAD
502         if (registered_toedevs > 0 &&
503             (so->so_options & SO_NO_OFFLOAD) == 0 &&
504             (error = tcp_offload_connect(so, nam)) == 0)
505                 goto out;
506 #endif
507         tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
508         error = tcp_output(tp);
509 out:
510         TCPDEBUG2(PRU_CONNECT);
511         INP_WUNLOCK(inp);
512         return (error);
513 }
514 #endif /* INET */
515
516 #ifdef INET6
517 static int
518 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
519 {
520         int error = 0;
521         struct inpcb *inp;
522         struct tcpcb *tp = NULL;
523         struct sockaddr_in6 *sin6p;
524
525         TCPDEBUG0;
526
527         sin6p = (struct sockaddr_in6 *)nam;
528         if (nam->sa_len != sizeof (*sin6p))
529                 return (EINVAL);
530         /*
531          * Must disallow TCP ``connections'' to multicast addresses.
532          */
533         if (sin6p->sin6_family == AF_INET6
534             && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
535                 return (EAFNOSUPPORT);
536
537         inp = sotoinpcb(so);
538         KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
539         INP_WLOCK(inp);
540         if (inp->inp_flags & INP_TIMEWAIT) {
541                 error = EADDRINUSE;
542                 goto out;
543         }
544         if (inp->inp_flags & INP_DROPPED) {
545                 error = ECONNREFUSED;
546                 goto out;
547         }
548         tp = intotcpcb(inp);
549         TCPDEBUG1();
550 #ifdef INET
551         /*
552          * XXXRW: Some confusion: V4/V6 flags relate to binding, and
553          * therefore probably require the hash lock, which isn't held here.
554          * Is this a significant problem?
555          */
556         if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
557                 struct sockaddr_in sin;
558
559                 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
560                         error = EINVAL;
561                         goto out;
562                 }
563
564                 in6_sin6_2_sin(&sin, sin6p);
565                 inp->inp_vflag |= INP_IPV4;
566                 inp->inp_vflag &= ~INP_IPV6;
567                 if ((error = prison_remote_ip4(td->td_ucred,
568                     &sin.sin_addr)) != 0)
569                         goto out;
570                 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
571                         goto out;
572 #ifdef TCP_OFFLOAD
573                 if (registered_toedevs > 0 &&
574                     (so->so_options & SO_NO_OFFLOAD) == 0 &&
575                     (error = tcp_offload_connect(so, nam)) == 0)
576                         goto out;
577 #endif
578                 error = tcp_output(tp);
579                 goto out;
580         }
581 #endif
582         inp->inp_vflag &= ~INP_IPV4;
583         inp->inp_vflag |= INP_IPV6;
584         inp->inp_inc.inc_flags |= INC_ISIPV6;
585         if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
586                 goto out;
587         if ((error = tcp6_connect(tp, nam, td)) != 0)
588                 goto out;
589 #ifdef TCP_OFFLOAD
590         if (registered_toedevs > 0 &&
591             (so->so_options & SO_NO_OFFLOAD) == 0 &&
592             (error = tcp_offload_connect(so, nam)) == 0)
593                 goto out;
594 #endif
595         tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
596         error = tcp_output(tp);
597
598 out:
599         TCPDEBUG2(PRU_CONNECT);
600         INP_WUNLOCK(inp);
601         return (error);
602 }
603 #endif /* INET6 */
604
605 /*
606  * Initiate disconnect from peer.
607  * If connection never passed embryonic stage, just drop;
608  * else if don't need to let data drain, then can just drop anyways,
609  * else have to begin TCP shutdown process: mark socket disconnecting,
610  * drain unread data, state switch to reflect user close, and
611  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
612  * when peer sends FIN and acks ours.
613  *
614  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
615  */
616 static int
617 tcp_usr_disconnect(struct socket *so)
618 {
619         struct inpcb *inp;
620         struct tcpcb *tp = NULL;
621         int error = 0;
622
623         TCPDEBUG0;
624         INP_INFO_WLOCK(&V_tcbinfo);
625         inp = sotoinpcb(so);
626         KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
627         INP_WLOCK(inp);
628         if (inp->inp_flags & INP_TIMEWAIT)
629                 goto out;
630         if (inp->inp_flags & INP_DROPPED) {
631                 error = ECONNRESET;
632                 goto out;
633         }
634         tp = intotcpcb(inp);
635         TCPDEBUG1();
636         tcp_disconnect(tp);
637 out:
638         TCPDEBUG2(PRU_DISCONNECT);
639         INP_WUNLOCK(inp);
640         INP_INFO_WUNLOCK(&V_tcbinfo);
641         return (error);
642 }
643
644 #ifdef INET
645 /*
646  * Accept a connection.  Essentially all the work is done at higher levels;
647  * just return the address of the peer, storing through addr.
648  */
649 static int
650 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
651 {
652         int error = 0;
653         struct inpcb *inp = NULL;
654         struct tcpcb *tp = NULL;
655         struct in_addr addr;
656         in_port_t port = 0;
657         TCPDEBUG0;
658
659         if (so->so_state & SS_ISDISCONNECTED)
660                 return (ECONNABORTED);
661
662         inp = sotoinpcb(so);
663         KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
664         INP_WLOCK(inp);
665         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
666                 error = ECONNABORTED;
667                 goto out;
668         }
669         tp = intotcpcb(inp);
670         TCPDEBUG1();
671
672         /*
673          * We inline in_getpeeraddr and COMMON_END here, so that we can
674          * copy the data of interest and defer the malloc until after we
675          * release the lock.
676          */
677         port = inp->inp_fport;
678         addr = inp->inp_faddr;
679
680 out:
681         TCPDEBUG2(PRU_ACCEPT);
682         INP_WUNLOCK(inp);
683         if (error == 0)
684                 *nam = in_sockaddr(port, &addr);
685         return error;
686 }
687 #endif /* INET */
688
689 #ifdef INET6
690 static int
691 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
692 {
693         struct inpcb *inp = NULL;
694         int error = 0;
695         struct tcpcb *tp = NULL;
696         struct in_addr addr;
697         struct in6_addr addr6;
698         in_port_t port = 0;
699         int v4 = 0;
700         TCPDEBUG0;
701
702         if (so->so_state & SS_ISDISCONNECTED)
703                 return (ECONNABORTED);
704
705         inp = sotoinpcb(so);
706         KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
707         INP_INFO_RLOCK(&V_tcbinfo);
708         INP_WLOCK(inp);
709         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
710                 error = ECONNABORTED;
711                 goto out;
712         }
713         tp = intotcpcb(inp);
714         TCPDEBUG1();
715
716         /*
717          * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
718          * copy the data of interest and defer the malloc until after we
719          * release the lock.
720          */
721         if (inp->inp_vflag & INP_IPV4) {
722                 v4 = 1;
723                 port = inp->inp_fport;
724                 addr = inp->inp_faddr;
725         } else {
726                 port = inp->inp_fport;
727                 addr6 = inp->in6p_faddr;
728         }
729
730 out:
731         TCPDEBUG2(PRU_ACCEPT);
732         INP_WUNLOCK(inp);
733         INP_INFO_RUNLOCK(&V_tcbinfo);
734         if (error == 0) {
735                 if (v4)
736                         *nam = in6_v4mapsin6_sockaddr(port, &addr);
737                 else
738                         *nam = in6_sockaddr(port, &addr6);
739         }
740         return error;
741 }
742 #endif /* INET6 */
743
744 /*
745  * Mark the connection as being incapable of further output.
746  */
747 static int
748 tcp_usr_shutdown(struct socket *so)
749 {
750         int error = 0;
751         struct inpcb *inp;
752         struct tcpcb *tp = NULL;
753
754         TCPDEBUG0;
755         INP_INFO_WLOCK(&V_tcbinfo);
756         inp = sotoinpcb(so);
757         KASSERT(inp != NULL, ("inp == NULL"));
758         INP_WLOCK(inp);
759         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
760                 error = ECONNRESET;
761                 goto out;
762         }
763         tp = intotcpcb(inp);
764         TCPDEBUG1();
765         socantsendmore(so);
766         tcp_usrclosed(tp);
767         if (!(inp->inp_flags & INP_DROPPED))
768                 error = tcp_output(tp);
769
770 out:
771         TCPDEBUG2(PRU_SHUTDOWN);
772         INP_WUNLOCK(inp);
773         INP_INFO_WUNLOCK(&V_tcbinfo);
774
775         return (error);
776 }
777
778 /*
779  * After a receive, possibly send window update to peer.
780  */
781 static int
782 tcp_usr_rcvd(struct socket *so, int flags)
783 {
784         struct inpcb *inp;
785         struct tcpcb *tp = NULL;
786         int error = 0;
787
788         TCPDEBUG0;
789         inp = sotoinpcb(so);
790         KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
791         INP_WLOCK(inp);
792         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
793                 error = ECONNRESET;
794                 goto out;
795         }
796         tp = intotcpcb(inp);
797         TCPDEBUG1();
798 #ifdef TCP_RFC7413
799         /*
800          * For passively-created TFO connections, don't attempt a window
801          * update while still in SYN_RECEIVED as this may trigger an early
802          * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
803          * application response data, or failing that, when the DELACK timer
804          * expires.
805          */
806         if ((tp->t_flags & TF_FASTOPEN) &&
807             (tp->t_state == TCPS_SYN_RECEIVED))
808                 goto out;
809 #endif
810 #ifdef TCP_OFFLOAD
811         if (tp->t_flags & TF_TOE)
812                 tcp_offload_rcvd(tp);
813         else
814 #endif
815         tcp_output(tp);
816
817 out:
818         TCPDEBUG2(PRU_RCVD);
819         INP_WUNLOCK(inp);
820         return (error);
821 }
822
823 /*
824  * Do a send by putting data in output queue and updating urgent
825  * marker if URG set.  Possibly send more data.  Unlike the other
826  * pru_*() routines, the mbuf chains are our responsibility.  We
827  * must either enqueue them or free them.  The other pru_* routines
828  * generally are caller-frees.
829  */
830 static int
831 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
832     struct sockaddr *nam, struct mbuf *control, struct thread *td)
833 {
834         int error = 0;
835         struct inpcb *inp;
836         struct tcpcb *tp = NULL;
837 #ifdef INET6
838         int isipv6;
839 #endif
840         TCPDEBUG0;
841
842         /*
843          * We require the pcbinfo lock if we will close the socket as part of
844          * this call.
845          */
846         if (flags & PRUS_EOF)
847                 INP_INFO_WLOCK(&V_tcbinfo);
848         inp = sotoinpcb(so);
849         KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
850         INP_WLOCK(inp);
851         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
852                 if (control)
853                         m_freem(control);
854                 if (m)
855                         m_freem(m);
856                 error = ECONNRESET;
857                 goto out;
858         }
859 #ifdef INET6
860         isipv6 = nam && nam->sa_family == AF_INET6;
861 #endif /* INET6 */
862         tp = intotcpcb(inp);
863         TCPDEBUG1();
864         if (control) {
865                 /* TCP doesn't do control messages (rights, creds, etc) */
866                 if (control->m_len) {
867                         m_freem(control);
868                         if (m)
869                                 m_freem(m);
870                         error = EINVAL;
871                         goto out;
872                 }
873                 m_freem(control);       /* empty control, just free it */
874         }
875         if (!(flags & PRUS_OOB)) {
876                 sbappendstream(&so->so_snd, m);
877                 if (nam && tp->t_state < TCPS_SYN_SENT) {
878                         /*
879                          * Do implied connect if not yet connected,
880                          * initialize window to default value, and
881                          * initialize maxseg/maxopd using peer's cached
882                          * MSS.
883                          */
884 #ifdef INET6
885                         if (isipv6)
886                                 error = tcp6_connect(tp, nam, td);
887 #endif /* INET6 */
888 #if defined(INET6) && defined(INET)
889                         else
890 #endif
891 #ifdef INET
892                                 error = tcp_connect(tp, nam, td);
893 #endif
894                         if (error)
895                                 goto out;
896                         tp->snd_wnd = TTCP_CLIENT_SND_WND;
897                         tcp_mss(tp, -1);
898                 }
899                 if (flags & PRUS_EOF) {
900                         /*
901                          * Close the send side of the connection after
902                          * the data is sent.
903                          */
904                         INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
905                         socantsendmore(so);
906                         tcp_usrclosed(tp);
907                 }
908                 if (!(inp->inp_flags & INP_DROPPED)) {
909                         if (flags & PRUS_MORETOCOME)
910                                 tp->t_flags |= TF_MORETOCOME;
911                         error = tcp_output(tp);
912                         if (flags & PRUS_MORETOCOME)
913                                 tp->t_flags &= ~TF_MORETOCOME;
914                 }
915         } else {
916                 /*
917                  * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
918                  */
919                 SOCKBUF_LOCK(&so->so_snd);
920                 if (sbspace(&so->so_snd) < -512) {
921                         SOCKBUF_UNLOCK(&so->so_snd);
922                         m_freem(m);
923                         error = ENOBUFS;
924                         goto out;
925                 }
926                 /*
927                  * According to RFC961 (Assigned Protocols),
928                  * the urgent pointer points to the last octet
929                  * of urgent data.  We continue, however,
930                  * to consider it to indicate the first octet
931                  * of data past the urgent section.
932                  * Otherwise, snd_up should be one lower.
933                  */
934                 sbappendstream_locked(&so->so_snd, m);
935                 SOCKBUF_UNLOCK(&so->so_snd);
936                 if (nam && tp->t_state < TCPS_SYN_SENT) {
937                         /*
938                          * Do implied connect if not yet connected,
939                          * initialize window to default value, and
940                          * initialize maxseg/maxopd using peer's cached
941                          * MSS.
942                          */
943 #ifdef INET6
944                         if (isipv6)
945                                 error = tcp6_connect(tp, nam, td);
946 #endif /* INET6 */
947 #if defined(INET6) && defined(INET)
948                         else
949 #endif
950 #ifdef INET
951                                 error = tcp_connect(tp, nam, td);
952 #endif
953                         if (error)
954                                 goto out;
955                         tp->snd_wnd = TTCP_CLIENT_SND_WND;
956                         tcp_mss(tp, -1);
957                 }
958                 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
959                 tp->t_flags |= TF_FORCEDATA;
960                 error = tcp_output(tp);
961                 tp->t_flags &= ~TF_FORCEDATA;
962         }
963 out:
964         TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
965                   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
966         INP_WUNLOCK(inp);
967         if (flags & PRUS_EOF)
968                 INP_INFO_WUNLOCK(&V_tcbinfo);
969         return (error);
970 }
971
972 /*
973  * Abort the TCP.  Drop the connection abruptly.
974  */
975 static void
976 tcp_usr_abort(struct socket *so)
977 {
978         struct inpcb *inp;
979         struct tcpcb *tp = NULL;
980         TCPDEBUG0;
981
982         inp = sotoinpcb(so);
983         KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
984
985         INP_INFO_WLOCK(&V_tcbinfo);
986         INP_WLOCK(inp);
987         KASSERT(inp->inp_socket != NULL,
988             ("tcp_usr_abort: inp_socket == NULL"));
989
990         /*
991          * If we still have full TCP state, and we're not dropped, drop.
992          */
993         if (!(inp->inp_flags & INP_TIMEWAIT) &&
994             !(inp->inp_flags & INP_DROPPED)) {
995                 tp = intotcpcb(inp);
996                 TCPDEBUG1();
997                 tcp_drop(tp, ECONNABORTED);
998                 TCPDEBUG2(PRU_ABORT);
999         }
1000         if (!(inp->inp_flags & INP_DROPPED)) {
1001                 SOCK_LOCK(so);
1002                 so->so_state |= SS_PROTOREF;
1003                 SOCK_UNLOCK(so);
1004                 inp->inp_flags |= INP_SOCKREF;
1005         }
1006         INP_WUNLOCK(inp);
1007         INP_INFO_WUNLOCK(&V_tcbinfo);
1008 }
1009
1010 /*
1011  * TCP socket is closed.  Start friendly disconnect.
1012  */
1013 static void
1014 tcp_usr_close(struct socket *so)
1015 {
1016         struct inpcb *inp;
1017         struct tcpcb *tp = NULL;
1018         TCPDEBUG0;
1019
1020         inp = sotoinpcb(so);
1021         KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1022
1023         INP_INFO_WLOCK(&V_tcbinfo);
1024         INP_WLOCK(inp);
1025         KASSERT(inp->inp_socket != NULL,
1026             ("tcp_usr_close: inp_socket == NULL"));
1027
1028         /*
1029          * If we still have full TCP state, and we're not dropped, initiate
1030          * a disconnect.
1031          */
1032         if (!(inp->inp_flags & INP_TIMEWAIT) &&
1033             !(inp->inp_flags & INP_DROPPED)) {
1034                 tp = intotcpcb(inp);
1035                 TCPDEBUG1();
1036                 tcp_disconnect(tp);
1037                 TCPDEBUG2(PRU_CLOSE);
1038         }
1039         if (!(inp->inp_flags & INP_DROPPED)) {
1040                 SOCK_LOCK(so);
1041                 so->so_state |= SS_PROTOREF;
1042                 SOCK_UNLOCK(so);
1043                 inp->inp_flags |= INP_SOCKREF;
1044         }
1045         INP_WUNLOCK(inp);
1046         INP_INFO_WUNLOCK(&V_tcbinfo);
1047 }
1048
1049 /*
1050  * Receive out-of-band data.
1051  */
1052 static int
1053 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1054 {
1055         int error = 0;
1056         struct inpcb *inp;
1057         struct tcpcb *tp = NULL;
1058
1059         TCPDEBUG0;
1060         inp = sotoinpcb(so);
1061         KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1062         INP_WLOCK(inp);
1063         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1064                 error = ECONNRESET;
1065                 goto out;
1066         }
1067         tp = intotcpcb(inp);
1068         TCPDEBUG1();
1069         if ((so->so_oobmark == 0 &&
1070              (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1071             so->so_options & SO_OOBINLINE ||
1072             tp->t_oobflags & TCPOOB_HADDATA) {
1073                 error = EINVAL;
1074                 goto out;
1075         }
1076         if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1077                 error = EWOULDBLOCK;
1078                 goto out;
1079         }
1080         m->m_len = 1;
1081         *mtod(m, caddr_t) = tp->t_iobc;
1082         if ((flags & MSG_PEEK) == 0)
1083                 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1084
1085 out:
1086         TCPDEBUG2(PRU_RCVOOB);
1087         INP_WUNLOCK(inp);
1088         return (error);
1089 }
1090
1091 #ifdef INET
1092 struct pr_usrreqs tcp_usrreqs = {
1093         .pru_abort =            tcp_usr_abort,
1094         .pru_accept =           tcp_usr_accept,
1095         .pru_attach =           tcp_usr_attach,
1096         .pru_bind =             tcp_usr_bind,
1097         .pru_connect =          tcp_usr_connect,
1098         .pru_control =          in_control,
1099         .pru_detach =           tcp_usr_detach,
1100         .pru_disconnect =       tcp_usr_disconnect,
1101         .pru_listen =           tcp_usr_listen,
1102         .pru_peeraddr =         in_getpeeraddr,
1103         .pru_rcvd =             tcp_usr_rcvd,
1104         .pru_rcvoob =           tcp_usr_rcvoob,
1105         .pru_send =             tcp_usr_send,
1106         .pru_shutdown =         tcp_usr_shutdown,
1107         .pru_sockaddr =         in_getsockaddr,
1108         .pru_sosetlabel =       in_pcbsosetlabel,
1109         .pru_close =            tcp_usr_close,
1110 };
1111 #endif /* INET */
1112
1113 #ifdef INET6
1114 struct pr_usrreqs tcp6_usrreqs = {
1115         .pru_abort =            tcp_usr_abort,
1116         .pru_accept =           tcp6_usr_accept,
1117         .pru_attach =           tcp_usr_attach,
1118         .pru_bind =             tcp6_usr_bind,
1119         .pru_connect =          tcp6_usr_connect,
1120         .pru_control =          in6_control,
1121         .pru_detach =           tcp_usr_detach,
1122         .pru_disconnect =       tcp_usr_disconnect,
1123         .pru_listen =           tcp6_usr_listen,
1124         .pru_peeraddr =         in6_mapped_peeraddr,
1125         .pru_rcvd =             tcp_usr_rcvd,
1126         .pru_rcvoob =           tcp_usr_rcvoob,
1127         .pru_send =             tcp_usr_send,
1128         .pru_shutdown =         tcp_usr_shutdown,
1129         .pru_sockaddr =         in6_mapped_sockaddr,
1130         .pru_sosetlabel =       in_pcbsosetlabel,
1131         .pru_close =            tcp_usr_close,
1132 };
1133 #endif /* INET6 */
1134
1135 #ifdef INET
1136 /*
1137  * Common subroutine to open a TCP connection to remote host specified
1138  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
1139  * port number if needed.  Call in_pcbconnect_setup to do the routing and
1140  * to choose a local host address (interface).  If there is an existing
1141  * incarnation of the same connection in TIME-WAIT state and if the remote
1142  * host was sending CC options and if the connection duration was < MSL, then
1143  * truncate the previous TIME-WAIT state and proceed.
1144  * Initialize connection parameters and enter SYN-SENT state.
1145  */
1146 static int
1147 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1148 {
1149         struct inpcb *inp = tp->t_inpcb, *oinp;
1150         struct socket *so = inp->inp_socket;
1151         struct in_addr laddr;
1152         u_short lport;
1153         int error;
1154
1155         INP_WLOCK_ASSERT(inp);
1156         INP_HASH_WLOCK(&V_tcbinfo);
1157
1158         if (inp->inp_lport == 0) {
1159                 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1160                 if (error)
1161                         goto out;
1162         }
1163
1164         /*
1165          * Cannot simply call in_pcbconnect, because there might be an
1166          * earlier incarnation of this same connection still in
1167          * TIME_WAIT state, creating an ADDRINUSE error.
1168          */
1169         laddr = inp->inp_laddr;
1170         lport = inp->inp_lport;
1171         error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1172             &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1173         if (error && oinp == NULL)
1174                 goto out;
1175         if (oinp) {
1176                 error = EADDRINUSE;
1177                 goto out;
1178         }
1179         inp->inp_laddr = laddr;
1180         in_pcbrehash(inp);
1181         INP_HASH_WUNLOCK(&V_tcbinfo);
1182
1183         /*
1184          * Compute window scaling to request:
1185          * Scale to fit into sweet spot.  See tcp_syncache.c.
1186          * XXX: This should move to tcp_output().
1187          */
1188         while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1189             (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1190                 tp->request_r_scale++;
1191
1192         soisconnecting(so);
1193         TCPSTAT_INC(tcps_connattempt);
1194         tcp_state_change(tp, TCPS_SYN_SENT);
1195         tp->iss = tcp_new_isn(tp);
1196         tcp_sendseqinit(tp);
1197
1198         return 0;
1199
1200 out:
1201         INP_HASH_WUNLOCK(&V_tcbinfo);
1202         return (error);
1203 }
1204 #endif /* INET */
1205
1206 #ifdef INET6
1207 static int
1208 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1209 {
1210         struct inpcb *inp = tp->t_inpcb, *oinp;
1211         struct socket *so = inp->inp_socket;
1212         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1213         struct in6_addr addr6;
1214         int error;
1215
1216         INP_WLOCK_ASSERT(inp);
1217         INP_HASH_WLOCK(&V_tcbinfo);
1218
1219         if (inp->inp_lport == 0) {
1220                 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1221                 if (error)
1222                         goto out;
1223         }
1224
1225         /*
1226          * Cannot simply call in_pcbconnect, because there might be an
1227          * earlier incarnation of this same connection still in
1228          * TIME_WAIT state, creating an ADDRINUSE error.
1229          * in6_pcbladdr() also handles scope zone IDs.
1230          *
1231          * XXXRW: We wouldn't need to expose in6_pcblookup_hash_locked()
1232          * outside of in6_pcb.c if there were an in6_pcbconnect_setup().
1233          */
1234         error = in6_pcbladdr(inp, nam, &addr6);
1235         if (error)
1236                 goto out;
1237         oinp = in6_pcblookup_hash_locked(inp->inp_pcbinfo,
1238                                   &sin6->sin6_addr, sin6->sin6_port,
1239                                   IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1240                                   ? &addr6
1241                                   : &inp->in6p_laddr,
1242                                   inp->inp_lport,  0, NULL);
1243         if (oinp) {
1244                 error = EADDRINUSE;
1245                 goto out;
1246         }
1247         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1248                 inp->in6p_laddr = addr6;
1249         inp->in6p_faddr = sin6->sin6_addr;
1250         inp->inp_fport = sin6->sin6_port;
1251         /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
1252         inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
1253         if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
1254                 inp->inp_flow |=
1255                     (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1256         in_pcbrehash(inp);
1257         INP_HASH_WUNLOCK(&V_tcbinfo);
1258
1259         /* Compute window scaling to request.  */
1260         while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1261             (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1262                 tp->request_r_scale++;
1263
1264         soisconnecting(so);
1265         TCPSTAT_INC(tcps_connattempt);
1266         tcp_state_change(tp, TCPS_SYN_SENT);
1267         tp->iss = tcp_new_isn(tp);
1268         tcp_sendseqinit(tp);
1269
1270         return 0;
1271
1272 out:
1273         INP_HASH_WUNLOCK(&V_tcbinfo);
1274         return error;
1275 }
1276 #endif /* INET6 */
1277
1278 /*
1279  * Export TCP internal state information via a struct tcp_info, based on the
1280  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1281  * (TCP state machine, etc).  We export all information using FreeBSD-native
1282  * constants -- for example, the numeric values for tcpi_state will differ
1283  * from Linux.
1284  */
1285 static void
1286 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1287 {
1288
1289         INP_WLOCK_ASSERT(tp->t_inpcb);
1290         bzero(ti, sizeof(*ti));
1291
1292         ti->tcpi_state = tp->t_state;
1293         if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1294                 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1295         if (tp->t_flags & TF_SACK_PERMIT)
1296                 ti->tcpi_options |= TCPI_OPT_SACK;
1297         if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1298                 ti->tcpi_options |= TCPI_OPT_WSCALE;
1299                 ti->tcpi_snd_wscale = tp->snd_scale;
1300                 ti->tcpi_rcv_wscale = tp->rcv_scale;
1301         }
1302
1303         ti->tcpi_rto = tp->t_rxtcur * tick;
1304         ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
1305         ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1306         ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1307
1308         ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1309         ti->tcpi_snd_cwnd = tp->snd_cwnd;
1310
1311         /*
1312          * FreeBSD-specific extension fields for tcp_info.
1313          */
1314         ti->tcpi_rcv_space = tp->rcv_wnd;
1315         ti->tcpi_rcv_nxt = tp->rcv_nxt;
1316         ti->tcpi_snd_wnd = tp->snd_wnd;
1317         ti->tcpi_snd_bwnd = 0;          /* Unused, kept for compat. */
1318         ti->tcpi_snd_nxt = tp->snd_nxt;
1319         ti->tcpi_snd_mss = tp->t_maxseg;
1320         ti->tcpi_rcv_mss = tp->t_maxseg;
1321         if (tp->t_flags & TF_TOE)
1322                 ti->tcpi_options |= TCPI_OPT_TOE;
1323         ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1324         ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1325         ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1326 }
1327
1328 /*
1329  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1330  * socket option arguments.  When it re-acquires the lock after the copy, it
1331  * has to revalidate that the connection is still valid for the socket
1332  * option.
1333  */
1334 #define INP_WLOCK_RECHECK(inp) do {                                     \
1335         INP_WLOCK(inp);                                                 \
1336         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {            \
1337                 INP_WUNLOCK(inp);                                       \
1338                 return (ECONNRESET);                                    \
1339         }                                                               \
1340         tp = intotcpcb(inp);                                            \
1341 } while(0)
1342
1343 int
1344 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1345 {
1346         int     error, opt, optval;
1347         u_int   ui;
1348         struct  inpcb *inp;
1349         struct  tcpcb *tp;
1350         struct  tcp_info ti;
1351         char buf[TCP_CA_NAME_MAX];
1352         struct cc_algo *algo;
1353
1354         error = 0;
1355         inp = sotoinpcb(so);
1356         KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1357         INP_WLOCK(inp);
1358         if (sopt->sopt_level != IPPROTO_TCP) {
1359 #ifdef INET6
1360                 if (inp->inp_vflag & INP_IPV6PROTO) {
1361                         INP_WUNLOCK(inp);
1362                         error = ip6_ctloutput(so, sopt);
1363                 }
1364 #endif /* INET6 */
1365 #if defined(INET6) && defined(INET)
1366                 else
1367 #endif
1368 #ifdef INET
1369                 {
1370                         INP_WUNLOCK(inp);
1371                         error = ip_ctloutput(so, sopt);
1372                 }
1373 #endif
1374                 return (error);
1375         }
1376         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1377                 INP_WUNLOCK(inp);
1378                 return (ECONNRESET);
1379         }
1380
1381         switch (sopt->sopt_dir) {
1382         case SOPT_SET:
1383                 switch (sopt->sopt_name) {
1384 #ifdef TCP_SIGNATURE
1385                 case TCP_MD5SIG:
1386                         INP_WUNLOCK(inp);
1387                         error = sooptcopyin(sopt, &optval, sizeof optval,
1388                             sizeof optval);
1389                         if (error)
1390                                 return (error);
1391
1392                         INP_WLOCK_RECHECK(inp);
1393                         if (optval > 0)
1394                                 tp->t_flags |= TF_SIGNATURE;
1395                         else
1396                                 tp->t_flags &= ~TF_SIGNATURE;
1397                         goto unlock_and_done;
1398 #endif /* TCP_SIGNATURE */
1399
1400                 case TCP_NODELAY:
1401                 case TCP_NOOPT:
1402                         INP_WUNLOCK(inp);
1403                         error = sooptcopyin(sopt, &optval, sizeof optval,
1404                             sizeof optval);
1405                         if (error)
1406                                 return (error);
1407
1408                         INP_WLOCK_RECHECK(inp);
1409                         switch (sopt->sopt_name) {
1410                         case TCP_NODELAY:
1411                                 opt = TF_NODELAY;
1412                                 break;
1413                         case TCP_NOOPT:
1414                                 opt = TF_NOOPT;
1415                                 break;
1416                         default:
1417                                 opt = 0; /* dead code to fool gcc */
1418                                 break;
1419                         }
1420
1421                         if (optval)
1422                                 tp->t_flags |= opt;
1423                         else
1424                                 tp->t_flags &= ~opt;
1425 unlock_and_done:
1426 #ifdef TCP_OFFLOAD
1427                         if (tp->t_flags & TF_TOE) {
1428                                 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1429                                     sopt->sopt_name);
1430                         }
1431 #endif
1432                         INP_WUNLOCK(inp);
1433                         break;
1434
1435                 case TCP_NOPUSH:
1436                         INP_WUNLOCK(inp);
1437                         error = sooptcopyin(sopt, &optval, sizeof optval,
1438                             sizeof optval);
1439                         if (error)
1440                                 return (error);
1441
1442                         INP_WLOCK_RECHECK(inp);
1443                         if (optval)
1444                                 tp->t_flags |= TF_NOPUSH;
1445                         else if (tp->t_flags & TF_NOPUSH) {
1446                                 tp->t_flags &= ~TF_NOPUSH;
1447                                 if (TCPS_HAVEESTABLISHED(tp->t_state))
1448                                         error = tcp_output(tp);
1449                         }
1450                         goto unlock_and_done;
1451
1452                 case TCP_MAXSEG:
1453                         INP_WUNLOCK(inp);
1454                         error = sooptcopyin(sopt, &optval, sizeof optval,
1455                             sizeof optval);
1456                         if (error)
1457                                 return (error);
1458
1459                         INP_WLOCK_RECHECK(inp);
1460                         if (optval > 0 && optval <= tp->t_maxseg &&
1461                             optval + 40 >= V_tcp_minmss)
1462                                 tp->t_maxseg = optval;
1463                         else
1464                                 error = EINVAL;
1465                         goto unlock_and_done;
1466
1467                 case TCP_INFO:
1468                         INP_WUNLOCK(inp);
1469                         error = EINVAL;
1470                         break;
1471
1472                 case TCP_CONGESTION:
1473                         INP_WUNLOCK(inp);
1474                         bzero(buf, sizeof(buf));
1475                         error = sooptcopyin(sopt, &buf, sizeof(buf), 1);
1476                         if (error)
1477                                 break;
1478                         INP_WLOCK_RECHECK(inp);
1479                         /*
1480                          * Return EINVAL if we can't find the requested cc algo.
1481                          */
1482                         error = EINVAL;
1483                         CC_LIST_RLOCK();
1484                         STAILQ_FOREACH(algo, &cc_list, entries) {
1485                                 if (strncmp(buf, algo->name, TCP_CA_NAME_MAX)
1486                                     == 0) {
1487                                         /* We've found the requested algo. */
1488                                         error = 0;
1489                                         /*
1490                                          * We hold a write lock over the tcb
1491                                          * so it's safe to do these things
1492                                          * without ordering concerns.
1493                                          */
1494                                         if (CC_ALGO(tp)->cb_destroy != NULL)
1495                                                 CC_ALGO(tp)->cb_destroy(tp->ccv);
1496                                         CC_ALGO(tp) = algo;
1497                                         /*
1498                                          * If something goes pear shaped
1499                                          * initialising the new algo,
1500                                          * fall back to newreno (which
1501                                          * does not require initialisation).
1502                                          */
1503                                         if (algo->cb_init != NULL)
1504                                                 if (algo->cb_init(tp->ccv) > 0) {
1505                                                         CC_ALGO(tp) = &newreno_cc_algo;
1506                                                         /*
1507                                                          * The only reason init
1508                                                          * should fail is
1509                                                          * because of malloc.
1510                                                          */
1511                                                         error = ENOMEM;
1512                                                 }
1513                                         break; /* Break the STAILQ_FOREACH. */
1514                                 }
1515                         }
1516                         CC_LIST_RUNLOCK();
1517                         goto unlock_and_done;
1518
1519                 case TCP_KEEPIDLE:
1520                 case TCP_KEEPINTVL:
1521                 case TCP_KEEPINIT:
1522                         INP_WUNLOCK(inp);
1523                         error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1524                         if (error)
1525                                 return (error);
1526
1527                         if (ui > (UINT_MAX / hz)) {
1528                                 error = EINVAL;
1529                                 break;
1530                         }
1531                         ui *= hz;
1532
1533                         INP_WLOCK_RECHECK(inp);
1534                         switch (sopt->sopt_name) {
1535                         case TCP_KEEPIDLE:
1536                                 tp->t_keepidle = ui;
1537                                 /*
1538                                  * XXX: better check current remaining
1539                                  * timeout and "merge" it with new value.
1540                                  */
1541                                 if ((tp->t_state > TCPS_LISTEN) &&
1542                                     (tp->t_state <= TCPS_CLOSING))
1543                                         tcp_timer_activate(tp, TT_KEEP,
1544                                             TP_KEEPIDLE(tp));
1545                                 break;
1546                         case TCP_KEEPINTVL:
1547                                 tp->t_keepintvl = ui;
1548                                 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1549                                     (TP_MAXIDLE(tp) > 0))
1550                                         tcp_timer_activate(tp, TT_2MSL,
1551                                             TP_MAXIDLE(tp));
1552                                 break;
1553                         case TCP_KEEPINIT:
1554                                 tp->t_keepinit = ui;
1555                                 if (tp->t_state == TCPS_SYN_RECEIVED ||
1556                                     tp->t_state == TCPS_SYN_SENT)
1557                                         tcp_timer_activate(tp, TT_KEEP,
1558                                             TP_KEEPINIT(tp));
1559                                 break;
1560                         }
1561                         goto unlock_and_done;
1562
1563                 case TCP_KEEPCNT:
1564                         INP_WUNLOCK(inp);
1565                         error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1566                         if (error)
1567                                 return (error);
1568
1569                         INP_WLOCK_RECHECK(inp);
1570                         tp->t_keepcnt = ui;
1571                         if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1572                             (TP_MAXIDLE(tp) > 0))
1573                                 tcp_timer_activate(tp, TT_2MSL,
1574                                     TP_MAXIDLE(tp));
1575                         goto unlock_and_done;
1576
1577 #ifdef TCP_RFC7413
1578                 case TCP_FASTOPEN:
1579                         INP_WUNLOCK(inp);
1580                         if (!V_tcp_fastopen_enabled)
1581                                 return (EPERM);
1582
1583                         error = sooptcopyin(sopt, &optval, sizeof optval,
1584                             sizeof optval);
1585                         if (error)
1586                                 return (error);
1587
1588                         INP_WLOCK_RECHECK(inp);
1589                         if (optval) {
1590                                 tp->t_flags |= TF_FASTOPEN;
1591                                 if ((tp->t_state == TCPS_LISTEN) &&
1592                                     (tp->t_tfo_pending == NULL))
1593                                         tp->t_tfo_pending =
1594                                             tcp_fastopen_alloc_counter();
1595                         } else
1596                                 tp->t_flags &= ~TF_FASTOPEN;
1597                         goto unlock_and_done;
1598 #endif
1599
1600                 default:
1601                         INP_WUNLOCK(inp);
1602                         error = ENOPROTOOPT;
1603                         break;
1604                 }
1605                 break;
1606
1607         case SOPT_GET:
1608                 tp = intotcpcb(inp);
1609                 switch (sopt->sopt_name) {
1610 #ifdef TCP_SIGNATURE
1611                 case TCP_MD5SIG:
1612                         optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1613                         INP_WUNLOCK(inp);
1614                         error = sooptcopyout(sopt, &optval, sizeof optval);
1615                         break;
1616 #endif
1617
1618                 case TCP_NODELAY:
1619                         optval = tp->t_flags & TF_NODELAY;
1620                         INP_WUNLOCK(inp);
1621                         error = sooptcopyout(sopt, &optval, sizeof optval);
1622                         break;
1623                 case TCP_MAXSEG:
1624                         optval = tp->t_maxseg;
1625                         INP_WUNLOCK(inp);
1626                         error = sooptcopyout(sopt, &optval, sizeof optval);
1627                         break;
1628                 case TCP_NOOPT:
1629                         optval = tp->t_flags & TF_NOOPT;
1630                         INP_WUNLOCK(inp);
1631                         error = sooptcopyout(sopt, &optval, sizeof optval);
1632                         break;
1633                 case TCP_NOPUSH:
1634                         optval = tp->t_flags & TF_NOPUSH;
1635                         INP_WUNLOCK(inp);
1636                         error = sooptcopyout(sopt, &optval, sizeof optval);
1637                         break;
1638                 case TCP_INFO:
1639                         tcp_fill_info(tp, &ti);
1640                         INP_WUNLOCK(inp);
1641                         error = sooptcopyout(sopt, &ti, sizeof ti);
1642                         break;
1643                 case TCP_CONGESTION:
1644                         bzero(buf, sizeof(buf));
1645                         strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1646                         INP_WUNLOCK(inp);
1647                         error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX);
1648                         break;
1649                 case TCP_KEEPIDLE:
1650                 case TCP_KEEPINTVL:
1651                 case TCP_KEEPINIT:
1652                 case TCP_KEEPCNT:
1653                         switch (sopt->sopt_name) {
1654                         case TCP_KEEPIDLE:
1655                                 ui = tp->t_keepidle / hz;
1656                                 break;
1657                         case TCP_KEEPINTVL:
1658                                 ui = tp->t_keepintvl / hz;
1659                                 break;
1660                         case TCP_KEEPINIT:
1661                                 ui = tp->t_keepinit / hz;
1662                                 break;
1663                         case TCP_KEEPCNT:
1664                                 ui = tp->t_keepcnt;
1665                                 break;
1666                         }
1667                         INP_WUNLOCK(inp);
1668                         error = sooptcopyout(sopt, &ui, sizeof(ui));
1669                         break;
1670 #ifdef TCP_RFC7413
1671                 case TCP_FASTOPEN:
1672                         optval = tp->t_flags & TF_FASTOPEN;
1673                         INP_WUNLOCK(inp);
1674                         error = sooptcopyout(sopt, &optval, sizeof optval);
1675                         break;
1676 #endif
1677                 default:
1678                         INP_WUNLOCK(inp);
1679                         error = ENOPROTOOPT;
1680                         break;
1681                 }
1682                 break;
1683         }
1684         return (error);
1685 }
1686 #undef INP_WLOCK_RECHECK
1687
1688 /*
1689  * Attach TCP protocol to socket, allocating
1690  * internet protocol control block, tcp control block,
1691  * bufer space, and entering LISTEN state if to accept connections.
1692  */
1693 static int
1694 tcp_attach(struct socket *so)
1695 {
1696         struct tcpcb *tp;
1697         struct inpcb *inp;
1698         int error;
1699
1700         if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1701                 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1702                 if (error)
1703                         return (error);
1704         }
1705         so->so_rcv.sb_flags |= SB_AUTOSIZE;
1706         so->so_snd.sb_flags |= SB_AUTOSIZE;
1707         INP_INFO_WLOCK(&V_tcbinfo);
1708         error = in_pcballoc(so, &V_tcbinfo);
1709         if (error) {
1710                 INP_INFO_WUNLOCK(&V_tcbinfo);
1711                 return (error);
1712         }
1713         inp = sotoinpcb(so);
1714 #ifdef INET6
1715         if (inp->inp_vflag & INP_IPV6PROTO) {
1716                 inp->inp_vflag |= INP_IPV6;
1717                 inp->in6p_hops = -1;    /* use kernel default */
1718         }
1719         else
1720 #endif
1721         inp->inp_vflag |= INP_IPV4;
1722         tp = tcp_newtcpcb(inp);
1723         if (tp == NULL) {
1724                 in_pcbdetach(inp);
1725                 in_pcbfree(inp);
1726                 INP_INFO_WUNLOCK(&V_tcbinfo);
1727                 return (ENOBUFS);
1728         }
1729         tp->t_state = TCPS_CLOSED;
1730         INP_WUNLOCK(inp);
1731         INP_INFO_WUNLOCK(&V_tcbinfo);
1732         return (0);
1733 }
1734
1735 /*
1736  * Initiate (or continue) disconnect.
1737  * If embryonic state, just send reset (once).
1738  * If in ``let data drain'' option and linger null, just drop.
1739  * Otherwise (hard), mark socket disconnecting and drop
1740  * current input data; switch states based on user close, and
1741  * send segment to peer (with FIN).
1742  */
1743 static void
1744 tcp_disconnect(struct tcpcb *tp)
1745 {
1746         struct inpcb *inp = tp->t_inpcb;
1747         struct socket *so = inp->inp_socket;
1748
1749         INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1750         INP_WLOCK_ASSERT(inp);
1751
1752         /*
1753          * Neither tcp_close() nor tcp_drop() should return NULL, as the
1754          * socket is still open.
1755          */
1756         if (tp->t_state < TCPS_ESTABLISHED) {
1757                 tp = tcp_close(tp);
1758                 KASSERT(tp != NULL,
1759                     ("tcp_disconnect: tcp_close() returned NULL"));
1760         } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1761                 tp = tcp_drop(tp, 0);
1762                 KASSERT(tp != NULL,
1763                     ("tcp_disconnect: tcp_drop() returned NULL"));
1764         } else {
1765                 soisdisconnecting(so);
1766                 sbflush(&so->so_rcv);
1767                 tcp_usrclosed(tp);
1768                 if (!(inp->inp_flags & INP_DROPPED))
1769                         tcp_output(tp);
1770         }
1771 }
1772
1773 /*
1774  * User issued close, and wish to trail through shutdown states:
1775  * if never received SYN, just forget it.  If got a SYN from peer,
1776  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1777  * If already got a FIN from peer, then almost done; go to LAST_ACK
1778  * state.  In all other cases, have already sent FIN to peer (e.g.
1779  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1780  * for peer to send FIN or not respond to keep-alives, etc.
1781  * We can let the user exit from the close as soon as the FIN is acked.
1782  */
1783 static void
1784 tcp_usrclosed(struct tcpcb *tp)
1785 {
1786
1787         INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1788         INP_WLOCK_ASSERT(tp->t_inpcb);
1789
1790         switch (tp->t_state) {
1791         case TCPS_LISTEN:
1792 #ifdef TCP_OFFLOAD
1793                 tcp_offload_listen_stop(tp);
1794 #endif
1795                 tcp_state_change(tp, TCPS_CLOSED);
1796                 /* FALLTHROUGH */
1797         case TCPS_CLOSED:
1798                 tp = tcp_close(tp);
1799                 /*
1800                  * tcp_close() should never return NULL here as the socket is
1801                  * still open.
1802                  */
1803                 KASSERT(tp != NULL,
1804                     ("tcp_usrclosed: tcp_close() returned NULL"));
1805                 break;
1806
1807         case TCPS_SYN_SENT:
1808         case TCPS_SYN_RECEIVED:
1809                 tp->t_flags |= TF_NEEDFIN;
1810                 break;
1811
1812         case TCPS_ESTABLISHED:
1813                 tcp_state_change(tp, TCPS_FIN_WAIT_1);
1814                 break;
1815
1816         case TCPS_CLOSE_WAIT:
1817                 tcp_state_change(tp, TCPS_LAST_ACK);
1818                 break;
1819         }
1820         if (tp->t_state >= TCPS_FIN_WAIT_2) {
1821                 soisdisconnected(tp->t_inpcb->inp_socket);
1822                 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
1823                 if (tp->t_state == TCPS_FIN_WAIT_2) {
1824                         int timeout;
1825
1826                         timeout = (tcp_fast_finwait2_recycle) ? 
1827                             tcp_finwait2_timeout : TP_MAXIDLE(tp);
1828                         tcp_timer_activate(tp, TT_2MSL, timeout);
1829                 }
1830         }
1831 }
1832
1833 #ifdef DDB
1834 static void
1835 db_print_indent(int indent)
1836 {
1837         int i;
1838
1839         for (i = 0; i < indent; i++)
1840                 db_printf(" ");
1841 }
1842
1843 static void
1844 db_print_tstate(int t_state)
1845 {
1846
1847         switch (t_state) {
1848         case TCPS_CLOSED:
1849                 db_printf("TCPS_CLOSED");
1850                 return;
1851
1852         case TCPS_LISTEN:
1853                 db_printf("TCPS_LISTEN");
1854                 return;
1855
1856         case TCPS_SYN_SENT:
1857                 db_printf("TCPS_SYN_SENT");
1858                 return;
1859
1860         case TCPS_SYN_RECEIVED:
1861                 db_printf("TCPS_SYN_RECEIVED");
1862                 return;
1863
1864         case TCPS_ESTABLISHED:
1865                 db_printf("TCPS_ESTABLISHED");
1866                 return;
1867
1868         case TCPS_CLOSE_WAIT:
1869                 db_printf("TCPS_CLOSE_WAIT");
1870                 return;
1871
1872         case TCPS_FIN_WAIT_1:
1873                 db_printf("TCPS_FIN_WAIT_1");
1874                 return;
1875
1876         case TCPS_CLOSING:
1877                 db_printf("TCPS_CLOSING");
1878                 return;
1879
1880         case TCPS_LAST_ACK:
1881                 db_printf("TCPS_LAST_ACK");
1882                 return;
1883
1884         case TCPS_FIN_WAIT_2:
1885                 db_printf("TCPS_FIN_WAIT_2");
1886                 return;
1887
1888         case TCPS_TIME_WAIT:
1889                 db_printf("TCPS_TIME_WAIT");
1890                 return;
1891
1892         default:
1893                 db_printf("unknown");
1894                 return;
1895         }
1896 }
1897
1898 static void
1899 db_print_tflags(u_int t_flags)
1900 {
1901         int comma;
1902
1903         comma = 0;
1904         if (t_flags & TF_ACKNOW) {
1905                 db_printf("%sTF_ACKNOW", comma ? ", " : "");
1906                 comma = 1;
1907         }
1908         if (t_flags & TF_DELACK) {
1909                 db_printf("%sTF_DELACK", comma ? ", " : "");
1910                 comma = 1;
1911         }
1912         if (t_flags & TF_NODELAY) {
1913                 db_printf("%sTF_NODELAY", comma ? ", " : "");
1914                 comma = 1;
1915         }
1916         if (t_flags & TF_NOOPT) {
1917                 db_printf("%sTF_NOOPT", comma ? ", " : "");
1918                 comma = 1;
1919         }
1920         if (t_flags & TF_SENTFIN) {
1921                 db_printf("%sTF_SENTFIN", comma ? ", " : "");
1922                 comma = 1;
1923         }
1924         if (t_flags & TF_REQ_SCALE) {
1925                 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1926                 comma = 1;
1927         }
1928         if (t_flags & TF_RCVD_SCALE) {
1929                 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1930                 comma = 1;
1931         }
1932         if (t_flags & TF_REQ_TSTMP) {
1933                 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1934                 comma = 1;
1935         }
1936         if (t_flags & TF_RCVD_TSTMP) {
1937                 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1938                 comma = 1;
1939         }
1940         if (t_flags & TF_SACK_PERMIT) {
1941                 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1942                 comma = 1;
1943         }
1944         if (t_flags & TF_NEEDSYN) {
1945                 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1946                 comma = 1;
1947         }
1948         if (t_flags & TF_NEEDFIN) {
1949                 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1950                 comma = 1;
1951         }
1952         if (t_flags & TF_NOPUSH) {
1953                 db_printf("%sTF_NOPUSH", comma ? ", " : "");
1954                 comma = 1;
1955         }
1956         if (t_flags & TF_MORETOCOME) {
1957                 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1958                 comma = 1;
1959         }
1960         if (t_flags & TF_LQ_OVERFLOW) {
1961                 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1962                 comma = 1;
1963         }
1964         if (t_flags & TF_LASTIDLE) {
1965                 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1966                 comma = 1;
1967         }
1968         if (t_flags & TF_RXWIN0SENT) {
1969                 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1970                 comma = 1;
1971         }
1972         if (t_flags & TF_FASTRECOVERY) {
1973                 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1974                 comma = 1;
1975         }
1976         if (t_flags & TF_CONGRECOVERY) {
1977                 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
1978                 comma = 1;
1979         }
1980         if (t_flags & TF_WASFRECOVERY) {
1981                 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1982                 comma = 1;
1983         }
1984         if (t_flags & TF_SIGNATURE) {
1985                 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1986                 comma = 1;
1987         }
1988         if (t_flags & TF_FORCEDATA) {
1989                 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1990                 comma = 1;
1991         }
1992         if (t_flags & TF_TSO) {
1993                 db_printf("%sTF_TSO", comma ? ", " : "");
1994                 comma = 1;
1995         }
1996         if (t_flags & TF_ECN_PERMIT) {
1997                 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
1998                 comma = 1;
1999         }
2000         if (t_flags & TF_FASTOPEN) {
2001                 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2002                 comma = 1;
2003         }
2004 }
2005
2006 static void
2007 db_print_toobflags(char t_oobflags)
2008 {
2009         int comma;
2010
2011         comma = 0;
2012         if (t_oobflags & TCPOOB_HAVEDATA) {
2013                 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2014                 comma = 1;
2015         }
2016         if (t_oobflags & TCPOOB_HADDATA) {
2017                 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2018                 comma = 1;
2019         }
2020 }
2021
2022 static void
2023 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2024 {
2025
2026         db_print_indent(indent);
2027         db_printf("%s at %p\n", name, tp);
2028
2029         indent += 2;
2030
2031         db_print_indent(indent);
2032         db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
2033            LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2034
2035         db_print_indent(indent);
2036         db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
2037             &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2038
2039         db_print_indent(indent);
2040         db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2041             &tp->t_timers->tt_delack, tp->t_inpcb);
2042
2043         db_print_indent(indent);
2044         db_printf("t_state: %d (", tp->t_state);
2045         db_print_tstate(tp->t_state);
2046         db_printf(")\n");
2047
2048         db_print_indent(indent);
2049         db_printf("t_flags: 0x%x (", tp->t_flags);
2050         db_print_tflags(tp->t_flags);
2051         db_printf(")\n");
2052
2053         db_print_indent(indent);
2054         db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
2055             tp->snd_una, tp->snd_max, tp->snd_nxt);
2056
2057         db_print_indent(indent);
2058         db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
2059            tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2060
2061         db_print_indent(indent);
2062         db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
2063             tp->iss, tp->irs, tp->rcv_nxt);
2064
2065         db_print_indent(indent);
2066         db_printf("rcv_adv: 0x%08x   rcv_wnd: %lu   rcv_up: 0x%08x\n",
2067             tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2068
2069         db_print_indent(indent);
2070         db_printf("snd_wnd: %lu   snd_cwnd: %lu\n",
2071            tp->snd_wnd, tp->snd_cwnd);
2072
2073         db_print_indent(indent);
2074         db_printf("snd_ssthresh: %lu   snd_recover: "
2075             "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2076
2077         db_print_indent(indent);
2078         db_printf("t_maxopd: %u   t_rcvtime: %u   t_startime: %u\n",
2079             tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
2080
2081         db_print_indent(indent);
2082         db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
2083             tp->t_rtttime, tp->t_rtseq);
2084
2085         db_print_indent(indent);
2086         db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
2087             tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2088
2089         db_print_indent(indent);
2090         db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
2091             "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2092             tp->t_rttbest);
2093
2094         db_print_indent(indent);
2095         db_printf("t_rttupdated: %lu   max_sndwnd: %lu   t_softerror: %d\n",
2096             tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2097
2098         db_print_indent(indent);
2099         db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2100         db_print_toobflags(tp->t_oobflags);
2101         db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
2102
2103         db_print_indent(indent);
2104         db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
2105             tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2106
2107         db_print_indent(indent);
2108         db_printf("ts_recent: %u   ts_recent_age: %u\n",
2109             tp->ts_recent, tp->ts_recent_age);
2110
2111         db_print_indent(indent);
2112         db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
2113             "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2114
2115         db_print_indent(indent);
2116         db_printf("snd_ssthresh_prev: %lu   snd_recover_prev: 0x%08x   "
2117             "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2118             tp->snd_recover_prev, tp->t_badrxtwin);
2119
2120         db_print_indent(indent);
2121         db_printf("snd_numholes: %d  snd_holes first: %p\n",
2122             tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2123
2124         db_print_indent(indent);
2125         db_printf("snd_fack: 0x%08x   rcv_numsacks: %d   sack_newdata: "
2126             "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2127
2128         /* Skip sackblks, sackhint. */
2129
2130         db_print_indent(indent);
2131         db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
2132             tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2133 }
2134
2135 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2136 {
2137         struct tcpcb *tp;
2138
2139         if (!have_addr) {
2140                 db_printf("usage: show tcpcb <addr>\n");
2141                 return;
2142         }
2143         tp = (struct tcpcb *)addr;
2144
2145         db_print_tcpcb(tp, "tcpcb", 0);
2146 }
2147 #endif