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