xref: /freebsd/usr.bin/netstat/if.c (revision ef0cb5db0af0d5d5b75b74f8e534fe601b7176d7)
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, *xname, buf[IFNAMSIZ+1];
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 ((ifa->ifa_flags & IFF_UP) == 0) {
345 			xname = stpcpy(buf, name);
346 			*xname++ = '*';
347 			*xname = '\0';
348 			xname = buf;
349 		} else
350 			xname = name;
351 
352 		if (Wflag)
353 			xo_emit("{etk:name/%s}{e:flags/0x%x}{d:/%7.7s}",
354 			    name, ifa->ifa_flags, xname);
355 		else
356 			xo_emit("{etk:name/%s}{e:flags/0x%x}{d:/%5.5s}",
357 			    name, ifa->ifa_flags, xname);
358 
359 #define IFA_MTU(ifa)	(((struct if_data *)(ifa)->ifa_data)->ifi_mtu)
360 		show_stat("lu", 6, "mtu", IFA_MTU(ifa), IFA_MTU(ifa));
361 #undef IFA_MTU
362 
363 		switch (ifa->ifa_addr->sa_family) {
364 		case AF_UNSPEC:
365 			xo_emit("{:network/%-13.13s} ", "none");
366 			xo_emit("{:address/%-15.15s} ", "none");
367 			break;
368 		case AF_INET:
369 		    {
370 			struct sockaddr_in *sin, *mask;
371 
372 			sin = (struct sockaddr_in *)ifa->ifa_addr;
373 			mask = (struct sockaddr_in *)ifa->ifa_netmask;
374 			xo_emit("{t:network/%-13.13s} ",
375 			    netname(sin->sin_addr.s_addr,
376 			    mask->sin_addr.s_addr));
377 			xo_emit("{t:address/%-17.17s} ",
378 			    routename(sin->sin_addr.s_addr));
379 
380 			network = true;
381 			break;
382 		    }
383 #ifdef INET6
384 		case AF_INET6:
385 		    {
386 			struct sockaddr_in6 *sin6, *mask;
387 
388 			sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
389 			mask = (struct sockaddr_in6 *)ifa->ifa_netmask;
390 
391 			xo_emit("{t:network/%-13.13s} ",
392 			    netname6(sin6, &mask->sin6_addr));
393 			getnameinfo(ifa->ifa_addr, ifa->ifa_addr->sa_len,
394 			    addr_buf, sizeof(addr_buf), 0, 0, NI_NUMERICHOST);
395 			xo_emit("{t:address/%-17.17s} ", addr_buf);
396 
397 			network = 1;
398 			break;
399 		    }
400 #endif /* INET6 */
401 		case AF_LINK:
402 		    {
403 			struct sockaddr_dl *sdl;
404 			char *cp, linknum[10];
405 			int len = 32;
406 			char buf[len];
407 			int n, z;
408 
409 			sdl = (struct sockaddr_dl *)ifa->ifa_addr;
410 			cp = (char *)LLADDR(sdl);
411 			n = sdl->sdl_alen;
412 			sprintf(linknum, "<Link#%d>", sdl->sdl_index);
413 			xo_emit("{t:network/%-13.13s} ", linknum);
414 			buf[0] = '\0';
415 			z = 0;
416 			while ((--n >= 0) && (z < len)) {
417 				snprintf(buf + z, len - z, "%02x%c",
418 				    *cp++ & 0xff, n > 0 ? ':' : ' ');
419 				z += 3;
420 			}
421 			if (z > 0)
422 				xo_emit("{:address/%*s}", 32 - z, buf);
423 			else
424 				xo_emit("{P:                  }");
425 			link = 1;
426 			break;
427 		    }
428 		}
429 
430 #define	IFA_STAT(s)	(((struct if_data *)ifa->ifa_data)->ifi_ ## s)
431 		show_stat("lu", 8, "received-packets", IFA_STAT(ipackets),
432 		    link|network);
433 		show_stat("lu", 5, "received-errors", IFA_STAT(ierrors), link);
434 		show_stat("lu", 5, "dropped-packets", IFA_STAT(iqdrops), link);
435 		if (bflag)
436 			show_stat("lu", 10, "received-bytes", IFA_STAT(ibytes),
437 			    link|network);
438 		show_stat("lu", 8, "sent-packets", IFA_STAT(opackets),
439 		    link|network);
440 		show_stat("lu", 5, "send-errors", IFA_STAT(oerrors), link);
441 		if (bflag)
442 			show_stat("lu", 10, "sent-bytes", IFA_STAT(obytes),
443 			    link|network);
444 		show_stat("NRSlu", 5, "collisions", IFA_STAT(collisions), link);
445 		if (dflag)
446 			show_stat("LSlu", 5, "dropped-packets",
447 			    IFA_STAT(oqdrops), link);
448 		xo_emit("\n");
449 
450 		if (!aflag) {
451 			xo_close_instance("interface");
452 			continue;
453 		}
454 
455 		/*
456 		 * Print family's multicast addresses.
457 		 */
458 		xo_open_list("multicast-address");
459 		for (ifma = next_ifma(ifmap, ifa->ifa_name,
460 		    ifa->ifa_addr->sa_family);
461 		    ifma != NULL;
462 		    ifma = next_ifma(ifma, ifa->ifa_name,
463 		    ifa->ifa_addr->sa_family)) {
464 			const char *fmt = NULL;
465 
466 			xo_open_instance("multicast-address");
467 			switch (ifma->ifma_addr->sa_family) {
468 			case AF_INET:
469 			    {
470 				struct sockaddr_in *sin;
471 
472 				sin = (struct sockaddr_in *)ifma->ifma_addr;
473 				fmt = routename(sin->sin_addr.s_addr);
474 				break;
475 			    }
476 #ifdef INET6
477 			case AF_INET6:
478 
479 				/* in6_fillscopeid(&msa.in6); */
480 				getnameinfo(ifma->ifma_addr,
481 				    ifma->ifma_addr->sa_len, addr_buf,
482 				    sizeof(addr_buf), 0, 0, NI_NUMERICHOST);
483 				xo_emit("{P:/%*s }{t:address/%-19.19s}",
484 				    Wflag ? 27 : 25, "", addr_buf);
485 				break;
486 #endif /* INET6 */
487 			case AF_LINK:
488 			    {
489 				struct sockaddr_dl *sdl;
490 
491 				sdl = (struct sockaddr_dl *)ifma->ifma_addr;
492 				switch (sdl->sdl_type) {
493 				case IFT_ETHER:
494 				case IFT_FDDI:
495 					fmt = ether_ntoa(
496 					    (struct ether_addr *)LLADDR(sdl));
497 					break;
498 				}
499 				break;
500 			    }
501 			}
502 
503 			if (fmt) {
504 				xo_emit("{P:/%*s }{t:address/%-17.17s/}",
505 				    Wflag ? 27 : 25, "", fmt);
506 				if (ifma->ifma_addr->sa_family == AF_LINK) {
507 					xo_emit(" {:received-packets/%8lu}",
508 					    IFA_STAT(imcasts));
509 					xo_emit("{P:/%*s}", bflag? 17 : 6, "");
510 					xo_emit(" {:sent-packets/%8lu}",
511 					    IFA_STAT(omcasts));
512  				}
513 				xo_emit("\n");
514 			}
515 			xo_close_instance("multicast-address");
516 			ifma = ifma->ifma_next;
517 		}
518 		xo_close_list("multicast-address");
519 		xo_close_instance("interface");
520 	}
521 	xo_close_list("interface");
522 
523 	freeifaddrs(ifap);
524 	if (aflag)
525 		freeifmaddrs(ifmap);
526 }
527 
528 struct iftot {
529 	u_long	ift_ip;			/* input packets */
530 	u_long	ift_ie;			/* input errors */
531 	u_long	ift_id;			/* input drops */
532 	u_long	ift_op;			/* output packets */
533 	u_long	ift_oe;			/* output errors */
534 	u_long	ift_od;			/* output drops */
535 	u_long	ift_co;			/* collisions */
536 	u_long	ift_ib;			/* input bytes */
537 	u_long	ift_ob;			/* output bytes */
538 };
539 
540 /*
541  * Obtain stats for interface(s).
542  */
543 static void
544 fill_iftot(struct iftot *st)
545 {
546 	struct ifaddrs *ifap, *ifa;
547 	bool found = false;
548 
549 	if (getifaddrs(&ifap) != 0)
550 		xo_err(EX_OSERR, "getifaddrs");
551 
552 	bzero(st, sizeof(*st));
553 
554 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
555 		if (ifa->ifa_addr->sa_family != AF_LINK)
556 			continue;
557 		if (interface) {
558 			if (strcmp(ifa->ifa_name, interface) == 0)
559 				found = true;
560 			else
561 				continue;
562 		}
563 
564 		st->ift_ip += IFA_STAT(ipackets);
565 		st->ift_ie += IFA_STAT(ierrors);
566 		st->ift_id += IFA_STAT(iqdrops);
567 		st->ift_ib += IFA_STAT(ibytes);
568 		st->ift_op += IFA_STAT(opackets);
569 		st->ift_oe += IFA_STAT(oerrors);
570 		st->ift_od += IFA_STAT(oqdrops);
571 		st->ift_ob += IFA_STAT(obytes);
572  		st->ift_co += IFA_STAT(collisions);
573 	}
574 
575 	if (interface && found == false)
576 		xo_err(EX_DATAERR, "interface %s not found", interface);
577 
578 	freeifaddrs(ifap);
579 }
580 
581 /*
582  * Set a flag to indicate that a signal from the periodic itimer has been
583  * caught.
584  */
585 static sig_atomic_t signalled;
586 static void
587 catchalarm(int signo __unused)
588 {
589 	signalled = true;
590 }
591 
592 /*
593  * Print a running summary of interface statistics.
594  * Repeat display every interval seconds, showing statistics
595  * collected over that interval.  Assumes that interval is non-zero.
596  * First line printed at top of screen is always cumulative.
597  */
598 static void
599 sidewaysintpr(int interval)
600 {
601 	struct iftot ift[2], *new, *old;
602 	struct itimerval interval_it;
603 	int oldmask, line;
604 
605 	new = &ift[0];
606 	old = &ift[1];
607 	fill_iftot(old);
608 
609 	(void)signal(SIGALRM, catchalarm);
610 	signalled = false;
611 	interval_it.it_interval.tv_sec = interval;
612 	interval_it.it_interval.tv_usec = 0;
613 	interval_it.it_value = interval_it.it_interval;
614 	setitimer(ITIMER_REAL, &interval_it, NULL);
615 	xo_open_list("interface-statistics");
616 
617 banner:
618 	xo_emit("{T:/%17s} {T:/%14s} {T:/%16s}\n", "input",
619 	    interface != NULL ? interface : "(Total)", "output");
620 	xo_emit("{T:/%10s} {T:/%5s} {T:/%5s} {T:/%10s} {T:/%10s} {T:/%5s} "
621 	    "{T:/%10s} {T:/%5s}",
622 	    "packets", "errs", "idrops", "bytes", "packets", "errs", "bytes",
623 	    "colls");
624 	if (dflag)
625 		xo_emit(" {T:/%5.5s}", "drops");
626 	xo_emit("\n");
627 	xo_flush();
628 	line = 0;
629 
630 loop:
631 	if ((noutputs != 0) && (--noutputs == 0)) {
632 		xo_close_list("interface-statistics");
633 		return;
634 	}
635 	oldmask = sigblock(sigmask(SIGALRM));
636 	while (!signalled)
637 		sigpause(0);
638 	signalled = false;
639 	sigsetmask(oldmask);
640 	line++;
641 
642 	fill_iftot(new);
643 
644 	xo_open_instance("stats");
645 	show_stat("lu", 10, "received-packets",
646 	    new->ift_ip - old->ift_ip, 1);
647 	show_stat("lu", 5, "received-errors",
648 	    new->ift_ie - old->ift_ie, 1);
649 	show_stat("lu", 5, "dropped-packets",
650 	    new->ift_id - old->ift_id, 1);
651 	show_stat("lu", 10, "received-bytes",
652 	    new->ift_ib - old->ift_ib, 1);
653 	show_stat("lu", 10, "sent-packets",
654 	    new->ift_op - old->ift_op, 1);
655 	show_stat("lu", 5, "send-errors",
656 	    new->ift_oe - old->ift_oe, 1);
657 	show_stat("lu", 10, "sent-bytes",
658 	    new->ift_ob - old->ift_ob, 1);
659 	show_stat("NRSlu", 5, "collisions",
660 	    new->ift_co - old->ift_co, 1);
661 	if (dflag)
662 		show_stat("LSlu", 5, "dropped-packets",
663 		    new->ift_od - old->ift_od, 1);
664 	xo_close_instance("stats");
665 	xo_emit("\n");
666 	xo_flush();
667 
668 	if (new == &ift[0]) {
669 		new = &ift[1];
670 		old = &ift[0];
671 	} else {
672 		new = &ift[0];
673 		old = &ift[1];
674 	}
675 
676 	if (line == 21)
677 		goto banner;
678 	else
679 		goto loop;
680 
681 	/* NOTREACHED */
682 }
683