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