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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
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>
44 #include <sys/socketvar.h>
45 #include <sys/sysctl.h>
47 #include <net/route.h>
48 #include <net/if_arp.h>
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip_carp.h>
54 #include <netinet/ip6.h>
56 #include <netinet/in_pcb.h>
57 #include <netinet/ip_icmp.h>
58 #include <netinet/icmp_var.h>
59 #include <netinet/igmp_var.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/pim_var.h>
62 #include <netinet/tcp.h>
63 #include <netinet/tcpip.h>
64 #include <netinet/tcp_seq.h>
66 #include <netinet/tcp_fsm.h>
67 #include <netinet/tcp_timer.h>
68 #include <netinet/tcp_var.h>
69 #include <netinet/udp.h>
70 #include <netinet/udp_var.h>
72 #include <arpa/inet.h>
87 char *inetname(struct in_addr *);
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, int istcp __unused)
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_qlen = so->so_qlen;
175 xso->so_incqlen = so->so_incqlen;
176 xso->so_qlimit = so->so_qlimit;
177 xso->so_timeo = so->so_timeo;
178 xso->so_error = so->so_error;
179 xso->so_oobmark = so->so_oobmark;
180 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
181 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
186 pcblist_kvm(u_long off, char **bufp, int istcp)
188 struct inpcbinfo pcbinfo;
189 struct inpcbhead listhead;
201 kread(off, &pcbinfo, sizeof(pcbinfo));
203 len = 2 * sizeof(xig) +
204 (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) *
205 sizeof(struct xtcpcb);
207 len = 2 * sizeof(xig) +
208 (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) *
209 sizeof(struct xinpcb);
210 if ((buf = malloc(len)) == NULL) {
211 xo_warnx("malloc %lu bytes", (u_long)len);
216 #define COPYOUT(obj, size) do { \
217 if (len < (size)) { \
218 xo_warnx("buffer size exceeded"); \
221 bcopy((obj), p, (size)); \
226 #define KREAD(off, buf, len) do { \
227 if (kread((uintptr_t)(off), (buf), (len)) != 0) \
231 /* Write out header. */
232 xig.xig_len = sizeof xig;
233 xig.xig_count = pcbinfo.ipi_count;
234 xig.xig_gen = pcbinfo.ipi_gencnt;
236 COPYOUT(&xig, sizeof xig);
238 /* Walk the PCB list. */
239 xt.xt_len = sizeof xt;
240 xi.xi_len = sizeof xi;
245 KREAD(pcbinfo.ipi_listhead, &listhead, sizeof(listhead));
246 LIST_FOREACH(inp, &listhead, inp_list) {
248 KREAD(inp, &xt.xt_inp, sizeof(*inp));
251 KREAD(inp, &xi.xi_inp, sizeof(*inp));
255 if (inp->inp_gencnt > pcbinfo.ipi_gencnt)
259 if (inp->inp_ppcb == NULL)
260 bzero(&xt.xt_tp, sizeof xt.xt_tp);
261 else if (inp->inp_flags & INP_TIMEWAIT) {
262 bzero(&xt.xt_tp, sizeof xt.xt_tp);
263 xt.xt_tp.t_state = TCPS_TIME_WAIT;
265 KREAD(inp->inp_ppcb, &xt.xt_tp,
268 if (inp->inp_socket) {
269 KREAD(inp->inp_socket, &so, sizeof(so));
270 if (sotoxsocket(&so, xso) != 0)
273 bzero(xso, sizeof(*xso));
275 xso->xso_protocol = IPPROTO_TCP;
278 COPYOUT(&xt, sizeof xt);
280 COPYOUT(&xi, sizeof xi);
283 /* Reread the pcbinfo and write out the footer. */
284 kread(off, &pcbinfo, sizeof(pcbinfo));
285 xig.xig_count = pcbinfo.ipi_count;
286 xig.xig_gen = pcbinfo.ipi_gencnt;
287 COPYOUT(&xig, sizeof xig);
300 * Print a summary of connections related to an Internet
301 * protocol. For TCP, also give state of connection.
302 * Listening processes (aflag) are suppressed unless the
303 * -a (all) flag is specified.
306 protopr(u_long off, const char *name, int af1, int proto)
309 static int first = 1;
312 struct tcpcb *tp = NULL;
314 struct xinpgen *xig, *oxig;
316 struct xtcp_timer *timer;
322 if (strncmp(name, "sdp", 3) != 0) {
346 if (!pcblist_sysctl(proto, name, &buf, istcp))
349 if (!pcblist_kvm(off, &buf, istcp))
353 oxig = xig = (struct xinpgen *)buf;
354 for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
355 xig->xig_len > sizeof(struct xinpgen);
356 xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
358 timer = &((struct xtcpcb *)xig)->xt_timer;
359 tp = &((struct xtcpcb *)xig)->xt_tp;
360 inp = &((struct xtcpcb *)xig)->xt_inp;
361 so = &((struct xtcpcb *)xig)->xt_socket;
363 inp = &((struct xinpcb *)xig)->xi_inp;
364 so = &((struct xinpcb *)xig)->xi_socket;
368 /* Ignore sockets for protocols other than the desired one. */
369 if (so->xso_protocol != proto)
372 /* Ignore PCBs which were freed during copyout. */
373 if (inp->inp_gencnt > oxig->xig_gen)
376 if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
378 || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
380 || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
382 && (inp->inp_vflag & INP_IPV6) == 0
389 (istcp && tp->t_state == TCPS_LISTEN)
390 || (af1 == AF_INET &&
391 inet_lnaof(inp->inp_laddr) == INADDR_ANY)
393 || (af1 == AF_INET6 &&
394 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
396 || (af1 == AF_UNSPEC &&
397 (((inp->inp_vflag & INP_IPV4) != 0 &&
398 inet_lnaof(inp->inp_laddr) == INADDR_ANY)
400 || ((inp->inp_vflag & INP_IPV6) != 0 &&
401 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
409 xo_emit("Active Internet connections");
411 xo_emit(" (including servers)");
414 "Current listen queue sizes (qlen/incqlen/maxqlen)");
417 xo_emit("{T:/%-*s} ", 2 * (int)sizeof(void *),
420 xo_emit((Aflag && !Wflag) ?
421 "{T:/%-5.5s} {T:/%-32.32s} {T:/%-18.18s}" :
422 ((!Wflag || af1 == AF_INET) ?
423 "{T:/%-5.5s} {T:/%-32.32s} {T:/%-22.22s}" :
424 "{T:/%-5.5s} {T:/%-32.32s} {T:/%-45.45s}"),
425 "Proto", "Listen", "Local Address");
427 xo_emit((Aflag && !Wflag) ?
428 "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%s}" :
429 ((!Wflag || af1 == AF_INET) ?
430 "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%s}" :
431 "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%s}"),
432 "Proto", "Rexmit", "OOORcv", "0-win",
433 "Local Address", "Foreign Address");
435 xo_emit((Aflag && !Wflag) ?
436 "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%-18.18s}" :
437 ((!Wflag || af1 == AF_INET) ?
438 "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%-22.22s}" :
439 "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%-45.45s}"),
440 "Proto", "Recv-Q", "Send-Q",
441 "Local Address", "Foreign Address");
442 if (!xflag && !Rflag)
446 xo_emit(" {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
447 "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
448 "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
449 "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s}",
450 "R-MBUF", "S-MBUF", "R-CLUS", "S-CLUS",
451 "R-HIWA", "S-HIWA", "R-LOWA", "S-LOWA",
452 "R-BCNT", "S-BCNT", "R-BMAX", "S-BMAX");
453 xo_emit(" {T:/%7.7s} {T:/%7.7s} {T:/%7.7s} "
454 "{T:/%7.7s} {T:/%7.7s} {T:/%7.7s}",
455 "rexmt", "persist", "keep", "2msl",
456 "delack", "rcvtime");
458 xo_emit(" {T:/%8.8s} {T:/%5.5s}",
464 if (Lflag && so->so_qlimit == 0)
466 xo_open_instance("socket");
469 xo_emit("{q:address/%*lx} ",
470 2 * (int)sizeof(void *),
471 (u_long)inp->inp_ppcb);
473 xo_emit("{q:adddress/%*lx} ",
474 2 * (int)sizeof(void *),
478 if ((inp->inp_vflag & INP_IPV6) != 0)
479 vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
483 vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
485 if (istcp && (tp->t_flags & TF_TOE) != 0)
486 xo_emit("{:protocol/%-3.3s%-2.2s/%s%s} ", "toe", vchar);
488 xo_emit("{:protocol/%-3.3s%-2.2s/%s%s} ", name, vchar);
492 snprintf(buf1, sizeof buf1, "%u/%u/%u", so->so_qlen,
493 so->so_incqlen, so->so_qlimit);
494 xo_emit("{:listen-queue-sizes/%-32.32s} ", buf1);
497 xo_emit("{:sent-retransmit-packets/%6u} "
498 "{:received-out-of-order-packets/%6u} "
499 "{:sent-zero-window/%6u} ",
500 tp->t_sndrexmitpack, tp->t_rcvoopack,
503 xo_emit("{P:/%21s}", "");
505 xo_emit("{:receive-bytes-waiting/%6u} "
506 "{:send-bytes-waiting/%6u} ",
507 so->so_rcv.sb_cc, so->so_snd.sb_cc);
510 if (inp->inp_vflag & INP_IPV4) {
511 inetprint("local", &inp->inp_laddr,
512 (int)inp->inp_lport, name, 1, af1);
514 inetprint("remote", &inp->inp_faddr,
515 (int)inp->inp_fport, name, 1, af1);
518 else if (inp->inp_vflag & INP_IPV6) {
519 inet6print("local", &inp->in6p_laddr,
520 (int)inp->inp_lport, name, 1);
522 inet6print("remote", &inp->in6p_faddr,
523 (int)inp->inp_fport, name, 1);
524 } /* else nothing printed now */
526 } else if (inp->inp_flags & INP_ANONPORT) {
527 if (inp->inp_vflag & INP_IPV4) {
528 inetprint("local", &inp->inp_laddr,
529 (int)inp->inp_lport, name, 1, af1);
531 inetprint("remote", &inp->inp_faddr,
532 (int)inp->inp_fport, name, 0, af1);
535 else if (inp->inp_vflag & INP_IPV6) {
536 inet6print("local", &inp->in6p_laddr,
537 (int)inp->inp_lport, name, 1);
539 inet6print("remote", &inp->in6p_faddr,
540 (int)inp->inp_fport, name, 0);
541 } /* else nothing printed now */
544 if (inp->inp_vflag & INP_IPV4) {
545 inetprint("local", &inp->inp_laddr,
546 (int)inp->inp_lport, name, 0, af1);
548 inetprint("remote", &inp->inp_faddr,
549 (int)inp->inp_fport, name,
550 inp->inp_lport != inp->inp_fport,
554 else if (inp->inp_vflag & INP_IPV6) {
555 inet6print("local", &inp->in6p_laddr,
556 (int)inp->inp_lport, name, 0);
558 inet6print("remote", &inp->in6p_faddr,
559 (int)inp->inp_fport, name,
560 inp->inp_lport != inp->inp_fport);
561 } /* else nothing printed now */
565 xo_emit("{:receive-mbufs/%6u} {:send-mbufs/%6u} "
566 "{:receive-clusters/%6u} {:send-clusters/%6u} "
567 "{:receive-high-water/%6u} {:send-high-water/%6u} "
568 "{:receive-low-water/%6u} {:send-low-water/%6u} "
569 "{:receive-mbuf-bytes/%6u} {:send-mbuf-bytes/%6u} "
570 "{:receive-mbuf-bytes-max/%6u} "
571 "{:send-mbuf-bytes-max/%6u}",
572 so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt,
573 so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt,
574 so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
575 so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
576 so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
577 so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
579 xo_emit(" {:retransmit-timer/%4d.%02d} "
580 "{:persist-timer/%4d.%02d} "
581 "{:keepalive-timer/%4d.%02d} "
582 "{:msl2-timer/%4d.%02d} "
583 "{:delay-ack-timer/%4d.%02d} "
584 "{:inactivity-timer/%4d.%02d}",
585 timer->tt_rexmt / 1000,
586 (timer->tt_rexmt % 1000) / 10,
587 timer->tt_persist / 1000,
588 (timer->tt_persist % 1000) / 10,
589 timer->tt_keep / 1000,
590 (timer->tt_keep % 1000) / 10,
591 timer->tt_2msl / 1000,
592 (timer->tt_2msl % 1000) / 10,
593 timer->tt_delack / 1000,
594 (timer->tt_delack % 1000) / 10,
595 timer->t_rcvtime / 1000,
596 (timer->t_rcvtime % 1000) / 10);
598 if (istcp && !Lflag && !xflag && !Tflag && !Rflag) {
599 if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
600 xo_emit("{:tcp-state/%d}", tp->t_state);
602 xo_emit("{:tcp-state/%s}",
603 tcpstates[tp->t_state]);
604 #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
605 /* Show T/TCP `hidden state' */
606 if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
607 xo_emit("{:need-syn-or-fin/*}");
608 #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
612 /* XXX: is this right Alfred */
613 xo_emit(" {:flow-id/%08x} {:flow-type/%5d}",
618 xo_close_instance("socket");
620 if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
621 if (oxig->xig_count > xig->xig_count) {
622 xo_emit("Some {d:lost/%s} sockets may have been "
624 } else if (oxig->xig_count < xig->xig_count) {
625 xo_emit("Some {d:created/%s} sockets may have been "
628 xo_emit("Some {d:changed/%s} sockets may have been "
629 "created or deleted.\n", name);
636 * Dump TCP statistics structure.
639 tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
641 struct tcpstat tcpstat;
642 uint64_t tcps_states[TCP_NSTATES];
651 if (fetch_stats("net.inet.tcp.stats", off, &tcpstat,
652 sizeof(tcpstat), kread_counters) != 0)
655 if (fetch_stats_ro("net.inet.tcp.states", nl[N_TCPS_STATES].n_value,
656 &tcps_states, sizeof(tcps_states), kread_counters) != 0)
659 xo_open_container("tcp");
660 xo_emit("{T:/%s}:\n", name);
662 #define p(f, m) if (tcpstat.f || sflag <= 1) \
663 xo_emit(m, (uintmax_t )tcpstat.f, plural(tcpstat.f))
664 #define p1a(f, m) if (tcpstat.f || sflag <= 1) \
665 xo_emit(m, (uintmax_t )tcpstat.f)
666 #define p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
667 xo_emit(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1), \
668 (uintmax_t )tcpstat.f2, plural(tcpstat.f2))
669 #define p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
670 xo_emit(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1), \
671 (uintmax_t )tcpstat.f2)
672 #define p3(f, m) if (tcpstat.f || sflag <= 1) \
673 xo_emit(m, (uintmax_t )tcpstat.f, pluralies(tcpstat.f))
675 p(tcps_sndtotal, "\t{:sent-packets/%ju} {N:/packet%s sent}\n");
676 p2(tcps_sndpack,tcps_sndbyte, "\t\t{:sent-data-packets/%ju} "
677 "{N:/data packet%s} ({:sent-data-bytes/%ju} {N:/byte%s})\n");
678 p2(tcps_sndrexmitpack, tcps_sndrexmitbyte, "\t\t"
679 "{:sent-retransmitted-packets/%ju} {N:/data packet%s} "
680 "({:sent-retransmitted-bytes/%ju} {N:/byte%s}) "
681 "{N:retransmitted}\n");
682 p(tcps_sndrexmitbad, "\t\t"
683 "{:sent-unnecessary-retransmitted-packets/%ju} "
684 "{N:/data packet%s unnecessarily retransmitted}\n");
685 p(tcps_mturesent, "\t\t{:sent-resends-by-mtu-discovery/%ju} "
686 "{N:/resend%s initiated by MTU discovery}\n");
687 p2a(tcps_sndacks, tcps_delack, "\t\t{:sent-ack-only-packets/%ju} "
688 "{N:/ack-only packet%s/} ({:sent-packets-delayed/%ju} "
690 p(tcps_sndurg, "\t\t{:sent-urg-only-packets/%ju} "
691 "{N:/URG only packet%s}\n");
692 p(tcps_sndprobe, "\t\t{:sent-window-probe-packets/%ju} "
693 "{N:/window probe packet%s}\n");
694 p(tcps_sndwinup, "\t\t{:sent-window-update-packets/%ju} "
695 "{N:/window update packet%s}\n");
696 p(tcps_sndctrl, "\t\t{:sent-control-packets/%ju} "
697 "{N:/control packet%s}\n");
698 p(tcps_rcvtotal, "\t{:received-packets/%ju} "
699 "{N:/packet%s received}\n");
700 p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t"
701 "{:received-ack-packets/%ju} {N:/ack%s} "
702 "{N:(for} {:received-ack-bytes/%ju} {N:/byte%s})\n");
703 p(tcps_rcvdupack, "\t\t{:received-duplicate-acks/%ju} "
704 "{N:/duplicate ack%s}\n");
705 p(tcps_rcvacktoomuch, "\t\t{:received-acks-for-unsent-data/%ju} "
706 "{N:/ack%s for unsent data}\n");
707 p2(tcps_rcvpack, tcps_rcvbyte, "\t\t"
708 "{:received-in-sequence-packets/%ju} {N:/packet%s} "
709 "({:received-in-sequence-bytes/%ju} {N:/byte%s}) "
710 "{N:received in-sequence}\n");
711 p2(tcps_rcvduppack, tcps_rcvdupbyte, "\t\t"
712 "{:received-completely-duplicate-packets/%ju} "
713 "{N:/completely duplicate packet%s} "
714 "({:received-completely-duplicate-bytes/%ju} {N:/byte%s})\n");
715 p(tcps_pawsdrop, "\t\t{:received-old-duplicate-packets/%ju} "
716 "{N:/old duplicate packet%s}\n");
717 p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte, "\t\t"
718 "{:received-some-duplicate-packets/%ju} "
719 "{N:/packet%s with some dup. data} "
720 "({:received-some-duplicate-bytes/%ju} {N:/byte%s duped/})\n");
721 p2(tcps_rcvoopack, tcps_rcvoobyte, "\t\t{:received-out-of-order/%ju} "
722 "{N:/out-of-order packet%s} "
723 "({:received-out-of-order-bytes/%ju} {N:/byte%s})\n");
724 p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin, "\t\t"
725 "{:received-after-window-packets/%ju} {N:/packet%s} "
726 "({:received-after-window-bytes/%ju} {N:/byte%s}) "
727 "{N:of data after window}\n");
728 p(tcps_rcvwinprobe, "\t\t{:received-window-probes/%ju} "
729 "{N:/window probe%s}\n");
730 p(tcps_rcvwinupd, "\t\t{:receive-window-update-packets/%ju} "
731 "{N:/window update packet%s}\n");
732 p(tcps_rcvafterclose, "\t\t{:received-after-close-packets/%ju} "
733 "{N:/packet%s received after close}\n");
734 p(tcps_rcvbadsum, "\t\t{:discard-bad-checksum/%ju} "
735 "{N:/discarded for bad checksum%s}\n");
736 p(tcps_rcvbadoff, "\t\t{:discard-bad-header-offset/%ju} "
737 "{N:/discarded for bad header offset field%s}\n");
738 p1a(tcps_rcvshort, "\t\t{:discard-too-short/%ju} "
739 "{N:discarded because packet too short}\n");
740 p1a(tcps_rcvmemdrop, "\t\t{:discard-memory-problems/%ju} "
741 "{N:discarded due to memory problems}\n");
742 p(tcps_connattempt, "\t{:connection-requests/%ju} "
743 "{N:/connection request%s}\n");
744 p(tcps_accepts, "\t{:connections-accepts/%ju} "
745 "{N:/connection accept%s}\n");
746 p(tcps_badsyn, "\t{:bad-connection-attempts/%ju} "
747 "{N:/bad connection attempt%s}\n");
748 p(tcps_listendrop, "\t{:listen-queue-overflows/%ju} "
749 "{N:/listen queue overflow%s}\n");
750 p(tcps_badrst, "\t{:ignored-in-window-resets/%ju} "
751 "{N:/ignored RSTs in the window%s}\n");
752 p(tcps_connects, "\t{:connections-established/%ju} "
753 "{N:/connection%s established (including accepts)}\n");
754 p2(tcps_closed, tcps_drops, "\t{:connections-closed/%ju} "
755 "{N:/connection%s closed (including} "
756 "{:connection-drops/%ju} {N:/drop%s})\n");
757 p(tcps_cachedrtt, "\t\t{:connections-updated-rtt-on-close/%ju} "
758 "{N:/connection%s updated cached RTT on close}\n");
759 p(tcps_cachedrttvar, "\t\t"
760 "{:connections-updated-variance-on-close/%ju} "
761 "{N:/connection%s updated cached RTT variance on close}\n");
762 p(tcps_cachedssthresh, "\t\t"
763 "{:connections-updated-ssthresh-on-close/%ju} "
764 "{N:/connection%s updated cached ssthresh on close}\n");
765 p(tcps_conndrops, "\t{:embryonic-connections-dropped/%ju} "
766 "{N:/embryonic connection%s dropped}\n");
767 p2(tcps_rttupdated, tcps_segstimed, "\t{:segments-updated-rtt/%ju} "
768 "{N:/segment%s updated rtt (of} "
769 "{:segment-update-attempts/%ju} {N:/attempt%s})\n");
770 p(tcps_rexmttimeo, "\t{:retransmit-timeouts/%ju} "
771 "{N:/retransmit timeout%s}\n");
772 p(tcps_timeoutdrop, "\t\t"
773 "{:connections-dropped-by-retransmit-timeout/%ju} "
774 "{N:/connection%s dropped by rexmit timeout}\n");
775 p(tcps_persisttimeo, "\t{:persist-timeout/%ju} "
776 "{N:/persist timeout%s}\n");
777 p(tcps_persistdrop, "\t\t"
778 "{:connections-dropped-by-persist-timeout/%ju} "
779 "{N:/connection%s dropped by persist timeout}\n");
780 p(tcps_finwait2_drops, "\t"
781 "{:connections-dropped-by-finwait2-timeout/%ju} "
782 "{N:/Connection%s (fin_wait_2) dropped because of timeout}\n");
783 p(tcps_keeptimeo, "\t{:keepalive-timeout/%ju} "
784 "{N:/keepalive timeout%s}\n");
785 p(tcps_keepprobe, "\t\t{:keepalive-probes/%ju} "
786 "{N:/keepalive probe%s sent}\n");
787 p(tcps_keepdrops, "\t\t{:connections-dropped-by-keepalives/%ju} "
788 "{N:/connection%s dropped by keepalive}\n");
789 p(tcps_predack, "\t{:ack-header-predictions/%ju} "
790 "{N:/correct ACK header prediction%s}\n");
791 p(tcps_preddat, "\t{:data-packet-header-predictions/%ju} "
792 "{N:/correct data packet header prediction%s}\n");
794 xo_open_container("syncache");
796 p3(tcps_sc_added, "\t{:entries-added/%ju} "
797 "{N:/syncache entr%s added}\n");
798 p1a(tcps_sc_retransmitted, "\t\t{:retransmitted/%ju} "
799 "{N:/retransmitted}\n");
800 p1a(tcps_sc_dupsyn, "\t\t{:duplicates/%ju} {N:/dupsyn}\n");
801 p1a(tcps_sc_dropped, "\t\t{:dropped/%ju} {N:/dropped}\n");
802 p1a(tcps_sc_completed, "\t\t{:completed/%ju} {N:/completed}\n");
803 p1a(tcps_sc_bucketoverflow, "\t\t{:bucket-overflow/%ju} "
804 "{N:/bucket overflow}\n");
805 p1a(tcps_sc_cacheoverflow, "\t\t{:cache-overflow/%ju} "
806 "{N:/cache overflow}\n");
807 p1a(tcps_sc_reset, "\t\t{:reset/%ju} {N:/reset}\n");
808 p1a(tcps_sc_stale, "\t\t{:stale/%ju} {N:/stale}\n");
809 p1a(tcps_sc_aborted, "\t\t{:aborted/%ju} {N:/aborted}\n");
810 p1a(tcps_sc_badack, "\t\t{:bad-ack/%ju} {N:/badack}\n");
811 p1a(tcps_sc_unreach, "\t\t{:unreachable/%ju} {N:/unreach}\n");
812 p(tcps_sc_zonefail, "\t\t{:zone-failures/%ju} {N:/zone failure%s}\n");
813 p(tcps_sc_sendcookie, "\t{:sent-cookies/%ju} {N:/cookie%s sent}\n");
814 p(tcps_sc_recvcookie, "\t{:receivd-cookies/%ju} "
815 "{N:/cookie%s received}\n");
817 xo_close_container("syncache");
819 xo_open_container("hostcache");
821 p3(tcps_hc_added, "\t{:entries-added/%ju} "
822 "{N:/hostcache entr%s added}\n");
823 p1a(tcps_hc_bucketoverflow, "\t\t{:buffer-overflows/%ju} "
824 "{N:/bucket overflow}\n");
826 xo_close_container("hostcache");
828 xo_open_container("sack");
830 p(tcps_sack_recovery_episode, "\t{:recovery-episodes/%ju} "
831 "{N:/SACK recovery episode%s}\n");
832 p(tcps_sack_rexmits, "\t{:segment-retransmits/%ju} "
833 "{N:/segment rexmit%s in SACK recovery episodes}\n");
834 p(tcps_sack_rexmit_bytes, "\t{:byte-retransmits/%ju} "
835 "{N:/byte rexmit%s in SACK recovery episodes}\n");
836 p(tcps_sack_rcv_blocks, "\t{:received-blocks/%ju} "
837 "{N:/SACK option%s (SACK blocks) received}\n");
838 p(tcps_sack_send_blocks, "\t{:sent-option-blocks/%ju} "
839 "{N:/SACK option%s (SACK blocks) sent}\n");
840 p1a(tcps_sack_sboverflow, "\t{:scoreboard-overflows/%ju} "
841 "{N:/SACK scoreboard overflow}\n");
843 xo_close_container("sack");
844 xo_open_container("ecn");
846 p(tcps_ecn_ce, "\t{:ce-packets/%ju} "
847 "{N:/packet%s with ECN CE bit set}\n");
848 p(tcps_ecn_ect0, "\t{:ect0-packets/%ju} "
849 "{N:/packet%s with ECN ECT(0) bit set}\n");
850 p(tcps_ecn_ect1, "\t{:ect1-packets/%ju} "
851 "{N:/packet%s with ECN ECT(1) bit set}\n");
852 p(tcps_ecn_shs, "\t{:handshakes/%ju} "
853 "{N:/successful ECN handshake%s}\n");
854 p(tcps_ecn_rcwnd, "\t{:congestion-reductions/%ju} "
855 "{N:/time%s ECN reduced the congestion window}\n");
861 xo_close_container("ecn");
863 xo_open_container("TCP connection count by state");
864 xo_emit("{T:/TCP connection count by state}:\n");
865 for (int i = 0; i < TCP_NSTATES; i++) {
867 * XXXGL: is there a way in libxo to use %s
868 * in the "content string" of a format
869 * string? I failed to do that, that's why
870 * a temporary buffer is used to construct
871 * format string for xo_emit().
875 if (sflag > 1 && tcps_states[i] == 0)
877 snprintf(fmtbuf, sizeof(fmtbuf), "\t{:%s/%%ju} "
878 "{Np:/connection ,connections} in %s state\n",
879 tcpstates[i], tcpstates[i]);
880 xo_emit(fmtbuf, (uintmax_t )tcps_states[i]);
882 xo_close_container("TCP connection count by state");
884 xo_close_container("tcp");
888 * Dump UDP statistics structure.
891 udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
893 struct udpstat udpstat;
903 if (fetch_stats("net.inet.udp.stats", off, &udpstat,
904 sizeof(udpstat), kread_counters) != 0)
907 xo_open_container("udp");
908 xo_emit("{T:/%s}:\n", name);
910 #define p(f, m) if (udpstat.f || sflag <= 1) \
911 xo_emit("\t" m, (uintmax_t)udpstat.f, plural(udpstat.f))
912 #define p1a(f, m) if (udpstat.f || sflag <= 1) \
913 xo_emit("\t" m, (uintmax_t)udpstat.f)
915 p(udps_ipackets, "{:received-datagrams/%ju} "
916 "{N:/datagram%s received}\n");
917 p1a(udps_hdrops, "{:dropped-incomplete-headers/%ju} "
918 "{N:/with incomplete header}\n");
919 p1a(udps_badlen, "{:dropped-bad-data-length/%ju} "
920 "{N:/with bad data length field}\n");
921 p1a(udps_badsum, "{:dropped-bad-checksum/%ju} "
922 "{N:/with bad checksum}\n");
923 p1a(udps_nosum, "{:dropped-no-checksum/%ju} "
924 "{N:/with no checksum}\n");
925 p1a(udps_noport, "{:dropped-no-socket/%ju} "
926 "{N:/dropped due to no socket}\n");
927 p(udps_noportbcast, "{:dropped-broadcast-multicast/%ju} "
928 "{N:/broadcast\\/multicast datagram%s undelivered}\n");
929 p1a(udps_fullsock, "{:dropped-full-socket-buffer/%ju} "
930 "{N:/dropped due to full socket buffers}\n");
931 p1a(udpps_pcbhashmiss, "{:not-for-hashed-pcb/%ju} "
932 "{N:/not for hashed pcb}\n");
933 delivered = udpstat.udps_ipackets -
934 udpstat.udps_hdrops -
935 udpstat.udps_badlen -
936 udpstat.udps_badsum -
937 udpstat.udps_noport -
938 udpstat.udps_noportbcast -
939 udpstat.udps_fullsock;
940 if (delivered || sflag <= 1)
941 xo_emit("\t{:delivered-packets/%ju} {N:/delivered}\n",
942 (uint64_t)delivered);
943 p(udps_opackets, "{:output-packets/%ju} {N:/datagram%s output}\n");
944 /* the next statistic is cumulative in udps_noportbcast */
945 p(udps_filtermcast, "{:multicast-source-filter-matches/%ju} "
946 "{N:/time%s multicast source filter matched}\n");
949 xo_close_container("udp");
953 * Dump CARP statistics structure.
956 carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
958 struct carpstats carpstat;
960 if (fetch_stats("net.inet.carp.stats", off, &carpstat,
961 sizeof(carpstat), kread_counters) != 0)
964 xo_open_container(name);
965 xo_emit("{T:/%s}:\n", name);
967 #define p(f, m) if (carpstat.f || sflag <= 1) \
968 xo_emit(m, (uintmax_t)carpstat.f, plural(carpstat.f))
969 #define p2(f, m) if (carpstat.f || sflag <= 1) \
970 xo_emit(m, (uintmax_t)carpstat.f)
972 p(carps_ipackets, "\t{:received-inet-packets/%ju} "
973 "{N:/packet%s received (IPv4)}\n");
974 p(carps_ipackets6, "\t{:received-inet6-packets/%ju} "
975 "{N:/packet%s received (IPv6)}\n");
976 p(carps_badttl, "\t\t{:dropped-wrong-ttl/%ju} "
977 "{N:/packet%s discarded for wrong TTL}\n");
978 p(carps_hdrops, "\t\t{:dropped-short-header/%ju} "
979 "{N:/packet%s shorter than header}\n");
980 p(carps_badsum, "\t\t{:dropped-bad-checksum/%ju} "
981 "{N:/discarded for bad checksum%s}\n");
982 p(carps_badver, "\t\t{:dropped-bad-version/%ju} "
983 "{N:/discarded packet%s with a bad version}\n");
984 p2(carps_badlen, "\t\t{:dropped-short-packet/%ju} "
985 "{N:/discarded because packet too short}\n");
986 p2(carps_badauth, "\t\t{:dropped-bad-authentication/%ju} "
987 "{N:/discarded for bad authentication}\n");
988 p2(carps_badvhid, "\t\t{:dropped-bad-vhid/%ju} "
989 "{N:/discarded for bad vhid}\n");
990 p2(carps_badaddrs, "\t\t{:dropped-bad-address-list/%ju} "
991 "{N:/discarded because of a bad address list}\n");
992 p(carps_opackets, "\t{:sent-inet-packets/%ju} "
993 "{N:/packet%s sent (IPv4)}\n");
994 p(carps_opackets6, "\t{:sent-inet6-packets/%ju} "
995 "{N:/packet%s sent (IPv6)}\n");
996 p2(carps_onomem, "\t\t{:send-failed-memory-error/%ju} "
997 "{N:/send failed due to mbuf memory error}\n");
999 p(carps_ostates, "\t\t{:send-state-updates/%s} "
1000 "{N:/state update%s sent}\n");
1004 xo_close_container(name);
1008 * Dump IP statistics structure.
1011 ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1013 struct ipstat ipstat;
1015 if (fetch_stats("net.inet.ip.stats", off, &ipstat,
1016 sizeof(ipstat), kread_counters) != 0)
1019 xo_open_container(name);
1020 xo_emit("{T:/%s}:\n", name);
1022 #define p(f, m) if (ipstat.f || sflag <= 1) \
1023 xo_emit(m, (uintmax_t )ipstat.f, plural(ipstat.f))
1024 #define p1a(f, m) if (ipstat.f || sflag <= 1) \
1025 xo_emit(m, (uintmax_t )ipstat.f)
1027 p(ips_total, "\t{:received-packets/%ju} "
1028 "{N:/total packet%s received}\n");
1029 p(ips_badsum, "\t{:dropped-bad-checksum/%ju} "
1030 "{N:/bad header checksum%s}\n");
1031 p1a(ips_toosmall, "\t{:dropped-below-minimum-size/%ju} "
1032 "{N:/with size smaller than minimum}\n");
1033 p1a(ips_tooshort, "\t{:dropped-short-packets/%ju} "
1034 "{N:/with data size < data length}\n");
1035 p1a(ips_toolong, "\t{:dropped-too-long/%ju} "
1036 "{N:/with ip length > max ip packet size}\n");
1037 p1a(ips_badhlen, "\t{:dropped-short-header-length/%ju} "
1038 "{N:/with header length < data size}\n");
1039 p1a(ips_badlen, "\t{:dropped-short-data/%ju} "
1040 "{N:/with data length < header length}\n");
1041 p1a(ips_badoptions, "\t{:dropped-bad-options/%ju} "
1042 "{N:/with bad options}\n");
1043 p1a(ips_badvers, "\t{:dropped-bad-version/%ju} "
1044 "{N:/with incorrect version number}\n");
1045 p(ips_fragments, "\t{:received-fragments/%ju} "
1046 "{N:/fragment%s received}\n");
1047 p(ips_fragdropped, "\t{:dropped-fragments/%ju} "
1048 "{N:/fragment%s dropped (dup or out of space)}\n");
1049 p(ips_fragtimeout, "\t{:dropped-fragments-after-timeout/%ju} "
1050 "{N:/fragment%s dropped after timeout}\n");
1051 p(ips_reassembled, "\t{:reassembled-packets/%ju} "
1052 "{N:/packet%s reassembled ok}\n");
1053 p(ips_delivered, "\t{:received-local-packets/%ju} "
1054 "{N:/packet%s for this host}\n");
1055 p(ips_noproto, "\t{:dropped-unknown-protocol/%ju} "
1056 "{N:/packet%s for unknown\\/unsupported protocol}\n");
1057 p(ips_forward, "\t{:forwarded-packets/%ju} "
1058 "{N:/packet%s forwarded}");
1059 p(ips_fastforward, " ({:fast-forwarded-packets/%ju} "
1060 "{N:/packet%s fast forwarded})");
1061 if (ipstat.ips_forward || sflag <= 1)
1063 p(ips_cantforward, "\t{:packets-cannot-forward/%ju} "
1064 "{N:/packet%s not forwardable}\n");
1065 p(ips_notmember, "\t{:received-unknown-multicast-group/%ju} "
1066 "{N:/packet%s received for unknown multicast group}\n");
1067 p(ips_redirectsent, "\t{:redirects-sent/%ju} "
1068 "{N:/redirect%s sent}\n");
1069 p(ips_localout, "\t{:sent-packets/%ju} "
1070 "{N:/packet%s sent from this host}\n");
1071 p(ips_rawout, "\t{:send-packets-fabricated-header/%ju} "
1072 "{N:/packet%s sent with fabricated ip header}\n");
1073 p(ips_odropped, "\t{:discard-no-mbufs/%ju} "
1074 "{N:/output packet%s dropped due to no bufs, etc.}\n");
1075 p(ips_noroute, "\t{:discard-no-route/%ju} "
1076 "{N:/output packet%s discarded due to no route}\n");
1077 p(ips_fragmented, "\t{:sent-fragments/%ju} "
1078 "{N:/output datagram%s fragmented}\n");
1079 p(ips_ofragments, "\t{:fragments-created/%ju} "
1080 "{N:/fragment%s created}\n");
1081 p(ips_cantfrag, "\t{:discard-cannot-fragment/%ju} "
1082 "{N:/datagram%s that can't be fragmented}\n");
1083 p(ips_nogif, "\t{:discard-tunnel-no-gif/%ju} "
1084 "{N:/tunneling packet%s that can't find gif}\n");
1085 p(ips_badaddr, "\t{:discard-bad-address/%ju} "
1086 "{N:/datagram%s with bad address in header}\n");
1089 xo_close_container(name);
1093 * Dump ARP statistics structure.
1096 arp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1098 struct arpstat arpstat;
1100 if (fetch_stats("net.link.ether.arp.stats", off, &arpstat,
1101 sizeof(arpstat), kread_counters) != 0)
1104 xo_open_container(name);
1105 xo_emit("{T:/%s}:\n", name);
1107 #define p(f, m) if (arpstat.f || sflag <= 1) \
1108 xo_emit("\t" m, (uintmax_t)arpstat.f, plural(arpstat.f))
1109 #define p2(f, m) if (arpstat.f || sflag <= 1) \
1110 xo_emit("\t" m, (uintmax_t)arpstat.f, pluralies(arpstat.f))
1112 p(txrequests, "{:sent-requests/%ju} {N:/ARP request%s sent}\n");
1113 p2(txreplies, "{:sent-replies/%ju} {N:/ARP repl%s sent}\n");
1114 p(rxrequests, "{:received-requests/%ju} "
1115 "{N:/ARP request%s received}\n");
1116 p2(rxreplies, "{:received-replies/%ju} "
1117 "{N:/ARP repl%s received}\n");
1118 p(received, "{:received-packers/%ju} "
1119 "{N:/ARP packet%s received}\n");
1120 p(dropped, "{:dropped-no-entry/%ju} "
1121 "{N:/total packet%s dropped due to no ARP entry}\n");
1122 p(timeouts, "{:entries-timeout/%ju} "
1123 "{N:/ARP entry%s timed out}\n");
1124 p(dupips, "{:dropped-duplicate-address/%ju} "
1125 "{N:/Duplicate IP%s seen}\n");
1128 xo_close_container(name);
1133 static const char *icmpnames[ICMP_MAXTYPE + 1] = {
1134 "echo reply", /* RFC 792 */
1137 "destination unreachable", /* RFC 792 */
1138 "source quench", /* RFC 792 */
1139 "routing redirect", /* RFC 792 */
1142 "echo", /* RFC 792 */
1143 "router advertisement", /* RFC 1256 */
1144 "router solicitation", /* RFC 1256 */
1145 "time exceeded", /* RFC 792 */
1146 "parameter problem", /* RFC 792 */
1147 "time stamp", /* RFC 792 */
1148 "time stamp reply", /* RFC 792 */
1149 "information request", /* RFC 792 */
1150 "information request reply", /* RFC 792 */
1151 "address mask request", /* RFC 950 */
1152 "address mask reply", /* RFC 950 */
1164 "icmp traceroute", /* RFC 1393 */
1165 "datagram conversion error", /* RFC 1475 */
1166 "mobile host redirect",
1167 "IPv6 where-are-you",
1169 "mobile registration req",
1170 "mobile registration reply",
1171 "domain name request", /* RFC 1788 */
1172 "domain name reply", /* RFC 1788 */
1174 "icmp photuris", /* RFC 2521 */
1178 * Dump ICMP statistics.
1181 icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1183 struct icmpstat icmpstat;
1187 if (fetch_stats("net.inet.icmp.stats", off, &icmpstat,
1188 sizeof(icmpstat), kread_counters) != 0)
1191 xo_open_container(name);
1192 xo_emit("{T:/%s}:\n", name);
1194 #define p(f, m) if (icmpstat.f || sflag <= 1) \
1195 xo_emit(m, icmpstat.f, plural(icmpstat.f))
1196 #define p1a(f, m) if (icmpstat.f || sflag <= 1) \
1197 xo_emit(m, icmpstat.f)
1198 #define p2(f, m) if (icmpstat.f || sflag <= 1) \
1199 xo_emit(m, icmpstat.f, plurales(icmpstat.f))
1201 p(icps_error, "\t{:icmp-calls/%lu} "
1202 "{N:/call%s to icmp_error}\n");
1203 p(icps_oldicmp, "\t{:errors-not-from-message/%lu} "
1204 "{N:/error%s not generated in response to an icmp message}\n");
1206 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) {
1207 if (icmpstat.icps_outhist[i] != 0) {
1209 xo_open_list("output-histogram");
1210 xo_emit("\tOutput histogram:\n");
1213 xo_open_instance("output-histogram");
1214 if (icmpnames[i] != NULL)
1215 xo_emit("\t\t{k:name/%s}: {:count/%lu}\n",
1216 icmpnames[i], icmpstat.icps_outhist[i]);
1218 xo_emit("\t\tunknown ICMP #{k:name/%d}: "
1220 i, icmpstat.icps_outhist[i]);
1221 xo_close_instance("output-histogram");
1225 xo_close_list("output-histogram");
1227 p(icps_badcode, "\t{:dropped-bad-code/%lu} "
1228 "{N:/message%s with bad code fields}\n");
1229 p(icps_tooshort, "\t{:dropped-too-short/%lu} "
1230 "{N:/message%s less than the minimum length}\n");
1231 p(icps_checksum, "\t{:dropped-bad-checksum/%lu} "
1232 "{N:/message%s with bad checksum}\n");
1233 p(icps_badlen, "\t{:dropped-bad-length/%lu} "
1234 "{N:/message%s with bad length}\n");
1235 p1a(icps_bmcastecho, "\t{:dropped-multicast-echo/%lu} "
1236 "{N:/multicast echo requests ignored}\n");
1237 p1a(icps_bmcasttstamp, "\t{:dropped-multicast-timestamp/%lu} "
1238 "{N:/multicast timestamp requests ignored}\n");
1240 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) {
1241 if (icmpstat.icps_inhist[i] != 0) {
1243 xo_open_list("input-histogram");
1244 xo_emit("\tInput histogram:\n");
1247 xo_open_instance("input-histogram");
1248 if (icmpnames[i] != NULL)
1249 xo_emit("\t\t{k:name/%s}: {:count/%lu}\n",
1251 icmpstat.icps_inhist[i]);
1254 "\t\tunknown ICMP #{k:name/%d}: {:count/%lu}\n",
1255 i, icmpstat.icps_inhist[i]);
1256 xo_close_instance("input-histogram");
1260 xo_close_list("input-histogram");
1262 p(icps_reflect, "\t{:sent-packets/%lu} "
1263 "{N:/message response%s generated}\n");
1264 p2(icps_badaddr, "\t{:discard-invalid-return-address/%lu} "
1265 "{N:/invalid return address%s}\n");
1266 p(icps_noroute, "\t{:discard-no-route/%lu} "
1267 "{N:/no return route%s}\n");
1273 if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) <
1276 xo_emit("\tICMP address mask responses are "
1277 "{q:icmp-address-responses/%sabled}\n", i ? "en" : "dis");
1280 xo_close_container(name);
1284 * Dump IGMP statistics structure.
1287 igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1289 struct igmpstat igmpstat;
1291 if (fetch_stats("net.inet.igmp.stats", 0, &igmpstat,
1292 sizeof(igmpstat), kread) != 0)
1295 if (igmpstat.igps_version != IGPS_VERSION_3) {
1296 xo_warnx("%s: version mismatch (%d != %d)", __func__,
1297 igmpstat.igps_version, IGPS_VERSION_3);
1299 if (igmpstat.igps_len != IGPS_VERSION3_LEN) {
1300 xo_warnx("%s: size mismatch (%d != %d)", __func__,
1301 igmpstat.igps_len, IGPS_VERSION3_LEN);
1304 xo_open_container(name);
1305 xo_emit("{T:/%s}:\n", name);
1307 #define p64(f, m) if (igmpstat.f || sflag <= 1) \
1308 xo_emit(m, (uintmax_t) igmpstat.f, plural(igmpstat.f))
1309 #define py64(f, m) if (igmpstat.f || sflag <= 1) \
1310 xo_emit(m, (uintmax_t) igmpstat.f, pluralies(igmpstat.f))
1312 p64(igps_rcv_total, "\t{:received-messages/%ju} "
1313 "{N:/message%s received}\n");
1314 p64(igps_rcv_tooshort, "\t{:dropped-too-short/%ju} "
1315 "{N:/message%s received with too few bytes}\n");
1316 p64(igps_rcv_badttl, "\t{:dropped-wrong-ttl/%ju} "
1317 "{N:/message%s received with wrong TTL}\n");
1318 p64(igps_rcv_badsum, "\t{:dropped-bad-checksum/%ju} "
1319 "{N:/message%s received with bad checksum}\n");
1320 py64(igps_rcv_v1v2_queries, "\t{:received-membership-queries/%ju} "
1321 "{N:/V1\\/V2 membership quer%s received}\n");
1322 py64(igps_rcv_v3_queries, "\t{:received-v3-membership-queries/%ju} "
1323 "{N:/V3 membership quer%s received}\n");
1324 py64(igps_rcv_badqueries, "\t{:dropped-membership-queries/%ju} "
1325 "{N:/membership quer%s received with invalid field(s)}\n");
1326 py64(igps_rcv_gen_queries, "\t{:received-general-queries/%ju} "
1327 "{N:/general quer%s received}\n");
1328 py64(igps_rcv_group_queries, "\t{:received-group-queries/%ju} "
1329 "{N:/group quer%s received}\n");
1330 py64(igps_rcv_gsr_queries, "\t{:received-group-source-queries/%ju} "
1331 "{N:/group-source quer%s received}\n");
1332 py64(igps_drop_gsr_queries, "\t{:dropped-group-source-queries/%ju} "
1333 "{N:/group-source quer%s dropped}\n");
1334 p64(igps_rcv_reports, "\t{:received-membership-requests/%ju} "
1335 "{N:/membership report%s received}\n");
1336 p64(igps_rcv_badreports, "\t{:dropped-membership-reports/%ju} "
1337 "{N:/membership report%s received with invalid field(s)}\n");
1338 p64(igps_rcv_ourreports, "\t"
1339 "{:received-membership-reports-matching/%ju} "
1340 "{N:/membership report%s received for groups to which we belong}"
1342 p64(igps_rcv_nora, "\t{:received-v3-reports-no-router-alert/%ju} "
1343 "{N:/V3 report%s received without Router Alert}\n");
1344 p64(igps_snd_reports, "\t{:sent-membership-reports/%ju} "
1345 "{N:/membership report%s sent}\n");
1348 xo_close_container(name);
1352 * Dump PIM statistics structure.
1355 pim_stats(u_long off __unused, const char *name, int af1 __unused,
1358 struct pimstat pimstat;
1360 if (fetch_stats("net.inet.pim.stats", off, &pimstat,
1361 sizeof(pimstat), kread_counters) != 0)
1364 xo_open_container(name);
1365 xo_emit("{T:/%s}:\n", name);
1367 #define p(f, m) if (pimstat.f || sflag <= 1) \
1368 xo_emit(m, (uintmax_t)pimstat.f, plural(pimstat.f))
1369 #define py(f, m) if (pimstat.f || sflag <= 1) \
1370 xo_emit(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
1372 p(pims_rcv_total_msgs, "\t{:received-messages/%ju} "
1373 "{N:/message%s received}\n");
1374 p(pims_rcv_total_bytes, "\t{:received-bytes/%ju} "
1375 "{N:/byte%s received}\n");
1376 p(pims_rcv_tooshort, "\t{:dropped-too-short/%ju} "
1377 "{N:/message%s received with too few bytes}\n");
1378 p(pims_rcv_badsum, "\t{:dropped-bad-checksum/%ju} "
1379 "{N:/message%s received with bad checksum}\n");
1380 p(pims_rcv_badversion, "\t{:dropped-bad-version/%ju} "
1381 "{N:/message%s received with bad version}\n");
1382 p(pims_rcv_registers_msgs, "\t{:received-data-register-messages/%ju} "
1383 "{N:/data register message%s received}\n");
1384 p(pims_rcv_registers_bytes, "\t{:received-data-register-bytes/%ju} "
1385 "{N:/data register byte%s received}\n");
1386 p(pims_rcv_registers_wrongiif, "\t"
1387 "{:received-data-register-wrong-interface/%ju} "
1388 "{N:/data register message%s received on wrong iif}\n");
1389 p(pims_rcv_badregisters, "\t{:received-bad-registers/%ju} "
1390 "{N:/bad register%s received}\n");
1391 p(pims_snd_registers_msgs, "\t{:sent-data-register-messages/%ju} "
1392 "{N:/data register message%s sent}\n");
1393 p(pims_snd_registers_bytes, "\t{:sent-data-register-bytes/%ju} "
1394 "{N:/data register byte%s sent}\n");
1397 xo_close_container(name);
1401 * Pretty print an Internet address (net address + port).
1404 inetprint(const char *container, struct in_addr *in, int port,
1405 const char *proto, int num_port, const int af1)
1407 struct servent *sp = 0;
1412 xo_open_container(container);
1415 sprintf(line, "%s.", inetname(in));
1417 sprintf(line, "%.*s.", (Aflag && !num_port) ? 12 : 16, inetname(in));
1418 cp = strchr(line, '\0');
1419 if (!num_port && port)
1420 sp = getservbyport((int)port, proto);
1421 if (sp || port == 0)
1422 sprintf(cp, "%.15s ", sp ? sp->s_name : "*");
1424 sprintf(cp, "%d ", ntohs((u_short)port));
1425 width = (Aflag && !Wflag) ? 18 :
1426 ((!Wflag || af1 == AF_INET) ? 22 : 45);
1428 xo_emit("{d:target/%-*s} ", width, line);
1430 xo_emit("{d:target/%-*.*s} ", width, width, line);
1432 int alen = cp - line - 1, plen = strlen(cp) - 1;
1433 xo_emit("{e:address/%*.*s}{e:port/%*.*s}", alen, alen, line, plen,
1437 xo_close_container(container);
1441 * Construct an Internet address representation.
1442 * If numeric_addr has been supplied, give
1443 * numeric value, otherwise try for symbolic name.
1446 inetname(struct in_addr *inp)
1449 static char line[MAXHOSTNAMELEN];
1454 if (!numeric_addr && inp->s_addr != INADDR_ANY) {
1455 int net = inet_netof(*inp);
1456 int lna = inet_lnaof(*inp);
1458 if (lna == INADDR_ANY) {
1459 np = getnetbyaddr(net, AF_INET);
1464 hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
1467 trimdomain(cp, strlen(cp));
1471 if (inp->s_addr == INADDR_ANY)
1474 strlcpy(line, cp, sizeof(line));
1476 inp->s_addr = ntohl(inp->s_addr);
1477 #define C(x) ((u_int)((x) & 0xff))
1478 sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
1479 C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));