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/route.h>
70 #include <netinet/cc.h>
71 #include <netinet/in.h>
72 #include <netinet/in_pcb.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/in_var.h>
75 #include <netinet/ip_var.h>
77 #include <netinet/ip6.h>
78 #include <netinet6/in6_pcb.h>
79 #include <netinet6/ip6_var.h>
80 #include <netinet6/scope6_var.h>
83 #include <netinet/tcp_fastopen.h>
85 #include <netinet/tcp_fsm.h>
86 #include <netinet/tcp_seq.h>
87 #include <netinet/tcp_timer.h>
88 #include <netinet/tcp_var.h>
89 #include <netinet/tcpip.h>
91 #include <netinet/tcp_debug.h>
94 #include <netinet/tcp_offload.h>
98 * TCP protocol interface to socket abstraction.
100 static int tcp_attach(struct socket *);
102 static int tcp_connect(struct tcpcb *, struct sockaddr *,
106 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
109 static void tcp_disconnect(struct tcpcb *);
110 static void tcp_usrclosed(struct tcpcb *);
111 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
114 #define TCPDEBUG0 int ostate = 0
115 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
116 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
117 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
121 #define TCPDEBUG2(req)
125 * TCP attaches to socket via pru_attach(), reserving space,
126 * and an internet control block.
129 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
132 struct tcpcb *tp = NULL;
137 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
140 error = tcp_attach(so);
144 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
145 so->so_linger = TCP_LINGERTIME;
150 TCPDEBUG2(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_WLOCK_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 * 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 * 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)
248 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
249 INP_INFO_WLOCK(&V_tcbinfo);
251 KASSERT(inp->inp_socket != NULL,
252 ("tcp_usr_detach: inp_socket == NULL"));
254 INP_INFO_WUNLOCK(&V_tcbinfo);
259 * Give the socket an address.
262 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
266 struct tcpcb *tp = NULL;
267 struct sockaddr_in *sinp;
269 sinp = (struct sockaddr_in *)nam;
270 if (nam->sa_len != sizeof (*sinp))
273 * Must check for multicast addresses and disallow binding
276 if (sinp->sin_family == AF_INET &&
277 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
278 return (EAFNOSUPPORT);
282 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
284 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
290 INP_HASH_WLOCK(&V_tcbinfo);
291 error = in_pcbbind(inp, nam, td->td_ucred);
292 INP_HASH_WUNLOCK(&V_tcbinfo);
303 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
307 struct tcpcb *tp = NULL;
308 struct sockaddr_in6 *sin6p;
310 sin6p = (struct sockaddr_in6 *)nam;
311 if (nam->sa_len != sizeof (*sin6p))
314 * Must check for multicast addresses and disallow binding
317 if (sin6p->sin6_family == AF_INET6 &&
318 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
319 return (EAFNOSUPPORT);
323 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
325 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
331 INP_HASH_WLOCK(&V_tcbinfo);
332 inp->inp_vflag &= ~INP_IPV4;
333 inp->inp_vflag |= INP_IPV6;
335 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
336 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
337 inp->inp_vflag |= INP_IPV4;
338 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
339 struct sockaddr_in sin;
341 in6_sin6_2_sin(&sin, sin6p);
342 inp->inp_vflag |= INP_IPV4;
343 inp->inp_vflag &= ~INP_IPV6;
344 error = in_pcbbind(inp, (struct sockaddr *)&sin,
346 INP_HASH_WUNLOCK(&V_tcbinfo);
351 error = in6_pcbbind(inp, nam, td->td_ucred);
352 INP_HASH_WUNLOCK(&V_tcbinfo);
362 * Prepare to accept connections.
365 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
369 struct tcpcb *tp = NULL;
373 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
375 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
382 error = solisten_proto_check(so);
383 INP_HASH_WLOCK(&V_tcbinfo);
384 if (error == 0 && inp->inp_lport == 0)
385 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
386 INP_HASH_WUNLOCK(&V_tcbinfo);
388 tcp_state_change(tp, TCPS_LISTEN);
389 solisten_proto(so, backlog);
391 if ((so->so_options & SO_NO_OFFLOAD) == 0)
392 tcp_offload_listen_start(tp);
398 if (tp->t_flags & TF_FASTOPEN)
399 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
402 TCPDEBUG2(PRU_LISTEN);
410 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
414 struct tcpcb *tp = NULL;
418 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
420 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
427 error = solisten_proto_check(so);
428 INP_HASH_WLOCK(&V_tcbinfo);
429 if (error == 0 && inp->inp_lport == 0) {
430 inp->inp_vflag &= ~INP_IPV4;
431 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
432 inp->inp_vflag |= INP_IPV4;
433 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
435 INP_HASH_WUNLOCK(&V_tcbinfo);
437 tcp_state_change(tp, TCPS_LISTEN);
438 solisten_proto(so, backlog);
440 if ((so->so_options & SO_NO_OFFLOAD) == 0)
441 tcp_offload_listen_start(tp);
447 if (tp->t_flags & TF_FASTOPEN)
448 tp->t_tfo_pending = tcp_fastopen_alloc_counter();
451 TCPDEBUG2(PRU_LISTEN);
459 * Initiate connection to peer.
460 * Create a template for use in transmissions on this connection.
461 * Enter SYN_SENT state, and mark socket as connecting.
462 * Start keep-alive timer, and seed output sequence space.
463 * Send initial segment on connection.
466 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
470 struct tcpcb *tp = NULL;
471 struct sockaddr_in *sinp;
473 sinp = (struct sockaddr_in *)nam;
474 if (nam->sa_len != sizeof (*sinp))
477 * Must disallow TCP ``connections'' to multicast addresses.
479 if (sinp->sin_family == AF_INET
480 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
481 return (EAFNOSUPPORT);
482 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
487 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
489 if (inp->inp_flags & INP_TIMEWAIT) {
493 if (inp->inp_flags & INP_DROPPED) {
494 error = ECONNREFUSED;
499 if ((error = tcp_connect(tp, nam, td)) != 0)
502 if (registered_toedevs > 0 &&
503 (so->so_options & SO_NO_OFFLOAD) == 0 &&
504 (error = tcp_offload_connect(so, nam)) == 0)
507 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
508 error = tcp_output(tp);
510 TCPDEBUG2(PRU_CONNECT);
518 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
522 struct tcpcb *tp = NULL;
523 struct sockaddr_in6 *sin6p;
527 sin6p = (struct sockaddr_in6 *)nam;
528 if (nam->sa_len != sizeof (*sin6p))
531 * Must disallow TCP ``connections'' to multicast addresses.
533 if (sin6p->sin6_family == AF_INET6
534 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
535 return (EAFNOSUPPORT);
538 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
540 if (inp->inp_flags & INP_TIMEWAIT) {
544 if (inp->inp_flags & INP_DROPPED) {
545 error = ECONNREFUSED;
552 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
553 * therefore probably require the hash lock, which isn't held here.
554 * Is this a significant problem?
556 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
557 struct sockaddr_in sin;
559 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
564 in6_sin6_2_sin(&sin, sin6p);
565 inp->inp_vflag |= INP_IPV4;
566 inp->inp_vflag &= ~INP_IPV6;
567 if ((error = prison_remote_ip4(td->td_ucred,
568 &sin.sin_addr)) != 0)
570 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
573 if (registered_toedevs > 0 &&
574 (so->so_options & SO_NO_OFFLOAD) == 0 &&
575 (error = tcp_offload_connect(so, nam)) == 0)
578 error = tcp_output(tp);
582 inp->inp_vflag &= ~INP_IPV4;
583 inp->inp_vflag |= INP_IPV6;
584 inp->inp_inc.inc_flags |= INC_ISIPV6;
585 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
587 if ((error = tcp6_connect(tp, nam, td)) != 0)
590 if (registered_toedevs > 0 &&
591 (so->so_options & SO_NO_OFFLOAD) == 0 &&
592 (error = tcp_offload_connect(so, nam)) == 0)
595 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
596 error = tcp_output(tp);
599 TCPDEBUG2(PRU_CONNECT);
606 * Initiate disconnect from peer.
607 * If connection never passed embryonic stage, just drop;
608 * else if don't need to let data drain, then can just drop anyways,
609 * else have to begin TCP shutdown process: mark socket disconnecting,
610 * drain unread data, state switch to reflect user close, and
611 * send segment (e.g. FIN) to peer. Socket will be really disconnected
612 * when peer sends FIN and acks ours.
614 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
617 tcp_usr_disconnect(struct socket *so)
620 struct tcpcb *tp = NULL;
624 INP_INFO_WLOCK(&V_tcbinfo);
626 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
628 if (inp->inp_flags & INP_TIMEWAIT)
630 if (inp->inp_flags & INP_DROPPED) {
638 TCPDEBUG2(PRU_DISCONNECT);
640 INP_INFO_WUNLOCK(&V_tcbinfo);
646 * Accept a connection. Essentially all the work is done at higher levels;
647 * just return the address of the peer, storing through addr.
650 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
653 struct inpcb *inp = NULL;
654 struct tcpcb *tp = NULL;
659 if (so->so_state & SS_ISDISCONNECTED)
660 return (ECONNABORTED);
663 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
665 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
666 error = ECONNABORTED;
673 * We inline in_getpeeraddr and COMMON_END here, so that we can
674 * copy the data of interest and defer the malloc until after we
677 port = inp->inp_fport;
678 addr = inp->inp_faddr;
681 TCPDEBUG2(PRU_ACCEPT);
684 *nam = in_sockaddr(port, &addr);
691 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
693 struct inpcb *inp = NULL;
695 struct tcpcb *tp = NULL;
697 struct in6_addr addr6;
702 if (so->so_state & SS_ISDISCONNECTED)
703 return (ECONNABORTED);
706 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
707 INP_INFO_RLOCK(&V_tcbinfo);
709 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
710 error = ECONNABORTED;
717 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
718 * copy the data of interest and defer the malloc until after we
721 if (inp->inp_vflag & INP_IPV4) {
723 port = inp->inp_fport;
724 addr = inp->inp_faddr;
726 port = inp->inp_fport;
727 addr6 = inp->in6p_faddr;
731 TCPDEBUG2(PRU_ACCEPT);
733 INP_INFO_RUNLOCK(&V_tcbinfo);
736 *nam = in6_v4mapsin6_sockaddr(port, &addr);
738 *nam = in6_sockaddr(port, &addr6);
745 * Mark the connection as being incapable of further output.
748 tcp_usr_shutdown(struct socket *so)
752 struct tcpcb *tp = NULL;
755 INP_INFO_WLOCK(&V_tcbinfo);
757 KASSERT(inp != NULL, ("inp == NULL"));
759 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
767 if (!(inp->inp_flags & INP_DROPPED))
768 error = tcp_output(tp);
771 TCPDEBUG2(PRU_SHUTDOWN);
773 INP_INFO_WUNLOCK(&V_tcbinfo);
779 * After a receive, possibly send window update to peer.
782 tcp_usr_rcvd(struct socket *so, int flags)
785 struct tcpcb *tp = NULL;
790 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
792 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
800 * For passively-created TFO connections, don't attempt a window
801 * update while still in SYN_RECEIVED as this may trigger an early
802 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with
803 * application response data, or failing that, when the DELACK timer
806 if ((tp->t_flags & TF_FASTOPEN) &&
807 (tp->t_state == TCPS_SYN_RECEIVED))
811 if (tp->t_flags & TF_TOE)
812 tcp_offload_rcvd(tp);
824 * Do a send by putting data in output queue and updating urgent
825 * marker if URG set. Possibly send more data. Unlike the other
826 * pru_*() routines, the mbuf chains are our responsibility. We
827 * must either enqueue them or free them. The other pru_* routines
828 * generally are caller-frees.
831 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
832 struct sockaddr *nam, struct mbuf *control, struct thread *td)
836 struct tcpcb *tp = NULL;
843 * We require the pcbinfo lock if we will close the socket as part of
846 if (flags & PRUS_EOF)
847 INP_INFO_WLOCK(&V_tcbinfo);
849 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
851 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
860 isipv6 = nam && nam->sa_family == AF_INET6;
865 /* TCP doesn't do control messages (rights, creds, etc) */
866 if (control->m_len) {
873 m_freem(control); /* empty control, just free it */
875 if (!(flags & PRUS_OOB)) {
876 sbappendstream(&so->so_snd, m);
877 if (nam && tp->t_state < TCPS_SYN_SENT) {
879 * Do implied connect if not yet connected,
880 * initialize window to default value, and
881 * initialize maxseg/maxopd using peer's cached
886 error = tcp6_connect(tp, nam, td);
888 #if defined(INET6) && defined(INET)
892 error = tcp_connect(tp, nam, td);
896 tp->snd_wnd = TTCP_CLIENT_SND_WND;
899 if (flags & PRUS_EOF) {
901 * Close the send side of the connection after
904 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
908 if (!(inp->inp_flags & INP_DROPPED)) {
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);
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 + so->so_snd.sb_cc;
959 tp->t_flags |= TF_FORCEDATA;
960 error = tcp_output(tp);
961 tp->t_flags &= ~TF_FORCEDATA;
964 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
965 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
967 if (flags & PRUS_EOF)
968 INP_INFO_WUNLOCK(&V_tcbinfo);
973 * Abort the TCP. Drop the connection abruptly.
976 tcp_usr_abort(struct socket *so)
979 struct tcpcb *tp = NULL;
983 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
985 INP_INFO_WLOCK(&V_tcbinfo);
987 KASSERT(inp->inp_socket != NULL,
988 ("tcp_usr_abort: inp_socket == NULL"));
991 * If we still have full TCP state, and we're not dropped, drop.
993 if (!(inp->inp_flags & INP_TIMEWAIT) &&
994 !(inp->inp_flags & INP_DROPPED)) {
997 tcp_drop(tp, ECONNABORTED);
998 TCPDEBUG2(PRU_ABORT);
1000 if (!(inp->inp_flags & INP_DROPPED)) {
1002 so->so_state |= SS_PROTOREF;
1004 inp->inp_flags |= INP_SOCKREF;
1007 INP_INFO_WUNLOCK(&V_tcbinfo);
1011 * TCP socket is closed. Start friendly disconnect.
1014 tcp_usr_close(struct socket *so)
1017 struct tcpcb *tp = NULL;
1020 inp = sotoinpcb(so);
1021 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1023 INP_INFO_WLOCK(&V_tcbinfo);
1025 KASSERT(inp->inp_socket != NULL,
1026 ("tcp_usr_close: inp_socket == NULL"));
1029 * If we still have full TCP state, and we're not dropped, initiate
1032 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1033 !(inp->inp_flags & INP_DROPPED)) {
1034 tp = intotcpcb(inp);
1037 TCPDEBUG2(PRU_CLOSE);
1039 if (!(inp->inp_flags & INP_DROPPED)) {
1041 so->so_state |= SS_PROTOREF;
1043 inp->inp_flags |= INP_SOCKREF;
1046 INP_INFO_WUNLOCK(&V_tcbinfo);
1050 * Receive out-of-band data.
1053 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1057 struct tcpcb *tp = NULL;
1060 inp = sotoinpcb(so);
1061 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1063 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1067 tp = intotcpcb(inp);
1069 if ((so->so_oobmark == 0 &&
1070 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1071 so->so_options & SO_OOBINLINE ||
1072 tp->t_oobflags & TCPOOB_HADDATA) {
1076 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1077 error = EWOULDBLOCK;
1081 *mtod(m, caddr_t) = tp->t_iobc;
1082 if ((flags & MSG_PEEK) == 0)
1083 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1086 TCPDEBUG2(PRU_RCVOOB);
1092 struct pr_usrreqs tcp_usrreqs = {
1093 .pru_abort = tcp_usr_abort,
1094 .pru_accept = tcp_usr_accept,
1095 .pru_attach = tcp_usr_attach,
1096 .pru_bind = tcp_usr_bind,
1097 .pru_connect = tcp_usr_connect,
1098 .pru_control = in_control,
1099 .pru_detach = tcp_usr_detach,
1100 .pru_disconnect = tcp_usr_disconnect,
1101 .pru_listen = tcp_usr_listen,
1102 .pru_peeraddr = in_getpeeraddr,
1103 .pru_rcvd = tcp_usr_rcvd,
1104 .pru_rcvoob = tcp_usr_rcvoob,
1105 .pru_send = tcp_usr_send,
1106 .pru_shutdown = tcp_usr_shutdown,
1107 .pru_sockaddr = in_getsockaddr,
1108 .pru_sosetlabel = in_pcbsosetlabel,
1109 .pru_close = tcp_usr_close,
1114 struct pr_usrreqs tcp6_usrreqs = {
1115 .pru_abort = tcp_usr_abort,
1116 .pru_accept = tcp6_usr_accept,
1117 .pru_attach = tcp_usr_attach,
1118 .pru_bind = tcp6_usr_bind,
1119 .pru_connect = tcp6_usr_connect,
1120 .pru_control = in6_control,
1121 .pru_detach = tcp_usr_detach,
1122 .pru_disconnect = tcp_usr_disconnect,
1123 .pru_listen = tcp6_usr_listen,
1124 .pru_peeraddr = in6_mapped_peeraddr,
1125 .pru_rcvd = tcp_usr_rcvd,
1126 .pru_rcvoob = tcp_usr_rcvoob,
1127 .pru_send = tcp_usr_send,
1128 .pru_shutdown = tcp_usr_shutdown,
1129 .pru_sockaddr = in6_mapped_sockaddr,
1130 .pru_sosetlabel = in_pcbsosetlabel,
1131 .pru_close = tcp_usr_close,
1137 * Common subroutine to open a TCP connection to remote host specified
1138 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1139 * port number if needed. Call in_pcbconnect_setup to do the routing and
1140 * to choose a local host address (interface). If there is an existing
1141 * incarnation of the same connection in TIME-WAIT state and if the remote
1142 * host was sending CC options and if the connection duration was < MSL, then
1143 * truncate the previous TIME-WAIT state and proceed.
1144 * Initialize connection parameters and enter SYN-SENT state.
1147 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1149 struct inpcb *inp = tp->t_inpcb, *oinp;
1150 struct socket *so = inp->inp_socket;
1151 struct in_addr laddr;
1155 INP_WLOCK_ASSERT(inp);
1156 INP_HASH_WLOCK(&V_tcbinfo);
1158 if (inp->inp_lport == 0) {
1159 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1165 * Cannot simply call in_pcbconnect, because there might be an
1166 * earlier incarnation of this same connection still in
1167 * TIME_WAIT state, creating an ADDRINUSE error.
1169 laddr = inp->inp_laddr;
1170 lport = inp->inp_lport;
1171 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1172 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1173 if (error && oinp == NULL)
1179 inp->inp_laddr = laddr;
1181 INP_HASH_WUNLOCK(&V_tcbinfo);
1184 * Compute window scaling to request:
1185 * Scale to fit into sweet spot. See tcp_syncache.c.
1186 * XXX: This should move to tcp_output().
1188 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1189 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1190 tp->request_r_scale++;
1193 TCPSTAT_INC(tcps_connattempt);
1194 tcp_state_change(tp, TCPS_SYN_SENT);
1195 tp->iss = tcp_new_isn(tp);
1196 tcp_sendseqinit(tp);
1201 INP_HASH_WUNLOCK(&V_tcbinfo);
1208 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1210 struct inpcb *inp = tp->t_inpcb, *oinp;
1211 struct socket *so = inp->inp_socket;
1212 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1213 struct in6_addr addr6;
1216 INP_WLOCK_ASSERT(inp);
1217 INP_HASH_WLOCK(&V_tcbinfo);
1219 if (inp->inp_lport == 0) {
1220 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1226 * Cannot simply call in_pcbconnect, because there might be an
1227 * earlier incarnation of this same connection still in
1228 * TIME_WAIT state, creating an ADDRINUSE error.
1229 * in6_pcbladdr() also handles scope zone IDs.
1231 * XXXRW: We wouldn't need to expose in6_pcblookup_hash_locked()
1232 * outside of in6_pcb.c if there were an in6_pcbconnect_setup().
1234 error = in6_pcbladdr(inp, nam, &addr6);
1237 oinp = in6_pcblookup_hash_locked(inp->inp_pcbinfo,
1238 &sin6->sin6_addr, sin6->sin6_port,
1239 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1242 inp->inp_lport, 0, NULL);
1247 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1248 inp->in6p_laddr = addr6;
1249 inp->in6p_faddr = sin6->sin6_addr;
1250 inp->inp_fport = sin6->sin6_port;
1251 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
1252 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
1253 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
1255 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1257 INP_HASH_WUNLOCK(&V_tcbinfo);
1259 /* Compute window scaling to request. */
1260 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1261 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1262 tp->request_r_scale++;
1265 TCPSTAT_INC(tcps_connattempt);
1266 tcp_state_change(tp, TCPS_SYN_SENT);
1267 tp->iss = tcp_new_isn(tp);
1268 tcp_sendseqinit(tp);
1273 INP_HASH_WUNLOCK(&V_tcbinfo);
1279 * Export TCP internal state information via a struct tcp_info, based on the
1280 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1281 * (TCP state machine, etc). We export all information using FreeBSD-native
1282 * constants -- for example, the numeric values for tcpi_state will differ
1286 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1289 INP_WLOCK_ASSERT(tp->t_inpcb);
1290 bzero(ti, sizeof(*ti));
1292 ti->tcpi_state = tp->t_state;
1293 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1294 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1295 if (tp->t_flags & TF_SACK_PERMIT)
1296 ti->tcpi_options |= TCPI_OPT_SACK;
1297 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1298 ti->tcpi_options |= TCPI_OPT_WSCALE;
1299 ti->tcpi_snd_wscale = tp->snd_scale;
1300 ti->tcpi_rcv_wscale = tp->rcv_scale;
1303 ti->tcpi_rto = tp->t_rxtcur * tick;
1304 ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
1305 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1306 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1308 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1309 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1312 * FreeBSD-specific extension fields for tcp_info.
1314 ti->tcpi_rcv_space = tp->rcv_wnd;
1315 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1316 ti->tcpi_snd_wnd = tp->snd_wnd;
1317 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1318 ti->tcpi_snd_nxt = tp->snd_nxt;
1319 ti->tcpi_snd_mss = tp->t_maxseg;
1320 ti->tcpi_rcv_mss = tp->t_maxseg;
1321 if (tp->t_flags & TF_TOE)
1322 ti->tcpi_options |= TCPI_OPT_TOE;
1323 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1324 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1325 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1329 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1330 * socket option arguments. When it re-acquires the lock after the copy, it
1331 * has to revalidate that the connection is still valid for the socket
1334 #define INP_WLOCK_RECHECK(inp) do { \
1336 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1338 return (ECONNRESET); \
1340 tp = intotcpcb(inp); \
1344 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1346 int error, opt, optval;
1351 char buf[TCP_CA_NAME_MAX];
1352 struct cc_algo *algo;
1355 inp = sotoinpcb(so);
1356 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1358 if (sopt->sopt_level != IPPROTO_TCP) {
1360 if (inp->inp_vflag & INP_IPV6PROTO) {
1362 error = ip6_ctloutput(so, sopt);
1365 #if defined(INET6) && defined(INET)
1371 error = ip_ctloutput(so, sopt);
1376 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1378 return (ECONNRESET);
1381 switch (sopt->sopt_dir) {
1383 switch (sopt->sopt_name) {
1384 #ifdef TCP_SIGNATURE
1387 error = sooptcopyin(sopt, &optval, sizeof optval,
1392 INP_WLOCK_RECHECK(inp);
1394 tp->t_flags |= TF_SIGNATURE;
1396 tp->t_flags &= ~TF_SIGNATURE;
1397 goto unlock_and_done;
1398 #endif /* TCP_SIGNATURE */
1403 error = sooptcopyin(sopt, &optval, sizeof optval,
1408 INP_WLOCK_RECHECK(inp);
1409 switch (sopt->sopt_name) {
1417 opt = 0; /* dead code to fool gcc */
1424 tp->t_flags &= ~opt;
1427 if (tp->t_flags & TF_TOE) {
1428 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1437 error = sooptcopyin(sopt, &optval, sizeof optval,
1442 INP_WLOCK_RECHECK(inp);
1444 tp->t_flags |= TF_NOPUSH;
1445 else if (tp->t_flags & TF_NOPUSH) {
1446 tp->t_flags &= ~TF_NOPUSH;
1447 if (TCPS_HAVEESTABLISHED(tp->t_state))
1448 error = tcp_output(tp);
1450 goto unlock_and_done;
1454 error = sooptcopyin(sopt, &optval, sizeof optval,
1459 INP_WLOCK_RECHECK(inp);
1460 if (optval > 0 && optval <= tp->t_maxseg &&
1461 optval + 40 >= V_tcp_minmss)
1462 tp->t_maxseg = optval;
1465 goto unlock_and_done;
1472 case TCP_CONGESTION:
1474 bzero(buf, sizeof(buf));
1475 error = sooptcopyin(sopt, &buf, sizeof(buf), 1);
1478 INP_WLOCK_RECHECK(inp);
1480 * Return EINVAL if we can't find the requested cc algo.
1484 STAILQ_FOREACH(algo, &cc_list, entries) {
1485 if (strncmp(buf, algo->name, TCP_CA_NAME_MAX)
1487 /* We've found the requested algo. */
1490 * We hold a write lock over the tcb
1491 * so it's safe to do these things
1492 * without ordering concerns.
1494 if (CC_ALGO(tp)->cb_destroy != NULL)
1495 CC_ALGO(tp)->cb_destroy(tp->ccv);
1498 * If something goes pear shaped
1499 * initialising the new algo,
1500 * fall back to newreno (which
1501 * does not require initialisation).
1503 if (algo->cb_init != NULL)
1504 if (algo->cb_init(tp->ccv) > 0) {
1505 CC_ALGO(tp) = &newreno_cc_algo;
1507 * The only reason init
1509 * because of malloc.
1513 break; /* Break the STAILQ_FOREACH. */
1517 goto unlock_and_done;
1523 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1527 if (ui > (UINT_MAX / hz)) {
1533 INP_WLOCK_RECHECK(inp);
1534 switch (sopt->sopt_name) {
1536 tp->t_keepidle = ui;
1538 * XXX: better check current remaining
1539 * timeout and "merge" it with new value.
1541 if ((tp->t_state > TCPS_LISTEN) &&
1542 (tp->t_state <= TCPS_CLOSING))
1543 tcp_timer_activate(tp, TT_KEEP,
1547 tp->t_keepintvl = ui;
1548 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1549 (TP_MAXIDLE(tp) > 0))
1550 tcp_timer_activate(tp, TT_2MSL,
1554 tp->t_keepinit = ui;
1555 if (tp->t_state == TCPS_SYN_RECEIVED ||
1556 tp->t_state == TCPS_SYN_SENT)
1557 tcp_timer_activate(tp, TT_KEEP,
1561 goto unlock_and_done;
1565 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1569 INP_WLOCK_RECHECK(inp);
1571 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1572 (TP_MAXIDLE(tp) > 0))
1573 tcp_timer_activate(tp, TT_2MSL,
1575 goto unlock_and_done;
1580 if (!V_tcp_fastopen_enabled)
1583 error = sooptcopyin(sopt, &optval, sizeof optval,
1588 INP_WLOCK_RECHECK(inp);
1590 tp->t_flags |= TF_FASTOPEN;
1591 if ((tp->t_state == TCPS_LISTEN) &&
1592 (tp->t_tfo_pending == NULL))
1594 tcp_fastopen_alloc_counter();
1596 tp->t_flags &= ~TF_FASTOPEN;
1597 goto unlock_and_done;
1602 error = ENOPROTOOPT;
1608 tp = intotcpcb(inp);
1609 switch (sopt->sopt_name) {
1610 #ifdef TCP_SIGNATURE
1612 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1614 error = sooptcopyout(sopt, &optval, sizeof optval);
1619 optval = tp->t_flags & TF_NODELAY;
1621 error = sooptcopyout(sopt, &optval, sizeof optval);
1624 optval = tp->t_maxseg;
1626 error = sooptcopyout(sopt, &optval, sizeof optval);
1629 optval = tp->t_flags & TF_NOOPT;
1631 error = sooptcopyout(sopt, &optval, sizeof optval);
1634 optval = tp->t_flags & TF_NOPUSH;
1636 error = sooptcopyout(sopt, &optval, sizeof optval);
1639 tcp_fill_info(tp, &ti);
1641 error = sooptcopyout(sopt, &ti, sizeof ti);
1643 case TCP_CONGESTION:
1644 bzero(buf, sizeof(buf));
1645 strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1647 error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX);
1653 switch (sopt->sopt_name) {
1655 ui = tp->t_keepidle / hz;
1658 ui = tp->t_keepintvl / hz;
1661 ui = tp->t_keepinit / hz;
1668 error = sooptcopyout(sopt, &ui, sizeof(ui));
1672 optval = tp->t_flags & TF_FASTOPEN;
1674 error = sooptcopyout(sopt, &optval, sizeof optval);
1679 error = ENOPROTOOPT;
1686 #undef INP_WLOCK_RECHECK
1689 * Attach TCP protocol to socket, allocating
1690 * internet protocol control block, tcp control block,
1691 * bufer space, and entering LISTEN state if to accept connections.
1694 tcp_attach(struct socket *so)
1700 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1701 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1705 so->so_rcv.sb_flags |= SB_AUTOSIZE;
1706 so->so_snd.sb_flags |= SB_AUTOSIZE;
1707 INP_INFO_WLOCK(&V_tcbinfo);
1708 error = in_pcballoc(so, &V_tcbinfo);
1710 INP_INFO_WUNLOCK(&V_tcbinfo);
1713 inp = sotoinpcb(so);
1715 if (inp->inp_vflag & INP_IPV6PROTO) {
1716 inp->inp_vflag |= INP_IPV6;
1717 inp->in6p_hops = -1; /* use kernel default */
1721 inp->inp_vflag |= INP_IPV4;
1722 tp = tcp_newtcpcb(inp);
1726 INP_INFO_WUNLOCK(&V_tcbinfo);
1729 tp->t_state = TCPS_CLOSED;
1731 INP_INFO_WUNLOCK(&V_tcbinfo);
1736 * Initiate (or continue) disconnect.
1737 * If embryonic state, just send reset (once).
1738 * If in ``let data drain'' option and linger null, just drop.
1739 * Otherwise (hard), mark socket disconnecting and drop
1740 * current input data; switch states based on user close, and
1741 * send segment to peer (with FIN).
1744 tcp_disconnect(struct tcpcb *tp)
1746 struct inpcb *inp = tp->t_inpcb;
1747 struct socket *so = inp->inp_socket;
1749 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1750 INP_WLOCK_ASSERT(inp);
1753 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1754 * socket is still open.
1756 if (tp->t_state < TCPS_ESTABLISHED) {
1759 ("tcp_disconnect: tcp_close() returned NULL"));
1760 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1761 tp = tcp_drop(tp, 0);
1763 ("tcp_disconnect: tcp_drop() returned NULL"));
1765 soisdisconnecting(so);
1766 sbflush(&so->so_rcv);
1768 if (!(inp->inp_flags & INP_DROPPED))
1774 * User issued close, and wish to trail through shutdown states:
1775 * if never received SYN, just forget it. If got a SYN from peer,
1776 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1777 * If already got a FIN from peer, then almost done; go to LAST_ACK
1778 * state. In all other cases, have already sent FIN to peer (e.g.
1779 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1780 * for peer to send FIN or not respond to keep-alives, etc.
1781 * We can let the user exit from the close as soon as the FIN is acked.
1784 tcp_usrclosed(struct tcpcb *tp)
1787 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1788 INP_WLOCK_ASSERT(tp->t_inpcb);
1790 switch (tp->t_state) {
1793 tcp_offload_listen_stop(tp);
1795 tcp_state_change(tp, TCPS_CLOSED);
1800 * tcp_close() should never return NULL here as the socket is
1804 ("tcp_usrclosed: tcp_close() returned NULL"));
1808 case TCPS_SYN_RECEIVED:
1809 tp->t_flags |= TF_NEEDFIN;
1812 case TCPS_ESTABLISHED:
1813 tcp_state_change(tp, TCPS_FIN_WAIT_1);
1816 case TCPS_CLOSE_WAIT:
1817 tcp_state_change(tp, TCPS_LAST_ACK);
1820 if (tp->t_state >= TCPS_FIN_WAIT_2) {
1821 soisdisconnected(tp->t_inpcb->inp_socket);
1822 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
1823 if (tp->t_state == TCPS_FIN_WAIT_2) {
1826 timeout = (tcp_fast_finwait2_recycle) ?
1827 tcp_finwait2_timeout : TP_MAXIDLE(tp);
1828 tcp_timer_activate(tp, TT_2MSL, timeout);
1835 db_print_indent(int indent)
1839 for (i = 0; i < indent; i++)
1844 db_print_tstate(int t_state)
1849 db_printf("TCPS_CLOSED");
1853 db_printf("TCPS_LISTEN");
1857 db_printf("TCPS_SYN_SENT");
1860 case TCPS_SYN_RECEIVED:
1861 db_printf("TCPS_SYN_RECEIVED");
1864 case TCPS_ESTABLISHED:
1865 db_printf("TCPS_ESTABLISHED");
1868 case TCPS_CLOSE_WAIT:
1869 db_printf("TCPS_CLOSE_WAIT");
1872 case TCPS_FIN_WAIT_1:
1873 db_printf("TCPS_FIN_WAIT_1");
1877 db_printf("TCPS_CLOSING");
1881 db_printf("TCPS_LAST_ACK");
1884 case TCPS_FIN_WAIT_2:
1885 db_printf("TCPS_FIN_WAIT_2");
1888 case TCPS_TIME_WAIT:
1889 db_printf("TCPS_TIME_WAIT");
1893 db_printf("unknown");
1899 db_print_tflags(u_int t_flags)
1904 if (t_flags & TF_ACKNOW) {
1905 db_printf("%sTF_ACKNOW", comma ? ", " : "");
1908 if (t_flags & TF_DELACK) {
1909 db_printf("%sTF_DELACK", comma ? ", " : "");
1912 if (t_flags & TF_NODELAY) {
1913 db_printf("%sTF_NODELAY", comma ? ", " : "");
1916 if (t_flags & TF_NOOPT) {
1917 db_printf("%sTF_NOOPT", comma ? ", " : "");
1920 if (t_flags & TF_SENTFIN) {
1921 db_printf("%sTF_SENTFIN", comma ? ", " : "");
1924 if (t_flags & TF_REQ_SCALE) {
1925 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1928 if (t_flags & TF_RCVD_SCALE) {
1929 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1932 if (t_flags & TF_REQ_TSTMP) {
1933 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1936 if (t_flags & TF_RCVD_TSTMP) {
1937 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1940 if (t_flags & TF_SACK_PERMIT) {
1941 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1944 if (t_flags & TF_NEEDSYN) {
1945 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1948 if (t_flags & TF_NEEDFIN) {
1949 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1952 if (t_flags & TF_NOPUSH) {
1953 db_printf("%sTF_NOPUSH", comma ? ", " : "");
1956 if (t_flags & TF_MORETOCOME) {
1957 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1960 if (t_flags & TF_LQ_OVERFLOW) {
1961 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1964 if (t_flags & TF_LASTIDLE) {
1965 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1968 if (t_flags & TF_RXWIN0SENT) {
1969 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1972 if (t_flags & TF_FASTRECOVERY) {
1973 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1976 if (t_flags & TF_CONGRECOVERY) {
1977 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
1980 if (t_flags & TF_WASFRECOVERY) {
1981 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1984 if (t_flags & TF_SIGNATURE) {
1985 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1988 if (t_flags & TF_FORCEDATA) {
1989 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1992 if (t_flags & TF_TSO) {
1993 db_printf("%sTF_TSO", comma ? ", " : "");
1996 if (t_flags & TF_ECN_PERMIT) {
1997 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
2000 if (t_flags & TF_FASTOPEN) {
2001 db_printf("%sTF_FASTOPEN", 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);