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