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