xref: /freebsd/usr.bin/netstat/inet.c (revision 0d66206fff44f864ea8a4b220c3a53b4caa959a0)
1 /*-
2  * Copyright (c) 1983, 1988, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #if 0
31 #ifndef lint
32 static char sccsid[] = "@(#)inet.c	8.5 (Berkeley) 5/24/95";
33 #endif /* not lint */
34 #endif
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/param.h>
40 #include <sys/queue.h>
41 #include <sys/domain.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #define	_WANT_SOCKET
45 #include <sys/socketvar.h>
46 #include <sys/sysctl.h>
47 
48 #include <net/route.h>
49 #include <net/if_arp.h>
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53 #include <netinet/ip_carp.h>
54 #ifdef INET6
55 #include <netinet/ip6.h>
56 #endif /* INET6 */
57 #include <netinet/in_pcb.h>
58 #include <netinet/ip_icmp.h>
59 #include <netinet/icmp_var.h>
60 #include <netinet/igmp_var.h>
61 #include <netinet/ip_divert.h>
62 #include <netinet/ip_var.h>
63 #include <netinet/pim_var.h>
64 #include <netinet/tcp.h>
65 #include <netinet/tcpip.h>
66 #include <netinet/tcp_seq.h>
67 #define	TCPSTATES
68 #include <netinet/tcp_fsm.h>
69 #include <netinet/tcp_timer.h>
70 #include <netinet/tcp_var.h>
71 #include <netinet/udp.h>
72 #include <netinet/udp_var.h>
73 
74 #include <arpa/inet.h>
75 #include <err.h>
76 #include <errno.h>
77 #include <libutil.h>
78 #include <netdb.h>
79 #include <stdint.h>
80 #include <stdio.h>
81 #include <stdlib.h>
82 #include <stdbool.h>
83 #include <string.h>
84 #include <unistd.h>
85 #include <libxo/xo.h>
86 #include "netstat.h"
87 #include "nl_defs.h"
88 
89 #define max(a, b) (((a) > (b)) ? (a) : (b))
90 
91 #ifdef INET
92 static void inetprint(const char *, struct in_addr *, int, const char *, int,
93     const int);
94 #endif
95 #ifdef INET6
96 static int udp_done, tcp_done, sdp_done;
97 #endif /* INET6 */
98 
99 static int
100 pcblist_sysctl(int proto, const char *name, char **bufp)
101 {
102 	const char *mibvar;
103 	char *buf;
104 	size_t len;
105 
106 	switch (proto) {
107 	case IPPROTO_TCP:
108 		mibvar = "net.inet.tcp.pcblist";
109 		break;
110 	case IPPROTO_UDP:
111 		mibvar = "net.inet.udp.pcblist";
112 		break;
113 	default:
114 		mibvar = "net.inet.raw.pcblist";
115 		break;
116 	}
117 	if (strncmp(name, "sdp", 3) == 0)
118 		mibvar = "net.inet.sdp.pcblist";
119 	else if (strncmp(name, "divert", 6) == 0)
120 		mibvar = "net.inet.divert.pcblist";
121 	len = 0;
122 	if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
123 		if (errno != ENOENT)
124 			xo_warn("sysctl: %s", mibvar);
125 		return (0);
126 	}
127 	if ((buf = malloc(len)) == NULL) {
128 		xo_warnx("malloc %lu bytes", (u_long)len);
129 		return (0);
130 	}
131 	if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
132 		xo_warn("sysctl: %s", mibvar);
133 		free(buf);
134 		return (0);
135 	}
136 	*bufp = buf;
137 	return (1);
138 }
139 
140 /*
141  * Copied directly from uipc_socket2.c.  We leave out some fields that are in
142  * nested structures that aren't used to avoid extra work.
143  */
144 static void
145 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
146 {
147 	xsb->sb_cc = sb->sb_ccc;
148 	xsb->sb_hiwat = sb->sb_hiwat;
149 	xsb->sb_mbcnt = sb->sb_mbcnt;
150 	xsb->sb_mbmax = sb->sb_mbmax;
151 	xsb->sb_lowat = sb->sb_lowat;
152 	xsb->sb_flags = sb->sb_flags;
153 	xsb->sb_timeo = sb->sb_timeo;
154 }
155 
156 int
157 sotoxsocket(struct socket *so, struct xsocket *xso)
158 {
159 	struct protosw proto;
160 	struct domain domain;
161 
162 	bzero(xso, sizeof *xso);
163 	xso->xso_len = sizeof *xso;
164 	xso->xso_so = (uintptr_t)so;
165 	xso->so_type = so->so_type;
166 	xso->so_options = so->so_options;
167 	xso->so_linger = so->so_linger;
168 	xso->so_state = so->so_state;
169 	xso->so_pcb = (uintptr_t)so->so_pcb;
170 	if (kread((uintptr_t)so->so_proto, &proto, sizeof(proto)) != 0)
171 		return (-1);
172 	xso->xso_protocol = proto.pr_protocol;
173 	if (kread((uintptr_t)proto.pr_domain, &domain, sizeof(domain)) != 0)
174 		return (-1);
175 	xso->xso_family = domain.dom_family;
176 	xso->so_timeo = so->so_timeo;
177 	xso->so_error = so->so_error;
178 	if ((so->so_options & SO_ACCEPTCONN) != 0) {
179 		xso->so_qlen = so->sol_qlen;
180 		xso->so_incqlen = so->sol_incqlen;
181 		xso->so_qlimit = so->sol_qlimit;
182 	} else {
183 		sbtoxsockbuf(&so->so_snd, &xso->so_snd);
184 		sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
185 		xso->so_oobmark = so->so_oobmark;
186 	}
187 	return (0);
188 }
189 
190 /*
191  * Print a summary of connections related to an Internet
192  * protocol.  For TCP, also give state of connection.
193  * Listening processes (aflag) are suppressed unless the
194  * -a (all) flag is specified.
195  */
196 void
197 protopr(u_long off, const char *name, int af1, int proto)
198 {
199 	static int first = 1;
200 	int istcp;
201 	char *buf;
202 	const char *vchar;
203 	struct xtcpcb *tp;
204 	struct xinpcb *inp;
205 	struct xinpgen *xig, *oxig;
206 	struct xsocket *so;
207 	int fnamelen, cnamelen;
208 
209 	istcp = 0;
210 	switch (proto) {
211 	case IPPROTO_TCP:
212 #ifdef INET6
213 		if (strncmp(name, "sdp", 3) != 0) {
214 			if (tcp_done != 0)
215 				return;
216 			else
217 				tcp_done = 1;
218 		} else {
219 			if (sdp_done != 0)
220 				return;
221 			else
222 				sdp_done = 1;
223 		}
224 #endif
225 		istcp = 1;
226 		break;
227 	case IPPROTO_UDP:
228 #ifdef INET6
229 		if (udp_done != 0)
230 			return;
231 		else
232 			udp_done = 1;
233 #endif
234 		break;
235 	}
236 
237 	if (!pcblist_sysctl(proto, name, &buf))
238 		return;
239 
240 	if (cflag || Cflag) {
241 		fnamelen = strlen("Stack");
242 		cnamelen = strlen("CC");
243 		oxig = xig = (struct xinpgen *)buf;
244 		for (xig = (struct xinpgen*)((char *)xig + xig->xig_len);
245 		    xig->xig_len > sizeof(struct xinpgen);
246 		    xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
247 			if (istcp) {
248 				tp = (struct xtcpcb *)xig;
249 				inp = &tp->xt_inp;
250 			} else {
251 				continue;
252 			}
253 			if (so->xso_protocol != proto)
254 				continue;
255 			if (inp->inp_gencnt > oxig->xig_gen)
256 				continue;
257 			fnamelen = max(fnamelen, (int)strlen(tp->xt_stack));
258 			cnamelen = max(cnamelen, (int)strlen(tp->xt_cc));
259 		}
260 	}
261 
262 	oxig = xig = (struct xinpgen *)buf;
263 	for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
264 	    xig->xig_len > sizeof(struct xinpgen);
265 	    xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
266 		if (istcp) {
267 			tp = (struct xtcpcb *)xig;
268 			inp = &tp->xt_inp;
269 		} else {
270 			inp = (struct xinpcb *)xig;
271 		}
272 		so = &inp->xi_socket;
273 
274 		/* Ignore sockets for protocols other than the desired one. */
275 		if (proto != 0 && so->xso_protocol != proto)
276 			continue;
277 
278 		/* Ignore PCBs which were freed during copyout. */
279 		if (inp->inp_gencnt > oxig->xig_gen)
280 			continue;
281 
282 		if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
283 #ifdef INET6
284 		    || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
285 #endif /* INET6 */
286 		    || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
287 #ifdef INET6
288 					  && (inp->inp_vflag & INP_IPV6) == 0
289 #endif /* INET6 */
290 			))
291 		    )
292 			continue;
293 		if (!aflag &&
294 		    (
295 		     (istcp && tp->t_state == TCPS_LISTEN)
296 		     || (af1 == AF_INET &&
297 		      inp->inp_laddr.s_addr == INADDR_ANY)
298 #ifdef INET6
299 		     || (af1 == AF_INET6 &&
300 			 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
301 #endif /* INET6 */
302 		     || (af1 == AF_UNSPEC &&
303 			 (((inp->inp_vflag & INP_IPV4) != 0 &&
304 			   inp->inp_laddr.s_addr == INADDR_ANY)
305 #ifdef INET6
306 			  || ((inp->inp_vflag & INP_IPV6) != 0 &&
307 			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
308 #endif
309 			  ))
310 		     ))
311 			continue;
312 
313 		if (first) {
314 			if (!Lflag) {
315 				xo_emit("Active Internet connections");
316 				if (aflag)
317 					xo_emit(" (including servers)");
318 			} else
319 				xo_emit(
320 	"Current listen queue sizes (qlen/incqlen/maxqlen)");
321 			xo_emit("\n");
322 			if (Aflag)
323 				xo_emit("{T:/%-*s} ", 2 * (int)sizeof(void *),
324 				    "Tcpcb");
325 			if (Lflag)
326 				xo_emit((Aflag && !Wflag) ?
327 				    "{T:/%-5.5s} {T:/%-32.32s} {T:/%-18.18s}" :
328 				    ((!Wflag || af1 == AF_INET) ?
329 				    "{T:/%-5.5s} {T:/%-32.32s} {T:/%-22.22s}" :
330 				    "{T:/%-5.5s} {T:/%-32.32s} {T:/%-45.45s}"),
331 				    "Proto", "Listen", "Local Address");
332 			else if (Tflag)
333 				xo_emit((Aflag && !Wflag) ?
334     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%s}" :
335 				    ((!Wflag || af1 == AF_INET) ?
336     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%s}" :
337     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%s}"),
338 				    "Proto", "Rexmit", "OOORcv", "0-win",
339 				    "Local Address", "Foreign Address");
340 			else {
341 				xo_emit((Aflag && !Wflag) ?
342     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%-18.18s}" :
343 				    ((!Wflag || af1 == AF_INET) ?
344     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%-22.22s}" :
345     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%-45.45s}"),
346 				    "Proto", "Recv-Q", "Send-Q",
347 				    "Local Address", "Foreign Address");
348 				if (!xflag && !Rflag)
349 					xo_emit(" {T:/%-11.11s}", "(state)");
350 			}
351 			if (xflag) {
352 				xo_emit("{T:/%-6.6s} {T:/%-6.6s} "
353 				    "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
354 				    "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s}",
355 				    "R-HIWA", "S-HIWA", "R-LOWA", "S-LOWA",
356 				    "R-BCNT", "S-BCNT", "R-BMAX", "S-BMAX");
357 				xo_emit(" {T:/%7.7s} {T:/%7.7s} {T:/%7.7s} "
358 				    "{T:/%7.7s} {T:/%7.7s} {T:/%7.7s}",
359 				    "rexmt", "persist", "keep", "2msl",
360 				    "delack", "rcvtime");
361 			} else if (Rflag) {
362 				xo_emit("  {T:/%8.8s} {T:/%5.5s}",
363 				    "flowid", "ftype");
364 			}
365 			if (cflag) {
366 				xo_emit(" {T:/%-*.*s}",
367 					fnamelen, fnamelen, "Stack");
368 			}
369 			if (Cflag)
370 				xo_emit(" {T:/%-*.*s} {T:/%10.10s}"
371 					" {T:/%10.10s} {T:/%5.5s}"
372 					" {T:/%3.3s}", cnamelen,
373 					cnamelen, "CC",
374 					"cwin",
375 					"ssthresh",
376 					"MSS",
377 					"ECN");
378 			if (Pflag)
379 				xo_emit(" {T:/%s}", "Log ID");
380 			xo_emit("\n");
381 			first = 0;
382 		}
383 		if (Lflag && so->so_qlimit == 0)
384 			continue;
385 		xo_open_instance("socket");
386 		if (Aflag)
387 			xo_emit("{q:address/%*lx} ", 2 * (int)sizeof(void *),
388 			    (u_long)so->so_pcb);
389 #ifdef INET6
390 		if ((inp->inp_vflag & INP_IPV6) != 0)
391 			vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
392 			    "46" : "6";
393 		else
394 #endif
395 		vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
396 		    "4" : "";
397 		if (istcp && (tp->t_flags & TF_TOE) != 0)
398 			xo_emit("{:protocol/%-3.3s%-2.2s/%s%s} ", "toe", vchar);
399 		else
400 			xo_emit("{:protocol/%-3.3s%-2.2s/%s%s} ", name, vchar);
401 		if (Lflag) {
402 			char buf1[33];
403 
404 			snprintf(buf1, sizeof buf1, "%u/%u/%u", so->so_qlen,
405 			    so->so_incqlen, so->so_qlimit);
406 			xo_emit("{:listen-queue-sizes/%-32.32s} ", buf1);
407 		} else if (Tflag) {
408 			if (istcp)
409 				xo_emit("{:sent-retransmit-packets/%6u} "
410 				    "{:received-out-of-order-packets/%6u} "
411 				    "{:sent-zero-window/%6u} ",
412 				    tp->t_sndrexmitpack, tp->t_rcvoopack,
413 				    tp->t_sndzerowin);
414 			else
415 				xo_emit("{P:/%21s}", "");
416 		} else {
417 			xo_emit("{:receive-bytes-waiting/%6u} "
418 			    "{:send-bytes-waiting/%6u} ",
419 			    so->so_rcv.sb_cc, so->so_snd.sb_cc);
420 		}
421 		if (numeric_port) {
422 #ifdef INET
423 			if (inp->inp_vflag & INP_IPV4) {
424 				inetprint("local", &inp->inp_laddr,
425 				    (int)inp->inp_lport, name, 1, af1);
426 				if (!Lflag)
427 					inetprint("remote", &inp->inp_faddr,
428 					    (int)inp->inp_fport, name, 1, af1);
429 			}
430 #endif
431 #if defined(INET) && defined(INET6)
432 			else
433 #endif
434 #ifdef INET6
435 			if (inp->inp_vflag & INP_IPV6) {
436 				inet6print("local", &inp->in6p_laddr,
437 				    (int)inp->inp_lport, name, 1);
438 				if (!Lflag)
439 					inet6print("remote", &inp->in6p_faddr,
440 					    (int)inp->inp_fport, name, 1);
441 			} /* else nothing printed now */
442 #endif /* INET6 */
443 		} else if (inp->inp_flags & INP_ANONPORT) {
444 #ifdef INET
445 			if (inp->inp_vflag & INP_IPV4) {
446 				inetprint("local", &inp->inp_laddr,
447 				    (int)inp->inp_lport, name, 1, af1);
448 				if (!Lflag)
449 					inetprint("remote", &inp->inp_faddr,
450 					    (int)inp->inp_fport, name, 0, af1);
451 			}
452 #endif
453 #if defined(INET) && defined(INET6)
454 			else
455 #endif
456 #ifdef INET6
457 			if (inp->inp_vflag & INP_IPV6) {
458 				inet6print("local", &inp->in6p_laddr,
459 				    (int)inp->inp_lport, name, 1);
460 				if (!Lflag)
461 					inet6print("remote", &inp->in6p_faddr,
462 					    (int)inp->inp_fport, name, 0);
463 			} /* else nothing printed now */
464 #endif /* INET6 */
465 		} else {
466 #ifdef INET
467 			if (inp->inp_vflag & INP_IPV4) {
468 				inetprint("local", &inp->inp_laddr,
469 				    (int)inp->inp_lport, name, 0, af1);
470 				if (!Lflag)
471 					inetprint("remote", &inp->inp_faddr,
472 					    (int)inp->inp_fport, name,
473 					    inp->inp_lport != inp->inp_fport,
474 					    af1);
475 			}
476 #endif
477 #if defined(INET) && defined(INET6)
478 			else
479 #endif
480 #ifdef INET6
481 			if (inp->inp_vflag & INP_IPV6) {
482 				inet6print("local", &inp->in6p_laddr,
483 				    (int)inp->inp_lport, name, 0);
484 				if (!Lflag)
485 					inet6print("remote", &inp->in6p_faddr,
486 					    (int)inp->inp_fport, name,
487 					    inp->inp_lport != inp->inp_fport);
488 			} /* else nothing printed now */
489 #endif /* INET6 */
490 		}
491 		if (xflag) {
492 			xo_emit("{:receive-high-water/%6u} "
493 			    "{:send-high-water/%6u} "
494 			    "{:receive-low-water/%6u} {:send-low-water/%6u} "
495 			    "{:receive-mbuf-bytes/%6u} {:send-mbuf-bytes/%6u} "
496 			    "{:receive-mbuf-bytes-max/%6u} "
497 			    "{:send-mbuf-bytes-max/%6u}",
498 			    so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
499 			    so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
500 			    so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
501 			    so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
502 			if (istcp)
503 				xo_emit(" {:retransmit-timer/%4d.%02d} "
504 				    "{:persist-timer/%4d.%02d} "
505 				    "{:keepalive-timer/%4d.%02d} "
506 				    "{:msl2-timer/%4d.%02d} "
507 				    "{:delay-ack-timer/%4d.%02d} "
508 				    "{:inactivity-timer/%4d.%02d}",
509 				    tp->tt_rexmt / 1000,
510 				    (tp->tt_rexmt % 1000) / 10,
511 				    tp->tt_persist / 1000,
512 				    (tp->tt_persist % 1000) / 10,
513 				    tp->tt_keep / 1000,
514 				    (tp->tt_keep % 1000) / 10,
515 				    tp->tt_2msl / 1000,
516 				    (tp->tt_2msl % 1000) / 10,
517 				    tp->tt_delack / 1000,
518 				    (tp->tt_delack % 1000) / 10,
519 				    tp->t_rcvtime / 1000,
520 				    (tp->t_rcvtime % 1000) / 10);
521 		}
522 		if (istcp && !Lflag && !xflag && !Tflag && !Rflag) {
523 			if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
524 				xo_emit("{:tcp-state/%-11d}", tp->t_state);
525 			else {
526 				xo_emit("{:tcp-state/%-11s}",
527 				    tcpstates[tp->t_state]);
528 #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
529 				/* Show T/TCP `hidden state' */
530 				if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
531 					xo_emit("{:need-syn-or-fin/*}");
532 #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
533 			}
534 		}
535 		if (Rflag) {
536 			/* XXX: is this right Alfred */
537 			xo_emit(" {:flow-id/%08x} {:flow-type/%5d}",
538 			    inp->inp_flowid,
539 			    inp->inp_flowtype);
540 		}
541 		if (istcp) {
542 			if (cflag)
543 				xo_emit(" {:stack/%-*.*s}",
544 
545 					fnamelen, fnamelen, tp->xt_stack);
546 			if (Cflag)
547 				xo_emit(" {:cc/%-*.*s}"
548 					" {:snd-cwnd/%10lu}"
549 					" {:snd-ssthresh/%10lu}"
550 					" {:t-maxseg/%5u} {:ecn/%3s}",
551 					cnamelen, cnamelen, tp->xt_cc,
552 					tp->t_snd_cwnd, tp->t_snd_ssthresh,
553 					tp->t_maxseg,
554 					(tp->t_state >= TCPS_ESTABLISHED ?
555 					    (tp->xt_ecn > 0 ?
556 						(tp->xt_ecn == 1 ?
557 						    "ecn" : "ace")
558 						: "off")
559 					    : "n/a"));
560 			if (Pflag)
561 				xo_emit(" {:log-id/%s}",
562 				    tp->xt_logid[0] == '\0' ?
563 				    "-" : tp->xt_logid);
564 		}
565 		xo_emit("\n");
566 		xo_close_instance("socket");
567 	}
568 	if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
569 		if (oxig->xig_count > xig->xig_count) {
570 			xo_emit("Some {d:lost/%s} sockets may have been "
571 			    "deleted.\n", name);
572 		} else if (oxig->xig_count < xig->xig_count) {
573 			xo_emit("Some {d:created/%s} sockets may have been "
574 			    "created.\n", name);
575 		} else {
576 			xo_emit("Some {d:changed/%s} sockets may have been "
577 			    "created or deleted.\n", name);
578 		}
579 	}
580 	free(buf);
581 }
582 
583 /*
584  * Dump TCP statistics structure.
585  */
586 void
587 tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
588 {
589 	struct tcpstat tcpstat;
590 	uint64_t tcps_states[TCP_NSTATES];
591 
592 #ifdef INET6
593 	if (tcp_done != 0)
594 		return;
595 	else
596 		tcp_done = 1;
597 #endif
598 
599 	if (fetch_stats("net.inet.tcp.stats", off, &tcpstat,
600 	    sizeof(tcpstat), kread_counters) != 0)
601 		return;
602 
603 	if (fetch_stats_ro("net.inet.tcp.states", nl[N_TCPS_STATES].n_value,
604 	    &tcps_states, sizeof(tcps_states), kread_counters) != 0)
605 		return;
606 
607 	xo_open_container("tcp");
608 	xo_emit("{T:/%s}:\n", name);
609 
610 #define	p(f, m) if (tcpstat.f || sflag <= 1)				\
611 	xo_emit(m, (uintmax_t )tcpstat.f, plural(tcpstat.f))
612 #define	p1a(f, m) if (tcpstat.f || sflag <= 1)				\
613 	xo_emit(m, (uintmax_t )tcpstat.f)
614 #define	p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1)	\
615 	xo_emit(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1),		\
616 	    (uintmax_t )tcpstat.f2, plural(tcpstat.f2))
617 #define	p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1)	\
618 	xo_emit(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1),		\
619 	    (uintmax_t )tcpstat.f2)
620 #define	p3(f, m) if (tcpstat.f || sflag <= 1)				\
621 	xo_emit(m, (uintmax_t )tcpstat.f, pluralies(tcpstat.f))
622 
623 	p(tcps_sndtotal, "\t{:sent-packets/%ju} {N:/packet%s sent}\n");
624 	p2(tcps_sndpack,tcps_sndbyte, "\t\t{:sent-data-packets/%ju} "
625 	    "{N:/data packet%s} ({:sent-data-bytes/%ju} {N:/byte%s})\n");
626 	p2(tcps_sndrexmitpack, tcps_sndrexmitbyte, "\t\t"
627 	    "{:sent-retransmitted-packets/%ju} {N:/data packet%s} "
628 	    "({:sent-retransmitted-bytes/%ju} {N:/byte%s}) "
629 	    "{N:retransmitted}\n");
630 	p(tcps_sndrexmitbad, "\t\t"
631 	    "{:sent-unnecessary-retransmitted-packets/%ju} "
632 	    "{N:/data packet%s unnecessarily retransmitted}\n");
633 	p(tcps_mturesent, "\t\t{:sent-resends-by-mtu-discovery/%ju} "
634 	    "{N:/resend%s initiated by MTU discovery}\n");
635 	p2a(tcps_sndacks, tcps_delack, "\t\t{:sent-ack-only-packets/%ju} "
636 	    "{N:/ack-only packet%s/} ({:sent-packets-delayed/%ju} "
637 	    "{N:delayed})\n");
638 	p(tcps_sndurg, "\t\t{:sent-urg-only-packets/%ju} "
639 	    "{N:/URG only packet%s}\n");
640 	p(tcps_sndprobe, "\t\t{:sent-window-probe-packets/%ju} "
641 	    "{N:/window probe packet%s}\n");
642 	p(tcps_sndwinup, "\t\t{:sent-window-update-packets/%ju} "
643 	    "{N:/window update packet%s}\n");
644 	p(tcps_sndctrl, "\t\t{:sent-control-packets/%ju} "
645 	    "{N:/control packet%s}\n");
646 	p(tcps_rcvtotal, "\t{:received-packets/%ju} "
647 	    "{N:/packet%s received}\n");
648 	p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t"
649 	    "{:received-ack-packets/%ju} {N:/ack%s} "
650 	    "{N:(for} {:received-ack-bytes/%ju} {N:/byte%s})\n");
651 	p(tcps_rcvdupack, "\t\t{:received-duplicate-acks/%ju} "
652 	    "{N:/duplicate ack%s}\n");
653 	p(tcps_tunneled_pkts, "\t\t{:received-udp-tunneled-pkts/%ju} "
654 	    "{N:/UDP tunneled pkt%s}\n");
655 	p(tcps_tunneled_errs, "\t\t{:received-bad-udp-tunneled-pkts/%ju} "
656 	    "{N:/UDP tunneled pkt cnt with error%s}\n");
657 	p(tcps_rcvacktoomuch, "\t\t{:received-acks-for-unsent-data/%ju} "
658 	    "{N:/ack%s for unsent data}\n");
659 	p2(tcps_rcvpack, tcps_rcvbyte, "\t\t"
660 	    "{:received-in-sequence-packets/%ju} {N:/packet%s} "
661 	    "({:received-in-sequence-bytes/%ju} {N:/byte%s}) "
662 	    "{N:received in-sequence}\n");
663 	p2(tcps_rcvduppack, tcps_rcvdupbyte, "\t\t"
664 	    "{:received-completely-duplicate-packets/%ju} "
665 	    "{N:/completely duplicate packet%s} "
666 	    "({:received-completely-duplicate-bytes/%ju} {N:/byte%s})\n");
667 	p(tcps_pawsdrop, "\t\t{:received-old-duplicate-packets/%ju} "
668 	    "{N:/old duplicate packet%s}\n");
669 	p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte, "\t\t"
670 	    "{:received-some-duplicate-packets/%ju} "
671 	    "{N:/packet%s with some dup. data} "
672 	    "({:received-some-duplicate-bytes/%ju} {N:/byte%s duped/})\n");
673 	p2(tcps_rcvoopack, tcps_rcvoobyte, "\t\t{:received-out-of-order/%ju} "
674 	    "{N:/out-of-order packet%s} "
675 	    "({:received-out-of-order-bytes/%ju} {N:/byte%s})\n");
676 	p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin, "\t\t"
677 	    "{:received-after-window-packets/%ju} {N:/packet%s} "
678 	    "({:received-after-window-bytes/%ju} {N:/byte%s}) "
679 	    "{N:of data after window}\n");
680 	p(tcps_rcvwinprobe, "\t\t{:received-window-probes/%ju} "
681 	    "{N:/window probe%s}\n");
682 	p(tcps_rcvwinupd, "\t\t{:receive-window-update-packets/%ju} "
683 	    "{N:/window update packet%s}\n");
684 	p(tcps_dsack_count, "\t\t{:received-with-dsack-packets/%ju} "
685 	    "{N:/packet%s received with dsack}\n");
686 	p(tcps_dsack_bytes, "\t\t{:received-with-dsack-bytes/%ju} "
687 	    "{N:/dsack byte%s received (no TLP involved)}\n");
688 	p(tcps_dsack_tlp_bytes, "\t\t{:received-with-dsack-bytes-tlp/%ju} "
689 	    "{N:/dsack byte%s received (TLP responsible)}\n");
690 	p(tcps_rcvafterclose, "\t\t{:received-after-close-packets/%ju} "
691 	    "{N:/packet%s received after close}\n");
692 	p(tcps_rcvbadsum, "\t\t{:discard-bad-checksum/%ju} "
693 	    "{N:/discarded for bad checksum%s}\n");
694 	p(tcps_rcvbadoff, "\t\t{:discard-bad-header-offset/%ju} "
695 	    "{N:/discarded for bad header offset field%s}\n");
696 	p1a(tcps_rcvshort, "\t\t{:discard-too-short/%ju} "
697 	    "{N:discarded because packet too short}\n");
698 	p1a(tcps_rcvreassfull, "\t\t{:discard-reassembly-queue-full/%ju} "
699 	    "{N:discarded due to full reassembly queue}\n");
700 	p(tcps_connattempt, "\t{:connection-requests/%ju} "
701 	    "{N:/connection request%s}\n");
702 	p(tcps_accepts, "\t{:connections-accepts/%ju} "
703 	    "{N:/connection accept%s}\n");
704 	p(tcps_badsyn, "\t{:bad-connection-attempts/%ju} "
705 	    "{N:/bad connection attempt%s}\n");
706 	p(tcps_listendrop, "\t{:listen-queue-overflows/%ju} "
707 	    "{N:/listen queue overflow%s}\n");
708 	p(tcps_badrst, "\t{:ignored-in-window-resets/%ju} "
709 	    "{N:/ignored RSTs in the window%s}\n");
710 	p(tcps_connects, "\t{:connections-established/%ju} "
711 	    "{N:/connection%s established (including accepts)}\n");
712 	p(tcps_usedrtt, "\t\t{:connections-hostcache-rtt/%ju} "
713 	    "{N:/time%s used RTT from hostcache}\n");
714 	p(tcps_usedrttvar, "\t\t{:connections-hostcache-rttvar/%ju} "
715 	    "{N:/time%s used RTT variance from hostcache}\n");
716 	p(tcps_usedssthresh, "\t\t{:connections-hostcache-ssthresh/%ju} "
717 	    "{N:/time%s used slow-start threshold from hostcache}\n");
718 	p2(tcps_closed, tcps_drops, "\t{:connections-closed/%ju} "
719 	    "{N:/connection%s closed (including} "
720 	    "{:connection-drops/%ju} {N:/drop%s})\n");
721 	p(tcps_cachedrtt, "\t\t{:connections-updated-rtt-on-close/%ju} "
722 	    "{N:/connection%s updated cached RTT on close}\n");
723 	p(tcps_cachedrttvar, "\t\t"
724 	    "{:connections-updated-variance-on-close/%ju} "
725 	    "{N:/connection%s updated cached RTT variance on close}\n");
726 	p(tcps_cachedssthresh, "\t\t"
727 	    "{:connections-updated-ssthresh-on-close/%ju} "
728 	    "{N:/connection%s updated cached ssthresh on close}\n");
729 	p(tcps_conndrops, "\t{:embryonic-connections-dropped/%ju} "
730 	    "{N:/embryonic connection%s dropped}\n");
731 	p2(tcps_rttupdated, tcps_segstimed, "\t{:segments-updated-rtt/%ju} "
732 	    "{N:/segment%s updated rtt (of} "
733 	    "{:segment-update-attempts/%ju} {N:/attempt%s})\n");
734 	p(tcps_rexmttimeo, "\t{:retransmit-timeouts/%ju} "
735 	    "{N:/retransmit timeout%s}\n");
736 	p(tcps_timeoutdrop, "\t\t"
737 	    "{:connections-dropped-by-retransmit-timeout/%ju} "
738 	    "{N:/connection%s dropped by rexmit timeout}\n");
739 	p(tcps_persisttimeo, "\t{:persist-timeout/%ju} "
740 	    "{N:/persist timeout%s}\n");
741 	p(tcps_persistdrop, "\t\t"
742 	    "{:connections-dropped-by-persist-timeout/%ju} "
743 	    "{N:/connection%s dropped by persist timeout}\n");
744 	p(tcps_finwait2_drops, "\t"
745 	    "{:connections-dropped-by-finwait2-timeout/%ju} "
746 	    "{N:/Connection%s (fin_wait_2) dropped because of timeout}\n");
747 	p(tcps_keeptimeo, "\t{:keepalive-timeout/%ju} "
748 	    "{N:/keepalive timeout%s}\n");
749 	p(tcps_keepprobe, "\t\t{:keepalive-probes/%ju} "
750 	    "{N:/keepalive probe%s sent}\n");
751 	p(tcps_keepdrops, "\t\t{:connections-dropped-by-keepalives/%ju} "
752 	    "{N:/connection%s dropped by keepalive}\n");
753 	p(tcps_progdrops, "\t{:connections-dropped-due-to-progress-time/%ju} "
754 	    "{N:/connection%s dropped due to exceeding progress time}\n");
755 	p(tcps_predack, "\t{:ack-header-predictions/%ju} "
756 	    "{N:/correct ACK header prediction%s}\n");
757 	p(tcps_preddat, "\t{:data-packet-header-predictions/%ju} "
758 	    "{N:/correct data packet header prediction%s}\n");
759 
760 	xo_open_container("syncache");
761 
762 	p3(tcps_sc_added, "\t{:entries-added/%ju} "
763 	    "{N:/syncache entr%s added}\n");
764 	p1a(tcps_sc_retransmitted, "\t\t{:retransmitted/%ju} "
765 	    "{N:/retransmitted}\n");
766 	p1a(tcps_sc_dupsyn, "\t\t{:duplicates/%ju} {N:/dupsyn}\n");
767 	p1a(tcps_sc_dropped, "\t\t{:dropped/%ju} {N:/dropped}\n");
768 	p1a(tcps_sc_completed, "\t\t{:completed/%ju} {N:/completed}\n");
769 	p1a(tcps_sc_bucketoverflow, "\t\t{:bucket-overflow/%ju} "
770 	    "{N:/bucket overflow}\n");
771 	p1a(tcps_sc_cacheoverflow, "\t\t{:cache-overflow/%ju} "
772 	    "{N:/cache overflow}\n");
773 	p1a(tcps_sc_reset, "\t\t{:reset/%ju} {N:/reset}\n");
774 	p1a(tcps_sc_stale, "\t\t{:stale/%ju} {N:/stale}\n");
775 	p1a(tcps_sc_aborted, "\t\t{:aborted/%ju} {N:/aborted}\n");
776 	p1a(tcps_sc_badack, "\t\t{:bad-ack/%ju} {N:/badack}\n");
777 	p1a(tcps_sc_unreach, "\t\t{:unreachable/%ju} {N:/unreach}\n");
778 	p(tcps_sc_zonefail, "\t\t{:zone-failures/%ju} {N:/zone failure%s}\n");
779 	p(tcps_sc_sendcookie, "\t{:sent-cookies/%ju} {N:/cookie%s sent}\n");
780 	p(tcps_sc_recvcookie, "\t{:receivd-cookies/%ju} "
781 	    "{N:/cookie%s received}\n");
782 
783 	xo_close_container("syncache");
784 
785 	xo_open_container("hostcache");
786 
787 	p3(tcps_hc_added, "\t{:entries-added/%ju} "
788 	    "{N:/hostcache entr%s added}\n");
789 	p1a(tcps_hc_bucketoverflow, "\t\t{:buffer-overflows/%ju} "
790 	    "{N:/bucket overflow}\n");
791 
792 	xo_close_container("hostcache");
793 
794 	xo_open_container("sack");
795 
796 	p(tcps_sack_recovery_episode, "\t{:recovery-episodes/%ju} "
797 	    "{N:/SACK recovery episode%s}\n");
798  	p(tcps_sack_rexmits, "\t{:segment-retransmits/%ju} "
799 	    "{N:/segment rexmit%s in SACK recovery episodes}\n");
800  	p(tcps_sack_rexmit_bytes, "\t{:byte-retransmits/%ju} "
801 	    "{N:/byte rexmit%s in SACK recovery episodes}\n");
802  	p(tcps_sack_rcv_blocks, "\t{:received-blocks/%ju} "
803 	    "{N:/SACK option%s (SACK blocks) received}\n");
804 	p(tcps_sack_send_blocks, "\t{:sent-option-blocks/%ju} "
805 	    "{N:/SACK option%s (SACK blocks) sent}\n");
806 	p(tcps_sack_lostrexmt, "\t{:lost-retransmissions/%ju} "
807 	    "{N:/SACK retransmission%s lost}\n");
808 	p1a(tcps_sack_sboverflow, "\t{:scoreboard-overflows/%ju} "
809 	    "{N:/SACK scoreboard overflow}\n");
810 
811 	xo_close_container("sack");
812 	xo_open_container("ecn");
813 
814 	p(tcps_ecn_rcvce, "\t{:received-ce-packets/%ju} "
815 	    "{N:/packet%s received with ECN CE bit set}\n");
816 	p(tcps_ecn_rcvect0, "\t{:received-ect0-packets/%ju} "
817 	    "{N:/packet%s received with ECN ECT(0) bit set}\n");
818 	p(tcps_ecn_rcvect1, "\t{:received-ect1-packets/%ju} "
819 	    "{N:/packet%s received with ECN ECT(1) bit set}\n");
820 	p(tcps_ecn_sndect0, "\t{:sent-ect0-packets/%ju} "
821 	    "{N:/packet%s sent with ECN ECT(0) bit set}\n");
822 	p(tcps_ecn_sndect1, "\t{:sent-ect1-packets/%ju} "
823 	    "{N:/packet%s sent with ECN ECT(1) bit set}\n");
824 	p(tcps_ecn_shs, "\t{:handshakes/%ju} "
825 	    "{N:/successful ECN handshake%s}\n");
826 	p(tcps_ecn_rcwnd, "\t{:congestion-reductions/%ju} "
827 	    "{N:/time%s ECN reduced the congestion window}\n");
828 
829 	p(tcps_ace_nect, "\t{:ace-nonect-syn/%ju} "
830 	    "{N:/ACE SYN packet%s with Non-ECT}\n");
831 	p(tcps_ace_ect0, "\t{:ace-ect0-syn/%ju} "
832 	    "{N:/ACE SYN packet%s with ECT0}\n");
833 	p(tcps_ace_ect1, "\t{:ace-ect1-syn/%ju} "
834 	    "{N:/ACE SYN packet%s with ECT1}\n");
835 	p(tcps_ace_ce, "\t{:ace-ce-syn/%ju} "
836 	    "{N:/ACE SYN packet%s with CE}\n");
837 
838 	xo_close_container("ecn");
839 	xo_open_container("tcp-signature");
840 	p(tcps_sig_rcvgoodsig, "\t{:received-good-signature/%ju} "
841 	    "{N:/packet%s with matching signature received}\n");
842 	p(tcps_sig_rcvbadsig, "\t{:received-bad-signature/%ju} "
843 	    "{N:/packet%s with bad signature received}\n");
844 	p(tcps_sig_err_buildsig, "\t{:failed-make-signature/%ju} "
845 	    "{N:/time%s failed to make signature due to no SA}\n");
846 	p(tcps_sig_err_sigopt, "\t{:no-signature-expected/%ju} "
847 	    "{N:/time%s unexpected signature received}\n");
848 	p(tcps_sig_err_nosigopt, "\t{:no-signature-provided/%ju} "
849 	    "{N:/time%s no signature provided by segment}\n");
850 
851 	xo_close_container("tcp-signature");
852 	xo_open_container("pmtud");
853 
854 	p(tcps_pmtud_blackhole_activated, "\t{:pmtud-activated/%ju} "
855 	    "{N:/Path MTU discovery black hole detection activation%s}\n");
856 	p(tcps_pmtud_blackhole_activated_min_mss,
857 	    "\t{:pmtud-activated-min-mss/%ju} "
858 	    "{N:/Path MTU discovery black hole detection min MSS activation%s}\n");
859 	p(tcps_pmtud_blackhole_failed, "\t{:pmtud-failed/%ju} "
860 	    "{N:/Path MTU discovery black hole detection failure%s}\n");
861 
862 	xo_close_container("pmtud");
863 	xo_open_container("tw");
864 
865 	p(tcps_tw_responds, "\t{:tw_responds/%ju} "
866 	    "{N:/time%s connection in TIME-WAIT responded with ACK}\n");
867 	p(tcps_tw_recycles, "\t{:tw_recycles/%ju} "
868 	    "{N:/time%s connection in TIME-WAIT was actively recycled}\n");
869 	p(tcps_tw_resets, "\t{:tw_resets/%ju} "
870 	    "{N:/time%s connection in TIME-WAIT responded with RST}\n");
871 
872 	xo_close_container("tw");
873  #undef p
874  #undef p1a
875  #undef p2
876  #undef p2a
877  #undef p3
878 
879 	xo_open_container("TCP connection count by state");
880 	xo_emit("{T:/TCP connection count by state}:\n");
881 	for (int i = 0; i < TCP_NSTATES; i++) {
882 		/*
883 		 * XXXGL: is there a way in libxo to use %s
884 		 * in the "content string" of a format
885 		 * string? I failed to do that, that's why
886 		 * a temporary buffer is used to construct
887 		 * format string for xo_emit().
888 		 */
889 		char fmtbuf[80];
890 
891 		if (sflag > 1 && tcps_states[i] == 0)
892 			continue;
893 		snprintf(fmtbuf, sizeof(fmtbuf), "\t{:%s/%%ju} "
894                     "{Np:/connection ,connections} in %s state\n",
895 		    tcpstates[i], tcpstates[i]);
896 		xo_emit(fmtbuf, (uintmax_t )tcps_states[i]);
897 	}
898 	xo_close_container("TCP connection count by state");
899 
900 	xo_close_container("tcp");
901 }
902 
903 /*
904  * Dump UDP statistics structure.
905  */
906 void
907 udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
908 {
909 	struct udpstat udpstat;
910 	uint64_t delivered;
911 
912 #ifdef INET6
913 	if (udp_done != 0)
914 		return;
915 	else
916 		udp_done = 1;
917 #endif
918 
919 	if (fetch_stats("net.inet.udp.stats", off, &udpstat,
920 	    sizeof(udpstat), kread_counters) != 0)
921 		return;
922 
923 	xo_open_container("udp");
924 	xo_emit("{T:/%s}:\n", name);
925 
926 #define	p(f, m) if (udpstat.f || sflag <= 1) \
927 	xo_emit("\t" m, (uintmax_t)udpstat.f, plural(udpstat.f))
928 #define	p1a(f, m) if (udpstat.f || sflag <= 1) \
929 	xo_emit("\t" m, (uintmax_t)udpstat.f)
930 
931 	p(udps_ipackets, "{:received-datagrams/%ju} "
932 	    "{N:/datagram%s received}\n");
933 	p1a(udps_hdrops, "{:dropped-incomplete-headers/%ju} "
934 	    "{N:/with incomplete header}\n");
935 	p1a(udps_badlen, "{:dropped-bad-data-length/%ju} "
936 	    "{N:/with bad data length field}\n");
937 	p1a(udps_badsum, "{:dropped-bad-checksum/%ju} "
938 	    "{N:/with bad checksum}\n");
939 	p1a(udps_nosum, "{:dropped-no-checksum/%ju} "
940 	    "{N:/with no checksum}\n");
941 	p1a(udps_noport, "{:dropped-no-socket/%ju} "
942 	    "{N:/dropped due to no socket}\n");
943 	p(udps_noportbcast, "{:dropped-broadcast-multicast/%ju} "
944 	    "{N:/broadcast\\/multicast datagram%s undelivered}\n");
945 	p1a(udps_fullsock, "{:dropped-full-socket-buffer/%ju} "
946 	    "{N:/dropped due to full socket buffers}\n");
947 	p1a(udpps_pcbhashmiss, "{:not-for-hashed-pcb/%ju} "
948 	    "{N:/not for hashed pcb}\n");
949 	delivered = udpstat.udps_ipackets -
950 		    udpstat.udps_hdrops -
951 		    udpstat.udps_badlen -
952 		    udpstat.udps_badsum -
953 		    udpstat.udps_noport -
954 		    udpstat.udps_noportbcast -
955 		    udpstat.udps_fullsock;
956 	if (delivered || sflag <= 1)
957 		xo_emit("\t{:delivered-packets/%ju} {N:/delivered}\n",
958 		    (uint64_t)delivered);
959 	p(udps_opackets, "{:output-packets/%ju} {N:/datagram%s output}\n");
960 	/* the next statistic is cumulative in udps_noportbcast */
961 	p(udps_filtermcast, "{:multicast-source-filter-matches/%ju} "
962 	    "{N:/time%s multicast source filter matched}\n");
963 #undef p
964 #undef p1a
965 	xo_close_container("udp");
966 }
967 
968 /*
969  * Dump CARP statistics structure.
970  */
971 void
972 carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
973 {
974 	struct carpstats carpstat;
975 
976 	if (fetch_stats("net.inet.carp.stats", off, &carpstat,
977 	    sizeof(carpstat), kread_counters) != 0)
978 		return;
979 
980 	xo_open_container(name);
981 	xo_emit("{T:/%s}:\n", name);
982 
983 #define	p(f, m) if (carpstat.f || sflag <= 1) \
984 	xo_emit(m, (uintmax_t)carpstat.f, plural(carpstat.f))
985 #define	p2(f, m) if (carpstat.f || sflag <= 1) \
986 	xo_emit(m, (uintmax_t)carpstat.f)
987 
988 	p(carps_ipackets, "\t{:received-inet-packets/%ju} "
989 	    "{N:/packet%s received (IPv4)}\n");
990 	p(carps_ipackets6, "\t{:received-inet6-packets/%ju} "
991 	    "{N:/packet%s received (IPv6)}\n");
992 	p(carps_badttl, "\t\t{:dropped-wrong-ttl/%ju} "
993 	    "{N:/packet%s discarded for wrong TTL}\n");
994 	p(carps_hdrops, "\t\t{:dropped-short-header/%ju} "
995 	    "{N:/packet%s shorter than header}\n");
996 	p(carps_badsum, "\t\t{:dropped-bad-checksum/%ju} "
997 	    "{N:/discarded for bad checksum%s}\n");
998 	p(carps_badver,	"\t\t{:dropped-bad-version/%ju} "
999 	    "{N:/discarded packet%s with a bad version}\n");
1000 	p2(carps_badlen, "\t\t{:dropped-short-packet/%ju} "
1001 	    "{N:/discarded because packet too short}\n");
1002 	p2(carps_badauth, "\t\t{:dropped-bad-authentication/%ju} "
1003 	    "{N:/discarded for bad authentication}\n");
1004 	p2(carps_badvhid, "\t\t{:dropped-bad-vhid/%ju} "
1005 	    "{N:/discarded for bad vhid}\n");
1006 	p2(carps_badaddrs, "\t\t{:dropped-bad-address-list/%ju} "
1007 	    "{N:/discarded because of a bad address list}\n");
1008 	p(carps_opackets, "\t{:sent-inet-packets/%ju} "
1009 	    "{N:/packet%s sent (IPv4)}\n");
1010 	p(carps_opackets6, "\t{:sent-inet6-packets/%ju} "
1011 	    "{N:/packet%s sent (IPv6)}\n");
1012 	p2(carps_onomem, "\t\t{:send-failed-memory-error/%ju} "
1013 	    "{N:/send failed due to mbuf memory error}\n");
1014 #if notyet
1015 	p(carps_ostates, "\t\t{:send-state-updates/%s} "
1016 	    "{N:/state update%s sent}\n");
1017 #endif
1018 #undef p
1019 #undef p2
1020 	xo_close_container(name);
1021 }
1022 
1023 /*
1024  * Dump IP statistics structure.
1025  */
1026 void
1027 ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1028 {
1029 	struct ipstat ipstat;
1030 
1031 	if (fetch_stats("net.inet.ip.stats", off, &ipstat,
1032 	    sizeof(ipstat), kread_counters) != 0)
1033 		return;
1034 
1035 	xo_open_container(name);
1036 	xo_emit("{T:/%s}:\n", name);
1037 
1038 #define	p(f, m) if (ipstat.f || sflag <= 1) \
1039 	xo_emit(m, (uintmax_t )ipstat.f, plural(ipstat.f))
1040 #define	p1a(f, m) if (ipstat.f || sflag <= 1) \
1041 	xo_emit(m, (uintmax_t )ipstat.f)
1042 
1043 	p(ips_total, "\t{:received-packets/%ju} "
1044 	    "{N:/total packet%s received}\n");
1045 	p(ips_badsum, "\t{:dropped-bad-checksum/%ju} "
1046 	    "{N:/bad header checksum%s}\n");
1047 	p1a(ips_toosmall, "\t{:dropped-below-minimum-size/%ju} "
1048 	    "{N:/with size smaller than minimum}\n");
1049 	p1a(ips_tooshort, "\t{:dropped-short-packets/%ju} "
1050 	    "{N:/with data size < data length}\n");
1051 	p1a(ips_toolong, "\t{:dropped-too-long/%ju} "
1052 	    "{N:/with ip length > max ip packet size}\n");
1053 	p1a(ips_badhlen, "\t{:dropped-short-header-length/%ju} "
1054 	    "{N:/with header length < data size}\n");
1055 	p1a(ips_badlen, "\t{:dropped-short-data/%ju} "
1056 	    "{N:/with data length < header length}\n");
1057 	p1a(ips_badoptions, "\t{:dropped-bad-options/%ju} "
1058 	    "{N:/with bad options}\n");
1059 	p1a(ips_badvers, "\t{:dropped-bad-version/%ju} "
1060 	    "{N:/with incorrect version number}\n");
1061 	p(ips_fragments, "\t{:received-fragments/%ju} "
1062 	    "{N:/fragment%s received}\n");
1063 	p(ips_fragdropped, "\t{:dropped-fragments/%ju} "
1064 	    "{N:/fragment%s dropped (dup or out of space)}\n");
1065 	p(ips_fragtimeout, "\t{:dropped-fragments-after-timeout/%ju} "
1066 	    "{N:/fragment%s dropped after timeout}\n");
1067 	p(ips_reassembled, "\t{:reassembled-packets/%ju} "
1068 	    "{N:/packet%s reassembled ok}\n");
1069 	p(ips_delivered, "\t{:received-local-packets/%ju} "
1070 	    "{N:/packet%s for this host}\n");
1071 	p(ips_noproto, "\t{:dropped-unknown-protocol/%ju} "
1072 	    "{N:/packet%s for unknown\\/unsupported protocol}\n");
1073 	p(ips_forward, "\t{:forwarded-packets/%ju} "
1074 	    "{N:/packet%s forwarded}");
1075 	p(ips_fastforward, " ({:fast-forwarded-packets/%ju} "
1076 	    "{N:/packet%s fast forwarded})");
1077 	if (ipstat.ips_forward || sflag <= 1)
1078 		xo_emit("\n");
1079 	p(ips_cantforward, "\t{:packets-cannot-forward/%ju} "
1080 	    "{N:/packet%s not forwardable}\n");
1081 	p(ips_notmember, "\t{:received-unknown-multicast-group/%ju} "
1082 	    "{N:/packet%s received for unknown multicast group}\n");
1083 	p(ips_redirectsent, "\t{:redirects-sent/%ju} "
1084 	    "{N:/redirect%s sent}\n");
1085 	p(ips_localout, "\t{:sent-packets/%ju} "
1086 	    "{N:/packet%s sent from this host}\n");
1087 	p(ips_rawout, "\t{:send-packets-fabricated-header/%ju} "
1088 	    "{N:/packet%s sent with fabricated ip header}\n");
1089 	p(ips_odropped, "\t{:discard-no-mbufs/%ju} "
1090 	    "{N:/output packet%s dropped due to no bufs, etc.}\n");
1091 	p(ips_noroute, "\t{:discard-no-route/%ju} "
1092 	    "{N:/output packet%s discarded due to no route}\n");
1093 	p(ips_fragmented, "\t{:sent-fragments/%ju} "
1094 	    "{N:/output datagram%s fragmented}\n");
1095 	p(ips_ofragments, "\t{:fragments-created/%ju} "
1096 	    "{N:/fragment%s created}\n");
1097 	p(ips_cantfrag, "\t{:discard-cannot-fragment/%ju} "
1098 	    "{N:/datagram%s that can't be fragmented}\n");
1099 	p(ips_nogif, "\t{:discard-tunnel-no-gif/%ju} "
1100 	    "{N:/tunneling packet%s that can't find gif}\n");
1101 	p(ips_badaddr, "\t{:discard-bad-address/%ju} "
1102 	    "{N:/datagram%s with bad address in header}\n");
1103 #undef p
1104 #undef p1a
1105 	xo_close_container(name);
1106 }
1107 
1108 /*
1109  * Dump ARP statistics structure.
1110  */
1111 void
1112 arp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1113 {
1114 	struct arpstat arpstat;
1115 
1116 	if (fetch_stats("net.link.ether.arp.stats", off, &arpstat,
1117 	    sizeof(arpstat), kread_counters) != 0)
1118 		return;
1119 
1120 	xo_open_container(name);
1121 	xo_emit("{T:/%s}:\n", name);
1122 
1123 #define	p(f, m) if (arpstat.f || sflag <= 1) \
1124 	xo_emit("\t" m, (uintmax_t)arpstat.f, plural(arpstat.f))
1125 #define	p2(f, m) if (arpstat.f || sflag <= 1) \
1126 	xo_emit("\t" m, (uintmax_t)arpstat.f, pluralies(arpstat.f))
1127 
1128 	p(txrequests, "{:sent-requests/%ju} {N:/ARP request%s sent}\n");
1129 	p(txerrors, "{:sent-failures/%ju} {N:/ARP request%s failed to sent}\n");
1130 	p2(txreplies, "{:sent-replies/%ju} {N:/ARP repl%s sent}\n");
1131 	p(rxrequests, "{:received-requests/%ju} "
1132 	    "{N:/ARP request%s received}\n");
1133 	p2(rxreplies, "{:received-replies/%ju} "
1134 	    "{N:/ARP repl%s received}\n");
1135 	p(received, "{:received-packets/%ju} "
1136 	    "{N:/ARP packet%s received}\n");
1137 	p(dropped, "{:dropped-no-entry/%ju} "
1138 	    "{N:/total packet%s dropped due to no ARP entry}\n");
1139 	p(timeouts, "{:entries-timeout/%ju} "
1140 	    "{N:/ARP entry%s timed out}\n");
1141 	p(dupips, "{:dropped-duplicate-address/%ju} "
1142 	    "{N:/Duplicate IP%s seen}\n");
1143 #undef p
1144 #undef p2
1145 	xo_close_container(name);
1146 }
1147 
1148 
1149 
1150 static	const char *icmpnames[ICMP_MAXTYPE + 1] = {
1151 	"echo reply",			/* RFC 792 */
1152 	"#1",
1153 	"#2",
1154 	"destination unreachable",	/* RFC 792 */
1155 	"source quench",		/* RFC 792 */
1156 	"routing redirect",		/* RFC 792 */
1157 	"#6",
1158 	"#7",
1159 	"echo",				/* RFC 792 */
1160 	"router advertisement",		/* RFC 1256 */
1161 	"router solicitation",		/* RFC 1256 */
1162 	"time exceeded",		/* RFC 792 */
1163 	"parameter problem",		/* RFC 792 */
1164 	"time stamp",			/* RFC 792 */
1165 	"time stamp reply",		/* RFC 792 */
1166 	"information request",		/* RFC 792 */
1167 	"information request reply",	/* RFC 792 */
1168 	"address mask request",		/* RFC 950 */
1169 	"address mask reply",		/* RFC 950 */
1170 	"#19",
1171 	"#20",
1172 	"#21",
1173 	"#22",
1174 	"#23",
1175 	"#24",
1176 	"#25",
1177 	"#26",
1178 	"#27",
1179 	"#28",
1180 	"#29",
1181 	"icmp traceroute",		/* RFC 1393 */
1182 	"datagram conversion error",	/* RFC 1475 */
1183 	"mobile host redirect",
1184 	"IPv6 where-are-you",
1185 	"IPv6 i-am-here",
1186 	"mobile registration req",
1187 	"mobile registration reply",
1188 	"domain name request",		/* RFC 1788 */
1189 	"domain name reply",		/* RFC 1788 */
1190 	"icmp SKIP",
1191 	"icmp photuris",		/* RFC 2521 */
1192 };
1193 
1194 /*
1195  * Dump ICMP statistics.
1196  */
1197 void
1198 icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1199 {
1200 	struct icmpstat icmpstat;
1201 	size_t len;
1202 	int i, first;
1203 
1204 	if (fetch_stats("net.inet.icmp.stats", off, &icmpstat,
1205 	    sizeof(icmpstat), kread_counters) != 0)
1206 		return;
1207 
1208 	xo_open_container(name);
1209 	xo_emit("{T:/%s}:\n", name);
1210 
1211 #define	p(f, m) if (icmpstat.f || sflag <= 1) \
1212 	xo_emit(m, icmpstat.f, plural(icmpstat.f))
1213 #define	p1a(f, m) if (icmpstat.f || sflag <= 1) \
1214 	xo_emit(m, icmpstat.f)
1215 #define	p2(f, m) if (icmpstat.f || sflag <= 1) \
1216 	xo_emit(m, icmpstat.f, plurales(icmpstat.f))
1217 
1218 	p(icps_error, "\t{:icmp-calls/%lu} "
1219 	    "{N:/call%s to icmp_error}\n");
1220 	p(icps_oldicmp, "\t{:errors-not-from-message/%lu} "
1221 	    "{N:/error%s not generated in response to an icmp message}\n");
1222 
1223 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) {
1224 		if (icmpstat.icps_outhist[i] != 0) {
1225 			if (first) {
1226 				xo_open_list("output-histogram");
1227 				xo_emit("\tOutput histogram:\n");
1228 				first = 0;
1229 			}
1230 			xo_open_instance("output-histogram");
1231 			if (icmpnames[i] != NULL)
1232 				xo_emit("\t\t{k:name/%s}: {:count/%lu}\n",
1233 				    icmpnames[i], icmpstat.icps_outhist[i]);
1234 			else
1235 				xo_emit("\t\tunknown ICMP #{k:name/%d}: "
1236 				    "{:count/%lu}\n",
1237 				    i, icmpstat.icps_outhist[i]);
1238 			xo_close_instance("output-histogram");
1239 		}
1240 	}
1241 	if (!first)
1242 		xo_close_list("output-histogram");
1243 
1244 	p(icps_badcode, "\t{:dropped-bad-code/%lu} "
1245 	    "{N:/message%s with bad code fields}\n");
1246 	p(icps_tooshort, "\t{:dropped-too-short/%lu} "
1247 	    "{N:/message%s less than the minimum length}\n");
1248 	p(icps_checksum, "\t{:dropped-bad-checksum/%lu} "
1249 	    "{N:/message%s with bad checksum}\n");
1250 	p(icps_badlen, "\t{:dropped-bad-length/%lu} "
1251 	    "{N:/message%s with bad length}\n");
1252 	p1a(icps_bmcastecho, "\t{:dropped-multicast-echo/%lu} "
1253 	    "{N:/multicast echo requests ignored}\n");
1254 	p1a(icps_bmcasttstamp, "\t{:dropped-multicast-timestamp/%lu} "
1255 	    "{N:/multicast timestamp requests ignored}\n");
1256 
1257 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) {
1258 		if (icmpstat.icps_inhist[i] != 0) {
1259 			if (first) {
1260 				xo_open_list("input-histogram");
1261 				xo_emit("\tInput histogram:\n");
1262 				first = 0;
1263 			}
1264 			xo_open_instance("input-histogram");
1265 			if (icmpnames[i] != NULL)
1266 				xo_emit("\t\t{k:name/%s}: {:count/%lu}\n",
1267 					icmpnames[i],
1268 					icmpstat.icps_inhist[i]);
1269 			else
1270 				xo_emit(
1271 			"\t\tunknown ICMP #{k:name/%d}: {:count/%lu}\n",
1272 					i, icmpstat.icps_inhist[i]);
1273 			xo_close_instance("input-histogram");
1274 		}
1275 	}
1276 	if (!first)
1277 		xo_close_list("input-histogram");
1278 
1279 	p(icps_reflect, "\t{:sent-packets/%lu} "
1280 	    "{N:/message response%s generated}\n");
1281 	p2(icps_badaddr, "\t{:discard-invalid-return-address/%lu} "
1282 	    "{N:/invalid return address%s}\n");
1283 	p(icps_noroute, "\t{:discard-no-route/%lu} "
1284 	    "{N:/no return route%s}\n");
1285 #undef p
1286 #undef p1a
1287 #undef p2
1288 	if (live) {
1289 		len = sizeof i;
1290 		if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) <
1291 		    0)
1292 			return;
1293 		xo_emit("\tICMP address mask responses are "
1294 		    "{q:icmp-address-responses/%sabled}\n", i ? "en" : "dis");
1295 	}
1296 
1297 	xo_close_container(name);
1298 }
1299 
1300 /*
1301  * Dump IGMP statistics structure.
1302  */
1303 void
1304 igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1305 {
1306 	struct igmpstat igmpstat;
1307 	int error, zflag0;
1308 
1309 	if (fetch_stats("net.inet.igmp.stats", 0, &igmpstat,
1310 	    sizeof(igmpstat), kread) != 0)
1311 		return;
1312 	/*
1313 	 * Reread net.inet.igmp.stats when zflag == 1.
1314 	 * This is because this MIB contains version number and
1315 	 * length of the structure which are not set when clearing
1316 	 * the counters.
1317 	 */
1318 	zflag0 = zflag;
1319 	if (zflag) {
1320 		zflag = 0;
1321 		error = fetch_stats("net.inet.igmp.stats", 0, &igmpstat,
1322 		    sizeof(igmpstat), kread);
1323 		zflag = zflag0;
1324 		if (error)
1325 			return;
1326 	}
1327 
1328 	if (igmpstat.igps_version != IGPS_VERSION_3) {
1329 		xo_warnx("%s: version mismatch (%d != %d)", __func__,
1330 		    igmpstat.igps_version, IGPS_VERSION_3);
1331 		return;
1332 	}
1333 	if (igmpstat.igps_len != IGPS_VERSION3_LEN) {
1334 		xo_warnx("%s: size mismatch (%d != %d)", __func__,
1335 		    igmpstat.igps_len, IGPS_VERSION3_LEN);
1336 		return;
1337 	}
1338 
1339 	xo_open_container(name);
1340 	xo_emit("{T:/%s}:\n", name);
1341 
1342 #define	p64(f, m) if (igmpstat.f || sflag <= 1) \
1343 	xo_emit(m, (uintmax_t) igmpstat.f, plural(igmpstat.f))
1344 #define	py64(f, m) if (igmpstat.f || sflag <= 1) \
1345 	xo_emit(m, (uintmax_t) igmpstat.f, pluralies(igmpstat.f))
1346 
1347 	p64(igps_rcv_total, "\t{:received-messages/%ju} "
1348 	    "{N:/message%s received}\n");
1349 	p64(igps_rcv_tooshort, "\t{:dropped-too-short/%ju} "
1350 	    "{N:/message%s received with too few bytes}\n");
1351 	p64(igps_rcv_badttl, "\t{:dropped-wrong-ttl/%ju} "
1352 	    "{N:/message%s received with wrong TTL}\n");
1353 	p64(igps_rcv_badsum, "\t{:dropped-bad-checksum/%ju} "
1354 	    "{N:/message%s received with bad checksum}\n");
1355 	py64(igps_rcv_v1v2_queries, "\t{:received-membership-queries/%ju} "
1356 	    "{N:/V1\\/V2 membership quer%s received}\n");
1357 	py64(igps_rcv_v3_queries, "\t{:received-v3-membership-queries/%ju} "
1358 	    "{N:/V3 membership quer%s received}\n");
1359 	py64(igps_rcv_badqueries, "\t{:dropped-membership-queries/%ju} "
1360 	    "{N:/membership quer%s received with invalid field(s)}\n");
1361 	py64(igps_rcv_gen_queries, "\t{:received-general-queries/%ju} "
1362 	    "{N:/general quer%s received}\n");
1363 	py64(igps_rcv_group_queries, "\t{:received-group-queries/%ju} "
1364 	    "{N:/group quer%s received}\n");
1365 	py64(igps_rcv_gsr_queries, "\t{:received-group-source-queries/%ju} "
1366 	    "{N:/group-source quer%s received}\n");
1367 	py64(igps_drop_gsr_queries, "\t{:dropped-group-source-queries/%ju} "
1368 	    "{N:/group-source quer%s dropped}\n");
1369 	p64(igps_rcv_reports, "\t{:received-membership-requests/%ju} "
1370 	    "{N:/membership report%s received}\n");
1371 	p64(igps_rcv_badreports, "\t{:dropped-membership-reports/%ju} "
1372 	    "{N:/membership report%s received with invalid field(s)}\n");
1373 	p64(igps_rcv_ourreports, "\t"
1374 	    "{:received-membership-reports-matching/%ju} "
1375 	    "{N:/membership report%s received for groups to which we belong}"
1376 	    "\n");
1377 	p64(igps_rcv_nora, "\t{:received-v3-reports-no-router-alert/%ju} "
1378 	    "{N:/V3 report%s received without Router Alert}\n");
1379 	p64(igps_snd_reports, "\t{:sent-membership-reports/%ju} "
1380 	    "{N:/membership report%s sent}\n");
1381 #undef p64
1382 #undef py64
1383 	xo_close_container(name);
1384 }
1385 
1386 /*
1387  * Dump PIM statistics structure.
1388  */
1389 void
1390 pim_stats(u_long off __unused, const char *name, int af1 __unused,
1391     int proto __unused)
1392 {
1393 	struct pimstat pimstat;
1394 
1395 	if (fetch_stats("net.inet.pim.stats", off, &pimstat,
1396 	    sizeof(pimstat), kread_counters) != 0)
1397 		return;
1398 
1399 	xo_open_container(name);
1400 	xo_emit("{T:/%s}:\n", name);
1401 
1402 #define	p(f, m) if (pimstat.f || sflag <= 1) \
1403 	xo_emit(m, (uintmax_t)pimstat.f, plural(pimstat.f))
1404 #define	py(f, m) if (pimstat.f || sflag <= 1) \
1405 	xo_emit(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
1406 
1407 	p(pims_rcv_total_msgs, "\t{:received-messages/%ju} "
1408 	    "{N:/message%s received}\n");
1409 	p(pims_rcv_total_bytes, "\t{:received-bytes/%ju} "
1410 	    "{N:/byte%s received}\n");
1411 	p(pims_rcv_tooshort, "\t{:dropped-too-short/%ju} "
1412 	    "{N:/message%s received with too few bytes}\n");
1413 	p(pims_rcv_badsum, "\t{:dropped-bad-checksum/%ju} "
1414 	    "{N:/message%s received with bad checksum}\n");
1415 	p(pims_rcv_badversion, "\t{:dropped-bad-version/%ju} "
1416 	    "{N:/message%s received with bad version}\n");
1417 	p(pims_rcv_registers_msgs, "\t{:received-data-register-messages/%ju} "
1418 	    "{N:/data register message%s received}\n");
1419 	p(pims_rcv_registers_bytes, "\t{:received-data-register-bytes/%ju} "
1420 	    "{N:/data register byte%s received}\n");
1421 	p(pims_rcv_registers_wrongiif, "\t"
1422 	    "{:received-data-register-wrong-interface/%ju} "
1423 	    "{N:/data register message%s received on wrong iif}\n");
1424 	p(pims_rcv_badregisters, "\t{:received-bad-registers/%ju} "
1425 	    "{N:/bad register%s received}\n");
1426 	p(pims_snd_registers_msgs, "\t{:sent-data-register-messages/%ju} "
1427 	    "{N:/data register message%s sent}\n");
1428 	p(pims_snd_registers_bytes, "\t{:sent-data-register-bytes/%ju} "
1429 	    "{N:/data register byte%s sent}\n");
1430 #undef p
1431 #undef py
1432 	xo_close_container(name);
1433 }
1434 
1435 /*
1436  * Dump divert(4) statistics structure.
1437  */
1438 void
1439 divert_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1440 {
1441 	struct divstat divstat;
1442 
1443 	if (fetch_stats("net.inet.divert.stats", off, &divstat,
1444 	    sizeof(divstat), kread_counters) != 0)
1445 		return;
1446 
1447 	xo_open_container(name);
1448 	xo_emit("{T:/%s}:\n", name);
1449 
1450 #define	p(f, m) if (divstat.f || sflag <= 1) \
1451 	xo_emit(m, (uintmax_t)divstat.f, plural(divstat.f))
1452 
1453 	p(div_diverted, "\t{:diverted-packets/%ju} "
1454 	    "{N:/packet%s successfully diverted to userland}\n");
1455 	p(div_noport, "\t{:noport-fails/%ju} "
1456 	    "{N:/packet%s failed to divert due to no socket bound at port}\n");
1457 	p(div_outbound, "\t{:outbound-packets/%ju} "
1458 	    "{N:/packet%s successfully re-injected as outbound}\n");
1459 	p(div_inbound, "\t{:inbound-packets/%ju} "
1460 	    "{N:/packet%s successfully re-injected as inbound}\n");
1461 #undef p
1462 	xo_close_container(name);
1463 }
1464 
1465 #ifdef INET
1466 /*
1467  * Pretty print an Internet address (net address + port).
1468  */
1469 static void
1470 inetprint(const char *container, struct in_addr *in, int port,
1471     const char *proto, int num_port, const int af1)
1472 {
1473 	struct servent *sp = 0;
1474 	char line[80], *cp;
1475 	int width;
1476 	size_t alen, plen;
1477 
1478 	if (container)
1479 		xo_open_container(container);
1480 
1481 	if (Wflag)
1482 	    snprintf(line, sizeof(line), "%s.", inetname(in));
1483 	else
1484 	    snprintf(line, sizeof(line), "%.*s.",
1485 		(Aflag && !num_port) ? 12 : 16, inetname(in));
1486 	alen = strlen(line);
1487 	cp = line + alen;
1488 	if (!num_port && port)
1489 		sp = getservbyport((int)port, proto);
1490 	if (sp || port == 0)
1491 		snprintf(cp, sizeof(line) - alen,
1492 		    "%.15s ", sp ? sp->s_name : "*");
1493 	else
1494 		snprintf(cp, sizeof(line) - alen,
1495 		    "%d ", ntohs((u_short)port));
1496 	width = (Aflag && !Wflag) ? 18 :
1497 		((!Wflag || af1 == AF_INET) ? 22 : 45);
1498 	if (Wflag)
1499 		xo_emit("{d:target/%-*s} ", width, line);
1500 	else
1501 		xo_emit("{d:target/%-*.*s} ", width, width, line);
1502 
1503 	plen = strlen(cp) - 1;
1504 	alen--;
1505 	xo_emit("{e:address/%*.*s}{e:port/%*.*s}", alen, alen, line, plen,
1506 	    plen, cp);
1507 
1508 	if (container)
1509 		xo_close_container(container);
1510 }
1511 
1512 /*
1513  * Construct an Internet address representation.
1514  * If numeric_addr has been supplied, give
1515  * numeric value, otherwise try for symbolic name.
1516  */
1517 char *
1518 inetname(struct in_addr *inp)
1519 {
1520 	char *cp;
1521 	static char line[MAXHOSTNAMELEN];
1522 	struct hostent *hp;
1523 
1524 	cp = 0;
1525 	if (!numeric_addr && inp->s_addr != INADDR_ANY) {
1526 		hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
1527 		if (hp) {
1528 			cp = hp->h_name;
1529 			trimdomain(cp, strlen(cp));
1530 		}
1531 	}
1532 	if (inp->s_addr == INADDR_ANY)
1533 		strcpy(line, "*");
1534 	else if (cp) {
1535 		strlcpy(line, cp, sizeof(line));
1536 	} else {
1537 		inp->s_addr = ntohl(inp->s_addr);
1538 #define	C(x)	((u_int)((x) & 0xff))
1539 		snprintf(line, sizeof(line), "%u.%u.%u.%u",
1540 		    C(inp->s_addr >> 24), C(inp->s_addr >> 16),
1541 		    C(inp->s_addr >> 8), C(inp->s_addr));
1542 	}
1543 	return (line);
1544 }
1545 #endif
1546