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;
130 struct sockaddr *sa = NULL;
135 if (ifnetaddr == 0) {
136 printf("ifnet: symbol not defined\n");
140 sidewaysintpr((unsigned)interval1, ifnetaddr);
143 if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead))
145 ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead);
146 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
150 printf("%-7.7s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
151 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
153 printf(" %10.10s","Ibytes");
154 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
156 printf(" %10.10s","Obytes");
157 printf(" %5s", "Coll");
159 printf(" %s", "Time");
161 printf(" %s", "Drop");
165 while (ifnetaddr || ifaddraddr) {
166 struct sockaddr_in *sin;
168 struct sockaddr_in6 *sin6;
176 if (ifaddraddr == 0) {
177 struct ifaddrhead head;
179 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
181 strlcpy(name, ifnet.if_xname, sizeof(name));
182 ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link);
183 if (interface != 0 && (strcmp(name, interface) != 0))
185 cp = strchr(name, '\0');
192 if ((ifnet.if_flags&IFF_UP) == 0)
196 if (kread((u_long)ifnet.if_addrheads,
197 (char *)&head, sizeof(head)))
201 (u_long)TAILQ_FIRST(&head);
202 if (ifaddrcont_addr == 0) {
205 if (kread(ifaddrcont_addr, (char *)&ifac,
208 ifaddraddr = (u_long)ifac.ifa;
211 ifaddrfound = ifaddraddr;
214 * Get the interface stats. These may get
215 * overriden below on a per-interface basis.
217 opackets = ifnet.if_opackets;
218 ipackets = ifnet.if_ipackets;
219 obytes = ifnet.if_obytes;
220 ibytes = ifnet.if_ibytes;
221 oerrors = ifnet.if_oerrors;
222 ierrors = ifnet.if_ierrors;
223 collisions = ifnet.if_collisions;
224 timer = ifnet.if_timer;
225 drops = ifnet.if_snd.ifq_drops;
227 if (ifaddraddr == 0) {
228 printf("%-7.7s %-5lu ", name, ifnet.if_mtu);
229 printf("%-13.13s ", "none");
230 printf("%-15.15s ", "none");
232 if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
236 #define CP(x) ((char *)(x))
237 cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
239 sa = (struct sockaddr *)cp;
240 if (af != AF_UNSPEC && sa->sa_family != af) {
242 (u_long)TAILQ_NEXT(&ifac, ifa_link);
243 if (ifaddrcont_addr == 0) {
246 if (kread(ifaddrcont_addr,
247 (char *)&ifac, sizeof(ifac))) {
251 ifaddraddr = (u_long)ifac.ifa;
255 printf("%-7.7s %-5lu ", name, ifnet.if_mtu);
256 switch (sa->sa_family) {
258 printf("%-13.13s ", "none");
259 printf("%-15.15s ", "none");
262 sin = (struct sockaddr_in *)sa;
264 /* can't use inet_makeaddr because kernel
265 * keeps nets unshifted.
267 in = inet_makeaddr(ifaddr.in.ia_subnet,
269 printf("%-13.13s ", netname(in.s_addr,
270 ifaddr.in.ia_subnetmask));
273 netname(htonl(ifaddr.in.ia_subnet),
274 ifaddr.in.ia_subnetmask));
277 routename(sin->sin_addr.s_addr));
283 sin6 = (struct sockaddr_in6 *)sa;
285 netname6(&ifaddr.in6.ia_addr,
286 &ifaddr.in6.ia_prefixmask.sin6_addr));
290 ntop_buf, sizeof(ntop_buf)));
297 struct sockaddr_ipx *sipx =
298 (struct sockaddr_ipx *)sa;
302 *(union ipx_net *) &net = sipx->sipx_addr.x_net;
303 sprintf(netnum, "%lx", (u_long)ntohl(net));
304 printf("ipx:%-8s ", netnum);
305 /* printf("ipx:%-8s ", netname(net, 0L)); */
307 ipx_phost((struct sockaddr *)sipx));
313 struct sockaddr_dl *sdl =
314 (struct sockaddr_dl *)sa;
316 cp = (char *)LLADDR(sdl);
318 sprintf(linknum, "<Link#%d>", sdl->sdl_index);
319 m = printf("%-11.11s ", linknum);
323 m = printf("(%d)", sa->sa_family);
324 for (cp = sa->sa_len + (char *)sa;
325 --cp > sa->sa_data && (*cp == 0);) {}
326 n = cp - sa->sa_data + 1;
330 m += printf("%02x%c", *cp++ & 0xff,
341 * Fixup the statistics for interfaces that
342 * update stats for their network addresses
345 opackets = ifaddr.in.ia_ifa.if_opackets;
346 ipackets = ifaddr.in.ia_ifa.if_ipackets;
347 obytes = ifaddr.in.ia_ifa.if_obytes;
348 ibytes = ifaddr.in.ia_ifa.if_ibytes;
352 (u_long)TAILQ_NEXT(&ifac, ifa_link);
353 if (ifaddrcont_addr == 0) {
356 if (kread(ifaddrcont_addr,
357 (char *)&ifac, sizeof(ifac))) {
360 ifaddraddr = (u_long)ifac.ifa;
365 show_stat("lu", 8, ipackets, link_layer|network_layer);
367 show_stat("lu", 5, ierrors, link_layer);
370 show_stat("lu", 10, ibytes, link_layer|network_layer);
373 show_stat("lu", 8, opackets, link_layer|network_layer);
375 show_stat("lu", 5, oerrors, link_layer);
378 show_stat("lu", 10, obytes, link_layer|network_layer);
381 show_stat("lu", 5, collisions, link_layer);
384 show_stat("d", 3, timer, link_layer);
388 show_stat("d", 3, drops, link_layer);
391 if (aflag && ifaddrfound) {
393 * Print family's multicast addresses
395 struct ifmultiaddr *multiaddr;
396 struct ifmultiaddr ifma;
399 struct sockaddr_in in;
401 struct sockaddr_in6 in6;
403 struct sockaddr_dl dl;
407 TAILQ_FOREACH(multiaddr, &ifnet.if_multiaddrs, ifma_link) {
408 if (kread((u_long)multiaddr, (char *)&ifma,
412 if (kread((u_long)ifma.ifma_addr, (char *)&msa,
415 if (msa.sa.sa_family != sa->sa_family)
419 switch (msa.sa.sa_family) {
421 fmt = routename(msa.in.sin_addr.s_addr);
425 printf("%23s %-19.19s(refs: %d)\n", "",
434 switch (msa.dl.sdl_type) {
438 (struct ether_addr *)
445 printf("%23s %s\n", "", fmt);
452 SLIST_ENTRY(iftot) chain;
453 char ift_name[IFNAMSIZ]; /* interface name */
454 u_long ift_ip; /* input packets */
455 u_long ift_ie; /* input errors */
456 u_long ift_op; /* output packets */
457 u_long ift_oe; /* output errors */
458 u_long ift_co; /* collisions */
459 u_int ift_dr; /* drops */
460 u_long ift_ib; /* input bytes */
461 u_long ift_ob; /* output bytes */
464 u_char signalled; /* set if alarm goes off "early" */
467 * Print a running summary of interface statistics.
468 * Repeat display every interval1 seconds, showing statistics
469 * collected over that interval. Assumes that interval1 is non-zero.
470 * First line printed at top of screen is always cumulative.
471 * XXX - should be rewritten to use ifmib(4).
474 sidewaysintpr(unsigned interval1, u_long off)
478 struct ifnethead ifnethead;
479 struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
482 u_long interesting_off;
484 if (kread(off, (char *)&ifnethead, sizeof ifnethead))
486 firstifnet = (u_long)TAILQ_FIRST(&ifnethead);
488 if ((iftot = malloc(sizeof(struct iftot))) == NULL) {
489 printf("malloc failed\n");
492 memset(iftot, 0, sizeof(struct iftot));
496 for (off = firstifnet, ip = iftot; off;) {
499 if (kread(off, (char *)&ifnet, sizeof ifnet))
501 strlcpy(name, ifnet.if_xname, sizeof(name));
502 if (interface && strcmp(name, interface) == 0) {
504 interesting_off = off;
506 snprintf(ip->ift_name, 16, "(%s)", name);
507 if ((ipn = malloc(sizeof(struct iftot))) == NULL) {
508 printf("malloc failed\n");
511 memset(ipn, 0, sizeof(struct iftot));
512 SLIST_NEXT(ip, chain) = ipn;
514 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
516 if ((total = malloc(sizeof(struct iftot))) == NULL) {
517 printf("malloc failed\n");
520 memset(total, 0, sizeof(struct iftot));
521 if ((sum = malloc(sizeof(struct iftot))) == NULL) {
522 printf("malloc failed\n");
525 memset(sum, 0, sizeof(struct iftot));
528 (void)signal(SIGALRM, catchalarm);
530 (void)alarm(interval1);
533 printf("%17s %14s %16s", "input",
534 interesting ? interesting->ift_name : "(Total)", "output");
536 printf("%10s %5s %10s %10s %5s %10s %5s",
537 "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
539 printf(" %5.5s", "drops");
544 if (interesting != NULL) {
546 if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) {
551 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
552 ifnet.if_ipackets - ip->ift_ip,
553 ifnet.if_ierrors - ip->ift_ie,
554 ifnet.if_ibytes - ip->ift_ib,
555 ifnet.if_opackets - ip->ift_op,
556 ifnet.if_oerrors - ip->ift_oe,
557 ifnet.if_obytes - ip->ift_ob,
558 ifnet.if_collisions - ip->ift_co);
560 printf(" %5u", ifnet.if_snd.ifq_drops - ip->ift_dr);
562 ip->ift_ip = ifnet.if_ipackets;
563 ip->ift_ie = ifnet.if_ierrors;
564 ip->ift_ib = ifnet.if_ibytes;
565 ip->ift_op = ifnet.if_opackets;
566 ip->ift_oe = ifnet.if_oerrors;
567 ip->ift_ob = ifnet.if_obytes;
568 ip->ift_co = ifnet.if_collisions;
569 ip->ift_dr = ifnet.if_snd.ifq_drops;
579 for (off = firstifnet, ip = iftot;
580 off && SLIST_NEXT(ip, chain) != NULL;
581 ip = SLIST_NEXT(ip, chain)) {
582 if (kread(off, (char *)&ifnet, sizeof ifnet)) {
586 sum->ift_ip += ifnet.if_ipackets;
587 sum->ift_ie += ifnet.if_ierrors;
588 sum->ift_ib += ifnet.if_ibytes;
589 sum->ift_op += ifnet.if_opackets;
590 sum->ift_oe += ifnet.if_oerrors;
591 sum->ift_ob += ifnet.if_obytes;
592 sum->ift_co += ifnet.if_collisions;
593 sum->ift_dr += ifnet.if_snd.ifq_drops;
594 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
597 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
598 sum->ift_ip - total->ift_ip,
599 sum->ift_ie - total->ift_ie,
600 sum->ift_ib - total->ift_ib,
601 sum->ift_op - total->ift_op,
602 sum->ift_oe - total->ift_oe,
603 sum->ift_ob - total->ift_ob,
604 sum->ift_co - total->ift_co);
606 printf(" %5u", sum->ift_dr - total->ift_dr);
613 oldmask = sigblock(sigmask(SIGALRM));
619 (void)alarm(interval1);
630 * Called if an interval expires before sidewaysintpr has completed a loop.
631 * Sets a flag to not wait for the alarm.
634 catchalarm(int signo __unused)