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.12 2008/03/07 11:34:21 sephe Exp $
38 #include <sys/param.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/ethernet.h>
49 #include <netinet/in.h>
50 #include <netinet/in_var.h>
51 #include <netproto/ipx/ipx.h>
52 #include <netproto/ipx/ipx_if.h>
54 #include <netiso/iso.h>
55 #include <netiso/iso_var.h>
57 #include <arpa/inet.h>
70 static void sidewaysintpr (u_int, u_long);
71 static void catchalarm (int);
74 static char ntop_buf[INET6_ADDRSTRLEN]; /* for inet_ntop() */
79 * Display a formatted value, or a '-' in the same space.
82 show_stat(const char *fmt, int width, u_long value, short showvalue)
86 /* Construct the format string */
88 sprintf(newfmt, "%%%d%s", width, fmt);
89 printf(newfmt, value);
91 sprintf(newfmt, "%%%ds", width);
99 * Print a description of the network interfaces.
102 intpr(int interval1, u_long ifnetaddr, void (*pfunc)(char *))
105 struct ifaddr_container ifac;
106 struct ifnethead ifnethead;
111 struct in6_ifaddr in6;
113 struct ipx_ifaddr ipx;
115 struct iso_ifaddr iso;
119 u_long ifaddrcont_addr;
131 struct sockaddr *sa = NULL;
136 if (ifnetaddr == 0) {
137 printf("ifnet: symbol not defined\n");
141 sidewaysintpr((unsigned)interval1, ifnetaddr);
144 if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead))
146 ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead);
147 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
151 printf("%-7.7s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
152 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
154 printf(" %10.10s","Ibytes");
155 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
157 printf(" %10.10s","Obytes");
158 printf(" %5s", "Coll");
160 printf(" %s", "Time");
162 printf(" %s", "Drop");
166 while (ifnetaddr || ifaddraddr) {
167 struct sockaddr_in *sin;
169 struct sockaddr_in6 *sin6;
177 if (ifaddraddr == 0) {
178 struct ifaddrhead head;
180 ifnetfound = ifnetaddr;
181 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
183 strlcpy(name, ifnet.if_xname, sizeof(name));
184 ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link);
185 if (interface != 0 && (strcmp(name, interface) != 0))
187 cp = strchr(name, '\0');
194 if ((ifnet.if_flags&IFF_UP) == 0)
198 if (kread((u_long)ifnet.if_addrheads,
199 (char *)&head, sizeof(head)))
203 (u_long)TAILQ_FIRST(&head);
204 if (ifaddrcont_addr == 0) {
207 if (kread(ifaddrcont_addr, (char *)&ifac,
210 ifaddraddr = (u_long)ifac.ifa;
213 ifaddrfound = ifaddraddr;
216 * Get the interface stats. These may get
217 * overriden below on a per-interface basis.
219 opackets = ifnet.if_opackets;
220 ipackets = ifnet.if_ipackets;
221 obytes = ifnet.if_obytes;
222 ibytes = ifnet.if_ibytes;
223 oerrors = ifnet.if_oerrors;
224 ierrors = ifnet.if_ierrors;
225 collisions = ifnet.if_collisions;
226 timer = ifnet.if_timer;
227 drops = ifnet.if_snd.ifq_drops;
229 if (ifaddraddr == 0) {
230 printf("%-7.7s %-5lu ", name, ifnet.if_mtu);
231 printf("%-13.13s ", "none");
232 printf("%-15.15s ", "none");
234 if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
238 #define CP(x) ((char *)(x))
239 cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
241 sa = (struct sockaddr *)cp;
242 if (af != AF_UNSPEC && sa->sa_family != af) {
244 (u_long)TAILQ_NEXT(&ifac, ifa_link);
245 if (ifaddrcont_addr == 0) {
248 if (kread(ifaddrcont_addr,
249 (char *)&ifac, sizeof(ifac))) {
253 ifaddraddr = (u_long)ifac.ifa;
257 printf("%-7.7s %-5lu ", name, ifnet.if_mtu);
258 switch (sa->sa_family) {
260 printf("%-13.13s ", "none");
261 printf("%-15.15s ", "none");
264 sin = (struct sockaddr_in *)sa;
266 /* can't use inet_makeaddr because kernel
267 * keeps nets unshifted.
269 in = inet_makeaddr(ifaddr.in.ia_subnet,
271 printf("%-13.13s ", netname(in.s_addr,
272 ifaddr.in.ia_subnetmask));
275 netname(htonl(ifaddr.in.ia_subnet),
276 ifaddr.in.ia_subnetmask));
279 routename(sin->sin_addr.s_addr));
285 sin6 = (struct sockaddr_in6 *)sa;
287 netname6(&ifaddr.in6.ia_addr,
288 &ifaddr.in6.ia_prefixmask.sin6_addr));
292 ntop_buf, sizeof(ntop_buf)));
299 struct sockaddr_ipx *sipx =
300 (struct sockaddr_ipx *)sa;
304 *(union ipx_net *) &net = sipx->sipx_addr.x_net;
305 sprintf(netnum, "%lx", (u_long)ntohl(net));
306 printf("ipx:%-8s ", netnum);
307 /* printf("ipx:%-8s ", netname(net, 0L)); */
309 ipx_phost((struct sockaddr *)sipx));
314 printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
315 printf("%-9.9s ",atalk_print(sa,0x0b) );
319 struct sockaddr_dl *sdl =
320 (struct sockaddr_dl *)sa;
322 cp = (char *)LLADDR(sdl);
324 sprintf(linknum, "<Link#%d>", sdl->sdl_index);
325 m = printf("%-11.11s ", linknum);
329 m = printf("(%d)", sa->sa_family);
330 for (cp = sa->sa_len + (char *)sa;
331 --cp > sa->sa_data && (*cp == 0);) {}
332 n = cp - sa->sa_data + 1;
336 m += printf("%02x%c", *cp++ & 0xff,
347 * Fixup the statistics for interfaces that
348 * update stats for their network addresses
351 opackets = ifaddr.in.ia_ifa.if_opackets;
352 ipackets = ifaddr.in.ia_ifa.if_ipackets;
353 obytes = ifaddr.in.ia_ifa.if_obytes;
354 ibytes = ifaddr.in.ia_ifa.if_ibytes;
358 (u_long)TAILQ_NEXT(&ifac, ifa_link);
359 if (ifaddrcont_addr == 0) {
362 if (kread(ifaddrcont_addr,
363 (char *)&ifac, sizeof(ifac))) {
366 ifaddraddr = (u_long)ifac.ifa;
371 show_stat("lu", 8, ipackets, link_layer|network_layer);
373 show_stat("lu", 5, ierrors, link_layer);
376 show_stat("lu", 10, ibytes, link_layer|network_layer);
379 show_stat("lu", 8, opackets, link_layer|network_layer);
381 show_stat("lu", 5, oerrors, link_layer);
384 show_stat("lu", 10, obytes, link_layer|network_layer);
387 show_stat("lu", 5, collisions, link_layer);
390 show_stat("d", 3, timer, link_layer);
394 show_stat("d", 3, drops, link_layer);
397 if (aflag && ifaddrfound) {
399 * Print family's multicast addresses
401 struct ifmultiaddr *multiaddr;
402 struct ifmultiaddr ifma;
405 struct sockaddr_in in;
407 struct sockaddr_in6 in6;
409 struct sockaddr_dl dl;
413 TAILQ_FOREACH(multiaddr, &ifnet.if_multiaddrs, ifma_link) {
414 if (kread((u_long)multiaddr, (char *)&ifma,
418 if (kread((u_long)ifma.ifma_addr, (char *)&msa,
421 if (msa.sa.sa_family != sa->sa_family)
425 switch (msa.sa.sa_family) {
427 fmt = routename(msa.in.sin_addr.s_addr);
431 printf("%23s %-19.19s(refs: %d)\n", "",
440 switch (msa.dl.sdl_type) {
444 (struct ether_addr *)
451 printf("%23s %s\n", "", fmt);
458 SLIST_ENTRY(iftot) chain;
459 char ift_name[IFNAMSIZ]; /* interface name */
460 u_long ift_ip; /* input packets */
461 u_long ift_ie; /* input errors */
462 u_long ift_op; /* output packets */
463 u_long ift_oe; /* output errors */
464 u_long ift_co; /* collisions */
465 u_int ift_dr; /* drops */
466 u_long ift_ib; /* input bytes */
467 u_long ift_ob; /* output bytes */
470 u_char signalled; /* set if alarm goes off "early" */
473 * Print a running summary of interface statistics.
474 * Repeat display every interval1 seconds, showing statistics
475 * collected over that interval. Assumes that interval1 is non-zero.
476 * First line printed at top of screen is always cumulative.
477 * XXX - should be rewritten to use ifmib(4).
480 sidewaysintpr(unsigned interval1, u_long off)
484 struct ifnethead ifnethead;
485 struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
488 u_long interesting_off;
490 if (kread(off, (char *)&ifnethead, sizeof ifnethead))
492 firstifnet = (u_long)TAILQ_FIRST(&ifnethead);
494 if ((iftot = malloc(sizeof(struct iftot))) == NULL) {
495 printf("malloc failed\n");
498 memset(iftot, 0, sizeof(struct iftot));
502 for (off = firstifnet, ip = iftot; off;) {
505 if (kread(off, (char *)&ifnet, sizeof ifnet))
507 strlcpy(name, ifnet.if_xname, sizeof(name));
508 if (interface && strcmp(name, interface) == 0) {
510 interesting_off = off;
512 snprintf(ip->ift_name, 16, "(%s)", name);
513 if ((ipn = malloc(sizeof(struct iftot))) == NULL) {
514 printf("malloc failed\n");
517 memset(ipn, 0, sizeof(struct iftot));
518 SLIST_NEXT(ip, chain) = ipn;
520 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
522 if ((total = malloc(sizeof(struct iftot))) == NULL) {
523 printf("malloc failed\n");
526 memset(total, 0, sizeof(struct iftot));
527 if ((sum = malloc(sizeof(struct iftot))) == NULL) {
528 printf("malloc failed\n");
531 memset(sum, 0, sizeof(struct iftot));
534 (void)signal(SIGALRM, catchalarm);
536 (void)alarm(interval1);
539 printf("%17s %14s %16s", "input",
540 interesting ? interesting->ift_name : "(Total)", "output");
542 printf("%10s %5s %10s %10s %5s %10s %5s",
543 "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
545 printf(" %5.5s", "drops");
550 if (interesting != NULL) {
552 if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) {
557 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
558 ifnet.if_ipackets - ip->ift_ip,
559 ifnet.if_ierrors - ip->ift_ie,
560 ifnet.if_ibytes - ip->ift_ib,
561 ifnet.if_opackets - ip->ift_op,
562 ifnet.if_oerrors - ip->ift_oe,
563 ifnet.if_obytes - ip->ift_ob,
564 ifnet.if_collisions - ip->ift_co);
566 printf(" %5u", ifnet.if_snd.ifq_drops - ip->ift_dr);
568 ip->ift_ip = ifnet.if_ipackets;
569 ip->ift_ie = ifnet.if_ierrors;
570 ip->ift_ib = ifnet.if_ibytes;
571 ip->ift_op = ifnet.if_opackets;
572 ip->ift_oe = ifnet.if_oerrors;
573 ip->ift_ob = ifnet.if_obytes;
574 ip->ift_co = ifnet.if_collisions;
575 ip->ift_dr = ifnet.if_snd.ifq_drops;
585 for (off = firstifnet, ip = iftot;
586 off && SLIST_NEXT(ip, chain) != NULL;
587 ip = SLIST_NEXT(ip, chain)) {
588 if (kread(off, (char *)&ifnet, sizeof ifnet)) {
592 sum->ift_ip += ifnet.if_ipackets;
593 sum->ift_ie += ifnet.if_ierrors;
594 sum->ift_ib += ifnet.if_ibytes;
595 sum->ift_op += ifnet.if_opackets;
596 sum->ift_oe += ifnet.if_oerrors;
597 sum->ift_ob += ifnet.if_obytes;
598 sum->ift_co += ifnet.if_collisions;
599 sum->ift_dr += ifnet.if_snd.ifq_drops;
600 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
603 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
604 sum->ift_ip - total->ift_ip,
605 sum->ift_ie - total->ift_ie,
606 sum->ift_ib - total->ift_ib,
607 sum->ift_op - total->ift_op,
608 sum->ift_oe - total->ift_oe,
609 sum->ift_ob - total->ift_ob,
610 sum->ift_co - total->ift_co);
612 printf(" %5u", sum->ift_dr - total->ift_dr);
619 oldmask = sigblock(sigmask(SIGALRM));
625 (void)alarm(interval1);
636 * Called if an interval expires before sidewaysintpr has completed a loop.
637 * Sets a flag to not wait for the alarm.
640 catchalarm(int signo __unused)