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