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>
67 #include <net/if_var.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>
83 #include <netinet/tcp_fsm.h>
84 #include <netinet/tcp_seq.h>
85 #include <netinet/tcp_timer.h>
86 #include <netinet/tcp_var.h>
87 #include <netinet/tcpip.h>
89 #include <netinet/tcp_debug.h>
92 #include <netinet/tcp_offload.h>
96 * TCP protocol interface to socket abstraction.
98 static int tcp_attach(struct socket *);
100 static int tcp_connect(struct tcpcb *, struct sockaddr *,
104 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
107 static void tcp_disconnect(struct tcpcb *);
108 static void tcp_usrclosed(struct tcpcb *);
109 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
112 #define TCPDEBUG0 int ostate = 0
113 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
114 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
115 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
119 #define TCPDEBUG2(req)
123 * TCP attaches to socket via pru_attach(), reserving space,
124 * and an internet control block.
127 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
130 struct tcpcb *tp = NULL;
135 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
138 error = tcp_attach(so);
142 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
143 so->so_linger = TCP_LINGERTIME;
148 TCPDEBUG2(PRU_ATTACH);
153 * tcp_detach is called when the socket layer loses its final reference
154 * to the socket, be it a file descriptor reference, a reference from TCP,
155 * etc. At this point, there is only one case in which we will keep around
156 * inpcb state: time wait.
158 * This function can probably be re-absorbed back into tcp_usr_detach() now
159 * that there is a single detach path.
162 tcp_detach(struct socket *so, struct inpcb *inp)
166 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
167 INP_WLOCK_ASSERT(inp);
169 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
170 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
174 if (inp->inp_flags & INP_TIMEWAIT) {
176 * There are two cases to handle: one in which the time wait
177 * state is being discarded (INP_DROPPED), and one in which
178 * this connection will remain in timewait. In the former,
179 * it is time to discard all state (except tcptw, which has
180 * already been discarded by the timewait close code, which
181 * should be further up the call stack somewhere). In the
182 * latter case, we detach from the socket, but leave the pcb
183 * present until timewait ends.
185 * XXXRW: Would it be cleaner to free the tcptw here?
187 * Astute question indeed, from twtcp perspective there are
188 * three cases to consider:
190 * #1 tcp_detach is called at tcptw creation time by
191 * tcp_twstart, then do not discard the newly created tcptw
192 * and leave inpcb present until timewait ends
193 * #2 tcp_detach is called at timewait end (or reuse) by
194 * tcp_twclose, then the tcptw has already been discarded
195 * and inpcb is freed here
196 * #3 tcp_detach is called() after timewait ends (or reuse)
197 * (e.g. by soclose), then tcptw has already been discarded
198 * and inpcb is freed here
200 * In all three cases the tcptw should not be freed here.
202 if (inp->inp_flags & INP_DROPPED) {
203 KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
204 "INP_DROPPED && tp != NULL"));
213 * If the connection is not in timewait, we consider two
214 * two conditions: one in which no further processing is
215 * necessary (dropped || embryonic), and one in which TCP is
216 * not yet done, but no longer requires the socket, so the
217 * pcb will persist for the time being.
219 * XXXRW: Does the second case still occur?
221 if (inp->inp_flags & INP_DROPPED ||
222 tp->t_state < TCPS_SYN_SENT) {
234 * pru_detach() detaches the TCP protocol from the socket.
235 * If the protocol state is non-embryonic, then can't
236 * do this directly: have to initiate a pru_disconnect(),
237 * which may finish later; embryonic TCB's can just
241 tcp_usr_detach(struct socket *so)
246 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
247 INP_INFO_WLOCK(&V_tcbinfo);
249 KASSERT(inp->inp_socket != NULL,
250 ("tcp_usr_detach: inp_socket == NULL"));
252 INP_INFO_WUNLOCK(&V_tcbinfo);
257 * Give the socket an address.
260 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
264 struct tcpcb *tp = NULL;
265 struct sockaddr_in *sinp;
267 sinp = (struct sockaddr_in *)nam;
268 if (nam->sa_len != sizeof (*sinp))
271 * Must check for multicast addresses and disallow binding
274 if (sinp->sin_family == AF_INET &&
275 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
276 return (EAFNOSUPPORT);
280 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
282 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
288 INP_HASH_WLOCK(&V_tcbinfo);
289 error = in_pcbbind(inp, nam, td->td_ucred);
290 INP_HASH_WUNLOCK(&V_tcbinfo);
301 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
305 struct tcpcb *tp = NULL;
306 struct sockaddr_in6 *sin6p;
308 sin6p = (struct sockaddr_in6 *)nam;
309 if (nam->sa_len != sizeof (*sin6p))
312 * Must check for multicast addresses and disallow binding
315 if (sin6p->sin6_family == AF_INET6 &&
316 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
317 return (EAFNOSUPPORT);
321 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
323 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
329 INP_HASH_WLOCK(&V_tcbinfo);
330 inp->inp_vflag &= ~INP_IPV4;
331 inp->inp_vflag |= INP_IPV6;
333 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
334 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
335 inp->inp_vflag |= INP_IPV4;
336 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
337 struct sockaddr_in sin;
339 in6_sin6_2_sin(&sin, sin6p);
340 inp->inp_vflag |= INP_IPV4;
341 inp->inp_vflag &= ~INP_IPV6;
342 error = in_pcbbind(inp, (struct sockaddr *)&sin,
344 INP_HASH_WUNLOCK(&V_tcbinfo);
349 error = in6_pcbbind(inp, nam, td->td_ucred);
350 INP_HASH_WUNLOCK(&V_tcbinfo);
360 * Prepare to accept connections.
363 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
367 struct tcpcb *tp = NULL;
371 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
373 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
380 error = solisten_proto_check(so);
381 INP_HASH_WLOCK(&V_tcbinfo);
382 if (error == 0 && inp->inp_lport == 0)
383 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
384 INP_HASH_WUNLOCK(&V_tcbinfo);
386 tcp_state_change(tp, TCPS_LISTEN);
387 solisten_proto(so, backlog);
389 if ((so->so_options & SO_NO_OFFLOAD) == 0)
390 tcp_offload_listen_start(tp);
396 TCPDEBUG2(PRU_LISTEN);
404 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
408 struct tcpcb *tp = NULL;
412 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
414 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
421 error = solisten_proto_check(so);
422 INP_HASH_WLOCK(&V_tcbinfo);
423 if (error == 0 && inp->inp_lport == 0) {
424 inp->inp_vflag &= ~INP_IPV4;
425 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
426 inp->inp_vflag |= INP_IPV4;
427 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
429 INP_HASH_WUNLOCK(&V_tcbinfo);
431 tcp_state_change(tp, TCPS_LISTEN);
432 solisten_proto(so, backlog);
434 if ((so->so_options & SO_NO_OFFLOAD) == 0)
435 tcp_offload_listen_start(tp);
441 TCPDEBUG2(PRU_LISTEN);
449 * Initiate connection to peer.
450 * Create a template for use in transmissions on this connection.
451 * Enter SYN_SENT state, and mark socket as connecting.
452 * Start keep-alive timer, and seed output sequence space.
453 * Send initial segment on connection.
456 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
460 struct tcpcb *tp = NULL;
461 struct sockaddr_in *sinp;
463 sinp = (struct sockaddr_in *)nam;
464 if (nam->sa_len != sizeof (*sinp))
467 * Must disallow TCP ``connections'' to multicast addresses.
469 if (sinp->sin_family == AF_INET
470 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
471 return (EAFNOSUPPORT);
472 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
477 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
479 if (inp->inp_flags & INP_TIMEWAIT) {
483 if (inp->inp_flags & INP_DROPPED) {
484 error = ECONNREFUSED;
489 if ((error = tcp_connect(tp, nam, td)) != 0)
492 if (registered_toedevs > 0 &&
493 (so->so_options & SO_NO_OFFLOAD) == 0 &&
494 (error = tcp_offload_connect(so, nam)) == 0)
497 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
498 error = tcp_output(tp);
500 TCPDEBUG2(PRU_CONNECT);
508 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
512 struct tcpcb *tp = NULL;
513 struct sockaddr_in6 *sin6p;
517 sin6p = (struct sockaddr_in6 *)nam;
518 if (nam->sa_len != sizeof (*sin6p))
521 * Must disallow TCP ``connections'' to multicast addresses.
523 if (sin6p->sin6_family == AF_INET6
524 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
525 return (EAFNOSUPPORT);
528 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
530 if (inp->inp_flags & INP_TIMEWAIT) {
534 if (inp->inp_flags & INP_DROPPED) {
535 error = ECONNREFUSED;
542 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
543 * therefore probably require the hash lock, which isn't held here.
544 * Is this a significant problem?
546 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
547 struct sockaddr_in sin;
549 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
554 in6_sin6_2_sin(&sin, sin6p);
555 inp->inp_vflag |= INP_IPV4;
556 inp->inp_vflag &= ~INP_IPV6;
557 if ((error = prison_remote_ip4(td->td_ucred,
558 &sin.sin_addr)) != 0)
560 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
563 if (registered_toedevs > 0 &&
564 (so->so_options & SO_NO_OFFLOAD) == 0 &&
565 (error = tcp_offload_connect(so, nam)) == 0)
568 error = tcp_output(tp);
572 inp->inp_vflag &= ~INP_IPV4;
573 inp->inp_vflag |= INP_IPV6;
574 inp->inp_inc.inc_flags |= INC_ISIPV6;
575 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
577 if ((error = tcp6_connect(tp, nam, td)) != 0)
580 if (registered_toedevs > 0 &&
581 (so->so_options & SO_NO_OFFLOAD) == 0 &&
582 (error = tcp_offload_connect(so, nam)) == 0)
585 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
586 error = tcp_output(tp);
589 TCPDEBUG2(PRU_CONNECT);
596 * Initiate disconnect from peer.
597 * If connection never passed embryonic stage, just drop;
598 * else if don't need to let data drain, then can just drop anyways,
599 * else have to begin TCP shutdown process: mark socket disconnecting,
600 * drain unread data, state switch to reflect user close, and
601 * send segment (e.g. FIN) to peer. Socket will be really disconnected
602 * when peer sends FIN and acks ours.
604 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
607 tcp_usr_disconnect(struct socket *so)
610 struct tcpcb *tp = NULL;
614 INP_INFO_WLOCK(&V_tcbinfo);
616 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
618 if (inp->inp_flags & INP_TIMEWAIT)
620 if (inp->inp_flags & INP_DROPPED) {
628 TCPDEBUG2(PRU_DISCONNECT);
630 INP_INFO_WUNLOCK(&V_tcbinfo);
636 * Accept a connection. Essentially all the work is done at higher levels;
637 * just return the address of the peer, storing through addr.
640 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
643 struct inpcb *inp = NULL;
644 struct tcpcb *tp = NULL;
649 if (so->so_state & SS_ISDISCONNECTED)
650 return (ECONNABORTED);
653 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
655 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
656 error = ECONNABORTED;
663 * We inline in_getpeeraddr and COMMON_END here, so that we can
664 * copy the data of interest and defer the malloc until after we
667 port = inp->inp_fport;
668 addr = inp->inp_faddr;
671 TCPDEBUG2(PRU_ACCEPT);
674 *nam = in_sockaddr(port, &addr);
681 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
683 struct inpcb *inp = NULL;
685 struct tcpcb *tp = NULL;
687 struct in6_addr addr6;
692 if (so->so_state & SS_ISDISCONNECTED)
693 return (ECONNABORTED);
696 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
697 INP_INFO_RLOCK(&V_tcbinfo);
699 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
700 error = ECONNABORTED;
707 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
708 * copy the data of interest and defer the malloc until after we
711 if (inp->inp_vflag & INP_IPV4) {
713 port = inp->inp_fport;
714 addr = inp->inp_faddr;
716 port = inp->inp_fport;
717 addr6 = inp->in6p_faddr;
721 TCPDEBUG2(PRU_ACCEPT);
723 INP_INFO_RUNLOCK(&V_tcbinfo);
726 *nam = in6_v4mapsin6_sockaddr(port, &addr);
728 *nam = in6_sockaddr(port, &addr6);
735 * Mark the connection as being incapable of further output.
738 tcp_usr_shutdown(struct socket *so)
742 struct tcpcb *tp = NULL;
745 INP_INFO_WLOCK(&V_tcbinfo);
747 KASSERT(inp != NULL, ("inp == NULL"));
749 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
757 if (!(inp->inp_flags & INP_DROPPED))
758 error = tcp_output(tp);
761 TCPDEBUG2(PRU_SHUTDOWN);
763 INP_INFO_WUNLOCK(&V_tcbinfo);
769 * After a receive, possibly send window update to peer.
772 tcp_usr_rcvd(struct socket *so, int flags)
775 struct tcpcb *tp = NULL;
780 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
782 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
789 if (tp->t_flags & TF_TOE)
790 tcp_offload_rcvd(tp);
802 * Do a send by putting data in output queue and updating urgent
803 * marker if URG set. Possibly send more data. Unlike the other
804 * pru_*() routines, the mbuf chains are our responsibility. We
805 * must either enqueue them or free them. The other pru_* routines
806 * generally are caller-frees.
809 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
810 struct sockaddr *nam, struct mbuf *control, struct thread *td)
814 struct tcpcb *tp = NULL;
821 * We require the pcbinfo lock if we will close the socket as part of
824 if (flags & PRUS_EOF)
825 INP_INFO_WLOCK(&V_tcbinfo);
827 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
829 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
833 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
834 * for freeing memory.
836 if (m && (flags & PRUS_NOTREADY) == 0)
842 isipv6 = nam && nam->sa_family == AF_INET6;
847 /* TCP doesn't do control messages (rights, creds, etc) */
848 if (control->m_len) {
855 m_freem(control); /* empty control, just free it */
857 if (!(flags & PRUS_OOB)) {
858 sbappendstream(&so->so_snd, m, flags);
859 if (nam && tp->t_state < TCPS_SYN_SENT) {
861 * Do implied connect if not yet connected,
862 * initialize window to default value, and
863 * initialize maxseg/maxopd using peer's cached
868 error = tcp6_connect(tp, nam, td);
870 #if defined(INET6) && defined(INET)
874 error = tcp_connect(tp, nam, td);
878 tp->snd_wnd = TTCP_CLIENT_SND_WND;
881 if (flags & PRUS_EOF) {
883 * Close the send side of the connection after
886 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
890 if (!(inp->inp_flags & INP_DROPPED) &&
891 !(flags & PRUS_NOTREADY)) {
892 if (flags & PRUS_MORETOCOME)
893 tp->t_flags |= TF_MORETOCOME;
894 error = tcp_output(tp);
895 if (flags & PRUS_MORETOCOME)
896 tp->t_flags &= ~TF_MORETOCOME;
900 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
902 SOCKBUF_LOCK(&so->so_snd);
903 if (sbspace(&so->so_snd) < -512) {
904 SOCKBUF_UNLOCK(&so->so_snd);
910 * According to RFC961 (Assigned Protocols),
911 * the urgent pointer points to the last octet
912 * of urgent data. We continue, however,
913 * to consider it to indicate the first octet
914 * of data past the urgent section.
915 * Otherwise, snd_up should be one lower.
917 sbappendstream_locked(&so->so_snd, m, flags);
918 SOCKBUF_UNLOCK(&so->so_snd);
919 if (nam && tp->t_state < TCPS_SYN_SENT) {
921 * Do implied connect if not yet connected,
922 * initialize window to default value, and
923 * initialize maxseg/maxopd using peer's cached
928 error = tcp6_connect(tp, nam, td);
930 #if defined(INET6) && defined(INET)
934 error = tcp_connect(tp, nam, td);
938 tp->snd_wnd = TTCP_CLIENT_SND_WND;
941 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
942 if (!(flags & PRUS_NOTREADY)) {
943 tp->t_flags |= TF_FORCEDATA;
944 error = tcp_output(tp);
945 tp->t_flags &= ~TF_FORCEDATA;
949 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
950 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
952 if (flags & PRUS_EOF)
953 INP_INFO_WUNLOCK(&V_tcbinfo);
958 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
966 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
968 for (int i = 0; i < count; i++)
974 SOCKBUF_LOCK(&so->so_snd);
975 error = sbready(&so->so_snd, m, count);
976 SOCKBUF_UNLOCK(&so->so_snd);
978 error = tcp_output(tp);
985 * Abort the TCP. Drop the connection abruptly.
988 tcp_usr_abort(struct socket *so)
991 struct tcpcb *tp = NULL;
995 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
997 INP_INFO_WLOCK(&V_tcbinfo);
999 KASSERT(inp->inp_socket != NULL,
1000 ("tcp_usr_abort: inp_socket == NULL"));
1003 * If we still have full TCP state, and we're not dropped, drop.
1005 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1006 !(inp->inp_flags & INP_DROPPED)) {
1007 tp = intotcpcb(inp);
1009 tcp_drop(tp, ECONNABORTED);
1010 TCPDEBUG2(PRU_ABORT);
1012 if (!(inp->inp_flags & INP_DROPPED)) {
1014 so->so_state |= SS_PROTOREF;
1016 inp->inp_flags |= INP_SOCKREF;
1019 INP_INFO_WUNLOCK(&V_tcbinfo);
1023 * TCP socket is closed. Start friendly disconnect.
1026 tcp_usr_close(struct socket *so)
1029 struct tcpcb *tp = NULL;
1032 inp = sotoinpcb(so);
1033 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1035 INP_INFO_WLOCK(&V_tcbinfo);
1037 KASSERT(inp->inp_socket != NULL,
1038 ("tcp_usr_close: inp_socket == NULL"));
1041 * If we still have full TCP state, and we're not dropped, initiate
1044 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1045 !(inp->inp_flags & INP_DROPPED)) {
1046 tp = intotcpcb(inp);
1049 TCPDEBUG2(PRU_CLOSE);
1051 if (!(inp->inp_flags & INP_DROPPED)) {
1053 so->so_state |= SS_PROTOREF;
1055 inp->inp_flags |= INP_SOCKREF;
1058 INP_INFO_WUNLOCK(&V_tcbinfo);
1062 * Receive out-of-band data.
1065 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1069 struct tcpcb *tp = NULL;
1072 inp = sotoinpcb(so);
1073 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1075 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1079 tp = intotcpcb(inp);
1081 if ((so->so_oobmark == 0 &&
1082 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1083 so->so_options & SO_OOBINLINE ||
1084 tp->t_oobflags & TCPOOB_HADDATA) {
1088 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1089 error = EWOULDBLOCK;
1093 *mtod(m, caddr_t) = tp->t_iobc;
1094 if ((flags & MSG_PEEK) == 0)
1095 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1098 TCPDEBUG2(PRU_RCVOOB);
1104 struct pr_usrreqs tcp_usrreqs = {
1105 .pru_abort = tcp_usr_abort,
1106 .pru_accept = tcp_usr_accept,
1107 .pru_attach = tcp_usr_attach,
1108 .pru_bind = tcp_usr_bind,
1109 .pru_connect = tcp_usr_connect,
1110 .pru_control = in_control,
1111 .pru_detach = tcp_usr_detach,
1112 .pru_disconnect = tcp_usr_disconnect,
1113 .pru_listen = tcp_usr_listen,
1114 .pru_peeraddr = in_getpeeraddr,
1115 .pru_rcvd = tcp_usr_rcvd,
1116 .pru_rcvoob = tcp_usr_rcvoob,
1117 .pru_send = tcp_usr_send,
1118 .pru_ready = tcp_usr_ready,
1119 .pru_shutdown = tcp_usr_shutdown,
1120 .pru_sockaddr = in_getsockaddr,
1121 .pru_sosetlabel = in_pcbsosetlabel,
1122 .pru_close = tcp_usr_close,
1127 struct pr_usrreqs tcp6_usrreqs = {
1128 .pru_abort = tcp_usr_abort,
1129 .pru_accept = tcp6_usr_accept,
1130 .pru_attach = tcp_usr_attach,
1131 .pru_bind = tcp6_usr_bind,
1132 .pru_connect = tcp6_usr_connect,
1133 .pru_control = in6_control,
1134 .pru_detach = tcp_usr_detach,
1135 .pru_disconnect = tcp_usr_disconnect,
1136 .pru_listen = tcp6_usr_listen,
1137 .pru_peeraddr = in6_mapped_peeraddr,
1138 .pru_rcvd = tcp_usr_rcvd,
1139 .pru_rcvoob = tcp_usr_rcvoob,
1140 .pru_send = tcp_usr_send,
1141 .pru_ready = tcp_usr_ready,
1142 .pru_shutdown = tcp_usr_shutdown,
1143 .pru_sockaddr = in6_mapped_sockaddr,
1144 .pru_sosetlabel = in_pcbsosetlabel,
1145 .pru_close = tcp_usr_close,
1151 * Common subroutine to open a TCP connection to remote host specified
1152 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1153 * port number if needed. Call in_pcbconnect_setup to do the routing and
1154 * to choose a local host address (interface). If there is an existing
1155 * incarnation of the same connection in TIME-WAIT state and if the remote
1156 * host was sending CC options and if the connection duration was < MSL, then
1157 * truncate the previous TIME-WAIT state and proceed.
1158 * Initialize connection parameters and enter SYN-SENT state.
1161 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1163 struct inpcb *inp = tp->t_inpcb, *oinp;
1164 struct socket *so = inp->inp_socket;
1165 struct in_addr laddr;
1169 INP_WLOCK_ASSERT(inp);
1170 INP_HASH_WLOCK(&V_tcbinfo);
1172 if (inp->inp_lport == 0) {
1173 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1179 * Cannot simply call in_pcbconnect, because there might be an
1180 * earlier incarnation of this same connection still in
1181 * TIME_WAIT state, creating an ADDRINUSE error.
1183 laddr = inp->inp_laddr;
1184 lport = inp->inp_lport;
1185 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1186 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1187 if (error && oinp == NULL)
1193 inp->inp_laddr = laddr;
1195 INP_HASH_WUNLOCK(&V_tcbinfo);
1198 * Compute window scaling to request:
1199 * Scale to fit into sweet spot. See tcp_syncache.c.
1200 * XXX: This should move to tcp_output().
1202 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1203 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1204 tp->request_r_scale++;
1207 TCPSTAT_INC(tcps_connattempt);
1208 tcp_state_change(tp, TCPS_SYN_SENT);
1209 tp->iss = tcp_new_isn(tp);
1210 tcp_sendseqinit(tp);
1215 INP_HASH_WUNLOCK(&V_tcbinfo);
1222 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1224 struct inpcb *inp = tp->t_inpcb;
1227 INP_WLOCK_ASSERT(inp);
1228 INP_HASH_WLOCK(&V_tcbinfo);
1230 if (inp->inp_lport == 0) {
1231 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1235 error = in6_pcbconnect(inp, nam, td->td_ucred);
1238 INP_HASH_WUNLOCK(&V_tcbinfo);
1240 /* Compute window scaling to request. */
1241 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1242 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1243 tp->request_r_scale++;
1245 soisconnecting(inp->inp_socket);
1246 TCPSTAT_INC(tcps_connattempt);
1247 tcp_state_change(tp, TCPS_SYN_SENT);
1248 tp->iss = tcp_new_isn(tp);
1249 tcp_sendseqinit(tp);
1254 INP_HASH_WUNLOCK(&V_tcbinfo);
1260 * Export TCP internal state information via a struct tcp_info, based on the
1261 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1262 * (TCP state machine, etc). We export all information using FreeBSD-native
1263 * constants -- for example, the numeric values for tcpi_state will differ
1267 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1270 INP_WLOCK_ASSERT(tp->t_inpcb);
1271 bzero(ti, sizeof(*ti));
1273 ti->tcpi_state = tp->t_state;
1274 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1275 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1276 if (tp->t_flags & TF_SACK_PERMIT)
1277 ti->tcpi_options |= TCPI_OPT_SACK;
1278 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1279 ti->tcpi_options |= TCPI_OPT_WSCALE;
1280 ti->tcpi_snd_wscale = tp->snd_scale;
1281 ti->tcpi_rcv_wscale = tp->rcv_scale;
1284 ti->tcpi_rto = tp->t_rxtcur * tick;
1285 ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
1286 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1287 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1289 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1290 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1293 * FreeBSD-specific extension fields for tcp_info.
1295 ti->tcpi_rcv_space = tp->rcv_wnd;
1296 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1297 ti->tcpi_snd_wnd = tp->snd_wnd;
1298 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1299 ti->tcpi_snd_nxt = tp->snd_nxt;
1300 ti->tcpi_snd_mss = tp->t_maxseg;
1301 ti->tcpi_rcv_mss = tp->t_maxseg;
1302 if (tp->t_flags & TF_TOE)
1303 ti->tcpi_options |= TCPI_OPT_TOE;
1304 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1305 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1306 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1310 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1311 * socket option arguments. When it re-acquires the lock after the copy, it
1312 * has to revalidate that the connection is still valid for the socket
1315 #define INP_WLOCK_RECHECK(inp) do { \
1317 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1319 return (ECONNRESET); \
1321 tp = intotcpcb(inp); \
1325 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1327 int error, opt, optval;
1332 char buf[TCP_CA_NAME_MAX];
1333 struct cc_algo *algo;
1336 inp = sotoinpcb(so);
1337 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1339 if (sopt->sopt_level != IPPROTO_TCP) {
1341 if (inp->inp_vflag & INP_IPV6PROTO) {
1343 error = ip6_ctloutput(so, sopt);
1346 #if defined(INET6) && defined(INET)
1352 error = ip_ctloutput(so, sopt);
1357 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1359 return (ECONNRESET);
1362 switch (sopt->sopt_dir) {
1364 switch (sopt->sopt_name) {
1365 #ifdef TCP_SIGNATURE
1368 error = sooptcopyin(sopt, &optval, sizeof optval,
1373 INP_WLOCK_RECHECK(inp);
1375 tp->t_flags |= TF_SIGNATURE;
1377 tp->t_flags &= ~TF_SIGNATURE;
1378 goto unlock_and_done;
1379 #endif /* TCP_SIGNATURE */
1384 error = sooptcopyin(sopt, &optval, sizeof optval,
1389 INP_WLOCK_RECHECK(inp);
1390 switch (sopt->sopt_name) {
1398 opt = 0; /* dead code to fool gcc */
1405 tp->t_flags &= ~opt;
1408 if (tp->t_flags & TF_TOE) {
1409 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1418 error = sooptcopyin(sopt, &optval, sizeof optval,
1423 INP_WLOCK_RECHECK(inp);
1425 tp->t_flags |= TF_NOPUSH;
1426 else if (tp->t_flags & TF_NOPUSH) {
1427 tp->t_flags &= ~TF_NOPUSH;
1428 if (TCPS_HAVEESTABLISHED(tp->t_state))
1429 error = tcp_output(tp);
1431 goto unlock_and_done;
1435 error = sooptcopyin(sopt, &optval, sizeof optval,
1440 INP_WLOCK_RECHECK(inp);
1441 if (optval > 0 && optval <= tp->t_maxseg &&
1442 optval + 40 >= V_tcp_minmss)
1443 tp->t_maxseg = optval;
1446 goto unlock_and_done;
1453 case TCP_CONGESTION:
1455 bzero(buf, sizeof(buf));
1456 error = sooptcopyin(sopt, &buf, sizeof(buf), 1);
1459 INP_WLOCK_RECHECK(inp);
1461 * Return EINVAL if we can't find the requested cc algo.
1465 STAILQ_FOREACH(algo, &cc_list, entries) {
1466 if (strncmp(buf, algo->name, TCP_CA_NAME_MAX)
1468 /* We've found the requested algo. */
1471 * We hold a write lock over the tcb
1472 * so it's safe to do these things
1473 * without ordering concerns.
1475 if (CC_ALGO(tp)->cb_destroy != NULL)
1476 CC_ALGO(tp)->cb_destroy(tp->ccv);
1479 * If something goes pear shaped
1480 * initialising the new algo,
1481 * fall back to newreno (which
1482 * does not require initialisation).
1484 if (algo->cb_init != NULL)
1485 if (algo->cb_init(tp->ccv) > 0) {
1486 CC_ALGO(tp) = &newreno_cc_algo;
1488 * The only reason init
1490 * because of malloc.
1494 break; /* Break the STAILQ_FOREACH. */
1498 goto unlock_and_done;
1504 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1508 if (ui > (UINT_MAX / hz)) {
1514 INP_WLOCK_RECHECK(inp);
1515 switch (sopt->sopt_name) {
1517 tp->t_keepidle = ui;
1519 * XXX: better check current remaining
1520 * timeout and "merge" it with new value.
1522 if ((tp->t_state > TCPS_LISTEN) &&
1523 (tp->t_state <= TCPS_CLOSING))
1524 tcp_timer_activate(tp, TT_KEEP,
1528 tp->t_keepintvl = ui;
1529 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1530 (TP_MAXIDLE(tp) > 0))
1531 tcp_timer_activate(tp, TT_2MSL,
1535 tp->t_keepinit = ui;
1536 if (tp->t_state == TCPS_SYN_RECEIVED ||
1537 tp->t_state == TCPS_SYN_SENT)
1538 tcp_timer_activate(tp, TT_KEEP,
1542 goto unlock_and_done;
1546 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1550 INP_WLOCK_RECHECK(inp);
1552 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1553 (TP_MAXIDLE(tp) > 0))
1554 tcp_timer_activate(tp, TT_2MSL,
1556 goto unlock_and_done;
1560 error = ENOPROTOOPT;
1566 tp = intotcpcb(inp);
1567 switch (sopt->sopt_name) {
1568 #ifdef TCP_SIGNATURE
1570 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1572 error = sooptcopyout(sopt, &optval, sizeof optval);
1577 optval = tp->t_flags & TF_NODELAY;
1579 error = sooptcopyout(sopt, &optval, sizeof optval);
1582 optval = tp->t_maxseg;
1584 error = sooptcopyout(sopt, &optval, sizeof optval);
1587 optval = tp->t_flags & TF_NOOPT;
1589 error = sooptcopyout(sopt, &optval, sizeof optval);
1592 optval = tp->t_flags & TF_NOPUSH;
1594 error = sooptcopyout(sopt, &optval, sizeof optval);
1597 tcp_fill_info(tp, &ti);
1599 error = sooptcopyout(sopt, &ti, sizeof ti);
1601 case TCP_CONGESTION:
1602 bzero(buf, sizeof(buf));
1603 strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1605 error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX);
1611 switch (sopt->sopt_name) {
1613 ui = tp->t_keepidle / hz;
1616 ui = tp->t_keepintvl / hz;
1619 ui = tp->t_keepinit / hz;
1626 error = sooptcopyout(sopt, &ui, sizeof(ui));
1630 error = ENOPROTOOPT;
1637 #undef INP_WLOCK_RECHECK
1640 * Attach TCP protocol to socket, allocating
1641 * internet protocol control block, tcp control block,
1642 * bufer space, and entering LISTEN state if to accept connections.
1645 tcp_attach(struct socket *so)
1651 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1652 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1656 so->so_rcv.sb_flags |= SB_AUTOSIZE;
1657 so->so_snd.sb_flags |= SB_AUTOSIZE;
1658 INP_INFO_WLOCK(&V_tcbinfo);
1659 error = in_pcballoc(so, &V_tcbinfo);
1661 INP_INFO_WUNLOCK(&V_tcbinfo);
1664 inp = sotoinpcb(so);
1666 if (inp->inp_vflag & INP_IPV6PROTO) {
1667 inp->inp_vflag |= INP_IPV6;
1668 inp->in6p_hops = -1; /* use kernel default */
1672 inp->inp_vflag |= INP_IPV4;
1673 tp = tcp_newtcpcb(inp);
1677 INP_INFO_WUNLOCK(&V_tcbinfo);
1680 tp->t_state = TCPS_CLOSED;
1682 INP_INFO_WUNLOCK(&V_tcbinfo);
1687 * Initiate (or continue) disconnect.
1688 * If embryonic state, just send reset (once).
1689 * If in ``let data drain'' option and linger null, just drop.
1690 * Otherwise (hard), mark socket disconnecting and drop
1691 * current input data; switch states based on user close, and
1692 * send segment to peer (with FIN).
1695 tcp_disconnect(struct tcpcb *tp)
1697 struct inpcb *inp = tp->t_inpcb;
1698 struct socket *so = inp->inp_socket;
1700 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1701 INP_WLOCK_ASSERT(inp);
1704 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1705 * socket is still open.
1707 if (tp->t_state < TCPS_ESTABLISHED) {
1710 ("tcp_disconnect: tcp_close() returned NULL"));
1711 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1712 tp = tcp_drop(tp, 0);
1714 ("tcp_disconnect: tcp_drop() returned NULL"));
1716 soisdisconnecting(so);
1717 sbflush(&so->so_rcv);
1719 if (!(inp->inp_flags & INP_DROPPED))
1725 * User issued close, and wish to trail through shutdown states:
1726 * if never received SYN, just forget it. If got a SYN from peer,
1727 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1728 * If already got a FIN from peer, then almost done; go to LAST_ACK
1729 * state. In all other cases, have already sent FIN to peer (e.g.
1730 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1731 * for peer to send FIN or not respond to keep-alives, etc.
1732 * We can let the user exit from the close as soon as the FIN is acked.
1735 tcp_usrclosed(struct tcpcb *tp)
1738 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1739 INP_WLOCK_ASSERT(tp->t_inpcb);
1741 switch (tp->t_state) {
1744 tcp_offload_listen_stop(tp);
1748 tcp_state_change(tp, TCPS_CLOSED);
1751 * tcp_close() should never return NULL here as the socket is
1755 ("tcp_usrclosed: tcp_close() returned NULL"));
1759 case TCPS_SYN_RECEIVED:
1760 tp->t_flags |= TF_NEEDFIN;
1763 case TCPS_ESTABLISHED:
1764 tcp_state_change(tp, TCPS_FIN_WAIT_1);
1767 case TCPS_CLOSE_WAIT:
1768 tcp_state_change(tp, TCPS_LAST_ACK);
1771 if (tp->t_state >= TCPS_FIN_WAIT_2) {
1772 soisdisconnected(tp->t_inpcb->inp_socket);
1773 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
1774 if (tp->t_state == TCPS_FIN_WAIT_2) {
1777 timeout = (tcp_fast_finwait2_recycle) ?
1778 tcp_finwait2_timeout : TP_MAXIDLE(tp);
1779 tcp_timer_activate(tp, TT_2MSL, timeout);
1786 db_print_indent(int indent)
1790 for (i = 0; i < indent; i++)
1795 db_print_tstate(int t_state)
1800 db_printf("TCPS_CLOSED");
1804 db_printf("TCPS_LISTEN");
1808 db_printf("TCPS_SYN_SENT");
1811 case TCPS_SYN_RECEIVED:
1812 db_printf("TCPS_SYN_RECEIVED");
1815 case TCPS_ESTABLISHED:
1816 db_printf("TCPS_ESTABLISHED");
1819 case TCPS_CLOSE_WAIT:
1820 db_printf("TCPS_CLOSE_WAIT");
1823 case TCPS_FIN_WAIT_1:
1824 db_printf("TCPS_FIN_WAIT_1");
1828 db_printf("TCPS_CLOSING");
1832 db_printf("TCPS_LAST_ACK");
1835 case TCPS_FIN_WAIT_2:
1836 db_printf("TCPS_FIN_WAIT_2");
1839 case TCPS_TIME_WAIT:
1840 db_printf("TCPS_TIME_WAIT");
1844 db_printf("unknown");
1850 db_print_tflags(u_int t_flags)
1855 if (t_flags & TF_ACKNOW) {
1856 db_printf("%sTF_ACKNOW", comma ? ", " : "");
1859 if (t_flags & TF_DELACK) {
1860 db_printf("%sTF_DELACK", comma ? ", " : "");
1863 if (t_flags & TF_NODELAY) {
1864 db_printf("%sTF_NODELAY", comma ? ", " : "");
1867 if (t_flags & TF_NOOPT) {
1868 db_printf("%sTF_NOOPT", comma ? ", " : "");
1871 if (t_flags & TF_SENTFIN) {
1872 db_printf("%sTF_SENTFIN", comma ? ", " : "");
1875 if (t_flags & TF_REQ_SCALE) {
1876 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1879 if (t_flags & TF_RCVD_SCALE) {
1880 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1883 if (t_flags & TF_REQ_TSTMP) {
1884 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1887 if (t_flags & TF_RCVD_TSTMP) {
1888 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1891 if (t_flags & TF_SACK_PERMIT) {
1892 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1895 if (t_flags & TF_NEEDSYN) {
1896 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1899 if (t_flags & TF_NEEDFIN) {
1900 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1903 if (t_flags & TF_NOPUSH) {
1904 db_printf("%sTF_NOPUSH", comma ? ", " : "");
1907 if (t_flags & TF_MORETOCOME) {
1908 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1911 if (t_flags & TF_LQ_OVERFLOW) {
1912 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1915 if (t_flags & TF_LASTIDLE) {
1916 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1919 if (t_flags & TF_RXWIN0SENT) {
1920 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1923 if (t_flags & TF_FASTRECOVERY) {
1924 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1927 if (t_flags & TF_CONGRECOVERY) {
1928 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
1931 if (t_flags & TF_WASFRECOVERY) {
1932 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1935 if (t_flags & TF_SIGNATURE) {
1936 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1939 if (t_flags & TF_FORCEDATA) {
1940 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1943 if (t_flags & TF_TSO) {
1944 db_printf("%sTF_TSO", comma ? ", " : "");
1947 if (t_flags & TF_ECN_PERMIT) {
1948 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
1954 db_print_toobflags(char t_oobflags)
1959 if (t_oobflags & TCPOOB_HAVEDATA) {
1960 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1963 if (t_oobflags & TCPOOB_HADDATA) {
1964 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1970 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1973 db_print_indent(indent);
1974 db_printf("%s at %p\n", name, tp);
1978 db_print_indent(indent);
1979 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
1980 tp->t_segq, tp->t_segqlen, tp->t_dupacks);
1982 db_print_indent(indent);
1983 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
1984 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
1986 db_print_indent(indent);
1987 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
1988 &tp->t_timers->tt_delack, tp->t_inpcb);
1990 db_print_indent(indent);
1991 db_printf("t_state: %d (", tp->t_state);
1992 db_print_tstate(tp->t_state);
1995 db_print_indent(indent);
1996 db_printf("t_flags: 0x%x (", tp->t_flags);
1997 db_print_tflags(tp->t_flags);
2000 db_print_indent(indent);
2001 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2002 tp->snd_una, tp->snd_max, tp->snd_nxt);
2004 db_print_indent(indent);
2005 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2006 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2008 db_print_indent(indent);
2009 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2010 tp->iss, tp->irs, tp->rcv_nxt);
2012 db_print_indent(indent);
2013 db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n",
2014 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2016 db_print_indent(indent);
2017 db_printf("snd_wnd: %lu snd_cwnd: %lu\n",
2018 tp->snd_wnd, tp->snd_cwnd);
2020 db_print_indent(indent);
2021 db_printf("snd_ssthresh: %lu snd_recover: "
2022 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2024 db_print_indent(indent);
2025 db_printf("t_maxopd: %u t_rcvtime: %u t_startime: %u\n",
2026 tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
2028 db_print_indent(indent);
2029 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2030 tp->t_rtttime, tp->t_rtseq);
2032 db_print_indent(indent);
2033 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2034 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2036 db_print_indent(indent);
2037 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2038 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2041 db_print_indent(indent);
2042 db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n",
2043 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2045 db_print_indent(indent);
2046 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2047 db_print_toobflags(tp->t_oobflags);
2048 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2050 db_print_indent(indent);
2051 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2052 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2054 db_print_indent(indent);
2055 db_printf("ts_recent: %u ts_recent_age: %u\n",
2056 tp->ts_recent, tp->ts_recent_age);
2058 db_print_indent(indent);
2059 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2060 "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2062 db_print_indent(indent);
2063 db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x "
2064 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2065 tp->snd_recover_prev, tp->t_badrxtwin);
2067 db_print_indent(indent);
2068 db_printf("snd_numholes: %d snd_holes first: %p\n",
2069 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2071 db_print_indent(indent);
2072 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
2073 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2075 /* Skip sackblks, sackhint. */
2077 db_print_indent(indent);
2078 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2079 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2082 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2087 db_printf("usage: show tcpcb <addr>\n");
2090 tp = (struct tcpcb *)addr;
2092 db_print_tcpcb(tp, "tcpcb", 0);