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