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