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