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