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