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_WLOCK_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 * 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 * 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)
265 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
266 INP_INFO_WLOCK(&V_tcbinfo);
268 KASSERT(inp->inp_socket != NULL,
269 ("tcp_usr_detach: inp_socket == NULL"));
271 INP_INFO_WUNLOCK(&V_tcbinfo);
276 * Give the socket an address.
279 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
283 struct tcpcb *tp = NULL;
284 struct sockaddr_in *sinp;
286 sinp = (struct sockaddr_in *)nam;
287 if (nam->sa_len != sizeof (*sinp))
290 * Must check for multicast addresses and disallow binding
293 if (sinp->sin_family == AF_INET &&
294 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
295 return (EAFNOSUPPORT);
299 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
301 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
307 INP_HASH_WLOCK(&V_tcbinfo);
308 error = in_pcbbind(inp, nam, td->td_ucred);
309 INP_HASH_WUNLOCK(&V_tcbinfo);
320 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
324 struct tcpcb *tp = NULL;
325 struct sockaddr_in6 *sin6p;
327 sin6p = (struct sockaddr_in6 *)nam;
328 if (nam->sa_len != sizeof (*sin6p))
331 * Must check for multicast addresses and disallow binding
334 if (sin6p->sin6_family == AF_INET6 &&
335 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
336 return (EAFNOSUPPORT);
340 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
342 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
348 INP_HASH_WLOCK(&V_tcbinfo);
349 inp->inp_vflag &= ~INP_IPV4;
350 inp->inp_vflag |= INP_IPV6;
352 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
353 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
354 inp->inp_vflag |= INP_IPV4;
355 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
356 struct sockaddr_in sin;
358 in6_sin6_2_sin(&sin, sin6p);
359 inp->inp_vflag |= INP_IPV4;
360 inp->inp_vflag &= ~INP_IPV6;
361 error = in_pcbbind(inp, (struct sockaddr *)&sin,
363 INP_HASH_WUNLOCK(&V_tcbinfo);
368 error = in6_pcbbind(inp, nam, td->td_ucred);
369 INP_HASH_WUNLOCK(&V_tcbinfo);
379 * Prepare to accept connections.
382 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
386 struct tcpcb *tp = NULL;
390 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
392 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
399 error = solisten_proto_check(so);
400 INP_HASH_WLOCK(&V_tcbinfo);
401 if (error == 0 && inp->inp_lport == 0)
402 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
403 INP_HASH_WUNLOCK(&V_tcbinfo);
405 tcp_state_change(tp, TCPS_LISTEN);
406 solisten_proto(so, backlog);
408 if ((so->so_options & SO_NO_OFFLOAD) == 0)
409 tcp_offload_listen_start(tp);
415 if (tp->t_flags & TF_FASTOPEN)
416 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
419 TCPDEBUG2(PRU_LISTEN);
427 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
431 struct tcpcb *tp = NULL;
435 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
437 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
444 error = solisten_proto_check(so);
445 INP_HASH_WLOCK(&V_tcbinfo);
446 if (error == 0 && inp->inp_lport == 0) {
447 inp->inp_vflag &= ~INP_IPV4;
448 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
449 inp->inp_vflag |= INP_IPV4;
450 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
452 INP_HASH_WUNLOCK(&V_tcbinfo);
454 tcp_state_change(tp, TCPS_LISTEN);
455 solisten_proto(so, backlog);
457 if ((so->so_options & SO_NO_OFFLOAD) == 0)
458 tcp_offload_listen_start(tp);
464 if (tp->t_flags & TF_FASTOPEN)
465 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
468 TCPDEBUG2(PRU_LISTEN);
476 * Initiate connection to peer.
477 * Create a template for use in transmissions on this connection.
478 * Enter SYN_SENT state, and mark socket as connecting.
479 * Start keep-alive timer, and seed output sequence space.
480 * Send initial segment on connection.
483 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
487 struct tcpcb *tp = NULL;
488 struct sockaddr_in *sinp;
490 sinp = (struct sockaddr_in *)nam;
491 if (nam->sa_len != sizeof (*sinp))
494 * Must disallow TCP ``connections'' to multicast addresses.
496 if (sinp->sin_family == AF_INET
497 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
498 return (EAFNOSUPPORT);
499 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
504 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
506 if (inp->inp_flags & INP_TIMEWAIT) {
510 if (inp->inp_flags & INP_DROPPED) {
511 error = ECONNREFUSED;
516 if ((error = tcp_connect(tp, nam, td)) != 0)
519 if (registered_toedevs > 0 &&
520 (so->so_options & SO_NO_OFFLOAD) == 0 &&
521 (error = tcp_offload_connect(so, nam)) == 0)
524 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
525 error = tcp_output(tp);
527 TCPDEBUG2(PRU_CONNECT);
535 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
539 struct tcpcb *tp = NULL;
540 struct sockaddr_in6 *sin6p;
544 sin6p = (struct sockaddr_in6 *)nam;
545 if (nam->sa_len != sizeof (*sin6p))
548 * Must disallow TCP ``connections'' to multicast addresses.
550 if (sin6p->sin6_family == AF_INET6
551 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
552 return (EAFNOSUPPORT);
555 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
557 if (inp->inp_flags & INP_TIMEWAIT) {
561 if (inp->inp_flags & INP_DROPPED) {
562 error = ECONNREFUSED;
569 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
570 * therefore probably require the hash lock, which isn't held here.
571 * Is this a significant problem?
573 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
574 struct sockaddr_in sin;
576 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
581 in6_sin6_2_sin(&sin, sin6p);
582 inp->inp_vflag |= INP_IPV4;
583 inp->inp_vflag &= ~INP_IPV6;
584 if ((error = prison_remote_ip4(td->td_ucred,
585 &sin.sin_addr)) != 0)
587 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
590 if (registered_toedevs > 0 &&
591 (so->so_options & SO_NO_OFFLOAD) == 0 &&
592 (error = tcp_offload_connect(so, nam)) == 0)
595 error = tcp_output(tp);
599 inp->inp_vflag &= ~INP_IPV4;
600 inp->inp_vflag |= INP_IPV6;
601 inp->inp_inc.inc_flags |= INC_ISIPV6;
602 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
604 if ((error = tcp6_connect(tp, nam, td)) != 0)
607 if (registered_toedevs > 0 &&
608 (so->so_options & SO_NO_OFFLOAD) == 0 &&
609 (error = tcp_offload_connect(so, nam)) == 0)
612 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
613 error = tcp_output(tp);
616 TCPDEBUG2(PRU_CONNECT);
623 * Initiate disconnect from peer.
624 * If connection never passed embryonic stage, just drop;
625 * else if don't need to let data drain, then can just drop anyways,
626 * else have to begin TCP shutdown process: mark socket disconnecting,
627 * drain unread data, state switch to reflect user close, and
628 * send segment (e.g. FIN) to peer. Socket will be really disconnected
629 * when peer sends FIN and acks ours.
631 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
634 tcp_usr_disconnect(struct socket *so)
637 struct tcpcb *tp = NULL;
641 INP_INFO_WLOCK(&V_tcbinfo);
643 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
645 if (inp->inp_flags & INP_TIMEWAIT)
647 if (inp->inp_flags & INP_DROPPED) {
655 TCPDEBUG2(PRU_DISCONNECT);
657 INP_INFO_WUNLOCK(&V_tcbinfo);
663 * Accept a connection. Essentially all the work is done at higher levels;
664 * just return the address of the peer, storing through addr.
667 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
670 struct inpcb *inp = NULL;
671 struct tcpcb *tp = NULL;
676 if (so->so_state & SS_ISDISCONNECTED)
677 return (ECONNABORTED);
680 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
682 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
683 error = ECONNABORTED;
690 * We inline in_getpeeraddr and COMMON_END here, so that we can
691 * copy the data of interest and defer the malloc until after we
694 port = inp->inp_fport;
695 addr = inp->inp_faddr;
698 TCPDEBUG2(PRU_ACCEPT);
701 *nam = in_sockaddr(port, &addr);
708 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
710 struct inpcb *inp = NULL;
712 struct tcpcb *tp = NULL;
714 struct in6_addr addr6;
719 if (so->so_state & SS_ISDISCONNECTED)
720 return (ECONNABORTED);
723 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
724 INP_INFO_RLOCK(&V_tcbinfo);
726 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
727 error = ECONNABORTED;
734 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
735 * copy the data of interest and defer the malloc until after we
738 if (inp->inp_vflag & INP_IPV4) {
740 port = inp->inp_fport;
741 addr = inp->inp_faddr;
743 port = inp->inp_fport;
744 addr6 = inp->in6p_faddr;
748 TCPDEBUG2(PRU_ACCEPT);
750 INP_INFO_RUNLOCK(&V_tcbinfo);
753 *nam = in6_v4mapsin6_sockaddr(port, &addr);
755 *nam = in6_sockaddr(port, &addr6);
762 * Mark the connection as being incapable of further output.
765 tcp_usr_shutdown(struct socket *so)
769 struct tcpcb *tp = NULL;
772 INP_INFO_WLOCK(&V_tcbinfo);
774 KASSERT(inp != NULL, ("inp == NULL"));
776 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
784 if (!(inp->inp_flags & INP_DROPPED))
785 error = tcp_output(tp);
788 TCPDEBUG2(PRU_SHUTDOWN);
790 INP_INFO_WUNLOCK(&V_tcbinfo);
796 * After a receive, possibly send window update to peer.
799 tcp_usr_rcvd(struct socket *so, int flags)
802 struct tcpcb *tp = NULL;
807 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
809 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
817 * For passively-created TFO connections, don't attempt a window
818 * update while still in SYN_RECEIVED as this may trigger an early
819 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
820 * application response data, or failing that, when the DELACK timer
823 if ((tp->t_flags & TF_FASTOPEN) &&
824 (tp->t_state == TCPS_SYN_RECEIVED))
828 if (tp->t_flags & TF_TOE)
829 tcp_offload_rcvd(tp);
841 * Do a send by putting data in output queue and updating urgent
842 * marker if URG set. Possibly send more data. Unlike the other
843 * pru_*() routines, the mbuf chains are our responsibility. We
844 * must either enqueue them or free them. The other pru_* routines
845 * generally are caller-frees.
848 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
849 struct sockaddr *nam, struct mbuf *control, struct thread *td)
853 struct tcpcb *tp = NULL;
860 * We require the pcbinfo lock if we will close the socket as part of
863 if (flags & PRUS_EOF)
864 INP_INFO_WLOCK(&V_tcbinfo);
866 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
868 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
877 isipv6 = nam && nam->sa_family == AF_INET6;
882 /* TCP doesn't do control messages (rights, creds, etc) */
883 if (control->m_len) {
890 m_freem(control); /* empty control, just free it */
892 if (!(flags & PRUS_OOB)) {
893 sbappendstream(&so->so_snd, m);
894 if (nam && tp->t_state < TCPS_SYN_SENT) {
896 * Do implied connect if not yet connected,
897 * initialize window to default value, and
898 * initialize maxseg/maxopd using peer's cached
903 error = tcp6_connect(tp, nam, td);
905 #if defined(INET6) && defined(INET)
909 error = tcp_connect(tp, nam, td);
913 tp->snd_wnd = TTCP_CLIENT_SND_WND;
916 if (flags & PRUS_EOF) {
918 * Close the send side of the connection after
921 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
925 if (!(inp->inp_flags & INP_DROPPED)) {
926 if (flags & PRUS_MORETOCOME)
927 tp->t_flags |= TF_MORETOCOME;
928 error = tcp_output(tp);
929 if (flags & PRUS_MORETOCOME)
930 tp->t_flags &= ~TF_MORETOCOME;
934 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
936 SOCKBUF_LOCK(&so->so_snd);
937 if (sbspace(&so->so_snd) < -512) {
938 SOCKBUF_UNLOCK(&so->so_snd);
944 * According to RFC961 (Assigned Protocols),
945 * the urgent pointer points to the last octet
946 * of urgent data. We continue, however,
947 * to consider it to indicate the first octet
948 * of data past the urgent section.
949 * Otherwise, snd_up should be one lower.
951 sbappendstream_locked(&so->so_snd, m);
952 SOCKBUF_UNLOCK(&so->so_snd);
953 if (nam && tp->t_state < TCPS_SYN_SENT) {
955 * Do implied connect if not yet connected,
956 * initialize window to default value, and
957 * initialize maxseg/maxopd using peer's cached
962 error = tcp6_connect(tp, nam, td);
964 #if defined(INET6) && defined(INET)
968 error = tcp_connect(tp, nam, td);
972 tp->snd_wnd = TTCP_CLIENT_SND_WND;
975 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
976 tp->t_flags |= TF_FORCEDATA;
977 error = tcp_output(tp);
978 tp->t_flags &= ~TF_FORCEDATA;
981 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
982 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
984 if (flags & PRUS_EOF)
985 INP_INFO_WUNLOCK(&V_tcbinfo);
990 * Abort the TCP. Drop the connection abruptly.
993 tcp_usr_abort(struct socket *so)
996 struct tcpcb *tp = NULL;
1000 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1002 INP_INFO_WLOCK(&V_tcbinfo);
1004 KASSERT(inp->inp_socket != NULL,
1005 ("tcp_usr_abort: inp_socket == NULL"));
1008 * If we still have full TCP state, and we're not dropped, drop.
1010 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1011 !(inp->inp_flags & INP_DROPPED)) {
1012 tp = intotcpcb(inp);
1014 tcp_drop(tp, ECONNABORTED);
1015 TCPDEBUG2(PRU_ABORT);
1017 if (!(inp->inp_flags & INP_DROPPED)) {
1019 so->so_state |= SS_PROTOREF;
1021 inp->inp_flags |= INP_SOCKREF;
1024 INP_INFO_WUNLOCK(&V_tcbinfo);
1028 * TCP socket is closed. Start friendly disconnect.
1031 tcp_usr_close(struct socket *so)
1034 struct tcpcb *tp = NULL;
1037 inp = sotoinpcb(so);
1038 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1040 INP_INFO_WLOCK(&V_tcbinfo);
1042 KASSERT(inp->inp_socket != NULL,
1043 ("tcp_usr_close: inp_socket == NULL"));
1046 * If we still have full TCP state, and we're not dropped, initiate
1049 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1050 !(inp->inp_flags & INP_DROPPED)) {
1051 tp = intotcpcb(inp);
1054 TCPDEBUG2(PRU_CLOSE);
1056 if (!(inp->inp_flags & INP_DROPPED)) {
1058 so->so_state |= SS_PROTOREF;
1060 inp->inp_flags |= INP_SOCKREF;
1063 INP_INFO_WUNLOCK(&V_tcbinfo);
1067 * Receive out-of-band data.
1070 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1074 struct tcpcb *tp = NULL;
1077 inp = sotoinpcb(so);
1078 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1080 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1084 tp = intotcpcb(inp);
1086 if ((so->so_oobmark == 0 &&
1087 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1088 so->so_options & SO_OOBINLINE ||
1089 tp->t_oobflags & TCPOOB_HADDATA) {
1093 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1094 error = EWOULDBLOCK;
1098 *mtod(m, caddr_t) = tp->t_iobc;
1099 if ((flags & MSG_PEEK) == 0)
1100 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1103 TCPDEBUG2(PRU_RCVOOB);
1109 struct pr_usrreqs tcp_usrreqs = {
1110 .pru_abort = tcp_usr_abort,
1111 .pru_accept = tcp_usr_accept,
1112 .pru_attach = tcp_usr_attach,
1113 .pru_bind = tcp_usr_bind,
1114 .pru_connect = tcp_usr_connect,
1115 .pru_control = in_control,
1116 .pru_detach = tcp_usr_detach,
1117 .pru_disconnect = tcp_usr_disconnect,
1118 .pru_listen = tcp_usr_listen,
1119 .pru_peeraddr = in_getpeeraddr,
1120 .pru_rcvd = tcp_usr_rcvd,
1121 .pru_rcvoob = tcp_usr_rcvoob,
1122 .pru_send = tcp_usr_send,
1123 .pru_shutdown = tcp_usr_shutdown,
1124 .pru_sockaddr = in_getsockaddr,
1125 .pru_sosetlabel = in_pcbsosetlabel,
1126 .pru_close = tcp_usr_close,
1131 struct pr_usrreqs tcp6_usrreqs = {
1132 .pru_abort = tcp_usr_abort,
1133 .pru_accept = tcp6_usr_accept,
1134 .pru_attach = tcp_usr_attach,
1135 .pru_bind = tcp6_usr_bind,
1136 .pru_connect = tcp6_usr_connect,
1137 .pru_control = in6_control,
1138 .pru_detach = tcp_usr_detach,
1139 .pru_disconnect = tcp_usr_disconnect,
1140 .pru_listen = tcp6_usr_listen,
1141 .pru_peeraddr = in6_mapped_peeraddr,
1142 .pru_rcvd = tcp_usr_rcvd,
1143 .pru_rcvoob = tcp_usr_rcvoob,
1144 .pru_send = tcp_usr_send,
1145 .pru_shutdown = tcp_usr_shutdown,
1146 .pru_sockaddr = in6_mapped_sockaddr,
1147 .pru_sosetlabel = in_pcbsosetlabel,
1148 .pru_close = tcp_usr_close,
1154 * Common subroutine to open a TCP connection to remote host specified
1155 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1156 * port number if needed. Call in_pcbconnect_setup to do the routing and
1157 * to choose a local host address (interface). If there is an existing
1158 * incarnation of the same connection in TIME-WAIT state and if the remote
1159 * host was sending CC options and if the connection duration was < MSL, then
1160 * truncate the previous TIME-WAIT state and proceed.
1161 * Initialize connection parameters and enter SYN-SENT state.
1164 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1166 struct inpcb *inp = tp->t_inpcb, *oinp;
1167 struct socket *so = inp->inp_socket;
1168 struct in_addr laddr;
1172 INP_WLOCK_ASSERT(inp);
1173 INP_HASH_WLOCK(&V_tcbinfo);
1175 if (inp->inp_lport == 0) {
1176 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1182 * Cannot simply call in_pcbconnect, because there might be an
1183 * earlier incarnation of this same connection still in
1184 * TIME_WAIT state, creating an ADDRINUSE error.
1186 laddr = inp->inp_laddr;
1187 lport = inp->inp_lport;
1188 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1189 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1190 if (error && oinp == NULL)
1196 inp->inp_laddr = laddr;
1198 INP_HASH_WUNLOCK(&V_tcbinfo);
1201 * Compute window scaling to request:
1202 * Scale to fit into sweet spot. See tcp_syncache.c.
1203 * XXX: This should move to tcp_output().
1205 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1206 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1207 tp->request_r_scale++;
1210 TCPSTAT_INC(tcps_connattempt);
1211 tcp_state_change(tp, TCPS_SYN_SENT);
1212 tp->iss = tcp_new_isn(tp);
1213 tcp_sendseqinit(tp);
1218 INP_HASH_WUNLOCK(&V_tcbinfo);
1225 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1227 struct inpcb *inp = tp->t_inpcb, *oinp;
1228 struct socket *so = inp->inp_socket;
1229 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1230 struct in6_addr addr6;
1233 INP_WLOCK_ASSERT(inp);
1234 INP_HASH_WLOCK(&V_tcbinfo);
1236 if (inp->inp_lport == 0) {
1237 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1243 * Cannot simply call in_pcbconnect, because there might be an
1244 * earlier incarnation of this same connection still in
1245 * TIME_WAIT state, creating an ADDRINUSE error.
1246 * in6_pcbladdr() also handles scope zone IDs.
1248 * XXXRW: We wouldn't need to expose in6_pcblookup_hash_locked()
1249 * outside of in6_pcb.c if there were an in6_pcbconnect_setup().
1251 error = in6_pcbladdr(inp, nam, &addr6);
1254 oinp = in6_pcblookup_hash_locked(inp->inp_pcbinfo,
1255 &sin6->sin6_addr, sin6->sin6_port,
1256 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1259 inp->inp_lport, 0, NULL);
1264 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1265 inp->in6p_laddr = addr6;
1266 inp->in6p_faddr = sin6->sin6_addr;
1267 inp->inp_fport = sin6->sin6_port;
1268 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
1269 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
1270 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
1272 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1274 INP_HASH_WUNLOCK(&V_tcbinfo);
1276 /* Compute window scaling to request. */
1277 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1278 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1279 tp->request_r_scale++;
1282 TCPSTAT_INC(tcps_connattempt);
1283 tcp_state_change(tp, TCPS_SYN_SENT);
1284 tp->iss = tcp_new_isn(tp);
1285 tcp_sendseqinit(tp);
1290 INP_HASH_WUNLOCK(&V_tcbinfo);
1296 * Export TCP internal state information via a struct tcp_info, based on the
1297 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1298 * (TCP state machine, etc). We export all information using FreeBSD-native
1299 * constants -- for example, the numeric values for tcpi_state will differ
1303 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1306 INP_WLOCK_ASSERT(tp->t_inpcb);
1307 bzero(ti, sizeof(*ti));
1309 ti->tcpi_state = tp->t_state;
1310 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1311 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1312 if (tp->t_flags & TF_SACK_PERMIT)
1313 ti->tcpi_options |= TCPI_OPT_SACK;
1314 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1315 ti->tcpi_options |= TCPI_OPT_WSCALE;
1316 ti->tcpi_snd_wscale = tp->snd_scale;
1317 ti->tcpi_rcv_wscale = tp->rcv_scale;
1320 ti->tcpi_rto = tp->t_rxtcur * tick;
1321 ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
1322 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1323 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1325 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1326 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1329 * FreeBSD-specific extension fields for tcp_info.
1331 ti->tcpi_rcv_space = tp->rcv_wnd;
1332 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1333 ti->tcpi_snd_wnd = tp->snd_wnd;
1334 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1335 ti->tcpi_snd_nxt = tp->snd_nxt;
1336 ti->tcpi_snd_mss = tp->t_maxseg;
1337 ti->tcpi_rcv_mss = tp->t_maxseg;
1338 if (tp->t_flags & TF_TOE)
1339 ti->tcpi_options |= TCPI_OPT_TOE;
1340 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1341 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1342 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1346 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1347 * socket option arguments. When it re-acquires the lock after the copy, it
1348 * has to revalidate that the connection is still valid for the socket
1351 #define INP_WLOCK_RECHECK(inp) do { \
1353 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1355 return (ECONNRESET); \
1357 tp = intotcpcb(inp); \
1361 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1363 int error, opt, optval;
1368 char buf[TCP_CA_NAME_MAX];
1369 struct cc_algo *algo;
1372 inp = sotoinpcb(so);
1373 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1375 if (sopt->sopt_level != IPPROTO_TCP) {
1377 if (inp->inp_vflag & INP_IPV6PROTO) {
1379 error = ip6_ctloutput(so, sopt);
1382 #if defined(INET6) && defined(INET)
1388 error = ip_ctloutput(so, sopt);
1393 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1395 return (ECONNRESET);
1398 switch (sopt->sopt_dir) {
1400 switch (sopt->sopt_name) {
1401 #ifdef TCP_SIGNATURE
1404 error = sooptcopyin(sopt, &optval, sizeof optval,
1409 INP_WLOCK_RECHECK(inp);
1411 tp->t_flags |= TF_SIGNATURE;
1413 tp->t_flags &= ~TF_SIGNATURE;
1414 goto unlock_and_done;
1415 #endif /* TCP_SIGNATURE */
1420 error = sooptcopyin(sopt, &optval, sizeof optval,
1425 INP_WLOCK_RECHECK(inp);
1426 switch (sopt->sopt_name) {
1434 opt = 0; /* dead code to fool gcc */
1441 tp->t_flags &= ~opt;
1444 if (tp->t_flags & TF_TOE) {
1445 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1454 error = sooptcopyin(sopt, &optval, sizeof optval,
1459 INP_WLOCK_RECHECK(inp);
1461 tp->t_flags |= TF_NOPUSH;
1462 else if (tp->t_flags & TF_NOPUSH) {
1463 tp->t_flags &= ~TF_NOPUSH;
1464 if (TCPS_HAVEESTABLISHED(tp->t_state))
1465 error = tcp_output(tp);
1467 goto unlock_and_done;
1471 error = sooptcopyin(sopt, &optval, sizeof optval,
1476 INP_WLOCK_RECHECK(inp);
1477 if (optval > 0 && optval <= tp->t_maxseg &&
1478 optval + 40 >= V_tcp_minmss)
1479 tp->t_maxseg = optval;
1482 goto unlock_and_done;
1489 case TCP_CONGESTION:
1491 bzero(buf, sizeof(buf));
1492 error = sooptcopyin(sopt, &buf, sizeof(buf), 1);
1495 INP_WLOCK_RECHECK(inp);
1497 * Return EINVAL if we can't find the requested cc algo.
1501 STAILQ_FOREACH(algo, &cc_list, entries) {
1502 if (strncmp(buf, algo->name, TCP_CA_NAME_MAX)
1504 /* We've found the requested algo. */
1507 * We hold a write lock over the tcb
1508 * so it's safe to do these things
1509 * without ordering concerns.
1511 if (CC_ALGO(tp)->cb_destroy != NULL)
1512 CC_ALGO(tp)->cb_destroy(tp->ccv);
1515 * If something goes pear shaped
1516 * initialising the new algo,
1517 * fall back to newreno (which
1518 * does not require initialisation).
1520 if (algo->cb_init != NULL)
1521 if (algo->cb_init(tp->ccv) > 0) {
1522 CC_ALGO(tp) = &newreno_cc_algo;
1524 * The only reason init
1526 * because of malloc.
1530 break; /* Break the STAILQ_FOREACH. */
1534 goto unlock_and_done;
1540 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1544 if (ui > (UINT_MAX / hz)) {
1550 INP_WLOCK_RECHECK(inp);
1551 switch (sopt->sopt_name) {
1553 tp->t_keepidle = ui;
1555 * XXX: better check current remaining
1556 * timeout and "merge" it with new value.
1558 if ((tp->t_state > TCPS_LISTEN) &&
1559 (tp->t_state <= TCPS_CLOSING))
1560 tcp_timer_activate(tp, TT_KEEP,
1564 tp->t_keepintvl = ui;
1565 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1566 (TP_MAXIDLE(tp) > 0))
1567 tcp_timer_activate(tp, TT_2MSL,
1571 tp->t_keepinit = ui;
1572 if (tp->t_state == TCPS_SYN_RECEIVED ||
1573 tp->t_state == TCPS_SYN_SENT)
1574 tcp_timer_activate(tp, TT_KEEP,
1578 goto unlock_and_done;
1582 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1586 INP_WLOCK_RECHECK(inp);
1588 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1589 (TP_MAXIDLE(tp) > 0))
1590 tcp_timer_activate(tp, TT_2MSL,
1592 goto unlock_and_done;
1597 if (!V_tcp_fastopen_enabled)
1600 error = sooptcopyin(sopt, &optval, sizeof optval,
1605 INP_WLOCK_RECHECK(inp);
1607 tp->t_flags |= TF_FASTOPEN;
1608 if ((tp->t_state == TCPS_LISTEN) &&
1609 (tp->t_tfo_pending == NULL))
1611 tcp_fastopen_alloc_counter();
1613 tp->t_flags &= ~TF_FASTOPEN;
1614 goto unlock_and_done;
1619 error = ENOPROTOOPT;
1625 tp = intotcpcb(inp);
1626 switch (sopt->sopt_name) {
1627 #ifdef TCP_SIGNATURE
1629 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1631 error = sooptcopyout(sopt, &optval, sizeof optval);
1636 optval = tp->t_flags & TF_NODELAY;
1638 error = sooptcopyout(sopt, &optval, sizeof optval);
1641 optval = tp->t_maxseg;
1643 error = sooptcopyout(sopt, &optval, sizeof optval);
1646 optval = tp->t_flags & TF_NOOPT;
1648 error = sooptcopyout(sopt, &optval, sizeof optval);
1651 optval = tp->t_flags & TF_NOPUSH;
1653 error = sooptcopyout(sopt, &optval, sizeof optval);
1656 tcp_fill_info(tp, &ti);
1658 error = sooptcopyout(sopt, &ti, sizeof ti);
1660 case TCP_CONGESTION:
1661 bzero(buf, sizeof(buf));
1662 strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1664 error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX);
1670 switch (sopt->sopt_name) {
1672 ui = tp->t_keepidle / hz;
1675 ui = tp->t_keepintvl / hz;
1678 ui = tp->t_keepinit / hz;
1685 error = sooptcopyout(sopt, &ui, sizeof(ui));
1689 optval = tp->t_flags & TF_FASTOPEN;
1691 error = sooptcopyout(sopt, &optval, sizeof optval);
1696 error = ENOPROTOOPT;
1703 #undef INP_WLOCK_RECHECK
1706 * Attach TCP protocol to socket, allocating
1707 * internet protocol control block, tcp control block,
1708 * bufer space, and entering LISTEN state if to accept connections.
1711 tcp_attach(struct socket *so)
1717 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1718 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1722 so->so_rcv.sb_flags |= SB_AUTOSIZE;
1723 so->so_snd.sb_flags |= SB_AUTOSIZE;
1724 INP_INFO_WLOCK(&V_tcbinfo);
1725 error = in_pcballoc(so, &V_tcbinfo);
1727 INP_INFO_WUNLOCK(&V_tcbinfo);
1730 inp = sotoinpcb(so);
1732 if (inp->inp_vflag & INP_IPV6PROTO) {
1733 inp->inp_vflag |= INP_IPV6;
1734 inp->in6p_hops = -1; /* use kernel default */
1738 inp->inp_vflag |= INP_IPV4;
1739 tp = tcp_newtcpcb(inp);
1743 INP_INFO_WUNLOCK(&V_tcbinfo);
1746 tp->t_state = TCPS_CLOSED;
1748 INP_INFO_WUNLOCK(&V_tcbinfo);
1753 * Initiate (or continue) disconnect.
1754 * If embryonic state, just send reset (once).
1755 * If in ``let data drain'' option and linger null, just drop.
1756 * Otherwise (hard), mark socket disconnecting and drop
1757 * current input data; switch states based on user close, and
1758 * send segment to peer (with FIN).
1761 tcp_disconnect(struct tcpcb *tp)
1763 struct inpcb *inp = tp->t_inpcb;
1764 struct socket *so = inp->inp_socket;
1766 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1767 INP_WLOCK_ASSERT(inp);
1770 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1771 * socket is still open.
1773 if (tp->t_state < TCPS_ESTABLISHED) {
1776 ("tcp_disconnect: tcp_close() returned NULL"));
1777 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1778 tp = tcp_drop(tp, 0);
1780 ("tcp_disconnect: tcp_drop() returned NULL"));
1782 soisdisconnecting(so);
1783 sbflush(&so->so_rcv);
1785 if (!(inp->inp_flags & INP_DROPPED))
1791 * User issued close, and wish to trail through shutdown states:
1792 * if never received SYN, just forget it. If got a SYN from peer,
1793 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1794 * If already got a FIN from peer, then almost done; go to LAST_ACK
1795 * state. In all other cases, have already sent FIN to peer (e.g.
1796 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1797 * for peer to send FIN or not respond to keep-alives, etc.
1798 * We can let the user exit from the close as soon as the FIN is acked.
1801 tcp_usrclosed(struct tcpcb *tp)
1804 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1805 INP_WLOCK_ASSERT(tp->t_inpcb);
1807 switch (tp->t_state) {
1810 tcp_offload_listen_stop(tp);
1812 tcp_state_change(tp, TCPS_CLOSED);
1817 * tcp_close() should never return NULL here as the socket is
1821 ("tcp_usrclosed: tcp_close() returned NULL"));
1825 case TCPS_SYN_RECEIVED:
1826 tp->t_flags |= TF_NEEDFIN;
1829 case TCPS_ESTABLISHED:
1830 tcp_state_change(tp, TCPS_FIN_WAIT_1);
1833 case TCPS_CLOSE_WAIT:
1834 tcp_state_change(tp, TCPS_LAST_ACK);
1837 if (tp->t_state >= TCPS_FIN_WAIT_2) {
1838 soisdisconnected(tp->t_inpcb->inp_socket);
1839 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
1840 if (tp->t_state == TCPS_FIN_WAIT_2) {
1843 timeout = (tcp_fast_finwait2_recycle) ?
1844 tcp_finwait2_timeout : TP_MAXIDLE(tp);
1845 tcp_timer_activate(tp, TT_2MSL, timeout);
1852 db_print_indent(int indent)
1856 for (i = 0; i < indent; i++)
1861 db_print_tstate(int t_state)
1866 db_printf("TCPS_CLOSED");
1870 db_printf("TCPS_LISTEN");
1874 db_printf("TCPS_SYN_SENT");
1877 case TCPS_SYN_RECEIVED:
1878 db_printf("TCPS_SYN_RECEIVED");
1881 case TCPS_ESTABLISHED:
1882 db_printf("TCPS_ESTABLISHED");
1885 case TCPS_CLOSE_WAIT:
1886 db_printf("TCPS_CLOSE_WAIT");
1889 case TCPS_FIN_WAIT_1:
1890 db_printf("TCPS_FIN_WAIT_1");
1894 db_printf("TCPS_CLOSING");
1898 db_printf("TCPS_LAST_ACK");
1901 case TCPS_FIN_WAIT_2:
1902 db_printf("TCPS_FIN_WAIT_2");
1905 case TCPS_TIME_WAIT:
1906 db_printf("TCPS_TIME_WAIT");
1910 db_printf("unknown");
1916 db_print_tflags(u_int t_flags)
1921 if (t_flags & TF_ACKNOW) {
1922 db_printf("%sTF_ACKNOW", comma ? ", " : "");
1925 if (t_flags & TF_DELACK) {
1926 db_printf("%sTF_DELACK", comma ? ", " : "");
1929 if (t_flags & TF_NODELAY) {
1930 db_printf("%sTF_NODELAY", comma ? ", " : "");
1933 if (t_flags & TF_NOOPT) {
1934 db_printf("%sTF_NOOPT", comma ? ", " : "");
1937 if (t_flags & TF_SENTFIN) {
1938 db_printf("%sTF_SENTFIN", comma ? ", " : "");
1941 if (t_flags & TF_REQ_SCALE) {
1942 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1945 if (t_flags & TF_RCVD_SCALE) {
1946 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1949 if (t_flags & TF_REQ_TSTMP) {
1950 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1953 if (t_flags & TF_RCVD_TSTMP) {
1954 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1957 if (t_flags & TF_SACK_PERMIT) {
1958 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1961 if (t_flags & TF_NEEDSYN) {
1962 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1965 if (t_flags & TF_NEEDFIN) {
1966 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1969 if (t_flags & TF_NOPUSH) {
1970 db_printf("%sTF_NOPUSH", comma ? ", " : "");
1973 if (t_flags & TF_MORETOCOME) {
1974 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1977 if (t_flags & TF_LQ_OVERFLOW) {
1978 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1981 if (t_flags & TF_LASTIDLE) {
1982 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1985 if (t_flags & TF_RXWIN0SENT) {
1986 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1989 if (t_flags & TF_FASTRECOVERY) {
1990 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1993 if (t_flags & TF_CONGRECOVERY) {
1994 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
1997 if (t_flags & TF_WASFRECOVERY) {
1998 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2001 if (t_flags & TF_SIGNATURE) {
2002 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2005 if (t_flags & TF_FORCEDATA) {
2006 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2009 if (t_flags & TF_TSO) {
2010 db_printf("%sTF_TSO", comma ? ", " : "");
2013 if (t_flags & TF_ECN_PERMIT) {
2014 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2017 if (t_flags & TF_FASTOPEN) {
2018 db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2024 db_print_toobflags(char t_oobflags)
2029 if (t_oobflags & TCPOOB_HAVEDATA) {
2030 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2033 if (t_oobflags & TCPOOB_HADDATA) {
2034 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2040 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2043 db_print_indent(indent);
2044 db_printf("%s at %p\n", name, tp);
2048 db_print_indent(indent);
2049 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2050 LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2052 db_print_indent(indent);
2053 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
2054 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2056 db_print_indent(indent);
2057 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2058 &tp->t_timers->tt_delack, tp->t_inpcb);
2060 db_print_indent(indent);
2061 db_printf("t_state: %d (", tp->t_state);
2062 db_print_tstate(tp->t_state);
2065 db_print_indent(indent);
2066 db_printf("t_flags: 0x%x (", tp->t_flags);
2067 db_print_tflags(tp->t_flags);
2070 db_print_indent(indent);
2071 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2072 tp->snd_una, tp->snd_max, tp->snd_nxt);
2074 db_print_indent(indent);
2075 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2076 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2078 db_print_indent(indent);
2079 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2080 tp->iss, tp->irs, tp->rcv_nxt);
2082 db_print_indent(indent);
2083 db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n",
2084 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2086 db_print_indent(indent);
2087 db_printf("snd_wnd: %lu snd_cwnd: %lu\n",
2088 tp->snd_wnd, tp->snd_cwnd);
2090 db_print_indent(indent);
2091 db_printf("snd_ssthresh: %lu snd_recover: "
2092 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2094 db_print_indent(indent);
2095 db_printf("t_maxopd: %u t_rcvtime: %u t_startime: %u\n",
2096 tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
2098 db_print_indent(indent);
2099 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2100 tp->t_rtttime, tp->t_rtseq);
2102 db_print_indent(indent);
2103 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2104 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2106 db_print_indent(indent);
2107 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2108 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2111 db_print_indent(indent);
2112 db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n",
2113 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2115 db_print_indent(indent);
2116 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2117 db_print_toobflags(tp->t_oobflags);
2118 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2120 db_print_indent(indent);
2121 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2122 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2124 db_print_indent(indent);
2125 db_printf("ts_recent: %u ts_recent_age: %u\n",
2126 tp->ts_recent, tp->ts_recent_age);
2128 db_print_indent(indent);
2129 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2130 "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2132 db_print_indent(indent);
2133 db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x "
2134 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2135 tp->snd_recover_prev, tp->t_badrxtwin);
2137 db_print_indent(indent);
2138 db_printf("snd_numholes: %d snd_holes first: %p\n",
2139 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2141 db_print_indent(indent);
2142 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
2143 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2145 /* Skip sackblks, sackhint. */
2147 db_print_indent(indent);
2148 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2149 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2152 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2157 db_printf("usage: show tcpcb <addr>\n");
2160 tp = (struct tcpcb *)addr;
2162 db_print_tcpcb(tp, "tcpcb", 0);