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