if.c revision 1.36 1 /* $NetBSD: if.c,v 1.36 2000/01/25 15:56:55 itojun 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.36 2000/01/25 15:56:55 itojun 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 printf("%-13.13s ", netname(in.s_addr,
207 ifaddr.in.ia_subnetmask));
208 #else
209 cp = netname(ifaddr.in.ia_subnet,
210 ifaddr.in.ia_subnetmask);
211 if (vflag)
212 n = strlen(cp) < 13 ? 13 : strlen(cp);
213 else
214 n = 13;
215 printf("%-*.*s ", n, n, cp);
216 #endif
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 cp = netname6(&ifaddr.in6.ia_addr,
244 &ifaddr.in6.ia_prefixmask.sin6_addr);
245 if (vflag)
246 n = strlen(cp) < 13 ? 13 : strlen(cp);
247 else
248 n = 13;
249 printf("%-*.*s ", n, n, cp);
250 #if 0 /* KAME_SCOPEID: don't do it twice */
251 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
252 sin6->sin6_scope_id =
253 ntohs(*(u_int16_t *)
254 &sin6->sin6_addr.s6_addr[2]);
255 sin6->sin6_addr.s6_addr[2] = 0;
256 sin6->sin6_addr.s6_addr[3] = 0;
257 }
258 #endif
259 if (getnameinfo((struct sockaddr *)sin6,
260 sin6->sin6_len,
261 hbuf, sizeof(hbuf), NULL, 0,
262 niflag) != 0) {
263 cp = "?";
264 } else
265 cp = hbuf;
266 if (vflag)
267 n = strlen(cp) < 17 ? 17 : strlen(cp);
268 else
269 n = 17;
270 printf("%-*.*s ", n, n, cp);
271 if (aflag) {
272 u_long multiaddr;
273 struct in6_multi inm;
274
275 multiaddr = (u_long)
276 ifaddr.in6.ia6_multiaddrs.lh_first;
277 while (multiaddr != 0) {
278 kread(multiaddr, (char *)&inm,
279 sizeof inm);
280 inet_ntop(AF_INET6, &inm.in6m_addr,
281 hbuf, sizeof(hbuf));
282 cp = hbuf;
283 if (vflag)
284 n = strlen(cp) < 17
285 ? 17 : strlen(cp);
286 else
287 n = 17;
288 printf("\n%25s %-*.*s ", "",
289 n, n, cp);
290 multiaddr =
291 (u_long)inm.in6m_entry.le_next;
292 }
293 }
294 break;
295 #endif /*INET6*/
296 #ifndef SMALL
297 case AF_APPLETALK:
298 printf("atalk:%-7.7s ",
299 atalk_print(sa,0x10));
300 printf("%-17.17s ", atalk_print(sa,0x0b));
301 break;
302 case AF_NS:
303 {
304 struct sockaddr_ns *sns =
305 (struct sockaddr_ns *)sa;
306 u_long net;
307 char netnum[8];
308
309 *(union ns_net *)&net = sns->sns_addr.x_net;
310 (void)sprintf(netnum, "%xH",
311 (u_int32_t)ntohl(net));
312 upHex(netnum);
313 printf("ns:%-10s ", netnum);
314 printf("%-17.17s ",
315 ns_phost((struct sockaddr *)sns));
316 }
317 break;
318 #endif
319 case AF_LINK:
320 {
321 struct sockaddr_dl *sdl =
322 (struct sockaddr_dl *)sa;
323 cp = (char *)LLADDR(sdl);
324 if (sdl->sdl_type == IFT_FDDI
325 || sdl->sdl_type == IFT_ETHER)
326 hexsep = ':', hexfmt = "%02x%c";
327 n = sdl->sdl_alen;
328 }
329 m = printf("%-13.13s ", "<Link>");
330 goto hexprint;
331 default:
332 m = printf("(%d)", sa->sa_family);
333 for (cp = sa->sa_len + (char *)sa;
334 --cp > sa->sa_data && (*cp == 0);) {}
335 n = cp - sa->sa_data + 1;
336 cp = sa->sa_data;
337 hexprint:
338 while (--n >= 0)
339 m += printf(hexfmt, *cp++ & 0xff,
340 n > 0 ? hexsep : ' ');
341 m = 32 - m;
342 while (m-- > 0)
343 putchar(' ');
344 break;
345 }
346 ifaddraddr = (u_long)ifaddr.ifa.ifa_list.tqe_next;
347 }
348 if (bflag) {
349 printf("%10llu %10llu",
350 (unsigned long long)ifnet.if_ibytes,
351 (unsigned long long)ifnet.if_obytes);
352 } else {
353 printf("%8llu %5llu %8llu %5llu %5llu",
354 (unsigned long long)ifnet.if_ipackets,
355 (unsigned long long)ifnet.if_ierrors,
356 (unsigned long long)ifnet.if_opackets,
357 (unsigned long long)ifnet.if_oerrors,
358 (unsigned long long)ifnet.if_collisions);
359 }
360 if (tflag)
361 printf(" %4d", ifnet.if_timer);
362 if (dflag)
363 printf(" %5d", ifnet.if_snd.ifq_drops);
364 putchar('\n');
365 }
366 }
367
368 #define MAXIF 100
369 struct iftot {
370 char ift_name[IFNAMSIZ]; /* interface name */
371 u_long ift_ip; /* input packets */
372 u_long ift_ib; /* input bytes */
373 u_long ift_ie; /* input errors */
374 u_long ift_op; /* output packets */
375 u_long ift_ob; /* output bytes */
376 u_long ift_oe; /* output errors */
377 u_long ift_co; /* collisions */
378 int ift_dr; /* drops */
379 } iftot[MAXIF];
380
381 u_char signalled; /* set if alarm goes off "early" */
382
383 /*
384 * Print a running summary of interface statistics.
385 * Repeat display every interval seconds, showing statistics
386 * collected over that interval. Assumes that interval is non-zero.
387 * First line printed at top of screen is always cumulative.
388 */
389 static void
390 sidewaysintpr(interval, off)
391 unsigned interval;
392 u_long off;
393 {
394 struct ifnet ifnet;
395 u_long firstifnet;
396 struct iftot *ip, *total;
397 int line;
398 struct iftot *lastif, *sum, *interesting;
399 struct ifnet_head ifhead; /* TAILQ_HEAD */
400 int oldmask;
401
402 /*
403 * Find the pointer to the first ifnet structure. Replace
404 * the pointer to the TAILQ_HEAD with the actual pointer
405 * to the first list element.
406 */
407 if (kread(off, (char *)&ifhead, sizeof ifhead))
408 return;
409 firstifnet = (u_long)ifhead.tqh_first;
410
411 lastif = iftot;
412 sum = iftot + MAXIF - 1;
413 total = sum - 1;
414 interesting = (interface == NULL) ? iftot : NULL;
415 for (off = firstifnet, ip = iftot; off;) {
416 if (kread(off, (char *)&ifnet, sizeof ifnet))
417 break;
418 memset(ip->ift_name, 0, sizeof(ip->ift_name));
419 snprintf(ip->ift_name, IFNAMSIZ, "%s", ifnet.if_xname);
420 if (interface && strcmp(ifnet.if_xname, interface) == 0)
421 interesting = ip;
422 ip++;
423 if (ip >= iftot + MAXIF - 2)
424 break;
425 off = (u_long)ifnet.if_list.tqe_next;
426 }
427 if (interesting == NULL) {
428 fprintf(stderr, "%s: %s: unknown interface\n",
429 __progname, interface);
430 exit(1);
431 }
432 lastif = ip;
433
434 (void)signal(SIGALRM, catchalarm);
435 signalled = NO;
436 (void)alarm(interval);
437 banner:
438 if (bflag)
439 printf("%7.7s in %8.8s %6.6s out %5.5s",
440 interesting->ift_name, " ",
441 interesting->ift_name, " ");
442 else
443 printf("%5.5s in %5.5s%5.5s out %5.5s %5.5s",
444 interesting->ift_name, " ",
445 interesting->ift_name, " ", " ");
446 if (dflag)
447 printf(" %5.5s", " ");
448 if (lastif - iftot > 0) {
449 if (bflag)
450 printf(" %7.7s in %8.8s %6.6s out %5.5s",
451 "total", " ", "total", " ");
452 else
453 printf(" %5.5s in %5.5s%5.5s out %5.5s %5.5s",
454 "total", " ", "total", " ", " ");
455 if (dflag)
456 printf(" %5.5s", " ");
457 }
458 for (ip = iftot; ip < iftot + MAXIF; ip++) {
459 ip->ift_ip = 0;
460 ip->ift_ib = 0;
461 ip->ift_ie = 0;
462 ip->ift_op = 0;
463 ip->ift_ob = 0;
464 ip->ift_oe = 0;
465 ip->ift_co = 0;
466 ip->ift_dr = 0;
467 }
468 putchar('\n');
469 if (bflag)
470 printf("%10.10s %8.8s %10.10s %5.5s",
471 "bytes", " ", "bytes", " ");
472 else
473 printf("%8.8s %5.5s %8.8s %5.5s %5.5s",
474 "packets", "errs", "packets", "errs", "colls");
475 if (dflag)
476 printf(" %5.5s", "drops");
477 if (lastif - iftot > 0) {
478 if (bflag)
479 printf(" %10.10s %8.8s %10.10s %5.5s",
480 "bytes", " ", "bytes", " ");
481 else
482 printf(" %8.8s %5.5s %8.8s %5.5s %5.5s",
483 "packets", "errs", "packets", "errs", "colls");
484 if (dflag)
485 printf(" %5.5s", "drops");
486 }
487 putchar('\n');
488 fflush(stdout);
489 line = 0;
490 loop:
491 sum->ift_ip = 0;
492 sum->ift_ib = 0;
493 sum->ift_ie = 0;
494 sum->ift_op = 0;
495 sum->ift_ob = 0;
496 sum->ift_oe = 0;
497 sum->ift_co = 0;
498 sum->ift_dr = 0;
499 for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
500 if (kread(off, (char *)&ifnet, sizeof ifnet)) {
501 off = 0;
502 continue;
503 }
504 if (ip == interesting) {
505 if (bflag) {
506 printf("%10llu %8.8s %10llu %5.5s",
507 (unsigned long long)(ifnet.if_ibytes -
508 ip->ift_ib), " ",
509 (unsigned long long)(ifnet.if_obytes -
510 ip->ift_ob), " ");
511 } else {
512 printf("%8llu %5llu %8llu %5llu %5llu",
513 (unsigned long long)
514 (ifnet.if_ipackets - ip->ift_ip),
515 (unsigned long long)
516 (ifnet.if_ierrors - ip->ift_ie),
517 (unsigned long long)
518 (ifnet.if_opackets - ip->ift_op),
519 (unsigned long long)
520 (ifnet.if_oerrors - ip->ift_oe),
521 (unsigned long long)
522 (ifnet.if_collisions - ip->ift_co));
523 }
524 if (dflag)
525 printf(" %5llu",
526 (unsigned long long)
527 (ifnet.if_snd.ifq_drops - ip->ift_dr));
528 }
529 ip->ift_ip = ifnet.if_ipackets;
530 ip->ift_ib = ifnet.if_ibytes;
531 ip->ift_ie = ifnet.if_ierrors;
532 ip->ift_op = ifnet.if_opackets;
533 ip->ift_ob = ifnet.if_obytes;
534 ip->ift_oe = ifnet.if_oerrors;
535 ip->ift_co = ifnet.if_collisions;
536 ip->ift_dr = ifnet.if_snd.ifq_drops;
537 sum->ift_ip += ip->ift_ip;
538 sum->ift_ib += ip->ift_ib;
539 sum->ift_ie += ip->ift_ie;
540 sum->ift_op += ip->ift_op;
541 sum->ift_ob += ip->ift_ob;
542 sum->ift_oe += ip->ift_oe;
543 sum->ift_co += ip->ift_co;
544 sum->ift_dr += ip->ift_dr;
545 off = (u_long)ifnet.if_list.tqe_next;
546 }
547 if (lastif - iftot > 0) {
548 if (bflag) {
549 printf(" %10llu %8.8s %10llu %5.5s",
550 (unsigned long long)
551 (sum->ift_ib - total->ift_ib), " ",
552 (unsigned long long)
553 (sum->ift_ob - total->ift_ob), " ");
554 } else {
555 printf(" %8llu %5llu %8llu %5llu %5llu",
556 (unsigned long long)
557 (sum->ift_ip - total->ift_ip),
558 (unsigned long long)
559 (sum->ift_ie - total->ift_ie),
560 (unsigned long long)
561 (sum->ift_op - total->ift_op),
562 (unsigned long long)
563 (sum->ift_oe - total->ift_oe),
564 (unsigned long long)
565 (sum->ift_co - total->ift_co));
566 }
567 if (dflag)
568 printf(" %5llu",
569 (unsigned long long)(sum->ift_dr - total->ift_dr));
570 }
571 *total = *sum;
572 putchar('\n');
573 fflush(stdout);
574 line++;
575 oldmask = sigblock(sigmask(SIGALRM));
576 if (! signalled) {
577 sigpause(0);
578 }
579 sigsetmask(oldmask);
580 signalled = NO;
581 (void)alarm(interval);
582 if (line == 21)
583 goto banner;
584 goto loop;
585 /*NOTREACHED*/
586 }
587
588 /*
589 * Called if an interval expires before sidewaysintpr has completed a loop.
590 * Sets a flag to not wait for the alarm.
591 */
592 static void
593 catchalarm(signo)
594 int signo;
595 {
596
597 signalled = YES;
598 }
599