]>
git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/if.c
e3f438e7c13fb46b781857806e600a3f4f1059d4
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1983, 1988, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 static char sccsid[] = "@(#)if.c 8.3 (Berkeley) 4/28/95";
65 static const char rcsid
[] =
66 "$Id: if.c,v 1.7 2006/01/16 04:53:59 lindak Exp $";
69 #include <sys/types.h>
70 #include <sys/socket.h>
71 #include <sys/sysctl.h>
75 #include <net/if_var.h>
76 #include <net/if_dl.h>
77 #include <net/if_types.h>
78 #include <net/if_mib.h>
79 #include <net/ethernet.h>
80 #include <net/route.h>
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
85 #include <arpa/inet.h>
99 #define ROUNDUP(a, size) (((a) & ((size) - 1)) ? (1 + ((a)|(size - 1))) : (a))
101 #define NEXT_SA(p) (struct sockaddr *) \
102 ((caddr_t)p + (p->sa_len ? ROUNDUP(p->sa_len, sizeof(uint32_t)) : \
105 static void sidewaysintpr ();
106 static void catchalarm (int);
109 char *netname6 (struct sockaddr_in6
*, struct sockaddr
*);
110 static char ntop_buf
[INET6_ADDRSTRLEN
]; /* for inet_ntop() */
114 * Display a formatted value, or a '-' in the same space.
117 show_stat(const char *fmt
, int width
, u_int64_t value
, short showvalue
)
121 /* Construct the format string */
123 sprintf(newfmt
, "%%%d%s", width
, fmt
);
124 printf(newfmt
, value
);
126 sprintf(newfmt
, "%%%ds", width
);
132 get_rti_info(int addrs
, struct sockaddr
*sa
, struct sockaddr
**rti_info
)
137 for (i
= 0; i
< RTAX_MAX
; i
++) {
138 if (addrs
& (1 << i
)) {
140 if (sa
->sa_len
< sizeof(struct sockaddr
))
141 len
+= sizeof(struct sockaddr
);
153 multipr(int family
, char *buf
, char *lim
)
157 for (next
= buf
; next
< lim
; ) {
158 struct ifma_msghdr2
*ifmam
= (struct ifma_msghdr2
*)next
;
159 struct sockaddr
*rti_info
[RTAX_MAX
];
163 next
+= ifmam
->ifmam_msglen
;
164 if (ifmam
->ifmam_type
== RTM_IFINFO2
)
166 else if (ifmam
->ifmam_type
!= RTM_NEWMADDR2
)
168 get_rti_info(ifmam
->ifmam_addrs
, (struct sockaddr
*)(ifmam
+ 1), rti_info
);
169 sa
= rti_info
[RTAX_IFA
];
171 if (sa
->sa_family
!= family
)
173 switch (sa
->sa_family
) {
175 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
177 fmt
= routename(sin
->sin_addr
.s_addr
);
182 struct sockaddr_in6 sin6
;
184 memcpy(&sin6
, sa
, sizeof(struct sockaddr_in6
));
186 if (IN6_IS_ADDR_LINKLOCAL(&sin6
.sin6_addr
) ||
187 IN6_IS_ADDR_MC_LINKLOCAL(&sin6
.sin6_addr
)) {
188 sin6
.sin6_scope_id
= ntohs(*(u_int16_t
*)&sin6
.sin6_addr
.s6_addr
[2]);
189 sin6
.sin6_addr
.s6_addr
[2] = 0;
190 sin6
.sin6_addr
.s6_addr
[3] = 0;
193 printf("%23s %-19.19s(refs: %d)\n", "",
194 inet_ntop(AF_INET6
, &sin6
.sin6_addr
,
195 ntop_buf
, sizeof(ntop_buf
)),
196 ifmam
->ifmam_refcount
);
201 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sa
;
203 switch (sdl
->sdl_type
) {
206 fmt
= ether_ntoa((struct ether_addr
*)
214 printf("%23s %s\n", "", fmt
);
219 * Print a description of the network interfaces.
222 intpr(void (*pfunc
)(char *))
224 u_int64_t opackets
= 0;
225 u_int64_t ipackets
= 0;
226 u_int64_t obytes
= 0;
227 u_int64_t ibytes
= 0;
228 u_int64_t oerrors
= 0;
229 u_int64_t ierrors
= 0;
230 u_int64_t collisions
= 0;
234 struct sockaddr
*sa
= NULL
;
239 char *buf
= NULL
, *lim
, *next
;
241 struct if_msghdr
*ifm
;
242 struct sockaddr
*rti_info
[RTAX_MAX
];
243 unsigned int ifindex
= 0;
251 ifindex
= if_nametoindex(interface
);
253 mib
[0] = CTL_NET
; // networking subsystem
254 mib
[1] = PF_ROUTE
; // type of information
255 mib
[2] = 0; // protocol (IPPROTO_xxx)
256 mib
[3] = 0; // address family
257 mib
[4] = NET_RT_IFLIST2
; // operation
259 if (sysctl(mib
, 6, NULL
, &len
, NULL
, 0) < 0)
261 if ((buf
= malloc(len
)) == NULL
) {
262 printf("malloc failed\n");
265 if (sysctl(mib
, 6, buf
, &len
, NULL
, 0) < 0) {
271 printf("%-5.5s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
272 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
274 printf(" %10.10s","Ibytes");
275 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
277 printf(" %10.10s","Obytes");
278 printf(" %5s", "Coll");
280 printf(" %s", "Time");
282 printf(" %s", "Drop");
286 for (next
= buf
; next
< lim
; ) {
292 ifm
= (struct if_msghdr
*)next
;
293 next
+= ifm
->ifm_msglen
;
295 if (ifm
->ifm_type
== RTM_IFINFO2
) {
296 struct if_msghdr2
*if2m
= (struct if_msghdr2
*)ifm
;
297 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)(if2m
+ 1);
299 strncpy(name
, sdl
->sdl_data
, sdl
->sdl_nlen
);
300 name
[sdl
->sdl_nlen
] = 0;
301 if (interface
!= 0 && if2m
->ifm_index
!= ifindex
)
303 cp
= index(name
, '\0');
310 if ((if2m
->ifm_flags
& IFF_UP
) == 0)
315 * Get the interface stats. These may get
316 * overriden below on a per-interface basis.
318 opackets
= if2m
->ifm_data
.ifi_opackets
;
319 ipackets
= if2m
->ifm_data
.ifi_ipackets
;
320 obytes
= if2m
->ifm_data
.ifi_obytes
;
321 ibytes
= if2m
->ifm_data
.ifi_ibytes
;
322 oerrors
=if2m
->ifm_data
.ifi_oerrors
;
323 ierrors
= if2m
->ifm_data
.ifi_ierrors
;
324 collisions
= if2m
->ifm_data
.ifi_collisions
;
325 timer
= if2m
->ifm_timer
;
326 drops
= if2m
->ifm_snd_drops
;
327 mtu
= if2m
->ifm_data
.ifi_mtu
;
329 get_rti_info(if2m
->ifm_addrs
, (struct sockaddr
*)(if2m
+ 1), rti_info
);
330 sa
= rti_info
[RTAX_IFP
];
331 } else if (ifm
->ifm_type
== RTM_NEWADDR
) {
332 struct ifa_msghdr
*ifam
= (struct ifa_msghdr
*)ifm
;
334 if (interface
!= 0 && ifam
->ifam_index
!= ifindex
)
336 get_rti_info(ifam
->ifam_addrs
, (struct sockaddr
*)(ifam
+ 1), rti_info
);
337 sa
= rti_info
[RTAX_IFA
];
341 printf("%-5.5s %-5u ", name
, mtu
);
344 printf("%-13.13s ", "none");
345 printf("%-15.15s ", "none");
347 switch (sa
->sa_family
) {
349 printf("%-13.13s ", "none");
350 printf("%-15.15s ", "none");
354 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
355 struct sockaddr_in mask
;
357 mask
.sin_addr
.s_addr
= 0;
359 rti_info
[RTAX_NETMASK
],
360 ((struct sockaddr_in
*)rti_info
[RTAX_NETMASK
])->sin_len
);
363 netname(sin
->sin_addr
.s_addr
& mask
.sin_addr
.s_addr
,
364 ntohl(mask
.sin_addr
.s_addr
)));
367 routename(sin
->sin_addr
.s_addr
));
374 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sa
;
375 struct sockaddr
*mask
= (struct sockaddr
*)rti_info
[RTAX_NETMASK
];
381 (char *)inet_ntop(AF_INET6
,
383 ntop_buf
, sizeof(ntop_buf
)));
390 struct sockaddr_dl
*sdl
=
391 (struct sockaddr_dl
*)sa
;
393 cp
= (char *)LLADDR(sdl
);
395 sprintf(linknum
, "<Link#%d>", sdl
->sdl_index
);
396 m
= printf("%-11.11s ", linknum
);
401 m
= printf("(%d)", sa
->sa_family
);
402 for (cp
= sa
->sa_len
+ (char *)sa
;
403 --cp
> sa
->sa_data
&& (*cp
== 0);) {}
404 n
= cp
- sa
->sa_data
+ 1;
408 m
+= printf("%02x%c", *cp
++ & 0xff,
419 show_stat("llu", 8, ipackets
, link_layer
|network_layer
);
421 show_stat("llu", 5, ierrors
, link_layer
);
424 show_stat("llu", 10, ibytes
, link_layer
|network_layer
);
427 show_stat("llu", 8, opackets
, link_layer
|network_layer
);
429 show_stat("llu", 5, oerrors
, link_layer
);
432 show_stat("llu", 10, obytes
, link_layer
|network_layer
);
435 show_stat("llu", 5, collisions
, link_layer
);
438 show_stat("ll", 3, timer
, link_layer
);
442 show_stat("ll", 3, drops
, link_layer
);
447 multipr(sa
->sa_family
, next
, lim
);
452 SLIST_ENTRY(iftot
) chain
;
453 char ift_name
[16]; /* interface name */
454 u_int64_t ift_ip
; /* input packets */
455 u_int64_t ift_ie
; /* input errors */
456 u_int64_t ift_op
; /* output packets */
457 u_int64_t ift_oe
; /* output errors */
458 u_int64_t ift_co
; /* collisions */
459 u_int64_t ift_dr
; /* drops */
460 u_int64_t ift_ib
; /* input bytes */
461 u_int64_t 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 interval seconds, showing statistics
469 * collected over that interval. Assumes that interval is non-zero.
470 * First line printed at top of screen is always cumulative.
471 * XXX - should be rewritten to use ifmib(4).
476 struct iftot
*total
, *sum
, *interesting
;
481 unsigned int ifcount
, i
;
482 struct ifmibdata
*ifmdall
= 0;
484 sigset_t sigset
, oldsigset
;
485 struct itimerval timer_interval
;
488 /* Common OID prefix */
491 name
[2] = NETLINK_GENERIC
;
494 name
[3] = IFMIB_SYSTEM
;
495 name
[4] = IFMIB_IFCOUNT
;
496 if (sysctl(name
, 5, &ifcount
, &len
, 0, 0) == 1)
497 err(1, "sysctl IFMIB_IFCOUNT");
499 len
= ifcount
* sizeof(struct ifmibdata
);
500 ifmdall
= malloc(len
);
502 err(1, "malloc failed");
503 name
[3] = IFMIB_IFALLDATA
;
505 name
[5] = IFDATA_GENERAL
;
506 if (sysctl(name
, 6, ifmdall
, &len
, (void *)0, 0) == -1)
507 err(1, "sysctl IFMIB_IFALLDATA");
511 for (i
= 0; i
< ifcount
; i
++) {
512 struct ifmibdata
*ifmd
= ifmdall
+ i
;
514 if (interface
&& strcmp(ifmd
->ifmd_name
, interface
) == 0) {
515 if ((interesting
= calloc(ifcount
, sizeof(struct iftot
))) == NULL
)
516 err(1, "malloc failed");
517 interesting_row
= i
+ 1;
518 snprintf(interesting
->ift_name
, 16, "(%s)", ifmd
->ifmd_name
);;
521 if ((total
= calloc(1, sizeof(struct iftot
))) == NULL
)
522 err(1, "malloc failed");
524 if ((sum
= calloc(1, sizeof(struct iftot
))) == NULL
)
525 err(1, "malloc failed");
527 /* create a timer that fires repeatedly every interval seconds */
528 timer_interval
.it_value
.tv_sec
= interval
;
529 timer_interval
.it_value
.tv_usec
= 0;
530 timer_interval
.it_interval
.tv_sec
= interval
;
531 timer_interval
.it_interval
.tv_usec
= 0;
532 (void)signal(SIGALRM
, catchalarm
);
534 (void)setitimer(ITIMER_REAL
, &timer_interval
, NULL
);
537 printf("%17s %14s %16s", "input",
538 interesting
? interesting
->ift_name
: "(Total)", "output");
540 printf("%10s %5s %10s %10s %5s %10s %5s",
541 "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
543 printf(" %5.5s", "drops");
548 if (interesting
!= NULL
) {
549 struct ifmibdata ifmd
;
551 len
= sizeof(struct ifmibdata
);
552 name
[3] = IFMIB_IFDATA
;
553 name
[4] = interesting_row
;
554 name
[5] = IFDATA_GENERAL
;
555 if (sysctl(name
, 6, &ifmd
, &len
, (void *)0, 0) == -1)
556 err(1, "sysctl IFDATA_GENERAL %d", interesting_row
);
559 printf("%10llu %5llu %10llu %10llu %5llu %10llu %5llu",
560 ifmd
.ifmd_data
.ifi_ipackets
- interesting
->ift_ip
,
561 ifmd
.ifmd_data
.ifi_ierrors
- interesting
->ift_ie
,
562 ifmd
.ifmd_data
.ifi_ibytes
- interesting
->ift_ib
,
563 ifmd
.ifmd_data
.ifi_opackets
- interesting
->ift_op
,
564 ifmd
.ifmd_data
.ifi_oerrors
- interesting
->ift_oe
,
565 ifmd
.ifmd_data
.ifi_obytes
- interesting
->ift_ob
,
566 ifmd
.ifmd_data
.ifi_collisions
- interesting
->ift_co
);
568 printf(" %5llu", ifmd
.ifmd_snd_drops
- interesting
->ift_dr
);
570 interesting
->ift_ip
= ifmd
.ifmd_data
.ifi_ipackets
;
571 interesting
->ift_ie
= ifmd
.ifmd_data
.ifi_ierrors
;
572 interesting
->ift_ib
= ifmd
.ifmd_data
.ifi_ibytes
;
573 interesting
->ift_op
= ifmd
.ifmd_data
.ifi_opackets
;
574 interesting
->ift_oe
= ifmd
.ifmd_data
.ifi_oerrors
;
575 interesting
->ift_ob
= ifmd
.ifmd_data
.ifi_obytes
;
576 interesting
->ift_co
= ifmd
.ifmd_data
.ifi_collisions
;
577 interesting
->ift_dr
= ifmd
.ifmd_snd_drops
;
579 unsigned int latest_ifcount
;
582 name
[3] = IFMIB_SYSTEM
;
583 name
[4] = IFMIB_IFCOUNT
;
584 if (sysctl(name
, 5, &latest_ifcount
, &len
, 0, 0) == 1)
585 err(1, "sysctl IFMIB_IFCOUNT");
586 if (latest_ifcount
> ifcount
) {
587 ifcount
= latest_ifcount
;
588 len
= ifcount
* sizeof(struct ifmibdata
);
590 ifmdall
= malloc(len
);
592 err(1, "malloc failed");
593 } else if (latest_ifcount
> ifcount
) {
594 ifcount
= latest_ifcount
;
595 len
= ifcount
* sizeof(struct ifmibdata
);
597 len
= ifcount
* sizeof(struct ifmibdata
);
598 name
[3] = IFMIB_IFALLDATA
;
600 name
[5] = IFDATA_GENERAL
;
601 if (sysctl(name
, 6, ifmdall
, &len
, (void *)0, 0) == -1)
602 err(1, "sysctl IFMIB_IFALLDATA");
612 for (i
= 0; i
< ifcount
; i
++) {
613 struct ifmibdata
*ifmd
= ifmdall
+ i
;
615 sum
->ift_ip
+= ifmd
->ifmd_data
.ifi_ipackets
;
616 sum
->ift_ie
+= ifmd
->ifmd_data
.ifi_ierrors
;
617 sum
->ift_ib
+= ifmd
->ifmd_data
.ifi_ibytes
;
618 sum
->ift_op
+= ifmd
->ifmd_data
.ifi_opackets
;
619 sum
->ift_oe
+= ifmd
->ifmd_data
.ifi_oerrors
;
620 sum
->ift_ob
+= ifmd
->ifmd_data
.ifi_obytes
;
621 sum
->ift_co
+= ifmd
->ifmd_data
.ifi_collisions
;
622 sum
->ift_dr
+= ifmd
->ifmd_snd_drops
;
625 printf("%10llu %5llu %10llu %10llu %5llu %10llu %5llu",
626 sum
->ift_ip
- total
->ift_ip
,
627 sum
->ift_ie
- total
->ift_ie
,
628 sum
->ift_ib
- total
->ift_ib
,
629 sum
->ift_op
- total
->ift_op
,
630 sum
->ift_oe
- total
->ift_oe
,
631 sum
->ift_ob
- total
->ift_ob
,
632 sum
->ift_co
- total
->ift_co
);
634 printf(" %5llu", sum
->ift_dr
- total
->ift_dr
);
641 sigemptyset(&sigset
);
642 sigaddset(&sigset
, SIGALRM
);
643 (void)sigprocmask(SIG_BLOCK
, &sigset
, &oldsigset
);
645 sigemptyset(&sigset
);
648 (void)sigprocmask(SIG_SETMASK
, &oldsigset
, NULL
);
661 intervalpr(void (*pr
)(uint32_t, char *, int), uint32_t off
, char *name
, int af
)
663 struct itimerval timer_interval
;
664 sigset_t sigset
, oldsigset
;
666 /* create a timer that fires repeatedly every interval seconds */
667 timer_interval
.it_value
.tv_sec
= interval
;
668 timer_interval
.it_value
.tv_usec
= 0;
669 timer_interval
.it_interval
.tv_sec
= interval
;
670 timer_interval
.it_interval
.tv_usec
= 0;
671 (void) signal(SIGALRM
, catchalarm
);
673 (void) setitimer(ITIMER_REAL
, &timer_interval
, NULL
);
679 sigemptyset(&sigset
);
680 sigaddset(&sigset
, SIGALRM
);
681 (void) sigprocmask(SIG_BLOCK
, &sigset
, &oldsigset
);
683 sigemptyset(&sigset
);
686 (void) sigprocmask(SIG_SETMASK
, &oldsigset
, NULL
);
692 * Called if an interval expires before sidewaysintpr has completed a loop.
693 * Sets a flag to not wait for the alarm.
696 catchalarm(int signo
)