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 if (inp->inp_flags & INP_DROPPED) {
188 KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
189 "INP_DROPPED && tp != NULL"));
198 * If the connection is not in timewait, we consider two
199 * two conditions: one in which no further processing is
200 * necessary (dropped || embryonic), and one in which TCP is
201 * not yet done, but no longer requires the socket, so the
202 * pcb will persist for the time being.
204 * XXXRW: Does the second case still occur?
206 if (inp->inp_flags & INP_DROPPED ||
207 tp->t_state < TCPS_SYN_SENT) {
219 * pru_detach() detaches the TCP protocol from the socket.
220 * If the protocol state is non-embryonic, then can't
221 * do this directly: have to initiate a pru_disconnect(),
222 * which may finish later; embryonic TCB's can just
226 tcp_usr_detach(struct socket *so)
231 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
232 INP_INFO_WLOCK(&V_tcbinfo);
234 KASSERT(inp->inp_socket != NULL,
235 ("tcp_usr_detach: inp_socket == NULL"));
237 INP_INFO_WUNLOCK(&V_tcbinfo);
242 * Give the socket an address.
245 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
249 struct tcpcb *tp = NULL;
250 struct sockaddr_in *sinp;
252 sinp = (struct sockaddr_in *)nam;
253 if (nam->sa_len != sizeof (*sinp))
256 * Must check for multicast addresses and disallow binding
259 if (sinp->sin_family == AF_INET &&
260 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
261 return (EAFNOSUPPORT);
265 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
267 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
273 INP_HASH_WLOCK(&V_tcbinfo);
274 error = in_pcbbind(inp, nam, td->td_ucred);
275 INP_HASH_WUNLOCK(&V_tcbinfo);
286 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
290 struct tcpcb *tp = NULL;
291 struct sockaddr_in6 *sin6p;
293 sin6p = (struct sockaddr_in6 *)nam;
294 if (nam->sa_len != sizeof (*sin6p))
297 * Must check for multicast addresses and disallow binding
300 if (sin6p->sin6_family == AF_INET6 &&
301 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
302 return (EAFNOSUPPORT);
306 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
308 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
314 INP_HASH_WLOCK(&V_tcbinfo);
315 inp->inp_vflag &= ~INP_IPV4;
316 inp->inp_vflag |= INP_IPV6;
318 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
319 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
320 inp->inp_vflag |= INP_IPV4;
321 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
322 struct sockaddr_in sin;
324 in6_sin6_2_sin(&sin, sin6p);
325 inp->inp_vflag |= INP_IPV4;
326 inp->inp_vflag &= ~INP_IPV6;
327 error = in_pcbbind(inp, (struct sockaddr *)&sin,
329 INP_HASH_WUNLOCK(&V_tcbinfo);
334 error = in6_pcbbind(inp, nam, td->td_ucred);
335 INP_HASH_WUNLOCK(&V_tcbinfo);
345 * Prepare to accept connections.
348 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
352 struct tcpcb *tp = NULL;
356 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
358 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
365 error = solisten_proto_check(so);
366 INP_HASH_WLOCK(&V_tcbinfo);
367 if (error == 0 && inp->inp_lport == 0)
368 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
369 INP_HASH_WUNLOCK(&V_tcbinfo);
371 tcp_state_change(tp, TCPS_LISTEN);
372 solisten_proto(so, backlog);
374 if ((so->so_options & SO_NO_OFFLOAD) == 0)
375 tcp_offload_listen_start(tp);
381 TCPDEBUG2(PRU_LISTEN);
389 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
393 struct tcpcb *tp = NULL;
397 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
399 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
406 error = solisten_proto_check(so);
407 INP_HASH_WLOCK(&V_tcbinfo);
408 if (error == 0 && inp->inp_lport == 0) {
409 inp->inp_vflag &= ~INP_IPV4;
410 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
411 inp->inp_vflag |= INP_IPV4;
412 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
414 INP_HASH_WUNLOCK(&V_tcbinfo);
416 tcp_state_change(tp, TCPS_LISTEN);
417 solisten_proto(so, backlog);
419 if ((so->so_options & SO_NO_OFFLOAD) == 0)
420 tcp_offload_listen_start(tp);
426 TCPDEBUG2(PRU_LISTEN);
434 * Initiate connection to peer.
435 * Create a template for use in transmissions on this connection.
436 * Enter SYN_SENT state, and mark socket as connecting.
437 * Start keep-alive timer, and seed output sequence space.
438 * Send initial segment on connection.
441 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
445 struct tcpcb *tp = NULL;
446 struct sockaddr_in *sinp;
448 sinp = (struct sockaddr_in *)nam;
449 if (nam->sa_len != sizeof (*sinp))
452 * Must disallow TCP ``connections'' to multicast addresses.
454 if (sinp->sin_family == AF_INET
455 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
456 return (EAFNOSUPPORT);
457 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
462 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
464 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
470 if ((error = tcp_connect(tp, nam, td)) != 0)
473 if (registered_toedevs > 0 &&
474 (so->so_options & SO_NO_OFFLOAD) == 0 &&
475 (error = tcp_offload_connect(so, nam)) == 0)
478 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
479 error = tcp_output(tp);
481 TCPDEBUG2(PRU_CONNECT);
489 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
493 struct tcpcb *tp = NULL;
494 struct sockaddr_in6 *sin6p;
498 sin6p = (struct sockaddr_in6 *)nam;
499 if (nam->sa_len != sizeof (*sin6p))
502 * Must disallow TCP ``connections'' to multicast addresses.
504 if (sin6p->sin6_family == AF_INET6
505 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
506 return (EAFNOSUPPORT);
509 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
511 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
519 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
520 * therefore probably require the hash lock, which isn't held here.
521 * Is this a significant problem?
523 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
524 struct sockaddr_in sin;
526 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
531 in6_sin6_2_sin(&sin, sin6p);
532 inp->inp_vflag |= INP_IPV4;
533 inp->inp_vflag &= ~INP_IPV6;
534 if ((error = prison_remote_ip4(td->td_ucred,
535 &sin.sin_addr)) != 0)
537 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
540 if (registered_toedevs > 0 &&
541 (so->so_options & SO_NO_OFFLOAD) == 0 &&
542 (error = tcp_offload_connect(so, nam)) == 0)
545 error = tcp_output(tp);
549 inp->inp_vflag &= ~INP_IPV4;
550 inp->inp_vflag |= INP_IPV6;
551 inp->inp_inc.inc_flags |= INC_ISIPV6;
552 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
554 if ((error = tcp6_connect(tp, nam, td)) != 0)
557 if (registered_toedevs > 0 &&
558 (so->so_options & SO_NO_OFFLOAD) == 0 &&
559 (error = tcp_offload_connect(so, nam)) == 0)
562 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
563 error = tcp_output(tp);
566 TCPDEBUG2(PRU_CONNECT);
573 * Initiate disconnect from peer.
574 * If connection never passed embryonic stage, just drop;
575 * else if don't need to let data drain, then can just drop anyways,
576 * else have to begin TCP shutdown process: mark socket disconnecting,
577 * drain unread data, state switch to reflect user close, and
578 * send segment (e.g. FIN) to peer. Socket will be really disconnected
579 * when peer sends FIN and acks ours.
581 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
584 tcp_usr_disconnect(struct socket *so)
587 struct tcpcb *tp = NULL;
591 INP_INFO_WLOCK(&V_tcbinfo);
593 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
595 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
603 TCPDEBUG2(PRU_DISCONNECT);
605 INP_INFO_WUNLOCK(&V_tcbinfo);
611 * Accept a connection. Essentially all the work is done at higher levels;
612 * just return the address of the peer, storing through addr.
615 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
618 struct inpcb *inp = NULL;
619 struct tcpcb *tp = NULL;
624 if (so->so_state & SS_ISDISCONNECTED)
625 return (ECONNABORTED);
628 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
630 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
631 error = ECONNABORTED;
638 * We inline in_getpeeraddr and COMMON_END here, so that we can
639 * copy the data of interest and defer the malloc until after we
642 port = inp->inp_fport;
643 addr = inp->inp_faddr;
646 TCPDEBUG2(PRU_ACCEPT);
649 *nam = in_sockaddr(port, &addr);
656 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
658 struct inpcb *inp = NULL;
660 struct tcpcb *tp = NULL;
662 struct in6_addr addr6;
667 if (so->so_state & SS_ISDISCONNECTED)
668 return (ECONNABORTED);
671 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
672 INP_INFO_RLOCK(&V_tcbinfo);
674 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
675 error = ECONNABORTED;
682 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
683 * copy the data of interest and defer the malloc until after we
686 if (inp->inp_vflag & INP_IPV4) {
688 port = inp->inp_fport;
689 addr = inp->inp_faddr;
691 port = inp->inp_fport;
692 addr6 = inp->in6p_faddr;
696 TCPDEBUG2(PRU_ACCEPT);
698 INP_INFO_RUNLOCK(&V_tcbinfo);
701 *nam = in6_v4mapsin6_sockaddr(port, &addr);
703 *nam = in6_sockaddr(port, &addr6);
710 * Mark the connection as being incapable of further output.
713 tcp_usr_shutdown(struct socket *so)
717 struct tcpcb *tp = NULL;
720 INP_INFO_WLOCK(&V_tcbinfo);
722 KASSERT(inp != NULL, ("inp == NULL"));
724 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
732 if (!(inp->inp_flags & INP_DROPPED))
733 error = tcp_output(tp);
736 TCPDEBUG2(PRU_SHUTDOWN);
738 INP_INFO_WUNLOCK(&V_tcbinfo);
744 * After a receive, possibly send window update to peer.
747 tcp_usr_rcvd(struct socket *so, int flags)
750 struct tcpcb *tp = NULL;
755 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
757 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
764 if (tp->t_flags & TF_TOE)
765 tcp_offload_rcvd(tp);
777 * Do a send by putting data in output queue and updating urgent
778 * marker if URG set. Possibly send more data. Unlike the other
779 * pru_*() routines, the mbuf chains are our responsibility. We
780 * must either enqueue them or free them. The other pru_* routines
781 * generally are caller-frees.
784 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
785 struct sockaddr *nam, struct mbuf *control, struct thread *td)
789 struct tcpcb *tp = NULL;
796 * We require the pcbinfo lock if we will close the socket as part of
799 if (flags & PRUS_EOF)
800 INP_INFO_WLOCK(&V_tcbinfo);
802 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
804 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
813 isipv6 = nam && nam->sa_family == AF_INET6;
818 /* TCP doesn't do control messages (rights, creds, etc) */
819 if (control->m_len) {
826 m_freem(control); /* empty control, just free it */
828 if (!(flags & PRUS_OOB)) {
829 sbappendstream(&so->so_snd, m);
830 if (nam && tp->t_state < TCPS_SYN_SENT) {
832 * Do implied connect if not yet connected,
833 * initialize window to default value, and
834 * initialize maxseg/maxopd using peer's cached
839 error = tcp6_connect(tp, nam, td);
841 #if defined(INET6) && defined(INET)
845 error = tcp_connect(tp, nam, td);
849 tp->snd_wnd = TTCP_CLIENT_SND_WND;
852 if (flags & PRUS_EOF) {
854 * Close the send side of the connection after
857 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
861 if (!(inp->inp_flags & INP_DROPPED)) {
862 if (flags & PRUS_MORETOCOME)
863 tp->t_flags |= TF_MORETOCOME;
864 error = tcp_output(tp);
865 if (flags & PRUS_MORETOCOME)
866 tp->t_flags &= ~TF_MORETOCOME;
870 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
872 SOCKBUF_LOCK(&so->so_snd);
873 if (sbspace(&so->so_snd) < -512) {
874 SOCKBUF_UNLOCK(&so->so_snd);
880 * According to RFC961 (Assigned Protocols),
881 * the urgent pointer points to the last octet
882 * of urgent data. We continue, however,
883 * to consider it to indicate the first octet
884 * of data past the urgent section.
885 * Otherwise, snd_up should be one lower.
887 sbappendstream_locked(&so->so_snd, m);
888 SOCKBUF_UNLOCK(&so->so_snd);
889 if (nam && tp->t_state < TCPS_SYN_SENT) {
891 * Do implied connect if not yet connected,
892 * initialize window to default value, and
893 * initialize maxseg/maxopd using peer's cached
898 error = tcp6_connect(tp, nam, td);
900 #if defined(INET6) && defined(INET)
904 error = tcp_connect(tp, nam, td);
908 tp->snd_wnd = TTCP_CLIENT_SND_WND;
911 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
912 tp->t_flags |= TF_FORCEDATA;
913 error = tcp_output(tp);
914 tp->t_flags &= ~TF_FORCEDATA;
917 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
918 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
920 if (flags & PRUS_EOF)
921 INP_INFO_WUNLOCK(&V_tcbinfo);
926 * Abort the TCP. Drop the connection abruptly.
929 tcp_usr_abort(struct socket *so)
932 struct tcpcb *tp = NULL;
936 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
938 INP_INFO_WLOCK(&V_tcbinfo);
940 KASSERT(inp->inp_socket != NULL,
941 ("tcp_usr_abort: inp_socket == NULL"));
944 * If we still have full TCP state, and we're not dropped, drop.
946 if (!(inp->inp_flags & INP_TIMEWAIT) &&
947 !(inp->inp_flags & INP_DROPPED)) {
950 tcp_drop(tp, ECONNABORTED);
951 TCPDEBUG2(PRU_ABORT);
953 if (!(inp->inp_flags & INP_DROPPED)) {
955 so->so_state |= SS_PROTOREF;
957 inp->inp_flags |= INP_SOCKREF;
960 INP_INFO_WUNLOCK(&V_tcbinfo);
964 * TCP socket is closed. Start friendly disconnect.
967 tcp_usr_close(struct socket *so)
970 struct tcpcb *tp = NULL;
974 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
976 INP_INFO_WLOCK(&V_tcbinfo);
978 KASSERT(inp->inp_socket != NULL,
979 ("tcp_usr_close: inp_socket == NULL"));
982 * If we still have full TCP state, and we're not dropped, initiate
985 if (!(inp->inp_flags & INP_TIMEWAIT) &&
986 !(inp->inp_flags & INP_DROPPED)) {
990 TCPDEBUG2(PRU_CLOSE);
992 if (!(inp->inp_flags & INP_DROPPED)) {
994 so->so_state |= SS_PROTOREF;
996 inp->inp_flags |= INP_SOCKREF;
999 INP_INFO_WUNLOCK(&V_tcbinfo);
1003 * Receive out-of-band data.
1006 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1010 struct tcpcb *tp = NULL;
1013 inp = sotoinpcb(so);
1014 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1016 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1020 tp = intotcpcb(inp);
1022 if ((so->so_oobmark == 0 &&
1023 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1024 so->so_options & SO_OOBINLINE ||
1025 tp->t_oobflags & TCPOOB_HADDATA) {
1029 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1030 error = EWOULDBLOCK;
1034 *mtod(m, caddr_t) = tp->t_iobc;
1035 if ((flags & MSG_PEEK) == 0)
1036 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1039 TCPDEBUG2(PRU_RCVOOB);
1045 struct pr_usrreqs tcp_usrreqs = {
1046 .pru_abort = tcp_usr_abort,
1047 .pru_accept = tcp_usr_accept,
1048 .pru_attach = tcp_usr_attach,
1049 .pru_bind = tcp_usr_bind,
1050 .pru_connect = tcp_usr_connect,
1051 .pru_control = in_control,
1052 .pru_detach = tcp_usr_detach,
1053 .pru_disconnect = tcp_usr_disconnect,
1054 .pru_listen = tcp_usr_listen,
1055 .pru_peeraddr = in_getpeeraddr,
1056 .pru_rcvd = tcp_usr_rcvd,
1057 .pru_rcvoob = tcp_usr_rcvoob,
1058 .pru_send = tcp_usr_send,
1059 .pru_shutdown = tcp_usr_shutdown,
1060 .pru_sockaddr = in_getsockaddr,
1061 .pru_sosetlabel = in_pcbsosetlabel,
1062 .pru_close = tcp_usr_close,
1067 struct pr_usrreqs tcp6_usrreqs = {
1068 .pru_abort = tcp_usr_abort,
1069 .pru_accept = tcp6_usr_accept,
1070 .pru_attach = tcp_usr_attach,
1071 .pru_bind = tcp6_usr_bind,
1072 .pru_connect = tcp6_usr_connect,
1073 .pru_control = in6_control,
1074 .pru_detach = tcp_usr_detach,
1075 .pru_disconnect = tcp_usr_disconnect,
1076 .pru_listen = tcp6_usr_listen,
1077 .pru_peeraddr = in6_mapped_peeraddr,
1078 .pru_rcvd = tcp_usr_rcvd,
1079 .pru_rcvoob = tcp_usr_rcvoob,
1080 .pru_send = tcp_usr_send,
1081 .pru_shutdown = tcp_usr_shutdown,
1082 .pru_sockaddr = in6_mapped_sockaddr,
1083 .pru_sosetlabel = in_pcbsosetlabel,
1084 .pru_close = tcp_usr_close,
1090 * Common subroutine to open a TCP connection to remote host specified
1091 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1092 * port number if needed. Call in_pcbconnect_setup to do the routing and
1093 * to choose a local host address (interface). If there is an existing
1094 * incarnation of the same connection in TIME-WAIT state and if the remote
1095 * host was sending CC options and if the connection duration was < MSL, then
1096 * truncate the previous TIME-WAIT state and proceed.
1097 * Initialize connection parameters and enter SYN-SENT state.
1100 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1102 struct inpcb *inp = tp->t_inpcb, *oinp;
1103 struct socket *so = inp->inp_socket;
1104 struct in_addr laddr;
1108 INP_WLOCK_ASSERT(inp);
1109 INP_HASH_WLOCK(&V_tcbinfo);
1111 if (inp->inp_lport == 0) {
1112 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1118 * Cannot simply call in_pcbconnect, because there might be an
1119 * earlier incarnation of this same connection still in
1120 * TIME_WAIT state, creating an ADDRINUSE error.
1122 laddr = inp->inp_laddr;
1123 lport = inp->inp_lport;
1124 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1125 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1126 if (error && oinp == NULL)
1132 inp->inp_laddr = laddr;
1134 INP_HASH_WUNLOCK(&V_tcbinfo);
1137 * Compute window scaling to request:
1138 * Scale to fit into sweet spot. See tcp_syncache.c.
1139 * XXX: This should move to tcp_output().
1141 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1142 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1143 tp->request_r_scale++;
1146 TCPSTAT_INC(tcps_connattempt);
1147 tcp_state_change(tp, TCPS_SYN_SENT);
1148 tp->iss = tcp_new_isn(tp);
1149 tcp_sendseqinit(tp);
1154 INP_HASH_WUNLOCK(&V_tcbinfo);
1161 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1163 struct inpcb *inp = tp->t_inpcb;
1166 INP_WLOCK_ASSERT(inp);
1167 INP_HASH_WLOCK(&V_tcbinfo);
1169 if (inp->inp_lport == 0) {
1170 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1174 error = in6_pcbconnect(inp, nam, td->td_ucred);
1177 INP_HASH_WUNLOCK(&V_tcbinfo);
1179 /* Compute window scaling to request. */
1180 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1181 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1182 tp->request_r_scale++;
1184 soisconnecting(inp->inp_socket);
1185 TCPSTAT_INC(tcps_connattempt);
1186 tcp_state_change(tp, TCPS_SYN_SENT);
1187 tp->iss = tcp_new_isn(tp);
1188 tcp_sendseqinit(tp);
1193 INP_HASH_WUNLOCK(&V_tcbinfo);
1199 * Export TCP internal state information via a struct tcp_info, based on the
1200 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1201 * (TCP state machine, etc). We export all information using FreeBSD-native
1202 * constants -- for example, the numeric values for tcpi_state will differ
1206 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1209 INP_WLOCK_ASSERT(tp->t_inpcb);
1210 bzero(ti, sizeof(*ti));
1212 ti->tcpi_state = tp->t_state;
1213 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1214 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1215 if (tp->t_flags & TF_SACK_PERMIT)
1216 ti->tcpi_options |= TCPI_OPT_SACK;
1217 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1218 ti->tcpi_options |= TCPI_OPT_WSCALE;
1219 ti->tcpi_snd_wscale = tp->snd_scale;
1220 ti->tcpi_rcv_wscale = tp->rcv_scale;
1223 ti->tcpi_rto = tp->t_rxtcur * tick;
1224 ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
1225 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1226 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1228 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1229 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1232 * FreeBSD-specific extension fields for tcp_info.
1234 ti->tcpi_rcv_space = tp->rcv_wnd;
1235 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1236 ti->tcpi_snd_wnd = tp->snd_wnd;
1237 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1238 ti->tcpi_snd_nxt = tp->snd_nxt;
1239 ti->tcpi_snd_mss = tp->t_maxseg;
1240 ti->tcpi_rcv_mss = tp->t_maxseg;
1241 if (tp->t_flags & TF_TOE)
1242 ti->tcpi_options |= TCPI_OPT_TOE;
1243 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1244 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1245 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1249 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1250 * socket option arguments. When it re-acquires the lock after the copy, it
1251 * has to revalidate that the connection is still valid for the socket
1254 #define INP_WLOCK_RECHECK(inp) do { \
1256 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1258 return (ECONNRESET); \
1260 tp = intotcpcb(inp); \
1264 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1266 int error, opt, optval;
1271 char buf[TCP_CA_NAME_MAX];
1272 struct cc_algo *algo;
1275 inp = sotoinpcb(so);
1276 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1278 if (sopt->sopt_level != IPPROTO_TCP) {
1280 if (inp->inp_vflag & INP_IPV6PROTO) {
1282 error = ip6_ctloutput(so, sopt);
1285 #if defined(INET6) && defined(INET)
1291 error = ip_ctloutput(so, sopt);
1296 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1298 return (ECONNRESET);
1301 switch (sopt->sopt_dir) {
1303 switch (sopt->sopt_name) {
1304 #ifdef TCP_SIGNATURE
1307 error = sooptcopyin(sopt, &optval, sizeof optval,
1312 INP_WLOCK_RECHECK(inp);
1314 tp->t_flags |= TF_SIGNATURE;
1316 tp->t_flags &= ~TF_SIGNATURE;
1317 goto unlock_and_done;
1318 #endif /* TCP_SIGNATURE */
1323 error = sooptcopyin(sopt, &optval, sizeof optval,
1328 INP_WLOCK_RECHECK(inp);
1329 switch (sopt->sopt_name) {
1337 opt = 0; /* dead code to fool gcc */
1344 tp->t_flags &= ~opt;
1347 if (tp->t_flags & TF_TOE) {
1348 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1357 error = sooptcopyin(sopt, &optval, sizeof optval,
1362 INP_WLOCK_RECHECK(inp);
1364 tp->t_flags |= TF_NOPUSH;
1365 else if (tp->t_flags & TF_NOPUSH) {
1366 tp->t_flags &= ~TF_NOPUSH;
1367 if (TCPS_HAVEESTABLISHED(tp->t_state))
1368 error = tcp_output(tp);
1370 goto unlock_and_done;
1374 error = sooptcopyin(sopt, &optval, sizeof optval,
1379 INP_WLOCK_RECHECK(inp);
1380 if (optval > 0 && optval <= tp->t_maxseg &&
1381 optval + 40 >= V_tcp_minmss)
1382 tp->t_maxseg = optval;
1385 goto unlock_and_done;
1392 case TCP_CONGESTION:
1394 bzero(buf, sizeof(buf));
1395 error = sooptcopyin(sopt, &buf, sizeof(buf), 1);
1398 INP_WLOCK_RECHECK(inp);
1400 * Return EINVAL if we can't find the requested cc algo.
1404 STAILQ_FOREACH(algo, &cc_list, entries) {
1405 if (strncmp(buf, algo->name, TCP_CA_NAME_MAX)
1407 /* We've found the requested algo. */
1410 * We hold a write lock over the tcb
1411 * so it's safe to do these things
1412 * without ordering concerns.
1414 if (CC_ALGO(tp)->cb_destroy != NULL)
1415 CC_ALGO(tp)->cb_destroy(tp->ccv);
1418 * If something goes pear shaped
1419 * initialising the new algo,
1420 * fall back to newreno (which
1421 * does not require initialisation).
1423 if (algo->cb_init != NULL)
1424 if (algo->cb_init(tp->ccv) > 0) {
1425 CC_ALGO(tp) = &newreno_cc_algo;
1427 * The only reason init
1429 * because of malloc.
1433 break; /* Break the STAILQ_FOREACH. */
1437 goto unlock_and_done;
1443 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1447 if (ui > (UINT_MAX / hz)) {
1453 INP_WLOCK_RECHECK(inp);
1454 switch (sopt->sopt_name) {
1456 tp->t_keepidle = ui;
1458 * XXX: better check current remaining
1459 * timeout and "merge" it with new value.
1461 if ((tp->t_state > TCPS_LISTEN) &&
1462 (tp->t_state <= TCPS_CLOSING))
1463 tcp_timer_activate(tp, TT_KEEP,
1467 tp->t_keepintvl = ui;
1468 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1469 (TP_MAXIDLE(tp) > 0))
1470 tcp_timer_activate(tp, TT_2MSL,
1474 tp->t_keepinit = ui;
1475 if (tp->t_state == TCPS_SYN_RECEIVED ||
1476 tp->t_state == TCPS_SYN_SENT)
1477 tcp_timer_activate(tp, TT_KEEP,
1481 goto unlock_and_done;
1485 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1489 INP_WLOCK_RECHECK(inp);
1491 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1492 (TP_MAXIDLE(tp) > 0))
1493 tcp_timer_activate(tp, TT_2MSL,
1495 goto unlock_and_done;
1499 error = ENOPROTOOPT;
1505 tp = intotcpcb(inp);
1506 switch (sopt->sopt_name) {
1507 #ifdef TCP_SIGNATURE
1509 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1511 error = sooptcopyout(sopt, &optval, sizeof optval);
1516 optval = tp->t_flags & TF_NODELAY;
1518 error = sooptcopyout(sopt, &optval, sizeof optval);
1521 optval = tp->t_maxseg;
1523 error = sooptcopyout(sopt, &optval, sizeof optval);
1526 optval = tp->t_flags & TF_NOOPT;
1528 error = sooptcopyout(sopt, &optval, sizeof optval);
1531 optval = tp->t_flags & TF_NOPUSH;
1533 error = sooptcopyout(sopt, &optval, sizeof optval);
1536 tcp_fill_info(tp, &ti);
1538 error = sooptcopyout(sopt, &ti, sizeof ti);
1540 case TCP_CONGESTION:
1541 bzero(buf, sizeof(buf));
1542 strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1544 error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX);
1550 switch (sopt->sopt_name) {
1552 ui = tp->t_keepidle / hz;
1555 ui = tp->t_keepintvl / hz;
1558 ui = tp->t_keepinit / hz;
1565 error = sooptcopyout(sopt, &ui, sizeof(ui));
1569 error = ENOPROTOOPT;
1576 #undef INP_WLOCK_RECHECK
1579 * Attach TCP protocol to socket, allocating
1580 * internet protocol control block, tcp control block,
1581 * bufer space, and entering LISTEN state if to accept connections.
1584 tcp_attach(struct socket *so)
1590 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1591 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1595 so->so_rcv.sb_flags |= SB_AUTOSIZE;
1596 so->so_snd.sb_flags |= SB_AUTOSIZE;
1597 INP_INFO_WLOCK(&V_tcbinfo);
1598 error = in_pcballoc(so, &V_tcbinfo);
1600 INP_INFO_WUNLOCK(&V_tcbinfo);
1603 inp = sotoinpcb(so);
1605 if (inp->inp_vflag & INP_IPV6PROTO) {
1606 inp->inp_vflag |= INP_IPV6;
1607 inp->in6p_hops = -1; /* use kernel default */
1611 inp->inp_vflag |= INP_IPV4;
1612 tp = tcp_newtcpcb(inp);
1616 INP_INFO_WUNLOCK(&V_tcbinfo);
1619 tp->t_state = TCPS_CLOSED;
1621 INP_INFO_WUNLOCK(&V_tcbinfo);
1626 * Initiate (or continue) disconnect.
1627 * If embryonic state, just send reset (once).
1628 * If in ``let data drain'' option and linger null, just drop.
1629 * Otherwise (hard), mark socket disconnecting and drop
1630 * current input data; switch states based on user close, and
1631 * send segment to peer (with FIN).
1634 tcp_disconnect(struct tcpcb *tp)
1636 struct inpcb *inp = tp->t_inpcb;
1637 struct socket *so = inp->inp_socket;
1639 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1640 INP_WLOCK_ASSERT(inp);
1643 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1644 * socket is still open.
1646 if (tp->t_state < TCPS_ESTABLISHED) {
1649 ("tcp_disconnect: tcp_close() returned NULL"));
1650 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1651 tp = tcp_drop(tp, 0);
1653 ("tcp_disconnect: tcp_drop() returned NULL"));
1655 soisdisconnecting(so);
1656 sbflush(&so->so_rcv);
1658 if (!(inp->inp_flags & INP_DROPPED))
1664 * User issued close, and wish to trail through shutdown states:
1665 * if never received SYN, just forget it. If got a SYN from peer,
1666 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1667 * If already got a FIN from peer, then almost done; go to LAST_ACK
1668 * state. In all other cases, have already sent FIN to peer (e.g.
1669 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1670 * for peer to send FIN or not respond to keep-alives, etc.
1671 * We can let the user exit from the close as soon as the FIN is acked.
1674 tcp_usrclosed(struct tcpcb *tp)
1677 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1678 INP_WLOCK_ASSERT(tp->t_inpcb);
1680 switch (tp->t_state) {
1683 tcp_offload_listen_stop(tp);
1687 tcp_state_change(tp, TCPS_CLOSED);
1690 * tcp_close() should never return NULL here as the socket is
1694 ("tcp_usrclosed: tcp_close() returned NULL"));
1698 case TCPS_SYN_RECEIVED:
1699 tp->t_flags |= TF_NEEDFIN;
1702 case TCPS_ESTABLISHED:
1703 tcp_state_change(tp, TCPS_FIN_WAIT_1);
1706 case TCPS_CLOSE_WAIT:
1707 tcp_state_change(tp, TCPS_LAST_ACK);
1710 if (tp->t_state >= TCPS_FIN_WAIT_2) {
1711 soisdisconnected(tp->t_inpcb->inp_socket);
1712 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
1713 if (tp->t_state == TCPS_FIN_WAIT_2) {
1716 timeout = (tcp_fast_finwait2_recycle) ?
1717 tcp_finwait2_timeout : TP_MAXIDLE(tp);
1718 tcp_timer_activate(tp, TT_2MSL, timeout);
1725 db_print_indent(int indent)
1729 for (i = 0; i < indent; i++)
1734 db_print_tstate(int t_state)
1739 db_printf("TCPS_CLOSED");
1743 db_printf("TCPS_LISTEN");
1747 db_printf("TCPS_SYN_SENT");
1750 case TCPS_SYN_RECEIVED:
1751 db_printf("TCPS_SYN_RECEIVED");
1754 case TCPS_ESTABLISHED:
1755 db_printf("TCPS_ESTABLISHED");
1758 case TCPS_CLOSE_WAIT:
1759 db_printf("TCPS_CLOSE_WAIT");
1762 case TCPS_FIN_WAIT_1:
1763 db_printf("TCPS_FIN_WAIT_1");
1767 db_printf("TCPS_CLOSING");
1771 db_printf("TCPS_LAST_ACK");
1774 case TCPS_FIN_WAIT_2:
1775 db_printf("TCPS_FIN_WAIT_2");
1778 case TCPS_TIME_WAIT:
1779 db_printf("TCPS_TIME_WAIT");
1783 db_printf("unknown");
1789 db_print_tflags(u_int t_flags)
1794 if (t_flags & TF_ACKNOW) {
1795 db_printf("%sTF_ACKNOW", comma ? ", " : "");
1798 if (t_flags & TF_DELACK) {
1799 db_printf("%sTF_DELACK", comma ? ", " : "");
1802 if (t_flags & TF_NODELAY) {
1803 db_printf("%sTF_NODELAY", comma ? ", " : "");
1806 if (t_flags & TF_NOOPT) {
1807 db_printf("%sTF_NOOPT", comma ? ", " : "");
1810 if (t_flags & TF_SENTFIN) {
1811 db_printf("%sTF_SENTFIN", comma ? ", " : "");
1814 if (t_flags & TF_REQ_SCALE) {
1815 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1818 if (t_flags & TF_RCVD_SCALE) {
1819 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1822 if (t_flags & TF_REQ_TSTMP) {
1823 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1826 if (t_flags & TF_RCVD_TSTMP) {
1827 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1830 if (t_flags & TF_SACK_PERMIT) {
1831 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1834 if (t_flags & TF_NEEDSYN) {
1835 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1838 if (t_flags & TF_NEEDFIN) {
1839 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1842 if (t_flags & TF_NOPUSH) {
1843 db_printf("%sTF_NOPUSH", comma ? ", " : "");
1846 if (t_flags & TF_MORETOCOME) {
1847 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1850 if (t_flags & TF_LQ_OVERFLOW) {
1851 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1854 if (t_flags & TF_LASTIDLE) {
1855 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1858 if (t_flags & TF_RXWIN0SENT) {
1859 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1862 if (t_flags & TF_FASTRECOVERY) {
1863 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1866 if (t_flags & TF_CONGRECOVERY) {
1867 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
1870 if (t_flags & TF_WASFRECOVERY) {
1871 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1874 if (t_flags & TF_SIGNATURE) {
1875 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1878 if (t_flags & TF_FORCEDATA) {
1879 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1882 if (t_flags & TF_TSO) {
1883 db_printf("%sTF_TSO", comma ? ", " : "");
1886 if (t_flags & TF_ECN_PERMIT) {
1887 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
1893 db_print_toobflags(char t_oobflags)
1898 if (t_oobflags & TCPOOB_HAVEDATA) {
1899 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1902 if (t_oobflags & TCPOOB_HADDATA) {
1903 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1909 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1912 db_print_indent(indent);
1913 db_printf("%s at %p\n", name, tp);
1917 db_print_indent(indent);
1918 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
1919 tp->t_segq, tp->t_segqlen, tp->t_dupacks);
1921 db_print_indent(indent);
1922 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
1923 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
1925 db_print_indent(indent);
1926 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
1927 &tp->t_timers->tt_delack, tp->t_inpcb);
1929 db_print_indent(indent);
1930 db_printf("t_state: %d (", tp->t_state);
1931 db_print_tstate(tp->t_state);
1934 db_print_indent(indent);
1935 db_printf("t_flags: 0x%x (", tp->t_flags);
1936 db_print_tflags(tp->t_flags);
1939 db_print_indent(indent);
1940 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
1941 tp->snd_una, tp->snd_max, tp->snd_nxt);
1943 db_print_indent(indent);
1944 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
1945 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
1947 db_print_indent(indent);
1948 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
1949 tp->iss, tp->irs, tp->rcv_nxt);
1951 db_print_indent(indent);
1952 db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n",
1953 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
1955 db_print_indent(indent);
1956 db_printf("snd_wnd: %lu snd_cwnd: %lu\n",
1957 tp->snd_wnd, tp->snd_cwnd);
1959 db_print_indent(indent);
1960 db_printf("snd_ssthresh: %lu snd_recover: "
1961 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
1963 db_print_indent(indent);
1964 db_printf("t_maxopd: %u t_rcvtime: %u t_startime: %u\n",
1965 tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
1967 db_print_indent(indent);
1968 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
1969 tp->t_rtttime, tp->t_rtseq);
1971 db_print_indent(indent);
1972 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
1973 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
1975 db_print_indent(indent);
1976 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
1977 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
1980 db_print_indent(indent);
1981 db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n",
1982 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
1984 db_print_indent(indent);
1985 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
1986 db_print_toobflags(tp->t_oobflags);
1987 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
1989 db_print_indent(indent);
1990 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
1991 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
1993 db_print_indent(indent);
1994 db_printf("ts_recent: %u ts_recent_age: %u\n",
1995 tp->ts_recent, tp->ts_recent_age);
1997 db_print_indent(indent);
1998 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
1999 "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2001 db_print_indent(indent);
2002 db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x "
2003 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2004 tp->snd_recover_prev, tp->t_badrxtwin);
2006 db_print_indent(indent);
2007 db_printf("snd_numholes: %d snd_holes first: %p\n",
2008 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2010 db_print_indent(indent);
2011 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
2012 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2014 /* Skip sackblks, sackhint. */
2016 db_print_indent(indent);
2017 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2018 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2021 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2026 db_printf("usage: show tcpcb <addr>\n");
2029 tp = (struct tcpcb *)addr;
2031 db_print_tcpcb(tp, "tcpcb", 0);