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