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