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