xref: /freebsd/usr.bin/netstat/inet.c (revision b25d74e06c0c15679da0de6ec4a0fac32e9e917f)
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>
440e229f34SGleb Smirnoff #define	_WANT_SOCKET
459b50d902SRodney W. Grimes #include <sys/socketvar.h>
464e00c309SGarrett Wollman #include <sys/sysctl.h>
479b50d902SRodney W. Grimes 
489b50d902SRodney W. Grimes #include <net/route.h>
4954fc657dSGeorge V. Neville-Neil #include <net/if_arp.h>
509b50d902SRodney W. Grimes #include <netinet/in.h>
519b50d902SRodney W. Grimes #include <netinet/in_systm.h>
529b50d902SRodney W. Grimes #include <netinet/ip.h>
53a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
54cfa1ca9dSYoshinobu Inoue #ifdef INET6
55cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
56cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
579b50d902SRodney W. Grimes #include <netinet/in_pcb.h>
589b50d902SRodney W. Grimes #include <netinet/ip_icmp.h>
599b50d902SRodney W. Grimes #include <netinet/icmp_var.h>
609b50d902SRodney W. Grimes #include <netinet/igmp_var.h>
619b50d902SRodney W. Grimes #include <netinet/ip_var.h>
62c7b9b5bbSJeffrey Hsu #include <netinet/pim_var.h>
639b50d902SRodney W. Grimes #include <netinet/tcp.h>
649b50d902SRodney W. Grimes #include <netinet/tcpip.h>
659b50d902SRodney W. Grimes #include <netinet/tcp_seq.h>
669b50d902SRodney W. Grimes #define	TCPSTATES
679b50d902SRodney W. Grimes #include <netinet/tcp_fsm.h>
689b50d902SRodney W. Grimes #include <netinet/tcp_timer.h>
699b50d902SRodney W. Grimes #include <netinet/tcp_var.h>
709b50d902SRodney W. Grimes #include <netinet/udp.h>
719b50d902SRodney W. Grimes #include <netinet/udp_var.h>
729b50d902SRodney W. Grimes 
739b50d902SRodney W. Grimes #include <arpa/inet.h>
744f81ef50SGarrett Wollman #include <err.h>
754f81ef50SGarrett Wollman #include <errno.h>
76d121b556SBrian Somers #include <libutil.h>
779b50d902SRodney W. Grimes #include <netdb.h>
787b95a1ebSYaroslav Tykhiy #include <stdint.h>
799b50d902SRodney W. Grimes #include <stdio.h>
804f81ef50SGarrett Wollman #include <stdlib.h>
81ade9ccfeSMarcel Moolenaar #include <stdbool.h>
829b50d902SRodney W. Grimes #include <string.h>
839b50d902SRodney W. Grimes #include <unistd.h>
84ade9ccfeSMarcel Moolenaar #include <libxo/xo.h>
859b50d902SRodney W. Grimes #include "netstat.h"
86dbfd8708SGleb Smirnoff #include "nl_defs.h"
879b50d902SRodney W. Grimes 
889784899dSBjoern A. Zeeb #ifdef INET
899784899dSBjoern A. Zeeb static void inetprint(const char *, struct in_addr *, int, const char *, int,
90046aad39SHajimu UMEMOTO     const int);
919784899dSBjoern A. Zeeb #endif
92cfa1ca9dSYoshinobu Inoue #ifdef INET6
93aa0a1e58SJeff Roberson static int udp_done, tcp_done, sdp_done;
94cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
959b50d902SRodney W. Grimes 
96feda1a43SJohn Baldwin static int
97cc65eb4eSGleb Smirnoff pcblist_sysctl(int proto, const char *name, char **bufp)
98feda1a43SJohn Baldwin {
99feda1a43SJohn Baldwin 	const char *mibvar;
100feda1a43SJohn Baldwin 	char *buf;
101feda1a43SJohn Baldwin 	size_t len;
102feda1a43SJohn Baldwin 
103feda1a43SJohn Baldwin 	switch (proto) {
104feda1a43SJohn Baldwin 	case IPPROTO_TCP:
105feda1a43SJohn Baldwin 		mibvar = "net.inet.tcp.pcblist";
106feda1a43SJohn Baldwin 		break;
107feda1a43SJohn Baldwin 	case IPPROTO_UDP:
108feda1a43SJohn Baldwin 		mibvar = "net.inet.udp.pcblist";
109feda1a43SJohn Baldwin 		break;
110feda1a43SJohn Baldwin 	case IPPROTO_DIVERT:
111feda1a43SJohn Baldwin 		mibvar = "net.inet.divert.pcblist";
112feda1a43SJohn Baldwin 		break;
113feda1a43SJohn Baldwin 	default:
114feda1a43SJohn Baldwin 		mibvar = "net.inet.raw.pcblist";
115feda1a43SJohn Baldwin 		break;
116feda1a43SJohn Baldwin 	}
117aa0a1e58SJeff Roberson 	if (strncmp(name, "sdp", 3) == 0)
118aa0a1e58SJeff Roberson 		mibvar = "net.inet.sdp.pcblist";
119feda1a43SJohn Baldwin 	len = 0;
120feda1a43SJohn Baldwin 	if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
121feda1a43SJohn Baldwin 		if (errno != ENOENT)
122ade9ccfeSMarcel Moolenaar 			xo_warn("sysctl: %s", mibvar);
123feda1a43SJohn Baldwin 		return (0);
124feda1a43SJohn Baldwin 	}
125ef1cb629SMarcelo Araujo 	if ((buf = malloc(len)) == NULL) {
126ade9ccfeSMarcel Moolenaar 		xo_warnx("malloc %lu bytes", (u_long)len);
127feda1a43SJohn Baldwin 		return (0);
128feda1a43SJohn Baldwin 	}
129feda1a43SJohn Baldwin 	if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
130ade9ccfeSMarcel Moolenaar 		xo_warn("sysctl: %s", mibvar);
131feda1a43SJohn Baldwin 		free(buf);
132feda1a43SJohn Baldwin 		return (0);
133feda1a43SJohn Baldwin 	}
134feda1a43SJohn Baldwin 	*bufp = buf;
135feda1a43SJohn Baldwin 	return (1);
136feda1a43SJohn Baldwin }
137feda1a43SJohn Baldwin 
138feda1a43SJohn Baldwin /*
139feda1a43SJohn Baldwin  * Copied directly from uipc_socket2.c.  We leave out some fields that are in
140feda1a43SJohn Baldwin  * nested structures that aren't used to avoid extra work.
141feda1a43SJohn Baldwin  */
142feda1a43SJohn Baldwin static void
143feda1a43SJohn Baldwin sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
144feda1a43SJohn Baldwin {
1450f9d0a73SGleb Smirnoff 	xsb->sb_cc = sb->sb_ccc;
146feda1a43SJohn Baldwin 	xsb->sb_hiwat = sb->sb_hiwat;
147feda1a43SJohn Baldwin 	xsb->sb_mbcnt = sb->sb_mbcnt;
14849f287f8SGeorge V. Neville-Neil 	xsb->sb_mcnt = sb->sb_mcnt;
14949f287f8SGeorge V. Neville-Neil 	xsb->sb_ccnt = sb->sb_ccnt;
150feda1a43SJohn Baldwin 	xsb->sb_mbmax = sb->sb_mbmax;
151feda1a43SJohn Baldwin 	xsb->sb_lowat = sb->sb_lowat;
152feda1a43SJohn Baldwin 	xsb->sb_flags = sb->sb_flags;
153feda1a43SJohn Baldwin 	xsb->sb_timeo = sb->sb_timeo;
154feda1a43SJohn Baldwin }
155feda1a43SJohn Baldwin 
156feda1a43SJohn Baldwin int
157feda1a43SJohn Baldwin sotoxsocket(struct socket *so, struct xsocket *xso)
158feda1a43SJohn Baldwin {
159feda1a43SJohn Baldwin 	struct protosw proto;
160feda1a43SJohn Baldwin 	struct domain domain;
161feda1a43SJohn Baldwin 
162feda1a43SJohn Baldwin 	bzero(xso, sizeof *xso);
163feda1a43SJohn Baldwin 	xso->xso_len = sizeof *xso;
1643a20f06aSBrooks Davis 	xso->xso_so = (uintptr_t)so;
165feda1a43SJohn Baldwin 	xso->so_type = so->so_type;
166feda1a43SJohn Baldwin 	xso->so_options = so->so_options;
167feda1a43SJohn Baldwin 	xso->so_linger = so->so_linger;
168feda1a43SJohn Baldwin 	xso->so_state = so->so_state;
1693a20f06aSBrooks Davis 	xso->so_pcb = (uintptr_t)so->so_pcb;
170feda1a43SJohn Baldwin 	if (kread((uintptr_t)so->so_proto, &proto, sizeof(proto)) != 0)
171feda1a43SJohn Baldwin 		return (-1);
172feda1a43SJohn Baldwin 	xso->xso_protocol = proto.pr_protocol;
173feda1a43SJohn Baldwin 	if (kread((uintptr_t)proto.pr_domain, &domain, sizeof(domain)) != 0)
174feda1a43SJohn Baldwin 		return (-1);
175feda1a43SJohn Baldwin 	xso->xso_family = domain.dom_family;
176feda1a43SJohn Baldwin 	xso->so_timeo = so->so_timeo;
177feda1a43SJohn Baldwin 	xso->so_error = so->so_error;
1780e229f34SGleb Smirnoff 	if ((so->so_options & SO_ACCEPTCONN) != 0) {
179779f106aSGleb Smirnoff 		xso->so_qlen = so->sol_qlen;
180779f106aSGleb Smirnoff 		xso->so_incqlen = so->sol_incqlen;
181779f106aSGleb Smirnoff 		xso->so_qlimit = so->sol_qlimit;
182779f106aSGleb Smirnoff 	} else {
183feda1a43SJohn Baldwin 		sbtoxsockbuf(&so->so_snd, &xso->so_snd);
184feda1a43SJohn Baldwin 		sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
185779f106aSGleb Smirnoff 		xso->so_oobmark = so->so_oobmark;
186779f106aSGleb Smirnoff 	}
187feda1a43SJohn Baldwin 	return (0);
188feda1a43SJohn Baldwin }
189feda1a43SJohn Baldwin 
1909b50d902SRodney W. Grimes /*
1919b50d902SRodney W. Grimes  * Print a summary of connections related to an Internet
1929b50d902SRodney W. Grimes  * protocol.  For TCP, also give state of connection.
1939b50d902SRodney W. Grimes  * Listening processes (aflag) are suppressed unless the
1949b50d902SRodney W. Grimes  * -a (all) flag is specified.
1959b50d902SRodney W. Grimes  */
1969b50d902SRodney W. Grimes void
197feda1a43SJohn Baldwin protopr(u_long off, const char *name, int af1, int proto)
1989b50d902SRodney W. Grimes {
1999b50d902SRodney W. Grimes 	static int first = 1;
200cc65eb4eSGleb Smirnoff 	int istcp;
2014f81ef50SGarrett Wollman 	char *buf;
202feda1a43SJohn Baldwin 	const char *vchar;
203cc65eb4eSGleb Smirnoff 	struct xtcpcb *tp;
204cc65eb4eSGleb Smirnoff 	struct xinpcb *inp;
2054f81ef50SGarrett Wollman 	struct xinpgen *xig, *oxig;
2064f81ef50SGarrett Wollman 	struct xsocket *so;
2079b50d902SRodney W. Grimes 
2084f81ef50SGarrett Wollman 	istcp = 0;
2094f81ef50SGarrett Wollman 	switch (proto) {
2104f81ef50SGarrett Wollman 	case IPPROTO_TCP:
211cfa1ca9dSYoshinobu Inoue #ifdef INET6
212aa0a1e58SJeff Roberson 		if (strncmp(name, "sdp", 3) != 0) {
213cfa1ca9dSYoshinobu Inoue 			if (tcp_done != 0)
214cfa1ca9dSYoshinobu Inoue 				return;
215cfa1ca9dSYoshinobu Inoue 			else
216cfa1ca9dSYoshinobu Inoue 				tcp_done = 1;
217aa0a1e58SJeff Roberson 		} else {
218aa0a1e58SJeff Roberson 			if (sdp_done != 0)
219aa0a1e58SJeff Roberson 				return;
220aa0a1e58SJeff Roberson 			else
221aa0a1e58SJeff Roberson 				sdp_done = 1;
222aa0a1e58SJeff Roberson 		}
223cfa1ca9dSYoshinobu Inoue #endif
2244f81ef50SGarrett Wollman 		istcp = 1;
2254f81ef50SGarrett Wollman 		break;
2264f81ef50SGarrett Wollman 	case IPPROTO_UDP:
227cfa1ca9dSYoshinobu Inoue #ifdef INET6
228cfa1ca9dSYoshinobu Inoue 		if (udp_done != 0)
229cfa1ca9dSYoshinobu Inoue 			return;
230cfa1ca9dSYoshinobu Inoue 		else
231cfa1ca9dSYoshinobu Inoue 			udp_done = 1;
232cfa1ca9dSYoshinobu Inoue #endif
2334f81ef50SGarrett Wollman 		break;
2344f81ef50SGarrett Wollman 	}
235cc65eb4eSGleb Smirnoff 
236cc65eb4eSGleb Smirnoff 	if (!pcblist_sysctl(proto, name, &buf))
2379b50d902SRodney W. Grimes 		return;
2384f81ef50SGarrett Wollman 
2394f81ef50SGarrett Wollman 	oxig = xig = (struct xinpgen *)buf;
2404f81ef50SGarrett Wollman 	for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
2414f81ef50SGarrett Wollman 	    xig->xig_len > sizeof(struct xinpgen);
2424f81ef50SGarrett Wollman 	    xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
2439b50d902SRodney W. Grimes 		if (istcp) {
244cc65eb4eSGleb Smirnoff 			tp = (struct xtcpcb *)xig;
245cc65eb4eSGleb Smirnoff 			inp = &tp->xt_inp;
2464f81ef50SGarrett Wollman 		} else {
247cc65eb4eSGleb Smirnoff 			inp = (struct xinpcb *)xig;
2489b50d902SRodney W. Grimes 		}
249cc65eb4eSGleb Smirnoff 		so = &inp->xi_socket;
2504f81ef50SGarrett Wollman 
2514f81ef50SGarrett Wollman 		/* Ignore sockets for protocols other than the desired one. */
252feda1a43SJohn Baldwin 		if (so->xso_protocol != proto)
2534f81ef50SGarrett Wollman 			continue;
2544f81ef50SGarrett Wollman 
2554f81ef50SGarrett Wollman 		/* Ignore PCBs which were freed during copyout. */
2564f81ef50SGarrett Wollman 		if (inp->inp_gencnt > oxig->xig_gen)
2574f81ef50SGarrett Wollman 			continue;
2584f81ef50SGarrett Wollman 
259a01e3379SDavid Malone 		if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
260cfa1ca9dSYoshinobu Inoue #ifdef INET6
261a01e3379SDavid Malone 		    || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
262cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
263a01e3379SDavid Malone 		    || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
264cfa1ca9dSYoshinobu Inoue #ifdef INET6
2653feeb332SDavid E. O'Brien 					  && (inp->inp_vflag & INP_IPV6) == 0
266cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
267cfa1ca9dSYoshinobu Inoue 			))
268cfa1ca9dSYoshinobu Inoue 		    )
269cfa1ca9dSYoshinobu Inoue 			continue;
270cfa1ca9dSYoshinobu Inoue 		if (!aflag &&
271cfa1ca9dSYoshinobu Inoue 		    (
2722b286cedSBruce M Simpson 		     (istcp && tp->t_state == TCPS_LISTEN)
2732b286cedSBruce M Simpson 		     || (af1 == AF_INET &&
274cfa1ca9dSYoshinobu Inoue 		      inet_lnaof(inp->inp_laddr) == INADDR_ANY)
275cfa1ca9dSYoshinobu Inoue #ifdef INET6
276a01e3379SDavid Malone 		     || (af1 == AF_INET6 &&
277cfa1ca9dSYoshinobu Inoue 			 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
278cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
279a01e3379SDavid Malone 		     || (af1 == AF_UNSPEC &&
280cfa1ca9dSYoshinobu Inoue 			 (((inp->inp_vflag & INP_IPV4) != 0 &&
281cfa1ca9dSYoshinobu Inoue 			   inet_lnaof(inp->inp_laddr) == INADDR_ANY)
282cfa1ca9dSYoshinobu Inoue #ifdef INET6
283cfa1ca9dSYoshinobu Inoue 			  || ((inp->inp_vflag & INP_IPV6) != 0 &&
284cfa1ca9dSYoshinobu Inoue 			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
285cfa1ca9dSYoshinobu Inoue #endif
286cfa1ca9dSYoshinobu Inoue 			  ))
287cfa1ca9dSYoshinobu Inoue 		     ))
2884f81ef50SGarrett Wollman 			continue;
2894f81ef50SGarrett Wollman 
2909b50d902SRodney W. Grimes 		if (first) {
291ac55add0SGuido van Rooij 			if (!Lflag) {
292ade9ccfeSMarcel Moolenaar 				xo_emit("Active Internet connections");
2939b50d902SRodney W. Grimes 				if (aflag)
294ade9ccfeSMarcel Moolenaar 					xo_emit(" (including servers)");
295ac55add0SGuido van Rooij 			} else
296ade9ccfeSMarcel Moolenaar 				xo_emit(
297ac55add0SGuido van Rooij 	"Current listen queue sizes (qlen/incqlen/maxqlen)");
298ade9ccfeSMarcel Moolenaar 			xo_emit("\n");
2999b50d902SRodney W. Grimes 			if (Aflag)
300ade9ccfeSMarcel Moolenaar 				xo_emit("{T:/%-*s} ", 2 * (int)sizeof(void *),
301ade9ccfeSMarcel Moolenaar 				    "Tcpcb");
302ac55add0SGuido van Rooij 			if (Lflag)
303ade9ccfeSMarcel Moolenaar 				xo_emit((Aflag && !Wflag) ?
304db2627f4SMichael Tuexen 				    "{T:/%-5.5s} {T:/%-32.32s} {T:/%-18.18s}" :
305046aad39SHajimu UMEMOTO 				    ((!Wflag || af1 == AF_INET) ?
306db2627f4SMichael Tuexen 				    "{T:/%-5.5s} {T:/%-32.32s} {T:/%-22.22s}" :
307db2627f4SMichael Tuexen 				    "{T:/%-5.5s} {T:/%-32.32s} {T:/%-45.45s}"),
308afc74015SRuslan Ermilov 				    "Proto", "Listen", "Local Address");
309afc74015SRuslan Ermilov 			else if (Tflag)
310ade9ccfeSMarcel Moolenaar 				xo_emit((Aflag && !Wflag) ?
311ade9ccfeSMarcel Moolenaar     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%s}" :
312046aad39SHajimu UMEMOTO 				    ((!Wflag || af1 == AF_INET) ?
313046aad39SHajimu UMEMOTO     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%s}" :
314046aad39SHajimu UMEMOTO     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%s}"),
315f5d34df5SGeorge V. Neville-Neil 				    "Proto", "Rexmit", "OOORcv", "0-win",
31649f287f8SGeorge V. Neville-Neil 				    "Local Address", "Foreign Address");
317afc74015SRuslan Ermilov 			else {
318ade9ccfeSMarcel Moolenaar 				xo_emit((Aflag && !Wflag) ?
319ade9ccfeSMarcel Moolenaar     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%-18.18s}" :
320046aad39SHajimu UMEMOTO 				    ((!Wflag || af1 == AF_INET) ?
321046aad39SHajimu UMEMOTO     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%-22.22s}" :
322046aad39SHajimu UMEMOTO     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%-45.45s}"),
323afc74015SRuslan Ermilov 				    "Proto", "Recv-Q", "Send-Q",
324afc74015SRuslan Ermilov 				    "Local Address", "Foreign Address");
32585b0f0f3SAdrian Chadd 				if (!xflag && !Rflag)
3262529f56eSJonathan T. Looney 					xo_emit(" {T:/%-11.11s}", "(state)");
327afc74015SRuslan Ermilov 			}
328b8614722SMike Silbersack 			if (xflag) {
329ade9ccfeSMarcel Moolenaar 				xo_emit(" {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
330ade9ccfeSMarcel Moolenaar 				    "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
331ade9ccfeSMarcel Moolenaar 				    "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
332ade9ccfeSMarcel Moolenaar 				    "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s}",
333ade9ccfeSMarcel Moolenaar 				    "R-MBUF", "S-MBUF", "R-CLUS", "S-CLUS",
334ade9ccfeSMarcel Moolenaar 				    "R-HIWA", "S-HIWA", "R-LOWA", "S-LOWA",
335ade9ccfeSMarcel Moolenaar 				    "R-BCNT", "S-BCNT", "R-BMAX", "S-BMAX");
336ade9ccfeSMarcel Moolenaar 				xo_emit(" {T:/%7.7s} {T:/%7.7s} {T:/%7.7s} "
337ade9ccfeSMarcel Moolenaar 				    "{T:/%7.7s} {T:/%7.7s} {T:/%7.7s}",
338ade9ccfeSMarcel Moolenaar 				    "rexmt", "persist", "keep", "2msl",
339ade9ccfeSMarcel Moolenaar 				    "delack", "rcvtime");
34085b0f0f3SAdrian Chadd 			} else if (Rflag) {
341ade9ccfeSMarcel Moolenaar 				xo_emit("  {T:/%8.8s} {T:/%5.5s}",
34285b0f0f3SAdrian Chadd 				    "flowid", "ftype");
34394f138feSGeorge V. Neville-Neil 			}
3442529f56eSJonathan T. Looney 			if (Pflag)
3452529f56eSJonathan T. Looney 				xo_emit(" {T:/%s}", "Log ID");
346ade9ccfeSMarcel Moolenaar 			xo_emit("\n");
3479b50d902SRodney W. Grimes 			first = 0;
3489b50d902SRodney W. Grimes 		}
349fb5d0fbdSRuslan Ermilov 		if (Lflag && so->so_qlimit == 0)
350fb5d0fbdSRuslan Ermilov 			continue;
351ade9ccfeSMarcel Moolenaar 		xo_open_instance("socket");
352e4ec3989SDag-Erling Smørgrav 		if (Aflag) {
353e4ec3989SDag-Erling Smørgrav 			if (istcp)
354ade9ccfeSMarcel Moolenaar 				xo_emit("{q:address/%*lx} ",
355ade9ccfeSMarcel Moolenaar 				    2 * (int)sizeof(void *),
356ade9ccfeSMarcel Moolenaar 				    (u_long)inp->inp_ppcb);
357e4ec3989SDag-Erling Smørgrav 			else
358d9646465SGleb Smirnoff 				xo_emit("{q:address/%*lx} ",
359ade9ccfeSMarcel Moolenaar 				    2 * (int)sizeof(void *),
360ade9ccfeSMarcel Moolenaar 				    (u_long)so->so_pcb);
361e4ec3989SDag-Erling Smørgrav 		}
362cfa1ca9dSYoshinobu Inoue #ifdef INET6
363fc60ab7bSYoshinobu Inoue 		if ((inp->inp_vflag & INP_IPV6) != 0)
3643feeb332SDavid E. O'Brien 			vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
3653feeb332SDavid E. O'Brien 			    "46" : "6";
366fc60ab7bSYoshinobu Inoue 		else
367cfa1ca9dSYoshinobu Inoue #endif
3683feeb332SDavid E. O'Brien 		vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
3693feeb332SDavid E. O'Brien 		    "4" : "";
37009fe6320SNavdeep Parhar 		if (istcp && (tp->t_flags & TF_TOE) != 0)
371ade9ccfeSMarcel Moolenaar 			xo_emit("{:protocol/%-3.3s%-2.2s/%s%s} ", "toe", vchar);
37209fe6320SNavdeep Parhar 		else
373ade9ccfeSMarcel Moolenaar 			xo_emit("{:protocol/%-3.3s%-2.2s/%s%s} ", name, vchar);
374fb5d0fbdSRuslan Ermilov 		if (Lflag) {
3757325dfbbSAlfred Perlstein 			char buf1[33];
376fc60ab7bSYoshinobu Inoue 
3777325dfbbSAlfred Perlstein 			snprintf(buf1, sizeof buf1, "%u/%u/%u", so->so_qlen,
378fb5d0fbdSRuslan Ermilov 			    so->so_incqlen, so->so_qlimit);
3797325dfbbSAlfred Perlstein 			xo_emit("{:listen-queue-sizes/%-32.32s} ", buf1);
380f5d34df5SGeorge V. Neville-Neil 		} else if (Tflag) {
381f5d34df5SGeorge V. Neville-Neil 			if (istcp)
382ade9ccfeSMarcel Moolenaar 				xo_emit("{:sent-retransmit-packets/%6u} "
383ade9ccfeSMarcel Moolenaar 				    "{:received-out-of-order-packets/%6u} "
384ade9ccfeSMarcel Moolenaar 				    "{:sent-zero-window/%6u} ",
385ade9ccfeSMarcel Moolenaar 				    tp->t_sndrexmitpack, tp->t_rcvoopack,
386ade9ccfeSMarcel Moolenaar 				    tp->t_sndzerowin);
3870eaa116eSHajimu UMEMOTO 			else
3880eaa116eSHajimu UMEMOTO 				xo_emit("{P:/%21s}", "");
389fb5d0fbdSRuslan Ermilov 		} else {
390ade9ccfeSMarcel Moolenaar 			xo_emit("{:receive-bytes-waiting/%6u} "
391ade9ccfeSMarcel Moolenaar 			    "{:send-bytes-waiting/%6u} ",
3920f9d0a73SGleb Smirnoff 			    so->so_rcv.sb_cc, so->so_snd.sb_cc);
393fc60ab7bSYoshinobu Inoue 		}
39465ea0024SAssar Westerlund 		if (numeric_port) {
3959784899dSBjoern A. Zeeb #ifdef INET
396cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
397ade9ccfeSMarcel Moolenaar 				inetprint("local", &inp->inp_laddr,
398046aad39SHajimu UMEMOTO 				    (int)inp->inp_lport, name, 1, af1);
399ac55add0SGuido van Rooij 				if (!Lflag)
400ade9ccfeSMarcel Moolenaar 					inetprint("remote", &inp->inp_faddr,
401046aad39SHajimu UMEMOTO 					    (int)inp->inp_fport, name, 1, af1);
402cfa1ca9dSYoshinobu Inoue 			}
4039784899dSBjoern A. Zeeb #endif
4049784899dSBjoern A. Zeeb #if defined(INET) && defined(INET6)
4059784899dSBjoern A. Zeeb 			else
4069784899dSBjoern A. Zeeb #endif
407cfa1ca9dSYoshinobu Inoue #ifdef INET6
4089784899dSBjoern A. Zeeb 			if (inp->inp_vflag & INP_IPV6) {
409ade9ccfeSMarcel Moolenaar 				inet6print("local", &inp->in6p_laddr,
410cfa1ca9dSYoshinobu Inoue 				    (int)inp->inp_lport, name, 1);
411ac55add0SGuido van Rooij 				if (!Lflag)
412ade9ccfeSMarcel Moolenaar 					inet6print("remote", &inp->in6p_faddr,
413cfa1ca9dSYoshinobu Inoue 					    (int)inp->inp_fport, name, 1);
414cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
415cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
4164f81ef50SGarrett Wollman 		} else if (inp->inp_flags & INP_ANONPORT) {
4179784899dSBjoern A. Zeeb #ifdef INET
418cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
419ade9ccfeSMarcel Moolenaar 				inetprint("local", &inp->inp_laddr,
420046aad39SHajimu UMEMOTO 				    (int)inp->inp_lport, name, 1, af1);
421ac55add0SGuido van Rooij 				if (!Lflag)
422ade9ccfeSMarcel Moolenaar 					inetprint("remote", &inp->inp_faddr,
423046aad39SHajimu UMEMOTO 					    (int)inp->inp_fport, name, 0, af1);
424cfa1ca9dSYoshinobu Inoue 			}
4259784899dSBjoern A. Zeeb #endif
4269784899dSBjoern A. Zeeb #if defined(INET) && defined(INET6)
4279784899dSBjoern A. Zeeb 			else
4289784899dSBjoern A. Zeeb #endif
429cfa1ca9dSYoshinobu Inoue #ifdef INET6
4309784899dSBjoern A. Zeeb 			if (inp->inp_vflag & INP_IPV6) {
431ade9ccfeSMarcel Moolenaar 				inet6print("local", &inp->in6p_laddr,
432cfa1ca9dSYoshinobu Inoue 				    (int)inp->inp_lport, name, 1);
433ac55add0SGuido van Rooij 				if (!Lflag)
434ade9ccfeSMarcel Moolenaar 					inet6print("remote", &inp->in6p_faddr,
435cfa1ca9dSYoshinobu Inoue 					    (int)inp->inp_fport, name, 0);
436cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
437cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
4388d612dd2SPoul-Henning Kamp 		} else {
4399784899dSBjoern A. Zeeb #ifdef INET
440cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
441ade9ccfeSMarcel Moolenaar 				inetprint("local", &inp->inp_laddr,
442046aad39SHajimu UMEMOTO 				    (int)inp->inp_lport, name, 0, af1);
443ac55add0SGuido van Rooij 				if (!Lflag)
444ade9ccfeSMarcel Moolenaar 					inetprint("remote", &inp->inp_faddr,
445ac55add0SGuido van Rooij 					    (int)inp->inp_fport, name,
446046aad39SHajimu UMEMOTO 					    inp->inp_lport != inp->inp_fport,
447046aad39SHajimu UMEMOTO 					    af1);
448cfa1ca9dSYoshinobu Inoue 			}
4499784899dSBjoern A. Zeeb #endif
4509784899dSBjoern A. Zeeb #if defined(INET) && defined(INET6)
4519784899dSBjoern A. Zeeb 			else
4529784899dSBjoern A. Zeeb #endif
453cfa1ca9dSYoshinobu Inoue #ifdef INET6
4549784899dSBjoern A. Zeeb 			if (inp->inp_vflag & INP_IPV6) {
455ade9ccfeSMarcel Moolenaar 				inet6print("local", &inp->in6p_laddr,
456cfa1ca9dSYoshinobu Inoue 				    (int)inp->inp_lport, name, 0);
457ac55add0SGuido van Rooij 				if (!Lflag)
458ade9ccfeSMarcel Moolenaar 					inet6print("remote", &inp->in6p_faddr,
459cfa1ca9dSYoshinobu Inoue 					    (int)inp->inp_fport, name,
4603feeb332SDavid E. O'Brien 					    inp->inp_lport != inp->inp_fport);
461cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
462cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
4638d612dd2SPoul-Henning Kamp 		}
46449f287f8SGeorge V. Neville-Neil 		if (xflag) {
465ade9ccfeSMarcel Moolenaar 			xo_emit("{:receive-mbufs/%6u} {:send-mbufs/%6u} "
466ade9ccfeSMarcel Moolenaar 			    "{:receive-clusters/%6u} {:send-clusters/%6u} "
467ade9ccfeSMarcel Moolenaar 			    "{:receive-high-water/%6u} {:send-high-water/%6u} "
468ade9ccfeSMarcel Moolenaar 			    "{:receive-low-water/%6u} {:send-low-water/%6u} "
469ade9ccfeSMarcel Moolenaar 			    "{:receive-mbuf-bytes/%6u} {:send-mbuf-bytes/%6u} "
470ade9ccfeSMarcel Moolenaar 			    "{:receive-mbuf-bytes-max/%6u} "
471ade9ccfeSMarcel Moolenaar 			    "{:send-mbuf-bytes-max/%6u}",
47249f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt,
47349f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt,
47449f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
47549f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
47649f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
47749f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
478cc65eb4eSGleb Smirnoff 			if (istcp)
479ade9ccfeSMarcel Moolenaar 				xo_emit(" {:retransmit-timer/%4d.%02d} "
480ade9ccfeSMarcel Moolenaar 				    "{:persist-timer/%4d.%02d} "
481ade9ccfeSMarcel Moolenaar 				    "{:keepalive-timer/%4d.%02d} "
482ade9ccfeSMarcel Moolenaar 				    "{:msl2-timer/%4d.%02d} "
483ade9ccfeSMarcel Moolenaar 				    "{:delay-ack-timer/%4d.%02d} "
484ade9ccfeSMarcel Moolenaar 				    "{:inactivity-timer/%4d.%02d}",
485cc65eb4eSGleb Smirnoff 				    tp->tt_rexmt / 1000,
486cc65eb4eSGleb Smirnoff 				    (tp->tt_rexmt % 1000) / 10,
487cc65eb4eSGleb Smirnoff 				    tp->tt_persist / 1000,
488cc65eb4eSGleb Smirnoff 				    (tp->tt_persist % 1000) / 10,
489cc65eb4eSGleb Smirnoff 				    tp->tt_keep / 1000,
490cc65eb4eSGleb Smirnoff 				    (tp->tt_keep % 1000) / 10,
491cc65eb4eSGleb Smirnoff 				    tp->tt_2msl / 1000,
492cc65eb4eSGleb Smirnoff 				    (tp->tt_2msl % 1000) / 10,
493cc65eb4eSGleb Smirnoff 				    tp->tt_delack / 1000,
494cc65eb4eSGleb Smirnoff 				    (tp->tt_delack % 1000) / 10,
495cc65eb4eSGleb Smirnoff 				    tp->t_rcvtime / 1000,
496cc65eb4eSGleb Smirnoff 				    (tp->t_rcvtime % 1000) / 10);
497b8614722SMike Silbersack 		}
49885b0f0f3SAdrian Chadd 		if (istcp && !Lflag && !xflag && !Tflag && !Rflag) {
4994f81ef50SGarrett Wollman 			if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
5002529f56eSJonathan T. Looney 				xo_emit("{:tcp-state/%-11d}", tp->t_state);
5019a94a597SGarrett Wollman 			else {
5022529f56eSJonathan T. Looney 				xo_emit("{:tcp-state/%-11s}",
503ade9ccfeSMarcel Moolenaar 				    tcpstates[tp->t_state]);
504513822ddSAdam David #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
5059a94a597SGarrett Wollman 				/* Show T/TCP `hidden state' */
5064f81ef50SGarrett Wollman 				if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
507ade9ccfeSMarcel Moolenaar 					xo_emit("{:need-syn-or-fin/*}");
508513822ddSAdam David #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
5099a94a597SGarrett Wollman 			}
5109b50d902SRodney W. Grimes 		}
51185b0f0f3SAdrian Chadd 		if (Rflag) {
512ade9ccfeSMarcel Moolenaar 			/* XXX: is this right Alfred */
513ade9ccfeSMarcel Moolenaar 			xo_emit(" {:flow-id/%08x} {:flow-type/%5d}",
51485b0f0f3SAdrian Chadd 			    inp->inp_flowid,
51585b0f0f3SAdrian Chadd 			    inp->inp_flowtype);
51685b0f0f3SAdrian Chadd 		}
5172529f56eSJonathan T. Looney 		if (istcp && Pflag)
5182529f56eSJonathan T. Looney 			xo_emit(" {:log-id/%s}", tp->xt_logid[0] == '\0' ?
5192529f56eSJonathan T. Looney 			    "-" : tp->xt_logid);
520ade9ccfeSMarcel Moolenaar 		xo_emit("\n");
521ade9ccfeSMarcel Moolenaar 		xo_close_instance("socket");
5229b50d902SRodney W. Grimes 	}
5234f81ef50SGarrett Wollman 	if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
5244f81ef50SGarrett Wollman 		if (oxig->xig_count > xig->xig_count) {
525ade9ccfeSMarcel Moolenaar 			xo_emit("Some {d:lost/%s} sockets may have been "
526ade9ccfeSMarcel Moolenaar 			    "deleted.\n", name);
5274f81ef50SGarrett Wollman 		} else if (oxig->xig_count < xig->xig_count) {
528ade9ccfeSMarcel Moolenaar 			xo_emit("Some {d:created/%s} sockets may have been "
529ade9ccfeSMarcel Moolenaar 			    "created.\n", name);
5304f81ef50SGarrett Wollman 		} else {
531ade9ccfeSMarcel Moolenaar 			xo_emit("Some {d:changed/%s} sockets may have been "
532ade9ccfeSMarcel Moolenaar 			    "created or deleted.\n", name);
5334f81ef50SGarrett Wollman 		}
5344f81ef50SGarrett Wollman 	}
5354f81ef50SGarrett Wollman 	free(buf);
5369b50d902SRodney W. Grimes }
5379b50d902SRodney W. Grimes 
5389b50d902SRodney W. Grimes /*
5399b50d902SRodney W. Grimes  * Dump TCP statistics structure.
5409b50d902SRodney W. Grimes  */
5419b50d902SRodney W. Grimes void
542feda1a43SJohn Baldwin tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
5439b50d902SRodney W. Grimes {
5449eddb899SMark Johnston 	struct tcpstat tcpstat;
545dbfd8708SGleb Smirnoff 	uint64_t tcps_states[TCP_NSTATES];
5469b50d902SRodney W. Grimes 
547cfa1ca9dSYoshinobu Inoue #ifdef INET6
548cfa1ca9dSYoshinobu Inoue 	if (tcp_done != 0)
549cfa1ca9dSYoshinobu Inoue 		return;
550cfa1ca9dSYoshinobu Inoue 	else
551cfa1ca9dSYoshinobu Inoue 		tcp_done = 1;
552cfa1ca9dSYoshinobu Inoue #endif
553cfa1ca9dSYoshinobu Inoue 
5549eddb899SMark Johnston 	if (fetch_stats("net.inet.tcp.stats", off, &tcpstat,
5559eddb899SMark Johnston 	    sizeof(tcpstat), kread_counters) != 0)
556feda1a43SJohn Baldwin 		return;
557feda1a43SJohn Baldwin 
558dbfd8708SGleb Smirnoff 	if (fetch_stats_ro("net.inet.tcp.states", nl[N_TCPS_STATES].n_value,
559dbfd8708SGleb Smirnoff 	    &tcps_states, sizeof(tcps_states), kread_counters) != 0)
560dbfd8708SGleb Smirnoff 		return;
561dbfd8708SGleb Smirnoff 
562ade9ccfeSMarcel Moolenaar 	xo_open_container("tcp");
563ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
5649b50d902SRodney W. Grimes 
5659b50d902SRodney W. Grimes #define	p(f, m) if (tcpstat.f || sflag <= 1)				\
566ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )tcpstat.f, plural(tcpstat.f))
56722694ebaSBruce Evans #define	p1a(f, m) if (tcpstat.f || sflag <= 1)				\
568ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )tcpstat.f)
5699b50d902SRodney W. Grimes #define	p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1)	\
570ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1),		\
5715923c293SGleb Smirnoff 	    (uintmax_t )tcpstat.f2, plural(tcpstat.f2))
57222694ebaSBruce Evans #define	p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1)	\
573ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1),		\
5745923c293SGleb Smirnoff 	    (uintmax_t )tcpstat.f2)
5759b50d902SRodney W. Grimes #define	p3(f, m) if (tcpstat.f || sflag <= 1)				\
576ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )tcpstat.f, pluralies(tcpstat.f))
5779b50d902SRodney W. Grimes 
578ade9ccfeSMarcel Moolenaar 	p(tcps_sndtotal, "\t{:sent-packets/%ju} {N:/packet%s sent}\n");
579ade9ccfeSMarcel Moolenaar 	p2(tcps_sndpack,tcps_sndbyte, "\t\t{:sent-data-packets/%ju} "
580ade9ccfeSMarcel Moolenaar 	    "{N:/data packet%s} ({:sent-data-bytes/%ju} {N:/byte%s})\n");
581ade9ccfeSMarcel Moolenaar 	p2(tcps_sndrexmitpack, tcps_sndrexmitbyte, "\t\t"
582ade9ccfeSMarcel Moolenaar 	    "{:sent-retransmitted-packets/%ju} {N:/data packet%s} "
583ade9ccfeSMarcel Moolenaar 	    "({:sent-retransmitted-bytes/%ju} {N:/byte%s}) "
584ade9ccfeSMarcel Moolenaar 	    "{N:retransmitted}\n");
585ade9ccfeSMarcel Moolenaar 	p(tcps_sndrexmitbad, "\t\t"
586ade9ccfeSMarcel Moolenaar 	    "{:sent-unnecessary-retransmitted-packets/%ju} "
587ade9ccfeSMarcel Moolenaar 	    "{N:/data packet%s unnecessarily retransmitted}\n");
588ade9ccfeSMarcel Moolenaar 	p(tcps_mturesent, "\t\t{:sent-resends-by-mtu-discovery/%ju} "
589ade9ccfeSMarcel Moolenaar 	    "{N:/resend%s initiated by MTU discovery}\n");
590ade9ccfeSMarcel Moolenaar 	p2a(tcps_sndacks, tcps_delack, "\t\t{:sent-ack-only-packets/%ju} "
591ade9ccfeSMarcel Moolenaar 	    "{N:/ack-only packet%s/} ({:sent-packets-delayed/%ju} "
592ade9ccfeSMarcel Moolenaar 	    "{N:delayed})\n");
593ade9ccfeSMarcel Moolenaar 	p(tcps_sndurg, "\t\t{:sent-urg-only-packets/%ju} "
594ade9ccfeSMarcel Moolenaar 	    "{N:/URG only packet%s}\n");
595ade9ccfeSMarcel Moolenaar 	p(tcps_sndprobe, "\t\t{:sent-window-probe-packets/%ju} "
596ade9ccfeSMarcel Moolenaar 	    "{N:/window probe packet%s}\n");
597ade9ccfeSMarcel Moolenaar 	p(tcps_sndwinup, "\t\t{:sent-window-update-packets/%ju} "
598ade9ccfeSMarcel Moolenaar 	    "{N:/window update packet%s}\n");
599ade9ccfeSMarcel Moolenaar 	p(tcps_sndctrl, "\t\t{:sent-control-packets/%ju} "
600ade9ccfeSMarcel Moolenaar 	    "{N:/control packet%s}\n");
601ade9ccfeSMarcel Moolenaar 	p(tcps_rcvtotal, "\t{:received-packets/%ju} "
602ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s received}\n");
603ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t"
604ade9ccfeSMarcel Moolenaar 	    "{:received-ack-packets/%ju} {N:/ack%s} "
605ade9ccfeSMarcel Moolenaar 	    "{N:(for} {:received-ack-bytes/%ju} {N:/byte%s})\n");
606ade9ccfeSMarcel Moolenaar 	p(tcps_rcvdupack, "\t\t{:received-duplicate-acks/%ju} "
607ade9ccfeSMarcel Moolenaar 	    "{N:/duplicate ack%s}\n");
608ade9ccfeSMarcel Moolenaar 	p(tcps_rcvacktoomuch, "\t\t{:received-acks-for-unsent-data/%ju} "
609ade9ccfeSMarcel Moolenaar 	    "{N:/ack%s for unsent data}\n");
610ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvpack, tcps_rcvbyte, "\t\t"
611ade9ccfeSMarcel Moolenaar 	    "{:received-in-sequence-packets/%ju} {N:/packet%s} "
612ade9ccfeSMarcel Moolenaar 	    "({:received-in-sequence-bytes/%ju} {N:/byte%s}) "
613ade9ccfeSMarcel Moolenaar 	    "{N:received in-sequence}\n");
614ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvduppack, tcps_rcvdupbyte, "\t\t"
615ade9ccfeSMarcel Moolenaar 	    "{:received-completely-duplicate-packets/%ju} "
616ade9ccfeSMarcel Moolenaar 	    "{N:/completely duplicate packet%s} "
617ade9ccfeSMarcel Moolenaar 	    "({:received-completely-duplicate-bytes/%ju} {N:/byte%s})\n");
618ade9ccfeSMarcel Moolenaar 	p(tcps_pawsdrop, "\t\t{:received-old-duplicate-packets/%ju} "
619ade9ccfeSMarcel Moolenaar 	    "{N:/old duplicate packet%s}\n");
620ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte, "\t\t"
621ade9ccfeSMarcel Moolenaar 	    "{:received-some-duplicate-packets/%ju} "
622ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s with some dup. data} "
623ade9ccfeSMarcel Moolenaar 	    "({:received-some-duplicate-bytes/%ju} {N:/byte%s duped/})\n");
624ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvoopack, tcps_rcvoobyte, "\t\t{:received-out-of-order/%ju} "
625ade9ccfeSMarcel Moolenaar 	    "{N:/out-of-order packet%s} "
626ade9ccfeSMarcel Moolenaar 	    "({:received-out-of-order-bytes/%ju} {N:/byte%s})\n");
627ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin, "\t\t"
628ade9ccfeSMarcel Moolenaar 	    "{:received-after-window-packets/%ju} {N:/packet%s} "
629ade9ccfeSMarcel Moolenaar 	    "({:received-after-window-bytes/%ju} {N:/byte%s}) "
630ade9ccfeSMarcel Moolenaar 	    "{N:of data after window}\n");
631ade9ccfeSMarcel Moolenaar 	p(tcps_rcvwinprobe, "\t\t{:received-window-probes/%ju} "
632ade9ccfeSMarcel Moolenaar 	    "{N:/window probe%s}\n");
633ade9ccfeSMarcel Moolenaar 	p(tcps_rcvwinupd, "\t\t{:receive-window-update-packets/%ju} "
634ade9ccfeSMarcel Moolenaar 	    "{N:/window update packet%s}\n");
635ade9ccfeSMarcel Moolenaar 	p(tcps_rcvafterclose, "\t\t{:received-after-close-packets/%ju} "
636ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s received after close}\n");
637ade9ccfeSMarcel Moolenaar 	p(tcps_rcvbadsum, "\t\t{:discard-bad-checksum/%ju} "
638ade9ccfeSMarcel Moolenaar 	    "{N:/discarded for bad checksum%s}\n");
639ade9ccfeSMarcel Moolenaar 	p(tcps_rcvbadoff, "\t\t{:discard-bad-header-offset/%ju} "
640ade9ccfeSMarcel Moolenaar 	    "{N:/discarded for bad header offset field%s}\n");
641ade9ccfeSMarcel Moolenaar 	p1a(tcps_rcvshort, "\t\t{:discard-too-short/%ju} "
642ade9ccfeSMarcel Moolenaar 	    "{N:discarded because packet too short}\n");
643ade9ccfeSMarcel Moolenaar 	p1a(tcps_rcvmemdrop, "\t\t{:discard-memory-problems/%ju} "
644ade9ccfeSMarcel Moolenaar 	    "{N:discarded due to memory problems}\n");
645ade9ccfeSMarcel Moolenaar 	p(tcps_connattempt, "\t{:connection-requests/%ju} "
646ade9ccfeSMarcel Moolenaar 	    "{N:/connection request%s}\n");
647ade9ccfeSMarcel Moolenaar 	p(tcps_accepts, "\t{:connections-accepts/%ju} "
648ade9ccfeSMarcel Moolenaar 	    "{N:/connection accept%s}\n");
649ade9ccfeSMarcel Moolenaar 	p(tcps_badsyn, "\t{:bad-connection-attempts/%ju} "
650ade9ccfeSMarcel Moolenaar 	    "{N:/bad connection attempt%s}\n");
651ade9ccfeSMarcel Moolenaar 	p(tcps_listendrop, "\t{:listen-queue-overflows/%ju} "
652ade9ccfeSMarcel Moolenaar 	    "{N:/listen queue overflow%s}\n");
653ade9ccfeSMarcel Moolenaar 	p(tcps_badrst, "\t{:ignored-in-window-resets/%ju} "
654ade9ccfeSMarcel Moolenaar 	    "{N:/ignored RSTs in the window%s}\n");
655ade9ccfeSMarcel Moolenaar 	p(tcps_connects, "\t{:connections-established/%ju} "
656ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s established (including accepts)}\n");
657e9d2c201SMarcelo Araujo 	p(tcps_usedrtt, "\t\t{:connections-hostcache-rtt/%ju} "
658e9d2c201SMarcelo Araujo 	    "{N:/time%s used RTT from hostcache}\n");
659e9d2c201SMarcelo Araujo 	p(tcps_usedrttvar, "\t\t{:connections-hostcache-rttvar/%ju} "
660e9d2c201SMarcelo Araujo 	    "{N:/time%s used RTT variance from hostcache}\n");
661e9d2c201SMarcelo Araujo 	p(tcps_usedssthresh, "\t\t{:connections-hostcache-ssthresh/%ju} "
662e9d2c201SMarcelo Araujo 	    "{N:/time%s used slow-start threshold from hostcache}\n");
663ade9ccfeSMarcel Moolenaar 	p2(tcps_closed, tcps_drops, "\t{:connections-closed/%ju} "
664ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s closed (including} "
665ade9ccfeSMarcel Moolenaar 	    "{:connection-drops/%ju} {N:/drop%s})\n");
666ade9ccfeSMarcel Moolenaar 	p(tcps_cachedrtt, "\t\t{:connections-updated-rtt-on-close/%ju} "
667ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s updated cached RTT on close}\n");
668ade9ccfeSMarcel Moolenaar 	p(tcps_cachedrttvar, "\t\t"
669ade9ccfeSMarcel Moolenaar 	    "{:connections-updated-variance-on-close/%ju} "
670ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s updated cached RTT variance on close}\n");
671ade9ccfeSMarcel Moolenaar 	p(tcps_cachedssthresh, "\t\t"
672ade9ccfeSMarcel Moolenaar 	    "{:connections-updated-ssthresh-on-close/%ju} "
673ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s updated cached ssthresh on close}\n");
674ade9ccfeSMarcel Moolenaar 	p(tcps_conndrops, "\t{:embryonic-connections-dropped/%ju} "
675ade9ccfeSMarcel Moolenaar 	    "{N:/embryonic connection%s dropped}\n");
676ade9ccfeSMarcel Moolenaar 	p2(tcps_rttupdated, tcps_segstimed, "\t{:segments-updated-rtt/%ju} "
677ade9ccfeSMarcel Moolenaar 	    "{N:/segment%s updated rtt (of} "
678ade9ccfeSMarcel Moolenaar 	    "{:segment-update-attempts/%ju} {N:/attempt%s})\n");
679ade9ccfeSMarcel Moolenaar 	p(tcps_rexmttimeo, "\t{:retransmit-timeouts/%ju} "
680ade9ccfeSMarcel Moolenaar 	    "{N:/retransmit timeout%s}\n");
681ade9ccfeSMarcel Moolenaar 	p(tcps_timeoutdrop, "\t\t"
682ade9ccfeSMarcel Moolenaar 	    "{:connections-dropped-by-retransmit-timeout/%ju} "
683ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s dropped by rexmit timeout}\n");
684ade9ccfeSMarcel Moolenaar 	p(tcps_persisttimeo, "\t{:persist-timeout/%ju} "
685ade9ccfeSMarcel Moolenaar 	    "{N:/persist timeout%s}\n");
686ade9ccfeSMarcel Moolenaar 	p(tcps_persistdrop, "\t\t"
687ade9ccfeSMarcel Moolenaar 	    "{:connections-dropped-by-persist-timeout/%ju} "
688ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s dropped by persist timeout}\n");
689ade9ccfeSMarcel Moolenaar 	p(tcps_finwait2_drops, "\t"
690ade9ccfeSMarcel Moolenaar 	    "{:connections-dropped-by-finwait2-timeout/%ju} "
691ade9ccfeSMarcel Moolenaar 	    "{N:/Connection%s (fin_wait_2) dropped because of timeout}\n");
692ade9ccfeSMarcel Moolenaar 	p(tcps_keeptimeo, "\t{:keepalive-timeout/%ju} "
693ade9ccfeSMarcel Moolenaar 	    "{N:/keepalive timeout%s}\n");
694ade9ccfeSMarcel Moolenaar 	p(tcps_keepprobe, "\t\t{:keepalive-probes/%ju} "
695ade9ccfeSMarcel Moolenaar 	    "{N:/keepalive probe%s sent}\n");
696ade9ccfeSMarcel Moolenaar 	p(tcps_keepdrops, "\t\t{:connections-dropped-by-keepalives/%ju} "
697ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s dropped by keepalive}\n");
698ade9ccfeSMarcel Moolenaar 	p(tcps_predack, "\t{:ack-header-predictions/%ju} "
699ade9ccfeSMarcel Moolenaar 	    "{N:/correct ACK header prediction%s}\n");
700ade9ccfeSMarcel Moolenaar 	p(tcps_preddat, "\t{:data-packet-header-predictions/%ju} "
701ade9ccfeSMarcel Moolenaar 	    "{N:/correct data packet header prediction%s}\n");
70260a31b3aSJonathan Lemon 
703ade9ccfeSMarcel Moolenaar 	xo_open_container("syncache");
704b6101dafSPaul Saab 
705ade9ccfeSMarcel Moolenaar 	p3(tcps_sc_added, "\t{:entries-added/%ju} "
706ade9ccfeSMarcel Moolenaar 	    "{N:/syncache entr%s added}\n");
707ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_retransmitted, "\t\t{:retransmitted/%ju} "
708ade9ccfeSMarcel Moolenaar 	    "{N:/retransmitted}\n");
709ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_dupsyn, "\t\t{:duplicates/%ju} {N:/dupsyn}\n");
710ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_dropped, "\t\t{:dropped/%ju} {N:/dropped}\n");
711ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_completed, "\t\t{:completed/%ju} {N:/completed}\n");
712ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_bucketoverflow, "\t\t{:bucket-overflow/%ju} "
713ade9ccfeSMarcel Moolenaar 	    "{N:/bucket overflow}\n");
714ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_cacheoverflow, "\t\t{:cache-overflow/%ju} "
715ade9ccfeSMarcel Moolenaar 	    "{N:/cache overflow}\n");
716ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_reset, "\t\t{:reset/%ju} {N:/reset}\n");
717ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_stale, "\t\t{:stale/%ju} {N:/stale}\n");
718ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_aborted, "\t\t{:aborted/%ju} {N:/aborted}\n");
719ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_badack, "\t\t{:bad-ack/%ju} {N:/badack}\n");
720ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_unreach, "\t\t{:unreachable/%ju} {N:/unreach}\n");
721ade9ccfeSMarcel Moolenaar 	p(tcps_sc_zonefail, "\t\t{:zone-failures/%ju} {N:/zone failure%s}\n");
722ade9ccfeSMarcel Moolenaar 	p(tcps_sc_sendcookie, "\t{:sent-cookies/%ju} {N:/cookie%s sent}\n");
723ade9ccfeSMarcel Moolenaar 	p(tcps_sc_recvcookie, "\t{:receivd-cookies/%ju} "
724ade9ccfeSMarcel Moolenaar 	    "{N:/cookie%s received}\n");
725699ef999SRuslan Ermilov 
726ade9ccfeSMarcel Moolenaar 	xo_close_container("syncache");
727b6101dafSPaul Saab 
728ade9ccfeSMarcel Moolenaar 	xo_open_container("hostcache");
729a4993b25SBjoern A. Zeeb 
730ade9ccfeSMarcel Moolenaar 	p3(tcps_hc_added, "\t{:entries-added/%ju} "
731ade9ccfeSMarcel Moolenaar 	    "{N:/hostcache entr%s added}\n");
732ade9ccfeSMarcel Moolenaar 	p1a(tcps_hc_bucketoverflow, "\t\t{:buffer-overflows/%ju} "
733ade9ccfeSMarcel Moolenaar 	    "{N:/bucket overflow}\n");
734ade9ccfeSMarcel Moolenaar 
735ade9ccfeSMarcel Moolenaar 	xo_close_container("hostcache");
736ade9ccfeSMarcel Moolenaar 
737ade9ccfeSMarcel Moolenaar 	xo_open_container("sack");
738ade9ccfeSMarcel Moolenaar 
739ade9ccfeSMarcel Moolenaar 	p(tcps_sack_recovery_episode, "\t{:recovery-episodes/%ju} "
740ade9ccfeSMarcel Moolenaar 	    "{N:/SACK recovery episode%s}\n");
741ade9ccfeSMarcel Moolenaar  	p(tcps_sack_rexmits, "\t{:segment-retransmits/%ju} "
742ade9ccfeSMarcel Moolenaar 	    "{N:/segment rexmit%s in SACK recovery episodes}\n");
743ade9ccfeSMarcel Moolenaar  	p(tcps_sack_rexmit_bytes, "\t{:byte-retransmits/%ju} "
744ade9ccfeSMarcel Moolenaar 	    "{N:/byte rexmit%s in SACK recovery episodes}\n");
745ade9ccfeSMarcel Moolenaar  	p(tcps_sack_rcv_blocks, "\t{:received-blocks/%ju} "
746ade9ccfeSMarcel Moolenaar 	    "{N:/SACK option%s (SACK blocks) received}\n");
747ade9ccfeSMarcel Moolenaar 	p(tcps_sack_send_blocks, "\t{:sent-option-blocks/%ju} "
748ade9ccfeSMarcel Moolenaar 	    "{N:/SACK option%s (SACK blocks) sent}\n");
749ade9ccfeSMarcel Moolenaar 	p1a(tcps_sack_sboverflow, "\t{:scoreboard-overflows/%ju} "
750ade9ccfeSMarcel Moolenaar 	    "{N:/SACK scoreboard overflow}\n");
751ade9ccfeSMarcel Moolenaar 
752ade9ccfeSMarcel Moolenaar 	xo_close_container("sack");
753ade9ccfeSMarcel Moolenaar 	xo_open_container("ecn");
754ade9ccfeSMarcel Moolenaar 
755ade9ccfeSMarcel Moolenaar 	p(tcps_ecn_ce, "\t{:ce-packets/%ju} "
756ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s with ECN CE bit set}\n");
757ade9ccfeSMarcel Moolenaar 	p(tcps_ecn_ect0, "\t{:ect0-packets/%ju} "
758ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s with ECN ECT(0) bit set}\n");
759ade9ccfeSMarcel Moolenaar 	p(tcps_ecn_ect1, "\t{:ect1-packets/%ju} "
760ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s with ECN ECT(1) bit set}\n");
761ade9ccfeSMarcel Moolenaar 	p(tcps_ecn_shs, "\t{:handshakes/%ju} "
762ade9ccfeSMarcel Moolenaar 	    "{N:/successful ECN handshake%s}\n");
763ade9ccfeSMarcel Moolenaar 	p(tcps_ecn_rcwnd, "\t{:congestion-reductions/%ju} "
764ade9ccfeSMarcel Moolenaar 	    "{N:/time%s ECN reduced the congestion window}\n");
765fcf59617SAndrey V. Elsukov 
766fcf59617SAndrey V. Elsukov 	xo_close_container("ecn");
767fcf59617SAndrey V. Elsukov 	xo_open_container("tcp-signature");
768fcf59617SAndrey V. Elsukov 	p(tcps_sig_rcvgoodsig, "\t{:received-good-signature/%ju} "
769fcf59617SAndrey V. Elsukov 	    "{N:/packet%s with matching signature received}\n");
770fcf59617SAndrey V. Elsukov 	p(tcps_sig_rcvbadsig, "\t{:received-bad-signature/%ju} "
771fcf59617SAndrey V. Elsukov 	    "{N:/packet%s with bad signature received}\n");
772fcf59617SAndrey V. Elsukov 	p(tcps_sig_err_buildsig, "\t{:failed-make-signature/%ju} "
773fcf59617SAndrey V. Elsukov 	    "{N:/time%s failed to make signature due to no SA}\n");
774fcf59617SAndrey V. Elsukov 	p(tcps_sig_err_sigopt, "\t{:no-signature-expected/%ju} "
775fcf59617SAndrey V. Elsukov 	    "{N:/time%s unexpected signature received}\n");
776fcf59617SAndrey V. Elsukov 	p(tcps_sig_err_nosigopt, "\t{:no-signature-provided/%ju} "
777fcf59617SAndrey V. Elsukov 	    "{N:/time%s no signature provided by segment}\n");
77832a04bb8SSean Bruno 
77932a04bb8SSean Bruno 	xo_close_container("tcp-signature");
78032a04bb8SSean Bruno 	xo_open_container("pmtud");
78132a04bb8SSean Bruno 
78232a04bb8SSean Bruno 	p(tcps_pmtud_blackhole_activated, "\t{:pmtud-activated/%ju} "
78332a04bb8SSean Bruno 	    "{N:/Path MTU discovery black hole detection activation%s}\n");
78432a04bb8SSean Bruno 	p(tcps_pmtud_blackhole_activated_min_mss,
78532a04bb8SSean Bruno 	    "\t{:pmtud-activated-min-mss/%ju} "
78632a04bb8SSean Bruno 	    "{N:/Path MTU discovery black hole detection min MSS activation%s}\n");
78732a04bb8SSean Bruno 	p(tcps_pmtud_blackhole_failed, "\t{:pmtud-failed/%ju} "
78832a04bb8SSean Bruno 	    "{N:/Path MTU discovery black hole detection failure%s}\n");
7899b50d902SRodney W. Grimes  #undef p
79022694ebaSBruce Evans  #undef p1a
7919b50d902SRodney W. Grimes  #undef p2
79222694ebaSBruce Evans  #undef p2a
7939b50d902SRodney W. Grimes  #undef p3
79432a04bb8SSean Bruno 	xo_close_container("pmtud");
79532a04bb8SSean Bruno 
796dbfd8708SGleb Smirnoff 
797dbfd8708SGleb Smirnoff 	xo_open_container("TCP connection count by state");
798dbfd8708SGleb Smirnoff 	xo_emit("{T:/TCP connection count by state}:\n");
799dbfd8708SGleb Smirnoff 	for (int i = 0; i < TCP_NSTATES; i++) {
800dbfd8708SGleb Smirnoff 		/*
801dbfd8708SGleb Smirnoff 		 * XXXGL: is there a way in libxo to use %s
802dbfd8708SGleb Smirnoff 		 * in the "content string" of a format
803dbfd8708SGleb Smirnoff 		 * string? I failed to do that, that's why
804dbfd8708SGleb Smirnoff 		 * a temporary buffer is used to construct
805dbfd8708SGleb Smirnoff 		 * format string for xo_emit().
806dbfd8708SGleb Smirnoff 		 */
807dbfd8708SGleb Smirnoff 		char fmtbuf[80];
808dbfd8708SGleb Smirnoff 
809dbfd8708SGleb Smirnoff 		if (sflag > 1 && tcps_states[i] == 0)
810dbfd8708SGleb Smirnoff 			continue;
811dbfd8708SGleb Smirnoff 		snprintf(fmtbuf, sizeof(fmtbuf), "\t{:%s/%%ju} "
812dbfd8708SGleb Smirnoff                     "{Np:/connection ,connections} in %s state\n",
813dbfd8708SGleb Smirnoff 		    tcpstates[i], tcpstates[i]);
814dbfd8708SGleb Smirnoff 		xo_emit(fmtbuf, (uintmax_t )tcps_states[i]);
815dbfd8708SGleb Smirnoff 	}
816dbfd8708SGleb Smirnoff 	xo_close_container("TCP connection count by state");
817dbfd8708SGleb Smirnoff 
818ade9ccfeSMarcel Moolenaar 	xo_close_container("tcp");
8199b50d902SRodney W. Grimes }
8209b50d902SRodney W. Grimes 
8219b50d902SRodney W. Grimes /*
8229b50d902SRodney W. Grimes  * Dump UDP statistics structure.
8239b50d902SRodney W. Grimes  */
8249b50d902SRodney W. Grimes void
825feda1a43SJohn Baldwin udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
8269b50d902SRodney W. Grimes {
8279eddb899SMark Johnston 	struct udpstat udpstat;
828c80211e3SAndrey V. Elsukov 	uint64_t delivered;
8299b50d902SRodney W. Grimes 
830cfa1ca9dSYoshinobu Inoue #ifdef INET6
831cfa1ca9dSYoshinobu Inoue 	if (udp_done != 0)
832cfa1ca9dSYoshinobu Inoue 		return;
833cfa1ca9dSYoshinobu Inoue 	else
834cfa1ca9dSYoshinobu Inoue 		udp_done = 1;
835cfa1ca9dSYoshinobu Inoue #endif
836cfa1ca9dSYoshinobu Inoue 
8379eddb899SMark Johnston 	if (fetch_stats("net.inet.udp.stats", off, &udpstat,
8389eddb899SMark Johnston 	    sizeof(udpstat), kread_counters) != 0)
839feda1a43SJohn Baldwin 		return;
840feda1a43SJohn Baldwin 
841ade9ccfeSMarcel Moolenaar 	xo_open_container("udp");
842ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
843ade9ccfeSMarcel Moolenaar 
8449b50d902SRodney W. Grimes #define	p(f, m) if (udpstat.f || sflag <= 1) \
845ade9ccfeSMarcel Moolenaar 	xo_emit("\t" m, (uintmax_t)udpstat.f, plural(udpstat.f))
84622694ebaSBruce Evans #define	p1a(f, m) if (udpstat.f || sflag <= 1) \
847ade9ccfeSMarcel Moolenaar 	xo_emit("\t" m, (uintmax_t)udpstat.f)
848ade9ccfeSMarcel Moolenaar 
849ade9ccfeSMarcel Moolenaar 	p(udps_ipackets, "{:received-datagrams/%ju} "
850ade9ccfeSMarcel Moolenaar 	    "{N:/datagram%s received}\n");
851ade9ccfeSMarcel Moolenaar 	p1a(udps_hdrops, "{:dropped-incomplete-headers/%ju} "
852ade9ccfeSMarcel Moolenaar 	    "{N:/with incomplete header}\n");
853ade9ccfeSMarcel Moolenaar 	p1a(udps_badlen, "{:dropped-bad-data-length/%ju} "
854ade9ccfeSMarcel Moolenaar 	    "{N:/with bad data length field}\n");
855ade9ccfeSMarcel Moolenaar 	p1a(udps_badsum, "{:dropped-bad-checksum/%ju} "
856ade9ccfeSMarcel Moolenaar 	    "{N:/with bad checksum}\n");
857ade9ccfeSMarcel Moolenaar 	p1a(udps_nosum, "{:dropped-no-checksum/%ju} "
858ade9ccfeSMarcel Moolenaar 	    "{N:/with no checksum}\n");
859ade9ccfeSMarcel Moolenaar 	p1a(udps_noport, "{:dropped-no-socket/%ju} "
860ade9ccfeSMarcel Moolenaar 	    "{N:/dropped due to no socket}\n");
861ade9ccfeSMarcel Moolenaar 	p(udps_noportbcast, "{:dropped-broadcast-multicast/%ju} "
862ade9ccfeSMarcel Moolenaar 	    "{N:/broadcast\\/multicast datagram%s undelivered}\n");
863ade9ccfeSMarcel Moolenaar 	p1a(udps_fullsock, "{:dropped-full-socket-buffer/%ju} "
864ade9ccfeSMarcel Moolenaar 	    "{N:/dropped due to full socket buffers}\n");
865ade9ccfeSMarcel Moolenaar 	p1a(udpps_pcbhashmiss, "{:not-for-hashed-pcb/%ju} "
866ade9ccfeSMarcel Moolenaar 	    "{N:/not for hashed pcb}\n");
8679b50d902SRodney W. Grimes 	delivered = udpstat.udps_ipackets -
8689b50d902SRodney W. Grimes 		    udpstat.udps_hdrops -
8699b50d902SRodney W. Grimes 		    udpstat.udps_badlen -
8709b50d902SRodney W. Grimes 		    udpstat.udps_badsum -
8719b50d902SRodney W. Grimes 		    udpstat.udps_noport -
8729b50d902SRodney W. Grimes 		    udpstat.udps_noportbcast -
8739b50d902SRodney W. Grimes 		    udpstat.udps_fullsock;
8749b50d902SRodney W. Grimes 	if (delivered || sflag <= 1)
875ade9ccfeSMarcel Moolenaar 		xo_emit("\t{:delivered-packets/%ju} {N:/delivered}\n",
876ade9ccfeSMarcel Moolenaar 		    (uint64_t)delivered);
877ade9ccfeSMarcel Moolenaar 	p(udps_opackets, "{:output-packets/%ju} {N:/datagram%s output}\n");
87871498f30SBruce M Simpson 	/* the next statistic is cumulative in udps_noportbcast */
879ade9ccfeSMarcel Moolenaar 	p(udps_filtermcast, "{:multicast-source-filter-matches/%ju} "
880ade9ccfeSMarcel Moolenaar 	    "{N:/time%s multicast source filter matched}\n");
8819b50d902SRodney W. Grimes #undef p
88222694ebaSBruce Evans #undef p1a
883ade9ccfeSMarcel Moolenaar 	xo_close_container("udp");
8849b50d902SRodney W. Grimes }
8859b50d902SRodney W. Grimes 
8869b50d902SRodney W. Grimes /*
887a9771948SGleb Smirnoff  * Dump CARP statistics structure.
888a9771948SGleb Smirnoff  */
889a9771948SGleb Smirnoff void
890feda1a43SJohn Baldwin carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
891a9771948SGleb Smirnoff {
8929eddb899SMark Johnston 	struct carpstats carpstat;
893a9771948SGleb Smirnoff 
8949eddb899SMark Johnston 	if (fetch_stats("net.inet.carp.stats", off, &carpstat,
8959eddb899SMark Johnston 	    sizeof(carpstat), kread_counters) != 0)
896a9771948SGleb Smirnoff 		return;
897a9771948SGleb Smirnoff 
898ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
899ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
900a9771948SGleb Smirnoff 
901a9771948SGleb Smirnoff #define	p(f, m) if (carpstat.f || sflag <= 1) \
902ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t)carpstat.f, plural(carpstat.f))
903a9771948SGleb Smirnoff #define	p2(f, m) if (carpstat.f || sflag <= 1) \
904ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t)carpstat.f)
905a9771948SGleb Smirnoff 
906ade9ccfeSMarcel Moolenaar 	p(carps_ipackets, "\t{:received-inet-packets/%ju} "
907ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s received (IPv4)}\n");
908ade9ccfeSMarcel Moolenaar 	p(carps_ipackets6, "\t{:received-inet6-packets/%ju} "
909ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s received (IPv6)}\n");
910ade9ccfeSMarcel Moolenaar 	p(carps_badttl, "\t\t{:dropped-wrong-ttl/%ju} "
911ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s discarded for wrong TTL}\n");
912ade9ccfeSMarcel Moolenaar 	p(carps_hdrops, "\t\t{:dropped-short-header/%ju} "
913ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s shorter than header}\n");
914ade9ccfeSMarcel Moolenaar 	p(carps_badsum, "\t\t{:dropped-bad-checksum/%ju} "
915ade9ccfeSMarcel Moolenaar 	    "{N:/discarded for bad checksum%s}\n");
916ade9ccfeSMarcel Moolenaar 	p(carps_badver,	"\t\t{:dropped-bad-version/%ju} "
917ade9ccfeSMarcel Moolenaar 	    "{N:/discarded packet%s with a bad version}\n");
918ade9ccfeSMarcel Moolenaar 	p2(carps_badlen, "\t\t{:dropped-short-packet/%ju} "
919ade9ccfeSMarcel Moolenaar 	    "{N:/discarded because packet too short}\n");
920ade9ccfeSMarcel Moolenaar 	p2(carps_badauth, "\t\t{:dropped-bad-authentication/%ju} "
921ade9ccfeSMarcel Moolenaar 	    "{N:/discarded for bad authentication}\n");
922ade9ccfeSMarcel Moolenaar 	p2(carps_badvhid, "\t\t{:dropped-bad-vhid/%ju} "
923ade9ccfeSMarcel Moolenaar 	    "{N:/discarded for bad vhid}\n");
924ade9ccfeSMarcel Moolenaar 	p2(carps_badaddrs, "\t\t{:dropped-bad-address-list/%ju} "
925ade9ccfeSMarcel Moolenaar 	    "{N:/discarded because of a bad address list}\n");
926ade9ccfeSMarcel Moolenaar 	p(carps_opackets, "\t{:sent-inet-packets/%ju} "
927ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s sent (IPv4)}\n");
928ade9ccfeSMarcel Moolenaar 	p(carps_opackets6, "\t{:sent-inet6-packets/%ju} "
929ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s sent (IPv6)}\n");
930ade9ccfeSMarcel Moolenaar 	p2(carps_onomem, "\t\t{:send-failed-memory-error/%ju} "
931ade9ccfeSMarcel Moolenaar 	    "{N:/send failed due to mbuf memory error}\n");
932a9771948SGleb Smirnoff #if notyet
933ade9ccfeSMarcel Moolenaar 	p(carps_ostates, "\t\t{:send-state-updates/%s} "
934ade9ccfeSMarcel Moolenaar 	    "{N:/state update%s sent}\n");
935a9771948SGleb Smirnoff #endif
936a9771948SGleb Smirnoff #undef p
937a9771948SGleb Smirnoff #undef p2
938ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
939a9771948SGleb Smirnoff }
940a9771948SGleb Smirnoff 
941a9771948SGleb Smirnoff /*
9429b50d902SRodney W. Grimes  * Dump IP statistics structure.
9439b50d902SRodney W. Grimes  */
9449b50d902SRodney W. Grimes void
945feda1a43SJohn Baldwin ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
9469b50d902SRodney W. Grimes {
9479eddb899SMark Johnston 	struct ipstat ipstat;
9489b50d902SRodney W. Grimes 
9499eddb899SMark Johnston 	if (fetch_stats("net.inet.ip.stats", off, &ipstat,
9509eddb899SMark Johnston 	    sizeof(ipstat), kread_counters) != 0)
9519b50d902SRodney W. Grimes 		return;
9524f81ef50SGarrett Wollman 
953ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
954ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
9559b50d902SRodney W. Grimes 
9569b50d902SRodney W. Grimes #define	p(f, m) if (ipstat.f || sflag <= 1) \
957ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )ipstat.f, plural(ipstat.f))
95822694ebaSBruce Evans #define	p1a(f, m) if (ipstat.f || sflag <= 1) \
959ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )ipstat.f)
9609b50d902SRodney W. Grimes 
961ade9ccfeSMarcel Moolenaar 	p(ips_total, "\t{:received-packets/%ju} "
962ade9ccfeSMarcel Moolenaar 	    "{N:/total packet%s received}\n");
963ade9ccfeSMarcel Moolenaar 	p(ips_badsum, "\t{:dropped-bad-checksum/%ju} "
964ade9ccfeSMarcel Moolenaar 	    "{N:/bad header checksum%s}\n");
965ade9ccfeSMarcel Moolenaar 	p1a(ips_toosmall, "\t{:dropped-below-minimum-size/%ju} "
966ade9ccfeSMarcel Moolenaar 	    "{N:/with size smaller than minimum}\n");
967ade9ccfeSMarcel Moolenaar 	p1a(ips_tooshort, "\t{:dropped-short-packets/%ju} "
968ade9ccfeSMarcel Moolenaar 	    "{N:/with data size < data length}\n");
969ade9ccfeSMarcel Moolenaar 	p1a(ips_toolong, "\t{:dropped-too-long/%ju} "
970ade9ccfeSMarcel Moolenaar 	    "{N:/with ip length > max ip packet size}\n");
971ade9ccfeSMarcel Moolenaar 	p1a(ips_badhlen, "\t{:dropped-short-header-length/%ju} "
972ade9ccfeSMarcel Moolenaar 	    "{N:/with header length < data size}\n");
973ade9ccfeSMarcel Moolenaar 	p1a(ips_badlen, "\t{:dropped-short-data/%ju} "
974ade9ccfeSMarcel Moolenaar 	    "{N:/with data length < header length}\n");
975ade9ccfeSMarcel Moolenaar 	p1a(ips_badoptions, "\t{:dropped-bad-options/%ju} "
976ade9ccfeSMarcel Moolenaar 	    "{N:/with bad options}\n");
977ade9ccfeSMarcel Moolenaar 	p1a(ips_badvers, "\t{:dropped-bad-version/%ju} "
978ade9ccfeSMarcel Moolenaar 	    "{N:/with incorrect version number}\n");
979ade9ccfeSMarcel Moolenaar 	p(ips_fragments, "\t{:received-fragments/%ju} "
980ade9ccfeSMarcel Moolenaar 	    "{N:/fragment%s received}\n");
981ade9ccfeSMarcel Moolenaar 	p(ips_fragdropped, "\t{:dropped-fragments/%ju} "
982ade9ccfeSMarcel Moolenaar 	    "{N:/fragment%s dropped (dup or out of space)}\n");
983ade9ccfeSMarcel Moolenaar 	p(ips_fragtimeout, "\t{:dropped-fragments-after-timeout/%ju} "
984ade9ccfeSMarcel Moolenaar 	    "{N:/fragment%s dropped after timeout}\n");
985ade9ccfeSMarcel Moolenaar 	p(ips_reassembled, "\t{:reassembled-packets/%ju} "
986ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s reassembled ok}\n");
987ade9ccfeSMarcel Moolenaar 	p(ips_delivered, "\t{:received-local-packets/%ju} "
988ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s for this host}\n");
989ade9ccfeSMarcel Moolenaar 	p(ips_noproto, "\t{:dropped-unknown-protocol/%ju} "
990ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s for unknown\\/unsupported protocol}\n");
991ade9ccfeSMarcel Moolenaar 	p(ips_forward, "\t{:forwarded-packets/%ju} "
992ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s forwarded}");
993ade9ccfeSMarcel Moolenaar 	p(ips_fastforward, " ({:fast-forwarded-packets/%ju} "
994ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s fast forwarded})");
995958d6f7fSPierre Beyssac 	if (ipstat.ips_forward || sflag <= 1)
996ade9ccfeSMarcel Moolenaar 		xo_emit("\n");
997ade9ccfeSMarcel Moolenaar 	p(ips_cantforward, "\t{:packets-cannot-forward/%ju} "
998ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s not forwardable}\n");
999ade9ccfeSMarcel Moolenaar 	p(ips_notmember, "\t{:received-unknown-multicast-group/%ju} "
1000ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s received for unknown multicast group}\n");
1001ade9ccfeSMarcel Moolenaar 	p(ips_redirectsent, "\t{:redirects-sent/%ju} "
1002ade9ccfeSMarcel Moolenaar 	    "{N:/redirect%s sent}\n");
1003ade9ccfeSMarcel Moolenaar 	p(ips_localout, "\t{:sent-packets/%ju} "
1004ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s sent from this host}\n");
1005ade9ccfeSMarcel Moolenaar 	p(ips_rawout, "\t{:send-packets-fabricated-header/%ju} "
1006ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s sent with fabricated ip header}\n");
1007ade9ccfeSMarcel Moolenaar 	p(ips_odropped, "\t{:discard-no-mbufs/%ju} "
1008ade9ccfeSMarcel Moolenaar 	    "{N:/output packet%s dropped due to no bufs, etc.}\n");
1009ade9ccfeSMarcel Moolenaar 	p(ips_noroute, "\t{:discard-no-route/%ju} "
1010ade9ccfeSMarcel Moolenaar 	    "{N:/output packet%s discarded due to no route}\n");
1011ade9ccfeSMarcel Moolenaar 	p(ips_fragmented, "\t{:sent-fragments/%ju} "
1012ade9ccfeSMarcel Moolenaar 	    "{N:/output datagram%s fragmented}\n");
1013ade9ccfeSMarcel Moolenaar 	p(ips_ofragments, "\t{:fragments-created/%ju} "
1014ade9ccfeSMarcel Moolenaar 	    "{N:/fragment%s created}\n");
1015ade9ccfeSMarcel Moolenaar 	p(ips_cantfrag, "\t{:discard-cannot-fragment/%ju} "
1016ade9ccfeSMarcel Moolenaar 	    "{N:/datagram%s that can't be fragmented}\n");
1017ade9ccfeSMarcel Moolenaar 	p(ips_nogif, "\t{:discard-tunnel-no-gif/%ju} "
1018ade9ccfeSMarcel Moolenaar 	    "{N:/tunneling packet%s that can't find gif}\n");
1019ade9ccfeSMarcel Moolenaar 	p(ips_badaddr, "\t{:discard-bad-address/%ju} "
1020ade9ccfeSMarcel Moolenaar 	    "{N:/datagram%s with bad address in header}\n");
10219b50d902SRodney W. Grimes #undef p
102222694ebaSBruce Evans #undef p1a
1023ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
10249b50d902SRodney W. Grimes }
10259b50d902SRodney W. Grimes 
102654fc657dSGeorge V. Neville-Neil /*
102754fc657dSGeorge V. Neville-Neil  * Dump ARP statistics structure.
102854fc657dSGeorge V. Neville-Neil  */
102954fc657dSGeorge V. Neville-Neil void
103054fc657dSGeorge V. Neville-Neil arp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
103154fc657dSGeorge V. Neville-Neil {
10329eddb899SMark Johnston 	struct arpstat arpstat;
103354fc657dSGeorge V. Neville-Neil 
10349eddb899SMark Johnston 	if (fetch_stats("net.link.ether.arp.stats", off, &arpstat,
10359eddb899SMark Johnston 	    sizeof(arpstat), kread_counters) != 0)
103654fc657dSGeorge V. Neville-Neil 		return;
103754fc657dSGeorge V. Neville-Neil 
1038ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
1039ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
104054fc657dSGeorge V. Neville-Neil 
104154fc657dSGeorge V. Neville-Neil #define	p(f, m) if (arpstat.f || sflag <= 1) \
1042ade9ccfeSMarcel Moolenaar 	xo_emit("\t" m, (uintmax_t)arpstat.f, plural(arpstat.f))
104354fc657dSGeorge V. Neville-Neil #define	p2(f, m) if (arpstat.f || sflag <= 1) \
1044ade9ccfeSMarcel Moolenaar 	xo_emit("\t" m, (uintmax_t)arpstat.f, pluralies(arpstat.f))
104554fc657dSGeorge V. Neville-Neil 
1046ade9ccfeSMarcel Moolenaar 	p(txrequests, "{:sent-requests/%ju} {N:/ARP request%s sent}\n");
1047*b25d74e0SBjoern A. Zeeb 	p(txerrors, "{:sent-failures/%ju} {N:/ARP request%s failed to sent}\n");
1048ade9ccfeSMarcel Moolenaar 	p2(txreplies, "{:sent-replies/%ju} {N:/ARP repl%s sent}\n");
1049ade9ccfeSMarcel Moolenaar 	p(rxrequests, "{:received-requests/%ju} "
1050ade9ccfeSMarcel Moolenaar 	    "{N:/ARP request%s received}\n");
1051ade9ccfeSMarcel Moolenaar 	p2(rxreplies, "{:received-replies/%ju} "
1052ade9ccfeSMarcel Moolenaar 	    "{N:/ARP repl%s received}\n");
1053ade9ccfeSMarcel Moolenaar 	p(received, "{:received-packers/%ju} "
1054ade9ccfeSMarcel Moolenaar 	    "{N:/ARP packet%s received}\n");
1055ade9ccfeSMarcel Moolenaar 	p(dropped, "{:dropped-no-entry/%ju} "
1056ade9ccfeSMarcel Moolenaar 	    "{N:/total packet%s dropped due to no ARP entry}\n");
1057ade9ccfeSMarcel Moolenaar 	p(timeouts, "{:entries-timeout/%ju} "
1058ade9ccfeSMarcel Moolenaar 	    "{N:/ARP entry%s timed out}\n");
1059ade9ccfeSMarcel Moolenaar 	p(dupips, "{:dropped-duplicate-address/%ju} "
1060ade9ccfeSMarcel Moolenaar 	    "{N:/Duplicate IP%s seen}\n");
106154fc657dSGeorge V. Neville-Neil #undef p
106254fc657dSGeorge V. Neville-Neil #undef p2
1063ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
106454fc657dSGeorge V. Neville-Neil }
106554fc657dSGeorge V. Neville-Neil 
106654fc657dSGeorge V. Neville-Neil 
106754fc657dSGeorge V. Neville-Neil 
10684063583aSMaxim Konovalov static	const char *icmpnames[ICMP_MAXTYPE + 1] = {
10694063583aSMaxim Konovalov 	"echo reply",			/* RFC 792 */
10709b50d902SRodney W. Grimes 	"#1",
10719b50d902SRodney W. Grimes 	"#2",
10724063583aSMaxim Konovalov 	"destination unreachable",	/* RFC 792 */
10734063583aSMaxim Konovalov 	"source quench",		/* RFC 792 */
10744063583aSMaxim Konovalov 	"routing redirect",		/* RFC 792 */
10759b50d902SRodney W. Grimes 	"#6",
10769b50d902SRodney W. Grimes 	"#7",
10774063583aSMaxim Konovalov 	"echo",				/* RFC 792 */
10784063583aSMaxim Konovalov 	"router advertisement",		/* RFC 1256 */
10794063583aSMaxim Konovalov 	"router solicitation",		/* RFC 1256 */
10804063583aSMaxim Konovalov 	"time exceeded",		/* RFC 792 */
10814063583aSMaxim Konovalov 	"parameter problem",		/* RFC 792 */
10824063583aSMaxim Konovalov 	"time stamp",			/* RFC 792 */
10834063583aSMaxim Konovalov 	"time stamp reply",		/* RFC 792 */
10844063583aSMaxim Konovalov 	"information request",		/* RFC 792 */
10854063583aSMaxim Konovalov 	"information request reply",	/* RFC 792 */
10864063583aSMaxim Konovalov 	"address mask request",		/* RFC 950 */
10874063583aSMaxim Konovalov 	"address mask reply",		/* RFC 950 */
10884063583aSMaxim Konovalov 	"#19",
10894063583aSMaxim Konovalov 	"#20",
10904063583aSMaxim Konovalov 	"#21",
10914063583aSMaxim Konovalov 	"#22",
10924063583aSMaxim Konovalov 	"#23",
10934063583aSMaxim Konovalov 	"#24",
10944063583aSMaxim Konovalov 	"#25",
10954063583aSMaxim Konovalov 	"#26",
10964063583aSMaxim Konovalov 	"#27",
10974063583aSMaxim Konovalov 	"#28",
10984063583aSMaxim Konovalov 	"#29",
10994063583aSMaxim Konovalov 	"icmp traceroute",		/* RFC 1393 */
11004063583aSMaxim Konovalov 	"datagram conversion error",	/* RFC 1475 */
11014063583aSMaxim Konovalov 	"mobile host redirect",
11024063583aSMaxim Konovalov 	"IPv6 where-are-you",
11034063583aSMaxim Konovalov 	"IPv6 i-am-here",
11044063583aSMaxim Konovalov 	"mobile registration req",
11054063583aSMaxim Konovalov 	"mobile registration reply",
11064063583aSMaxim Konovalov 	"domain name request",		/* RFC 1788 */
11074063583aSMaxim Konovalov 	"domain name reply",		/* RFC 1788 */
11084063583aSMaxim Konovalov 	"icmp SKIP",
11094063583aSMaxim Konovalov 	"icmp photuris",		/* RFC 2521 */
11109b50d902SRodney W. Grimes };
11119b50d902SRodney W. Grimes 
11129b50d902SRodney W. Grimes /*
11139b50d902SRodney W. Grimes  * Dump ICMP statistics.
11149b50d902SRodney W. Grimes  */
11159b50d902SRodney W. Grimes void
1116feda1a43SJohn Baldwin icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
11179b50d902SRodney W. Grimes {
11189eddb899SMark Johnston 	struct icmpstat icmpstat;
11194e00c309SGarrett Wollman 	size_t len;
11209eddb899SMark Johnston 	int i, first;
11219b50d902SRodney W. Grimes 
11229eddb899SMark Johnston 	if (fetch_stats("net.inet.icmp.stats", off, &icmpstat,
11239eddb899SMark Johnston 	    sizeof(icmpstat), kread_counters) != 0)
1124c73d99b5SRuslan Ermilov 		return;
11254e00c309SGarrett Wollman 
1126ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
1127ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
11289b50d902SRodney W. Grimes 
11299b50d902SRodney W. Grimes #define	p(f, m) if (icmpstat.f || sflag <= 1) \
1130ade9ccfeSMarcel Moolenaar 	xo_emit(m, icmpstat.f, plural(icmpstat.f))
113122694ebaSBruce Evans #define	p1a(f, m) if (icmpstat.f || sflag <= 1) \
1132ade9ccfeSMarcel Moolenaar 	xo_emit(m, icmpstat.f)
1133bd714208SRuslan Ermilov #define	p2(f, m) if (icmpstat.f || sflag <= 1) \
1134ade9ccfeSMarcel Moolenaar 	xo_emit(m, icmpstat.f, plurales(icmpstat.f))
11359b50d902SRodney W. Grimes 
1136ade9ccfeSMarcel Moolenaar 	p(icps_error, "\t{:icmp-calls/%lu} "
1137ade9ccfeSMarcel Moolenaar 	    "{N:/call%s to icmp_error}\n");
1138ade9ccfeSMarcel Moolenaar 	p(icps_oldicmp, "\t{:errors-not-from-message/%lu} "
1139ade9ccfeSMarcel Moolenaar 	    "{N:/error%s not generated in response to an icmp message}\n");
1140ade9ccfeSMarcel Moolenaar 
1141ade9ccfeSMarcel Moolenaar 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) {
11429b50d902SRodney W. Grimes 		if (icmpstat.icps_outhist[i] != 0) {
11439b50d902SRodney W. Grimes 			if (first) {
1144ade9ccfeSMarcel Moolenaar 				xo_open_list("output-histogram");
1145ade9ccfeSMarcel Moolenaar 				xo_emit("\tOutput histogram:\n");
11469b50d902SRodney W. Grimes 				first = 0;
11479b50d902SRodney W. Grimes 			}
1148ade9ccfeSMarcel Moolenaar 			xo_open_instance("output-histogram");
11494063583aSMaxim Konovalov 			if (icmpnames[i] != NULL)
1150ade9ccfeSMarcel Moolenaar 				xo_emit("\t\t{k:name/%s}: {:count/%lu}\n",
1151ade9ccfeSMarcel Moolenaar 				    icmpnames[i], icmpstat.icps_outhist[i]);
11524063583aSMaxim Konovalov 			else
1153ade9ccfeSMarcel Moolenaar 				xo_emit("\t\tunknown ICMP #{k:name/%d}: "
1154ade9ccfeSMarcel Moolenaar 				    "{:count/%lu}\n",
1155ade9ccfeSMarcel Moolenaar 				    i, icmpstat.icps_outhist[i]);
1156ade9ccfeSMarcel Moolenaar 			xo_close_instance("output-histogram");
11579b50d902SRodney W. Grimes 		}
1158ade9ccfeSMarcel Moolenaar 	}
1159ade9ccfeSMarcel Moolenaar 	if (!first)
1160ade9ccfeSMarcel Moolenaar 		xo_close_list("output-histogram");
1161ade9ccfeSMarcel Moolenaar 
1162ade9ccfeSMarcel Moolenaar 	p(icps_badcode, "\t{:dropped-bad-code/%lu} "
1163ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with bad code fields}\n");
1164ade9ccfeSMarcel Moolenaar 	p(icps_tooshort, "\t{:dropped-too-short/%lu} "
1165ade9ccfeSMarcel Moolenaar 	    "{N:/message%s less than the minimum length}\n");
1166ade9ccfeSMarcel Moolenaar 	p(icps_checksum, "\t{:dropped-bad-checksum/%lu} "
1167ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with bad checksum}\n");
1168ade9ccfeSMarcel Moolenaar 	p(icps_badlen, "\t{:dropped-bad-length/%lu} "
1169ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with bad length}\n");
1170ade9ccfeSMarcel Moolenaar 	p1a(icps_bmcastecho, "\t{:dropped-multicast-echo/%lu} "
1171ade9ccfeSMarcel Moolenaar 	    "{N:/multicast echo requests ignored}\n");
1172ade9ccfeSMarcel Moolenaar 	p1a(icps_bmcasttstamp, "\t{:dropped-multicast-timestamp/%lu} "
1173ade9ccfeSMarcel Moolenaar 	    "{N:/multicast timestamp requests ignored}\n");
1174ade9ccfeSMarcel Moolenaar 
1175ade9ccfeSMarcel Moolenaar 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) {
11769b50d902SRodney W. Grimes 		if (icmpstat.icps_inhist[i] != 0) {
11779b50d902SRodney W. Grimes 			if (first) {
1178ade9ccfeSMarcel Moolenaar 				xo_open_list("input-histogram");
1179ade9ccfeSMarcel Moolenaar 				xo_emit("\tInput histogram:\n");
11809b50d902SRodney W. Grimes 				first = 0;
11819b50d902SRodney W. Grimes 			}
1182ade9ccfeSMarcel Moolenaar 			xo_open_instance("input-histogram");
11834063583aSMaxim Konovalov 			if (icmpnames[i] != NULL)
1184ade9ccfeSMarcel Moolenaar 				xo_emit("\t\t{k:name/%s}: {:count/%lu}\n",
1185ade9ccfeSMarcel Moolenaar 					icmpnames[i],
11869b50d902SRodney W. Grimes 					icmpstat.icps_inhist[i]);
11874063583aSMaxim Konovalov 			else
1188ade9ccfeSMarcel Moolenaar 				xo_emit(
1189ade9ccfeSMarcel Moolenaar 			"\t\tunknown ICMP #{k:name/%d}: {:count/%lu}\n",
1190ade9ccfeSMarcel Moolenaar 					i, icmpstat.icps_inhist[i]);
1191ade9ccfeSMarcel Moolenaar 			xo_close_instance("input-histogram");
11929b50d902SRodney W. Grimes 		}
1193ade9ccfeSMarcel Moolenaar 	}
1194ade9ccfeSMarcel Moolenaar 	if (!first)
1195ade9ccfeSMarcel Moolenaar 		xo_close_list("input-histogram");
1196ade9ccfeSMarcel Moolenaar 
1197ade9ccfeSMarcel Moolenaar 	p(icps_reflect, "\t{:sent-packets/%lu} "
1198ade9ccfeSMarcel Moolenaar 	    "{N:/message response%s generated}\n");
1199ade9ccfeSMarcel Moolenaar 	p2(icps_badaddr, "\t{:discard-invalid-return-address/%lu} "
1200ade9ccfeSMarcel Moolenaar 	    "{N:/invalid return address%s}\n");
1201ade9ccfeSMarcel Moolenaar 	p(icps_noroute, "\t{:discard-no-route/%lu} "
1202ade9ccfeSMarcel Moolenaar 	    "{N:/no return route%s}\n");
12039b50d902SRodney W. Grimes #undef p
120422694ebaSBruce Evans #undef p1a
1205bd714208SRuslan Ermilov #undef p2
1206feda1a43SJohn Baldwin 	if (live) {
12074e00c309SGarrett Wollman 		len = sizeof i;
1208feda1a43SJohn Baldwin 		if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) <
1209feda1a43SJohn Baldwin 		    0)
12104e00c309SGarrett Wollman 			return;
1211ade9ccfeSMarcel Moolenaar 		xo_emit("\tICMP address mask responses are "
1212ade9ccfeSMarcel Moolenaar 		    "{q:icmp-address-responses/%sabled}\n", i ? "en" : "dis");
12139b50d902SRodney W. Grimes 	}
1214ade9ccfeSMarcel Moolenaar 
1215ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
1216feda1a43SJohn Baldwin }
12179b50d902SRodney W. Grimes 
1218d10910e6SBruce M Simpson /*
1219d10910e6SBruce M Simpson  * Dump IGMP statistics structure.
1220d10910e6SBruce M Simpson  */
1221d10910e6SBruce M Simpson void
1222d10910e6SBruce M Simpson igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1223d10910e6SBruce M Simpson {
12249eddb899SMark Johnston 	struct igmpstat igmpstat;
1225d10910e6SBruce M Simpson 
12269eddb899SMark Johnston 	if (fetch_stats("net.inet.igmp.stats", 0, &igmpstat,
12279eddb899SMark Johnston 	    sizeof(igmpstat), kread) != 0)
1228d10910e6SBruce M Simpson 		return;
1229d10910e6SBruce M Simpson 
1230d10910e6SBruce M Simpson 	if (igmpstat.igps_version != IGPS_VERSION_3) {
1231ade9ccfeSMarcel Moolenaar 		xo_warnx("%s: version mismatch (%d != %d)", __func__,
1232d10910e6SBruce M Simpson 		    igmpstat.igps_version, IGPS_VERSION_3);
1233d10910e6SBruce M Simpson 	}
1234d10910e6SBruce M Simpson 	if (igmpstat.igps_len != IGPS_VERSION3_LEN) {
1235ade9ccfeSMarcel Moolenaar 		xo_warnx("%s: size mismatch (%d != %d)", __func__,
1236d10910e6SBruce M Simpson 		    igmpstat.igps_len, IGPS_VERSION3_LEN);
1237d10910e6SBruce M Simpson 	}
1238d10910e6SBruce M Simpson 
1239ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
1240ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
1241d10910e6SBruce M Simpson 
1242d10910e6SBruce M Simpson #define	p64(f, m) if (igmpstat.f || sflag <= 1) \
1243ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t) igmpstat.f, plural(igmpstat.f))
1244d10910e6SBruce M Simpson #define	py64(f, m) if (igmpstat.f || sflag <= 1) \
1245ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t) igmpstat.f, pluralies(igmpstat.f))
1246ade9ccfeSMarcel Moolenaar 
1247ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_total, "\t{:received-messages/%ju} "
1248ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received}\n");
1249ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_tooshort, "\t{:dropped-too-short/%ju} "
1250ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with too few bytes}\n");
1251ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_badttl, "\t{:dropped-wrong-ttl/%ju} "
1252ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with wrong TTL}\n");
1253ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_badsum, "\t{:dropped-bad-checksum/%ju} "
1254ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with bad checksum}\n");
1255ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_v1v2_queries, "\t{:received-membership-queries/%ju} "
1256ade9ccfeSMarcel Moolenaar 	    "{N:/V1\\/V2 membership quer%s received}\n");
1257ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_v3_queries, "\t{:received-v3-membership-queries/%ju} "
1258ade9ccfeSMarcel Moolenaar 	    "{N:/V3 membership quer%s received}\n");
1259ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_badqueries, "\t{:dropped-membership-queries/%ju} "
1260ade9ccfeSMarcel Moolenaar 	    "{N:/membership quer%s received with invalid field(s)}\n");
1261ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_gen_queries, "\t{:received-general-queries/%ju} "
1262ade9ccfeSMarcel Moolenaar 	    "{N:/general quer%s received}\n");
1263ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_group_queries, "\t{:received-group-queries/%ju} "
1264ade9ccfeSMarcel Moolenaar 	    "{N:/group quer%s received}\n");
1265ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_gsr_queries, "\t{:received-group-source-queries/%ju} "
1266ade9ccfeSMarcel Moolenaar 	    "{N:/group-source quer%s received}\n");
1267ade9ccfeSMarcel Moolenaar 	py64(igps_drop_gsr_queries, "\t{:dropped-group-source-queries/%ju} "
1268ade9ccfeSMarcel Moolenaar 	    "{N:/group-source quer%s dropped}\n");
1269ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_reports, "\t{:received-membership-requests/%ju} "
1270ade9ccfeSMarcel Moolenaar 	    "{N:/membership report%s received}\n");
1271ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_badreports, "\t{:dropped-membership-reports/%ju} "
1272ade9ccfeSMarcel Moolenaar 	    "{N:/membership report%s received with invalid field(s)}\n");
1273ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_ourreports, "\t"
1274ade9ccfeSMarcel Moolenaar 	    "{:received-membership-reports-matching/%ju} "
1275ade9ccfeSMarcel Moolenaar 	    "{N:/membership report%s received for groups to which we belong}"
1276ade9ccfeSMarcel Moolenaar 	    "\n");
1277ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_nora, "\t{:received-v3-reports-no-router-alert/%ju} "
1278ade9ccfeSMarcel Moolenaar 	    "{N:/V3 report%s received without Router Alert}\n");
1279ade9ccfeSMarcel Moolenaar 	p64(igps_snd_reports, "\t{:sent-membership-reports/%ju} "
1280ade9ccfeSMarcel Moolenaar 	    "{N:/membership report%s sent}\n");
1281d10910e6SBruce M Simpson #undef p64
1282d10910e6SBruce M Simpson #undef py64
1283ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
1284d10910e6SBruce M Simpson }
12859b50d902SRodney W. Grimes 
12869b50d902SRodney W. Grimes /*
1287c7b9b5bbSJeffrey Hsu  * Dump PIM statistics structure.
1288c7b9b5bbSJeffrey Hsu  */
1289c7b9b5bbSJeffrey Hsu void
1290feda1a43SJohn Baldwin pim_stats(u_long off __unused, const char *name, int af1 __unused,
1291feda1a43SJohn Baldwin     int proto __unused)
1292c7b9b5bbSJeffrey Hsu {
12939eddb899SMark Johnston 	struct pimstat pimstat;
1294c7b9b5bbSJeffrey Hsu 
12959eddb899SMark Johnston 	if (fetch_stats("net.inet.pim.stats", off, &pimstat,
12969eddb899SMark Johnston 	    sizeof(pimstat), kread_counters) != 0)
1297c7b9b5bbSJeffrey Hsu 		return;
1298c7b9b5bbSJeffrey Hsu 
1299ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
1300ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
1301c7b9b5bbSJeffrey Hsu 
1302c7b9b5bbSJeffrey Hsu #define	p(f, m) if (pimstat.f || sflag <= 1) \
1303ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t)pimstat.f, plural(pimstat.f))
1304c7b9b5bbSJeffrey Hsu #define	py(f, m) if (pimstat.f || sflag <= 1) \
1305ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
1306ade9ccfeSMarcel Moolenaar 
1307ade9ccfeSMarcel Moolenaar 	p(pims_rcv_total_msgs, "\t{:received-messages/%ju} "
1308ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received}\n");
1309ade9ccfeSMarcel Moolenaar 	p(pims_rcv_total_bytes, "\t{:received-bytes/%ju} "
1310ade9ccfeSMarcel Moolenaar 	    "{N:/byte%s received}\n");
1311ade9ccfeSMarcel Moolenaar 	p(pims_rcv_tooshort, "\t{:dropped-too-short/%ju} "
1312ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with too few bytes}\n");
1313ade9ccfeSMarcel Moolenaar 	p(pims_rcv_badsum, "\t{:dropped-bad-checksum/%ju} "
1314ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with bad checksum}\n");
1315ade9ccfeSMarcel Moolenaar 	p(pims_rcv_badversion, "\t{:dropped-bad-version/%ju} "
1316ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with bad version}\n");
1317ade9ccfeSMarcel Moolenaar 	p(pims_rcv_registers_msgs, "\t{:received-data-register-messages/%ju} "
1318ade9ccfeSMarcel Moolenaar 	    "{N:/data register message%s received}\n");
1319ade9ccfeSMarcel Moolenaar 	p(pims_rcv_registers_bytes, "\t{:received-data-register-bytes/%ju} "
1320ade9ccfeSMarcel Moolenaar 	    "{N:/data register byte%s received}\n");
1321ade9ccfeSMarcel Moolenaar 	p(pims_rcv_registers_wrongiif, "\t"
1322ade9ccfeSMarcel Moolenaar 	    "{:received-data-register-wrong-interface/%ju} "
1323ade9ccfeSMarcel Moolenaar 	    "{N:/data register message%s received on wrong iif}\n");
1324ade9ccfeSMarcel Moolenaar 	p(pims_rcv_badregisters, "\t{:received-bad-registers/%ju} "
1325ade9ccfeSMarcel Moolenaar 	    "{N:/bad register%s received}\n");
1326ade9ccfeSMarcel Moolenaar 	p(pims_snd_registers_msgs, "\t{:sent-data-register-messages/%ju} "
1327ade9ccfeSMarcel Moolenaar 	    "{N:/data register message%s sent}\n");
1328ade9ccfeSMarcel Moolenaar 	p(pims_snd_registers_bytes, "\t{:sent-data-register-bytes/%ju} "
1329ade9ccfeSMarcel Moolenaar 	    "{N:/data register byte%s sent}\n");
1330c7b9b5bbSJeffrey Hsu #undef p
1331c7b9b5bbSJeffrey Hsu #undef py
1332ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
1333c7b9b5bbSJeffrey Hsu }
1334c7b9b5bbSJeffrey Hsu 
13359784899dSBjoern A. Zeeb #ifdef INET
1336c7b9b5bbSJeffrey Hsu /*
13379b50d902SRodney W. Grimes  * Pretty print an Internet address (net address + port).
13389b50d902SRodney W. Grimes  */
13399784899dSBjoern A. Zeeb static void
1340ade9ccfeSMarcel Moolenaar inetprint(const char *container, struct in_addr *in, int port,
1341046aad39SHajimu UMEMOTO     const char *proto, int num_port, const int af1)
13429b50d902SRodney W. Grimes {
13439b50d902SRodney W. Grimes 	struct servent *sp = 0;
13449b50d902SRodney W. Grimes 	char line[80], *cp;
1345cfa1ca9dSYoshinobu Inoue 	int width;
1346f193c8ceSXin LI 	size_t alen, plen;
13479b50d902SRodney W. Grimes 
1348ade9ccfeSMarcel Moolenaar 	if (container)
1349ade9ccfeSMarcel Moolenaar 		xo_open_container(container);
1350ade9ccfeSMarcel Moolenaar 
1351080b7f49SDag-Erling Smørgrav 	if (Wflag)
1352f193c8ceSXin LI 	    snprintf(line, sizeof(line), "%s.", inetname(in));
1353080b7f49SDag-Erling Smørgrav 	else
1354f193c8ceSXin LI 	    snprintf(line, sizeof(line), "%.*s.",
1355f193c8ceSXin LI 		(Aflag && !num_port) ? 12 : 16, inetname(in));
1356f193c8ceSXin LI 	alen = strlen(line);
1357f193c8ceSXin LI 	cp = line + alen;
1358a01e3379SDavid Malone 	if (!num_port && port)
13599b50d902SRodney W. Grimes 		sp = getservbyport((int)port, proto);
13609b50d902SRodney W. Grimes 	if (sp || port == 0)
1361f193c8ceSXin LI 		snprintf(cp, sizeof(line) - alen,
1362f193c8ceSXin LI 		    "%.15s ", sp ? sp->s_name : "*");
13639b50d902SRodney W. Grimes 	else
1364f193c8ceSXin LI 		snprintf(cp, sizeof(line) - alen,
1365f193c8ceSXin LI 		    "%d ", ntohs((u_short)port));
1366046aad39SHajimu UMEMOTO 	width = (Aflag && !Wflag) ? 18 :
1367046aad39SHajimu UMEMOTO 		((!Wflag || af1 == AF_INET) ? 22 : 45);
1368080b7f49SDag-Erling Smørgrav 	if (Wflag)
1369ade9ccfeSMarcel Moolenaar 		xo_emit("{d:target/%-*s} ", width, line);
1370080b7f49SDag-Erling Smørgrav 	else
1371ade9ccfeSMarcel Moolenaar 		xo_emit("{d:target/%-*.*s} ", width, width, line);
1372ade9ccfeSMarcel Moolenaar 
1373f193c8ceSXin LI 	plen = strlen(cp) - 1;
1374f193c8ceSXin LI 	alen--;
1375ade9ccfeSMarcel Moolenaar 	xo_emit("{e:address/%*.*s}{e:port/%*.*s}", alen, alen, line, plen,
1376ade9ccfeSMarcel Moolenaar 	    plen, cp);
1377ade9ccfeSMarcel Moolenaar 
1378ade9ccfeSMarcel Moolenaar 	if (container)
1379ade9ccfeSMarcel Moolenaar 		xo_close_container(container);
13809b50d902SRodney W. Grimes }
13819b50d902SRodney W. Grimes 
13829b50d902SRodney W. Grimes /*
13839b50d902SRodney W. Grimes  * Construct an Internet address representation.
138465ea0024SAssar Westerlund  * If numeric_addr has been supplied, give
13859b50d902SRodney W. Grimes  * numeric value, otherwise try for symbolic name.
13869b50d902SRodney W. Grimes  */
13879b50d902SRodney W. Grimes char *
13885e051718SAssar Westerlund inetname(struct in_addr *inp)
13899b50d902SRodney W. Grimes {
1390a01e3379SDavid Malone 	char *cp;
1391d121b556SBrian Somers 	static char line[MAXHOSTNAMELEN];
13929b50d902SRodney W. Grimes 	struct hostent *hp;
13939b50d902SRodney W. Grimes 	struct netent *np;
13949b50d902SRodney W. Grimes 
13959b50d902SRodney W. Grimes 	cp = 0;
139665ea0024SAssar Westerlund 	if (!numeric_addr && inp->s_addr != INADDR_ANY) {
13979b50d902SRodney W. Grimes 		int net = inet_netof(*inp);
13989b50d902SRodney W. Grimes 		int lna = inet_lnaof(*inp);
13999b50d902SRodney W. Grimes 
14009b50d902SRodney W. Grimes 		if (lna == INADDR_ANY) {
14019b50d902SRodney W. Grimes 			np = getnetbyaddr(net, AF_INET);
14029b50d902SRodney W. Grimes 			if (np)
14039b50d902SRodney W. Grimes 				cp = np->n_name;
14049b50d902SRodney W. Grimes 		}
1405ef1cb629SMarcelo Araujo 		if (cp == NULL) {
14069b50d902SRodney W. Grimes 			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
14079b50d902SRodney W. Grimes 			if (hp) {
14089b50d902SRodney W. Grimes 				cp = hp->h_name;
1409d121b556SBrian Somers 				trimdomain(cp, strlen(cp));
14109b50d902SRodney W. Grimes 			}
14119b50d902SRodney W. Grimes 		}
14129b50d902SRodney W. Grimes 	}
14139b50d902SRodney W. Grimes 	if (inp->s_addr == INADDR_ANY)
14149b50d902SRodney W. Grimes 		strcpy(line, "*");
14159a1f6729SWarner Losh 	else if (cp) {
14161c109628SXin LI 		strlcpy(line, cp, sizeof(line));
14179a1f6729SWarner Losh 	} else {
14189b50d902SRodney W. Grimes 		inp->s_addr = ntohl(inp->s_addr);
141922694ebaSBruce Evans #define	C(x)	((u_int)((x) & 0xff))
1420f193c8ceSXin LI 		snprintf(line, sizeof(line), "%u.%u.%u.%u",
1421f193c8ceSXin LI 		    C(inp->s_addr >> 24), C(inp->s_addr >> 16),
1422f193c8ceSXin LI 		    C(inp->s_addr >> 8), C(inp->s_addr));
14239b50d902SRodney W. Grimes 	}
14249b50d902SRodney W. Grimes 	return (line);
14259b50d902SRodney W. Grimes }
14269784899dSBjoern A. Zeeb #endif
1427