2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#)if.c 8.3 (Berkeley) 4/28/95
34 * $FreeBSD: src/usr.bin/netstat/if.c,v 1.32.2.9 2001/09/17 14:35:46 ru Exp $
35 * $DragonFly: src/usr.bin/netstat/if.c,v 1.3 2003/08/08 04:18:43 dillon Exp $
38 #include <sys/types.h>
39 #include <sys/protosw.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
45 #include <net/if_var.h>
46 #include <net/if_dl.h>
47 #include <net/if_types.h>
48 #include <net/bridge/bridge.h>
49 #include <net/ethernet.h>
50 #include <netinet/in.h>
51 #include <netinet/in_var.h>
52 #include <netproto/ipx/ipx.h>
53 #include <netproto/ipx/ipx_if.h>
55 #include <netproto/ns/ns.h>
56 #include <netproto/ns/ns_if.h>
59 #include <netiso/iso.h>
60 #include <netiso/iso_var.h>
62 #include <arpa/inet.h>
75 static void sidewaysintpr (u_int, u_long);
76 static void catchalarm (int);
79 char *netname6 (struct sockaddr_in6 *, struct in6_addr *);
80 static char ntop_buf[INET6_ADDRSTRLEN]; /* for inet_ntop() */
84 /* print bridge statistics */
86 bdg_stats(u_long dummy __unused, char *name, int af __unused)
99 if (sysctl(mib,4, &s,&slen,NULL,0)==-1)
100 return ; /* no bridging */
107 printf("-- Bridging statistics (%s) --\n", name) ;
109 "Name In Out Forward Drop Bcast Mcast Local Unknown\n");
110 for (i = 0 ; i < 16 ; i++) {
112 printf("%-6s %9ld%9ld%9ld%9ld%9ld%9ld%9ld%9ld\n",
114 s.s[i].p_in[(int)BDG_IN],
115 s.s[i].p_in[(int)BDG_OUT],
116 s.s[i].p_in[(int)BDG_FORWARD],
117 s.s[i].p_in[(int)BDG_DROP],
118 s.s[i].p_in[(int)BDG_BCAST],
119 s.s[i].p_in[(int)BDG_MCAST],
120 s.s[i].p_in[(int)BDG_LOCAL],
121 s.s[i].p_in[(int)BDG_UNKNOWN] );
129 * Display a formatted value, or a '-' in the same space.
132 show_stat(const char *fmt, int width, u_long value, short showvalue)
136 /* Construct the format string */
138 sprintf(newfmt, "%%%d%s", width, fmt);
139 printf(newfmt, value);
141 sprintf(newfmt, "%%%ds", width);
149 * Print a description of the network interfaces.
152 intpr(int interval, u_long ifnetaddr, void (*pfunc)(char *))
155 struct ifnethead ifnethead;
160 struct in6_ifaddr in6;
162 struct ipx_ifaddr ipx;
167 struct iso_ifaddr iso;
182 struct sockaddr *sa = NULL;
183 char name[32], tname[16];
187 if (ifnetaddr == 0) {
188 printf("ifnet: symbol not defined\n");
192 sidewaysintpr((unsigned)interval, ifnetaddr);
195 if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead))
197 ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead);
198 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
202 printf("%-5.5s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
203 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
205 printf(" %10.10s","Ibytes");
206 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
208 printf(" %10.10s","Obytes");
209 printf(" %5s", "Coll");
211 printf(" %s", "Time");
213 printf(" %s", "Drop");
217 while (ifnetaddr || ifaddraddr) {
218 struct sockaddr_in *sin;
220 struct sockaddr_in6 *sin6;
228 if (ifaddraddr == 0) {
229 ifnetfound = ifnetaddr;
230 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) ||
231 kread((u_long)ifnet.if_name, tname, 16))
234 ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link);
235 snprintf(name, 32, "%s%d", tname, ifnet.if_unit);
236 if (interface != 0 && (strcmp(name, interface) != 0))
238 cp = index(name, '\0');
245 if ((ifnet.if_flags&IFF_UP) == 0)
248 ifaddraddr = (u_long)TAILQ_FIRST(&ifnet.if_addrhead);
250 ifaddrfound = ifaddraddr;
253 * Get the interface stats. These may get
254 * overriden below on a per-interface basis.
256 opackets = ifnet.if_opackets;
257 ipackets = ifnet.if_ipackets;
258 obytes = ifnet.if_obytes;
259 ibytes = ifnet.if_ibytes;
260 oerrors = ifnet.if_oerrors;
261 ierrors = ifnet.if_ierrors;
262 collisions = ifnet.if_collisions;
263 timer = ifnet.if_timer;
264 drops = ifnet.if_snd.ifq_drops;
266 if (ifaddraddr == 0) {
267 printf("%-5.5s %-5lu ", name, ifnet.if_mtu);
268 printf("%-13.13s ", "none");
269 printf("%-15.15s ", "none");
271 if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
275 #define CP(x) ((char *)(x))
276 cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
278 sa = (struct sockaddr *)cp;
279 if (af != AF_UNSPEC && sa->sa_family != af) {
281 (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
284 printf("%-5.5s %-5lu ", name, ifnet.if_mtu);
285 switch (sa->sa_family) {
287 printf("%-13.13s ", "none");
288 printf("%-15.15s ", "none");
291 sin = (struct sockaddr_in *)sa;
293 /* can't use inet_makeaddr because kernel
294 * keeps nets unshifted.
296 in = inet_makeaddr(ifaddr.in.ia_subnet,
298 printf("%-13.13s ", netname(in.s_addr,
299 ifaddr.in.ia_subnetmask));
302 netname(htonl(ifaddr.in.ia_subnet),
303 ifaddr.in.ia_subnetmask));
306 routename(sin->sin_addr.s_addr));
312 sin6 = (struct sockaddr_in6 *)sa;
314 netname6(&ifaddr.in6.ia_addr,
315 &ifaddr.in6.ia_prefixmask.sin6_addr));
317 (char *)inet_ntop(AF_INET6,
319 ntop_buf, sizeof(ntop_buf)));
326 struct sockaddr_ipx *sipx =
327 (struct sockaddr_ipx *)sa;
331 *(union ipx_net *) &net = sipx->sipx_addr.x_net;
332 sprintf(netnum, "%lx", (u_long)ntohl(net));
333 printf("ipx:%-8s ", netnum);
334 /* printf("ipx:%-8s ", netname(net, 0L)); */
336 ipx_phost((struct sockaddr *)sipx));
341 printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
342 printf("%-9.9s ",atalk_print(sa,0x0b) );
347 struct sockaddr_ns *sns =
348 (struct sockaddr_ns *)sa;
352 *(union ns_net *) &net = sns->sns_addr.x_net;
353 sprintf(netnum, "%lxH", ntohl(net));
355 printf("ns:%-8s ", netnum);
357 ns_phost((struct sockaddr *)sns));
363 struct sockaddr_dl *sdl =
364 (struct sockaddr_dl *)sa;
366 cp = (char *)LLADDR(sdl);
368 sprintf(linknum, "<Link#%d>", sdl->sdl_index);
369 m = printf("%-11.11s ", linknum);
373 m = printf("(%d)", sa->sa_family);
374 for (cp = sa->sa_len + (char *)sa;
375 --cp > sa->sa_data && (*cp == 0);) {}
376 n = cp - sa->sa_data + 1;
380 m += printf("%02x%c", *cp++ & 0xff,
391 * Fixup the statistics for interfaces that
392 * update stats for their network addresses
395 opackets = ifaddr.in.ia_ifa.if_opackets;
396 ipackets = ifaddr.in.ia_ifa.if_ipackets;
397 obytes = ifaddr.in.ia_ifa.if_obytes;
398 ibytes = ifaddr.in.ia_ifa.if_ibytes;
401 ifaddraddr = (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
404 show_stat("lu", 8, ipackets, link_layer|network_layer);
406 show_stat("lu", 5, ierrors, link_layer);
409 show_stat("lu", 10, ibytes, link_layer|network_layer);
412 show_stat("lu", 8, opackets, link_layer|network_layer);
414 show_stat("lu", 5, oerrors, link_layer);
417 show_stat("lu", 10, obytes, link_layer|network_layer);
420 show_stat("lu", 5, collisions, link_layer);
423 show_stat("d", 3, timer, link_layer);
427 show_stat("d", 3, drops, link_layer);
430 if (aflag && ifaddrfound) {
432 * Print family's multicast addresses
435 struct ifmultiaddr ifma;
438 struct sockaddr_in in;
440 struct sockaddr_in6 in6;
442 struct sockaddr_dl dl;
446 for(multiaddr = (u_long)ifnet.if_multiaddrs.lh_first;
448 multiaddr = (u_long)ifma.ifma_link.le_next) {
449 if (kread(multiaddr, (char *)&ifma,
452 if (kread((u_long)ifma.ifma_addr, (char *)&msa,
455 if (msa.sa.sa_family != sa->sa_family)
459 switch (msa.sa.sa_family) {
461 fmt = routename(msa.in.sin_addr.s_addr);
465 printf("%23s %-19.19s(refs: %d)\n", "",
474 switch (msa.dl.sdl_type) {
478 (struct ether_addr *)
485 printf("%23s %s\n", "", fmt);
492 SLIST_ENTRY(iftot) chain;
493 char ift_name[16]; /* interface name */
494 u_long ift_ip; /* input packets */
495 u_long ift_ie; /* input errors */
496 u_long ift_op; /* output packets */
497 u_long ift_oe; /* output errors */
498 u_long ift_co; /* collisions */
499 u_int ift_dr; /* drops */
500 u_long ift_ib; /* input bytes */
501 u_long ift_ob; /* output bytes */
504 u_char signalled; /* set if alarm goes off "early" */
507 * Print a running summary of interface statistics.
508 * Repeat display every interval seconds, showing statistics
509 * collected over that interval. Assumes that interval is non-zero.
510 * First line printed at top of screen is always cumulative.
511 * XXX - should be rewritten to use ifmib(4).
514 sidewaysintpr(unsigned interval, u_long off)
518 struct ifnethead ifnethead;
519 struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
522 u_long interesting_off;
524 if (kread(off, (char *)&ifnethead, sizeof ifnethead))
526 firstifnet = (u_long)TAILQ_FIRST(&ifnethead);
528 if ((iftot = malloc(sizeof(struct iftot))) == NULL) {
529 printf("malloc failed\n");
532 memset(iftot, 0, sizeof(struct iftot));
536 for (off = firstifnet, ip = iftot; off;) {
537 char name[16], tname[16];
539 if (kread(off, (char *)&ifnet, sizeof ifnet))
541 if (kread((u_long)ifnet.if_name, tname, 16))
544 snprintf(name, 16, "%s%d", tname, ifnet.if_unit);
545 if (interface && strcmp(name, interface) == 0) {
547 interesting_off = off;
549 snprintf(ip->ift_name, 16, "(%s)", name);;
550 if ((ipn = malloc(sizeof(struct iftot))) == NULL) {
551 printf("malloc failed\n");
554 memset(ipn, 0, sizeof(struct iftot));
555 SLIST_NEXT(ip, chain) = ipn;
557 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
559 if ((total = malloc(sizeof(struct iftot))) == NULL) {
560 printf("malloc failed\n");
563 memset(total, 0, sizeof(struct iftot));
564 if ((sum = malloc(sizeof(struct iftot))) == NULL) {
565 printf("malloc failed\n");
568 memset(sum, 0, sizeof(struct iftot));
571 (void)signal(SIGALRM, catchalarm);
573 (void)alarm(interval);
576 printf("%17s %14s %16s", "input",
577 interesting ? interesting->ift_name : "(Total)", "output");
579 printf("%10s %5s %10s %10s %5s %10s %5s",
580 "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
582 printf(" %5.5s", "drops");
587 if (interesting != NULL) {
589 if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) {
594 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
595 ifnet.if_ipackets - ip->ift_ip,
596 ifnet.if_ierrors - ip->ift_ie,
597 ifnet.if_ibytes - ip->ift_ib,
598 ifnet.if_opackets - ip->ift_op,
599 ifnet.if_oerrors - ip->ift_oe,
600 ifnet.if_obytes - ip->ift_ob,
601 ifnet.if_collisions - ip->ift_co);
603 printf(" %5u", ifnet.if_snd.ifq_drops - ip->ift_dr);
605 ip->ift_ip = ifnet.if_ipackets;
606 ip->ift_ie = ifnet.if_ierrors;
607 ip->ift_ib = ifnet.if_ibytes;
608 ip->ift_op = ifnet.if_opackets;
609 ip->ift_oe = ifnet.if_oerrors;
610 ip->ift_ob = ifnet.if_obytes;
611 ip->ift_co = ifnet.if_collisions;
612 ip->ift_dr = ifnet.if_snd.ifq_drops;
622 for (off = firstifnet, ip = iftot;
623 off && SLIST_NEXT(ip, chain) != NULL;
624 ip = SLIST_NEXT(ip, chain)) {
625 if (kread(off, (char *)&ifnet, sizeof ifnet)) {
629 sum->ift_ip += ifnet.if_ipackets;
630 sum->ift_ie += ifnet.if_ierrors;
631 sum->ift_ib += ifnet.if_ibytes;
632 sum->ift_op += ifnet.if_opackets;
633 sum->ift_oe += ifnet.if_oerrors;
634 sum->ift_ob += ifnet.if_obytes;
635 sum->ift_co += ifnet.if_collisions;
636 sum->ift_dr += ifnet.if_snd.ifq_drops;
637 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
640 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
641 sum->ift_ip - total->ift_ip,
642 sum->ift_ie - total->ift_ie,
643 sum->ift_ib - total->ift_ib,
644 sum->ift_op - total->ift_op,
645 sum->ift_oe - total->ift_oe,
646 sum->ift_ob - total->ift_ob,
647 sum->ift_co - total->ift_co);
649 printf(" %5u", sum->ift_dr - total->ift_dr);
656 oldmask = sigblock(sigmask(SIGALRM));
662 (void)alarm(interval);
673 * Called if an interval expires before sidewaysintpr has completed a loop.
674 * Sets a flag to not wait for the alarm.
677 catchalarm(int signo __unused)