xref: /freebsd/usr.bin/netstat/if.c (revision 78007886c995898a9494648343e5236bca1cbba3)
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/ethernet.h>
54 #include <net/pfvar.h>
55 #include <net/if_pfsync.h>
56 #include <netinet/in.h>
57 #include <netinet/in_var.h>
58 #include <netipx/ipx.h>
59 #include <netipx/ipx_if.h>
60 #include <arpa/inet.h>
61 
62 #include <err.h>
63 #include <errno.h>
64 #include <libutil.h>
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 ntop_buf[INET6_ADDRSTRLEN];		/* for inet_ntop() */
82 #endif
83 
84 /*
85  * Dump pfsync statistics structure.
86  */
87 void
88 pfsync_stats(u_long off __unused, const char *name, int af1 __unused)
89 {
90 	struct pfsyncstats pfsyncstat, zerostat;
91 	size_t len = sizeof(struct pfsyncstats);
92 
93 	if (zflag)
94 		memset(&zerostat, 0, len);
95 	if (sysctlbyname("net.inet.pfsync.stats", &pfsyncstat, &len,
96 	    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
97 		if (errno != ENOENT)
98 			warn("sysctl: net.inet.pfsync.stats");
99 		return;
100 	}
101 
102 	printf("%s:\n", name);
103 
104 #define p(f, m) if (pfsyncstat.f || sflag <= 1) \
105 	printf(m, (uintmax_t)pfsyncstat.f, plural(pfsyncstat.f))
106 #define p2(f, m) if (pfsyncstat.f || sflag <= 1) \
107 	printf(m, (uintmax_t)pfsyncstat.f)
108 
109 	p(pfsyncs_ipackets, "\t%ju packet%s received (IPv4)\n");
110 	p(pfsyncs_ipackets6, "\t%ju packet%s received (IPv6)\n");
111 	p(pfsyncs_badif, "\t\t%ju packet%s discarded for bad interface\n");
112 	p(pfsyncs_badttl, "\t\t%ju packet%s discarded for bad ttl\n");
113 	p(pfsyncs_hdrops, "\t\t%ju packet%s shorter than header\n");
114 	p(pfsyncs_badver, "\t\t%ju packet%s discarded for bad version\n");
115 	p(pfsyncs_badauth, "\t\t%ju packet%s discarded for bad HMAC\n");
116 	p(pfsyncs_badact,"\t\t%ju packet%s discarded for bad action\n");
117 	p(pfsyncs_badlen, "\t\t%ju packet%s discarded for short packet\n");
118 	p(pfsyncs_badval, "\t\t%ju state%s discarded for bad values\n");
119 	p(pfsyncs_stale, "\t\t%ju stale state%s\n");
120 	p(pfsyncs_badstate, "\t\t%ju failed state lookup/insert%s\n");
121 	p(pfsyncs_opackets, "\t%ju packet%s sent (IPv4)\n");
122 	p(pfsyncs_opackets6, "\t%ju packet%s sent (IPv6)\n");
123 	p2(pfsyncs_onomem, "\t\t%ju send failed due to mbuf memory error\n");
124 	p2(pfsyncs_oerrors, "\t\t%ju send error\n");
125 #undef p
126 #undef p2
127 }
128 
129 /*
130  * Display a formatted value, or a '-' in the same space.
131  */
132 static void
133 show_stat(const char *fmt, int width, u_long value, short showvalue)
134 {
135 	const char *lsep, *rsep;
136 	char newfmt[32];
137 
138 	lsep = "";
139 	if (strncmp(fmt, "LS", 2) == 0) {
140 		lsep = " ";
141 		fmt += 2;
142 	}
143 	rsep = " ";
144 	if (strncmp(fmt, "NRS", 3) == 0) {
145 		rsep = "";
146 		fmt += 3;
147 	}
148 	if (showvalue == 0) {
149 		/* Print just dash. */
150 		sprintf(newfmt, "%s%%%ds%s", lsep, width, rsep);
151 		printf(newfmt, "-");
152 		return;
153 	}
154 
155 	if (hflag) {
156 		char buf[5];
157 
158 		/* Format in human readable form. */
159 		humanize_number(buf, sizeof(buf), (int64_t)value, "",
160 		    HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
161 		sprintf(newfmt, "%s%%%ds%s", lsep, width, rsep);
162 		printf(newfmt, buf);
163 	} else {
164 		/* Construct the format string. */
165 		sprintf(newfmt, "%s%%%d%s%s", lsep, width, fmt, rsep);
166 		printf(newfmt, value);
167 	}
168 }
169 
170 /*
171  * Print a description of the network interfaces.
172  */
173 void
174 intpr(int interval1, u_long ifnetaddr, void (*pfunc)(char *))
175 {
176 	struct ifnet ifnet;
177 	struct ifnethead ifnethead;
178 	union {
179 		struct ifaddr ifa;
180 		struct in_ifaddr in;
181 #ifdef INET6
182 		struct in6_ifaddr in6;
183 #endif
184 		struct ipx_ifaddr ipx;
185 	} ifaddr;
186 	u_long ifaddraddr;
187 	u_long ifaddrfound;
188 	u_long ifnetfound;
189 	u_long opackets;
190 	u_long ipackets;
191 	u_long obytes;
192 	u_long ibytes;
193 	u_long omcasts;
194 	u_long imcasts;
195 	u_long oerrors;
196 	u_long ierrors;
197 	u_long collisions;
198 	short timer;
199 	int drops;
200 	struct sockaddr *sa = NULL;
201 	char name[IFNAMSIZ];
202 	short network_layer;
203 	short link_layer;
204 
205 	if (ifnetaddr == 0) {
206 		printf("ifnet: symbol not defined\n");
207 		return;
208 	}
209 	if (interval1) {
210 		sidewaysintpr(interval1, ifnetaddr);
211 		return;
212 	}
213 	if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead))
214 		return;
215 	ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead);
216 	if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
217 		return;
218 
219 	if (!pfunc) {
220 		if (Wflag)
221 			printf("%-7.7s", "Name");
222 		else
223 			printf("%-5.5s", "Name");
224 		printf(" %5.5s %-13.13s %-17.17s %8.8s %5.5s",
225 		    "Mtu", "Network", "Address", "Ipkts", "Ierrs");
226 		if (bflag)
227 			printf(" %10.10s","Ibytes");
228 		printf(" %8.8s %5.5s", "Opkts", "Oerrs");
229 		if (bflag)
230 			printf(" %10.10s","Obytes");
231 		printf(" %5s", "Coll");
232 		if (tflag)
233 			printf(" %s", "Time");
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 			ifnetfound = ifnetaddr;
252 			if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
253 				return;
254 			strlcpy(name, ifnet.if_xname, sizeof(name));
255 			ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link);
256 			if (interface != 0 && (strcmp(name, interface) != 0))
257 				continue;
258 			cp = index(name, '\0');
259 
260 			if (pfunc) {
261 				(*pfunc)(name);
262 				continue;
263 			}
264 
265 			if ((ifnet.if_flags&IFF_UP) == 0)
266 				*cp++ = '*';
267 			*cp = '\0';
268 			ifaddraddr = (u_long)TAILQ_FIRST(&ifnet.if_addrhead);
269 		}
270 		ifaddrfound = ifaddraddr;
271 
272 		/*
273 		 * Get the interface stats.  These may get
274 		 * overriden below on a per-interface basis.
275 		 */
276 		opackets = ifnet.if_opackets;
277 		ipackets = ifnet.if_ipackets;
278 		obytes = ifnet.if_obytes;
279 		ibytes = ifnet.if_ibytes;
280 		omcasts = ifnet.if_omcasts;
281 		imcasts = ifnet.if_imcasts;
282 		oerrors = ifnet.if_oerrors;
283 		ierrors = ifnet.if_ierrors;
284 		collisions = ifnet.if_collisions;
285 		timer = ifnet.if_timer;
286 		drops = ifnet.if_snd.ifq_drops;
287 
288 		if (ifaddraddr == 0) {
289 			if (Wflag)
290 				printf("%-7.7s", name);
291 			else
292 				printf("%-5.5s", name);
293 			printf(" %5lu ", ifnet.if_mtu);
294 			printf("%-13.13s ", "none");
295 			printf("%-17.17s ", "none");
296 		} else {
297 			if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
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 				printf("%-13.13s ",
344 				       netname6(&ifaddr.in6.ia_addr,
345 						&ifaddr.in6.ia_prefixmask.sin6_addr));
346 				printf("%-17.17s ",
347 				    inet_ntop(AF_INET6,
348 					&sockin6->sin6_addr,
349 					ntop_buf, sizeof(ntop_buf)));
350 
351 				network_layer = 1;
352 				break;
353 #endif /*INET6*/
354 			case AF_IPX:
355 				{
356 				struct sockaddr_ipx *sipx =
357 					(struct sockaddr_ipx *)sa;
358 				u_long net;
359 				char netnum[10];
360 
361 				*(union ipx_net *) &net = sipx->sipx_addr.x_net;
362 				sprintf(netnum, "%lx", (u_long)ntohl(net));
363 				printf("ipx:%-8s  ", netnum);
364 /*				printf("ipx:%-8s ", netname(net, 0L)); */
365 				printf("%-17s ",
366 				    ipx_phost((struct sockaddr *)sipx));
367 				}
368 
369 				network_layer = 1;
370 				break;
371 
372 			case AF_APPLETALK:
373 				printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
374 				printf("%-11.11s  ",atalk_print(sa,0x0b) );
375 				break;
376 			case AF_LINK:
377 				{
378 				struct sockaddr_dl *sdl =
379 					(struct sockaddr_dl *)sa;
380 				char linknum[10];
381 				cp = (char *)LLADDR(sdl);
382 				n = sdl->sdl_alen;
383 				sprintf(linknum, "<Link#%d>", sdl->sdl_index);
384 				m = printf("%-13.13s ", linknum);
385 				}
386 				goto hexprint;
387 			default:
388 				m = printf("(%d)", sa->sa_family);
389 				for (cp = sa->sa_len + (char *)sa;
390 					--cp > sa->sa_data && (*cp == 0);) {}
391 				n = cp - sa->sa_data + 1;
392 				cp = sa->sa_data;
393 			hexprint:
394 				while (--n >= 0)
395 					m += printf("%02x%c", *cp++ & 0xff,
396 						    n > 0 ? ':' : ' ');
397 				m = 32 - m;
398 				while (m-- > 0)
399 					putchar(' ');
400 
401 				link_layer = 1;
402 				break;
403 			}
404 
405 			/*
406 			 * Fixup the statistics for interfaces that
407 			 * update stats for their network addresses
408 			 */
409 			if (network_layer) {
410 				opackets = ifaddr.in.ia_ifa.if_opackets;
411 				ipackets = ifaddr.in.ia_ifa.if_ipackets;
412 				obytes = ifaddr.in.ia_ifa.if_obytes;
413 				ibytes = ifaddr.in.ia_ifa.if_ibytes;
414 			}
415 
416 			ifaddraddr = (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
417 		}
418 
419 		show_stat("lu", 8, ipackets, link_layer|network_layer);
420 		show_stat("lu", 5, ierrors, link_layer);
421 		if (bflag)
422 			show_stat("lu", 10, ibytes, link_layer|network_layer);
423 
424 		show_stat("lu", 8, opackets, link_layer|network_layer);
425 		show_stat("lu", 5, oerrors, link_layer);
426 		if (bflag)
427 			show_stat("lu", 10, obytes, link_layer|network_layer);
428 
429 		show_stat("NRSlu", 5, collisions, link_layer);
430 		if (tflag)
431 			show_stat("LSd", 4, timer, 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))
455 					break;
456 				multiaddr = &ifma;
457 				if (kread((u_long)ifma.ifma_addr, (char *)&msa,
458 					  sizeof msa))
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 					printf("%*s %-19.19s(refs: %d)\n",
471 					       Wflag ? 27 : 25, "",
472 					       inet_ntop(AF_INET6,
473 							 &msa.in6.sin6_addr,
474 							 ntop_buf,
475 							 sizeof(ntop_buf)),
476 					       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_op;			/* output packets */
512 	u_long	ift_oe;			/* output errors */
513 	u_long	ift_co;			/* collisions */
514 	u_int	ift_dr;			/* drops */
515 	u_long	ift_ib;			/* input bytes */
516 	u_long	ift_ob;			/* output bytes */
517 };
518 
519 u_char	signalled;			/* set if alarm goes off "early" */
520 
521 /*
522  * Print a running summary of interface statistics.
523  * Repeat display every interval1 seconds, showing statistics
524  * collected over that interval.  Assumes that interval1 is non-zero.
525  * First line printed at top of screen is always cumulative.
526  * XXX - should be rewritten to use ifmib(4).
527  */
528 static void
529 sidewaysintpr(int interval1, u_long off)
530 {
531 	struct ifnet ifnet;
532 	u_long firstifnet;
533 	struct ifnethead ifnethead;
534 	struct itimerval interval_it;
535 	struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
536 	int line;
537 	int oldmask, first;
538 	u_long interesting_off;
539 
540 	if (kread(off, (char *)&ifnethead, sizeof ifnethead))
541 		return;
542 	firstifnet = (u_long)TAILQ_FIRST(&ifnethead);
543 
544 	if ((iftot = malloc(sizeof(struct iftot))) == NULL) {
545 		printf("malloc failed\n");
546 		exit(1);
547 	}
548 	memset(iftot, 0, sizeof(struct iftot));
549 
550 	interesting = NULL;
551 	interesting_off = 0;
552 	for (off = firstifnet, ip = iftot; off;) {
553 		char name[IFNAMSIZ];
554 
555 		if (kread(off, (char *)&ifnet, sizeof ifnet))
556 			break;
557 		strlcpy(name, ifnet.if_xname, sizeof(name));
558 		if (interface && strcmp(name, interface) == 0) {
559 			interesting = ip;
560 			interesting_off = off;
561 		}
562 		snprintf(ip->ift_name, sizeof(ip->ift_name), "(%s)", name);;
563 		if ((ipn = malloc(sizeof(struct iftot))) == NULL) {
564 			printf("malloc failed\n");
565 			exit(1);
566 		}
567 		memset(ipn, 0, sizeof(struct iftot));
568 		SLIST_NEXT(ip, chain) = ipn;
569 		ip = ipn;
570 		off = (u_long)TAILQ_NEXT(&ifnet, if_link);
571 	}
572 	if (interface && interesting == NULL)
573 		errx(1, "%s: unknown interface", interface);
574 	if ((total = malloc(sizeof(struct iftot))) == NULL) {
575 		printf("malloc failed\n");
576 		exit(1);
577 	}
578 	memset(total, 0, sizeof(struct iftot));
579 	if ((sum = malloc(sizeof(struct iftot))) == NULL) {
580 		printf("malloc failed\n");
581 		exit(1);
582 	}
583 	memset(sum, 0, sizeof(struct iftot));
584 
585 	(void)signal(SIGALRM, catchalarm);
586 	signalled = NO;
587 	interval_it.it_interval.tv_sec = interval1;
588 	interval_it.it_interval.tv_usec = 0;
589 	interval_it.it_value = interval_it.it_interval;
590 	setitimer(ITIMER_REAL, &interval_it, NULL);
591 	first = 1;
592 banner:
593 	printf("%17s %14s %16s", "input",
594 	    interesting ? interesting->ift_name : "(Total)", "output");
595 	putchar('\n');
596 	printf("%10s %5s %10s %10s %5s %10s %5s",
597 	    "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
598 	if (dflag)
599 		printf(" %5.5s", "drops");
600 	putchar('\n');
601 	fflush(stdout);
602 	line = 0;
603 loop:
604 	if (interesting != NULL) {
605 		ip = interesting;
606 		if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) {
607 			printf("???\n");
608 			exit(1);
609 		};
610 		if (!first) {
611 			show_stat("lu", 10, ifnet.if_ipackets - ip->ift_ip, 1);
612 			show_stat("lu", 5, ifnet.if_ierrors - ip->ift_ie, 1);
613 			show_stat("lu", 10, ifnet.if_ibytes - ip->ift_ib, 1);
614 			show_stat("lu", 10, ifnet.if_opackets - ip->ift_op, 1);
615 			show_stat("lu", 5, ifnet.if_oerrors - ip->ift_oe, 1);
616 			show_stat("lu", 10, ifnet.if_obytes - ip->ift_ob, 1);
617 			show_stat("NRSlu", 5,
618 			    ifnet.if_collisions - ip->ift_co, 1);
619 			if (dflag)
620 				show_stat("LSu", 5,
621 				    ifnet.if_snd.ifq_drops - ip->ift_dr, 1);
622 		}
623 		ip->ift_ip = ifnet.if_ipackets;
624 		ip->ift_ie = ifnet.if_ierrors;
625 		ip->ift_ib = ifnet.if_ibytes;
626 		ip->ift_op = ifnet.if_opackets;
627 		ip->ift_oe = ifnet.if_oerrors;
628 		ip->ift_ob = ifnet.if_obytes;
629 		ip->ift_co = ifnet.if_collisions;
630 		ip->ift_dr = ifnet.if_snd.ifq_drops;
631 	} else {
632 		sum->ift_ip = 0;
633 		sum->ift_ie = 0;
634 		sum->ift_ib = 0;
635 		sum->ift_op = 0;
636 		sum->ift_oe = 0;
637 		sum->ift_ob = 0;
638 		sum->ift_co = 0;
639 		sum->ift_dr = 0;
640 		for (off = firstifnet, ip = iftot;
641 		     off && SLIST_NEXT(ip, chain) != NULL;
642 		     ip = SLIST_NEXT(ip, chain)) {
643 			if (kread(off, (char *)&ifnet, sizeof ifnet)) {
644 				off = 0;
645 				continue;
646 			}
647 			sum->ift_ip += ifnet.if_ipackets;
648 			sum->ift_ie += ifnet.if_ierrors;
649 			sum->ift_ib += ifnet.if_ibytes;
650 			sum->ift_op += ifnet.if_opackets;
651 			sum->ift_oe += ifnet.if_oerrors;
652 			sum->ift_ob += ifnet.if_obytes;
653 			sum->ift_co += ifnet.if_collisions;
654 			sum->ift_dr += ifnet.if_snd.ifq_drops;
655 			off = (u_long)TAILQ_NEXT(&ifnet, if_link);
656 		}
657 		if (!first) {
658 			show_stat("lu", 10, sum->ift_ip - total->ift_ip, 1);
659 			show_stat("lu", 5, sum->ift_ie - total->ift_ie, 1);
660 			show_stat("lu", 10, sum->ift_ib - total->ift_ib, 1);
661 			show_stat("lu", 10, sum->ift_op - total->ift_op, 1);
662 			show_stat("lu", 5, sum->ift_oe - total->ift_oe, 1);
663 			show_stat("lu", 10, sum->ift_ob - total->ift_ob, 1);
664 			show_stat("NRSlu", 5, sum->ift_co - total->ift_co, 1);
665 			if (dflag)
666 				show_stat("LSu", 5,
667 				    sum->ift_dr - total->ift_dr, 1);
668 		}
669 		*total = *sum;
670 	}
671 	if (!first)
672 		putchar('\n');
673 	fflush(stdout);
674 	oldmask = sigblock(sigmask(SIGALRM));
675 	while (!signalled)
676 		sigpause(0);
677 	signalled = NO;
678 	sigsetmask(oldmask);
679 	line++;
680 	first = 0;
681 	if (line == 21)
682 		goto banner;
683 	else
684 		goto loop;
685 	/*NOTREACHED*/
686 }
687 
688 /*
689  * Set a flag to indicate that a signal from the periodic itimer has been
690  * caught.
691  */
692 static void
693 catchalarm(int signo __unused)
694 {
695 	signalled = YES;
696 }
697