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_pcap.h>
93 #include <netinet/tcp_debug.h>
96 #include <netinet/tcp_offload.h>
100 * TCP protocol interface to socket abstraction.
102 static int tcp_attach(struct socket *);
104 static int tcp_connect(struct tcpcb *, struct sockaddr *,
108 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
111 static void tcp_disconnect(struct tcpcb *);
112 static void tcp_usrclosed(struct tcpcb *);
113 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
116 #define TCPDEBUG0 int ostate = 0
117 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
118 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
119 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
123 #define TCPDEBUG2(req)
127 * TCP attaches to socket via pru_attach(), reserving space,
128 * and an internet control block.
131 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
134 struct tcpcb *tp = NULL;
139 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
142 error = tcp_attach(so);
146 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
147 so->so_linger = TCP_LINGERTIME;
152 TCPDEBUG2(PRU_ATTACH);
153 TCP_PROBE2(debug__user, tp, PRU_ATTACH);
158 * tcp_detach is called when the socket layer loses its final reference
159 * to the socket, be it a file descriptor reference, a reference from TCP,
160 * etc. At this point, there is only one case in which we will keep around
161 * inpcb state: time wait.
163 * This function can probably be re-absorbed back into tcp_usr_detach() now
164 * that there is a single detach path.
167 tcp_detach(struct socket *so, struct inpcb *inp)
171 INP_INFO_LOCK_ASSERT(&V_tcbinfo);
172 INP_WLOCK_ASSERT(inp);
174 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
175 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
179 if (inp->inp_flags & INP_TIMEWAIT) {
181 * There are two cases to handle: one in which the time wait
182 * state is being discarded (INP_DROPPED), and one in which
183 * this connection will remain in timewait. In the former,
184 * it is time to discard all state (except tcptw, which has
185 * already been discarded by the timewait close code, which
186 * should be further up the call stack somewhere). In the
187 * latter case, we detach from the socket, but leave the pcb
188 * present until timewait ends.
190 * XXXRW: Would it be cleaner to free the tcptw here?
192 * Astute question indeed, from twtcp perspective there are
193 * three cases to consider:
195 * #1 tcp_detach is called at tcptw creation time by
196 * tcp_twstart, then do not discard the newly created tcptw
197 * and leave inpcb present until timewait ends
198 * #2 tcp_detach is called at timewait end (or reuse) by
199 * tcp_twclose, then the tcptw has already been discarded
200 * (or reused) and inpcb is freed here
201 * #3 tcp_detach is called() after timewait ends (or reuse)
202 * (e.g. by soclose), then tcptw has already been discarded
203 * (or reused) and inpcb is freed here
205 * In all three cases the tcptw should not be freed here.
207 if (inp->inp_flags & INP_DROPPED) {
208 KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
209 "INP_DROPPED && tp != NULL"));
218 * If the connection is not in timewait, we consider two
219 * two conditions: one in which no further processing is
220 * necessary (dropped || embryonic), and one in which TCP is
221 * not yet done, but no longer requires the socket, so the
222 * pcb will persist for the time being.
224 * XXXRW: Does the second case still occur?
226 if (inp->inp_flags & INP_DROPPED ||
227 tp->t_state < TCPS_SYN_SENT) {
239 * pru_detach() detaches the TCP protocol from the socket.
240 * If the protocol state is non-embryonic, then can't
241 * do this directly: have to initiate a pru_disconnect(),
242 * which may finish later; embryonic TCB's can just
246 tcp_usr_detach(struct socket *so)
252 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
253 if (!INP_INFO_WLOCKED(&V_tcbinfo)) {
254 INP_INFO_RLOCK(&V_tcbinfo);
258 KASSERT(inp->inp_socket != NULL,
259 ("tcp_usr_detach: inp_socket == NULL"));
262 INP_INFO_RUNLOCK(&V_tcbinfo);
267 * Give the socket an address.
270 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
274 struct tcpcb *tp = NULL;
275 struct sockaddr_in *sinp;
277 sinp = (struct sockaddr_in *)nam;
278 if (nam->sa_len != sizeof (*sinp))
281 * Must check for multicast addresses and disallow binding
284 if (sinp->sin_family == AF_INET &&
285 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
286 return (EAFNOSUPPORT);
290 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
292 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
298 INP_HASH_WLOCK(&V_tcbinfo);
299 error = in_pcbbind(inp, nam, td->td_ucred);
300 INP_HASH_WUNLOCK(&V_tcbinfo);
303 TCP_PROBE2(debug__user, tp, PRU_BIND);
312 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
316 struct tcpcb *tp = NULL;
317 struct sockaddr_in6 *sin6p;
319 sin6p = (struct sockaddr_in6 *)nam;
320 if (nam->sa_len != sizeof (*sin6p))
323 * Must check for multicast addresses and disallow binding
326 if (sin6p->sin6_family == AF_INET6 &&
327 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
328 return (EAFNOSUPPORT);
332 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
334 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
340 INP_HASH_WLOCK(&V_tcbinfo);
341 inp->inp_vflag &= ~INP_IPV4;
342 inp->inp_vflag |= INP_IPV6;
344 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
345 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
346 inp->inp_vflag |= INP_IPV4;
347 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
348 struct sockaddr_in sin;
350 in6_sin6_2_sin(&sin, sin6p);
351 inp->inp_vflag |= INP_IPV4;
352 inp->inp_vflag &= ~INP_IPV6;
353 error = in_pcbbind(inp, (struct sockaddr *)&sin,
355 INP_HASH_WUNLOCK(&V_tcbinfo);
360 error = in6_pcbbind(inp, nam, td->td_ucred);
361 INP_HASH_WUNLOCK(&V_tcbinfo);
364 TCP_PROBE2(debug__user, tp, PRU_BIND);
372 * Prepare to accept connections.
375 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
379 struct tcpcb *tp = NULL;
383 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
385 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
392 error = solisten_proto_check(so);
393 INP_HASH_WLOCK(&V_tcbinfo);
394 if (error == 0 && inp->inp_lport == 0)
395 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
396 INP_HASH_WUNLOCK(&V_tcbinfo);
398 tcp_state_change(tp, TCPS_LISTEN);
399 solisten_proto(so, backlog);
401 if ((so->so_options & SO_NO_OFFLOAD) == 0)
402 tcp_offload_listen_start(tp);
408 TCPDEBUG2(PRU_LISTEN);
409 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
417 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
421 struct tcpcb *tp = NULL;
425 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
427 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
434 error = solisten_proto_check(so);
435 INP_HASH_WLOCK(&V_tcbinfo);
436 if (error == 0 && inp->inp_lport == 0) {
437 inp->inp_vflag &= ~INP_IPV4;
438 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
439 inp->inp_vflag |= INP_IPV4;
440 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
442 INP_HASH_WUNLOCK(&V_tcbinfo);
444 tcp_state_change(tp, TCPS_LISTEN);
445 solisten_proto(so, backlog);
447 if ((so->so_options & SO_NO_OFFLOAD) == 0)
448 tcp_offload_listen_start(tp);
454 TCPDEBUG2(PRU_LISTEN);
455 TCP_PROBE2(debug__user, tp, PRU_LISTEN);
463 * Initiate connection to peer.
464 * Create a template for use in transmissions on this connection.
465 * Enter SYN_SENT state, and mark socket as connecting.
466 * Start keep-alive timer, and seed output sequence space.
467 * Send initial segment on connection.
470 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
474 struct tcpcb *tp = NULL;
475 struct sockaddr_in *sinp;
477 sinp = (struct sockaddr_in *)nam;
478 if (nam->sa_len != sizeof (*sinp))
481 * Must disallow TCP ``connections'' to multicast addresses.
483 if (sinp->sin_family == AF_INET
484 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
485 return (EAFNOSUPPORT);
486 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
491 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
493 if (inp->inp_flags & INP_TIMEWAIT) {
497 if (inp->inp_flags & INP_DROPPED) {
498 error = ECONNREFUSED;
503 if ((error = tcp_connect(tp, nam, td)) != 0)
506 if (registered_toedevs > 0 &&
507 (so->so_options & SO_NO_OFFLOAD) == 0 &&
508 (error = tcp_offload_connect(so, nam)) == 0)
511 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
512 error = tcp_output(tp);
514 TCPDEBUG2(PRU_CONNECT);
522 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
526 struct tcpcb *tp = NULL;
527 struct sockaddr_in6 *sin6p;
531 sin6p = (struct sockaddr_in6 *)nam;
532 if (nam->sa_len != sizeof (*sin6p))
535 * Must disallow TCP ``connections'' to multicast addresses.
537 if (sin6p->sin6_family == AF_INET6
538 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
539 return (EAFNOSUPPORT);
542 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
544 if (inp->inp_flags & INP_TIMEWAIT) {
548 if (inp->inp_flags & INP_DROPPED) {
549 error = ECONNREFUSED;
556 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
557 * therefore probably require the hash lock, which isn't held here.
558 * Is this a significant problem?
560 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
561 struct sockaddr_in sin;
563 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
568 in6_sin6_2_sin(&sin, sin6p);
569 inp->inp_vflag |= INP_IPV4;
570 inp->inp_vflag &= ~INP_IPV6;
571 if ((error = prison_remote_ip4(td->td_ucred,
572 &sin.sin_addr)) != 0)
574 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
577 if (registered_toedevs > 0 &&
578 (so->so_options & SO_NO_OFFLOAD) == 0 &&
579 (error = tcp_offload_connect(so, nam)) == 0)
582 error = tcp_output(tp);
586 inp->inp_vflag &= ~INP_IPV4;
587 inp->inp_vflag |= INP_IPV6;
588 inp->inp_inc.inc_flags |= INC_ISIPV6;
589 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
591 if ((error = tcp6_connect(tp, nam, td)) != 0)
594 if (registered_toedevs > 0 &&
595 (so->so_options & SO_NO_OFFLOAD) == 0 &&
596 (error = tcp_offload_connect(so, nam)) == 0)
599 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
600 error = tcp_output(tp);
603 TCPDEBUG2(PRU_CONNECT);
604 TCP_PROBE2(debug__user, tp, PRU_CONNECT);
611 * Initiate disconnect from peer.
612 * If connection never passed embryonic stage, just drop;
613 * else if don't need to let data drain, then can just drop anyways,
614 * else have to begin TCP shutdown process: mark socket disconnecting,
615 * drain unread data, state switch to reflect user close, and
616 * send segment (e.g. FIN) to peer. Socket will be really disconnected
617 * when peer sends FIN and acks ours.
619 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
622 tcp_usr_disconnect(struct socket *so)
625 struct tcpcb *tp = NULL;
629 INP_INFO_RLOCK(&V_tcbinfo);
631 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
633 if (inp->inp_flags & INP_TIMEWAIT)
635 if (inp->inp_flags & INP_DROPPED) {
643 TCPDEBUG2(PRU_DISCONNECT);
644 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
646 INP_INFO_RUNLOCK(&V_tcbinfo);
652 * Accept a connection. Essentially all the work is done at higher levels;
653 * just return the address of the peer, storing through addr.
656 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
659 struct inpcb *inp = NULL;
660 struct tcpcb *tp = NULL;
665 if (so->so_state & SS_ISDISCONNECTED)
666 return (ECONNABORTED);
669 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
671 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
672 error = ECONNABORTED;
679 * We inline in_getpeeraddr and COMMON_END here, so that we can
680 * copy the data of interest and defer the malloc until after we
683 port = inp->inp_fport;
684 addr = inp->inp_faddr;
687 TCPDEBUG2(PRU_ACCEPT);
688 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
691 *nam = in_sockaddr(port, &addr);
698 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
700 struct inpcb *inp = NULL;
702 struct tcpcb *tp = NULL;
704 struct in6_addr addr6;
709 if (so->so_state & SS_ISDISCONNECTED)
710 return (ECONNABORTED);
713 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
714 INP_INFO_RLOCK(&V_tcbinfo);
716 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
717 error = ECONNABORTED;
724 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
725 * copy the data of interest and defer the malloc until after we
728 if (inp->inp_vflag & INP_IPV4) {
730 port = inp->inp_fport;
731 addr = inp->inp_faddr;
733 port = inp->inp_fport;
734 addr6 = inp->in6p_faddr;
738 TCPDEBUG2(PRU_ACCEPT);
739 TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
741 INP_INFO_RUNLOCK(&V_tcbinfo);
744 *nam = in6_v4mapsin6_sockaddr(port, &addr);
746 *nam = in6_sockaddr(port, &addr6);
753 * Mark the connection as being incapable of further output.
756 tcp_usr_shutdown(struct socket *so)
760 struct tcpcb *tp = NULL;
763 INP_INFO_RLOCK(&V_tcbinfo);
765 KASSERT(inp != NULL, ("inp == NULL"));
767 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
775 if (!(inp->inp_flags & INP_DROPPED))
776 error = tcp_output(tp);
779 TCPDEBUG2(PRU_SHUTDOWN);
780 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
782 INP_INFO_RUNLOCK(&V_tcbinfo);
788 * After a receive, possibly send window update to peer.
791 tcp_usr_rcvd(struct socket *so, int flags)
794 struct tcpcb *tp = NULL;
799 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
801 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
808 if (tp->t_flags & TF_TOE)
809 tcp_offload_rcvd(tp);
816 TCP_PROBE2(debug__user, tp, PRU_RCVD);
822 * Do a send by putting data in output queue and updating urgent
823 * marker if URG set. Possibly send more data. Unlike the other
824 * pru_*() routines, the mbuf chains are our responsibility. We
825 * must either enqueue them or free them. The other pru_* routines
826 * generally are caller-frees.
829 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
830 struct sockaddr *nam, struct mbuf *control, struct thread *td)
834 struct tcpcb *tp = NULL;
841 * We require the pcbinfo lock if we will close the socket as part of
844 if (flags & PRUS_EOF)
845 INP_INFO_RLOCK(&V_tcbinfo);
847 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
849 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
853 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
854 * for freeing memory.
856 if (m && (flags & PRUS_NOTREADY) == 0)
862 isipv6 = nam && nam->sa_family == AF_INET6;
867 /* TCP doesn't do control messages (rights, creds, etc) */
868 if (control->m_len) {
875 m_freem(control); /* empty control, just free it */
877 if (!(flags & PRUS_OOB)) {
878 sbappendstream(&so->so_snd, m, flags);
879 if (nam && tp->t_state < TCPS_SYN_SENT) {
881 * Do implied connect if not yet connected,
882 * initialize window to default value, and
883 * initialize maxseg/maxopd using peer's cached
888 error = tcp6_connect(tp, nam, td);
890 #if defined(INET6) && defined(INET)
894 error = tcp_connect(tp, nam, td);
898 tp->snd_wnd = TTCP_CLIENT_SND_WND;
901 if (flags & PRUS_EOF) {
903 * Close the send side of the connection after
906 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
910 if (!(inp->inp_flags & INP_DROPPED) &&
911 !(flags & PRUS_NOTREADY)) {
912 if (flags & PRUS_MORETOCOME)
913 tp->t_flags |= TF_MORETOCOME;
914 error = tcp_output(tp);
915 if (flags & PRUS_MORETOCOME)
916 tp->t_flags &= ~TF_MORETOCOME;
920 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
922 SOCKBUF_LOCK(&so->so_snd);
923 if (sbspace(&so->so_snd) < -512) {
924 SOCKBUF_UNLOCK(&so->so_snd);
930 * According to RFC961 (Assigned Protocols),
931 * the urgent pointer points to the last octet
932 * of urgent data. We continue, however,
933 * to consider it to indicate the first octet
934 * of data past the urgent section.
935 * Otherwise, snd_up should be one lower.
937 sbappendstream_locked(&so->so_snd, m, flags);
938 SOCKBUF_UNLOCK(&so->so_snd);
939 if (nam && tp->t_state < TCPS_SYN_SENT) {
941 * Do implied connect if not yet connected,
942 * initialize window to default value, and
943 * initialize maxseg/maxopd using peer's cached
948 error = tcp6_connect(tp, nam, td);
950 #if defined(INET6) && defined(INET)
954 error = tcp_connect(tp, nam, td);
958 tp->snd_wnd = TTCP_CLIENT_SND_WND;
961 tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
962 if (!(flags & PRUS_NOTREADY)) {
963 tp->t_flags |= TF_FORCEDATA;
964 error = tcp_output(tp);
965 tp->t_flags &= ~TF_FORCEDATA;
969 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
970 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
971 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
972 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
974 if (flags & PRUS_EOF)
975 INP_INFO_RUNLOCK(&V_tcbinfo);
980 tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
988 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
990 for (int i = 0; i < count; i++)
996 SOCKBUF_LOCK(&so->so_snd);
997 error = sbready(&so->so_snd, m, count);
998 SOCKBUF_UNLOCK(&so->so_snd);
1000 error = tcp_output(tp);
1007 * Abort the TCP. Drop the connection abruptly.
1010 tcp_usr_abort(struct socket *so)
1013 struct tcpcb *tp = NULL;
1016 inp = sotoinpcb(so);
1017 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1019 INP_INFO_RLOCK(&V_tcbinfo);
1021 KASSERT(inp->inp_socket != NULL,
1022 ("tcp_usr_abort: inp_socket == NULL"));
1025 * If we still have full TCP state, and we're not dropped, drop.
1027 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1028 !(inp->inp_flags & INP_DROPPED)) {
1029 tp = intotcpcb(inp);
1031 tcp_drop(tp, ECONNABORTED);
1032 TCPDEBUG2(PRU_ABORT);
1033 TCP_PROBE2(debug__user, tp, PRU_ABORT);
1035 if (!(inp->inp_flags & INP_DROPPED)) {
1037 so->so_state |= SS_PROTOREF;
1039 inp->inp_flags |= INP_SOCKREF;
1042 INP_INFO_RUNLOCK(&V_tcbinfo);
1046 * TCP socket is closed. Start friendly disconnect.
1049 tcp_usr_close(struct socket *so)
1052 struct tcpcb *tp = NULL;
1055 inp = sotoinpcb(so);
1056 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1058 INP_INFO_RLOCK(&V_tcbinfo);
1060 KASSERT(inp->inp_socket != NULL,
1061 ("tcp_usr_close: inp_socket == NULL"));
1064 * If we still have full TCP state, and we're not dropped, initiate
1067 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1068 !(inp->inp_flags & INP_DROPPED)) {
1069 tp = intotcpcb(inp);
1072 TCPDEBUG2(PRU_CLOSE);
1073 TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1075 if (!(inp->inp_flags & INP_DROPPED)) {
1077 so->so_state |= SS_PROTOREF;
1079 inp->inp_flags |= INP_SOCKREF;
1082 INP_INFO_RUNLOCK(&V_tcbinfo);
1086 * Receive out-of-band data.
1089 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1093 struct tcpcb *tp = NULL;
1096 inp = sotoinpcb(so);
1097 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1099 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1103 tp = intotcpcb(inp);
1105 if ((so->so_oobmark == 0 &&
1106 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1107 so->so_options & SO_OOBINLINE ||
1108 tp->t_oobflags & TCPOOB_HADDATA) {
1112 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1113 error = EWOULDBLOCK;
1117 *mtod(m, caddr_t) = tp->t_iobc;
1118 if ((flags & MSG_PEEK) == 0)
1119 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1122 TCPDEBUG2(PRU_RCVOOB);
1123 TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
1129 struct pr_usrreqs tcp_usrreqs = {
1130 .pru_abort = tcp_usr_abort,
1131 .pru_accept = tcp_usr_accept,
1132 .pru_attach = tcp_usr_attach,
1133 .pru_bind = tcp_usr_bind,
1134 .pru_connect = tcp_usr_connect,
1135 .pru_control = in_control,
1136 .pru_detach = tcp_usr_detach,
1137 .pru_disconnect = tcp_usr_disconnect,
1138 .pru_listen = tcp_usr_listen,
1139 .pru_peeraddr = in_getpeeraddr,
1140 .pru_rcvd = tcp_usr_rcvd,
1141 .pru_rcvoob = tcp_usr_rcvoob,
1142 .pru_send = tcp_usr_send,
1143 .pru_ready = tcp_usr_ready,
1144 .pru_shutdown = tcp_usr_shutdown,
1145 .pru_sockaddr = in_getsockaddr,
1146 .pru_sosetlabel = in_pcbsosetlabel,
1147 .pru_close = tcp_usr_close,
1152 struct pr_usrreqs tcp6_usrreqs = {
1153 .pru_abort = tcp_usr_abort,
1154 .pru_accept = tcp6_usr_accept,
1155 .pru_attach = tcp_usr_attach,
1156 .pru_bind = tcp6_usr_bind,
1157 .pru_connect = tcp6_usr_connect,
1158 .pru_control = in6_control,
1159 .pru_detach = tcp_usr_detach,
1160 .pru_disconnect = tcp_usr_disconnect,
1161 .pru_listen = tcp6_usr_listen,
1162 .pru_peeraddr = in6_mapped_peeraddr,
1163 .pru_rcvd = tcp_usr_rcvd,
1164 .pru_rcvoob = tcp_usr_rcvoob,
1165 .pru_send = tcp_usr_send,
1166 .pru_ready = tcp_usr_ready,
1167 .pru_shutdown = tcp_usr_shutdown,
1168 .pru_sockaddr = in6_mapped_sockaddr,
1169 .pru_sosetlabel = in_pcbsosetlabel,
1170 .pru_close = tcp_usr_close,
1176 * Common subroutine to open a TCP connection to remote host specified
1177 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1178 * port number if needed. Call in_pcbconnect_setup to do the routing and
1179 * to choose a local host address (interface). If there is an existing
1180 * incarnation of the same connection in TIME-WAIT state and if the remote
1181 * host was sending CC options and if the connection duration was < MSL, then
1182 * truncate the previous TIME-WAIT state and proceed.
1183 * Initialize connection parameters and enter SYN-SENT state.
1186 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1188 struct inpcb *inp = tp->t_inpcb, *oinp;
1189 struct socket *so = inp->inp_socket;
1190 struct in_addr laddr;
1194 INP_WLOCK_ASSERT(inp);
1195 INP_HASH_WLOCK(&V_tcbinfo);
1197 if (inp->inp_lport == 0) {
1198 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1204 * Cannot simply call in_pcbconnect, because there might be an
1205 * earlier incarnation of this same connection still in
1206 * TIME_WAIT state, creating an ADDRINUSE error.
1208 laddr = inp->inp_laddr;
1209 lport = inp->inp_lport;
1210 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1211 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1212 if (error && oinp == NULL)
1218 inp->inp_laddr = laddr;
1220 INP_HASH_WUNLOCK(&V_tcbinfo);
1223 * Compute window scaling to request:
1224 * Scale to fit into sweet spot. See tcp_syncache.c.
1225 * XXX: This should move to tcp_output().
1227 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1228 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1229 tp->request_r_scale++;
1232 TCPSTAT_INC(tcps_connattempt);
1233 tcp_state_change(tp, TCPS_SYN_SENT);
1234 tp->iss = tcp_new_isn(tp);
1235 tcp_sendseqinit(tp);
1240 INP_HASH_WUNLOCK(&V_tcbinfo);
1247 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1249 struct inpcb *inp = tp->t_inpcb;
1252 INP_WLOCK_ASSERT(inp);
1253 INP_HASH_WLOCK(&V_tcbinfo);
1255 if (inp->inp_lport == 0) {
1256 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1260 error = in6_pcbconnect(inp, nam, td->td_ucred);
1263 INP_HASH_WUNLOCK(&V_tcbinfo);
1265 /* Compute window scaling to request. */
1266 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1267 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1268 tp->request_r_scale++;
1270 soisconnecting(inp->inp_socket);
1271 TCPSTAT_INC(tcps_connattempt);
1272 tcp_state_change(tp, TCPS_SYN_SENT);
1273 tp->iss = tcp_new_isn(tp);
1274 tcp_sendseqinit(tp);
1279 INP_HASH_WUNLOCK(&V_tcbinfo);
1285 * Export TCP internal state information via a struct tcp_info, based on the
1286 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1287 * (TCP state machine, etc). We export all information using FreeBSD-native
1288 * constants -- for example, the numeric values for tcpi_state will differ
1292 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1295 INP_WLOCK_ASSERT(tp->t_inpcb);
1296 bzero(ti, sizeof(*ti));
1298 ti->tcpi_state = tp->t_state;
1299 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1300 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1301 if (tp->t_flags & TF_SACK_PERMIT)
1302 ti->tcpi_options |= TCPI_OPT_SACK;
1303 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1304 ti->tcpi_options |= TCPI_OPT_WSCALE;
1305 ti->tcpi_snd_wscale = tp->snd_scale;
1306 ti->tcpi_rcv_wscale = tp->rcv_scale;
1309 ti->tcpi_rto = tp->t_rxtcur * tick;
1310 ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
1311 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1312 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1314 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1315 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1318 * FreeBSD-specific extension fields for tcp_info.
1320 ti->tcpi_rcv_space = tp->rcv_wnd;
1321 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1322 ti->tcpi_snd_wnd = tp->snd_wnd;
1323 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1324 ti->tcpi_snd_nxt = tp->snd_nxt;
1325 ti->tcpi_snd_mss = tp->t_maxseg;
1326 ti->tcpi_rcv_mss = tp->t_maxseg;
1327 if (tp->t_flags & TF_TOE)
1328 ti->tcpi_options |= TCPI_OPT_TOE;
1329 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1330 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1331 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1335 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1336 * socket option arguments. When it re-acquires the lock after the copy, it
1337 * has to revalidate that the connection is still valid for the socket
1340 #define INP_WLOCK_RECHECK(inp) do { \
1342 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1344 return (ECONNRESET); \
1346 tp = intotcpcb(inp); \
1350 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1352 int error, opt, optval;
1357 char buf[TCP_CA_NAME_MAX];
1358 struct cc_algo *algo;
1361 inp = sotoinpcb(so);
1362 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1364 if (sopt->sopt_level != IPPROTO_TCP) {
1366 if (inp->inp_vflag & INP_IPV6PROTO) {
1368 error = ip6_ctloutput(so, sopt);
1371 #if defined(INET6) && defined(INET)
1377 error = ip_ctloutput(so, sopt);
1382 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1384 return (ECONNRESET);
1387 switch (sopt->sopt_dir) {
1389 switch (sopt->sopt_name) {
1390 #ifdef TCP_SIGNATURE
1393 error = sooptcopyin(sopt, &optval, sizeof optval,
1398 INP_WLOCK_RECHECK(inp);
1400 tp->t_flags |= TF_SIGNATURE;
1402 tp->t_flags &= ~TF_SIGNATURE;
1403 goto unlock_and_done;
1404 #endif /* TCP_SIGNATURE */
1409 error = sooptcopyin(sopt, &optval, sizeof optval,
1414 INP_WLOCK_RECHECK(inp);
1415 switch (sopt->sopt_name) {
1423 opt = 0; /* dead code to fool gcc */
1430 tp->t_flags &= ~opt;
1433 if (tp->t_flags & TF_TOE) {
1434 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1443 error = sooptcopyin(sopt, &optval, sizeof optval,
1448 INP_WLOCK_RECHECK(inp);
1450 tp->t_flags |= TF_NOPUSH;
1451 else if (tp->t_flags & TF_NOPUSH) {
1452 tp->t_flags &= ~TF_NOPUSH;
1453 if (TCPS_HAVEESTABLISHED(tp->t_state))
1454 error = tcp_output(tp);
1456 goto unlock_and_done;
1460 error = sooptcopyin(sopt, &optval, sizeof optval,
1465 INP_WLOCK_RECHECK(inp);
1466 if (optval > 0 && optval <= tp->t_maxseg &&
1467 optval + 40 >= V_tcp_minmss)
1468 tp->t_maxseg = optval;
1471 goto unlock_and_done;
1478 case TCP_CONGESTION:
1480 bzero(buf, sizeof(buf));
1481 error = sooptcopyin(sopt, &buf, sizeof(buf), 1);
1484 INP_WLOCK_RECHECK(inp);
1486 * Return EINVAL if we can't find the requested cc algo.
1490 STAILQ_FOREACH(algo, &cc_list, entries) {
1491 if (strncmp(buf, algo->name, TCP_CA_NAME_MAX)
1493 /* We've found the requested algo. */
1496 * We hold a write lock over the tcb
1497 * so it's safe to do these things
1498 * without ordering concerns.
1500 if (CC_ALGO(tp)->cb_destroy != NULL)
1501 CC_ALGO(tp)->cb_destroy(tp->ccv);
1504 * If something goes pear shaped
1505 * initialising the new algo,
1506 * fall back to newreno (which
1507 * does not require initialisation).
1509 if (algo->cb_init != NULL)
1510 if (algo->cb_init(tp->ccv) > 0) {
1511 CC_ALGO(tp) = &newreno_cc_algo;
1513 * The only reason init
1515 * because of malloc.
1519 break; /* Break the STAILQ_FOREACH. */
1523 goto unlock_and_done;
1529 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1533 if (ui > (UINT_MAX / hz)) {
1539 INP_WLOCK_RECHECK(inp);
1540 switch (sopt->sopt_name) {
1542 tp->t_keepidle = ui;
1544 * XXX: better check current remaining
1545 * timeout and "merge" it with new value.
1547 if ((tp->t_state > TCPS_LISTEN) &&
1548 (tp->t_state <= TCPS_CLOSING))
1549 tcp_timer_activate(tp, TT_KEEP,
1553 tp->t_keepintvl = ui;
1554 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1555 (TP_MAXIDLE(tp) > 0))
1556 tcp_timer_activate(tp, TT_2MSL,
1560 tp->t_keepinit = ui;
1561 if (tp->t_state == TCPS_SYN_RECEIVED ||
1562 tp->t_state == TCPS_SYN_SENT)
1563 tcp_timer_activate(tp, TT_KEEP,
1567 goto unlock_and_done;
1571 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1575 INP_WLOCK_RECHECK(inp);
1577 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1578 (TP_MAXIDLE(tp) > 0))
1579 tcp_timer_activate(tp, TT_2MSL,
1581 goto unlock_and_done;
1587 error = sooptcopyin(sopt, &optval, sizeof optval,
1592 INP_WLOCK_RECHECK(inp);
1594 tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
1595 &(tp->t_outpkts) : &(tp->t_inpkts),
1599 goto unlock_and_done;
1604 error = ENOPROTOOPT;
1610 tp = intotcpcb(inp);
1611 switch (sopt->sopt_name) {
1612 #ifdef TCP_SIGNATURE
1614 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1616 error = sooptcopyout(sopt, &optval, sizeof optval);
1621 optval = tp->t_flags & TF_NODELAY;
1623 error = sooptcopyout(sopt, &optval, sizeof optval);
1626 optval = tp->t_maxseg;
1628 error = sooptcopyout(sopt, &optval, sizeof optval);
1631 optval = tp->t_flags & TF_NOOPT;
1633 error = sooptcopyout(sopt, &optval, sizeof optval);
1636 optval = tp->t_flags & TF_NOPUSH;
1638 error = sooptcopyout(sopt, &optval, sizeof optval);
1641 tcp_fill_info(tp, &ti);
1643 error = sooptcopyout(sopt, &ti, sizeof ti);
1645 case TCP_CONGESTION:
1646 bzero(buf, sizeof(buf));
1647 strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1649 error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX);
1655 switch (sopt->sopt_name) {
1657 ui = tp->t_keepidle / hz;
1660 ui = tp->t_keepintvl / hz;
1663 ui = tp->t_keepinit / hz;
1670 error = sooptcopyout(sopt, &ui, sizeof(ui));
1675 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
1676 &(tp->t_outpkts) : &(tp->t_inpkts));
1678 error = sooptcopyout(sopt, &optval, sizeof optval);
1683 error = ENOPROTOOPT;
1690 #undef INP_WLOCK_RECHECK
1693 * Attach TCP protocol to socket, allocating
1694 * internet protocol control block, tcp control block,
1695 * bufer space, and entering LISTEN state if to accept connections.
1698 tcp_attach(struct socket *so)
1704 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1705 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1709 so->so_rcv.sb_flags |= SB_AUTOSIZE;
1710 so->so_snd.sb_flags |= SB_AUTOSIZE;
1711 INP_INFO_RLOCK(&V_tcbinfo);
1712 error = in_pcballoc(so, &V_tcbinfo);
1714 INP_INFO_RUNLOCK(&V_tcbinfo);
1717 inp = sotoinpcb(so);
1719 if (inp->inp_vflag & INP_IPV6PROTO) {
1720 inp->inp_vflag |= INP_IPV6;
1721 inp->in6p_hops = -1; /* use kernel default */
1725 inp->inp_vflag |= INP_IPV4;
1726 tp = tcp_newtcpcb(inp);
1730 INP_INFO_RUNLOCK(&V_tcbinfo);
1733 tp->t_state = TCPS_CLOSED;
1735 INP_INFO_RUNLOCK(&V_tcbinfo);
1740 * Initiate (or continue) disconnect.
1741 * If embryonic state, just send reset (once).
1742 * If in ``let data drain'' option and linger null, just drop.
1743 * Otherwise (hard), mark socket disconnecting and drop
1744 * current input data; switch states based on user close, and
1745 * send segment to peer (with FIN).
1748 tcp_disconnect(struct tcpcb *tp)
1750 struct inpcb *inp = tp->t_inpcb;
1751 struct socket *so = inp->inp_socket;
1753 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1754 INP_WLOCK_ASSERT(inp);
1757 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1758 * socket is still open.
1760 if (tp->t_state < TCPS_ESTABLISHED) {
1763 ("tcp_disconnect: tcp_close() returned NULL"));
1764 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1765 tp = tcp_drop(tp, 0);
1767 ("tcp_disconnect: tcp_drop() returned NULL"));
1769 soisdisconnecting(so);
1770 sbflush(&so->so_rcv);
1772 if (!(inp->inp_flags & INP_DROPPED))
1778 * User issued close, and wish to trail through shutdown states:
1779 * if never received SYN, just forget it. If got a SYN from peer,
1780 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1781 * If already got a FIN from peer, then almost done; go to LAST_ACK
1782 * state. In all other cases, have already sent FIN to peer (e.g.
1783 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1784 * for peer to send FIN or not respond to keep-alives, etc.
1785 * We can let the user exit from the close as soon as the FIN is acked.
1788 tcp_usrclosed(struct tcpcb *tp)
1791 INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1792 INP_WLOCK_ASSERT(tp->t_inpcb);
1794 switch (tp->t_state) {
1797 tcp_offload_listen_stop(tp);
1799 tcp_state_change(tp, TCPS_CLOSED);
1804 * tcp_close() should never return NULL here as the socket is
1808 ("tcp_usrclosed: tcp_close() returned NULL"));
1812 case TCPS_SYN_RECEIVED:
1813 tp->t_flags |= TF_NEEDFIN;
1816 case TCPS_ESTABLISHED:
1817 tcp_state_change(tp, TCPS_FIN_WAIT_1);
1820 case TCPS_CLOSE_WAIT:
1821 tcp_state_change(tp, TCPS_LAST_ACK);
1824 if (tp->t_state >= TCPS_FIN_WAIT_2) {
1825 soisdisconnected(tp->t_inpcb->inp_socket);
1826 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
1827 if (tp->t_state == TCPS_FIN_WAIT_2) {
1830 timeout = (tcp_fast_finwait2_recycle) ?
1831 tcp_finwait2_timeout : TP_MAXIDLE(tp);
1832 tcp_timer_activate(tp, TT_2MSL, timeout);
1839 db_print_indent(int indent)
1843 for (i = 0; i < indent; i++)
1848 db_print_tstate(int t_state)
1853 db_printf("TCPS_CLOSED");
1857 db_printf("TCPS_LISTEN");
1861 db_printf("TCPS_SYN_SENT");
1864 case TCPS_SYN_RECEIVED:
1865 db_printf("TCPS_SYN_RECEIVED");
1868 case TCPS_ESTABLISHED:
1869 db_printf("TCPS_ESTABLISHED");
1872 case TCPS_CLOSE_WAIT:
1873 db_printf("TCPS_CLOSE_WAIT");
1876 case TCPS_FIN_WAIT_1:
1877 db_printf("TCPS_FIN_WAIT_1");
1881 db_printf("TCPS_CLOSING");
1885 db_printf("TCPS_LAST_ACK");
1888 case TCPS_FIN_WAIT_2:
1889 db_printf("TCPS_FIN_WAIT_2");
1892 case TCPS_TIME_WAIT:
1893 db_printf("TCPS_TIME_WAIT");
1897 db_printf("unknown");
1903 db_print_tflags(u_int t_flags)
1908 if (t_flags & TF_ACKNOW) {
1909 db_printf("%sTF_ACKNOW", comma ? ", " : "");
1912 if (t_flags & TF_DELACK) {
1913 db_printf("%sTF_DELACK", comma ? ", " : "");
1916 if (t_flags & TF_NODELAY) {
1917 db_printf("%sTF_NODELAY", comma ? ", " : "");
1920 if (t_flags & TF_NOOPT) {
1921 db_printf("%sTF_NOOPT", comma ? ", " : "");
1924 if (t_flags & TF_SENTFIN) {
1925 db_printf("%sTF_SENTFIN", comma ? ", " : "");
1928 if (t_flags & TF_REQ_SCALE) {
1929 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1932 if (t_flags & TF_RCVD_SCALE) {
1933 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1936 if (t_flags & TF_REQ_TSTMP) {
1937 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1940 if (t_flags & TF_RCVD_TSTMP) {
1941 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1944 if (t_flags & TF_SACK_PERMIT) {
1945 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1948 if (t_flags & TF_NEEDSYN) {
1949 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1952 if (t_flags & TF_NEEDFIN) {
1953 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1956 if (t_flags & TF_NOPUSH) {
1957 db_printf("%sTF_NOPUSH", comma ? ", " : "");
1960 if (t_flags & TF_MORETOCOME) {
1961 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1964 if (t_flags & TF_LQ_OVERFLOW) {
1965 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1968 if (t_flags & TF_LASTIDLE) {
1969 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1972 if (t_flags & TF_RXWIN0SENT) {
1973 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1976 if (t_flags & TF_FASTRECOVERY) {
1977 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1980 if (t_flags & TF_CONGRECOVERY) {
1981 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
1984 if (t_flags & TF_WASFRECOVERY) {
1985 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1988 if (t_flags & TF_SIGNATURE) {
1989 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1992 if (t_flags & TF_FORCEDATA) {
1993 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1996 if (t_flags & TF_TSO) {
1997 db_printf("%sTF_TSO", comma ? ", " : "");
2000 if (t_flags & TF_ECN_PERMIT) {
2001 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2007 db_print_toobflags(char t_oobflags)
2012 if (t_oobflags & TCPOOB_HAVEDATA) {
2013 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2016 if (t_oobflags & TCPOOB_HADDATA) {
2017 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2023 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2026 db_print_indent(indent);
2027 db_printf("%s at %p\n", name, tp);
2031 db_print_indent(indent);
2032 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
2033 LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2035 db_print_indent(indent);
2036 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
2037 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2039 db_print_indent(indent);
2040 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2041 &tp->t_timers->tt_delack, tp->t_inpcb);
2043 db_print_indent(indent);
2044 db_printf("t_state: %d (", tp->t_state);
2045 db_print_tstate(tp->t_state);
2048 db_print_indent(indent);
2049 db_printf("t_flags: 0x%x (", tp->t_flags);
2050 db_print_tflags(tp->t_flags);
2053 db_print_indent(indent);
2054 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
2055 tp->snd_una, tp->snd_max, tp->snd_nxt);
2057 db_print_indent(indent);
2058 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2059 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2061 db_print_indent(indent);
2062 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2063 tp->iss, tp->irs, tp->rcv_nxt);
2065 db_print_indent(indent);
2066 db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n",
2067 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2069 db_print_indent(indent);
2070 db_printf("snd_wnd: %lu snd_cwnd: %lu\n",
2071 tp->snd_wnd, tp->snd_cwnd);
2073 db_print_indent(indent);
2074 db_printf("snd_ssthresh: %lu snd_recover: "
2075 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2077 db_print_indent(indent);
2078 db_printf("t_maxopd: %u t_rcvtime: %u t_startime: %u\n",
2079 tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
2081 db_print_indent(indent);
2082 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2083 tp->t_rtttime, tp->t_rtseq);
2085 db_print_indent(indent);
2086 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2087 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2089 db_print_indent(indent);
2090 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2091 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2094 db_print_indent(indent);
2095 db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n",
2096 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2098 db_print_indent(indent);
2099 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2100 db_print_toobflags(tp->t_oobflags);
2101 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2103 db_print_indent(indent);
2104 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2105 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2107 db_print_indent(indent);
2108 db_printf("ts_recent: %u ts_recent_age: %u\n",
2109 tp->ts_recent, tp->ts_recent_age);
2111 db_print_indent(indent);
2112 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2113 "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2115 db_print_indent(indent);
2116 db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x "
2117 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2118 tp->snd_recover_prev, tp->t_badrxtwin);
2120 db_print_indent(indent);
2121 db_printf("snd_numholes: %d snd_holes first: %p\n",
2122 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2124 db_print_indent(indent);
2125 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
2126 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2128 /* Skip sackblks, sackhint. */
2130 db_print_indent(indent);
2131 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2132 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2135 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2140 db_printf("usage: show tcpcb <addr>\n");
2143 tp = (struct tcpcb *)addr;
2145 db_print_tcpcb(tp, "tcpcb", 0);