xref: /freebsd/usr.bin/netstat/inet.c (revision 2529f56ed32d808dc3911c369c731f1b21664de6)
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 
88046aad39SHajimu UMEMOTO void	inetprint(const char *, struct in_addr *, int, const char *, int,
89046aad39SHajimu UMEMOTO     const int);
90cfa1ca9dSYoshinobu Inoue #ifdef INET6
91aa0a1e58SJeff Roberson static int udp_done, tcp_done, sdp_done;
92cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
939b50d902SRodney W. Grimes 
94feda1a43SJohn Baldwin static int
95cc65eb4eSGleb Smirnoff pcblist_sysctl(int proto, const char *name, char **bufp)
96feda1a43SJohn Baldwin {
97feda1a43SJohn Baldwin 	const char *mibvar;
98feda1a43SJohn Baldwin 	char *buf;
99feda1a43SJohn Baldwin 	size_t len;
100feda1a43SJohn Baldwin 
101feda1a43SJohn Baldwin 	switch (proto) {
102feda1a43SJohn Baldwin 	case IPPROTO_TCP:
103feda1a43SJohn Baldwin 		mibvar = "net.inet.tcp.pcblist";
104feda1a43SJohn Baldwin 		break;
105feda1a43SJohn Baldwin 	case IPPROTO_UDP:
106feda1a43SJohn Baldwin 		mibvar = "net.inet.udp.pcblist";
107feda1a43SJohn Baldwin 		break;
108feda1a43SJohn Baldwin 	case IPPROTO_DIVERT:
109feda1a43SJohn Baldwin 		mibvar = "net.inet.divert.pcblist";
110feda1a43SJohn Baldwin 		break;
111feda1a43SJohn Baldwin 	default:
112feda1a43SJohn Baldwin 		mibvar = "net.inet.raw.pcblist";
113feda1a43SJohn Baldwin 		break;
114feda1a43SJohn Baldwin 	}
115aa0a1e58SJeff Roberson 	if (strncmp(name, "sdp", 3) == 0)
116aa0a1e58SJeff Roberson 		mibvar = "net.inet.sdp.pcblist";
117feda1a43SJohn Baldwin 	len = 0;
118feda1a43SJohn Baldwin 	if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
119feda1a43SJohn Baldwin 		if (errno != ENOENT)
120ade9ccfeSMarcel Moolenaar 			xo_warn("sysctl: %s", mibvar);
121feda1a43SJohn Baldwin 		return (0);
122feda1a43SJohn Baldwin 	}
123ef1cb629SMarcelo Araujo 	if ((buf = malloc(len)) == NULL) {
124ade9ccfeSMarcel Moolenaar 		xo_warnx("malloc %lu bytes", (u_long)len);
125feda1a43SJohn Baldwin 		return (0);
126feda1a43SJohn Baldwin 	}
127feda1a43SJohn Baldwin 	if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
128ade9ccfeSMarcel Moolenaar 		xo_warn("sysctl: %s", mibvar);
129feda1a43SJohn Baldwin 		free(buf);
130feda1a43SJohn Baldwin 		return (0);
131feda1a43SJohn Baldwin 	}
132feda1a43SJohn Baldwin 	*bufp = buf;
133feda1a43SJohn Baldwin 	return (1);
134feda1a43SJohn Baldwin }
135feda1a43SJohn Baldwin 
136feda1a43SJohn Baldwin /*
137feda1a43SJohn Baldwin  * Copied directly from uipc_socket2.c.  We leave out some fields that are in
138feda1a43SJohn Baldwin  * nested structures that aren't used to avoid extra work.
139feda1a43SJohn Baldwin  */
140feda1a43SJohn Baldwin static void
141feda1a43SJohn Baldwin sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
142feda1a43SJohn Baldwin {
1430f9d0a73SGleb Smirnoff 	xsb->sb_cc = sb->sb_ccc;
144feda1a43SJohn Baldwin 	xsb->sb_hiwat = sb->sb_hiwat;
145feda1a43SJohn Baldwin 	xsb->sb_mbcnt = sb->sb_mbcnt;
14649f287f8SGeorge V. Neville-Neil 	xsb->sb_mcnt = sb->sb_mcnt;
14749f287f8SGeorge V. Neville-Neil 	xsb->sb_ccnt = sb->sb_ccnt;
148feda1a43SJohn Baldwin 	xsb->sb_mbmax = sb->sb_mbmax;
149feda1a43SJohn Baldwin 	xsb->sb_lowat = sb->sb_lowat;
150feda1a43SJohn Baldwin 	xsb->sb_flags = sb->sb_flags;
151feda1a43SJohn Baldwin 	xsb->sb_timeo = sb->sb_timeo;
152feda1a43SJohn Baldwin }
153feda1a43SJohn Baldwin 
154feda1a43SJohn Baldwin int
155feda1a43SJohn Baldwin sotoxsocket(struct socket *so, struct xsocket *xso)
156feda1a43SJohn Baldwin {
157feda1a43SJohn Baldwin 	struct protosw proto;
158feda1a43SJohn Baldwin 	struct domain domain;
159feda1a43SJohn Baldwin 
160feda1a43SJohn Baldwin 	bzero(xso, sizeof *xso);
161feda1a43SJohn Baldwin 	xso->xso_len = sizeof *xso;
162feda1a43SJohn Baldwin 	xso->xso_so = so;
163feda1a43SJohn Baldwin 	xso->so_type = so->so_type;
164feda1a43SJohn Baldwin 	xso->so_options = so->so_options;
165feda1a43SJohn Baldwin 	xso->so_linger = so->so_linger;
166feda1a43SJohn Baldwin 	xso->so_state = so->so_state;
167feda1a43SJohn Baldwin 	xso->so_pcb = so->so_pcb;
168feda1a43SJohn Baldwin 	if (kread((uintptr_t)so->so_proto, &proto, sizeof(proto)) != 0)
169feda1a43SJohn Baldwin 		return (-1);
170feda1a43SJohn Baldwin 	xso->xso_protocol = proto.pr_protocol;
171feda1a43SJohn Baldwin 	if (kread((uintptr_t)proto.pr_domain, &domain, sizeof(domain)) != 0)
172feda1a43SJohn Baldwin 		return (-1);
173feda1a43SJohn Baldwin 	xso->xso_family = domain.dom_family;
174feda1a43SJohn Baldwin 	xso->so_timeo = so->so_timeo;
175feda1a43SJohn Baldwin 	xso->so_error = so->so_error;
1760e229f34SGleb Smirnoff 	if ((so->so_options & SO_ACCEPTCONN) != 0) {
177779f106aSGleb Smirnoff 		xso->so_qlen = so->sol_qlen;
178779f106aSGleb Smirnoff 		xso->so_incqlen = so->sol_incqlen;
179779f106aSGleb Smirnoff 		xso->so_qlimit = so->sol_qlimit;
180779f106aSGleb Smirnoff 	} else {
181feda1a43SJohn Baldwin 		sbtoxsockbuf(&so->so_snd, &xso->so_snd);
182feda1a43SJohn Baldwin 		sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
183779f106aSGleb Smirnoff 		xso->so_oobmark = so->so_oobmark;
184779f106aSGleb Smirnoff 	}
185feda1a43SJohn Baldwin 	return (0);
186feda1a43SJohn Baldwin }
187feda1a43SJohn Baldwin 
1889b50d902SRodney W. Grimes /*
1899b50d902SRodney W. Grimes  * Print a summary of connections related to an Internet
1909b50d902SRodney W. Grimes  * protocol.  For TCP, also give state of connection.
1919b50d902SRodney W. Grimes  * Listening processes (aflag) are suppressed unless the
1929b50d902SRodney W. Grimes  * -a (all) flag is specified.
1939b50d902SRodney W. Grimes  */
1949b50d902SRodney W. Grimes void
195feda1a43SJohn Baldwin protopr(u_long off, const char *name, int af1, int proto)
1969b50d902SRodney W. Grimes {
1979b50d902SRodney W. Grimes 	static int first = 1;
198cc65eb4eSGleb Smirnoff 	int istcp;
1994f81ef50SGarrett Wollman 	char *buf;
200feda1a43SJohn Baldwin 	const char *vchar;
201cc65eb4eSGleb Smirnoff 	struct xtcpcb *tp;
202cc65eb4eSGleb Smirnoff 	struct xinpcb *inp;
2034f81ef50SGarrett Wollman 	struct xinpgen *xig, *oxig;
2044f81ef50SGarrett Wollman 	struct xsocket *so;
2059b50d902SRodney W. Grimes 
2064f81ef50SGarrett Wollman 	istcp = 0;
2074f81ef50SGarrett Wollman 	switch (proto) {
2084f81ef50SGarrett Wollman 	case IPPROTO_TCP:
209cfa1ca9dSYoshinobu Inoue #ifdef INET6
210aa0a1e58SJeff Roberson 		if (strncmp(name, "sdp", 3) != 0) {
211cfa1ca9dSYoshinobu Inoue 			if (tcp_done != 0)
212cfa1ca9dSYoshinobu Inoue 				return;
213cfa1ca9dSYoshinobu Inoue 			else
214cfa1ca9dSYoshinobu Inoue 				tcp_done = 1;
215aa0a1e58SJeff Roberson 		} else {
216aa0a1e58SJeff Roberson 			if (sdp_done != 0)
217aa0a1e58SJeff Roberson 				return;
218aa0a1e58SJeff Roberson 			else
219aa0a1e58SJeff Roberson 				sdp_done = 1;
220aa0a1e58SJeff Roberson 		}
221cfa1ca9dSYoshinobu Inoue #endif
2224f81ef50SGarrett Wollman 		istcp = 1;
2234f81ef50SGarrett Wollman 		break;
2244f81ef50SGarrett Wollman 	case IPPROTO_UDP:
225cfa1ca9dSYoshinobu Inoue #ifdef INET6
226cfa1ca9dSYoshinobu Inoue 		if (udp_done != 0)
227cfa1ca9dSYoshinobu Inoue 			return;
228cfa1ca9dSYoshinobu Inoue 		else
229cfa1ca9dSYoshinobu Inoue 			udp_done = 1;
230cfa1ca9dSYoshinobu Inoue #endif
2314f81ef50SGarrett Wollman 		break;
2324f81ef50SGarrett Wollman 	}
233cc65eb4eSGleb Smirnoff 
234cc65eb4eSGleb Smirnoff 	if (!pcblist_sysctl(proto, name, &buf))
2359b50d902SRodney W. Grimes 		return;
2364f81ef50SGarrett Wollman 
2374f81ef50SGarrett Wollman 	oxig = xig = (struct xinpgen *)buf;
2384f81ef50SGarrett Wollman 	for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
2394f81ef50SGarrett Wollman 	    xig->xig_len > sizeof(struct xinpgen);
2404f81ef50SGarrett Wollman 	    xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
2419b50d902SRodney W. Grimes 		if (istcp) {
242cc65eb4eSGleb Smirnoff 			tp = (struct xtcpcb *)xig;
243cc65eb4eSGleb Smirnoff 			inp = &tp->xt_inp;
2444f81ef50SGarrett Wollman 		} else {
245cc65eb4eSGleb Smirnoff 			inp = (struct xinpcb *)xig;
2469b50d902SRodney W. Grimes 		}
247cc65eb4eSGleb Smirnoff 		so = &inp->xi_socket;
2484f81ef50SGarrett Wollman 
2494f81ef50SGarrett Wollman 		/* Ignore sockets for protocols other than the desired one. */
250feda1a43SJohn Baldwin 		if (so->xso_protocol != proto)
2514f81ef50SGarrett Wollman 			continue;
2524f81ef50SGarrett Wollman 
2534f81ef50SGarrett Wollman 		/* Ignore PCBs which were freed during copyout. */
2544f81ef50SGarrett Wollman 		if (inp->inp_gencnt > oxig->xig_gen)
2554f81ef50SGarrett Wollman 			continue;
2564f81ef50SGarrett Wollman 
257a01e3379SDavid Malone 		if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
258cfa1ca9dSYoshinobu Inoue #ifdef INET6
259a01e3379SDavid Malone 		    || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
260cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
261a01e3379SDavid Malone 		    || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
262cfa1ca9dSYoshinobu Inoue #ifdef INET6
2633feeb332SDavid E. O'Brien 					  && (inp->inp_vflag & INP_IPV6) == 0
264cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
265cfa1ca9dSYoshinobu Inoue 			))
266cfa1ca9dSYoshinobu Inoue 		    )
267cfa1ca9dSYoshinobu Inoue 			continue;
268cfa1ca9dSYoshinobu Inoue 		if (!aflag &&
269cfa1ca9dSYoshinobu Inoue 		    (
2702b286cedSBruce M Simpson 		     (istcp && tp->t_state == TCPS_LISTEN)
2712b286cedSBruce M Simpson 		     || (af1 == AF_INET &&
272cfa1ca9dSYoshinobu Inoue 		      inet_lnaof(inp->inp_laddr) == INADDR_ANY)
273cfa1ca9dSYoshinobu Inoue #ifdef INET6
274a01e3379SDavid Malone 		     || (af1 == AF_INET6 &&
275cfa1ca9dSYoshinobu Inoue 			 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
276cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
277a01e3379SDavid Malone 		     || (af1 == AF_UNSPEC &&
278cfa1ca9dSYoshinobu Inoue 			 (((inp->inp_vflag & INP_IPV4) != 0 &&
279cfa1ca9dSYoshinobu Inoue 			   inet_lnaof(inp->inp_laddr) == INADDR_ANY)
280cfa1ca9dSYoshinobu Inoue #ifdef INET6
281cfa1ca9dSYoshinobu Inoue 			  || ((inp->inp_vflag & INP_IPV6) != 0 &&
282cfa1ca9dSYoshinobu Inoue 			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
283cfa1ca9dSYoshinobu Inoue #endif
284cfa1ca9dSYoshinobu Inoue 			  ))
285cfa1ca9dSYoshinobu Inoue 		     ))
2864f81ef50SGarrett Wollman 			continue;
2874f81ef50SGarrett Wollman 
2889b50d902SRodney W. Grimes 		if (first) {
289ac55add0SGuido van Rooij 			if (!Lflag) {
290ade9ccfeSMarcel Moolenaar 				xo_emit("Active Internet connections");
2919b50d902SRodney W. Grimes 				if (aflag)
292ade9ccfeSMarcel Moolenaar 					xo_emit(" (including servers)");
293ac55add0SGuido van Rooij 			} else
294ade9ccfeSMarcel Moolenaar 				xo_emit(
295ac55add0SGuido van Rooij 	"Current listen queue sizes (qlen/incqlen/maxqlen)");
296ade9ccfeSMarcel Moolenaar 			xo_emit("\n");
2979b50d902SRodney W. Grimes 			if (Aflag)
298ade9ccfeSMarcel Moolenaar 				xo_emit("{T:/%-*s} ", 2 * (int)sizeof(void *),
299ade9ccfeSMarcel Moolenaar 				    "Tcpcb");
300ac55add0SGuido van Rooij 			if (Lflag)
301ade9ccfeSMarcel Moolenaar 				xo_emit((Aflag && !Wflag) ?
302db2627f4SMichael Tuexen 				    "{T:/%-5.5s} {T:/%-32.32s} {T:/%-18.18s}" :
303046aad39SHajimu UMEMOTO 				    ((!Wflag || af1 == AF_INET) ?
304db2627f4SMichael Tuexen 				    "{T:/%-5.5s} {T:/%-32.32s} {T:/%-22.22s}" :
305db2627f4SMichael Tuexen 				    "{T:/%-5.5s} {T:/%-32.32s} {T:/%-45.45s}"),
306afc74015SRuslan Ermilov 				    "Proto", "Listen", "Local Address");
307afc74015SRuslan Ermilov 			else if (Tflag)
308ade9ccfeSMarcel Moolenaar 				xo_emit((Aflag && !Wflag) ?
309ade9ccfeSMarcel Moolenaar     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%s}" :
310046aad39SHajimu UMEMOTO 				    ((!Wflag || af1 == AF_INET) ?
311046aad39SHajimu UMEMOTO     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%s}" :
312046aad39SHajimu UMEMOTO     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%s}"),
313f5d34df5SGeorge V. Neville-Neil 				    "Proto", "Rexmit", "OOORcv", "0-win",
31449f287f8SGeorge V. Neville-Neil 				    "Local Address", "Foreign Address");
315afc74015SRuslan Ermilov 			else {
316ade9ccfeSMarcel Moolenaar 				xo_emit((Aflag && !Wflag) ?
317ade9ccfeSMarcel Moolenaar     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%-18.18s}" :
318046aad39SHajimu UMEMOTO 				    ((!Wflag || af1 == AF_INET) ?
319046aad39SHajimu UMEMOTO     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%-22.22s}" :
320046aad39SHajimu UMEMOTO     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%-45.45s}"),
321afc74015SRuslan Ermilov 				    "Proto", "Recv-Q", "Send-Q",
322afc74015SRuslan Ermilov 				    "Local Address", "Foreign Address");
32385b0f0f3SAdrian Chadd 				if (!xflag && !Rflag)
324*2529f56eSJonathan T. Looney 					xo_emit(" {T:/%-11.11s}", "(state)");
325afc74015SRuslan Ermilov 			}
326b8614722SMike Silbersack 			if (xflag) {
327ade9ccfeSMarcel Moolenaar 				xo_emit(" {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 				    "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s}",
331ade9ccfeSMarcel Moolenaar 				    "R-MBUF", "S-MBUF", "R-CLUS", "S-CLUS",
332ade9ccfeSMarcel Moolenaar 				    "R-HIWA", "S-HIWA", "R-LOWA", "S-LOWA",
333ade9ccfeSMarcel Moolenaar 				    "R-BCNT", "S-BCNT", "R-BMAX", "S-BMAX");
334ade9ccfeSMarcel Moolenaar 				xo_emit(" {T:/%7.7s} {T:/%7.7s} {T:/%7.7s} "
335ade9ccfeSMarcel Moolenaar 				    "{T:/%7.7s} {T:/%7.7s} {T:/%7.7s}",
336ade9ccfeSMarcel Moolenaar 				    "rexmt", "persist", "keep", "2msl",
337ade9ccfeSMarcel Moolenaar 				    "delack", "rcvtime");
33885b0f0f3SAdrian Chadd 			} else if (Rflag) {
339ade9ccfeSMarcel Moolenaar 				xo_emit("  {T:/%8.8s} {T:/%5.5s}",
34085b0f0f3SAdrian Chadd 				    "flowid", "ftype");
34194f138feSGeorge V. Neville-Neil 			}
342*2529f56eSJonathan T. Looney 			if (Pflag)
343*2529f56eSJonathan T. Looney 				xo_emit(" {T:/%s}", "Log ID");
344ade9ccfeSMarcel Moolenaar 			xo_emit("\n");
3459b50d902SRodney W. Grimes 			first = 0;
3469b50d902SRodney W. Grimes 		}
347fb5d0fbdSRuslan Ermilov 		if (Lflag && so->so_qlimit == 0)
348fb5d0fbdSRuslan Ermilov 			continue;
349ade9ccfeSMarcel Moolenaar 		xo_open_instance("socket");
350e4ec3989SDag-Erling Smørgrav 		if (Aflag) {
351e4ec3989SDag-Erling Smørgrav 			if (istcp)
352ade9ccfeSMarcel Moolenaar 				xo_emit("{q:address/%*lx} ",
353ade9ccfeSMarcel Moolenaar 				    2 * (int)sizeof(void *),
354ade9ccfeSMarcel Moolenaar 				    (u_long)inp->inp_ppcb);
355e4ec3989SDag-Erling Smørgrav 			else
356d9646465SGleb Smirnoff 				xo_emit("{q:address/%*lx} ",
357ade9ccfeSMarcel Moolenaar 				    2 * (int)sizeof(void *),
358ade9ccfeSMarcel Moolenaar 				    (u_long)so->so_pcb);
359e4ec3989SDag-Erling Smørgrav 		}
360cfa1ca9dSYoshinobu Inoue #ifdef INET6
361fc60ab7bSYoshinobu Inoue 		if ((inp->inp_vflag & INP_IPV6) != 0)
3623feeb332SDavid E. O'Brien 			vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
3633feeb332SDavid E. O'Brien 			    "46" : "6";
364fc60ab7bSYoshinobu Inoue 		else
365cfa1ca9dSYoshinobu Inoue #endif
3663feeb332SDavid E. O'Brien 		vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
3673feeb332SDavid E. O'Brien 		    "4" : "";
36809fe6320SNavdeep Parhar 		if (istcp && (tp->t_flags & TF_TOE) != 0)
369ade9ccfeSMarcel Moolenaar 			xo_emit("{:protocol/%-3.3s%-2.2s/%s%s} ", "toe", vchar);
37009fe6320SNavdeep Parhar 		else
371ade9ccfeSMarcel Moolenaar 			xo_emit("{:protocol/%-3.3s%-2.2s/%s%s} ", name, vchar);
372fb5d0fbdSRuslan Ermilov 		if (Lflag) {
3737325dfbbSAlfred Perlstein 			char buf1[33];
374fc60ab7bSYoshinobu Inoue 
3757325dfbbSAlfred Perlstein 			snprintf(buf1, sizeof buf1, "%u/%u/%u", so->so_qlen,
376fb5d0fbdSRuslan Ermilov 			    so->so_incqlen, so->so_qlimit);
3777325dfbbSAlfred Perlstein 			xo_emit("{:listen-queue-sizes/%-32.32s} ", buf1);
378f5d34df5SGeorge V. Neville-Neil 		} else if (Tflag) {
379f5d34df5SGeorge V. Neville-Neil 			if (istcp)
380ade9ccfeSMarcel Moolenaar 				xo_emit("{:sent-retransmit-packets/%6u} "
381ade9ccfeSMarcel Moolenaar 				    "{:received-out-of-order-packets/%6u} "
382ade9ccfeSMarcel Moolenaar 				    "{:sent-zero-window/%6u} ",
383ade9ccfeSMarcel Moolenaar 				    tp->t_sndrexmitpack, tp->t_rcvoopack,
384ade9ccfeSMarcel Moolenaar 				    tp->t_sndzerowin);
3850eaa116eSHajimu UMEMOTO 			else
3860eaa116eSHajimu UMEMOTO 				xo_emit("{P:/%21s}", "");
387fb5d0fbdSRuslan Ermilov 		} else {
388ade9ccfeSMarcel Moolenaar 			xo_emit("{:receive-bytes-waiting/%6u} "
389ade9ccfeSMarcel Moolenaar 			    "{:send-bytes-waiting/%6u} ",
3900f9d0a73SGleb Smirnoff 			    so->so_rcv.sb_cc, so->so_snd.sb_cc);
391fc60ab7bSYoshinobu Inoue 		}
39265ea0024SAssar Westerlund 		if (numeric_port) {
393cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
394ade9ccfeSMarcel Moolenaar 				inetprint("local", &inp->inp_laddr,
395046aad39SHajimu UMEMOTO 				    (int)inp->inp_lport, name, 1, af1);
396ac55add0SGuido van Rooij 				if (!Lflag)
397ade9ccfeSMarcel Moolenaar 					inetprint("remote", &inp->inp_faddr,
398046aad39SHajimu UMEMOTO 					    (int)inp->inp_fport, name, 1, af1);
399cfa1ca9dSYoshinobu Inoue 			}
400cfa1ca9dSYoshinobu Inoue #ifdef INET6
401cfa1ca9dSYoshinobu Inoue 			else if (inp->inp_vflag & INP_IPV6) {
402ade9ccfeSMarcel Moolenaar 				inet6print("local", &inp->in6p_laddr,
403cfa1ca9dSYoshinobu Inoue 				    (int)inp->inp_lport, name, 1);
404ac55add0SGuido van Rooij 				if (!Lflag)
405ade9ccfeSMarcel Moolenaar 					inet6print("remote", &inp->in6p_faddr,
406cfa1ca9dSYoshinobu Inoue 					    (int)inp->inp_fport, name, 1);
407cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
408cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
4094f81ef50SGarrett Wollman 		} else if (inp->inp_flags & INP_ANONPORT) {
410cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
411ade9ccfeSMarcel Moolenaar 				inetprint("local", &inp->inp_laddr,
412046aad39SHajimu UMEMOTO 				    (int)inp->inp_lport, name, 1, af1);
413ac55add0SGuido van Rooij 				if (!Lflag)
414ade9ccfeSMarcel Moolenaar 					inetprint("remote", &inp->inp_faddr,
415046aad39SHajimu UMEMOTO 					    (int)inp->inp_fport, name, 0, af1);
416cfa1ca9dSYoshinobu Inoue 			}
417cfa1ca9dSYoshinobu Inoue #ifdef INET6
418cfa1ca9dSYoshinobu Inoue 			else if (inp->inp_vflag & INP_IPV6) {
419ade9ccfeSMarcel Moolenaar 				inet6print("local", &inp->in6p_laddr,
420cfa1ca9dSYoshinobu Inoue 				    (int)inp->inp_lport, name, 1);
421ac55add0SGuido van Rooij 				if (!Lflag)
422ade9ccfeSMarcel Moolenaar 					inet6print("remote", &inp->in6p_faddr,
423cfa1ca9dSYoshinobu Inoue 					    (int)inp->inp_fport, name, 0);
424cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
425cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
4268d612dd2SPoul-Henning Kamp 		} else {
427cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
428ade9ccfeSMarcel Moolenaar 				inetprint("local", &inp->inp_laddr,
429046aad39SHajimu UMEMOTO 				    (int)inp->inp_lport, name, 0, af1);
430ac55add0SGuido van Rooij 				if (!Lflag)
431ade9ccfeSMarcel Moolenaar 					inetprint("remote", &inp->inp_faddr,
432ac55add0SGuido van Rooij 					    (int)inp->inp_fport, name,
433046aad39SHajimu UMEMOTO 					    inp->inp_lport != inp->inp_fport,
434046aad39SHajimu UMEMOTO 					    af1);
435cfa1ca9dSYoshinobu Inoue 			}
436cfa1ca9dSYoshinobu Inoue #ifdef INET6
437cfa1ca9dSYoshinobu Inoue 			else if (inp->inp_vflag & INP_IPV6) {
438ade9ccfeSMarcel Moolenaar 				inet6print("local", &inp->in6p_laddr,
439cfa1ca9dSYoshinobu Inoue 				    (int)inp->inp_lport, name, 0);
440ac55add0SGuido van Rooij 				if (!Lflag)
441ade9ccfeSMarcel Moolenaar 					inet6print("remote", &inp->in6p_faddr,
442cfa1ca9dSYoshinobu Inoue 					    (int)inp->inp_fport, name,
4433feeb332SDavid E. O'Brien 					    inp->inp_lport != inp->inp_fport);
444cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
445cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
4468d612dd2SPoul-Henning Kamp 		}
44749f287f8SGeorge V. Neville-Neil 		if (xflag) {
448ade9ccfeSMarcel Moolenaar 			xo_emit("{:receive-mbufs/%6u} {:send-mbufs/%6u} "
449ade9ccfeSMarcel Moolenaar 			    "{:receive-clusters/%6u} {:send-clusters/%6u} "
450ade9ccfeSMarcel Moolenaar 			    "{:receive-high-water/%6u} {:send-high-water/%6u} "
451ade9ccfeSMarcel Moolenaar 			    "{:receive-low-water/%6u} {:send-low-water/%6u} "
452ade9ccfeSMarcel Moolenaar 			    "{:receive-mbuf-bytes/%6u} {:send-mbuf-bytes/%6u} "
453ade9ccfeSMarcel Moolenaar 			    "{:receive-mbuf-bytes-max/%6u} "
454ade9ccfeSMarcel Moolenaar 			    "{:send-mbuf-bytes-max/%6u}",
45549f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt,
45649f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt,
45749f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
45849f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
45949f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
46049f287f8SGeorge V. Neville-Neil 			    so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
461cc65eb4eSGleb Smirnoff 			if (istcp)
462ade9ccfeSMarcel Moolenaar 				xo_emit(" {:retransmit-timer/%4d.%02d} "
463ade9ccfeSMarcel Moolenaar 				    "{:persist-timer/%4d.%02d} "
464ade9ccfeSMarcel Moolenaar 				    "{:keepalive-timer/%4d.%02d} "
465ade9ccfeSMarcel Moolenaar 				    "{:msl2-timer/%4d.%02d} "
466ade9ccfeSMarcel Moolenaar 				    "{:delay-ack-timer/%4d.%02d} "
467ade9ccfeSMarcel Moolenaar 				    "{:inactivity-timer/%4d.%02d}",
468cc65eb4eSGleb Smirnoff 				    tp->tt_rexmt / 1000,
469cc65eb4eSGleb Smirnoff 				    (tp->tt_rexmt % 1000) / 10,
470cc65eb4eSGleb Smirnoff 				    tp->tt_persist / 1000,
471cc65eb4eSGleb Smirnoff 				    (tp->tt_persist % 1000) / 10,
472cc65eb4eSGleb Smirnoff 				    tp->tt_keep / 1000,
473cc65eb4eSGleb Smirnoff 				    (tp->tt_keep % 1000) / 10,
474cc65eb4eSGleb Smirnoff 				    tp->tt_2msl / 1000,
475cc65eb4eSGleb Smirnoff 				    (tp->tt_2msl % 1000) / 10,
476cc65eb4eSGleb Smirnoff 				    tp->tt_delack / 1000,
477cc65eb4eSGleb Smirnoff 				    (tp->tt_delack % 1000) / 10,
478cc65eb4eSGleb Smirnoff 				    tp->t_rcvtime / 1000,
479cc65eb4eSGleb Smirnoff 				    (tp->t_rcvtime % 1000) / 10);
480b8614722SMike Silbersack 		}
48185b0f0f3SAdrian Chadd 		if (istcp && !Lflag && !xflag && !Tflag && !Rflag) {
4824f81ef50SGarrett Wollman 			if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
483*2529f56eSJonathan T. Looney 				xo_emit("{:tcp-state/%-11d}", tp->t_state);
4849a94a597SGarrett Wollman 			else {
485*2529f56eSJonathan T. Looney 				xo_emit("{:tcp-state/%-11s}",
486ade9ccfeSMarcel Moolenaar 				    tcpstates[tp->t_state]);
487513822ddSAdam David #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
4889a94a597SGarrett Wollman 				/* Show T/TCP `hidden state' */
4894f81ef50SGarrett Wollman 				if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
490ade9ccfeSMarcel Moolenaar 					xo_emit("{:need-syn-or-fin/*}");
491513822ddSAdam David #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
4929a94a597SGarrett Wollman 			}
4939b50d902SRodney W. Grimes 		}
49485b0f0f3SAdrian Chadd 		if (Rflag) {
495ade9ccfeSMarcel Moolenaar 			/* XXX: is this right Alfred */
496ade9ccfeSMarcel Moolenaar 			xo_emit(" {:flow-id/%08x} {:flow-type/%5d}",
49785b0f0f3SAdrian Chadd 			    inp->inp_flowid,
49885b0f0f3SAdrian Chadd 			    inp->inp_flowtype);
49985b0f0f3SAdrian Chadd 		}
500*2529f56eSJonathan T. Looney 		if (istcp && Pflag)
501*2529f56eSJonathan T. Looney 			xo_emit(" {:log-id/%s}", tp->xt_logid[0] == '\0' ?
502*2529f56eSJonathan T. Looney 			    "-" : tp->xt_logid);
503ade9ccfeSMarcel Moolenaar 		xo_emit("\n");
504ade9ccfeSMarcel Moolenaar 		xo_close_instance("socket");
5059b50d902SRodney W. Grimes 	}
5064f81ef50SGarrett Wollman 	if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
5074f81ef50SGarrett Wollman 		if (oxig->xig_count > xig->xig_count) {
508ade9ccfeSMarcel Moolenaar 			xo_emit("Some {d:lost/%s} sockets may have been "
509ade9ccfeSMarcel Moolenaar 			    "deleted.\n", name);
5104f81ef50SGarrett Wollman 		} else if (oxig->xig_count < xig->xig_count) {
511ade9ccfeSMarcel Moolenaar 			xo_emit("Some {d:created/%s} sockets may have been "
512ade9ccfeSMarcel Moolenaar 			    "created.\n", name);
5134f81ef50SGarrett Wollman 		} else {
514ade9ccfeSMarcel Moolenaar 			xo_emit("Some {d:changed/%s} sockets may have been "
515ade9ccfeSMarcel Moolenaar 			    "created or deleted.\n", name);
5164f81ef50SGarrett Wollman 		}
5174f81ef50SGarrett Wollman 	}
5184f81ef50SGarrett Wollman 	free(buf);
5199b50d902SRodney W. Grimes }
5209b50d902SRodney W. Grimes 
5219b50d902SRodney W. Grimes /*
5229b50d902SRodney W. Grimes  * Dump TCP statistics structure.
5239b50d902SRodney W. Grimes  */
5249b50d902SRodney W. Grimes void
525feda1a43SJohn Baldwin tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
5269b50d902SRodney W. Grimes {
5279eddb899SMark Johnston 	struct tcpstat tcpstat;
528dbfd8708SGleb Smirnoff 	uint64_t tcps_states[TCP_NSTATES];
5299b50d902SRodney W. Grimes 
530cfa1ca9dSYoshinobu Inoue #ifdef INET6
531cfa1ca9dSYoshinobu Inoue 	if (tcp_done != 0)
532cfa1ca9dSYoshinobu Inoue 		return;
533cfa1ca9dSYoshinobu Inoue 	else
534cfa1ca9dSYoshinobu Inoue 		tcp_done = 1;
535cfa1ca9dSYoshinobu Inoue #endif
536cfa1ca9dSYoshinobu Inoue 
5379eddb899SMark Johnston 	if (fetch_stats("net.inet.tcp.stats", off, &tcpstat,
5389eddb899SMark Johnston 	    sizeof(tcpstat), kread_counters) != 0)
539feda1a43SJohn Baldwin 		return;
540feda1a43SJohn Baldwin 
541dbfd8708SGleb Smirnoff 	if (fetch_stats_ro("net.inet.tcp.states", nl[N_TCPS_STATES].n_value,
542dbfd8708SGleb Smirnoff 	    &tcps_states, sizeof(tcps_states), kread_counters) != 0)
543dbfd8708SGleb Smirnoff 		return;
544dbfd8708SGleb Smirnoff 
545ade9ccfeSMarcel Moolenaar 	xo_open_container("tcp");
546ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
5479b50d902SRodney W. Grimes 
5489b50d902SRodney W. Grimes #define	p(f, m) if (tcpstat.f || sflag <= 1)				\
549ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )tcpstat.f, plural(tcpstat.f))
55022694ebaSBruce Evans #define	p1a(f, m) if (tcpstat.f || sflag <= 1)				\
551ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )tcpstat.f)
5529b50d902SRodney W. Grimes #define	p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1)	\
553ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1),		\
5545923c293SGleb Smirnoff 	    (uintmax_t )tcpstat.f2, plural(tcpstat.f2))
55522694ebaSBruce Evans #define	p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1)	\
556ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1),		\
5575923c293SGleb Smirnoff 	    (uintmax_t )tcpstat.f2)
5589b50d902SRodney W. Grimes #define	p3(f, m) if (tcpstat.f || sflag <= 1)				\
559ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )tcpstat.f, pluralies(tcpstat.f))
5609b50d902SRodney W. Grimes 
561ade9ccfeSMarcel Moolenaar 	p(tcps_sndtotal, "\t{:sent-packets/%ju} {N:/packet%s sent}\n");
562ade9ccfeSMarcel Moolenaar 	p2(tcps_sndpack,tcps_sndbyte, "\t\t{:sent-data-packets/%ju} "
563ade9ccfeSMarcel Moolenaar 	    "{N:/data packet%s} ({:sent-data-bytes/%ju} {N:/byte%s})\n");
564ade9ccfeSMarcel Moolenaar 	p2(tcps_sndrexmitpack, tcps_sndrexmitbyte, "\t\t"
565ade9ccfeSMarcel Moolenaar 	    "{:sent-retransmitted-packets/%ju} {N:/data packet%s} "
566ade9ccfeSMarcel Moolenaar 	    "({:sent-retransmitted-bytes/%ju} {N:/byte%s}) "
567ade9ccfeSMarcel Moolenaar 	    "{N:retransmitted}\n");
568ade9ccfeSMarcel Moolenaar 	p(tcps_sndrexmitbad, "\t\t"
569ade9ccfeSMarcel Moolenaar 	    "{:sent-unnecessary-retransmitted-packets/%ju} "
570ade9ccfeSMarcel Moolenaar 	    "{N:/data packet%s unnecessarily retransmitted}\n");
571ade9ccfeSMarcel Moolenaar 	p(tcps_mturesent, "\t\t{:sent-resends-by-mtu-discovery/%ju} "
572ade9ccfeSMarcel Moolenaar 	    "{N:/resend%s initiated by MTU discovery}\n");
573ade9ccfeSMarcel Moolenaar 	p2a(tcps_sndacks, tcps_delack, "\t\t{:sent-ack-only-packets/%ju} "
574ade9ccfeSMarcel Moolenaar 	    "{N:/ack-only packet%s/} ({:sent-packets-delayed/%ju} "
575ade9ccfeSMarcel Moolenaar 	    "{N:delayed})\n");
576ade9ccfeSMarcel Moolenaar 	p(tcps_sndurg, "\t\t{:sent-urg-only-packets/%ju} "
577ade9ccfeSMarcel Moolenaar 	    "{N:/URG only packet%s}\n");
578ade9ccfeSMarcel Moolenaar 	p(tcps_sndprobe, "\t\t{:sent-window-probe-packets/%ju} "
579ade9ccfeSMarcel Moolenaar 	    "{N:/window probe packet%s}\n");
580ade9ccfeSMarcel Moolenaar 	p(tcps_sndwinup, "\t\t{:sent-window-update-packets/%ju} "
581ade9ccfeSMarcel Moolenaar 	    "{N:/window update packet%s}\n");
582ade9ccfeSMarcel Moolenaar 	p(tcps_sndctrl, "\t\t{:sent-control-packets/%ju} "
583ade9ccfeSMarcel Moolenaar 	    "{N:/control packet%s}\n");
584ade9ccfeSMarcel Moolenaar 	p(tcps_rcvtotal, "\t{:received-packets/%ju} "
585ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s received}\n");
586ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t"
587ade9ccfeSMarcel Moolenaar 	    "{:received-ack-packets/%ju} {N:/ack%s} "
588ade9ccfeSMarcel Moolenaar 	    "{N:(for} {:received-ack-bytes/%ju} {N:/byte%s})\n");
589ade9ccfeSMarcel Moolenaar 	p(tcps_rcvdupack, "\t\t{:received-duplicate-acks/%ju} "
590ade9ccfeSMarcel Moolenaar 	    "{N:/duplicate ack%s}\n");
591ade9ccfeSMarcel Moolenaar 	p(tcps_rcvacktoomuch, "\t\t{:received-acks-for-unsent-data/%ju} "
592ade9ccfeSMarcel Moolenaar 	    "{N:/ack%s for unsent data}\n");
593ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvpack, tcps_rcvbyte, "\t\t"
594ade9ccfeSMarcel Moolenaar 	    "{:received-in-sequence-packets/%ju} {N:/packet%s} "
595ade9ccfeSMarcel Moolenaar 	    "({:received-in-sequence-bytes/%ju} {N:/byte%s}) "
596ade9ccfeSMarcel Moolenaar 	    "{N:received in-sequence}\n");
597ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvduppack, tcps_rcvdupbyte, "\t\t"
598ade9ccfeSMarcel Moolenaar 	    "{:received-completely-duplicate-packets/%ju} "
599ade9ccfeSMarcel Moolenaar 	    "{N:/completely duplicate packet%s} "
600ade9ccfeSMarcel Moolenaar 	    "({:received-completely-duplicate-bytes/%ju} {N:/byte%s})\n");
601ade9ccfeSMarcel Moolenaar 	p(tcps_pawsdrop, "\t\t{:received-old-duplicate-packets/%ju} "
602ade9ccfeSMarcel Moolenaar 	    "{N:/old duplicate packet%s}\n");
603ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte, "\t\t"
604ade9ccfeSMarcel Moolenaar 	    "{:received-some-duplicate-packets/%ju} "
605ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s with some dup. data} "
606ade9ccfeSMarcel Moolenaar 	    "({:received-some-duplicate-bytes/%ju} {N:/byte%s duped/})\n");
607ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvoopack, tcps_rcvoobyte, "\t\t{:received-out-of-order/%ju} "
608ade9ccfeSMarcel Moolenaar 	    "{N:/out-of-order packet%s} "
609ade9ccfeSMarcel Moolenaar 	    "({:received-out-of-order-bytes/%ju} {N:/byte%s})\n");
610ade9ccfeSMarcel Moolenaar 	p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin, "\t\t"
611ade9ccfeSMarcel Moolenaar 	    "{:received-after-window-packets/%ju} {N:/packet%s} "
612ade9ccfeSMarcel Moolenaar 	    "({:received-after-window-bytes/%ju} {N:/byte%s}) "
613ade9ccfeSMarcel Moolenaar 	    "{N:of data after window}\n");
614ade9ccfeSMarcel Moolenaar 	p(tcps_rcvwinprobe, "\t\t{:received-window-probes/%ju} "
615ade9ccfeSMarcel Moolenaar 	    "{N:/window probe%s}\n");
616ade9ccfeSMarcel Moolenaar 	p(tcps_rcvwinupd, "\t\t{:receive-window-update-packets/%ju} "
617ade9ccfeSMarcel Moolenaar 	    "{N:/window update packet%s}\n");
618ade9ccfeSMarcel Moolenaar 	p(tcps_rcvafterclose, "\t\t{:received-after-close-packets/%ju} "
619ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s received after close}\n");
620ade9ccfeSMarcel Moolenaar 	p(tcps_rcvbadsum, "\t\t{:discard-bad-checksum/%ju} "
621ade9ccfeSMarcel Moolenaar 	    "{N:/discarded for bad checksum%s}\n");
622ade9ccfeSMarcel Moolenaar 	p(tcps_rcvbadoff, "\t\t{:discard-bad-header-offset/%ju} "
623ade9ccfeSMarcel Moolenaar 	    "{N:/discarded for bad header offset field%s}\n");
624ade9ccfeSMarcel Moolenaar 	p1a(tcps_rcvshort, "\t\t{:discard-too-short/%ju} "
625ade9ccfeSMarcel Moolenaar 	    "{N:discarded because packet too short}\n");
626ade9ccfeSMarcel Moolenaar 	p1a(tcps_rcvmemdrop, "\t\t{:discard-memory-problems/%ju} "
627ade9ccfeSMarcel Moolenaar 	    "{N:discarded due to memory problems}\n");
628ade9ccfeSMarcel Moolenaar 	p(tcps_connattempt, "\t{:connection-requests/%ju} "
629ade9ccfeSMarcel Moolenaar 	    "{N:/connection request%s}\n");
630ade9ccfeSMarcel Moolenaar 	p(tcps_accepts, "\t{:connections-accepts/%ju} "
631ade9ccfeSMarcel Moolenaar 	    "{N:/connection accept%s}\n");
632ade9ccfeSMarcel Moolenaar 	p(tcps_badsyn, "\t{:bad-connection-attempts/%ju} "
633ade9ccfeSMarcel Moolenaar 	    "{N:/bad connection attempt%s}\n");
634ade9ccfeSMarcel Moolenaar 	p(tcps_listendrop, "\t{:listen-queue-overflows/%ju} "
635ade9ccfeSMarcel Moolenaar 	    "{N:/listen queue overflow%s}\n");
636ade9ccfeSMarcel Moolenaar 	p(tcps_badrst, "\t{:ignored-in-window-resets/%ju} "
637ade9ccfeSMarcel Moolenaar 	    "{N:/ignored RSTs in the window%s}\n");
638ade9ccfeSMarcel Moolenaar 	p(tcps_connects, "\t{:connections-established/%ju} "
639ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s established (including accepts)}\n");
640e9d2c201SMarcelo Araujo 	p(tcps_usedrtt, "\t\t{:connections-hostcache-rtt/%ju} "
641e9d2c201SMarcelo Araujo 	    "{N:/time%s used RTT from hostcache}\n");
642e9d2c201SMarcelo Araujo 	p(tcps_usedrttvar, "\t\t{:connections-hostcache-rttvar/%ju} "
643e9d2c201SMarcelo Araujo 	    "{N:/time%s used RTT variance from hostcache}\n");
644e9d2c201SMarcelo Araujo 	p(tcps_usedssthresh, "\t\t{:connections-hostcache-ssthresh/%ju} "
645e9d2c201SMarcelo Araujo 	    "{N:/time%s used slow-start threshold from hostcache}\n");
646ade9ccfeSMarcel Moolenaar 	p2(tcps_closed, tcps_drops, "\t{:connections-closed/%ju} "
647ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s closed (including} "
648ade9ccfeSMarcel Moolenaar 	    "{:connection-drops/%ju} {N:/drop%s})\n");
649ade9ccfeSMarcel Moolenaar 	p(tcps_cachedrtt, "\t\t{:connections-updated-rtt-on-close/%ju} "
650ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s updated cached RTT on close}\n");
651ade9ccfeSMarcel Moolenaar 	p(tcps_cachedrttvar, "\t\t"
652ade9ccfeSMarcel Moolenaar 	    "{:connections-updated-variance-on-close/%ju} "
653ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s updated cached RTT variance on close}\n");
654ade9ccfeSMarcel Moolenaar 	p(tcps_cachedssthresh, "\t\t"
655ade9ccfeSMarcel Moolenaar 	    "{:connections-updated-ssthresh-on-close/%ju} "
656ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s updated cached ssthresh on close}\n");
657ade9ccfeSMarcel Moolenaar 	p(tcps_conndrops, "\t{:embryonic-connections-dropped/%ju} "
658ade9ccfeSMarcel Moolenaar 	    "{N:/embryonic connection%s dropped}\n");
659ade9ccfeSMarcel Moolenaar 	p2(tcps_rttupdated, tcps_segstimed, "\t{:segments-updated-rtt/%ju} "
660ade9ccfeSMarcel Moolenaar 	    "{N:/segment%s updated rtt (of} "
661ade9ccfeSMarcel Moolenaar 	    "{:segment-update-attempts/%ju} {N:/attempt%s})\n");
662ade9ccfeSMarcel Moolenaar 	p(tcps_rexmttimeo, "\t{:retransmit-timeouts/%ju} "
663ade9ccfeSMarcel Moolenaar 	    "{N:/retransmit timeout%s}\n");
664ade9ccfeSMarcel Moolenaar 	p(tcps_timeoutdrop, "\t\t"
665ade9ccfeSMarcel Moolenaar 	    "{:connections-dropped-by-retransmit-timeout/%ju} "
666ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s dropped by rexmit timeout}\n");
667ade9ccfeSMarcel Moolenaar 	p(tcps_persisttimeo, "\t{:persist-timeout/%ju} "
668ade9ccfeSMarcel Moolenaar 	    "{N:/persist timeout%s}\n");
669ade9ccfeSMarcel Moolenaar 	p(tcps_persistdrop, "\t\t"
670ade9ccfeSMarcel Moolenaar 	    "{:connections-dropped-by-persist-timeout/%ju} "
671ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s dropped by persist timeout}\n");
672ade9ccfeSMarcel Moolenaar 	p(tcps_finwait2_drops, "\t"
673ade9ccfeSMarcel Moolenaar 	    "{:connections-dropped-by-finwait2-timeout/%ju} "
674ade9ccfeSMarcel Moolenaar 	    "{N:/Connection%s (fin_wait_2) dropped because of timeout}\n");
675ade9ccfeSMarcel Moolenaar 	p(tcps_keeptimeo, "\t{:keepalive-timeout/%ju} "
676ade9ccfeSMarcel Moolenaar 	    "{N:/keepalive timeout%s}\n");
677ade9ccfeSMarcel Moolenaar 	p(tcps_keepprobe, "\t\t{:keepalive-probes/%ju} "
678ade9ccfeSMarcel Moolenaar 	    "{N:/keepalive probe%s sent}\n");
679ade9ccfeSMarcel Moolenaar 	p(tcps_keepdrops, "\t\t{:connections-dropped-by-keepalives/%ju} "
680ade9ccfeSMarcel Moolenaar 	    "{N:/connection%s dropped by keepalive}\n");
681ade9ccfeSMarcel Moolenaar 	p(tcps_predack, "\t{:ack-header-predictions/%ju} "
682ade9ccfeSMarcel Moolenaar 	    "{N:/correct ACK header prediction%s}\n");
683ade9ccfeSMarcel Moolenaar 	p(tcps_preddat, "\t{:data-packet-header-predictions/%ju} "
684ade9ccfeSMarcel Moolenaar 	    "{N:/correct data packet header prediction%s}\n");
68560a31b3aSJonathan Lemon 
686ade9ccfeSMarcel Moolenaar 	xo_open_container("syncache");
687b6101dafSPaul Saab 
688ade9ccfeSMarcel Moolenaar 	p3(tcps_sc_added, "\t{:entries-added/%ju} "
689ade9ccfeSMarcel Moolenaar 	    "{N:/syncache entr%s added}\n");
690ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_retransmitted, "\t\t{:retransmitted/%ju} "
691ade9ccfeSMarcel Moolenaar 	    "{N:/retransmitted}\n");
692ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_dupsyn, "\t\t{:duplicates/%ju} {N:/dupsyn}\n");
693ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_dropped, "\t\t{:dropped/%ju} {N:/dropped}\n");
694ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_completed, "\t\t{:completed/%ju} {N:/completed}\n");
695ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_bucketoverflow, "\t\t{:bucket-overflow/%ju} "
696ade9ccfeSMarcel Moolenaar 	    "{N:/bucket overflow}\n");
697ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_cacheoverflow, "\t\t{:cache-overflow/%ju} "
698ade9ccfeSMarcel Moolenaar 	    "{N:/cache overflow}\n");
699ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_reset, "\t\t{:reset/%ju} {N:/reset}\n");
700ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_stale, "\t\t{:stale/%ju} {N:/stale}\n");
701ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_aborted, "\t\t{:aborted/%ju} {N:/aborted}\n");
702ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_badack, "\t\t{:bad-ack/%ju} {N:/badack}\n");
703ade9ccfeSMarcel Moolenaar 	p1a(tcps_sc_unreach, "\t\t{:unreachable/%ju} {N:/unreach}\n");
704ade9ccfeSMarcel Moolenaar 	p(tcps_sc_zonefail, "\t\t{:zone-failures/%ju} {N:/zone failure%s}\n");
705ade9ccfeSMarcel Moolenaar 	p(tcps_sc_sendcookie, "\t{:sent-cookies/%ju} {N:/cookie%s sent}\n");
706ade9ccfeSMarcel Moolenaar 	p(tcps_sc_recvcookie, "\t{:receivd-cookies/%ju} "
707ade9ccfeSMarcel Moolenaar 	    "{N:/cookie%s received}\n");
708699ef999SRuslan Ermilov 
709ade9ccfeSMarcel Moolenaar 	xo_close_container("syncache");
710b6101dafSPaul Saab 
711ade9ccfeSMarcel Moolenaar 	xo_open_container("hostcache");
712a4993b25SBjoern A. Zeeb 
713ade9ccfeSMarcel Moolenaar 	p3(tcps_hc_added, "\t{:entries-added/%ju} "
714ade9ccfeSMarcel Moolenaar 	    "{N:/hostcache entr%s added}\n");
715ade9ccfeSMarcel Moolenaar 	p1a(tcps_hc_bucketoverflow, "\t\t{:buffer-overflows/%ju} "
716ade9ccfeSMarcel Moolenaar 	    "{N:/bucket overflow}\n");
717ade9ccfeSMarcel Moolenaar 
718ade9ccfeSMarcel Moolenaar 	xo_close_container("hostcache");
719ade9ccfeSMarcel Moolenaar 
720ade9ccfeSMarcel Moolenaar 	xo_open_container("sack");
721ade9ccfeSMarcel Moolenaar 
722ade9ccfeSMarcel Moolenaar 	p(tcps_sack_recovery_episode, "\t{:recovery-episodes/%ju} "
723ade9ccfeSMarcel Moolenaar 	    "{N:/SACK recovery episode%s}\n");
724ade9ccfeSMarcel Moolenaar  	p(tcps_sack_rexmits, "\t{:segment-retransmits/%ju} "
725ade9ccfeSMarcel Moolenaar 	    "{N:/segment rexmit%s in SACK recovery episodes}\n");
726ade9ccfeSMarcel Moolenaar  	p(tcps_sack_rexmit_bytes, "\t{:byte-retransmits/%ju} "
727ade9ccfeSMarcel Moolenaar 	    "{N:/byte rexmit%s in SACK recovery episodes}\n");
728ade9ccfeSMarcel Moolenaar  	p(tcps_sack_rcv_blocks, "\t{:received-blocks/%ju} "
729ade9ccfeSMarcel Moolenaar 	    "{N:/SACK option%s (SACK blocks) received}\n");
730ade9ccfeSMarcel Moolenaar 	p(tcps_sack_send_blocks, "\t{:sent-option-blocks/%ju} "
731ade9ccfeSMarcel Moolenaar 	    "{N:/SACK option%s (SACK blocks) sent}\n");
732ade9ccfeSMarcel Moolenaar 	p1a(tcps_sack_sboverflow, "\t{:scoreboard-overflows/%ju} "
733ade9ccfeSMarcel Moolenaar 	    "{N:/SACK scoreboard overflow}\n");
734ade9ccfeSMarcel Moolenaar 
735ade9ccfeSMarcel Moolenaar 	xo_close_container("sack");
736ade9ccfeSMarcel Moolenaar 	xo_open_container("ecn");
737ade9ccfeSMarcel Moolenaar 
738ade9ccfeSMarcel Moolenaar 	p(tcps_ecn_ce, "\t{:ce-packets/%ju} "
739ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s with ECN CE bit set}\n");
740ade9ccfeSMarcel Moolenaar 	p(tcps_ecn_ect0, "\t{:ect0-packets/%ju} "
741ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s with ECN ECT(0) bit set}\n");
742ade9ccfeSMarcel Moolenaar 	p(tcps_ecn_ect1, "\t{:ect1-packets/%ju} "
743ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s with ECN ECT(1) bit set}\n");
744ade9ccfeSMarcel Moolenaar 	p(tcps_ecn_shs, "\t{:handshakes/%ju} "
745ade9ccfeSMarcel Moolenaar 	    "{N:/successful ECN handshake%s}\n");
746ade9ccfeSMarcel Moolenaar 	p(tcps_ecn_rcwnd, "\t{:congestion-reductions/%ju} "
747ade9ccfeSMarcel Moolenaar 	    "{N:/time%s ECN reduced the congestion window}\n");
748fcf59617SAndrey V. Elsukov 
749fcf59617SAndrey V. Elsukov 	xo_close_container("ecn");
750fcf59617SAndrey V. Elsukov 	xo_open_container("tcp-signature");
751fcf59617SAndrey V. Elsukov 	p(tcps_sig_rcvgoodsig, "\t{:received-good-signature/%ju} "
752fcf59617SAndrey V. Elsukov 	    "{N:/packet%s with matching signature received}\n");
753fcf59617SAndrey V. Elsukov 	p(tcps_sig_rcvbadsig, "\t{:received-bad-signature/%ju} "
754fcf59617SAndrey V. Elsukov 	    "{N:/packet%s with bad signature received}\n");
755fcf59617SAndrey V. Elsukov 	p(tcps_sig_err_buildsig, "\t{:failed-make-signature/%ju} "
756fcf59617SAndrey V. Elsukov 	    "{N:/time%s failed to make signature due to no SA}\n");
757fcf59617SAndrey V. Elsukov 	p(tcps_sig_err_sigopt, "\t{:no-signature-expected/%ju} "
758fcf59617SAndrey V. Elsukov 	    "{N:/time%s unexpected signature received}\n");
759fcf59617SAndrey V. Elsukov 	p(tcps_sig_err_nosigopt, "\t{:no-signature-provided/%ju} "
760fcf59617SAndrey V. Elsukov 	    "{N:/time%s no signature provided by segment}\n");
76132a04bb8SSean Bruno 
76232a04bb8SSean Bruno 	xo_close_container("tcp-signature");
76332a04bb8SSean Bruno 	xo_open_container("pmtud");
76432a04bb8SSean Bruno 
76532a04bb8SSean Bruno 	p(tcps_pmtud_blackhole_activated, "\t{:pmtud-activated/%ju} "
76632a04bb8SSean Bruno 	    "{N:/Path MTU discovery black hole detection activation%s}\n");
76732a04bb8SSean Bruno 	p(tcps_pmtud_blackhole_activated_min_mss,
76832a04bb8SSean Bruno 	    "\t{:pmtud-activated-min-mss/%ju} "
76932a04bb8SSean Bruno 	    "{N:/Path MTU discovery black hole detection min MSS activation%s}\n");
77032a04bb8SSean Bruno 	p(tcps_pmtud_blackhole_failed, "\t{:pmtud-failed/%ju} "
77132a04bb8SSean Bruno 	    "{N:/Path MTU discovery black hole detection failure%s}\n");
7729b50d902SRodney W. Grimes  #undef p
77322694ebaSBruce Evans  #undef p1a
7749b50d902SRodney W. Grimes  #undef p2
77522694ebaSBruce Evans  #undef p2a
7769b50d902SRodney W. Grimes  #undef p3
77732a04bb8SSean Bruno 	xo_close_container("pmtud");
77832a04bb8SSean Bruno 
779dbfd8708SGleb Smirnoff 
780dbfd8708SGleb Smirnoff 	xo_open_container("TCP connection count by state");
781dbfd8708SGleb Smirnoff 	xo_emit("{T:/TCP connection count by state}:\n");
782dbfd8708SGleb Smirnoff 	for (int i = 0; i < TCP_NSTATES; i++) {
783dbfd8708SGleb Smirnoff 		/*
784dbfd8708SGleb Smirnoff 		 * XXXGL: is there a way in libxo to use %s
785dbfd8708SGleb Smirnoff 		 * in the "content string" of a format
786dbfd8708SGleb Smirnoff 		 * string? I failed to do that, that's why
787dbfd8708SGleb Smirnoff 		 * a temporary buffer is used to construct
788dbfd8708SGleb Smirnoff 		 * format string for xo_emit().
789dbfd8708SGleb Smirnoff 		 */
790dbfd8708SGleb Smirnoff 		char fmtbuf[80];
791dbfd8708SGleb Smirnoff 
792dbfd8708SGleb Smirnoff 		if (sflag > 1 && tcps_states[i] == 0)
793dbfd8708SGleb Smirnoff 			continue;
794dbfd8708SGleb Smirnoff 		snprintf(fmtbuf, sizeof(fmtbuf), "\t{:%s/%%ju} "
795dbfd8708SGleb Smirnoff                     "{Np:/connection ,connections} in %s state\n",
796dbfd8708SGleb Smirnoff 		    tcpstates[i], tcpstates[i]);
797dbfd8708SGleb Smirnoff 		xo_emit(fmtbuf, (uintmax_t )tcps_states[i]);
798dbfd8708SGleb Smirnoff 	}
799dbfd8708SGleb Smirnoff 	xo_close_container("TCP connection count by state");
800dbfd8708SGleb Smirnoff 
801ade9ccfeSMarcel Moolenaar 	xo_close_container("tcp");
8029b50d902SRodney W. Grimes }
8039b50d902SRodney W. Grimes 
8049b50d902SRodney W. Grimes /*
8059b50d902SRodney W. Grimes  * Dump UDP statistics structure.
8069b50d902SRodney W. Grimes  */
8079b50d902SRodney W. Grimes void
808feda1a43SJohn Baldwin udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
8099b50d902SRodney W. Grimes {
8109eddb899SMark Johnston 	struct udpstat udpstat;
811c80211e3SAndrey V. Elsukov 	uint64_t delivered;
8129b50d902SRodney W. Grimes 
813cfa1ca9dSYoshinobu Inoue #ifdef INET6
814cfa1ca9dSYoshinobu Inoue 	if (udp_done != 0)
815cfa1ca9dSYoshinobu Inoue 		return;
816cfa1ca9dSYoshinobu Inoue 	else
817cfa1ca9dSYoshinobu Inoue 		udp_done = 1;
818cfa1ca9dSYoshinobu Inoue #endif
819cfa1ca9dSYoshinobu Inoue 
8209eddb899SMark Johnston 	if (fetch_stats("net.inet.udp.stats", off, &udpstat,
8219eddb899SMark Johnston 	    sizeof(udpstat), kread_counters) != 0)
822feda1a43SJohn Baldwin 		return;
823feda1a43SJohn Baldwin 
824ade9ccfeSMarcel Moolenaar 	xo_open_container("udp");
825ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
826ade9ccfeSMarcel Moolenaar 
8279b50d902SRodney W. Grimes #define	p(f, m) if (udpstat.f || sflag <= 1) \
828ade9ccfeSMarcel Moolenaar 	xo_emit("\t" m, (uintmax_t)udpstat.f, plural(udpstat.f))
82922694ebaSBruce Evans #define	p1a(f, m) if (udpstat.f || sflag <= 1) \
830ade9ccfeSMarcel Moolenaar 	xo_emit("\t" m, (uintmax_t)udpstat.f)
831ade9ccfeSMarcel Moolenaar 
832ade9ccfeSMarcel Moolenaar 	p(udps_ipackets, "{:received-datagrams/%ju} "
833ade9ccfeSMarcel Moolenaar 	    "{N:/datagram%s received}\n");
834ade9ccfeSMarcel Moolenaar 	p1a(udps_hdrops, "{:dropped-incomplete-headers/%ju} "
835ade9ccfeSMarcel Moolenaar 	    "{N:/with incomplete header}\n");
836ade9ccfeSMarcel Moolenaar 	p1a(udps_badlen, "{:dropped-bad-data-length/%ju} "
837ade9ccfeSMarcel Moolenaar 	    "{N:/with bad data length field}\n");
838ade9ccfeSMarcel Moolenaar 	p1a(udps_badsum, "{:dropped-bad-checksum/%ju} "
839ade9ccfeSMarcel Moolenaar 	    "{N:/with bad checksum}\n");
840ade9ccfeSMarcel Moolenaar 	p1a(udps_nosum, "{:dropped-no-checksum/%ju} "
841ade9ccfeSMarcel Moolenaar 	    "{N:/with no checksum}\n");
842ade9ccfeSMarcel Moolenaar 	p1a(udps_noport, "{:dropped-no-socket/%ju} "
843ade9ccfeSMarcel Moolenaar 	    "{N:/dropped due to no socket}\n");
844ade9ccfeSMarcel Moolenaar 	p(udps_noportbcast, "{:dropped-broadcast-multicast/%ju} "
845ade9ccfeSMarcel Moolenaar 	    "{N:/broadcast\\/multicast datagram%s undelivered}\n");
846ade9ccfeSMarcel Moolenaar 	p1a(udps_fullsock, "{:dropped-full-socket-buffer/%ju} "
847ade9ccfeSMarcel Moolenaar 	    "{N:/dropped due to full socket buffers}\n");
848ade9ccfeSMarcel Moolenaar 	p1a(udpps_pcbhashmiss, "{:not-for-hashed-pcb/%ju} "
849ade9ccfeSMarcel Moolenaar 	    "{N:/not for hashed pcb}\n");
8509b50d902SRodney W. Grimes 	delivered = udpstat.udps_ipackets -
8519b50d902SRodney W. Grimes 		    udpstat.udps_hdrops -
8529b50d902SRodney W. Grimes 		    udpstat.udps_badlen -
8539b50d902SRodney W. Grimes 		    udpstat.udps_badsum -
8549b50d902SRodney W. Grimes 		    udpstat.udps_noport -
8559b50d902SRodney W. Grimes 		    udpstat.udps_noportbcast -
8569b50d902SRodney W. Grimes 		    udpstat.udps_fullsock;
8579b50d902SRodney W. Grimes 	if (delivered || sflag <= 1)
858ade9ccfeSMarcel Moolenaar 		xo_emit("\t{:delivered-packets/%ju} {N:/delivered}\n",
859ade9ccfeSMarcel Moolenaar 		    (uint64_t)delivered);
860ade9ccfeSMarcel Moolenaar 	p(udps_opackets, "{:output-packets/%ju} {N:/datagram%s output}\n");
86171498f30SBruce M Simpson 	/* the next statistic is cumulative in udps_noportbcast */
862ade9ccfeSMarcel Moolenaar 	p(udps_filtermcast, "{:multicast-source-filter-matches/%ju} "
863ade9ccfeSMarcel Moolenaar 	    "{N:/time%s multicast source filter matched}\n");
8649b50d902SRodney W. Grimes #undef p
86522694ebaSBruce Evans #undef p1a
866ade9ccfeSMarcel Moolenaar 	xo_close_container("udp");
8679b50d902SRodney W. Grimes }
8689b50d902SRodney W. Grimes 
8699b50d902SRodney W. Grimes /*
870a9771948SGleb Smirnoff  * Dump CARP statistics structure.
871a9771948SGleb Smirnoff  */
872a9771948SGleb Smirnoff void
873feda1a43SJohn Baldwin carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
874a9771948SGleb Smirnoff {
8759eddb899SMark Johnston 	struct carpstats carpstat;
876a9771948SGleb Smirnoff 
8779eddb899SMark Johnston 	if (fetch_stats("net.inet.carp.stats", off, &carpstat,
8789eddb899SMark Johnston 	    sizeof(carpstat), kread_counters) != 0)
879a9771948SGleb Smirnoff 		return;
880a9771948SGleb Smirnoff 
881ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
882ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
883a9771948SGleb Smirnoff 
884a9771948SGleb Smirnoff #define	p(f, m) if (carpstat.f || sflag <= 1) \
885ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t)carpstat.f, plural(carpstat.f))
886a9771948SGleb Smirnoff #define	p2(f, m) if (carpstat.f || sflag <= 1) \
887ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t)carpstat.f)
888a9771948SGleb Smirnoff 
889ade9ccfeSMarcel Moolenaar 	p(carps_ipackets, "\t{:received-inet-packets/%ju} "
890ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s received (IPv4)}\n");
891ade9ccfeSMarcel Moolenaar 	p(carps_ipackets6, "\t{:received-inet6-packets/%ju} "
892ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s received (IPv6)}\n");
893ade9ccfeSMarcel Moolenaar 	p(carps_badttl, "\t\t{:dropped-wrong-ttl/%ju} "
894ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s discarded for wrong TTL}\n");
895ade9ccfeSMarcel Moolenaar 	p(carps_hdrops, "\t\t{:dropped-short-header/%ju} "
896ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s shorter than header}\n");
897ade9ccfeSMarcel Moolenaar 	p(carps_badsum, "\t\t{:dropped-bad-checksum/%ju} "
898ade9ccfeSMarcel Moolenaar 	    "{N:/discarded for bad checksum%s}\n");
899ade9ccfeSMarcel Moolenaar 	p(carps_badver,	"\t\t{:dropped-bad-version/%ju} "
900ade9ccfeSMarcel Moolenaar 	    "{N:/discarded packet%s with a bad version}\n");
901ade9ccfeSMarcel Moolenaar 	p2(carps_badlen, "\t\t{:dropped-short-packet/%ju} "
902ade9ccfeSMarcel Moolenaar 	    "{N:/discarded because packet too short}\n");
903ade9ccfeSMarcel Moolenaar 	p2(carps_badauth, "\t\t{:dropped-bad-authentication/%ju} "
904ade9ccfeSMarcel Moolenaar 	    "{N:/discarded for bad authentication}\n");
905ade9ccfeSMarcel Moolenaar 	p2(carps_badvhid, "\t\t{:dropped-bad-vhid/%ju} "
906ade9ccfeSMarcel Moolenaar 	    "{N:/discarded for bad vhid}\n");
907ade9ccfeSMarcel Moolenaar 	p2(carps_badaddrs, "\t\t{:dropped-bad-address-list/%ju} "
908ade9ccfeSMarcel Moolenaar 	    "{N:/discarded because of a bad address list}\n");
909ade9ccfeSMarcel Moolenaar 	p(carps_opackets, "\t{:sent-inet-packets/%ju} "
910ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s sent (IPv4)}\n");
911ade9ccfeSMarcel Moolenaar 	p(carps_opackets6, "\t{:sent-inet6-packets/%ju} "
912ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s sent (IPv6)}\n");
913ade9ccfeSMarcel Moolenaar 	p2(carps_onomem, "\t\t{:send-failed-memory-error/%ju} "
914ade9ccfeSMarcel Moolenaar 	    "{N:/send failed due to mbuf memory error}\n");
915a9771948SGleb Smirnoff #if notyet
916ade9ccfeSMarcel Moolenaar 	p(carps_ostates, "\t\t{:send-state-updates/%s} "
917ade9ccfeSMarcel Moolenaar 	    "{N:/state update%s sent}\n");
918a9771948SGleb Smirnoff #endif
919a9771948SGleb Smirnoff #undef p
920a9771948SGleb Smirnoff #undef p2
921ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
922a9771948SGleb Smirnoff }
923a9771948SGleb Smirnoff 
924a9771948SGleb Smirnoff /*
9259b50d902SRodney W. Grimes  * Dump IP statistics structure.
9269b50d902SRodney W. Grimes  */
9279b50d902SRodney W. Grimes void
928feda1a43SJohn Baldwin ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
9299b50d902SRodney W. Grimes {
9309eddb899SMark Johnston 	struct ipstat ipstat;
9319b50d902SRodney W. Grimes 
9329eddb899SMark Johnston 	if (fetch_stats("net.inet.ip.stats", off, &ipstat,
9339eddb899SMark Johnston 	    sizeof(ipstat), kread_counters) != 0)
9349b50d902SRodney W. Grimes 		return;
9354f81ef50SGarrett Wollman 
936ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
937ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
9389b50d902SRodney W. Grimes 
9399b50d902SRodney W. Grimes #define	p(f, m) if (ipstat.f || sflag <= 1) \
940ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )ipstat.f, plural(ipstat.f))
94122694ebaSBruce Evans #define	p1a(f, m) if (ipstat.f || sflag <= 1) \
942ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t )ipstat.f)
9439b50d902SRodney W. Grimes 
944ade9ccfeSMarcel Moolenaar 	p(ips_total, "\t{:received-packets/%ju} "
945ade9ccfeSMarcel Moolenaar 	    "{N:/total packet%s received}\n");
946ade9ccfeSMarcel Moolenaar 	p(ips_badsum, "\t{:dropped-bad-checksum/%ju} "
947ade9ccfeSMarcel Moolenaar 	    "{N:/bad header checksum%s}\n");
948ade9ccfeSMarcel Moolenaar 	p1a(ips_toosmall, "\t{:dropped-below-minimum-size/%ju} "
949ade9ccfeSMarcel Moolenaar 	    "{N:/with size smaller than minimum}\n");
950ade9ccfeSMarcel Moolenaar 	p1a(ips_tooshort, "\t{:dropped-short-packets/%ju} "
951ade9ccfeSMarcel Moolenaar 	    "{N:/with data size < data length}\n");
952ade9ccfeSMarcel Moolenaar 	p1a(ips_toolong, "\t{:dropped-too-long/%ju} "
953ade9ccfeSMarcel Moolenaar 	    "{N:/with ip length > max ip packet size}\n");
954ade9ccfeSMarcel Moolenaar 	p1a(ips_badhlen, "\t{:dropped-short-header-length/%ju} "
955ade9ccfeSMarcel Moolenaar 	    "{N:/with header length < data size}\n");
956ade9ccfeSMarcel Moolenaar 	p1a(ips_badlen, "\t{:dropped-short-data/%ju} "
957ade9ccfeSMarcel Moolenaar 	    "{N:/with data length < header length}\n");
958ade9ccfeSMarcel Moolenaar 	p1a(ips_badoptions, "\t{:dropped-bad-options/%ju} "
959ade9ccfeSMarcel Moolenaar 	    "{N:/with bad options}\n");
960ade9ccfeSMarcel Moolenaar 	p1a(ips_badvers, "\t{:dropped-bad-version/%ju} "
961ade9ccfeSMarcel Moolenaar 	    "{N:/with incorrect version number}\n");
962ade9ccfeSMarcel Moolenaar 	p(ips_fragments, "\t{:received-fragments/%ju} "
963ade9ccfeSMarcel Moolenaar 	    "{N:/fragment%s received}\n");
964ade9ccfeSMarcel Moolenaar 	p(ips_fragdropped, "\t{:dropped-fragments/%ju} "
965ade9ccfeSMarcel Moolenaar 	    "{N:/fragment%s dropped (dup or out of space)}\n");
966ade9ccfeSMarcel Moolenaar 	p(ips_fragtimeout, "\t{:dropped-fragments-after-timeout/%ju} "
967ade9ccfeSMarcel Moolenaar 	    "{N:/fragment%s dropped after timeout}\n");
968ade9ccfeSMarcel Moolenaar 	p(ips_reassembled, "\t{:reassembled-packets/%ju} "
969ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s reassembled ok}\n");
970ade9ccfeSMarcel Moolenaar 	p(ips_delivered, "\t{:received-local-packets/%ju} "
971ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s for this host}\n");
972ade9ccfeSMarcel Moolenaar 	p(ips_noproto, "\t{:dropped-unknown-protocol/%ju} "
973ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s for unknown\\/unsupported protocol}\n");
974ade9ccfeSMarcel Moolenaar 	p(ips_forward, "\t{:forwarded-packets/%ju} "
975ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s forwarded}");
976ade9ccfeSMarcel Moolenaar 	p(ips_fastforward, " ({:fast-forwarded-packets/%ju} "
977ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s fast forwarded})");
978958d6f7fSPierre Beyssac 	if (ipstat.ips_forward || sflag <= 1)
979ade9ccfeSMarcel Moolenaar 		xo_emit("\n");
980ade9ccfeSMarcel Moolenaar 	p(ips_cantforward, "\t{:packets-cannot-forward/%ju} "
981ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s not forwardable}\n");
982ade9ccfeSMarcel Moolenaar 	p(ips_notmember, "\t{:received-unknown-multicast-group/%ju} "
983ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s received for unknown multicast group}\n");
984ade9ccfeSMarcel Moolenaar 	p(ips_redirectsent, "\t{:redirects-sent/%ju} "
985ade9ccfeSMarcel Moolenaar 	    "{N:/redirect%s sent}\n");
986ade9ccfeSMarcel Moolenaar 	p(ips_localout, "\t{:sent-packets/%ju} "
987ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s sent from this host}\n");
988ade9ccfeSMarcel Moolenaar 	p(ips_rawout, "\t{:send-packets-fabricated-header/%ju} "
989ade9ccfeSMarcel Moolenaar 	    "{N:/packet%s sent with fabricated ip header}\n");
990ade9ccfeSMarcel Moolenaar 	p(ips_odropped, "\t{:discard-no-mbufs/%ju} "
991ade9ccfeSMarcel Moolenaar 	    "{N:/output packet%s dropped due to no bufs, etc.}\n");
992ade9ccfeSMarcel Moolenaar 	p(ips_noroute, "\t{:discard-no-route/%ju} "
993ade9ccfeSMarcel Moolenaar 	    "{N:/output packet%s discarded due to no route}\n");
994ade9ccfeSMarcel Moolenaar 	p(ips_fragmented, "\t{:sent-fragments/%ju} "
995ade9ccfeSMarcel Moolenaar 	    "{N:/output datagram%s fragmented}\n");
996ade9ccfeSMarcel Moolenaar 	p(ips_ofragments, "\t{:fragments-created/%ju} "
997ade9ccfeSMarcel Moolenaar 	    "{N:/fragment%s created}\n");
998ade9ccfeSMarcel Moolenaar 	p(ips_cantfrag, "\t{:discard-cannot-fragment/%ju} "
999ade9ccfeSMarcel Moolenaar 	    "{N:/datagram%s that can't be fragmented}\n");
1000ade9ccfeSMarcel Moolenaar 	p(ips_nogif, "\t{:discard-tunnel-no-gif/%ju} "
1001ade9ccfeSMarcel Moolenaar 	    "{N:/tunneling packet%s that can't find gif}\n");
1002ade9ccfeSMarcel Moolenaar 	p(ips_badaddr, "\t{:discard-bad-address/%ju} "
1003ade9ccfeSMarcel Moolenaar 	    "{N:/datagram%s with bad address in header}\n");
10049b50d902SRodney W. Grimes #undef p
100522694ebaSBruce Evans #undef p1a
1006ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
10079b50d902SRodney W. Grimes }
10089b50d902SRodney W. Grimes 
100954fc657dSGeorge V. Neville-Neil /*
101054fc657dSGeorge V. Neville-Neil  * Dump ARP statistics structure.
101154fc657dSGeorge V. Neville-Neil  */
101254fc657dSGeorge V. Neville-Neil void
101354fc657dSGeorge V. Neville-Neil arp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
101454fc657dSGeorge V. Neville-Neil {
10159eddb899SMark Johnston 	struct arpstat arpstat;
101654fc657dSGeorge V. Neville-Neil 
10179eddb899SMark Johnston 	if (fetch_stats("net.link.ether.arp.stats", off, &arpstat,
10189eddb899SMark Johnston 	    sizeof(arpstat), kread_counters) != 0)
101954fc657dSGeorge V. Neville-Neil 		return;
102054fc657dSGeorge V. Neville-Neil 
1021ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
1022ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
102354fc657dSGeorge V. Neville-Neil 
102454fc657dSGeorge V. Neville-Neil #define	p(f, m) if (arpstat.f || sflag <= 1) \
1025ade9ccfeSMarcel Moolenaar 	xo_emit("\t" m, (uintmax_t)arpstat.f, plural(arpstat.f))
102654fc657dSGeorge V. Neville-Neil #define	p2(f, m) if (arpstat.f || sflag <= 1) \
1027ade9ccfeSMarcel Moolenaar 	xo_emit("\t" m, (uintmax_t)arpstat.f, pluralies(arpstat.f))
102854fc657dSGeorge V. Neville-Neil 
1029ade9ccfeSMarcel Moolenaar 	p(txrequests, "{:sent-requests/%ju} {N:/ARP request%s sent}\n");
1030ade9ccfeSMarcel Moolenaar 	p2(txreplies, "{:sent-replies/%ju} {N:/ARP repl%s sent}\n");
1031ade9ccfeSMarcel Moolenaar 	p(rxrequests, "{:received-requests/%ju} "
1032ade9ccfeSMarcel Moolenaar 	    "{N:/ARP request%s received}\n");
1033ade9ccfeSMarcel Moolenaar 	p2(rxreplies, "{:received-replies/%ju} "
1034ade9ccfeSMarcel Moolenaar 	    "{N:/ARP repl%s received}\n");
1035ade9ccfeSMarcel Moolenaar 	p(received, "{:received-packers/%ju} "
1036ade9ccfeSMarcel Moolenaar 	    "{N:/ARP packet%s received}\n");
1037ade9ccfeSMarcel Moolenaar 	p(dropped, "{:dropped-no-entry/%ju} "
1038ade9ccfeSMarcel Moolenaar 	    "{N:/total packet%s dropped due to no ARP entry}\n");
1039ade9ccfeSMarcel Moolenaar 	p(timeouts, "{:entries-timeout/%ju} "
1040ade9ccfeSMarcel Moolenaar 	    "{N:/ARP entry%s timed out}\n");
1041ade9ccfeSMarcel Moolenaar 	p(dupips, "{:dropped-duplicate-address/%ju} "
1042ade9ccfeSMarcel Moolenaar 	    "{N:/Duplicate IP%s seen}\n");
104354fc657dSGeorge V. Neville-Neil #undef p
104454fc657dSGeorge V. Neville-Neil #undef p2
1045ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
104654fc657dSGeorge V. Neville-Neil }
104754fc657dSGeorge V. Neville-Neil 
104854fc657dSGeorge V. Neville-Neil 
104954fc657dSGeorge V. Neville-Neil 
10504063583aSMaxim Konovalov static	const char *icmpnames[ICMP_MAXTYPE + 1] = {
10514063583aSMaxim Konovalov 	"echo reply",			/* RFC 792 */
10529b50d902SRodney W. Grimes 	"#1",
10539b50d902SRodney W. Grimes 	"#2",
10544063583aSMaxim Konovalov 	"destination unreachable",	/* RFC 792 */
10554063583aSMaxim Konovalov 	"source quench",		/* RFC 792 */
10564063583aSMaxim Konovalov 	"routing redirect",		/* RFC 792 */
10579b50d902SRodney W. Grimes 	"#6",
10589b50d902SRodney W. Grimes 	"#7",
10594063583aSMaxim Konovalov 	"echo",				/* RFC 792 */
10604063583aSMaxim Konovalov 	"router advertisement",		/* RFC 1256 */
10614063583aSMaxim Konovalov 	"router solicitation",		/* RFC 1256 */
10624063583aSMaxim Konovalov 	"time exceeded",		/* RFC 792 */
10634063583aSMaxim Konovalov 	"parameter problem",		/* RFC 792 */
10644063583aSMaxim Konovalov 	"time stamp",			/* RFC 792 */
10654063583aSMaxim Konovalov 	"time stamp reply",		/* RFC 792 */
10664063583aSMaxim Konovalov 	"information request",		/* RFC 792 */
10674063583aSMaxim Konovalov 	"information request reply",	/* RFC 792 */
10684063583aSMaxim Konovalov 	"address mask request",		/* RFC 950 */
10694063583aSMaxim Konovalov 	"address mask reply",		/* RFC 950 */
10704063583aSMaxim Konovalov 	"#19",
10714063583aSMaxim Konovalov 	"#20",
10724063583aSMaxim Konovalov 	"#21",
10734063583aSMaxim Konovalov 	"#22",
10744063583aSMaxim Konovalov 	"#23",
10754063583aSMaxim Konovalov 	"#24",
10764063583aSMaxim Konovalov 	"#25",
10774063583aSMaxim Konovalov 	"#26",
10784063583aSMaxim Konovalov 	"#27",
10794063583aSMaxim Konovalov 	"#28",
10804063583aSMaxim Konovalov 	"#29",
10814063583aSMaxim Konovalov 	"icmp traceroute",		/* RFC 1393 */
10824063583aSMaxim Konovalov 	"datagram conversion error",	/* RFC 1475 */
10834063583aSMaxim Konovalov 	"mobile host redirect",
10844063583aSMaxim Konovalov 	"IPv6 where-are-you",
10854063583aSMaxim Konovalov 	"IPv6 i-am-here",
10864063583aSMaxim Konovalov 	"mobile registration req",
10874063583aSMaxim Konovalov 	"mobile registration reply",
10884063583aSMaxim Konovalov 	"domain name request",		/* RFC 1788 */
10894063583aSMaxim Konovalov 	"domain name reply",		/* RFC 1788 */
10904063583aSMaxim Konovalov 	"icmp SKIP",
10914063583aSMaxim Konovalov 	"icmp photuris",		/* RFC 2521 */
10929b50d902SRodney W. Grimes };
10939b50d902SRodney W. Grimes 
10949b50d902SRodney W. Grimes /*
10959b50d902SRodney W. Grimes  * Dump ICMP statistics.
10969b50d902SRodney W. Grimes  */
10979b50d902SRodney W. Grimes void
1098feda1a43SJohn Baldwin icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
10999b50d902SRodney W. Grimes {
11009eddb899SMark Johnston 	struct icmpstat icmpstat;
11014e00c309SGarrett Wollman 	size_t len;
11029eddb899SMark Johnston 	int i, first;
11039b50d902SRodney W. Grimes 
11049eddb899SMark Johnston 	if (fetch_stats("net.inet.icmp.stats", off, &icmpstat,
11059eddb899SMark Johnston 	    sizeof(icmpstat), kread_counters) != 0)
1106c73d99b5SRuslan Ermilov 		return;
11074e00c309SGarrett Wollman 
1108ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
1109ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
11109b50d902SRodney W. Grimes 
11119b50d902SRodney W. Grimes #define	p(f, m) if (icmpstat.f || sflag <= 1) \
1112ade9ccfeSMarcel Moolenaar 	xo_emit(m, icmpstat.f, plural(icmpstat.f))
111322694ebaSBruce Evans #define	p1a(f, m) if (icmpstat.f || sflag <= 1) \
1114ade9ccfeSMarcel Moolenaar 	xo_emit(m, icmpstat.f)
1115bd714208SRuslan Ermilov #define	p2(f, m) if (icmpstat.f || sflag <= 1) \
1116ade9ccfeSMarcel Moolenaar 	xo_emit(m, icmpstat.f, plurales(icmpstat.f))
11179b50d902SRodney W. Grimes 
1118ade9ccfeSMarcel Moolenaar 	p(icps_error, "\t{:icmp-calls/%lu} "
1119ade9ccfeSMarcel Moolenaar 	    "{N:/call%s to icmp_error}\n");
1120ade9ccfeSMarcel Moolenaar 	p(icps_oldicmp, "\t{:errors-not-from-message/%lu} "
1121ade9ccfeSMarcel Moolenaar 	    "{N:/error%s not generated in response to an icmp message}\n");
1122ade9ccfeSMarcel Moolenaar 
1123ade9ccfeSMarcel Moolenaar 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) {
11249b50d902SRodney W. Grimes 		if (icmpstat.icps_outhist[i] != 0) {
11259b50d902SRodney W. Grimes 			if (first) {
1126ade9ccfeSMarcel Moolenaar 				xo_open_list("output-histogram");
1127ade9ccfeSMarcel Moolenaar 				xo_emit("\tOutput histogram:\n");
11289b50d902SRodney W. Grimes 				first = 0;
11299b50d902SRodney W. Grimes 			}
1130ade9ccfeSMarcel Moolenaar 			xo_open_instance("output-histogram");
11314063583aSMaxim Konovalov 			if (icmpnames[i] != NULL)
1132ade9ccfeSMarcel Moolenaar 				xo_emit("\t\t{k:name/%s}: {:count/%lu}\n",
1133ade9ccfeSMarcel Moolenaar 				    icmpnames[i], icmpstat.icps_outhist[i]);
11344063583aSMaxim Konovalov 			else
1135ade9ccfeSMarcel Moolenaar 				xo_emit("\t\tunknown ICMP #{k:name/%d}: "
1136ade9ccfeSMarcel Moolenaar 				    "{:count/%lu}\n",
1137ade9ccfeSMarcel Moolenaar 				    i, icmpstat.icps_outhist[i]);
1138ade9ccfeSMarcel Moolenaar 			xo_close_instance("output-histogram");
11399b50d902SRodney W. Grimes 		}
1140ade9ccfeSMarcel Moolenaar 	}
1141ade9ccfeSMarcel Moolenaar 	if (!first)
1142ade9ccfeSMarcel Moolenaar 		xo_close_list("output-histogram");
1143ade9ccfeSMarcel Moolenaar 
1144ade9ccfeSMarcel Moolenaar 	p(icps_badcode, "\t{:dropped-bad-code/%lu} "
1145ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with bad code fields}\n");
1146ade9ccfeSMarcel Moolenaar 	p(icps_tooshort, "\t{:dropped-too-short/%lu} "
1147ade9ccfeSMarcel Moolenaar 	    "{N:/message%s less than the minimum length}\n");
1148ade9ccfeSMarcel Moolenaar 	p(icps_checksum, "\t{:dropped-bad-checksum/%lu} "
1149ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with bad checksum}\n");
1150ade9ccfeSMarcel Moolenaar 	p(icps_badlen, "\t{:dropped-bad-length/%lu} "
1151ade9ccfeSMarcel Moolenaar 	    "{N:/message%s with bad length}\n");
1152ade9ccfeSMarcel Moolenaar 	p1a(icps_bmcastecho, "\t{:dropped-multicast-echo/%lu} "
1153ade9ccfeSMarcel Moolenaar 	    "{N:/multicast echo requests ignored}\n");
1154ade9ccfeSMarcel Moolenaar 	p1a(icps_bmcasttstamp, "\t{:dropped-multicast-timestamp/%lu} "
1155ade9ccfeSMarcel Moolenaar 	    "{N:/multicast timestamp requests ignored}\n");
1156ade9ccfeSMarcel Moolenaar 
1157ade9ccfeSMarcel Moolenaar 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) {
11589b50d902SRodney W. Grimes 		if (icmpstat.icps_inhist[i] != 0) {
11599b50d902SRodney W. Grimes 			if (first) {
1160ade9ccfeSMarcel Moolenaar 				xo_open_list("input-histogram");
1161ade9ccfeSMarcel Moolenaar 				xo_emit("\tInput histogram:\n");
11629b50d902SRodney W. Grimes 				first = 0;
11639b50d902SRodney W. Grimes 			}
1164ade9ccfeSMarcel Moolenaar 			xo_open_instance("input-histogram");
11654063583aSMaxim Konovalov 			if (icmpnames[i] != NULL)
1166ade9ccfeSMarcel Moolenaar 				xo_emit("\t\t{k:name/%s}: {:count/%lu}\n",
1167ade9ccfeSMarcel Moolenaar 					icmpnames[i],
11689b50d902SRodney W. Grimes 					icmpstat.icps_inhist[i]);
11694063583aSMaxim Konovalov 			else
1170ade9ccfeSMarcel Moolenaar 				xo_emit(
1171ade9ccfeSMarcel Moolenaar 			"\t\tunknown ICMP #{k:name/%d}: {:count/%lu}\n",
1172ade9ccfeSMarcel Moolenaar 					i, icmpstat.icps_inhist[i]);
1173ade9ccfeSMarcel Moolenaar 			xo_close_instance("input-histogram");
11749b50d902SRodney W. Grimes 		}
1175ade9ccfeSMarcel Moolenaar 	}
1176ade9ccfeSMarcel Moolenaar 	if (!first)
1177ade9ccfeSMarcel Moolenaar 		xo_close_list("input-histogram");
1178ade9ccfeSMarcel Moolenaar 
1179ade9ccfeSMarcel Moolenaar 	p(icps_reflect, "\t{:sent-packets/%lu} "
1180ade9ccfeSMarcel Moolenaar 	    "{N:/message response%s generated}\n");
1181ade9ccfeSMarcel Moolenaar 	p2(icps_badaddr, "\t{:discard-invalid-return-address/%lu} "
1182ade9ccfeSMarcel Moolenaar 	    "{N:/invalid return address%s}\n");
1183ade9ccfeSMarcel Moolenaar 	p(icps_noroute, "\t{:discard-no-route/%lu} "
1184ade9ccfeSMarcel Moolenaar 	    "{N:/no return route%s}\n");
11859b50d902SRodney W. Grimes #undef p
118622694ebaSBruce Evans #undef p1a
1187bd714208SRuslan Ermilov #undef p2
1188feda1a43SJohn Baldwin 	if (live) {
11894e00c309SGarrett Wollman 		len = sizeof i;
1190feda1a43SJohn Baldwin 		if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) <
1191feda1a43SJohn Baldwin 		    0)
11924e00c309SGarrett Wollman 			return;
1193ade9ccfeSMarcel Moolenaar 		xo_emit("\tICMP address mask responses are "
1194ade9ccfeSMarcel Moolenaar 		    "{q:icmp-address-responses/%sabled}\n", i ? "en" : "dis");
11959b50d902SRodney W. Grimes 	}
1196ade9ccfeSMarcel Moolenaar 
1197ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
1198feda1a43SJohn Baldwin }
11999b50d902SRodney W. Grimes 
1200d10910e6SBruce M Simpson /*
1201d10910e6SBruce M Simpson  * Dump IGMP statistics structure.
1202d10910e6SBruce M Simpson  */
1203d10910e6SBruce M Simpson void
1204d10910e6SBruce M Simpson igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1205d10910e6SBruce M Simpson {
12069eddb899SMark Johnston 	struct igmpstat igmpstat;
1207d10910e6SBruce M Simpson 
12089eddb899SMark Johnston 	if (fetch_stats("net.inet.igmp.stats", 0, &igmpstat,
12099eddb899SMark Johnston 	    sizeof(igmpstat), kread) != 0)
1210d10910e6SBruce M Simpson 		return;
1211d10910e6SBruce M Simpson 
1212d10910e6SBruce M Simpson 	if (igmpstat.igps_version != IGPS_VERSION_3) {
1213ade9ccfeSMarcel Moolenaar 		xo_warnx("%s: version mismatch (%d != %d)", __func__,
1214d10910e6SBruce M Simpson 		    igmpstat.igps_version, IGPS_VERSION_3);
1215d10910e6SBruce M Simpson 	}
1216d10910e6SBruce M Simpson 	if (igmpstat.igps_len != IGPS_VERSION3_LEN) {
1217ade9ccfeSMarcel Moolenaar 		xo_warnx("%s: size mismatch (%d != %d)", __func__,
1218d10910e6SBruce M Simpson 		    igmpstat.igps_len, IGPS_VERSION3_LEN);
1219d10910e6SBruce M Simpson 	}
1220d10910e6SBruce M Simpson 
1221ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
1222ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
1223d10910e6SBruce M Simpson 
1224d10910e6SBruce M Simpson #define	p64(f, m) if (igmpstat.f || sflag <= 1) \
1225ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t) igmpstat.f, plural(igmpstat.f))
1226d10910e6SBruce M Simpson #define	py64(f, m) if (igmpstat.f || sflag <= 1) \
1227ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t) igmpstat.f, pluralies(igmpstat.f))
1228ade9ccfeSMarcel Moolenaar 
1229ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_total, "\t{:received-messages/%ju} "
1230ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received}\n");
1231ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_tooshort, "\t{:dropped-too-short/%ju} "
1232ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with too few bytes}\n");
1233ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_badttl, "\t{:dropped-wrong-ttl/%ju} "
1234ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with wrong TTL}\n");
1235ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_badsum, "\t{:dropped-bad-checksum/%ju} "
1236ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with bad checksum}\n");
1237ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_v1v2_queries, "\t{:received-membership-queries/%ju} "
1238ade9ccfeSMarcel Moolenaar 	    "{N:/V1\\/V2 membership quer%s received}\n");
1239ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_v3_queries, "\t{:received-v3-membership-queries/%ju} "
1240ade9ccfeSMarcel Moolenaar 	    "{N:/V3 membership quer%s received}\n");
1241ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_badqueries, "\t{:dropped-membership-queries/%ju} "
1242ade9ccfeSMarcel Moolenaar 	    "{N:/membership quer%s received with invalid field(s)}\n");
1243ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_gen_queries, "\t{:received-general-queries/%ju} "
1244ade9ccfeSMarcel Moolenaar 	    "{N:/general quer%s received}\n");
1245ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_group_queries, "\t{:received-group-queries/%ju} "
1246ade9ccfeSMarcel Moolenaar 	    "{N:/group quer%s received}\n");
1247ade9ccfeSMarcel Moolenaar 	py64(igps_rcv_gsr_queries, "\t{:received-group-source-queries/%ju} "
1248ade9ccfeSMarcel Moolenaar 	    "{N:/group-source quer%s received}\n");
1249ade9ccfeSMarcel Moolenaar 	py64(igps_drop_gsr_queries, "\t{:dropped-group-source-queries/%ju} "
1250ade9ccfeSMarcel Moolenaar 	    "{N:/group-source quer%s dropped}\n");
1251ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_reports, "\t{:received-membership-requests/%ju} "
1252ade9ccfeSMarcel Moolenaar 	    "{N:/membership report%s received}\n");
1253ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_badreports, "\t{:dropped-membership-reports/%ju} "
1254ade9ccfeSMarcel Moolenaar 	    "{N:/membership report%s received with invalid field(s)}\n");
1255ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_ourreports, "\t"
1256ade9ccfeSMarcel Moolenaar 	    "{:received-membership-reports-matching/%ju} "
1257ade9ccfeSMarcel Moolenaar 	    "{N:/membership report%s received for groups to which we belong}"
1258ade9ccfeSMarcel Moolenaar 	    "\n");
1259ade9ccfeSMarcel Moolenaar 	p64(igps_rcv_nora, "\t{:received-v3-reports-no-router-alert/%ju} "
1260ade9ccfeSMarcel Moolenaar 	    "{N:/V3 report%s received without Router Alert}\n");
1261ade9ccfeSMarcel Moolenaar 	p64(igps_snd_reports, "\t{:sent-membership-reports/%ju} "
1262ade9ccfeSMarcel Moolenaar 	    "{N:/membership report%s sent}\n");
1263d10910e6SBruce M Simpson #undef p64
1264d10910e6SBruce M Simpson #undef py64
1265ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
1266d10910e6SBruce M Simpson }
12679b50d902SRodney W. Grimes 
12689b50d902SRodney W. Grimes /*
1269c7b9b5bbSJeffrey Hsu  * Dump PIM statistics structure.
1270c7b9b5bbSJeffrey Hsu  */
1271c7b9b5bbSJeffrey Hsu void
1272feda1a43SJohn Baldwin pim_stats(u_long off __unused, const char *name, int af1 __unused,
1273feda1a43SJohn Baldwin     int proto __unused)
1274c7b9b5bbSJeffrey Hsu {
12759eddb899SMark Johnston 	struct pimstat pimstat;
1276c7b9b5bbSJeffrey Hsu 
12779eddb899SMark Johnston 	if (fetch_stats("net.inet.pim.stats", off, &pimstat,
12789eddb899SMark Johnston 	    sizeof(pimstat), kread_counters) != 0)
1279c7b9b5bbSJeffrey Hsu 		return;
1280c7b9b5bbSJeffrey Hsu 
1281ade9ccfeSMarcel Moolenaar 	xo_open_container(name);
1282ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
1283c7b9b5bbSJeffrey Hsu 
1284c7b9b5bbSJeffrey Hsu #define	p(f, m) if (pimstat.f || sflag <= 1) \
1285ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t)pimstat.f, plural(pimstat.f))
1286c7b9b5bbSJeffrey Hsu #define	py(f, m) if (pimstat.f || sflag <= 1) \
1287ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
1288ade9ccfeSMarcel Moolenaar 
1289ade9ccfeSMarcel Moolenaar 	p(pims_rcv_total_msgs, "\t{:received-messages/%ju} "
1290ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received}\n");
1291ade9ccfeSMarcel Moolenaar 	p(pims_rcv_total_bytes, "\t{:received-bytes/%ju} "
1292ade9ccfeSMarcel Moolenaar 	    "{N:/byte%s received}\n");
1293ade9ccfeSMarcel Moolenaar 	p(pims_rcv_tooshort, "\t{:dropped-too-short/%ju} "
1294ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with too few bytes}\n");
1295ade9ccfeSMarcel Moolenaar 	p(pims_rcv_badsum, "\t{:dropped-bad-checksum/%ju} "
1296ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with bad checksum}\n");
1297ade9ccfeSMarcel Moolenaar 	p(pims_rcv_badversion, "\t{:dropped-bad-version/%ju} "
1298ade9ccfeSMarcel Moolenaar 	    "{N:/message%s received with bad version}\n");
1299ade9ccfeSMarcel Moolenaar 	p(pims_rcv_registers_msgs, "\t{:received-data-register-messages/%ju} "
1300ade9ccfeSMarcel Moolenaar 	    "{N:/data register message%s received}\n");
1301ade9ccfeSMarcel Moolenaar 	p(pims_rcv_registers_bytes, "\t{:received-data-register-bytes/%ju} "
1302ade9ccfeSMarcel Moolenaar 	    "{N:/data register byte%s received}\n");
1303ade9ccfeSMarcel Moolenaar 	p(pims_rcv_registers_wrongiif, "\t"
1304ade9ccfeSMarcel Moolenaar 	    "{:received-data-register-wrong-interface/%ju} "
1305ade9ccfeSMarcel Moolenaar 	    "{N:/data register message%s received on wrong iif}\n");
1306ade9ccfeSMarcel Moolenaar 	p(pims_rcv_badregisters, "\t{:received-bad-registers/%ju} "
1307ade9ccfeSMarcel Moolenaar 	    "{N:/bad register%s received}\n");
1308ade9ccfeSMarcel Moolenaar 	p(pims_snd_registers_msgs, "\t{:sent-data-register-messages/%ju} "
1309ade9ccfeSMarcel Moolenaar 	    "{N:/data register message%s sent}\n");
1310ade9ccfeSMarcel Moolenaar 	p(pims_snd_registers_bytes, "\t{:sent-data-register-bytes/%ju} "
1311ade9ccfeSMarcel Moolenaar 	    "{N:/data register byte%s sent}\n");
1312c7b9b5bbSJeffrey Hsu #undef p
1313c7b9b5bbSJeffrey Hsu #undef py
1314ade9ccfeSMarcel Moolenaar 	xo_close_container(name);
1315c7b9b5bbSJeffrey Hsu }
1316c7b9b5bbSJeffrey Hsu 
1317c7b9b5bbSJeffrey Hsu /*
13189b50d902SRodney W. Grimes  * Pretty print an Internet address (net address + port).
13199b50d902SRodney W. Grimes  */
13209b50d902SRodney W. Grimes void
1321ade9ccfeSMarcel Moolenaar inetprint(const char *container, struct in_addr *in, int port,
1322046aad39SHajimu UMEMOTO     const char *proto, int num_port, const int af1)
13239b50d902SRodney W. Grimes {
13249b50d902SRodney W. Grimes 	struct servent *sp = 0;
13259b50d902SRodney W. Grimes 	char line[80], *cp;
1326cfa1ca9dSYoshinobu Inoue 	int width;
1327f193c8ceSXin LI 	size_t alen, plen;
13289b50d902SRodney W. Grimes 
1329ade9ccfeSMarcel Moolenaar 	if (container)
1330ade9ccfeSMarcel Moolenaar 		xo_open_container(container);
1331ade9ccfeSMarcel Moolenaar 
1332080b7f49SDag-Erling Smørgrav 	if (Wflag)
1333f193c8ceSXin LI 	    snprintf(line, sizeof(line), "%s.", inetname(in));
1334080b7f49SDag-Erling Smørgrav 	else
1335f193c8ceSXin LI 	    snprintf(line, sizeof(line), "%.*s.",
1336f193c8ceSXin LI 		(Aflag && !num_port) ? 12 : 16, inetname(in));
1337f193c8ceSXin LI 	alen = strlen(line);
1338f193c8ceSXin LI 	cp = line + alen;
1339a01e3379SDavid Malone 	if (!num_port && port)
13409b50d902SRodney W. Grimes 		sp = getservbyport((int)port, proto);
13419b50d902SRodney W. Grimes 	if (sp || port == 0)
1342f193c8ceSXin LI 		snprintf(cp, sizeof(line) - alen,
1343f193c8ceSXin LI 		    "%.15s ", sp ? sp->s_name : "*");
13449b50d902SRodney W. Grimes 	else
1345f193c8ceSXin LI 		snprintf(cp, sizeof(line) - alen,
1346f193c8ceSXin LI 		    "%d ", ntohs((u_short)port));
1347046aad39SHajimu UMEMOTO 	width = (Aflag && !Wflag) ? 18 :
1348046aad39SHajimu UMEMOTO 		((!Wflag || af1 == AF_INET) ? 22 : 45);
1349080b7f49SDag-Erling Smørgrav 	if (Wflag)
1350ade9ccfeSMarcel Moolenaar 		xo_emit("{d:target/%-*s} ", width, line);
1351080b7f49SDag-Erling Smørgrav 	else
1352ade9ccfeSMarcel Moolenaar 		xo_emit("{d:target/%-*.*s} ", width, width, line);
1353ade9ccfeSMarcel Moolenaar 
1354f193c8ceSXin LI 	plen = strlen(cp) - 1;
1355f193c8ceSXin LI 	alen--;
1356ade9ccfeSMarcel Moolenaar 	xo_emit("{e:address/%*.*s}{e:port/%*.*s}", alen, alen, line, plen,
1357ade9ccfeSMarcel Moolenaar 	    plen, cp);
1358ade9ccfeSMarcel Moolenaar 
1359ade9ccfeSMarcel Moolenaar 	if (container)
1360ade9ccfeSMarcel Moolenaar 		xo_close_container(container);
13619b50d902SRodney W. Grimes }
13629b50d902SRodney W. Grimes 
13639b50d902SRodney W. Grimes /*
13649b50d902SRodney W. Grimes  * Construct an Internet address representation.
136565ea0024SAssar Westerlund  * If numeric_addr has been supplied, give
13669b50d902SRodney W. Grimes  * numeric value, otherwise try for symbolic name.
13679b50d902SRodney W. Grimes  */
13689b50d902SRodney W. Grimes char *
13695e051718SAssar Westerlund inetname(struct in_addr *inp)
13709b50d902SRodney W. Grimes {
1371a01e3379SDavid Malone 	char *cp;
1372d121b556SBrian Somers 	static char line[MAXHOSTNAMELEN];
13739b50d902SRodney W. Grimes 	struct hostent *hp;
13749b50d902SRodney W. Grimes 	struct netent *np;
13759b50d902SRodney W. Grimes 
13769b50d902SRodney W. Grimes 	cp = 0;
137765ea0024SAssar Westerlund 	if (!numeric_addr && inp->s_addr != INADDR_ANY) {
13789b50d902SRodney W. Grimes 		int net = inet_netof(*inp);
13799b50d902SRodney W. Grimes 		int lna = inet_lnaof(*inp);
13809b50d902SRodney W. Grimes 
13819b50d902SRodney W. Grimes 		if (lna == INADDR_ANY) {
13829b50d902SRodney W. Grimes 			np = getnetbyaddr(net, AF_INET);
13839b50d902SRodney W. Grimes 			if (np)
13849b50d902SRodney W. Grimes 				cp = np->n_name;
13859b50d902SRodney W. Grimes 		}
1386ef1cb629SMarcelo Araujo 		if (cp == NULL) {
13879b50d902SRodney W. Grimes 			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
13889b50d902SRodney W. Grimes 			if (hp) {
13899b50d902SRodney W. Grimes 				cp = hp->h_name;
1390d121b556SBrian Somers 				trimdomain(cp, strlen(cp));
13919b50d902SRodney W. Grimes 			}
13929b50d902SRodney W. Grimes 		}
13939b50d902SRodney W. Grimes 	}
13949b50d902SRodney W. Grimes 	if (inp->s_addr == INADDR_ANY)
13959b50d902SRodney W. Grimes 		strcpy(line, "*");
13969a1f6729SWarner Losh 	else if (cp) {
13971c109628SXin LI 		strlcpy(line, cp, sizeof(line));
13989a1f6729SWarner Losh 	} else {
13999b50d902SRodney W. Grimes 		inp->s_addr = ntohl(inp->s_addr);
140022694ebaSBruce Evans #define	C(x)	((u_int)((x) & 0xff))
1401f193c8ceSXin LI 		snprintf(line, sizeof(line), "%u.%u.%u.%u",
1402f193c8ceSXin LI 		    C(inp->s_addr >> 24), C(inp->s_addr >> 16),
1403f193c8ceSXin LI 		    C(inp->s_addr >> 8), C(inp->s_addr));
14049b50d902SRodney W. Grimes 	}
14059b50d902SRodney W. Grimes 	return (line);
14069b50d902SRodney W. Grimes }
1407