]> git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/if.c
network_cmds-356.9.tar.gz
[apple/network_cmds.git] / netstat.tproj / if.c
1 /*
2 * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (c) 1983, 1988, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
61 #ifndef lint
62 /*
63 static char sccsid[] = "@(#)if.c 8.3 (Berkeley) 4/28/95";
64 */
65 static const char rcsid[] =
66 "$Id: if.c,v 1.7 2006/01/16 04:53:59 lindak Exp $";
67 #endif /* not lint */
68
69 #include <sys/types.h>
70 #include <sys/socket.h>
71 #include <sys/sysctl.h>
72 #include <sys/time.h>
73
74 #include <net/if.h>
75 #include <net/if_var.h>
76 #include <net/if_dl.h>
77 #include <net/if_types.h>
78 #include <net/if_mib.h>
79 #include <net/if_llreach.h>
80 #include <net/ethernet.h>
81 #include <net/route.h>
82
83 #include <netinet/in.h>
84 #include <netinet/in_var.h>
85
86 #include <arpa/inet.h>
87
88 #include <signal.h>
89 #include <stdio.h>
90 #include <string.h>
91 #include <unistd.h>
92 #include <stdlib.h>
93 #include <err.h>
94 #include <errno.h>
95
96 #include "netstat.h"
97
98 #define YES 1
99 #define NO 0
100
101 #define ROUNDUP(a, size) (((a) & ((size) - 1)) ? (1 + ((a)|(size - 1))) : (a))
102
103 #define NEXT_SA(p) (struct sockaddr *) \
104 ((caddr_t)p + (p->sa_len ? ROUNDUP(p->sa_len, sizeof(uint32_t)) : \
105 sizeof(uint32_t)))
106
107 static void sidewaysintpr ();
108 static void catchalarm (int);
109 static char *sec2str(time_t);
110 static void llreach_sysctl(uint32_t);
111
112 #ifdef INET6
113 char *netname6 (struct sockaddr_in6 *, struct sockaddr *);
114 static char ntop_buf[INET6_ADDRSTRLEN]; /* for inet_ntop() */
115 #endif
116
117 /*
118 * Display a formatted value, or a '-' in the same space.
119 */
120 static void
121 show_stat(const char *fmt, int width, u_int64_t value, short showvalue)
122 {
123 char newfmt[32];
124
125 /* Construct the format string */
126 if (showvalue) {
127 snprintf(newfmt, sizeof(newfmt), "%%%d%s", width, fmt);
128 printf(newfmt, value);
129 } else {
130 snprintf(newfmt, sizeof(newfmt), "%%%ds", width);
131 printf(newfmt, "-");
132 }
133 }
134
135 size_t
136 get_rti_info(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
137 {
138 int i;
139 size_t len = 0;
140
141 for (i = 0; i < RTAX_MAX; i++) {
142 if (addrs & (1 << i)) {
143 rti_info[i] = sa;
144 if (sa->sa_len < sizeof(struct sockaddr))
145 len += sizeof(struct sockaddr);
146 else
147 len += sa->sa_len;
148 sa = NEXT_SA(sa);
149 } else {
150 rti_info[i] = NULL;
151 }
152 }
153 return len;
154 }
155
156 static void
157 multipr(int family, char *buf, char *lim)
158 {
159 char *next;
160
161 for (next = buf; next < lim; ) {
162 struct ifma_msghdr2 *ifmam = (struct ifma_msghdr2 *)next;
163 struct sockaddr *rti_info[RTAX_MAX];
164 struct sockaddr *sa;
165 const char *fmt = 0;
166
167 next += ifmam->ifmam_msglen;
168 if (ifmam->ifmam_type == RTM_IFINFO2)
169 break;
170 else if (ifmam->ifmam_type != RTM_NEWMADDR2)
171 continue;
172 get_rti_info(ifmam->ifmam_addrs, (struct sockaddr*)(ifmam + 1), rti_info);
173 sa = rti_info[RTAX_IFA];
174
175 if (sa->sa_family != family)
176 continue;
177 switch (sa->sa_family) {
178 case AF_INET: {
179 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
180
181 fmt = routename(sin->sin_addr.s_addr);
182 break;
183 }
184 #ifdef INET6
185 case AF_INET6: {
186 struct sockaddr_in6 sin6;
187
188 memcpy(&sin6, sa, sizeof(struct sockaddr_in6));
189
190 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
191 IN6_IS_ADDR_MC_NODELOCAL(&sin6.sin6_addr) ||
192 IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) {
193 sin6.sin6_scope_id = ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
194 sin6.sin6_addr.s6_addr[2] = 0;
195 sin6.sin6_addr.s6_addr[3] = 0;
196 }
197
198 printf("%23s %-19.19s(refs: %d)\n", "",
199 inet_ntop(AF_INET6, &sin6.sin6_addr,
200 ntop_buf, sizeof(ntop_buf)),
201 ifmam->ifmam_refcount);
202 break;
203 }
204 #endif /* INET6 */
205 case AF_LINK: {
206 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
207
208 switch (sdl->sdl_type) {
209 case IFT_ETHER:
210 case IFT_FDDI:
211 fmt = ether_ntoa((struct ether_addr *)
212 LLADDR(sdl));
213 break;
214 }
215 break;
216 }
217 }
218 if (fmt)
219 printf("%23s %s\n", "", fmt);
220 }
221 }
222
223 /*
224 * Print a description of the network interfaces.
225 */
226 void
227 intpr(void (*pfunc)(char *))
228 {
229 u_int64_t opackets = 0;
230 u_int64_t ipackets = 0;
231 u_int64_t obytes = 0;
232 u_int64_t ibytes = 0;
233 u_int64_t oerrors = 0;
234 u_int64_t ierrors = 0;
235 u_int64_t collisions = 0;
236 uint32_t mtu = 0;
237 short timer = 0;
238 int drops = 0;
239 struct sockaddr *sa = NULL;
240 char name[32];
241 short network_layer;
242 short link_layer;
243 int mib[6];
244 char *buf = NULL, *lim, *next;
245 size_t len;
246 struct if_msghdr *ifm;
247 struct sockaddr *rti_info[RTAX_MAX];
248 unsigned int ifindex = 0;
249
250 if (interval) {
251 sidewaysintpr();
252 return;
253 }
254
255 if (interface != 0)
256 ifindex = if_nametoindex(interface);
257
258 mib[0] = CTL_NET; // networking subsystem
259 mib[1] = PF_ROUTE; // type of information
260 mib[2] = 0; // protocol (IPPROTO_xxx)
261 mib[3] = 0; // address family
262 mib[4] = NET_RT_IFLIST2; // operation
263 mib[5] = 0;
264 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
265 return;
266 if ((buf = malloc(len)) == NULL) {
267 printf("malloc failed\n");
268 exit(1);
269 }
270 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
271 if (buf)
272 free(buf);
273 return;
274 }
275
276 if (!pfunc) {
277 printf("%-5.5s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
278 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
279 if (prioflag >= 0)
280 printf(" %8.8s", "Itcpkts");
281 if (bflag) {
282 printf(" %10.10s","Ibytes");
283 if (prioflag >= 0)
284 printf(" %10.10s", "Itcbytes");
285 }
286 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
287 if (prioflag >= 0)
288 printf(" %8.8s", "Otcpkts");
289 if (bflag) {
290 printf(" %10.10s","Obytes");
291 if (prioflag >= 0)
292 printf(" %10.10s", "Otcbytes");
293 }
294 printf(" %5s", "Coll");
295 if (tflag)
296 printf(" %s", "Time");
297 if (dflag)
298 printf(" %s", "Drop");
299 putchar('\n');
300 }
301 lim = buf + len;
302 for (next = buf; next < lim; ) {
303 char *cp;
304 int n, m;
305 struct ifmibdata_supplemental ifmsupp;
306 u_int64_t ift_itcp = 0; /* input tc packets */
307 u_int64_t ift_itcb = 0; /* input tc bytes */
308 u_int64_t ift_otcp = 0; /* output tc packets */
309 u_int64_t ift_otcb = 0; /* output tc bytes */
310
311 bzero(&ifmsupp, sizeof(struct ifmibdata_supplemental));
312
313 network_layer = 0;
314 link_layer = 0;
315 ifm = (struct if_msghdr *)next;
316 next += ifm->ifm_msglen;
317
318 if (ifm->ifm_type == RTM_IFINFO2) {
319 struct if_msghdr2 *if2m = (struct if_msghdr2 *)ifm;
320 struct sockaddr_dl *sdl = (struct sockaddr_dl *)(if2m + 1);
321
322 strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
323 name[sdl->sdl_nlen] = 0;
324 if (interface != 0 && if2m->ifm_index != ifindex)
325 continue;
326 cp = index(name, '\0');
327
328 if (pfunc) {
329 (*pfunc)(name);
330 continue;
331 }
332
333 if ((if2m->ifm_flags & IFF_UP) == 0)
334 *cp++ = '*';
335 *cp = '\0';
336
337 /*
338 * Get the interface stats. These may get
339 * overriden below on a per-interface basis.
340 */
341 opackets = if2m->ifm_data.ifi_opackets;
342 ipackets = if2m->ifm_data.ifi_ipackets;
343 obytes = if2m->ifm_data.ifi_obytes;
344 ibytes = if2m->ifm_data.ifi_ibytes;
345 oerrors =if2m->ifm_data.ifi_oerrors;
346 ierrors = if2m->ifm_data.ifi_ierrors;
347 collisions = if2m->ifm_data.ifi_collisions;
348 timer = if2m->ifm_timer;
349 drops = if2m->ifm_snd_drops;
350 mtu = if2m->ifm_data.ifi_mtu;
351
352 if (prioflag >= 0) {
353 int name[6];
354 size_t miblen = sizeof(struct ifmibdata_supplemental);
355
356 /* Common OID prefix */
357 name[0] = CTL_NET;
358 name[1] = PF_LINK;
359 name[2] = NETLINK_GENERIC;
360 name[3] = IFMIB_IFDATA;
361 name[4] = if2m->ifm_index;
362 name[5] = IFDATA_SUPPLEMENTAL;
363 if (sysctl(name, 6, &ifmsupp, &miblen, (void *)0, 0) == -1)
364 err(1, "sysctl IFDATA_SUPPLEMENTAL");
365
366 switch (prioflag) {
367 case SO_TC_BK:
368 ift_itcp = ifmsupp.ifmd_traffic_class.ifi_ibkpackets;
369 ift_itcb = ifmsupp.ifmd_traffic_class.ifi_ibkbytes;
370 ift_otcp = ifmsupp.ifmd_traffic_class.ifi_obkpackets;
371 ift_otcb = ifmsupp.ifmd_traffic_class.ifi_obkbytes;
372 break;
373 case SO_TC_VI:
374 ift_itcp = ifmsupp.ifmd_traffic_class.ifi_ivipackets;
375 ift_itcb = ifmsupp.ifmd_traffic_class.ifi_ivibytes;
376 ift_otcp = ifmsupp.ifmd_traffic_class.ifi_ovipackets;
377 ift_otcb = ifmsupp.ifmd_traffic_class.ifi_ovibytes;
378 break;
379 case SO_TC_VO:
380 ift_itcp = ifmsupp.ifmd_traffic_class.ifi_ivopackets;
381 ift_itcb = ifmsupp.ifmd_traffic_class.ifi_ivobytes;
382 ift_otcp = ifmsupp.ifmd_traffic_class.ifi_ovopackets;
383 ift_otcb = ifmsupp.ifmd_traffic_class.ifi_ovobytes;
384 break;
385 default:
386 ift_itcp = 0;
387 ift_itcb = 0;
388 ift_otcp = 0;
389 ift_otcb = 0;
390 break;
391 }
392 }
393
394 get_rti_info(if2m->ifm_addrs, (struct sockaddr*)(if2m + 1), rti_info);
395 sa = rti_info[RTAX_IFP];
396 } else if (ifm->ifm_type == RTM_NEWADDR) {
397 struct ifa_msghdr *ifam = (struct ifa_msghdr *)ifm;
398
399 if (interface != 0 && ifam->ifam_index != ifindex)
400 continue;
401 get_rti_info(ifam->ifam_addrs, (struct sockaddr*)(ifam + 1), rti_info);
402 sa = rti_info[RTAX_IFA];
403 } else {
404 continue;
405 }
406 printf("%-5.5s %-5u ", name, mtu);
407
408 if (sa == 0) {
409 printf("%-13.13s ", "none");
410 printf("%-15.15s ", "none");
411 } else {
412 switch (sa->sa_family) {
413 case AF_UNSPEC:
414 printf("%-13.13s ", "none");
415 printf("%-15.15s ", "none");
416 break;
417
418 case AF_INET: {
419 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
420 struct sockaddr_in mask;
421
422 mask.sin_addr.s_addr = 0;
423 memcpy(&mask,
424 rti_info[RTAX_NETMASK],
425 ((struct sockaddr_in *)rti_info[RTAX_NETMASK])->sin_len);
426
427 printf("%-13.13s ",
428 netname(sin->sin_addr.s_addr & mask.sin_addr.s_addr,
429 ntohl(mask.sin_addr.s_addr)));
430
431 printf("%-15.15s ",
432 routename(sin->sin_addr.s_addr));
433
434 network_layer = 1;
435 break;
436 }
437 #ifdef INET6
438 case AF_INET6: {
439 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
440 struct sockaddr *mask = (struct sockaddr *)rti_info[RTAX_NETMASK];
441
442 printf("%-11.11s ",
443 netname6(sin6,
444 mask));
445 printf("%-17.17s ",
446 (char *)inet_ntop(AF_INET6,
447 &sin6->sin6_addr,
448 ntop_buf, sizeof(ntop_buf)));
449
450 network_layer = 1;
451 break;
452 }
453 #endif /*INET6*/
454 case AF_LINK: {
455 struct sockaddr_dl *sdl =
456 (struct sockaddr_dl *)sa;
457 char linknum[10];
458 cp = (char *)LLADDR(sdl);
459 n = sdl->sdl_alen;
460 snprintf(linknum, sizeof(linknum), "<Link#%d>", sdl->sdl_index);
461 m = printf("%-11.11s ", linknum);
462 goto hexprint;
463 }
464
465 default:
466 m = printf("(%d)", sa->sa_family);
467 for (cp = sa->sa_len + (char *)sa;
468 --cp > sa->sa_data && (*cp == 0);) {}
469 n = cp - sa->sa_data + 1;
470 cp = sa->sa_data;
471 hexprint:
472 while (--n >= 0)
473 m += printf("%02x%c", *cp++ & 0xff,
474 n > 0 ? ':' : ' ');
475 m = 30 - m;
476 while (m-- > 0)
477 putchar(' ');
478
479 link_layer = 1;
480 break;
481 }
482 }
483
484 show_stat("llu", 8, ipackets, link_layer|network_layer);
485 printf(" ");
486 show_stat("llu", 5, ierrors, link_layer);
487 printf(" ");
488 if (prioflag >= 0) {
489 show_stat("llu", 8, ift_itcp, link_layer|network_layer);
490 printf(" ");
491 }
492 if (bflag) {
493 show_stat("llu", 10, ibytes, link_layer|network_layer);
494 printf(" ");
495 if (prioflag >= 0) {
496 show_stat("llu", 8, ift_itcb, link_layer|network_layer);
497 printf(" ");
498 }
499 }
500 show_stat("llu", 8, opackets, link_layer|network_layer);
501 printf(" ");
502 show_stat("llu", 5, oerrors, link_layer);
503 printf(" ");
504 if (prioflag >= 0) {
505 show_stat("llu", 8, ift_otcp, link_layer|network_layer);
506 printf(" ");
507 }
508 if (bflag) {
509 show_stat("llu", 10, obytes, link_layer|network_layer);
510 printf(" ");
511 if (prioflag >= 0) {
512 show_stat("llu", 8, ift_otcb, link_layer|network_layer);
513 printf(" ");
514 }
515 }
516 show_stat("llu", 5, collisions, link_layer);
517 if (tflag) {
518 printf(" ");
519 show_stat("ll", 3, timer, link_layer);
520 }
521 if (dflag) {
522 printf(" ");
523 show_stat("ll", 3, drops, link_layer);
524 }
525 putchar('\n');
526
527 if (aflag)
528 multipr(sa->sa_family, next, lim);
529 }
530 free(buf);
531 }
532
533 struct iftot {
534 SLIST_ENTRY(iftot) chain;
535 char ift_name[16]; /* interface name */
536 u_int64_t ift_ip; /* input packets */
537 u_int64_t ift_ie; /* input errors */
538 u_int64_t ift_op; /* output packets */
539 u_int64_t ift_oe; /* output errors */
540 u_int64_t ift_co; /* collisions */
541 u_int64_t ift_dr; /* drops */
542 u_int64_t ift_ib; /* input bytes */
543 u_int64_t ift_ob; /* output bytes */
544 u_int64_t ift_itcp; /* input tc packets */
545 u_int64_t ift_itcb; /* input tc bytes */
546 u_int64_t ift_otcp; /* output tc packets */
547 u_int64_t ift_otcb; /* output tc bytes */
548 };
549
550 u_char signalled; /* set if alarm goes off "early" */
551
552 /*
553 * Print a running summary of interface statistics.
554 * Repeat display every interval seconds, showing statistics
555 * collected over that interval. Assumes that interval is non-zero.
556 * First line printed at top of screen is always cumulative.
557 * XXX - should be rewritten to use ifmib(4).
558 */
559 static void
560 sidewaysintpr()
561 {
562 struct iftot *total, *sum, *interesting;
563 register int line;
564 int first;
565 int name[6];
566 size_t len;
567 unsigned int ifcount, i;
568 struct ifmibdata *ifmdall = 0;
569 int interesting_row;
570 sigset_t sigset, oldsigset;
571 struct itimerval timer_interval;
572
573 /* Common OID prefix */
574 name[0] = CTL_NET;
575 name[1] = PF_LINK;
576 name[2] = NETLINK_GENERIC;
577
578 len = sizeof(int);
579 name[3] = IFMIB_SYSTEM;
580 name[4] = IFMIB_IFCOUNT;
581 if (sysctl(name, 5, &ifcount, &len, 0, 0) == 1)
582 err(1, "sysctl IFMIB_IFCOUNT");
583
584 len = ifcount * sizeof(struct ifmibdata);
585 ifmdall = malloc(len);
586 if (ifmdall == 0)
587 err(1, "malloc failed");
588 name[3] = IFMIB_IFALLDATA;
589 name[4] = 0;
590 name[5] = IFDATA_GENERAL;
591 if (sysctl(name, 6, ifmdall, &len, (void *)0, 0) == -1)
592 err(1, "sysctl IFMIB_IFALLDATA");
593
594 interesting = NULL;
595 interesting_row = 0;
596 for (i = 0; i < ifcount; i++) {
597 struct ifmibdata *ifmd = ifmdall + i;
598
599 if (interface && strcmp(ifmd->ifmd_name, interface) == 0) {
600 if ((interesting = calloc(ifcount, sizeof(struct iftot))) == NULL)
601 err(1, "malloc failed");
602 interesting_row = if_nametoindex(interface);
603 snprintf(interesting->ift_name, 16, "(%s)", ifmd->ifmd_name);;
604 }
605 }
606 if ((total = calloc(1, sizeof(struct iftot))) == NULL)
607 err(1, "malloc failed");
608
609 if ((sum = calloc(1, sizeof(struct iftot))) == NULL)
610 err(1, "malloc failed");
611
612 /* create a timer that fires repeatedly every interval seconds */
613 timer_interval.it_value.tv_sec = interval;
614 timer_interval.it_value.tv_usec = 0;
615 timer_interval.it_interval.tv_sec = interval;
616 timer_interval.it_interval.tv_usec = 0;
617 (void)signal(SIGALRM, catchalarm);
618 signalled = NO;
619 (void)setitimer(ITIMER_REAL, &timer_interval, NULL);
620 first = 1;
621 banner:
622 if (prioflag >= 0)
623 printf("%37s %14s %16s", "input",
624 interesting ? interesting->ift_name : "(Total)", "output");
625 else
626 printf("%17s %14s %16s", "input",
627 interesting ? interesting->ift_name : "(Total)", "output");
628 putchar('\n');
629 printf("%10s %5s %10s ",
630 "packets", "errs", "bytes");
631 if (prioflag >= 0)
632 printf(" %10s %10s", "tcpkts", "tcbytes");
633 printf("%10s %5s %10s %5s",
634 "packets", "errs", "bytes", "colls");
635 if (dflag)
636 printf(" %5.5s", "drops");
637 if (prioflag >= 0)
638 printf(" %10s %10s", "tcpkts", "tcbytes");
639 putchar('\n');
640 fflush(stdout);
641 line = 0;
642 loop:
643 if (interesting != NULL) {
644 struct ifmibdata ifmd;
645 struct ifmibdata_supplemental ifmsupp;
646
647 len = sizeof(struct ifmibdata);
648 name[3] = IFMIB_IFDATA;
649 name[4] = interesting_row;
650 name[5] = IFDATA_GENERAL;
651 if (sysctl(name, 6, &ifmd, &len, (void *)0, 0) == -1)
652 err(1, "sysctl IFDATA_GENERAL %d", interesting_row);
653
654 if (prioflag >= 0) {
655 len = sizeof(struct ifmibdata_supplemental);
656 name[3] = IFMIB_IFDATA;
657 name[4] = interesting_row;
658 name[5] = IFDATA_SUPPLEMENTAL;
659 if (sysctl(name, 6, &ifmsupp, &len, (void *)0, 0) == -1)
660 err(1, "sysctl IFDATA_SUPPLEMENTAL %d", interesting_row);
661 }
662 if (!first) {
663 printf("%10llu %5llu %10llu ",
664 ifmd.ifmd_data.ifi_ipackets - interesting->ift_ip,
665 ifmd.ifmd_data.ifi_ierrors - interesting->ift_ie,
666 ifmd.ifmd_data.ifi_ibytes - interesting->ift_ib);
667 switch (prioflag) {
668 case SO_TC_BK:
669 printf("%10llu %10llu ",
670 ifmsupp.ifmd_traffic_class.ifi_ibkpackets - interesting->ift_itcp,
671 ifmsupp.ifmd_traffic_class.ifi_ibkbytes - interesting->ift_itcb);
672 break;
673 case SO_TC_VI:
674 printf("%10llu %10llu ",
675 ifmsupp.ifmd_traffic_class.ifi_ivipackets - interesting->ift_itcp,
676 ifmsupp.ifmd_traffic_class.ifi_ivibytes - interesting->ift_itcb);
677 break;
678 case SO_TC_VO:
679 printf("%10llu %10llu ",
680 ifmsupp.ifmd_traffic_class.ifi_ivopackets - interesting->ift_itcp,
681 ifmsupp.ifmd_traffic_class.ifi_ivobytes - interesting->ift_itcb);
682 break;
683 default:
684 break;
685 }
686 printf("%10llu %5llu %10llu %5llu",
687 ifmd.ifmd_data.ifi_opackets - interesting->ift_op,
688 ifmd.ifmd_data.ifi_oerrors - interesting->ift_oe,
689 ifmd.ifmd_data.ifi_obytes - interesting->ift_ob,
690 ifmd.ifmd_data.ifi_collisions - interesting->ift_co);
691 if (dflag)
692 printf(" %5llu", ifmd.ifmd_snd_drops - interesting->ift_dr);
693 switch (prioflag) {
694 case SO_TC_BK:
695 printf(" %10llu %10llu",
696 ifmsupp.ifmd_traffic_class.ifi_obkpackets - interesting->ift_otcp,
697 ifmsupp.ifmd_traffic_class.ifi_obkbytes - interesting->ift_otcb);
698 break;
699 case SO_TC_VI:
700 printf(" %10llu %10llu",
701 ifmsupp.ifmd_traffic_class.ifi_ovipackets - interesting->ift_otcp,
702 ifmsupp.ifmd_traffic_class.ifi_ovibytes - interesting->ift_otcb);
703 break;
704 case SO_TC_VO:
705 printf(" %10llu %10llu",
706 ifmsupp.ifmd_traffic_class.ifi_ovopackets - interesting->ift_otcp,
707 ifmsupp.ifmd_traffic_class.ifi_ovobytes - interesting->ift_otcb);
708 break;
709 default:
710 break;
711 }
712 }
713 interesting->ift_ip = ifmd.ifmd_data.ifi_ipackets;
714 interesting->ift_ie = ifmd.ifmd_data.ifi_ierrors;
715 interesting->ift_ib = ifmd.ifmd_data.ifi_ibytes;
716 interesting->ift_op = ifmd.ifmd_data.ifi_opackets;
717 interesting->ift_oe = ifmd.ifmd_data.ifi_oerrors;
718 interesting->ift_ob = ifmd.ifmd_data.ifi_obytes;
719 interesting->ift_co = ifmd.ifmd_data.ifi_collisions;
720 interesting->ift_dr = ifmd.ifmd_snd_drops;
721 /* private counters */
722 switch (prioflag) {
723 case SO_TC_BK:
724 interesting->ift_itcp = ifmsupp.ifmd_traffic_class.ifi_ibkpackets;
725 interesting->ift_itcb = ifmsupp.ifmd_traffic_class.ifi_ibkbytes;
726 interesting->ift_otcp = ifmsupp.ifmd_traffic_class.ifi_obkpackets;
727 interesting->ift_otcb = ifmsupp.ifmd_traffic_class.ifi_obkbytes;
728 break;
729 case SO_TC_VI:
730 interesting->ift_itcp = ifmsupp.ifmd_traffic_class.ifi_ivipackets;
731 interesting->ift_itcb = ifmsupp.ifmd_traffic_class.ifi_ivibytes;
732 interesting->ift_otcp = ifmsupp.ifmd_traffic_class.ifi_ovipackets;
733 interesting->ift_otcb = ifmsupp.ifmd_traffic_class.ifi_ovibytes;
734 break;
735 case SO_TC_VO:
736 interesting->ift_itcp = ifmsupp.ifmd_traffic_class.ifi_ivopackets;
737 interesting->ift_itcb = ifmsupp.ifmd_traffic_class.ifi_ivobytes;
738 interesting->ift_otcp = ifmsupp.ifmd_traffic_class.ifi_ovopackets;
739 interesting->ift_otcb = ifmsupp.ifmd_traffic_class.ifi_ovobytes;
740 break;
741 default:
742 break;
743 }
744 } else {
745 unsigned int latest_ifcount;
746 struct ifmibdata_supplemental *ifmsuppall = NULL;
747
748 len = sizeof(int);
749 name[3] = IFMIB_SYSTEM;
750 name[4] = IFMIB_IFCOUNT;
751 if (sysctl(name, 5, &latest_ifcount, &len, 0, 0) == 1)
752 err(1, "sysctl IFMIB_IFCOUNT");
753 if (latest_ifcount > ifcount) {
754 ifcount = latest_ifcount;
755 len = ifcount * sizeof(struct ifmibdata);
756 free(ifmdall);
757 ifmdall = malloc(len);
758 if (ifmdall == 0)
759 err(1, "malloc ifmdall failed");
760 } else if (latest_ifcount > ifcount) {
761 ifcount = latest_ifcount;
762 len = ifcount * sizeof(struct ifmibdata);
763 }
764 len = ifcount * sizeof(struct ifmibdata);
765 name[3] = IFMIB_IFALLDATA;
766 name[4] = 0;
767 name[5] = IFDATA_GENERAL;
768 if (sysctl(name, 6, ifmdall, &len, (void *)0, 0) == -1)
769 err(1, "sysctl IFMIB_IFALLDATA");
770 if (prioflag >= 0) {
771 len = ifcount * sizeof(struct ifmibdata_supplemental);
772 ifmsuppall = malloc(len);
773 if (ifmsuppall == NULL)
774 err(1, "malloc ifmsuppall failed");
775 name[3] = IFMIB_IFALLDATA;
776 name[4] = 0;
777 name[5] = IFDATA_SUPPLEMENTAL;
778 if (sysctl(name, 6, ifmsuppall, &len, (void *)0, 0) == -1)
779 err(1, "sysctl IFMIB_IFALLDATA SUPPLEMENTAL");
780 }
781 sum->ift_ip = 0;
782 sum->ift_ie = 0;
783 sum->ift_ib = 0;
784 sum->ift_op = 0;
785 sum->ift_oe = 0;
786 sum->ift_ob = 0;
787 sum->ift_co = 0;
788 sum->ift_dr = 0;
789 sum->ift_itcp = 0;
790 sum->ift_itcb = 0;
791 sum->ift_otcp = 0;
792 sum->ift_otcb = 0;
793 for (i = 0; i < ifcount; i++) {
794 struct ifmibdata *ifmd = ifmdall + i;
795
796 sum->ift_ip += ifmd->ifmd_data.ifi_ipackets;
797 sum->ift_ie += ifmd->ifmd_data.ifi_ierrors;
798 sum->ift_ib += ifmd->ifmd_data.ifi_ibytes;
799 sum->ift_op += ifmd->ifmd_data.ifi_opackets;
800 sum->ift_oe += ifmd->ifmd_data.ifi_oerrors;
801 sum->ift_ob += ifmd->ifmd_data.ifi_obytes;
802 sum->ift_co += ifmd->ifmd_data.ifi_collisions;
803 sum->ift_dr += ifmd->ifmd_snd_drops;
804 /* private counters */
805 if (prioflag >= 0) {
806 struct ifmibdata_supplemental *ifmsupp = ifmsuppall + i;
807 switch (prioflag) {
808 case SO_TC_BK:
809 sum->ift_itcp += ifmsupp->ifmd_traffic_class.ifi_ibkpackets;
810 sum->ift_itcb += ifmsupp->ifmd_traffic_class.ifi_ibkbytes;
811 sum->ift_otcp += ifmsupp->ifmd_traffic_class.ifi_obkpackets;
812 sum->ift_otcb += ifmsupp->ifmd_traffic_class.ifi_obkbytes;
813 break;
814 case SO_TC_VI:
815 sum->ift_itcp += ifmsupp->ifmd_traffic_class.ifi_ivipackets;
816 sum->ift_itcb += ifmsupp->ifmd_traffic_class.ifi_ivibytes;
817 sum->ift_otcp += ifmsupp->ifmd_traffic_class.ifi_ovipackets;
818 sum->ift_otcb += ifmsupp->ifmd_traffic_class.ifi_ovibytes;
819 break;
820 case SO_TC_VO:
821 sum->ift_itcp += ifmsupp->ifmd_traffic_class.ifi_ivopackets;
822 sum->ift_itcb += ifmsupp->ifmd_traffic_class.ifi_ivobytes;
823 sum->ift_otcp += ifmsupp->ifmd_traffic_class.ifi_ovopackets;
824 sum->ift_otcb += ifmsupp->ifmd_traffic_class.ifi_ovobytes;
825 break;
826 default:
827 break;
828 }
829 }
830 }
831 if (!first) {
832 printf("%10llu %5llu %10llu ",
833 sum->ift_ip - total->ift_ip,
834 sum->ift_ie - total->ift_ie,
835 sum->ift_ib - total->ift_ib);
836 if (prioflag >= 0)
837 printf(" %10llu %10llu",
838 sum->ift_itcp - total->ift_itcp,
839 sum->ift_itcb - total->ift_itcb);
840 printf("%10llu %5llu %10llu %5llu",
841 sum->ift_op - total->ift_op,
842 sum->ift_oe - total->ift_oe,
843 sum->ift_ob - total->ift_ob,
844 sum->ift_co - total->ift_co);
845 if (dflag)
846 printf(" %5llu", sum->ift_dr - total->ift_dr);
847 if (prioflag >= 0)
848 printf(" %10llu %10llu",
849 sum->ift_otcp - total->ift_otcp,
850 sum->ift_otcb - total->ift_otcb);
851 }
852 *total = *sum;
853 }
854 if (!first)
855 putchar('\n');
856 fflush(stdout);
857 sigemptyset(&sigset);
858 sigaddset(&sigset, SIGALRM);
859 (void)sigprocmask(SIG_BLOCK, &sigset, &oldsigset);
860 if (!signalled) {
861 sigemptyset(&sigset);
862 sigsuspend(&sigset);
863 }
864 (void)sigprocmask(SIG_SETMASK, &oldsigset, NULL);
865
866 signalled = NO;
867 line++;
868 first = 0;
869 if (line == 21)
870 goto banner;
871 else
872 goto loop;
873 /*NOTREACHED*/
874 }
875
876 void
877 intervalpr(void (*pr)(uint32_t, char *, int), uint32_t off, char *name , int af)
878 {
879 struct itimerval timer_interval;
880 sigset_t sigset, oldsigset;
881
882 /* create a timer that fires repeatedly every interval seconds */
883 timer_interval.it_value.tv_sec = interval;
884 timer_interval.it_value.tv_usec = 0;
885 timer_interval.it_interval.tv_sec = interval;
886 timer_interval.it_interval.tv_usec = 0;
887 (void) signal(SIGALRM, catchalarm);
888 signalled = NO;
889 (void) setitimer(ITIMER_REAL, &timer_interval, NULL);
890
891 for (;;) {
892 pr(off, name, af);
893
894 fflush(stdout);
895 sigemptyset(&sigset);
896 sigaddset(&sigset, SIGALRM);
897 (void) sigprocmask(SIG_BLOCK, &sigset, &oldsigset);
898 if (!signalled) {
899 sigemptyset(&sigset);
900 sigsuspend(&sigset);
901 }
902 (void) sigprocmask(SIG_SETMASK, &oldsigset, NULL);
903 signalled = NO;
904 }
905 }
906
907 /*
908 * Called if an interval expires before sidewaysintpr has completed a loop.
909 * Sets a flag to not wait for the alarm.
910 */
911 static void
912 catchalarm(int signo )
913 {
914 signalled = YES;
915 }
916
917 static char *
918 sec2str(total)
919 time_t total;
920 {
921 static char result[256];
922 int days, hours, mins, secs;
923 int first = 1;
924 char *p = result;
925
926 days = total / 3600 / 24;
927 hours = (total / 3600) % 24;
928 mins = (total / 60) % 60;
929 secs = total % 60;
930
931 if (days) {
932 first = 0;
933 p += snprintf(p, sizeof(result) - (p - result), "%dd", days);
934 }
935 if (!first || hours) {
936 first = 0;
937 p += snprintf(p, sizeof(result) - (p - result), "%dh", hours);
938 }
939 if (!first || mins) {
940 first = 0;
941 p += snprintf(p, sizeof(result) - (p - result), "%dm", mins);
942 }
943 snprintf(p, sizeof(result) - (p - result), "%ds", secs);
944
945 return(result);
946 }
947
948 void
949 intpr_ri(void (*pfunc)(char *))
950 {
951 int mib[6];
952 char *buf = NULL, *lim, *next;
953 size_t len;
954 unsigned int ifindex = 0;
955 struct if_msghdr2 *if2m;
956
957 if (interface != 0) {
958 ifindex = if_nametoindex(interface);
959 if (ifindex == 0) {
960 printf("interface name is not valid: %s\n", interface);
961 exit(1);
962 }
963 }
964
965 mib[0] = CTL_NET; /* networking subsystem */
966 mib[1] = PF_ROUTE; /* type of information */
967 mib[2] = 0; /* protocol (IPPROTO_xxx) */
968 mib[3] = 0; /* address family */
969 mib[4] = NET_RT_IFLIST2; /* operation */
970 mib[5] = 0;
971 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
972 return;
973 if ((buf = malloc(len)) == NULL) {
974 printf("malloc failed\n");
975 exit(1);
976 }
977 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
978 free(buf);
979 return;
980 }
981
982 printf("%-6s %-17s %8.8s %-9.9s %4s %4s\n",
983 "Proto", "Linklayer Address", "Netif", "Expire", "Refs", "Prbs");
984
985 lim = buf + len;
986 if2m = (struct if_msghdr2 *)buf;
987
988 for (next = buf; next < lim; ) {
989 if2m = (struct if_msghdr2 *)next;
990 next += if2m->ifm_msglen;
991
992 if (if2m->ifm_type != RTM_IFINFO2)
993 continue;
994 else if (interface != 0 && if2m->ifm_index != ifindex)
995 continue;
996
997 llreach_sysctl(if2m->ifm_index);
998 }
999 free(buf);
1000 }
1001
1002 static void
1003 llreach_sysctl(uint32_t ifindex)
1004 {
1005 #define MAX_SYSCTL_TRY 5
1006 int mib[6], i, ntry = 0;
1007 size_t mibsize, len, needed, cnt;
1008 struct if_llreach_info *lri;
1009 struct timeval time;
1010 char *buf;
1011 char ifname[IF_NAMESIZE];
1012
1013 bzero(&mib, sizeof (mib));
1014 mibsize = sizeof (mib) / sizeof (mib[0]);
1015 if (sysctlnametomib("net.link.generic.system.llreach_info", mib,
1016 &mibsize) == -1) {
1017 perror("sysctlnametomib");
1018 return;
1019 }
1020
1021 needed = 0;
1022 mib[5] = ifindex;
1023
1024 mibsize = sizeof (mib) / sizeof (mib[0]);
1025 do {
1026 if (sysctl(mib, mibsize, NULL, &needed, NULL, 0) == -1) {
1027 perror("sysctl net.link.generic.system.llreach_info");
1028 return;
1029 }
1030 if ((buf = malloc(needed)) == NULL) {
1031 perror("malloc");
1032 return;
1033 }
1034 if (sysctl(mib, mibsize, buf, &needed, NULL, 0) == -1) {
1035 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
1036 perror("sysctl");
1037 goto out_free;
1038 }
1039 free(buf);
1040 buf = NULL;
1041 }
1042 } while (buf == NULL);
1043
1044 len = needed;
1045 cnt = len / sizeof (*lri);
1046 lri = (struct if_llreach_info *)buf;
1047
1048 gettimeofday(&time, 0);
1049 if (if_indextoname(ifindex, ifname) == NULL)
1050 snprintf(ifname, sizeof (ifname), "%s", "?");
1051
1052 for (i = 0; i < cnt; i++, lri++) {
1053 printf("0x%-4x %-17s %8.8s ", lri->lri_proto,
1054 ether_ntoa((struct ether_addr *)lri->lri_addr), ifname);
1055
1056 if (lri->lri_expire > time.tv_sec)
1057 printf("%-9.9s", sec2str(lri->lri_expire - time.tv_sec));
1058 else if (lri->lri_expire == 0)
1059 printf("%-9.9s", "permanent");
1060 else
1061 printf("%-9.9s", "expired");
1062
1063 printf(" %4d", lri->lri_refcnt);
1064 if (lri->lri_probes)
1065 printf(" %4d", lri->lri_probes);
1066 printf("\n");
1067 len -= sizeof (*lri);
1068 }
1069 if (len > 0) {
1070 fprintf(stderr, "warning: %u trailing bytes from %s\n",
1071 (unsigned int)len, "net.link.generic.system.llreach_info");
1072 }
1073
1074 out_free:
1075 free(buf);
1076 #undef MAX_SYSCTL_TRY
1077 }