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 <netproto/ns/ns.h>
55 #include <netproto/ns/ns_if.h>
58 #include <netiso/iso.h>
59 #include <netiso/iso_var.h>
61 #include <arpa/inet.h>
74 static void sidewaysintpr (u_int, u_long);
75 static void catchalarm (int);
78 char *netname6 (struct sockaddr_in6 *, struct in6_addr *);
79 static char ntop_buf[INET6_ADDRSTRLEN]; /* for inet_ntop() */
84 * Display a formatted value, or a '-' in the same space.
87 show_stat(const char *fmt, int width, u_long value, short showvalue)
91 /* Construct the format string */
93 sprintf(newfmt, "%%%d%s", width, fmt);
94 printf(newfmt, value);
96 sprintf(newfmt, "%%%ds", width);
104 * Print a description of the network interfaces.
107 intpr(int interval, u_long ifnetaddr, void (*pfunc)(char *))
110 struct ifaddr_container ifac;
111 struct ifnethead ifnethead;
116 struct in6_ifaddr in6;
118 struct ipx_ifaddr ipx;
123 struct iso_ifaddr iso;
127 u_long ifaddrcont_addr;
139 struct sockaddr *sa = NULL;
144 if (ifnetaddr == 0) {
145 printf("ifnet: symbol not defined\n");
149 sidewaysintpr((unsigned)interval, ifnetaddr);
152 if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead))
154 ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead);
155 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
159 printf("%-7.7s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
160 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
162 printf(" %10.10s","Ibytes");
163 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
165 printf(" %10.10s","Obytes");
166 printf(" %5s", "Coll");
168 printf(" %s", "Time");
170 printf(" %s", "Drop");
174 while (ifnetaddr || ifaddraddr) {
175 struct sockaddr_in *sin;
177 struct sockaddr_in6 *sin6;
185 if (ifaddraddr == 0) {
186 struct ifaddrhead head;
188 ifnetfound = ifnetaddr;
189 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
191 strlcpy(name, ifnet.if_xname, sizeof(name));
192 ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link);
193 if (interface != 0 && (strcmp(name, interface) != 0))
195 cp = strchr(name, '\0');
202 if ((ifnet.if_flags&IFF_UP) == 0)
206 if (kread((u_long)ifnet.if_addrheads,
207 (char *)&head, sizeof(head)))
211 (u_long)TAILQ_FIRST(&head);
212 if (ifaddrcont_addr == 0) {
215 if (kread(ifaddrcont_addr, (char *)&ifac,
218 ifaddraddr = (u_long)ifac.ifa;
221 ifaddrfound = ifaddraddr;
224 * Get the interface stats. These may get
225 * overriden below on a per-interface basis.
227 opackets = ifnet.if_opackets;
228 ipackets = ifnet.if_ipackets;
229 obytes = ifnet.if_obytes;
230 ibytes = ifnet.if_ibytes;
231 oerrors = ifnet.if_oerrors;
232 ierrors = ifnet.if_ierrors;
233 collisions = ifnet.if_collisions;
234 timer = ifnet.if_timer;
235 drops = ifnet.if_snd.ifq_drops;
237 if (ifaddraddr == 0) {
238 printf("%-7.7s %-5lu ", name, ifnet.if_mtu);
239 printf("%-13.13s ", "none");
240 printf("%-15.15s ", "none");
242 if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
246 #define CP(x) ((char *)(x))
247 cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
249 sa = (struct sockaddr *)cp;
250 if (af != AF_UNSPEC && sa->sa_family != af) {
252 (u_long)TAILQ_NEXT(&ifac, ifa_link);
253 if (ifaddrcont_addr == 0) {
256 if (kread(ifaddrcont_addr,
257 (char *)&ifac, sizeof(ifac))) {
261 ifaddraddr = (u_long)ifac.ifa;
265 printf("%-7.7s %-5lu ", name, ifnet.if_mtu);
266 switch (sa->sa_family) {
268 printf("%-13.13s ", "none");
269 printf("%-15.15s ", "none");
272 sin = (struct sockaddr_in *)sa;
274 /* can't use inet_makeaddr because kernel
275 * keeps nets unshifted.
277 in = inet_makeaddr(ifaddr.in.ia_subnet,
279 printf("%-13.13s ", netname(in.s_addr,
280 ifaddr.in.ia_subnetmask));
283 netname(htonl(ifaddr.in.ia_subnet),
284 ifaddr.in.ia_subnetmask));
287 routename(sin->sin_addr.s_addr));
293 sin6 = (struct sockaddr_in6 *)sa;
295 netname6(&ifaddr.in6.ia_addr,
296 &ifaddr.in6.ia_prefixmask.sin6_addr));
298 (char *)inet_ntop(AF_INET6,
300 ntop_buf, sizeof(ntop_buf)));
307 struct sockaddr_ipx *sipx =
308 (struct sockaddr_ipx *)sa;
312 *(union ipx_net *) &net = sipx->sipx_addr.x_net;
313 sprintf(netnum, "%lx", (u_long)ntohl(net));
314 printf("ipx:%-8s ", netnum);
315 /* printf("ipx:%-8s ", netname(net, 0L)); */
317 ipx_phost((struct sockaddr *)sipx));
322 printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
323 printf("%-9.9s ",atalk_print(sa,0x0b) );
328 struct sockaddr_ns *sns =
329 (struct sockaddr_ns *)sa;
333 *(union ns_net *) &net = sns->sns_addr.x_net;
334 sprintf(netnum, "%lxH", ntohl(net));
336 printf("ns:%-8s ", netnum);
338 ns_phost((struct sockaddr *)sns));
344 struct sockaddr_dl *sdl =
345 (struct sockaddr_dl *)sa;
347 cp = (char *)LLADDR(sdl);
349 sprintf(linknum, "<Link#%d>", sdl->sdl_index);
350 m = printf("%-11.11s ", linknum);
354 m = printf("(%d)", sa->sa_family);
355 for (cp = sa->sa_len + (char *)sa;
356 --cp > sa->sa_data && (*cp == 0);) {}
357 n = cp - sa->sa_data + 1;
361 m += printf("%02x%c", *cp++ & 0xff,
372 * Fixup the statistics for interfaces that
373 * update stats for their network addresses
376 opackets = ifaddr.in.ia_ifa.if_opackets;
377 ipackets = ifaddr.in.ia_ifa.if_ipackets;
378 obytes = ifaddr.in.ia_ifa.if_obytes;
379 ibytes = ifaddr.in.ia_ifa.if_ibytes;
383 (u_long)TAILQ_NEXT(&ifac, ifa_link);
384 if (ifaddrcont_addr == 0) {
387 if (kread(ifaddrcont_addr,
388 (char *)&ifac, sizeof(ifac))) {
391 ifaddraddr = (u_long)ifac.ifa;
396 show_stat("lu", 8, ipackets, link_layer|network_layer);
398 show_stat("lu", 5, ierrors, link_layer);
401 show_stat("lu", 10, ibytes, link_layer|network_layer);
404 show_stat("lu", 8, opackets, link_layer|network_layer);
406 show_stat("lu", 5, oerrors, link_layer);
409 show_stat("lu", 10, obytes, link_layer|network_layer);
412 show_stat("lu", 5, collisions, link_layer);
415 show_stat("d", 3, timer, link_layer);
419 show_stat("d", 3, drops, link_layer);
422 if (aflag && ifaddrfound) {
424 * Print family's multicast addresses
427 struct ifmultiaddr ifma;
430 struct sockaddr_in in;
432 struct sockaddr_in6 in6;
434 struct sockaddr_dl dl;
438 for(multiaddr = (u_long)ifnet.if_multiaddrs.lh_first;
440 multiaddr = (u_long)ifma.ifma_link.le_next) {
441 if (kread(multiaddr, (char *)&ifma,
444 if (kread((u_long)ifma.ifma_addr, (char *)&msa,
447 if (msa.sa.sa_family != sa->sa_family)
451 switch (msa.sa.sa_family) {
453 fmt = routename(msa.in.sin_addr.s_addr);
457 printf("%23s %-19.19s(refs: %d)\n", "",
466 switch (msa.dl.sdl_type) {
470 (struct ether_addr *)
477 printf("%23s %s\n", "", fmt);
484 SLIST_ENTRY(iftot) chain;
485 char ift_name[IFNAMSIZ]; /* interface name */
486 u_long ift_ip; /* input packets */
487 u_long ift_ie; /* input errors */
488 u_long ift_op; /* output packets */
489 u_long ift_oe; /* output errors */
490 u_long ift_co; /* collisions */
491 u_int ift_dr; /* drops */
492 u_long ift_ib; /* input bytes */
493 u_long ift_ob; /* output bytes */
496 u_char signalled; /* set if alarm goes off "early" */
499 * Print a running summary of interface statistics.
500 * Repeat display every interval seconds, showing statistics
501 * collected over that interval. Assumes that interval is non-zero.
502 * First line printed at top of screen is always cumulative.
503 * XXX - should be rewritten to use ifmib(4).
506 sidewaysintpr(unsigned interval, u_long off)
510 struct ifnethead ifnethead;
511 struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
514 u_long interesting_off;
516 if (kread(off, (char *)&ifnethead, sizeof ifnethead))
518 firstifnet = (u_long)TAILQ_FIRST(&ifnethead);
520 if ((iftot = malloc(sizeof(struct iftot))) == NULL) {
521 printf("malloc failed\n");
524 memset(iftot, 0, sizeof(struct iftot));
528 for (off = firstifnet, ip = iftot; off;) {
531 if (kread(off, (char *)&ifnet, sizeof ifnet))
533 strlcpy(name, ifnet.if_xname, sizeof(name));
534 if (interface && strcmp(name, interface) == 0) {
536 interesting_off = off;
538 snprintf(ip->ift_name, 16, "(%s)", name);
539 if ((ipn = malloc(sizeof(struct iftot))) == NULL) {
540 printf("malloc failed\n");
543 memset(ipn, 0, sizeof(struct iftot));
544 SLIST_NEXT(ip, chain) = ipn;
546 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
548 if ((total = malloc(sizeof(struct iftot))) == NULL) {
549 printf("malloc failed\n");
552 memset(total, 0, sizeof(struct iftot));
553 if ((sum = malloc(sizeof(struct iftot))) == NULL) {
554 printf("malloc failed\n");
557 memset(sum, 0, sizeof(struct iftot));
560 (void)signal(SIGALRM, catchalarm);
562 (void)alarm(interval);
565 printf("%17s %14s %16s", "input",
566 interesting ? interesting->ift_name : "(Total)", "output");
568 printf("%10s %5s %10s %10s %5s %10s %5s",
569 "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
571 printf(" %5.5s", "drops");
576 if (interesting != NULL) {
578 if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) {
583 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
584 ifnet.if_ipackets - ip->ift_ip,
585 ifnet.if_ierrors - ip->ift_ie,
586 ifnet.if_ibytes - ip->ift_ib,
587 ifnet.if_opackets - ip->ift_op,
588 ifnet.if_oerrors - ip->ift_oe,
589 ifnet.if_obytes - ip->ift_ob,
590 ifnet.if_collisions - ip->ift_co);
592 printf(" %5u", ifnet.if_snd.ifq_drops - ip->ift_dr);
594 ip->ift_ip = ifnet.if_ipackets;
595 ip->ift_ie = ifnet.if_ierrors;
596 ip->ift_ib = ifnet.if_ibytes;
597 ip->ift_op = ifnet.if_opackets;
598 ip->ift_oe = ifnet.if_oerrors;
599 ip->ift_ob = ifnet.if_obytes;
600 ip->ift_co = ifnet.if_collisions;
601 ip->ift_dr = ifnet.if_snd.ifq_drops;
611 for (off = firstifnet, ip = iftot;
612 off && SLIST_NEXT(ip, chain) != NULL;
613 ip = SLIST_NEXT(ip, chain)) {
614 if (kread(off, (char *)&ifnet, sizeof ifnet)) {
618 sum->ift_ip += ifnet.if_ipackets;
619 sum->ift_ie += ifnet.if_ierrors;
620 sum->ift_ib += ifnet.if_ibytes;
621 sum->ift_op += ifnet.if_opackets;
622 sum->ift_oe += ifnet.if_oerrors;
623 sum->ift_ob += ifnet.if_obytes;
624 sum->ift_co += ifnet.if_collisions;
625 sum->ift_dr += ifnet.if_snd.ifq_drops;
626 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
629 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
630 sum->ift_ip - total->ift_ip,
631 sum->ift_ie - total->ift_ie,
632 sum->ift_ib - total->ift_ib,
633 sum->ift_op - total->ift_op,
634 sum->ift_oe - total->ift_oe,
635 sum->ift_ob - total->ift_ob,
636 sum->ift_co - total->ift_co);
638 printf(" %5u", sum->ift_dr - total->ift_dr);
645 oldmask = sigblock(sigmask(SIGALRM));
651 (void)alarm(interval);
662 * Called if an interval expires before sidewaysintpr has completed a loop.
663 * Sets a flag to not wait for the alarm.
666 catchalarm(int signo __unused)