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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)inet.c 8.5 (Berkeley) 5/24/95";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/queue.h>
45 #include <sys/domain.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sysctl.h>
49 #include <sys/protosw.h>
51 #include <net/route.h>
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip_carp.h>
57 #include <netinet/ip6.h>
59 #include <netinet/in_pcb.h>
60 #include <netinet/ip_icmp.h>
61 #include <netinet/icmp_var.h>
62 #include <netinet/igmp_var.h>
63 #include <netinet/ip_var.h>
64 #include <netinet/pim_var.h>
65 #include <netinet/tcp.h>
66 #include <netinet/tcpip.h>
67 #include <netinet/tcp_seq.h>
69 #include <netinet/tcp_fsm.h>
70 #include <netinet/tcp_timer.h>
71 #include <netinet/tcp_var.h>
72 #include <netinet/tcp_debug.h>
73 #include <netinet/udp.h>
74 #include <netinet/udp_var.h>
76 #include <arpa/inet.h>
88 char *inetname (struct in_addr *);
89 void inetprint (struct in_addr *, int, const char *, int);
91 static int udp_done, tcp_done;
95 pcblist_sysctl(int proto, char **bufp, int istcp)
103 mibvar = "net.inet.tcp.pcblist";
106 mibvar = "net.inet.udp.pcblist";
109 mibvar = "net.inet.divert.pcblist";
112 mibvar = "net.inet.raw.pcblist";
117 if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
119 warn("sysctl: %s", mibvar);
122 if ((buf = malloc(len)) == 0) {
123 warnx("malloc %lu bytes", (u_long)len);
126 if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
127 warn("sysctl: %s", mibvar);
136 * Copied directly from uipc_socket2.c. We leave out some fields that are in
137 * nested structures that aren't used to avoid extra work.
140 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
142 xsb->sb_cc = sb->sb_cc;
143 xsb->sb_hiwat = sb->sb_hiwat;
144 xsb->sb_mbcnt = sb->sb_mbcnt;
145 xsb->sb_mbmax = sb->sb_mbmax;
146 xsb->sb_lowat = sb->sb_lowat;
147 xsb->sb_flags = sb->sb_flags;
148 xsb->sb_timeo = sb->sb_timeo;
152 sotoxsocket(struct socket *so, struct xsocket *xso)
154 struct protosw proto;
155 struct domain domain;
157 bzero(xso, sizeof *xso);
158 xso->xso_len = sizeof *xso;
160 xso->so_type = so->so_type;
161 xso->so_options = so->so_options;
162 xso->so_linger = so->so_linger;
163 xso->so_state = so->so_state;
164 xso->so_pcb = so->so_pcb;
165 if (kread((uintptr_t)so->so_proto, &proto, sizeof(proto)) != 0)
167 xso->xso_protocol = proto.pr_protocol;
168 if (kread((uintptr_t)proto.pr_domain, &domain, sizeof(domain)) != 0)
170 xso->xso_family = domain.dom_family;
171 xso->so_qlen = so->so_qlen;
172 xso->so_incqlen = so->so_incqlen;
173 xso->so_qlimit = so->so_qlimit;
174 xso->so_timeo = so->so_timeo;
175 xso->so_error = so->so_error;
176 xso->so_oobmark = so->so_oobmark;
177 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
178 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
183 pcblist_kvm(u_long off, char **bufp, int istcp)
185 struct inpcbinfo pcbinfo;
186 struct inpcbhead listhead;
198 kread(off, &pcbinfo, sizeof(pcbinfo));
200 len = 2 * sizeof(xig) +
201 (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) *
202 sizeof(struct xtcpcb);
204 len = 2 * sizeof(xig) +
205 (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) *
206 sizeof(struct xinpcb);
207 if ((buf = malloc(len)) == 0) {
208 warnx("malloc %lu bytes", (u_long)len);
213 #define COPYOUT(obj, size) do { \
214 if (len < (size)) { \
215 warnx("buffer size exceeded"); \
218 bcopy((obj), p, (size)); \
223 #define KREAD(off, buf, len) do { \
224 if (kread((uintptr_t)(off), (buf), (len)) != 0) \
228 /* Write out header. */
229 xig.xig_len = sizeof xig;
230 xig.xig_count = pcbinfo.ipi_count;
231 xig.xig_gen = pcbinfo.ipi_gencnt;
233 COPYOUT(&xig, sizeof xig);
235 /* Walk the PCB list. */
236 xt.xt_len = sizeof xt;
237 xi.xi_len = sizeof xi;
242 KREAD(pcbinfo.ipi_listhead, &listhead, sizeof(listhead));
243 LIST_FOREACH(inp, &listhead, inp_list) {
245 KREAD(inp, &xt.xt_inp, sizeof(*inp));
248 KREAD(inp, &xi.xi_inp, sizeof(*inp));
252 if (inp->inp_gencnt > pcbinfo.ipi_gencnt)
256 if (inp->inp_ppcb == NULL)
257 bzero(&xt.xt_tp, sizeof xt.xt_tp);
258 else if (inp->inp_vflag & INP_TIMEWAIT) {
259 bzero(&xt.xt_tp, sizeof xt.xt_tp);
260 xt.xt_tp.t_state = TCPS_TIME_WAIT;
262 KREAD(inp->inp_ppcb, &xt.xt_tp,
265 if (inp->inp_socket) {
266 KREAD(inp->inp_socket, &so, sizeof(so));
267 if (sotoxsocket(&so, xso) != 0)
270 bzero(xso, sizeof(*xso));
272 xso->xso_protocol = IPPROTO_TCP;
275 COPYOUT(&xt, sizeof xt);
277 COPYOUT(&xi, sizeof xi);
280 /* Reread the pcbinfo and write out the footer. */
281 kread(off, &pcbinfo, sizeof(pcbinfo));
282 xig.xig_count = pcbinfo.ipi_count;
283 xig.xig_gen = pcbinfo.ipi_gencnt;
284 COPYOUT(&xig, sizeof xig);
297 * Print a summary of connections related to an Internet
298 * protocol. For TCP, also give state of connection.
299 * Listening processes (aflag) are suppressed unless the
300 * -a (all) flag is specified.
303 protopr(u_long off, const char *name, int af1, int proto)
306 static int first = 1;
309 struct tcpcb *tp = NULL;
311 struct xinpgen *xig, *oxig;
335 if (!pcblist_sysctl(proto, &buf, istcp))
338 if (!pcblist_kvm(off, &buf, istcp))
342 oxig = xig = (struct xinpgen *)buf;
343 for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
344 xig->xig_len > sizeof(struct xinpgen);
345 xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
347 tp = &((struct xtcpcb *)xig)->xt_tp;
348 inp = &((struct xtcpcb *)xig)->xt_inp;
349 so = &((struct xtcpcb *)xig)->xt_socket;
351 inp = &((struct xinpcb *)xig)->xi_inp;
352 so = &((struct xinpcb *)xig)->xi_socket;
355 /* Ignore sockets for protocols other than the desired one. */
356 if (so->xso_protocol != proto)
359 /* Ignore PCBs which were freed during copyout. */
360 if (inp->inp_gencnt > oxig->xig_gen)
363 if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
365 || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
367 || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
377 (istcp && tp->t_state == TCPS_LISTEN)
378 || (af1 == AF_INET &&
379 inet_lnaof(inp->inp_laddr) == INADDR_ANY)
381 || (af1 == AF_INET6 &&
382 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
384 || (af1 == AF_UNSPEC &&
385 (((inp->inp_vflag & INP_IPV4) != 0 &&
386 inet_lnaof(inp->inp_laddr) == INADDR_ANY)
388 || ((inp->inp_vflag & INP_IPV6) != 0 &&
389 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
397 printf("Active Internet connections");
399 printf(" (including servers)");
402 "Current listen queue sizes (qlen/incqlen/maxqlen)");
405 printf("%-8.8s ", "Tcpcb");
407 printf("%-5.5s %-14.14s %-22.22s\n",
408 "Proto", "Listen", "Local Address");
410 printf((Aflag && !Wflag) ?
411 "%-5.5s %-6.6s %-6.6s %-18.18s %-18.18s %s\n" :
412 "%-5.5s %-6.6s %-6.6s %-22.22s %-22.22s %s\n",
413 "Proto", "Recv-Q", "Send-Q",
414 "Local Address", "Foreign Address",
418 if (Lflag && so->so_qlimit == 0)
422 printf("%8lx ", (u_long)inp->inp_ppcb);
424 printf("%8lx ", (u_long)so->so_pcb);
427 if ((inp->inp_vflag & INP_IPV6) != 0)
428 vchar = ((inp->inp_vflag & INP_IPV4) != 0)
432 vchar = ((inp->inp_vflag & INP_IPV4) != 0)
434 printf("%-3.3s%-2.2s ", name, vchar);
438 snprintf(buf1, 15, "%d/%d/%d", so->so_qlen,
439 so->so_incqlen, so->so_qlimit);
440 printf("%-14.14s ", buf1);
447 if (inp->inp_vflag & INP_IPV4) {
448 inetprint(&inp->inp_laddr, (int)inp->inp_lport,
451 inetprint(&inp->inp_faddr,
452 (int)inp->inp_fport, name, 1);
455 else if (inp->inp_vflag & INP_IPV6) {
456 inet6print(&inp->in6p_laddr,
457 (int)inp->inp_lport, name, 1);
459 inet6print(&inp->in6p_faddr,
460 (int)inp->inp_fport, name, 1);
461 } /* else nothing printed now */
463 } else if (inp->inp_flags & INP_ANONPORT) {
464 if (inp->inp_vflag & INP_IPV4) {
465 inetprint(&inp->inp_laddr, (int)inp->inp_lport,
468 inetprint(&inp->inp_faddr,
469 (int)inp->inp_fport, name, 0);
472 else if (inp->inp_vflag & INP_IPV6) {
473 inet6print(&inp->in6p_laddr,
474 (int)inp->inp_lport, name, 1);
476 inet6print(&inp->in6p_faddr,
477 (int)inp->inp_fport, name, 0);
478 } /* else nothing printed now */
481 if (inp->inp_vflag & INP_IPV4) {
482 inetprint(&inp->inp_laddr, (int)inp->inp_lport,
485 inetprint(&inp->inp_faddr,
486 (int)inp->inp_fport, name,
491 else if (inp->inp_vflag & INP_IPV6) {
492 inet6print(&inp->in6p_laddr,
493 (int)inp->inp_lport, name, 0);
495 inet6print(&inp->in6p_faddr,
496 (int)inp->inp_fport, name,
499 } /* else nothing printed now */
502 if (istcp && !Lflag) {
503 if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
504 printf("%d", tp->t_state);
506 printf("%s", tcpstates[tp->t_state]);
507 #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
508 /* Show T/TCP `hidden state' */
509 if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
511 #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
516 if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
517 if (oxig->xig_count > xig->xig_count) {
518 printf("Some %s sockets may have been deleted.\n",
520 } else if (oxig->xig_count < xig->xig_count) {
521 printf("Some %s sockets may have been created.\n",
524 printf("Some %s sockets may have been created or deleted.\n",
532 * Dump TCP statistics structure.
535 tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
537 struct tcpstat tcpstat, zerostat;
538 size_t len = sizeof tcpstat;
549 memset(&zerostat, 0, len);
550 if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &len,
551 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
552 warn("sysctl: net.inet.tcp.stats");
556 kread(off, &tcpstat, len);
558 printf ("%s:\n", name);
560 #define p(f, m) if (tcpstat.f || sflag <= 1) \
561 printf(m, tcpstat.f, plural(tcpstat.f))
562 #define p1a(f, m) if (tcpstat.f || sflag <= 1) \
564 #define p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
565 printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2))
566 #define p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
567 printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2)
568 #define p3(f, m) if (tcpstat.f || sflag <= 1) \
569 printf(m, tcpstat.f, plurales(tcpstat.f))
571 p(tcps_sndtotal, "\t%lu packet%s sent\n");
572 p2(tcps_sndpack,tcps_sndbyte,
573 "\t\t%lu data packet%s (%lu byte%s)\n");
574 p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
575 "\t\t%lu data packet%s (%lu byte%s) retransmitted\n");
577 "\t\t%lu data packet%s unnecessarily retransmitted\n");
578 p(tcps_mturesent, "\t\t%lu resend%s initiated by MTU discovery\n");
579 p2a(tcps_sndacks, tcps_delack,
580 "\t\t%lu ack-only packet%s (%lu delayed)\n");
581 p(tcps_sndurg, "\t\t%lu URG only packet%s\n");
582 p(tcps_sndprobe, "\t\t%lu window probe packet%s\n");
583 p(tcps_sndwinup, "\t\t%lu window update packet%s\n");
584 p(tcps_sndctrl, "\t\t%lu control packet%s\n");
585 p(tcps_rcvtotal, "\t%lu packet%s received\n");
586 p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t%lu ack%s (for %lu byte%s)\n");
587 p(tcps_rcvdupack, "\t\t%lu duplicate ack%s\n");
588 p(tcps_rcvacktoomuch, "\t\t%lu ack%s for unsent data\n");
589 p2(tcps_rcvpack, tcps_rcvbyte,
590 "\t\t%lu packet%s (%lu byte%s) received in-sequence\n");
591 p2(tcps_rcvduppack, tcps_rcvdupbyte,
592 "\t\t%lu completely duplicate packet%s (%lu byte%s)\n");
593 p(tcps_pawsdrop, "\t\t%lu old duplicate packet%s\n");
594 p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
595 "\t\t%lu packet%s with some dup. data (%lu byte%s duped)\n");
596 p2(tcps_rcvoopack, tcps_rcvoobyte,
597 "\t\t%lu out-of-order packet%s (%lu byte%s)\n");
598 p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
599 "\t\t%lu packet%s (%lu byte%s) of data after window\n");
600 p(tcps_rcvwinprobe, "\t\t%lu window probe%s\n");
601 p(tcps_rcvwinupd, "\t\t%lu window update packet%s\n");
602 p(tcps_rcvafterclose, "\t\t%lu packet%s received after close\n");
603 p(tcps_rcvbadsum, "\t\t%lu discarded for bad checksum%s\n");
604 p(tcps_rcvbadoff, "\t\t%lu discarded for bad header offset field%s\n");
605 p1a(tcps_rcvshort, "\t\t%lu discarded because packet too short\n");
606 p1a(tcps_rcvmemdrop, "\t\t%lu discarded due to memory problems\n");
607 p(tcps_connattempt, "\t%lu connection request%s\n");
608 p(tcps_accepts, "\t%lu connection accept%s\n");
609 p(tcps_badsyn, "\t%lu bad connection attempt%s\n");
610 p(tcps_listendrop, "\t%lu listen queue overflow%s\n");
611 p(tcps_badrst, "\t%lu ignored RSTs in the window%s\n");
612 p(tcps_connects, "\t%lu connection%s established (including accepts)\n");
613 p2(tcps_closed, tcps_drops,
614 "\t%lu connection%s closed (including %lu drop%s)\n");
615 p(tcps_cachedrtt, "\t\t%lu connection%s updated cached RTT on close\n");
617 "\t\t%lu connection%s updated cached RTT variance on close\n");
618 p(tcps_cachedssthresh,
619 "\t\t%lu connection%s updated cached ssthresh on close\n");
620 p(tcps_conndrops, "\t%lu embryonic connection%s dropped\n");
621 p2(tcps_rttupdated, tcps_segstimed,
622 "\t%lu segment%s updated rtt (of %lu attempt%s)\n");
623 p(tcps_rexmttimeo, "\t%lu retransmit timeout%s\n");
624 p(tcps_timeoutdrop, "\t\t%lu connection%s dropped by rexmit timeout\n");
625 p(tcps_persisttimeo, "\t%lu persist timeout%s\n");
626 p(tcps_persistdrop, "\t\t%lu connection%s dropped by persist timeout\n");
627 p(tcps_finwait2_drops, "\t%lu Connection%s (fin_wait_2) dropped because of timeout\n");
628 p(tcps_keeptimeo, "\t%lu keepalive timeout%s\n");
629 p(tcps_keepprobe, "\t\t%lu keepalive probe%s sent\n");
630 p(tcps_keepdrops, "\t\t%lu connection%s dropped by keepalive\n");
631 p(tcps_predack, "\t%lu correct ACK header prediction%s\n");
632 p(tcps_preddat, "\t%lu correct data packet header prediction%s\n");
634 p(tcps_sc_added, "\t%lu syncache entrie%s added\n");
635 p1a(tcps_sc_retransmitted, "\t\t%lu retransmitted\n");
636 p1a(tcps_sc_dupsyn, "\t\t%lu dupsyn\n");
637 p1a(tcps_sc_dropped, "\t\t%lu dropped\n");
638 p1a(tcps_sc_completed, "\t\t%lu completed\n");
639 p1a(tcps_sc_bucketoverflow, "\t\t%lu bucket overflow\n");
640 p1a(tcps_sc_cacheoverflow, "\t\t%lu cache overflow\n");
641 p1a(tcps_sc_reset, "\t\t%lu reset\n");
642 p1a(tcps_sc_stale, "\t\t%lu stale\n");
643 p1a(tcps_sc_aborted, "\t\t%lu aborted\n");
644 p1a(tcps_sc_badack, "\t\t%lu badack\n");
645 p1a(tcps_sc_unreach, "\t\t%lu unreach\n");
646 p(tcps_sc_zonefail, "\t\t%lu zone failure%s\n");
647 p(tcps_sc_sendcookie, "\t%lu cookie%s sent\n");
648 p(tcps_sc_recvcookie, "\t%lu cookie%s received\n");
650 p(tcps_sack_recovery_episode, "\t%lu SACK recovery episode%s\n");
652 "\t%lu segment rexmit%s in SACK recovery episodes\n");
653 p(tcps_sack_rexmit_bytes,
654 "\t%lu byte rexmit%s in SACK recovery episodes\n");
655 p(tcps_sack_rcv_blocks,
656 "\t%lu SACK option%s (SACK blocks) received\n");
657 p(tcps_sack_send_blocks, "\t%lu SACK option%s (SACK blocks) sent\n");
658 p1a(tcps_sack_sboverflow, "\t%lu SACK scoreboard overflow\n");
668 * Dump UDP statistics structure.
671 udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
673 struct udpstat udpstat, zerostat;
674 size_t len = sizeof udpstat;
686 memset(&zerostat, 0, len);
687 if (sysctlbyname("net.inet.udp.stats", &udpstat, &len,
688 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
689 warn("sysctl: net.inet.udp.stats");
693 kread(off, &udpstat, len);
695 printf("%s:\n", name);
696 #define p(f, m) if (udpstat.f || sflag <= 1) \
697 printf(m, udpstat.f, plural(udpstat.f))
698 #define p1a(f, m) if (udpstat.f || sflag <= 1) \
700 p(udps_ipackets, "\t%lu datagram%s received\n");
701 p1a(udps_hdrops, "\t%lu with incomplete header\n");
702 p1a(udps_badlen, "\t%lu with bad data length field\n");
703 p1a(udps_badsum, "\t%lu with bad checksum\n");
704 p1a(udps_nosum, "\t%lu with no checksum\n");
705 p1a(udps_noport, "\t%lu dropped due to no socket\n");
707 "\t%lu broadcast/multicast datagram%s undelivered\n");
708 p1a(udps_fullsock, "\t%lu dropped due to full socket buffers\n");
709 p1a(udpps_pcbhashmiss, "\t%lu not for hashed pcb\n");
710 delivered = udpstat.udps_ipackets -
711 udpstat.udps_hdrops -
712 udpstat.udps_badlen -
713 udpstat.udps_badsum -
714 udpstat.udps_noport -
715 udpstat.udps_noportbcast -
716 udpstat.udps_fullsock;
717 if (delivered || sflag <= 1)
718 printf("\t%lu delivered\n", delivered);
719 p(udps_opackets, "\t%lu datagram%s output\n");
720 /* the next statistic is cumulative in udps_noportbcast */
722 "\t%lu time%s multicast source filter matched\n");
728 * Dump CARP statistics structure.
731 carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
733 struct carpstats carpstat, zerostat;
734 size_t len = sizeof(struct carpstats);
738 memset(&zerostat, 0, len);
739 if (sysctlbyname("net.inet.carp.stats", &carpstat, &len,
740 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
742 warn("sysctl: net.inet.carp.stats");
748 kread(off, &carpstat, len);
751 printf("%s:\n", name);
753 #define p(f, m) if (carpstat.f || sflag <= 1) \
754 printf(m, (uintmax_t)carpstat.f, plural(carpstat.f))
755 #define p2(f, m) if (carpstat.f || sflag <= 1) \
756 printf(m, (uintmax_t)carpstat.f)
758 p(carps_ipackets, "\t%ju packet%s received (IPv4)\n");
759 p(carps_ipackets6, "\t%ju packet%s received (IPv6)\n");
760 p(carps_badttl, "\t\t%ju packet%s discarded for wrong TTL\n");
761 p(carps_hdrops, "\t\t%ju packet%s shorter than header\n");
762 p(carps_badsum, "\t\t%ju discarded for bad checksum%s\n");
763 p(carps_badver, "\t\t%ju discarded packet%s with a bad version\n");
764 p2(carps_badlen, "\t\t%ju discarded because packet too short\n");
765 p2(carps_badauth, "\t\t%ju discarded for bad authentication\n");
766 p2(carps_badvhid, "\t\t%ju discarded for bad vhid\n");
767 p2(carps_badaddrs, "\t\t%ju discarded because of a bad address list\n");
768 p(carps_opackets, "\t%ju packet%s sent (IPv4)\n");
769 p(carps_opackets6, "\t%ju packet%s sent (IPv6)\n");
770 p2(carps_onomem, "\t\t%ju send failed due to mbuf memory error\n");
772 p(carps_ostates, "\t\t%s state update%s sent\n");
779 * Dump IP statistics structure.
782 ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
784 struct ipstat ipstat, zerostat;
785 size_t len = sizeof ipstat;
789 memset(&zerostat, 0, len);
790 if (sysctlbyname("net.inet.ip.stats", &ipstat, &len,
791 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
792 warn("sysctl: net.inet.ip.stats");
796 kread(off, &ipstat, len);
798 printf("%s:\n", name);
800 #define p(f, m) if (ipstat.f || sflag <= 1) \
801 printf(m, ipstat.f, plural(ipstat.f))
802 #define p1a(f, m) if (ipstat.f || sflag <= 1) \
805 p(ips_total, "\t%lu total packet%s received\n");
806 p(ips_badsum, "\t%lu bad header checksum%s\n");
807 p1a(ips_toosmall, "\t%lu with size smaller than minimum\n");
808 p1a(ips_tooshort, "\t%lu with data size < data length\n");
809 p1a(ips_toolong, "\t%lu with ip length > max ip packet size\n");
810 p1a(ips_badhlen, "\t%lu with header length < data size\n");
811 p1a(ips_badlen, "\t%lu with data length < header length\n");
812 p1a(ips_badoptions, "\t%lu with bad options\n");
813 p1a(ips_badvers, "\t%lu with incorrect version number\n");
814 p(ips_fragments, "\t%lu fragment%s received\n");
815 p(ips_fragdropped, "\t%lu fragment%s dropped (dup or out of space)\n");
816 p(ips_fragtimeout, "\t%lu fragment%s dropped after timeout\n");
817 p(ips_reassembled, "\t%lu packet%s reassembled ok\n");
818 p(ips_delivered, "\t%lu packet%s for this host\n");
819 p(ips_noproto, "\t%lu packet%s for unknown/unsupported protocol\n");
820 p(ips_forward, "\t%lu packet%s forwarded");
821 p(ips_fastforward, " (%lu packet%s fast forwarded)");
822 if (ipstat.ips_forward || sflag <= 1)
824 p(ips_cantforward, "\t%lu packet%s not forwardable\n");
826 "\t%lu packet%s received for unknown multicast group\n");
827 p(ips_redirectsent, "\t%lu redirect%s sent\n");
828 p(ips_localout, "\t%lu packet%s sent from this host\n");
829 p(ips_rawout, "\t%lu packet%s sent with fabricated ip header\n");
831 "\t%lu output packet%s dropped due to no bufs, etc.\n");
832 p(ips_noroute, "\t%lu output packet%s discarded due to no route\n");
833 p(ips_fragmented, "\t%lu output datagram%s fragmented\n");
834 p(ips_ofragments, "\t%lu fragment%s created\n");
835 p(ips_cantfrag, "\t%lu datagram%s that can't be fragmented\n");
836 p(ips_nogif, "\t%lu tunneling packet%s that can't find gif\n");
837 p(ips_badaddr, "\t%lu datagram%s with bad address in header\n");
842 static const char *icmpnames[ICMP_MAXTYPE + 1] = {
843 "echo reply", /* RFC 792 */
846 "destination unreachable", /* RFC 792 */
847 "source quench", /* RFC 792 */
848 "routing redirect", /* RFC 792 */
851 "echo", /* RFC 792 */
852 "router advertisement", /* RFC 1256 */
853 "router solicitation", /* RFC 1256 */
854 "time exceeded", /* RFC 792 */
855 "parameter problem", /* RFC 792 */
856 "time stamp", /* RFC 792 */
857 "time stamp reply", /* RFC 792 */
858 "information request", /* RFC 792 */
859 "information request reply", /* RFC 792 */
860 "address mask request", /* RFC 950 */
861 "address mask reply", /* RFC 950 */
873 "icmp traceroute", /* RFC 1393 */
874 "datagram conversion error", /* RFC 1475 */
875 "mobile host redirect",
876 "IPv6 where-are-you",
878 "mobile registration req",
879 "mobile registration reply",
880 "domain name request", /* RFC 1788 */
881 "domain name reply", /* RFC 1788 */
883 "icmp photuris", /* RFC 2521 */
887 * Dump ICMP statistics.
890 icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
892 struct icmpstat icmpstat, zerostat;
896 len = sizeof icmpstat;
899 memset(&zerostat, 0, len);
900 if (sysctlbyname("net.inet.icmp.stats", &icmpstat, &len,
901 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
902 warn("sysctl: net.inet.icmp.stats");
906 kread(off, &icmpstat, len);
908 printf("%s:\n", name);
910 #define p(f, m) if (icmpstat.f || sflag <= 1) \
911 printf(m, icmpstat.f, plural(icmpstat.f))
912 #define p1a(f, m) if (icmpstat.f || sflag <= 1) \
913 printf(m, icmpstat.f)
914 #define p2(f, m) if (icmpstat.f || sflag <= 1) \
915 printf(m, icmpstat.f, plurales(icmpstat.f))
917 p(icps_error, "\t%lu call%s to icmp_error\n");
919 "\t%lu error%s not generated in response to an icmp message\n");
920 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
921 if (icmpstat.icps_outhist[i] != 0) {
923 printf("\tOutput histogram:\n");
926 if (icmpnames[i] != NULL)
927 printf("\t\t%s: %lu\n", icmpnames[i],
928 icmpstat.icps_outhist[i]);
930 printf("\t\tunknown ICMP #%d: %lu\n", i,
931 icmpstat.icps_outhist[i]);
933 p(icps_badcode, "\t%lu message%s with bad code fields\n");
934 p(icps_tooshort, "\t%lu message%s < minimum length\n");
935 p(icps_checksum, "\t%lu bad checksum%s\n");
936 p(icps_badlen, "\t%lu message%s with bad length\n");
937 p1a(icps_bmcastecho, "\t%lu multicast echo requests ignored\n");
938 p1a(icps_bmcasttstamp, "\t%lu multicast timestamp requests ignored\n");
939 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
940 if (icmpstat.icps_inhist[i] != 0) {
942 printf("\tInput histogram:\n");
945 if (icmpnames[i] != NULL)
946 printf("\t\t%s: %lu\n", icmpnames[i],
947 icmpstat.icps_inhist[i]);
949 printf("\t\tunknown ICMP #%d: %lu\n", i,
950 icmpstat.icps_inhist[i]);
952 p(icps_reflect, "\t%lu message response%s generated\n");
953 p2(icps_badaddr, "\t%lu invalid return address%s\n");
954 p(icps_noroute, "\t%lu no return route%s\n");
960 if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) <
963 printf("\tICMP address mask responses are %sabled\n",
969 * Dump IGMP statistics structure.
972 igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
974 struct igmpstat igmpstat, zerostat;
975 size_t len = sizeof igmpstat;
979 memset(&zerostat, 0, len);
980 if (sysctlbyname("net.inet.igmp.stats", &igmpstat, &len,
981 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
982 warn("sysctl: net.inet.igmp.stats");
986 kread(off, &igmpstat, len);
988 printf("%s:\n", name);
990 #define p(f, m) if (igmpstat.f || sflag <= 1) \
991 printf(m, igmpstat.f, plural(igmpstat.f))
992 #define py(f, m) if (igmpstat.f || sflag <= 1) \
993 printf(m, igmpstat.f, igmpstat.f != 1 ? "ies" : "y")
994 p(igps_rcv_total, "\t%u message%s received\n");
995 p(igps_rcv_tooshort, "\t%u message%s received with too few bytes\n");
996 p(igps_rcv_badsum, "\t%u message%s received with bad checksum\n");
997 py(igps_rcv_queries, "\t%u membership quer%s received\n");
998 py(igps_rcv_badqueries, "\t%u membership quer%s received with invalid field(s)\n");
999 p(igps_rcv_reports, "\t%u membership report%s received\n");
1000 p(igps_rcv_badreports, "\t%u membership report%s received with invalid field(s)\n");
1001 p(igps_rcv_ourreports, "\t%u membership report%s received for groups to which we belong\n");
1002 p(igps_snd_reports, "\t%u membership report%s sent\n");
1008 * Dump PIM statistics structure.
1011 pim_stats(u_long off __unused, const char *name, int af1 __unused,
1014 struct pimstat pimstat, zerostat;
1015 size_t len = sizeof pimstat;
1019 memset(&zerostat, 0, len);
1020 if (sysctlbyname("net.inet.pim.stats", &pimstat, &len,
1021 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1022 if (errno != ENOENT)
1023 warn("sysctl: net.inet.pim.stats");
1029 kread(off, &pimstat, len);
1032 printf("%s:\n", name);
1034 #define p(f, m) if (pimstat.f || sflag <= 1) \
1035 printf(m, (uintmax_t)pimstat.f, plural(pimstat.f))
1036 #define py(f, m) if (pimstat.f || sflag <= 1) \
1037 printf(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
1038 p(pims_rcv_total_msgs, "\t%ju message%s received\n");
1039 p(pims_rcv_total_bytes, "\t%ju byte%s received\n");
1040 p(pims_rcv_tooshort, "\t%ju message%s received with too few bytes\n");
1041 p(pims_rcv_badsum, "\t%ju message%s received with bad checksum\n");
1042 p(pims_rcv_badversion, "\t%ju message%s received with bad version\n");
1043 p(pims_rcv_registers_msgs, "\t%ju data register message%s received\n");
1044 p(pims_rcv_registers_bytes, "\t%ju data register byte%s received\n");
1045 p(pims_rcv_registers_wrongiif, "\t%ju data register message%s received on wrong iif\n");
1046 p(pims_rcv_badregisters, "\t%ju bad register%s received\n");
1047 p(pims_snd_registers_msgs, "\t%ju data register message%s sent\n");
1048 p(pims_snd_registers_bytes, "\t%ju data register byte%s sent\n");
1054 * Pretty print an Internet address (net address + port).
1057 inetprint(struct in_addr *in, int port, const char *proto, int num_port)
1059 struct servent *sp = 0;
1064 sprintf(line, "%s.", inetname(in));
1066 sprintf(line, "%.*s.", (Aflag && !num_port) ? 12 : 16, inetname(in));
1067 cp = index(line, '\0');
1068 if (!num_port && port)
1069 sp = getservbyport((int)port, proto);
1070 if (sp || port == 0)
1071 sprintf(cp, "%.15s ", sp ? sp->s_name : "*");
1073 sprintf(cp, "%d ", ntohs((u_short)port));
1074 width = (Aflag && !Wflag) ? 18 : 22;
1076 printf("%-*s ", width, line);
1078 printf("%-*.*s ", width, width, line);
1082 * Construct an Internet address representation.
1083 * If numeric_addr has been supplied, give
1084 * numeric value, otherwise try for symbolic name.
1087 inetname(struct in_addr *inp)
1090 static char line[MAXHOSTNAMELEN];
1095 if (!numeric_addr && inp->s_addr != INADDR_ANY) {
1096 int net = inet_netof(*inp);
1097 int lna = inet_lnaof(*inp);
1099 if (lna == INADDR_ANY) {
1100 np = getnetbyaddr(net, AF_INET);
1105 hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
1108 trimdomain(cp, strlen(cp));
1112 if (inp->s_addr == INADDR_ANY)
1115 strncpy(line, cp, sizeof(line) - 1);
1116 line[sizeof(line) - 1] = '\0';
1118 inp->s_addr = ntohl(inp->s_addr);
1119 #define C(x) ((u_int)((x) & 0xff))
1120 sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
1121 C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));