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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if.c 8.3 (Berkeley) 4/28/95
30 * $FreeBSD: src/usr.bin/netstat/if.c,v 1.32.2.9 2001/09/17 14:35:46 ru Exp $
33 #define _KERNEL_STRUCTURES
34 #include <sys/param.h>
35 #include <sys/protosw.h>
36 #include <sys/socket.h>
37 #include <sys/sysctl.h>
41 #include <net/if_var.h>
42 #include <net/if_dl.h>
43 #include <net/if_types.h>
44 #include <net/ethernet.h>
45 #include <netinet/in.h>
46 #include <netinet/in_var.h>
47 #include <arpa/inet.h>
61 static void sidewaysintpr (u_int
, u_long
, int);
62 static void catchalarm (int);
65 static char ntop_buf
[INET6_ADDRSTRLEN
]; /* for inet_ntop() */
70 * Display a formatted value, or a '-' in the same space.
73 show_stat(const char *fmt
, int width
, u_long value
, short showvalue
)
78 /* no value, just the dash */
79 sprintf(newfmt
, "%%%ds", width
);
84 if (hflag
) { /* human-readable */
86 humanize_number(buf
, sizeof buf
, (int64_t) value
, "",
88 HN_NOSPACE
| HN_DECIMAL
);
89 sprintf(newfmt
, "%%%ds", width
);
92 sprintf(newfmt
, "%%%d%s", width
, fmt
);
93 printf(newfmt
, value
);
100 * Print a description of the network interfaces.
103 intpr(int interval1
, u_long ifnetaddr
, void (*pfunc
)(char *), u_long ncpusaddr
)
106 struct ifdata_pcpu ifdata
;
107 struct ifaddr_container ifac
;
108 struct ifnethead ifnethead
;
113 struct in6_ifaddr in6
;
117 u_long ifaddrcont_addr
;
129 struct sockaddr
*sa
= NULL
;
135 if (kread(ncpusaddr
, (char *)&ncpus
, sizeof(ncpus
)))
138 if (ifnetaddr
== 0) {
139 printf("ifnet: symbol not defined\n");
143 sidewaysintpr((unsigned)interval1
, ifnetaddr
, ncpus
);
146 if (kread(ifnetaddr
, (char *)&ifnethead
, sizeof ifnethead
))
148 ifnetaddr
= (u_long
)TAILQ_FIRST(&ifnethead
);
149 if (kread(ifnetaddr
, (char *)&ifnet
, sizeof ifnet
))
153 printf("%-7.7s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
154 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
156 printf(" %10.10s","Ibytes");
157 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
159 printf(" %10.10s","Obytes");
160 printf(" %5s", "Coll");
162 printf(" %s", "Time");
164 printf(" %s", "Drop");
169 while (ifnetaddr
|| ifaddraddr
) {
170 struct sockaddr_in
*sin
;
172 struct sockaddr_in6
*sin6
;
180 if (ifaddraddr
== 0) {
181 struct ifaddrhead head
;
183 if (kread(ifnetaddr
, (char *)&ifnet
, sizeof ifnet
))
185 strlcpy(name
, ifnet
.if_xname
, sizeof(name
));
186 ifnetaddr
= (u_long
)TAILQ_NEXT(&ifnet
, if_link
);
187 if (interface
!= 0 && (strcmp(name
, interface
) != 0))
189 cp
= strchr(name
, '\0');
196 if ((ifnet
.if_flags
&IFF_UP
) == 0)
200 if (kread((u_long
)ifnet
.if_addrheads
,
201 (char *)&head
, sizeof(head
)))
205 (u_long
)TAILQ_FIRST(&head
);
206 if (ifaddrcont_addr
== 0) {
209 if (kread(ifaddrcont_addr
, (char *)&ifac
,
212 ifaddraddr
= (u_long
)ifac
.ifa
;
215 ifaddrfound
= ifaddraddr
;
218 * Get the interface stats. These may get
219 * overriden below on a per-interface basis.
221 ifdataaddr
= (u_long
)ifnet
.if_data_pcpu
;
222 if (kread(ifdataaddr
, (char *)&ifdata
, sizeof(ifdata
)))
224 opackets
= ifdata
.ifd_opackets
;
225 ipackets
= ifdata
.ifd_ipackets
;
226 obytes
= ifdata
.ifd_obytes
;
227 ibytes
= ifdata
.ifd_ibytes
;
228 oerrors
= ifdata
.ifd_oerrors
;
229 ierrors
= ifdata
.ifd_ierrors
;
230 collisions
= ifdata
.ifd_collisions
;
232 for (cpu
= 1; cpu
< ncpus
; ++cpu
) {
233 if (kread(ifdataaddr
+ (cpu
* sizeof(ifdata
)),
234 (char *)&ifdata
, sizeof(ifdata
)))
236 opackets
+= ifdata
.ifd_opackets
;
237 ipackets
+= ifdata
.ifd_ipackets
;
238 obytes
+= ifdata
.ifd_obytes
;
239 ibytes
+= ifdata
.ifd_ibytes
;
240 oerrors
+= ifdata
.ifd_oerrors
;
241 ierrors
+= ifdata
.ifd_ierrors
;
242 collisions
+= ifdata
.ifd_collisions
;
245 timer
= ifnet
.if_timer
;
248 if (ifaddraddr
== 0) {
249 printf("%-7.7s %-5lu ", name
, ifnet
.if_mtu
);
250 printf("%-13.13s ", "none");
251 printf("%-15.15s ", "none");
253 if (kread(ifaddraddr
, (char *)&ifaddr
, sizeof ifaddr
)) {
258 ifaddr
.ifa
.if_ipackets
= ifac
.ifa_ipackets
;
259 ifaddr
.ifa
.if_ibytes
= ifac
.ifa_ibytes
;
260 ifaddr
.ifa
.if_opackets
= ifac
.ifa_opackets
;
261 ifaddr
.ifa
.if_obytes
= ifac
.ifa_obytes
;
262 for (cpu
= 1; cpu
< ncpus
; ++cpu
) {
263 struct ifaddr_container nifac
;
265 if (kread(ifaddrcont_addr
+
266 (cpu
* sizeof(nifac
)),
267 (char *)&nifac
, sizeof(nifac
))) {
271 ifaddr
.ifa
.if_ipackets
+= nifac
.ifa_ipackets
;
272 ifaddr
.ifa
.if_ibytes
+= nifac
.ifa_ibytes
;
273 ifaddr
.ifa
.if_opackets
+= nifac
.ifa_opackets
;
274 ifaddr
.ifa
.if_obytes
+= nifac
.ifa_obytes
;
277 #define CP(x) ((char *)(x))
278 cp
= (CP(ifaddr
.ifa
.ifa_addr
) - CP(ifaddraddr
)) +
280 sa
= (struct sockaddr
*)cp
;
281 if (af
!= AF_UNSPEC
&& sa
->sa_family
!= af
) {
283 (u_long
)TAILQ_NEXT(&ifac
, ifa_link
);
284 if (ifaddrcont_addr
== 0) {
287 if (kread(ifaddrcont_addr
,
288 (char *)&ifac
, sizeof(ifac
))) {
292 ifaddraddr
= (u_long
)ifac
.ifa
;
296 printf("%-7.7s %-5lu ", name
, ifnet
.if_mtu
);
297 switch (sa
->sa_family
) {
299 printf("%-13.13s ", "none");
300 printf("%-15.15s ", "none");
303 sin
= (struct sockaddr_in
*)sa
;
305 /* can't use inet_makeaddr because kernel
306 * keeps nets unshifted.
308 in
= inet_makeaddr(ifaddr
.in
.ia_subnet
,
310 printf("%-13.13s ", netname(in
.s_addr
,
311 ifaddr
.in
.ia_subnetmask
));
314 netname(htonl(ifaddr
.in
.ia_subnet
),
315 ifaddr
.in
.ia_subnetmask
));
318 routename(sin
->sin_addr
.s_addr
));
324 sin6
= (struct sockaddr_in6
*)sa
;
326 netname6(&ifaddr
.in6
.ia_addr
,
327 &ifaddr
.in6
.ia_prefixmask
.sin6_addr
));
331 ntop_buf
, sizeof(ntop_buf
)));
338 struct sockaddr_dl
*sdl
=
339 (struct sockaddr_dl
*)sa
;
341 cp
= (char *)LLADDR(sdl
);
343 sprintf(linknum
, "<Link#%d>", sdl
->sdl_index
);
344 m
= printf("%-11.11s ", linknum
);
348 m
= printf("(%d)", sa
->sa_family
);
349 for (cp
= sa
->sa_len
+ (char *)sa
;
350 --cp
> sa
->sa_data
&& (*cp
== 0);) {}
351 n
= cp
- sa
->sa_data
+ 1;
355 m
+= printf("%02x%c", *cp
++ & 0xff,
366 * Fixup the statistics for interfaces that
367 * update stats for their network addresses
370 opackets
= ifaddr
.ifa
.if_opackets
;
371 ipackets
= ifaddr
.ifa
.if_ipackets
;
372 obytes
= ifaddr
.ifa
.if_obytes
;
373 ibytes
= ifaddr
.ifa
.if_ibytes
;
377 (u_long
)TAILQ_NEXT(&ifac
, ifa_link
);
378 if (ifaddrcont_addr
== 0) {
381 if (kread(ifaddrcont_addr
,
382 (char *)&ifac
, sizeof(ifac
))) {
385 ifaddraddr
= (u_long
)ifac
.ifa
;
390 show_stat("lu", 8, ipackets
, link_layer
|network_layer
);
392 show_stat("lu", 5, ierrors
, link_layer
);
395 show_stat("lu", 10, ibytes
, link_layer
|network_layer
);
398 show_stat("lu", 8, opackets
, link_layer
|network_layer
);
400 show_stat("lu", 5, oerrors
, link_layer
);
403 show_stat("lu", 10, obytes
, link_layer
|network_layer
);
406 show_stat("lu", 5, collisions
, link_layer
);
409 show_stat("d", 3, timer
, link_layer
);
413 show_stat("d", 3, drops
, link_layer
);
416 if (aflag
&& ifaddrfound
) {
418 * Print family's multicast addresses
420 struct ifmultiaddr
*multiaddr
;
421 struct ifmultiaddr ifma
;
424 struct sockaddr_in in
;
426 struct sockaddr_in6 in6
;
428 struct sockaddr_dl dl
;
432 TAILQ_FOREACH(multiaddr
, &ifnet
.if_multiaddrs
, ifma_link
) {
433 if (kread((u_long
)multiaddr
, (char *)&ifma
,
437 if (kread((u_long
)ifma
.ifma_addr
, (char *)&msa
,
440 if (msa
.sa
.sa_family
!= sa
->sa_family
)
444 switch (msa
.sa
.sa_family
) {
446 fmt
= routename(msa
.in
.sin_addr
.s_addr
);
450 printf("%23s %-19.19s(refs: %d)\n", "",
459 switch (msa
.dl
.sdl_type
) {
463 (struct ether_addr
*)
470 printf("%23s %s\n", "", fmt
);
477 SLIST_ENTRY(iftot
) chain
;
478 char ift_name
[IFNAMSIZ
]; /* interface name */
479 u_long ift_ip
; /* input packets */
480 u_long ift_ie
; /* input errors */
481 u_long ift_op
; /* output packets */
482 u_long ift_oe
; /* output errors */
483 u_long ift_co
; /* collisions */
484 u_int ift_dr
; /* drops */
485 u_long ift_ib
; /* input bytes */
486 u_long ift_ob
; /* output bytes */
489 u_char signalled
; /* set if alarm goes off "early" */
492 * Print a running summary of interface statistics.
493 * Repeat display every interval1 seconds, showing statistics
494 * collected over that interval. Assumes that interval1 is non-zero.
495 * First line printed at top of screen is always cumulative.
496 * XXX - should be rewritten to use ifmib(4).
499 sidewaysintpr(unsigned interval1
, u_long off
, int ncpus
)
503 struct ifnethead ifnethead
;
504 struct ifdata_pcpu ifdata
;
505 struct iftot
*iftot
, *ip
, *ipn
, *total
, *sum
, *interesting
;
508 u_long interesting_off
;
511 if (kread(off
, (char *)&ifnethead
, sizeof ifnethead
))
513 firstifnet
= (u_long
)TAILQ_FIRST(&ifnethead
);
515 if ((iftot
= malloc(sizeof(struct iftot
))) == NULL
) {
516 printf("malloc failed\n");
519 memset(iftot
, 0, sizeof(struct iftot
));
523 for (off
= firstifnet
, ip
= iftot
; off
;) {
526 if (kread(off
, (char *)&ifnet
, sizeof ifnet
))
528 strlcpy(name
, ifnet
.if_xname
, sizeof(name
));
529 if (interface
&& strcmp(name
, interface
) == 0) {
531 interesting_off
= off
;
533 snprintf(ip
->ift_name
, 16, "(%s)", name
);
534 if ((ipn
= malloc(sizeof(struct iftot
))) == NULL
) {
535 printf("malloc failed\n");
538 memset(ipn
, 0, sizeof(struct iftot
));
539 SLIST_NEXT(ip
, chain
) = ipn
;
541 off
= (u_long
)TAILQ_NEXT(&ifnet
, if_link
);
543 if ((total
= malloc(sizeof(struct iftot
))) == NULL
) {
544 printf("malloc failed\n");
547 memset(total
, 0, sizeof(struct iftot
));
548 if ((sum
= malloc(sizeof(struct iftot
))) == NULL
) {
549 printf("malloc failed\n");
552 memset(sum
, 0, sizeof(struct iftot
));
555 (void)signal(SIGALRM
, catchalarm
);
557 (void)alarm(interval1
);
560 printf("%17s %14s %16s", "input",
561 interesting
? interesting
->ift_name
: "(Total)", "output");
563 printf("%10s %5s %10s %10s %5s %10s %5s",
564 "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
566 printf(" %5.5s", "drops");
571 if (interesting
!= NULL
) {
573 if (kread(interesting_off
, (char *)&ifnet
, sizeof ifnet
)) {
578 ifdata_addr
= (u_long
)ifnet
.if_data_pcpu
;
579 if (kread(ifdata_addr
, (char *)&ifdata
, sizeof(ifdata
))) {
580 printf("ifdata 1\n");
583 ifnet
.if_ipackets
= ifdata
.ifd_ipackets
;
584 ifnet
.if_ierrors
= ifdata
.ifd_ierrors
;
585 ifnet
.if_ibytes
= ifdata
.ifd_ibytes
;
586 ifnet
.if_opackets
= ifdata
.ifd_opackets
;
587 ifnet
.if_oerrors
= ifdata
.ifd_oerrors
;
588 ifnet
.if_obytes
= ifdata
.ifd_obytes
;
589 ifnet
.if_collisions
= ifdata
.ifd_collisions
;
591 for (cpu
= 1; cpu
< ncpus
; ++cpu
) {
592 if (kread(ifdata_addr
+ (cpu
* sizeof(ifdata
)),
593 (char *)&ifdata
, sizeof(ifdata
))) {
594 printf("ifdata 2\n");
597 ifnet
.if_ipackets
+= ifdata
.ifd_ipackets
;
598 ifnet
.if_ierrors
+= ifdata
.ifd_ierrors
;
599 ifnet
.if_ibytes
+= ifdata
.ifd_ibytes
;
600 ifnet
.if_opackets
+= ifdata
.ifd_opackets
;
601 ifnet
.if_oerrors
+= ifdata
.ifd_oerrors
;
602 ifnet
.if_obytes
+= ifdata
.ifd_obytes
;
603 ifnet
.if_collisions
+= ifdata
.ifd_collisions
;
607 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
608 ifnet
.if_ipackets
- ip
->ift_ip
,
609 ifnet
.if_ierrors
- ip
->ift_ie
,
610 ifnet
.if_ibytes
- ip
->ift_ib
,
611 ifnet
.if_opackets
- ip
->ift_op
,
612 ifnet
.if_oerrors
- ip
->ift_oe
,
613 ifnet
.if_obytes
- ip
->ift_ob
,
614 ifnet
.if_collisions
- ip
->ift_co
);
616 printf(" %5u", 0 - ip
->ift_dr
);
618 ip
->ift_ip
= ifnet
.if_ipackets
;
619 ip
->ift_ie
= ifnet
.if_ierrors
;
620 ip
->ift_ib
= ifnet
.if_ibytes
;
621 ip
->ift_op
= ifnet
.if_opackets
;
622 ip
->ift_oe
= ifnet
.if_oerrors
;
623 ip
->ift_ob
= ifnet
.if_obytes
;
624 ip
->ift_co
= ifnet
.if_collisions
;
635 for (off
= firstifnet
, ip
= iftot
;
636 off
&& SLIST_NEXT(ip
, chain
) != NULL
;
637 ip
= SLIST_NEXT(ip
, chain
)) {
638 if (kread(off
, (char *)&ifnet
, sizeof ifnet
)) {
643 ifdata_addr
= (u_long
)ifnet
.if_data_pcpu
;
644 if (kread(ifdata_addr
, (char *)&ifdata
,
646 printf("ifdata 3\n");
649 ifnet
.if_ipackets
= ifdata
.ifd_ipackets
;
650 ifnet
.if_ierrors
= ifdata
.ifd_ierrors
;
651 ifnet
.if_ibytes
= ifdata
.ifd_ibytes
;
652 ifnet
.if_opackets
= ifdata
.ifd_opackets
;
653 ifnet
.if_oerrors
= ifdata
.ifd_oerrors
;
654 ifnet
.if_obytes
= ifdata
.ifd_obytes
;
655 ifnet
.if_collisions
= ifdata
.ifd_collisions
;
657 for (cpu
= 1; cpu
< ncpus
; ++cpu
) {
658 if (kread(ifdata_addr
+ (cpu
* sizeof(ifdata
)),
659 (char *)&ifdata
, sizeof(ifdata
))) {
660 printf("ifdata 2\n");
663 ifnet
.if_ipackets
+= ifdata
.ifd_ipackets
;
664 ifnet
.if_ierrors
+= ifdata
.ifd_ierrors
;
665 ifnet
.if_ibytes
+= ifdata
.ifd_ibytes
;
666 ifnet
.if_opackets
+= ifdata
.ifd_opackets
;
667 ifnet
.if_oerrors
+= ifdata
.ifd_oerrors
;
668 ifnet
.if_obytes
+= ifdata
.ifd_obytes
;
669 ifnet
.if_collisions
+= ifdata
.ifd_collisions
;
673 * Don't double-count interfaces that are associated
674 * with bridges, they will be rolled up by the
675 * bridge. Errors and collisions are not rolled up.
677 if (ifnet
.if_bridge
) {
678 sum
->ift_ie
+= ifnet
.if_ierrors
;
679 sum
->ift_oe
+= ifnet
.if_oerrors
;
680 sum
->ift_co
+= ifnet
.if_collisions
;
682 sum
->ift_ip
+= ifnet
.if_ipackets
;
683 sum
->ift_ie
+= ifnet
.if_ierrors
;
684 sum
->ift_ib
+= ifnet
.if_ibytes
;
685 sum
->ift_op
+= ifnet
.if_opackets
;
686 sum
->ift_oe
+= ifnet
.if_oerrors
;
687 sum
->ift_ob
+= ifnet
.if_obytes
;
688 sum
->ift_co
+= ifnet
.if_collisions
;
691 off
= (u_long
)TAILQ_NEXT(&ifnet
, if_link
);
694 /* %10lu %5lu %10lu %10lu %5lu %10lu %5lu */
695 show_stat("lu", 10, sum
->ift_ip
- total
->ift_ip
, 1);
696 show_stat("lu", 5+1, sum
->ift_ie
- total
->ift_ie
, 1);
697 show_stat("lu", 10+1, sum
->ift_ib
- total
->ift_ib
, 1);
698 show_stat("lu", 10+1, sum
->ift_op
- total
->ift_op
, 1);
699 show_stat("lu", 5+1, sum
->ift_oe
- total
->ift_oe
, 1);
700 show_stat("lu", 10+1, sum
->ift_ob
- total
->ift_ob
, 1);
701 show_stat("lu", 5+1, sum
->ift_co
- total
->ift_co
, 1);
703 show_stat("u", 5+1, sum
->ift_dr
- total
->ift_dr
, 1);
710 oldmask
= sigblock(sigmask(SIGALRM
));
716 (void)alarm(interval1
);
727 * Called if an interval expires before sidewaysintpr has completed a loop.
728 * Sets a flag to not wait for the alarm.
731 catchalarm(int signo __unused
)