]> git.saurik.com Git - apple/network_cmds.git/blame_incremental - netstat.tproj/if.c
network_cmds-201.tar.gz
[apple/network_cmds.git] / netstat.tproj / if.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35/*
36static char sccsid[] = "@(#)if.c 8.3 (Berkeley) 4/28/95";
37*/
38static const char rcsid[] =
39 "$Id: if.c,v 1.2 2002/03/05 20:35:13 lindak Exp $";
40#endif /* not lint */
41
42#include <sys/types.h>
43#include <sys/protosw.h>
44#include <sys/socket.h>
45#include <sys/sysctl.h>
46#include <sys/time.h>
47
48#include <net/if.h>
49#include <net/if_var.h>
50#include <net/if_dl.h>
51#include <net/if_types.h>
52#include <net/ethernet.h>
53#include <netinet/in.h>
54#include <netinet/in_var.h>
55
56#ifdef IPX
57#include <netipx/ipx.h>
58#include <netipx/ipx_if.h>
59#endif
60
61#ifdef NS
62#include <netns/ns.h>
63#include <netns/ns_if.h>
64#endif
65#include <arpa/inet.h>
66
67#include <signal.h>
68#include <stdio.h>
69#include <string.h>
70#include <unistd.h>
71
72#include "netstat.h"
73
74#define YES 1
75#define NO 0
76
77static void sidewaysintpr (u_int, u_long);
78static void catchalarm (int);
79
80#ifdef INET6
81char *netname6 (struct sockaddr_in6 *, struct in6_addr *);
82static char ntop_buf[INET6_ADDRSTRLEN]; /* for inet_ntop() */
83static int bdg_done;
84#endif
85
86#if 0
87/* print bridge statistics */
88void
89bdg_stats(u_long dummy , char *name, int af )
90{
91 int i;
92 size_t slen ;
93 struct bdg_stats s ;
94 int mib[4] ;
95
96 slen = sizeof(s);
97
98 mib[0] = CTL_NET ;
99 mib[1] = PF_LINK ;
100 mib[2] = IFT_ETHER ;
101 if (sysctl(mib,4, &s,&slen,NULL,0)==-1)
102 return ; /* no bridging */
103#ifdef INET6
104 if (bdg_done != 0)
105 return;
106 else
107 bdg_done = 1;
108#endif
109 printf("-- Bridging statistics (%s) --\n", name) ;
110 printf(
111"Name In Out Forward Drop Bcast Mcast Local Unknown\n");
112 for (i = 0 ; i < 16 ; i++) {
113 if (s.s[i].name[0])
114 printf("%-6s %9ld%9ld%9ld%9ld%9ld%9ld%9ld%9ld\n",
115 s.s[i].name,
116 s.s[i].p_in[(int)BDG_IN],
117 s.s[i].p_in[(int)BDG_OUT],
118 s.s[i].p_in[(int)BDG_FORWARD],
119 s.s[i].p_in[(int)BDG_DROP],
120 s.s[i].p_in[(int)BDG_BCAST],
121 s.s[i].p_in[(int)BDG_MCAST],
122 s.s[i].p_in[(int)BDG_LOCAL],
123 s.s[i].p_in[(int)BDG_UNKNOWN] );
124 }
125}
126
127#endif
128
129
130/*
131 * Display a formatted value, or a '-' in the same space.
132 */
133static void
134show_stat(const char *fmt, int width, u_long value, short showvalue)
135{
136 char newfmt[32];
137
138 /* Construct the format string */
139 if (showvalue) {
140 sprintf(newfmt, "%%%d%s", width, fmt);
141 printf(newfmt, value);
142 } else {
143 sprintf(newfmt, "%%%ds", width);
144 printf(newfmt, "-");
145 }
146}
147
148
149
150/*
151 * Print a description of the network interfaces.
152 */
153void
154intpr(int interval, u_long ifnetaddr, void (*pfunc)(char *))
155{
156 struct ifnet ifnet;
157 struct ifnethead ifnethead;
158 union {
159 struct ifaddr ifa;
160 struct in_ifaddr in;
161#ifdef INET6
162 struct in6_ifaddr in6;
163#endif
164#if 0
165 struct ipx_ifaddr ipx;
166#endif
167#ifdef NS
168 struct ns_ifaddr ns;
169#endif
170#ifdef ISO
171 struct iso_ifaddr iso;
172#endif
173 } ifaddr;
174 u_long ifaddraddr;
175 u_long ifaddrfound;
176 u_long ifnetfound;
177 u_long opackets;
178 u_long ipackets;
179 u_long obytes;
180 u_long ibytes;
181 u_long oerrors;
182 u_long ierrors;
183 u_long collisions;
184 short timer;
185 int drops;
186 struct sockaddr *sa = NULL;
187 char name[32], tname[16];
188 short network_layer;
189 short link_layer;
190
191 if (ifnetaddr == 0) {
192 printf("ifnet: symbol not defined\n");
193 return;
194 }
195 if (interval) {
196 sidewaysintpr((unsigned)interval, ifnetaddr);
197 return;
198 }
199 if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead))
200 return;
201 ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead);
202 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
203 return;
204
205 if (!pfunc) {
206 printf("%-5.5s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
207 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
208 if (bflag)
209 printf(" %10.10s","Ibytes");
210 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
211 if (bflag)
212 printf(" %10.10s","Obytes");
213 printf(" %5s", "Coll");
214 if (tflag)
215 printf(" %s", "Time");
216 if (dflag)
217 printf(" %s", "Drop");
218 putchar('\n');
219 }
220 ifaddraddr = 0;
221 while (ifnetaddr || ifaddraddr) {
222 struct sockaddr_in *sin;
223#ifdef INET6
224 struct sockaddr_in6 *sin6;
225#endif
226 register char *cp;
227 int n, m;
228
229 network_layer = 0;
230 link_layer = 0;
231
232 if (ifaddraddr == 0) {
233 ifnetfound = ifnetaddr;
234 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) ||
235 kread((u_long)ifnet.if_name, tname, 16))
236 return;
237 tname[15] = '\0';
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))
241 continue;
242 cp = index(name, '\0');
243
244 if (pfunc) {
245 (*pfunc)(name);
246 continue;
247 }
248
249 if ((ifnet.if_flags&IFF_UP) == 0)
250 *cp++ = '*';
251 *cp = '\0';
252 ifaddraddr = (u_long)TAILQ_FIRST(&ifnet.if_addrhead);
253 }
254 printf("%-5.5s %-5lu ", name, ifnet.if_mtu);
255 ifaddrfound = ifaddraddr;
256
257 /*
258 * Get the interface stats. These may get
259 * overriden below on a per-interface basis.
260 */
261 opackets = ifnet.if_opackets;
262 ipackets = ifnet.if_ipackets;
263 obytes = ifnet.if_obytes;
264 ibytes = ifnet.if_ibytes;
265 oerrors = ifnet.if_oerrors;
266 ierrors = ifnet.if_ierrors;
267 collisions = ifnet.if_collisions;
268 timer = ifnet.if_timer;
269 drops = ifnet.if_snd.ifq_drops;
270
271 if (ifaddraddr == 0) {
272 printf("%-13.13s ", "none");
273 printf("%-15.15s ", "none");
274 } else {
275 if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
276 ifaddraddr = 0;
277 continue;
278 }
279#define CP(x) ((char *)(x))
280 cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
281 CP(&ifaddr);
282 sa = (struct sockaddr *)cp;
283 switch (sa->sa_family) {
284 case AF_UNSPEC:
285 printf("%-13.13s ", "none");
286 printf("%-15.15s ", "none");
287 break;
288 case AF_INET:
289 sin = (struct sockaddr_in *)sa;
290#ifdef notdef
291 /* can't use inet_makeaddr because kernel
292 * keeps nets unshifted.
293 */
294 in = inet_makeaddr(ifaddr.in.ia_subnet,
295 INADDR_ANY);
296 printf("%-13.13s ", netname(in.s_addr,
297 ifaddr.in.ia_subnetmask));
298#else
299 printf("%-13.13s ",
300 netname(htonl(ifaddr.in.ia_subnet),
301 ifaddr.in.ia_subnetmask));
302#endif
303 printf("%-15.15s ",
304 routename(sin->sin_addr.s_addr));
305
306 network_layer = 1;
307 break;
308#ifdef INET6
309 case AF_INET6:
310 sin6 = (struct sockaddr_in6 *)sa;
311 printf("%-11.11s ",
312 netname6(&ifaddr.in6.ia_addr,
313 &ifaddr.in6.ia_prefixmask.sin6_addr));
314 printf("%-17.17s ",
315 (char *)inet_ntop(AF_INET6,
316 &sin6->sin6_addr,
317 ntop_buf, sizeof(ntop_buf)));
318
319 network_layer = 1;
320 break;
321#endif /*INET6*/
322#if 0
323 case AF_IPX:
324 {
325 struct sockaddr_ipx *sipx =
326 (struct sockaddr_ipx *)sa;
327 u_long net;
328 char netnum[10];
329
330 *(union ipx_net *) &net = sipx->sipx_addr.x_net;
331 sprintf(netnum, "%lx", (u_long)ntohl(net));
332 printf("ipx:%-8s ", netnum);
333/* printf("ipx:%-8s ", netname(net, 0L)); */
334 printf("%-15s ",
335 ipx_phost((struct sockaddr *)sipx));
336 }
337 break;
338
339 case AF_APPLETALK:
340 printf("atalk:%-12.12s ",atalk_print(sa,0x10) );
341 printf("%-9.9s ",atalk_print(sa,0x0b) );
342 break;
343#endif
344#ifdef NS
345 case AF_NS:
346 {
347 struct sockaddr_ns *sns =
348 (struct sockaddr_ns *)sa;
349 u_long net;
350 char netnum[10];
351
352 *(union ns_net *) &net = sns->sns_addr.x_net;
353 sprintf(netnum, "%lxH", ntohl(net));
354 upHex(netnum);
355 printf("ns:%-8s ", netnum);
356 printf("%-15s ",
357 ns_phost((struct sockaddr *)sns));
358 }
359 break;
360#endif
361 case AF_LINK:
362 {
363 struct sockaddr_dl *sdl =
364 (struct sockaddr_dl *)sa;
365 char linknum[10];
366 cp = (char *)LLADDR(sdl);
367 n = sdl->sdl_alen;
368 sprintf(linknum, "<Link#%d>", sdl->sdl_index);
369 m = printf("%-11.11s ", linknum);
370 }
371 goto hexprint;
372 default:
373 m = printf("(%d)", sa->sa_family);
374 for (cp = sa->sa_len + (char *)sa;
375 --cp > sa->sa_data && (*cp == 0);) {}
376 n = cp - sa->sa_data + 1;
377 cp = sa->sa_data;
378 hexprint:
379 while (--n >= 0)
380 m += printf("%02x%c", *cp++ & 0xff,
381 n > 0 ? ':' : ' ');
382 m = 30 - m;
383 while (m-- > 0)
384 putchar(' ');
385
386 link_layer = 1;
387 break;
388 }
389#ifndef __APPLE__
390 /*
391 * Fixup the statistics for interfaces that
392 * update stats for their network addresses
393 */
394 if (network_layer) {
395 opackets = ifaddr.in.ia_ifa.if_opackets;
396 ipackets = ifaddr.in.ia_ifa.if_ipackets;
397 obytes = ifaddr.in.ia_ifa.if_obytes;
398 ibytes = ifaddr.in.ia_ifa.if_ibytes;
399 }
400#endif
401 ifaddraddr = (u_long)TAILQ_NEXT(&ifaddr.ifa, ifa_link);
402 }
403
404 show_stat("lu", 8, ipackets, link_layer|network_layer);
405 printf(" ");
406 show_stat("lu", 5, ierrors, link_layer);
407 printf(" ");
408 if (bflag) {
409 show_stat("lu", 10, ibytes, link_layer|network_layer);
410 printf(" ");
411 }
412 show_stat("lu", 8, opackets, link_layer|network_layer);
413 printf(" ");
414 show_stat("lu", 5, oerrors, link_layer);
415 printf(" ");
416 if (bflag) {
417 show_stat("lu", 10, obytes, link_layer|network_layer);
418 printf(" ");
419 }
420 show_stat("lu", 5, collisions, link_layer);
421 if (tflag) {
422 printf(" ");
423 show_stat("d", 3, timer, link_layer);
424 }
425 if (dflag) {
426 printf(" ");
427 show_stat("d", 3, drops, link_layer);
428 }
429 putchar('\n');
430 if (aflag && ifaddrfound) {
431 /*
432 * Print family's multicast addresses
433 */
434 u_long multiaddr;
435 struct ifmultiaddr ifma;
436 union {
437 struct sockaddr sa;
438 struct sockaddr_in in;
439#ifdef INET6
440 struct sockaddr_in6 in6;
441#endif /* INET6 */
442 struct sockaddr_dl dl;
443 } msa;
444 const char *fmt;
445
446 for(multiaddr = (u_long)ifnet.if_multiaddrs.lh_first;
447 multiaddr;
448 multiaddr = (u_long)ifma.ifma_link.le_next) {
449 if (kread(multiaddr, (char *)&ifma,
450 sizeof ifma))
451 break;
452 if (kread((u_long)ifma.ifma_addr, (char *)&msa,
453 sizeof msa))
454 break;
455 if (msa.sa.sa_family != sa->sa_family)
456 continue;
457
458 fmt = 0;
459 switch (msa.sa.sa_family) {
460 case AF_INET:
461 fmt = routename(msa.in.sin_addr.s_addr);
462 break;
463#ifdef INET6
464 case AF_INET6:
465 printf("%23s %-19.19s(refs: %d)\n", "",
466 inet_ntop(AF_INET6,
467 &msa.in6.sin6_addr,
468 ntop_buf,
469 sizeof(ntop_buf)),
470 ifma.ifma_refcount);
471 break;
472#endif /* INET6 */
473 case AF_LINK:
474 switch (msa.dl.sdl_type) {
475 case IFT_ETHER:
476 case IFT_FDDI:
477 fmt = ether_ntoa(
478 (struct ether_addr *)
479 LLADDR(&msa.dl));
480 break;
481 }
482 break;
483 }
484 if (fmt)
485 printf("%23s %s\n", "", fmt);
486 }
487 }
488 }
489}
490
491struct iftot {
492 SLIST_ENTRY(iftot) chain;
493 char ift_name[16]; /* interface name */
494 u_long ift_ip; /* input packets */
495 u_long ift_ie; /* input errors */
496 u_long ift_op; /* output packets */
497 u_long ift_oe; /* output errors */
498 u_long ift_co; /* collisions */
499 u_int ift_dr; /* drops */
500 u_long ift_ib; /* input bytes */
501 u_long ift_ob; /* output bytes */
502};
503
504u_char signalled; /* set if alarm goes off "early" */
505
506/*
507 * Print a running summary of interface statistics.
508 * Repeat display every interval seconds, showing statistics
509 * collected over that interval. Assumes that interval is non-zero.
510 * First line printed at top of screen is always cumulative.
511 * XXX - should be rewritten to use ifmib(4).
512 */
513static void
514sidewaysintpr(unsigned interval, u_long off)
515{
516 struct ifnet ifnet;
517 u_long firstifnet;
518 struct ifnethead ifnethead;
519 struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
520 register int line;
521 int oldmask, first;
522 u_long interesting_off;
523
524 if (kread(off, (char *)&ifnethead, sizeof ifnethead))
525 return;
526 firstifnet = (u_long)TAILQ_FIRST(&ifnethead);
527
528 if ((iftot = malloc(sizeof(struct iftot))) == NULL) {
529 printf("malloc failed\n");
530 exit(1);
531 }
532 memset(iftot, 0, sizeof(struct iftot));
533
534 interesting = NULL;
535 interesting_off = 0;
536 for (off = firstifnet, ip = iftot; off;) {
537 char name[16], tname[16];
538
539 if (kread(off, (char *)&ifnet, sizeof ifnet))
540 break;
541 if (kread((u_long)ifnet.if_name, tname, 16))
542 break;
543 tname[15] = '\0';
544 snprintf(name, 16, "%s%d", tname, ifnet.if_unit);
545 if (interface && strcmp(name, interface) == 0) {
546 interesting = ip;
547 interesting_off = off;
548 }
549 snprintf(ip->ift_name, 16, "(%s)", name);;
550 if ((ipn = malloc(sizeof(struct iftot))) == NULL) {
551 printf("malloc failed\n");
552 exit(1);
553 }
554 memset(ipn, 0, sizeof(struct iftot));
555 SLIST_NEXT(ip, chain) = ipn;
556 ip = ipn;
557 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
558 }
559 if ((total = malloc(sizeof(struct iftot))) == NULL) {
560 printf("malloc failed\n");
561 exit(1);
562 }
563 memset(total, 0, sizeof(struct iftot));
564 if ((sum = malloc(sizeof(struct iftot))) == NULL) {
565 printf("malloc failed\n");
566 exit(1);
567 }
568 memset(sum, 0, sizeof(struct iftot));
569
570
571 (void)signal(SIGALRM, catchalarm);
572 signalled = NO;
573 (void)alarm(interval);
574 first = 1;
575banner:
576 printf("%17s %14s %16s", "input",
577 interesting ? interesting->ift_name : "(Total)", "output");
578 putchar('\n');
579 printf("%10s %5s %10s %10s %5s %10s %5s",
580 "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
581 if (dflag)
582 printf(" %5.5s", "drops");
583 putchar('\n');
584 fflush(stdout);
585 line = 0;
586loop:
587 if (interesting != NULL) {
588 ip = interesting;
589 if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) {
590 printf("???\n");
591 exit(1);
592 };
593 if (!first) {
594 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
595 ifnet.if_ipackets - ip->ift_ip,
596 ifnet.if_ierrors - ip->ift_ie,
597 ifnet.if_ibytes - ip->ift_ib,
598 ifnet.if_opackets - ip->ift_op,
599 ifnet.if_oerrors - ip->ift_oe,
600 ifnet.if_obytes - ip->ift_ob,
601 ifnet.if_collisions - ip->ift_co);
602 if (dflag)
603 printf(" %5u", ifnet.if_snd.ifq_drops - ip->ift_dr);
604 }
605 ip->ift_ip = ifnet.if_ipackets;
606 ip->ift_ie = ifnet.if_ierrors;
607 ip->ift_ib = ifnet.if_ibytes;
608 ip->ift_op = ifnet.if_opackets;
609 ip->ift_oe = ifnet.if_oerrors;
610 ip->ift_ob = ifnet.if_obytes;
611 ip->ift_co = ifnet.if_collisions;
612 ip->ift_dr = ifnet.if_snd.ifq_drops;
613 } else {
614 sum->ift_ip = 0;
615 sum->ift_ie = 0;
616 sum->ift_ib = 0;
617 sum->ift_op = 0;
618 sum->ift_oe = 0;
619 sum->ift_ob = 0;
620 sum->ift_co = 0;
621 sum->ift_dr = 0;
622 for (off = firstifnet, ip = iftot;
623 off && SLIST_NEXT(ip, chain) != NULL;
624 ip = SLIST_NEXT(ip, chain)) {
625 if (kread(off, (char *)&ifnet, sizeof ifnet)) {
626 off = 0;
627 continue;
628 }
629 sum->ift_ip += ifnet.if_ipackets;
630 sum->ift_ie += ifnet.if_ierrors;
631 sum->ift_ib += ifnet.if_ibytes;
632 sum->ift_op += ifnet.if_opackets;
633 sum->ift_oe += ifnet.if_oerrors;
634 sum->ift_ob += ifnet.if_obytes;
635 sum->ift_co += ifnet.if_collisions;
636 sum->ift_dr += ifnet.if_snd.ifq_drops;
637 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
638 }
639 if (!first) {
640 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
641 sum->ift_ip - total->ift_ip,
642 sum->ift_ie - total->ift_ie,
643 sum->ift_ib - total->ift_ib,
644 sum->ift_op - total->ift_op,
645 sum->ift_oe - total->ift_oe,
646 sum->ift_ob - total->ift_ob,
647 sum->ift_co - total->ift_co);
648 if (dflag)
649 printf(" %5u", sum->ift_dr - total->ift_dr);
650 }
651 *total = *sum;
652 }
653 if (!first)
654 putchar('\n');
655 fflush(stdout);
656 oldmask = sigblock(sigmask(SIGALRM));
657 if (! signalled) {
658 sigpause(0);
659 }
660 sigsetmask(oldmask);
661 signalled = NO;
662 (void)alarm(interval);
663 line++;
664 first = 0;
665 if (line == 21)
666 goto banner;
667 else
668 goto loop;
669 /*NOTREACHED*/
670}
671
672/*
673 * Called if an interval expires before sidewaysintpr has completed a loop.
674 * Sets a flag to not wait for the alarm.
675 */
676static void
677catchalarm(int signo )
678{
679 signalled = YES;
680}