xref: /openbsd/usr.bin/netstat/if.c (revision 4cfece93)
1 /*	$OpenBSD: if.c,v 1.76 2019/06/28 13:35:02 deraadt Exp $	*/
2 /*	$NetBSD: if.c,v 1.16.4.2 1996/06/07 21:46:46 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 1988, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>	/* roundup() */
34 #include <sys/types.h>
35 #include <sys/ioctl.h>
36 #include <sys/protosw.h>
37 #include <sys/socket.h>
38 #include <sys/sysctl.h>
39 
40 #include <net/if.h>
41 #include <net/if_dl.h>
42 #include <net/if_types.h>
43 #include <net/route.h>
44 #include <netinet/in.h>
45 #include <netinet/in_var.h>
46 #include <netinet/if_ether.h>
47 #include <arpa/inet.h>
48 
49 #include <err.h>
50 #include <limits.h>
51 #include <signal.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <util.h>
57 
58 #include "netstat.h"
59 
60 static void print_addr(struct sockaddr *, struct sockaddr **, struct if_data *);
61 static void sidewaysintpr(u_int, int);
62 static void catchalarm(int);
63 static void get_rtaddrs(int, struct sockaddr *, struct sockaddr **);
64 static void fetchifs(void);
65 
66 struct iftot;
67 
68 struct if_show_err {
69 	const char *name;
70 	const char *iname;
71 	const char *oname;
72 	uint64_t (*count)(uint64_t, uint64_t);
73 };
74 
75 static uint64_t	if_show_fails(uint64_t, uint64_t);
76 static uint64_t	if_show_errors(uint64_t, uint64_t);
77 static uint64_t	if_show_qdrops(uint64_t, uint64_t);
78 
79 static const struct if_show_err if_show_errs[] = {
80 	[IF_SHOW_FAIL] = { "fails", "Ifail", "Ofail", if_show_fails },
81 	[IF_SHOW_ERRS] = { "errs", "Ierrs", "Oerrs", if_show_errors },
82 	[IF_SHOW_DROP] = { "drops", "Idrop", "Odrop", if_show_qdrops },
83 };
84 static const struct if_show_err *if_errs = if_show_errs;
85 
86 /*
87  * Print a description of the network interfaces.
88  * NOTE: ifnetaddr is the location of the kernel global "ifnet",
89  * which is a TAILQ_HEAD.
90  */
91 void
92 intpr(int interval, int repeatcount)
93 {
94 	struct if_msghdr ifm;
95 	int mib[6] = { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
96 	char name[IFNAMSIZ + 1];	/* + 1 for the '*' */
97 	char *buf = NULL, *next, *lim, *cp;
98 	struct rt_msghdr *rtm;
99 	struct ifa_msghdr *ifam;
100 	struct if_data *ifd;
101 	struct sockaddr *sa, *rti_info[RTAX_MAX];
102 	struct sockaddr_dl *sdl;
103 	u_int64_t total = 0;
104 	size_t len;
105 
106 	if_errs = &if_show_errs[dflag];
107 
108 	if (interval) {
109 		sidewaysintpr((unsigned)interval, repeatcount);
110 		return;
111 	}
112 
113 	len = get_sysctl(mib, 6, &buf);
114 
115 	printf("%-7.7s %-5.5s %-11.11s %-17.17s ",
116 	    "Name", "Mtu", "Network", "Address");
117 	if (bflag)
118 		printf("%10.10s %10.10s", "Ibytes", "Obytes");
119 	else {
120 		printf("%8.8s %5.5s %8.8s %5.5s %5.5s",
121 		    "Ipkts", if_errs->iname,
122 		    "Opkts", if_errs->oname, "Colls");
123 	}
124 	if (tflag)
125 		printf(" %s", "Time");
126 	putchar('\n');
127 
128 	lim = buf + len;
129 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
130 		rtm = (struct rt_msghdr *)next;
131 		if (rtm->rtm_version != RTM_VERSION)
132 			continue;
133 		switch (rtm->rtm_type) {
134 		case RTM_IFINFO:
135 			total = 0;
136 			bcopy(next, &ifm, sizeof ifm);
137 			ifd = &ifm.ifm_data;
138 
139 			sa = (struct sockaddr *)(next + rtm->rtm_hdrlen);
140 			get_rtaddrs(ifm.ifm_addrs, sa, rti_info);
141 
142 			sdl = (struct sockaddr_dl *)rti_info[RTAX_IFP];
143 			if (sdl == NULL || sdl->sdl_family != AF_LINK)
144 				continue;
145 			bzero(name, sizeof(name));
146 			if (sdl->sdl_nlen >= IFNAMSIZ)
147 				memcpy(name, sdl->sdl_data, IFNAMSIZ - 1);
148 			else if (sdl->sdl_nlen > 0)
149 				memcpy(name, sdl->sdl_data, sdl->sdl_nlen);
150 
151 			if (interface != 0 && strcmp(name, interface) != 0)
152 				continue;
153 
154 			/* mark inactive interfaces with a '*' */
155 			cp = strchr(name, '\0');
156 			if ((ifm.ifm_flags & IFF_UP) == 0)
157 				*cp++ = '*';
158 			*cp = '\0';
159 
160 			if (qflag) {
161 				total = ifd->ifi_ibytes + ifd->ifi_obytes +
162 				    ifd->ifi_ipackets +
163 				    ifd->ifi_opackets +
164 				    ifd->ifi_collisions;
165 				total += if_errs->count(ifd->ifi_ierrors,
166 				    ifd->ifi_iqdrops);
167 				total += if_errs->count(ifd->ifi_oerrors,
168 				    ifd->ifi_oqdrops);
169 				if (tflag)
170 					total += 0; // XXX ifnet.if_timer;
171 				if (total == 0)
172 					continue;
173 			}
174 
175 			printf("%-7s %-5d ", name, ifd->ifi_mtu);
176 			print_addr(rti_info[RTAX_IFP], rti_info, ifd);
177 			break;
178 		case RTM_NEWADDR:
179 			if (qflag && total == 0)
180 				continue;
181 			if (interface != 0 && strcmp(name, interface) != 0)
182 				continue;
183 
184 			ifam = (struct ifa_msghdr *)next;
185 			if ((ifam->ifam_addrs & (RTA_NETMASK | RTA_IFA |
186 			    RTA_BRD)) == 0)
187 				break;
188 
189 			sa = (struct sockaddr *)(next + rtm->rtm_hdrlen);
190 			get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
191 
192 			printf("%-7s %-5d ", name, ifd->ifi_mtu);
193 			print_addr(rti_info[RTAX_IFA], rti_info, ifd);
194 			break;
195 		}
196 	}
197 	free(buf);
198 }
199 
200 static void
201 print_addr(struct sockaddr *sa, struct sockaddr **rtinfo, struct if_data *ifd)
202 {
203 	struct sockaddr_dl *sdl;
204 	struct sockaddr_in *sin;
205 	struct sockaddr_in6 *sin6;
206 	char *cp;
207 	int m, n;
208 
209 	switch (sa->sa_family) {
210 	case AF_UNSPEC:
211 		printf("%-11.11s ", "none");
212 		printf("%-17.17s ", "none");
213 		break;
214 	case AF_INET:
215 		sin = (struct sockaddr_in *)sa;
216 		cp = netname4(sin->sin_addr.s_addr,
217 		    ((struct sockaddr_in *)rtinfo[RTAX_NETMASK])->sin_addr.s_addr);
218 		if (vflag)
219 			n = strlen(cp) < 11 ? 11 : strlen(cp);
220 		else
221 			n = 11;
222 		printf("%-*.*s ", n, n, cp);
223 		cp = routename4(sin->sin_addr.s_addr);
224 		if (vflag)
225 			n = strlen(cp) < 17 ? 17 : strlen(cp);
226 		else
227 			n = 17;
228 		printf("%-*.*s ", n, n, cp);
229 
230 		break;
231 	case AF_INET6:
232 		sin6 = (struct sockaddr_in6 *)sa;
233 #ifdef __KAME__
234 		if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
235 			sin6->sin6_scope_id =
236 			    ntohs(*(u_int16_t *)
237 			    &sin6->sin6_addr.s6_addr[2]);
238 			sin6->sin6_addr.s6_addr[2] = 0;
239 			sin6->sin6_addr.s6_addr[3] = 0;
240 		}
241 #endif
242 		cp = netname6(sin6,
243 		    (struct sockaddr_in6 *)rtinfo[RTAX_NETMASK]);
244 		if (vflag)
245 			n = strlen(cp) < 11 ? 11 : strlen(cp);
246 		else
247 			n = 11;
248 		printf("%-*.*s ", n, n, cp);
249 		cp = routename6(sin6);
250 		if (vflag)
251 			n = strlen(cp) < 17 ? 17 : strlen(cp);
252 		else
253 			n = 17;
254 		printf("%-*.*s ", n, n, cp);
255 		break;
256 	case AF_LINK:
257 		sdl = (struct sockaddr_dl *)sa;
258 		m = printf("%-11.11s ", "<Link>");
259 		if (sdl->sdl_type == IFT_ETHER ||
260 		    sdl->sdl_type == IFT_CARP ||
261 		    sdl->sdl_type == IFT_FDDI ||
262 		    sdl->sdl_type == IFT_ISO88025)
263 			printf("%-17.17s ",
264 			    ether_ntoa((struct ether_addr *)LLADDR(sdl)));
265 		else {
266 			cp = (char *)LLADDR(sdl);
267 			n = sdl->sdl_alen;
268 			goto hexprint;
269 		}
270 		break;
271 	default:
272 		m = printf("(%d)", sa->sa_family);
273 		for (cp = sa->sa_len + (char *)sa;
274 			--cp > sa->sa_data && (*cp == 0);) {}
275 		n = cp - sa->sa_data + 1;
276 		cp = sa->sa_data;
277 hexprint:
278 		while (--n >= 0)
279 			m += printf("%x%c", *cp++ & 0xff,
280 				    n > 0 ? '.' : ' ');
281 		m = 30 - m;
282 		while (m-- > 0)
283 			putchar(' ');
284 		break;
285 	}
286 	if (bflag) {
287 		if (hflag) {
288 			char ibytes[FMT_SCALED_STRSIZE];
289 			char obytes[FMT_SCALED_STRSIZE];
290 			fmt_scaled(ifd->ifi_ibytes, ibytes);
291 			fmt_scaled(ifd->ifi_obytes, obytes);
292 			printf("%10s %10s", ibytes, obytes);
293 		} else
294 			printf("%10llu %10llu",
295 			    ifd->ifi_ibytes, ifd->ifi_obytes);
296 	} else
297 		printf("%8llu %5llu %8llu %5llu %5llu",
298 		    ifd->ifi_ipackets,
299 		    if_errs->count(ifd->ifi_ierrors, ifd->ifi_iqdrops),
300 		    ifd->ifi_opackets,
301 		    if_errs->count(ifd->ifi_oerrors, ifd->ifi_oqdrops),
302 		    ifd->ifi_collisions);
303 	if (tflag)
304 		printf(" %4d", 0 /* XXX ifnet.if_timer */);
305 	putchar('\n');
306 }
307 
308 struct	iftot {
309 	char	ift_name[IFNAMSIZ];	/* interface name */
310 	u_int64_t ift_ip;		/* input packets */
311 	u_int64_t ift_ib;		/* input bytes */
312 	u_int64_t ift_ie;		/* input errors */
313 	u_int64_t ift_iq;		/* input qdrops */
314 	u_int64_t ift_op;		/* output packets */
315 	u_int64_t ift_ob;		/* output bytes */
316 	u_int64_t ift_oe;		/* output errors */
317 	u_int64_t ift_oq;		/* output qdrops */
318 	u_int64_t ift_co;		/* collisions */
319 } ip_cur, ip_old, sum_cur, sum_old;
320 
321 volatile sig_atomic_t signalled;	/* set if alarm goes off "early" */
322 
323 /*
324  * Print a running summary of interface statistics.
325  * Repeat display every interval seconds, showing statistics
326  * collected over that interval.  Assumes that interval is non-zero.
327  * First line printed at top of screen is always cumulative.
328  */
329 static void
330 sidewaysintpr(unsigned int interval, int repeatcount)
331 {
332 	sigset_t emptyset;
333 	int line;
334 	char ibytes[FMT_SCALED_STRSIZE];
335 	char obytes[FMT_SCALED_STRSIZE];
336 
337 	fetchifs();
338 	if (ip_cur.ift_name[0] == '\0') {
339 		fprintf(stderr, "%s: %s: unknown interface\n",
340 		    __progname, interface);
341 		exit(1);
342 	}
343 
344 	(void)signal(SIGALRM, catchalarm);
345 	signalled = 0;
346 	(void)alarm(interval);
347 banner:
348 	if (bflag)
349 		printf("%7.7s in %8.8s %6.6s out %5.5s",
350 		    ip_cur.ift_name, " ",
351 		    ip_cur.ift_name, " ");
352 	else
353 		printf("%5.5s in %5.5s%5.5s out %5.5s %5.5s",
354 		    ip_cur.ift_name, " ",
355 		    ip_cur.ift_name, " ", " ");
356 
357 	if (bflag)
358 		printf("  %7.7s in %8.8s %6.6s out %5.5s",
359 		    "total", " ", "total", " ");
360 	else
361 		printf("  %5.5s in %5.5s%5.5s out %5.5s %5.5s",
362 		    "total", " ", "total", " ", " ");
363 	putchar('\n');
364 	if (bflag)
365 		printf("%10.10s %8.8s %10.10s %5.5s",
366 		    "bytes", " ", "bytes", " ");
367 	else
368 		printf("%8.8s %5.5s %8.8s %5.5s %5.5s",
369 		    "packets", if_errs->name,
370 		    "packets", if_errs->name, "colls");
371 
372 	if (bflag)
373 		printf("%10.10s %8.8s %10.10s %5.5s",
374 		    "bytes", " ", "bytes", " ");
375 	else
376 		printf("  %8.8s %5.5s %8.8s %5.5s %5.5s",
377 		    "packets", "errs", "packets", "errs", "colls");
378 	putchar('\n');
379 	fflush(stdout);
380 	line = 0;
381 	bzero(&ip_old, sizeof(ip_old));
382 	bzero(&sum_old, sizeof(sum_old));
383 loop:
384 	bzero(&sum_cur, sizeof(sum_cur));
385 
386 	fetchifs();
387 
388 	if (bflag) {
389 		if (hflag) {
390 			fmt_scaled(ip_cur.ift_ib - ip_old.ift_ib, ibytes);
391 			fmt_scaled(ip_cur.ift_ob - ip_old.ift_ob, obytes);
392 			printf("%10s %8.8s %10s %5.5s",
393 			    ibytes, " ", obytes, " ");
394 		} else
395 			printf("%10llu %8.8s %10llu %5.5s",
396 			    ip_cur.ift_ib - ip_old.ift_ib, " ",
397 			    ip_cur.ift_ob - ip_old.ift_ob, " ");
398 	} else
399 		printf("%8llu %5llu %8llu %5llu %5llu",
400 		    ip_cur.ift_ip - ip_old.ift_ip,
401 		    if_errs->count(ip_cur.ift_ie - ip_old.ift_ie,
402 		     ip_cur.ift_iq - ip_old.ift_iq),
403 		    ip_cur.ift_op - ip_old.ift_op,
404 		    if_errs->count(ip_cur.ift_oe - ip_old.ift_oe,
405 		     ip_cur.ift_oq - ip_old.ift_oq),
406 		    ip_cur.ift_co - ip_old.ift_co);
407 
408 	ip_old = ip_cur;
409 
410 	if (bflag) {
411 		if (hflag) {
412 			fmt_scaled(sum_cur.ift_ib - sum_old.ift_ib, ibytes);
413 			fmt_scaled(sum_cur.ift_ob - sum_old.ift_ob, obytes);
414 			printf("%10s %8.8s %10s %5.5s",
415 			    ibytes, " ", obytes, " ");
416 		} else
417 			printf("%10llu %8.8s %10llu %5.5s",
418 			    sum_cur.ift_ib - sum_old.ift_ib, " ",
419 			    sum_cur.ift_ob - sum_old.ift_ob, " ");
420 	} else
421 		printf("%8llu %5llu %8llu %5llu %5llu",
422 		    sum_cur.ift_ip - sum_old.ift_ip,
423 		    if_errs->count(sum_cur.ift_ie - sum_old.ift_ie,
424 		     sum_cur.ift_iq - sum_old.ift_iq),
425 		    sum_cur.ift_op - sum_old.ift_op,
426 		    if_errs->count(sum_cur.ift_oe - sum_old.ift_oe,
427 		     sum_cur.ift_oq - sum_old.ift_oq),
428 		    sum_cur.ift_co - sum_old.ift_co);
429 
430 	sum_old = sum_cur;
431 
432 	putchar('\n');
433 	fflush(stdout);
434 	if (repeatcount && --repeatcount == 0)
435 		return;
436 	line++;
437 	sigemptyset(&emptyset);
438 	if (!signalled)
439 		sigsuspend(&emptyset);
440 	signalled = 0;
441 	(void)alarm(interval);
442 	if (line == 21 && isatty(STDOUT_FILENO))
443 		goto banner;
444 	goto loop;
445 }
446 
447 /*
448  * Called if an interval expires before sidewaysintpr has completed a loop.
449  * Sets a flag to not wait for the alarm.
450  */
451 /* ARGSUSED */
452 static void
453 catchalarm(int signo)
454 {
455 	signalled = 1;
456 }
457 
458 static void
459 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
460 {
461 	int i;
462 
463 	for (i = 0; i < RTAX_MAX; i++) {
464 		if (addrs & (1 << i)) {
465 			rti_info[i] = sa;
466 			sa = (struct sockaddr *)((char *)(sa) +
467 			    roundup(sa->sa_len, sizeof(long)));
468 		} else
469 			rti_info[i] = NULL;
470 	}
471 }
472 
473 
474 static int
475 isegress(char *name)
476 {
477 	static int s = -1;
478 	int len;
479 	struct ifgroupreq ifgr;
480 	struct ifg_req *ifg;
481 	int rv = 0;
482 
483 	if (s == -1) {
484 		if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
485 			return 0;
486 	}
487 
488 	memset(&ifgr, 0, sizeof(ifgr));
489 	strlcpy(ifgr.ifgr_name, name, IFNAMSIZ);
490 
491 	if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
492 		return 0;
493 	}
494 
495 	len = ifgr.ifgr_len;
496 	ifgr.ifgr_groups = calloc(len, 1);
497 	if (ifgr.ifgr_groups == NULL)
498 		err(1, "getifgroups");
499 	if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
500 		err(1, "SIOCGIFGROUP");
501 
502 	ifg = ifgr.ifgr_groups;
503 	for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
504 		len -= sizeof(struct ifg_req);
505 		if (strcmp(ifg->ifgrq_group, IFG_EGRESS) == 0)
506 			rv = 1;
507 	}
508 
509 	free(ifgr.ifgr_groups);
510 	return rv;
511 }
512 
513 static void
514 fetchifs(void)
515 {
516 	struct if_msghdr ifm;
517 	int mib[6] = { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
518 	struct rt_msghdr *rtm;
519 	struct if_data *ifd;
520 	struct sockaddr *sa, *rti_info[RTAX_MAX];
521 	struct sockaddr_dl *sdl;
522 	char *buf = NULL, *next, *lim;
523 	char name[IFNAMSIZ];
524 	size_t len;
525 	int takeit = 0;
526 	int foundone = 0;
527 
528 	len = get_sysctl(mib, 6, &buf);
529 
530 	memset(&ip_cur, 0, sizeof(ip_cur));
531 	lim = buf + len;
532 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
533 		rtm = (struct rt_msghdr *)next;
534 		if (rtm->rtm_version != RTM_VERSION)
535 			continue;
536 		switch (rtm->rtm_type) {
537 		case RTM_IFINFO:
538 			bcopy(next, &ifm, sizeof ifm);
539 			ifd = &ifm.ifm_data;
540 
541 			sa = (struct sockaddr *)(next + rtm->rtm_hdrlen);
542 			get_rtaddrs(ifm.ifm_addrs, sa, rti_info);
543 
544 			sdl = (struct sockaddr_dl *)rti_info[RTAX_IFP];
545 			if (sdl == NULL || sdl->sdl_family != AF_LINK)
546 				continue;
547 			bzero(name, sizeof(name));
548 			if (sdl->sdl_nlen >= IFNAMSIZ)
549 				memcpy(name, sdl->sdl_data, IFNAMSIZ - 1);
550 			else if (sdl->sdl_nlen > 0)
551 				memcpy(name, sdl->sdl_data, sdl->sdl_nlen);
552 
553 			if (interface != NULL && !strcmp(name, interface)) {
554 				takeit = 1;
555 			} else if (interface == NULL && foundone == 0 &&
556 			    isegress(name)) {
557 				takeit = 1;
558 				foundone = 1;
559 			} else
560 				takeit = 0;
561 			if (takeit) {
562 				strlcpy(ip_cur.ift_name, name,
563 				    sizeof(ip_cur.ift_name));
564 				ip_cur.ift_ip = ifd->ifi_ipackets;
565 				ip_cur.ift_ib = ifd->ifi_ibytes;
566 				ip_cur.ift_ie = ifd->ifi_ierrors;
567 				ip_cur.ift_iq = ifd->ifi_iqdrops;
568 				ip_cur.ift_op = ifd->ifi_opackets;
569 				ip_cur.ift_ob = ifd->ifi_obytes;
570 				ip_cur.ift_oe = ifd->ifi_oerrors;
571 				ip_cur.ift_oq = ifd->ifi_oqdrops;
572 				ip_cur.ift_co = ifd->ifi_collisions;
573 			}
574 
575 			sum_cur.ift_ip += ifd->ifi_ipackets;
576 			sum_cur.ift_ib += ifd->ifi_ibytes;
577 			sum_cur.ift_ie += ifd->ifi_ierrors;
578 			sum_cur.ift_iq += ifd->ifi_iqdrops;
579 			sum_cur.ift_op += ifd->ifi_opackets;
580 			sum_cur.ift_ob += ifd->ifi_obytes;
581 			sum_cur.ift_oe += ifd->ifi_oerrors;
582 			sum_cur.ift_oq += ifd->ifi_oqdrops;
583 			sum_cur.ift_co += ifd->ifi_collisions;
584 			break;
585 		}
586 	}
587 	if (interface == NULL && foundone == 0) {
588 		strlcpy(ip_cur.ift_name, name,
589 		    sizeof(ip_cur.ift_name));
590 		ip_cur.ift_ip = ifd->ifi_ipackets;
591 		ip_cur.ift_ib = ifd->ifi_ibytes;
592 		ip_cur.ift_ie = ifd->ifi_ierrors;
593 		ip_cur.ift_iq = ifd->ifi_iqdrops;
594 		ip_cur.ift_op = ifd->ifi_opackets;
595 		ip_cur.ift_ob = ifd->ifi_obytes;
596 		ip_cur.ift_oe = ifd->ifi_oerrors;
597 		ip_cur.ift_oq = ifd->ifi_oqdrops;
598 		ip_cur.ift_co = ifd->ifi_collisions;
599 	}
600 	free(buf);
601 }
602 
603 static uint64_t
604 if_show_fails(uint64_t errors, uint64_t qdrops)
605 {
606 	return (errors + qdrops);
607 }
608 
609 static uint64_t
610 if_show_errors(uint64_t errors, uint64_t qdrops)
611 {
612 	return (errors);
613 }
614 
615 static uint64_t
616 if_show_qdrops(uint64_t errors, uint64_t qdrops)
617 {
618 	return (qdrops);
619 }
620