xref: /freebsd/usr.bin/netstat/if.c (revision 3642298923e528d795e3a30ec165d2b469e28b40)
1 /*
2  * Copyright (c) 1983, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #if 0
35 #ifndef lint
36 static char sccsid[] = "@(#)if.c	8.3 (Berkeley) 4/28/95";
37 #endif /* not lint */
38 #endif
39 
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 #include <sys/types.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/sysctl.h>
47 #include <sys/time.h>
48 
49 #include <net/if.h>
50 #include <net/if_var.h>
51 #include <net/if_dl.h>
52 #include <net/if_types.h>
53 #include <net/bridge.h>
54 #include <net/ethernet.h>
55 #include <net/pfvar.h>
56 #include <net/if_pfsync.h>
57 #include <netinet/in.h>
58 #include <netinet/in_var.h>
59 #include <netipx/ipx.h>
60 #include <netipx/ipx_if.h>
61 #include <arpa/inet.h>
62 
63 #include <err.h>
64 #include <errno.h>
65 #include <libutil.h>
66 #include <signal.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <unistd.h>
71 
72 #include "netstat.h"
73 
74 #define	YES	1
75 #define	NO	0
76 
77 static void sidewaysintpr (u_int, u_long);
78 static void catchalarm (int);
79 
80 #ifdef INET6
81 static char ntop_buf[INET6_ADDRSTRLEN];		/* for inet_ntop() */
82 static int bdg_done;
83 #endif
84 
85 /* print bridge statistics */
86 void
87 bdg_stats(u_long dummy __unused, const char *name, int af1 __unused)
88 {
89     int i;
90     size_t slen ;
91     struct bdg_stats s ;
92     int mib[4] ;
93 
94     slen = sizeof(s);
95 
96     mib[0] = CTL_NET ;
97     mib[1] = PF_LINK ;
98     mib[2] = IFT_ETHER ;
99     mib[3] = PF_BDG ;
100     if (sysctl(mib,4, &s,&slen,NULL,0)==-1)
101 	return ; /* no bridging */
102 #ifdef INET6
103     if (bdg_done != 0)
104 	return;
105     else
106 	bdg_done = 1;
107 #endif
108     printf("-- Bridging statistics (%s) --\n", name) ;
109     printf(
110 "Name          In      Out  Forward     Drop    Bcast    Mcast    Local  Unknown\n");
111     for (i = 0 ; i < 16 ; i++) {
112 	if (s.s[i].name[0])
113 	printf("%-6s %9ld%9ld%9ld%9ld%9ld%9ld%9ld%9ld\n",
114 	  s.s[i].name,
115 	  s.s[i].p_in[(int)BDG_IN],
116 	  s.s[i].p_in[(int)BDG_OUT],
117 	  s.s[i].p_in[(int)BDG_FORWARD],
118 	  s.s[i].p_in[(int)BDG_DROP],
119 	  s.s[i].p_in[(int)BDG_BCAST],
120 	  s.s[i].p_in[(int)BDG_MCAST],
121 	  s.s[i].p_in[(int)BDG_LOCAL],
122 	  s.s[i].p_in[(int)BDG_UNKNOWN] );
123     }
124 }
125 
126 /*
127  * Dump pfsync statistics structure.
128  */
129 void
130 pfsync_stats(u_long off __unused, const char *name, int af1 __unused)
131 {
132 	struct pfsyncstats pfsyncstat, zerostat;
133 	size_t len = sizeof(struct pfsyncstats);
134 
135 	if (zflag)
136 		memset(&zerostat, 0, len);
137 	if (sysctlbyname("net.inet.pfsync.stats", &pfsyncstat, &len,
138 	    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
139 		if (errno != ENOENT)
140 			warn("sysctl: net.inet.pfsync.stats");
141 		return;
142 	}
143 
144 	printf("%s:\n", name);
145 
146 #define p(f, m) if (pfsyncstat.f || sflag <= 1) \
147 	printf(m, (unsigned long long)pfsyncstat.f, plural(pfsyncstat.f))
148 #define p2(f, m) if (pfsyncstat.f || sflag <= 1) \
149 	printf(m, (unsigned long long)pfsyncstat.f)
150 
151 	p(pfsyncs_ipackets, "\t%llu packet%s received (IPv4)\n");
152 	p(pfsyncs_ipackets6, "\t%llu packet%s received (IPv6)\n");
153 	p(pfsyncs_badif, "\t\t%llu packet%s discarded for bad interface\n");
154 	p(pfsyncs_badttl, "\t\t%llu packet%s discarded for bad ttl\n");
155 	p(pfsyncs_hdrops, "\t\t%llu packet%s shorter than header\n");
156 	p(pfsyncs_badver, "\t\t%llu packet%s discarded for bad version\n");
157 	p(pfsyncs_badauth, "\t\t%llu packet%s discarded for bad HMAC\n");
158 	p(pfsyncs_badact,"\t\t%llu packet%s discarded for bad action\n");
159 	p(pfsyncs_badlen, "\t\t%llu packet%s discarded for short packet\n");
160 	p(pfsyncs_badval, "\t\t%llu state%s discarded for bad values\n");
161 	p(pfsyncs_stale, "\t\t%llu stale state%s\n");
162 	p(pfsyncs_badstate, "\t\t%llu failed state lookup/insert%s\n");
163 	p(pfsyncs_opackets, "\t%llu packet%s sent (IPv4)\n");
164 	p(pfsyncs_opackets6, "\t%llu packet%s sent (IPv6)\n");
165 	p2(pfsyncs_onomem, "\t\t%llu send failed due to mbuf memory error\n");
166 	p2(pfsyncs_oerrors, "\t\t%llu send error\n");
167 #undef p
168 #undef p2
169 }
170 
171 /*
172  * Display a formatted value, or a '-' in the same space.
173  */
174 static void
175 show_stat(const char *fmt, int width, u_long value, short showvalue)
176 {
177 	char newfmt[32];
178 
179 	if (showvalue == 0) {
180 		/* Print just dash. */
181 		sprintf(newfmt, "%%%ds", width);
182 		printf(newfmt, "-");
183 		return;
184 	}
185 
186 	if (hflag) {
187 		char buf[5];
188 
189 		/* Format in human readable form. */
190 		humanize_number(buf, sizeof(buf), (int64_t)value, "",
191 		    HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
192 		sprintf(newfmt, "%%%ds", width);
193 		printf(newfmt, buf);
194 	} else {
195 		/* Construct the format string. */
196 		sprintf(newfmt, "%%%d%s", width, fmt);
197 		printf(newfmt, value);
198 	}
199 }
200 
201 /*
202  * Print a description of the network interfaces.
203  */
204 void
205 intpr(int _interval, u_long ifnetaddr, void (*pfunc)(char *))
206 {
207 	struct ifnet ifnet;
208 	struct ifnethead ifnethead;
209 	union {
210 		struct ifaddr ifa;
211 		struct in_ifaddr in;
212 #ifdef INET6
213 		struct in6_ifaddr in6;
214 #endif
215 		struct ipx_ifaddr ipx;
216 	} ifaddr;
217 	u_long ifaddraddr;
218 	u_long ifaddrfound;
219 	u_long ifnetfound;
220 	u_long opackets;
221 	u_long ipackets;
222 	u_long obytes;
223 	u_long ibytes;
224 	u_long omcasts;
225 	u_long imcasts;
226 	u_long oerrors;
227 	u_long ierrors;
228 	u_long collisions;
229 	short timer;
230 	int drops;
231 	struct sockaddr *sa = NULL;
232 	char name[IFNAMSIZ];
233 	short network_layer;
234 	short link_layer;
235 
236 	if (ifnetaddr == 0) {
237 		printf("ifnet: symbol not defined\n");
238 		return;
239 	}
240 	if (_interval) {
241 		sidewaysintpr((unsigned)_interval, ifnetaddr);
242 		return;
243 	}
244 	if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead))
245 		return;
246 	ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead);
247 	if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
248 		return;
249 
250 	if (!pfunc) {
251 		if (Wflag)
252 			printf("%-7.7s", "Name");
253 		else
254 			printf("%-5.5s", "Name");
255 		printf(" %5.5s %-13.13s %-17.17s %8.8s %5.5s",
256 		    "Mtu", "Network", "Address", "Ipkts", "Ierrs");
257 		if (bflag)
258 			printf(" %10.10s","Ibytes");
259 		printf(" %8.8s %5.5s", "Opkts", "Oerrs");
260 		if (bflag)
261 			printf(" %10.10s","Obytes");
262 		printf(" %5s", "Coll");
263 		if (tflag)
264 			printf(" %s", "Time");
265 		if (dflag)
266 			printf(" %s", "Drop");
267 		putchar('\n');
268 	}
269 	ifaddraddr = 0;
270 	while (ifnetaddr || ifaddraddr) {
271 		struct sockaddr_in *sockin;
272 #ifdef INET6
273 		struct sockaddr_in6 *sockin6;
274 #endif
275 		char *cp;
276 		int n, m;
277 
278 		network_layer = 0;
279 		link_layer = 0;
280 
281 		if (ifaddraddr == 0) {
282 			ifnetfound = ifnetaddr;
283 			if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
284 				return;
285 			strlcpy(name, ifnet.if_xname, sizeof(name));
286 			ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link);
287 			if (interface != 0 && (strcmp(name, interface) != 0))
288 				continue;
289 			cp = index(name, '\0');
290 
291 			if (pfunc) {
292 				(*pfunc)(name);
293 				continue;
294 			}
295 
296 			if ((ifnet.if_flags&IFF_UP) == 0)
297 				*cp++ = '*';
298 			*cp = '\0';
299 			ifaddraddr = (u_long)TAILQ_FIRST(&ifnet.if_addrhead);
300 		}
301 		ifaddrfound = ifaddraddr;
302 
303 		/*
304 		 * Get the interface stats.  These may get
305 		 * overriden below on a per-interface basis.
306 		 */
307 		opackets = ifnet.if_opackets;
308 		ipackets = ifnet.if_ipackets;
309 		obytes = ifnet.if_obytes;
310 		ibytes = ifnet.if_ibytes;
311 		omcasts = ifnet.if_omcasts;
312 		imcasts = ifnet.if_imcasts;
313 		oerrors = ifnet.if_oerrors;
314 		ierrors = ifnet.if_ierrors;
315 		collisions = ifnet.if_collisions;
316 		timer = ifnet.if_timer;
317 		drops = ifnet.if_snd.ifq_drops;
318 
319 		if (ifaddraddr == 0) {
320 			if (Wflag)
321 				printf("%-7.7s", name);
322 			else
323 				printf("%-5.5s", name);
324 			printf(" %5lu ", ifnet.if_mtu);
325 			printf("%-13.13s ", "none");
326 			printf("%-17.17s ", "none");
327 		} else {
328 			if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
329 				ifaddraddr = 0;
330 				continue;
331 			}
332 #define CP(x) ((char *)(x))
333 			cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
334 				CP(&ifaddr);
335 			sa = (struct sockaddr *)cp;
336 			if (af != AF_UNSPEC && sa->sa_family != af) {
337 				ifaddraddr =
338 				    (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
339 				continue;
340 			}
341 			if (Wflag)
342 				printf("%-7.7s", name);
343 			else
344 				printf("%-5.5s", name);
345 			printf(" %5lu ", ifnet.if_mtu);
346 			switch (sa->sa_family) {
347 			case AF_UNSPEC:
348 				printf("%-13.13s ", "none");
349 				printf("%-15.15s ", "none");
350 				break;
351 			case AF_INET:
352 				sockin = (struct sockaddr_in *)sa;
353 #ifdef notdef
354 				/* can't use inet_makeaddr because kernel
355 				 * keeps nets unshifted.
356 				 */
357 				in = inet_makeaddr(ifaddr.in.ia_subnet,
358 					INADDR_ANY);
359 				printf("%-13.13s ", netname(in.s_addr,
360 				    ifaddr.in.ia_subnetmask));
361 #else
362 				printf("%-13.13s ",
363 				    netname(htonl(ifaddr.in.ia_subnet),
364 				    ifaddr.in.ia_subnetmask));
365 #endif
366 				printf("%-17.17s ",
367 				    routename(sockin->sin_addr.s_addr));
368 
369 				network_layer = 1;
370 				break;
371 #ifdef INET6
372 			case AF_INET6:
373 				sockin6 = (struct sockaddr_in6 *)sa;
374 				printf("%-13.13s ",
375 				       netname6(&ifaddr.in6.ia_addr,
376 						&ifaddr.in6.ia_prefixmask.sin6_addr));
377 				printf("%-17.17s ",
378 				    inet_ntop(AF_INET6,
379 					&sockin6->sin6_addr,
380 					ntop_buf, sizeof(ntop_buf)));
381 
382 				network_layer = 1;
383 				break;
384 #endif /*INET6*/
385 			case AF_IPX:
386 				{
387 				struct sockaddr_ipx *sipx =
388 					(struct sockaddr_ipx *)sa;
389 				u_long net;
390 				char netnum[10];
391 
392 				*(union ipx_net *) &net = sipx->sipx_addr.x_net;
393 				sprintf(netnum, "%lx", (u_long)ntohl(net));
394 				printf("ipx:%-8s  ", netnum);
395 /*				printf("ipx:%-8s ", netname(net, 0L)); */
396 				printf("%-17s ",
397 				    ipx_phost((struct sockaddr *)sipx));
398 				}
399 
400 				network_layer = 1;
401 				break;
402 
403 			case AF_APPLETALK:
404 				printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
405 				printf("%-11.11s  ",atalk_print(sa,0x0b) );
406 				break;
407 			case AF_LINK:
408 				{
409 				struct sockaddr_dl *sdl =
410 					(struct sockaddr_dl *)sa;
411 				char linknum[10];
412 				cp = (char *)LLADDR(sdl);
413 				n = sdl->sdl_alen;
414 				sprintf(linknum, "<Link#%d>", sdl->sdl_index);
415 				m = printf("%-13.13s ", linknum);
416 				}
417 				goto hexprint;
418 			default:
419 				m = printf("(%d)", sa->sa_family);
420 				for (cp = sa->sa_len + (char *)sa;
421 					--cp > sa->sa_data && (*cp == 0);) {}
422 				n = cp - sa->sa_data + 1;
423 				cp = sa->sa_data;
424 			hexprint:
425 				while (--n >= 0)
426 					m += printf("%02x%c", *cp++ & 0xff,
427 						    n > 0 ? ':' : ' ');
428 				m = 32 - m;
429 				while (m-- > 0)
430 					putchar(' ');
431 
432 				link_layer = 1;
433 				break;
434 			}
435 
436 			/*
437 			 * Fixup the statistics for interfaces that
438 			 * update stats for their network addresses
439 			 */
440 			if (network_layer) {
441 				opackets = ifaddr.in.ia_ifa.if_opackets;
442 				ipackets = ifaddr.in.ia_ifa.if_ipackets;
443 				obytes = ifaddr.in.ia_ifa.if_obytes;
444 				ibytes = ifaddr.in.ia_ifa.if_ibytes;
445 			}
446 
447 			ifaddraddr = (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
448 		}
449 
450 		show_stat("lu", 8, ipackets, link_layer|network_layer);
451 		printf(" ");
452 		show_stat("lu", 5, ierrors, link_layer);
453 		printf(" ");
454 		if (bflag) {
455 			show_stat("lu", 10, ibytes, link_layer|network_layer);
456 			printf(" ");
457 		}
458 		show_stat("lu", 8, opackets, link_layer|network_layer);
459 		printf(" ");
460 		show_stat("lu", 5, oerrors, link_layer);
461 		printf(" ");
462 		if (bflag) {
463 			show_stat("lu", 10, obytes, link_layer|network_layer);
464 			printf(" ");
465 		}
466 		show_stat("lu", 5, collisions, link_layer);
467 		if (tflag) {
468 			printf(" ");
469 			show_stat("d", 3, timer, link_layer);
470 		}
471 		if (dflag) {
472 			printf(" ");
473 			show_stat("d", 3, drops, link_layer);
474 		}
475 		putchar('\n');
476 		if (aflag && ifaddrfound) {
477 			/*
478 			 * Print family's multicast addresses
479 			 */
480 			struct ifmultiaddr *multiaddr;
481 			struct ifmultiaddr ifma;
482 			union {
483 				struct sockaddr sa;
484 				struct sockaddr_in in;
485 #ifdef INET6
486 				struct sockaddr_in6 in6;
487 #endif /* INET6 */
488 				struct sockaddr_dl dl;
489 			} msa;
490 			const char *fmt;
491 
492 			TAILQ_FOREACH(multiaddr, &ifnet.if_multiaddrs, ifma_link) {
493 				if (kread((u_long)multiaddr, (char *)&ifma,
494 					  sizeof ifma))
495 					break;
496 				multiaddr = &ifma;
497 				if (kread((u_long)ifma.ifma_addr, (char *)&msa,
498 					  sizeof msa))
499 					break;
500 				if (msa.sa.sa_family != sa->sa_family)
501 					continue;
502 
503 				fmt = 0;
504 				switch (msa.sa.sa_family) {
505 				case AF_INET:
506 					fmt = routename(msa.in.sin_addr.s_addr);
507 					break;
508 #ifdef INET6
509 				case AF_INET6:
510 					printf("%*s %-19.19s(refs: %d)\n",
511 					       Wflag ? 27 : 25, "",
512 					       inet_ntop(AF_INET6,
513 							 &msa.in6.sin6_addr,
514 							 ntop_buf,
515 							 sizeof(ntop_buf)),
516 					       ifma.ifma_refcount);
517 					break;
518 #endif /* INET6 */
519 				case AF_LINK:
520 					switch (msa.dl.sdl_type) {
521 					case IFT_ETHER:
522 					case IFT_FDDI:
523 						fmt = ether_ntoa(
524 							(struct ether_addr *)
525 							LLADDR(&msa.dl));
526 						break;
527 					}
528 					break;
529 				}
530 				if (fmt) {
531 					printf("%*s %-17.17s",
532 					    Wflag ? 27 : 25, "", fmt);
533 					if (msa.sa.sa_family == AF_LINK) {
534 						printf(" %8lu", imcasts);
535 						printf("%*s",
536 						    bflag ? 17 : 6, "");
537 						printf(" %8lu", omcasts);
538 					}
539 					putchar('\n');
540 				}
541 			}
542 		}
543 	}
544 }
545 
546 struct	iftot {
547 	SLIST_ENTRY(iftot) chain;
548 	char	ift_name[IFNAMSIZ];	/* interface name */
549 	u_long	ift_ip;			/* input packets */
550 	u_long	ift_ie;			/* input errors */
551 	u_long	ift_op;			/* output packets */
552 	u_long	ift_oe;			/* output errors */
553 	u_long	ift_co;			/* collisions */
554 	u_int	ift_dr;			/* drops */
555 	u_long	ift_ib;			/* input bytes */
556 	u_long	ift_ob;			/* output bytes */
557 };
558 
559 u_char	signalled;			/* set if alarm goes off "early" */
560 
561 /*
562  * Print a running summary of interface statistics.
563  * Repeat display every interval seconds, showing statistics
564  * collected over that interval.  Assumes that interval is non-zero.
565  * First line printed at top of screen is always cumulative.
566  * XXX - should be rewritten to use ifmib(4).
567  */
568 static void
569 sidewaysintpr(unsigned interval1, u_long off)
570 {
571 	struct ifnet ifnet;
572 	u_long firstifnet;
573 	struct ifnethead ifnethead;
574 	struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
575 	int line;
576 	int oldmask, first;
577 	u_long interesting_off;
578 
579 	if (kread(off, (char *)&ifnethead, sizeof ifnethead))
580 		return;
581 	firstifnet = (u_long)TAILQ_FIRST(&ifnethead);
582 
583 	if ((iftot = malloc(sizeof(struct iftot))) == NULL) {
584 		printf("malloc failed\n");
585 		exit(1);
586 	}
587 	memset(iftot, 0, sizeof(struct iftot));
588 
589 	interesting = NULL;
590 	interesting_off = 0;
591 	for (off = firstifnet, ip = iftot; off;) {
592 		char name[IFNAMSIZ];
593 
594 		if (kread(off, (char *)&ifnet, sizeof ifnet))
595 			break;
596 		strlcpy(name, ifnet.if_xname, sizeof(name));
597 		if (interface && strcmp(name, interface) == 0) {
598 			interesting = ip;
599 			interesting_off = off;
600 		}
601 		snprintf(ip->ift_name, sizeof(ip->ift_name), "(%s)", name);;
602 		if ((ipn = malloc(sizeof(struct iftot))) == NULL) {
603 			printf("malloc failed\n");
604 			exit(1);
605 		}
606 		memset(ipn, 0, sizeof(struct iftot));
607 		SLIST_NEXT(ip, chain) = ipn;
608 		ip = ipn;
609 		off = (u_long)TAILQ_NEXT(&ifnet, if_link);
610 	}
611 	if (interface && interesting == NULL)
612 		errx(1, "%s: unknown interface", interface);
613 	if ((total = malloc(sizeof(struct iftot))) == NULL) {
614 		printf("malloc failed\n");
615 		exit(1);
616 	}
617 	memset(total, 0, sizeof(struct iftot));
618 	if ((sum = malloc(sizeof(struct iftot))) == NULL) {
619 		printf("malloc failed\n");
620 		exit(1);
621 	}
622 	memset(sum, 0, sizeof(struct iftot));
623 
624 	(void)signal(SIGALRM, catchalarm);
625 	signalled = NO;
626 	(void)alarm(interval1);
627 	first = 1;
628 banner:
629 	printf("%17s %14s %16s", "input",
630 	    interesting ? interesting->ift_name : "(Total)", "output");
631 	putchar('\n');
632 	printf("%10s %5s %10s %10s %5s %10s %5s",
633 	    "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
634 	if (dflag)
635 		printf(" %5.5s", "drops");
636 	putchar('\n');
637 	fflush(stdout);
638 	line = 0;
639 loop:
640 	if (interesting != NULL) {
641 		ip = interesting;
642 		if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) {
643 			printf("???\n");
644 			exit(1);
645 		};
646 		if (!first) {
647 			printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
648 				ifnet.if_ipackets - ip->ift_ip,
649 				ifnet.if_ierrors - ip->ift_ie,
650 				ifnet.if_ibytes - ip->ift_ib,
651 				ifnet.if_opackets - ip->ift_op,
652 				ifnet.if_oerrors - ip->ift_oe,
653 				ifnet.if_obytes - ip->ift_ob,
654 				ifnet.if_collisions - ip->ift_co);
655 			if (dflag)
656 				printf(" %5u", ifnet.if_snd.ifq_drops - ip->ift_dr);
657 		}
658 		ip->ift_ip = ifnet.if_ipackets;
659 		ip->ift_ie = ifnet.if_ierrors;
660 		ip->ift_ib = ifnet.if_ibytes;
661 		ip->ift_op = ifnet.if_opackets;
662 		ip->ift_oe = ifnet.if_oerrors;
663 		ip->ift_ob = ifnet.if_obytes;
664 		ip->ift_co = ifnet.if_collisions;
665 		ip->ift_dr = ifnet.if_snd.ifq_drops;
666 	} else {
667 		sum->ift_ip = 0;
668 		sum->ift_ie = 0;
669 		sum->ift_ib = 0;
670 		sum->ift_op = 0;
671 		sum->ift_oe = 0;
672 		sum->ift_ob = 0;
673 		sum->ift_co = 0;
674 		sum->ift_dr = 0;
675 		for (off = firstifnet, ip = iftot;
676 		     off && SLIST_NEXT(ip, chain) != NULL;
677 		     ip = SLIST_NEXT(ip, chain)) {
678 			if (kread(off, (char *)&ifnet, sizeof ifnet)) {
679 				off = 0;
680 				continue;
681 			}
682 			sum->ift_ip += ifnet.if_ipackets;
683 			sum->ift_ie += ifnet.if_ierrors;
684 			sum->ift_ib += ifnet.if_ibytes;
685 			sum->ift_op += ifnet.if_opackets;
686 			sum->ift_oe += ifnet.if_oerrors;
687 			sum->ift_ob += ifnet.if_obytes;
688 			sum->ift_co += ifnet.if_collisions;
689 			sum->ift_dr += ifnet.if_snd.ifq_drops;
690 			off = (u_long)TAILQ_NEXT(&ifnet, if_link);
691 		}
692 		if (!first) {
693 			printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
694 				sum->ift_ip - total->ift_ip,
695 				sum->ift_ie - total->ift_ie,
696 				sum->ift_ib - total->ift_ib,
697 				sum->ift_op - total->ift_op,
698 				sum->ift_oe - total->ift_oe,
699 				sum->ift_ob - total->ift_ob,
700 				sum->ift_co - total->ift_co);
701 			if (dflag)
702 				printf(" %5u", sum->ift_dr - total->ift_dr);
703 		}
704 		*total = *sum;
705 	}
706 	if (!first)
707 		putchar('\n');
708 	fflush(stdout);
709 	oldmask = sigblock(sigmask(SIGALRM));
710 	if (! signalled) {
711 		sigpause(0);
712 	}
713 	sigsetmask(oldmask);
714 	signalled = NO;
715 	(void)alarm(interval1);
716 	line++;
717 	first = 0;
718 	if (line == 21)
719 		goto banner;
720 	else
721 		goto loop;
722 	/*NOTREACHED*/
723 }
724 
725 /*
726  * Called if an interval expires before sidewaysintpr has completed a loop.
727  * Sets a flag to not wait for the alarm.
728  */
729 static void
730 catchalarm(int signo __unused)
731 {
732 	signalled = YES;
733 }
734