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 static char ntop_buf
[INET6_ADDRSTRLEN
]; /* for inet_ntop() */
83 * Display a formatted value, or a '-' in the same space.
86 show_stat(const char *fmt
, int width
, u_long value
, short showvalue
)
90 /* Construct the format string */
92 sprintf(newfmt
, "%%%d%s", width
, fmt
);
93 printf(newfmt
, value
);
95 sprintf(newfmt
, "%%%ds", width
);
103 * Print a description of the network interfaces.
106 intpr(int interval1
, u_long ifnetaddr
, void (*pfunc
)(char *))
109 struct ifaddr_container ifac
;
110 struct ifnethead ifnethead
;
115 struct in6_ifaddr in6
;
117 struct ipx_ifaddr ipx
;
122 struct iso_ifaddr iso
;
126 u_long ifaddrcont_addr
;
138 struct sockaddr
*sa
= NULL
;
143 if (ifnetaddr
== 0) {
144 printf("ifnet: symbol not defined\n");
148 sidewaysintpr((unsigned)interval1
, ifnetaddr
);
151 if (kread(ifnetaddr
, (char *)&ifnethead
, sizeof ifnethead
))
153 ifnetaddr
= (u_long
)TAILQ_FIRST(&ifnethead
);
154 if (kread(ifnetaddr
, (char *)&ifnet
, sizeof ifnet
))
158 printf("%-7.7s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
159 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
161 printf(" %10.10s","Ibytes");
162 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
164 printf(" %10.10s","Obytes");
165 printf(" %5s", "Coll");
167 printf(" %s", "Time");
169 printf(" %s", "Drop");
173 while (ifnetaddr
|| ifaddraddr
) {
174 struct sockaddr_in
*sin
;
176 struct sockaddr_in6
*sin6
;
184 if (ifaddraddr
== 0) {
185 struct ifaddrhead head
;
187 ifnetfound
= ifnetaddr
;
188 if (kread(ifnetaddr
, (char *)&ifnet
, sizeof ifnet
))
190 strlcpy(name
, ifnet
.if_xname
, sizeof(name
));
191 ifnetaddr
= (u_long
)TAILQ_NEXT(&ifnet
, if_link
);
192 if (interface
!= 0 && (strcmp(name
, interface
) != 0))
194 cp
= strchr(name
, '\0');
201 if ((ifnet
.if_flags
&IFF_UP
) == 0)
205 if (kread((u_long
)ifnet
.if_addrheads
,
206 (char *)&head
, sizeof(head
)))
210 (u_long
)TAILQ_FIRST(&head
);
211 if (ifaddrcont_addr
== 0) {
214 if (kread(ifaddrcont_addr
, (char *)&ifac
,
217 ifaddraddr
= (u_long
)ifac
.ifa
;
220 ifaddrfound
= ifaddraddr
;
223 * Get the interface stats. These may get
224 * overriden below on a per-interface basis.
226 opackets
= ifnet
.if_opackets
;
227 ipackets
= ifnet
.if_ipackets
;
228 obytes
= ifnet
.if_obytes
;
229 ibytes
= ifnet
.if_ibytes
;
230 oerrors
= ifnet
.if_oerrors
;
231 ierrors
= ifnet
.if_ierrors
;
232 collisions
= ifnet
.if_collisions
;
233 timer
= ifnet
.if_timer
;
234 drops
= ifnet
.if_snd
.ifq_drops
;
236 if (ifaddraddr
== 0) {
237 printf("%-7.7s %-5lu ", name
, ifnet
.if_mtu
);
238 printf("%-13.13s ", "none");
239 printf("%-15.15s ", "none");
241 if (kread(ifaddraddr
, (char *)&ifaddr
, sizeof ifaddr
)) {
245 #define CP(x) ((char *)(x))
246 cp
= (CP(ifaddr
.ifa
.ifa_addr
) - CP(ifaddraddr
)) +
248 sa
= (struct sockaddr
*)cp
;
249 if (af
!= AF_UNSPEC
&& sa
->sa_family
!= af
) {
251 (u_long
)TAILQ_NEXT(&ifac
, ifa_link
);
252 if (ifaddrcont_addr
== 0) {
255 if (kread(ifaddrcont_addr
,
256 (char *)&ifac
, sizeof(ifac
))) {
260 ifaddraddr
= (u_long
)ifac
.ifa
;
264 printf("%-7.7s %-5lu ", name
, ifnet
.if_mtu
);
265 switch (sa
->sa_family
) {
267 printf("%-13.13s ", "none");
268 printf("%-15.15s ", "none");
271 sin
= (struct sockaddr_in
*)sa
;
273 /* can't use inet_makeaddr because kernel
274 * keeps nets unshifted.
276 in
= inet_makeaddr(ifaddr
.in
.ia_subnet
,
278 printf("%-13.13s ", netname(in
.s_addr
,
279 ifaddr
.in
.ia_subnetmask
));
282 netname(htonl(ifaddr
.in
.ia_subnet
),
283 ifaddr
.in
.ia_subnetmask
));
286 routename(sin
->sin_addr
.s_addr
));
292 sin6
= (struct sockaddr_in6
*)sa
;
294 netname6(&ifaddr
.in6
.ia_addr
,
295 &ifaddr
.in6
.ia_prefixmask
.sin6_addr
));
299 ntop_buf
, sizeof(ntop_buf
)));
306 struct sockaddr_ipx
*sipx
=
307 (struct sockaddr_ipx
*)sa
;
311 *(union ipx_net
*) &net
= sipx
->sipx_addr
.x_net
;
312 sprintf(netnum
, "%lx", (u_long
)ntohl(net
));
313 printf("ipx:%-8s ", netnum
);
314 /* printf("ipx:%-8s ", netname(net, 0L)); */
316 ipx_phost((struct sockaddr
*)sipx
));
321 printf("atalk:%-12.12s ",atalk_print(sa
,0x10) );
322 printf("%-9.9s ",atalk_print(sa
,0x0b) );
327 struct sockaddr_ns
*sns
=
328 (struct sockaddr_ns
*)sa
;
332 *(union ns_net
*) &net
= sns
->sns_addr
.x_net
;
333 sprintf(netnum
, "%lxH", ntohl(net
));
335 printf("ns:%-8s ", netnum
);
337 ns_phost((struct sockaddr
*)sns
));
343 struct sockaddr_dl
*sdl
=
344 (struct sockaddr_dl
*)sa
;
346 cp
= (char *)LLADDR(sdl
);
348 sprintf(linknum
, "<Link#%d>", sdl
->sdl_index
);
349 m
= printf("%-11.11s ", linknum
);
353 m
= printf("(%d)", sa
->sa_family
);
354 for (cp
= sa
->sa_len
+ (char *)sa
;
355 --cp
> sa
->sa_data
&& (*cp
== 0);) {}
356 n
= cp
- sa
->sa_data
+ 1;
360 m
+= printf("%02x%c", *cp
++ & 0xff,
371 * Fixup the statistics for interfaces that
372 * update stats for their network addresses
375 opackets
= ifaddr
.in
.ia_ifa
.if_opackets
;
376 ipackets
= ifaddr
.in
.ia_ifa
.if_ipackets
;
377 obytes
= ifaddr
.in
.ia_ifa
.if_obytes
;
378 ibytes
= ifaddr
.in
.ia_ifa
.if_ibytes
;
382 (u_long
)TAILQ_NEXT(&ifac
, ifa_link
);
383 if (ifaddrcont_addr
== 0) {
386 if (kread(ifaddrcont_addr
,
387 (char *)&ifac
, sizeof(ifac
))) {
390 ifaddraddr
= (u_long
)ifac
.ifa
;
395 show_stat("lu", 8, ipackets
, link_layer
|network_layer
);
397 show_stat("lu", 5, ierrors
, link_layer
);
400 show_stat("lu", 10, ibytes
, link_layer
|network_layer
);
403 show_stat("lu", 8, opackets
, link_layer
|network_layer
);
405 show_stat("lu", 5, oerrors
, link_layer
);
408 show_stat("lu", 10, obytes
, link_layer
|network_layer
);
411 show_stat("lu", 5, collisions
, link_layer
);
414 show_stat("d", 3, timer
, link_layer
);
418 show_stat("d", 3, drops
, link_layer
);
421 if (aflag
&& ifaddrfound
) {
423 * Print family's multicast addresses
426 struct ifmultiaddr ifma
;
429 struct sockaddr_in in
;
431 struct sockaddr_in6 in6
;
433 struct sockaddr_dl dl
;
437 for(multiaddr
= (u_long
)ifnet
.if_multiaddrs
.lh_first
;
439 multiaddr
= (u_long
)ifma
.ifma_link
.le_next
) {
440 if (kread(multiaddr
, (char *)&ifma
,
443 if (kread((u_long
)ifma
.ifma_addr
, (char *)&msa
,
446 if (msa
.sa
.sa_family
!= sa
->sa_family
)
450 switch (msa
.sa
.sa_family
) {
452 fmt
= routename(msa
.in
.sin_addr
.s_addr
);
456 printf("%23s %-19.19s(refs: %d)\n", "",
465 switch (msa
.dl
.sdl_type
) {
469 (struct ether_addr
*)
476 printf("%23s %s\n", "", fmt
);
483 SLIST_ENTRY(iftot
) chain
;
484 char ift_name
[IFNAMSIZ
]; /* interface name */
485 u_long ift_ip
; /* input packets */
486 u_long ift_ie
; /* input errors */
487 u_long ift_op
; /* output packets */
488 u_long ift_oe
; /* output errors */
489 u_long ift_co
; /* collisions */
490 u_int ift_dr
; /* drops */
491 u_long ift_ib
; /* input bytes */
492 u_long ift_ob
; /* output bytes */
495 u_char signalled
; /* set if alarm goes off "early" */
498 * Print a running summary of interface statistics.
499 * Repeat display every interval1 seconds, showing statistics
500 * collected over that interval. Assumes that interval1 is non-zero.
501 * First line printed at top of screen is always cumulative.
502 * XXX - should be rewritten to use ifmib(4).
505 sidewaysintpr(unsigned interval1
, u_long off
)
509 struct ifnethead ifnethead
;
510 struct iftot
*iftot
, *ip
, *ipn
, *total
, *sum
, *interesting
;
513 u_long interesting_off
;
515 if (kread(off
, (char *)&ifnethead
, sizeof ifnethead
))
517 firstifnet
= (u_long
)TAILQ_FIRST(&ifnethead
);
519 if ((iftot
= malloc(sizeof(struct iftot
))) == NULL
) {
520 printf("malloc failed\n");
523 memset(iftot
, 0, sizeof(struct iftot
));
527 for (off
= firstifnet
, ip
= iftot
; off
;) {
530 if (kread(off
, (char *)&ifnet
, sizeof ifnet
))
532 strlcpy(name
, ifnet
.if_xname
, sizeof(name
));
533 if (interface
&& strcmp(name
, interface
) == 0) {
535 interesting_off
= off
;
537 snprintf(ip
->ift_name
, 16, "(%s)", name
);
538 if ((ipn
= malloc(sizeof(struct iftot
))) == NULL
) {
539 printf("malloc failed\n");
542 memset(ipn
, 0, sizeof(struct iftot
));
543 SLIST_NEXT(ip
, chain
) = ipn
;
545 off
= (u_long
)TAILQ_NEXT(&ifnet
, if_link
);
547 if ((total
= malloc(sizeof(struct iftot
))) == NULL
) {
548 printf("malloc failed\n");
551 memset(total
, 0, sizeof(struct iftot
));
552 if ((sum
= malloc(sizeof(struct iftot
))) == NULL
) {
553 printf("malloc failed\n");
556 memset(sum
, 0, sizeof(struct iftot
));
559 (void)signal(SIGALRM
, catchalarm
);
561 (void)alarm(interval1
);
564 printf("%17s %14s %16s", "input",
565 interesting
? interesting
->ift_name
: "(Total)", "output");
567 printf("%10s %5s %10s %10s %5s %10s %5s",
568 "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
570 printf(" %5.5s", "drops");
575 if (interesting
!= NULL
) {
577 if (kread(interesting_off
, (char *)&ifnet
, sizeof ifnet
)) {
582 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
583 ifnet
.if_ipackets
- ip
->ift_ip
,
584 ifnet
.if_ierrors
- ip
->ift_ie
,
585 ifnet
.if_ibytes
- ip
->ift_ib
,
586 ifnet
.if_opackets
- ip
->ift_op
,
587 ifnet
.if_oerrors
- ip
->ift_oe
,
588 ifnet
.if_obytes
- ip
->ift_ob
,
589 ifnet
.if_collisions
- ip
->ift_co
);
591 printf(" %5u", ifnet
.if_snd
.ifq_drops
- ip
->ift_dr
);
593 ip
->ift_ip
= ifnet
.if_ipackets
;
594 ip
->ift_ie
= ifnet
.if_ierrors
;
595 ip
->ift_ib
= ifnet
.if_ibytes
;
596 ip
->ift_op
= ifnet
.if_opackets
;
597 ip
->ift_oe
= ifnet
.if_oerrors
;
598 ip
->ift_ob
= ifnet
.if_obytes
;
599 ip
->ift_co
= ifnet
.if_collisions
;
600 ip
->ift_dr
= ifnet
.if_snd
.ifq_drops
;
610 for (off
= firstifnet
, ip
= iftot
;
611 off
&& SLIST_NEXT(ip
, chain
) != NULL
;
612 ip
= SLIST_NEXT(ip
, chain
)) {
613 if (kread(off
, (char *)&ifnet
, sizeof ifnet
)) {
617 sum
->ift_ip
+= ifnet
.if_ipackets
;
618 sum
->ift_ie
+= ifnet
.if_ierrors
;
619 sum
->ift_ib
+= ifnet
.if_ibytes
;
620 sum
->ift_op
+= ifnet
.if_opackets
;
621 sum
->ift_oe
+= ifnet
.if_oerrors
;
622 sum
->ift_ob
+= ifnet
.if_obytes
;
623 sum
->ift_co
+= ifnet
.if_collisions
;
624 sum
->ift_dr
+= ifnet
.if_snd
.ifq_drops
;
625 off
= (u_long
)TAILQ_NEXT(&ifnet
, if_link
);
628 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
629 sum
->ift_ip
- total
->ift_ip
,
630 sum
->ift_ie
- total
->ift_ie
,
631 sum
->ift_ib
- total
->ift_ib
,
632 sum
->ift_op
- total
->ift_op
,
633 sum
->ift_oe
- total
->ift_oe
,
634 sum
->ift_ob
- total
->ift_ob
,
635 sum
->ift_co
- total
->ift_co
);
637 printf(" %5u", sum
->ift_dr
- total
->ift_dr
);
644 oldmask
= sigblock(sigmask(SIGALRM
));
650 (void)alarm(interval1
);
661 * Called if an interval expires before sidewaysintpr has completed a loop.
662 * Sets a flag to not wait for the alarm.
665 catchalarm(int signo __unused
)