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