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