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