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