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>
82 #include <netinet/tcp_fsm.h>
83 #include <netinet/tcp_seq.h>
84 #include <netinet/tcp_timer.h>
85 #include <netinet/tcp_var.h>
86 #include <netinet/tcpip.h>
88 #include <netinet/tcp_debug.h>
91 #include <netinet/tcp_offload.h>
95 * TCP protocol interface to socket abstraction.
97 static int tcp_attach(struct socket *);
99 static int tcp_connect(struct tcpcb *, struct sockaddr *,
103 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
106 static void tcp_disconnect(struct tcpcb *);
107 static void tcp_usrclosed(struct tcpcb *);
108 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
111 #define TCPDEBUG0 int ostate = 0
112 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
113 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
114 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
118 #define TCPDEBUG2(req)
122 * TCP attaches to socket via pru_attach(), reserving space,
123 * and an internet control block.
126 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
129 struct tcpcb *tp = NULL;
134 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
137 error = tcp_attach(so);
141 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
142 so->so_linger = TCP_LINGERTIME;
147 TCPDEBUG2(PRU_ATTACH);
152 * tcp_detach is called when the socket layer loses its final reference
153 * to the socket, be it a file descriptor reference, a reference from TCP,
154 * etc. At this point, there is only one case in which we will keep around
155 * inpcb state: time wait.
157 * This function can probably be re-absorbed back into tcp_usr_detach() now
158 * that there is a single detach path.
161 tcp_detach(struct socket *so, struct inpcb *inp)
165 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
166 INP_WLOCK_ASSERT(inp);
168 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
169 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
173 if (inp->inp_flags & INP_TIMEWAIT) {
175 * There are two cases to handle: one in which the time wait
176 * state is being discarded (INP_DROPPED), and one in which
177 * this connection will remain in timewait. In the former,
178 * it is time to discard all state (except tcptw, which has
179 * already been discarded by the timewait close code, which
180 * should be further up the call stack somewhere). In the
181 * latter case, we detach from the socket, but leave the pcb
182 * present until timewait ends.
184 * XXXRW: Would it be cleaner to free the tcptw here?
186 * Astute question indeed, from twtcp perspective there are
187 * three cases to consider:
189 * #1 tcp_detach is called at tcptw creation time by
190 * tcp_twstart, then do not discard the newly created tcptw
191 * and leave inpcb present until timewait ends
192 * #2 tcp_detach is called at timewait end (or reuse) by
193 * tcp_twclose, then the tcptw has already been discarded
194 * and inpcb is freed here
195 * #3 tcp_detach is called() after timewait ends (or reuse)
196 * (e.g. by soclose), then tcptw has already been discarded
197 * and inpcb is freed here
199 * In all three cases the tcptw should not be freed here.
201 if (inp->inp_flags & INP_DROPPED) {
202 KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
203 "INP_DROPPED && tp != NULL"));
212 * If the connection is not in timewait, we consider two
213 * two conditions: one in which no further processing is
214 * necessary (dropped || embryonic), and one in which TCP is
215 * not yet done, but no longer requires the socket, so the
216 * pcb will persist for the time being.
218 * XXXRW: Does the second case still occur?
220 if (inp->inp_flags & INP_DROPPED ||
221 tp->t_state < TCPS_SYN_SENT) {
233 * pru_detach() detaches the TCP protocol from the socket.
234 * If the protocol state is non-embryonic, then can't
235 * do this directly: have to initiate a pru_disconnect(),
236 * which may finish later; embryonic TCB's can just
240 tcp_usr_detach(struct socket *so)
245 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
246 INP_INFO_WLOCK(&V_tcbinfo);
248 KASSERT(inp->inp_socket != NULL,
249 ("tcp_usr_detach: inp_socket == NULL"));
251 INP_INFO_WUNLOCK(&V_tcbinfo);
256 * Give the socket an address.
259 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
263 struct tcpcb *tp = NULL;
264 struct sockaddr_in *sinp;
266 sinp = (struct sockaddr_in *)nam;
267 if (nam->sa_len != sizeof (*sinp))
270 * Must check for multicast addresses and disallow binding
273 if (sinp->sin_family == AF_INET &&
274 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
275 return (EAFNOSUPPORT);
279 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
281 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
287 INP_HASH_WLOCK(&V_tcbinfo);
288 error = in_pcbbind(inp, nam, td->td_ucred);
289 INP_HASH_WUNLOCK(&V_tcbinfo);
300 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
304 struct tcpcb *tp = NULL;
305 struct sockaddr_in6 *sin6p;
307 sin6p = (struct sockaddr_in6 *)nam;
308 if (nam->sa_len != sizeof (*sin6p))
311 * Must check for multicast addresses and disallow binding
314 if (sin6p->sin6_family == AF_INET6 &&
315 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
316 return (EAFNOSUPPORT);
320 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
322 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
328 INP_HASH_WLOCK(&V_tcbinfo);
329 inp->inp_vflag &= ~INP_IPV4;
330 inp->inp_vflag |= INP_IPV6;
332 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
333 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
334 inp->inp_vflag |= INP_IPV4;
335 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
336 struct sockaddr_in sin;
338 in6_sin6_2_sin(&sin, sin6p);
339 inp->inp_vflag |= INP_IPV4;
340 inp->inp_vflag &= ~INP_IPV6;
341 error = in_pcbbind(inp, (struct sockaddr *)&sin,
343 INP_HASH_WUNLOCK(&V_tcbinfo);
348 error = in6_pcbbind(inp, nam, td->td_ucred);
349 INP_HASH_WUNLOCK(&V_tcbinfo);
359 * Prepare to accept connections.
362 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
366 struct tcpcb *tp = NULL;
370 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
372 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
379 error = solisten_proto_check(so);
380 INP_HASH_WLOCK(&V_tcbinfo);
381 if (error == 0 && inp->inp_lport == 0)
382 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
383 INP_HASH_WUNLOCK(&V_tcbinfo);
385 tcp_state_change(tp, TCPS_LISTEN);
386 solisten_proto(so, backlog);
388 if ((so->so_options & SO_NO_OFFLOAD) == 0)
389 tcp_offload_listen_start(tp);
395 TCPDEBUG2(PRU_LISTEN);
403 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
407 struct tcpcb *tp = NULL;
411 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
413 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
420 error = solisten_proto_check(so);
421 INP_HASH_WLOCK(&V_tcbinfo);
422 if (error == 0 && inp->inp_lport == 0) {
423 inp->inp_vflag &= ~INP_IPV4;
424 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
425 inp->inp_vflag |= INP_IPV4;
426 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
428 INP_HASH_WUNLOCK(&V_tcbinfo);
430 tcp_state_change(tp, TCPS_LISTEN);
431 solisten_proto(so, backlog);
433 if ((so->so_options & SO_NO_OFFLOAD) == 0)
434 tcp_offload_listen_start(tp);
440 TCPDEBUG2(PRU_LISTEN);
448 * Initiate connection to peer.
449 * Create a template for use in transmissions on this connection.
450 * Enter SYN_SENT state, and mark socket as connecting.
451 * Start keep-alive timer, and seed output sequence space.
452 * Send initial segment on connection.
455 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
459 struct tcpcb *tp = NULL;
460 struct sockaddr_in *sinp;
462 sinp = (struct sockaddr_in *)nam;
463 if (nam->sa_len != sizeof (*sinp))
466 * Must disallow TCP ``connections'' to multicast addresses.
468 if (sinp->sin_family == AF_INET
469 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
470 return (EAFNOSUPPORT);
471 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
476 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
478 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
484 if ((error = tcp_connect(tp, nam, td)) != 0)
487 if (registered_toedevs > 0 &&
488 (so->so_options & SO_NO_OFFLOAD) == 0 &&
489 (error = tcp_offload_connect(so, nam)) == 0)
492 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
493 error = tcp_output(tp);
495 TCPDEBUG2(PRU_CONNECT);
503 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
507 struct tcpcb *tp = NULL;
508 struct sockaddr_in6 *sin6p;
512 sin6p = (struct sockaddr_in6 *)nam;
513 if (nam->sa_len != sizeof (*sin6p))
516 * Must disallow TCP ``connections'' to multicast addresses.
518 if (sin6p->sin6_family == AF_INET6
519 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
520 return (EAFNOSUPPORT);
523 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
525 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
533 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
534 * therefore probably require the hash lock, which isn't held here.
535 * Is this a significant problem?
537 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
538 struct sockaddr_in sin;
540 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
545 in6_sin6_2_sin(&sin, sin6p);
546 inp->inp_vflag |= INP_IPV4;
547 inp->inp_vflag &= ~INP_IPV6;
548 if ((error = prison_remote_ip4(td->td_ucred,
549 &sin.sin_addr)) != 0)
551 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
554 if (registered_toedevs > 0 &&
555 (so->so_options & SO_NO_OFFLOAD) == 0 &&
556 (error = tcp_offload_connect(so, nam)) == 0)
559 error = tcp_output(tp);
563 inp->inp_vflag &= ~INP_IPV4;
564 inp->inp_vflag |= INP_IPV6;
565 inp->inp_inc.inc_flags |= INC_ISIPV6;
566 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
568 if ((error = tcp6_connect(tp, nam, td)) != 0)
571 if (registered_toedevs > 0 &&
572 (so->so_options & SO_NO_OFFLOAD) == 0 &&
573 (error = tcp_offload_connect(so, nam)) == 0)
576 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
577 error = tcp_output(tp);
580 TCPDEBUG2(PRU_CONNECT);
587 * Initiate disconnect from peer.
588 * If connection never passed embryonic stage, just drop;
589 * else if don't need to let data drain, then can just drop anyways,
590 * else have to begin TCP shutdown process: mark socket disconnecting,
591 * drain unread data, state switch to reflect user close, and
592 * send segment (e.g. FIN) to peer. Socket will be really disconnected
593 * when peer sends FIN and acks ours.
595 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
598 tcp_usr_disconnect(struct socket *so)
601 struct tcpcb *tp = NULL;
605 INP_INFO_WLOCK(&V_tcbinfo);
607 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
609 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
617 TCPDEBUG2(PRU_DISCONNECT);
619 INP_INFO_WUNLOCK(&V_tcbinfo);
625 * Accept a connection. Essentially all the work is done at higher levels;
626 * just return the address of the peer, storing through addr.
628 * The rationale for acquiring the tcbinfo lock here is somewhat complicated,
629 * and is described in detail in the commit log entry for r175612. Acquiring
630 * it delays an accept(2) racing with sonewconn(), which inserts the socket
631 * before the inpcb address/port fields are initialized. A better fix would
632 * prevent the socket from being placed in the listen queue until all fields
633 * are fully initialized.
636 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
639 struct inpcb *inp = NULL;
640 struct tcpcb *tp = NULL;
645 if (so->so_state & SS_ISDISCONNECTED)
646 return (ECONNABORTED);
649 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
650 INP_INFO_RLOCK(&V_tcbinfo);
652 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
653 error = ECONNABORTED;
660 * We inline in_getpeeraddr and COMMON_END here, so that we can
661 * copy the data of interest and defer the malloc until after we
664 port = inp->inp_fport;
665 addr = inp->inp_faddr;
668 TCPDEBUG2(PRU_ACCEPT);
670 INP_INFO_RUNLOCK(&V_tcbinfo);
672 *nam = in_sockaddr(port, &addr);
679 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
681 struct inpcb *inp = NULL;
683 struct tcpcb *tp = NULL;
685 struct in6_addr addr6;
690 if (so->so_state & SS_ISDISCONNECTED)
691 return (ECONNABORTED);
694 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
695 INP_INFO_RLOCK(&V_tcbinfo);
697 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
698 error = ECONNABORTED;
705 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
706 * copy the data of interest and defer the malloc until after we
709 if (inp->inp_vflag & INP_IPV4) {
711 port = inp->inp_fport;
712 addr = inp->inp_faddr;
714 port = inp->inp_fport;
715 addr6 = inp->in6p_faddr;
719 TCPDEBUG2(PRU_ACCEPT);
721 INP_INFO_RUNLOCK(&V_tcbinfo);
724 *nam = in6_v4mapsin6_sockaddr(port, &addr);
726 *nam = in6_sockaddr(port, &addr6);
733 * Mark the connection as being incapable of further output.
736 tcp_usr_shutdown(struct socket *so)
740 struct tcpcb *tp = NULL;
743 INP_INFO_WLOCK(&V_tcbinfo);
745 KASSERT(inp != NULL, ("inp == NULL"));
747 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
755 if (!(inp->inp_flags & INP_DROPPED))
756 error = tcp_output(tp);
759 TCPDEBUG2(PRU_SHUTDOWN);
761 INP_INFO_WUNLOCK(&V_tcbinfo);
767 * After a receive, possibly send window update to peer.
770 tcp_usr_rcvd(struct socket *so, int flags)
773 struct tcpcb *tp = NULL;
778 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
780 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
787 if (tp->t_flags & TF_TOE)
788 tcp_offload_rcvd(tp);
800 * Do a send by putting data in output queue and updating urgent
801 * marker if URG set. Possibly send more data. Unlike the other
802 * pru_*() routines, the mbuf chains are our responsibility. We
803 * must either enqueue them or free them. The other pru_* routines
804 * generally are caller-frees.
807 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
808 struct sockaddr *nam, struct mbuf *control, struct thread *td)
812 struct tcpcb *tp = NULL;
819 * We require the pcbinfo lock if we will close the socket as part of
822 if (flags & PRUS_EOF)
823 INP_INFO_WLOCK(&V_tcbinfo);
825 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
827 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
836 isipv6 = nam && nam->sa_family == AF_INET6;
841 /* TCP doesn't do control messages (rights, creds, etc) */
842 if (control->m_len) {
849 m_freem(control); /* empty control, just free it */
851 if (!(flags & PRUS_OOB)) {
852 sbappendstream(&so->so_snd, m);
853 if (nam && tp->t_state < TCPS_SYN_SENT) {
855 * Do implied connect if not yet connected,
856 * initialize window to default value, and
857 * initialize maxseg/maxopd using peer's cached
862 error = tcp6_connect(tp, nam, td);
864 #if defined(INET6) && defined(INET)
868 error = tcp_connect(tp, nam, td);
872 tp->snd_wnd = TTCP_CLIENT_SND_WND;
875 if (flags & PRUS_EOF) {
877 * Close the send side of the connection after
880 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
884 if (!(inp->inp_flags & INP_DROPPED)) {
885 if (flags & PRUS_MORETOCOME)
886 tp->t_flags |= TF_MORETOCOME;
887 error = tcp_output(tp);
888 if (flags & PRUS_MORETOCOME)
889 tp->t_flags &= ~TF_MORETOCOME;
893 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
895 SOCKBUF_LOCK(&so->so_snd);
896 if (sbspace(&so->so_snd) < -512) {
897 SOCKBUF_UNLOCK(&so->so_snd);
903 * According to RFC961 (Assigned Protocols),
904 * the urgent pointer points to the last octet
905 * of urgent data. We continue, however,
906 * to consider it to indicate the first octet
907 * of data past the urgent section.
908 * Otherwise, snd_up should be one lower.
910 sbappendstream_locked(&so->so_snd, m);
911 SOCKBUF_UNLOCK(&so->so_snd);
912 if (nam && tp->t_state < TCPS_SYN_SENT) {
914 * Do implied connect if not yet connected,
915 * initialize window to default value, and
916 * initialize maxseg/maxopd using peer's cached
921 error = tcp6_connect(tp, nam, td);
923 #if defined(INET6) && defined(INET)
927 error = tcp_connect(tp, nam, td);
931 tp->snd_wnd = TTCP_CLIENT_SND_WND;
934 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
935 tp->t_flags |= TF_FORCEDATA;
936 error = tcp_output(tp);
937 tp->t_flags &= ~TF_FORCEDATA;
940 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
941 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
943 if (flags & PRUS_EOF)
944 INP_INFO_WUNLOCK(&V_tcbinfo);
949 * Abort the TCP. Drop the connection abruptly.
952 tcp_usr_abort(struct socket *so)
955 struct tcpcb *tp = NULL;
959 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
961 INP_INFO_WLOCK(&V_tcbinfo);
963 KASSERT(inp->inp_socket != NULL,
964 ("tcp_usr_abort: inp_socket == NULL"));
967 * If we still have full TCP state, and we're not dropped, drop.
969 if (!(inp->inp_flags & INP_TIMEWAIT) &&
970 !(inp->inp_flags & INP_DROPPED)) {
973 tcp_drop(tp, ECONNABORTED);
974 TCPDEBUG2(PRU_ABORT);
976 if (!(inp->inp_flags & INP_DROPPED)) {
978 so->so_state |= SS_PROTOREF;
980 inp->inp_flags |= INP_SOCKREF;
983 INP_INFO_WUNLOCK(&V_tcbinfo);
987 * TCP socket is closed. Start friendly disconnect.
990 tcp_usr_close(struct socket *so)
993 struct tcpcb *tp = NULL;
997 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
999 INP_INFO_WLOCK(&V_tcbinfo);
1001 KASSERT(inp->inp_socket != NULL,
1002 ("tcp_usr_close: inp_socket == NULL"));
1005 * If we still have full TCP state, and we're not dropped, initiate
1008 if (!(inp->inp_flags & INP_TIMEWAIT) &&
1009 !(inp->inp_flags & INP_DROPPED)) {
1010 tp = intotcpcb(inp);
1013 TCPDEBUG2(PRU_CLOSE);
1015 if (!(inp->inp_flags & INP_DROPPED)) {
1017 so->so_state |= SS_PROTOREF;
1019 inp->inp_flags |= INP_SOCKREF;
1022 INP_INFO_WUNLOCK(&V_tcbinfo);
1026 * Receive out-of-band data.
1029 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1033 struct tcpcb *tp = NULL;
1036 inp = sotoinpcb(so);
1037 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
1039 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1043 tp = intotcpcb(inp);
1045 if ((so->so_oobmark == 0 &&
1046 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
1047 so->so_options & SO_OOBINLINE ||
1048 tp->t_oobflags & TCPOOB_HADDATA) {
1052 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1053 error = EWOULDBLOCK;
1057 *mtod(m, caddr_t) = tp->t_iobc;
1058 if ((flags & MSG_PEEK) == 0)
1059 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1062 TCPDEBUG2(PRU_RCVOOB);
1068 struct pr_usrreqs tcp_usrreqs = {
1069 .pru_abort = tcp_usr_abort,
1070 .pru_accept = tcp_usr_accept,
1071 .pru_attach = tcp_usr_attach,
1072 .pru_bind = tcp_usr_bind,
1073 .pru_connect = tcp_usr_connect,
1074 .pru_control = in_control,
1075 .pru_detach = tcp_usr_detach,
1076 .pru_disconnect = tcp_usr_disconnect,
1077 .pru_listen = tcp_usr_listen,
1078 .pru_peeraddr = in_getpeeraddr,
1079 .pru_rcvd = tcp_usr_rcvd,
1080 .pru_rcvoob = tcp_usr_rcvoob,
1081 .pru_send = tcp_usr_send,
1082 .pru_shutdown = tcp_usr_shutdown,
1083 .pru_sockaddr = in_getsockaddr,
1084 .pru_sosetlabel = in_pcbsosetlabel,
1085 .pru_close = tcp_usr_close,
1090 struct pr_usrreqs tcp6_usrreqs = {
1091 .pru_abort = tcp_usr_abort,
1092 .pru_accept = tcp6_usr_accept,
1093 .pru_attach = tcp_usr_attach,
1094 .pru_bind = tcp6_usr_bind,
1095 .pru_connect = tcp6_usr_connect,
1096 .pru_control = in6_control,
1097 .pru_detach = tcp_usr_detach,
1098 .pru_disconnect = tcp_usr_disconnect,
1099 .pru_listen = tcp6_usr_listen,
1100 .pru_peeraddr = in6_mapped_peeraddr,
1101 .pru_rcvd = tcp_usr_rcvd,
1102 .pru_rcvoob = tcp_usr_rcvoob,
1103 .pru_send = tcp_usr_send,
1104 .pru_shutdown = tcp_usr_shutdown,
1105 .pru_sockaddr = in6_mapped_sockaddr,
1106 .pru_sosetlabel = in_pcbsosetlabel,
1107 .pru_close = tcp_usr_close,
1113 * Common subroutine to open a TCP connection to remote host specified
1114 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1115 * port number if needed. Call in_pcbconnect_setup to do the routing and
1116 * to choose a local host address (interface). If there is an existing
1117 * incarnation of the same connection in TIME-WAIT state and if the remote
1118 * host was sending CC options and if the connection duration was < MSL, then
1119 * truncate the previous TIME-WAIT state and proceed.
1120 * Initialize connection parameters and enter SYN-SENT state.
1123 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1125 struct inpcb *inp = tp->t_inpcb, *oinp;
1126 struct socket *so = inp->inp_socket;
1127 struct in_addr laddr;
1131 INP_WLOCK_ASSERT(inp);
1132 INP_HASH_WLOCK(&V_tcbinfo);
1134 if (inp->inp_lport == 0) {
1135 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1141 * Cannot simply call in_pcbconnect, because there might be an
1142 * earlier incarnation of this same connection still in
1143 * TIME_WAIT state, creating an ADDRINUSE error.
1145 laddr = inp->inp_laddr;
1146 lport = inp->inp_lport;
1147 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1148 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1149 if (error && oinp == NULL)
1155 inp->inp_laddr = laddr;
1157 INP_HASH_WUNLOCK(&V_tcbinfo);
1160 * Compute window scaling to request:
1161 * Scale to fit into sweet spot. See tcp_syncache.c.
1162 * XXX: This should move to tcp_output().
1164 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1165 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1166 tp->request_r_scale++;
1169 TCPSTAT_INC(tcps_connattempt);
1170 tcp_state_change(tp, TCPS_SYN_SENT);
1171 tp->iss = tcp_new_isn(tp);
1172 tcp_sendseqinit(tp);
1177 INP_HASH_WUNLOCK(&V_tcbinfo);
1184 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1186 struct inpcb *inp = tp->t_inpcb, *oinp;
1187 struct socket *so = inp->inp_socket;
1188 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1189 struct in6_addr addr6;
1192 INP_WLOCK_ASSERT(inp);
1193 INP_HASH_WLOCK(&V_tcbinfo);
1195 if (inp->inp_lport == 0) {
1196 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1202 * Cannot simply call in_pcbconnect, because there might be an
1203 * earlier incarnation of this same connection still in
1204 * TIME_WAIT state, creating an ADDRINUSE error.
1205 * in6_pcbladdr() also handles scope zone IDs.
1207 * XXXRW: We wouldn't need to expose in6_pcblookup_hash_locked()
1208 * outside of in6_pcb.c if there were an in6_pcbconnect_setup().
1210 error = in6_pcbladdr(inp, nam, &addr6);
1213 oinp = in6_pcblookup_hash_locked(inp->inp_pcbinfo,
1214 &sin6->sin6_addr, sin6->sin6_port,
1215 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1218 inp->inp_lport, 0, NULL);
1223 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1224 inp->in6p_laddr = addr6;
1225 inp->in6p_faddr = sin6->sin6_addr;
1226 inp->inp_fport = sin6->sin6_port;
1227 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
1228 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
1229 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
1231 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1233 INP_HASH_WUNLOCK(&V_tcbinfo);
1235 /* Compute window scaling to request. */
1236 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1237 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1238 tp->request_r_scale++;
1241 TCPSTAT_INC(tcps_connattempt);
1242 tcp_state_change(tp, TCPS_SYN_SENT);
1243 tp->iss = tcp_new_isn(tp);
1244 tcp_sendseqinit(tp);
1249 INP_HASH_WUNLOCK(&V_tcbinfo);
1255 * Export TCP internal state information via a struct tcp_info, based on the
1256 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1257 * (TCP state machine, etc). We export all information using FreeBSD-native
1258 * constants -- for example, the numeric values for tcpi_state will differ
1262 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1265 INP_WLOCK_ASSERT(tp->t_inpcb);
1266 bzero(ti, sizeof(*ti));
1268 ti->tcpi_state = tp->t_state;
1269 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1270 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1271 if (tp->t_flags & TF_SACK_PERMIT)
1272 ti->tcpi_options |= TCPI_OPT_SACK;
1273 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1274 ti->tcpi_options |= TCPI_OPT_WSCALE;
1275 ti->tcpi_snd_wscale = tp->snd_scale;
1276 ti->tcpi_rcv_wscale = tp->rcv_scale;
1279 ti->tcpi_rto = tp->t_rxtcur * tick;
1280 ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
1281 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1282 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1284 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1285 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1288 * FreeBSD-specific extension fields for tcp_info.
1290 ti->tcpi_rcv_space = tp->rcv_wnd;
1291 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1292 ti->tcpi_snd_wnd = tp->snd_wnd;
1293 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */
1294 ti->tcpi_snd_nxt = tp->snd_nxt;
1295 ti->tcpi_snd_mss = tp->t_maxseg;
1296 ti->tcpi_rcv_mss = tp->t_maxseg;
1297 if (tp->t_flags & TF_TOE)
1298 ti->tcpi_options |= TCPI_OPT_TOE;
1299 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1300 ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1301 ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1305 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1306 * socket option arguments. When it re-acquires the lock after the copy, it
1307 * has to revalidate that the connection is still valid for the socket
1310 #define INP_WLOCK_RECHECK(inp) do { \
1312 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1314 return (ECONNRESET); \
1316 tp = intotcpcb(inp); \
1320 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1322 int error, opt, optval;
1327 char buf[TCP_CA_NAME_MAX];
1328 struct cc_algo *algo;
1331 inp = sotoinpcb(so);
1332 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1334 if (sopt->sopt_level != IPPROTO_TCP) {
1336 if (inp->inp_vflag & INP_IPV6PROTO) {
1338 error = ip6_ctloutput(so, sopt);
1341 #if defined(INET6) && defined(INET)
1347 error = ip_ctloutput(so, sopt);
1352 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1354 return (ECONNRESET);
1357 switch (sopt->sopt_dir) {
1359 switch (sopt->sopt_name) {
1360 #ifdef TCP_SIGNATURE
1363 error = sooptcopyin(sopt, &optval, sizeof optval,
1368 INP_WLOCK_RECHECK(inp);
1370 tp->t_flags |= TF_SIGNATURE;
1372 tp->t_flags &= ~TF_SIGNATURE;
1373 goto unlock_and_done;
1374 #endif /* TCP_SIGNATURE */
1379 error = sooptcopyin(sopt, &optval, sizeof optval,
1384 INP_WLOCK_RECHECK(inp);
1385 switch (sopt->sopt_name) {
1393 opt = 0; /* dead code to fool gcc */
1400 tp->t_flags &= ~opt;
1403 if (tp->t_flags & TF_TOE) {
1404 tcp_offload_ctloutput(tp, sopt->sopt_dir,
1413 error = sooptcopyin(sopt, &optval, sizeof optval,
1418 INP_WLOCK_RECHECK(inp);
1420 tp->t_flags |= TF_NOPUSH;
1421 else if (tp->t_flags & TF_NOPUSH) {
1422 tp->t_flags &= ~TF_NOPUSH;
1423 if (TCPS_HAVEESTABLISHED(tp->t_state))
1424 error = tcp_output(tp);
1426 goto unlock_and_done;
1430 error = sooptcopyin(sopt, &optval, sizeof optval,
1435 INP_WLOCK_RECHECK(inp);
1436 if (optval > 0 && optval <= tp->t_maxseg &&
1437 optval + 40 >= V_tcp_minmss)
1438 tp->t_maxseg = optval;
1441 goto unlock_and_done;
1448 case TCP_CONGESTION:
1450 bzero(buf, sizeof(buf));
1451 error = sooptcopyin(sopt, &buf, sizeof(buf), 1);
1454 INP_WLOCK_RECHECK(inp);
1456 * Return EINVAL if we can't find the requested cc algo.
1460 STAILQ_FOREACH(algo, &cc_list, entries) {
1461 if (strncmp(buf, algo->name, TCP_CA_NAME_MAX)
1463 /* We've found the requested algo. */
1466 * We hold a write lock over the tcb
1467 * so it's safe to do these things
1468 * without ordering concerns.
1470 if (CC_ALGO(tp)->cb_destroy != NULL)
1471 CC_ALGO(tp)->cb_destroy(tp->ccv);
1474 * If something goes pear shaped
1475 * initialising the new algo,
1476 * fall back to newreno (which
1477 * does not require initialisation).
1479 if (algo->cb_init != NULL)
1480 if (algo->cb_init(tp->ccv) > 0) {
1481 CC_ALGO(tp) = &newreno_cc_algo;
1483 * The only reason init
1485 * because of malloc.
1489 break; /* Break the STAILQ_FOREACH. */
1493 goto unlock_and_done;
1499 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1503 if (ui > (UINT_MAX / hz)) {
1509 INP_WLOCK_RECHECK(inp);
1510 switch (sopt->sopt_name) {
1512 tp->t_keepidle = ui;
1514 * XXX: better check current remaining
1515 * timeout and "merge" it with new value.
1517 if ((tp->t_state > TCPS_LISTEN) &&
1518 (tp->t_state <= TCPS_CLOSING))
1519 tcp_timer_activate(tp, TT_KEEP,
1523 tp->t_keepintvl = ui;
1524 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1525 (TP_MAXIDLE(tp) > 0))
1526 tcp_timer_activate(tp, TT_2MSL,
1530 tp->t_keepinit = ui;
1531 if (tp->t_state == TCPS_SYN_RECEIVED ||
1532 tp->t_state == TCPS_SYN_SENT)
1533 tcp_timer_activate(tp, TT_KEEP,
1537 goto unlock_and_done;
1541 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
1545 INP_WLOCK_RECHECK(inp);
1547 if ((tp->t_state == TCPS_FIN_WAIT_2) &&
1548 (TP_MAXIDLE(tp) > 0))
1549 tcp_timer_activate(tp, TT_2MSL,
1551 goto unlock_and_done;
1555 error = ENOPROTOOPT;
1561 tp = intotcpcb(inp);
1562 switch (sopt->sopt_name) {
1563 #ifdef TCP_SIGNATURE
1565 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1567 error = sooptcopyout(sopt, &optval, sizeof optval);
1572 optval = tp->t_flags & TF_NODELAY;
1574 error = sooptcopyout(sopt, &optval, sizeof optval);
1577 optval = tp->t_maxseg;
1579 error = sooptcopyout(sopt, &optval, sizeof optval);
1582 optval = tp->t_flags & TF_NOOPT;
1584 error = sooptcopyout(sopt, &optval, sizeof optval);
1587 optval = tp->t_flags & TF_NOPUSH;
1589 error = sooptcopyout(sopt, &optval, sizeof optval);
1592 tcp_fill_info(tp, &ti);
1594 error = sooptcopyout(sopt, &ti, sizeof ti);
1596 case TCP_CONGESTION:
1597 bzero(buf, sizeof(buf));
1598 strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
1600 error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX);
1606 switch (sopt->sopt_name) {
1608 ui = tp->t_keepidle / hz;
1611 ui = tp->t_keepintvl / hz;
1614 ui = tp->t_keepinit / hz;
1621 error = sooptcopyout(sopt, &ui, sizeof(ui));
1625 error = ENOPROTOOPT;
1632 #undef INP_WLOCK_RECHECK
1635 * Attach TCP protocol to socket, allocating
1636 * internet protocol control block, tcp control block,
1637 * bufer space, and entering LISTEN state if to accept connections.
1640 tcp_attach(struct socket *so)
1646 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1647 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1651 so->so_rcv.sb_flags |= SB_AUTOSIZE;
1652 so->so_snd.sb_flags |= SB_AUTOSIZE;
1653 INP_INFO_WLOCK(&V_tcbinfo);
1654 error = in_pcballoc(so, &V_tcbinfo);
1656 INP_INFO_WUNLOCK(&V_tcbinfo);
1659 inp = sotoinpcb(so);
1661 if (inp->inp_vflag & INP_IPV6PROTO) {
1662 inp->inp_vflag |= INP_IPV6;
1663 inp->in6p_hops = -1; /* use kernel default */
1667 inp->inp_vflag |= INP_IPV4;
1668 tp = tcp_newtcpcb(inp);
1672 INP_INFO_WUNLOCK(&V_tcbinfo);
1675 tp->t_state = TCPS_CLOSED;
1677 INP_INFO_WUNLOCK(&V_tcbinfo);
1682 * Initiate (or continue) disconnect.
1683 * If embryonic state, just send reset (once).
1684 * If in ``let data drain'' option and linger null, just drop.
1685 * Otherwise (hard), mark socket disconnecting and drop
1686 * current input data; switch states based on user close, and
1687 * send segment to peer (with FIN).
1690 tcp_disconnect(struct tcpcb *tp)
1692 struct inpcb *inp = tp->t_inpcb;
1693 struct socket *so = inp->inp_socket;
1695 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1696 INP_WLOCK_ASSERT(inp);
1699 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1700 * socket is still open.
1702 if (tp->t_state < TCPS_ESTABLISHED) {
1705 ("tcp_disconnect: tcp_close() returned NULL"));
1706 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1707 tp = tcp_drop(tp, 0);
1709 ("tcp_disconnect: tcp_drop() returned NULL"));
1711 soisdisconnecting(so);
1712 sbflush(&so->so_rcv);
1714 if (!(inp->inp_flags & INP_DROPPED))
1720 * User issued close, and wish to trail through shutdown states:
1721 * if never received SYN, just forget it. If got a SYN from peer,
1722 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1723 * If already got a FIN from peer, then almost done; go to LAST_ACK
1724 * state. In all other cases, have already sent FIN to peer (e.g.
1725 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1726 * for peer to send FIN or not respond to keep-alives, etc.
1727 * We can let the user exit from the close as soon as the FIN is acked.
1730 tcp_usrclosed(struct tcpcb *tp)
1733 INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
1734 INP_WLOCK_ASSERT(tp->t_inpcb);
1736 switch (tp->t_state) {
1739 tcp_offload_listen_stop(tp);
1743 tcp_state_change(tp, TCPS_CLOSED);
1746 * tcp_close() should never return NULL here as the socket is
1750 ("tcp_usrclosed: tcp_close() returned NULL"));
1754 case TCPS_SYN_RECEIVED:
1755 tp->t_flags |= TF_NEEDFIN;
1758 case TCPS_ESTABLISHED:
1759 tcp_state_change(tp, TCPS_FIN_WAIT_1);
1762 case TCPS_CLOSE_WAIT:
1763 tcp_state_change(tp, TCPS_LAST_ACK);
1766 if (tp->t_state >= TCPS_FIN_WAIT_2) {
1767 soisdisconnected(tp->t_inpcb->inp_socket);
1768 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
1769 if (tp->t_state == TCPS_FIN_WAIT_2) {
1772 timeout = (tcp_fast_finwait2_recycle) ?
1773 tcp_finwait2_timeout : TP_MAXIDLE(tp);
1774 tcp_timer_activate(tp, TT_2MSL, timeout);
1781 db_print_indent(int indent)
1785 for (i = 0; i < indent; i++)
1790 db_print_tstate(int t_state)
1795 db_printf("TCPS_CLOSED");
1799 db_printf("TCPS_LISTEN");
1803 db_printf("TCPS_SYN_SENT");
1806 case TCPS_SYN_RECEIVED:
1807 db_printf("TCPS_SYN_RECEIVED");
1810 case TCPS_ESTABLISHED:
1811 db_printf("TCPS_ESTABLISHED");
1814 case TCPS_CLOSE_WAIT:
1815 db_printf("TCPS_CLOSE_WAIT");
1818 case TCPS_FIN_WAIT_1:
1819 db_printf("TCPS_FIN_WAIT_1");
1823 db_printf("TCPS_CLOSING");
1827 db_printf("TCPS_LAST_ACK");
1830 case TCPS_FIN_WAIT_2:
1831 db_printf("TCPS_FIN_WAIT_2");
1834 case TCPS_TIME_WAIT:
1835 db_printf("TCPS_TIME_WAIT");
1839 db_printf("unknown");
1845 db_print_tflags(u_int t_flags)
1850 if (t_flags & TF_ACKNOW) {
1851 db_printf("%sTF_ACKNOW", comma ? ", " : "");
1854 if (t_flags & TF_DELACK) {
1855 db_printf("%sTF_DELACK", comma ? ", " : "");
1858 if (t_flags & TF_NODELAY) {
1859 db_printf("%sTF_NODELAY", comma ? ", " : "");
1862 if (t_flags & TF_NOOPT) {
1863 db_printf("%sTF_NOOPT", comma ? ", " : "");
1866 if (t_flags & TF_SENTFIN) {
1867 db_printf("%sTF_SENTFIN", comma ? ", " : "");
1870 if (t_flags & TF_REQ_SCALE) {
1871 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1874 if (t_flags & TF_RCVD_SCALE) {
1875 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1878 if (t_flags & TF_REQ_TSTMP) {
1879 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1882 if (t_flags & TF_RCVD_TSTMP) {
1883 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1886 if (t_flags & TF_SACK_PERMIT) {
1887 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1890 if (t_flags & TF_NEEDSYN) {
1891 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1894 if (t_flags & TF_NEEDFIN) {
1895 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1898 if (t_flags & TF_NOPUSH) {
1899 db_printf("%sTF_NOPUSH", comma ? ", " : "");
1902 if (t_flags & TF_MORETOCOME) {
1903 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1906 if (t_flags & TF_LQ_OVERFLOW) {
1907 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1910 if (t_flags & TF_LASTIDLE) {
1911 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1914 if (t_flags & TF_RXWIN0SENT) {
1915 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1918 if (t_flags & TF_FASTRECOVERY) {
1919 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1922 if (t_flags & TF_CONGRECOVERY) {
1923 db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
1926 if (t_flags & TF_WASFRECOVERY) {
1927 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1930 if (t_flags & TF_SIGNATURE) {
1931 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1934 if (t_flags & TF_FORCEDATA) {
1935 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1938 if (t_flags & TF_TSO) {
1939 db_printf("%sTF_TSO", comma ? ", " : "");
1942 if (t_flags & TF_ECN_PERMIT) {
1943 db_printf("%sTF_ECN_PERMIT", comma ? ", " : "");
1949 db_print_toobflags(char t_oobflags)
1954 if (t_oobflags & TCPOOB_HAVEDATA) {
1955 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1958 if (t_oobflags & TCPOOB_HADDATA) {
1959 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1965 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1968 db_print_indent(indent);
1969 db_printf("%s at %p\n", name, tp);
1973 db_print_indent(indent);
1974 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
1975 LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
1977 db_print_indent(indent);
1978 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
1979 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
1981 db_print_indent(indent);
1982 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
1983 &tp->t_timers->tt_delack, tp->t_inpcb);
1985 db_print_indent(indent);
1986 db_printf("t_state: %d (", tp->t_state);
1987 db_print_tstate(tp->t_state);
1990 db_print_indent(indent);
1991 db_printf("t_flags: 0x%x (", tp->t_flags);
1992 db_print_tflags(tp->t_flags);
1995 db_print_indent(indent);
1996 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
1997 tp->snd_una, tp->snd_max, tp->snd_nxt);
1999 db_print_indent(indent);
2000 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
2001 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2003 db_print_indent(indent);
2004 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
2005 tp->iss, tp->irs, tp->rcv_nxt);
2007 db_print_indent(indent);
2008 db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n",
2009 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2011 db_print_indent(indent);
2012 db_printf("snd_wnd: %lu snd_cwnd: %lu\n",
2013 tp->snd_wnd, tp->snd_cwnd);
2015 db_print_indent(indent);
2016 db_printf("snd_ssthresh: %lu snd_recover: "
2017 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2019 db_print_indent(indent);
2020 db_printf("t_maxopd: %u t_rcvtime: %u t_startime: %u\n",
2021 tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
2023 db_print_indent(indent);
2024 db_printf("t_rttime: %u t_rtsq: 0x%08x\n",
2025 tp->t_rtttime, tp->t_rtseq);
2027 db_print_indent(indent);
2028 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n",
2029 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2031 db_print_indent(indent);
2032 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
2033 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2036 db_print_indent(indent);
2037 db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n",
2038 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2040 db_print_indent(indent);
2041 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2042 db_print_toobflags(tp->t_oobflags);
2043 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
2045 db_print_indent(indent);
2046 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
2047 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2049 db_print_indent(indent);
2050 db_printf("ts_recent: %u ts_recent_age: %u\n",
2051 tp->ts_recent, tp->ts_recent_age);
2053 db_print_indent(indent);
2054 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
2055 "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2057 db_print_indent(indent);
2058 db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x "
2059 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2060 tp->snd_recover_prev, tp->t_badrxtwin);
2062 db_print_indent(indent);
2063 db_printf("snd_numholes: %d snd_holes first: %p\n",
2064 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2066 db_print_indent(indent);
2067 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
2068 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
2070 /* Skip sackblks, sackhint. */
2072 db_print_indent(indent);
2073 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
2074 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2077 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2082 db_printf("usage: show tcpcb <addr>\n");
2085 tp = (struct tcpcb *)addr;
2087 db_print_tcpcb(tp, "tcpcb", 0);