xref: /freebsd/usr.bin/netstat/inet.c (revision fe5a6266dde08cece15025c75a80a45d977c57c2)
165475bc8SDavid E. O'Brien /*-
205ddff6eSPeter Wemm  * Copyright (c) 1983, 1988, 1993, 1995
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
149b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
159b50d902SRodney W. Grimes  *    without specific prior written permission.
169b50d902SRodney W. Grimes  *
179b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
189b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
219b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279b50d902SRodney W. Grimes  * SUCH DAMAGE.
289b50d902SRodney W. Grimes  */
299b50d902SRodney W. Grimes 
309b50d902SRodney W. Grimes #include <sys/param.h>
31d8d89152SDavid Greenman #include <sys/queue.h>
32feda1a43SJohn Baldwin #include <sys/domain.h>
33*fe5a6266SGleb Smirnoff #define	_WANT_PROTOSW
34f35a2092SMaksim Yevmenkin #include <sys/protosw.h>
359b50d902SRodney W. Grimes #include <sys/socket.h>
360e229f34SGleb Smirnoff #define	_WANT_SOCKET
379b50d902SRodney W. Grimes #include <sys/socketvar.h>
384e00c309SGarrett Wollman #include <sys/sysctl.h>
399b50d902SRodney W. Grimes 
409b50d902SRodney W. Grimes #include <net/route.h>
4154fc657dSGeorge V. Neville-Neil #include <net/if_arp.h>
429b50d902SRodney W. Grimes #include <netinet/in.h>
439b50d902SRodney W. Grimes #include <netinet/in_systm.h>
449b50d902SRodney W. Grimes #include <netinet/ip.h>
45a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
46cfa1ca9dSYoshinobu Inoue #ifdef INET6
47cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
48cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
499b50d902SRodney W. Grimes #include <netinet/in_pcb.h>
509b50d902SRodney W. Grimes #include <netinet/ip_icmp.h>
519b50d902SRodney W. Grimes #include <netinet/icmp_var.h>
529b50d902SRodney W. Grimes #include <netinet/igmp_var.h>
532b1c7217SGleb Smirnoff #include <netinet/ip_divert.h>
549b50d902SRodney W. Grimes #include <netinet/ip_var.h>
55c7b9b5bbSJeffrey Hsu #include <netinet/pim_var.h>
569b50d902SRodney W. Grimes #include <netinet/tcp.h>
579b50d902SRodney W. Grimes #include <netinet/tcpip.h>
589b50d902SRodney W. Grimes #include <netinet/tcp_seq.h>
599b50d902SRodney W. Grimes #define	TCPSTATES
609b50d902SRodney W. Grimes #include <netinet/tcp_fsm.h>
619b50d902SRodney W. Grimes #include <netinet/tcp_timer.h>
629b50d902SRodney W. Grimes #include <netinet/tcp_var.h>
639b50d902SRodney W. Grimes #include <netinet/udp.h>
649b50d902SRodney W. Grimes #include <netinet/udp_var.h>
659b50d902SRodney W. Grimes 
669b50d902SRodney W. Grimes #include <arpa/inet.h>
674f81ef50SGarrett Wollman #include <errno.h>
68d121b556SBrian Somers #include <libutil.h>
699b50d902SRodney W. Grimes #include <netdb.h>
707b95a1ebSYaroslav Tykhiy #include <stdint.h>
719b50d902SRodney W. Grimes #include <stdio.h>
724f81ef50SGarrett Wollman #include <stdlib.h>
73ade9ccfeSMarcel Moolenaar #include <stdbool.h>
749b50d902SRodney W. Grimes #include <string.h>
759b50d902SRodney W. Grimes #include <unistd.h>
76ade9ccfeSMarcel Moolenaar #include <libxo/xo.h>
779b50d902SRodney W. Grimes #include "netstat.h"
78dbfd8708SGleb Smirnoff #include "nl_defs.h"
799b50d902SRodney W. Grimes 
800e5e35e3SRichard Scheffenegger #define max(a, b) (((a) > (b)) ? (a) : (b))
810e5e35e3SRichard Scheffenegger 
829784899dSBjoern A. Zeeb #ifdef INET
839784899dSBjoern A. Zeeb static void inetprint(const char *, struct in_addr *, int, const char *, int,
84046aad39SHajimu UMEMOTO     const int);
859784899dSBjoern A. Zeeb #endif
86cfa1ca9dSYoshinobu Inoue #ifdef INET6
87aa0a1e58SJeff Roberson static int udp_done, tcp_done, sdp_done;
88cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
899b50d902SRodney W. Grimes 
90feda1a43SJohn Baldwin static int
pcblist_sysctl(int proto,const char * name,char ** bufp)91cc65eb4eSGleb Smirnoff pcblist_sysctl(int proto, const char *name, char **bufp)
92feda1a43SJohn Baldwin {
93feda1a43SJohn Baldwin 	const char *mibvar;
94feda1a43SJohn Baldwin 	char *buf;
95feda1a43SJohn Baldwin 	size_t len;
96feda1a43SJohn Baldwin 
97feda1a43SJohn Baldwin 	switch (proto) {
98feda1a43SJohn Baldwin 	case IPPROTO_TCP:
99feda1a43SJohn Baldwin 		mibvar = "net.inet.tcp.pcblist";
100feda1a43SJohn Baldwin 		break;
101feda1a43SJohn Baldwin 	case IPPROTO_UDP:
102feda1a43SJohn Baldwin 		mibvar = "net.inet.udp.pcblist";
103feda1a43SJohn Baldwin 		break;
104feda1a43SJohn Baldwin 	default:
105feda1a43SJohn Baldwin 		mibvar = "net.inet.raw.pcblist";
106feda1a43SJohn Baldwin 		break;
107feda1a43SJohn Baldwin 	}
108aa0a1e58SJeff Roberson 	if (strncmp(name, "sdp", 3) == 0)
109aa0a1e58SJeff Roberson 		mibvar = "net.inet.sdp.pcblist";
1108624f434SGleb Smirnoff 	else if (strncmp(name, "divert", 6) == 0)
1118624f434SGleb Smirnoff 		mibvar = "net.inet.divert.pcblist";
112feda1a43SJohn Baldwin 	len = 0;
113feda1a43SJohn Baldwin 	if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
114feda1a43SJohn Baldwin 		if (errno != ENOENT)
115ade9ccfeSMarcel Moolenaar 			xo_warn("sysctl: %s", mibvar);
116feda1a43SJohn Baldwin 		return (0);
117feda1a43SJohn Baldwin 	}
118ef1cb629SMarcelo Araujo 	if ((buf = malloc(len)) == NULL) {
119ade9ccfeSMarcel Moolenaar 		xo_warnx("malloc %lu bytes", (u_long)len);
120feda1a43SJohn Baldwin 		return (0);
121feda1a43SJohn Baldwin 	}
122feda1a43SJohn Baldwin 	if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
123ade9ccfeSMarcel Moolenaar 		xo_warn("sysctl: %s", mibvar);
124feda1a43SJohn Baldwin 		free(buf);
125feda1a43SJohn Baldwin 		return (0);
126feda1a43SJohn Baldwin 	}
127feda1a43SJohn Baldwin 	*bufp = buf;
128feda1a43SJohn Baldwin 	return (1);
129feda1a43SJohn Baldwin }
130feda1a43SJohn Baldwin 
131feda1a43SJohn Baldwin /*
132feda1a43SJohn Baldwin  * Copied directly from uipc_socket2.c.  We leave out some fields that are in
133feda1a43SJohn Baldwin  * nested structures that aren't used to avoid extra work.
134feda1a43SJohn Baldwin  */
135feda1a43SJohn Baldwin static void
sbtoxsockbuf(struct sockbuf * sb,struct xsockbuf * xsb)136feda1a43SJohn Baldwin sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
137feda1a43SJohn Baldwin {
1380f9d0a73SGleb Smirnoff 	xsb->sb_cc = sb->sb_ccc;
139feda1a43SJohn Baldwin 	xsb->sb_hiwat = sb->sb_hiwat;
140feda1a43SJohn Baldwin 	xsb->sb_mbcnt = sb->sb_mbcnt;
141feda1a43SJohn Baldwin 	xsb->sb_mbmax = sb->sb_mbmax;
142feda1a43SJohn Baldwin 	xsb->sb_lowat = sb->sb_lowat;
143feda1a43SJohn Baldwin 	xsb->sb_flags = sb->sb_flags;
144feda1a43SJohn Baldwin 	xsb->sb_timeo = sb->sb_timeo;
145feda1a43SJohn Baldwin }
146feda1a43SJohn Baldwin 
147feda1a43SJohn Baldwin int
sotoxsocket(struct socket * so,struct xsocket * xso)148feda1a43SJohn Baldwin sotoxsocket(struct socket *so, struct xsocket *xso)
149feda1a43SJohn Baldwin {
150feda1a43SJohn Baldwin 	struct protosw proto;
151feda1a43SJohn Baldwin 	struct domain domain;
152feda1a43SJohn Baldwin 
153feda1a43SJohn Baldwin 	bzero(xso, sizeof *xso);
154feda1a43SJohn Baldwin 	xso->xso_len = sizeof *xso;
1553a20f06aSBrooks Davis 	xso->xso_so = (uintptr_t)so;
156feda1a43SJohn Baldwin 	xso->so_type = so->so_type;
157feda1a43SJohn Baldwin 	xso->so_options = so->so_options;
158feda1a43SJohn Baldwin 	xso->so_linger = so->so_linger;
159feda1a43SJohn Baldwin 	xso->so_state = so->so_state;
1603a20f06aSBrooks Davis 	xso->so_pcb = (uintptr_t)so->so_pcb;
161feda1a43SJohn Baldwin 	if (kread((uintptr_t)so->so_proto, &proto, sizeof(proto)) != 0)
162feda1a43SJohn Baldwin 		return (-1);
163feda1a43SJohn Baldwin 	xso->xso_protocol = proto.pr_protocol;
164feda1a43SJohn Baldwin 	if (kread((uintptr_t)proto.pr_domain, &domain, sizeof(domain)) != 0)
165feda1a43SJohn Baldwin 		return (-1);
166feda1a43SJohn Baldwin 	xso->xso_family = domain.dom_family;
167feda1a43SJohn Baldwin 	xso->so_timeo = so->so_timeo;
168feda1a43SJohn Baldwin 	xso->so_error = so->so_error;
1690e229f34SGleb Smirnoff 	if ((so->so_options & SO_ACCEPTCONN) != 0) {
170779f106aSGleb Smirnoff 		xso->so_qlen = so->sol_qlen;
171779f106aSGleb Smirnoff 		xso->so_incqlen = so->sol_incqlen;
172779f106aSGleb Smirnoff 		xso->so_qlimit = so->sol_qlimit;
173779f106aSGleb Smirnoff 	} else {
174feda1a43SJohn Baldwin 		sbtoxsockbuf(&so->so_snd, &xso->so_snd);
175feda1a43SJohn Baldwin 		sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
176779f106aSGleb Smirnoff 		xso->so_oobmark = so->so_oobmark;
177779f106aSGleb Smirnoff 	}
178feda1a43SJohn Baldwin 	return (0);
179feda1a43SJohn Baldwin }
180feda1a43SJohn Baldwin 
1819b50d902SRodney W. Grimes /*
1829b50d902SRodney W. Grimes  * Print a summary of connections related to an Internet
1839b50d902SRodney W. Grimes  * protocol.  For TCP, also give state of connection.
1849b50d902SRodney W. Grimes  * Listening processes (aflag) are suppressed unless the
1859b50d902SRodney W. Grimes  * -a (all) flag is specified.
1869b50d902SRodney W. Grimes  */
1879b50d902SRodney W. Grimes void
protopr(u_long off,const char * name,int af1,int proto)188feda1a43SJohn Baldwin protopr(u_long off, const char *name, int af1, int proto)
1899b50d902SRodney W. Grimes {
1909b50d902SRodney W. Grimes 	static int first = 1;
191cc65eb4eSGleb Smirnoff 	int istcp;
1924f81ef50SGarrett Wollman 	char *buf;
193feda1a43SJohn Baldwin 	const char *vchar;
194cc65eb4eSGleb Smirnoff 	struct xtcpcb *tp;
195cc65eb4eSGleb Smirnoff 	struct xinpcb *inp;
1964f81ef50SGarrett Wollman 	struct xinpgen *xig, *oxig;
1974f81ef50SGarrett Wollman 	struct xsocket *so;
1980e5e35e3SRichard Scheffenegger 	int fnamelen, cnamelen;
1999b50d902SRodney W. Grimes 
2004f81ef50SGarrett Wollman 	istcp = 0;
2014f81ef50SGarrett Wollman 	switch (proto) {
2024f81ef50SGarrett Wollman 	case IPPROTO_TCP:
203cfa1ca9dSYoshinobu Inoue #ifdef INET6
204aa0a1e58SJeff Roberson 		if (strncmp(name, "sdp", 3) != 0) {
205cfa1ca9dSYoshinobu Inoue 			if (tcp_done != 0)
206cfa1ca9dSYoshinobu Inoue 				return;
207cfa1ca9dSYoshinobu Inoue 			else
208cfa1ca9dSYoshinobu Inoue 				tcp_done = 1;
209aa0a1e58SJeff Roberson 		} else {
210aa0a1e58SJeff Roberson 			if (sdp_done != 0)
211aa0a1e58SJeff Roberson 				return;
212aa0a1e58SJeff Roberson 			else
213aa0a1e58SJeff Roberson 				sdp_done = 1;
214aa0a1e58SJeff Roberson 		}
215cfa1ca9dSYoshinobu Inoue #endif
2164f81ef50SGarrett Wollman 		istcp = 1;
2174f81ef50SGarrett Wollman 		break;
2184f81ef50SGarrett Wollman 	case IPPROTO_UDP:
219cfa1ca9dSYoshinobu Inoue #ifdef INET6
220cfa1ca9dSYoshinobu Inoue 		if (udp_done != 0)
221cfa1ca9dSYoshinobu Inoue 			return;
222cfa1ca9dSYoshinobu Inoue 		else
223cfa1ca9dSYoshinobu Inoue 			udp_done = 1;
224cfa1ca9dSYoshinobu Inoue #endif
2254f81ef50SGarrett Wollman 		break;
2264f81ef50SGarrett Wollman 	}
227cc65eb4eSGleb Smirnoff 
228cc65eb4eSGleb Smirnoff 	if (!pcblist_sysctl(proto, name, &buf))
2299b50d902SRodney W. Grimes 		return;
2304f81ef50SGarrett Wollman 
2318e7c2e14SRichard Scheffenegger 	if (istcp && (cflag || Cflag)) {
2320e5e35e3SRichard Scheffenegger 		fnamelen = strlen("Stack");
2330e5e35e3SRichard Scheffenegger 		cnamelen = strlen("CC");
2340e5e35e3SRichard Scheffenegger 		oxig = xig = (struct xinpgen *)buf;
2350e5e35e3SRichard Scheffenegger 		for (xig = (struct xinpgen*)((char *)xig + xig->xig_len);
2360e5e35e3SRichard Scheffenegger 		    xig->xig_len > sizeof(struct xinpgen);
2370e5e35e3SRichard Scheffenegger 		    xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
2380e5e35e3SRichard Scheffenegger 			tp = (struct xtcpcb *)xig;
2390e5e35e3SRichard Scheffenegger 			inp = &tp->xt_inp;
2400e5e35e3SRichard Scheffenegger 			if (inp->inp_gencnt > oxig->xig_gen)
2410e5e35e3SRichard Scheffenegger 				continue;
2428e7c2e14SRichard Scheffenegger 			so = &inp->xi_socket;
2438e7c2e14SRichard Scheffenegger 			if (so->xso_protocol != proto)
2448e7c2e14SRichard Scheffenegger 				continue;
2450e5e35e3SRichard Scheffenegger 			fnamelen = max(fnamelen, (int)strlen(tp->xt_stack));
2460e5e35e3SRichard Scheffenegger 			cnamelen = max(cnamelen, (int)strlen(tp->xt_cc));
2470e5e35e3SRichard Scheffenegger 		}
2480e5e35e3SRichard Scheffenegger 	}
2490e5e35e3SRichard Scheffenegger 
2504f81ef50SGarrett Wollman 	oxig = xig = (struct xinpgen *)buf;
2514f81ef50SGarrett Wollman 	for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
2524f81ef50SGarrett Wollman 	    xig->xig_len > sizeof(struct xinpgen);
2534f81ef50SGarrett Wollman 	    xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
2549b50d902SRodney W. Grimes 		if (istcp) {
255cc65eb4eSGleb Smirnoff 			tp = (struct xtcpcb *)xig;
256cc65eb4eSGleb Smirnoff 			inp = &tp->xt_inp;
2574f81ef50SGarrett Wollman 		} else {
258cc65eb4eSGleb Smirnoff 			inp = (struct xinpcb *)xig;
2599b50d902SRodney W. Grimes 		}
260cc65eb4eSGleb Smirnoff 		so = &inp->xi_socket;
2614f81ef50SGarrett Wollman 
2624f81ef50SGarrett Wollman 		/* Ignore sockets for protocols other than the desired one. */
2638624f434SGleb Smirnoff 		if (proto != 0 && so->xso_protocol != proto)
2644f81ef50SGarrett Wollman 			continue;
2654f81ef50SGarrett Wollman 
2664f81ef50SGarrett Wollman 		/* Ignore PCBs which were freed during copyout. */
2674f81ef50SGarrett Wollman 		if (inp->inp_gencnt > oxig->xig_gen)
2684f81ef50SGarrett Wollman 			continue;
2694f81ef50SGarrett Wollman 
270a01e3379SDavid Malone 		if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
271cfa1ca9dSYoshinobu Inoue #ifdef INET6
272a01e3379SDavid Malone 		    || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
273cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
274a01e3379SDavid Malone 		    || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
275cfa1ca9dSYoshinobu Inoue #ifdef INET6
2763feeb332SDavid E. O'Brien 					  && (inp->inp_vflag & INP_IPV6) == 0
277cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
278cfa1ca9dSYoshinobu Inoue 			))
279cfa1ca9dSYoshinobu Inoue 		    )
280cfa1ca9dSYoshinobu Inoue 			continue;
281cfa1ca9dSYoshinobu Inoue 		if (!aflag &&
282cfa1ca9dSYoshinobu Inoue 		    (
2832b286cedSBruce M Simpson 		     (istcp && tp->t_state == TCPS_LISTEN)
2842b286cedSBruce M Simpson 		     || (af1 == AF_INET &&
285bd27c71cSMike Karels 		      inp->inp_laddr.s_addr == INADDR_ANY)
286cfa1ca9dSYoshinobu Inoue #ifdef INET6
287a01e3379SDavid Malone 		     || (af1 == AF_INET6 &&
288cfa1ca9dSYoshinobu Inoue 			 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
289cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
290a01e3379SDavid Malone 		     || (af1 == AF_UNSPEC &&
291cfa1ca9dSYoshinobu Inoue 			 (((inp->inp_vflag & INP_IPV4) != 0 &&
292bd27c71cSMike Karels 			   inp->inp_laddr.s_addr == INADDR_ANY)
293cfa1ca9dSYoshinobu Inoue #ifdef INET6
294cfa1ca9dSYoshinobu Inoue 			  || ((inp->inp_vflag & INP_IPV6) != 0 &&
295cfa1ca9dSYoshinobu Inoue 			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
296cfa1ca9dSYoshinobu Inoue #endif
297cfa1ca9dSYoshinobu Inoue 			  ))
298cfa1ca9dSYoshinobu Inoue 		     ))
2994f81ef50SGarrett Wollman 			continue;
3004f81ef50SGarrett Wollman 
3019b50d902SRodney W. Grimes 		if (first) {
302ac55add0SGuido van Rooij 			if (!Lflag) {
303ade9ccfeSMarcel Moolenaar 				xo_emit("Active Internet connections");
3049b50d902SRodney W. Grimes 				if (aflag)
305ade9ccfeSMarcel Moolenaar 					xo_emit(" (including servers)");
306ac55add0SGuido van Rooij 			} else
307ade9ccfeSMarcel Moolenaar 				xo_emit(
308ac55add0SGuido van Rooij 	"Current listen queue sizes (qlen/incqlen/maxqlen)");
309ade9ccfeSMarcel Moolenaar 			xo_emit("\n");
3109b50d902SRodney W. Grimes 			if (Aflag)
311ade9ccfeSMarcel Moolenaar 				xo_emit("{T:/%-*s} ", 2 * (int)sizeof(void *),
312ade9ccfeSMarcel Moolenaar 				    "Tcpcb");
313ac55add0SGuido van Rooij 			if (Lflag)
314ade9ccfeSMarcel Moolenaar 				xo_emit((Aflag && !Wflag) ?
315db2627f4SMichael Tuexen 				    "{T:/%-5.5s} {T:/%-32.32s} {T:/%-18.18s}" :
316046aad39SHajimu UMEMOTO 				    ((!Wflag || af1 == AF_INET) ?
317db2627f4SMichael Tuexen 				    "{T:/%-5.5s} {T:/%-32.32s} {T:/%-22.22s}" :
318db2627f4SMichael Tuexen 				    "{T:/%-5.5s} {T:/%-32.32s} {T:/%-45.45s}"),
319afc74015SRuslan Ermilov 				    "Proto", "Listen", "Local Address");
320afc74015SRuslan Ermilov 			else if (Tflag)
321ade9ccfeSMarcel Moolenaar 				xo_emit((Aflag && !Wflag) ?
322ade9ccfeSMarcel Moolenaar     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%s}" :
323046aad39SHajimu UMEMOTO 				    ((!Wflag || af1 == AF_INET) ?
324046aad39SHajimu UMEMOTO     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%s}" :
325046aad39SHajimu UMEMOTO     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%s}"),
326f5d34df5SGeorge V. Neville-Neil 				    "Proto", "Rexmit", "OOORcv", "0-win",
32749f287f8SGeorge V. Neville-Neil 				    "Local Address", "Foreign Address");
328afc74015SRuslan Ermilov 			else {
329ade9ccfeSMarcel Moolenaar 				xo_emit((Aflag && !Wflag) ?
330ade9ccfeSMarcel Moolenaar     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%-18.18s}" :
331046aad39SHajimu UMEMOTO 				    ((!Wflag || af1 == AF_INET) ?
332046aad39SHajimu UMEMOTO     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%-22.22s}" :
333046aad39SHajimu UMEMOTO     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%-45.45s}"),
334afc74015SRuslan Ermilov 				    "Proto", "Recv-Q", "Send-Q",
335afc74015SRuslan Ermilov 				    "Local Address", "Foreign Address");
33685b0f0f3SAdrian Chadd 				if (!xflag && !Rflag)
3372529f56eSJonathan T. Looney 					xo_emit(" {T:/%-11.11s}", "(state)");
338afc74015SRuslan Ermilov 			}
339b8614722SMike Silbersack 			if (xflag) {
340d59bc188SGleb Smirnoff 				xo_emit("{T:/%-6.6s} {T:/%-6.6s} "
341ade9ccfeSMarcel Moolenaar 				    "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
342ade9ccfeSMarcel Moolenaar 				    "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s}",
343ade9ccfeSMarcel Moolenaar 				    "R-HIWA", "S-HIWA", "R-LOWA", "S-LOWA",
344ade9ccfeSMarcel Moolenaar 				    "R-BCNT", "S-BCNT", "R-BMAX", "S-BMAX");
345ade9ccfeSMarcel Moolenaar 				xo_emit(" {T:/%7.7s} {T:/%7.7s} {T:/%7.7s} "
346ade9ccfeSMarcel Moolenaar 				    "{T:/%7.7s} {T:/%7.7s} {T:/%7.7s}",
347ade9ccfeSMarcel Moolenaar 				    "rexmt", "persist", "keep", "2msl",
348ade9ccfeSMarcel Moolenaar 				    "delack", "rcvtime");
34985b0f0f3SAdrian Chadd 			} else if (Rflag) {
350ade9ccfeSMarcel Moolenaar 				xo_emit("  {T:/%8.8s} {T:/%5.5s}",
35185b0f0f3SAdrian Chadd 				    "flowid", "ftype");
35294f138feSGeorge V. Neville-Neil 			}
3530e5e35e3SRichard Scheffenegger 			if (cflag) {
3540e5e35e3SRichard Scheffenegger 				xo_emit(" {T:/%-*.*s}",
3550e5e35e3SRichard Scheffenegger 					fnamelen, fnamelen, "Stack");
3560e5e35e3SRichard Scheffenegger 			}
357b98a21f6SMichael Tuexen 			if (Cflag)
3580e5e35e3SRichard Scheffenegger 				xo_emit(" {T:/%-*.*s} {T:/%10.10s}"
3590e5e35e3SRichard Scheffenegger 					" {T:/%10.10s} {T:/%5.5s}"
3600e5e35e3SRichard Scheffenegger 					" {T:/%3.3s}", cnamelen,
3610e5e35e3SRichard Scheffenegger 					cnamelen, "CC",
3620e5e35e3SRichard Scheffenegger 					"cwin",
3630e5e35e3SRichard Scheffenegger 					"ssthresh",
3640e5e35e3SRichard Scheffenegger 					"MSS",
3650e5e35e3SRichard Scheffenegger 					"ECN");
3662529f56eSJonathan T. Looney 			if (Pflag)
3672529f56eSJonathan T. Looney 				xo_emit(" {T:/%s}", "Log ID");
368ade9ccfeSMarcel Moolenaar 			xo_emit("\n");
3699b50d902SRodney W. Grimes 			first = 0;
3709b50d902SRodney W. Grimes 		}
371fb5d0fbdSRuslan Ermilov 		if (Lflag && so->so_qlimit == 0)
372fb5d0fbdSRuslan Ermilov 			continue;
373ade9ccfeSMarcel Moolenaar 		xo_open_instance("socket");
3748e813d07SGleb Smirnoff 		if (Aflag)
3758e813d07SGleb Smirnoff 			xo_emit("{q:address/%*lx} ", 2 * (int)sizeof(void *),
376ade9ccfeSMarcel Moolenaar 			    (u_long)so->so_pcb);
377cfa1ca9dSYoshinobu Inoue #ifdef INET6
378fc60ab7bSYoshinobu Inoue 		if ((inp->inp_vflag & INP_IPV6) != 0)
3793feeb332SDavid E. O'Brien 			vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
3803feeb332SDavid E. O'Brien 			    "46" : "6";
381fc60ab7bSYoshinobu Inoue 		else
382cfa1ca9dSYoshinobu Inoue #endif
3833feeb332SDavid E. O'Brien 		vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
3843feeb332SDavid E. O'Brien 		    "4" : "";
38509fe6320SNavdeep Parhar 		if (istcp && (tp->t_flags & TF_TOE) != 0)
386ade9ccfeSMarcel Moolenaar 			xo_emit("{:protocol/%-3.3s%-2.2s/%s%s} ", "toe", vchar);
38709fe6320SNavdeep Parhar 		else
388ade9ccfeSMarcel Moolenaar 			xo_emit("{:protocol/%-3.3s%-2.2s/%s%s} ", name, vchar);
389fb5d0fbdSRuslan Ermilov 		if (Lflag) {
3907325dfbbSAlfred Perlstein 			char buf1[33];
391fc60ab7bSYoshinobu Inoue 
3927325dfbbSAlfred Perlstein 			snprintf(buf1, sizeof buf1, "%u/%u/%u", so->so_qlen,
393fb5d0fbdSRuslan Ermilov 			    so->so_incqlen, so->so_qlimit);
3947325dfbbSAlfred Perlstein 			xo_emit("{:listen-queue-sizes/%-32.32s} ", buf1);
395f5d34df5SGeorge V. Neville-Neil 		} else if (Tflag) {
396f5d34df5SGeorge V. Neville-Neil 			if (istcp)
397ade9ccfeSMarcel Moolenaar 				xo_emit("{:sent-retransmit-packets/%6u} "
398ade9ccfeSMarcel Moolenaar 				    "{:received-out-of-order-packets/%6u} "
399ade9ccfeSMarcel Moolenaar 				    "{:sent-zero-window/%6u} ",
400ade9ccfeSMarcel Moolenaar 				    tp->t_sndrexmitpack, tp->t_rcvoopack,
401ade9ccfeSMarcel Moolenaar 				    tp->t_sndzerowin);
4020eaa116eSHajimu UMEMOTO 			else
4030eaa116eSHajimu UMEMOTO 				xo_emit("{P:/%21s}", "");
404fb5d0fbdSRuslan Ermilov 		} else {
405ade9ccfeSMarcel Moolenaar 			xo_emit("{:receive-bytes-waiting/%6u} "
406ade9ccfeSMarcel Moolenaar 			    "{:send-bytes-waiting/%6u} ",
4070f9d0a73SGleb Smirnoff 			    so->so_rcv.sb_cc, so->so_snd.sb_cc);
408fc60ab7bSYoshinobu Inoue 		}
40965ea0024SAssar Westerlund 		if (numeric_port) {
4109784899dSBjoern A. Zeeb #ifdef INET
411cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
412ade9ccfeSMarcel Moolenaar 				inetprint("local", &inp->inp_laddr,
413046aad39SHajimu UMEMOTO 				    (int)inp->inp_lport, name, 1, af1);
414ac55add0SGuido van Rooij 				if (!Lflag)
415ade9ccfeSMarcel Moolenaar 					inetprint("remote", &inp->inp_faddr,
416046aad39SHajimu UMEMOTO 					    (int)inp->inp_fport, name, 1, af1);
417cfa1ca9dSYoshinobu Inoue 			}
4189784899dSBjoern A. Zeeb #endif
4199784899dSBjoern A. Zeeb #if defined(INET) && defined(INET6)
4209784899dSBjoern A. Zeeb 			else
4219784899dSBjoern A. Zeeb #endif
422cfa1ca9dSYoshinobu Inoue #ifdef INET6
4239784899dSBjoern A. Zeeb 			if (inp->inp_vflag & INP_IPV6) {
424ade9ccfeSMarcel Moolenaar 				inet6print("local", &inp->in6p_laddr,
425cfa1ca9dSYoshinobu Inoue 				    (int)inp->inp_lport, name, 1);
426ac55add0SGuido van Rooij 				if (!Lflag)
427ade9ccfeSMarcel Moolenaar 					inet6print("remote", &inp->in6p_faddr,
428cfa1ca9dSYoshinobu Inoue 					    (int)inp->inp_fport, name, 1);
429cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
430cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
4314f81ef50SGarrett Wollman 		} else if (inp->inp_flags & INP_ANONPORT) {
4329784899dSBjoern A. Zeeb #ifdef INET
433cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
434ade9ccfeSMarcel Moolenaar 				inetprint("local", &inp->inp_laddr,
435046aad39SHajimu UMEMOTO 				    (int)inp->inp_lport, name, 1, af1);
436ac55add0SGuido van Rooij 				if (!Lflag)
437ade9ccfeSMarcel Moolenaar 					inetprint("remote", &inp->inp_faddr,
438046aad39SHajimu UMEMOTO 					    (int)inp->inp_fport, name, 0, af1);
439cfa1ca9dSYoshinobu Inoue 			}
4409784899dSBjoern A. Zeeb #endif
4419784899dSBjoern A. Zeeb #if defined(INET) && defined(INET6)
4429784899dSBjoern A. Zeeb 			else
4439784899dSBjoern A. Zeeb #endif
444cfa1ca9dSYoshinobu Inoue #ifdef INET6
4459784899dSBjoern A. Zeeb 			if (inp->inp_vflag & INP_IPV6) {
446ade9ccfeSMarcel Moolenaar 				inet6print("local", &inp->in6p_laddr,
447cfa1ca9dSYoshinobu Inoue 				    (int)inp->inp_lport, name, 1);
448ac55add0SGuido van Rooij 				if (!Lflag)
449ade9ccfeSMarcel Moolenaar 					inet6print("remote", &inp->in6p_faddr,
450cfa1ca9dSYoshinobu Inoue 					    (int)inp->inp_fport, name, 0);
451cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
452cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
4538d612dd2SPoul-Henning Kamp 		} else {
4549784899dSBjoern A. Zeeb #ifdef INET
455cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
456ade9ccfeSMarcel Moolenaar 				inetprint("local", &inp->inp_laddr,
457046aad39SHajimu UMEMOTO 				    (int)inp->inp_lport, name, 0, af1);
458ac55add0SGuido van Rooij 				if (!Lflag)
459ade9ccfeSMarcel Moolenaar 					inetprint("remote", &inp->inp_faddr,
460ac55add0SGuido van Rooij 					    (int)inp->inp_fport, name,
461046aad39SHajimu UMEMOTO 					    inp->inp_lport != inp->inp_fport,
462046aad39SHajimu UMEMOTO 					    af1);
463cfa1ca9dSYoshinobu Inoue 			}
4649784899dSBjoern A. Zeeb #endif
4659784899dSBjoern A. Zeeb #if defined(INET) && defined(INET6)
4669784899dSBjoern A. Zeeb 			else
4679784899dSBjoern A. Zeeb #endif
468cfa1ca9dSYoshinobu Inoue #ifdef INET6
4699784899dSBjoern A. Zeeb 			if (inp->inp_vflag & INP_IPV6) {
470ade9ccfeSMarcel Moolenaar 				inet6print("local", &inp->in6p_laddr,
471cfa1ca9dSYoshinobu Inoue 				    (int)inp->inp_lport, name, 0);
472ac55add0SGuido van Rooij 				if (!Lflag)
473ade9ccfeSMarcel Moolenaar 					inet6print("remote", &inp->in6p_faddr,
474cfa1ca9dSYoshinobu Inoue 					    (int)inp->inp_fport, name,
4753feeb332SDavid E. O'Brien 					    inp->inp_lport != inp->inp_fport);
476cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
477cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
4788d612dd2SPoul-Henning Kamp 		}
47949f287f8SGeorge V. Neville-Neil 		if (xflag) {
480d59bc188SGleb Smirnoff 			xo_emit("{:receive-high-water/%6u} "
481d59bc188SGleb Smirnoff 			    "{:send-high-water/%6u} "
482ade9ccfeSMarcel Moolenaar 			    "{:receive-low-water/%6u} {:send-low-water/%6u} "
483ade9ccfeSMarcel Moolenaar 			    "{:receive-mbuf-bytes/%6u} {:send-mbuf-bytes/%6u} "
484ade9ccfeSMarcel Moolenaar 			    "{:receive-mbuf-bytes-max/%6u} "
485ade9ccfeSMarcel Moolenaar 			    "{:send-mbuf-bytes-max/%6u}",
48649f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
48749f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
48849f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
48949f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
490cc65eb4eSGleb Smirnoff 			if (istcp)
491ade9ccfeSMarcel Moolenaar 				xo_emit(" {:retransmit-timer/%4d.%02d} "
492ade9ccfeSMarcel Moolenaar 				    "{:persist-timer/%4d.%02d} "
493ade9ccfeSMarcel Moolenaar 				    "{:keepalive-timer/%4d.%02d} "
494ade9ccfeSMarcel Moolenaar 				    "{:msl2-timer/%4d.%02d} "
495ade9ccfeSMarcel Moolenaar 				    "{:delay-ack-timer/%4d.%02d} "
496ade9ccfeSMarcel Moolenaar 				    "{:inactivity-timer/%4d.%02d}",
497cc65eb4eSGleb Smirnoff 				    tp->tt_rexmt / 1000,
498cc65eb4eSGleb Smirnoff 				    (tp->tt_rexmt % 1000) / 10,
499cc65eb4eSGleb Smirnoff 				    tp->tt_persist / 1000,
500cc65eb4eSGleb Smirnoff 				    (tp->tt_persist % 1000) / 10,
501cc65eb4eSGleb Smirnoff 				    tp->tt_keep / 1000,
502cc65eb4eSGleb Smirnoff 				    (tp->tt_keep % 1000) / 10,
503cc65eb4eSGleb Smirnoff 				    tp->tt_2msl / 1000,
504cc65eb4eSGleb Smirnoff 				    (tp->tt_2msl % 1000) / 10,
505cc65eb4eSGleb Smirnoff 				    tp->tt_delack / 1000,
506cc65eb4eSGleb Smirnoff 				    (tp->tt_delack % 1000) / 10,
507cc65eb4eSGleb Smirnoff 				    tp->t_rcvtime / 1000,
508cc65eb4eSGleb Smirnoff 				    (tp->t_rcvtime % 1000) / 10);
509b8614722SMike Silbersack 		}
51085b0f0f3SAdrian Chadd 		if (istcp && !Lflag && !xflag && !Tflag && !Rflag) {
5114f81ef50SGarrett Wollman 			if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
5122529f56eSJonathan T. Looney 				xo_emit("{:tcp-state/%-11d}", tp->t_state);
5139a94a597SGarrett Wollman 			else {
5142529f56eSJonathan T. Looney 				xo_emit("{:tcp-state/%-11s}",
515ade9ccfeSMarcel Moolenaar 				    tcpstates[tp->t_state]);
516513822ddSAdam David #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
5179a94a597SGarrett Wollman 				/* Show T/TCP `hidden state' */
5184f81ef50SGarrett Wollman 				if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
519ade9ccfeSMarcel Moolenaar 					xo_emit("{:need-syn-or-fin/*}");
520513822ddSAdam David #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
5219a94a597SGarrett Wollman 			}
5229b50d902SRodney W. Grimes 		}
52385b0f0f3SAdrian Chadd 		if (Rflag) {
524ade9ccfeSMarcel Moolenaar 			/* XXX: is this right Alfred */
525ade9ccfeSMarcel Moolenaar 			xo_emit(" {:flow-id/%08x} {:flow-type/%5d}",
52685b0f0f3SAdrian Chadd 			    inp->inp_flowid,
52785b0f0f3SAdrian Chadd 			    inp->inp_flowtype);
52885b0f0f3SAdrian Chadd 		}
529b98a21f6SMichael Tuexen 		if (istcp) {
5300e5e35e3SRichard Scheffenegger 			if (cflag)
5310e5e35e3SRichard Scheffenegger 				xo_emit(" {:stack/%-*.*s}",
5320e5e35e3SRichard Scheffenegger 
5330e5e35e3SRichard Scheffenegger 					fnamelen, fnamelen, tp->xt_stack);
534b98a21f6SMichael Tuexen 			if (Cflag)
5350e5e35e3SRichard Scheffenegger 				xo_emit(" {:cc/%-*.*s}"
5360e5e35e3SRichard Scheffenegger 					" {:snd-cwnd/%10lu}"
5370e5e35e3SRichard Scheffenegger 					" {:snd-ssthresh/%10lu}"
5380e5e35e3SRichard Scheffenegger 					" {:t-maxseg/%5u} {:ecn/%3s}",
5390e5e35e3SRichard Scheffenegger 					cnamelen, cnamelen, tp->xt_cc,
5400e5e35e3SRichard Scheffenegger 					tp->t_snd_cwnd, tp->t_snd_ssthresh,
5410e5e35e3SRichard Scheffenegger 					tp->t_maxseg,
5420e5e35e3SRichard Scheffenegger 					(tp->t_state >= TCPS_ESTABLISHED ?
5430e5e35e3SRichard Scheffenegger 					    (tp->xt_ecn > 0 ?
5440e5e35e3SRichard Scheffenegger 						(tp->xt_ecn == 1 ?
5450e5e35e3SRichard Scheffenegger 						    "ecn" : "ace")
5460e5e35e3SRichard Scheffenegger 						: "off")
5470e5e35e3SRichard Scheffenegger 					    : "n/a"));
548b98a21f6SMichael Tuexen 			if (Pflag)
549b98a21f6SMichael Tuexen 				xo_emit(" {:log-id/%s}",
550b98a21f6SMichael Tuexen 				    tp->xt_logid[0] == '\0' ?
5512529f56eSJonathan T. Looney 				    "-" : tp->xt_logid);
552b98a21f6SMichael Tuexen 		}
553ade9ccfeSMarcel Moolenaar 		xo_emit("\n");
554ade9ccfeSMarcel Moolenaar 		xo_close_instance("socket");
5559b50d902SRodney W. Grimes 	}
5564f81ef50SGarrett Wollman 	if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
5574f81ef50SGarrett Wollman 		if (oxig->xig_count > xig->xig_count) {
558ade9ccfeSMarcel Moolenaar 			xo_emit("Some {d:lost/%s} sockets may have been "
559ade9ccfeSMarcel Moolenaar 			    "deleted.\n", name);
5604f81ef50SGarrett Wollman 		} else if (oxig->xig_count < xig->xig_count) {
561ade9ccfeSMarcel Moolenaar 			xo_emit("Some {d:created/%s} sockets may have been "
562ade9ccfeSMarcel Moolenaar 			    "created.\n", name);
5634f81ef50SGarrett Wollman 		} else {
564ade9ccfeSMarcel Moolenaar 			xo_emit("Some {d:changed/%s} sockets may have been "
565ade9ccfeSMarcel Moolenaar 			    "created or deleted.\n", name);
5664f81ef50SGarrett Wollman 		}
5674f81ef50SGarrett Wollman 	}
5684f81ef50SGarrett Wollman 	free(buf);
5699b50d902SRodney W. Grimes }
5709b50d902SRodney W. Grimes 
5719b50d902SRodney W. Grimes /*
5729b50d902SRodney W. Grimes  * Dump TCP statistics structure.
5739b50d902SRodney W. Grimes  */
5749b50d902SRodney W. Grimes void
tcp_stats(u_long off,const char * name,int af1 __unused,int proto __unused)575feda1a43SJohn Baldwin tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
5769b50d902SRodney W. Grimes {
5779eddb899SMark Johnston 	struct tcpstat tcpstat;
578dbfd8708SGleb Smirnoff 	uint64_t tcps_states[TCP_NSTATES];
5799b50d902SRodney W. Grimes 
580cfa1ca9dSYoshinobu Inoue #ifdef INET6
581cfa1ca9dSYoshinobu Inoue 	if (tcp_done != 0)
582cfa1ca9dSYoshinobu Inoue 		return;
583cfa1ca9dSYoshinobu Inoue 	else
584cfa1ca9dSYoshinobu Inoue 		tcp_done = 1;
585cfa1ca9dSYoshinobu Inoue #endif
586cfa1ca9dSYoshinobu Inoue 
5879eddb899SMark Johnston 	if (fetch_stats("net.inet.tcp.stats", off, &tcpstat,
5889eddb899SMark Johnston 	    sizeof(tcpstat), kread_counters) != 0)
589feda1a43SJohn Baldwin 		return;
590feda1a43SJohn Baldwin 
591dbfd8708SGleb Smirnoff 	if (fetch_stats_ro("net.inet.tcp.states", nl[N_TCPS_STATES].n_value,
592dbfd8708SGleb Smirnoff 	    &tcps_states, sizeof(tcps_states), kread_counters) != 0)
593dbfd8708SGleb Smirnoff 		return;
594dbfd8708SGleb Smirnoff 
595ade9ccfeSMarcel Moolenaar 	xo_open_container("tcp");
596ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
5979b50d902SRodney W. Grimes 
5989b50d902SRodney W. Grimes #define	p(f, m) if (tcpstat.f || sflag <= 1)				\
599ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )tcpstat.f, plural(tcpstat.f))
60022694ebaSBruce Evans #define	p1a(f, m) if (tcpstat.f || sflag <= 1)				\
601ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )tcpstat.f)
6029b50d902SRodney W. Grimes #define	p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1)	\
603ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1),		\
6045923c293SGleb Smirnoff 	    (uintmax_t )tcpstat.f2, plural(tcpstat.f2))
60522694ebaSBruce Evans #define	p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1)	\
606ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1),		\
6075923c293SGleb Smirnoff 	    (uintmax_t )tcpstat.f2)
6089b50d902SRodney W. Grimes #define	p3(f, m) if (tcpstat.f || sflag <= 1)				\
609ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )tcpstat.f, pluralies(tcpstat.f))
6109b50d902SRodney W. Grimes 
611ade9ccfeSMarcel Moolenaar 	p(tcps_sndtotal, "\t{:sent-packets/%ju} {N:/packet%s sent}\n");
612ade9ccfeSMarcel Moolenaar 	p2(tcps_sndpack,tcps_sndbyte, "\t\t{:sent-data-packets/%ju} "
613ade9ccfeSMarcel Moolenaar 	    "{N:/data packet%s} ({:sent-data-bytes/%ju} {N:/byte%s})\n");
614ade9ccfeSMarcel Moolenaar 	p2(tcps_sndrexmitpack, tcps_sndrexmitbyte, "\t\t"
615ade9ccfeSMarcel Moolenaar 	    "{:sent-retransmitted-packets/%ju} {N:/data packet%s} "
616ade9ccfeSMarcel Moolenaar 	    "({:sent-retransmitted-bytes/%ju} {N:/byte%s}) "
617ade9ccfeSMarcel Moolenaar 	    "{N:retransmitted}\n");
618ade9ccfeSMarcel Moolenaar 	p(tcps_sndrexmitbad, "\t\t"
619ade9ccfeSMarcel Moolenaar 	    "{:sent-unnecessary-retransmitted-packets/%ju} "
620ade9ccfeSMarcel Moolenaar 	    "{N:/data packet%s unnecessarily retransmitted}\n");
621ade9ccfeSMarcel Moolenaar 	p(tcps_mturesent, "\t\t{:sent-resends-by-mtu-discovery/%ju} "
622ade9ccfeSMarcel Moolenaar 	    "{N:/resend%s initiated by MTU discovery}\n");
623ade9ccfeSMarcel Moolenaar 	p2a(tcps_sndacks, tcps_delack, "\t\t{:sent-ack-only-packets/%ju} "
624ade9ccfeSMarcel Moolenaar 	    "{N:/ack-only packet%s/} ({:sent-packets-delayed/%ju} "
625ade9ccfeSMarcel Moolenaar 	    "{N:delayed})\n");
626ade9ccfeSMarcel Moolenaar 	p(tcps_sndurg, "\t\t{:sent-urg-only-packets/%ju} "
627ade9ccfeSMarcel Moolenaar 	    "{N:/URG only packet%s}\n");
628ade9ccfeSMarcel Moolenaar 	p(tcps_sndprobe, "\t\t{:sent-window-probe-packets/%ju} "
629ade9ccfeSMarcel Moolenaar 	    "{N:/window probe packet%s}\n");
630ade9ccfeSMarcel Moolenaar 	p(tcps_sndwinup, "\t\t{:sent-window-update-packets/%ju} "
631ade9ccfeSMarcel Moolenaar 	    "{N:/window update packet%s}\n");
632ade9ccfeSMarcel Moolenaar 	p(tcps_sndctrl, "\t\t{:sent-control-packets/%ju} "
633ade9ccfeSMarcel Moolenaar 	    "{N:/control packet%s}\n");
634ade9ccfeSMarcel Moolenaar 	p(tcps_rcvtotal, "\t{:received-packets/%ju} "
635ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s received}\n");
636ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t"
637ade9ccfeSMarcel Moolenaar 	    "{:received-ack-packets/%ju} {N:/ack%s} "
638ade9ccfeSMarcel Moolenaar 	    "{N:(for} {:received-ack-bytes/%ju} {N:/byte%s})\n");
639ade9ccfeSMarcel Moolenaar 	p(tcps_rcvdupack, "\t\t{:received-duplicate-acks/%ju} "
640ade9ccfeSMarcel Moolenaar 	    "{N:/duplicate ack%s}\n");
6419e644c23SMichael Tuexen 	p(tcps_tunneled_pkts, "\t\t{:received-udp-tunneled-pkts/%ju} "
6429e644c23SMichael Tuexen 	    "{N:/UDP tunneled pkt%s}\n");
6439e644c23SMichael Tuexen 	p(tcps_tunneled_errs, "\t\t{:received-bad-udp-tunneled-pkts/%ju} "
6449e644c23SMichael Tuexen 	    "{N:/UDP tunneled pkt cnt with error%s}\n");
645646c28eaSMichael Tuexen 	p(tcps_rcvacktoomuch, "\t\t{:received-acks-for-data-not-yet-sent/%ju} "
646646c28eaSMichael Tuexen 	    "{N:/ack%s for data not yet sent}\n");
647646c28eaSMichael Tuexen 	p(tcps_rcvghostack, "\t\t{:received-acks-for-data-never-been-sent/%ju} "
648646c28eaSMichael Tuexen 	    "{N:/ack%s for data never been sent (ghost acks)}\n");
649646c28eaSMichael Tuexen 	p(tcps_rcvacktooold, "\t\t{:received-acks-for-data-being-too-old/%ju} "
650646c28eaSMichael Tuexen 	    "{N:/ack%s for data being too old}\n");
651ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvpack, tcps_rcvbyte, "\t\t"
652ade9ccfeSMarcel Moolenaar 	    "{:received-in-sequence-packets/%ju} {N:/packet%s} "
653ade9ccfeSMarcel Moolenaar 	    "({:received-in-sequence-bytes/%ju} {N:/byte%s}) "
654ade9ccfeSMarcel Moolenaar 	    "{N:received in-sequence}\n");
655ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvduppack, tcps_rcvdupbyte, "\t\t"
656ade9ccfeSMarcel Moolenaar 	    "{:received-completely-duplicate-packets/%ju} "
657ade9ccfeSMarcel Moolenaar 	    "{N:/completely duplicate packet%s} "
658ade9ccfeSMarcel Moolenaar 	    "({:received-completely-duplicate-bytes/%ju} {N:/byte%s})\n");
659ade9ccfeSMarcel Moolenaar 	p(tcps_pawsdrop, "\t\t{:received-old-duplicate-packets/%ju} "
660ade9ccfeSMarcel Moolenaar 	    "{N:/old duplicate packet%s}\n");
661ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte, "\t\t"
662ade9ccfeSMarcel Moolenaar 	    "{:received-some-duplicate-packets/%ju} "
663ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s with some dup. data} "
664ade9ccfeSMarcel Moolenaar 	    "({:received-some-duplicate-bytes/%ju} {N:/byte%s duped/})\n");
665ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvoopack, tcps_rcvoobyte, "\t\t{:received-out-of-order/%ju} "
666ade9ccfeSMarcel Moolenaar 	    "{N:/out-of-order packet%s} "
667ade9ccfeSMarcel Moolenaar 	    "({:received-out-of-order-bytes/%ju} {N:/byte%s})\n");
668ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin, "\t\t"
669ade9ccfeSMarcel Moolenaar 	    "{:received-after-window-packets/%ju} {N:/packet%s} "
670ade9ccfeSMarcel Moolenaar 	    "({:received-after-window-bytes/%ju} {N:/byte%s}) "
671ade9ccfeSMarcel Moolenaar 	    "{N:of data after window}\n");
672ade9ccfeSMarcel Moolenaar 	p(tcps_rcvwinprobe, "\t\t{:received-window-probes/%ju} "
673ade9ccfeSMarcel Moolenaar 	    "{N:/window probe%s}\n");
674ade9ccfeSMarcel Moolenaar 	p(tcps_rcvwinupd, "\t\t{:receive-window-update-packets/%ju} "
675ade9ccfeSMarcel Moolenaar 	    "{N:/window update packet%s}\n");
676a36230f7SRandall Stewart 	p(tcps_dsack_count, "\t\t{:received-with-dsack-packets/%ju} "
677a36230f7SRandall Stewart 	    "{N:/packet%s received with dsack}\n");
678a36230f7SRandall Stewart 	p(tcps_dsack_bytes, "\t\t{:received-with-dsack-bytes/%ju} "
679a36230f7SRandall Stewart 	    "{N:/dsack byte%s received (no TLP involved)}\n");
680a36230f7SRandall Stewart 	p(tcps_dsack_tlp_bytes, "\t\t{:received-with-dsack-bytes-tlp/%ju} "
681a36230f7SRandall Stewart 	    "{N:/dsack byte%s received (TLP responsible)}\n");
682ade9ccfeSMarcel Moolenaar 	p(tcps_rcvafterclose, "\t\t{:received-after-close-packets/%ju} "
683ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s received after close}\n");
684ade9ccfeSMarcel Moolenaar 	p(tcps_rcvbadsum, "\t\t{:discard-bad-checksum/%ju} "
685ade9ccfeSMarcel Moolenaar 	    "{N:/discarded for bad checksum%s}\n");
686ade9ccfeSMarcel Moolenaar 	p(tcps_rcvbadoff, "\t\t{:discard-bad-header-offset/%ju} "
687ade9ccfeSMarcel Moolenaar 	    "{N:/discarded for bad header offset field%s}\n");
688ade9ccfeSMarcel Moolenaar 	p1a(tcps_rcvshort, "\t\t{:discard-too-short/%ju} "
689ade9ccfeSMarcel Moolenaar 	    "{N:discarded because packet too short}\n");
690fbf0f310SJohn Baldwin 	p1a(tcps_rcvreassfull, "\t\t{:discard-reassembly-queue-full/%ju} "
691fbf0f310SJohn Baldwin 	    "{N:discarded due to full reassembly queue}\n");
692ade9ccfeSMarcel Moolenaar 	p(tcps_connattempt, "\t{:connection-requests/%ju} "
693ade9ccfeSMarcel Moolenaar 	    "{N:/connection request%s}\n");
694ade9ccfeSMarcel Moolenaar 	p(tcps_accepts, "\t{:connections-accepts/%ju} "
695ade9ccfeSMarcel Moolenaar 	    "{N:/connection accept%s}\n");
696ade9ccfeSMarcel Moolenaar 	p(tcps_badsyn, "\t{:bad-connection-attempts/%ju} "
697ade9ccfeSMarcel Moolenaar 	    "{N:/bad connection attempt%s}\n");
698ade9ccfeSMarcel Moolenaar 	p(tcps_listendrop, "\t{:listen-queue-overflows/%ju} "
699ade9ccfeSMarcel Moolenaar 	    "{N:/listen queue overflow%s}\n");
700ade9ccfeSMarcel Moolenaar 	p(tcps_badrst, "\t{:ignored-in-window-resets/%ju} "
701ade9ccfeSMarcel Moolenaar 	    "{N:/ignored RSTs in the window%s}\n");
702ade9ccfeSMarcel Moolenaar 	p(tcps_connects, "\t{:connections-established/%ju} "
703ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s established (including accepts)}\n");
704e9d2c201SMarcelo Araujo 	p(tcps_usedrtt, "\t\t{:connections-hostcache-rtt/%ju} "
705e9d2c201SMarcelo Araujo 	    "{N:/time%s used RTT from hostcache}\n");
706e9d2c201SMarcelo Araujo 	p(tcps_usedrttvar, "\t\t{:connections-hostcache-rttvar/%ju} "
707e9d2c201SMarcelo Araujo 	    "{N:/time%s used RTT variance from hostcache}\n");
708e9d2c201SMarcelo Araujo 	p(tcps_usedssthresh, "\t\t{:connections-hostcache-ssthresh/%ju} "
709e9d2c201SMarcelo Araujo 	    "{N:/time%s used slow-start threshold from hostcache}\n");
710ade9ccfeSMarcel Moolenaar 	p2(tcps_closed, tcps_drops, "\t{:connections-closed/%ju} "
711ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s closed (including} "
712ade9ccfeSMarcel Moolenaar 	    "{:connection-drops/%ju} {N:/drop%s})\n");
713ade9ccfeSMarcel Moolenaar 	p(tcps_cachedrtt, "\t\t{:connections-updated-rtt-on-close/%ju} "
714ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s updated cached RTT on close}\n");
715ade9ccfeSMarcel Moolenaar 	p(tcps_cachedrttvar, "\t\t"
716ade9ccfeSMarcel Moolenaar 	    "{:connections-updated-variance-on-close/%ju} "
717ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s updated cached RTT variance on close}\n");
718ade9ccfeSMarcel Moolenaar 	p(tcps_cachedssthresh, "\t\t"
719ade9ccfeSMarcel Moolenaar 	    "{:connections-updated-ssthresh-on-close/%ju} "
720ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s updated cached ssthresh on close}\n");
721ade9ccfeSMarcel Moolenaar 	p(tcps_conndrops, "\t{:embryonic-connections-dropped/%ju} "
722ade9ccfeSMarcel Moolenaar 	    "{N:/embryonic connection%s dropped}\n");
723ade9ccfeSMarcel Moolenaar 	p2(tcps_rttupdated, tcps_segstimed, "\t{:segments-updated-rtt/%ju} "
724ade9ccfeSMarcel Moolenaar 	    "{N:/segment%s updated rtt (of} "
725ade9ccfeSMarcel Moolenaar 	    "{:segment-update-attempts/%ju} {N:/attempt%s})\n");
726ade9ccfeSMarcel Moolenaar 	p(tcps_rexmttimeo, "\t{:retransmit-timeouts/%ju} "
727ade9ccfeSMarcel Moolenaar 	    "{N:/retransmit timeout%s}\n");
728ade9ccfeSMarcel Moolenaar 	p(tcps_timeoutdrop, "\t\t"
729ade9ccfeSMarcel Moolenaar 	    "{:connections-dropped-by-retransmit-timeout/%ju} "
730ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s dropped by rexmit timeout}\n");
731ade9ccfeSMarcel Moolenaar 	p(tcps_persisttimeo, "\t{:persist-timeout/%ju} "
732ade9ccfeSMarcel Moolenaar 	    "{N:/persist timeout%s}\n");
733ade9ccfeSMarcel Moolenaar 	p(tcps_persistdrop, "\t\t"
734ade9ccfeSMarcel Moolenaar 	    "{:connections-dropped-by-persist-timeout/%ju} "
735ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s dropped by persist timeout}\n");
736ade9ccfeSMarcel Moolenaar 	p(tcps_finwait2_drops, "\t"
737ade9ccfeSMarcel Moolenaar 	    "{:connections-dropped-by-finwait2-timeout/%ju} "
738ade9ccfeSMarcel Moolenaar 	    "{N:/Connection%s (fin_wait_2) dropped because of timeout}\n");
739ade9ccfeSMarcel Moolenaar 	p(tcps_keeptimeo, "\t{:keepalive-timeout/%ju} "
740ade9ccfeSMarcel Moolenaar 	    "{N:/keepalive timeout%s}\n");
741ade9ccfeSMarcel Moolenaar 	p(tcps_keepprobe, "\t\t{:keepalive-probes/%ju} "
742ade9ccfeSMarcel Moolenaar 	    "{N:/keepalive probe%s sent}\n");
743ade9ccfeSMarcel Moolenaar 	p(tcps_keepdrops, "\t\t{:connections-dropped-by-keepalives/%ju} "
744ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s dropped by keepalive}\n");
74508af8aacSRandall Stewart 	p(tcps_progdrops, "\t{:connections-dropped-due-to-progress-time/%ju} "
74608af8aacSRandall Stewart 	    "{N:/connection%s dropped due to exceeding progress time}\n");
747ade9ccfeSMarcel Moolenaar 	p(tcps_predack, "\t{:ack-header-predictions/%ju} "
748ade9ccfeSMarcel Moolenaar 	    "{N:/correct ACK header prediction%s}\n");
749ade9ccfeSMarcel Moolenaar 	p(tcps_preddat, "\t{:data-packet-header-predictions/%ju} "
750ade9ccfeSMarcel Moolenaar 	    "{N:/correct data packet header prediction%s}\n");
75160a31b3aSJonathan Lemon 
752ade9ccfeSMarcel Moolenaar 	xo_open_container("syncache");
753b6101dafSPaul Saab 
754ade9ccfeSMarcel Moolenaar 	p3(tcps_sc_added, "\t{:entries-added/%ju} "
755ade9ccfeSMarcel Moolenaar 	    "{N:/syncache entr%s added}\n");
756ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_retransmitted, "\t\t{:retransmitted/%ju} "
757ade9ccfeSMarcel Moolenaar 	    "{N:/retransmitted}\n");
758ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_dupsyn, "\t\t{:duplicates/%ju} {N:/dupsyn}\n");
759ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_dropped, "\t\t{:dropped/%ju} {N:/dropped}\n");
760ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_completed, "\t\t{:completed/%ju} {N:/completed}\n");
761ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_bucketoverflow, "\t\t{:bucket-overflow/%ju} "
762ade9ccfeSMarcel Moolenaar 	    "{N:/bucket overflow}\n");
763ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_cacheoverflow, "\t\t{:cache-overflow/%ju} "
764ade9ccfeSMarcel Moolenaar 	    "{N:/cache overflow}\n");
765ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_reset, "\t\t{:reset/%ju} {N:/reset}\n");
766ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_stale, "\t\t{:stale/%ju} {N:/stale}\n");
767ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_aborted, "\t\t{:aborted/%ju} {N:/aborted}\n");
768ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_badack, "\t\t{:bad-ack/%ju} {N:/badack}\n");
769ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_unreach, "\t\t{:unreachable/%ju} {N:/unreach}\n");
770ade9ccfeSMarcel Moolenaar 	p(tcps_sc_zonefail, "\t\t{:zone-failures/%ju} {N:/zone failure%s}\n");
771ade9ccfeSMarcel Moolenaar 	p(tcps_sc_sendcookie, "\t{:sent-cookies/%ju} {N:/cookie%s sent}\n");
772ade9ccfeSMarcel Moolenaar 	p(tcps_sc_recvcookie, "\t{:receivd-cookies/%ju} "
773ade9ccfeSMarcel Moolenaar 	    "{N:/cookie%s received}\n");
774699ef999SRuslan Ermilov 
775ade9ccfeSMarcel Moolenaar 	xo_close_container("syncache");
776b6101dafSPaul Saab 
777ade9ccfeSMarcel Moolenaar 	xo_open_container("hostcache");
778a4993b25SBjoern A. Zeeb 
779ade9ccfeSMarcel Moolenaar 	p3(tcps_hc_added, "\t{:entries-added/%ju} "
780ade9ccfeSMarcel Moolenaar 	    "{N:/hostcache entr%s added}\n");
781ade9ccfeSMarcel Moolenaar 	p1a(tcps_hc_bucketoverflow, "\t\t{:buffer-overflows/%ju} "
782ade9ccfeSMarcel Moolenaar 	    "{N:/bucket overflow}\n");
783ade9ccfeSMarcel Moolenaar 
784ade9ccfeSMarcel Moolenaar 	xo_close_container("hostcache");
785ade9ccfeSMarcel Moolenaar 
786ade9ccfeSMarcel Moolenaar 	xo_open_container("sack");
787ade9ccfeSMarcel Moolenaar 
788ade9ccfeSMarcel Moolenaar 	p(tcps_sack_recovery_episode, "\t{:recovery-episodes/%ju} "
789ade9ccfeSMarcel Moolenaar 	    "{N:/SACK recovery episode%s}\n");
790ade9ccfeSMarcel Moolenaar 	p(tcps_sack_rexmits, "\t{:segment-retransmits/%ju} "
791ade9ccfeSMarcel Moolenaar 	    "{N:/segment rexmit%s in SACK recovery episodes}\n");
7922a9aae9eSRichard Scheffenegger 	p(tcps_sack_rexmits_tso, "\t{:tso-chunk-retransmits/%ju} "
7932a9aae9eSRichard Scheffenegger 	    "{N:/tso chunk rexmit%s in SACK recovery episodes}\n");
794ade9ccfeSMarcel Moolenaar 	p(tcps_sack_rexmit_bytes, "\t{:byte-retransmits/%ju} "
795ade9ccfeSMarcel Moolenaar 	    "{N:/byte rexmit%s in SACK recovery episodes}\n");
796ade9ccfeSMarcel Moolenaar 	p(tcps_sack_rcv_blocks, "\t{:received-blocks/%ju} "
797ade9ccfeSMarcel Moolenaar 	    "{N:/SACK option%s (SACK blocks) received}\n");
798ade9ccfeSMarcel Moolenaar 	p(tcps_sack_send_blocks, "\t{:sent-option-blocks/%ju} "
799ade9ccfeSMarcel Moolenaar 	    "{N:/SACK option%s (SACK blocks) sent}\n");
800c55b340fSRichard Scheffenegger 	p(tcps_sack_lostrexmt, "\t{:lost-retransmissions/%ju} "
8010471a8c7SRichard Scheffenegger 	    "{N:/SACK retransmission%s lost}\n");
802ade9ccfeSMarcel Moolenaar 	p1a(tcps_sack_sboverflow, "\t{:scoreboard-overflows/%ju} "
803ade9ccfeSMarcel Moolenaar 	    "{N:/SACK scoreboard overflow}\n");
804ade9ccfeSMarcel Moolenaar 
805ade9ccfeSMarcel Moolenaar 	xo_close_container("sack");
806ade9ccfeSMarcel Moolenaar 	xo_open_container("ecn");
807ade9ccfeSMarcel Moolenaar 
8081a70101aSRichard Scheffenegger 	p(tcps_ecn_rcvce, "\t{:received-ce-packets/%ju} "
8091a70101aSRichard Scheffenegger 	    "{N:/packet%s received with ECN CE bit set}\n");
8101a70101aSRichard Scheffenegger 	p(tcps_ecn_rcvect0, "\t{:received-ect0-packets/%ju} "
8111a70101aSRichard Scheffenegger 	    "{N:/packet%s received with ECN ECT(0) bit set}\n");
8121a70101aSRichard Scheffenegger 	p(tcps_ecn_rcvect1, "\t{:received-ect1-packets/%ju} "
8131a70101aSRichard Scheffenegger 	    "{N:/packet%s received with ECN ECT(1) bit set}\n");
8141a70101aSRichard Scheffenegger 	p(tcps_ecn_sndect0, "\t{:sent-ect0-packets/%ju} "
8151a70101aSRichard Scheffenegger 	    "{N:/packet%s sent with ECN ECT(0) bit set}\n");
8161a70101aSRichard Scheffenegger 	p(tcps_ecn_sndect1, "\t{:sent-ect1-packets/%ju} "
8171a70101aSRichard Scheffenegger 	    "{N:/packet%s sent with ECN ECT(1) bit set}\n");
818ade9ccfeSMarcel Moolenaar 	p(tcps_ecn_shs, "\t{:handshakes/%ju} "
819ade9ccfeSMarcel Moolenaar 	    "{N:/successful ECN handshake%s}\n");
820ade9ccfeSMarcel Moolenaar 	p(tcps_ecn_rcwnd, "\t{:congestion-reductions/%ju} "
821ade9ccfeSMarcel Moolenaar 	    "{N:/time%s ECN reduced the congestion window}\n");
822fcf59617SAndrey V. Elsukov 
8233f169c54SRichard Scheffenegger 	p(tcps_ace_nect, "\t{:ace-nonect-syn/%ju} "
8243f169c54SRichard Scheffenegger 	    "{N:/ACE SYN packet%s with Non-ECT}\n");
8253f169c54SRichard Scheffenegger 	p(tcps_ace_ect0, "\t{:ace-ect0-syn/%ju} "
8263f169c54SRichard Scheffenegger 	    "{N:/ACE SYN packet%s with ECT0}\n");
8273f169c54SRichard Scheffenegger 	p(tcps_ace_ect1, "\t{:ace-ect1-syn/%ju} "
8283f169c54SRichard Scheffenegger 	    "{N:/ACE SYN packet%s with ECT1}\n");
8293f169c54SRichard Scheffenegger 	p(tcps_ace_ce, "\t{:ace-ce-syn/%ju} "
8303f169c54SRichard Scheffenegger 	    "{N:/ACE SYN packet%s with CE}\n");
8313f169c54SRichard Scheffenegger 
832fcf59617SAndrey V. Elsukov 	xo_close_container("ecn");
833fcf59617SAndrey V. Elsukov 	xo_open_container("tcp-signature");
834fcf59617SAndrey V. Elsukov 	p(tcps_sig_rcvgoodsig, "\t{:received-good-signature/%ju} "
835fcf59617SAndrey V. Elsukov 	    "{N:/packet%s with matching signature received}\n");
836fcf59617SAndrey V. Elsukov 	p(tcps_sig_rcvbadsig, "\t{:received-bad-signature/%ju} "
837fcf59617SAndrey V. Elsukov 	    "{N:/packet%s with bad signature received}\n");
838fcf59617SAndrey V. Elsukov 	p(tcps_sig_err_buildsig, "\t{:failed-make-signature/%ju} "
839fcf59617SAndrey V. Elsukov 	    "{N:/time%s failed to make signature due to no SA}\n");
840fcf59617SAndrey V. Elsukov 	p(tcps_sig_err_sigopt, "\t{:no-signature-expected/%ju} "
841fcf59617SAndrey V. Elsukov 	    "{N:/time%s unexpected signature received}\n");
842fcf59617SAndrey V. Elsukov 	p(tcps_sig_err_nosigopt, "\t{:no-signature-provided/%ju} "
843fcf59617SAndrey V. Elsukov 	    "{N:/time%s no signature provided by segment}\n");
84432a04bb8SSean Bruno 
84532a04bb8SSean Bruno 	xo_close_container("tcp-signature");
84632a04bb8SSean Bruno 	xo_open_container("pmtud");
84732a04bb8SSean Bruno 
84832a04bb8SSean Bruno 	p(tcps_pmtud_blackhole_activated, "\t{:pmtud-activated/%ju} "
84932a04bb8SSean Bruno 	    "{N:/Path MTU discovery black hole detection activation%s}\n");
85032a04bb8SSean Bruno 	p(tcps_pmtud_blackhole_activated_min_mss,
85132a04bb8SSean Bruno 	    "\t{:pmtud-activated-min-mss/%ju} "
85232a04bb8SSean Bruno 	    "{N:/Path MTU discovery black hole detection min MSS activation%s}\n");
85332a04bb8SSean Bruno 	p(tcps_pmtud_blackhole_failed, "\t{:pmtud-failed/%ju} "
85432a04bb8SSean Bruno 	    "{N:/Path MTU discovery black hole detection failure%s}\n");
85571d2d5adSGleb Smirnoff 
85671d2d5adSGleb Smirnoff 	xo_close_container("pmtud");
85771d2d5adSGleb Smirnoff 	xo_open_container("tw");
85871d2d5adSGleb Smirnoff 
85971d2d5adSGleb Smirnoff 	p(tcps_tw_responds, "\t{:tw_responds/%ju} "
86071d2d5adSGleb Smirnoff 	    "{N:/time%s connection in TIME-WAIT responded with ACK}\n");
86171d2d5adSGleb Smirnoff 	p(tcps_tw_recycles, "\t{:tw_recycles/%ju} "
86271d2d5adSGleb Smirnoff 	    "{N:/time%s connection in TIME-WAIT was actively recycled}\n");
86371d2d5adSGleb Smirnoff 	p(tcps_tw_resets, "\t{:tw_resets/%ju} "
86471d2d5adSGleb Smirnoff 	    "{N:/time%s connection in TIME-WAIT responded with RST}\n");
86571d2d5adSGleb Smirnoff 
86671d2d5adSGleb Smirnoff 	xo_close_container("tw");
8679b50d902SRodney W. Grimes  #undef p
86822694ebaSBruce Evans  #undef p1a
8699b50d902SRodney W. Grimes  #undef p2
87022694ebaSBruce Evans  #undef p2a
8719b50d902SRodney W. Grimes  #undef p3
872dbfd8708SGleb Smirnoff 
873dbfd8708SGleb Smirnoff 	xo_open_container("TCP connection count by state");
874dbfd8708SGleb Smirnoff 	xo_emit("{T:/TCP connection count by state}:\n");
875dbfd8708SGleb Smirnoff 	for (int i = 0; i < TCP_NSTATES; i++) {
876dbfd8708SGleb Smirnoff 		/*
877dbfd8708SGleb Smirnoff 		 * XXXGL: is there a way in libxo to use %s
878dbfd8708SGleb Smirnoff 		 * in the "content string" of a format
879dbfd8708SGleb Smirnoff 		 * string? I failed to do that, that's why
880dbfd8708SGleb Smirnoff 		 * a temporary buffer is used to construct
881dbfd8708SGleb Smirnoff 		 * format string for xo_emit().
882dbfd8708SGleb Smirnoff 		 */
883dbfd8708SGleb Smirnoff 		char fmtbuf[80];
884dbfd8708SGleb Smirnoff 
885dbfd8708SGleb Smirnoff 		if (sflag > 1 && tcps_states[i] == 0)
886dbfd8708SGleb Smirnoff 			continue;
887dbfd8708SGleb Smirnoff 		snprintf(fmtbuf, sizeof(fmtbuf), "\t{:%s/%%ju} "
888dbfd8708SGleb Smirnoff                     "{Np:/connection ,connections} in %s state\n",
889dbfd8708SGleb Smirnoff 		    tcpstates[i], tcpstates[i]);
890dbfd8708SGleb Smirnoff 		xo_emit(fmtbuf, (uintmax_t )tcps_states[i]);
891dbfd8708SGleb Smirnoff 	}
892dbfd8708SGleb Smirnoff 	xo_close_container("TCP connection count by state");
893dbfd8708SGleb Smirnoff 
894ade9ccfeSMarcel Moolenaar 	xo_close_container("tcp");
8959b50d902SRodney W. Grimes }
8969b50d902SRodney W. Grimes 
8979b50d902SRodney W. Grimes /*
8989b50d902SRodney W. Grimes  * Dump UDP statistics structure.
8999b50d902SRodney W. Grimes  */
9009b50d902SRodney W. Grimes void
udp_stats(u_long off,const char * name,int af1 __unused,int proto __unused)901feda1a43SJohn Baldwin udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
9029b50d902SRodney W. Grimes {
9039eddb899SMark Johnston 	struct udpstat udpstat;
904c80211e3SAndrey V. Elsukov 	uint64_t delivered;
9059b50d902SRodney W. Grimes 
906cfa1ca9dSYoshinobu Inoue #ifdef INET6
907cfa1ca9dSYoshinobu Inoue 	if (udp_done != 0)
908cfa1ca9dSYoshinobu Inoue 		return;
909cfa1ca9dSYoshinobu Inoue 	else
910cfa1ca9dSYoshinobu Inoue 		udp_done = 1;
911cfa1ca9dSYoshinobu Inoue #endif
912cfa1ca9dSYoshinobu Inoue 
9139eddb899SMark Johnston 	if (fetch_stats("net.inet.udp.stats", off, &udpstat,
9149eddb899SMark Johnston 	    sizeof(udpstat), kread_counters) != 0)
915feda1a43SJohn Baldwin 		return;
916feda1a43SJohn Baldwin 
917ade9ccfeSMarcel Moolenaar 	xo_open_container("udp");
918ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
919ade9ccfeSMarcel Moolenaar 
9209b50d902SRodney W. Grimes #define	p(f, m) if (udpstat.f || sflag <= 1) \
921ade9ccfeSMarcel Moolenaar 	xo_emit("\t" m, (uintmax_t)udpstat.f, plural(udpstat.f))
92222694ebaSBruce Evans #define	p1a(f, m) if (udpstat.f || sflag <= 1) \
923ade9ccfeSMarcel Moolenaar 	xo_emit("\t" m, (uintmax_t)udpstat.f)
924ade9ccfeSMarcel Moolenaar 
925ade9ccfeSMarcel Moolenaar 	p(udps_ipackets, "{:received-datagrams/%ju} "
926ade9ccfeSMarcel Moolenaar 	    "{N:/datagram%s received}\n");
927ade9ccfeSMarcel Moolenaar 	p1a(udps_hdrops, "{:dropped-incomplete-headers/%ju} "
928ade9ccfeSMarcel Moolenaar 	    "{N:/with incomplete header}\n");
929ade9ccfeSMarcel Moolenaar 	p1a(udps_badlen, "{:dropped-bad-data-length/%ju} "
930ade9ccfeSMarcel Moolenaar 	    "{N:/with bad data length field}\n");
931ade9ccfeSMarcel Moolenaar 	p1a(udps_badsum, "{:dropped-bad-checksum/%ju} "
932ade9ccfeSMarcel Moolenaar 	    "{N:/with bad checksum}\n");
933ade9ccfeSMarcel Moolenaar 	p1a(udps_nosum, "{:dropped-no-checksum/%ju} "
934ade9ccfeSMarcel Moolenaar 	    "{N:/with no checksum}\n");
935ade9ccfeSMarcel Moolenaar 	p1a(udps_noport, "{:dropped-no-socket/%ju} "
936ade9ccfeSMarcel Moolenaar 	    "{N:/dropped due to no socket}\n");
937ade9ccfeSMarcel Moolenaar 	p(udps_noportbcast, "{:dropped-broadcast-multicast/%ju} "
938ade9ccfeSMarcel Moolenaar 	    "{N:/broadcast\\/multicast datagram%s undelivered}\n");
939ade9ccfeSMarcel Moolenaar 	p1a(udps_fullsock, "{:dropped-full-socket-buffer/%ju} "
940ade9ccfeSMarcel Moolenaar 	    "{N:/dropped due to full socket buffers}\n");
941ade9ccfeSMarcel Moolenaar 	p1a(udpps_pcbhashmiss, "{:not-for-hashed-pcb/%ju} "
942ade9ccfeSMarcel Moolenaar 	    "{N:/not for hashed pcb}\n");
9439b50d902SRodney W. Grimes 	delivered = udpstat.udps_ipackets -
9449b50d902SRodney W. Grimes 		    udpstat.udps_hdrops -
9459b50d902SRodney W. Grimes 		    udpstat.udps_badlen -
9469b50d902SRodney W. Grimes 		    udpstat.udps_badsum -
9479b50d902SRodney W. Grimes 		    udpstat.udps_noport -
9489b50d902SRodney W. Grimes 		    udpstat.udps_noportbcast -
9499b50d902SRodney W. Grimes 		    udpstat.udps_fullsock;
9509b50d902SRodney W. Grimes 	if (delivered || sflag <= 1)
951ade9ccfeSMarcel Moolenaar 		xo_emit("\t{:delivered-packets/%ju} {N:/delivered}\n",
952ade9ccfeSMarcel Moolenaar 		    (uint64_t)delivered);
953ade9ccfeSMarcel Moolenaar 	p(udps_opackets, "{:output-packets/%ju} {N:/datagram%s output}\n");
95471498f30SBruce M Simpson 	/* the next statistic is cumulative in udps_noportbcast */
955ade9ccfeSMarcel Moolenaar 	p(udps_filtermcast, "{:multicast-source-filter-matches/%ju} "
956ade9ccfeSMarcel Moolenaar 	    "{N:/time%s multicast source filter matched}\n");
9579b50d902SRodney W. Grimes #undef p
95822694ebaSBruce Evans #undef p1a
959ade9ccfeSMarcel Moolenaar 	xo_close_container("udp");
9609b50d902SRodney W. Grimes }
9619b50d902SRodney W. Grimes 
9629b50d902SRodney W. Grimes /*
963a9771948SGleb Smirnoff  * Dump CARP statistics structure.
964a9771948SGleb Smirnoff  */
965a9771948SGleb Smirnoff void
carp_stats(u_long off,const char * name,int af1 __unused,int proto __unused)966feda1a43SJohn Baldwin carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
967a9771948SGleb Smirnoff {
9689eddb899SMark Johnston 	struct carpstats carpstat;
969a9771948SGleb Smirnoff 
9709eddb899SMark Johnston 	if (fetch_stats("net.inet.carp.stats", off, &carpstat,
9719eddb899SMark Johnston 	    sizeof(carpstat), kread_counters) != 0)
972a9771948SGleb Smirnoff 		return;
973a9771948SGleb Smirnoff 
974ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
975ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
976a9771948SGleb Smirnoff 
977a9771948SGleb Smirnoff #define	p(f, m) if (carpstat.f || sflag <= 1) \
978ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t)carpstat.f, plural(carpstat.f))
979a9771948SGleb Smirnoff #define	p2(f, m) if (carpstat.f || sflag <= 1) \
980ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t)carpstat.f)
981a9771948SGleb Smirnoff 
982ade9ccfeSMarcel Moolenaar 	p(carps_ipackets, "\t{:received-inet-packets/%ju} "
983ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s received (IPv4)}\n");
984ade9ccfeSMarcel Moolenaar 	p(carps_ipackets6, "\t{:received-inet6-packets/%ju} "
985ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s received (IPv6)}\n");
986ade9ccfeSMarcel Moolenaar 	p(carps_badttl, "\t\t{:dropped-wrong-ttl/%ju} "
987ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s discarded for wrong TTL}\n");
988ade9ccfeSMarcel Moolenaar 	p(carps_hdrops, "\t\t{:dropped-short-header/%ju} "
989ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s shorter than header}\n");
990ade9ccfeSMarcel Moolenaar 	p(carps_badsum, "\t\t{:dropped-bad-checksum/%ju} "
991ade9ccfeSMarcel Moolenaar 	    "{N:/discarded for bad checksum%s}\n");
992ade9ccfeSMarcel Moolenaar 	p(carps_badver,	"\t\t{:dropped-bad-version/%ju} "
993ade9ccfeSMarcel Moolenaar 	    "{N:/discarded packet%s with a bad version}\n");
994ade9ccfeSMarcel Moolenaar 	p2(carps_badlen, "\t\t{:dropped-short-packet/%ju} "
995ade9ccfeSMarcel Moolenaar 	    "{N:/discarded because packet too short}\n");
996ade9ccfeSMarcel Moolenaar 	p2(carps_badauth, "\t\t{:dropped-bad-authentication/%ju} "
997ade9ccfeSMarcel Moolenaar 	    "{N:/discarded for bad authentication}\n");
998ade9ccfeSMarcel Moolenaar 	p2(carps_badvhid, "\t\t{:dropped-bad-vhid/%ju} "
999ade9ccfeSMarcel Moolenaar 	    "{N:/discarded for bad vhid}\n");
1000ade9ccfeSMarcel Moolenaar 	p2(carps_badaddrs, "\t\t{:dropped-bad-address-list/%ju} "
1001ade9ccfeSMarcel Moolenaar 	    "{N:/discarded because of a bad address list}\n");
1002ade9ccfeSMarcel Moolenaar 	p(carps_opackets, "\t{:sent-inet-packets/%ju} "
1003ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s sent (IPv4)}\n");
1004ade9ccfeSMarcel Moolenaar 	p(carps_opackets6, "\t{:sent-inet6-packets/%ju} "
1005ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s sent (IPv6)}\n");
1006ade9ccfeSMarcel Moolenaar 	p2(carps_onomem, "\t\t{:send-failed-memory-error/%ju} "
1007ade9ccfeSMarcel Moolenaar 	    "{N:/send failed due to mbuf memory error}\n");
1008a9771948SGleb Smirnoff #if notyet
1009ade9ccfeSMarcel Moolenaar 	p(carps_ostates, "\t\t{:send-state-updates/%s} "
1010ade9ccfeSMarcel Moolenaar 	    "{N:/state update%s sent}\n");
1011a9771948SGleb Smirnoff #endif
1012a9771948SGleb Smirnoff #undef p
1013a9771948SGleb Smirnoff #undef p2
1014ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
1015a9771948SGleb Smirnoff }
1016a9771948SGleb Smirnoff 
1017a9771948SGleb Smirnoff /*
10189b50d902SRodney W. Grimes  * Dump IP statistics structure.
10199b50d902SRodney W. Grimes  */
10209b50d902SRodney W. Grimes void
ip_stats(u_long off,const char * name,int af1 __unused,int proto __unused)1021feda1a43SJohn Baldwin ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
10229b50d902SRodney W. Grimes {
10239eddb899SMark Johnston 	struct ipstat ipstat;
10249b50d902SRodney W. Grimes 
10259eddb899SMark Johnston 	if (fetch_stats("net.inet.ip.stats", off, &ipstat,
10269eddb899SMark Johnston 	    sizeof(ipstat), kread_counters) != 0)
10279b50d902SRodney W. Grimes 		return;
10284f81ef50SGarrett Wollman 
1029ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
1030ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
10319b50d902SRodney W. Grimes 
10329b50d902SRodney W. Grimes #define	p(f, m) if (ipstat.f || sflag <= 1) \
1033ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )ipstat.f, plural(ipstat.f))
103422694ebaSBruce Evans #define	p1a(f, m) if (ipstat.f || sflag <= 1) \
1035ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )ipstat.f)
10369b50d902SRodney W. Grimes 
1037ade9ccfeSMarcel Moolenaar 	p(ips_total, "\t{:received-packets/%ju} "
1038ade9ccfeSMarcel Moolenaar 	    "{N:/total packet%s received}\n");
1039ade9ccfeSMarcel Moolenaar 	p(ips_badsum, "\t{:dropped-bad-checksum/%ju} "
1040ade9ccfeSMarcel Moolenaar 	    "{N:/bad header checksum%s}\n");
1041ade9ccfeSMarcel Moolenaar 	p1a(ips_toosmall, "\t{:dropped-below-minimum-size/%ju} "
1042ade9ccfeSMarcel Moolenaar 	    "{N:/with size smaller than minimum}\n");
1043ade9ccfeSMarcel Moolenaar 	p1a(ips_tooshort, "\t{:dropped-short-packets/%ju} "
1044ade9ccfeSMarcel Moolenaar 	    "{N:/with data size < data length}\n");
1045ade9ccfeSMarcel Moolenaar 	p1a(ips_toolong, "\t{:dropped-too-long/%ju} "
1046ade9ccfeSMarcel Moolenaar 	    "{N:/with ip length > max ip packet size}\n");
1047ade9ccfeSMarcel Moolenaar 	p1a(ips_badhlen, "\t{:dropped-short-header-length/%ju} "
1048ade9ccfeSMarcel Moolenaar 	    "{N:/with header length < data size}\n");
1049ade9ccfeSMarcel Moolenaar 	p1a(ips_badlen, "\t{:dropped-short-data/%ju} "
1050ade9ccfeSMarcel Moolenaar 	    "{N:/with data length < header length}\n");
1051ade9ccfeSMarcel Moolenaar 	p1a(ips_badoptions, "\t{:dropped-bad-options/%ju} "
1052ade9ccfeSMarcel Moolenaar 	    "{N:/with bad options}\n");
1053ade9ccfeSMarcel Moolenaar 	p1a(ips_badvers, "\t{:dropped-bad-version/%ju} "
1054ade9ccfeSMarcel Moolenaar 	    "{N:/with incorrect version number}\n");
1055ade9ccfeSMarcel Moolenaar 	p(ips_fragments, "\t{:received-fragments/%ju} "
1056ade9ccfeSMarcel Moolenaar 	    "{N:/fragment%s received}\n");
1057ade9ccfeSMarcel Moolenaar 	p(ips_fragdropped, "\t{:dropped-fragments/%ju} "
1058ade9ccfeSMarcel Moolenaar 	    "{N:/fragment%s dropped (dup or out of space)}\n");
1059ade9ccfeSMarcel Moolenaar 	p(ips_fragtimeout, "\t{:dropped-fragments-after-timeout/%ju} "
1060ade9ccfeSMarcel Moolenaar 	    "{N:/fragment%s dropped after timeout}\n");
1061ade9ccfeSMarcel Moolenaar 	p(ips_reassembled, "\t{:reassembled-packets/%ju} "
1062ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s reassembled ok}\n");
1063ade9ccfeSMarcel Moolenaar 	p(ips_delivered, "\t{:received-local-packets/%ju} "
1064ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s for this host}\n");
1065ade9ccfeSMarcel Moolenaar 	p(ips_noproto, "\t{:dropped-unknown-protocol/%ju} "
1066ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s for unknown\\/unsupported protocol}\n");
1067ade9ccfeSMarcel Moolenaar 	p(ips_forward, "\t{:forwarded-packets/%ju} "
1068ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s forwarded}");
1069ade9ccfeSMarcel Moolenaar 	p(ips_fastforward, " ({:fast-forwarded-packets/%ju} "
1070ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s fast forwarded})");
1071958d6f7fSPierre Beyssac 	if (ipstat.ips_forward || sflag <= 1)
1072ade9ccfeSMarcel Moolenaar 		xo_emit("\n");
1073ade9ccfeSMarcel Moolenaar 	p(ips_cantforward, "\t{:packets-cannot-forward/%ju} "
1074ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s not forwardable}\n");
1075ade9ccfeSMarcel Moolenaar 	p(ips_notmember, "\t{:received-unknown-multicast-group/%ju} "
1076ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s received for unknown multicast group}\n");
1077ade9ccfeSMarcel Moolenaar 	p(ips_redirectsent, "\t{:redirects-sent/%ju} "
1078ade9ccfeSMarcel Moolenaar 	    "{N:/redirect%s sent}\n");
1079ade9ccfeSMarcel Moolenaar 	p(ips_localout, "\t{:sent-packets/%ju} "
1080ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s sent from this host}\n");
1081ade9ccfeSMarcel Moolenaar 	p(ips_rawout, "\t{:send-packets-fabricated-header/%ju} "
1082ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s sent with fabricated ip header}\n");
1083ade9ccfeSMarcel Moolenaar 	p(ips_odropped, "\t{:discard-no-mbufs/%ju} "
1084ade9ccfeSMarcel Moolenaar 	    "{N:/output packet%s dropped due to no bufs, etc.}\n");
1085ade9ccfeSMarcel Moolenaar 	p(ips_noroute, "\t{:discard-no-route/%ju} "
1086ade9ccfeSMarcel Moolenaar 	    "{N:/output packet%s discarded due to no route}\n");
1087ade9ccfeSMarcel Moolenaar 	p(ips_fragmented, "\t{:sent-fragments/%ju} "
1088ade9ccfeSMarcel Moolenaar 	    "{N:/output datagram%s fragmented}\n");
1089ade9ccfeSMarcel Moolenaar 	p(ips_ofragments, "\t{:fragments-created/%ju} "
1090ade9ccfeSMarcel Moolenaar 	    "{N:/fragment%s created}\n");
1091ade9ccfeSMarcel Moolenaar 	p(ips_cantfrag, "\t{:discard-cannot-fragment/%ju} "
1092ade9ccfeSMarcel Moolenaar 	    "{N:/datagram%s that can't be fragmented}\n");
1093ade9ccfeSMarcel Moolenaar 	p(ips_nogif, "\t{:discard-tunnel-no-gif/%ju} "
1094ade9ccfeSMarcel Moolenaar 	    "{N:/tunneling packet%s that can't find gif}\n");
1095ade9ccfeSMarcel Moolenaar 	p(ips_badaddr, "\t{:discard-bad-address/%ju} "
1096ade9ccfeSMarcel Moolenaar 	    "{N:/datagram%s with bad address in header}\n");
10979b50d902SRodney W. Grimes #undef p
109822694ebaSBruce Evans #undef p1a
1099ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
11009b50d902SRodney W. Grimes }
11019b50d902SRodney W. Grimes 
110254fc657dSGeorge V. Neville-Neil /*
110354fc657dSGeorge V. Neville-Neil  * Dump ARP statistics structure.
110454fc657dSGeorge V. Neville-Neil  */
110554fc657dSGeorge V. Neville-Neil void
arp_stats(u_long off,const char * name,int af1 __unused,int proto __unused)110654fc657dSGeorge V. Neville-Neil arp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
110754fc657dSGeorge V. Neville-Neil {
11089eddb899SMark Johnston 	struct arpstat arpstat;
110954fc657dSGeorge V. Neville-Neil 
11109eddb899SMark Johnston 	if (fetch_stats("net.link.ether.arp.stats", off, &arpstat,
11119eddb899SMark Johnston 	    sizeof(arpstat), kread_counters) != 0)
111254fc657dSGeorge V. Neville-Neil 		return;
111354fc657dSGeorge V. Neville-Neil 
1114ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
1115ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
111654fc657dSGeorge V. Neville-Neil 
111754fc657dSGeorge V. Neville-Neil #define	p(f, m) if (arpstat.f || sflag <= 1) \
1118ade9ccfeSMarcel Moolenaar 	xo_emit("\t" m, (uintmax_t)arpstat.f, plural(arpstat.f))
111954fc657dSGeorge V. Neville-Neil #define	p2(f, m) if (arpstat.f || sflag <= 1) \
1120ade9ccfeSMarcel Moolenaar 	xo_emit("\t" m, (uintmax_t)arpstat.f, pluralies(arpstat.f))
112154fc657dSGeorge V. Neville-Neil 
1122ade9ccfeSMarcel Moolenaar 	p(txrequests, "{:sent-requests/%ju} {N:/ARP request%s sent}\n");
1123b25d74e0SBjoern A. Zeeb 	p(txerrors, "{:sent-failures/%ju} {N:/ARP request%s failed to sent}\n");
1124ade9ccfeSMarcel Moolenaar 	p2(txreplies, "{:sent-replies/%ju} {N:/ARP repl%s sent}\n");
1125ade9ccfeSMarcel Moolenaar 	p(rxrequests, "{:received-requests/%ju} "
1126ade9ccfeSMarcel Moolenaar 	    "{N:/ARP request%s received}\n");
1127ade9ccfeSMarcel Moolenaar 	p2(rxreplies, "{:received-replies/%ju} "
1128ade9ccfeSMarcel Moolenaar 	    "{N:/ARP repl%s received}\n");
1129c5b8d7b7SStefan Eßer 	p(received, "{:received-packets/%ju} "
1130ade9ccfeSMarcel Moolenaar 	    "{N:/ARP packet%s received}\n");
1131ade9ccfeSMarcel Moolenaar 	p(dropped, "{:dropped-no-entry/%ju} "
1132ade9ccfeSMarcel Moolenaar 	    "{N:/total packet%s dropped due to no ARP entry}\n");
1133ade9ccfeSMarcel Moolenaar 	p(timeouts, "{:entries-timeout/%ju} "
1134ade9ccfeSMarcel Moolenaar 	    "{N:/ARP entry%s timed out}\n");
1135ade9ccfeSMarcel Moolenaar 	p(dupips, "{:dropped-duplicate-address/%ju} "
1136ade9ccfeSMarcel Moolenaar 	    "{N:/Duplicate IP%s seen}\n");
113754fc657dSGeorge V. Neville-Neil #undef p
113854fc657dSGeorge V. Neville-Neil #undef p2
1139ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
114054fc657dSGeorge V. Neville-Neil }
114154fc657dSGeorge V. Neville-Neil 
114254fc657dSGeorge V. Neville-Neil 
114354fc657dSGeorge V. Neville-Neil 
11444063583aSMaxim Konovalov static	const char *icmpnames[ICMP_MAXTYPE + 1] = {
11454063583aSMaxim Konovalov 	"echo reply",			/* RFC 792 */
11469b50d902SRodney W. Grimes 	"#1",
11479b50d902SRodney W. Grimes 	"#2",
11484063583aSMaxim Konovalov 	"destination unreachable",	/* RFC 792 */
11494063583aSMaxim Konovalov 	"source quench",		/* RFC 792 */
11504063583aSMaxim Konovalov 	"routing redirect",		/* RFC 792 */
11519b50d902SRodney W. Grimes 	"#6",
11529b50d902SRodney W. Grimes 	"#7",
11534063583aSMaxim Konovalov 	"echo",				/* RFC 792 */
11544063583aSMaxim Konovalov 	"router advertisement",		/* RFC 1256 */
11554063583aSMaxim Konovalov 	"router solicitation",		/* RFC 1256 */
11564063583aSMaxim Konovalov 	"time exceeded",		/* RFC 792 */
11574063583aSMaxim Konovalov 	"parameter problem",		/* RFC 792 */
11584063583aSMaxim Konovalov 	"time stamp",			/* RFC 792 */
11594063583aSMaxim Konovalov 	"time stamp reply",		/* RFC 792 */
11604063583aSMaxim Konovalov 	"information request",		/* RFC 792 */
11614063583aSMaxim Konovalov 	"information request reply",	/* RFC 792 */
11624063583aSMaxim Konovalov 	"address mask request",		/* RFC 950 */
11634063583aSMaxim Konovalov 	"address mask reply",		/* RFC 950 */
11644063583aSMaxim Konovalov 	"#19",
11654063583aSMaxim Konovalov 	"#20",
11664063583aSMaxim Konovalov 	"#21",
11674063583aSMaxim Konovalov 	"#22",
11684063583aSMaxim Konovalov 	"#23",
11694063583aSMaxim Konovalov 	"#24",
11704063583aSMaxim Konovalov 	"#25",
11714063583aSMaxim Konovalov 	"#26",
11724063583aSMaxim Konovalov 	"#27",
11734063583aSMaxim Konovalov 	"#28",
11744063583aSMaxim Konovalov 	"#29",
11754063583aSMaxim Konovalov 	"icmp traceroute",		/* RFC 1393 */
11764063583aSMaxim Konovalov 	"datagram conversion error",	/* RFC 1475 */
11774063583aSMaxim Konovalov 	"mobile host redirect",
11784063583aSMaxim Konovalov 	"IPv6 where-are-you",
11794063583aSMaxim Konovalov 	"IPv6 i-am-here",
11804063583aSMaxim Konovalov 	"mobile registration req",
11814063583aSMaxim Konovalov 	"mobile registration reply",
11824063583aSMaxim Konovalov 	"domain name request",		/* RFC 1788 */
11834063583aSMaxim Konovalov 	"domain name reply",		/* RFC 1788 */
11844063583aSMaxim Konovalov 	"icmp SKIP",
11854063583aSMaxim Konovalov 	"icmp photuris",		/* RFC 2521 */
11869b50d902SRodney W. Grimes };
11879b50d902SRodney W. Grimes 
11889b50d902SRodney W. Grimes /*
11899b50d902SRodney W. Grimes  * Dump ICMP statistics.
11909b50d902SRodney W. Grimes  */
11919b50d902SRodney W. Grimes void
icmp_stats(u_long off,const char * name,int af1 __unused,int proto __unused)1192feda1a43SJohn Baldwin icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
11939b50d902SRodney W. Grimes {
11949eddb899SMark Johnston 	struct icmpstat icmpstat;
11954e00c309SGarrett Wollman 	size_t len;
11969eddb899SMark Johnston 	int i, first;
11979b50d902SRodney W. Grimes 
11989eddb899SMark Johnston 	if (fetch_stats("net.inet.icmp.stats", off, &icmpstat,
11999eddb899SMark Johnston 	    sizeof(icmpstat), kread_counters) != 0)
1200c73d99b5SRuslan Ermilov 		return;
12014e00c309SGarrett Wollman 
1202ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
1203ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
12049b50d902SRodney W. Grimes 
12059b50d902SRodney W. Grimes #define	p(f, m) if (icmpstat.f || sflag <= 1) \
1206ade9ccfeSMarcel Moolenaar 	xo_emit(m, icmpstat.f, plural(icmpstat.f))
120722694ebaSBruce Evans #define	p1a(f, m) if (icmpstat.f || sflag <= 1) \
1208ade9ccfeSMarcel Moolenaar 	xo_emit(m, icmpstat.f)
1209bd714208SRuslan Ermilov #define	p2(f, m) if (icmpstat.f || sflag <= 1) \
1210ade9ccfeSMarcel Moolenaar 	xo_emit(m, icmpstat.f, plurales(icmpstat.f))
12119b50d902SRodney W. Grimes 
1212ade9ccfeSMarcel Moolenaar 	p(icps_error, "\t{:icmp-calls/%lu} "
1213ade9ccfeSMarcel Moolenaar 	    "{N:/call%s to icmp_error}\n");
1214ade9ccfeSMarcel Moolenaar 	p(icps_oldicmp, "\t{:errors-not-from-message/%lu} "
1215ade9ccfeSMarcel Moolenaar 	    "{N:/error%s not generated in response to an icmp message}\n");
1216ade9ccfeSMarcel Moolenaar 
1217ade9ccfeSMarcel Moolenaar 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) {
12189b50d902SRodney W. Grimes 		if (icmpstat.icps_outhist[i] != 0) {
12199b50d902SRodney W. Grimes 			if (first) {
1220ade9ccfeSMarcel Moolenaar 				xo_open_list("output-histogram");
1221ade9ccfeSMarcel Moolenaar 				xo_emit("\tOutput histogram:\n");
12229b50d902SRodney W. Grimes 				first = 0;
12239b50d902SRodney W. Grimes 			}
1224ade9ccfeSMarcel Moolenaar 			xo_open_instance("output-histogram");
12254063583aSMaxim Konovalov 			if (icmpnames[i] != NULL)
1226ade9ccfeSMarcel Moolenaar 				xo_emit("\t\t{k:name/%s}: {:count/%lu}\n",
1227ade9ccfeSMarcel Moolenaar 				    icmpnames[i], icmpstat.icps_outhist[i]);
12284063583aSMaxim Konovalov 			else
1229ade9ccfeSMarcel Moolenaar 				xo_emit("\t\tunknown ICMP #{k:name/%d}: "
1230ade9ccfeSMarcel Moolenaar 				    "{:count/%lu}\n",
1231ade9ccfeSMarcel Moolenaar 				    i, icmpstat.icps_outhist[i]);
1232ade9ccfeSMarcel Moolenaar 			xo_close_instance("output-histogram");
12339b50d902SRodney W. Grimes 		}
1234ade9ccfeSMarcel Moolenaar 	}
1235ade9ccfeSMarcel Moolenaar 	if (!first)
1236ade9ccfeSMarcel Moolenaar 		xo_close_list("output-histogram");
1237ade9ccfeSMarcel Moolenaar 
1238ade9ccfeSMarcel Moolenaar 	p(icps_badcode, "\t{:dropped-bad-code/%lu} "
1239ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with bad code fields}\n");
1240ade9ccfeSMarcel Moolenaar 	p(icps_tooshort, "\t{:dropped-too-short/%lu} "
1241ade9ccfeSMarcel Moolenaar 	    "{N:/message%s less than the minimum length}\n");
1242ade9ccfeSMarcel Moolenaar 	p(icps_checksum, "\t{:dropped-bad-checksum/%lu} "
1243ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with bad checksum}\n");
1244ade9ccfeSMarcel Moolenaar 	p(icps_badlen, "\t{:dropped-bad-length/%lu} "
1245ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with bad length}\n");
1246ade9ccfeSMarcel Moolenaar 	p1a(icps_bmcastecho, "\t{:dropped-multicast-echo/%lu} "
1247ade9ccfeSMarcel Moolenaar 	    "{N:/multicast echo requests ignored}\n");
1248ade9ccfeSMarcel Moolenaar 	p1a(icps_bmcasttstamp, "\t{:dropped-multicast-timestamp/%lu} "
1249ade9ccfeSMarcel Moolenaar 	    "{N:/multicast timestamp requests ignored}\n");
1250ade9ccfeSMarcel Moolenaar 
1251ade9ccfeSMarcel Moolenaar 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) {
12529b50d902SRodney W. Grimes 		if (icmpstat.icps_inhist[i] != 0) {
12539b50d902SRodney W. Grimes 			if (first) {
1254ade9ccfeSMarcel Moolenaar 				xo_open_list("input-histogram");
1255ade9ccfeSMarcel Moolenaar 				xo_emit("\tInput histogram:\n");
12569b50d902SRodney W. Grimes 				first = 0;
12579b50d902SRodney W. Grimes 			}
1258ade9ccfeSMarcel Moolenaar 			xo_open_instance("input-histogram");
12594063583aSMaxim Konovalov 			if (icmpnames[i] != NULL)
1260ade9ccfeSMarcel Moolenaar 				xo_emit("\t\t{k:name/%s}: {:count/%lu}\n",
1261ade9ccfeSMarcel Moolenaar 					icmpnames[i],
12629b50d902SRodney W. Grimes 					icmpstat.icps_inhist[i]);
12634063583aSMaxim Konovalov 			else
1264ade9ccfeSMarcel Moolenaar 				xo_emit(
1265ade9ccfeSMarcel Moolenaar 			"\t\tunknown ICMP #{k:name/%d}: {:count/%lu}\n",
1266ade9ccfeSMarcel Moolenaar 					i, icmpstat.icps_inhist[i]);
1267ade9ccfeSMarcel Moolenaar 			xo_close_instance("input-histogram");
12689b50d902SRodney W. Grimes 		}
1269ade9ccfeSMarcel Moolenaar 	}
1270ade9ccfeSMarcel Moolenaar 	if (!first)
1271ade9ccfeSMarcel Moolenaar 		xo_close_list("input-histogram");
1272ade9ccfeSMarcel Moolenaar 
1273ade9ccfeSMarcel Moolenaar 	p(icps_reflect, "\t{:sent-packets/%lu} "
1274ade9ccfeSMarcel Moolenaar 	    "{N:/message response%s generated}\n");
1275ade9ccfeSMarcel Moolenaar 	p2(icps_badaddr, "\t{:discard-invalid-return-address/%lu} "
1276ade9ccfeSMarcel Moolenaar 	    "{N:/invalid return address%s}\n");
1277ade9ccfeSMarcel Moolenaar 	p(icps_noroute, "\t{:discard-no-route/%lu} "
1278ade9ccfeSMarcel Moolenaar 	    "{N:/no return route%s}\n");
12799b50d902SRodney W. Grimes #undef p
128022694ebaSBruce Evans #undef p1a
1281bd714208SRuslan Ermilov #undef p2
1282feda1a43SJohn Baldwin 	if (live) {
12834e00c309SGarrett Wollman 		len = sizeof i;
1284feda1a43SJohn Baldwin 		if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) <
1285feda1a43SJohn Baldwin 		    0)
12864e00c309SGarrett Wollman 			return;
1287ade9ccfeSMarcel Moolenaar 		xo_emit("\tICMP address mask responses are "
1288ade9ccfeSMarcel Moolenaar 		    "{q:icmp-address-responses/%sabled}\n", i ? "en" : "dis");
12899b50d902SRodney W. Grimes 	}
1290ade9ccfeSMarcel Moolenaar 
1291ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
1292feda1a43SJohn Baldwin }
12939b50d902SRodney W. Grimes 
1294d10910e6SBruce M Simpson /*
1295d10910e6SBruce M Simpson  * Dump IGMP statistics structure.
1296d10910e6SBruce M Simpson  */
1297d10910e6SBruce M Simpson void
igmp_stats(u_long off,const char * name,int af1 __unused,int proto __unused)1298d10910e6SBruce M Simpson igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1299d10910e6SBruce M Simpson {
13009eddb899SMark Johnston 	struct igmpstat igmpstat;
1301d726e633SHiroki Sato 	int error, zflag0;
1302d10910e6SBruce M Simpson 
13039eddb899SMark Johnston 	if (fetch_stats("net.inet.igmp.stats", 0, &igmpstat,
13049eddb899SMark Johnston 	    sizeof(igmpstat), kread) != 0)
1305d10910e6SBruce M Simpson 		return;
1306d726e633SHiroki Sato 	/*
1307d726e633SHiroki Sato 	 * Reread net.inet.igmp.stats when zflag == 1.
1308d726e633SHiroki Sato 	 * This is because this MIB contains version number and
1309d726e633SHiroki Sato 	 * length of the structure which are not set when clearing
1310d726e633SHiroki Sato 	 * the counters.
1311d726e633SHiroki Sato 	 */
1312d726e633SHiroki Sato 	zflag0 = zflag;
1313d726e633SHiroki Sato 	if (zflag) {
1314d726e633SHiroki Sato 		zflag = 0;
1315d726e633SHiroki Sato 		error = fetch_stats("net.inet.igmp.stats", 0, &igmpstat,
1316d726e633SHiroki Sato 		    sizeof(igmpstat), kread);
1317d726e633SHiroki Sato 		zflag = zflag0;
1318d726e633SHiroki Sato 		if (error)
1319d726e633SHiroki Sato 			return;
1320d726e633SHiroki Sato 	}
1321d10910e6SBruce M Simpson 
1322d10910e6SBruce M Simpson 	if (igmpstat.igps_version != IGPS_VERSION_3) {
1323ade9ccfeSMarcel Moolenaar 		xo_warnx("%s: version mismatch (%d != %d)", __func__,
1324d10910e6SBruce M Simpson 		    igmpstat.igps_version, IGPS_VERSION_3);
13254df8c942SBjoern A. Zeeb 		return;
1326d10910e6SBruce M Simpson 	}
1327d10910e6SBruce M Simpson 	if (igmpstat.igps_len != IGPS_VERSION3_LEN) {
1328ade9ccfeSMarcel Moolenaar 		xo_warnx("%s: size mismatch (%d != %d)", __func__,
1329d10910e6SBruce M Simpson 		    igmpstat.igps_len, IGPS_VERSION3_LEN);
13304df8c942SBjoern A. Zeeb 		return;
1331d10910e6SBruce M Simpson 	}
1332d10910e6SBruce M Simpson 
1333ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
1334ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
1335d10910e6SBruce M Simpson 
1336d10910e6SBruce M Simpson #define	p64(f, m) if (igmpstat.f || sflag <= 1) \
1337ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t) igmpstat.f, plural(igmpstat.f))
1338d10910e6SBruce M Simpson #define	py64(f, m) if (igmpstat.f || sflag <= 1) \
1339ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t) igmpstat.f, pluralies(igmpstat.f))
1340ade9ccfeSMarcel Moolenaar 
1341ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_total, "\t{:received-messages/%ju} "
1342ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received}\n");
1343ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_tooshort, "\t{:dropped-too-short/%ju} "
1344ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with too few bytes}\n");
1345ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_badttl, "\t{:dropped-wrong-ttl/%ju} "
1346ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with wrong TTL}\n");
1347ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_badsum, "\t{:dropped-bad-checksum/%ju} "
1348ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with bad checksum}\n");
1349ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_v1v2_queries, "\t{:received-membership-queries/%ju} "
1350ade9ccfeSMarcel Moolenaar 	    "{N:/V1\\/V2 membership quer%s received}\n");
1351ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_v3_queries, "\t{:received-v3-membership-queries/%ju} "
1352ade9ccfeSMarcel Moolenaar 	    "{N:/V3 membership quer%s received}\n");
1353ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_badqueries, "\t{:dropped-membership-queries/%ju} "
1354ade9ccfeSMarcel Moolenaar 	    "{N:/membership quer%s received with invalid field(s)}\n");
1355ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_gen_queries, "\t{:received-general-queries/%ju} "
1356ade9ccfeSMarcel Moolenaar 	    "{N:/general quer%s received}\n");
1357ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_group_queries, "\t{:received-group-queries/%ju} "
1358ade9ccfeSMarcel Moolenaar 	    "{N:/group quer%s received}\n");
1359ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_gsr_queries, "\t{:received-group-source-queries/%ju} "
1360ade9ccfeSMarcel Moolenaar 	    "{N:/group-source quer%s received}\n");
1361ade9ccfeSMarcel Moolenaar 	py64(igps_drop_gsr_queries, "\t{:dropped-group-source-queries/%ju} "
1362ade9ccfeSMarcel Moolenaar 	    "{N:/group-source quer%s dropped}\n");
1363ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_reports, "\t{:received-membership-requests/%ju} "
1364ade9ccfeSMarcel Moolenaar 	    "{N:/membership report%s received}\n");
1365ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_badreports, "\t{:dropped-membership-reports/%ju} "
1366ade9ccfeSMarcel Moolenaar 	    "{N:/membership report%s received with invalid field(s)}\n");
1367ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_ourreports, "\t"
1368ade9ccfeSMarcel Moolenaar 	    "{:received-membership-reports-matching/%ju} "
1369ade9ccfeSMarcel Moolenaar 	    "{N:/membership report%s received for groups to which we belong}"
1370ade9ccfeSMarcel Moolenaar 	    "\n");
1371ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_nora, "\t{:received-v3-reports-no-router-alert/%ju} "
1372ade9ccfeSMarcel Moolenaar 	    "{N:/V3 report%s received without Router Alert}\n");
1373ade9ccfeSMarcel Moolenaar 	p64(igps_snd_reports, "\t{:sent-membership-reports/%ju} "
1374ade9ccfeSMarcel Moolenaar 	    "{N:/membership report%s sent}\n");
1375d10910e6SBruce M Simpson #undef p64
1376d10910e6SBruce M Simpson #undef py64
1377ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
1378d10910e6SBruce M Simpson }
13799b50d902SRodney W. Grimes 
13809b50d902SRodney W. Grimes /*
1381c7b9b5bbSJeffrey Hsu  * Dump PIM statistics structure.
1382c7b9b5bbSJeffrey Hsu  */
1383c7b9b5bbSJeffrey Hsu void
pim_stats(u_long off __unused,const char * name,int af1 __unused,int proto __unused)1384feda1a43SJohn Baldwin pim_stats(u_long off __unused, const char *name, int af1 __unused,
1385feda1a43SJohn Baldwin     int proto __unused)
1386c7b9b5bbSJeffrey Hsu {
13879eddb899SMark Johnston 	struct pimstat pimstat;
1388c7b9b5bbSJeffrey Hsu 
13899eddb899SMark Johnston 	if (fetch_stats("net.inet.pim.stats", off, &pimstat,
13909eddb899SMark Johnston 	    sizeof(pimstat), kread_counters) != 0)
1391c7b9b5bbSJeffrey Hsu 		return;
1392c7b9b5bbSJeffrey Hsu 
1393ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
1394ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
1395c7b9b5bbSJeffrey Hsu 
1396c7b9b5bbSJeffrey Hsu #define	p(f, m) if (pimstat.f || sflag <= 1) \
1397ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t)pimstat.f, plural(pimstat.f))
1398c7b9b5bbSJeffrey Hsu #define	py(f, m) if (pimstat.f || sflag <= 1) \
1399ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
1400ade9ccfeSMarcel Moolenaar 
1401ade9ccfeSMarcel Moolenaar 	p(pims_rcv_total_msgs, "\t{:received-messages/%ju} "
1402ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received}\n");
1403ade9ccfeSMarcel Moolenaar 	p(pims_rcv_total_bytes, "\t{:received-bytes/%ju} "
1404ade9ccfeSMarcel Moolenaar 	    "{N:/byte%s received}\n");
1405ade9ccfeSMarcel Moolenaar 	p(pims_rcv_tooshort, "\t{:dropped-too-short/%ju} "
1406ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with too few bytes}\n");
1407ade9ccfeSMarcel Moolenaar 	p(pims_rcv_badsum, "\t{:dropped-bad-checksum/%ju} "
1408ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with bad checksum}\n");
1409ade9ccfeSMarcel Moolenaar 	p(pims_rcv_badversion, "\t{:dropped-bad-version/%ju} "
1410ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with bad version}\n");
1411ade9ccfeSMarcel Moolenaar 	p(pims_rcv_registers_msgs, "\t{:received-data-register-messages/%ju} "
1412ade9ccfeSMarcel Moolenaar 	    "{N:/data register message%s received}\n");
1413ade9ccfeSMarcel Moolenaar 	p(pims_rcv_registers_bytes, "\t{:received-data-register-bytes/%ju} "
1414ade9ccfeSMarcel Moolenaar 	    "{N:/data register byte%s received}\n");
1415ade9ccfeSMarcel Moolenaar 	p(pims_rcv_registers_wrongiif, "\t"
1416ade9ccfeSMarcel Moolenaar 	    "{:received-data-register-wrong-interface/%ju} "
1417ade9ccfeSMarcel Moolenaar 	    "{N:/data register message%s received on wrong iif}\n");
1418ade9ccfeSMarcel Moolenaar 	p(pims_rcv_badregisters, "\t{:received-bad-registers/%ju} "
1419ade9ccfeSMarcel Moolenaar 	    "{N:/bad register%s received}\n");
1420ade9ccfeSMarcel Moolenaar 	p(pims_snd_registers_msgs, "\t{:sent-data-register-messages/%ju} "
1421ade9ccfeSMarcel Moolenaar 	    "{N:/data register message%s sent}\n");
1422ade9ccfeSMarcel Moolenaar 	p(pims_snd_registers_bytes, "\t{:sent-data-register-bytes/%ju} "
1423ade9ccfeSMarcel Moolenaar 	    "{N:/data register byte%s sent}\n");
1424c7b9b5bbSJeffrey Hsu #undef p
1425c7b9b5bbSJeffrey Hsu #undef py
1426ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
1427c7b9b5bbSJeffrey Hsu }
1428c7b9b5bbSJeffrey Hsu 
14292b1c7217SGleb Smirnoff /*
14302b1c7217SGleb Smirnoff  * Dump divert(4) statistics structure.
14312b1c7217SGleb Smirnoff  */
14322b1c7217SGleb Smirnoff void
divert_stats(u_long off,const char * name,int af1 __unused,int proto __unused)14332b1c7217SGleb Smirnoff divert_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
14342b1c7217SGleb Smirnoff {
14352b1c7217SGleb Smirnoff 	struct divstat divstat;
14362b1c7217SGleb Smirnoff 
14372b1c7217SGleb Smirnoff 	if (fetch_stats("net.inet.divert.stats", off, &divstat,
14382b1c7217SGleb Smirnoff 	    sizeof(divstat), kread_counters) != 0)
14392b1c7217SGleb Smirnoff 		return;
14402b1c7217SGleb Smirnoff 
14412b1c7217SGleb Smirnoff 	xo_open_container(name);
14422b1c7217SGleb Smirnoff 	xo_emit("{T:/%s}:\n", name);
14432b1c7217SGleb Smirnoff 
14442b1c7217SGleb Smirnoff #define	p(f, m) if (divstat.f || sflag <= 1) \
14452b1c7217SGleb Smirnoff 	xo_emit(m, (uintmax_t)divstat.f, plural(divstat.f))
14462b1c7217SGleb Smirnoff 
14472b1c7217SGleb Smirnoff 	p(div_diverted, "\t{:diverted-packets/%ju} "
14482b1c7217SGleb Smirnoff 	    "{N:/packet%s successfully diverted to userland}\n");
14492b1c7217SGleb Smirnoff 	p(div_noport, "\t{:noport-fails/%ju} "
14502b1c7217SGleb Smirnoff 	    "{N:/packet%s failed to divert due to no socket bound at port}\n");
14512b1c7217SGleb Smirnoff 	p(div_outbound, "\t{:outbound-packets/%ju} "
14522b1c7217SGleb Smirnoff 	    "{N:/packet%s successfully re-injected as outbound}\n");
14532b1c7217SGleb Smirnoff 	p(div_inbound, "\t{:inbound-packets/%ju} "
14542b1c7217SGleb Smirnoff 	    "{N:/packet%s successfully re-injected as inbound}\n");
14552b1c7217SGleb Smirnoff #undef p
14562b1c7217SGleb Smirnoff 	xo_close_container(name);
14572b1c7217SGleb Smirnoff }
14582b1c7217SGleb Smirnoff 
14599784899dSBjoern A. Zeeb #ifdef INET
1460c7b9b5bbSJeffrey Hsu /*
14619b50d902SRodney W. Grimes  * Pretty print an Internet address (net address + port).
14629b50d902SRodney W. Grimes  */
14639784899dSBjoern A. Zeeb static void
inetprint(const char * container,struct in_addr * in,int port,const char * proto,int num_port,const int af1)1464ade9ccfeSMarcel Moolenaar inetprint(const char *container, struct in_addr *in, int port,
1465046aad39SHajimu UMEMOTO     const char *proto, int num_port, const int af1)
14669b50d902SRodney W. Grimes {
14679b50d902SRodney W. Grimes 	struct servent *sp = 0;
14689b50d902SRodney W. Grimes 	char line[80], *cp;
1469cfa1ca9dSYoshinobu Inoue 	int width;
1470f193c8ceSXin LI 	size_t alen, plen;
14719b50d902SRodney W. Grimes 
1472ade9ccfeSMarcel Moolenaar 	if (container)
1473ade9ccfeSMarcel Moolenaar 		xo_open_container(container);
1474ade9ccfeSMarcel Moolenaar 
1475080b7f49SDag-Erling Smørgrav 	if (Wflag)
1476f193c8ceSXin LI 	    snprintf(line, sizeof(line), "%s.", inetname(in));
1477080b7f49SDag-Erling Smørgrav 	else
1478f193c8ceSXin LI 	    snprintf(line, sizeof(line), "%.*s.",
1479f193c8ceSXin LI 		(Aflag && !num_port) ? 12 : 16, inetname(in));
1480f193c8ceSXin LI 	alen = strlen(line);
1481f193c8ceSXin LI 	cp = line + alen;
1482a01e3379SDavid Malone 	if (!num_port && port)
14839b50d902SRodney W. Grimes 		sp = getservbyport((int)port, proto);
14849b50d902SRodney W. Grimes 	if (sp || port == 0)
1485f193c8ceSXin LI 		snprintf(cp, sizeof(line) - alen,
1486f193c8ceSXin LI 		    "%.15s ", sp ? sp->s_name : "*");
14879b50d902SRodney W. Grimes 	else
1488f193c8ceSXin LI 		snprintf(cp, sizeof(line) - alen,
1489f193c8ceSXin LI 		    "%d ", ntohs((u_short)port));
1490046aad39SHajimu UMEMOTO 	width = (Aflag && !Wflag) ? 18 :
1491046aad39SHajimu UMEMOTO 		((!Wflag || af1 == AF_INET) ? 22 : 45);
1492080b7f49SDag-Erling Smørgrav 	if (Wflag)
1493ade9ccfeSMarcel Moolenaar 		xo_emit("{d:target/%-*s} ", width, line);
1494080b7f49SDag-Erling Smørgrav 	else
1495ade9ccfeSMarcel Moolenaar 		xo_emit("{d:target/%-*.*s} ", width, width, line);
1496ade9ccfeSMarcel Moolenaar 
1497f193c8ceSXin LI 	plen = strlen(cp) - 1;
1498f193c8ceSXin LI 	alen--;
1499ade9ccfeSMarcel Moolenaar 	xo_emit("{e:address/%*.*s}{e:port/%*.*s}", alen, alen, line, plen,
1500ade9ccfeSMarcel Moolenaar 	    plen, cp);
1501ade9ccfeSMarcel Moolenaar 
1502ade9ccfeSMarcel Moolenaar 	if (container)
1503ade9ccfeSMarcel Moolenaar 		xo_close_container(container);
15049b50d902SRodney W. Grimes }
15059b50d902SRodney W. Grimes 
15069b50d902SRodney W. Grimes /*
15079b50d902SRodney W. Grimes  * Construct an Internet address representation.
150865ea0024SAssar Westerlund  * If numeric_addr has been supplied, give
15099b50d902SRodney W. Grimes  * numeric value, otherwise try for symbolic name.
15109b50d902SRodney W. Grimes  */
15119b50d902SRodney W. Grimes char *
inetname(struct in_addr * inp)15125e051718SAssar Westerlund inetname(struct in_addr *inp)
15139b50d902SRodney W. Grimes {
1514a01e3379SDavid Malone 	char *cp;
1515d121b556SBrian Somers 	static char line[MAXHOSTNAMELEN];
15169b50d902SRodney W. Grimes 	struct hostent *hp;
15179b50d902SRodney W. Grimes 
15189b50d902SRodney W. Grimes 	cp = 0;
151965ea0024SAssar Westerlund 	if (!numeric_addr && inp->s_addr != INADDR_ANY) {
15209b50d902SRodney W. Grimes 		hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
15219b50d902SRodney W. Grimes 		if (hp) {
15229b50d902SRodney W. Grimes 			cp = hp->h_name;
1523d121b556SBrian Somers 			trimdomain(cp, strlen(cp));
15249b50d902SRodney W. Grimes 		}
15259b50d902SRodney W. Grimes 	}
15269b50d902SRodney W. Grimes 	if (inp->s_addr == INADDR_ANY)
15279b50d902SRodney W. Grimes 		strcpy(line, "*");
15289a1f6729SWarner Losh 	else if (cp) {
15291c109628SXin LI 		strlcpy(line, cp, sizeof(line));
15309a1f6729SWarner Losh 	} else {
15319b50d902SRodney W. Grimes 		inp->s_addr = ntohl(inp->s_addr);
153222694ebaSBruce Evans #define	C(x)	((u_int)((x) & 0xff))
1533f193c8ceSXin LI 		snprintf(line, sizeof(line), "%u.%u.%u.%u",
1534f193c8ceSXin LI 		    C(inp->s_addr >> 24), C(inp->s_addr >> 16),
1535f193c8ceSXin LI 		    C(inp->s_addr >> 8), C(inp->s_addr));
15369b50d902SRodney W. Grimes 	}
15379b50d902SRodney W. Grimes 	return (line);
15389b50d902SRodney W. Grimes }
15399784899dSBjoern A. Zeeb #endif
1540