1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2013 Gleb Smirnoff <glebius@FreeBSD.org> 5 * Copyright (c) 1983, 1988, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #if 0 34 #endif 35 36 #include <sys/cdefs.h> 37 #include <sys/param.h> 38 #include <sys/protosw.h> 39 #include <sys/socket.h> 40 #include <sys/socketvar.h> 41 #include <sys/time.h> 42 43 #include <net/if.h> 44 #include <net/if_dl.h> 45 #include <net/if_types.h> 46 #include <net/ethernet.h> 47 #include <netinet/in.h> 48 #include <netinet/in_var.h> 49 #include <arpa/inet.h> 50 #ifdef PF 51 #include <net/pfvar.h> 52 #include <net/if_pfsync.h> 53 #endif 54 55 #include <err.h> 56 #include <errno.h> 57 #include <ifaddrs.h> 58 #include <libutil.h> 59 #ifdef INET6 60 #include <netdb.h> 61 #endif 62 #include <signal.h> 63 #include <stdbool.h> 64 #include <stdint.h> 65 #include <stdio.h> 66 #include <stdlib.h> 67 #include <string.h> 68 #include <sysexits.h> 69 #include <unistd.h> 70 #include <libxo/xo.h> 71 72 #include "netstat.h" 73 74 static void sidewaysintpr(void); 75 76 #ifdef PF 77 static const char* pfsyncacts[] = { 78 /* PFSYNC_ACT_CLR */ "clear all request", 79 /* PFSYNC_ACT_INS_1301 */ "13.1 state insert", 80 /* PFSYNC_ACT_INS_ACK */ "state inserted ack", 81 /* PFSYNC_ACT_UPD_1301 */ "13.1 state update", 82 /* PFSYNC_ACT_UPD_C */ "compressed state update", 83 /* PFSYNC_ACT_UPD_REQ */ "uncompressed state request", 84 /* PFSYNC_ACT_DEL */ "state delete", 85 /* PFSYNC_ACT_DEL_C */ "compressed state delete", 86 /* PFSYNC_ACT_INS_F */ "fragment insert", 87 /* PFSYNC_ACT_DEL_F */ "fragment delete", 88 /* PFSYNC_ACT_BUS */ "bulk update mark", 89 /* PFSYNC_ACT_TDB */ "TDB replay counter update", 90 /* PFSYNC_ACT_EOF */ "end of frame mark", 91 /* PFSYNC_ACT_INS_1400 */ "state insert", 92 /* PFSYNC_ACT_UPD_1400 */ "state update", 93 }; 94 95 static const char* pfsyncacts_name[] = { 96 /* PFSYNC_ACT_CLR */ "clear-all-request", 97 /* PFSYNC_ACT_INS_1301 */ "state-insert-1301", 98 /* PFSYNC_ACT_INS_ACK */ "state-inserted-ack", 99 /* PFSYNC_ACT_UPD_1301 */ "state-update-1301", 100 /* PFSYNC_ACT_UPD_C */ "compressed-state-update", 101 /* PFSYNC_ACT_UPD_REQ */ "uncompressed-state-request", 102 /* PFSYNC_ACT_DEL */ "state-delete", 103 /* PFSYNC_ACT_DEL_C */ "compressed-state-delete", 104 /* PFSYNC_ACT_INS_F */ "fragment-insert", 105 /* PFSYNC_ACT_DEL_F */ "fragment-delete", 106 /* PFSYNC_ACT_BUS */ "bulk-update-mark", 107 /* PFSYNC_ACT_TDB */ "TDB-replay-counter-update", 108 /* PFSYNC_ACT_EOF */ "end-of-frame-mark", 109 /* PFSYNC_ACT_INS_1400 */ "state-insert", 110 /* PFSYNC_ACT_UPD_1400 */ "state-update", 111 }; 112 113 static void 114 pfsync_acts_stats(const char *list, const char *desc, uint64_t *a) 115 { 116 int i; 117 118 xo_open_list(list); 119 for (i = 0; i < PFSYNC_ACT_MAX; i++, a++) { 120 if (*a || sflag <= 1) { 121 xo_open_instance(list); 122 xo_emit("\t\t{e:name}{:count/%ju} {N:/%s%s %s}\n", 123 pfsyncacts_name[i], (uintmax_t)(*a), 124 pfsyncacts[i], plural(*a), desc); 125 xo_close_instance(list); 126 } 127 } 128 xo_close_list(list); 129 } 130 131 /* 132 * Dump pfsync statistics structure. 133 */ 134 void 135 pfsync_stats(u_long off, const char *name, int af1 __unused, int proto __unused) 136 { 137 struct pfsyncstats pfsyncstat; 138 139 if (fetch_stats("net.pfsync.stats", off, &pfsyncstat, 140 sizeof(pfsyncstat), kread) != 0) 141 return; 142 143 xo_emit("{T:/%s}:\n", name); 144 xo_open_container(name); 145 146 #define p(f, m) if (pfsyncstat.f || sflag <= 1) \ 147 xo_emit(m, (uintmax_t)pfsyncstat.f, plural(pfsyncstat.f)) 148 149 p(pfsyncs_ipackets, "\t{:received-inet-packets/%ju} " 150 "{N:/packet%s received (IPv4)}\n"); 151 p(pfsyncs_ipackets6, "\t{:received-inet6-packets/%ju} " 152 "{N:/packet%s received (IPv6)}\n"); 153 pfsync_acts_stats("input-histogram", "received", 154 &pfsyncstat.pfsyncs_iacts[0]); 155 p(pfsyncs_badif, "\t\t{:dropped-bad-interface/%ju} " 156 "{N:/packet%s discarded for bad interface}\n"); 157 p(pfsyncs_badttl, "\t\t{:dropped-bad-ttl/%ju} " 158 "{N:/packet%s discarded for bad ttl}\n"); 159 p(pfsyncs_hdrops, "\t\t{:dropped-short-header/%ju} " 160 "{N:/packet%s shorter than header}\n"); 161 p(pfsyncs_badver, "\t\t{:dropped-bad-version/%ju} " 162 "{N:/packet%s discarded for bad version}\n"); 163 p(pfsyncs_badauth, "\t\t{:dropped-bad-auth/%ju} " 164 "{N:/packet%s discarded for bad HMAC}\n"); 165 p(pfsyncs_badact,"\t\t{:dropped-bad-action/%ju} " 166 "{N:/packet%s discarded for bad action}\n"); 167 p(pfsyncs_badlen, "\t\t{:dropped-short/%ju} " 168 "{N:/packet%s discarded for short packet}\n"); 169 p(pfsyncs_badval, "\t\t{:dropped-bad-values/%ju} " 170 "{N:/state%s discarded for bad values}\n"); 171 p(pfsyncs_stale, "\t\t{:dropped-stale-state/%ju} " 172 "{N:/stale state%s}\n"); 173 p(pfsyncs_badstate, "\t\t{:dropped-failed-lookup/%ju} " 174 "{N:/failed state lookup\\/insert%s}\n"); 175 p(pfsyncs_opackets, "\t{:sent-inet-packets/%ju} " 176 "{N:/packet%s sent (IPv4})\n"); 177 p(pfsyncs_opackets6, "\t{:send-inet6-packets/%ju} " 178 "{N:/packet%s sent (IPv6})\n"); 179 pfsync_acts_stats("output-histogram", "sent", 180 &pfsyncstat.pfsyncs_oacts[0]); 181 p(pfsyncs_onomem, "\t\t{:discarded-no-memory/%ju} " 182 "{N:/failure%s due to mbuf memory error}\n"); 183 p(pfsyncs_oerrors, "\t\t{:send-errors/%ju} " 184 "{N:/send error%s}\n"); 185 #undef p 186 xo_close_container(name); 187 } 188 #endif /* PF */ 189 190 /* 191 * Display a formatted value, or a '-' in the same space. 192 */ 193 static void 194 show_stat(const char *fmt, int width, const char *name, 195 u_long value, short showvalue, int div1000) 196 { 197 const char *lsep, *rsep; 198 char newfmt[64]; 199 200 lsep = ""; 201 if (strncmp(fmt, "LS", 2) == 0) { 202 lsep = " "; 203 fmt += 2; 204 } 205 rsep = " "; 206 if (strncmp(fmt, "NRS", 3) == 0) { 207 rsep = ""; 208 fmt += 3; 209 } 210 if (showvalue == 0) { 211 /* Print just dash. */ 212 xo_emit("{P:/%s}{D:/%*s}{P:/%s}", lsep, width, "-", rsep); 213 return; 214 } 215 216 /* 217 * XXX: workaround {P:} modifier can't be empty and doesn't seem to 218 * take args... so we need to conditionally include it in the format. 219 */ 220 #define maybe_pad(pad) do { \ 221 if (strlen(pad)) { \ 222 snprintf(newfmt, sizeof(newfmt), "{P:%s}", pad); \ 223 xo_emit(newfmt); \ 224 } \ 225 } while (0) 226 227 if (hflag) { 228 char buf[5]; 229 230 /* Format in human readable form. */ 231 humanize_number(buf, sizeof(buf), (int64_t)value, "", 232 HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL | \ 233 ((div1000) ? HN_DIVISOR_1000 : 0)); 234 maybe_pad(lsep); 235 snprintf(newfmt, sizeof(newfmt), "{:%s/%%%ds}", name, width); 236 xo_emit(newfmt, buf); 237 maybe_pad(rsep); 238 } else { 239 /* Construct the format string. */ 240 maybe_pad(lsep); 241 snprintf(newfmt, sizeof(newfmt), "{:%s/%%%d%s}", 242 name, width, fmt); 243 xo_emit(newfmt, value); 244 maybe_pad(rsep); 245 } 246 } 247 248 /* 249 * Find next multiaddr for a given interface name. 250 */ 251 static struct ifmaddrs * 252 next_ifma(struct ifmaddrs *ifma, const char *name, const sa_family_t family) 253 { 254 255 for(; ifma != NULL; ifma = ifma->ifma_next) { 256 struct sockaddr_dl *sdl; 257 258 sdl = (struct sockaddr_dl *)ifma->ifma_name; 259 if (ifma->ifma_addr->sa_family == family && 260 strcmp(sdl->sdl_data, name) == 0) 261 break; 262 } 263 264 return (ifma); 265 } 266 267 enum process_op { MEASURE, EMIT }; 268 269 static void 270 process_ifa_addr(enum process_op op, struct ifaddrs *ifa, int *max_net_len, 271 int *max_addr_len, bool *network, bool *link) 272 { 273 int net_len, addr_len; 274 const char *nn, *rn; 275 276 if (op == EMIT) { 277 net_len = *max_net_len; 278 addr_len = *max_addr_len; 279 } 280 281 switch (ifa->ifa_addr->sa_family) { 282 case AF_UNSPEC: 283 if (op == MEASURE) { 284 net_len = strlen("none"); 285 addr_len = strlen("none"); 286 } else { 287 xo_emit("{:network/%-*.*s} ", net_len, net_len, 288 "none"); 289 xo_emit("{:address/%-*.*s} ", addr_len, addr_len, 290 "none"); 291 } 292 break; 293 case AF_INET: 294 #ifdef INET6 295 case AF_INET6: 296 #endif /* INET6 */ 297 nn = netname(ifa->ifa_addr, ifa->ifa_netmask); 298 rn = routename(ifa->ifa_addr, numeric_addr); 299 if (op == MEASURE) { 300 net_len = strlen(nn); 301 addr_len = strlen(rn); 302 } else { 303 xo_emit("{t:network/%-*s} ", net_len, nn); 304 xo_emit("{t:address/%-*s} ", addr_len, rn); 305 } 306 307 if (network != NULL) 308 *network = true; 309 break; 310 case AF_LINK: 311 { 312 struct sockaddr_dl *sdl; 313 char linknum[sizeof("<Link#32767>")]; 314 315 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 316 snprintf(linknum, sizeof(linknum), "<Link#%d>", sdl->sdl_index); 317 if (op == MEASURE) { 318 net_len = strlen(linknum); 319 if (sdl->sdl_nlen == 0 && 320 sdl->sdl_alen == 0 && 321 sdl->sdl_slen == 0) 322 addr_len = 1; 323 else 324 addr_len = strlen(routename(ifa->ifa_addr, 1)); 325 } else { 326 xo_emit("{t:network/%-*.*s} ", net_len, net_len, 327 linknum); 328 if (sdl->sdl_nlen == 0 && 329 sdl->sdl_alen == 0 && 330 sdl->sdl_slen == 0) 331 xo_emit("{P:/%*s} ", addr_len, ""); 332 else 333 xo_emit("{t:address/%-*.*s} ", addr_len, 334 addr_len, routename(ifa->ifa_addr, 1)); 335 } 336 if (link != NULL) 337 *link = true; 338 break; 339 } 340 } 341 342 if (op == MEASURE) { 343 if (net_len > *max_net_len) 344 *max_net_len = net_len; 345 if (addr_len > *max_addr_len) 346 *max_addr_len = addr_len; 347 } 348 } 349 350 static int 351 max_num_len(int max_len, u_long num) 352 { 353 int len = 2; /* include space */ 354 355 for (; num > 10; len++) 356 num /= 10; 357 return (MAX(max_len, len)); 358 } 359 360 /* 361 * Print a description of the network interfaces. 362 */ 363 void 364 intpr(void (*pfunc)(char *), int af) 365 { 366 struct ifaddrs *ifap, *ifa; 367 struct ifmaddrs *ifmap, *ifma; 368 u_int ifn_len_max = 5, ifn_len; 369 u_int net_len = strlen("Network "), addr_len = strlen("Address "); 370 u_int npkt_len = 8, nbyte_len = 10, nerr_len = 5; 371 372 if (interval) 373 return sidewaysintpr(); 374 375 if (getifaddrs(&ifap) != 0) 376 err(EX_OSERR, "getifaddrs"); 377 if (aflag && getifmaddrs(&ifmap) != 0) 378 err(EX_OSERR, "getifmaddrs"); 379 380 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 381 if (interface != NULL && 382 strcmp(ifa->ifa_name, interface) != 0) 383 continue; 384 if (af != AF_UNSPEC && ifa->ifa_addr->sa_family != af) 385 continue; 386 ifn_len = strlen(ifa->ifa_name); 387 if ((ifa->ifa_flags & IFF_UP) == 0) 388 ++ifn_len; 389 ifn_len_max = MAX(ifn_len_max, ifn_len); 390 process_ifa_addr(MEASURE, ifa, &net_len, &addr_len, 391 NULL, NULL); 392 393 #define IFA_STAT(s) (((struct if_data *)ifa->ifa_data)->ifi_ ## s) 394 if (!hflag) { 395 npkt_len = max_num_len(npkt_len, IFA_STAT(ipackets)); 396 npkt_len = max_num_len(npkt_len, IFA_STAT(opackets)); 397 nerr_len = max_num_len(nerr_len, IFA_STAT(ierrors)); 398 nerr_len = max_num_len(nerr_len, IFA_STAT(iqdrops)); 399 nerr_len = max_num_len(nerr_len, IFA_STAT(collisions)); 400 if (dflag) 401 nerr_len = max_num_len(nerr_len, 402 IFA_STAT(oqdrops)); 403 if (bflag) { 404 nbyte_len = max_num_len(nbyte_len, 405 IFA_STAT(ibytes)); 406 nbyte_len = max_num_len(nbyte_len, 407 IFA_STAT(obytes)); 408 } 409 } 410 } 411 412 xo_open_list("interface"); 413 if (!pfunc) { 414 xo_emit("{T:/%-*.*s}", ifn_len_max, ifn_len_max, "Name"); 415 xo_emit(" {T:/%5.5s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} " 416 "{T:/%*.*s} {T:/%*.*s}", 417 "Mtu", net_len, net_len, "Network", addr_len, addr_len, 418 "Address", npkt_len, npkt_len, "Ipkts", 419 nerr_len, nerr_len, "Ierrs", nerr_len, nerr_len, "Idrop"); 420 if (bflag) 421 xo_emit(" {T:/%*.*s}", nbyte_len, nbyte_len, "Ibytes"); 422 xo_emit(" {T:/%*.*s} {T:/%*.*s}", npkt_len, npkt_len, "Opkts", 423 nerr_len, nerr_len, "Oerrs"); 424 if (bflag) 425 xo_emit(" {T:/%*.*s}", nbyte_len, nbyte_len, "Obytes"); 426 xo_emit(" {T:/%*s}", nerr_len, "Coll"); 427 if (dflag) 428 xo_emit(" {T:/%*.*s}", nerr_len, nerr_len, "Drop"); 429 xo_emit("\n"); 430 } 431 432 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 433 bool network = false, link = false; 434 char *name, *xname, buf[IFNAMSIZ+1]; 435 436 if (interface != NULL && strcmp(ifa->ifa_name, interface) != 0) 437 continue; 438 439 name = ifa->ifa_name; 440 441 if (pfunc) { 442 443 (*pfunc)(name); 444 445 /* 446 * Skip all ifaddrs belonging to same interface. 447 */ 448 while(ifa->ifa_next != NULL && 449 (strcmp(ifa->ifa_next->ifa_name, name) == 0)) { 450 ifa = ifa->ifa_next; 451 } 452 continue; 453 } 454 455 if (af != AF_UNSPEC && ifa->ifa_addr->sa_family != af) 456 continue; 457 458 xo_open_instance("interface"); 459 460 if ((ifa->ifa_flags & IFF_UP) == 0) { 461 xname = stpcpy(buf, name); 462 *xname++ = '*'; 463 *xname = '\0'; 464 xname = buf; 465 } else 466 xname = name; 467 468 xo_emit("{d:/%-*.*s}{etk:name}{eq:flags/0x%x}", 469 ifn_len_max, ifn_len_max, xname, name, ifa->ifa_flags); 470 471 #define IFA_MTU(ifa) (((struct if_data *)(ifa)->ifa_data)->ifi_mtu) 472 show_stat("lu", 6, "mtu", IFA_MTU(ifa), IFA_MTU(ifa), 0); 473 #undef IFA_MTU 474 475 process_ifa_addr(EMIT, ifa, &net_len, &addr_len, 476 &network, &link); 477 478 show_stat("lu", npkt_len, "received-packets", 479 IFA_STAT(ipackets), link|network, 1); 480 show_stat("lu", nerr_len, "received-errors", IFA_STAT(ierrors), 481 link, 1); 482 show_stat("lu", nerr_len, "dropped-packets", IFA_STAT(iqdrops), 483 link, 1); 484 if (bflag) 485 show_stat("lu", nbyte_len, "received-bytes", 486 IFA_STAT(ibytes), link|network, 0); 487 show_stat("lu", npkt_len, "sent-packets", IFA_STAT(opackets), 488 link|network, 1); 489 show_stat("lu", nerr_len, "send-errors", IFA_STAT(oerrors), 490 link, 1); 491 if (bflag) 492 show_stat("lu", nbyte_len, "sent-bytes", 493 IFA_STAT(obytes), link|network, 0); 494 show_stat("NRSlu", nerr_len, "collisions", IFA_STAT(collisions), 495 link, 1); 496 if (dflag) 497 show_stat("LSlu", nerr_len, "dropped-packets", 498 IFA_STAT(oqdrops), link, 1); 499 xo_emit("\n"); 500 501 if (!aflag) { 502 xo_close_instance("interface"); 503 continue; 504 } 505 506 /* 507 * Print family's multicast addresses. 508 */ 509 xo_open_list("multicast-address"); 510 for (ifma = next_ifma(ifmap, ifa->ifa_name, 511 ifa->ifa_addr->sa_family); 512 ifma != NULL; 513 ifma = next_ifma(ifma, ifa->ifa_name, 514 ifa->ifa_addr->sa_family)) { 515 const char *fmt = NULL; 516 517 xo_open_instance("multicast-address"); 518 switch (ifma->ifma_addr->sa_family) { 519 case AF_LINK: 520 { 521 struct sockaddr_dl *sdl; 522 523 sdl = (struct sockaddr_dl *)ifma->ifma_addr; 524 if (sdl->sdl_type != IFT_ETHER && 525 sdl->sdl_type != IFT_FDDI) 526 break; 527 } 528 /* FALLTHROUGH */ 529 case AF_INET: 530 #ifdef INET6 531 case AF_INET6: 532 #endif /* INET6 */ 533 fmt = routename(ifma->ifma_addr, numeric_addr); 534 break; 535 } 536 if (fmt) { 537 if (Wflag) 538 xo_emit("{P:/%27s }" 539 "{t:address/%-17s/}", "", fmt); 540 else 541 xo_emit("{P:/%25s }" 542 "{t:address/%-17.17s/}", "", fmt); 543 if (ifma->ifma_addr->sa_family == AF_LINK) { 544 xo_emit(" {:received-packets/%8lu}", 545 IFA_STAT(imcasts)); 546 xo_emit("{P:/%*s}", bflag? 17 : 6, ""); 547 xo_emit(" {:sent-packets/%8lu}", 548 IFA_STAT(omcasts)); 549 } 550 xo_emit("\n"); 551 } 552 xo_close_instance("multicast-address"); 553 ifma = ifma->ifma_next; 554 } 555 xo_close_list("multicast-address"); 556 xo_close_instance("interface"); 557 } 558 xo_close_list("interface"); 559 560 freeifaddrs(ifap); 561 if (aflag) 562 freeifmaddrs(ifmap); 563 } 564 565 struct iftot { 566 u_long ift_ip; /* input packets */ 567 u_long ift_ie; /* input errors */ 568 u_long ift_id; /* input drops */ 569 u_long ift_op; /* output packets */ 570 u_long ift_oe; /* output errors */ 571 u_long ift_od; /* output drops */ 572 u_long ift_co; /* collisions */ 573 u_long ift_ib; /* input bytes */ 574 u_long ift_ob; /* output bytes */ 575 }; 576 577 /* 578 * Obtain stats for interface(s). 579 */ 580 static void 581 fill_iftot(struct iftot *st) 582 { 583 struct ifaddrs *ifap, *ifa; 584 bool found = false; 585 586 if (getifaddrs(&ifap) != 0) 587 xo_err(EX_OSERR, "getifaddrs"); 588 589 bzero(st, sizeof(*st)); 590 591 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 592 if (ifa->ifa_addr->sa_family != AF_LINK) 593 continue; 594 if (interface) { 595 if (strcmp(ifa->ifa_name, interface) == 0) 596 found = true; 597 else 598 continue; 599 } 600 601 st->ift_ip += IFA_STAT(ipackets); 602 st->ift_ie += IFA_STAT(ierrors); 603 st->ift_id += IFA_STAT(iqdrops); 604 st->ift_ib += IFA_STAT(ibytes); 605 st->ift_op += IFA_STAT(opackets); 606 st->ift_oe += IFA_STAT(oerrors); 607 st->ift_od += IFA_STAT(oqdrops); 608 st->ift_ob += IFA_STAT(obytes); 609 st->ift_co += IFA_STAT(collisions); 610 } 611 612 if (interface && found == false) 613 xo_err(EX_DATAERR, "interface %s not found", interface); 614 615 freeifaddrs(ifap); 616 } 617 618 /* 619 * Set a flag to indicate that a signal from the periodic itimer has been 620 * caught. 621 */ 622 static sig_atomic_t signalled; 623 static void 624 catchalarm(int signo __unused) 625 { 626 signalled = true; 627 } 628 629 /* 630 * Print a running summary of interface statistics. 631 * Repeat display every interval seconds, showing statistics 632 * collected over that interval. Assumes that interval is non-zero. 633 * First line printed at top of screen is always cumulative. 634 */ 635 static void 636 sidewaysintpr(void) 637 { 638 struct iftot ift[2], *new, *old; 639 struct itimerval interval_it; 640 int oldmask, line; 641 642 new = &ift[0]; 643 old = &ift[1]; 644 fill_iftot(old); 645 646 (void)signal(SIGALRM, catchalarm); 647 signalled = false; 648 interval_it.it_interval.tv_sec = interval; 649 interval_it.it_interval.tv_usec = 0; 650 interval_it.it_value = interval_it.it_interval; 651 setitimer(ITIMER_REAL, &interval_it, NULL); 652 xo_open_list("interface-statistics"); 653 654 banner: 655 xo_emit("{T:/%17s} {T:/%14s} {T:/%16s}\n", "input", 656 interface != NULL ? interface : "(Total)", "output"); 657 xo_emit("{T:/%10s} {T:/%5s} {T:/%5s} {T:/%10s} {T:/%10s} {T:/%5s} " 658 "{T:/%10s} {T:/%5s}", 659 "packets", "errs", "idrops", "bytes", "packets", "errs", "bytes", 660 "colls"); 661 if (dflag) 662 xo_emit(" {T:/%5.5s}", "drops"); 663 xo_emit("\n"); 664 xo_flush(); 665 line = 0; 666 667 loop: 668 if ((noutputs != 0) && (--noutputs == 0)) { 669 xo_close_list("interface-statistics"); 670 return; 671 } 672 oldmask = sigblock(sigmask(SIGALRM)); 673 while (!signalled) 674 sigpause(0); 675 signalled = false; 676 sigsetmask(oldmask); 677 line++; 678 679 fill_iftot(new); 680 681 xo_open_instance("stats"); 682 show_stat("lu", 10, "received-packets", 683 new->ift_ip - old->ift_ip, 1, 1); 684 show_stat("lu", 5, "received-errors", 685 new->ift_ie - old->ift_ie, 1, 1); 686 show_stat("lu", 5, "dropped-packets", 687 new->ift_id - old->ift_id, 1, 1); 688 show_stat("lu", 10, "received-bytes", 689 new->ift_ib - old->ift_ib, 1, 0); 690 show_stat("lu", 10, "sent-packets", 691 new->ift_op - old->ift_op, 1, 1); 692 show_stat("lu", 5, "send-errors", 693 new->ift_oe - old->ift_oe, 1, 1); 694 show_stat("lu", 10, "sent-bytes", 695 new->ift_ob - old->ift_ob, 1, 0); 696 show_stat("NRSlu", 5, "collisions", 697 new->ift_co - old->ift_co, 1, 1); 698 if (dflag) 699 show_stat("LSlu", 5, "dropped-packets", 700 new->ift_od - old->ift_od, 1, 1); 701 xo_close_instance("stats"); 702 xo_emit("\n"); 703 xo_flush(); 704 705 if (new == &ift[0]) { 706 new = &ift[1]; 707 old = &ift[0]; 708 } else { 709 new = &ift[0]; 710 old = &ift[1]; 711 } 712 713 if (line == 21) 714 goto banner; 715 else 716 goto loop; 717 718 /* NOTREACHED */ 719 } 720