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