xref: /freebsd/usr.bin/netstat/if.c (revision 458b591fb0ccbeaf80f7116cd145fe896cbf7421)
1 /*-
2  * Copyright (c) 2013 Gleb Smirnoff <glebius@FreeBSD.org>
3  * Copyright (c) 1983, 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 4. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #if 0
32 #ifndef lint
33 static char sccsid[] = "@(#)if.c	8.3 (Berkeley) 4/28/95";
34 #endif /* not lint */
35 #endif
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/types.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/time.h>
45 
46 #include <net/if.h>
47 #include <net/if_dl.h>
48 #include <net/if_types.h>
49 #include <net/ethernet.h>
50 #include <netinet/in.h>
51 #include <netinet/in_var.h>
52 #include <arpa/inet.h>
53 #ifdef PF
54 #include <net/pfvar.h>
55 #include <net/if_pfsync.h>
56 #endif
57 
58 #include <err.h>
59 #include <errno.h>
60 #include <ifaddrs.h>
61 #include <libutil.h>
62 #ifdef INET6
63 #include <netdb.h>
64 #endif
65 #include <signal.h>
66 #include <stdbool.h>
67 #include <stdint.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <sysexits.h>
72 #include <unistd.h>
73 #include <libxo/xo.h>
74 
75 #include "netstat.h"
76 
77 static void sidewaysintpr(void);
78 
79 #ifdef PF
80 static const char* pfsyncacts[] = {
81 	/* PFSYNC_ACT_CLR */		"clear all request",
82 	/* PFSYNC_ACT_INS */		"state insert",
83 	/* PFSYNC_ACT_INS_ACK */	"state inserted ack",
84 	/* PFSYNC_ACT_UPD */		"state update",
85 	/* PFSYNC_ACT_UPD_C */		"compressed state update",
86 	/* PFSYNC_ACT_UPD_REQ */	"uncompressed state request",
87 	/* PFSYNC_ACT_DEL */		"state delete",
88 	/* PFSYNC_ACT_DEL_C */		"compressed state delete",
89 	/* PFSYNC_ACT_INS_F */		"fragment insert",
90 	/* PFSYNC_ACT_DEL_F */		"fragment delete",
91 	/* PFSYNC_ACT_BUS */		"bulk update mark",
92 	/* PFSYNC_ACT_TDB */		"TDB replay counter update",
93 	/* PFSYNC_ACT_EOF */		"end of frame mark",
94 };
95 
96 static const char* pfsyncacts_name[] = {
97 	/* PFSYNC_ACT_CLR */		"clear-all-request",
98 	/* PFSYNC_ACT_INS */		"state-insert",
99 	/* PFSYNC_ACT_INS_ACK */	"state-inserted-ack",
100 	/* PFSYNC_ACT_UPD */		"state-update",
101 	/* PFSYNC_ACT_UPD_C */		"compressed-state-update",
102 	/* PFSYNC_ACT_UPD_REQ */	"uncompressed-state-request",
103 	/* PFSYNC_ACT_DEL */		"state-delete",
104 	/* PFSYNC_ACT_DEL_C */		"compressed-state-delete",
105 	/* PFSYNC_ACT_INS_F */		"fragment-insert",
106 	/* PFSYNC_ACT_DEL_F */		"fragment-delete",
107 	/* PFSYNC_ACT_BUS */		"bulk-update-mark",
108 	/* PFSYNC_ACT_TDB */		"TDB-replay-counter-update",
109 	/* PFSYNC_ACT_EOF */		"end-of-frame-mark",
110 };
111 
112 static void
113 pfsync_acts_stats(const char *list, const char *desc, uint64_t *a)
114 {
115 	int i;
116 
117 	xo_open_list(list);
118 	for (i = 0; i < PFSYNC_ACT_MAX; i++, a++) {
119 		if (*a || sflag <= 1) {
120 			xo_open_instance(list);
121 			xo_emit("\t\t{e:name}{:count/%ju} {N:/%s%s %s}\n",
122 			    pfsyncacts_name[i], (uintmax_t)(*a),
123 			    pfsyncacts[i], plural(*a), desc);
124 			xo_close_instance(list);
125 		}
126 	}
127 	xo_close_list(list);
128 }
129 
130 /*
131  * Dump pfsync statistics structure.
132  */
133 void
134 pfsync_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
135 {
136 	struct pfsyncstats pfsyncstat;
137 
138 	if (fetch_stats("net.pfsync.stats", off, &pfsyncstat,
139 	    sizeof(pfsyncstat), kread) != 0)
140 		return;
141 
142 	xo_emit("{T:/%s}:\n", name);
143 	xo_open_container(name);
144 
145 #define	p(f, m) if (pfsyncstat.f || sflag <= 1) \
146 	xo_emit(m, (uintmax_t)pfsyncstat.f, plural(pfsyncstat.f))
147 
148 	p(pfsyncs_ipackets, "\t{:received-inet-packets/%ju} "
149 	    "{N:/packet%s received (IPv4)}\n");
150 	p(pfsyncs_ipackets6, "\t{:received-inet6-packets/%ju} "
151 	    "{N:/packet%s received (IPv6)}\n");
152 	pfsync_acts_stats("input-histogram", "received",
153 	    &pfsyncstat.pfsyncs_iacts[0]);
154 	p(pfsyncs_badif, "\t\t/{:dropped-bad-interface/%ju} "
155 	    "{N:/packet%s discarded for bad interface}\n");
156 	p(pfsyncs_badttl, "\t\t{:dropped-bad-ttl/%ju} "
157 	    "{N:/packet%s discarded for bad ttl}\n");
158 	p(pfsyncs_hdrops, "\t\t{:dropped-short-header/%ju} "
159 	    "{N:/packet%s shorter than header}\n");
160 	p(pfsyncs_badver, "\t\t{:dropped-bad-version/%ju} "
161 	    "{N:/packet%s discarded for bad version}\n");
162 	p(pfsyncs_badauth, "\t\t{:dropped-bad-auth/%ju} "
163 	    "{N:/packet%s discarded for bad HMAC}\n");
164 	p(pfsyncs_badact,"\t\t{:dropped-bad-action/%ju} "
165 	    "{N:/packet%s discarded for bad action}\n");
166 	p(pfsyncs_badlen, "\t\t{:dropped-short/%ju} "
167 	    "{N:/packet%s discarded for short packet}\n");
168 	p(pfsyncs_badval, "\t\t{:dropped-bad-values/%ju} "
169 	    "{N:/state%s discarded for bad values}\n");
170 	p(pfsyncs_stale, "\t\t{:dropped-stale-state/%ju} "
171 	    "{N:/stale state%s}\n");
172 	p(pfsyncs_badstate, "\t\t{:dropped-failed-lookup/%ju} "
173 	    "{N:/failed state lookup\\/insert%s}\n");
174 	p(pfsyncs_opackets, "\t{:sent-inet-packets/%ju} "
175 	    "{N:/packet%s sent (IPv4})\n");
176 	p(pfsyncs_opackets6, "\t{:send-inet6-packets/%ju} "
177 	    "{N:/packet%s sent (IPv6})\n");
178 	pfsync_acts_stats("output-histogram", "sent",
179 	    &pfsyncstat.pfsyncs_oacts[0]);
180 	p(pfsyncs_onomem, "\t\t{:discarded-no-memory/%ju} "
181 	    "{N:/failure%s due to mbuf memory error}\n");
182 	p(pfsyncs_oerrors, "\t\t{:send-errors/%ju} "
183 	    "{N:/send error%s}\n");
184 #undef p
185 	xo_close_container(name);
186 }
187 #endif /* PF */
188 
189 /*
190  * Display a formatted value, or a '-' in the same space.
191  */
192 static void
193 show_stat(const char *fmt, int width, const char *name,
194     u_long value, short showvalue, int div1000)
195 {
196 	const char *lsep, *rsep;
197 	char newfmt[64];
198 
199 	lsep = "";
200 	if (strncmp(fmt, "LS", 2) == 0) {
201 		lsep = " ";
202 		fmt += 2;
203 	}
204 	rsep = " ";
205 	if (strncmp(fmt, "NRS", 3) == 0) {
206 		rsep = "";
207 		fmt += 3;
208 	}
209 	if (showvalue == 0) {
210 		/* Print just dash. */
211 		xo_emit("{P:/%s}{D:/%*s}{P:/%s}", lsep, width, "-", rsep);
212 		return;
213 	}
214 
215 	/*
216 	 * XXX: workaround {P:} modifier can't be empty and doesn't seem to
217 	 * take args... so we need to conditionally include it in the format.
218 	 */
219 #define maybe_pad(pad)	do {						    \
220 	if (strlen(pad)) {						    \
221 		snprintf(newfmt, sizeof(newfmt), "{P:%s}", pad);	    \
222 		xo_emit(newfmt);					    \
223 	}								    \
224 } while (0)
225 
226 	if (hflag) {
227 		char buf[5];
228 
229 		/* Format in human readable form. */
230 		humanize_number(buf, sizeof(buf), (int64_t)value, "",
231 		    HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL | \
232 		    ((div1000) ? HN_DIVISOR_1000 : 0));
233 		maybe_pad(lsep);
234 		snprintf(newfmt, sizeof(newfmt), "{:%s/%%%ds}", name, width);
235 		xo_emit(newfmt, buf);
236 		maybe_pad(rsep);
237 	} else {
238 		/* Construct the format string. */
239 		maybe_pad(lsep);
240 		snprintf(newfmt, sizeof(newfmt), "{:%s/%%%d%s}",
241 		    name, width, fmt);
242 		xo_emit(newfmt, value);
243 		maybe_pad(rsep);
244 	}
245 }
246 
247 /*
248  * Find next multiaddr for a given interface name.
249  */
250 static struct ifmaddrs *
251 next_ifma(struct ifmaddrs *ifma, const char *name, const sa_family_t family)
252 {
253 
254 	for(; ifma != NULL; ifma = ifma->ifma_next) {
255 		struct sockaddr_dl *sdl;
256 
257 		sdl = (struct sockaddr_dl *)ifma->ifma_name;
258 		if (ifma->ifma_addr->sa_family == family &&
259 		    strcmp(sdl->sdl_data, name) == 0)
260 			break;
261 	}
262 
263 	return (ifma);
264 }
265 
266 /*
267  * Print a description of the network interfaces.
268  */
269 void
270 intpr(void (*pfunc)(char *), int af)
271 {
272 	struct ifaddrs *ifap, *ifa;
273 	struct ifmaddrs *ifmap, *ifma;
274 	u_int ifn_len_max = 5, ifn_len;
275 
276 	if (interval)
277 		return sidewaysintpr();
278 
279 	if (getifaddrs(&ifap) != 0)
280 		err(EX_OSERR, "getifaddrs");
281 	if (aflag && getifmaddrs(&ifmap) != 0)
282 		err(EX_OSERR, "getifmaddrs");
283 
284 	if (Wflag) {
285 		for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
286 			if (interface != NULL &&
287 			    strcmp(ifa->ifa_name, interface) != 0)
288 				continue;
289 			if (af != AF_UNSPEC && ifa->ifa_addr->sa_family != af)
290 				continue;
291 			ifn_len = strlen(ifa->ifa_name);
292 			if ((ifa->ifa_flags & IFF_UP) == 0)
293 				++ifn_len;
294 			ifn_len_max = MAX(ifn_len_max, ifn_len);
295 		}
296 	}
297 
298 	xo_open_list("interface");
299 	if (!pfunc) {
300 		xo_emit("{T:/%-*.*s}", ifn_len_max, ifn_len_max, "Name");
301 		xo_emit(" {T:/%5.5s} {T:/%-13.13s} {T:/%-17.17s} {T:/%8.8s} "
302 		    "{T:/%5.5s} {T:/%5.5s}",
303 		    "Mtu", "Network", "Address", "Ipkts", "Ierrs", "Idrop");
304 		if (bflag)
305 			xo_emit(" {T:/%10.10s}","Ibytes");
306 		xo_emit(" {T:/%8.8s} {T:/%5.5s}", "Opkts", "Oerrs");
307 		if (bflag)
308 			xo_emit(" {T:/%10.10s}","Obytes");
309 		xo_emit(" {T:/%5s}", "Coll");
310 		if (dflag)
311 			xo_emit(" {T:/%s}", "Drop");
312 		xo_emit("\n");
313 	}
314 
315 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
316 		bool network = false, link = false;
317 		char *name, *xname, buf[IFNAMSIZ+1];
318 
319 		if (interface != NULL && strcmp(ifa->ifa_name, interface) != 0)
320 			continue;
321 
322 		name = ifa->ifa_name;
323 
324 		if (pfunc) {
325 
326 			(*pfunc)(name);
327 
328 			/*
329 			 * Skip all ifaddrs belonging to same interface.
330 			 */
331 			while(ifa->ifa_next != NULL &&
332 			    (strcmp(ifa->ifa_next->ifa_name, name) == 0)) {
333 				ifa = ifa->ifa_next;
334 			}
335 			continue;
336 		}
337 
338 		if (af != AF_UNSPEC && ifa->ifa_addr->sa_family != af)
339 			continue;
340 
341 		xo_open_instance("interface");
342 
343 		if ((ifa->ifa_flags & IFF_UP) == 0) {
344 			xname = stpcpy(buf, name);
345 			*xname++ = '*';
346 			*xname = '\0';
347 			xname = buf;
348 		} else
349 			xname = name;
350 
351 		xo_emit("{etk:name/%s}{e:flags/0x%x}{d:/%-*.*s}",
352 		    name, ifa->ifa_flags, ifn_len_max, ifn_len_max, xname);
353 
354 #define IFA_MTU(ifa)	(((struct if_data *)(ifa)->ifa_data)->ifi_mtu)
355 		show_stat("lu", 6, "mtu", IFA_MTU(ifa), IFA_MTU(ifa), 0);
356 #undef IFA_MTU
357 
358 		switch (ifa->ifa_addr->sa_family) {
359 		case AF_UNSPEC:
360 			xo_emit("{:network/%-13.13s} ", "none");
361 			xo_emit("{:address/%-15.15s} ", "none");
362 			break;
363 		case AF_INET:
364 #ifdef INET6
365 		case AF_INET6:
366 #endif /* INET6 */
367 			if (Wflag) {
368 				xo_emit("{t:network/%-13s} ",
369 				    netname(ifa->ifa_addr, ifa->ifa_netmask));
370 				xo_emit("{t:address/%-17s} ",
371 				    routename(ifa->ifa_addr, numeric_addr));
372 			} else {
373 				xo_emit("{t:network/%-13.13s} ",
374 				    netname(ifa->ifa_addr, ifa->ifa_netmask));
375 				xo_emit("{t:address/%-17.17s} ",
376 				    routename(ifa->ifa_addr, numeric_addr));
377 			}
378 
379 			network = true;
380 			break;
381 		case AF_LINK:
382 		    {
383 			struct sockaddr_dl *sdl;
384 			char linknum[10];
385 
386 			sdl = (struct sockaddr_dl *)ifa->ifa_addr;
387 			sprintf(linknum, "<Link#%d>", sdl->sdl_index);
388 			xo_emit("{t:network/%-13.13s} ", linknum);
389 			if (sdl->sdl_nlen == 0 &&
390 			    sdl->sdl_alen == 0 &&
391 			    sdl->sdl_slen == 0)
392 				xo_emit("{P:                  }");
393 			else
394 				xo_emit("{:address/%-17.17s} ",
395 				    routename(ifa->ifa_addr, 1));
396 			link = true;
397 			break;
398 		    }
399 		}
400 
401 #define	IFA_STAT(s)	(((struct if_data *)ifa->ifa_data)->ifi_ ## s)
402 		show_stat("lu", 8, "received-packets", IFA_STAT(ipackets),
403 		    link|network, 1);
404 		show_stat("lu", 5, "received-errors", IFA_STAT(ierrors),
405 		    link, 1);
406 		show_stat("lu", 5, "dropped-packets", IFA_STAT(iqdrops),
407 		    link, 1);
408 		if (bflag)
409 			show_stat("lu", 10, "received-bytes", IFA_STAT(ibytes),
410 			    link|network, 0);
411 		show_stat("lu", 8, "sent-packets", IFA_STAT(opackets),
412 		    link|network, 1);
413 		show_stat("lu", 5, "send-errors", IFA_STAT(oerrors), link, 1);
414 		if (bflag)
415 			show_stat("lu", 10, "sent-bytes", IFA_STAT(obytes),
416 			    link|network, 0);
417 		show_stat("NRSlu", 5, "collisions", IFA_STAT(collisions),
418 		    link, 1);
419 		if (dflag)
420 			show_stat("LSlu", 5, "dropped-packets",
421 			    IFA_STAT(oqdrops), link, 1);
422 		xo_emit("\n");
423 
424 		if (!aflag) {
425 			xo_close_instance("interface");
426 			continue;
427 		}
428 
429 		/*
430 		 * Print family's multicast addresses.
431 		 */
432 		xo_open_list("multicast-address");
433 		for (ifma = next_ifma(ifmap, ifa->ifa_name,
434 		    ifa->ifa_addr->sa_family);
435 		    ifma != NULL;
436 		    ifma = next_ifma(ifma, ifa->ifa_name,
437 		    ifa->ifa_addr->sa_family)) {
438 			const char *fmt = NULL;
439 
440 			xo_open_instance("multicast-address");
441 			switch (ifma->ifma_addr->sa_family) {
442 			case AF_LINK:
443 			    {
444 				struct sockaddr_dl *sdl;
445 
446 				sdl = (struct sockaddr_dl *)ifma->ifma_addr;
447 				if (sdl->sdl_type != IFT_ETHER &&
448 				    sdl->sdl_type != IFT_FDDI)
449 					break;
450 			    }
451 				/* FALLTHROUGH */
452 			case AF_INET:
453 #ifdef INET6
454 			case AF_INET6:
455 #endif /* INET6 */
456 				fmt = routename(ifma->ifma_addr, numeric_addr);
457 				break;
458 			}
459 			if (fmt) {
460 				if (Wflag)
461 					xo_emit("{P:/%27s }"
462 					    "{t:address/%-17s/}", "", fmt);
463 				else
464 					xo_emit("{P:/%25s }"
465 					    "{t:address/%-17.17s/}", "", fmt);
466 				if (ifma->ifma_addr->sa_family == AF_LINK) {
467 					xo_emit(" {:received-packets/%8lu}",
468 					    IFA_STAT(imcasts));
469 					xo_emit("{P:/%*s}", bflag? 17 : 6, "");
470 					xo_emit(" {:sent-packets/%8lu}",
471 					    IFA_STAT(omcasts));
472  				}
473 				xo_emit("\n");
474 			}
475 			xo_close_instance("multicast-address");
476 			ifma = ifma->ifma_next;
477 		}
478 		xo_close_list("multicast-address");
479 		xo_close_instance("interface");
480 	}
481 	xo_close_list("interface");
482 
483 	freeifaddrs(ifap);
484 	if (aflag)
485 		freeifmaddrs(ifmap);
486 }
487 
488 struct iftot {
489 	u_long	ift_ip;			/* input packets */
490 	u_long	ift_ie;			/* input errors */
491 	u_long	ift_id;			/* input drops */
492 	u_long	ift_op;			/* output packets */
493 	u_long	ift_oe;			/* output errors */
494 	u_long	ift_od;			/* output drops */
495 	u_long	ift_co;			/* collisions */
496 	u_long	ift_ib;			/* input bytes */
497 	u_long	ift_ob;			/* output bytes */
498 };
499 
500 /*
501  * Obtain stats for interface(s).
502  */
503 static void
504 fill_iftot(struct iftot *st)
505 {
506 	struct ifaddrs *ifap, *ifa;
507 	bool found = false;
508 
509 	if (getifaddrs(&ifap) != 0)
510 		xo_err(EX_OSERR, "getifaddrs");
511 
512 	bzero(st, sizeof(*st));
513 
514 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
515 		if (ifa->ifa_addr->sa_family != AF_LINK)
516 			continue;
517 		if (interface) {
518 			if (strcmp(ifa->ifa_name, interface) == 0)
519 				found = true;
520 			else
521 				continue;
522 		}
523 
524 		st->ift_ip += IFA_STAT(ipackets);
525 		st->ift_ie += IFA_STAT(ierrors);
526 		st->ift_id += IFA_STAT(iqdrops);
527 		st->ift_ib += IFA_STAT(ibytes);
528 		st->ift_op += IFA_STAT(opackets);
529 		st->ift_oe += IFA_STAT(oerrors);
530 		st->ift_od += IFA_STAT(oqdrops);
531 		st->ift_ob += IFA_STAT(obytes);
532  		st->ift_co += IFA_STAT(collisions);
533 	}
534 
535 	if (interface && found == false)
536 		xo_err(EX_DATAERR, "interface %s not found", interface);
537 
538 	freeifaddrs(ifap);
539 }
540 
541 /*
542  * Set a flag to indicate that a signal from the periodic itimer has been
543  * caught.
544  */
545 static sig_atomic_t signalled;
546 static void
547 catchalarm(int signo __unused)
548 {
549 	signalled = true;
550 }
551 
552 /*
553  * Print a running summary of interface statistics.
554  * Repeat display every interval seconds, showing statistics
555  * collected over that interval.  Assumes that interval is non-zero.
556  * First line printed at top of screen is always cumulative.
557  */
558 static void
559 sidewaysintpr(void)
560 {
561 	struct iftot ift[2], *new, *old;
562 	struct itimerval interval_it;
563 	int oldmask, line;
564 
565 	new = &ift[0];
566 	old = &ift[1];
567 	fill_iftot(old);
568 
569 	(void)signal(SIGALRM, catchalarm);
570 	signalled = false;
571 	interval_it.it_interval.tv_sec = interval;
572 	interval_it.it_interval.tv_usec = 0;
573 	interval_it.it_value = interval_it.it_interval;
574 	setitimer(ITIMER_REAL, &interval_it, NULL);
575 	xo_open_list("interface-statistics");
576 
577 banner:
578 	xo_emit("{T:/%17s} {T:/%14s} {T:/%16s}\n", "input",
579 	    interface != NULL ? interface : "(Total)", "output");
580 	xo_emit("{T:/%10s} {T:/%5s} {T:/%5s} {T:/%10s} {T:/%10s} {T:/%5s} "
581 	    "{T:/%10s} {T:/%5s}",
582 	    "packets", "errs", "idrops", "bytes", "packets", "errs", "bytes",
583 	    "colls");
584 	if (dflag)
585 		xo_emit(" {T:/%5.5s}", "drops");
586 	xo_emit("\n");
587 	xo_flush();
588 	line = 0;
589 
590 loop:
591 	if ((noutputs != 0) && (--noutputs == 0)) {
592 		xo_close_list("interface-statistics");
593 		return;
594 	}
595 	oldmask = sigblock(sigmask(SIGALRM));
596 	while (!signalled)
597 		sigpause(0);
598 	signalled = false;
599 	sigsetmask(oldmask);
600 	line++;
601 
602 	fill_iftot(new);
603 
604 	xo_open_instance("stats");
605 	show_stat("lu", 10, "received-packets",
606 	    new->ift_ip - old->ift_ip, 1, 1);
607 	show_stat("lu", 5, "received-errors",
608 	    new->ift_ie - old->ift_ie, 1, 1);
609 	show_stat("lu", 5, "dropped-packets",
610 	    new->ift_id - old->ift_id, 1, 1);
611 	show_stat("lu", 10, "received-bytes",
612 	    new->ift_ib - old->ift_ib, 1, 0);
613 	show_stat("lu", 10, "sent-packets",
614 	    new->ift_op - old->ift_op, 1, 1);
615 	show_stat("lu", 5, "send-errors",
616 	    new->ift_oe - old->ift_oe, 1, 1);
617 	show_stat("lu", 10, "sent-bytes",
618 	    new->ift_ob - old->ift_ob, 1, 0);
619 	show_stat("NRSlu", 5, "collisions",
620 	    new->ift_co - old->ift_co, 1, 1);
621 	if (dflag)
622 		show_stat("LSlu", 5, "dropped-packets",
623 		    new->ift_od - old->ift_od, 1, 1);
624 	xo_close_instance("stats");
625 	xo_emit("\n");
626 	xo_flush();
627 
628 	if (new == &ift[0]) {
629 		new = &ift[1];
630 		old = &ift[0];
631 	} else {
632 		new = &ift[0];
633 		old = &ift[1];
634 	}
635 
636 	if (line == 21)
637 		goto banner;
638 	else
639 		goto loop;
640 
641 	/* NOTREACHED */
642 }
643