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 $
37 #define _KERNEL_STRUCTURES
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));
315 struct sockaddr_dl *sdl =
316 (struct sockaddr_dl *)sa;
318 cp = (char *)LLADDR(sdl);
320 sprintf(linknum, "<Link#%d>", sdl->sdl_index);
321 m = printf("%-11.11s ", linknum);
325 m = printf("(%d)", sa->sa_family);
326 for (cp = sa->sa_len + (char *)sa;
327 --cp > sa->sa_data && (*cp == 0);) {}
328 n = cp - sa->sa_data + 1;
332 m += printf("%02x%c", *cp++ & 0xff,
343 * Fixup the statistics for interfaces that
344 * update stats for their network addresses
347 opackets = ifaddr.in.ia_ifa.if_opackets;
348 ipackets = ifaddr.in.ia_ifa.if_ipackets;
349 obytes = ifaddr.in.ia_ifa.if_obytes;
350 ibytes = ifaddr.in.ia_ifa.if_ibytes;
354 (u_long)TAILQ_NEXT(&ifac, ifa_link);
355 if (ifaddrcont_addr == 0) {
358 if (kread(ifaddrcont_addr,
359 (char *)&ifac, sizeof(ifac))) {
362 ifaddraddr = (u_long)ifac.ifa;
367 show_stat("lu", 8, ipackets, link_layer|network_layer);
369 show_stat("lu", 5, ierrors, link_layer);
372 show_stat("lu", 10, ibytes, link_layer|network_layer);
375 show_stat("lu", 8, opackets, link_layer|network_layer);
377 show_stat("lu", 5, oerrors, link_layer);
380 show_stat("lu", 10, obytes, link_layer|network_layer);
383 show_stat("lu", 5, collisions, link_layer);
386 show_stat("d", 3, timer, link_layer);
390 show_stat("d", 3, drops, link_layer);
393 if (aflag && ifaddrfound) {
395 * Print family's multicast addresses
397 struct ifmultiaddr *multiaddr;
398 struct ifmultiaddr ifma;
401 struct sockaddr_in in;
403 struct sockaddr_in6 in6;
405 struct sockaddr_dl dl;
409 TAILQ_FOREACH(multiaddr, &ifnet.if_multiaddrs, ifma_link) {
410 if (kread((u_long)multiaddr, (char *)&ifma,
414 if (kread((u_long)ifma.ifma_addr, (char *)&msa,
417 if (msa.sa.sa_family != sa->sa_family)
421 switch (msa.sa.sa_family) {
423 fmt = routename(msa.in.sin_addr.s_addr);
427 printf("%23s %-19.19s(refs: %d)\n", "",
436 switch (msa.dl.sdl_type) {
440 (struct ether_addr *)
447 printf("%23s %s\n", "", fmt);
454 SLIST_ENTRY(iftot) chain;
455 char ift_name[IFNAMSIZ]; /* interface name */
456 u_long ift_ip; /* input packets */
457 u_long ift_ie; /* input errors */
458 u_long ift_op; /* output packets */
459 u_long ift_oe; /* output errors */
460 u_long ift_co; /* collisions */
461 u_int ift_dr; /* drops */
462 u_long ift_ib; /* input bytes */
463 u_long ift_ob; /* output bytes */
466 u_char signalled; /* set if alarm goes off "early" */
469 * Print a running summary of interface statistics.
470 * Repeat display every interval1 seconds, showing statistics
471 * collected over that interval. Assumes that interval1 is non-zero.
472 * First line printed at top of screen is always cumulative.
473 * XXX - should be rewritten to use ifmib(4).
476 sidewaysintpr(unsigned interval1, u_long off)
480 struct ifnethead ifnethead;
481 struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
484 u_long interesting_off;
486 if (kread(off, (char *)&ifnethead, sizeof ifnethead))
488 firstifnet = (u_long)TAILQ_FIRST(&ifnethead);
490 if ((iftot = malloc(sizeof(struct iftot))) == NULL) {
491 printf("malloc failed\n");
494 memset(iftot, 0, sizeof(struct iftot));
498 for (off = firstifnet, ip = iftot; off;) {
501 if (kread(off, (char *)&ifnet, sizeof ifnet))
503 strlcpy(name, ifnet.if_xname, sizeof(name));
504 if (interface && strcmp(name, interface) == 0) {
506 interesting_off = off;
508 snprintf(ip->ift_name, 16, "(%s)", name);
509 if ((ipn = malloc(sizeof(struct iftot))) == NULL) {
510 printf("malloc failed\n");
513 memset(ipn, 0, sizeof(struct iftot));
514 SLIST_NEXT(ip, chain) = ipn;
516 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
518 if ((total = malloc(sizeof(struct iftot))) == NULL) {
519 printf("malloc failed\n");
522 memset(total, 0, sizeof(struct iftot));
523 if ((sum = malloc(sizeof(struct iftot))) == NULL) {
524 printf("malloc failed\n");
527 memset(sum, 0, sizeof(struct iftot));
530 (void)signal(SIGALRM, catchalarm);
532 (void)alarm(interval1);
535 printf("%17s %14s %16s", "input",
536 interesting ? interesting->ift_name : "(Total)", "output");
538 printf("%10s %5s %10s %10s %5s %10s %5s",
539 "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
541 printf(" %5.5s", "drops");
546 if (interesting != NULL) {
548 if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) {
553 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
554 ifnet.if_ipackets - ip->ift_ip,
555 ifnet.if_ierrors - ip->ift_ie,
556 ifnet.if_ibytes - ip->ift_ib,
557 ifnet.if_opackets - ip->ift_op,
558 ifnet.if_oerrors - ip->ift_oe,
559 ifnet.if_obytes - ip->ift_ob,
560 ifnet.if_collisions - ip->ift_co);
562 printf(" %5u", ifnet.if_snd.ifq_drops - ip->ift_dr);
564 ip->ift_ip = ifnet.if_ipackets;
565 ip->ift_ie = ifnet.if_ierrors;
566 ip->ift_ib = ifnet.if_ibytes;
567 ip->ift_op = ifnet.if_opackets;
568 ip->ift_oe = ifnet.if_oerrors;
569 ip->ift_ob = ifnet.if_obytes;
570 ip->ift_co = ifnet.if_collisions;
571 ip->ift_dr = ifnet.if_snd.ifq_drops;
581 for (off = firstifnet, ip = iftot;
582 off && SLIST_NEXT(ip, chain) != NULL;
583 ip = SLIST_NEXT(ip, chain)) {
584 if (kread(off, (char *)&ifnet, sizeof ifnet)) {
588 sum->ift_ip += ifnet.if_ipackets;
589 sum->ift_ie += ifnet.if_ierrors;
590 sum->ift_ib += ifnet.if_ibytes;
591 sum->ift_op += ifnet.if_opackets;
592 sum->ift_oe += ifnet.if_oerrors;
593 sum->ift_ob += ifnet.if_obytes;
594 sum->ift_co += ifnet.if_collisions;
595 sum->ift_dr += ifnet.if_snd.ifq_drops;
596 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
599 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
600 sum->ift_ip - total->ift_ip,
601 sum->ift_ie - total->ift_ie,
602 sum->ift_ib - total->ift_ib,
603 sum->ift_op - total->ift_op,
604 sum->ift_oe - total->ift_oe,
605 sum->ift_ob - total->ift_ob,
606 sum->ift_co - total->ift_co);
608 printf(" %5u", sum->ift_dr - total->ift_dr);
615 oldmask = sigblock(sigmask(SIGALRM));
621 (void)alarm(interval1);
632 * Called if an interval expires before sidewaysintpr has completed a loop.
633 * Sets a flag to not wait for the alarm.
636 catchalarm(int signo __unused)