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_kdtrace.h>
74 #include <netinet/in_pcb.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/in_var.h>
77 #include <netinet/ip_var.h>
79 #include <netinet/ip6.h>
80 #include <netinet6/in6_pcb.h>
81 #include <netinet6/ip6_var.h>
82 #include <netinet6/scope6_var.h>
84 #include <netinet/tcp_fsm.h>
85 #include <netinet/tcp_seq.h>
86 #include <netinet/tcp_timer.h>
87 #include <netinet/tcp_var.h>
88 #include <netinet/tcpip.h>
90 #include <netinet/tcp_debug.h>
93 #include <netinet/tcp_offload.h>
97 * TCP protocol interface to socket abstraction.
99 static int tcp_attach(struct socket *);
101 static int tcp_connect(struct tcpcb *, struct sockaddr *,
105 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
108 static void tcp_disconnect(struct tcpcb *);
109 static void tcp_usrclosed(struct tcpcb *);
110 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
113 #define TCPDEBUG0 int ostate = 0
114 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
115 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
116 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
120 #define TCPDEBUG2(req)
124 * TCP attaches to socket via pru_attach(), reserving space,
125 * and an internet control block.
128 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
131 struct tcpcb *tp = NULL;
136 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
139 error = tcp_attach(so);
143 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
144 so->so_linger = TCP_LINGERTIME;
149 TCPDEBUG2(PRU_ATTACH);
150 TCP_PROBE2(debug__user, tp, PRU_ATTACH);
155 * tcp_detach is called when the socket layer loses its final reference
156 * to the socket, be it a file descriptor reference, a reference from TCP,
157 * etc. At this point, there is only one case in which we will keep around
158 * inpcb state: time wait.
160 * This function can probably be re-absorbed back into tcp_usr_detach() now
161 * that there is a single detach path.
164 tcp_detach(struct socket *so, struct inpcb *inp)
168 INP_INFO_LOCK_ASSERT(&V_tcbinfo);
169 INP_WLOCK_ASSERT(inp);
171 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
172 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
176 if (inp->inp_flags & INP_TIMEWAIT) {
178 * There are two cases to handle: one in which the time wait
179 * state is being discarded (INP_DROPPED), and one in which
180 * this connection will remain in timewait. In the former,
181 * it is time to discard all state (except tcptw, which has
182 * already been discarded by the timewait close code, which
183 * should be further up the call stack somewhere). In the
184 * latter case, we detach from the socket, but leave the pcb
185 * present until timewait ends.
187 * XXXRW: Would it be cleaner to free the tcptw here?
189 * Astute question indeed, from twtcp perspective there are
190 * three cases to consider:
192 * #1 tcp_detach is called at tcptw creation time by
193 * tcp_twstart, then do not discard the newly created tcptw
194 * and leave inpcb present until timewait ends
195 * #2 tcp_detach is called at timewait end (or reuse) by
196 * tcp_twclose, then the tcptw has already been discarded
197 * (or reused) and inpcb is freed here
198 * #3 tcp_detach is called() after timewait ends (or reuse)
199 * (e.g. by soclose), then tcptw has already been discarded
200 * (or reused) and inpcb is freed here
202 * In all three cases the tcptw should not be freed here.
204 if (inp->inp_flags & INP_DROPPED) {
205 KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
206 "INP_DROPPED && tp != NULL"));
215 * If the connection is not in timewait, we consider two
216 * two conditions: one in which no further processing is
217 * necessary (dropped || embryonic), and one in which TCP is
218 * not yet done, but no longer requires the socket, so the
219 * pcb will persist for the time being.
221 * XXXRW: Does the second case still occur?
223 if (inp->inp_flags & INP_DROPPED ||
224 tp->t_state < TCPS_SYN_SENT) {
236 * pru_detach() detaches the TCP protocol from the socket.
237 * If the protocol state is non-embryonic, then can't
238 * do this directly: have to initiate a pru_disconnect(),
239 * which may finish later; embryonic TCB's can just
243 tcp_usr_detach(struct socket *so)
249 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
250 if (!INP_INFO_WLOCKED(&V_tcbinfo)) {
251 INP_INFO_RLOCK(&V_tcbinfo);
255 KASSERT(inp->inp_socket != NULL,
256 ("tcp_usr_detach: inp_socket == NULL"));
259 INP_INFO_RUNLOCK(&V_tcbinfo);
264 * Give the socket an address.
267 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
271 struct tcpcb *tp = NULL;
272 struct sockaddr_in *sinp;
274 sinp = (struct sockaddr_in *)nam;
275 if (nam->sa_len != sizeof (*sinp))
278 * Must check for multicast addresses and disallow binding
281 if (sinp->sin_family == AF_INET &&
282 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
283 return (EAFNOSUPPORT);
287 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
289 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
295 INP_HASH_WLOCK(&V_tcbinfo);
296 error = in_pcbbind(inp, nam, td->td_ucred);
297 INP_HASH_WUNLOCK(&V_tcbinfo);
300 TCP_PROBE2(debug__user, tp, PRU_BIND);
309 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
313 struct tcpcb *tp = NULL;
314 struct sockaddr_in6 *sin6p;
316 sin6p = (struct sockaddr_in6 *)nam;
317 if (nam->sa_len != sizeof (*sin6p))
320 * Must check for multicast addresses and disallow binding
323 if (sin6p->sin6_family == AF_INET6 &&
324 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
325 return (EAFNOSUPPORT);
329 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
331 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
337 INP_HASH_WLOCK(&V_tcbinfo);
338 inp->inp_vflag &= ~INP_IPV4;
339 inp->inp_vflag |= INP_IPV6;
341 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
342 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
343 inp->inp_vflag |= INP_IPV4;
344 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
345 struct sockaddr_in sin;
347 in6_sin6_2_sin(&sin, sin6p);
348 inp->inp_vflag |= INP_IPV4;
349 inp->inp_vflag &= ~INP_IPV6;
350 error = in_pcbbind(inp, (struct sockaddr *)&sin,
352 INP_HASH_WUNLOCK(&V_tcbinfo);
357 error = in6_pcbbind(inp, nam, td->td_ucred);
358 INP_HASH_WUNLOCK(&V_tcbinfo);
361 TCP_PROBE2(debug__user, tp, PRU_BIND);
369 * Prepare to accept connections.
372 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
376 struct tcpcb *tp = NULL;
380 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
382 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
389 error = solisten_proto_check(so);
390 INP_HASH_WLOCK(&V_tcbinfo);
391 if (error == 0 && inp->inp_lport == 0)
392 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
393 INP_HASH_WUNLOCK(&V_tcbinfo);
395 tcp_state_change(tp, TCPS_LISTEN);
396 solisten_proto(so, backlog);
398 if ((so->so_options & SO_NO_OFFLOAD) == 0)
399 tcp_offload_listen_start(tp);
405 TCPDEBUG2(PRU_LISTEN);
406 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
414 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
418 struct tcpcb *tp = NULL;
422 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
424 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
431 error = solisten_proto_check(so);
432 INP_HASH_WLOCK(&V_tcbinfo);
433 if (error == 0 && inp->inp_lport == 0) {
434 inp->inp_vflag &= ~INP_IPV4;
435 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
436 inp->inp_vflag |= INP_IPV4;
437 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
439 INP_HASH_WUNLOCK(&V_tcbinfo);
441 tcp_state_change(tp, TCPS_LISTEN);
442 solisten_proto(so, backlog);
444 if ((so->so_options & SO_NO_OFFLOAD) == 0)
445 tcp_offload_listen_start(tp);
451 TCPDEBUG2(PRU_LISTEN);
452 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
460 * Initiate connection to peer.
461 * Create a template for use in transmissions on this connection.
462 * Enter SYN_SENT state, and mark socket as connecting.
463 * Start keep-alive timer, and seed output sequence space.
464 * Send initial segment on connection.
467 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
471 struct tcpcb *tp = NULL;
472 struct sockaddr_in *sinp;
474 sinp = (struct sockaddr_in *)nam;
475 if (nam->sa_len != sizeof (*sinp))
478 * Must disallow TCP ``connections'' to multicast addresses.
480 if (sinp->sin_family == AF_INET
481 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
482 return (EAFNOSUPPORT);
483 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
488 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
490 if (inp->inp_flags & INP_TIMEWAIT) {
494 if (inp->inp_flags & INP_DROPPED) {
495 error = ECONNREFUSED;
500 if ((error = tcp_connect(tp, nam, td)) != 0)
503 if (registered_toedevs > 0 &&
504 (so->so_options & SO_NO_OFFLOAD) == 0 &&
505 (error = tcp_offload_connect(so, nam)) == 0)
508 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
509 error = tcp_output(tp);
511 TCPDEBUG2(PRU_CONNECT);
519 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
523 struct tcpcb *tp = NULL;
524 struct sockaddr_in6 *sin6p;
528 sin6p = (struct sockaddr_in6 *)nam;
529 if (nam->sa_len != sizeof (*sin6p))
532 * Must disallow TCP ``connections'' to multicast addresses.
534 if (sin6p->sin6_family == AF_INET6
535 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
536 return (EAFNOSUPPORT);
539 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
541 if (inp->inp_flags & INP_TIMEWAIT) {
545 if (inp->inp_flags & INP_DROPPED) {
546 error = ECONNREFUSED;
553 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
554 * therefore probably require the hash lock, which isn't held here.
555 * Is this a significant problem?
557 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
558 struct sockaddr_in sin;
560 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
565 in6_sin6_2_sin(&sin, sin6p);
566 inp->inp_vflag |= INP_IPV4;
567 inp->inp_vflag &= ~INP_IPV6;
568 if ((error = prison_remote_ip4(td->td_ucred,
569 &sin.sin_addr)) != 0)
571 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
574 if (registered_toedevs > 0 &&
575 (so->so_options & SO_NO_OFFLOAD) == 0 &&
576 (error = tcp_offload_connect(so, nam)) == 0)
579 error = tcp_output(tp);
583 inp->inp_vflag &= ~INP_IPV4;
584 inp->inp_vflag |= INP_IPV6;
585 inp->inp_inc.inc_flags |= INC_ISIPV6;
586 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
588 if ((error = tcp6_connect(tp, nam, td)) != 0)
591 if (registered_toedevs > 0 &&
592 (so->so_options & SO_NO_OFFLOAD) == 0 &&
593 (error = tcp_offload_connect(so, nam)) == 0)
596 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
597 error = tcp_output(tp);
600 TCPDEBUG2(PRU_CONNECT);
601 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
608 * Initiate disconnect from peer.
609 * If connection never passed embryonic stage, just drop;
610 * else if don't need to let data drain, then can just drop anyways,
611 * else have to begin TCP shutdown process: mark socket disconnecting,
612 * drain unread data, state switch to reflect user close, and
613 * send segment (e.g. FIN) to peer. Socket will be really disconnected
614 * when peer sends FIN and acks ours.
616 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
619 tcp_usr_disconnect(struct socket *so)
622 struct tcpcb *tp = NULL;
626 INP_INFO_RLOCK(&V_tcbinfo);
628 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
630 if (inp->inp_flags & INP_TIMEWAIT)
632 if (inp->inp_flags & INP_DROPPED) {
640 TCPDEBUG2(PRU_DISCONNECT);
641 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
643 INP_INFO_RUNLOCK(&V_tcbinfo);
649 * Accept a connection. Essentially all the work is done at higher levels;
650 * just return the address of the peer, storing through addr.
653 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
656 struct inpcb *inp = NULL;
657 struct tcpcb *tp = NULL;
662 if (so->so_state & SS_ISDISCONNECTED)
663 return (ECONNABORTED);
666 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
668 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
669 error = ECONNABORTED;
676 * We inline in_getpeeraddr and COMMON_END here, so that we can
677 * copy the data of interest and defer the malloc until after we
680 port = inp->inp_fport;
681 addr = inp->inp_faddr;
684 TCPDEBUG2(PRU_ACCEPT);
685 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
688 *nam = in_sockaddr(port, &addr);
695 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
697 struct inpcb *inp = NULL;
699 struct tcpcb *tp = NULL;
701 struct in6_addr addr6;
706 if (so->so_state & SS_ISDISCONNECTED)
707 return (ECONNABORTED);
710 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
711 INP_INFO_RLOCK(&V_tcbinfo);
713 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
714 error = ECONNABORTED;
721 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
722 * copy the data of interest and defer the malloc until after we
725 if (inp->inp_vflag & INP_IPV4) {
727 port = inp->inp_fport;
728 addr = inp->inp_faddr;
730 port = inp->inp_fport;
731 addr6 = inp->in6p_faddr;
735 TCPDEBUG2(PRU_ACCEPT);
736 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
738 INP_INFO_RUNLOCK(&V_tcbinfo);
741 *nam = in6_v4mapsin6_sockaddr(port, &addr);
743 *nam = in6_sockaddr(port, &addr6);
750 * Mark the connection as being incapable of further output.
753 tcp_usr_shutdown(struct socket *so)
757 struct tcpcb *tp = NULL;
760 INP_INFO_RLOCK(&V_tcbinfo);
762 KASSERT(inp != NULL, ("inp == NULL"));
764 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
772 if (!(inp->inp_flags & INP_DROPPED))
773 error = tcp_output(tp);
776 TCPDEBUG2(PRU_SHUTDOWN);
777 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
779 INP_INFO_RUNLOCK(&V_tcbinfo);
785 * After a receive, possibly send window update to peer.
788 tcp_usr_rcvd(struct socket *so, int flags)
791 struct tcpcb *tp = NULL;
796 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
798 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
805 if (tp->t_flags & TF_TOE)
806 tcp_offload_rcvd(tp);
813 TCP_PROBE2(debug__user, tp, PRU_RCVD);
819 * Do a send by putting data in output queue and updating urgent
820 * marker if URG set. Possibly send more data. Unlike the other
821 * pru_*() routines, the mbuf chains are our responsibility. We
822 * must either enqueue them or free them. The other pru_* routines
823 * generally are caller-frees.
826 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
827 struct sockaddr *nam, struct mbuf *control, struct thread *td)
831 struct tcpcb *tp = NULL;
838 * We require the pcbinfo lock if we will close the socket as part of
841 if (flags & PRUS_EOF)
842 INP_INFO_RLOCK(&V_tcbinfo);
844 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
846 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
850 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
851 * for freeing memory.
853 if (m && (flags & PRUS_NOTREADY) == 0)
859 isipv6 = nam && nam->sa_family == AF_INET6;
864 /* TCP doesn't do control messages (rights, creds, etc) */
865 if (control->m_len) {
872 m_freem(control); /* empty control, just free it */
874 if (!(flags & PRUS_OOB)) {
875 sbappendstream(&so->so_snd, m, flags);
876 if (nam && tp->t_state < TCPS_SYN_SENT) {
878 * Do implied connect if not yet connected,
879 * initialize window to default value, and
880 * initialize maxseg/maxopd using peer's cached
885 error = tcp6_connect(tp, nam, td);
887 #if defined(INET6) && defined(INET)
891 error = tcp_connect(tp, nam, td);
895 tp->snd_wnd = TTCP_CLIENT_SND_WND;
898 if (flags & PRUS_EOF) {
900 * Close the send side of the connection after
903 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
907 if (!(inp->inp_flags & INP_DROPPED) &&
908 !(flags & PRUS_NOTREADY)) {
909 if (flags & PRUS_MORETOCOME)
910 tp->t_flags |= TF_MORETOCOME;
911 error = tcp_output(tp);
912 if (flags & PRUS_MORETOCOME)
913 tp->t_flags &= ~TF_MORETOCOME;
917 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
919 SOCKBUF_LOCK(&so->so_snd);
920 if (sbspace(&so->so_snd) < -512) {
921 SOCKBUF_UNLOCK(&so->so_snd);
927 * According to RFC961 (Assigned Protocols),
928 * the urgent pointer points to the last octet
929 * of urgent data. We continue, however,
930 * to consider it to indicate the first octet
931 * of data past the urgent section.
932 * Otherwise, snd_up should be one lower.
934 sbappendstream_locked(&so->so_snd, m, flags);
935 SOCKBUF_UNLOCK(&so->so_snd);
936 if (nam && tp->t_state < TCPS_SYN_SENT) {
938 * Do implied connect if not yet connected,
939 * initialize window to default value, and
940 * initialize maxseg/maxopd using peer's cached
945 error = tcp6_connect(tp, nam, td);
947 #if defined(INET6) && defined(INET)
951 error = tcp_connect(tp, nam, td);
955 tp->snd_wnd = TTCP_CLIENT_SND_WND;
958 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
959 if (!(flags & PRUS_NOTREADY)) {
960 tp->t_flags |= TF_FORCEDATA;
961 error = tcp_output(tp);
962 tp->t_flags &= ~TF_FORCEDATA;
966 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
967 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
968 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
969 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
971 if (flags & PRUS_EOF)
972 INP_INFO_RUNLOCK(&V_tcbinfo);
977 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
985 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
987 for (int i = 0; i < count; i++)
993 SOCKBUF_LOCK(&so->so_snd);
994 error = sbready(&so->so_snd, m, count);
995 SOCKBUF_UNLOCK(&so->so_snd);
997 error = tcp_output(tp);
1004 * Abort the TCP. Drop the connection abruptly.
1007 tcp_usr_abort(struct socket *so)
1010 struct tcpcb *tp = NULL;
1013 inp = sotoinpcb(so);
1014 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1016 INP_INFO_RLOCK(&V_tcbinfo);
1018 KASSERT(inp->inp_socket != NULL,
1019 ("tcp_usr_abort: inp_socket == NULL"));
1022 * If we still have full TCP state, and we're not dropped, drop.
1024 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1025 !(inp->inp_flags & INP_DROPPED)) {
1026 tp = intotcpcb(inp);
1028 tcp_drop(tp, ECONNABORTED);
1029 TCPDEBUG2(PRU_ABORT);
1030 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1032 if (!(inp->inp_flags & INP_DROPPED)) {
1034 so->so_state |= SS_PROTOREF;
1036 inp->inp_flags |= INP_SOCKREF;
1039 INP_INFO_RUNLOCK(&V_tcbinfo);
1043 * TCP socket is closed. Start friendly disconnect.
1046 tcp_usr_close(struct socket *so)
1049 struct tcpcb *tp = NULL;
1052 inp = sotoinpcb(so);
1053 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1055 INP_INFO_RLOCK(&V_tcbinfo);
1057 KASSERT(inp->inp_socket != NULL,
1058 ("tcp_usr_close: inp_socket == NULL"));
1061 * If we still have full TCP state, and we're not dropped, initiate
1064 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1065 !(inp->inp_flags & INP_DROPPED)) {
1066 tp = intotcpcb(inp);
1069 TCPDEBUG2(PRU_CLOSE);
1070 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1072 if (!(inp->inp_flags & INP_DROPPED)) {
1074 so->so_state |= SS_PROTOREF;
1076 inp->inp_flags |= INP_SOCKREF;
1079 INP_INFO_RUNLOCK(&V_tcbinfo);
1083 * Receive out-of-band data.
1086 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1090 struct tcpcb *tp = NULL;
1093 inp = sotoinpcb(so);
1094 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1096 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1100 tp = intotcpcb(inp);
1102 if ((so->so_oobmark == 0 &&
1103 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1104 so->so_options & SO_OOBINLINE ||
1105 tp->t_oobflags & TCPOOB_HADDATA) {
1109 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1110 error = EWOULDBLOCK;
1114 *mtod(m, caddr_t) = tp->t_iobc;
1115 if ((flags & MSG_PEEK) == 0)
1116 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1119 TCPDEBUG2(PRU_RCVOOB);
1120 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1126 struct pr_usrreqs tcp_usrreqs = {
1127 .pru_abort = tcp_usr_abort,
1128 .pru_accept = tcp_usr_accept,
1129 .pru_attach = tcp_usr_attach,
1130 .pru_bind = tcp_usr_bind,
1131 .pru_connect = tcp_usr_connect,
1132 .pru_control = in_control,
1133 .pru_detach = tcp_usr_detach,
1134 .pru_disconnect = tcp_usr_disconnect,
1135 .pru_listen = tcp_usr_listen,
1136 .pru_peeraddr = in_getpeeraddr,
1137 .pru_rcvd = tcp_usr_rcvd,
1138 .pru_rcvoob = tcp_usr_rcvoob,
1139 .pru_send = tcp_usr_send,
1140 .pru_ready = tcp_usr_ready,
1141 .pru_shutdown = tcp_usr_shutdown,
1142 .pru_sockaddr = in_getsockaddr,
1143 .pru_sosetlabel = in_pcbsosetlabel,
1144 .pru_close = tcp_usr_close,
1149 struct pr_usrreqs tcp6_usrreqs = {
1150 .pru_abort = tcp_usr_abort,
1151 .pru_accept = tcp6_usr_accept,
1152 .pru_attach = tcp_usr_attach,
1153 .pru_bind = tcp6_usr_bind,
1154 .pru_connect = tcp6_usr_connect,
1155 .pru_control = in6_control,
1156 .pru_detach = tcp_usr_detach,
1157 .pru_disconnect = tcp_usr_disconnect,
1158 .pru_listen = tcp6_usr_listen,
1159 .pru_peeraddr = in6_mapped_peeraddr,
1160 .pru_rcvd = tcp_usr_rcvd,
1161 .pru_rcvoob = tcp_usr_rcvoob,
1162 .pru_send = tcp_usr_send,
1163 .pru_ready = tcp_usr_ready,
1164 .pru_shutdown = tcp_usr_shutdown,
1165 .pru_sockaddr = in6_mapped_sockaddr,
1166 .pru_sosetlabel = in_pcbsosetlabel,
1167 .pru_close = tcp_usr_close,
1173 * Common subroutine to open a TCP connection to remote host specified
1174 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1175 * port number if needed. Call in_pcbconnect_setup to do the routing and
1176 * to choose a local host address (interface). If there is an existing
1177 * incarnation of the same connection in TIME-WAIT state and if the remote
1178 * host was sending CC options and if the connection duration was < MSL, then
1179 * truncate the previous TIME-WAIT state and proceed.
1180 * Initialize connection parameters and enter SYN-SENT state.
1183 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1185 struct inpcb *inp = tp->t_inpcb, *oinp;
1186 struct socket *so = inp->inp_socket;
1187 struct in_addr laddr;
1191 INP_WLOCK_ASSERT(inp);
1192 INP_HASH_WLOCK(&V_tcbinfo);
1194 if (inp->inp_lport == 0) {
1195 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1201 * Cannot simply call in_pcbconnect, because there might be an
1202 * earlier incarnation of this same connection still in
1203 * TIME_WAIT state, creating an ADDRINUSE error.
1205 laddr = inp->inp_laddr;
1206 lport = inp->inp_lport;
1207 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1208 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1209 if (error && oinp == NULL)
1215 inp->inp_laddr = laddr;
1217 INP_HASH_WUNLOCK(&V_tcbinfo);
1220 * Compute window scaling to request:
1221 * Scale to fit into sweet spot. See tcp_syncache.c.
1222 * XXX: This should move to tcp_output().
1224 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1225 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1226 tp->request_r_scale++;
1229 TCPSTAT_INC(tcps_connattempt);
1230 tcp_state_change(tp, TCPS_SYN_SENT);
1231 tp->iss = tcp_new_isn(tp);
1232 tcp_sendseqinit(tp);
1237 INP_HASH_WUNLOCK(&V_tcbinfo);
1244 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1246 struct inpcb *inp = tp->t_inpcb;
1249 INP_WLOCK_ASSERT(inp);
1250 INP_HASH_WLOCK(&V_tcbinfo);
1252 if (inp->inp_lport == 0) {
1253 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1257 error = in6_pcbconnect(inp, nam, td->td_ucred);
1260 INP_HASH_WUNLOCK(&V_tcbinfo);
1262 /* Compute window scaling to request. */
1263 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1264 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1265 tp->request_r_scale++;
1267 soisconnecting(inp->inp_socket);
1268 TCPSTAT_INC(tcps_connattempt);
1269 tcp_state_change(tp, TCPS_SYN_SENT);
1270 tp->iss = tcp_new_isn(tp);
1271 tcp_sendseqinit(tp);
1276 INP_HASH_WUNLOCK(&V_tcbinfo);
1282 * Export TCP internal state information via a struct tcp_info, based on the
1283 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1284 * (TCP state machine, etc). We export all information using FreeBSD-native
1285 * constants -- for example, the numeric values for tcpi_state will differ
1289 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1292 INP_WLOCK_ASSERT(tp->t_inpcb);
1293 bzero(ti, sizeof(*ti));
1295 ti->tcpi_state = tp->t_state;
1296 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1297 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1298 if (tp->t_flags & TF_SACK_PERMIT)
1299 ti->tcpi_options |= TCPI_OPT_SACK;
1300 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1301 ti->tcpi_options |= TCPI_OPT_WSCALE;
1302 ti->tcpi_snd_wscale = tp->snd_scale;
1303 ti->tcpi_rcv_wscale = tp->rcv_scale;
1306 ti->tcpi_rto = tp->t_rxtcur * tick;
1307 ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
1308 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1309 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1311 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1312 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1315 * FreeBSD-specific extension fields for tcp_info.
1317 ti->tcpi_rcv_space = tp->rcv_wnd;
1318 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1319 ti->tcpi_snd_wnd = tp->snd_wnd;
1320 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1321 ti->tcpi_snd_nxt = tp->snd_nxt;
1322 ti->tcpi_snd_mss = tp->t_maxseg;
1323 ti->tcpi_rcv_mss = tp->t_maxseg;
1324 if (tp->t_flags & TF_TOE)
1325 ti->tcpi_options |= TCPI_OPT_TOE;
1326 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1327 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1328 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1332 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1333 * socket option arguments. When it re-acquires the lock after the copy, it
1334 * has to revalidate that the connection is still valid for the socket
1337 #define INP_WLOCK_RECHECK(inp) do { \
1339 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1341 return (ECONNRESET); \
1343 tp = intotcpcb(inp); \
1347 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1349 int error, opt, optval;
1354 char buf[TCP_CA_NAME_MAX];
1355 struct cc_algo *algo;
1358 inp = sotoinpcb(so);
1359 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1361 if (sopt->sopt_level != IPPROTO_TCP) {
1363 if (inp->inp_vflag & INP_IPV6PROTO) {
1365 error = ip6_ctloutput(so, sopt);
1368 #if defined(INET6) && defined(INET)
1374 error = ip_ctloutput(so, sopt);
1379 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1381 return (ECONNRESET);
1384 switch (sopt->sopt_dir) {
1386 switch (sopt->sopt_name) {
1387 #ifdef TCP_SIGNATURE
1390 error = sooptcopyin(sopt, &optval, sizeof optval,
1395 INP_WLOCK_RECHECK(inp);
1397 tp->t_flags |= TF_SIGNATURE;
1399 tp->t_flags &= ~TF_SIGNATURE;
1400 goto unlock_and_done;
1401 #endif /* TCP_SIGNATURE */
1406 error = sooptcopyin(sopt, &optval, sizeof optval,
1411 INP_WLOCK_RECHECK(inp);
1412 switch (sopt->sopt_name) {
1420 opt = 0; /* dead code to fool gcc */
1427 tp->t_flags &= ~opt;
1430 if (tp->t_flags & TF_TOE) {
1431 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1440 error = sooptcopyin(sopt, &optval, sizeof optval,
1445 INP_WLOCK_RECHECK(inp);
1447 tp->t_flags |= TF_NOPUSH;
1448 else if (tp->t_flags & TF_NOPUSH) {
1449 tp->t_flags &= ~TF_NOPUSH;
1450 if (TCPS_HAVEESTABLISHED(tp->t_state))
1451 error = tcp_output(tp);
1453 goto unlock_and_done;
1457 error = sooptcopyin(sopt, &optval, sizeof optval,
1462 INP_WLOCK_RECHECK(inp);
1463 if (optval > 0 && optval <= tp->t_maxseg &&
1464 optval + 40 >= V_tcp_minmss)
1465 tp->t_maxseg = optval;
1468 goto unlock_and_done;
1475 case TCP_CONGESTION:
1477 bzero(buf, sizeof(buf));
1478 error = sooptcopyin(sopt, &buf, sizeof(buf), 1);
1481 INP_WLOCK_RECHECK(inp);
1483 * Return EINVAL if we can't find the requested cc algo.
1487 STAILQ_FOREACH(algo, &cc_list, entries) {
1488 if (strncmp(buf, algo->name, TCP_CA_NAME_MAX)
1490 /* We've found the requested algo. */
1493 * We hold a write lock over the tcb
1494 * so it's safe to do these things
1495 * without ordering concerns.
1497 if (CC_ALGO(tp)->cb_destroy != NULL)
1498 CC_ALGO(tp)->cb_destroy(tp->ccv);
1501 * If something goes pear shaped
1502 * initialising the new algo,
1503 * fall back to newreno (which
1504 * does not require initialisation).
1506 if (algo->cb_init != NULL)
1507 if (algo->cb_init(tp->ccv) > 0) {
1508 CC_ALGO(tp) = &newreno_cc_algo;
1510 * The only reason init
1512 * because of malloc.
1516 break; /* Break the STAILQ_FOREACH. */
1520 goto unlock_and_done;
1526 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1530 if (ui > (UINT_MAX / hz)) {
1536 INP_WLOCK_RECHECK(inp);
1537 switch (sopt->sopt_name) {
1539 tp->t_keepidle = ui;
1541 * XXX: better check current remaining
1542 * timeout and "merge" it with new value.
1544 if ((tp->t_state > TCPS_LISTEN) &&
1545 (tp->t_state <= TCPS_CLOSING))
1546 tcp_timer_activate(tp, TT_KEEP,
1550 tp->t_keepintvl = ui;
1551 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1552 (TP_MAXIDLE(tp) > 0))
1553 tcp_timer_activate(tp, TT_2MSL,
1557 tp->t_keepinit = ui;
1558 if (tp->t_state == TCPS_SYN_RECEIVED ||
1559 tp->t_state == TCPS_SYN_SENT)
1560 tcp_timer_activate(tp, TT_KEEP,
1564 goto unlock_and_done;
1568 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1572 INP_WLOCK_RECHECK(inp);
1574 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1575 (TP_MAXIDLE(tp) > 0))
1576 tcp_timer_activate(tp, TT_2MSL,
1578 goto unlock_and_done;
1582 error = ENOPROTOOPT;
1588 tp = intotcpcb(inp);
1589 switch (sopt->sopt_name) {
1590 #ifdef TCP_SIGNATURE
1592 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1594 error = sooptcopyout(sopt, &optval, sizeof optval);
1599 optval = tp->t_flags & TF_NODELAY;
1601 error = sooptcopyout(sopt, &optval, sizeof optval);
1604 optval = tp->t_maxseg;
1606 error = sooptcopyout(sopt, &optval, sizeof optval);
1609 optval = tp->t_flags & TF_NOOPT;
1611 error = sooptcopyout(sopt, &optval, sizeof optval);
1614 optval = tp->t_flags & TF_NOPUSH;
1616 error = sooptcopyout(sopt, &optval, sizeof optval);
1619 tcp_fill_info(tp, &ti);
1621 error = sooptcopyout(sopt, &ti, sizeof ti);
1623 case TCP_CONGESTION:
1624 bzero(buf, sizeof(buf));
1625 strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1627 error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX);
1633 switch (sopt->sopt_name) {
1635 ui = tp->t_keepidle / hz;
1638 ui = tp->t_keepintvl / hz;
1641 ui = tp->t_keepinit / hz;
1648 error = sooptcopyout(sopt, &ui, sizeof(ui));
1652 error = ENOPROTOOPT;
1659 #undef INP_WLOCK_RECHECK
1662 * Attach TCP protocol to socket, allocating
1663 * internet protocol control block, tcp control block,
1664 * bufer space, and entering LISTEN state if to accept connections.
1667 tcp_attach(struct socket *so)
1673 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1674 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1678 so->so_rcv.sb_flags |= SB_AUTOSIZE;
1679 so->so_snd.sb_flags |= SB_AUTOSIZE;
1680 INP_INFO_RLOCK(&V_tcbinfo);
1681 error = in_pcballoc(so, &V_tcbinfo);
1683 INP_INFO_RUNLOCK(&V_tcbinfo);
1686 inp = sotoinpcb(so);
1688 if (inp->inp_vflag & INP_IPV6PROTO) {
1689 inp->inp_vflag |= INP_IPV6;
1690 inp->in6p_hops = -1; /* use kernel default */
1694 inp->inp_vflag |= INP_IPV4;
1695 tp = tcp_newtcpcb(inp);
1699 INP_INFO_RUNLOCK(&V_tcbinfo);
1702 tp->t_state = TCPS_CLOSED;
1704 INP_INFO_RUNLOCK(&V_tcbinfo);
1709 * Initiate (or continue) disconnect.
1710 * If embryonic state, just send reset (once).
1711 * If in ``let data drain'' option and linger null, just drop.
1712 * Otherwise (hard), mark socket disconnecting and drop
1713 * current input data; switch states based on user close, and
1714 * send segment to peer (with FIN).
1717 tcp_disconnect(struct tcpcb *tp)
1719 struct inpcb *inp = tp->t_inpcb;
1720 struct socket *so = inp->inp_socket;
1722 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1723 INP_WLOCK_ASSERT(inp);
1726 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1727 * socket is still open.
1729 if (tp->t_state < TCPS_ESTABLISHED) {
1732 ("tcp_disconnect: tcp_close() returned NULL"));
1733 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1734 tp = tcp_drop(tp, 0);
1736 ("tcp_disconnect: tcp_drop() returned NULL"));
1738 soisdisconnecting(so);
1739 sbflush(&so->so_rcv);
1741 if (!(inp->inp_flags & INP_DROPPED))
1747 * User issued close, and wish to trail through shutdown states:
1748 * if never received SYN, just forget it. If got a SYN from peer,
1749 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1750 * If already got a FIN from peer, then almost done; go to LAST_ACK
1751 * state. In all other cases, have already sent FIN to peer (e.g.
1752 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1753 * for peer to send FIN or not respond to keep-alives, etc.
1754 * We can let the user exit from the close as soon as the FIN is acked.
1757 tcp_usrclosed(struct tcpcb *tp)
1760 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1761 INP_WLOCK_ASSERT(tp->t_inpcb);
1763 switch (tp->t_state) {
1766 tcp_offload_listen_stop(tp);
1768 tcp_state_change(tp, TCPS_CLOSED);
1773 * tcp_close() should never return NULL here as the socket is
1777 ("tcp_usrclosed: tcp_close() returned NULL"));
1781 case TCPS_SYN_RECEIVED:
1782 tp->t_flags |= TF_NEEDFIN;
1785 case TCPS_ESTABLISHED:
1786 tcp_state_change(tp, TCPS_FIN_WAIT_1);
1789 case TCPS_CLOSE_WAIT:
1790 tcp_state_change(tp, TCPS_LAST_ACK);
1793 if (tp->t_state >= TCPS_FIN_WAIT_2) {
1794 soisdisconnected(tp->t_inpcb->inp_socket);
1795 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
1796 if (tp->t_state == TCPS_FIN_WAIT_2) {
1799 timeout = (tcp_fast_finwait2_recycle) ?
1800 tcp_finwait2_timeout : TP_MAXIDLE(tp);
1801 tcp_timer_activate(tp, TT_2MSL, timeout);
1808 db_print_indent(int indent)
1812 for (i = 0; i < indent; i++)
1817 db_print_tstate(int t_state)
1822 db_printf("TCPS_CLOSED");
1826 db_printf("TCPS_LISTEN");
1830 db_printf("TCPS_SYN_SENT");
1833 case TCPS_SYN_RECEIVED:
1834 db_printf("TCPS_SYN_RECEIVED");
1837 case TCPS_ESTABLISHED:
1838 db_printf("TCPS_ESTABLISHED");
1841 case TCPS_CLOSE_WAIT:
1842 db_printf("TCPS_CLOSE_WAIT");
1845 case TCPS_FIN_WAIT_1:
1846 db_printf("TCPS_FIN_WAIT_1");
1850 db_printf("TCPS_CLOSING");
1854 db_printf("TCPS_LAST_ACK");
1857 case TCPS_FIN_WAIT_2:
1858 db_printf("TCPS_FIN_WAIT_2");
1861 case TCPS_TIME_WAIT:
1862 db_printf("TCPS_TIME_WAIT");
1866 db_printf("unknown");
1872 db_print_tflags(u_int t_flags)
1877 if (t_flags & TF_ACKNOW) {
1878 db_printf("%sTF_ACKNOW", comma ? ", " : "");
1881 if (t_flags & TF_DELACK) {
1882 db_printf("%sTF_DELACK", comma ? ", " : "");
1885 if (t_flags & TF_NODELAY) {
1886 db_printf("%sTF_NODELAY", comma ? ", " : "");
1889 if (t_flags & TF_NOOPT) {
1890 db_printf("%sTF_NOOPT", comma ? ", " : "");
1893 if (t_flags & TF_SENTFIN) {
1894 db_printf("%sTF_SENTFIN", comma ? ", " : "");
1897 if (t_flags & TF_REQ_SCALE) {
1898 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1901 if (t_flags & TF_RCVD_SCALE) {
1902 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1905 if (t_flags & TF_REQ_TSTMP) {
1906 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1909 if (t_flags & TF_RCVD_TSTMP) {
1910 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1913 if (t_flags & TF_SACK_PERMIT) {
1914 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1917 if (t_flags & TF_NEEDSYN) {
1918 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1921 if (t_flags & TF_NEEDFIN) {
1922 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1925 if (t_flags & TF_NOPUSH) {
1926 db_printf("%sTF_NOPUSH", comma ? ", " : "");
1929 if (t_flags & TF_MORETOCOME) {
1930 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1933 if (t_flags & TF_LQ_OVERFLOW) {
1934 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1937 if (t_flags & TF_LASTIDLE) {
1938 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1941 if (t_flags & TF_RXWIN0SENT) {
1942 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1945 if (t_flags & TF_FASTRECOVERY) {
1946 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1949 if (t_flags & TF_CONGRECOVERY) {
1950 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
1953 if (t_flags & TF_WASFRECOVERY) {
1954 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1957 if (t_flags & TF_SIGNATURE) {
1958 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1961 if (t_flags & TF_FORCEDATA) {
1962 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1965 if (t_flags & TF_TSO) {
1966 db_printf("%sTF_TSO", comma ? ", " : "");
1969 if (t_flags & TF_ECN_PERMIT) {
1970 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
1976 db_print_toobflags(char t_oobflags)
1981 if (t_oobflags & TCPOOB_HAVEDATA) {
1982 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1985 if (t_oobflags & TCPOOB_HADDATA) {
1986 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1992 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1995 db_print_indent(indent);
1996 db_printf("%s at %p\n", name, tp);
2000 db_print_indent(indent);
2001 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2002 LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2004 db_print_indent(indent);
2005 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
2006 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2008 db_print_indent(indent);
2009 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2010 &tp->t_timers->tt_delack, tp->t_inpcb);
2012 db_print_indent(indent);
2013 db_printf("t_state: %d (", tp->t_state);
2014 db_print_tstate(tp->t_state);
2017 db_print_indent(indent);
2018 db_printf("t_flags: 0x%x (", tp->t_flags);
2019 db_print_tflags(tp->t_flags);
2022 db_print_indent(indent);
2023 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2024 tp->snd_una, tp->snd_max, tp->snd_nxt);
2026 db_print_indent(indent);
2027 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2028 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2030 db_print_indent(indent);
2031 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2032 tp->iss, tp->irs, tp->rcv_nxt);
2034 db_print_indent(indent);
2035 db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n",
2036 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2038 db_print_indent(indent);
2039 db_printf("snd_wnd: %lu snd_cwnd: %lu\n",
2040 tp->snd_wnd, tp->snd_cwnd);
2042 db_print_indent(indent);
2043 db_printf("snd_ssthresh: %lu snd_recover: "
2044 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2046 db_print_indent(indent);
2047 db_printf("t_maxopd: %u t_rcvtime: %u t_startime: %u\n",
2048 tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
2050 db_print_indent(indent);
2051 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2052 tp->t_rtttime, tp->t_rtseq);
2054 db_print_indent(indent);
2055 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2056 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2058 db_print_indent(indent);
2059 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2060 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2063 db_print_indent(indent);
2064 db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n",
2065 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2067 db_print_indent(indent);
2068 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2069 db_print_toobflags(tp->t_oobflags);
2070 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2072 db_print_indent(indent);
2073 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2074 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2076 db_print_indent(indent);
2077 db_printf("ts_recent: %u ts_recent_age: %u\n",
2078 tp->ts_recent, tp->ts_recent_age);
2080 db_print_indent(indent);
2081 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2082 "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2084 db_print_indent(indent);
2085 db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x "
2086 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2087 tp->snd_recover_prev, tp->t_badrxtwin);
2089 db_print_indent(indent);
2090 db_printf("snd_numholes: %d snd_holes first: %p\n",
2091 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2093 db_print_indent(indent);
2094 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
2095 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2097 /* Skip sackblks, sackhint. */
2099 db_print_indent(indent);
2100 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2101 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2104 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2109 db_printf("usage: show tcpcb <addr>\n");
2112 tp = (struct tcpcb *)addr;
2114 db_print_tcpcb(tp, "tcpcb", 0);