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