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