2 * Copyright (c) 1983, 1988, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 static char sccsid[] = "@(#)inet.c 8.5 (Berkeley) 5/24/95";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
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>
45 #include <sys/socketvar.h>
46 #include <sys/sysctl.h>
48 #include <net/route.h>
49 #include <net/if_arp.h>
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53 #include <netinet/ip_carp.h>
55 #include <netinet/ip6.h>
57 #include <netinet/in_pcb.h>
58 #include <netinet/ip_icmp.h>
59 #include <netinet/icmp_var.h>
60 #include <netinet/igmp_var.h>
61 #include <netinet/ip_var.h>
62 #include <netinet/pim_var.h>
63 #include <netinet/tcp.h>
64 #include <netinet/tcpip.h>
65 #include <netinet/tcp_seq.h>
67 #include <netinet/tcp_fsm.h>
68 #include <netinet/tcp_timer.h>
69 #include <netinet/tcp_var.h>
70 #include <netinet/udp.h>
71 #include <netinet/udp_var.h>
73 #include <arpa/inet.h>
88 void inetprint(const char *, struct in_addr *, int, const char *, int,
91 static int udp_done, tcp_done, sdp_done;
95 pcblist_sysctl(int proto, const char *name, char **bufp)
103 mibvar = "net.inet.tcp.pcblist";
106 mibvar = "net.inet.udp.pcblist";
109 mibvar = "net.inet.divert.pcblist";
112 mibvar = "net.inet.raw.pcblist";
115 if (strncmp(name, "sdp", 3) == 0)
116 mibvar = "net.inet.sdp.pcblist";
118 if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
120 xo_warn("sysctl: %s", mibvar);
123 if ((buf = malloc(len)) == NULL) {
124 xo_warnx("malloc %lu bytes", (u_long)len);
127 if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
128 xo_warn("sysctl: %s", mibvar);
137 * Copied directly from uipc_socket2.c. We leave out some fields that are in
138 * nested structures that aren't used to avoid extra work.
141 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
143 xsb->sb_cc = sb->sb_ccc;
144 xsb->sb_hiwat = sb->sb_hiwat;
145 xsb->sb_mbcnt = sb->sb_mbcnt;
146 xsb->sb_mcnt = sb->sb_mcnt;
147 xsb->sb_ccnt = sb->sb_ccnt;
148 xsb->sb_mbmax = sb->sb_mbmax;
149 xsb->sb_lowat = sb->sb_lowat;
150 xsb->sb_flags = sb->sb_flags;
151 xsb->sb_timeo = sb->sb_timeo;
155 sotoxsocket(struct socket *so, struct xsocket *xso)
157 struct protosw proto;
158 struct domain domain;
160 bzero(xso, sizeof *xso);
161 xso->xso_len = sizeof *xso;
163 xso->so_type = so->so_type;
164 xso->so_options = so->so_options;
165 xso->so_linger = so->so_linger;
166 xso->so_state = so->so_state;
167 xso->so_pcb = so->so_pcb;
168 if (kread((uintptr_t)so->so_proto, &proto, sizeof(proto)) != 0)
170 xso->xso_protocol = proto.pr_protocol;
171 if (kread((uintptr_t)proto.pr_domain, &domain, sizeof(domain)) != 0)
173 xso->xso_family = domain.dom_family;
174 xso->so_timeo = so->so_timeo;
175 xso->so_error = so->so_error;
176 if ((so->so_options & SO_ACCEPTCONN) != 0) {
177 xso->so_qlen = so->sol_qlen;
178 xso->so_incqlen = so->sol_incqlen;
179 xso->so_qlimit = so->sol_qlimit;
181 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
182 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
183 xso->so_oobmark = so->so_oobmark;
189 * Print a summary of connections related to an Internet
190 * protocol. For TCP, also give state of connection.
191 * Listening processes (aflag) are suppressed unless the
192 * -a (all) flag is specified.
195 protopr(u_long off, const char *name, int af1, int proto)
197 static int first = 1;
203 struct xinpgen *xig, *oxig;
210 if (strncmp(name, "sdp", 3) != 0) {
234 if (!pcblist_sysctl(proto, name, &buf))
237 oxig = xig = (struct xinpgen *)buf;
238 for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
239 xig->xig_len > sizeof(struct xinpgen);
240 xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
242 tp = (struct xtcpcb *)xig;
245 inp = (struct xinpcb *)xig;
247 so = &inp->xi_socket;
249 /* Ignore sockets for protocols other than the desired one. */
250 if (so->xso_protocol != proto)
253 /* Ignore PCBs which were freed during copyout. */
254 if (inp->inp_gencnt > oxig->xig_gen)
257 if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
259 || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
261 || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
263 && (inp->inp_vflag & INP_IPV6) == 0
270 (istcp && tp->t_state == TCPS_LISTEN)
271 || (af1 == AF_INET &&
272 inet_lnaof(inp->inp_laddr) == INADDR_ANY)
274 || (af1 == AF_INET6 &&
275 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
277 || (af1 == AF_UNSPEC &&
278 (((inp->inp_vflag & INP_IPV4) != 0 &&
279 inet_lnaof(inp->inp_laddr) == INADDR_ANY)
281 || ((inp->inp_vflag & INP_IPV6) != 0 &&
282 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
290 xo_emit("Active Internet connections");
292 xo_emit(" (including servers)");
295 "Current listen queue sizes (qlen/incqlen/maxqlen)");
298 xo_emit("{T:/%-*s} ", 2 * (int)sizeof(void *),
301 xo_emit((Aflag && !Wflag) ?
302 "{T:/%-5.5s} {T:/%-32.32s} {T:/%-18.18s}" :
303 ((!Wflag || af1 == AF_INET) ?
304 "{T:/%-5.5s} {T:/%-32.32s} {T:/%-22.22s}" :
305 "{T:/%-5.5s} {T:/%-32.32s} {T:/%-45.45s}"),
306 "Proto", "Listen", "Local Address");
308 xo_emit((Aflag && !Wflag) ?
309 "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%s}" :
310 ((!Wflag || af1 == AF_INET) ?
311 "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%s}" :
312 "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%s}"),
313 "Proto", "Rexmit", "OOORcv", "0-win",
314 "Local Address", "Foreign Address");
316 xo_emit((Aflag && !Wflag) ?
317 "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%-18.18s}" :
318 ((!Wflag || af1 == AF_INET) ?
319 "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%-22.22s}" :
320 "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%-45.45s}"),
321 "Proto", "Recv-Q", "Send-Q",
322 "Local Address", "Foreign Address");
323 if (!xflag && !Rflag)
327 xo_emit(" {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
328 "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
329 "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
330 "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s}",
331 "R-MBUF", "S-MBUF", "R-CLUS", "S-CLUS",
332 "R-HIWA", "S-HIWA", "R-LOWA", "S-LOWA",
333 "R-BCNT", "S-BCNT", "R-BMAX", "S-BMAX");
334 xo_emit(" {T:/%7.7s} {T:/%7.7s} {T:/%7.7s} "
335 "{T:/%7.7s} {T:/%7.7s} {T:/%7.7s}",
336 "rexmt", "persist", "keep", "2msl",
337 "delack", "rcvtime");
339 xo_emit(" {T:/%8.8s} {T:/%5.5s}",
345 if (Lflag && so->so_qlimit == 0)
347 xo_open_instance("socket");
350 xo_emit("{q:address/%*lx} ",
351 2 * (int)sizeof(void *),
352 (u_long)inp->inp_ppcb);
354 xo_emit("{q:address/%*lx} ",
355 2 * (int)sizeof(void *),
359 if ((inp->inp_vflag & INP_IPV6) != 0)
360 vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
364 vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
366 if (istcp && (tp->t_flags & TF_TOE) != 0)
367 xo_emit("{:protocol/%-3.3s%-2.2s/%s%s} ", "toe", vchar);
369 xo_emit("{:protocol/%-3.3s%-2.2s/%s%s} ", name, vchar);
373 snprintf(buf1, sizeof buf1, "%u/%u/%u", so->so_qlen,
374 so->so_incqlen, so->so_qlimit);
375 xo_emit("{:listen-queue-sizes/%-32.32s} ", buf1);
378 xo_emit("{:sent-retransmit-packets/%6u} "
379 "{:received-out-of-order-packets/%6u} "
380 "{:sent-zero-window/%6u} ",
381 tp->t_sndrexmitpack, tp->t_rcvoopack,
384 xo_emit("{P:/%21s}", "");
386 xo_emit("{:receive-bytes-waiting/%6u} "
387 "{:send-bytes-waiting/%6u} ",
388 so->so_rcv.sb_cc, so->so_snd.sb_cc);
391 if (inp->inp_vflag & INP_IPV4) {
392 inetprint("local", &inp->inp_laddr,
393 (int)inp->inp_lport, name, 1, af1);
395 inetprint("remote", &inp->inp_faddr,
396 (int)inp->inp_fport, name, 1, af1);
399 else if (inp->inp_vflag & INP_IPV6) {
400 inet6print("local", &inp->in6p_laddr,
401 (int)inp->inp_lport, name, 1);
403 inet6print("remote", &inp->in6p_faddr,
404 (int)inp->inp_fport, name, 1);
405 } /* else nothing printed now */
407 } else if (inp->inp_flags & INP_ANONPORT) {
408 if (inp->inp_vflag & INP_IPV4) {
409 inetprint("local", &inp->inp_laddr,
410 (int)inp->inp_lport, name, 1, af1);
412 inetprint("remote", &inp->inp_faddr,
413 (int)inp->inp_fport, name, 0, af1);
416 else if (inp->inp_vflag & INP_IPV6) {
417 inet6print("local", &inp->in6p_laddr,
418 (int)inp->inp_lport, name, 1);
420 inet6print("remote", &inp->in6p_faddr,
421 (int)inp->inp_fport, name, 0);
422 } /* else nothing printed now */
425 if (inp->inp_vflag & INP_IPV4) {
426 inetprint("local", &inp->inp_laddr,
427 (int)inp->inp_lport, name, 0, af1);
429 inetprint("remote", &inp->inp_faddr,
430 (int)inp->inp_fport, name,
431 inp->inp_lport != inp->inp_fport,
435 else if (inp->inp_vflag & INP_IPV6) {
436 inet6print("local", &inp->in6p_laddr,
437 (int)inp->inp_lport, name, 0);
439 inet6print("remote", &inp->in6p_faddr,
440 (int)inp->inp_fport, name,
441 inp->inp_lport != inp->inp_fport);
442 } /* else nothing printed now */
446 xo_emit("{:receive-mbufs/%6u} {:send-mbufs/%6u} "
447 "{:receive-clusters/%6u} {:send-clusters/%6u} "
448 "{:receive-high-water/%6u} {:send-high-water/%6u} "
449 "{:receive-low-water/%6u} {:send-low-water/%6u} "
450 "{:receive-mbuf-bytes/%6u} {:send-mbuf-bytes/%6u} "
451 "{:receive-mbuf-bytes-max/%6u} "
452 "{:send-mbuf-bytes-max/%6u}",
453 so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt,
454 so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt,
455 so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
456 so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
457 so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
458 so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
460 xo_emit(" {:retransmit-timer/%4d.%02d} "
461 "{:persist-timer/%4d.%02d} "
462 "{:keepalive-timer/%4d.%02d} "
463 "{:msl2-timer/%4d.%02d} "
464 "{:delay-ack-timer/%4d.%02d} "
465 "{:inactivity-timer/%4d.%02d}",
467 (tp->tt_rexmt % 1000) / 10,
468 tp->tt_persist / 1000,
469 (tp->tt_persist % 1000) / 10,
471 (tp->tt_keep % 1000) / 10,
473 (tp->tt_2msl % 1000) / 10,
474 tp->tt_delack / 1000,
475 (tp->tt_delack % 1000) / 10,
476 tp->t_rcvtime / 1000,
477 (tp->t_rcvtime % 1000) / 10);
479 if (istcp && !Lflag && !xflag && !Tflag && !Rflag) {
480 if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
481 xo_emit("{:tcp-state/%d}", tp->t_state);
483 xo_emit("{:tcp-state/%s}",
484 tcpstates[tp->t_state]);
485 #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
486 /* Show T/TCP `hidden state' */
487 if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
488 xo_emit("{:need-syn-or-fin/*}");
489 #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
493 /* XXX: is this right Alfred */
494 xo_emit(" {:flow-id/%08x} {:flow-type/%5d}",
499 xo_close_instance("socket");
501 if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
502 if (oxig->xig_count > xig->xig_count) {
503 xo_emit("Some {d:lost/%s} sockets may have been "
505 } else if (oxig->xig_count < xig->xig_count) {
506 xo_emit("Some {d:created/%s} sockets may have been "
509 xo_emit("Some {d:changed/%s} sockets may have been "
510 "created or deleted.\n", name);
517 * Dump TCP statistics structure.
520 tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
522 struct tcpstat tcpstat;
523 uint64_t tcps_states[TCP_NSTATES];
532 if (fetch_stats("net.inet.tcp.stats", off, &tcpstat,
533 sizeof(tcpstat), kread_counters) != 0)
536 if (fetch_stats_ro("net.inet.tcp.states", nl[N_TCPS_STATES].n_value,
537 &tcps_states, sizeof(tcps_states), kread_counters) != 0)
540 xo_open_container("tcp");
541 xo_emit("{T:/%s}:\n", name);
543 #define p(f, m) if (tcpstat.f || sflag <= 1) \
544 xo_emit(m, (uintmax_t )tcpstat.f, plural(tcpstat.f))
545 #define p1a(f, m) if (tcpstat.f || sflag <= 1) \
546 xo_emit(m, (uintmax_t )tcpstat.f)
547 #define p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
548 xo_emit(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1), \
549 (uintmax_t )tcpstat.f2, plural(tcpstat.f2))
550 #define p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
551 xo_emit(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1), \
552 (uintmax_t )tcpstat.f2)
553 #define p3(f, m) if (tcpstat.f || sflag <= 1) \
554 xo_emit(m, (uintmax_t )tcpstat.f, pluralies(tcpstat.f))
556 p(tcps_sndtotal, "\t{:sent-packets/%ju} {N:/packet%s sent}\n");
557 p2(tcps_sndpack,tcps_sndbyte, "\t\t{:sent-data-packets/%ju} "
558 "{N:/data packet%s} ({:sent-data-bytes/%ju} {N:/byte%s})\n");
559 p2(tcps_sndrexmitpack, tcps_sndrexmitbyte, "\t\t"
560 "{:sent-retransmitted-packets/%ju} {N:/data packet%s} "
561 "({:sent-retransmitted-bytes/%ju} {N:/byte%s}) "
562 "{N:retransmitted}\n");
563 p(tcps_sndrexmitbad, "\t\t"
564 "{:sent-unnecessary-retransmitted-packets/%ju} "
565 "{N:/data packet%s unnecessarily retransmitted}\n");
566 p(tcps_mturesent, "\t\t{:sent-resends-by-mtu-discovery/%ju} "
567 "{N:/resend%s initiated by MTU discovery}\n");
568 p2a(tcps_sndacks, tcps_delack, "\t\t{:sent-ack-only-packets/%ju} "
569 "{N:/ack-only packet%s/} ({:sent-packets-delayed/%ju} "
571 p(tcps_sndurg, "\t\t{:sent-urg-only-packets/%ju} "
572 "{N:/URG only packet%s}\n");
573 p(tcps_sndprobe, "\t\t{:sent-window-probe-packets/%ju} "
574 "{N:/window probe packet%s}\n");
575 p(tcps_sndwinup, "\t\t{:sent-window-update-packets/%ju} "
576 "{N:/window update packet%s}\n");
577 p(tcps_sndctrl, "\t\t{:sent-control-packets/%ju} "
578 "{N:/control packet%s}\n");
579 p(tcps_rcvtotal, "\t{:received-packets/%ju} "
580 "{N:/packet%s received}\n");
581 p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t"
582 "{:received-ack-packets/%ju} {N:/ack%s} "
583 "{N:(for} {:received-ack-bytes/%ju} {N:/byte%s})\n");
584 p(tcps_rcvdupack, "\t\t{:received-duplicate-acks/%ju} "
585 "{N:/duplicate ack%s}\n");
586 p(tcps_rcvacktoomuch, "\t\t{:received-acks-for-unsent-data/%ju} "
587 "{N:/ack%s for unsent data}\n");
588 p2(tcps_rcvpack, tcps_rcvbyte, "\t\t"
589 "{:received-in-sequence-packets/%ju} {N:/packet%s} "
590 "({:received-in-sequence-bytes/%ju} {N:/byte%s}) "
591 "{N:received in-sequence}\n");
592 p2(tcps_rcvduppack, tcps_rcvdupbyte, "\t\t"
593 "{:received-completely-duplicate-packets/%ju} "
594 "{N:/completely duplicate packet%s} "
595 "({:received-completely-duplicate-bytes/%ju} {N:/byte%s})\n");
596 p(tcps_pawsdrop, "\t\t{:received-old-duplicate-packets/%ju} "
597 "{N:/old duplicate packet%s}\n");
598 p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte, "\t\t"
599 "{:received-some-duplicate-packets/%ju} "
600 "{N:/packet%s with some dup. data} "
601 "({:received-some-duplicate-bytes/%ju} {N:/byte%s duped/})\n");
602 p2(tcps_rcvoopack, tcps_rcvoobyte, "\t\t{:received-out-of-order/%ju} "
603 "{N:/out-of-order packet%s} "
604 "({:received-out-of-order-bytes/%ju} {N:/byte%s})\n");
605 p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin, "\t\t"
606 "{:received-after-window-packets/%ju} {N:/packet%s} "
607 "({:received-after-window-bytes/%ju} {N:/byte%s}) "
608 "{N:of data after window}\n");
609 p(tcps_rcvwinprobe, "\t\t{:received-window-probes/%ju} "
610 "{N:/window probe%s}\n");
611 p(tcps_rcvwinupd, "\t\t{:receive-window-update-packets/%ju} "
612 "{N:/window update packet%s}\n");
613 p(tcps_rcvafterclose, "\t\t{:received-after-close-packets/%ju} "
614 "{N:/packet%s received after close}\n");
615 p(tcps_rcvbadsum, "\t\t{:discard-bad-checksum/%ju} "
616 "{N:/discarded for bad checksum%s}\n");
617 p(tcps_rcvbadoff, "\t\t{:discard-bad-header-offset/%ju} "
618 "{N:/discarded for bad header offset field%s}\n");
619 p1a(tcps_rcvshort, "\t\t{:discard-too-short/%ju} "
620 "{N:discarded because packet too short}\n");
621 p1a(tcps_rcvmemdrop, "\t\t{:discard-memory-problems/%ju} "
622 "{N:discarded due to memory problems}\n");
623 p(tcps_connattempt, "\t{:connection-requests/%ju} "
624 "{N:/connection request%s}\n");
625 p(tcps_accepts, "\t{:connections-accepts/%ju} "
626 "{N:/connection accept%s}\n");
627 p(tcps_badsyn, "\t{:bad-connection-attempts/%ju} "
628 "{N:/bad connection attempt%s}\n");
629 p(tcps_listendrop, "\t{:listen-queue-overflows/%ju} "
630 "{N:/listen queue overflow%s}\n");
631 p(tcps_badrst, "\t{:ignored-in-window-resets/%ju} "
632 "{N:/ignored RSTs in the window%s}\n");
633 p(tcps_connects, "\t{:connections-established/%ju} "
634 "{N:/connection%s established (including accepts)}\n");
635 p(tcps_usedrtt, "\t\t{:connections-hostcache-rtt/%ju} "
636 "{N:/time%s used RTT from hostcache}\n");
637 p(tcps_usedrttvar, "\t\t{:connections-hostcache-rttvar/%ju} "
638 "{N:/time%s used RTT variance from hostcache}\n");
639 p(tcps_usedssthresh, "\t\t{:connections-hostcache-ssthresh/%ju} "
640 "{N:/time%s used slow-start threshold from hostcache}\n");
641 p2(tcps_closed, tcps_drops, "\t{:connections-closed/%ju} "
642 "{N:/connection%s closed (including} "
643 "{:connection-drops/%ju} {N:/drop%s})\n");
644 p(tcps_cachedrtt, "\t\t{:connections-updated-rtt-on-close/%ju} "
645 "{N:/connection%s updated cached RTT on close}\n");
646 p(tcps_cachedrttvar, "\t\t"
647 "{:connections-updated-variance-on-close/%ju} "
648 "{N:/connection%s updated cached RTT variance on close}\n");
649 p(tcps_cachedssthresh, "\t\t"
650 "{:connections-updated-ssthresh-on-close/%ju} "
651 "{N:/connection%s updated cached ssthresh on close}\n");
652 p(tcps_conndrops, "\t{:embryonic-connections-dropped/%ju} "
653 "{N:/embryonic connection%s dropped}\n");
654 p2(tcps_rttupdated, tcps_segstimed, "\t{:segments-updated-rtt/%ju} "
655 "{N:/segment%s updated rtt (of} "
656 "{:segment-update-attempts/%ju} {N:/attempt%s})\n");
657 p(tcps_rexmttimeo, "\t{:retransmit-timeouts/%ju} "
658 "{N:/retransmit timeout%s}\n");
659 p(tcps_timeoutdrop, "\t\t"
660 "{:connections-dropped-by-retransmit-timeout/%ju} "
661 "{N:/connection%s dropped by rexmit timeout}\n");
662 p(tcps_persisttimeo, "\t{:persist-timeout/%ju} "
663 "{N:/persist timeout%s}\n");
664 p(tcps_persistdrop, "\t\t"
665 "{:connections-dropped-by-persist-timeout/%ju} "
666 "{N:/connection%s dropped by persist timeout}\n");
667 p(tcps_finwait2_drops, "\t"
668 "{:connections-dropped-by-finwait2-timeout/%ju} "
669 "{N:/Connection%s (fin_wait_2) dropped because of timeout}\n");
670 p(tcps_keeptimeo, "\t{:keepalive-timeout/%ju} "
671 "{N:/keepalive timeout%s}\n");
672 p(tcps_keepprobe, "\t\t{:keepalive-probes/%ju} "
673 "{N:/keepalive probe%s sent}\n");
674 p(tcps_keepdrops, "\t\t{:connections-dropped-by-keepalives/%ju} "
675 "{N:/connection%s dropped by keepalive}\n");
676 p(tcps_predack, "\t{:ack-header-predictions/%ju} "
677 "{N:/correct ACK header prediction%s}\n");
678 p(tcps_preddat, "\t{:data-packet-header-predictions/%ju} "
679 "{N:/correct data packet header prediction%s}\n");
681 xo_open_container("syncache");
683 p3(tcps_sc_added, "\t{:entries-added/%ju} "
684 "{N:/syncache entr%s added}\n");
685 p1a(tcps_sc_retransmitted, "\t\t{:retransmitted/%ju} "
686 "{N:/retransmitted}\n");
687 p1a(tcps_sc_dupsyn, "\t\t{:duplicates/%ju} {N:/dupsyn}\n");
688 p1a(tcps_sc_dropped, "\t\t{:dropped/%ju} {N:/dropped}\n");
689 p1a(tcps_sc_completed, "\t\t{:completed/%ju} {N:/completed}\n");
690 p1a(tcps_sc_bucketoverflow, "\t\t{:bucket-overflow/%ju} "
691 "{N:/bucket overflow}\n");
692 p1a(tcps_sc_cacheoverflow, "\t\t{:cache-overflow/%ju} "
693 "{N:/cache overflow}\n");
694 p1a(tcps_sc_reset, "\t\t{:reset/%ju} {N:/reset}\n");
695 p1a(tcps_sc_stale, "\t\t{:stale/%ju} {N:/stale}\n");
696 p1a(tcps_sc_aborted, "\t\t{:aborted/%ju} {N:/aborted}\n");
697 p1a(tcps_sc_badack, "\t\t{:bad-ack/%ju} {N:/badack}\n");
698 p1a(tcps_sc_unreach, "\t\t{:unreachable/%ju} {N:/unreach}\n");
699 p(tcps_sc_zonefail, "\t\t{:zone-failures/%ju} {N:/zone failure%s}\n");
700 p(tcps_sc_sendcookie, "\t{:sent-cookies/%ju} {N:/cookie%s sent}\n");
701 p(tcps_sc_recvcookie, "\t{:receivd-cookies/%ju} "
702 "{N:/cookie%s received}\n");
704 xo_close_container("syncache");
706 xo_open_container("hostcache");
708 p3(tcps_hc_added, "\t{:entries-added/%ju} "
709 "{N:/hostcache entr%s added}\n");
710 p1a(tcps_hc_bucketoverflow, "\t\t{:buffer-overflows/%ju} "
711 "{N:/bucket overflow}\n");
713 xo_close_container("hostcache");
715 xo_open_container("sack");
717 p(tcps_sack_recovery_episode, "\t{:recovery-episodes/%ju} "
718 "{N:/SACK recovery episode%s}\n");
719 p(tcps_sack_rexmits, "\t{:segment-retransmits/%ju} "
720 "{N:/segment rexmit%s in SACK recovery episodes}\n");
721 p(tcps_sack_rexmit_bytes, "\t{:byte-retransmits/%ju} "
722 "{N:/byte rexmit%s in SACK recovery episodes}\n");
723 p(tcps_sack_rcv_blocks, "\t{:received-blocks/%ju} "
724 "{N:/SACK option%s (SACK blocks) received}\n");
725 p(tcps_sack_send_blocks, "\t{:sent-option-blocks/%ju} "
726 "{N:/SACK option%s (SACK blocks) sent}\n");
727 p1a(tcps_sack_sboverflow, "\t{:scoreboard-overflows/%ju} "
728 "{N:/SACK scoreboard overflow}\n");
730 xo_close_container("sack");
731 xo_open_container("ecn");
733 p(tcps_ecn_ce, "\t{:ce-packets/%ju} "
734 "{N:/packet%s with ECN CE bit set}\n");
735 p(tcps_ecn_ect0, "\t{:ect0-packets/%ju} "
736 "{N:/packet%s with ECN ECT(0) bit set}\n");
737 p(tcps_ecn_ect1, "\t{:ect1-packets/%ju} "
738 "{N:/packet%s with ECN ECT(1) bit set}\n");
739 p(tcps_ecn_shs, "\t{:handshakes/%ju} "
740 "{N:/successful ECN handshake%s}\n");
741 p(tcps_ecn_rcwnd, "\t{:congestion-reductions/%ju} "
742 "{N:/time%s ECN reduced the congestion window}\n");
744 xo_close_container("ecn");
745 xo_open_container("tcp-signature");
746 p(tcps_sig_rcvgoodsig, "\t{:received-good-signature/%ju} "
747 "{N:/packet%s with matching signature received}\n");
748 p(tcps_sig_rcvbadsig, "\t{:received-bad-signature/%ju} "
749 "{N:/packet%s with bad signature received}\n");
750 p(tcps_sig_err_buildsig, "\t{:failed-make-signature/%ju} "
751 "{N:/time%s failed to make signature due to no SA}\n");
752 p(tcps_sig_err_sigopt, "\t{:no-signature-expected/%ju} "
753 "{N:/time%s unexpected signature received}\n");
754 p(tcps_sig_err_nosigopt, "\t{:no-signature-provided/%ju} "
755 "{N:/time%s no signature provided by segment}\n");
757 xo_close_container("tcp-signature");
758 xo_open_container("pmtud");
760 p(tcps_pmtud_blackhole_activated, "\t{:pmtud-activated/%ju} "
761 "{N:/Path MTU discovery black hole detection activation%s}\n");
762 p(tcps_pmtud_blackhole_activated_min_mss,
763 "\t{:pmtud-activated-min-mss/%ju} "
764 "{N:/Path MTU discovery black hole detection min MSS activation%s}\n");
765 p(tcps_pmtud_blackhole_failed, "\t{:pmtud-failed/%ju} "
766 "{N:/Path MTU discovery black hole detection failure%s}\n");
772 xo_close_container("pmtud");
775 xo_open_container("TCP connection count by state");
776 xo_emit("{T:/TCP connection count by state}:\n");
777 for (int i = 0; i < TCP_NSTATES; i++) {
779 * XXXGL: is there a way in libxo to use %s
780 * in the "content string" of a format
781 * string? I failed to do that, that's why
782 * a temporary buffer is used to construct
783 * format string for xo_emit().
787 if (sflag > 1 && tcps_states[i] == 0)
789 snprintf(fmtbuf, sizeof(fmtbuf), "\t{:%s/%%ju} "
790 "{Np:/connection ,connections} in %s state\n",
791 tcpstates[i], tcpstates[i]);
792 xo_emit(fmtbuf, (uintmax_t )tcps_states[i]);
794 xo_close_container("TCP connection count by state");
796 xo_close_container("tcp");
800 * Dump UDP statistics structure.
803 udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
805 struct udpstat udpstat;
815 if (fetch_stats("net.inet.udp.stats", off, &udpstat,
816 sizeof(udpstat), kread_counters) != 0)
819 xo_open_container("udp");
820 xo_emit("{T:/%s}:\n", name);
822 #define p(f, m) if (udpstat.f || sflag <= 1) \
823 xo_emit("\t" m, (uintmax_t)udpstat.f, plural(udpstat.f))
824 #define p1a(f, m) if (udpstat.f || sflag <= 1) \
825 xo_emit("\t" m, (uintmax_t)udpstat.f)
827 p(udps_ipackets, "{:received-datagrams/%ju} "
828 "{N:/datagram%s received}\n");
829 p1a(udps_hdrops, "{:dropped-incomplete-headers/%ju} "
830 "{N:/with incomplete header}\n");
831 p1a(udps_badlen, "{:dropped-bad-data-length/%ju} "
832 "{N:/with bad data length field}\n");
833 p1a(udps_badsum, "{:dropped-bad-checksum/%ju} "
834 "{N:/with bad checksum}\n");
835 p1a(udps_nosum, "{:dropped-no-checksum/%ju} "
836 "{N:/with no checksum}\n");
837 p1a(udps_noport, "{:dropped-no-socket/%ju} "
838 "{N:/dropped due to no socket}\n");
839 p(udps_noportbcast, "{:dropped-broadcast-multicast/%ju} "
840 "{N:/broadcast\\/multicast datagram%s undelivered}\n");
841 p1a(udps_fullsock, "{:dropped-full-socket-buffer/%ju} "
842 "{N:/dropped due to full socket buffers}\n");
843 p1a(udpps_pcbhashmiss, "{:not-for-hashed-pcb/%ju} "
844 "{N:/not for hashed pcb}\n");
845 delivered = udpstat.udps_ipackets -
846 udpstat.udps_hdrops -
847 udpstat.udps_badlen -
848 udpstat.udps_badsum -
849 udpstat.udps_noport -
850 udpstat.udps_noportbcast -
851 udpstat.udps_fullsock;
852 if (delivered || sflag <= 1)
853 xo_emit("\t{:delivered-packets/%ju} {N:/delivered}\n",
854 (uint64_t)delivered);
855 p(udps_opackets, "{:output-packets/%ju} {N:/datagram%s output}\n");
856 /* the next statistic is cumulative in udps_noportbcast */
857 p(udps_filtermcast, "{:multicast-source-filter-matches/%ju} "
858 "{N:/time%s multicast source filter matched}\n");
861 xo_close_container("udp");
865 * Dump CARP statistics structure.
868 carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
870 struct carpstats carpstat;
872 if (fetch_stats("net.inet.carp.stats", off, &carpstat,
873 sizeof(carpstat), kread_counters) != 0)
876 xo_open_container(name);
877 xo_emit("{T:/%s}:\n", name);
879 #define p(f, m) if (carpstat.f || sflag <= 1) \
880 xo_emit(m, (uintmax_t)carpstat.f, plural(carpstat.f))
881 #define p2(f, m) if (carpstat.f || sflag <= 1) \
882 xo_emit(m, (uintmax_t)carpstat.f)
884 p(carps_ipackets, "\t{:received-inet-packets/%ju} "
885 "{N:/packet%s received (IPv4)}\n");
886 p(carps_ipackets6, "\t{:received-inet6-packets/%ju} "
887 "{N:/packet%s received (IPv6)}\n");
888 p(carps_badttl, "\t\t{:dropped-wrong-ttl/%ju} "
889 "{N:/packet%s discarded for wrong TTL}\n");
890 p(carps_hdrops, "\t\t{:dropped-short-header/%ju} "
891 "{N:/packet%s shorter than header}\n");
892 p(carps_badsum, "\t\t{:dropped-bad-checksum/%ju} "
893 "{N:/discarded for bad checksum%s}\n");
894 p(carps_badver, "\t\t{:dropped-bad-version/%ju} "
895 "{N:/discarded packet%s with a bad version}\n");
896 p2(carps_badlen, "\t\t{:dropped-short-packet/%ju} "
897 "{N:/discarded because packet too short}\n");
898 p2(carps_badauth, "\t\t{:dropped-bad-authentication/%ju} "
899 "{N:/discarded for bad authentication}\n");
900 p2(carps_badvhid, "\t\t{:dropped-bad-vhid/%ju} "
901 "{N:/discarded for bad vhid}\n");
902 p2(carps_badaddrs, "\t\t{:dropped-bad-address-list/%ju} "
903 "{N:/discarded because of a bad address list}\n");
904 p(carps_opackets, "\t{:sent-inet-packets/%ju} "
905 "{N:/packet%s sent (IPv4)}\n");
906 p(carps_opackets6, "\t{:sent-inet6-packets/%ju} "
907 "{N:/packet%s sent (IPv6)}\n");
908 p2(carps_onomem, "\t\t{:send-failed-memory-error/%ju} "
909 "{N:/send failed due to mbuf memory error}\n");
911 p(carps_ostates, "\t\t{:send-state-updates/%s} "
912 "{N:/state update%s sent}\n");
916 xo_close_container(name);
920 * Dump IP statistics structure.
923 ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
925 struct ipstat ipstat;
927 if (fetch_stats("net.inet.ip.stats", off, &ipstat,
928 sizeof(ipstat), kread_counters) != 0)
931 xo_open_container(name);
932 xo_emit("{T:/%s}:\n", name);
934 #define p(f, m) if (ipstat.f || sflag <= 1) \
935 xo_emit(m, (uintmax_t )ipstat.f, plural(ipstat.f))
936 #define p1a(f, m) if (ipstat.f || sflag <= 1) \
937 xo_emit(m, (uintmax_t )ipstat.f)
939 p(ips_total, "\t{:received-packets/%ju} "
940 "{N:/total packet%s received}\n");
941 p(ips_badsum, "\t{:dropped-bad-checksum/%ju} "
942 "{N:/bad header checksum%s}\n");
943 p1a(ips_toosmall, "\t{:dropped-below-minimum-size/%ju} "
944 "{N:/with size smaller than minimum}\n");
945 p1a(ips_tooshort, "\t{:dropped-short-packets/%ju} "
946 "{N:/with data size < data length}\n");
947 p1a(ips_toolong, "\t{:dropped-too-long/%ju} "
948 "{N:/with ip length > max ip packet size}\n");
949 p1a(ips_badhlen, "\t{:dropped-short-header-length/%ju} "
950 "{N:/with header length < data size}\n");
951 p1a(ips_badlen, "\t{:dropped-short-data/%ju} "
952 "{N:/with data length < header length}\n");
953 p1a(ips_badoptions, "\t{:dropped-bad-options/%ju} "
954 "{N:/with bad options}\n");
955 p1a(ips_badvers, "\t{:dropped-bad-version/%ju} "
956 "{N:/with incorrect version number}\n");
957 p(ips_fragments, "\t{:received-fragments/%ju} "
958 "{N:/fragment%s received}\n");
959 p(ips_fragdropped, "\t{:dropped-fragments/%ju} "
960 "{N:/fragment%s dropped (dup or out of space)}\n");
961 p(ips_fragtimeout, "\t{:dropped-fragments-after-timeout/%ju} "
962 "{N:/fragment%s dropped after timeout}\n");
963 p(ips_reassembled, "\t{:reassembled-packets/%ju} "
964 "{N:/packet%s reassembled ok}\n");
965 p(ips_delivered, "\t{:received-local-packets/%ju} "
966 "{N:/packet%s for this host}\n");
967 p(ips_noproto, "\t{:dropped-unknown-protocol/%ju} "
968 "{N:/packet%s for unknown\\/unsupported protocol}\n");
969 p(ips_forward, "\t{:forwarded-packets/%ju} "
970 "{N:/packet%s forwarded}");
971 p(ips_fastforward, " ({:fast-forwarded-packets/%ju} "
972 "{N:/packet%s fast forwarded})");
973 if (ipstat.ips_forward || sflag <= 1)
975 p(ips_cantforward, "\t{:packets-cannot-forward/%ju} "
976 "{N:/packet%s not forwardable}\n");
977 p(ips_notmember, "\t{:received-unknown-multicast-group/%ju} "
978 "{N:/packet%s received for unknown multicast group}\n");
979 p(ips_redirectsent, "\t{:redirects-sent/%ju} "
980 "{N:/redirect%s sent}\n");
981 p(ips_localout, "\t{:sent-packets/%ju} "
982 "{N:/packet%s sent from this host}\n");
983 p(ips_rawout, "\t{:send-packets-fabricated-header/%ju} "
984 "{N:/packet%s sent with fabricated ip header}\n");
985 p(ips_odropped, "\t{:discard-no-mbufs/%ju} "
986 "{N:/output packet%s dropped due to no bufs, etc.}\n");
987 p(ips_noroute, "\t{:discard-no-route/%ju} "
988 "{N:/output packet%s discarded due to no route}\n");
989 p(ips_fragmented, "\t{:sent-fragments/%ju} "
990 "{N:/output datagram%s fragmented}\n");
991 p(ips_ofragments, "\t{:fragments-created/%ju} "
992 "{N:/fragment%s created}\n");
993 p(ips_cantfrag, "\t{:discard-cannot-fragment/%ju} "
994 "{N:/datagram%s that can't be fragmented}\n");
995 p(ips_nogif, "\t{:discard-tunnel-no-gif/%ju} "
996 "{N:/tunneling packet%s that can't find gif}\n");
997 p(ips_badaddr, "\t{:discard-bad-address/%ju} "
998 "{N:/datagram%s with bad address in header}\n");
1001 xo_close_container(name);
1005 * Dump ARP statistics structure.
1008 arp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1010 struct arpstat arpstat;
1012 if (fetch_stats("net.link.ether.arp.stats", off, &arpstat,
1013 sizeof(arpstat), kread_counters) != 0)
1016 xo_open_container(name);
1017 xo_emit("{T:/%s}:\n", name);
1019 #define p(f, m) if (arpstat.f || sflag <= 1) \
1020 xo_emit("\t" m, (uintmax_t)arpstat.f, plural(arpstat.f))
1021 #define p2(f, m) if (arpstat.f || sflag <= 1) \
1022 xo_emit("\t" m, (uintmax_t)arpstat.f, pluralies(arpstat.f))
1024 p(txrequests, "{:sent-requests/%ju} {N:/ARP request%s sent}\n");
1025 p2(txreplies, "{:sent-replies/%ju} {N:/ARP repl%s sent}\n");
1026 p(rxrequests, "{:received-requests/%ju} "
1027 "{N:/ARP request%s received}\n");
1028 p2(rxreplies, "{:received-replies/%ju} "
1029 "{N:/ARP repl%s received}\n");
1030 p(received, "{:received-packers/%ju} "
1031 "{N:/ARP packet%s received}\n");
1032 p(dropped, "{:dropped-no-entry/%ju} "
1033 "{N:/total packet%s dropped due to no ARP entry}\n");
1034 p(timeouts, "{:entries-timeout/%ju} "
1035 "{N:/ARP entry%s timed out}\n");
1036 p(dupips, "{:dropped-duplicate-address/%ju} "
1037 "{N:/Duplicate IP%s seen}\n");
1040 xo_close_container(name);
1045 static const char *icmpnames[ICMP_MAXTYPE + 1] = {
1046 "echo reply", /* RFC 792 */
1049 "destination unreachable", /* RFC 792 */
1050 "source quench", /* RFC 792 */
1051 "routing redirect", /* RFC 792 */
1054 "echo", /* RFC 792 */
1055 "router advertisement", /* RFC 1256 */
1056 "router solicitation", /* RFC 1256 */
1057 "time exceeded", /* RFC 792 */
1058 "parameter problem", /* RFC 792 */
1059 "time stamp", /* RFC 792 */
1060 "time stamp reply", /* RFC 792 */
1061 "information request", /* RFC 792 */
1062 "information request reply", /* RFC 792 */
1063 "address mask request", /* RFC 950 */
1064 "address mask reply", /* RFC 950 */
1076 "icmp traceroute", /* RFC 1393 */
1077 "datagram conversion error", /* RFC 1475 */
1078 "mobile host redirect",
1079 "IPv6 where-are-you",
1081 "mobile registration req",
1082 "mobile registration reply",
1083 "domain name request", /* RFC 1788 */
1084 "domain name reply", /* RFC 1788 */
1086 "icmp photuris", /* RFC 2521 */
1090 * Dump ICMP statistics.
1093 icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1095 struct icmpstat icmpstat;
1099 if (fetch_stats("net.inet.icmp.stats", off, &icmpstat,
1100 sizeof(icmpstat), kread_counters) != 0)
1103 xo_open_container(name);
1104 xo_emit("{T:/%s}:\n", name);
1106 #define p(f, m) if (icmpstat.f || sflag <= 1) \
1107 xo_emit(m, icmpstat.f, plural(icmpstat.f))
1108 #define p1a(f, m) if (icmpstat.f || sflag <= 1) \
1109 xo_emit(m, icmpstat.f)
1110 #define p2(f, m) if (icmpstat.f || sflag <= 1) \
1111 xo_emit(m, icmpstat.f, plurales(icmpstat.f))
1113 p(icps_error, "\t{:icmp-calls/%lu} "
1114 "{N:/call%s to icmp_error}\n");
1115 p(icps_oldicmp, "\t{:errors-not-from-message/%lu} "
1116 "{N:/error%s not generated in response to an icmp message}\n");
1118 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) {
1119 if (icmpstat.icps_outhist[i] != 0) {
1121 xo_open_list("output-histogram");
1122 xo_emit("\tOutput histogram:\n");
1125 xo_open_instance("output-histogram");
1126 if (icmpnames[i] != NULL)
1127 xo_emit("\t\t{k:name/%s}: {:count/%lu}\n",
1128 icmpnames[i], icmpstat.icps_outhist[i]);
1130 xo_emit("\t\tunknown ICMP #{k:name/%d}: "
1132 i, icmpstat.icps_outhist[i]);
1133 xo_close_instance("output-histogram");
1137 xo_close_list("output-histogram");
1139 p(icps_badcode, "\t{:dropped-bad-code/%lu} "
1140 "{N:/message%s with bad code fields}\n");
1141 p(icps_tooshort, "\t{:dropped-too-short/%lu} "
1142 "{N:/message%s less than the minimum length}\n");
1143 p(icps_checksum, "\t{:dropped-bad-checksum/%lu} "
1144 "{N:/message%s with bad checksum}\n");
1145 p(icps_badlen, "\t{:dropped-bad-length/%lu} "
1146 "{N:/message%s with bad length}\n");
1147 p1a(icps_bmcastecho, "\t{:dropped-multicast-echo/%lu} "
1148 "{N:/multicast echo requests ignored}\n");
1149 p1a(icps_bmcasttstamp, "\t{:dropped-multicast-timestamp/%lu} "
1150 "{N:/multicast timestamp requests ignored}\n");
1152 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) {
1153 if (icmpstat.icps_inhist[i] != 0) {
1155 xo_open_list("input-histogram");
1156 xo_emit("\tInput histogram:\n");
1159 xo_open_instance("input-histogram");
1160 if (icmpnames[i] != NULL)
1161 xo_emit("\t\t{k:name/%s}: {:count/%lu}\n",
1163 icmpstat.icps_inhist[i]);
1166 "\t\tunknown ICMP #{k:name/%d}: {:count/%lu}\n",
1167 i, icmpstat.icps_inhist[i]);
1168 xo_close_instance("input-histogram");
1172 xo_close_list("input-histogram");
1174 p(icps_reflect, "\t{:sent-packets/%lu} "
1175 "{N:/message response%s generated}\n");
1176 p2(icps_badaddr, "\t{:discard-invalid-return-address/%lu} "
1177 "{N:/invalid return address%s}\n");
1178 p(icps_noroute, "\t{:discard-no-route/%lu} "
1179 "{N:/no return route%s}\n");
1185 if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) <
1188 xo_emit("\tICMP address mask responses are "
1189 "{q:icmp-address-responses/%sabled}\n", i ? "en" : "dis");
1192 xo_close_container(name);
1196 * Dump IGMP statistics structure.
1199 igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1201 struct igmpstat igmpstat;
1203 if (fetch_stats("net.inet.igmp.stats", 0, &igmpstat,
1204 sizeof(igmpstat), kread) != 0)
1207 if (igmpstat.igps_version != IGPS_VERSION_3) {
1208 xo_warnx("%s: version mismatch (%d != %d)", __func__,
1209 igmpstat.igps_version, IGPS_VERSION_3);
1211 if (igmpstat.igps_len != IGPS_VERSION3_LEN) {
1212 xo_warnx("%s: size mismatch (%d != %d)", __func__,
1213 igmpstat.igps_len, IGPS_VERSION3_LEN);
1216 xo_open_container(name);
1217 xo_emit("{T:/%s}:\n", name);
1219 #define p64(f, m) if (igmpstat.f || sflag <= 1) \
1220 xo_emit(m, (uintmax_t) igmpstat.f, plural(igmpstat.f))
1221 #define py64(f, m) if (igmpstat.f || sflag <= 1) \
1222 xo_emit(m, (uintmax_t) igmpstat.f, pluralies(igmpstat.f))
1224 p64(igps_rcv_total, "\t{:received-messages/%ju} "
1225 "{N:/message%s received}\n");
1226 p64(igps_rcv_tooshort, "\t{:dropped-too-short/%ju} "
1227 "{N:/message%s received with too few bytes}\n");
1228 p64(igps_rcv_badttl, "\t{:dropped-wrong-ttl/%ju} "
1229 "{N:/message%s received with wrong TTL}\n");
1230 p64(igps_rcv_badsum, "\t{:dropped-bad-checksum/%ju} "
1231 "{N:/message%s received with bad checksum}\n");
1232 py64(igps_rcv_v1v2_queries, "\t{:received-membership-queries/%ju} "
1233 "{N:/V1\\/V2 membership quer%s received}\n");
1234 py64(igps_rcv_v3_queries, "\t{:received-v3-membership-queries/%ju} "
1235 "{N:/V3 membership quer%s received}\n");
1236 py64(igps_rcv_badqueries, "\t{:dropped-membership-queries/%ju} "
1237 "{N:/membership quer%s received with invalid field(s)}\n");
1238 py64(igps_rcv_gen_queries, "\t{:received-general-queries/%ju} "
1239 "{N:/general quer%s received}\n");
1240 py64(igps_rcv_group_queries, "\t{:received-group-queries/%ju} "
1241 "{N:/group quer%s received}\n");
1242 py64(igps_rcv_gsr_queries, "\t{:received-group-source-queries/%ju} "
1243 "{N:/group-source quer%s received}\n");
1244 py64(igps_drop_gsr_queries, "\t{:dropped-group-source-queries/%ju} "
1245 "{N:/group-source quer%s dropped}\n");
1246 p64(igps_rcv_reports, "\t{:received-membership-requests/%ju} "
1247 "{N:/membership report%s received}\n");
1248 p64(igps_rcv_badreports, "\t{:dropped-membership-reports/%ju} "
1249 "{N:/membership report%s received with invalid field(s)}\n");
1250 p64(igps_rcv_ourreports, "\t"
1251 "{:received-membership-reports-matching/%ju} "
1252 "{N:/membership report%s received for groups to which we belong}"
1254 p64(igps_rcv_nora, "\t{:received-v3-reports-no-router-alert/%ju} "
1255 "{N:/V3 report%s received without Router Alert}\n");
1256 p64(igps_snd_reports, "\t{:sent-membership-reports/%ju} "
1257 "{N:/membership report%s sent}\n");
1260 xo_close_container(name);
1264 * Dump PIM statistics structure.
1267 pim_stats(u_long off __unused, const char *name, int af1 __unused,
1270 struct pimstat pimstat;
1272 if (fetch_stats("net.inet.pim.stats", off, &pimstat,
1273 sizeof(pimstat), kread_counters) != 0)
1276 xo_open_container(name);
1277 xo_emit("{T:/%s}:\n", name);
1279 #define p(f, m) if (pimstat.f || sflag <= 1) \
1280 xo_emit(m, (uintmax_t)pimstat.f, plural(pimstat.f))
1281 #define py(f, m) if (pimstat.f || sflag <= 1) \
1282 xo_emit(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
1284 p(pims_rcv_total_msgs, "\t{:received-messages/%ju} "
1285 "{N:/message%s received}\n");
1286 p(pims_rcv_total_bytes, "\t{:received-bytes/%ju} "
1287 "{N:/byte%s received}\n");
1288 p(pims_rcv_tooshort, "\t{:dropped-too-short/%ju} "
1289 "{N:/message%s received with too few bytes}\n");
1290 p(pims_rcv_badsum, "\t{:dropped-bad-checksum/%ju} "
1291 "{N:/message%s received with bad checksum}\n");
1292 p(pims_rcv_badversion, "\t{:dropped-bad-version/%ju} "
1293 "{N:/message%s received with bad version}\n");
1294 p(pims_rcv_registers_msgs, "\t{:received-data-register-messages/%ju} "
1295 "{N:/data register message%s received}\n");
1296 p(pims_rcv_registers_bytes, "\t{:received-data-register-bytes/%ju} "
1297 "{N:/data register byte%s received}\n");
1298 p(pims_rcv_registers_wrongiif, "\t"
1299 "{:received-data-register-wrong-interface/%ju} "
1300 "{N:/data register message%s received on wrong iif}\n");
1301 p(pims_rcv_badregisters, "\t{:received-bad-registers/%ju} "
1302 "{N:/bad register%s received}\n");
1303 p(pims_snd_registers_msgs, "\t{:sent-data-register-messages/%ju} "
1304 "{N:/data register message%s sent}\n");
1305 p(pims_snd_registers_bytes, "\t{:sent-data-register-bytes/%ju} "
1306 "{N:/data register byte%s sent}\n");
1309 xo_close_container(name);
1313 * Pretty print an Internet address (net address + port).
1316 inetprint(const char *container, struct in_addr *in, int port,
1317 const char *proto, int num_port, const int af1)
1319 struct servent *sp = 0;
1325 xo_open_container(container);
1328 snprintf(line, sizeof(line), "%s.", inetname(in));
1330 snprintf(line, sizeof(line), "%.*s.",
1331 (Aflag && !num_port) ? 12 : 16, inetname(in));
1332 alen = strlen(line);
1334 if (!num_port && port)
1335 sp = getservbyport((int)port, proto);
1336 if (sp || port == 0)
1337 snprintf(cp, sizeof(line) - alen,
1338 "%.15s ", sp ? sp->s_name : "*");
1340 snprintf(cp, sizeof(line) - alen,
1341 "%d ", ntohs((u_short)port));
1342 width = (Aflag && !Wflag) ? 18 :
1343 ((!Wflag || af1 == AF_INET) ? 22 : 45);
1345 xo_emit("{d:target/%-*s} ", width, line);
1347 xo_emit("{d:target/%-*.*s} ", width, width, line);
1349 plen = strlen(cp) - 1;
1351 xo_emit("{e:address/%*.*s}{e:port/%*.*s}", alen, alen, line, plen,
1355 xo_close_container(container);
1359 * Construct an Internet address representation.
1360 * If numeric_addr has been supplied, give
1361 * numeric value, otherwise try for symbolic name.
1364 inetname(struct in_addr *inp)
1367 static char line[MAXHOSTNAMELEN];
1372 if (!numeric_addr && inp->s_addr != INADDR_ANY) {
1373 int net = inet_netof(*inp);
1374 int lna = inet_lnaof(*inp);
1376 if (lna == INADDR_ANY) {
1377 np = getnetbyaddr(net, AF_INET);
1382 hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
1385 trimdomain(cp, strlen(cp));
1389 if (inp->s_addr == INADDR_ANY)
1392 strlcpy(line, cp, sizeof(line));
1394 inp->s_addr = ntohl(inp->s_addr);
1395 #define C(x) ((u_int)((x) & 0xff))
1396 snprintf(line, sizeof(line), "%u.%u.%u.%u",
1397 C(inp->s_addr >> 24), C(inp->s_addr >> 16),
1398 C(inp->s_addr >> 8), C(inp->s_addr));