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