Annotation of src/usr.bin/netstat/if.c, Revision 1.75
1.75 ! dlg 1: /* $OpenBSD: if.c,v 1.74 2015/10/05 15:40:39 uebayasi Exp $ */
1.6 deraadt 2: /* $NetBSD: if.c,v 1.16.4.2 1996/06/07 21:46:46 thorpej Exp $ */
1.1 deraadt 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.
1.35 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 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:
1.70 deraadt 33: #include <sys/param.h> /* roundup() */
1.1 deraadt 34: #include <sys/types.h>
1.63 tedu 35: #include <sys/ioctl.h>
1.1 deraadt 36: #include <sys/protosw.h>
37: #include <sys/socket.h>
1.56 claudio 38: #include <sys/sysctl.h>
1.1 deraadt 39:
40: #include <net/if.h>
41: #include <net/if_dl.h>
1.5 deraadt 42: #include <net/if_types.h>
1.56 claudio 43: #include <net/route.h>
1.1 deraadt 44: #include <netinet/in.h>
45: #include <netinet/in_var.h>
1.11 millert 46: #include <netinet/if_ether.h>
1.1 deraadt 47: #include <arpa/inet.h>
48:
1.60 chl 49: #include <err.h>
1.13 millert 50: #include <limits.h>
1.1 deraadt 51: #include <signal.h>
52: #include <stdio.h>
1.36 david 53: #include <stdlib.h>
1.1 deraadt 54: #include <string.h>
55: #include <unistd.h>
1.65 tedu 56: #include <util.h>
1.1 deraadt 57:
58: #include "netstat.h"
59:
1.56 claudio 60: static void print_addr(struct sockaddr *, struct sockaddr **, struct if_data *);
1.62 tedu 61: static void sidewaysintpr(u_int, int);
1.29 millert 62: static void catchalarm(int);
1.56 claudio 63: static void get_rtaddrs(int, struct sockaddr *, struct sockaddr **);
64: static void fetchifs(void);
1.1 deraadt 65:
1.75 ! dlg 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:
1.1 deraadt 86: /*
87: * Print a description of the network interfaces.
1.3 deraadt 88: * NOTE: ifnetaddr is the location of the kernel global "ifnet",
89: * which is a TAILQ_HEAD.
1.1 deraadt 90: */
91: void
1.62 tedu 92: intpr(int interval, int repeatcount)
1.1 deraadt 93: {
1.56 claudio 94: struct if_msghdr ifm;
1.68 guenther 95: int mib[6] = { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
1.56 claudio 96: char name[IFNAMSIZ + 1]; /* + 1 for the '*' */
1.72 claudio 97: char *buf = NULL, *next, *lim, *cp;
1.56 claudio 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;
1.1 deraadt 105:
1.75 ! dlg 106: if_errs = &if_show_errs[dflag];
! 107:
1.1 deraadt 108: if (interval) {
1.62 tedu 109: sidewaysintpr((unsigned)interval, repeatcount);
1.1 deraadt 110: return;
111: }
1.3 deraadt 112:
1.72 claudio 113: len = get_sysctl(mib, 6, &buf);
1.3 deraadt 114:
1.22 camield 115: printf("%-7.7s %-5.5s %-11.11s %-17.17s ",
1.33 deraadt 116: "Name", "Mtu", "Network", "Address");
1.22 camield 117: if (bflag)
118: printf("%10.10s %10.10s", "Ibytes", "Obytes");
1.75 ! dlg 119: else {
1.22 camield 120: printf("%8.8s %5.5s %8.8s %5.5s %5.5s",
1.75 ! dlg 121: "Ipkts", if_errs->iname,
! 122: "Opkts", if_errs->oname, "Colls");
! 123: }
1.1 deraadt 124: if (tflag)
125: printf(" %s", "Time");
126: putchar('\n');
1.56 claudio 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:
1.3 deraadt 151: if (interface != 0 && strcmp(name, interface) != 0)
1.1 deraadt 152: continue;
1.56 claudio 153:
154: /* mark inactive interfaces with a '*' */
1.10 millert 155: cp = strchr(name, '\0');
1.56 claudio 156: if ((ifm.ifm_flags & IFF_UP) == 0)
1.1 deraadt 157: *cp++ = '*';
158: *cp = '\0';
1.25 brian 159:
1.56 claudio 160: if (qflag) {
161: total = ifd->ifi_ibytes + ifd->ifi_obytes +
1.75 ! dlg 162: ifd->ifi_ipackets +
! 163: ifd->ifi_opackets +
1.56 claudio 164: ifd->ifi_collisions;
1.75 ! dlg 165: total += if_errs->count(ifd->ifi_ierrors,
! 166: ifd->ifi_iqdrops);
! 167: total += if_errs->count(ifd->ifi_oerrors,
! 168: ifd->ifi_oqdrops);
1.56 claudio 169: if (tflag)
170: total += 0; // XXX ifnet.if_timer;
171: if (total == 0)
172: continue;
173: }
174:
1.61 tedu 175: printf("%-7s %-5d ", name, ifd->ifi_mtu);
1.56 claudio 176: print_addr(rti_info[RTAX_IFP], rti_info, ifd);
177: break;
178: case RTM_NEWADDR:
179: if (qflag && total == 0)
1.25 brian 180: continue;
1.57 claudio 181: if (interface != 0 && strcmp(name, interface) != 0)
182: continue;
183:
1.56 claudio 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:
1.61 tedu 192: printf("%-7s %-5d ", name, ifd->ifi_mtu);
1.56 claudio 193: print_addr(rti_info[RTAX_IFA], rti_info, ifd);
194: break;
1.25 brian 195: }
1.56 claudio 196: }
1.58 dhill 197: free(buf);
1.56 claudio 198: }
1.25 brian 199:
1.56 claudio 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: }
1.1 deraadt 241: #endif
1.56 claudio 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;
1.1 deraadt 269: }
1.56 claudio 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;
1.1 deraadt 285: }
1.65 tedu 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
1.66 tedu 294: printf("%10llu %10llu",
1.65 tedu 295: ifd->ifi_ibytes, ifd->ifi_obytes);
296: } else
1.56 claudio 297: printf("%8llu %5llu %8llu %5llu %5llu",
1.75 ! dlg 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),
1.56 claudio 302: ifd->ifi_collisions);
303: if (tflag)
304: printf(" %4d", 0 /* XXX ifnet.if_timer */);
305: putchar('\n');
1.1 deraadt 306: }
307:
308: struct iftot {
1.3 deraadt 309: char ift_name[IFNAMSIZ]; /* interface name */
1.53 mk 310: u_int64_t ift_ip; /* input packets */
311: u_int64_t ift_ib; /* input bytes */
312: u_int64_t ift_ie; /* input errors */
1.75 ! dlg 313: u_int64_t ift_iq; /* input qdrops */
1.53 mk 314: u_int64_t ift_op; /* output packets */
315: u_int64_t ift_ob; /* output bytes */
316: u_int64_t ift_oe; /* output errors */
1.75 ! dlg 317: u_int64_t ift_oq; /* output qdrops */
1.53 mk 318: u_int64_t ift_co; /* collisions */
1.56 claudio 319: } ip_cur, ip_old, sum_cur, sum_old;
1.1 deraadt 320:
1.26 millert 321: volatile sig_atomic_t signalled; /* set if alarm goes off "early" */
1.1 deraadt 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
1.62 tedu 330: sidewaysintpr(unsigned int interval, int repeatcount)
1.1 deraadt 331: {
1.56 claudio 332: sigset_t emptyset;
1.27 mpech 333: int line;
1.65 tedu 334: char ibytes[FMT_SCALED_STRSIZE];
335: char obytes[FMT_SCALED_STRSIZE];
1.3 deraadt 336:
1.56 claudio 337: fetchifs();
338: if (ip_cur.ift_name[0] == '\0') {
1.4 deraadt 339: fprintf(stderr, "%s: %s: unknown interface\n",
340: __progname, interface);
341: exit(1);
1.1 deraadt 342: }
343:
344: (void)signal(SIGALRM, catchalarm);
1.56 claudio 345: signalled = 0;
1.1 deraadt 346: (void)alarm(interval);
347: banner:
1.22 camield 348: if (bflag)
349: printf("%7.7s in %8.8s %6.6s out %5.5s",
1.56 claudio 350: ip_cur.ift_name, " ",
351: ip_cur.ift_name, " ");
1.22 camield 352: else
353: printf("%5.5s in %5.5s%5.5s out %5.5s %5.5s",
1.56 claudio 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", " ", " ");
1.1 deraadt 363: putchar('\n');
1.22 camield 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",
1.75 ! dlg 369: "packets", if_errs->name,
! 370: "packets", if_errs->name, "colls");
1.56 claudio 371:
372: if (bflag)
1.65 tedu 373: printf("%10.10s %8.8s %10.10s %5.5s",
1.56 claudio 374: "bytes", " ", "bytes", " ");
375: else
376: printf(" %8.8s %5.5s %8.8s %5.5s %5.5s",
377: "packets", "errs", "packets", "errs", "colls");
1.1 deraadt 378: putchar('\n');
379: fflush(stdout);
380: line = 0;
1.56 claudio 381: bzero(&ip_old, sizeof(ip_old));
382: bzero(&sum_old, sizeof(sum_old));
1.1 deraadt 383: loop:
1.56 claudio 384: bzero(&sum_cur, sizeof(sum_cur));
385:
386: fetchifs();
387:
1.65 tedu 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
1.56 claudio 399: printf("%8llu %5llu %8llu %5llu %5llu",
400: ip_cur.ift_ip - ip_old.ift_ip,
1.75 ! dlg 401: if_errs->count(ip_cur.ift_ie - ip_old.ift_ie,
! 402: ip_cur.ift_iq - ip_old.ift_iq),
1.56 claudio 403: ip_cur.ift_op - ip_old.ift_op,
1.75 ! dlg 404: if_errs->count(ip_cur.ift_oe - ip_old.ift_oe,
! 405: ip_cur.ift_oq - ip_old.ift_oq),
1.56 claudio 406: ip_cur.ift_co - ip_old.ift_co);
407:
408: ip_old = ip_cur;
409:
1.65 tedu 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);
1.75 ! dlg 414: printf("%10s %8.8s %10s %5.5s",
1.65 tedu 415: ibytes, " ", obytes, " ");
416: } else
1.75 ! dlg 417: printf("%10llu %8.8s %10llu %5.5s",
1.65 tedu 418: sum_cur.ift_ib - sum_old.ift_ib, " ",
419: sum_cur.ift_ob - sum_old.ift_ob, " ");
420: } else
1.75 ! dlg 421: printf("%8llu %5llu %8llu %5llu %5llu",
1.56 claudio 422: sum_cur.ift_ip - sum_old.ift_ip,
1.75 ! dlg 423: if_errs->count(sum_cur.ift_ie - sum_old.ift_ie,
! 424: sum_cur.ift_iq - sum_old.ift_iq),
1.56 claudio 425: sum_cur.ift_op - sum_old.ift_op,
1.75 ! dlg 426: if_errs->count(sum_cur.ift_oe - sum_old.ift_oe,
! 427: sum_cur.ift_oq - sum_old.ift_oq),
1.56 claudio 428: sum_cur.ift_co - sum_old.ift_co);
429:
430: sum_old = sum_cur;
431:
1.1 deraadt 432: putchar('\n');
433: fflush(stdout);
1.62 tedu 434: if (repeatcount && --repeatcount == 0)
435: return;
1.1 deraadt 436: line++;
1.26 millert 437: sigemptyset(&emptyset);
438: if (!signalled)
439: sigsuspend(&emptyset);
1.56 claudio 440: signalled = 0;
1.1 deraadt 441: (void)alarm(interval);
1.62 tedu 442: if (line == 21 && isatty(STDOUT_FILENO))
1.1 deraadt 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: */
1.46 deraadt 451: /* ARGSUSED */
1.1 deraadt 452: static void
1.33 deraadt 453: catchalarm(int signo)
1.1 deraadt 454: {
1.56 claudio 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:
1.63 tedu 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)) < 0)
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:
1.56 claudio 513: static void
514: fetchifs(void)
515: {
516: struct if_msghdr ifm;
1.68 guenther 517: int mib[6] = { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
1.56 claudio 518: struct rt_msghdr *rtm;
519: struct if_data *ifd;
520: struct sockaddr *sa, *rti_info[RTAX_MAX];
521: struct sockaddr_dl *sdl;
1.73 sthen 522: char *buf = NULL, *next, *lim;
1.56 claudio 523: char name[IFNAMSIZ];
524: size_t len;
1.63 tedu 525: int takeit = 0;
526: int foundone = 0;
1.56 claudio 527:
1.72 claudio 528: len = get_sysctl(mib, 6, &buf);
1.56 claudio 529:
1.63 tedu 530: memset(&ip_cur, 0, sizeof(ip_cur));
1.56 claudio 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:
1.59 claudio 553: if (interface != NULL && !strcmp(name, interface)) {
1.63 tedu 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) {
1.56 claudio 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;
1.75 ! dlg 567: ip_cur.ift_iq = ifd->ifi_iqdrops;
1.56 claudio 568: ip_cur.ift_op = ifd->ifi_opackets;
569: ip_cur.ift_ob = ifd->ifi_obytes;
570: ip_cur.ift_oe = ifd->ifi_oerrors;
1.75 ! dlg 571: ip_cur.ift_oq = ifd->ifi_oqdrops;
1.56 claudio 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;
1.75 ! dlg 578: sum_cur.ift_iq += ifd->ifi_iqdrops;
1.56 claudio 579: sum_cur.ift_op += ifd->ifi_opackets;
580: sum_cur.ift_ob += ifd->ifi_obytes;
581: sum_cur.ift_oe += ifd->ifi_oerrors;
1.75 ! dlg 582: sum_cur.ift_oq += ifd->ifi_oqdrops;
1.56 claudio 583: sum_cur.ift_co += ifd->ifi_collisions;
584: break;
585: }
586: }
1.63 tedu 587: if (interface == NULL && foundone == 0) {
1.56 claudio 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;
1.75 ! dlg 593: ip_cur.ift_iq = ifd->ifi_iqdrops;
1.56 claudio 594: ip_cur.ift_op = ifd->ifi_opackets;
595: ip_cur.ift_ob = ifd->ifi_obytes;
596: ip_cur.ift_oe = ifd->ifi_oerrors;
1.75 ! dlg 597: ip_cur.ift_oq = ifd->ifi_oqdrops;
1.56 claudio 598: ip_cur.ift_co = ifd->ifi_collisions;
599: }
1.58 dhill 600: free(buf);
1.75 ! dlg 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);
1.47 claudio 619: }