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