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