]>
git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/if.c
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
[] =
39 "$Id: if.c,v 1.6.40.1 2006/01/10 05:26:27 lindak Exp $";
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <sys/sysctl.h>
48 #include <net/if_var.h>
49 #include <net/if_dl.h>
50 #include <net/if_types.h>
51 #include <net/if_mib.h>
52 #include <net/ethernet.h>
53 #include <net/route.h>
55 #include <netinet/in.h>
56 #include <netinet/in_var.h>
59 #include <netipx/ipx.h>
60 #include <netipx/ipx_if.h>
65 #include <netns/ns_if.h>
67 #include <arpa/inet.h>
81 #define ROUNDUP(a, size) (((a) & ((size) - 1)) ? (1 + ((a)|(size - 1))) : (a))
83 #define NEXT_SA(p) (struct sockaddr *) \
84 ((caddr_t)p + (p->sa_len ? ROUNDUP(p->sa_len, sizeof(u_long)) : \
87 static void sidewaysintpr ();
88 static void catchalarm (int);
91 char *netname6 (struct sockaddr_in6
*, struct sockaddr
*);
92 static char ntop_buf
[INET6_ADDRSTRLEN
]; /* for inet_ntop() */
100 /* print bridge statistics */
102 bdg_stats(u_long dummy
, char *name
, int af
)
114 if (sysctl(mib
,4, &s
,&slen
,NULL
,0)==-1)
115 return ; /* no bridging */
122 printf("-- Bridging statistics (%s) --\n", name
) ;
124 "Name In Out Forward Drop Bcast Mcast Local Unknown\n");
125 for (i
= 0 ; i
< 16 ; i
++) {
127 printf("%-6s %9ld%9ld%9ld%9ld%9ld%9ld%9ld%9ld\n",
129 s
.s
[i
].p_in
[(int)BDG_IN
],
130 s
.s
[i
].p_in
[(int)BDG_OUT
],
131 s
.s
[i
].p_in
[(int)BDG_FORWARD
],
132 s
.s
[i
].p_in
[(int)BDG_DROP
],
133 s
.s
[i
].p_in
[(int)BDG_BCAST
],
134 s
.s
[i
].p_in
[(int)BDG_MCAST
],
135 s
.s
[i
].p_in
[(int)BDG_LOCAL
],
136 s
.s
[i
].p_in
[(int)BDG_UNKNOWN
] );
144 * Display a formatted value, or a '-' in the same space.
147 show_stat(const char *fmt
, int width
, u_int64_t value
, short showvalue
)
151 /* Construct the format string */
153 sprintf(newfmt
, "%%%d%s", width
, fmt
);
154 printf(newfmt
, value
);
156 sprintf(newfmt
, "%%%ds", width
);
162 get_rti_info(int addrs
, struct sockaddr
*sa
, struct sockaddr
**rti_info
)
167 for (i
= 0; i
< RTAX_MAX
; i
++) {
168 if (addrs
& (1 << i
)) {
170 if (sa
->sa_len
< sizeof(struct sockaddr
))
171 len
+= sizeof(struct sockaddr
);
183 multipr(int family
, char *buf
, char *lim
)
187 for (next
= buf
; next
< lim
; ) {
188 struct ifma_msghdr2
*ifmam
= (struct ifma_msghdr2
*)next
;
189 struct sockaddr
*rti_info
[RTAX_MAX
];
193 next
+= ifmam
->ifmam_msglen
;
194 if (ifmam
->ifmam_type
== RTM_IFINFO2
)
196 else if (ifmam
->ifmam_type
!= RTM_NEWMADDR2
)
198 get_rti_info(ifmam
->ifmam_addrs
, (struct sockaddr
*)(ifmam
+ 1), rti_info
);
199 sa
= rti_info
[RTAX_IFA
];
201 if (sa
->sa_family
!= family
)
203 switch (sa
->sa_family
) {
205 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
207 fmt
= routename(sin
->sin_addr
.s_addr
);
212 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sa
;
214 printf("%23s %-19.19s(refs: %d)\n", "",
219 ifmam
->ifmam_refcount
);
224 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sa
;
226 switch (sdl
->sdl_type
) {
230 (struct ether_addr
*)
238 printf("%23s %s\n", "", fmt
);
243 * Print a description of the network interfaces.
246 intpr(void (*pfunc
)(char *))
248 u_int64_t opackets
= 0;
249 u_int64_t ipackets
= 0;
250 u_int64_t obytes
= 0;
251 u_int64_t ibytes
= 0;
252 u_int64_t oerrors
= 0;
253 u_int64_t ierrors
= 0;
254 u_int64_t collisions
= 0;
258 struct sockaddr
*sa
= NULL
;
263 char *buf
= NULL
, *lim
, *next
;
265 struct if_msghdr
*ifm
;
266 struct sockaddr
*rti_info
[RTAX_MAX
];
267 unsigned int ifindex
= 0;
275 ifindex
= if_nametoindex(interface
);
277 mib
[0] = CTL_NET
; // networking subsystem
278 mib
[1] = PF_ROUTE
; // type of information
279 mib
[2] = 0; // protocol (IPPROTO_xxx)
280 mib
[3] = 0; // address family
281 mib
[4] = NET_RT_IFLIST2
; // operation
283 if (sysctl(mib
, 6, NULL
, &len
, NULL
, 0) < 0)
285 if ((buf
= malloc(len
)) == NULL
) {
286 printf("malloc failed\n");
289 if (sysctl(mib
, 6, buf
, &len
, NULL
, 0) < 0) {
295 printf("%-5.5s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
296 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
298 printf(" %10.10s","Ibytes");
299 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
301 printf(" %10.10s","Obytes");
302 printf(" %5s", "Coll");
304 printf(" %s", "Time");
306 printf(" %s", "Drop");
310 for (next
= buf
; next
< lim
; ) {
316 ifm
= (struct if_msghdr
*)next
;
317 next
+= ifm
->ifm_msglen
;
319 if (ifm
->ifm_type
== RTM_IFINFO2
) {
320 struct if_msghdr2
*if2m
= (struct if_msghdr2
*)ifm
;
321 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)(if2m
+ 1);
323 strncpy(name
, sdl
->sdl_data
, sdl
->sdl_nlen
);
324 name
[sdl
->sdl_nlen
] = 0;
325 if (interface
!= 0 && if2m
->ifm_index
!= ifindex
)
327 cp
= index(name
, '\0');
334 if ((if2m
->ifm_flags
& IFF_UP
) == 0)
339 * Get the interface stats. These may get
340 * overriden below on a per-interface basis.
342 opackets
= if2m
->ifm_data
.ifi_opackets
;
343 ipackets
= if2m
->ifm_data
.ifi_ipackets
;
344 obytes
= if2m
->ifm_data
.ifi_obytes
;
345 ibytes
= if2m
->ifm_data
.ifi_ibytes
;
346 oerrors
=if2m
->ifm_data
.ifi_oerrors
;
347 ierrors
= if2m
->ifm_data
.ifi_ierrors
;
348 collisions
= if2m
->ifm_data
.ifi_collisions
;
349 timer
= if2m
->ifm_timer
;
350 drops
= if2m
->ifm_snd_drops
;
351 mtu
= if2m
->ifm_data
.ifi_mtu
;
353 get_rti_info(if2m
->ifm_addrs
, (struct sockaddr
*)(if2m
+ 1), rti_info
);
354 sa
= rti_info
[RTAX_IFP
];
355 } else if (ifm
->ifm_type
== RTM_NEWADDR
) {
356 struct ifa_msghdr
*ifam
= (struct ifa_msghdr
*)ifm
;
358 if (interface
!= 0 && ifam
->ifam_index
!= ifindex
)
360 get_rti_info(ifam
->ifam_addrs
, (struct sockaddr
*)(ifam
+ 1), rti_info
);
361 sa
= rti_info
[RTAX_IFA
];
364 printf("%-5.5s %-5lu ", name
, mtu
);
367 printf("%-13.13s ", "none");
368 printf("%-15.15s ", "none");
370 switch (sa
->sa_family
) {
372 printf("%-13.13s ", "none");
373 printf("%-15.15s ", "none");
376 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
377 struct sockaddr_in mask
;
379 mask
.sin_addr
.s_addr
= 0;
380 memcpy(&mask
, rti_info
[RTAX_NETMASK
], ((struct sockaddr_in
*)rti_info
[RTAX_NETMASK
])->sin_len
);
382 printf("%-13.13s ", netname(sin
->sin_addr
.s_addr
& mask
.sin_addr
.s_addr
,
383 ntohl(mask
.sin_addr
.s_addr
)));
386 routename(sin
->sin_addr
.s_addr
));
393 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sa
;
394 struct sockaddr
*mask
= (struct sockaddr
*)rti_info
[RTAX_NETMASK
];
400 (char *)inet_ntop(AF_INET6
,
402 ntop_buf
, sizeof(ntop_buf
)));
410 struct sockaddr_dl
*sdl
=
411 (struct sockaddr_dl
*)sa
;
413 cp
= (char *)LLADDR(sdl
);
415 sprintf(linknum
, "<Link#%d>", sdl
->sdl_index
);
416 m
= printf("%-11.11s ", linknum
);
420 m
= printf("(%d)", sa
->sa_family
);
421 for (cp
= sa
->sa_len
+ (char *)sa
;
422 --cp
> sa
->sa_data
&& (*cp
== 0);) {}
423 n
= cp
- sa
->sa_data
+ 1;
427 m
+= printf("%02x%c", *cp
++ & 0xff,
438 * Fixup the statistics for interfaces that
439 * update stats for their network addresses
442 opackets
= ifaddr
.in
.ia_ifa
.if_opackets
;
443 ipackets
= ifaddr
.in
.ia_ifa
.if_ipackets
;
444 obytes
= ifaddr
.in
.ia_ifa
.if_obytes
;
445 ibytes
= ifaddr
.in
.ia_ifa
.if_ibytes
;
450 show_stat("llu", 8, ipackets
, link_layer
|network_layer
);
452 show_stat("llu", 5, ierrors
, link_layer
);
455 show_stat("llu", 10, ibytes
, link_layer
|network_layer
);
458 show_stat("llu", 8, opackets
, link_layer
|network_layer
);
460 show_stat("llu", 5, oerrors
, link_layer
);
463 show_stat("llu", 10, obytes
, link_layer
|network_layer
);
466 show_stat("llu", 5, collisions
, link_layer
);
469 show_stat("ll", 3, timer
, link_layer
);
473 show_stat("ll", 3, drops
, link_layer
);
478 multipr(sa
->sa_family
, next
, lim
);
483 SLIST_ENTRY(iftot
) chain
;
484 char ift_name
[16]; /* interface name */
485 u_int64_t ift_ip
; /* input packets */
486 u_int64_t ift_ie
; /* input errors */
487 u_int64_t ift_op
; /* output packets */
488 u_int64_t ift_oe
; /* output errors */
489 u_int64_t ift_co
; /* collisions */
490 u_int64_t ift_dr
; /* drops */
491 u_int64_t ift_ib
; /* input bytes */
492 u_int64_t 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 interval seconds, showing statistics
500 * collected over that interval. Assumes that interval is non-zero.
501 * First line printed at top of screen is always cumulative.
502 * XXX - should be rewritten to use ifmib(4).
507 struct iftot
*total
, *sum
, *interesting
;
512 unsigned int ifcount
, i
;
513 struct ifmibdata
*ifmdall
= 0;
516 /* Common OID prefix */
519 name
[2] = NETLINK_GENERIC
;
522 name
[3] = IFMIB_SYSTEM
;
523 name
[4] = IFMIB_IFCOUNT
;
524 if (sysctl(name
, 5, &ifcount
, &len
, 0, 0) == 1)
525 err(1, "sysctl IFMIB_IFCOUNT");
527 len
= ifcount
* sizeof(struct ifmibdata
);
528 ifmdall
= malloc(len
);
530 err(1, "malloc failed");
531 name
[3] = IFMIB_IFALLDATA
;
533 name
[5] = IFDATA_GENERAL
;
534 if (sysctl(name
, 6, ifmdall
, &len
, (void *)0, 0) == -1)
535 err(1, "sysctl IFMIB_IFALLDATA");
539 for (i
= 0; i
< ifcount
; i
++) {
540 struct ifmibdata
*ifmd
= ifmdall
+ i
;
542 if (interface
&& strcmp(ifmd
->ifmd_name
, interface
) == 0) {
543 if ((interesting
= calloc(ifcount
, sizeof(struct iftot
))) == NULL
)
544 err(1, "malloc failed");
545 interesting_row
= i
+ 1;
546 snprintf(interesting
->ift_name
, 16, "(%s)", ifmd
->ifmd_name
);;
549 if ((total
= calloc(1, sizeof(struct iftot
))) == NULL
)
550 err(1, "malloc failed");
552 if ((sum
= calloc(1, sizeof(struct iftot
))) == NULL
)
553 err(1, "malloc failed");
556 (void)signal(SIGALRM
, catchalarm
);
558 (void)alarm(interval
);
561 printf("%17s %14s %16s", "input",
562 interesting
? interesting
->ift_name
: "(Total)", "output");
564 printf("%10s %5s %10s %10s %5s %10s %5s",
565 "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
567 printf(" %5.5s", "drops");
572 if (interesting
!= NULL
) {
573 struct ifmibdata ifmd
;
575 len
= sizeof(struct ifmibdata
);
576 name
[3] = IFMIB_IFDATA
;
577 name
[4] = interesting_row
;
578 name
[5] = IFDATA_GENERAL
;
579 if (sysctl(name
, 6, &ifmd
, &len
, (void *)0, 0) == -1)
580 err(1, "sysctl IFDATA_GENERAL %d", interesting_row
);
583 printf("%10llu %5llu %10llu %10llu %5llu %10llu %5llu",
584 ifmd
.ifmd_data
.ifi_ipackets
- interesting
->ift_ip
,
585 ifmd
.ifmd_data
.ifi_ierrors
- interesting
->ift_ie
,
586 ifmd
.ifmd_data
.ifi_ibytes
- interesting
->ift_ib
,
587 ifmd
.ifmd_data
.ifi_opackets
- interesting
->ift_op
,
588 ifmd
.ifmd_data
.ifi_oerrors
- interesting
->ift_oe
,
589 ifmd
.ifmd_data
.ifi_obytes
- interesting
->ift_ob
,
590 ifmd
.ifmd_data
.ifi_collisions
- interesting
->ift_co
);
592 printf(" %5llu", ifmd
.ifmd_snd_drops
- interesting
->ift_dr
);
594 interesting
->ift_ip
= ifmd
.ifmd_data
.ifi_ipackets
;
595 interesting
->ift_ie
= ifmd
.ifmd_data
.ifi_ierrors
;
596 interesting
->ift_ib
= ifmd
.ifmd_data
.ifi_ibytes
;
597 interesting
->ift_op
= ifmd
.ifmd_data
.ifi_opackets
;
598 interesting
->ift_oe
= ifmd
.ifmd_data
.ifi_oerrors
;
599 interesting
->ift_ob
= ifmd
.ifmd_data
.ifi_obytes
;
600 interesting
->ift_co
= ifmd
.ifmd_data
.ifi_collisions
;
601 interesting
->ift_dr
= ifmd
.ifmd_snd_drops
;
603 unsigned int latest_ifcount
;
606 name
[3] = IFMIB_SYSTEM
;
607 name
[4] = IFMIB_IFCOUNT
;
608 if (sysctl(name
, 5, &latest_ifcount
, &len
, 0, 0) == 1)
609 err(1, "sysctl IFMIB_IFCOUNT");
610 if (latest_ifcount
> ifcount
) {
611 ifcount
= latest_ifcount
;
612 len
= ifcount
* sizeof(struct ifmibdata
);
614 ifmdall
= malloc(len
);
616 err(1, "malloc failed");
617 } else if (latest_ifcount
> ifcount
) {
618 ifcount
= latest_ifcount
;
619 len
= ifcount
* sizeof(struct ifmibdata
);
621 len
= ifcount
* sizeof(struct ifmibdata
);
622 name
[3] = IFMIB_IFALLDATA
;
624 name
[5] = IFDATA_GENERAL
;
625 if (sysctl(name
, 6, ifmdall
, &len
, (void *)0, 0) == -1)
626 err(1, "sysctl IFMIB_IFALLDATA");
636 for (i
= 0; i
< ifcount
; i
++) {
637 struct ifmibdata
*ifmd
= ifmdall
+ i
;
639 sum
->ift_ip
+= ifmd
->ifmd_data
.ifi_ipackets
;
640 sum
->ift_ie
+= ifmd
->ifmd_data
.ifi_ierrors
;
641 sum
->ift_ib
+= ifmd
->ifmd_data
.ifi_ibytes
;
642 sum
->ift_op
+= ifmd
->ifmd_data
.ifi_opackets
;
643 sum
->ift_oe
+= ifmd
->ifmd_data
.ifi_oerrors
;
644 sum
->ift_ob
+= ifmd
->ifmd_data
.ifi_obytes
;
645 sum
->ift_co
+= ifmd
->ifmd_data
.ifi_collisions
;
646 sum
->ift_dr
+= ifmd
->ifmd_snd_drops
;
649 printf("%10llu %5llu %10llu %10llu %5llu %10llu %5llu",
650 sum
->ift_ip
- total
->ift_ip
,
651 sum
->ift_ie
- total
->ift_ie
,
652 sum
->ift_ib
- total
->ift_ib
,
653 sum
->ift_op
- total
->ift_op
,
654 sum
->ift_oe
- total
->ift_oe
,
655 sum
->ift_ob
- total
->ift_ob
,
656 sum
->ift_co
- total
->ift_co
);
658 printf(" %5llu", sum
->ift_dr
- total
->ift_dr
);
665 oldmask
= sigblock(sigmask(SIGALRM
));
671 (void)alarm(interval
);
682 * Called if an interval expires before sidewaysintpr has completed a loop.
683 * Sets a flag to not wait for the alarm.
686 catchalarm(int signo
)