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
36 static char sccsid[] = "@(#)if.c 8.3 (Berkeley) 4/28/95";
38 static const char rcsid[] =
42 #include <sys/types.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/sysctl.h>
49 #include <net/if_var.h>
50 #include <net/if_dl.h>
51 #include <net/if_types.h>
52 #include <net/bridge.h>
53 #include <net/ethernet.h>
54 #include <netinet/in.h>
55 #include <netinet/in_var.h>
56 #include <netipx/ipx.h>
57 #include <netipx/ipx_if.h>
60 #include <netns/ns_if.h>
63 #include <netiso/iso.h>
64 #include <netiso/iso_var.h>
66 #include <arpa/inet.h>
79 static void sidewaysintpr (u_int, u_long);
80 static void catchalarm (int);
83 char *netname6 (struct sockaddr_in6 *, struct in6_addr *);
84 static char ntop_buf[INET6_ADDRSTRLEN]; /* for inet_ntop() */
88 /* print bridge statistics */
90 bdg_stats(u_long dummy __unused, char *name, int af __unused)
103 if (sysctl(mib,4, &s,&slen,NULL,0)==-1)
104 return ; /* no bridging */
111 printf("-- Bridging statistics (%s) --\n", name) ;
113 "Name In Out Forward Drop Bcast Mcast Local Unknown\n");
114 for (i = 0 ; i < 16 ; i++) {
116 printf("%-6s %9ld%9ld%9ld%9ld%9ld%9ld%9ld%9ld\n",
118 s.s[i].p_in[(int)BDG_IN],
119 s.s[i].p_in[(int)BDG_OUT],
120 s.s[i].p_in[(int)BDG_FORWARD],
121 s.s[i].p_in[(int)BDG_DROP],
122 s.s[i].p_in[(int)BDG_BCAST],
123 s.s[i].p_in[(int)BDG_MCAST],
124 s.s[i].p_in[(int)BDG_LOCAL],
125 s.s[i].p_in[(int)BDG_UNKNOWN] );
133 * Display a formatted value, or a '-' in the same space.
136 show_stat(const char *fmt, int width, u_long value, short showvalue)
140 /* Construct the format string */
142 sprintf(newfmt, "%%%d%s", width, fmt);
143 printf(newfmt, value);
145 sprintf(newfmt, "%%%ds", width);
153 * Print a description of the network interfaces.
156 intpr(int interval, u_long ifnetaddr, void (*pfunc)(char *))
159 struct ifnethead ifnethead;
164 struct in6_ifaddr in6;
166 struct ipx_ifaddr ipx;
171 struct iso_ifaddr iso;
186 struct sockaddr *sa = NULL;
187 char name[32], tname[16];
191 if (ifnetaddr == 0) {
192 printf("ifnet: symbol not defined\n");
196 sidewaysintpr((unsigned)interval, ifnetaddr);
199 if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead))
201 ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead);
202 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
206 printf("%-5.5s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
207 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
209 printf(" %10.10s","Ibytes");
210 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
212 printf(" %10.10s","Obytes");
213 printf(" %5s", "Coll");
215 printf(" %s", "Time");
217 printf(" %s", "Drop");
221 while (ifnetaddr || ifaddraddr) {
222 struct sockaddr_in *sin;
224 struct sockaddr_in6 *sin6;
232 if (ifaddraddr == 0) {
233 ifnetfound = ifnetaddr;
234 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) ||
235 kread((u_long)ifnet.if_name, tname, 16))
238 ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link);
239 snprintf(name, 32, "%s%d", tname, ifnet.if_unit);
240 if (interface != 0 && (strcmp(name, interface) != 0))
242 cp = index(name, '\0');
249 if ((ifnet.if_flags&IFF_UP) == 0)
252 ifaddraddr = (u_long)TAILQ_FIRST(&ifnet.if_addrhead);
254 ifaddrfound = ifaddraddr;
257 * Get the interface stats. These may get
258 * overriden below on a per-interface basis.
260 opackets = ifnet.if_opackets;
261 ipackets = ifnet.if_ipackets;
262 obytes = ifnet.if_obytes;
263 ibytes = ifnet.if_ibytes;
264 oerrors = ifnet.if_oerrors;
265 ierrors = ifnet.if_ierrors;
266 collisions = ifnet.if_collisions;
267 timer = ifnet.if_timer;
268 drops = ifnet.if_snd.ifq_drops;
270 if (ifaddraddr == 0) {
271 printf("%-5.5s %-5lu ", name, ifnet.if_mtu);
272 printf("%-13.13s ", "none");
273 printf("%-15.15s ", "none");
275 if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
279 #define CP(x) ((char *)(x))
280 cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
282 sa = (struct sockaddr *)cp;
283 if (af != AF_UNSPEC && sa->sa_family != af) {
285 (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
288 printf("%-5.5s %-5lu ", name, ifnet.if_mtu);
289 switch (sa->sa_family) {
291 printf("%-13.13s ", "none");
292 printf("%-15.15s ", "none");
295 sin = (struct sockaddr_in *)sa;
297 /* can't use inet_makeaddr because kernel
298 * keeps nets unshifted.
300 in = inet_makeaddr(ifaddr.in.ia_subnet,
302 printf("%-13.13s ", netname(in.s_addr,
303 ifaddr.in.ia_subnetmask));
306 netname(htonl(ifaddr.in.ia_subnet),
307 ifaddr.in.ia_subnetmask));
310 routename(sin->sin_addr.s_addr));
316 sin6 = (struct sockaddr_in6 *)sa;
318 netname6(&ifaddr.in6.ia_addr,
319 &ifaddr.in6.ia_prefixmask.sin6_addr));
321 (char *)inet_ntop(AF_INET6,
323 ntop_buf, sizeof(ntop_buf)));
330 struct sockaddr_ipx *sipx =
331 (struct sockaddr_ipx *)sa;
335 *(union ipx_net *) &net = sipx->sipx_addr.x_net;
336 sprintf(netnum, "%lx", (u_long)ntohl(net));
337 printf("ipx:%-8s ", netnum);
338 /* printf("ipx:%-8s ", netname(net, 0L)); */
340 ipx_phost((struct sockaddr *)sipx));
345 printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
346 printf("%-9.9s ",atalk_print(sa,0x0b) );
351 struct sockaddr_ns *sns =
352 (struct sockaddr_ns *)sa;
356 *(union ns_net *) &net = sns->sns_addr.x_net;
357 sprintf(netnum, "%lxH", ntohl(net));
359 printf("ns:%-8s ", netnum);
361 ns_phost((struct sockaddr *)sns));
367 struct sockaddr_dl *sdl =
368 (struct sockaddr_dl *)sa;
370 cp = (char *)LLADDR(sdl);
372 sprintf(linknum, "<Link#%d>", sdl->sdl_index);
373 m = printf("%-11.11s ", linknum);
377 m = printf("(%d)", sa->sa_family);
378 for (cp = sa->sa_len + (char *)sa;
379 --cp > sa->sa_data && (*cp == 0);) {}
380 n = cp - sa->sa_data + 1;
384 m += printf("%02x%c", *cp++ & 0xff,
395 * Fixup the statistics for interfaces that
396 * update stats for their network addresses
399 opackets = ifaddr.in.ia_ifa.if_opackets;
400 ipackets = ifaddr.in.ia_ifa.if_ipackets;
401 obytes = ifaddr.in.ia_ifa.if_obytes;
402 ibytes = ifaddr.in.ia_ifa.if_ibytes;
405 ifaddraddr = (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
408 show_stat("lu", 8, ipackets, link_layer|network_layer);
410 show_stat("lu", 5, ierrors, link_layer);
413 show_stat("lu", 10, ibytes, link_layer|network_layer);
416 show_stat("lu", 8, opackets, link_layer|network_layer);
418 show_stat("lu", 5, oerrors, link_layer);
421 show_stat("lu", 10, obytes, link_layer|network_layer);
424 show_stat("lu", 5, collisions, link_layer);
427 show_stat("d", 3, timer, link_layer);
431 show_stat("d", 3, drops, link_layer);
434 if (aflag && ifaddrfound) {
436 * Print family's multicast addresses
438 struct ifmultiaddr *multiaddr;
439 struct ifmultiaddr ifma;
442 struct sockaddr_in in;
444 struct sockaddr_in6 in6;
446 struct sockaddr_dl dl;
450 TAILQ_FOREACH(multiaddr, &ifnet.if_multiaddrs, ifma_link) {
451 if (kread((u_long)multiaddr, (char *)&ifma,
455 if (kread((u_long)ifma.ifma_addr, (char *)&msa,
458 if (msa.sa.sa_family != sa->sa_family)
462 switch (msa.sa.sa_family) {
464 fmt = routename(msa.in.sin_addr.s_addr);
468 printf("%23s %-19.19s(refs: %d)\n", "",
477 switch (msa.dl.sdl_type) {
481 (struct ether_addr *)
488 printf("%23s %s\n", "", fmt);
495 SLIST_ENTRY(iftot) chain;
496 char ift_name[16]; /* interface name */
497 u_long ift_ip; /* input packets */
498 u_long ift_ie; /* input errors */
499 u_long ift_op; /* output packets */
500 u_long ift_oe; /* output errors */
501 u_long ift_co; /* collisions */
502 u_int ift_dr; /* drops */
503 u_long ift_ib; /* input bytes */
504 u_long ift_ob; /* output bytes */
507 u_char signalled; /* set if alarm goes off "early" */
510 * Print a running summary of interface statistics.
511 * Repeat display every interval seconds, showing statistics
512 * collected over that interval. Assumes that interval is non-zero.
513 * First line printed at top of screen is always cumulative.
514 * XXX - should be rewritten to use ifmib(4).
517 sidewaysintpr(unsigned interval, u_long off)
521 struct ifnethead ifnethead;
522 struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
525 u_long interesting_off;
527 if (kread(off, (char *)&ifnethead, sizeof ifnethead))
529 firstifnet = (u_long)TAILQ_FIRST(&ifnethead);
531 if ((iftot = malloc(sizeof(struct iftot))) == NULL) {
532 printf("malloc failed\n");
535 memset(iftot, 0, sizeof(struct iftot));
539 for (off = firstifnet, ip = iftot; off;) {
540 char name[16], tname[16];
542 if (kread(off, (char *)&ifnet, sizeof ifnet))
544 if (kread((u_long)ifnet.if_name, tname, 16))
547 snprintf(name, 16, "%s%d", tname, ifnet.if_unit);
548 if (interface && strcmp(name, interface) == 0) {
550 interesting_off = off;
552 snprintf(ip->ift_name, 16, "(%s)", name);;
553 if ((ipn = malloc(sizeof(struct iftot))) == NULL) {
554 printf("malloc failed\n");
557 memset(ipn, 0, sizeof(struct iftot));
558 SLIST_NEXT(ip, chain) = ipn;
560 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
562 if ((total = malloc(sizeof(struct iftot))) == NULL) {
563 printf("malloc failed\n");
566 memset(total, 0, sizeof(struct iftot));
567 if ((sum = malloc(sizeof(struct iftot))) == NULL) {
568 printf("malloc failed\n");
571 memset(sum, 0, sizeof(struct iftot));
574 (void)signal(SIGALRM, catchalarm);
576 (void)alarm(interval);
579 printf("%17s %14s %16s", "input",
580 interesting ? interesting->ift_name : "(Total)", "output");
582 printf("%10s %5s %10s %10s %5s %10s %5s",
583 "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
585 printf(" %5.5s", "drops");
590 if (interesting != NULL) {
592 if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) {
597 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
598 ifnet.if_ipackets - ip->ift_ip,
599 ifnet.if_ierrors - ip->ift_ie,
600 ifnet.if_ibytes - ip->ift_ib,
601 ifnet.if_opackets - ip->ift_op,
602 ifnet.if_oerrors - ip->ift_oe,
603 ifnet.if_obytes - ip->ift_ob,
604 ifnet.if_collisions - ip->ift_co);
606 printf(" %5u", ifnet.if_snd.ifq_drops - ip->ift_dr);
608 ip->ift_ip = ifnet.if_ipackets;
609 ip->ift_ie = ifnet.if_ierrors;
610 ip->ift_ib = ifnet.if_ibytes;
611 ip->ift_op = ifnet.if_opackets;
612 ip->ift_oe = ifnet.if_oerrors;
613 ip->ift_ob = ifnet.if_obytes;
614 ip->ift_co = ifnet.if_collisions;
615 ip->ift_dr = ifnet.if_snd.ifq_drops;
625 for (off = firstifnet, ip = iftot;
626 off && SLIST_NEXT(ip, chain) != NULL;
627 ip = SLIST_NEXT(ip, chain)) {
628 if (kread(off, (char *)&ifnet, sizeof ifnet)) {
632 sum->ift_ip += ifnet.if_ipackets;
633 sum->ift_ie += ifnet.if_ierrors;
634 sum->ift_ib += ifnet.if_ibytes;
635 sum->ift_op += ifnet.if_opackets;
636 sum->ift_oe += ifnet.if_oerrors;
637 sum->ift_ob += ifnet.if_obytes;
638 sum->ift_co += ifnet.if_collisions;
639 sum->ift_dr += ifnet.if_snd.ifq_drops;
640 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
643 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
644 sum->ift_ip - total->ift_ip,
645 sum->ift_ie - total->ift_ie,
646 sum->ift_ib - total->ift_ib,
647 sum->ift_op - total->ift_op,
648 sum->ift_oe - total->ift_oe,
649 sum->ift_ob - total->ift_ob,
650 sum->ift_co - total->ift_co);
652 printf(" %5u", sum->ift_dr - total->ift_dr);
659 oldmask = sigblock(sigmask(SIGALRM));
665 (void)alarm(interval);
676 * Called if an interval expires before sidewaysintpr has completed a loop.
677 * Sets a flag to not wait for the alarm.
680 catchalarm(int signo __unused)