1 /*- 2 * Copyright (c) 1983, 1988, 1993, 1995 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[] = "@(#)inet.c 8.5 (Berkeley) 5/24/95"; 37 #endif /* not lint */ 38 #endif 39 40 #include <sys/cdefs.h> 41 __FBSDID("$FreeBSD$"); 42 43 #include <sys/param.h> 44 #include <sys/queue.h> 45 #include <sys/domain.h> 46 #include <sys/protosw.h> 47 #include <sys/socket.h> 48 #include <sys/socketvar.h> 49 #include <sys/sysctl.h> 50 51 #include <net/route.h> 52 #include <net/if_arp.h> 53 #include <netinet/in.h> 54 #include <netinet/in_systm.h> 55 #include <netinet/ip.h> 56 #include <netinet/ip_carp.h> 57 #ifdef INET6 58 #include <netinet/ip6.h> 59 #endif /* INET6 */ 60 #include <netinet/in_pcb.h> 61 #include <netinet/ip_icmp.h> 62 #include <netinet/icmp_var.h> 63 #include <netinet/igmp_var.h> 64 #include <netinet/ip_var.h> 65 #include <netinet/pim_var.h> 66 #include <netinet/tcp.h> 67 #include <netinet/tcpip.h> 68 #include <netinet/tcp_seq.h> 69 #define TCPSTATES 70 #include <netinet/tcp_fsm.h> 71 #include <netinet/tcp_timer.h> 72 #include <netinet/tcp_var.h> 73 #include <netinet/tcp_debug.h> 74 #include <netinet/udp.h> 75 #include <netinet/udp_var.h> 76 77 #include <arpa/inet.h> 78 #include <err.h> 79 #include <errno.h> 80 #include <libutil.h> 81 #include <netdb.h> 82 #include <stdint.h> 83 #include <stdio.h> 84 #include <stdlib.h> 85 #include <string.h> 86 #include <unistd.h> 87 #include "netstat.h" 88 89 char *inetname(struct in_addr *); 90 void inetprint(struct in_addr *, int, const char *, int); 91 #ifdef INET6 92 static int udp_done, tcp_done; 93 #endif /* INET6 */ 94 95 static int 96 pcblist_sysctl(int proto, char **bufp, int istcp) 97 { 98 const char *mibvar; 99 char *buf; 100 size_t len; 101 102 switch (proto) { 103 case IPPROTO_TCP: 104 mibvar = "net.inet.tcp.pcblist"; 105 break; 106 case IPPROTO_UDP: 107 mibvar = "net.inet.udp.pcblist"; 108 break; 109 case IPPROTO_DIVERT: 110 mibvar = "net.inet.divert.pcblist"; 111 break; 112 default: 113 mibvar = "net.inet.raw.pcblist"; 114 break; 115 } 116 117 len = 0; 118 if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) { 119 if (errno != ENOENT) 120 warn("sysctl: %s", mibvar); 121 return (0); 122 } 123 if ((buf = malloc(len)) == 0) { 124 warnx("malloc %lu bytes", (u_long)len); 125 return (0); 126 } 127 if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) { 128 warn("sysctl: %s", mibvar); 129 free(buf); 130 return (0); 131 } 132 *bufp = buf; 133 return (1); 134 } 135 136 /* 137 * Copied directly from uipc_socket2.c. We leave out some fields that are in 138 * nested structures that aren't used to avoid extra work. 139 */ 140 static void 141 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb) 142 { 143 xsb->sb_cc = sb->sb_cc; 144 xsb->sb_hiwat = sb->sb_hiwat; 145 xsb->sb_mbcnt = sb->sb_mbcnt; 146 xsb->sb_mcnt = sb->sb_mcnt; 147 xsb->sb_ccnt = sb->sb_ccnt; 148 xsb->sb_mbmax = sb->sb_mbmax; 149 xsb->sb_lowat = sb->sb_lowat; 150 xsb->sb_flags = sb->sb_flags; 151 xsb->sb_timeo = sb->sb_timeo; 152 } 153 154 int 155 sotoxsocket(struct socket *so, struct xsocket *xso) 156 { 157 struct protosw proto; 158 struct domain domain; 159 160 bzero(xso, sizeof *xso); 161 xso->xso_len = sizeof *xso; 162 xso->xso_so = so; 163 xso->so_type = so->so_type; 164 xso->so_options = so->so_options; 165 xso->so_linger = so->so_linger; 166 xso->so_state = so->so_state; 167 xso->so_pcb = so->so_pcb; 168 if (kread((uintptr_t)so->so_proto, &proto, sizeof(proto)) != 0) 169 return (-1); 170 xso->xso_protocol = proto.pr_protocol; 171 if (kread((uintptr_t)proto.pr_domain, &domain, sizeof(domain)) != 0) 172 return (-1); 173 xso->xso_family = domain.dom_family; 174 xso->so_qlen = so->so_qlen; 175 xso->so_incqlen = so->so_incqlen; 176 xso->so_qlimit = so->so_qlimit; 177 xso->so_timeo = so->so_timeo; 178 xso->so_error = so->so_error; 179 xso->so_oobmark = so->so_oobmark; 180 sbtoxsockbuf(&so->so_snd, &xso->so_snd); 181 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv); 182 return (0); 183 } 184 185 static int 186 pcblist_kvm(u_long off, char **bufp, int istcp) 187 { 188 struct inpcbinfo pcbinfo; 189 struct inpcbhead listhead; 190 struct inpcb *inp; 191 struct xinpcb xi; 192 struct xinpgen xig; 193 struct xtcpcb xt; 194 struct socket so; 195 struct xsocket *xso; 196 char *buf, *p; 197 size_t len; 198 199 if (off == 0) 200 return (0); 201 kread(off, &pcbinfo, sizeof(pcbinfo)); 202 if (istcp) 203 len = 2 * sizeof(xig) + 204 (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) * 205 sizeof(struct xtcpcb); 206 else 207 len = 2 * sizeof(xig) + 208 (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) * 209 sizeof(struct xinpcb); 210 if ((buf = malloc(len)) == 0) { 211 warnx("malloc %lu bytes", (u_long)len); 212 return (0); 213 } 214 p = buf; 215 216 #define COPYOUT(obj, size) do { \ 217 if (len < (size)) { \ 218 warnx("buffer size exceeded"); \ 219 goto fail; \ 220 } \ 221 bcopy((obj), p, (size)); \ 222 len -= (size); \ 223 p += (size); \ 224 } while (0) 225 226 #define KREAD(off, buf, len) do { \ 227 if (kread((uintptr_t)(off), (buf), (len)) != 0) \ 228 goto fail; \ 229 } while (0) 230 231 /* Write out header. */ 232 xig.xig_len = sizeof xig; 233 xig.xig_count = pcbinfo.ipi_count; 234 xig.xig_gen = pcbinfo.ipi_gencnt; 235 xig.xig_sogen = 0; 236 COPYOUT(&xig, sizeof xig); 237 238 /* Walk the PCB list. */ 239 xt.xt_len = sizeof xt; 240 xi.xi_len = sizeof xi; 241 if (istcp) 242 xso = &xt.xt_socket; 243 else 244 xso = &xi.xi_socket; 245 KREAD(pcbinfo.ipi_listhead, &listhead, sizeof(listhead)); 246 LIST_FOREACH(inp, &listhead, inp_list) { 247 if (istcp) { 248 KREAD(inp, &xt.xt_inp, sizeof(*inp)); 249 inp = &xt.xt_inp; 250 } else { 251 KREAD(inp, &xi.xi_inp, sizeof(*inp)); 252 inp = &xi.xi_inp; 253 } 254 255 if (inp->inp_gencnt > pcbinfo.ipi_gencnt) 256 continue; 257 258 if (istcp) { 259 if (inp->inp_ppcb == NULL) 260 bzero(&xt.xt_tp, sizeof xt.xt_tp); 261 else if (inp->inp_flags & INP_TIMEWAIT) { 262 bzero(&xt.xt_tp, sizeof xt.xt_tp); 263 xt.xt_tp.t_state = TCPS_TIME_WAIT; 264 } else 265 KREAD(inp->inp_ppcb, &xt.xt_tp, 266 sizeof xt.xt_tp); 267 } 268 if (inp->inp_socket) { 269 KREAD(inp->inp_socket, &so, sizeof(so)); 270 if (sotoxsocket(&so, xso) != 0) 271 goto fail; 272 } else { 273 bzero(xso, sizeof(*xso)); 274 if (istcp) 275 xso->xso_protocol = IPPROTO_TCP; 276 } 277 if (istcp) 278 COPYOUT(&xt, sizeof xt); 279 else 280 COPYOUT(&xi, sizeof xi); 281 } 282 283 /* Reread the pcbinfo and write out the footer. */ 284 kread(off, &pcbinfo, sizeof(pcbinfo)); 285 xig.xig_count = pcbinfo.ipi_count; 286 xig.xig_gen = pcbinfo.ipi_gencnt; 287 COPYOUT(&xig, sizeof xig); 288 289 *bufp = buf; 290 return (1); 291 292 fail: 293 free(buf); 294 return (0); 295 #undef COPYOUT 296 #undef KREAD 297 } 298 299 /* 300 * Print a summary of connections related to an Internet 301 * protocol. For TCP, also give state of connection. 302 * Listening processes (aflag) are suppressed unless the 303 * -a (all) flag is specified. 304 */ 305 void 306 protopr(u_long off, const char *name, int af1, int proto) 307 { 308 int istcp; 309 static int first = 1; 310 char *buf; 311 const char *vchar; 312 struct tcpcb *tp = NULL; 313 struct inpcb *inp; 314 struct xinpgen *xig, *oxig; 315 struct xsocket *so; 316 struct xtcp_timer *timer; 317 318 istcp = 0; 319 switch (proto) { 320 case IPPROTO_TCP: 321 #ifdef INET6 322 if (tcp_done != 0) 323 return; 324 else 325 tcp_done = 1; 326 #endif 327 istcp = 1; 328 break; 329 case IPPROTO_UDP: 330 #ifdef INET6 331 if (udp_done != 0) 332 return; 333 else 334 udp_done = 1; 335 #endif 336 break; 337 } 338 if (live) { 339 if (!pcblist_sysctl(proto, &buf, istcp)) 340 return; 341 } else { 342 if (!pcblist_kvm(off, &buf, istcp)) 343 return; 344 } 345 346 oxig = xig = (struct xinpgen *)buf; 347 for (xig = (struct xinpgen *)((char *)xig + xig->xig_len); 348 xig->xig_len > sizeof(struct xinpgen); 349 xig = (struct xinpgen *)((char *)xig + xig->xig_len)) { 350 if (istcp) { 351 timer = &((struct xtcpcb *)xig)->xt_timer; 352 tp = &((struct xtcpcb *)xig)->xt_tp; 353 inp = &((struct xtcpcb *)xig)->xt_inp; 354 so = &((struct xtcpcb *)xig)->xt_socket; 355 } else { 356 inp = &((struct xinpcb *)xig)->xi_inp; 357 so = &((struct xinpcb *)xig)->xi_socket; 358 timer = NULL; 359 } 360 361 /* Ignore sockets for protocols other than the desired one. */ 362 if (so->xso_protocol != proto) 363 continue; 364 365 /* Ignore PCBs which were freed during copyout. */ 366 if (inp->inp_gencnt > oxig->xig_gen) 367 continue; 368 369 if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0) 370 #ifdef INET6 371 || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0) 372 #endif /* INET6 */ 373 || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0 374 #ifdef INET6 375 && (inp->inp_vflag & INP_IPV6) == 0 376 #endif /* INET6 */ 377 )) 378 ) 379 continue; 380 if (!aflag && 381 ( 382 (istcp && tp->t_state == TCPS_LISTEN) 383 || (af1 == AF_INET && 384 inet_lnaof(inp->inp_laddr) == INADDR_ANY) 385 #ifdef INET6 386 || (af1 == AF_INET6 && 387 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 388 #endif /* INET6 */ 389 || (af1 == AF_UNSPEC && 390 (((inp->inp_vflag & INP_IPV4) != 0 && 391 inet_lnaof(inp->inp_laddr) == INADDR_ANY) 392 #ifdef INET6 393 || ((inp->inp_vflag & INP_IPV6) != 0 && 394 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 395 #endif 396 )) 397 )) 398 continue; 399 400 if (first) { 401 if (!Lflag) { 402 printf("Active Internet connections"); 403 if (aflag) 404 printf(" (including servers)"); 405 } else 406 printf( 407 "Current listen queue sizes (qlen/incqlen/maxqlen)"); 408 putchar('\n'); 409 if (Aflag) 410 printf("%-8.8s ", "Tcpcb"); 411 if (Lflag) 412 printf("%-5.5s %-14.14s %-22.22s\n", 413 "Proto", "Listen", "Local Address"); 414 else { 415 printf((Aflag && !Wflag) ? 416 "%-5.5s %-6.6s %-6.6s %-18.18s %-18.18s" : 417 "%-5.5s %-6.6s %-6.6s %-22.22s %-22.22s", 418 "Proto", "Recv-Q", "Send-Q", 419 "Local Address", "Foreign Address"); 420 if (xflag) { 421 printf("%-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s ", 422 "R-MBUF", "S-MBUF", "R-CLUS", 423 "S-CLUS", "R-HIWA", "S-HIWA", 424 "R-LOWA", "S-LOWA", "R-BCNT", 425 "S-BCNT", "R-BMAX", "S-BMAX"); 426 printf("%7.7s %7.7s %7.7s %7.7s %7.7s %7.7s %s\n", 427 "rexmt", "persist", "keep", 428 "2msl", "delack", "rcvtime", 429 "(state)"); 430 } else 431 printf("(state)\n"); 432 } 433 first = 0; 434 } 435 if (Lflag && so->so_qlimit == 0) 436 continue; 437 if (Aflag) { 438 if (istcp) 439 printf("%8lx ", (u_long)inp->inp_ppcb); 440 else 441 printf("%8lx ", (u_long)so->so_pcb); 442 } 443 #ifdef INET6 444 if ((inp->inp_vflag & INP_IPV6) != 0) 445 vchar = ((inp->inp_vflag & INP_IPV4) != 0) ? 446 "46" : "6 "; 447 else 448 #endif 449 vchar = ((inp->inp_vflag & INP_IPV4) != 0) ? 450 "4 " : " "; 451 printf("%-3.3s%-2.2s ", name, vchar); 452 if (Lflag) { 453 char buf1[15]; 454 455 snprintf(buf1, 15, "%d/%d/%d", so->so_qlen, 456 so->so_incqlen, so->so_qlimit); 457 printf("%-14.14s ", buf1); 458 } else { 459 printf("%6u %6u ", so->so_rcv.sb_cc, so->so_snd.sb_cc); 460 } 461 if (numeric_port) { 462 if (inp->inp_vflag & INP_IPV4) { 463 inetprint(&inp->inp_laddr, (int)inp->inp_lport, 464 name, 1); 465 if (!Lflag) 466 inetprint(&inp->inp_faddr, 467 (int)inp->inp_fport, name, 1); 468 } 469 #ifdef INET6 470 else if (inp->inp_vflag & INP_IPV6) { 471 inet6print(&inp->in6p_laddr, 472 (int)inp->inp_lport, name, 1); 473 if (!Lflag) 474 inet6print(&inp->in6p_faddr, 475 (int)inp->inp_fport, name, 1); 476 } /* else nothing printed now */ 477 #endif /* INET6 */ 478 } else if (inp->inp_flags & INP_ANONPORT) { 479 if (inp->inp_vflag & INP_IPV4) { 480 inetprint(&inp->inp_laddr, (int)inp->inp_lport, 481 name, 1); 482 if (!Lflag) 483 inetprint(&inp->inp_faddr, 484 (int)inp->inp_fport, name, 0); 485 } 486 #ifdef INET6 487 else if (inp->inp_vflag & INP_IPV6) { 488 inet6print(&inp->in6p_laddr, 489 (int)inp->inp_lport, name, 1); 490 if (!Lflag) 491 inet6print(&inp->in6p_faddr, 492 (int)inp->inp_fport, name, 0); 493 } /* else nothing printed now */ 494 #endif /* INET6 */ 495 } else { 496 if (inp->inp_vflag & INP_IPV4) { 497 inetprint(&inp->inp_laddr, (int)inp->inp_lport, 498 name, 0); 499 if (!Lflag) 500 inetprint(&inp->inp_faddr, 501 (int)inp->inp_fport, name, 502 inp->inp_lport != inp->inp_fport); 503 } 504 #ifdef INET6 505 else if (inp->inp_vflag & INP_IPV6) { 506 inet6print(&inp->in6p_laddr, 507 (int)inp->inp_lport, name, 0); 508 if (!Lflag) 509 inet6print(&inp->in6p_faddr, 510 (int)inp->inp_fport, name, 511 inp->inp_lport != inp->inp_fport); 512 } /* else nothing printed now */ 513 #endif /* INET6 */ 514 } 515 if (xflag) { 516 if (Lflag) 517 printf("%21s %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u ", 518 " ", 519 so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt, 520 so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt, 521 so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat, 522 so->so_rcv.sb_lowat, so->so_snd.sb_lowat, 523 so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt, 524 so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax); 525 else { 526 printf("%6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u ", 527 so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt, 528 so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt, 529 so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat, 530 so->so_rcv.sb_lowat, so->so_snd.sb_lowat, 531 so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt, 532 so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax); 533 if (timer != NULL) 534 printf("%4d.%02d %4d.%02d %4d.%02d %4d.%02d %4d.%02d %4d.%02d ", 535 timer->tt_rexmt / 1000, (timer->tt_rexmt % 1000) / 10, 536 timer->tt_persist / 1000, (timer->tt_persist % 1000) / 10, 537 timer->tt_keep / 1000, (timer->tt_keep % 1000) / 10, 538 timer->tt_2msl / 1000, (timer->tt_2msl % 1000) / 10, 539 timer->tt_delack / 1000, (timer->tt_delack % 1000) / 10, 540 timer->t_rcvtime / 1000, (timer->t_rcvtime % 1000) / 10); 541 } 542 } 543 if (istcp && !Lflag) { 544 if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES) 545 printf("%d", tp->t_state); 546 else { 547 printf("%s", tcpstates[tp->t_state]); 548 #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN) 549 /* Show T/TCP `hidden state' */ 550 if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) 551 putchar('*'); 552 #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */ 553 } 554 } 555 putchar('\n'); 556 } 557 if (xig != oxig && xig->xig_gen != oxig->xig_gen) { 558 if (oxig->xig_count > xig->xig_count) { 559 printf("Some %s sockets may have been deleted.\n", 560 name); 561 } else if (oxig->xig_count < xig->xig_count) { 562 printf("Some %s sockets may have been created.\n", 563 name); 564 } else { 565 printf( 566 "Some %s sockets may have been created or deleted.\n", 567 name); 568 } 569 } 570 free(buf); 571 } 572 573 /* 574 * Dump TCP statistics structure. 575 */ 576 void 577 tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) 578 { 579 struct tcpstat tcpstat, zerostat; 580 size_t len = sizeof tcpstat; 581 582 #ifdef INET6 583 if (tcp_done != 0) 584 return; 585 else 586 tcp_done = 1; 587 #endif 588 589 if (live) { 590 if (zflag) 591 memset(&zerostat, 0, len); 592 if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &len, 593 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { 594 warn("sysctl: net.inet.tcp.stats"); 595 return; 596 } 597 } else 598 kread(off, &tcpstat, len); 599 600 printf ("%s:\n", name); 601 602 #define p(f, m) if (tcpstat.f || sflag <= 1) \ 603 printf(m, tcpstat.f, plural(tcpstat.f)) 604 #define p1a(f, m) if (tcpstat.f || sflag <= 1) \ 605 printf(m, tcpstat.f) 606 #define p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \ 607 printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2)) 608 #define p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \ 609 printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2) 610 #define p3(f, m) if (tcpstat.f || sflag <= 1) \ 611 printf(m, tcpstat.f, pluralies(tcpstat.f)) 612 613 p(tcps_sndtotal, "\t%lu packet%s sent\n"); 614 p2(tcps_sndpack,tcps_sndbyte, "\t\t%lu data packet%s (%lu byte%s)\n"); 615 p2(tcps_sndrexmitpack, tcps_sndrexmitbyte, 616 "\t\t%lu data packet%s (%lu byte%s) retransmitted\n"); 617 p(tcps_sndrexmitbad, 618 "\t\t%lu data packet%s unnecessarily retransmitted\n"); 619 p(tcps_mturesent, "\t\t%lu resend%s initiated by MTU discovery\n"); 620 p2a(tcps_sndacks, tcps_delack, 621 "\t\t%lu ack-only packet%s (%lu delayed)\n"); 622 p(tcps_sndurg, "\t\t%lu URG only packet%s\n"); 623 p(tcps_sndprobe, "\t\t%lu window probe packet%s\n"); 624 p(tcps_sndwinup, "\t\t%lu window update packet%s\n"); 625 p(tcps_sndctrl, "\t\t%lu control packet%s\n"); 626 p(tcps_rcvtotal, "\t%lu packet%s received\n"); 627 p2(tcps_rcvackpack, tcps_rcvackbyte, 628 "\t\t%lu ack%s (for %lu byte%s)\n"); 629 p(tcps_rcvdupack, "\t\t%lu duplicate ack%s\n"); 630 p(tcps_rcvacktoomuch, "\t\t%lu ack%s for unsent data\n"); 631 p2(tcps_rcvpack, tcps_rcvbyte, 632 "\t\t%lu packet%s (%lu byte%s) received in-sequence\n"); 633 p2(tcps_rcvduppack, tcps_rcvdupbyte, 634 "\t\t%lu completely duplicate packet%s (%lu byte%s)\n"); 635 p(tcps_pawsdrop, "\t\t%lu old duplicate packet%s\n"); 636 p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte, 637 "\t\t%lu packet%s with some dup. data (%lu byte%s duped)\n"); 638 p2(tcps_rcvoopack, tcps_rcvoobyte, 639 "\t\t%lu out-of-order packet%s (%lu byte%s)\n"); 640 p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin, 641 "\t\t%lu packet%s (%lu byte%s) of data after window\n"); 642 p(tcps_rcvwinprobe, "\t\t%lu window probe%s\n"); 643 p(tcps_rcvwinupd, "\t\t%lu window update packet%s\n"); 644 p(tcps_rcvafterclose, "\t\t%lu packet%s received after close\n"); 645 p(tcps_rcvbadsum, "\t\t%lu discarded for bad checksum%s\n"); 646 p(tcps_rcvbadoff, "\t\t%lu discarded for bad header offset field%s\n"); 647 p1a(tcps_rcvshort, "\t\t%lu discarded because packet too short\n"); 648 p1a(tcps_rcvmemdrop, "\t\t%lu discarded due to memory problems\n"); 649 p(tcps_connattempt, "\t%lu connection request%s\n"); 650 p(tcps_accepts, "\t%lu connection accept%s\n"); 651 p(tcps_badsyn, "\t%lu bad connection attempt%s\n"); 652 p(tcps_listendrop, "\t%lu listen queue overflow%s\n"); 653 p(tcps_badrst, "\t%lu ignored RSTs in the window%s\n"); 654 p(tcps_connects, "\t%lu connection%s established (including accepts)\n"); 655 p2(tcps_closed, tcps_drops, 656 "\t%lu connection%s closed (including %lu drop%s)\n"); 657 p(tcps_cachedrtt, "\t\t%lu connection%s updated cached RTT on close\n"); 658 p(tcps_cachedrttvar, 659 "\t\t%lu connection%s updated cached RTT variance on close\n"); 660 p(tcps_cachedssthresh, 661 "\t\t%lu connection%s updated cached ssthresh on close\n"); 662 p(tcps_conndrops, "\t%lu embryonic connection%s dropped\n"); 663 p2(tcps_rttupdated, tcps_segstimed, 664 "\t%lu segment%s updated rtt (of %lu attempt%s)\n"); 665 p(tcps_rexmttimeo, "\t%lu retransmit timeout%s\n"); 666 p(tcps_timeoutdrop, "\t\t%lu connection%s dropped by rexmit timeout\n"); 667 p(tcps_persisttimeo, "\t%lu persist timeout%s\n"); 668 p(tcps_persistdrop, "\t\t%lu connection%s dropped by persist timeout\n"); 669 p(tcps_finwait2_drops, 670 "\t%lu Connection%s (fin_wait_2) dropped because of timeout\n"); 671 p(tcps_keeptimeo, "\t%lu keepalive timeout%s\n"); 672 p(tcps_keepprobe, "\t\t%lu keepalive probe%s sent\n"); 673 p(tcps_keepdrops, "\t\t%lu connection%s dropped by keepalive\n"); 674 p(tcps_predack, "\t%lu correct ACK header prediction%s\n"); 675 p(tcps_preddat, "\t%lu correct data packet header prediction%s\n"); 676 677 p3(tcps_sc_added, "\t%lu syncache entr%s added\n"); 678 p1a(tcps_sc_retransmitted, "\t\t%lu retransmitted\n"); 679 p1a(tcps_sc_dupsyn, "\t\t%lu dupsyn\n"); 680 p1a(tcps_sc_dropped, "\t\t%lu dropped\n"); 681 p1a(tcps_sc_completed, "\t\t%lu completed\n"); 682 p1a(tcps_sc_bucketoverflow, "\t\t%lu bucket overflow\n"); 683 p1a(tcps_sc_cacheoverflow, "\t\t%lu cache overflow\n"); 684 p1a(tcps_sc_reset, "\t\t%lu reset\n"); 685 p1a(tcps_sc_stale, "\t\t%lu stale\n"); 686 p1a(tcps_sc_aborted, "\t\t%lu aborted\n"); 687 p1a(tcps_sc_badack, "\t\t%lu badack\n"); 688 p1a(tcps_sc_unreach, "\t\t%lu unreach\n"); 689 p(tcps_sc_zonefail, "\t\t%lu zone failure%s\n"); 690 p(tcps_sc_sendcookie, "\t%lu cookie%s sent\n"); 691 p(tcps_sc_recvcookie, "\t%lu cookie%s received\n"); 692 693 p(tcps_sack_recovery_episode, "\t%lu SACK recovery episode%s\n"); 694 p(tcps_sack_rexmits, 695 "\t%lu segment rexmit%s in SACK recovery episodes\n"); 696 p(tcps_sack_rexmit_bytes, 697 "\t%lu byte rexmit%s in SACK recovery episodes\n"); 698 p(tcps_sack_rcv_blocks, 699 "\t%lu SACK option%s (SACK blocks) received\n"); 700 p(tcps_sack_send_blocks, "\t%lu SACK option%s (SACK blocks) sent\n"); 701 p1a(tcps_sack_sboverflow, "\t%lu SACK scoreboard overflow\n"); 702 703 p(tcps_ecn_ce, "\t%lu packet%s with ECN CE bit set\n"); 704 p(tcps_ecn_ect0, "\t%lu packet%s with ECN ECT(0) bit set\n"); 705 p(tcps_ecn_ect1, "\t%lu packet%s with ECN ECT(1) bit set\n"); 706 p(tcps_ecn_shs, "\t%lu successful ECN handshake%s\n"); 707 p(tcps_ecn_rcwnd, "\t%lu time%s ECN reduced the congestion window\n"); 708 #undef p 709 #undef p1a 710 #undef p2 711 #undef p2a 712 #undef p3 713 } 714 715 /* 716 * Dump UDP statistics structure. 717 */ 718 void 719 udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) 720 { 721 struct udpstat udpstat, zerostat; 722 size_t len = sizeof udpstat; 723 u_long delivered; 724 725 #ifdef INET6 726 if (udp_done != 0) 727 return; 728 else 729 udp_done = 1; 730 #endif 731 732 if (live) { 733 if (zflag) 734 memset(&zerostat, 0, len); 735 if (sysctlbyname("net.inet.udp.stats", &udpstat, &len, 736 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { 737 warn("sysctl: net.inet.udp.stats"); 738 return; 739 } 740 } else 741 kread(off, &udpstat, len); 742 743 printf("%s:\n", name); 744 #define p(f, m) if (udpstat.f || sflag <= 1) \ 745 printf(m, udpstat.f, plural(udpstat.f)) 746 #define p1a(f, m) if (udpstat.f || sflag <= 1) \ 747 printf(m, udpstat.f) 748 p(udps_ipackets, "\t%lu datagram%s received\n"); 749 p1a(udps_hdrops, "\t%lu with incomplete header\n"); 750 p1a(udps_badlen, "\t%lu with bad data length field\n"); 751 p1a(udps_badsum, "\t%lu with bad checksum\n"); 752 p1a(udps_nosum, "\t%lu with no checksum\n"); 753 p1a(udps_noport, "\t%lu dropped due to no socket\n"); 754 p(udps_noportbcast, 755 "\t%lu broadcast/multicast datagram%s undelivered\n"); 756 p1a(udps_fullsock, "\t%lu dropped due to full socket buffers\n"); 757 p1a(udpps_pcbhashmiss, "\t%lu not for hashed pcb\n"); 758 delivered = udpstat.udps_ipackets - 759 udpstat.udps_hdrops - 760 udpstat.udps_badlen - 761 udpstat.udps_badsum - 762 udpstat.udps_noport - 763 udpstat.udps_noportbcast - 764 udpstat.udps_fullsock; 765 if (delivered || sflag <= 1) 766 printf("\t%lu delivered\n", delivered); 767 p(udps_opackets, "\t%lu datagram%s output\n"); 768 /* the next statistic is cumulative in udps_noportbcast */ 769 p(udps_filtermcast, 770 "\t%lu time%s multicast source filter matched\n"); 771 #undef p 772 #undef p1a 773 } 774 775 /* 776 * Dump CARP statistics structure. 777 */ 778 void 779 carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) 780 { 781 struct carpstats carpstat, zerostat; 782 size_t len = sizeof(struct carpstats); 783 784 if (live) { 785 if (zflag) 786 memset(&zerostat, 0, len); 787 if (sysctlbyname("net.inet.carp.stats", &carpstat, &len, 788 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { 789 if (errno != ENOENT) 790 warn("sysctl: net.inet.carp.stats"); 791 return; 792 } 793 } else { 794 if (off == 0) 795 return; 796 kread(off, &carpstat, len); 797 } 798 799 printf("%s:\n", name); 800 801 #define p(f, m) if (carpstat.f || sflag <= 1) \ 802 printf(m, (uintmax_t)carpstat.f, plural(carpstat.f)) 803 #define p2(f, m) if (carpstat.f || sflag <= 1) \ 804 printf(m, (uintmax_t)carpstat.f) 805 806 p(carps_ipackets, "\t%ju packet%s received (IPv4)\n"); 807 p(carps_ipackets6, "\t%ju packet%s received (IPv6)\n"); 808 p(carps_badttl, "\t\t%ju packet%s discarded for wrong TTL\n"); 809 p(carps_hdrops, "\t\t%ju packet%s shorter than header\n"); 810 p(carps_badsum, "\t\t%ju discarded for bad checksum%s\n"); 811 p(carps_badver, "\t\t%ju discarded packet%s with a bad version\n"); 812 p2(carps_badlen, "\t\t%ju discarded because packet too short\n"); 813 p2(carps_badauth, "\t\t%ju discarded for bad authentication\n"); 814 p2(carps_badvhid, "\t\t%ju discarded for bad vhid\n"); 815 p2(carps_badaddrs, "\t\t%ju discarded because of a bad address list\n"); 816 p(carps_opackets, "\t%ju packet%s sent (IPv4)\n"); 817 p(carps_opackets6, "\t%ju packet%s sent (IPv6)\n"); 818 p2(carps_onomem, "\t\t%ju send failed due to mbuf memory error\n"); 819 #if notyet 820 p(carps_ostates, "\t\t%s state update%s sent\n"); 821 #endif 822 #undef p 823 #undef p2 824 } 825 826 /* 827 * Dump IP statistics structure. 828 */ 829 void 830 ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused) 831 { 832 struct ipstat ipstat, zerostat; 833 size_t len = sizeof ipstat; 834 835 if (live) { 836 if (zflag) 837 memset(&zerostat, 0, len); 838 if (sysctlbyname("net.inet.ip.stats", &ipstat, &len, 839 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { 840 warn("sysctl: net.inet.ip.stats"); 841 return; 842 } 843 } else 844 kread(off, &ipstat, len); 845 846 printf("%s:\n", name); 847 848 #define p(f, m) if (ipstat.f || sflag <= 1) \ 849 printf(m, ipstat.f, plural(ipstat.f)) 850 #define p1a(f, m) if (ipstat.f || sflag <= 1) \ 851 printf(m, ipstat.f) 852 853 p(ips_total, "\t%lu total packet%s received\n"); 854 p(ips_badsum, "\t%lu bad header checksum%s\n"); 855 p1a(ips_toosmall, "\t%lu with size smaller than minimum\n"); 856 p1a(ips_tooshort, "\t%lu with data size < data length\n"); 857 p1a(ips_toolong, "\t%lu with ip length > max ip packet size\n"); 858 p1a(ips_badhlen, "\t%lu with header length < data size\n"); 859 p1a(ips_badlen, "\t%lu with data length < header length\n"); 860 p1a(ips_badoptions, "\t%lu with bad options\n"); 861 p1a(ips_badvers, "\t%lu with incorrect version number\n"); 862 p(ips_fragments, "\t%lu fragment%s received\n"); 863 p(ips_fragdropped, "\t%lu fragment%s dropped (dup or out of space)\n"); 864 p(ips_fragtimeout, "\t%lu fragment%s dropped after timeout\n"); 865 p(ips_reassembled, "\t%lu packet%s reassembled ok\n"); 866 p(ips_delivered, "\t%lu packet%s for this host\n"); 867 p(ips_noproto, "\t%lu packet%s for unknown/unsupported protocol\n"); 868 p(ips_forward, "\t%lu packet%s forwarded"); 869 p(ips_fastforward, " (%lu packet%s fast forwarded)"); 870 if (ipstat.ips_forward || sflag <= 1) 871 putchar('\n'); 872 p(ips_cantforward, "\t%lu packet%s not forwardable\n"); 873 p(ips_notmember, 874 "\t%lu packet%s received for unknown multicast group\n"); 875 p(ips_redirectsent, "\t%lu redirect%s sent\n"); 876 p(ips_localout, "\t%lu packet%s sent from this host\n"); 877 p(ips_rawout, "\t%lu packet%s sent with fabricated ip header\n"); 878 p(ips_odropped, 879 "\t%lu output packet%s dropped due to no bufs, etc.\n"); 880 p(ips_noroute, "\t%lu output packet%s discarded due to no route\n"); 881 p(ips_fragmented, "\t%lu output datagram%s fragmented\n"); 882 p(ips_ofragments, "\t%lu fragment%s created\n"); 883 p(ips_cantfrag, "\t%lu datagram%s that can't be fragmented\n"); 884 p(ips_nogif, "\t%lu tunneling packet%s that can't find gif\n"); 885 p(ips_badaddr, "\t%lu datagram%s with bad address in header\n"); 886 #undef p 887 #undef p1a 888 } 889 890 /* 891 * Dump ARP statistics structure. 892 */ 893 void 894 arp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) 895 { 896 struct arpstat arpstat, zerostat; 897 size_t len = sizeof(arpstat); 898 899 if (live) { 900 if (zflag) 901 memset(&zerostat, 0, len); 902 if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len, 903 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { 904 warn("sysctl: net.link.ether.arp.stats"); 905 return; 906 } 907 } else 908 kread(off, &arpstat, len); 909 910 printf("%s:\n", name); 911 912 #define p(f, m) if (arpstat.f || sflag <= 1) \ 913 printf(m, arpstat.f, plural(arpstat.f)) 914 #define p2(f, m) if (arpstat.f || sflag <= 1) \ 915 printf(m, arpstat.f, pluralies(arpstat.f)) 916 917 p(txrequests, "\t%lu ARP request%s sent\n"); 918 p2(txreplies, "\t%lu ARP repl%s sent\n"); 919 p(rxrequests, "\t%lu ARP request%s received\n"); 920 p2(rxreplies, "\t%lu ARP repl%s received\n"); 921 p(received, "\t%lu ARP packet%s received\n"); 922 p(dropped, "\t%lu total packet%s dropped due to no ARP entry\n"); 923 p(timeouts, "\t%lu ARP entry%s timed out\n"); 924 p(dupips, "\t%lu Duplicate IP%s seen\n"); 925 #undef p 926 #undef p2 927 } 928 929 930 931 static const char *icmpnames[ICMP_MAXTYPE + 1] = { 932 "echo reply", /* RFC 792 */ 933 "#1", 934 "#2", 935 "destination unreachable", /* RFC 792 */ 936 "source quench", /* RFC 792 */ 937 "routing redirect", /* RFC 792 */ 938 "#6", 939 "#7", 940 "echo", /* RFC 792 */ 941 "router advertisement", /* RFC 1256 */ 942 "router solicitation", /* RFC 1256 */ 943 "time exceeded", /* RFC 792 */ 944 "parameter problem", /* RFC 792 */ 945 "time stamp", /* RFC 792 */ 946 "time stamp reply", /* RFC 792 */ 947 "information request", /* RFC 792 */ 948 "information request reply", /* RFC 792 */ 949 "address mask request", /* RFC 950 */ 950 "address mask reply", /* RFC 950 */ 951 "#19", 952 "#20", 953 "#21", 954 "#22", 955 "#23", 956 "#24", 957 "#25", 958 "#26", 959 "#27", 960 "#28", 961 "#29", 962 "icmp traceroute", /* RFC 1393 */ 963 "datagram conversion error", /* RFC 1475 */ 964 "mobile host redirect", 965 "IPv6 where-are-you", 966 "IPv6 i-am-here", 967 "mobile registration req", 968 "mobile registration reply", 969 "domain name request", /* RFC 1788 */ 970 "domain name reply", /* RFC 1788 */ 971 "icmp SKIP", 972 "icmp photuris", /* RFC 2521 */ 973 }; 974 975 /* 976 * Dump ICMP statistics. 977 */ 978 void 979 icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) 980 { 981 struct icmpstat icmpstat, zerostat; 982 int i, first; 983 size_t len; 984 985 len = sizeof icmpstat; 986 if (live) { 987 if (zflag) 988 memset(&zerostat, 0, len); 989 if (sysctlbyname("net.inet.icmp.stats", &icmpstat, &len, 990 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { 991 warn("sysctl: net.inet.icmp.stats"); 992 return; 993 } 994 } else 995 kread(off, &icmpstat, len); 996 997 printf("%s:\n", name); 998 999 #define p(f, m) if (icmpstat.f || sflag <= 1) \ 1000 printf(m, icmpstat.f, plural(icmpstat.f)) 1001 #define p1a(f, m) if (icmpstat.f || sflag <= 1) \ 1002 printf(m, icmpstat.f) 1003 #define p2(f, m) if (icmpstat.f || sflag <= 1) \ 1004 printf(m, icmpstat.f, plurales(icmpstat.f)) 1005 1006 p(icps_error, "\t%lu call%s to icmp_error\n"); 1007 p(icps_oldicmp, 1008 "\t%lu error%s not generated in response to an icmp message\n"); 1009 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) 1010 if (icmpstat.icps_outhist[i] != 0) { 1011 if (first) { 1012 printf("\tOutput histogram:\n"); 1013 first = 0; 1014 } 1015 if (icmpnames[i] != NULL) 1016 printf("\t\t%s: %lu\n", icmpnames[i], 1017 icmpstat.icps_outhist[i]); 1018 else 1019 printf("\t\tunknown ICMP #%d: %lu\n", i, 1020 icmpstat.icps_outhist[i]); 1021 } 1022 p(icps_badcode, "\t%lu message%s with bad code fields\n"); 1023 p(icps_tooshort, "\t%lu message%s less than the minimum length\n"); 1024 p(icps_checksum, "\t%lu message%s with bad checksum\n"); 1025 p(icps_badlen, "\t%lu message%s with bad length\n"); 1026 p1a(icps_bmcastecho, "\t%lu multicast echo requests ignored\n"); 1027 p1a(icps_bmcasttstamp, "\t%lu multicast timestamp requests ignored\n"); 1028 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) 1029 if (icmpstat.icps_inhist[i] != 0) { 1030 if (first) { 1031 printf("\tInput histogram:\n"); 1032 first = 0; 1033 } 1034 if (icmpnames[i] != NULL) 1035 printf("\t\t%s: %lu\n", icmpnames[i], 1036 icmpstat.icps_inhist[i]); 1037 else 1038 printf("\t\tunknown ICMP #%d: %lu\n", i, 1039 icmpstat.icps_inhist[i]); 1040 } 1041 p(icps_reflect, "\t%lu message response%s generated\n"); 1042 p2(icps_badaddr, "\t%lu invalid return address%s\n"); 1043 p(icps_noroute, "\t%lu no return route%s\n"); 1044 #undef p 1045 #undef p1a 1046 #undef p2 1047 if (live) { 1048 len = sizeof i; 1049 if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) < 1050 0) 1051 return; 1052 printf("\tICMP address mask responses are %sabled\n", 1053 i ? "en" : "dis"); 1054 } 1055 } 1056 1057 #ifndef BURN_BRIDGES 1058 /* 1059 * Dump IGMP statistics structure (pre 8.x kernel). 1060 */ 1061 static void 1062 igmp_stats_live_old(u_long off, const char *name) 1063 { 1064 struct oigmpstat oigmpstat, zerostat; 1065 size_t len = sizeof(oigmpstat); 1066 1067 if (zflag) 1068 memset(&zerostat, 0, len); 1069 if (sysctlbyname("net.inet.igmp.stats", &oigmpstat, &len, 1070 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { 1071 warn("sysctl: net.inet.igmp.stats"); 1072 return; 1073 } 1074 1075 printf("%s:\n", name); 1076 1077 #define p(f, m) if (oigmpstat.f || sflag <= 1) \ 1078 printf(m, oigmpstat.f, plural(oigmpstat.f)) 1079 #define py(f, m) if (oigmpstat.f || sflag <= 1) \ 1080 printf(m, oigmpstat.f, oigmpstat.f != 1 ? "ies" : "y") 1081 p(igps_rcv_total, "\t%u message%s received\n"); 1082 p(igps_rcv_tooshort, "\t%u message%s received with too few bytes\n"); 1083 p(igps_rcv_badsum, "\t%u message%s received with bad checksum\n"); 1084 py(igps_rcv_queries, "\t%u membership quer%s received\n"); 1085 py(igps_rcv_badqueries, 1086 "\t%u membership quer%s received with invalid field(s)\n"); 1087 p(igps_rcv_reports, "\t%u membership report%s received\n"); 1088 p(igps_rcv_badreports, 1089 "\t%u membership report%s received with invalid field(s)\n"); 1090 p(igps_rcv_ourreports, 1091 "\t%u membership report%s received for groups to which we belong\n"); 1092 p(igps_snd_reports, "\t%u membership report%s sent\n"); 1093 #undef p 1094 #undef py 1095 } 1096 #endif /* !BURN_BRIDGES */ 1097 1098 /* 1099 * Dump IGMP statistics structure. 1100 */ 1101 void 1102 igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) 1103 { 1104 struct igmpstat igmpstat, zerostat; 1105 size_t len; 1106 1107 #ifndef BURN_BRIDGES 1108 if (live) { 1109 /* 1110 * Detect if we are being run against a pre-IGMPv3 kernel. 1111 * We cannot do this for a core file as the legacy 1112 * struct igmpstat has no size field, nor does it 1113 * export it in any readily-available symbols. 1114 */ 1115 len = 0; 1116 if (sysctlbyname("net.inet.igmp.stats", NULL, &len, NULL, 1117 0) < 0) { 1118 warn("sysctl: net.inet.igmp.stats"); 1119 return; 1120 } 1121 if (len < sizeof(igmpstat)) { 1122 igmp_stats_live_old(off, name); 1123 return; 1124 } 1125 } 1126 #endif /* !BURN_BRIDGES */ 1127 1128 len = sizeof(igmpstat); 1129 if (live) { 1130 if (zflag) 1131 memset(&zerostat, 0, len); 1132 if (sysctlbyname("net.inet.igmp.stats", &igmpstat, &len, 1133 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { 1134 warn("sysctl: net.inet.igmp.stats"); 1135 return; 1136 } 1137 } else { 1138 len = sizeof(igmpstat); 1139 kread(off, &igmpstat, len); 1140 } 1141 1142 if (igmpstat.igps_version != IGPS_VERSION_3) { 1143 warnx("%s: version mismatch (%d != %d)", __func__, 1144 igmpstat.igps_version, IGPS_VERSION_3); 1145 } 1146 if (igmpstat.igps_len != IGPS_VERSION3_LEN) { 1147 warnx("%s: size mismatch (%d != %d)", __func__, 1148 igmpstat.igps_len, IGPS_VERSION3_LEN); 1149 } 1150 1151 printf("%s:\n", name); 1152 1153 #define p64(f, m) if (igmpstat.f || sflag <= 1) \ 1154 printf(m, (uintmax_t) igmpstat.f, plural(igmpstat.f)) 1155 #define py64(f, m) if (igmpstat.f || sflag <= 1) \ 1156 printf(m, (uintmax_t) igmpstat.f, pluralies(igmpstat.f)) 1157 p64(igps_rcv_total, "\t%ju message%s received\n"); 1158 p64(igps_rcv_tooshort, "\t%ju message%s received with too few bytes\n"); 1159 p64(igps_rcv_badttl, "\t%ju message%s received with wrong TTL\n"); 1160 p64(igps_rcv_badsum, "\t%ju message%s received with bad checksum\n"); 1161 py64(igps_rcv_v1v2_queries, "\t%ju V1/V2 membership quer%s received\n"); 1162 py64(igps_rcv_v3_queries, "\t%ju V3 membership quer%s received\n"); 1163 py64(igps_rcv_badqueries, 1164 "\t%ju membership quer%s received with invalid field(s)\n"); 1165 py64(igps_rcv_gen_queries, "\t%ju general quer%s received\n"); 1166 py64(igps_rcv_group_queries, "\t%ju group quer%s received\n"); 1167 py64(igps_rcv_gsr_queries, "\t%ju group-source quer%s received\n"); 1168 py64(igps_drop_gsr_queries, "\t%ju group-source quer%s dropped\n"); 1169 p64(igps_rcv_reports, "\t%ju membership report%s received\n"); 1170 p64(igps_rcv_badreports, 1171 "\t%ju membership report%s received with invalid field(s)\n"); 1172 p64(igps_rcv_ourreports, 1173 "\t%ju membership report%s received for groups to which we belong\n"); 1174 p64(igps_rcv_nora, "\t%ju V3 report%s received without Router Alert\n"); 1175 p64(igps_snd_reports, "\t%ju membership report%s sent\n"); 1176 #undef p64 1177 #undef py64 1178 } 1179 1180 /* 1181 * Dump PIM statistics structure. 1182 */ 1183 void 1184 pim_stats(u_long off __unused, const char *name, int af1 __unused, 1185 int proto __unused) 1186 { 1187 struct pimstat pimstat, zerostat; 1188 size_t len = sizeof pimstat; 1189 1190 if (live) { 1191 if (zflag) 1192 memset(&zerostat, 0, len); 1193 if (sysctlbyname("net.inet.pim.stats", &pimstat, &len, 1194 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { 1195 if (errno != ENOENT) 1196 warn("sysctl: net.inet.pim.stats"); 1197 return; 1198 } 1199 } else { 1200 if (off == 0) 1201 return; 1202 kread(off, &pimstat, len); 1203 } 1204 1205 printf("%s:\n", name); 1206 1207 #define p(f, m) if (pimstat.f || sflag <= 1) \ 1208 printf(m, (uintmax_t)pimstat.f, plural(pimstat.f)) 1209 #define py(f, m) if (pimstat.f || sflag <= 1) \ 1210 printf(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y") 1211 p(pims_rcv_total_msgs, "\t%ju message%s received\n"); 1212 p(pims_rcv_total_bytes, "\t%ju byte%s received\n"); 1213 p(pims_rcv_tooshort, "\t%ju message%s received with too few bytes\n"); 1214 p(pims_rcv_badsum, "\t%ju message%s received with bad checksum\n"); 1215 p(pims_rcv_badversion, "\t%ju message%s received with bad version\n"); 1216 p(pims_rcv_registers_msgs, "\t%ju data register message%s received\n"); 1217 p(pims_rcv_registers_bytes, "\t%ju data register byte%s received\n"); 1218 p(pims_rcv_registers_wrongiif, 1219 "\t%ju data register message%s received on wrong iif\n"); 1220 p(pims_rcv_badregisters, "\t%ju bad register%s received\n"); 1221 p(pims_snd_registers_msgs, "\t%ju data register message%s sent\n"); 1222 p(pims_snd_registers_bytes, "\t%ju data register byte%s sent\n"); 1223 #undef p 1224 #undef py 1225 } 1226 1227 /* 1228 * Pretty print an Internet address (net address + port). 1229 */ 1230 void 1231 inetprint(struct in_addr *in, int port, const char *proto, int num_port) 1232 { 1233 struct servent *sp = 0; 1234 char line[80], *cp; 1235 int width; 1236 1237 if (Wflag) 1238 sprintf(line, "%s.", inetname(in)); 1239 else 1240 sprintf(line, "%.*s.", (Aflag && !num_port) ? 12 : 16, inetname(in)); 1241 cp = index(line, '\0'); 1242 if (!num_port && port) 1243 sp = getservbyport((int)port, proto); 1244 if (sp || port == 0) 1245 sprintf(cp, "%.15s ", sp ? sp->s_name : "*"); 1246 else 1247 sprintf(cp, "%d ", ntohs((u_short)port)); 1248 width = (Aflag && !Wflag) ? 18 : 22; 1249 if (Wflag) 1250 printf("%-*s ", width, line); 1251 else 1252 printf("%-*.*s ", width, width, line); 1253 } 1254 1255 /* 1256 * Construct an Internet address representation. 1257 * If numeric_addr has been supplied, give 1258 * numeric value, otherwise try for symbolic name. 1259 */ 1260 char * 1261 inetname(struct in_addr *inp) 1262 { 1263 char *cp; 1264 static char line[MAXHOSTNAMELEN]; 1265 struct hostent *hp; 1266 struct netent *np; 1267 1268 cp = 0; 1269 if (!numeric_addr && inp->s_addr != INADDR_ANY) { 1270 int net = inet_netof(*inp); 1271 int lna = inet_lnaof(*inp); 1272 1273 if (lna == INADDR_ANY) { 1274 np = getnetbyaddr(net, AF_INET); 1275 if (np) 1276 cp = np->n_name; 1277 } 1278 if (cp == 0) { 1279 hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET); 1280 if (hp) { 1281 cp = hp->h_name; 1282 trimdomain(cp, strlen(cp)); 1283 } 1284 } 1285 } 1286 if (inp->s_addr == INADDR_ANY) 1287 strcpy(line, "*"); 1288 else if (cp) { 1289 strlcpy(line, cp, sizeof(line)); 1290 } else { 1291 inp->s_addr = ntohl(inp->s_addr); 1292 #define C(x) ((u_int)((x) & 0xff)) 1293 sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24), 1294 C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr)); 1295 } 1296 return (line); 1297 } 1298