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.
8 * Portions of this software were developed by Robert N. M. Watson under
9 * contract to Juniper Networks, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
35 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
43 #include "opt_inet6.h"
44 #include "opt_tcpdebug.h"
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>
54 #include <sys/domain.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/protosw.h>
61 #include <sys/syslog.h>
68 #include <net/route.h>
71 #include <netinet/cc.h>
72 #include <netinet/in.h>
73 #include <netinet/in_pcb.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/in_var.h>
76 #include <netinet/ip_var.h>
78 #include <netinet/ip6.h>
79 #include <netinet6/in6_pcb.h>
80 #include <netinet6/ip6_var.h>
81 #include <netinet6/scope6_var.h>
84 #include <netinet/tcp_fastopen.h>
86 #include <netinet/tcp_fsm.h>
87 #include <netinet/tcp_seq.h>
88 #include <netinet/tcp_timer.h>
89 #include <netinet/tcp_var.h>
90 #include <netinet/tcpip.h>
92 #include <netinet/tcp_debug.h>
95 #include <netinet/tcp_offload.h>
99 * TCP protocol interface to socket abstraction.
101 static int tcp_attach(struct socket *);
103 static int tcp_connect(struct tcpcb *, struct sockaddr *,
107 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
110 static void tcp_disconnect(struct tcpcb *);
111 static void tcp_usrclosed(struct tcpcb *);
112 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
115 #define TCPDEBUG0 int ostate = 0
116 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
117 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
118 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
122 #define TCPDEBUG2(req)
126 * TCP attaches to socket via pru_attach(), reserving space,
127 * and an internet control block.
130 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
133 struct tcpcb *tp = NULL;
138 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
141 error = tcp_attach(so);
145 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
146 so->so_linger = TCP_LINGERTIME;
151 TCPDEBUG2(PRU_ATTACH);
156 * tcp_detach is called when the socket layer loses its final reference
157 * to the socket, be it a file descriptor reference, a reference from TCP,
158 * etc. At this point, there is only one case in which we will keep around
159 * inpcb state: time wait.
161 * This function can probably be re-absorbed back into tcp_usr_detach() now
162 * that there is a single detach path.
165 tcp_detach(struct socket *so, struct inpcb *inp)
169 INP_INFO_LOCK_ASSERT(&V_tcbinfo);
170 INP_WLOCK_ASSERT(inp);
172 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
173 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
177 if (inp->inp_flags & INP_TIMEWAIT) {
179 * There are two cases to handle: one in which the time wait
180 * state is being discarded (INP_DROPPED), and one in which
181 * this connection will remain in timewait. In the former,
182 * it is time to discard all state (except tcptw, which has
183 * already been discarded by the timewait close code, which
184 * should be further up the call stack somewhere). In the
185 * latter case, we detach from the socket, but leave the pcb
186 * present until timewait ends.
188 * XXXRW: Would it be cleaner to free the tcptw here?
190 * Astute question indeed, from twtcp perspective there are
191 * three cases to consider:
193 * #1 tcp_detach is called at tcptw creation time by
194 * tcp_twstart, then do not discard the newly created tcptw
195 * and leave inpcb present until timewait ends
196 * #2 tcp_detach is called at timewait end (or reuse) by
197 * tcp_twclose, then the tcptw has already been discarded
198 * (or reused) and inpcb is freed here
199 * #3 tcp_detach is called() after timewait ends (or reuse)
200 * (e.g. by soclose), then tcptw has already been discarded
201 * (or reused) and inpcb is freed here
203 * In all three cases the tcptw should not be freed here.
205 if (inp->inp_flags & INP_DROPPED) {
207 if (__predict_true(tp == NULL)) {
211 * This case should not happen as in TIMEWAIT
212 * state the inp should not be destroyed before
213 * its tcptw. If INVARIANTS is defined, panic.
216 panic("%s: Panic before an inp double-free: "
217 "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
220 log(LOG_ERR, "%s: Avoid an inp double-free: "
221 "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
232 * If the connection is not in timewait, we consider two
233 * two conditions: one in which no further processing is
234 * necessary (dropped || embryonic), and one in which TCP is
235 * not yet done, but no longer requires the socket, so the
236 * pcb will persist for the time being.
238 * XXXRW: Does the second case still occur?
240 if (inp->inp_flags & INP_DROPPED ||
241 tp->t_state < TCPS_SYN_SENT) {
253 * pru_detach() detaches the TCP protocol from the socket.
254 * If the protocol state is non-embryonic, then can't
255 * do this directly: have to initiate a pru_disconnect(),
256 * which may finish later; embryonic TCB's can just
260 tcp_usr_detach(struct socket *so)
266 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
267 if (!INP_INFO_WLOCKED(&V_tcbinfo)) {
268 INP_INFO_RLOCK(&V_tcbinfo);
272 KASSERT(inp->inp_socket != NULL,
273 ("tcp_usr_detach: inp_socket == NULL"));
276 INP_INFO_RUNLOCK(&V_tcbinfo);
281 * Give the socket an address.
284 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
288 struct tcpcb *tp = NULL;
289 struct sockaddr_in *sinp;
291 sinp = (struct sockaddr_in *)nam;
292 if (nam->sa_len != sizeof (*sinp))
295 * Must check for multicast addresses and disallow binding
298 if (sinp->sin_family == AF_INET &&
299 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
300 return (EAFNOSUPPORT);
304 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
306 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
312 INP_HASH_WLOCK(&V_tcbinfo);
313 error = in_pcbbind(inp, nam, td->td_ucred);
314 INP_HASH_WUNLOCK(&V_tcbinfo);
325 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
329 struct tcpcb *tp = NULL;
330 struct sockaddr_in6 *sin6p;
332 sin6p = (struct sockaddr_in6 *)nam;
333 if (nam->sa_len != sizeof (*sin6p))
336 * Must check for multicast addresses and disallow binding
339 if (sin6p->sin6_family == AF_INET6 &&
340 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
341 return (EAFNOSUPPORT);
345 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
347 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
353 INP_HASH_WLOCK(&V_tcbinfo);
354 inp->inp_vflag &= ~INP_IPV4;
355 inp->inp_vflag |= INP_IPV6;
357 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
358 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
359 inp->inp_vflag |= INP_IPV4;
360 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
361 struct sockaddr_in sin;
363 in6_sin6_2_sin(&sin, sin6p);
364 inp->inp_vflag |= INP_IPV4;
365 inp->inp_vflag &= ~INP_IPV6;
366 error = in_pcbbind(inp, (struct sockaddr *)&sin,
368 INP_HASH_WUNLOCK(&V_tcbinfo);
373 error = in6_pcbbind(inp, nam, td->td_ucred);
374 INP_HASH_WUNLOCK(&V_tcbinfo);
384 * Prepare to accept connections.
387 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
391 struct tcpcb *tp = NULL;
395 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
397 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
404 error = solisten_proto_check(so);
405 INP_HASH_WLOCK(&V_tcbinfo);
406 if (error == 0 && inp->inp_lport == 0)
407 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
408 INP_HASH_WUNLOCK(&V_tcbinfo);
410 tcp_state_change(tp, TCPS_LISTEN);
411 solisten_proto(so, backlog);
413 if ((so->so_options & SO_NO_OFFLOAD) == 0)
414 tcp_offload_listen_start(tp);
420 if (tp->t_flags & TF_FASTOPEN)
421 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
424 TCPDEBUG2(PRU_LISTEN);
432 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
436 struct tcpcb *tp = NULL;
440 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
442 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
449 error = solisten_proto_check(so);
450 INP_HASH_WLOCK(&V_tcbinfo);
451 if (error == 0 && inp->inp_lport == 0) {
452 inp->inp_vflag &= ~INP_IPV4;
453 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
454 inp->inp_vflag |= INP_IPV4;
455 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
457 INP_HASH_WUNLOCK(&V_tcbinfo);
459 tcp_state_change(tp, TCPS_LISTEN);
460 solisten_proto(so, backlog);
462 if ((so->so_options & SO_NO_OFFLOAD) == 0)
463 tcp_offload_listen_start(tp);
469 if (tp->t_flags & TF_FASTOPEN)
470 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
473 TCPDEBUG2(PRU_LISTEN);
481 * Initiate connection to peer.
482 * Create a template for use in transmissions on this connection.
483 * Enter SYN_SENT state, and mark socket as connecting.
484 * Start keep-alive timer, and seed output sequence space.
485 * Send initial segment on connection.
488 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
492 struct tcpcb *tp = NULL;
493 struct sockaddr_in *sinp;
495 sinp = (struct sockaddr_in *)nam;
496 if (nam->sa_len != sizeof (*sinp))
499 * Must disallow TCP ``connections'' to multicast addresses.
501 if (sinp->sin_family == AF_INET
502 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
503 return (EAFNOSUPPORT);
504 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
509 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
511 if (inp->inp_flags & INP_TIMEWAIT) {
515 if (inp->inp_flags & INP_DROPPED) {
516 error = ECONNREFUSED;
521 if ((error = tcp_connect(tp, nam, td)) != 0)
524 if (registered_toedevs > 0 &&
525 (so->so_options & SO_NO_OFFLOAD) == 0 &&
526 (error = tcp_offload_connect(so, nam)) == 0)
529 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
530 error = tcp_output(tp);
532 TCPDEBUG2(PRU_CONNECT);
540 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
544 struct tcpcb *tp = NULL;
545 struct sockaddr_in6 *sin6p;
549 sin6p = (struct sockaddr_in6 *)nam;
550 if (nam->sa_len != sizeof (*sin6p))
553 * Must disallow TCP ``connections'' to multicast addresses.
555 if (sin6p->sin6_family == AF_INET6
556 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
557 return (EAFNOSUPPORT);
560 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
562 if (inp->inp_flags & INP_TIMEWAIT) {
566 if (inp->inp_flags & INP_DROPPED) {
567 error = ECONNREFUSED;
574 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
575 * therefore probably require the hash lock, which isn't held here.
576 * Is this a significant problem?
578 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
579 struct sockaddr_in sin;
581 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
586 in6_sin6_2_sin(&sin, sin6p);
587 inp->inp_vflag |= INP_IPV4;
588 inp->inp_vflag &= ~INP_IPV6;
589 if ((error = prison_remote_ip4(td->td_ucred,
590 &sin.sin_addr)) != 0)
592 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
595 if (registered_toedevs > 0 &&
596 (so->so_options & SO_NO_OFFLOAD) == 0 &&
597 (error = tcp_offload_connect(so, nam)) == 0)
600 error = tcp_output(tp);
604 inp->inp_vflag &= ~INP_IPV4;
605 inp->inp_vflag |= INP_IPV6;
606 inp->inp_inc.inc_flags |= INC_ISIPV6;
607 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
609 if ((error = tcp6_connect(tp, nam, td)) != 0)
612 if (registered_toedevs > 0 &&
613 (so->so_options & SO_NO_OFFLOAD) == 0 &&
614 (error = tcp_offload_connect(so, nam)) == 0)
617 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
618 error = tcp_output(tp);
621 TCPDEBUG2(PRU_CONNECT);
628 * Initiate disconnect from peer.
629 * If connection never passed embryonic stage, just drop;
630 * else if don't need to let data drain, then can just drop anyways,
631 * else have to begin TCP shutdown process: mark socket disconnecting,
632 * drain unread data, state switch to reflect user close, and
633 * send segment (e.g. FIN) to peer. Socket will be really disconnected
634 * when peer sends FIN and acks ours.
636 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
639 tcp_usr_disconnect(struct socket *so)
642 struct tcpcb *tp = NULL;
646 INP_INFO_RLOCK(&V_tcbinfo);
648 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
650 if (inp->inp_flags & INP_TIMEWAIT)
652 if (inp->inp_flags & INP_DROPPED) {
660 TCPDEBUG2(PRU_DISCONNECT);
662 INP_INFO_RUNLOCK(&V_tcbinfo);
668 * Accept a connection. Essentially all the work is done at higher levels;
669 * just return the address of the peer, storing through addr.
672 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
675 struct inpcb *inp = NULL;
676 struct tcpcb *tp = NULL;
681 if (so->so_state & SS_ISDISCONNECTED)
682 return (ECONNABORTED);
685 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
687 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
688 error = ECONNABORTED;
695 * We inline in_getpeeraddr and COMMON_END here, so that we can
696 * copy the data of interest and defer the malloc until after we
699 port = inp->inp_fport;
700 addr = inp->inp_faddr;
703 TCPDEBUG2(PRU_ACCEPT);
706 *nam = in_sockaddr(port, &addr);
713 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
715 struct inpcb *inp = NULL;
717 struct tcpcb *tp = NULL;
719 struct in6_addr addr6;
724 if (so->so_state & SS_ISDISCONNECTED)
725 return (ECONNABORTED);
728 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
729 INP_INFO_RLOCK(&V_tcbinfo);
731 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
732 error = ECONNABORTED;
739 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
740 * copy the data of interest and defer the malloc until after we
743 if (inp->inp_vflag & INP_IPV4) {
745 port = inp->inp_fport;
746 addr = inp->inp_faddr;
748 port = inp->inp_fport;
749 addr6 = inp->in6p_faddr;
753 TCPDEBUG2(PRU_ACCEPT);
755 INP_INFO_RUNLOCK(&V_tcbinfo);
758 *nam = in6_v4mapsin6_sockaddr(port, &addr);
760 *nam = in6_sockaddr(port, &addr6);
767 * Mark the connection as being incapable of further output.
770 tcp_usr_shutdown(struct socket *so)
774 struct tcpcb *tp = NULL;
777 INP_INFO_RLOCK(&V_tcbinfo);
779 KASSERT(inp != NULL, ("inp == NULL"));
781 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
789 if (!(inp->inp_flags & INP_DROPPED))
790 error = tcp_output(tp);
793 TCPDEBUG2(PRU_SHUTDOWN);
795 INP_INFO_RUNLOCK(&V_tcbinfo);
801 * After a receive, possibly send window update to peer.
804 tcp_usr_rcvd(struct socket *so, int flags)
807 struct tcpcb *tp = NULL;
812 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
814 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
822 * For passively-created TFO connections, don't attempt a window
823 * update while still in SYN_RECEIVED as this may trigger an early
824 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
825 * application response data, or failing that, when the DELACK timer
828 if ((tp->t_flags & TF_FASTOPEN) &&
829 (tp->t_state == TCPS_SYN_RECEIVED))
833 if (tp->t_flags & TF_TOE)
834 tcp_offload_rcvd(tp);
846 * Do a send by putting data in output queue and updating urgent
847 * marker if URG set. Possibly send more data. Unlike the other
848 * pru_*() routines, the mbuf chains are our responsibility. We
849 * must either enqueue them or free them. The other pru_* routines
850 * generally are caller-frees.
853 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
854 struct sockaddr *nam, struct mbuf *control, struct thread *td)
858 struct tcpcb *tp = NULL;
865 * We require the pcbinfo lock if we will close the socket as part of
868 if (flags & PRUS_EOF)
869 INP_INFO_RLOCK(&V_tcbinfo);
871 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
873 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
882 isipv6 = nam && nam->sa_family == AF_INET6;
887 /* TCP doesn't do control messages (rights, creds, etc) */
888 if (control->m_len) {
895 m_freem(control); /* empty control, just free it */
897 if (!(flags & PRUS_OOB)) {
898 sbappendstream(&so->so_snd, m);
899 if (nam && tp->t_state < TCPS_SYN_SENT) {
901 * Do implied connect if not yet connected,
902 * initialize window to default value, and
903 * initialize maxseg/maxopd using peer's cached
908 error = tcp6_connect(tp, nam, td);
910 #if defined(INET6) && defined(INET)
914 error = tcp_connect(tp, nam, td);
918 tp->snd_wnd = TTCP_CLIENT_SND_WND;
921 if (flags & PRUS_EOF) {
923 * Close the send side of the connection after
926 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
930 if (!(inp->inp_flags & INP_DROPPED)) {
931 if (flags & PRUS_MORETOCOME)
932 tp->t_flags |= TF_MORETOCOME;
933 error = tcp_output(tp);
934 if (flags & PRUS_MORETOCOME)
935 tp->t_flags &= ~TF_MORETOCOME;
939 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
941 SOCKBUF_LOCK(&so->so_snd);
942 if (sbspace(&so->so_snd) < -512) {
943 SOCKBUF_UNLOCK(&so->so_snd);
949 * According to RFC961 (Assigned Protocols),
950 * the urgent pointer points to the last octet
951 * of urgent data. We continue, however,
952 * to consider it to indicate the first octet
953 * of data past the urgent section.
954 * Otherwise, snd_up should be one lower.
956 sbappendstream_locked(&so->so_snd, m);
957 SOCKBUF_UNLOCK(&so->so_snd);
958 if (nam && tp->t_state < TCPS_SYN_SENT) {
960 * Do implied connect if not yet connected,
961 * initialize window to default value, and
962 * initialize maxseg/maxopd using peer's cached
967 error = tcp6_connect(tp, nam, td);
969 #if defined(INET6) && defined(INET)
973 error = tcp_connect(tp, nam, td);
977 tp->snd_wnd = TTCP_CLIENT_SND_WND;
980 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
981 tp->t_flags |= TF_FORCEDATA;
982 error = tcp_output(tp);
983 tp->t_flags &= ~TF_FORCEDATA;
986 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
987 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
989 if (flags & PRUS_EOF)
990 INP_INFO_RUNLOCK(&V_tcbinfo);
995 * Abort the TCP. Drop the connection abruptly.
998 tcp_usr_abort(struct socket *so)
1001 struct tcpcb *tp = NULL;
1004 inp = sotoinpcb(so);
1005 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1007 INP_INFO_RLOCK(&V_tcbinfo);
1009 KASSERT(inp->inp_socket != NULL,
1010 ("tcp_usr_abort: inp_socket == NULL"));
1013 * If we still have full TCP state, and we're not dropped, drop.
1015 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1016 !(inp->inp_flags & INP_DROPPED)) {
1017 tp = intotcpcb(inp);
1019 tcp_drop(tp, ECONNABORTED);
1020 TCPDEBUG2(PRU_ABORT);
1022 if (!(inp->inp_flags & INP_DROPPED)) {
1024 so->so_state |= SS_PROTOREF;
1026 inp->inp_flags |= INP_SOCKREF;
1029 INP_INFO_RUNLOCK(&V_tcbinfo);
1033 * TCP socket is closed. Start friendly disconnect.
1036 tcp_usr_close(struct socket *so)
1039 struct tcpcb *tp = NULL;
1042 inp = sotoinpcb(so);
1043 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1045 INP_INFO_RLOCK(&V_tcbinfo);
1047 KASSERT(inp->inp_socket != NULL,
1048 ("tcp_usr_close: inp_socket == NULL"));
1051 * If we still have full TCP state, and we're not dropped, initiate
1054 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1055 !(inp->inp_flags & INP_DROPPED)) {
1056 tp = intotcpcb(inp);
1059 TCPDEBUG2(PRU_CLOSE);
1061 if (!(inp->inp_flags & INP_DROPPED)) {
1063 so->so_state |= SS_PROTOREF;
1065 inp->inp_flags |= INP_SOCKREF;
1068 INP_INFO_RUNLOCK(&V_tcbinfo);
1072 * Receive out-of-band data.
1075 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1079 struct tcpcb *tp = NULL;
1082 inp = sotoinpcb(so);
1083 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1085 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1089 tp = intotcpcb(inp);
1091 if ((so->so_oobmark == 0 &&
1092 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1093 so->so_options & SO_OOBINLINE ||
1094 tp->t_oobflags & TCPOOB_HADDATA) {
1098 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1099 error = EWOULDBLOCK;
1103 *mtod(m, caddr_t) = tp->t_iobc;
1104 if ((flags & MSG_PEEK) == 0)
1105 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1108 TCPDEBUG2(PRU_RCVOOB);
1114 struct pr_usrreqs tcp_usrreqs = {
1115 .pru_abort = tcp_usr_abort,
1116 .pru_accept = tcp_usr_accept,
1117 .pru_attach = tcp_usr_attach,
1118 .pru_bind = tcp_usr_bind,
1119 .pru_connect = tcp_usr_connect,
1120 .pru_control = in_control,
1121 .pru_detach = tcp_usr_detach,
1122 .pru_disconnect = tcp_usr_disconnect,
1123 .pru_listen = tcp_usr_listen,
1124 .pru_peeraddr = in_getpeeraddr,
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 = in_getsockaddr,
1130 .pru_sosetlabel = in_pcbsosetlabel,
1131 .pru_close = tcp_usr_close,
1136 struct pr_usrreqs tcp6_usrreqs = {
1137 .pru_abort = tcp_usr_abort,
1138 .pru_accept = tcp6_usr_accept,
1139 .pru_attach = tcp_usr_attach,
1140 .pru_bind = tcp6_usr_bind,
1141 .pru_connect = tcp6_usr_connect,
1142 .pru_control = in6_control,
1143 .pru_detach = tcp_usr_detach,
1144 .pru_disconnect = tcp_usr_disconnect,
1145 .pru_listen = tcp6_usr_listen,
1146 .pru_peeraddr = in6_mapped_peeraddr,
1147 .pru_rcvd = tcp_usr_rcvd,
1148 .pru_rcvoob = tcp_usr_rcvoob,
1149 .pru_send = tcp_usr_send,
1150 .pru_shutdown = tcp_usr_shutdown,
1151 .pru_sockaddr = in6_mapped_sockaddr,
1152 .pru_sosetlabel = in_pcbsosetlabel,
1153 .pru_close = tcp_usr_close,
1159 * Common subroutine to open a TCP connection to remote host specified
1160 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1161 * port number if needed. Call in_pcbconnect_setup to do the routing and
1162 * to choose a local host address (interface). If there is an existing
1163 * incarnation of the same connection in TIME-WAIT state and if the remote
1164 * host was sending CC options and if the connection duration was < MSL, then
1165 * truncate the previous TIME-WAIT state and proceed.
1166 * Initialize connection parameters and enter SYN-SENT state.
1169 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1171 struct inpcb *inp = tp->t_inpcb, *oinp;
1172 struct socket *so = inp->inp_socket;
1173 struct in_addr laddr;
1177 INP_WLOCK_ASSERT(inp);
1178 INP_HASH_WLOCK(&V_tcbinfo);
1180 if (inp->inp_lport == 0) {
1181 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1187 * Cannot simply call in_pcbconnect, because there might be an
1188 * earlier incarnation of this same connection still in
1189 * TIME_WAIT state, creating an ADDRINUSE error.
1191 laddr = inp->inp_laddr;
1192 lport = inp->inp_lport;
1193 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1194 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1195 if (error && oinp == NULL)
1201 inp->inp_laddr = laddr;
1203 INP_HASH_WUNLOCK(&V_tcbinfo);
1206 * Compute window scaling to request:
1207 * Scale to fit into sweet spot. See tcp_syncache.c.
1208 * XXX: This should move to tcp_output().
1210 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1211 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1212 tp->request_r_scale++;
1215 TCPSTAT_INC(tcps_connattempt);
1216 tcp_state_change(tp, TCPS_SYN_SENT);
1217 tp->iss = tcp_new_isn(tp);
1218 tcp_sendseqinit(tp);
1223 INP_HASH_WUNLOCK(&V_tcbinfo);
1230 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1232 struct inpcb *inp = tp->t_inpcb, *oinp;
1233 struct socket *so = inp->inp_socket;
1234 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1235 struct in6_addr addr6;
1238 INP_WLOCK_ASSERT(inp);
1239 INP_HASH_WLOCK(&V_tcbinfo);
1241 if (inp->inp_lport == 0) {
1242 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1248 * Cannot simply call in_pcbconnect, because there might be an
1249 * earlier incarnation of this same connection still in
1250 * TIME_WAIT state, creating an ADDRINUSE error.
1251 * in6_pcbladdr() also handles scope zone IDs.
1253 * XXXRW: We wouldn't need to expose in6_pcblookup_hash_locked()
1254 * outside of in6_pcb.c if there were an in6_pcbconnect_setup().
1256 error = in6_pcbladdr(inp, nam, &addr6);
1259 oinp = in6_pcblookup_hash_locked(inp->inp_pcbinfo,
1260 &sin6->sin6_addr, sin6->sin6_port,
1261 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1264 inp->inp_lport, 0, NULL);
1269 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1270 inp->in6p_laddr = addr6;
1271 inp->in6p_faddr = sin6->sin6_addr;
1272 inp->inp_fport = sin6->sin6_port;
1273 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
1274 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
1275 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
1277 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1279 INP_HASH_WUNLOCK(&V_tcbinfo);
1281 /* Compute window scaling to request. */
1282 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1283 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1284 tp->request_r_scale++;
1287 TCPSTAT_INC(tcps_connattempt);
1288 tcp_state_change(tp, TCPS_SYN_SENT);
1289 tp->iss = tcp_new_isn(tp);
1290 tcp_sendseqinit(tp);
1295 INP_HASH_WUNLOCK(&V_tcbinfo);
1301 * Export TCP internal state information via a struct tcp_info, based on the
1302 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1303 * (TCP state machine, etc). We export all information using FreeBSD-native
1304 * constants -- for example, the numeric values for tcpi_state will differ
1308 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1311 INP_WLOCK_ASSERT(tp->t_inpcb);
1312 bzero(ti, sizeof(*ti));
1314 ti->tcpi_state = tp->t_state;
1315 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1316 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1317 if (tp->t_flags & TF_SACK_PERMIT)
1318 ti->tcpi_options |= TCPI_OPT_SACK;
1319 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1320 ti->tcpi_options |= TCPI_OPT_WSCALE;
1321 ti->tcpi_snd_wscale = tp->snd_scale;
1322 ti->tcpi_rcv_wscale = tp->rcv_scale;
1325 ti->tcpi_rto = tp->t_rxtcur * tick;
1326 ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
1327 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1328 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1330 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1331 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1334 * FreeBSD-specific extension fields for tcp_info.
1336 ti->tcpi_rcv_space = tp->rcv_wnd;
1337 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1338 ti->tcpi_snd_wnd = tp->snd_wnd;
1339 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1340 ti->tcpi_snd_nxt = tp->snd_nxt;
1341 ti->tcpi_snd_mss = tp->t_maxseg;
1342 ti->tcpi_rcv_mss = tp->t_maxseg;
1343 if (tp->t_flags & TF_TOE)
1344 ti->tcpi_options |= TCPI_OPT_TOE;
1345 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1346 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1347 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1351 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1352 * socket option arguments. When it re-acquires the lock after the copy, it
1353 * has to revalidate that the connection is still valid for the socket
1356 #define INP_WLOCK_RECHECK(inp) do { \
1358 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1360 return (ECONNRESET); \
1362 tp = intotcpcb(inp); \
1366 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1368 int error, opt, optval;
1373 char buf[TCP_CA_NAME_MAX];
1374 struct cc_algo *algo;
1377 inp = sotoinpcb(so);
1378 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1380 if (sopt->sopt_level != IPPROTO_TCP) {
1382 if (inp->inp_vflag & INP_IPV6PROTO) {
1384 error = ip6_ctloutput(so, sopt);
1387 #if defined(INET6) && defined(INET)
1393 error = ip_ctloutput(so, sopt);
1398 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1400 return (ECONNRESET);
1403 switch (sopt->sopt_dir) {
1405 switch (sopt->sopt_name) {
1406 #ifdef TCP_SIGNATURE
1409 error = sooptcopyin(sopt, &optval, sizeof optval,
1414 INP_WLOCK_RECHECK(inp);
1416 tp->t_flags |= TF_SIGNATURE;
1418 tp->t_flags &= ~TF_SIGNATURE;
1419 goto unlock_and_done;
1420 #endif /* TCP_SIGNATURE */
1425 error = sooptcopyin(sopt, &optval, sizeof optval,
1430 INP_WLOCK_RECHECK(inp);
1431 switch (sopt->sopt_name) {
1439 opt = 0; /* dead code to fool gcc */
1446 tp->t_flags &= ~opt;
1449 if (tp->t_flags & TF_TOE) {
1450 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1459 error = sooptcopyin(sopt, &optval, sizeof optval,
1464 INP_WLOCK_RECHECK(inp);
1466 tp->t_flags |= TF_NOPUSH;
1467 else if (tp->t_flags & TF_NOPUSH) {
1468 tp->t_flags &= ~TF_NOPUSH;
1469 if (TCPS_HAVEESTABLISHED(tp->t_state))
1470 error = tcp_output(tp);
1472 goto unlock_and_done;
1476 error = sooptcopyin(sopt, &optval, sizeof optval,
1481 INP_WLOCK_RECHECK(inp);
1482 if (optval > 0 && optval <= tp->t_maxseg &&
1483 optval + 40 >= V_tcp_minmss)
1484 tp->t_maxseg = optval;
1487 goto unlock_and_done;
1494 case TCP_CONGESTION:
1496 bzero(buf, sizeof(buf));
1497 error = sooptcopyin(sopt, &buf, sizeof(buf), 1);
1500 INP_WLOCK_RECHECK(inp);
1502 * Return EINVAL if we can't find the requested cc algo.
1506 STAILQ_FOREACH(algo, &cc_list, entries) {
1507 if (strncmp(buf, algo->name, TCP_CA_NAME_MAX)
1509 /* We've found the requested algo. */
1512 * We hold a write lock over the tcb
1513 * so it's safe to do these things
1514 * without ordering concerns.
1516 if (CC_ALGO(tp)->cb_destroy != NULL)
1517 CC_ALGO(tp)->cb_destroy(tp->ccv);
1520 * If something goes pear shaped
1521 * initialising the new algo,
1522 * fall back to newreno (which
1523 * does not require initialisation).
1525 if (algo->cb_init != NULL)
1526 if (algo->cb_init(tp->ccv) > 0) {
1527 CC_ALGO(tp) = &newreno_cc_algo;
1529 * The only reason init
1531 * because of malloc.
1535 break; /* Break the STAILQ_FOREACH. */
1539 goto unlock_and_done;
1545 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1549 if (ui > (UINT_MAX / hz)) {
1555 INP_WLOCK_RECHECK(inp);
1556 switch (sopt->sopt_name) {
1558 tp->t_keepidle = ui;
1560 * XXX: better check current remaining
1561 * timeout and "merge" it with new value.
1563 if ((tp->t_state > TCPS_LISTEN) &&
1564 (tp->t_state <= TCPS_CLOSING))
1565 tcp_timer_activate(tp, TT_KEEP,
1569 tp->t_keepintvl = ui;
1570 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1571 (TP_MAXIDLE(tp) > 0))
1572 tcp_timer_activate(tp, TT_2MSL,
1576 tp->t_keepinit = ui;
1577 if (tp->t_state == TCPS_SYN_RECEIVED ||
1578 tp->t_state == TCPS_SYN_SENT)
1579 tcp_timer_activate(tp, TT_KEEP,
1583 goto unlock_and_done;
1587 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1591 INP_WLOCK_RECHECK(inp);
1593 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1594 (TP_MAXIDLE(tp) > 0))
1595 tcp_timer_activate(tp, TT_2MSL,
1597 goto unlock_and_done;
1602 if (!V_tcp_fastopen_enabled)
1605 error = sooptcopyin(sopt, &optval, sizeof optval,
1610 INP_WLOCK_RECHECK(inp);
1612 tp->t_flags |= TF_FASTOPEN;
1613 if ((tp->t_state == TCPS_LISTEN) &&
1614 (tp->t_tfo_pending == NULL))
1616 tcp_fastopen_alloc_counter();
1618 tp->t_flags &= ~TF_FASTOPEN;
1619 goto unlock_and_done;
1624 error = ENOPROTOOPT;
1630 tp = intotcpcb(inp);
1631 switch (sopt->sopt_name) {
1632 #ifdef TCP_SIGNATURE
1634 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1636 error = sooptcopyout(sopt, &optval, sizeof optval);
1641 optval = tp->t_flags & TF_NODELAY;
1643 error = sooptcopyout(sopt, &optval, sizeof optval);
1646 optval = tp->t_maxseg;
1648 error = sooptcopyout(sopt, &optval, sizeof optval);
1651 optval = tp->t_flags & TF_NOOPT;
1653 error = sooptcopyout(sopt, &optval, sizeof optval);
1656 optval = tp->t_flags & TF_NOPUSH;
1658 error = sooptcopyout(sopt, &optval, sizeof optval);
1661 tcp_fill_info(tp, &ti);
1663 error = sooptcopyout(sopt, &ti, sizeof ti);
1665 case TCP_CONGESTION:
1666 bzero(buf, sizeof(buf));
1667 strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1669 error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX);
1675 switch (sopt->sopt_name) {
1677 ui = tp->t_keepidle / hz;
1680 ui = tp->t_keepintvl / hz;
1683 ui = tp->t_keepinit / hz;
1690 error = sooptcopyout(sopt, &ui, sizeof(ui));
1694 optval = tp->t_flags & TF_FASTOPEN;
1696 error = sooptcopyout(sopt, &optval, sizeof optval);
1701 error = ENOPROTOOPT;
1708 #undef INP_WLOCK_RECHECK
1711 * Attach TCP protocol to socket, allocating
1712 * internet protocol control block, tcp control block,
1713 * bufer space, and entering LISTEN state if to accept connections.
1716 tcp_attach(struct socket *so)
1722 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1723 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1727 so->so_rcv.sb_flags |= SB_AUTOSIZE;
1728 so->so_snd.sb_flags |= SB_AUTOSIZE;
1729 INP_INFO_RLOCK(&V_tcbinfo);
1730 error = in_pcballoc(so, &V_tcbinfo);
1732 INP_INFO_RUNLOCK(&V_tcbinfo);
1735 inp = sotoinpcb(so);
1737 if (inp->inp_vflag & INP_IPV6PROTO) {
1738 inp->inp_vflag |= INP_IPV6;
1739 inp->in6p_hops = -1; /* use kernel default */
1743 inp->inp_vflag |= INP_IPV4;
1744 tp = tcp_newtcpcb(inp);
1748 INP_INFO_RUNLOCK(&V_tcbinfo);
1751 tp->t_state = TCPS_CLOSED;
1753 INP_INFO_RUNLOCK(&V_tcbinfo);
1758 * Initiate (or continue) disconnect.
1759 * If embryonic state, just send reset (once).
1760 * If in ``let data drain'' option and linger null, just drop.
1761 * Otherwise (hard), mark socket disconnecting and drop
1762 * current input data; switch states based on user close, and
1763 * send segment to peer (with FIN).
1766 tcp_disconnect(struct tcpcb *tp)
1768 struct inpcb *inp = tp->t_inpcb;
1769 struct socket *so = inp->inp_socket;
1771 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1772 INP_WLOCK_ASSERT(inp);
1775 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1776 * socket is still open.
1778 if (tp->t_state < TCPS_ESTABLISHED) {
1781 ("tcp_disconnect: tcp_close() returned NULL"));
1782 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1783 tp = tcp_drop(tp, 0);
1785 ("tcp_disconnect: tcp_drop() returned NULL"));
1787 soisdisconnecting(so);
1788 sbflush(&so->so_rcv);
1790 if (!(inp->inp_flags & INP_DROPPED))
1796 * User issued close, and wish to trail through shutdown states:
1797 * if never received SYN, just forget it. If got a SYN from peer,
1798 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1799 * If already got a FIN from peer, then almost done; go to LAST_ACK
1800 * state. In all other cases, have already sent FIN to peer (e.g.
1801 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1802 * for peer to send FIN or not respond to keep-alives, etc.
1803 * We can let the user exit from the close as soon as the FIN is acked.
1806 tcp_usrclosed(struct tcpcb *tp)
1809 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1810 INP_WLOCK_ASSERT(tp->t_inpcb);
1812 switch (tp->t_state) {
1815 tcp_offload_listen_stop(tp);
1817 tcp_state_change(tp, TCPS_CLOSED);
1822 * tcp_close() should never return NULL here as the socket is
1826 ("tcp_usrclosed: tcp_close() returned NULL"));
1830 case TCPS_SYN_RECEIVED:
1831 tp->t_flags |= TF_NEEDFIN;
1834 case TCPS_ESTABLISHED:
1835 tcp_state_change(tp, TCPS_FIN_WAIT_1);
1838 case TCPS_CLOSE_WAIT:
1839 tcp_state_change(tp, TCPS_LAST_ACK);
1842 if (tp->t_state >= TCPS_FIN_WAIT_2) {
1843 soisdisconnected(tp->t_inpcb->inp_socket);
1844 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
1845 if (tp->t_state == TCPS_FIN_WAIT_2) {
1848 timeout = (tcp_fast_finwait2_recycle) ?
1849 tcp_finwait2_timeout : TP_MAXIDLE(tp);
1850 tcp_timer_activate(tp, TT_2MSL, timeout);
1857 db_print_indent(int indent)
1861 for (i = 0; i < indent; i++)
1866 db_print_tstate(int t_state)
1871 db_printf("TCPS_CLOSED");
1875 db_printf("TCPS_LISTEN");
1879 db_printf("TCPS_SYN_SENT");
1882 case TCPS_SYN_RECEIVED:
1883 db_printf("TCPS_SYN_RECEIVED");
1886 case TCPS_ESTABLISHED:
1887 db_printf("TCPS_ESTABLISHED");
1890 case TCPS_CLOSE_WAIT:
1891 db_printf("TCPS_CLOSE_WAIT");
1894 case TCPS_FIN_WAIT_1:
1895 db_printf("TCPS_FIN_WAIT_1");
1899 db_printf("TCPS_CLOSING");
1903 db_printf("TCPS_LAST_ACK");
1906 case TCPS_FIN_WAIT_2:
1907 db_printf("TCPS_FIN_WAIT_2");
1910 case TCPS_TIME_WAIT:
1911 db_printf("TCPS_TIME_WAIT");
1915 db_printf("unknown");
1921 db_print_tflags(u_int t_flags)
1926 if (t_flags & TF_ACKNOW) {
1927 db_printf("%sTF_ACKNOW", comma ? ", " : "");
1930 if (t_flags & TF_DELACK) {
1931 db_printf("%sTF_DELACK", comma ? ", " : "");
1934 if (t_flags & TF_NODELAY) {
1935 db_printf("%sTF_NODELAY", comma ? ", " : "");
1938 if (t_flags & TF_NOOPT) {
1939 db_printf("%sTF_NOOPT", comma ? ", " : "");
1942 if (t_flags & TF_SENTFIN) {
1943 db_printf("%sTF_SENTFIN", comma ? ", " : "");
1946 if (t_flags & TF_REQ_SCALE) {
1947 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1950 if (t_flags & TF_RCVD_SCALE) {
1951 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1954 if (t_flags & TF_REQ_TSTMP) {
1955 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1958 if (t_flags & TF_RCVD_TSTMP) {
1959 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1962 if (t_flags & TF_SACK_PERMIT) {
1963 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1966 if (t_flags & TF_NEEDSYN) {
1967 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1970 if (t_flags & TF_NEEDFIN) {
1971 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1974 if (t_flags & TF_NOPUSH) {
1975 db_printf("%sTF_NOPUSH", comma ? ", " : "");
1978 if (t_flags & TF_MORETOCOME) {
1979 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1982 if (t_flags & TF_LQ_OVERFLOW) {
1983 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1986 if (t_flags & TF_LASTIDLE) {
1987 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1990 if (t_flags & TF_RXWIN0SENT) {
1991 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1994 if (t_flags & TF_FASTRECOVERY) {
1995 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1998 if (t_flags & TF_CONGRECOVERY) {
1999 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2002 if (t_flags & TF_WASFRECOVERY) {
2003 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2006 if (t_flags & TF_SIGNATURE) {
2007 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2010 if (t_flags & TF_FORCEDATA) {
2011 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2014 if (t_flags & TF_TSO) {
2015 db_printf("%sTF_TSO", comma ? ", " : "");
2018 if (t_flags & TF_ECN_PERMIT) {
2019 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2022 if (t_flags & TF_FASTOPEN) {
2023 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2029 db_print_toobflags(char t_oobflags)
2034 if (t_oobflags & TCPOOB_HAVEDATA) {
2035 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2038 if (t_oobflags & TCPOOB_HADDATA) {
2039 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2045 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2048 db_print_indent(indent);
2049 db_printf("%s at %p\n", name, tp);
2053 db_print_indent(indent);
2054 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2055 LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2057 db_print_indent(indent);
2058 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
2059 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2061 db_print_indent(indent);
2062 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2063 &tp->t_timers->tt_delack, tp->t_inpcb);
2065 db_print_indent(indent);
2066 db_printf("t_state: %d (", tp->t_state);
2067 db_print_tstate(tp->t_state);
2070 db_print_indent(indent);
2071 db_printf("t_flags: 0x%x (", tp->t_flags);
2072 db_print_tflags(tp->t_flags);
2075 db_print_indent(indent);
2076 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2077 tp->snd_una, tp->snd_max, tp->snd_nxt);
2079 db_print_indent(indent);
2080 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2081 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2083 db_print_indent(indent);
2084 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2085 tp->iss, tp->irs, tp->rcv_nxt);
2087 db_print_indent(indent);
2088 db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n",
2089 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2091 db_print_indent(indent);
2092 db_printf("snd_wnd: %lu snd_cwnd: %lu\n",
2093 tp->snd_wnd, tp->snd_cwnd);
2095 db_print_indent(indent);
2096 db_printf("snd_ssthresh: %lu snd_recover: "
2097 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2099 db_print_indent(indent);
2100 db_printf("t_maxopd: %u t_rcvtime: %u t_startime: %u\n",
2101 tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
2103 db_print_indent(indent);
2104 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2105 tp->t_rtttime, tp->t_rtseq);
2107 db_print_indent(indent);
2108 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2109 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2111 db_print_indent(indent);
2112 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2113 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2116 db_print_indent(indent);
2117 db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n",
2118 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2120 db_print_indent(indent);
2121 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2122 db_print_toobflags(tp->t_oobflags);
2123 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2125 db_print_indent(indent);
2126 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2127 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2129 db_print_indent(indent);
2130 db_printf("ts_recent: %u ts_recent_age: %u\n",
2131 tp->ts_recent, tp->ts_recent_age);
2133 db_print_indent(indent);
2134 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2135 "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2137 db_print_indent(indent);
2138 db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x "
2139 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2140 tp->snd_recover_prev, tp->t_badrxtwin);
2142 db_print_indent(indent);
2143 db_printf("snd_numholes: %d snd_holes first: %p\n",
2144 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2146 db_print_indent(indent);
2147 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
2148 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2150 /* Skip sackblks, sackhint. */
2152 db_print_indent(indent);
2153 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2154 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2157 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2162 db_printf("usage: show tcpcb <addr>\n");
2165 tp = (struct tcpcb *)addr;
2167 db_print_tcpcb(tp, "tcpcb", 0);