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_LOCK_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 * (or reused) 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 * (or reused) 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)
247 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
248 if (!INP_INFO_WLOCKED(&V_tcbinfo)) {
249 INP_INFO_RLOCK(&V_tcbinfo);
253 KASSERT(inp->inp_socket != NULL,
254 ("tcp_usr_detach: inp_socket == NULL"));
257 INP_INFO_RUNLOCK(&V_tcbinfo);
262 * Give the socket an address.
265 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
269 struct tcpcb *tp = NULL;
270 struct sockaddr_in *sinp;
272 sinp = (struct sockaddr_in *)nam;
273 if (nam->sa_len != sizeof (*sinp))
276 * Must check for multicast addresses and disallow binding
279 if (sinp->sin_family == AF_INET &&
280 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
281 return (EAFNOSUPPORT);
285 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
287 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
293 INP_HASH_WLOCK(&V_tcbinfo);
294 error = in_pcbbind(inp, nam, td->td_ucred);
295 INP_HASH_WUNLOCK(&V_tcbinfo);
306 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
310 struct tcpcb *tp = NULL;
311 struct sockaddr_in6 *sin6p;
313 sin6p = (struct sockaddr_in6 *)nam;
314 if (nam->sa_len != sizeof (*sin6p))
317 * Must check for multicast addresses and disallow binding
320 if (sin6p->sin6_family == AF_INET6 &&
321 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
322 return (EAFNOSUPPORT);
326 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
328 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
334 INP_HASH_WLOCK(&V_tcbinfo);
335 inp->inp_vflag &= ~INP_IPV4;
336 inp->inp_vflag |= INP_IPV6;
338 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
339 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
340 inp->inp_vflag |= INP_IPV4;
341 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
342 struct sockaddr_in sin;
344 in6_sin6_2_sin(&sin, sin6p);
345 inp->inp_vflag |= INP_IPV4;
346 inp->inp_vflag &= ~INP_IPV6;
347 error = in_pcbbind(inp, (struct sockaddr *)&sin,
349 INP_HASH_WUNLOCK(&V_tcbinfo);
354 error = in6_pcbbind(inp, nam, td->td_ucred);
355 INP_HASH_WUNLOCK(&V_tcbinfo);
365 * Prepare to accept connections.
368 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
372 struct tcpcb *tp = NULL;
376 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
378 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
385 error = solisten_proto_check(so);
386 INP_HASH_WLOCK(&V_tcbinfo);
387 if (error == 0 && inp->inp_lport == 0)
388 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
389 INP_HASH_WUNLOCK(&V_tcbinfo);
391 tcp_state_change(tp, TCPS_LISTEN);
392 solisten_proto(so, backlog);
394 if ((so->so_options & SO_NO_OFFLOAD) == 0)
395 tcp_offload_listen_start(tp);
401 TCPDEBUG2(PRU_LISTEN);
409 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
413 struct tcpcb *tp = NULL;
417 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
419 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
426 error = solisten_proto_check(so);
427 INP_HASH_WLOCK(&V_tcbinfo);
428 if (error == 0 && inp->inp_lport == 0) {
429 inp->inp_vflag &= ~INP_IPV4;
430 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
431 inp->inp_vflag |= INP_IPV4;
432 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
434 INP_HASH_WUNLOCK(&V_tcbinfo);
436 tcp_state_change(tp, TCPS_LISTEN);
437 solisten_proto(so, backlog);
439 if ((so->so_options & SO_NO_OFFLOAD) == 0)
440 tcp_offload_listen_start(tp);
446 TCPDEBUG2(PRU_LISTEN);
454 * Initiate connection to peer.
455 * Create a template for use in transmissions on this connection.
456 * Enter SYN_SENT state, and mark socket as connecting.
457 * Start keep-alive timer, and seed output sequence space.
458 * Send initial segment on connection.
461 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
465 struct tcpcb *tp = NULL;
466 struct sockaddr_in *sinp;
468 sinp = (struct sockaddr_in *)nam;
469 if (nam->sa_len != sizeof (*sinp))
472 * Must disallow TCP ``connections'' to multicast addresses.
474 if (sinp->sin_family == AF_INET
475 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
476 return (EAFNOSUPPORT);
477 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
482 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
484 if (inp->inp_flags & INP_TIMEWAIT) {
488 if (inp->inp_flags & INP_DROPPED) {
489 error = ECONNREFUSED;
494 if ((error = tcp_connect(tp, nam, td)) != 0)
497 if (registered_toedevs > 0 &&
498 (so->so_options & SO_NO_OFFLOAD) == 0 &&
499 (error = tcp_offload_connect(so, nam)) == 0)
502 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
503 error = tcp_output(tp);
505 TCPDEBUG2(PRU_CONNECT);
513 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
517 struct tcpcb *tp = NULL;
518 struct sockaddr_in6 *sin6p;
522 sin6p = (struct sockaddr_in6 *)nam;
523 if (nam->sa_len != sizeof (*sin6p))
526 * Must disallow TCP ``connections'' to multicast addresses.
528 if (sin6p->sin6_family == AF_INET6
529 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
530 return (EAFNOSUPPORT);
533 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
535 if (inp->inp_flags & INP_TIMEWAIT) {
539 if (inp->inp_flags & INP_DROPPED) {
540 error = ECONNREFUSED;
547 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
548 * therefore probably require the hash lock, which isn't held here.
549 * Is this a significant problem?
551 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
552 struct sockaddr_in sin;
554 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
559 in6_sin6_2_sin(&sin, sin6p);
560 inp->inp_vflag |= INP_IPV4;
561 inp->inp_vflag &= ~INP_IPV6;
562 if ((error = prison_remote_ip4(td->td_ucred,
563 &sin.sin_addr)) != 0)
565 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
568 if (registered_toedevs > 0 &&
569 (so->so_options & SO_NO_OFFLOAD) == 0 &&
570 (error = tcp_offload_connect(so, nam)) == 0)
573 error = tcp_output(tp);
577 inp->inp_vflag &= ~INP_IPV4;
578 inp->inp_vflag |= INP_IPV6;
579 inp->inp_inc.inc_flags |= INC_ISIPV6;
580 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
582 if ((error = tcp6_connect(tp, nam, td)) != 0)
585 if (registered_toedevs > 0 &&
586 (so->so_options & SO_NO_OFFLOAD) == 0 &&
587 (error = tcp_offload_connect(so, nam)) == 0)
590 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
591 error = tcp_output(tp);
594 TCPDEBUG2(PRU_CONNECT);
601 * Initiate disconnect from peer.
602 * If connection never passed embryonic stage, just drop;
603 * else if don't need to let data drain, then can just drop anyways,
604 * else have to begin TCP shutdown process: mark socket disconnecting,
605 * drain unread data, state switch to reflect user close, and
606 * send segment (e.g. FIN) to peer. Socket will be really disconnected
607 * when peer sends FIN and acks ours.
609 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
612 tcp_usr_disconnect(struct socket *so)
615 struct tcpcb *tp = NULL;
619 INP_INFO_RLOCK(&V_tcbinfo);
621 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
623 if (inp->inp_flags & INP_TIMEWAIT)
625 if (inp->inp_flags & INP_DROPPED) {
633 TCPDEBUG2(PRU_DISCONNECT);
635 INP_INFO_RUNLOCK(&V_tcbinfo);
641 * Accept a connection. Essentially all the work is done at higher levels;
642 * just return the address of the peer, storing through addr.
645 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
648 struct inpcb *inp = NULL;
649 struct tcpcb *tp = NULL;
654 if (so->so_state & SS_ISDISCONNECTED)
655 return (ECONNABORTED);
658 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
660 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
661 error = ECONNABORTED;
668 * We inline in_getpeeraddr and COMMON_END here, so that we can
669 * copy the data of interest and defer the malloc until after we
672 port = inp->inp_fport;
673 addr = inp->inp_faddr;
676 TCPDEBUG2(PRU_ACCEPT);
679 *nam = in_sockaddr(port, &addr);
686 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
688 struct inpcb *inp = NULL;
690 struct tcpcb *tp = NULL;
692 struct in6_addr addr6;
697 if (so->so_state & SS_ISDISCONNECTED)
698 return (ECONNABORTED);
701 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
702 INP_INFO_RLOCK(&V_tcbinfo);
704 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
705 error = ECONNABORTED;
712 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
713 * copy the data of interest and defer the malloc until after we
716 if (inp->inp_vflag & INP_IPV4) {
718 port = inp->inp_fport;
719 addr = inp->inp_faddr;
721 port = inp->inp_fport;
722 addr6 = inp->in6p_faddr;
726 TCPDEBUG2(PRU_ACCEPT);
728 INP_INFO_RUNLOCK(&V_tcbinfo);
731 *nam = in6_v4mapsin6_sockaddr(port, &addr);
733 *nam = in6_sockaddr(port, &addr6);
740 * Mark the connection as being incapable of further output.
743 tcp_usr_shutdown(struct socket *so)
747 struct tcpcb *tp = NULL;
750 INP_INFO_RLOCK(&V_tcbinfo);
752 KASSERT(inp != NULL, ("inp == NULL"));
754 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
762 if (!(inp->inp_flags & INP_DROPPED))
763 error = tcp_output(tp);
766 TCPDEBUG2(PRU_SHUTDOWN);
768 INP_INFO_RUNLOCK(&V_tcbinfo);
774 * After a receive, possibly send window update to peer.
777 tcp_usr_rcvd(struct socket *so, int flags)
780 struct tcpcb *tp = NULL;
785 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
787 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
794 if (tp->t_flags & TF_TOE)
795 tcp_offload_rcvd(tp);
807 * Do a send by putting data in output queue and updating urgent
808 * marker if URG set. Possibly send more data. Unlike the other
809 * pru_*() routines, the mbuf chains are our responsibility. We
810 * must either enqueue them or free them. The other pru_* routines
811 * generally are caller-frees.
814 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
815 struct sockaddr *nam, struct mbuf *control, struct thread *td)
819 struct tcpcb *tp = NULL;
826 * We require the pcbinfo lock if we will close the socket as part of
829 if (flags & PRUS_EOF)
830 INP_INFO_RLOCK(&V_tcbinfo);
832 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
834 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
838 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
839 * for freeing memory.
841 if (m && (flags & PRUS_NOTREADY) == 0)
847 isipv6 = nam && nam->sa_family == AF_INET6;
852 /* TCP doesn't do control messages (rights, creds, etc) */
853 if (control->m_len) {
860 m_freem(control); /* empty control, just free it */
862 if (!(flags & PRUS_OOB)) {
863 sbappendstream(&so->so_snd, m, flags);
864 if (nam && tp->t_state < TCPS_SYN_SENT) {
866 * Do implied connect if not yet connected,
867 * initialize window to default value, and
868 * initialize maxseg/maxopd using peer's cached
873 error = tcp6_connect(tp, nam, td);
875 #if defined(INET6) && defined(INET)
879 error = tcp_connect(tp, nam, td);
883 tp->snd_wnd = TTCP_CLIENT_SND_WND;
886 if (flags & PRUS_EOF) {
888 * Close the send side of the connection after
891 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
895 if (!(inp->inp_flags & INP_DROPPED) &&
896 !(flags & PRUS_NOTREADY)) {
897 if (flags & PRUS_MORETOCOME)
898 tp->t_flags |= TF_MORETOCOME;
899 error = tcp_output(tp);
900 if (flags & PRUS_MORETOCOME)
901 tp->t_flags &= ~TF_MORETOCOME;
905 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
907 SOCKBUF_LOCK(&so->so_snd);
908 if (sbspace(&so->so_snd) < -512) {
909 SOCKBUF_UNLOCK(&so->so_snd);
915 * According to RFC961 (Assigned Protocols),
916 * the urgent pointer points to the last octet
917 * of urgent data. We continue, however,
918 * to consider it to indicate the first octet
919 * of data past the urgent section.
920 * Otherwise, snd_up should be one lower.
922 sbappendstream_locked(&so->so_snd, m, flags);
923 SOCKBUF_UNLOCK(&so->so_snd);
924 if (nam && tp->t_state < TCPS_SYN_SENT) {
926 * Do implied connect if not yet connected,
927 * initialize window to default value, and
928 * initialize maxseg/maxopd using peer's cached
933 error = tcp6_connect(tp, nam, td);
935 #if defined(INET6) && defined(INET)
939 error = tcp_connect(tp, nam, td);
943 tp->snd_wnd = TTCP_CLIENT_SND_WND;
946 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
947 if (!(flags & PRUS_NOTREADY)) {
948 tp->t_flags |= TF_FORCEDATA;
949 error = tcp_output(tp);
950 tp->t_flags &= ~TF_FORCEDATA;
954 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
955 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
957 if (flags & PRUS_EOF)
958 INP_INFO_RUNLOCK(&V_tcbinfo);
963 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
971 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
973 for (int i = 0; i < count; i++)
979 SOCKBUF_LOCK(&so->so_snd);
980 error = sbready(&so->so_snd, m, count);
981 SOCKBUF_UNLOCK(&so->so_snd);
983 error = tcp_output(tp);
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_RLOCK(&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_RUNLOCK(&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_RLOCK(&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_RUNLOCK(&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_ready = tcp_usr_ready,
1124 .pru_shutdown = tcp_usr_shutdown,
1125 .pru_sockaddr = in_getsockaddr,
1126 .pru_sosetlabel = in_pcbsosetlabel,
1127 .pru_close = tcp_usr_close,
1132 struct pr_usrreqs tcp6_usrreqs = {
1133 .pru_abort = tcp_usr_abort,
1134 .pru_accept = tcp6_usr_accept,
1135 .pru_attach = tcp_usr_attach,
1136 .pru_bind = tcp6_usr_bind,
1137 .pru_connect = tcp6_usr_connect,
1138 .pru_control = in6_control,
1139 .pru_detach = tcp_usr_detach,
1140 .pru_disconnect = tcp_usr_disconnect,
1141 .pru_listen = tcp6_usr_listen,
1142 .pru_peeraddr = in6_mapped_peeraddr,
1143 .pru_rcvd = tcp_usr_rcvd,
1144 .pru_rcvoob = tcp_usr_rcvoob,
1145 .pru_send = tcp_usr_send,
1146 .pru_ready = tcp_usr_ready,
1147 .pru_shutdown = tcp_usr_shutdown,
1148 .pru_sockaddr = in6_mapped_sockaddr,
1149 .pru_sosetlabel = in_pcbsosetlabel,
1150 .pru_close = tcp_usr_close,
1156 * Common subroutine to open a TCP connection to remote host specified
1157 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1158 * port number if needed. Call in_pcbconnect_setup to do the routing and
1159 * to choose a local host address (interface). If there is an existing
1160 * incarnation of the same connection in TIME-WAIT state and if the remote
1161 * host was sending CC options and if the connection duration was < MSL, then
1162 * truncate the previous TIME-WAIT state and proceed.
1163 * Initialize connection parameters and enter SYN-SENT state.
1166 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1168 struct inpcb *inp = tp->t_inpcb, *oinp;
1169 struct socket *so = inp->inp_socket;
1170 struct in_addr laddr;
1174 INP_WLOCK_ASSERT(inp);
1175 INP_HASH_WLOCK(&V_tcbinfo);
1177 if (inp->inp_lport == 0) {
1178 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1184 * Cannot simply call in_pcbconnect, because there might be an
1185 * earlier incarnation of this same connection still in
1186 * TIME_WAIT state, creating an ADDRINUSE error.
1188 laddr = inp->inp_laddr;
1189 lport = inp->inp_lport;
1190 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1191 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1192 if (error && oinp == NULL)
1198 inp->inp_laddr = laddr;
1200 INP_HASH_WUNLOCK(&V_tcbinfo);
1203 * Compute window scaling to request:
1204 * Scale to fit into sweet spot. See tcp_syncache.c.
1205 * XXX: This should move to tcp_output().
1207 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1208 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1209 tp->request_r_scale++;
1212 TCPSTAT_INC(tcps_connattempt);
1213 tcp_state_change(tp, TCPS_SYN_SENT);
1214 tp->iss = tcp_new_isn(tp);
1215 tcp_sendseqinit(tp);
1220 INP_HASH_WUNLOCK(&V_tcbinfo);
1227 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1229 struct inpcb *inp = tp->t_inpcb;
1232 INP_WLOCK_ASSERT(inp);
1233 INP_HASH_WLOCK(&V_tcbinfo);
1235 if (inp->inp_lport == 0) {
1236 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1240 error = in6_pcbconnect(inp, nam, td->td_ucred);
1243 INP_HASH_WUNLOCK(&V_tcbinfo);
1245 /* Compute window scaling to request. */
1246 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1247 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1248 tp->request_r_scale++;
1250 soisconnecting(inp->inp_socket);
1251 TCPSTAT_INC(tcps_connattempt);
1252 tcp_state_change(tp, TCPS_SYN_SENT);
1253 tp->iss = tcp_new_isn(tp);
1254 tcp_sendseqinit(tp);
1259 INP_HASH_WUNLOCK(&V_tcbinfo);
1265 * Export TCP internal state information via a struct tcp_info, based on the
1266 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1267 * (TCP state machine, etc). We export all information using FreeBSD-native
1268 * constants -- for example, the numeric values for tcpi_state will differ
1272 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1275 INP_WLOCK_ASSERT(tp->t_inpcb);
1276 bzero(ti, sizeof(*ti));
1278 ti->tcpi_state = tp->t_state;
1279 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1280 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1281 if (tp->t_flags & TF_SACK_PERMIT)
1282 ti->tcpi_options |= TCPI_OPT_SACK;
1283 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1284 ti->tcpi_options |= TCPI_OPT_WSCALE;
1285 ti->tcpi_snd_wscale = tp->snd_scale;
1286 ti->tcpi_rcv_wscale = tp->rcv_scale;
1289 ti->tcpi_rto = tp->t_rxtcur * tick;
1290 ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
1291 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1292 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1294 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1295 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1298 * FreeBSD-specific extension fields for tcp_info.
1300 ti->tcpi_rcv_space = tp->rcv_wnd;
1301 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1302 ti->tcpi_snd_wnd = tp->snd_wnd;
1303 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1304 ti->tcpi_snd_nxt = tp->snd_nxt;
1305 ti->tcpi_snd_mss = tp->t_maxseg;
1306 ti->tcpi_rcv_mss = tp->t_maxseg;
1307 if (tp->t_flags & TF_TOE)
1308 ti->tcpi_options |= TCPI_OPT_TOE;
1309 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1310 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1311 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1315 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1316 * socket option arguments. When it re-acquires the lock after the copy, it
1317 * has to revalidate that the connection is still valid for the socket
1320 #define INP_WLOCK_RECHECK(inp) do { \
1322 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1324 return (ECONNRESET); \
1326 tp = intotcpcb(inp); \
1330 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1332 int error, opt, optval;
1337 char buf[TCP_CA_NAME_MAX];
1338 struct cc_algo *algo;
1341 inp = sotoinpcb(so);
1342 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1344 if (sopt->sopt_level != IPPROTO_TCP) {
1346 if (inp->inp_vflag & INP_IPV6PROTO) {
1348 error = ip6_ctloutput(so, sopt);
1351 #if defined(INET6) && defined(INET)
1357 error = ip_ctloutput(so, sopt);
1362 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1364 return (ECONNRESET);
1367 switch (sopt->sopt_dir) {
1369 switch (sopt->sopt_name) {
1370 #ifdef TCP_SIGNATURE
1373 error = sooptcopyin(sopt, &optval, sizeof optval,
1378 INP_WLOCK_RECHECK(inp);
1380 tp->t_flags |= TF_SIGNATURE;
1382 tp->t_flags &= ~TF_SIGNATURE;
1383 goto unlock_and_done;
1384 #endif /* TCP_SIGNATURE */
1389 error = sooptcopyin(sopt, &optval, sizeof optval,
1394 INP_WLOCK_RECHECK(inp);
1395 switch (sopt->sopt_name) {
1403 opt = 0; /* dead code to fool gcc */
1410 tp->t_flags &= ~opt;
1413 if (tp->t_flags & TF_TOE) {
1414 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1423 error = sooptcopyin(sopt, &optval, sizeof optval,
1428 INP_WLOCK_RECHECK(inp);
1430 tp->t_flags |= TF_NOPUSH;
1431 else if (tp->t_flags & TF_NOPUSH) {
1432 tp->t_flags &= ~TF_NOPUSH;
1433 if (TCPS_HAVEESTABLISHED(tp->t_state))
1434 error = tcp_output(tp);
1436 goto unlock_and_done;
1440 error = sooptcopyin(sopt, &optval, sizeof optval,
1445 INP_WLOCK_RECHECK(inp);
1446 if (optval > 0 && optval <= tp->t_maxseg &&
1447 optval + 40 >= V_tcp_minmss)
1448 tp->t_maxseg = optval;
1451 goto unlock_and_done;
1458 case TCP_CONGESTION:
1460 bzero(buf, sizeof(buf));
1461 error = sooptcopyin(sopt, &buf, sizeof(buf), 1);
1464 INP_WLOCK_RECHECK(inp);
1466 * Return EINVAL if we can't find the requested cc algo.
1470 STAILQ_FOREACH(algo, &cc_list, entries) {
1471 if (strncmp(buf, algo->name, TCP_CA_NAME_MAX)
1473 /* We've found the requested algo. */
1476 * We hold a write lock over the tcb
1477 * so it's safe to do these things
1478 * without ordering concerns.
1480 if (CC_ALGO(tp)->cb_destroy != NULL)
1481 CC_ALGO(tp)->cb_destroy(tp->ccv);
1484 * If something goes pear shaped
1485 * initialising the new algo,
1486 * fall back to newreno (which
1487 * does not require initialisation).
1489 if (algo->cb_init != NULL)
1490 if (algo->cb_init(tp->ccv) > 0) {
1491 CC_ALGO(tp) = &newreno_cc_algo;
1493 * The only reason init
1495 * because of malloc.
1499 break; /* Break the STAILQ_FOREACH. */
1503 goto unlock_and_done;
1509 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1513 if (ui > (UINT_MAX / hz)) {
1519 INP_WLOCK_RECHECK(inp);
1520 switch (sopt->sopt_name) {
1522 tp->t_keepidle = ui;
1524 * XXX: better check current remaining
1525 * timeout and "merge" it with new value.
1527 if ((tp->t_state > TCPS_LISTEN) &&
1528 (tp->t_state <= TCPS_CLOSING))
1529 tcp_timer_activate(tp, TT_KEEP,
1533 tp->t_keepintvl = ui;
1534 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1535 (TP_MAXIDLE(tp) > 0))
1536 tcp_timer_activate(tp, TT_2MSL,
1540 tp->t_keepinit = ui;
1541 if (tp->t_state == TCPS_SYN_RECEIVED ||
1542 tp->t_state == TCPS_SYN_SENT)
1543 tcp_timer_activate(tp, TT_KEEP,
1547 goto unlock_and_done;
1551 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1555 INP_WLOCK_RECHECK(inp);
1557 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1558 (TP_MAXIDLE(tp) > 0))
1559 tcp_timer_activate(tp, TT_2MSL,
1561 goto unlock_and_done;
1565 error = ENOPROTOOPT;
1571 tp = intotcpcb(inp);
1572 switch (sopt->sopt_name) {
1573 #ifdef TCP_SIGNATURE
1575 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1577 error = sooptcopyout(sopt, &optval, sizeof optval);
1582 optval = tp->t_flags & TF_NODELAY;
1584 error = sooptcopyout(sopt, &optval, sizeof optval);
1587 optval = tp->t_maxseg;
1589 error = sooptcopyout(sopt, &optval, sizeof optval);
1592 optval = tp->t_flags & TF_NOOPT;
1594 error = sooptcopyout(sopt, &optval, sizeof optval);
1597 optval = tp->t_flags & TF_NOPUSH;
1599 error = sooptcopyout(sopt, &optval, sizeof optval);
1602 tcp_fill_info(tp, &ti);
1604 error = sooptcopyout(sopt, &ti, sizeof ti);
1606 case TCP_CONGESTION:
1607 bzero(buf, sizeof(buf));
1608 strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1610 error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX);
1616 switch (sopt->sopt_name) {
1618 ui = tp->t_keepidle / hz;
1621 ui = tp->t_keepintvl / hz;
1624 ui = tp->t_keepinit / hz;
1631 error = sooptcopyout(sopt, &ui, sizeof(ui));
1635 error = ENOPROTOOPT;
1642 #undef INP_WLOCK_RECHECK
1645 * Attach TCP protocol to socket, allocating
1646 * internet protocol control block, tcp control block,
1647 * bufer space, and entering LISTEN state if to accept connections.
1650 tcp_attach(struct socket *so)
1656 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1657 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1661 so->so_rcv.sb_flags |= SB_AUTOSIZE;
1662 so->so_snd.sb_flags |= SB_AUTOSIZE;
1663 INP_INFO_RLOCK(&V_tcbinfo);
1664 error = in_pcballoc(so, &V_tcbinfo);
1666 INP_INFO_RUNLOCK(&V_tcbinfo);
1669 inp = sotoinpcb(so);
1671 if (inp->inp_vflag & INP_IPV6PROTO) {
1672 inp->inp_vflag |= INP_IPV6;
1673 inp->in6p_hops = -1; /* use kernel default */
1677 inp->inp_vflag |= INP_IPV4;
1678 tp = tcp_newtcpcb(inp);
1682 INP_INFO_RUNLOCK(&V_tcbinfo);
1685 tp->t_state = TCPS_CLOSED;
1687 INP_INFO_RUNLOCK(&V_tcbinfo);
1692 * Initiate (or continue) disconnect.
1693 * If embryonic state, just send reset (once).
1694 * If in ``let data drain'' option and linger null, just drop.
1695 * Otherwise (hard), mark socket disconnecting and drop
1696 * current input data; switch states based on user close, and
1697 * send segment to peer (with FIN).
1700 tcp_disconnect(struct tcpcb *tp)
1702 struct inpcb *inp = tp->t_inpcb;
1703 struct socket *so = inp->inp_socket;
1705 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1706 INP_WLOCK_ASSERT(inp);
1709 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1710 * socket is still open.
1712 if (tp->t_state < TCPS_ESTABLISHED) {
1715 ("tcp_disconnect: tcp_close() returned NULL"));
1716 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1717 tp = tcp_drop(tp, 0);
1719 ("tcp_disconnect: tcp_drop() returned NULL"));
1721 soisdisconnecting(so);
1722 sbflush(&so->so_rcv);
1724 if (!(inp->inp_flags & INP_DROPPED))
1730 * User issued close, and wish to trail through shutdown states:
1731 * if never received SYN, just forget it. If got a SYN from peer,
1732 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1733 * If already got a FIN from peer, then almost done; go to LAST_ACK
1734 * state. In all other cases, have already sent FIN to peer (e.g.
1735 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1736 * for peer to send FIN or not respond to keep-alives, etc.
1737 * We can let the user exit from the close as soon as the FIN is acked.
1740 tcp_usrclosed(struct tcpcb *tp)
1743 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1744 INP_WLOCK_ASSERT(tp->t_inpcb);
1746 switch (tp->t_state) {
1749 tcp_offload_listen_stop(tp);
1753 tcp_state_change(tp, TCPS_CLOSED);
1756 * tcp_close() should never return NULL here as the socket is
1760 ("tcp_usrclosed: tcp_close() returned NULL"));
1764 case TCPS_SYN_RECEIVED:
1765 tp->t_flags |= TF_NEEDFIN;
1768 case TCPS_ESTABLISHED:
1769 tcp_state_change(tp, TCPS_FIN_WAIT_1);
1772 case TCPS_CLOSE_WAIT:
1773 tcp_state_change(tp, TCPS_LAST_ACK);
1776 if (tp->t_state >= TCPS_FIN_WAIT_2) {
1777 soisdisconnected(tp->t_inpcb->inp_socket);
1778 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
1779 if (tp->t_state == TCPS_FIN_WAIT_2) {
1782 timeout = (tcp_fast_finwait2_recycle) ?
1783 tcp_finwait2_timeout : TP_MAXIDLE(tp);
1784 tcp_timer_activate(tp, TT_2MSL, timeout);
1791 db_print_indent(int indent)
1795 for (i = 0; i < indent; i++)
1800 db_print_tstate(int t_state)
1805 db_printf("TCPS_CLOSED");
1809 db_printf("TCPS_LISTEN");
1813 db_printf("TCPS_SYN_SENT");
1816 case TCPS_SYN_RECEIVED:
1817 db_printf("TCPS_SYN_RECEIVED");
1820 case TCPS_ESTABLISHED:
1821 db_printf("TCPS_ESTABLISHED");
1824 case TCPS_CLOSE_WAIT:
1825 db_printf("TCPS_CLOSE_WAIT");
1828 case TCPS_FIN_WAIT_1:
1829 db_printf("TCPS_FIN_WAIT_1");
1833 db_printf("TCPS_CLOSING");
1837 db_printf("TCPS_LAST_ACK");
1840 case TCPS_FIN_WAIT_2:
1841 db_printf("TCPS_FIN_WAIT_2");
1844 case TCPS_TIME_WAIT:
1845 db_printf("TCPS_TIME_WAIT");
1849 db_printf("unknown");
1855 db_print_tflags(u_int t_flags)
1860 if (t_flags & TF_ACKNOW) {
1861 db_printf("%sTF_ACKNOW", comma ? ", " : "");
1864 if (t_flags & TF_DELACK) {
1865 db_printf("%sTF_DELACK", comma ? ", " : "");
1868 if (t_flags & TF_NODELAY) {
1869 db_printf("%sTF_NODELAY", comma ? ", " : "");
1872 if (t_flags & TF_NOOPT) {
1873 db_printf("%sTF_NOOPT", comma ? ", " : "");
1876 if (t_flags & TF_SENTFIN) {
1877 db_printf("%sTF_SENTFIN", comma ? ", " : "");
1880 if (t_flags & TF_REQ_SCALE) {
1881 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1884 if (t_flags & TF_RCVD_SCALE) {
1885 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1888 if (t_flags & TF_REQ_TSTMP) {
1889 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1892 if (t_flags & TF_RCVD_TSTMP) {
1893 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1896 if (t_flags & TF_SACK_PERMIT) {
1897 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1900 if (t_flags & TF_NEEDSYN) {
1901 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1904 if (t_flags & TF_NEEDFIN) {
1905 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1908 if (t_flags & TF_NOPUSH) {
1909 db_printf("%sTF_NOPUSH", comma ? ", " : "");
1912 if (t_flags & TF_MORETOCOME) {
1913 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1916 if (t_flags & TF_LQ_OVERFLOW) {
1917 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1920 if (t_flags & TF_LASTIDLE) {
1921 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1924 if (t_flags & TF_RXWIN0SENT) {
1925 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1928 if (t_flags & TF_FASTRECOVERY) {
1929 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1932 if (t_flags & TF_CONGRECOVERY) {
1933 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
1936 if (t_flags & TF_WASFRECOVERY) {
1937 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1940 if (t_flags & TF_SIGNATURE) {
1941 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1944 if (t_flags & TF_FORCEDATA) {
1945 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1948 if (t_flags & TF_TSO) {
1949 db_printf("%sTF_TSO", comma ? ", " : "");
1952 if (t_flags & TF_ECN_PERMIT) {
1953 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
1959 db_print_toobflags(char t_oobflags)
1964 if (t_oobflags & TCPOOB_HAVEDATA) {
1965 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1968 if (t_oobflags & TCPOOB_HADDATA) {
1969 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1975 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1978 db_print_indent(indent);
1979 db_printf("%s at %p\n", name, tp);
1983 db_print_indent(indent);
1984 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
1985 LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
1987 db_print_indent(indent);
1988 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
1989 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
1991 db_print_indent(indent);
1992 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
1993 &tp->t_timers->tt_delack, tp->t_inpcb);
1995 db_print_indent(indent);
1996 db_printf("t_state: %d (", tp->t_state);
1997 db_print_tstate(tp->t_state);
2000 db_print_indent(indent);
2001 db_printf("t_flags: 0x%x (", tp->t_flags);
2002 db_print_tflags(tp->t_flags);
2005 db_print_indent(indent);
2006 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2007 tp->snd_una, tp->snd_max, tp->snd_nxt);
2009 db_print_indent(indent);
2010 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2011 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2013 db_print_indent(indent);
2014 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2015 tp->iss, tp->irs, tp->rcv_nxt);
2017 db_print_indent(indent);
2018 db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n",
2019 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2021 db_print_indent(indent);
2022 db_printf("snd_wnd: %lu snd_cwnd: %lu\n",
2023 tp->snd_wnd, tp->snd_cwnd);
2025 db_print_indent(indent);
2026 db_printf("snd_ssthresh: %lu snd_recover: "
2027 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2029 db_print_indent(indent);
2030 db_printf("t_maxopd: %u t_rcvtime: %u t_startime: %u\n",
2031 tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
2033 db_print_indent(indent);
2034 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2035 tp->t_rtttime, tp->t_rtseq);
2037 db_print_indent(indent);
2038 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2039 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2041 db_print_indent(indent);
2042 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2043 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2046 db_print_indent(indent);
2047 db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n",
2048 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2050 db_print_indent(indent);
2051 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2052 db_print_toobflags(tp->t_oobflags);
2053 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2055 db_print_indent(indent);
2056 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2057 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2059 db_print_indent(indent);
2060 db_printf("ts_recent: %u ts_recent_age: %u\n",
2061 tp->ts_recent, tp->ts_recent_age);
2063 db_print_indent(indent);
2064 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2065 "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2067 db_print_indent(indent);
2068 db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x "
2069 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2070 tp->snd_recover_prev, tp->t_badrxtwin);
2072 db_print_indent(indent);
2073 db_printf("snd_numholes: %d snd_holes first: %p\n",
2074 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2076 db_print_indent(indent);
2077 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
2078 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2080 /* Skip sackblks, sackhint. */
2082 db_print_indent(indent);
2083 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2084 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2087 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2092 db_printf("usage: show tcpcb <addr>\n");
2095 tp = (struct tcpcb *)addr;
2097 db_print_tcpcb(tp, "tcpcb", 0);