if.c revision 1.40 1 /* $NetBSD: if.c,v 1.40 2000/04/19 03:26:55 enami Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "from: @(#)if.c 8.2 (Berkeley) 2/21/94";
40 #else
41 __RCSID("$NetBSD: if.c,v 1.40 2000/04/19 03:26:55 enami Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/types.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48
49 #include <net/if.h>
50 #include <net/if_dl.h>
51 #include <net/if_types.h>
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
54 #include <netns/ns.h>
55 #include <netns/ns_if.h>
56 #include <netiso/iso.h>
57 #include <netiso/iso_var.h>
58 #include <arpa/inet.h>
59
60 #include <signal.h>
61 #include <stdio.h>
62 #include <string.h>
63 #include <unistd.h>
64 #include <netdb.h>
65
66 #include "netstat.h"
67
68 #define YES 1
69 #define NO 0
70
71 static void sidewaysintpr __P((u_int, u_long));
72 static void catchalarm __P((int));
73
74 #ifdef INET6
75 char *netname6 __P((struct sockaddr_in6 *, struct in6_addr *));
76 #endif
77
78 /*
79 * Print a description of the network interfaces.
80 * NOTE: ifnetaddr is the location of the kernel global "ifnet",
81 * which is a TAILQ_HEAD.
82 */
83 void
84 intpr(interval, ifnetaddr, pfunc)
85 int interval;
86 u_long ifnetaddr;
87 void (*pfunc)(char *);
88 {
89 struct ifnet ifnet;
90 union {
91 struct ifaddr ifa;
92 struct in_ifaddr in;
93 #ifdef INET6
94 struct in6_ifaddr in6;
95 #endif /* INET6 */
96 struct ns_ifaddr ns;
97 struct iso_ifaddr iso;
98 } ifaddr;
99 u_long ifaddraddr;
100 struct sockaddr *sa;
101 struct ifnet_head ifhead; /* TAILQ_HEAD */
102 char name[IFNAMSIZ];
103 #ifdef INET6
104 char hbuf[NI_MAXHOST]; /* for getnameinfo() */
105 #ifdef KAME_SCOPEID
106 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
107 #else
108 const int niflag = NI_NUMERICHOST;
109 #endif
110 #endif
111
112 if (ifnetaddr == 0) {
113 printf("ifnet: symbol not defined\n");
114 return;
115 }
116 if (interval) {
117 sidewaysintpr((unsigned)interval, ifnetaddr);
118 return;
119 }
120
121 /*
122 * Find the pointer to the first ifnet structure. Replace
123 * the pointer to the TAILQ_HEAD with the actual pointer
124 * to the first list element.
125 */
126 if (kread(ifnetaddr, (char *)&ifhead, sizeof ifhead))
127 return;
128 ifnetaddr = (u_long)ifhead.tqh_first;
129
130 if (!sflag & !pflag) {
131 if (bflag) {
132 printf("%-5.5s %-5.5s %-13.13s %-17.17s "
133 "%10.10s %10.10s",
134 "Name", "Mtu", "Network", "Address",
135 "Ibytes", "Obytes");
136 } else {
137 printf("%-5.5s %-5.5s %-13.13s %-17.17s "
138 "%8.8s %5.5s %8.8s %5.5s %5.5s",
139 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs",
140 "Opkts", "Oerrs", "Colls");
141 }
142 if (tflag)
143 printf(" %4.4s", "Time");
144 if (dflag)
145 printf(" %5.5s", "Drops");
146 putchar('\n');
147 }
148 ifaddraddr = 0;
149 while (ifnetaddr || ifaddraddr) {
150 struct sockaddr_in *sin;
151 #ifdef INET6
152 struct sockaddr_in6 *sin6;
153 #endif /* INET6 */
154 char *cp;
155 int n, m;
156
157 if (ifaddraddr == 0) {
158 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
159 return;
160 memmove(name, ifnet.if_xname, IFNAMSIZ);
161 name[IFNAMSIZ - 1] = '\0'; /* sanity */
162 ifnetaddr = (u_long)ifnet.if_list.tqe_next;
163 if (interface != 0 && strcmp(name, interface) != 0)
164 continue;
165 cp = strchr(name, '\0');
166
167 if (pfunc) {
168 (*pfunc)(name);
169 continue;
170 }
171
172 if ((ifnet.if_flags & IFF_UP) == 0)
173 *cp++ = '*';
174 *cp = '\0';
175 ifaddraddr = (u_long)ifnet.if_addrlist.tqh_first;
176 }
177 printf("%-5.5s %-5llu ", name,
178 (unsigned long long)ifnet.if_mtu);
179 if (ifaddraddr == 0) {
180 printf("%-13.13s ", "none");
181 printf("%-17.17s ", "none");
182 } else {
183 char hexsep = '.'; /* for hexprint */
184 const char *hexfmt = "%x%c"; /* for hexprint */
185 if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
186 ifaddraddr = 0;
187 continue;
188 }
189 #define CP(x) ((char *)(x))
190 cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
191 CP(&ifaddr); sa = (struct sockaddr *)cp;
192 switch (sa->sa_family) {
193 case AF_UNSPEC:
194 printf("%-13.13s ", "none");
195 printf("%-17.17s ", "none");
196 break;
197 case AF_INET:
198 sin = (struct sockaddr_in *)sa;
199 #ifdef notdef
200 /*
201 * can't use inet_makeaddr because kernel
202 * keeps nets unshifted.
203 */
204 in = inet_makeaddr(ifaddr.in.ia_subnet,
205 INADDR_ANY);
206 cp = netname(in.s_addr,
207 ifaddr.in.ia_subnetmask);
208 #else
209 cp = netname(ifaddr.in.ia_subnet,
210 ifaddr.in.ia_subnetmask);
211 #endif
212 if (vflag)
213 n = strlen(cp) < 13 ? 13 : strlen(cp);
214 else
215 n = 13;
216 printf("%-*.*s ", n, n, cp);
217 cp = routename(sin->sin_addr.s_addr);
218 if (vflag)
219 n = strlen(cp) < 17 ? 17 : strlen(cp);
220 else
221 n = 17;
222 printf("%-*.*s ", n, n, cp);
223 if (aflag) {
224 u_long multiaddr;
225 struct in_multi inm;
226
227 multiaddr = (u_long)
228 ifaddr.in.ia_multiaddrs.lh_first;
229 while (multiaddr != 0) {
230 kread(multiaddr, (char *)&inm,
231 sizeof inm);
232 printf("\n%25s %-17.17s ", "",
233 routename(
234 inm.inm_addr.s_addr));
235 multiaddr =
236 (u_long)inm.inm_list.le_next;
237 }
238 }
239 break;
240 #ifdef INET6
241 case AF_INET6:
242 sin6 = (struct sockaddr_in6 *)sa;
243 #ifdef KAME_SCOPEID
244 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
245 sin6->sin6_scope_id =
246 ntohs(*(u_int16_t *)
247 &sin6->sin6_addr.s6_addr[2]);
248 /* too little width */
249 if (!vflag)
250 sin6->sin6_scope_id = 0;
251 sin6->sin6_addr.s6_addr[2] = 0;
252 sin6->sin6_addr.s6_addr[3] = 0;
253 }
254 #endif
255 cp = netname6(&ifaddr.in6.ia_addr,
256 &ifaddr.in6.ia_prefixmask.sin6_addr);
257 if (vflag)
258 n = strlen(cp) < 13 ? 13 : strlen(cp);
259 else
260 n = 13;
261 printf("%-*.*s ", n, n, cp);
262 if (getnameinfo((struct sockaddr *)sin6,
263 sin6->sin6_len,
264 hbuf, sizeof(hbuf), NULL, 0,
265 niflag) != 0) {
266 cp = "?";
267 } else
268 cp = hbuf;
269 if (vflag)
270 n = strlen(cp) < 17 ? 17 : strlen(cp);
271 else
272 n = 17;
273 printf("%-*.*s ", n, n, cp);
274 if (aflag) {
275 u_long multiaddr;
276 struct in6_multi inm;
277 struct sockaddr_in6 sin6;
278
279 multiaddr = (u_long)
280 ifaddr.in6.ia6_multiaddrs.lh_first;
281 while (multiaddr != 0) {
282 kread(multiaddr, (char *)&inm,
283 sizeof inm);
284 memset(&sin6, 0, sizeof(sin6));
285 sin6.sin6_len = sizeof(struct sockaddr_in6);
286 sin6.sin6_family = AF_INET6;
287 sin6.sin6_addr = inm.in6m_addr;
288 sin6.sin6_scope_id =
289 ntohs(*(u_int16_t *)
290 &sin6.sin6_addr.s6_addr[2]);
291 sin6.sin6_addr.s6_addr[2] = 0;
292 sin6.sin6_addr.s6_addr[3] = 0;
293 if (getnameinfo((struct sockaddr *)&sin6,
294 sin6.sin6_len, hbuf,
295 sizeof(hbuf), NULL, 0,
296 niflag) != 0) {
297 strcpy(hbuf, "??");
298 }
299 cp = hbuf;
300 if (vflag)
301 n = strlen(cp) < 17
302 ? 17 : strlen(cp);
303 else
304 n = 17;
305 printf("\n%25s %-*.*s ", "",
306 n, n, cp);
307 multiaddr =
308 (u_long)inm.in6m_entry.le_next;
309 }
310 }
311 break;
312 #endif /*INET6*/
313 #ifndef SMALL
314 case AF_APPLETALK:
315 printf("atalk:%-7.7s ",
316 atalk_print(sa,0x10));
317 printf("%-17.17s ", atalk_print(sa,0x0b));
318 break;
319 case AF_NS:
320 {
321 struct sockaddr_ns *sns =
322 (struct sockaddr_ns *)sa;
323 u_long net;
324 char netnum[8];
325
326 *(union ns_net *)&net = sns->sns_addr.x_net;
327 (void)sprintf(netnum, "%xH",
328 (u_int32_t)ntohl(net));
329 upHex(netnum);
330 printf("ns:%-10s ", netnum);
331 printf("%-17.17s ",
332 ns_phost((struct sockaddr *)sns));
333 }
334 break;
335 #endif
336 case AF_LINK:
337 {
338 struct sockaddr_dl *sdl =
339 (struct sockaddr_dl *)sa;
340 cp = (char *)LLADDR(sdl);
341 if (sdl->sdl_type == IFT_FDDI
342 || sdl->sdl_type == IFT_ETHER)
343 hexsep = ':', hexfmt = "%02x%c";
344 n = sdl->sdl_alen;
345 }
346 m = printf("%-13.13s ", "<Link>");
347 goto hexprint;
348 default:
349 m = printf("(%d)", sa->sa_family);
350 for (cp = sa->sa_len + (char *)sa;
351 --cp > sa->sa_data && (*cp == 0);) {}
352 n = cp - sa->sa_data + 1;
353 cp = sa->sa_data;
354 hexprint:
355 while (--n >= 0)
356 m += printf(hexfmt, *cp++ & 0xff,
357 n > 0 ? hexsep : ' ');
358 m = 32 - m;
359 while (m-- > 0)
360 putchar(' ');
361 break;
362 }
363 ifaddraddr = (u_long)ifaddr.ifa.ifa_list.tqe_next;
364 }
365 if (bflag) {
366 printf("%10llu %10llu",
367 (unsigned long long)ifnet.if_ibytes,
368 (unsigned long long)ifnet.if_obytes);
369 } else {
370 printf("%8llu %5llu %8llu %5llu %5llu",
371 (unsigned long long)ifnet.if_ipackets,
372 (unsigned long long)ifnet.if_ierrors,
373 (unsigned long long)ifnet.if_opackets,
374 (unsigned long long)ifnet.if_oerrors,
375 (unsigned long long)ifnet.if_collisions);
376 }
377 if (tflag)
378 printf(" %4d", ifnet.if_timer);
379 if (dflag)
380 printf(" %5d", ifnet.if_snd.ifq_drops);
381 putchar('\n');
382 }
383 }
384
385 #define MAXIF 100
386 struct iftot {
387 char ift_name[IFNAMSIZ]; /* interface name */
388 u_quad_t ift_ip; /* input packets */
389 u_quad_t ift_ib; /* input bytes */
390 u_quad_t ift_ie; /* input errors */
391 u_quad_t ift_op; /* output packets */
392 u_quad_t ift_ob; /* output bytes */
393 u_quad_t ift_oe; /* output errors */
394 u_quad_t ift_co; /* collisions */
395 int ift_dr; /* drops */
396 } iftot[MAXIF];
397
398 u_char signalled; /* set if alarm goes off "early" */
399
400 /*
401 * Print a running summary of interface statistics.
402 * Repeat display every interval seconds, showing statistics
403 * collected over that interval. Assumes that interval is non-zero.
404 * First line printed at top of screen is always cumulative.
405 */
406 static void
407 sidewaysintpr(interval, off)
408 unsigned interval;
409 u_long off;
410 {
411 struct ifnet ifnet;
412 u_long firstifnet;
413 struct iftot *ip, *total;
414 int line;
415 struct iftot *lastif, *sum, *interesting;
416 struct ifnet_head ifhead; /* TAILQ_HEAD */
417 int oldmask;
418
419 /*
420 * Find the pointer to the first ifnet structure. Replace
421 * the pointer to the TAILQ_HEAD with the actual pointer
422 * to the first list element.
423 */
424 if (kread(off, (char *)&ifhead, sizeof ifhead))
425 return;
426 firstifnet = (u_long)ifhead.tqh_first;
427
428 lastif = iftot;
429 sum = iftot + MAXIF - 1;
430 total = sum - 1;
431 interesting = (interface == NULL) ? iftot : NULL;
432 for (off = firstifnet, ip = iftot; off;) {
433 if (kread(off, (char *)&ifnet, sizeof ifnet))
434 break;
435 memset(ip->ift_name, 0, sizeof(ip->ift_name));
436 snprintf(ip->ift_name, IFNAMSIZ, "%s", ifnet.if_xname);
437 if (interface && strcmp(ifnet.if_xname, interface) == 0)
438 interesting = ip;
439 ip++;
440 if (ip >= iftot + MAXIF - 2)
441 break;
442 off = (u_long)ifnet.if_list.tqe_next;
443 }
444 if (interesting == NULL) {
445 fprintf(stderr, "%s: %s: unknown interface\n",
446 __progname, interface);
447 exit(1);
448 }
449 lastif = ip;
450
451 (void)signal(SIGALRM, catchalarm);
452 signalled = NO;
453 (void)alarm(interval);
454 banner:
455 if (bflag)
456 printf("%7.7s in %8.8s %6.6s out %5.5s",
457 interesting->ift_name, " ",
458 interesting->ift_name, " ");
459 else
460 printf("%5.5s in %5.5s%5.5s out %5.5s %5.5s",
461 interesting->ift_name, " ",
462 interesting->ift_name, " ", " ");
463 if (dflag)
464 printf(" %5.5s", " ");
465 if (lastif - iftot > 0) {
466 if (bflag)
467 printf(" %7.7s in %8.8s %6.6s out %5.5s",
468 "total", " ", "total", " ");
469 else
470 printf(" %5.5s in %5.5s%5.5s out %5.5s %5.5s",
471 "total", " ", "total", " ", " ");
472 if (dflag)
473 printf(" %5.5s", " ");
474 }
475 for (ip = iftot; ip < iftot + MAXIF; ip++) {
476 ip->ift_ip = 0;
477 ip->ift_ib = 0;
478 ip->ift_ie = 0;
479 ip->ift_op = 0;
480 ip->ift_ob = 0;
481 ip->ift_oe = 0;
482 ip->ift_co = 0;
483 ip->ift_dr = 0;
484 }
485 putchar('\n');
486 if (bflag)
487 printf("%10.10s %8.8s %10.10s %5.5s",
488 "bytes", " ", "bytes", " ");
489 else
490 printf("%8.8s %5.5s %8.8s %5.5s %5.5s",
491 "packets", "errs", "packets", "errs", "colls");
492 if (dflag)
493 printf(" %5.5s", "drops");
494 if (lastif - iftot > 0) {
495 if (bflag)
496 printf(" %10.10s %8.8s %10.10s %5.5s",
497 "bytes", " ", "bytes", " ");
498 else
499 printf(" %8.8s %5.5s %8.8s %5.5s %5.5s",
500 "packets", "errs", "packets", "errs", "colls");
501 if (dflag)
502 printf(" %5.5s", "drops");
503 }
504 putchar('\n');
505 fflush(stdout);
506 line = 0;
507 loop:
508 sum->ift_ip = 0;
509 sum->ift_ib = 0;
510 sum->ift_ie = 0;
511 sum->ift_op = 0;
512 sum->ift_ob = 0;
513 sum->ift_oe = 0;
514 sum->ift_co = 0;
515 sum->ift_dr = 0;
516 for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
517 if (kread(off, (char *)&ifnet, sizeof ifnet)) {
518 off = 0;
519 continue;
520 }
521 if (ip == interesting) {
522 if (bflag) {
523 printf("%10llu %8.8s %10llu %5.5s",
524 (unsigned long long)(ifnet.if_ibytes -
525 ip->ift_ib), " ",
526 (unsigned long long)(ifnet.if_obytes -
527 ip->ift_ob), " ");
528 } else {
529 printf("%8llu %5llu %8llu %5llu %5llu",
530 (unsigned long long)
531 (ifnet.if_ipackets - ip->ift_ip),
532 (unsigned long long)
533 (ifnet.if_ierrors - ip->ift_ie),
534 (unsigned long long)
535 (ifnet.if_opackets - ip->ift_op),
536 (unsigned long long)
537 (ifnet.if_oerrors - ip->ift_oe),
538 (unsigned long long)
539 (ifnet.if_collisions - ip->ift_co));
540 }
541 if (dflag)
542 printf(" %5llu",
543 (unsigned long long)
544 (ifnet.if_snd.ifq_drops - ip->ift_dr));
545 }
546 ip->ift_ip = ifnet.if_ipackets;
547 ip->ift_ib = ifnet.if_ibytes;
548 ip->ift_ie = ifnet.if_ierrors;
549 ip->ift_op = ifnet.if_opackets;
550 ip->ift_ob = ifnet.if_obytes;
551 ip->ift_oe = ifnet.if_oerrors;
552 ip->ift_co = ifnet.if_collisions;
553 ip->ift_dr = ifnet.if_snd.ifq_drops;
554 sum->ift_ip += ip->ift_ip;
555 sum->ift_ib += ip->ift_ib;
556 sum->ift_ie += ip->ift_ie;
557 sum->ift_op += ip->ift_op;
558 sum->ift_ob += ip->ift_ob;
559 sum->ift_oe += ip->ift_oe;
560 sum->ift_co += ip->ift_co;
561 sum->ift_dr += ip->ift_dr;
562 off = (u_long)ifnet.if_list.tqe_next;
563 }
564 if (lastif - iftot > 0) {
565 if (bflag) {
566 printf(" %10llu %8.8s %10llu %5.5s",
567 (unsigned long long)
568 (sum->ift_ib - total->ift_ib), " ",
569 (unsigned long long)
570 (sum->ift_ob - total->ift_ob), " ");
571 } else {
572 printf(" %8llu %5llu %8llu %5llu %5llu",
573 (unsigned long long)
574 (sum->ift_ip - total->ift_ip),
575 (unsigned long long)
576 (sum->ift_ie - total->ift_ie),
577 (unsigned long long)
578 (sum->ift_op - total->ift_op),
579 (unsigned long long)
580 (sum->ift_oe - total->ift_oe),
581 (unsigned long long)
582 (sum->ift_co - total->ift_co));
583 }
584 if (dflag)
585 printf(" %5llu",
586 (unsigned long long)(sum->ift_dr - total->ift_dr));
587 }
588 *total = *sum;
589 putchar('\n');
590 fflush(stdout);
591 line++;
592 oldmask = sigblock(sigmask(SIGALRM));
593 if (! signalled) {
594 sigpause(0);
595 }
596 sigsetmask(oldmask);
597 signalled = NO;
598 (void)alarm(interval);
599 if (line == 21)
600 goto banner;
601 goto loop;
602 /*NOTREACHED*/
603 }
604
605 /*
606 * Called if an interval expires before sidewaysintpr has completed a loop.
607 * Sets a flag to not wait for the alarm.
608 */
609 static void
610 catchalarm(signo)
611 int signo;
612 {
613
614 signalled = YES;
615 }
616