xref: /freebsd/usr.bin/netstat/inet.c (revision 779f106aa169256b7010a1d8f963ff656b881e92)
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 
306cc6f122SPhilippe Charnier #if 0
319b50d902SRodney W. Grimes #ifndef lint
3205ddff6eSPeter Wemm static char sccsid[] = "@(#)inet.c	8.5 (Berkeley) 5/24/95";
339b50d902SRodney W. Grimes #endif /* not lint */
346cc6f122SPhilippe Charnier #endif
356cc6f122SPhilippe Charnier 
366cc6f122SPhilippe Charnier #include <sys/cdefs.h>
376cc6f122SPhilippe Charnier __FBSDID("$FreeBSD$");
389b50d902SRodney W. Grimes 
399b50d902SRodney W. Grimes #include <sys/param.h>
40d8d89152SDavid Greenman #include <sys/queue.h>
41feda1a43SJohn Baldwin #include <sys/domain.h>
42f35a2092SMaksim Yevmenkin #include <sys/protosw.h>
439b50d902SRodney W. Grimes #include <sys/socket.h>
449b50d902SRodney W. Grimes #include <sys/socketvar.h>
454e00c309SGarrett Wollman #include <sys/sysctl.h>
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes #include <net/route.h>
4854fc657dSGeorge V. Neville-Neil #include <net/if_arp.h>
499b50d902SRodney W. Grimes #include <netinet/in.h>
509b50d902SRodney W. Grimes #include <netinet/in_systm.h>
519b50d902SRodney W. Grimes #include <netinet/ip.h>
52a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
53cfa1ca9dSYoshinobu Inoue #ifdef INET6
54cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
55cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
569b50d902SRodney W. Grimes #include <netinet/in_pcb.h>
579b50d902SRodney W. Grimes #include <netinet/ip_icmp.h>
589b50d902SRodney W. Grimes #include <netinet/icmp_var.h>
599b50d902SRodney W. Grimes #include <netinet/igmp_var.h>
609b50d902SRodney W. Grimes #include <netinet/ip_var.h>
61c7b9b5bbSJeffrey Hsu #include <netinet/pim_var.h>
629b50d902SRodney W. Grimes #include <netinet/tcp.h>
639b50d902SRodney W. Grimes #include <netinet/tcpip.h>
649b50d902SRodney W. Grimes #include <netinet/tcp_seq.h>
659b50d902SRodney W. Grimes #define	TCPSTATES
669b50d902SRodney W. Grimes #include <netinet/tcp_fsm.h>
679b50d902SRodney W. Grimes #include <netinet/tcp_timer.h>
689b50d902SRodney W. Grimes #include <netinet/tcp_var.h>
699b50d902SRodney W. Grimes #include <netinet/udp.h>
709b50d902SRodney W. Grimes #include <netinet/udp_var.h>
719b50d902SRodney W. Grimes 
729b50d902SRodney W. Grimes #include <arpa/inet.h>
734f81ef50SGarrett Wollman #include <err.h>
744f81ef50SGarrett Wollman #include <errno.h>
75d121b556SBrian Somers #include <libutil.h>
769b50d902SRodney W. Grimes #include <netdb.h>
777b95a1ebSYaroslav Tykhiy #include <stdint.h>
789b50d902SRodney W. Grimes #include <stdio.h>
794f81ef50SGarrett Wollman #include <stdlib.h>
80ade9ccfeSMarcel Moolenaar #include <stdbool.h>
819b50d902SRodney W. Grimes #include <string.h>
829b50d902SRodney W. Grimes #include <unistd.h>
83ade9ccfeSMarcel Moolenaar #include <libxo/xo.h>
849b50d902SRodney W. Grimes #include "netstat.h"
85dbfd8708SGleb Smirnoff #include "nl_defs.h"
869b50d902SRodney W. Grimes 
87046aad39SHajimu UMEMOTO void	inetprint(const char *, struct in_addr *, int, const char *, int,
88046aad39SHajimu UMEMOTO     const int);
89cfa1ca9dSYoshinobu Inoue #ifdef INET6
90aa0a1e58SJeff Roberson static int udp_done, tcp_done, sdp_done;
91cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
929b50d902SRodney W. Grimes 
93feda1a43SJohn Baldwin static int
94cc65eb4eSGleb Smirnoff pcblist_sysctl(int proto, const char *name, char **bufp)
95feda1a43SJohn Baldwin {
96feda1a43SJohn Baldwin 	const char *mibvar;
97feda1a43SJohn Baldwin 	char *buf;
98feda1a43SJohn Baldwin 	size_t len;
99feda1a43SJohn Baldwin 
100feda1a43SJohn Baldwin 	switch (proto) {
101feda1a43SJohn Baldwin 	case IPPROTO_TCP:
102feda1a43SJohn Baldwin 		mibvar = "net.inet.tcp.pcblist";
103feda1a43SJohn Baldwin 		break;
104feda1a43SJohn Baldwin 	case IPPROTO_UDP:
105feda1a43SJohn Baldwin 		mibvar = "net.inet.udp.pcblist";
106feda1a43SJohn Baldwin 		break;
107feda1a43SJohn Baldwin 	case IPPROTO_DIVERT:
108feda1a43SJohn Baldwin 		mibvar = "net.inet.divert.pcblist";
109feda1a43SJohn Baldwin 		break;
110feda1a43SJohn Baldwin 	default:
111feda1a43SJohn Baldwin 		mibvar = "net.inet.raw.pcblist";
112feda1a43SJohn Baldwin 		break;
113feda1a43SJohn Baldwin 	}
114aa0a1e58SJeff Roberson 	if (strncmp(name, "sdp", 3) == 0)
115aa0a1e58SJeff Roberson 		mibvar = "net.inet.sdp.pcblist";
116feda1a43SJohn Baldwin 	len = 0;
117feda1a43SJohn Baldwin 	if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
118feda1a43SJohn Baldwin 		if (errno != ENOENT)
119ade9ccfeSMarcel Moolenaar 			xo_warn("sysctl: %s", mibvar);
120feda1a43SJohn Baldwin 		return (0);
121feda1a43SJohn Baldwin 	}
122ef1cb629SMarcelo Araujo 	if ((buf = malloc(len)) == NULL) {
123ade9ccfeSMarcel Moolenaar 		xo_warnx("malloc %lu bytes", (u_long)len);
124feda1a43SJohn Baldwin 		return (0);
125feda1a43SJohn Baldwin 	}
126feda1a43SJohn Baldwin 	if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
127ade9ccfeSMarcel Moolenaar 		xo_warn("sysctl: %s", mibvar);
128feda1a43SJohn Baldwin 		free(buf);
129feda1a43SJohn Baldwin 		return (0);
130feda1a43SJohn Baldwin 	}
131feda1a43SJohn Baldwin 	*bufp = buf;
132feda1a43SJohn Baldwin 	return (1);
133feda1a43SJohn Baldwin }
134feda1a43SJohn Baldwin 
135feda1a43SJohn Baldwin /*
136feda1a43SJohn Baldwin  * Copied directly from uipc_socket2.c.  We leave out some fields that are in
137feda1a43SJohn Baldwin  * nested structures that aren't used to avoid extra work.
138feda1a43SJohn Baldwin  */
139feda1a43SJohn Baldwin static void
140feda1a43SJohn Baldwin sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
141feda1a43SJohn Baldwin {
1420f9d0a73SGleb Smirnoff 	xsb->sb_cc = sb->sb_ccc;
143feda1a43SJohn Baldwin 	xsb->sb_hiwat = sb->sb_hiwat;
144feda1a43SJohn Baldwin 	xsb->sb_mbcnt = sb->sb_mbcnt;
14549f287f8SGeorge V. Neville-Neil 	xsb->sb_mcnt = sb->sb_mcnt;
14649f287f8SGeorge V. Neville-Neil 	xsb->sb_ccnt = sb->sb_ccnt;
147feda1a43SJohn Baldwin 	xsb->sb_mbmax = sb->sb_mbmax;
148feda1a43SJohn Baldwin 	xsb->sb_lowat = sb->sb_lowat;
149feda1a43SJohn Baldwin 	xsb->sb_flags = sb->sb_flags;
150feda1a43SJohn Baldwin 	xsb->sb_timeo = sb->sb_timeo;
151feda1a43SJohn Baldwin }
152feda1a43SJohn Baldwin 
153feda1a43SJohn Baldwin int
154feda1a43SJohn Baldwin sotoxsocket(struct socket *so, struct xsocket *xso)
155feda1a43SJohn Baldwin {
156feda1a43SJohn Baldwin 	struct protosw proto;
157feda1a43SJohn Baldwin 	struct domain domain;
158feda1a43SJohn Baldwin 
159feda1a43SJohn Baldwin 	bzero(xso, sizeof *xso);
160feda1a43SJohn Baldwin 	xso->xso_len = sizeof *xso;
161feda1a43SJohn Baldwin 	xso->xso_so = so;
162feda1a43SJohn Baldwin 	xso->so_type = so->so_type;
163feda1a43SJohn Baldwin 	xso->so_options = so->so_options;
164feda1a43SJohn Baldwin 	xso->so_linger = so->so_linger;
165feda1a43SJohn Baldwin 	xso->so_state = so->so_state;
166feda1a43SJohn Baldwin 	xso->so_pcb = so->so_pcb;
167feda1a43SJohn Baldwin 	if (kread((uintptr_t)so->so_proto, &proto, sizeof(proto)) != 0)
168feda1a43SJohn Baldwin 		return (-1);
169feda1a43SJohn Baldwin 	xso->xso_protocol = proto.pr_protocol;
170feda1a43SJohn Baldwin 	if (kread((uintptr_t)proto.pr_domain, &domain, sizeof(domain)) != 0)
171feda1a43SJohn Baldwin 		return (-1);
172feda1a43SJohn Baldwin 	xso->xso_family = domain.dom_family;
173feda1a43SJohn Baldwin 	xso->so_timeo = so->so_timeo;
174feda1a43SJohn Baldwin 	xso->so_error = so->so_error;
175*779f106aSGleb Smirnoff 	if (SOLISTENING(so)) {
176*779f106aSGleb Smirnoff 		xso->so_qlen = so->sol_qlen;
177*779f106aSGleb Smirnoff 		xso->so_incqlen = so->sol_incqlen;
178*779f106aSGleb Smirnoff 		xso->so_qlimit = so->sol_qlimit;
179*779f106aSGleb Smirnoff 	} else {
180feda1a43SJohn Baldwin 		sbtoxsockbuf(&so->so_snd, &xso->so_snd);
181feda1a43SJohn Baldwin 		sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
182*779f106aSGleb Smirnoff 		xso->so_oobmark = so->so_oobmark;
183*779f106aSGleb Smirnoff 	}
184feda1a43SJohn Baldwin 	return (0);
185feda1a43SJohn Baldwin }
186feda1a43SJohn Baldwin 
1879b50d902SRodney W. Grimes /*
1889b50d902SRodney W. Grimes  * Print a summary of connections related to an Internet
1899b50d902SRodney W. Grimes  * protocol.  For TCP, also give state of connection.
1909b50d902SRodney W. Grimes  * Listening processes (aflag) are suppressed unless the
1919b50d902SRodney W. Grimes  * -a (all) flag is specified.
1929b50d902SRodney W. Grimes  */
1939b50d902SRodney W. Grimes void
194feda1a43SJohn Baldwin protopr(u_long off, const char *name, int af1, int proto)
1959b50d902SRodney W. Grimes {
1969b50d902SRodney W. Grimes 	static int first = 1;
197cc65eb4eSGleb Smirnoff 	int istcp;
1984f81ef50SGarrett Wollman 	char *buf;
199feda1a43SJohn Baldwin 	const char *vchar;
200cc65eb4eSGleb Smirnoff 	struct xtcpcb *tp;
201cc65eb4eSGleb Smirnoff 	struct xinpcb *inp;
2024f81ef50SGarrett Wollman 	struct xinpgen *xig, *oxig;
2034f81ef50SGarrett Wollman 	struct xsocket *so;
2049b50d902SRodney W. Grimes 
2054f81ef50SGarrett Wollman 	istcp = 0;
2064f81ef50SGarrett Wollman 	switch (proto) {
2074f81ef50SGarrett Wollman 	case IPPROTO_TCP:
208cfa1ca9dSYoshinobu Inoue #ifdef INET6
209aa0a1e58SJeff Roberson 		if (strncmp(name, "sdp", 3) != 0) {
210cfa1ca9dSYoshinobu Inoue 			if (tcp_done != 0)
211cfa1ca9dSYoshinobu Inoue 				return;
212cfa1ca9dSYoshinobu Inoue 			else
213cfa1ca9dSYoshinobu Inoue 				tcp_done = 1;
214aa0a1e58SJeff Roberson 		} else {
215aa0a1e58SJeff Roberson 			if (sdp_done != 0)
216aa0a1e58SJeff Roberson 				return;
217aa0a1e58SJeff Roberson 			else
218aa0a1e58SJeff Roberson 				sdp_done = 1;
219aa0a1e58SJeff Roberson 		}
220cfa1ca9dSYoshinobu Inoue #endif
2214f81ef50SGarrett Wollman 		istcp = 1;
2224f81ef50SGarrett Wollman 		break;
2234f81ef50SGarrett Wollman 	case IPPROTO_UDP:
224cfa1ca9dSYoshinobu Inoue #ifdef INET6
225cfa1ca9dSYoshinobu Inoue 		if (udp_done != 0)
226cfa1ca9dSYoshinobu Inoue 			return;
227cfa1ca9dSYoshinobu Inoue 		else
228cfa1ca9dSYoshinobu Inoue 			udp_done = 1;
229cfa1ca9dSYoshinobu Inoue #endif
2304f81ef50SGarrett Wollman 		break;
2314f81ef50SGarrett Wollman 	}
232cc65eb4eSGleb Smirnoff 
233cc65eb4eSGleb Smirnoff 	if (!pcblist_sysctl(proto, name, &buf))
2349b50d902SRodney W. Grimes 		return;
2354f81ef50SGarrett Wollman 
2364f81ef50SGarrett Wollman 	oxig = xig = (struct xinpgen *)buf;
2374f81ef50SGarrett Wollman 	for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
2384f81ef50SGarrett Wollman 	    xig->xig_len > sizeof(struct xinpgen);
2394f81ef50SGarrett Wollman 	    xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
2409b50d902SRodney W. Grimes 		if (istcp) {
241cc65eb4eSGleb Smirnoff 			tp = (struct xtcpcb *)xig;
242cc65eb4eSGleb Smirnoff 			inp = &tp->xt_inp;
2434f81ef50SGarrett Wollman 		} else {
244cc65eb4eSGleb Smirnoff 			inp = (struct xinpcb *)xig;
2459b50d902SRodney W. Grimes 		}
246cc65eb4eSGleb Smirnoff 		so = &inp->xi_socket;
2474f81ef50SGarrett Wollman 
2484f81ef50SGarrett Wollman 		/* Ignore sockets for protocols other than the desired one. */
249feda1a43SJohn Baldwin 		if (so->xso_protocol != proto)
2504f81ef50SGarrett Wollman 			continue;
2514f81ef50SGarrett Wollman 
2524f81ef50SGarrett Wollman 		/* Ignore PCBs which were freed during copyout. */
2534f81ef50SGarrett Wollman 		if (inp->inp_gencnt > oxig->xig_gen)
2544f81ef50SGarrett Wollman 			continue;
2554f81ef50SGarrett Wollman 
256a01e3379SDavid Malone 		if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
257cfa1ca9dSYoshinobu Inoue #ifdef INET6
258a01e3379SDavid Malone 		    || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
259cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
260a01e3379SDavid Malone 		    || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
261cfa1ca9dSYoshinobu Inoue #ifdef INET6
2623feeb332SDavid E. O'Brien 					  && (inp->inp_vflag & INP_IPV6) == 0
263cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
264cfa1ca9dSYoshinobu Inoue 			))
265cfa1ca9dSYoshinobu Inoue 		    )
266cfa1ca9dSYoshinobu Inoue 			continue;
267cfa1ca9dSYoshinobu Inoue 		if (!aflag &&
268cfa1ca9dSYoshinobu Inoue 		    (
2692b286cedSBruce M Simpson 		     (istcp && tp->t_state == TCPS_LISTEN)
2702b286cedSBruce M Simpson 		     || (af1 == AF_INET &&
271cfa1ca9dSYoshinobu Inoue 		      inet_lnaof(inp->inp_laddr) == INADDR_ANY)
272cfa1ca9dSYoshinobu Inoue #ifdef INET6
273a01e3379SDavid Malone 		     || (af1 == AF_INET6 &&
274cfa1ca9dSYoshinobu Inoue 			 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
275cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
276a01e3379SDavid Malone 		     || (af1 == AF_UNSPEC &&
277cfa1ca9dSYoshinobu Inoue 			 (((inp->inp_vflag & INP_IPV4) != 0 &&
278cfa1ca9dSYoshinobu Inoue 			   inet_lnaof(inp->inp_laddr) == INADDR_ANY)
279cfa1ca9dSYoshinobu Inoue #ifdef INET6
280cfa1ca9dSYoshinobu Inoue 			  || ((inp->inp_vflag & INP_IPV6) != 0 &&
281cfa1ca9dSYoshinobu Inoue 			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
282cfa1ca9dSYoshinobu Inoue #endif
283cfa1ca9dSYoshinobu Inoue 			  ))
284cfa1ca9dSYoshinobu Inoue 		     ))
2854f81ef50SGarrett Wollman 			continue;
2864f81ef50SGarrett Wollman 
2879b50d902SRodney W. Grimes 		if (first) {
288ac55add0SGuido van Rooij 			if (!Lflag) {
289ade9ccfeSMarcel Moolenaar 				xo_emit("Active Internet connections");
2909b50d902SRodney W. Grimes 				if (aflag)
291ade9ccfeSMarcel Moolenaar 					xo_emit(" (including servers)");
292ac55add0SGuido van Rooij 			} else
293ade9ccfeSMarcel Moolenaar 				xo_emit(
294ac55add0SGuido van Rooij 	"Current listen queue sizes (qlen/incqlen/maxqlen)");
295ade9ccfeSMarcel Moolenaar 			xo_emit("\n");
2969b50d902SRodney W. Grimes 			if (Aflag)
297ade9ccfeSMarcel Moolenaar 				xo_emit("{T:/%-*s} ", 2 * (int)sizeof(void *),
298ade9ccfeSMarcel Moolenaar 				    "Tcpcb");
299ac55add0SGuido van Rooij 			if (Lflag)
300ade9ccfeSMarcel Moolenaar 				xo_emit((Aflag && !Wflag) ?
301db2627f4SMichael Tuexen 				    "{T:/%-5.5s} {T:/%-32.32s} {T:/%-18.18s}" :
302046aad39SHajimu UMEMOTO 				    ((!Wflag || af1 == AF_INET) ?
303db2627f4SMichael Tuexen 				    "{T:/%-5.5s} {T:/%-32.32s} {T:/%-22.22s}" :
304db2627f4SMichael Tuexen 				    "{T:/%-5.5s} {T:/%-32.32s} {T:/%-45.45s}"),
305afc74015SRuslan Ermilov 				    "Proto", "Listen", "Local Address");
306afc74015SRuslan Ermilov 			else if (Tflag)
307ade9ccfeSMarcel Moolenaar 				xo_emit((Aflag && !Wflag) ?
308ade9ccfeSMarcel Moolenaar     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%s}" :
309046aad39SHajimu UMEMOTO 				    ((!Wflag || af1 == AF_INET) ?
310046aad39SHajimu UMEMOTO     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%s}" :
311046aad39SHajimu UMEMOTO     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%s}"),
312f5d34df5SGeorge V. Neville-Neil 				    "Proto", "Rexmit", "OOORcv", "0-win",
31349f287f8SGeorge V. Neville-Neil 				    "Local Address", "Foreign Address");
314afc74015SRuslan Ermilov 			else {
315ade9ccfeSMarcel Moolenaar 				xo_emit((Aflag && !Wflag) ?
316ade9ccfeSMarcel Moolenaar     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%-18.18s}" :
317046aad39SHajimu UMEMOTO 				    ((!Wflag || af1 == AF_INET) ?
318046aad39SHajimu UMEMOTO     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%-22.22s}" :
319046aad39SHajimu UMEMOTO     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%-45.45s}"),
320afc74015SRuslan Ermilov 				    "Proto", "Recv-Q", "Send-Q",
321afc74015SRuslan Ermilov 				    "Local Address", "Foreign Address");
32285b0f0f3SAdrian Chadd 				if (!xflag && !Rflag)
323ade9ccfeSMarcel Moolenaar 					xo_emit(" (state)");
324afc74015SRuslan Ermilov 			}
325b8614722SMike Silbersack 			if (xflag) {
326ade9ccfeSMarcel Moolenaar 				xo_emit(" {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
327ade9ccfeSMarcel Moolenaar 				    "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
328ade9ccfeSMarcel Moolenaar 				    "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
329ade9ccfeSMarcel Moolenaar 				    "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s}",
330ade9ccfeSMarcel Moolenaar 				    "R-MBUF", "S-MBUF", "R-CLUS", "S-CLUS",
331ade9ccfeSMarcel Moolenaar 				    "R-HIWA", "S-HIWA", "R-LOWA", "S-LOWA",
332ade9ccfeSMarcel Moolenaar 				    "R-BCNT", "S-BCNT", "R-BMAX", "S-BMAX");
333ade9ccfeSMarcel Moolenaar 				xo_emit(" {T:/%7.7s} {T:/%7.7s} {T:/%7.7s} "
334ade9ccfeSMarcel Moolenaar 				    "{T:/%7.7s} {T:/%7.7s} {T:/%7.7s}",
335ade9ccfeSMarcel Moolenaar 				    "rexmt", "persist", "keep", "2msl",
336ade9ccfeSMarcel Moolenaar 				    "delack", "rcvtime");
33785b0f0f3SAdrian Chadd 			} else if (Rflag) {
338ade9ccfeSMarcel Moolenaar 				xo_emit("  {T:/%8.8s} {T:/%5.5s}",
33985b0f0f3SAdrian Chadd 				    "flowid", "ftype");
34094f138feSGeorge V. Neville-Neil 			}
341ade9ccfeSMarcel Moolenaar 			xo_emit("\n");
3429b50d902SRodney W. Grimes 			first = 0;
3439b50d902SRodney W. Grimes 		}
344fb5d0fbdSRuslan Ermilov 		if (Lflag && so->so_qlimit == 0)
345fb5d0fbdSRuslan Ermilov 			continue;
346ade9ccfeSMarcel Moolenaar 		xo_open_instance("socket");
347e4ec3989SDag-Erling Smørgrav 		if (Aflag) {
348e4ec3989SDag-Erling Smørgrav 			if (istcp)
349ade9ccfeSMarcel Moolenaar 				xo_emit("{q:address/%*lx} ",
350ade9ccfeSMarcel Moolenaar 				    2 * (int)sizeof(void *),
351ade9ccfeSMarcel Moolenaar 				    (u_long)inp->inp_ppcb);
352e4ec3989SDag-Erling Smørgrav 			else
353d9646465SGleb Smirnoff 				xo_emit("{q:address/%*lx} ",
354ade9ccfeSMarcel Moolenaar 				    2 * (int)sizeof(void *),
355ade9ccfeSMarcel Moolenaar 				    (u_long)so->so_pcb);
356e4ec3989SDag-Erling Smørgrav 		}
357cfa1ca9dSYoshinobu Inoue #ifdef INET6
358fc60ab7bSYoshinobu Inoue 		if ((inp->inp_vflag & INP_IPV6) != 0)
3593feeb332SDavid E. O'Brien 			vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
3603feeb332SDavid E. O'Brien 			    "46" : "6";
361fc60ab7bSYoshinobu Inoue 		else
362cfa1ca9dSYoshinobu Inoue #endif
3633feeb332SDavid E. O'Brien 		vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
3643feeb332SDavid E. O'Brien 		    "4" : "";
36509fe6320SNavdeep Parhar 		if (istcp && (tp->t_flags & TF_TOE) != 0)
366ade9ccfeSMarcel Moolenaar 			xo_emit("{:protocol/%-3.3s%-2.2s/%s%s} ", "toe", vchar);
36709fe6320SNavdeep Parhar 		else
368ade9ccfeSMarcel Moolenaar 			xo_emit("{:protocol/%-3.3s%-2.2s/%s%s} ", name, vchar);
369fb5d0fbdSRuslan Ermilov 		if (Lflag) {
3707325dfbbSAlfred Perlstein 			char buf1[33];
371fc60ab7bSYoshinobu Inoue 
3727325dfbbSAlfred Perlstein 			snprintf(buf1, sizeof buf1, "%u/%u/%u", so->so_qlen,
373fb5d0fbdSRuslan Ermilov 			    so->so_incqlen, so->so_qlimit);
3747325dfbbSAlfred Perlstein 			xo_emit("{:listen-queue-sizes/%-32.32s} ", buf1);
375f5d34df5SGeorge V. Neville-Neil 		} else if (Tflag) {
376f5d34df5SGeorge V. Neville-Neil 			if (istcp)
377ade9ccfeSMarcel Moolenaar 				xo_emit("{:sent-retransmit-packets/%6u} "
378ade9ccfeSMarcel Moolenaar 				    "{:received-out-of-order-packets/%6u} "
379ade9ccfeSMarcel Moolenaar 				    "{:sent-zero-window/%6u} ",
380ade9ccfeSMarcel Moolenaar 				    tp->t_sndrexmitpack, tp->t_rcvoopack,
381ade9ccfeSMarcel Moolenaar 				    tp->t_sndzerowin);
3820eaa116eSHajimu UMEMOTO 			else
3830eaa116eSHajimu UMEMOTO 				xo_emit("{P:/%21s}", "");
384fb5d0fbdSRuslan Ermilov 		} else {
385ade9ccfeSMarcel Moolenaar 			xo_emit("{:receive-bytes-waiting/%6u} "
386ade9ccfeSMarcel Moolenaar 			    "{:send-bytes-waiting/%6u} ",
3870f9d0a73SGleb Smirnoff 			    so->so_rcv.sb_cc, so->so_snd.sb_cc);
388fc60ab7bSYoshinobu Inoue 		}
38965ea0024SAssar Westerlund 		if (numeric_port) {
390cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
391ade9ccfeSMarcel Moolenaar 				inetprint("local", &inp->inp_laddr,
392046aad39SHajimu UMEMOTO 				    (int)inp->inp_lport, name, 1, af1);
393ac55add0SGuido van Rooij 				if (!Lflag)
394ade9ccfeSMarcel Moolenaar 					inetprint("remote", &inp->inp_faddr,
395046aad39SHajimu UMEMOTO 					    (int)inp->inp_fport, name, 1, af1);
396cfa1ca9dSYoshinobu Inoue 			}
397cfa1ca9dSYoshinobu Inoue #ifdef INET6
398cfa1ca9dSYoshinobu Inoue 			else if (inp->inp_vflag & INP_IPV6) {
399ade9ccfeSMarcel Moolenaar 				inet6print("local", &inp->in6p_laddr,
400cfa1ca9dSYoshinobu Inoue 				    (int)inp->inp_lport, name, 1);
401ac55add0SGuido van Rooij 				if (!Lflag)
402ade9ccfeSMarcel Moolenaar 					inet6print("remote", &inp->in6p_faddr,
403cfa1ca9dSYoshinobu Inoue 					    (int)inp->inp_fport, name, 1);
404cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
405cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
4064f81ef50SGarrett Wollman 		} else if (inp->inp_flags & INP_ANONPORT) {
407cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
408ade9ccfeSMarcel Moolenaar 				inetprint("local", &inp->inp_laddr,
409046aad39SHajimu UMEMOTO 				    (int)inp->inp_lport, name, 1, af1);
410ac55add0SGuido van Rooij 				if (!Lflag)
411ade9ccfeSMarcel Moolenaar 					inetprint("remote", &inp->inp_faddr,
412046aad39SHajimu UMEMOTO 					    (int)inp->inp_fport, name, 0, af1);
413cfa1ca9dSYoshinobu Inoue 			}
414cfa1ca9dSYoshinobu Inoue #ifdef INET6
415cfa1ca9dSYoshinobu Inoue 			else if (inp->inp_vflag & INP_IPV6) {
416ade9ccfeSMarcel Moolenaar 				inet6print("local", &inp->in6p_laddr,
417cfa1ca9dSYoshinobu Inoue 				    (int)inp->inp_lport, name, 1);
418ac55add0SGuido van Rooij 				if (!Lflag)
419ade9ccfeSMarcel Moolenaar 					inet6print("remote", &inp->in6p_faddr,
420cfa1ca9dSYoshinobu Inoue 					    (int)inp->inp_fport, name, 0);
421cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
422cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
4238d612dd2SPoul-Henning Kamp 		} else {
424cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
425ade9ccfeSMarcel Moolenaar 				inetprint("local", &inp->inp_laddr,
426046aad39SHajimu UMEMOTO 				    (int)inp->inp_lport, name, 0, af1);
427ac55add0SGuido van Rooij 				if (!Lflag)
428ade9ccfeSMarcel Moolenaar 					inetprint("remote", &inp->inp_faddr,
429ac55add0SGuido van Rooij 					    (int)inp->inp_fport, name,
430046aad39SHajimu UMEMOTO 					    inp->inp_lport != inp->inp_fport,
431046aad39SHajimu UMEMOTO 					    af1);
432cfa1ca9dSYoshinobu Inoue 			}
433cfa1ca9dSYoshinobu Inoue #ifdef INET6
434cfa1ca9dSYoshinobu Inoue 			else if (inp->inp_vflag & INP_IPV6) {
435ade9ccfeSMarcel Moolenaar 				inet6print("local", &inp->in6p_laddr,
436cfa1ca9dSYoshinobu Inoue 				    (int)inp->inp_lport, name, 0);
437ac55add0SGuido van Rooij 				if (!Lflag)
438ade9ccfeSMarcel Moolenaar 					inet6print("remote", &inp->in6p_faddr,
439cfa1ca9dSYoshinobu Inoue 					    (int)inp->inp_fport, name,
4403feeb332SDavid E. O'Brien 					    inp->inp_lport != inp->inp_fport);
441cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
442cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
4438d612dd2SPoul-Henning Kamp 		}
44449f287f8SGeorge V. Neville-Neil 		if (xflag) {
445ade9ccfeSMarcel Moolenaar 			xo_emit("{:receive-mbufs/%6u} {:send-mbufs/%6u} "
446ade9ccfeSMarcel Moolenaar 			    "{:receive-clusters/%6u} {:send-clusters/%6u} "
447ade9ccfeSMarcel Moolenaar 			    "{:receive-high-water/%6u} {:send-high-water/%6u} "
448ade9ccfeSMarcel Moolenaar 			    "{:receive-low-water/%6u} {:send-low-water/%6u} "
449ade9ccfeSMarcel Moolenaar 			    "{:receive-mbuf-bytes/%6u} {:send-mbuf-bytes/%6u} "
450ade9ccfeSMarcel Moolenaar 			    "{:receive-mbuf-bytes-max/%6u} "
451ade9ccfeSMarcel Moolenaar 			    "{:send-mbuf-bytes-max/%6u}",
45249f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt,
45349f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt,
45449f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
45549f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
45649f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
45749f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
458cc65eb4eSGleb Smirnoff 			if (istcp)
459ade9ccfeSMarcel Moolenaar 				xo_emit(" {:retransmit-timer/%4d.%02d} "
460ade9ccfeSMarcel Moolenaar 				    "{:persist-timer/%4d.%02d} "
461ade9ccfeSMarcel Moolenaar 				    "{:keepalive-timer/%4d.%02d} "
462ade9ccfeSMarcel Moolenaar 				    "{:msl2-timer/%4d.%02d} "
463ade9ccfeSMarcel Moolenaar 				    "{:delay-ack-timer/%4d.%02d} "
464ade9ccfeSMarcel Moolenaar 				    "{:inactivity-timer/%4d.%02d}",
465cc65eb4eSGleb Smirnoff 				    tp->tt_rexmt / 1000,
466cc65eb4eSGleb Smirnoff 				    (tp->tt_rexmt % 1000) / 10,
467cc65eb4eSGleb Smirnoff 				    tp->tt_persist / 1000,
468cc65eb4eSGleb Smirnoff 				    (tp->tt_persist % 1000) / 10,
469cc65eb4eSGleb Smirnoff 				    tp->tt_keep / 1000,
470cc65eb4eSGleb Smirnoff 				    (tp->tt_keep % 1000) / 10,
471cc65eb4eSGleb Smirnoff 				    tp->tt_2msl / 1000,
472cc65eb4eSGleb Smirnoff 				    (tp->tt_2msl % 1000) / 10,
473cc65eb4eSGleb Smirnoff 				    tp->tt_delack / 1000,
474cc65eb4eSGleb Smirnoff 				    (tp->tt_delack % 1000) / 10,
475cc65eb4eSGleb Smirnoff 				    tp->t_rcvtime / 1000,
476cc65eb4eSGleb Smirnoff 				    (tp->t_rcvtime % 1000) / 10);
477b8614722SMike Silbersack 		}
47885b0f0f3SAdrian Chadd 		if (istcp && !Lflag && !xflag && !Tflag && !Rflag) {
4794f81ef50SGarrett Wollman 			if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
480ade9ccfeSMarcel Moolenaar 				xo_emit("{:tcp-state/%d}", tp->t_state);
4819a94a597SGarrett Wollman 			else {
482ade9ccfeSMarcel Moolenaar 				xo_emit("{:tcp-state/%s}",
483ade9ccfeSMarcel Moolenaar 				    tcpstates[tp->t_state]);
484513822ddSAdam David #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
4859a94a597SGarrett Wollman 				/* Show T/TCP `hidden state' */
4864f81ef50SGarrett Wollman 				if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
487ade9ccfeSMarcel Moolenaar 					xo_emit("{:need-syn-or-fin/*}");
488513822ddSAdam David #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
4899a94a597SGarrett Wollman 			}
4909b50d902SRodney W. Grimes 		}
49185b0f0f3SAdrian Chadd 		if (Rflag) {
492ade9ccfeSMarcel Moolenaar 			/* XXX: is this right Alfred */
493ade9ccfeSMarcel Moolenaar 			xo_emit(" {:flow-id/%08x} {:flow-type/%5d}",
49485b0f0f3SAdrian Chadd 			    inp->inp_flowid,
49585b0f0f3SAdrian Chadd 			    inp->inp_flowtype);
49685b0f0f3SAdrian Chadd 		}
497ade9ccfeSMarcel Moolenaar 		xo_emit("\n");
498ade9ccfeSMarcel Moolenaar 		xo_close_instance("socket");
4999b50d902SRodney W. Grimes 	}
5004f81ef50SGarrett Wollman 	if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
5014f81ef50SGarrett Wollman 		if (oxig->xig_count > xig->xig_count) {
502ade9ccfeSMarcel Moolenaar 			xo_emit("Some {d:lost/%s} sockets may have been "
503ade9ccfeSMarcel Moolenaar 			    "deleted.\n", name);
5044f81ef50SGarrett Wollman 		} else if (oxig->xig_count < xig->xig_count) {
505ade9ccfeSMarcel Moolenaar 			xo_emit("Some {d:created/%s} sockets may have been "
506ade9ccfeSMarcel Moolenaar 			    "created.\n", name);
5074f81ef50SGarrett Wollman 		} else {
508ade9ccfeSMarcel Moolenaar 			xo_emit("Some {d:changed/%s} sockets may have been "
509ade9ccfeSMarcel Moolenaar 			    "created or deleted.\n", name);
5104f81ef50SGarrett Wollman 		}
5114f81ef50SGarrett Wollman 	}
5124f81ef50SGarrett Wollman 	free(buf);
5139b50d902SRodney W. Grimes }
5149b50d902SRodney W. Grimes 
5159b50d902SRodney W. Grimes /*
5169b50d902SRodney W. Grimes  * Dump TCP statistics structure.
5179b50d902SRodney W. Grimes  */
5189b50d902SRodney W. Grimes void
519feda1a43SJohn Baldwin tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
5209b50d902SRodney W. Grimes {
5219eddb899SMark Johnston 	struct tcpstat tcpstat;
522dbfd8708SGleb Smirnoff 	uint64_t tcps_states[TCP_NSTATES];
5239b50d902SRodney W. Grimes 
524cfa1ca9dSYoshinobu Inoue #ifdef INET6
525cfa1ca9dSYoshinobu Inoue 	if (tcp_done != 0)
526cfa1ca9dSYoshinobu Inoue 		return;
527cfa1ca9dSYoshinobu Inoue 	else
528cfa1ca9dSYoshinobu Inoue 		tcp_done = 1;
529cfa1ca9dSYoshinobu Inoue #endif
530cfa1ca9dSYoshinobu Inoue 
5319eddb899SMark Johnston 	if (fetch_stats("net.inet.tcp.stats", off, &tcpstat,
5329eddb899SMark Johnston 	    sizeof(tcpstat), kread_counters) != 0)
533feda1a43SJohn Baldwin 		return;
534feda1a43SJohn Baldwin 
535dbfd8708SGleb Smirnoff 	if (fetch_stats_ro("net.inet.tcp.states", nl[N_TCPS_STATES].n_value,
536dbfd8708SGleb Smirnoff 	    &tcps_states, sizeof(tcps_states), kread_counters) != 0)
537dbfd8708SGleb Smirnoff 		return;
538dbfd8708SGleb Smirnoff 
539ade9ccfeSMarcel Moolenaar 	xo_open_container("tcp");
540ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
5419b50d902SRodney W. Grimes 
5429b50d902SRodney W. Grimes #define	p(f, m) if (tcpstat.f || sflag <= 1)				\
543ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )tcpstat.f, plural(tcpstat.f))
54422694ebaSBruce Evans #define	p1a(f, m) if (tcpstat.f || sflag <= 1)				\
545ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )tcpstat.f)
5469b50d902SRodney W. Grimes #define	p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1)	\
547ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1),		\
5485923c293SGleb Smirnoff 	    (uintmax_t )tcpstat.f2, plural(tcpstat.f2))
54922694ebaSBruce Evans #define	p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1)	\
550ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1),		\
5515923c293SGleb Smirnoff 	    (uintmax_t )tcpstat.f2)
5529b50d902SRodney W. Grimes #define	p3(f, m) if (tcpstat.f || sflag <= 1)				\
553ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )tcpstat.f, pluralies(tcpstat.f))
5549b50d902SRodney W. Grimes 
555ade9ccfeSMarcel Moolenaar 	p(tcps_sndtotal, "\t{:sent-packets/%ju} {N:/packet%s sent}\n");
556ade9ccfeSMarcel Moolenaar 	p2(tcps_sndpack,tcps_sndbyte, "\t\t{:sent-data-packets/%ju} "
557ade9ccfeSMarcel Moolenaar 	    "{N:/data packet%s} ({:sent-data-bytes/%ju} {N:/byte%s})\n");
558ade9ccfeSMarcel Moolenaar 	p2(tcps_sndrexmitpack, tcps_sndrexmitbyte, "\t\t"
559ade9ccfeSMarcel Moolenaar 	    "{:sent-retransmitted-packets/%ju} {N:/data packet%s} "
560ade9ccfeSMarcel Moolenaar 	    "({:sent-retransmitted-bytes/%ju} {N:/byte%s}) "
561ade9ccfeSMarcel Moolenaar 	    "{N:retransmitted}\n");
562ade9ccfeSMarcel Moolenaar 	p(tcps_sndrexmitbad, "\t\t"
563ade9ccfeSMarcel Moolenaar 	    "{:sent-unnecessary-retransmitted-packets/%ju} "
564ade9ccfeSMarcel Moolenaar 	    "{N:/data packet%s unnecessarily retransmitted}\n");
565ade9ccfeSMarcel Moolenaar 	p(tcps_mturesent, "\t\t{:sent-resends-by-mtu-discovery/%ju} "
566ade9ccfeSMarcel Moolenaar 	    "{N:/resend%s initiated by MTU discovery}\n");
567ade9ccfeSMarcel Moolenaar 	p2a(tcps_sndacks, tcps_delack, "\t\t{:sent-ack-only-packets/%ju} "
568ade9ccfeSMarcel Moolenaar 	    "{N:/ack-only packet%s/} ({:sent-packets-delayed/%ju} "
569ade9ccfeSMarcel Moolenaar 	    "{N:delayed})\n");
570ade9ccfeSMarcel Moolenaar 	p(tcps_sndurg, "\t\t{:sent-urg-only-packets/%ju} "
571ade9ccfeSMarcel Moolenaar 	    "{N:/URG only packet%s}\n");
572ade9ccfeSMarcel Moolenaar 	p(tcps_sndprobe, "\t\t{:sent-window-probe-packets/%ju} "
573ade9ccfeSMarcel Moolenaar 	    "{N:/window probe packet%s}\n");
574ade9ccfeSMarcel Moolenaar 	p(tcps_sndwinup, "\t\t{:sent-window-update-packets/%ju} "
575ade9ccfeSMarcel Moolenaar 	    "{N:/window update packet%s}\n");
576ade9ccfeSMarcel Moolenaar 	p(tcps_sndctrl, "\t\t{:sent-control-packets/%ju} "
577ade9ccfeSMarcel Moolenaar 	    "{N:/control packet%s}\n");
578ade9ccfeSMarcel Moolenaar 	p(tcps_rcvtotal, "\t{:received-packets/%ju} "
579ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s received}\n");
580ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t"
581ade9ccfeSMarcel Moolenaar 	    "{:received-ack-packets/%ju} {N:/ack%s} "
582ade9ccfeSMarcel Moolenaar 	    "{N:(for} {:received-ack-bytes/%ju} {N:/byte%s})\n");
583ade9ccfeSMarcel Moolenaar 	p(tcps_rcvdupack, "\t\t{:received-duplicate-acks/%ju} "
584ade9ccfeSMarcel Moolenaar 	    "{N:/duplicate ack%s}\n");
585ade9ccfeSMarcel Moolenaar 	p(tcps_rcvacktoomuch, "\t\t{:received-acks-for-unsent-data/%ju} "
586ade9ccfeSMarcel Moolenaar 	    "{N:/ack%s for unsent data}\n");
587ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvpack, tcps_rcvbyte, "\t\t"
588ade9ccfeSMarcel Moolenaar 	    "{:received-in-sequence-packets/%ju} {N:/packet%s} "
589ade9ccfeSMarcel Moolenaar 	    "({:received-in-sequence-bytes/%ju} {N:/byte%s}) "
590ade9ccfeSMarcel Moolenaar 	    "{N:received in-sequence}\n");
591ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvduppack, tcps_rcvdupbyte, "\t\t"
592ade9ccfeSMarcel Moolenaar 	    "{:received-completely-duplicate-packets/%ju} "
593ade9ccfeSMarcel Moolenaar 	    "{N:/completely duplicate packet%s} "
594ade9ccfeSMarcel Moolenaar 	    "({:received-completely-duplicate-bytes/%ju} {N:/byte%s})\n");
595ade9ccfeSMarcel Moolenaar 	p(tcps_pawsdrop, "\t\t{:received-old-duplicate-packets/%ju} "
596ade9ccfeSMarcel Moolenaar 	    "{N:/old duplicate packet%s}\n");
597ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte, "\t\t"
598ade9ccfeSMarcel Moolenaar 	    "{:received-some-duplicate-packets/%ju} "
599ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s with some dup. data} "
600ade9ccfeSMarcel Moolenaar 	    "({:received-some-duplicate-bytes/%ju} {N:/byte%s duped/})\n");
601ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvoopack, tcps_rcvoobyte, "\t\t{:received-out-of-order/%ju} "
602ade9ccfeSMarcel Moolenaar 	    "{N:/out-of-order packet%s} "
603ade9ccfeSMarcel Moolenaar 	    "({:received-out-of-order-bytes/%ju} {N:/byte%s})\n");
604ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin, "\t\t"
605ade9ccfeSMarcel Moolenaar 	    "{:received-after-window-packets/%ju} {N:/packet%s} "
606ade9ccfeSMarcel Moolenaar 	    "({:received-after-window-bytes/%ju} {N:/byte%s}) "
607ade9ccfeSMarcel Moolenaar 	    "{N:of data after window}\n");
608ade9ccfeSMarcel Moolenaar 	p(tcps_rcvwinprobe, "\t\t{:received-window-probes/%ju} "
609ade9ccfeSMarcel Moolenaar 	    "{N:/window probe%s}\n");
610ade9ccfeSMarcel Moolenaar 	p(tcps_rcvwinupd, "\t\t{:receive-window-update-packets/%ju} "
611ade9ccfeSMarcel Moolenaar 	    "{N:/window update packet%s}\n");
612ade9ccfeSMarcel Moolenaar 	p(tcps_rcvafterclose, "\t\t{:received-after-close-packets/%ju} "
613ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s received after close}\n");
614ade9ccfeSMarcel Moolenaar 	p(tcps_rcvbadsum, "\t\t{:discard-bad-checksum/%ju} "
615ade9ccfeSMarcel Moolenaar 	    "{N:/discarded for bad checksum%s}\n");
616ade9ccfeSMarcel Moolenaar 	p(tcps_rcvbadoff, "\t\t{:discard-bad-header-offset/%ju} "
617ade9ccfeSMarcel Moolenaar 	    "{N:/discarded for bad header offset field%s}\n");
618ade9ccfeSMarcel Moolenaar 	p1a(tcps_rcvshort, "\t\t{:discard-too-short/%ju} "
619ade9ccfeSMarcel Moolenaar 	    "{N:discarded because packet too short}\n");
620ade9ccfeSMarcel Moolenaar 	p1a(tcps_rcvmemdrop, "\t\t{:discard-memory-problems/%ju} "
621ade9ccfeSMarcel Moolenaar 	    "{N:discarded due to memory problems}\n");
622ade9ccfeSMarcel Moolenaar 	p(tcps_connattempt, "\t{:connection-requests/%ju} "
623ade9ccfeSMarcel Moolenaar 	    "{N:/connection request%s}\n");
624ade9ccfeSMarcel Moolenaar 	p(tcps_accepts, "\t{:connections-accepts/%ju} "
625ade9ccfeSMarcel Moolenaar 	    "{N:/connection accept%s}\n");
626ade9ccfeSMarcel Moolenaar 	p(tcps_badsyn, "\t{:bad-connection-attempts/%ju} "
627ade9ccfeSMarcel Moolenaar 	    "{N:/bad connection attempt%s}\n");
628ade9ccfeSMarcel Moolenaar 	p(tcps_listendrop, "\t{:listen-queue-overflows/%ju} "
629ade9ccfeSMarcel Moolenaar 	    "{N:/listen queue overflow%s}\n");
630ade9ccfeSMarcel Moolenaar 	p(tcps_badrst, "\t{:ignored-in-window-resets/%ju} "
631ade9ccfeSMarcel Moolenaar 	    "{N:/ignored RSTs in the window%s}\n");
632ade9ccfeSMarcel Moolenaar 	p(tcps_connects, "\t{:connections-established/%ju} "
633ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s established (including accepts)}\n");
634e9d2c201SMarcelo Araujo 	p(tcps_usedrtt, "\t\t{:connections-hostcache-rtt/%ju} "
635e9d2c201SMarcelo Araujo 	    "{N:/time%s used RTT from hostcache}\n");
636e9d2c201SMarcelo Araujo 	p(tcps_usedrttvar, "\t\t{:connections-hostcache-rttvar/%ju} "
637e9d2c201SMarcelo Araujo 	    "{N:/time%s used RTT variance from hostcache}\n");
638e9d2c201SMarcelo Araujo 	p(tcps_usedssthresh, "\t\t{:connections-hostcache-ssthresh/%ju} "
639e9d2c201SMarcelo Araujo 	    "{N:/time%s used slow-start threshold from hostcache}\n");
640ade9ccfeSMarcel Moolenaar 	p2(tcps_closed, tcps_drops, "\t{:connections-closed/%ju} "
641ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s closed (including} "
642ade9ccfeSMarcel Moolenaar 	    "{:connection-drops/%ju} {N:/drop%s})\n");
643ade9ccfeSMarcel Moolenaar 	p(tcps_cachedrtt, "\t\t{:connections-updated-rtt-on-close/%ju} "
644ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s updated cached RTT on close}\n");
645ade9ccfeSMarcel Moolenaar 	p(tcps_cachedrttvar, "\t\t"
646ade9ccfeSMarcel Moolenaar 	    "{:connections-updated-variance-on-close/%ju} "
647ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s updated cached RTT variance on close}\n");
648ade9ccfeSMarcel Moolenaar 	p(tcps_cachedssthresh, "\t\t"
649ade9ccfeSMarcel Moolenaar 	    "{:connections-updated-ssthresh-on-close/%ju} "
650ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s updated cached ssthresh on close}\n");
651ade9ccfeSMarcel Moolenaar 	p(tcps_conndrops, "\t{:embryonic-connections-dropped/%ju} "
652ade9ccfeSMarcel Moolenaar 	    "{N:/embryonic connection%s dropped}\n");
653ade9ccfeSMarcel Moolenaar 	p2(tcps_rttupdated, tcps_segstimed, "\t{:segments-updated-rtt/%ju} "
654ade9ccfeSMarcel Moolenaar 	    "{N:/segment%s updated rtt (of} "
655ade9ccfeSMarcel Moolenaar 	    "{:segment-update-attempts/%ju} {N:/attempt%s})\n");
656ade9ccfeSMarcel Moolenaar 	p(tcps_rexmttimeo, "\t{:retransmit-timeouts/%ju} "
657ade9ccfeSMarcel Moolenaar 	    "{N:/retransmit timeout%s}\n");
658ade9ccfeSMarcel Moolenaar 	p(tcps_timeoutdrop, "\t\t"
659ade9ccfeSMarcel Moolenaar 	    "{:connections-dropped-by-retransmit-timeout/%ju} "
660ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s dropped by rexmit timeout}\n");
661ade9ccfeSMarcel Moolenaar 	p(tcps_persisttimeo, "\t{:persist-timeout/%ju} "
662ade9ccfeSMarcel Moolenaar 	    "{N:/persist timeout%s}\n");
663ade9ccfeSMarcel Moolenaar 	p(tcps_persistdrop, "\t\t"
664ade9ccfeSMarcel Moolenaar 	    "{:connections-dropped-by-persist-timeout/%ju} "
665ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s dropped by persist timeout}\n");
666ade9ccfeSMarcel Moolenaar 	p(tcps_finwait2_drops, "\t"
667ade9ccfeSMarcel Moolenaar 	    "{:connections-dropped-by-finwait2-timeout/%ju} "
668ade9ccfeSMarcel Moolenaar 	    "{N:/Connection%s (fin_wait_2) dropped because of timeout}\n");
669ade9ccfeSMarcel Moolenaar 	p(tcps_keeptimeo, "\t{:keepalive-timeout/%ju} "
670ade9ccfeSMarcel Moolenaar 	    "{N:/keepalive timeout%s}\n");
671ade9ccfeSMarcel Moolenaar 	p(tcps_keepprobe, "\t\t{:keepalive-probes/%ju} "
672ade9ccfeSMarcel Moolenaar 	    "{N:/keepalive probe%s sent}\n");
673ade9ccfeSMarcel Moolenaar 	p(tcps_keepdrops, "\t\t{:connections-dropped-by-keepalives/%ju} "
674ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s dropped by keepalive}\n");
675ade9ccfeSMarcel Moolenaar 	p(tcps_predack, "\t{:ack-header-predictions/%ju} "
676ade9ccfeSMarcel Moolenaar 	    "{N:/correct ACK header prediction%s}\n");
677ade9ccfeSMarcel Moolenaar 	p(tcps_preddat, "\t{:data-packet-header-predictions/%ju} "
678ade9ccfeSMarcel Moolenaar 	    "{N:/correct data packet header prediction%s}\n");
67960a31b3aSJonathan Lemon 
680ade9ccfeSMarcel Moolenaar 	xo_open_container("syncache");
681b6101dafSPaul Saab 
682ade9ccfeSMarcel Moolenaar 	p3(tcps_sc_added, "\t{:entries-added/%ju} "
683ade9ccfeSMarcel Moolenaar 	    "{N:/syncache entr%s added}\n");
684ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_retransmitted, "\t\t{:retransmitted/%ju} "
685ade9ccfeSMarcel Moolenaar 	    "{N:/retransmitted}\n");
686ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_dupsyn, "\t\t{:duplicates/%ju} {N:/dupsyn}\n");
687ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_dropped, "\t\t{:dropped/%ju} {N:/dropped}\n");
688ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_completed, "\t\t{:completed/%ju} {N:/completed}\n");
689ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_bucketoverflow, "\t\t{:bucket-overflow/%ju} "
690ade9ccfeSMarcel Moolenaar 	    "{N:/bucket overflow}\n");
691ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_cacheoverflow, "\t\t{:cache-overflow/%ju} "
692ade9ccfeSMarcel Moolenaar 	    "{N:/cache overflow}\n");
693ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_reset, "\t\t{:reset/%ju} {N:/reset}\n");
694ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_stale, "\t\t{:stale/%ju} {N:/stale}\n");
695ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_aborted, "\t\t{:aborted/%ju} {N:/aborted}\n");
696ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_badack, "\t\t{:bad-ack/%ju} {N:/badack}\n");
697ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_unreach, "\t\t{:unreachable/%ju} {N:/unreach}\n");
698ade9ccfeSMarcel Moolenaar 	p(tcps_sc_zonefail, "\t\t{:zone-failures/%ju} {N:/zone failure%s}\n");
699ade9ccfeSMarcel Moolenaar 	p(tcps_sc_sendcookie, "\t{:sent-cookies/%ju} {N:/cookie%s sent}\n");
700ade9ccfeSMarcel Moolenaar 	p(tcps_sc_recvcookie, "\t{:receivd-cookies/%ju} "
701ade9ccfeSMarcel Moolenaar 	    "{N:/cookie%s received}\n");
702699ef999SRuslan Ermilov 
703ade9ccfeSMarcel Moolenaar 	xo_close_container("syncache");
704b6101dafSPaul Saab 
705ade9ccfeSMarcel Moolenaar 	xo_open_container("hostcache");
706a4993b25SBjoern A. Zeeb 
707ade9ccfeSMarcel Moolenaar 	p3(tcps_hc_added, "\t{:entries-added/%ju} "
708ade9ccfeSMarcel Moolenaar 	    "{N:/hostcache entr%s added}\n");
709ade9ccfeSMarcel Moolenaar 	p1a(tcps_hc_bucketoverflow, "\t\t{:buffer-overflows/%ju} "
710ade9ccfeSMarcel Moolenaar 	    "{N:/bucket overflow}\n");
711ade9ccfeSMarcel Moolenaar 
712ade9ccfeSMarcel Moolenaar 	xo_close_container("hostcache");
713ade9ccfeSMarcel Moolenaar 
714ade9ccfeSMarcel Moolenaar 	xo_open_container("sack");
715ade9ccfeSMarcel Moolenaar 
716ade9ccfeSMarcel Moolenaar 	p(tcps_sack_recovery_episode, "\t{:recovery-episodes/%ju} "
717ade9ccfeSMarcel Moolenaar 	    "{N:/SACK recovery episode%s}\n");
718ade9ccfeSMarcel Moolenaar  	p(tcps_sack_rexmits, "\t{:segment-retransmits/%ju} "
719ade9ccfeSMarcel Moolenaar 	    "{N:/segment rexmit%s in SACK recovery episodes}\n");
720ade9ccfeSMarcel Moolenaar  	p(tcps_sack_rexmit_bytes, "\t{:byte-retransmits/%ju} "
721ade9ccfeSMarcel Moolenaar 	    "{N:/byte rexmit%s in SACK recovery episodes}\n");
722ade9ccfeSMarcel Moolenaar  	p(tcps_sack_rcv_blocks, "\t{:received-blocks/%ju} "
723ade9ccfeSMarcel Moolenaar 	    "{N:/SACK option%s (SACK blocks) received}\n");
724ade9ccfeSMarcel Moolenaar 	p(tcps_sack_send_blocks, "\t{:sent-option-blocks/%ju} "
725ade9ccfeSMarcel Moolenaar 	    "{N:/SACK option%s (SACK blocks) sent}\n");
726ade9ccfeSMarcel Moolenaar 	p1a(tcps_sack_sboverflow, "\t{:scoreboard-overflows/%ju} "
727ade9ccfeSMarcel Moolenaar 	    "{N:/SACK scoreboard overflow}\n");
728ade9ccfeSMarcel Moolenaar 
729ade9ccfeSMarcel Moolenaar 	xo_close_container("sack");
730ade9ccfeSMarcel Moolenaar 	xo_open_container("ecn");
731ade9ccfeSMarcel Moolenaar 
732ade9ccfeSMarcel Moolenaar 	p(tcps_ecn_ce, "\t{:ce-packets/%ju} "
733ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s with ECN CE bit set}\n");
734ade9ccfeSMarcel Moolenaar 	p(tcps_ecn_ect0, "\t{:ect0-packets/%ju} "
735ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s with ECN ECT(0) bit set}\n");
736ade9ccfeSMarcel Moolenaar 	p(tcps_ecn_ect1, "\t{:ect1-packets/%ju} "
737ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s with ECN ECT(1) bit set}\n");
738ade9ccfeSMarcel Moolenaar 	p(tcps_ecn_shs, "\t{:handshakes/%ju} "
739ade9ccfeSMarcel Moolenaar 	    "{N:/successful ECN handshake%s}\n");
740ade9ccfeSMarcel Moolenaar 	p(tcps_ecn_rcwnd, "\t{:congestion-reductions/%ju} "
741ade9ccfeSMarcel Moolenaar 	    "{N:/time%s ECN reduced the congestion window}\n");
742fcf59617SAndrey V. Elsukov 
743fcf59617SAndrey V. Elsukov 	xo_close_container("ecn");
744fcf59617SAndrey V. Elsukov 	xo_open_container("tcp-signature");
745fcf59617SAndrey V. Elsukov 	p(tcps_sig_rcvgoodsig, "\t{:received-good-signature/%ju} "
746fcf59617SAndrey V. Elsukov 	    "{N:/packet%s with matching signature received}\n");
747fcf59617SAndrey V. Elsukov 	p(tcps_sig_rcvbadsig, "\t{:received-bad-signature/%ju} "
748fcf59617SAndrey V. Elsukov 	    "{N:/packet%s with bad signature received}\n");
749fcf59617SAndrey V. Elsukov 	p(tcps_sig_err_buildsig, "\t{:failed-make-signature/%ju} "
750fcf59617SAndrey V. Elsukov 	    "{N:/time%s failed to make signature due to no SA}\n");
751fcf59617SAndrey V. Elsukov 	p(tcps_sig_err_sigopt, "\t{:no-signature-expected/%ju} "
752fcf59617SAndrey V. Elsukov 	    "{N:/time%s unexpected signature received}\n");
753fcf59617SAndrey V. Elsukov 	p(tcps_sig_err_nosigopt, "\t{:no-signature-provided/%ju} "
754fcf59617SAndrey V. Elsukov 	    "{N:/time%s no signature provided by segment}\n");
7559b50d902SRodney W. Grimes  #undef p
75622694ebaSBruce Evans  #undef p1a
7579b50d902SRodney W. Grimes  #undef p2
75822694ebaSBruce Evans  #undef p2a
7599b50d902SRodney W. Grimes  #undef p3
760fcf59617SAndrey V. Elsukov 	xo_close_container("tcp-signature");
761dbfd8708SGleb Smirnoff 
762dbfd8708SGleb Smirnoff 	xo_open_container("TCP connection count by state");
763dbfd8708SGleb Smirnoff 	xo_emit("{T:/TCP connection count by state}:\n");
764dbfd8708SGleb Smirnoff 	for (int i = 0; i < TCP_NSTATES; i++) {
765dbfd8708SGleb Smirnoff 		/*
766dbfd8708SGleb Smirnoff 		 * XXXGL: is there a way in libxo to use %s
767dbfd8708SGleb Smirnoff 		 * in the "content string" of a format
768dbfd8708SGleb Smirnoff 		 * string? I failed to do that, that's why
769dbfd8708SGleb Smirnoff 		 * a temporary buffer is used to construct
770dbfd8708SGleb Smirnoff 		 * format string for xo_emit().
771dbfd8708SGleb Smirnoff 		 */
772dbfd8708SGleb Smirnoff 		char fmtbuf[80];
773dbfd8708SGleb Smirnoff 
774dbfd8708SGleb Smirnoff 		if (sflag > 1 && tcps_states[i] == 0)
775dbfd8708SGleb Smirnoff 			continue;
776dbfd8708SGleb Smirnoff 		snprintf(fmtbuf, sizeof(fmtbuf), "\t{:%s/%%ju} "
777dbfd8708SGleb Smirnoff                     "{Np:/connection ,connections} in %s state\n",
778dbfd8708SGleb Smirnoff 		    tcpstates[i], tcpstates[i]);
779dbfd8708SGleb Smirnoff 		xo_emit(fmtbuf, (uintmax_t )tcps_states[i]);
780dbfd8708SGleb Smirnoff 	}
781dbfd8708SGleb Smirnoff 	xo_close_container("TCP connection count by state");
782dbfd8708SGleb Smirnoff 
783ade9ccfeSMarcel Moolenaar 	xo_close_container("tcp");
7849b50d902SRodney W. Grimes }
7859b50d902SRodney W. Grimes 
7869b50d902SRodney W. Grimes /*
7879b50d902SRodney W. Grimes  * Dump UDP statistics structure.
7889b50d902SRodney W. Grimes  */
7899b50d902SRodney W. Grimes void
790feda1a43SJohn Baldwin udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
7919b50d902SRodney W. Grimes {
7929eddb899SMark Johnston 	struct udpstat udpstat;
793c80211e3SAndrey V. Elsukov 	uint64_t delivered;
7949b50d902SRodney W. Grimes 
795cfa1ca9dSYoshinobu Inoue #ifdef INET6
796cfa1ca9dSYoshinobu Inoue 	if (udp_done != 0)
797cfa1ca9dSYoshinobu Inoue 		return;
798cfa1ca9dSYoshinobu Inoue 	else
799cfa1ca9dSYoshinobu Inoue 		udp_done = 1;
800cfa1ca9dSYoshinobu Inoue #endif
801cfa1ca9dSYoshinobu Inoue 
8029eddb899SMark Johnston 	if (fetch_stats("net.inet.udp.stats", off, &udpstat,
8039eddb899SMark Johnston 	    sizeof(udpstat), kread_counters) != 0)
804feda1a43SJohn Baldwin 		return;
805feda1a43SJohn Baldwin 
806ade9ccfeSMarcel Moolenaar 	xo_open_container("udp");
807ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
808ade9ccfeSMarcel Moolenaar 
8099b50d902SRodney W. Grimes #define	p(f, m) if (udpstat.f || sflag <= 1) \
810ade9ccfeSMarcel Moolenaar 	xo_emit("\t" m, (uintmax_t)udpstat.f, plural(udpstat.f))
81122694ebaSBruce Evans #define	p1a(f, m) if (udpstat.f || sflag <= 1) \
812ade9ccfeSMarcel Moolenaar 	xo_emit("\t" m, (uintmax_t)udpstat.f)
813ade9ccfeSMarcel Moolenaar 
814ade9ccfeSMarcel Moolenaar 	p(udps_ipackets, "{:received-datagrams/%ju} "
815ade9ccfeSMarcel Moolenaar 	    "{N:/datagram%s received}\n");
816ade9ccfeSMarcel Moolenaar 	p1a(udps_hdrops, "{:dropped-incomplete-headers/%ju} "
817ade9ccfeSMarcel Moolenaar 	    "{N:/with incomplete header}\n");
818ade9ccfeSMarcel Moolenaar 	p1a(udps_badlen, "{:dropped-bad-data-length/%ju} "
819ade9ccfeSMarcel Moolenaar 	    "{N:/with bad data length field}\n");
820ade9ccfeSMarcel Moolenaar 	p1a(udps_badsum, "{:dropped-bad-checksum/%ju} "
821ade9ccfeSMarcel Moolenaar 	    "{N:/with bad checksum}\n");
822ade9ccfeSMarcel Moolenaar 	p1a(udps_nosum, "{:dropped-no-checksum/%ju} "
823ade9ccfeSMarcel Moolenaar 	    "{N:/with no checksum}\n");
824ade9ccfeSMarcel Moolenaar 	p1a(udps_noport, "{:dropped-no-socket/%ju} "
825ade9ccfeSMarcel Moolenaar 	    "{N:/dropped due to no socket}\n");
826ade9ccfeSMarcel Moolenaar 	p(udps_noportbcast, "{:dropped-broadcast-multicast/%ju} "
827ade9ccfeSMarcel Moolenaar 	    "{N:/broadcast\\/multicast datagram%s undelivered}\n");
828ade9ccfeSMarcel Moolenaar 	p1a(udps_fullsock, "{:dropped-full-socket-buffer/%ju} "
829ade9ccfeSMarcel Moolenaar 	    "{N:/dropped due to full socket buffers}\n");
830ade9ccfeSMarcel Moolenaar 	p1a(udpps_pcbhashmiss, "{:not-for-hashed-pcb/%ju} "
831ade9ccfeSMarcel Moolenaar 	    "{N:/not for hashed pcb}\n");
8329b50d902SRodney W. Grimes 	delivered = udpstat.udps_ipackets -
8339b50d902SRodney W. Grimes 		    udpstat.udps_hdrops -
8349b50d902SRodney W. Grimes 		    udpstat.udps_badlen -
8359b50d902SRodney W. Grimes 		    udpstat.udps_badsum -
8369b50d902SRodney W. Grimes 		    udpstat.udps_noport -
8379b50d902SRodney W. Grimes 		    udpstat.udps_noportbcast -
8389b50d902SRodney W. Grimes 		    udpstat.udps_fullsock;
8399b50d902SRodney W. Grimes 	if (delivered || sflag <= 1)
840ade9ccfeSMarcel Moolenaar 		xo_emit("\t{:delivered-packets/%ju} {N:/delivered}\n",
841ade9ccfeSMarcel Moolenaar 		    (uint64_t)delivered);
842ade9ccfeSMarcel Moolenaar 	p(udps_opackets, "{:output-packets/%ju} {N:/datagram%s output}\n");
84371498f30SBruce M Simpson 	/* the next statistic is cumulative in udps_noportbcast */
844ade9ccfeSMarcel Moolenaar 	p(udps_filtermcast, "{:multicast-source-filter-matches/%ju} "
845ade9ccfeSMarcel Moolenaar 	    "{N:/time%s multicast source filter matched}\n");
8469b50d902SRodney W. Grimes #undef p
84722694ebaSBruce Evans #undef p1a
848ade9ccfeSMarcel Moolenaar 	xo_close_container("udp");
8499b50d902SRodney W. Grimes }
8509b50d902SRodney W. Grimes 
8519b50d902SRodney W. Grimes /*
852a9771948SGleb Smirnoff  * Dump CARP statistics structure.
853a9771948SGleb Smirnoff  */
854a9771948SGleb Smirnoff void
855feda1a43SJohn Baldwin carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
856a9771948SGleb Smirnoff {
8579eddb899SMark Johnston 	struct carpstats carpstat;
858a9771948SGleb Smirnoff 
8599eddb899SMark Johnston 	if (fetch_stats("net.inet.carp.stats", off, &carpstat,
8609eddb899SMark Johnston 	    sizeof(carpstat), kread_counters) != 0)
861a9771948SGleb Smirnoff 		return;
862a9771948SGleb Smirnoff 
863ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
864ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
865a9771948SGleb Smirnoff 
866a9771948SGleb Smirnoff #define	p(f, m) if (carpstat.f || sflag <= 1) \
867ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t)carpstat.f, plural(carpstat.f))
868a9771948SGleb Smirnoff #define	p2(f, m) if (carpstat.f || sflag <= 1) \
869ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t)carpstat.f)
870a9771948SGleb Smirnoff 
871ade9ccfeSMarcel Moolenaar 	p(carps_ipackets, "\t{:received-inet-packets/%ju} "
872ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s received (IPv4)}\n");
873ade9ccfeSMarcel Moolenaar 	p(carps_ipackets6, "\t{:received-inet6-packets/%ju} "
874ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s received (IPv6)}\n");
875ade9ccfeSMarcel Moolenaar 	p(carps_badttl, "\t\t{:dropped-wrong-ttl/%ju} "
876ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s discarded for wrong TTL}\n");
877ade9ccfeSMarcel Moolenaar 	p(carps_hdrops, "\t\t{:dropped-short-header/%ju} "
878ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s shorter than header}\n");
879ade9ccfeSMarcel Moolenaar 	p(carps_badsum, "\t\t{:dropped-bad-checksum/%ju} "
880ade9ccfeSMarcel Moolenaar 	    "{N:/discarded for bad checksum%s}\n");
881ade9ccfeSMarcel Moolenaar 	p(carps_badver,	"\t\t{:dropped-bad-version/%ju} "
882ade9ccfeSMarcel Moolenaar 	    "{N:/discarded packet%s with a bad version}\n");
883ade9ccfeSMarcel Moolenaar 	p2(carps_badlen, "\t\t{:dropped-short-packet/%ju} "
884ade9ccfeSMarcel Moolenaar 	    "{N:/discarded because packet too short}\n");
885ade9ccfeSMarcel Moolenaar 	p2(carps_badauth, "\t\t{:dropped-bad-authentication/%ju} "
886ade9ccfeSMarcel Moolenaar 	    "{N:/discarded for bad authentication}\n");
887ade9ccfeSMarcel Moolenaar 	p2(carps_badvhid, "\t\t{:dropped-bad-vhid/%ju} "
888ade9ccfeSMarcel Moolenaar 	    "{N:/discarded for bad vhid}\n");
889ade9ccfeSMarcel Moolenaar 	p2(carps_badaddrs, "\t\t{:dropped-bad-address-list/%ju} "
890ade9ccfeSMarcel Moolenaar 	    "{N:/discarded because of a bad address list}\n");
891ade9ccfeSMarcel Moolenaar 	p(carps_opackets, "\t{:sent-inet-packets/%ju} "
892ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s sent (IPv4)}\n");
893ade9ccfeSMarcel Moolenaar 	p(carps_opackets6, "\t{:sent-inet6-packets/%ju} "
894ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s sent (IPv6)}\n");
895ade9ccfeSMarcel Moolenaar 	p2(carps_onomem, "\t\t{:send-failed-memory-error/%ju} "
896ade9ccfeSMarcel Moolenaar 	    "{N:/send failed due to mbuf memory error}\n");
897a9771948SGleb Smirnoff #if notyet
898ade9ccfeSMarcel Moolenaar 	p(carps_ostates, "\t\t{:send-state-updates/%s} "
899ade9ccfeSMarcel Moolenaar 	    "{N:/state update%s sent}\n");
900a9771948SGleb Smirnoff #endif
901a9771948SGleb Smirnoff #undef p
902a9771948SGleb Smirnoff #undef p2
903ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
904a9771948SGleb Smirnoff }
905a9771948SGleb Smirnoff 
906a9771948SGleb Smirnoff /*
9079b50d902SRodney W. Grimes  * Dump IP statistics structure.
9089b50d902SRodney W. Grimes  */
9099b50d902SRodney W. Grimes void
910feda1a43SJohn Baldwin ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
9119b50d902SRodney W. Grimes {
9129eddb899SMark Johnston 	struct ipstat ipstat;
9139b50d902SRodney W. Grimes 
9149eddb899SMark Johnston 	if (fetch_stats("net.inet.ip.stats", off, &ipstat,
9159eddb899SMark Johnston 	    sizeof(ipstat), kread_counters) != 0)
9169b50d902SRodney W. Grimes 		return;
9174f81ef50SGarrett Wollman 
918ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
919ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
9209b50d902SRodney W. Grimes 
9219b50d902SRodney W. Grimes #define	p(f, m) if (ipstat.f || sflag <= 1) \
922ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )ipstat.f, plural(ipstat.f))
92322694ebaSBruce Evans #define	p1a(f, m) if (ipstat.f || sflag <= 1) \
924ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )ipstat.f)
9259b50d902SRodney W. Grimes 
926ade9ccfeSMarcel Moolenaar 	p(ips_total, "\t{:received-packets/%ju} "
927ade9ccfeSMarcel Moolenaar 	    "{N:/total packet%s received}\n");
928ade9ccfeSMarcel Moolenaar 	p(ips_badsum, "\t{:dropped-bad-checksum/%ju} "
929ade9ccfeSMarcel Moolenaar 	    "{N:/bad header checksum%s}\n");
930ade9ccfeSMarcel Moolenaar 	p1a(ips_toosmall, "\t{:dropped-below-minimum-size/%ju} "
931ade9ccfeSMarcel Moolenaar 	    "{N:/with size smaller than minimum}\n");
932ade9ccfeSMarcel Moolenaar 	p1a(ips_tooshort, "\t{:dropped-short-packets/%ju} "
933ade9ccfeSMarcel Moolenaar 	    "{N:/with data size < data length}\n");
934ade9ccfeSMarcel Moolenaar 	p1a(ips_toolong, "\t{:dropped-too-long/%ju} "
935ade9ccfeSMarcel Moolenaar 	    "{N:/with ip length > max ip packet size}\n");
936ade9ccfeSMarcel Moolenaar 	p1a(ips_badhlen, "\t{:dropped-short-header-length/%ju} "
937ade9ccfeSMarcel Moolenaar 	    "{N:/with header length < data size}\n");
938ade9ccfeSMarcel Moolenaar 	p1a(ips_badlen, "\t{:dropped-short-data/%ju} "
939ade9ccfeSMarcel Moolenaar 	    "{N:/with data length < header length}\n");
940ade9ccfeSMarcel Moolenaar 	p1a(ips_badoptions, "\t{:dropped-bad-options/%ju} "
941ade9ccfeSMarcel Moolenaar 	    "{N:/with bad options}\n");
942ade9ccfeSMarcel Moolenaar 	p1a(ips_badvers, "\t{:dropped-bad-version/%ju} "
943ade9ccfeSMarcel Moolenaar 	    "{N:/with incorrect version number}\n");
944ade9ccfeSMarcel Moolenaar 	p(ips_fragments, "\t{:received-fragments/%ju} "
945ade9ccfeSMarcel Moolenaar 	    "{N:/fragment%s received}\n");
946ade9ccfeSMarcel Moolenaar 	p(ips_fragdropped, "\t{:dropped-fragments/%ju} "
947ade9ccfeSMarcel Moolenaar 	    "{N:/fragment%s dropped (dup or out of space)}\n");
948ade9ccfeSMarcel Moolenaar 	p(ips_fragtimeout, "\t{:dropped-fragments-after-timeout/%ju} "
949ade9ccfeSMarcel Moolenaar 	    "{N:/fragment%s dropped after timeout}\n");
950ade9ccfeSMarcel Moolenaar 	p(ips_reassembled, "\t{:reassembled-packets/%ju} "
951ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s reassembled ok}\n");
952ade9ccfeSMarcel Moolenaar 	p(ips_delivered, "\t{:received-local-packets/%ju} "
953ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s for this host}\n");
954ade9ccfeSMarcel Moolenaar 	p(ips_noproto, "\t{:dropped-unknown-protocol/%ju} "
955ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s for unknown\\/unsupported protocol}\n");
956ade9ccfeSMarcel Moolenaar 	p(ips_forward, "\t{:forwarded-packets/%ju} "
957ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s forwarded}");
958ade9ccfeSMarcel Moolenaar 	p(ips_fastforward, " ({:fast-forwarded-packets/%ju} "
959ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s fast forwarded})");
960958d6f7fSPierre Beyssac 	if (ipstat.ips_forward || sflag <= 1)
961ade9ccfeSMarcel Moolenaar 		xo_emit("\n");
962ade9ccfeSMarcel Moolenaar 	p(ips_cantforward, "\t{:packets-cannot-forward/%ju} "
963ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s not forwardable}\n");
964ade9ccfeSMarcel Moolenaar 	p(ips_notmember, "\t{:received-unknown-multicast-group/%ju} "
965ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s received for unknown multicast group}\n");
966ade9ccfeSMarcel Moolenaar 	p(ips_redirectsent, "\t{:redirects-sent/%ju} "
967ade9ccfeSMarcel Moolenaar 	    "{N:/redirect%s sent}\n");
968ade9ccfeSMarcel Moolenaar 	p(ips_localout, "\t{:sent-packets/%ju} "
969ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s sent from this host}\n");
970ade9ccfeSMarcel Moolenaar 	p(ips_rawout, "\t{:send-packets-fabricated-header/%ju} "
971ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s sent with fabricated ip header}\n");
972ade9ccfeSMarcel Moolenaar 	p(ips_odropped, "\t{:discard-no-mbufs/%ju} "
973ade9ccfeSMarcel Moolenaar 	    "{N:/output packet%s dropped due to no bufs, etc.}\n");
974ade9ccfeSMarcel Moolenaar 	p(ips_noroute, "\t{:discard-no-route/%ju} "
975ade9ccfeSMarcel Moolenaar 	    "{N:/output packet%s discarded due to no route}\n");
976ade9ccfeSMarcel Moolenaar 	p(ips_fragmented, "\t{:sent-fragments/%ju} "
977ade9ccfeSMarcel Moolenaar 	    "{N:/output datagram%s fragmented}\n");
978ade9ccfeSMarcel Moolenaar 	p(ips_ofragments, "\t{:fragments-created/%ju} "
979ade9ccfeSMarcel Moolenaar 	    "{N:/fragment%s created}\n");
980ade9ccfeSMarcel Moolenaar 	p(ips_cantfrag, "\t{:discard-cannot-fragment/%ju} "
981ade9ccfeSMarcel Moolenaar 	    "{N:/datagram%s that can't be fragmented}\n");
982ade9ccfeSMarcel Moolenaar 	p(ips_nogif, "\t{:discard-tunnel-no-gif/%ju} "
983ade9ccfeSMarcel Moolenaar 	    "{N:/tunneling packet%s that can't find gif}\n");
984ade9ccfeSMarcel Moolenaar 	p(ips_badaddr, "\t{:discard-bad-address/%ju} "
985ade9ccfeSMarcel Moolenaar 	    "{N:/datagram%s with bad address in header}\n");
9869b50d902SRodney W. Grimes #undef p
98722694ebaSBruce Evans #undef p1a
988ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
9899b50d902SRodney W. Grimes }
9909b50d902SRodney W. Grimes 
99154fc657dSGeorge V. Neville-Neil /*
99254fc657dSGeorge V. Neville-Neil  * Dump ARP statistics structure.
99354fc657dSGeorge V. Neville-Neil  */
99454fc657dSGeorge V. Neville-Neil void
99554fc657dSGeorge V. Neville-Neil arp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
99654fc657dSGeorge V. Neville-Neil {
9979eddb899SMark Johnston 	struct arpstat arpstat;
99854fc657dSGeorge V. Neville-Neil 
9999eddb899SMark Johnston 	if (fetch_stats("net.link.ether.arp.stats", off, &arpstat,
10009eddb899SMark Johnston 	    sizeof(arpstat), kread_counters) != 0)
100154fc657dSGeorge V. Neville-Neil 		return;
100254fc657dSGeorge V. Neville-Neil 
1003ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
1004ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
100554fc657dSGeorge V. Neville-Neil 
100654fc657dSGeorge V. Neville-Neil #define	p(f, m) if (arpstat.f || sflag <= 1) \
1007ade9ccfeSMarcel Moolenaar 	xo_emit("\t" m, (uintmax_t)arpstat.f, plural(arpstat.f))
100854fc657dSGeorge V. Neville-Neil #define	p2(f, m) if (arpstat.f || sflag <= 1) \
1009ade9ccfeSMarcel Moolenaar 	xo_emit("\t" m, (uintmax_t)arpstat.f, pluralies(arpstat.f))
101054fc657dSGeorge V. Neville-Neil 
1011ade9ccfeSMarcel Moolenaar 	p(txrequests, "{:sent-requests/%ju} {N:/ARP request%s sent}\n");
1012ade9ccfeSMarcel Moolenaar 	p2(txreplies, "{:sent-replies/%ju} {N:/ARP repl%s sent}\n");
1013ade9ccfeSMarcel Moolenaar 	p(rxrequests, "{:received-requests/%ju} "
1014ade9ccfeSMarcel Moolenaar 	    "{N:/ARP request%s received}\n");
1015ade9ccfeSMarcel Moolenaar 	p2(rxreplies, "{:received-replies/%ju} "
1016ade9ccfeSMarcel Moolenaar 	    "{N:/ARP repl%s received}\n");
1017ade9ccfeSMarcel Moolenaar 	p(received, "{:received-packers/%ju} "
1018ade9ccfeSMarcel Moolenaar 	    "{N:/ARP packet%s received}\n");
1019ade9ccfeSMarcel Moolenaar 	p(dropped, "{:dropped-no-entry/%ju} "
1020ade9ccfeSMarcel Moolenaar 	    "{N:/total packet%s dropped due to no ARP entry}\n");
1021ade9ccfeSMarcel Moolenaar 	p(timeouts, "{:entries-timeout/%ju} "
1022ade9ccfeSMarcel Moolenaar 	    "{N:/ARP entry%s timed out}\n");
1023ade9ccfeSMarcel Moolenaar 	p(dupips, "{:dropped-duplicate-address/%ju} "
1024ade9ccfeSMarcel Moolenaar 	    "{N:/Duplicate IP%s seen}\n");
102554fc657dSGeorge V. Neville-Neil #undef p
102654fc657dSGeorge V. Neville-Neil #undef p2
1027ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
102854fc657dSGeorge V. Neville-Neil }
102954fc657dSGeorge V. Neville-Neil 
103054fc657dSGeorge V. Neville-Neil 
103154fc657dSGeorge V. Neville-Neil 
10324063583aSMaxim Konovalov static	const char *icmpnames[ICMP_MAXTYPE + 1] = {
10334063583aSMaxim Konovalov 	"echo reply",			/* RFC 792 */
10349b50d902SRodney W. Grimes 	"#1",
10359b50d902SRodney W. Grimes 	"#2",
10364063583aSMaxim Konovalov 	"destination unreachable",	/* RFC 792 */
10374063583aSMaxim Konovalov 	"source quench",		/* RFC 792 */
10384063583aSMaxim Konovalov 	"routing redirect",		/* RFC 792 */
10399b50d902SRodney W. Grimes 	"#6",
10409b50d902SRodney W. Grimes 	"#7",
10414063583aSMaxim Konovalov 	"echo",				/* RFC 792 */
10424063583aSMaxim Konovalov 	"router advertisement",		/* RFC 1256 */
10434063583aSMaxim Konovalov 	"router solicitation",		/* RFC 1256 */
10444063583aSMaxim Konovalov 	"time exceeded",		/* RFC 792 */
10454063583aSMaxim Konovalov 	"parameter problem",		/* RFC 792 */
10464063583aSMaxim Konovalov 	"time stamp",			/* RFC 792 */
10474063583aSMaxim Konovalov 	"time stamp reply",		/* RFC 792 */
10484063583aSMaxim Konovalov 	"information request",		/* RFC 792 */
10494063583aSMaxim Konovalov 	"information request reply",	/* RFC 792 */
10504063583aSMaxim Konovalov 	"address mask request",		/* RFC 950 */
10514063583aSMaxim Konovalov 	"address mask reply",		/* RFC 950 */
10524063583aSMaxim Konovalov 	"#19",
10534063583aSMaxim Konovalov 	"#20",
10544063583aSMaxim Konovalov 	"#21",
10554063583aSMaxim Konovalov 	"#22",
10564063583aSMaxim Konovalov 	"#23",
10574063583aSMaxim Konovalov 	"#24",
10584063583aSMaxim Konovalov 	"#25",
10594063583aSMaxim Konovalov 	"#26",
10604063583aSMaxim Konovalov 	"#27",
10614063583aSMaxim Konovalov 	"#28",
10624063583aSMaxim Konovalov 	"#29",
10634063583aSMaxim Konovalov 	"icmp traceroute",		/* RFC 1393 */
10644063583aSMaxim Konovalov 	"datagram conversion error",	/* RFC 1475 */
10654063583aSMaxim Konovalov 	"mobile host redirect",
10664063583aSMaxim Konovalov 	"IPv6 where-are-you",
10674063583aSMaxim Konovalov 	"IPv6 i-am-here",
10684063583aSMaxim Konovalov 	"mobile registration req",
10694063583aSMaxim Konovalov 	"mobile registration reply",
10704063583aSMaxim Konovalov 	"domain name request",		/* RFC 1788 */
10714063583aSMaxim Konovalov 	"domain name reply",		/* RFC 1788 */
10724063583aSMaxim Konovalov 	"icmp SKIP",
10734063583aSMaxim Konovalov 	"icmp photuris",		/* RFC 2521 */
10749b50d902SRodney W. Grimes };
10759b50d902SRodney W. Grimes 
10769b50d902SRodney W. Grimes /*
10779b50d902SRodney W. Grimes  * Dump ICMP statistics.
10789b50d902SRodney W. Grimes  */
10799b50d902SRodney W. Grimes void
1080feda1a43SJohn Baldwin icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
10819b50d902SRodney W. Grimes {
10829eddb899SMark Johnston 	struct icmpstat icmpstat;
10834e00c309SGarrett Wollman 	size_t len;
10849eddb899SMark Johnston 	int i, first;
10859b50d902SRodney W. Grimes 
10869eddb899SMark Johnston 	if (fetch_stats("net.inet.icmp.stats", off, &icmpstat,
10879eddb899SMark Johnston 	    sizeof(icmpstat), kread_counters) != 0)
1088c73d99b5SRuslan Ermilov 		return;
10894e00c309SGarrett Wollman 
1090ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
1091ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
10929b50d902SRodney W. Grimes 
10939b50d902SRodney W. Grimes #define	p(f, m) if (icmpstat.f || sflag <= 1) \
1094ade9ccfeSMarcel Moolenaar 	xo_emit(m, icmpstat.f, plural(icmpstat.f))
109522694ebaSBruce Evans #define	p1a(f, m) if (icmpstat.f || sflag <= 1) \
1096ade9ccfeSMarcel Moolenaar 	xo_emit(m, icmpstat.f)
1097bd714208SRuslan Ermilov #define	p2(f, m) if (icmpstat.f || sflag <= 1) \
1098ade9ccfeSMarcel Moolenaar 	xo_emit(m, icmpstat.f, plurales(icmpstat.f))
10999b50d902SRodney W. Grimes 
1100ade9ccfeSMarcel Moolenaar 	p(icps_error, "\t{:icmp-calls/%lu} "
1101ade9ccfeSMarcel Moolenaar 	    "{N:/call%s to icmp_error}\n");
1102ade9ccfeSMarcel Moolenaar 	p(icps_oldicmp, "\t{:errors-not-from-message/%lu} "
1103ade9ccfeSMarcel Moolenaar 	    "{N:/error%s not generated in response to an icmp message}\n");
1104ade9ccfeSMarcel Moolenaar 
1105ade9ccfeSMarcel Moolenaar 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) {
11069b50d902SRodney W. Grimes 		if (icmpstat.icps_outhist[i] != 0) {
11079b50d902SRodney W. Grimes 			if (first) {
1108ade9ccfeSMarcel Moolenaar 				xo_open_list("output-histogram");
1109ade9ccfeSMarcel Moolenaar 				xo_emit("\tOutput histogram:\n");
11109b50d902SRodney W. Grimes 				first = 0;
11119b50d902SRodney W. Grimes 			}
1112ade9ccfeSMarcel Moolenaar 			xo_open_instance("output-histogram");
11134063583aSMaxim Konovalov 			if (icmpnames[i] != NULL)
1114ade9ccfeSMarcel Moolenaar 				xo_emit("\t\t{k:name/%s}: {:count/%lu}\n",
1115ade9ccfeSMarcel Moolenaar 				    icmpnames[i], icmpstat.icps_outhist[i]);
11164063583aSMaxim Konovalov 			else
1117ade9ccfeSMarcel Moolenaar 				xo_emit("\t\tunknown ICMP #{k:name/%d}: "
1118ade9ccfeSMarcel Moolenaar 				    "{:count/%lu}\n",
1119ade9ccfeSMarcel Moolenaar 				    i, icmpstat.icps_outhist[i]);
1120ade9ccfeSMarcel Moolenaar 			xo_close_instance("output-histogram");
11219b50d902SRodney W. Grimes 		}
1122ade9ccfeSMarcel Moolenaar 	}
1123ade9ccfeSMarcel Moolenaar 	if (!first)
1124ade9ccfeSMarcel Moolenaar 		xo_close_list("output-histogram");
1125ade9ccfeSMarcel Moolenaar 
1126ade9ccfeSMarcel Moolenaar 	p(icps_badcode, "\t{:dropped-bad-code/%lu} "
1127ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with bad code fields}\n");
1128ade9ccfeSMarcel Moolenaar 	p(icps_tooshort, "\t{:dropped-too-short/%lu} "
1129ade9ccfeSMarcel Moolenaar 	    "{N:/message%s less than the minimum length}\n");
1130ade9ccfeSMarcel Moolenaar 	p(icps_checksum, "\t{:dropped-bad-checksum/%lu} "
1131ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with bad checksum}\n");
1132ade9ccfeSMarcel Moolenaar 	p(icps_badlen, "\t{:dropped-bad-length/%lu} "
1133ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with bad length}\n");
1134ade9ccfeSMarcel Moolenaar 	p1a(icps_bmcastecho, "\t{:dropped-multicast-echo/%lu} "
1135ade9ccfeSMarcel Moolenaar 	    "{N:/multicast echo requests ignored}\n");
1136ade9ccfeSMarcel Moolenaar 	p1a(icps_bmcasttstamp, "\t{:dropped-multicast-timestamp/%lu} "
1137ade9ccfeSMarcel Moolenaar 	    "{N:/multicast timestamp requests ignored}\n");
1138ade9ccfeSMarcel Moolenaar 
1139ade9ccfeSMarcel Moolenaar 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) {
11409b50d902SRodney W. Grimes 		if (icmpstat.icps_inhist[i] != 0) {
11419b50d902SRodney W. Grimes 			if (first) {
1142ade9ccfeSMarcel Moolenaar 				xo_open_list("input-histogram");
1143ade9ccfeSMarcel Moolenaar 				xo_emit("\tInput histogram:\n");
11449b50d902SRodney W. Grimes 				first = 0;
11459b50d902SRodney W. Grimes 			}
1146ade9ccfeSMarcel Moolenaar 			xo_open_instance("input-histogram");
11474063583aSMaxim Konovalov 			if (icmpnames[i] != NULL)
1148ade9ccfeSMarcel Moolenaar 				xo_emit("\t\t{k:name/%s}: {:count/%lu}\n",
1149ade9ccfeSMarcel Moolenaar 					icmpnames[i],
11509b50d902SRodney W. Grimes 					icmpstat.icps_inhist[i]);
11514063583aSMaxim Konovalov 			else
1152ade9ccfeSMarcel Moolenaar 				xo_emit(
1153ade9ccfeSMarcel Moolenaar 			"\t\tunknown ICMP #{k:name/%d}: {:count/%lu}\n",
1154ade9ccfeSMarcel Moolenaar 					i, icmpstat.icps_inhist[i]);
1155ade9ccfeSMarcel Moolenaar 			xo_close_instance("input-histogram");
11569b50d902SRodney W. Grimes 		}
1157ade9ccfeSMarcel Moolenaar 	}
1158ade9ccfeSMarcel Moolenaar 	if (!first)
1159ade9ccfeSMarcel Moolenaar 		xo_close_list("input-histogram");
1160ade9ccfeSMarcel Moolenaar 
1161ade9ccfeSMarcel Moolenaar 	p(icps_reflect, "\t{:sent-packets/%lu} "
1162ade9ccfeSMarcel Moolenaar 	    "{N:/message response%s generated}\n");
1163ade9ccfeSMarcel Moolenaar 	p2(icps_badaddr, "\t{:discard-invalid-return-address/%lu} "
1164ade9ccfeSMarcel Moolenaar 	    "{N:/invalid return address%s}\n");
1165ade9ccfeSMarcel Moolenaar 	p(icps_noroute, "\t{:discard-no-route/%lu} "
1166ade9ccfeSMarcel Moolenaar 	    "{N:/no return route%s}\n");
11679b50d902SRodney W. Grimes #undef p
116822694ebaSBruce Evans #undef p1a
1169bd714208SRuslan Ermilov #undef p2
1170feda1a43SJohn Baldwin 	if (live) {
11714e00c309SGarrett Wollman 		len = sizeof i;
1172feda1a43SJohn Baldwin 		if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) <
1173feda1a43SJohn Baldwin 		    0)
11744e00c309SGarrett Wollman 			return;
1175ade9ccfeSMarcel Moolenaar 		xo_emit("\tICMP address mask responses are "
1176ade9ccfeSMarcel Moolenaar 		    "{q:icmp-address-responses/%sabled}\n", i ? "en" : "dis");
11779b50d902SRodney W. Grimes 	}
1178ade9ccfeSMarcel Moolenaar 
1179ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
1180feda1a43SJohn Baldwin }
11819b50d902SRodney W. Grimes 
1182d10910e6SBruce M Simpson /*
1183d10910e6SBruce M Simpson  * Dump IGMP statistics structure.
1184d10910e6SBruce M Simpson  */
1185d10910e6SBruce M Simpson void
1186d10910e6SBruce M Simpson igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1187d10910e6SBruce M Simpson {
11889eddb899SMark Johnston 	struct igmpstat igmpstat;
1189d10910e6SBruce M Simpson 
11909eddb899SMark Johnston 	if (fetch_stats("net.inet.igmp.stats", 0, &igmpstat,
11919eddb899SMark Johnston 	    sizeof(igmpstat), kread) != 0)
1192d10910e6SBruce M Simpson 		return;
1193d10910e6SBruce M Simpson 
1194d10910e6SBruce M Simpson 	if (igmpstat.igps_version != IGPS_VERSION_3) {
1195ade9ccfeSMarcel Moolenaar 		xo_warnx("%s: version mismatch (%d != %d)", __func__,
1196d10910e6SBruce M Simpson 		    igmpstat.igps_version, IGPS_VERSION_3);
1197d10910e6SBruce M Simpson 	}
1198d10910e6SBruce M Simpson 	if (igmpstat.igps_len != IGPS_VERSION3_LEN) {
1199ade9ccfeSMarcel Moolenaar 		xo_warnx("%s: size mismatch (%d != %d)", __func__,
1200d10910e6SBruce M Simpson 		    igmpstat.igps_len, IGPS_VERSION3_LEN);
1201d10910e6SBruce M Simpson 	}
1202d10910e6SBruce M Simpson 
1203ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
1204ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
1205d10910e6SBruce M Simpson 
1206d10910e6SBruce M Simpson #define	p64(f, m) if (igmpstat.f || sflag <= 1) \
1207ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t) igmpstat.f, plural(igmpstat.f))
1208d10910e6SBruce M Simpson #define	py64(f, m) if (igmpstat.f || sflag <= 1) \
1209ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t) igmpstat.f, pluralies(igmpstat.f))
1210ade9ccfeSMarcel Moolenaar 
1211ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_total, "\t{:received-messages/%ju} "
1212ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received}\n");
1213ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_tooshort, "\t{:dropped-too-short/%ju} "
1214ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with too few bytes}\n");
1215ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_badttl, "\t{:dropped-wrong-ttl/%ju} "
1216ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with wrong TTL}\n");
1217ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_badsum, "\t{:dropped-bad-checksum/%ju} "
1218ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with bad checksum}\n");
1219ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_v1v2_queries, "\t{:received-membership-queries/%ju} "
1220ade9ccfeSMarcel Moolenaar 	    "{N:/V1\\/V2 membership quer%s received}\n");
1221ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_v3_queries, "\t{:received-v3-membership-queries/%ju} "
1222ade9ccfeSMarcel Moolenaar 	    "{N:/V3 membership quer%s received}\n");
1223ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_badqueries, "\t{:dropped-membership-queries/%ju} "
1224ade9ccfeSMarcel Moolenaar 	    "{N:/membership quer%s received with invalid field(s)}\n");
1225ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_gen_queries, "\t{:received-general-queries/%ju} "
1226ade9ccfeSMarcel Moolenaar 	    "{N:/general quer%s received}\n");
1227ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_group_queries, "\t{:received-group-queries/%ju} "
1228ade9ccfeSMarcel Moolenaar 	    "{N:/group quer%s received}\n");
1229ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_gsr_queries, "\t{:received-group-source-queries/%ju} "
1230ade9ccfeSMarcel Moolenaar 	    "{N:/group-source quer%s received}\n");
1231ade9ccfeSMarcel Moolenaar 	py64(igps_drop_gsr_queries, "\t{:dropped-group-source-queries/%ju} "
1232ade9ccfeSMarcel Moolenaar 	    "{N:/group-source quer%s dropped}\n");
1233ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_reports, "\t{:received-membership-requests/%ju} "
1234ade9ccfeSMarcel Moolenaar 	    "{N:/membership report%s received}\n");
1235ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_badreports, "\t{:dropped-membership-reports/%ju} "
1236ade9ccfeSMarcel Moolenaar 	    "{N:/membership report%s received with invalid field(s)}\n");
1237ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_ourreports, "\t"
1238ade9ccfeSMarcel Moolenaar 	    "{:received-membership-reports-matching/%ju} "
1239ade9ccfeSMarcel Moolenaar 	    "{N:/membership report%s received for groups to which we belong}"
1240ade9ccfeSMarcel Moolenaar 	    "\n");
1241ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_nora, "\t{:received-v3-reports-no-router-alert/%ju} "
1242ade9ccfeSMarcel Moolenaar 	    "{N:/V3 report%s received without Router Alert}\n");
1243ade9ccfeSMarcel Moolenaar 	p64(igps_snd_reports, "\t{:sent-membership-reports/%ju} "
1244ade9ccfeSMarcel Moolenaar 	    "{N:/membership report%s sent}\n");
1245d10910e6SBruce M Simpson #undef p64
1246d10910e6SBruce M Simpson #undef py64
1247ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
1248d10910e6SBruce M Simpson }
12499b50d902SRodney W. Grimes 
12509b50d902SRodney W. Grimes /*
1251c7b9b5bbSJeffrey Hsu  * Dump PIM statistics structure.
1252c7b9b5bbSJeffrey Hsu  */
1253c7b9b5bbSJeffrey Hsu void
1254feda1a43SJohn Baldwin pim_stats(u_long off __unused, const char *name, int af1 __unused,
1255feda1a43SJohn Baldwin     int proto __unused)
1256c7b9b5bbSJeffrey Hsu {
12579eddb899SMark Johnston 	struct pimstat pimstat;
1258c7b9b5bbSJeffrey Hsu 
12599eddb899SMark Johnston 	if (fetch_stats("net.inet.pim.stats", off, &pimstat,
12609eddb899SMark Johnston 	    sizeof(pimstat), kread_counters) != 0)
1261c7b9b5bbSJeffrey Hsu 		return;
1262c7b9b5bbSJeffrey Hsu 
1263ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
1264ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
1265c7b9b5bbSJeffrey Hsu 
1266c7b9b5bbSJeffrey Hsu #define	p(f, m) if (pimstat.f || sflag <= 1) \
1267ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t)pimstat.f, plural(pimstat.f))
1268c7b9b5bbSJeffrey Hsu #define	py(f, m) if (pimstat.f || sflag <= 1) \
1269ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
1270ade9ccfeSMarcel Moolenaar 
1271ade9ccfeSMarcel Moolenaar 	p(pims_rcv_total_msgs, "\t{:received-messages/%ju} "
1272ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received}\n");
1273ade9ccfeSMarcel Moolenaar 	p(pims_rcv_total_bytes, "\t{:received-bytes/%ju} "
1274ade9ccfeSMarcel Moolenaar 	    "{N:/byte%s received}\n");
1275ade9ccfeSMarcel Moolenaar 	p(pims_rcv_tooshort, "\t{:dropped-too-short/%ju} "
1276ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with too few bytes}\n");
1277ade9ccfeSMarcel Moolenaar 	p(pims_rcv_badsum, "\t{:dropped-bad-checksum/%ju} "
1278ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with bad checksum}\n");
1279ade9ccfeSMarcel Moolenaar 	p(pims_rcv_badversion, "\t{:dropped-bad-version/%ju} "
1280ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with bad version}\n");
1281ade9ccfeSMarcel Moolenaar 	p(pims_rcv_registers_msgs, "\t{:received-data-register-messages/%ju} "
1282ade9ccfeSMarcel Moolenaar 	    "{N:/data register message%s received}\n");
1283ade9ccfeSMarcel Moolenaar 	p(pims_rcv_registers_bytes, "\t{:received-data-register-bytes/%ju} "
1284ade9ccfeSMarcel Moolenaar 	    "{N:/data register byte%s received}\n");
1285ade9ccfeSMarcel Moolenaar 	p(pims_rcv_registers_wrongiif, "\t"
1286ade9ccfeSMarcel Moolenaar 	    "{:received-data-register-wrong-interface/%ju} "
1287ade9ccfeSMarcel Moolenaar 	    "{N:/data register message%s received on wrong iif}\n");
1288ade9ccfeSMarcel Moolenaar 	p(pims_rcv_badregisters, "\t{:received-bad-registers/%ju} "
1289ade9ccfeSMarcel Moolenaar 	    "{N:/bad register%s received}\n");
1290ade9ccfeSMarcel Moolenaar 	p(pims_snd_registers_msgs, "\t{:sent-data-register-messages/%ju} "
1291ade9ccfeSMarcel Moolenaar 	    "{N:/data register message%s sent}\n");
1292ade9ccfeSMarcel Moolenaar 	p(pims_snd_registers_bytes, "\t{:sent-data-register-bytes/%ju} "
1293ade9ccfeSMarcel Moolenaar 	    "{N:/data register byte%s sent}\n");
1294c7b9b5bbSJeffrey Hsu #undef p
1295c7b9b5bbSJeffrey Hsu #undef py
1296ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
1297c7b9b5bbSJeffrey Hsu }
1298c7b9b5bbSJeffrey Hsu 
1299c7b9b5bbSJeffrey Hsu /*
13009b50d902SRodney W. Grimes  * Pretty print an Internet address (net address + port).
13019b50d902SRodney W. Grimes  */
13029b50d902SRodney W. Grimes void
1303ade9ccfeSMarcel Moolenaar inetprint(const char *container, struct in_addr *in, int port,
1304046aad39SHajimu UMEMOTO     const char *proto, int num_port, const int af1)
13059b50d902SRodney W. Grimes {
13069b50d902SRodney W. Grimes 	struct servent *sp = 0;
13079b50d902SRodney W. Grimes 	char line[80], *cp;
1308cfa1ca9dSYoshinobu Inoue 	int width;
1309f193c8ceSXin LI 	size_t alen, plen;
13109b50d902SRodney W. Grimes 
1311ade9ccfeSMarcel Moolenaar 	if (container)
1312ade9ccfeSMarcel Moolenaar 		xo_open_container(container);
1313ade9ccfeSMarcel Moolenaar 
1314080b7f49SDag-Erling Smørgrav 	if (Wflag)
1315f193c8ceSXin LI 	    snprintf(line, sizeof(line), "%s.", inetname(in));
1316080b7f49SDag-Erling Smørgrav 	else
1317f193c8ceSXin LI 	    snprintf(line, sizeof(line), "%.*s.",
1318f193c8ceSXin LI 		(Aflag && !num_port) ? 12 : 16, inetname(in));
1319f193c8ceSXin LI 	alen = strlen(line);
1320f193c8ceSXin LI 	cp = line + alen;
1321a01e3379SDavid Malone 	if (!num_port && port)
13229b50d902SRodney W. Grimes 		sp = getservbyport((int)port, proto);
13239b50d902SRodney W. Grimes 	if (sp || port == 0)
1324f193c8ceSXin LI 		snprintf(cp, sizeof(line) - alen,
1325f193c8ceSXin LI 		    "%.15s ", sp ? sp->s_name : "*");
13269b50d902SRodney W. Grimes 	else
1327f193c8ceSXin LI 		snprintf(cp, sizeof(line) - alen,
1328f193c8ceSXin LI 		    "%d ", ntohs((u_short)port));
1329046aad39SHajimu UMEMOTO 	width = (Aflag && !Wflag) ? 18 :
1330046aad39SHajimu UMEMOTO 		((!Wflag || af1 == AF_INET) ? 22 : 45);
1331080b7f49SDag-Erling Smørgrav 	if (Wflag)
1332ade9ccfeSMarcel Moolenaar 		xo_emit("{d:target/%-*s} ", width, line);
1333080b7f49SDag-Erling Smørgrav 	else
1334ade9ccfeSMarcel Moolenaar 		xo_emit("{d:target/%-*.*s} ", width, width, line);
1335ade9ccfeSMarcel Moolenaar 
1336f193c8ceSXin LI 	plen = strlen(cp) - 1;
1337f193c8ceSXin LI 	alen--;
1338ade9ccfeSMarcel Moolenaar 	xo_emit("{e:address/%*.*s}{e:port/%*.*s}", alen, alen, line, plen,
1339ade9ccfeSMarcel Moolenaar 	    plen, cp);
1340ade9ccfeSMarcel Moolenaar 
1341ade9ccfeSMarcel Moolenaar 	if (container)
1342ade9ccfeSMarcel Moolenaar 		xo_close_container(container);
13439b50d902SRodney W. Grimes }
13449b50d902SRodney W. Grimes 
13459b50d902SRodney W. Grimes /*
13469b50d902SRodney W. Grimes  * Construct an Internet address representation.
134765ea0024SAssar Westerlund  * If numeric_addr has been supplied, give
13489b50d902SRodney W. Grimes  * numeric value, otherwise try for symbolic name.
13499b50d902SRodney W. Grimes  */
13509b50d902SRodney W. Grimes char *
13515e051718SAssar Westerlund inetname(struct in_addr *inp)
13529b50d902SRodney W. Grimes {
1353a01e3379SDavid Malone 	char *cp;
1354d121b556SBrian Somers 	static char line[MAXHOSTNAMELEN];
13559b50d902SRodney W. Grimes 	struct hostent *hp;
13569b50d902SRodney W. Grimes 	struct netent *np;
13579b50d902SRodney W. Grimes 
13589b50d902SRodney W. Grimes 	cp = 0;
135965ea0024SAssar Westerlund 	if (!numeric_addr && inp->s_addr != INADDR_ANY) {
13609b50d902SRodney W. Grimes 		int net = inet_netof(*inp);
13619b50d902SRodney W. Grimes 		int lna = inet_lnaof(*inp);
13629b50d902SRodney W. Grimes 
13639b50d902SRodney W. Grimes 		if (lna == INADDR_ANY) {
13649b50d902SRodney W. Grimes 			np = getnetbyaddr(net, AF_INET);
13659b50d902SRodney W. Grimes 			if (np)
13669b50d902SRodney W. Grimes 				cp = np->n_name;
13679b50d902SRodney W. Grimes 		}
1368ef1cb629SMarcelo Araujo 		if (cp == NULL) {
13699b50d902SRodney W. Grimes 			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
13709b50d902SRodney W. Grimes 			if (hp) {
13719b50d902SRodney W. Grimes 				cp = hp->h_name;
1372d121b556SBrian Somers 				trimdomain(cp, strlen(cp));
13739b50d902SRodney W. Grimes 			}
13749b50d902SRodney W. Grimes 		}
13759b50d902SRodney W. Grimes 	}
13769b50d902SRodney W. Grimes 	if (inp->s_addr == INADDR_ANY)
13779b50d902SRodney W. Grimes 		strcpy(line, "*");
13789a1f6729SWarner Losh 	else if (cp) {
13791c109628SXin LI 		strlcpy(line, cp, sizeof(line));
13809a1f6729SWarner Losh 	} else {
13819b50d902SRodney W. Grimes 		inp->s_addr = ntohl(inp->s_addr);
138222694ebaSBruce Evans #define	C(x)	((u_int)((x) & 0xff))
1383f193c8ceSXin LI 		snprintf(line, sizeof(line), "%u.%u.%u.%u",
1384f193c8ceSXin LI 		    C(inp->s_addr >> 24), C(inp->s_addr >> 16),
1385f193c8ceSXin LI 		    C(inp->s_addr >> 8), C(inp->s_addr));
13869b50d902SRodney W. Grimes 	}
13879b50d902SRodney W. Grimes 	return (line);
13889b50d902SRodney W. Grimes }
1389