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/tcp_debug.h>
70 #include <netinet/udp.h>
71 #include <netinet/udp_var.h>
73 #include <arpa/inet.h>
85 char *inetname(struct in_addr *);
86 void inetprint(struct in_addr *, int, const char *, int);
88 static int udp_done, tcp_done, sdp_done;
92 pcblist_sysctl(int proto, const char *name, char **bufp, int istcp __unused)
100 mibvar = "net.inet.tcp.pcblist";
103 mibvar = "net.inet.udp.pcblist";
106 mibvar = "net.inet.divert.pcblist";
109 mibvar = "net.inet.raw.pcblist";
112 if (strncmp(name, "sdp", 3) == 0)
113 mibvar = "net.inet.sdp.pcblist";
115 if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
117 warn("sysctl: %s", mibvar);
120 if ((buf = malloc(len)) == 0) {
121 warnx("malloc %lu bytes", (u_long)len);
124 if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
125 warn("sysctl: %s", mibvar);
134 * Copied directly from uipc_socket2.c. We leave out some fields that are in
135 * nested structures that aren't used to avoid extra work.
138 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
140 xsb->sb_cc = sb->sb_cc;
141 xsb->sb_hiwat = sb->sb_hiwat;
142 xsb->sb_mbcnt = sb->sb_mbcnt;
143 xsb->sb_mcnt = sb->sb_mcnt;
144 xsb->sb_ccnt = sb->sb_ccnt;
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_flags & 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;
313 struct xtcp_timer *timer;
319 if (strncmp(name, "sdp", 3) != 0) {
343 if (!pcblist_sysctl(proto, name, &buf, istcp))
346 if (!pcblist_kvm(off, &buf, istcp))
350 oxig = xig = (struct xinpgen *)buf;
351 for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
352 xig->xig_len > sizeof(struct xinpgen);
353 xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
355 timer = &((struct xtcpcb *)xig)->xt_timer;
356 tp = &((struct xtcpcb *)xig)->xt_tp;
357 inp = &((struct xtcpcb *)xig)->xt_inp;
358 so = &((struct xtcpcb *)xig)->xt_socket;
360 inp = &((struct xinpcb *)xig)->xi_inp;
361 so = &((struct xinpcb *)xig)->xi_socket;
365 /* Ignore sockets for protocols other than the desired one. */
366 if (so->xso_protocol != proto)
369 /* Ignore PCBs which were freed during copyout. */
370 if (inp->inp_gencnt > oxig->xig_gen)
373 if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
375 || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
377 || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
379 && (inp->inp_vflag & INP_IPV6) == 0
386 (istcp && tp->t_state == TCPS_LISTEN)
387 || (af1 == AF_INET &&
388 inet_lnaof(inp->inp_laddr) == INADDR_ANY)
390 || (af1 == AF_INET6 &&
391 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
393 || (af1 == AF_UNSPEC &&
394 (((inp->inp_vflag & INP_IPV4) != 0 &&
395 inet_lnaof(inp->inp_laddr) == INADDR_ANY)
397 || ((inp->inp_vflag & INP_IPV6) != 0 &&
398 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
406 printf("Active Internet connections");
408 printf(" (including servers)");
411 "Current listen queue sizes (qlen/incqlen/maxqlen)");
414 printf("%-*s ", 2 * (int)sizeof(void *), "Tcpcb");
416 printf((Aflag && !Wflag) ?
417 "%-5.5s %-14.14s %-18.18s" :
418 "%-5.5s %-14.14s %-22.22s",
419 "Proto", "Listen", "Local Address");
421 printf((Aflag && !Wflag) ?
422 "%-5.5s %-6.6s %-6.6s %-6.6s %-18.18s %s" :
423 "%-5.5s %-6.6s %-6.6s %-6.6s %-22.22s %s",
424 "Proto", "Rexmit", "OOORcv", "0-win",
425 "Local Address", "Foreign Address");
427 printf((Aflag && !Wflag) ?
428 "%-5.5s %-6.6s %-6.6s %-18.18s %-18.18s" :
429 "%-5.5s %-6.6s %-6.6s %-22.22s %-22.22s",
430 "Proto", "Recv-Q", "Send-Q",
431 "Local Address", "Foreign Address");
436 printf(" %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s",
437 "R-MBUF", "S-MBUF", "R-CLUS",
438 "S-CLUS", "R-HIWA", "S-HIWA",
439 "R-LOWA", "S-LOWA", "R-BCNT",
440 "S-BCNT", "R-BMAX", "S-BMAX");
441 printf(" %7.7s %7.7s %7.7s %7.7s %7.7s %7.7s",
442 "rexmt", "persist", "keep",
443 "2msl", "delack", "rcvtime");
448 if (Lflag && so->so_qlimit == 0)
452 printf("%*lx ", 2 * (int)sizeof(void *), (u_long)inp->inp_ppcb);
454 printf("%*lx ", 2 * (int)sizeof(void *), (u_long)so->so_pcb);
457 if ((inp->inp_vflag & INP_IPV6) != 0)
458 vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
462 vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
464 if (istcp && (tp->t_flags & TF_TOE) != 0)
465 printf("%-3.3s%-2.2s ", "toe", vchar);
467 printf("%-3.3s%-2.2s ", name, vchar);
471 snprintf(buf1, 15, "%d/%d/%d", so->so_qlen,
472 so->so_incqlen, so->so_qlimit);
473 printf("%-14.14s ", buf1);
476 printf("%6u %6u %6u ", tp->t_sndrexmitpack,
477 tp->t_rcvoopack, tp->t_sndzerowin);
479 printf("%6u %6u ", so->so_rcv.sb_cc, so->so_snd.sb_cc);
482 if (inp->inp_vflag & INP_IPV4) {
483 inetprint(&inp->inp_laddr, (int)inp->inp_lport,
486 inetprint(&inp->inp_faddr,
487 (int)inp->inp_fport, name, 1);
490 else if (inp->inp_vflag & INP_IPV6) {
491 inet6print(&inp->in6p_laddr,
492 (int)inp->inp_lport, name, 1);
494 inet6print(&inp->in6p_faddr,
495 (int)inp->inp_fport, name, 1);
496 } /* else nothing printed now */
498 } else if (inp->inp_flags & INP_ANONPORT) {
499 if (inp->inp_vflag & INP_IPV4) {
500 inetprint(&inp->inp_laddr, (int)inp->inp_lport,
503 inetprint(&inp->inp_faddr,
504 (int)inp->inp_fport, name, 0);
507 else if (inp->inp_vflag & INP_IPV6) {
508 inet6print(&inp->in6p_laddr,
509 (int)inp->inp_lport, name, 1);
511 inet6print(&inp->in6p_faddr,
512 (int)inp->inp_fport, name, 0);
513 } /* else nothing printed now */
516 if (inp->inp_vflag & INP_IPV4) {
517 inetprint(&inp->inp_laddr, (int)inp->inp_lport,
520 inetprint(&inp->inp_faddr,
521 (int)inp->inp_fport, name,
522 inp->inp_lport != inp->inp_fport);
525 else if (inp->inp_vflag & INP_IPV6) {
526 inet6print(&inp->in6p_laddr,
527 (int)inp->inp_lport, name, 0);
529 inet6print(&inp->in6p_faddr,
530 (int)inp->inp_fport, name,
531 inp->inp_lport != inp->inp_fport);
532 } /* else nothing printed now */
536 printf("%6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u",
537 so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt,
538 so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt,
539 so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
540 so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
541 so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
542 so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
544 printf(" %4d.%02d %4d.%02d %4d.%02d %4d.%02d %4d.%02d %4d.%02d",
545 timer->tt_rexmt / 1000, (timer->tt_rexmt % 1000) / 10,
546 timer->tt_persist / 1000, (timer->tt_persist % 1000) / 10,
547 timer->tt_keep / 1000, (timer->tt_keep % 1000) / 10,
548 timer->tt_2msl / 1000, (timer->tt_2msl % 1000) / 10,
549 timer->tt_delack / 1000, (timer->tt_delack % 1000) / 10,
550 timer->t_rcvtime / 1000, (timer->t_rcvtime % 1000) / 10);
552 if (istcp && !Lflag && !xflag && !Tflag) {
553 if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
554 printf("%d", tp->t_state);
556 printf("%s", tcpstates[tp->t_state]);
557 #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
558 /* Show T/TCP `hidden state' */
559 if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
561 #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
566 if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
567 if (oxig->xig_count > xig->xig_count) {
568 printf("Some %s sockets may have been deleted.\n",
570 } else if (oxig->xig_count < xig->xig_count) {
571 printf("Some %s sockets may have been created.\n",
575 "Some %s sockets may have been created or deleted.\n",
583 * Dump TCP statistics structure.
586 tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
588 struct tcpstat tcpstat, zerostat;
589 size_t len = sizeof tcpstat;
600 memset(&zerostat, 0, len);
601 if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &len,
602 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
603 warn("sysctl: net.inet.tcp.stats");
607 kread_counters(off, &tcpstat, len);
609 printf ("%s:\n", name);
611 #define p(f, m) if (tcpstat.f || sflag <= 1) \
612 printf(m, (uintmax_t )tcpstat.f, plural(tcpstat.f))
614 #define p1a(f, m) if (tcpstat.f || sflag <= 1) \
615 printf(m, (uintmax_t )tcpstat.f)
617 #define p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
618 printf(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1), \
619 (uintmax_t )tcpstat.f2, plural(tcpstat.f2))
621 #define p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
622 printf(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1), \
623 (uintmax_t )tcpstat.f2)
625 #define p3(f, m) if (tcpstat.f || sflag <= 1) \
626 printf(m, (uintmax_t )tcpstat.f, pluralies(tcpstat.f))
628 p(tcps_sndtotal, "\t%ju packet%s sent\n");
629 p2(tcps_sndpack,tcps_sndbyte, "\t\t%ju data packet%s (%ju byte%s)\n");
630 p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
631 "\t\t%ju data packet%s (%ju byte%s) retransmitted\n");
633 "\t\t%ju data packet%s unnecessarily retransmitted\n");
634 p(tcps_mturesent, "\t\t%ju resend%s initiated by MTU discovery\n");
635 p2a(tcps_sndacks, tcps_delack,
636 "\t\t%ju ack-only packet%s (%ju delayed)\n");
637 p(tcps_sndurg, "\t\t%ju URG only packet%s\n");
638 p(tcps_sndprobe, "\t\t%ju window probe packet%s\n");
639 p(tcps_sndwinup, "\t\t%ju window update packet%s\n");
640 p(tcps_sndctrl, "\t\t%ju control packet%s\n");
641 p(tcps_rcvtotal, "\t%ju packet%s received\n");
642 p2(tcps_rcvackpack, tcps_rcvackbyte,
643 "\t\t%ju ack%s (for %ju byte%s)\n");
644 p(tcps_rcvdupack, "\t\t%ju duplicate ack%s\n");
645 p(tcps_rcvacktoomuch, "\t\t%ju ack%s for unsent data\n");
646 p2(tcps_rcvpack, tcps_rcvbyte,
647 "\t\t%ju packet%s (%ju byte%s) received in-sequence\n");
648 p2(tcps_rcvduppack, tcps_rcvdupbyte,
649 "\t\t%ju completely duplicate packet%s (%ju byte%s)\n");
650 p(tcps_pawsdrop, "\t\t%ju old duplicate packet%s\n");
651 p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
652 "\t\t%ju packet%s with some dup. data (%ju byte%s duped)\n");
653 p2(tcps_rcvoopack, tcps_rcvoobyte,
654 "\t\t%ju out-of-order packet%s (%ju byte%s)\n");
655 p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
656 "\t\t%ju packet%s (%ju byte%s) of data after window\n");
657 p(tcps_rcvwinprobe, "\t\t%ju window probe%s\n");
658 p(tcps_rcvwinupd, "\t\t%ju window update packet%s\n");
659 p(tcps_rcvafterclose, "\t\t%ju packet%s received after close\n");
660 p(tcps_rcvbadsum, "\t\t%ju discarded for bad checksum%s\n");
661 p(tcps_rcvbadoff, "\t\t%ju discarded for bad header offset field%s\n");
662 p1a(tcps_rcvshort, "\t\t%ju discarded because packet too short\n");
663 p1a(tcps_rcvmemdrop, "\t\t%ju discarded due to memory problems\n");
664 p(tcps_connattempt, "\t%ju connection request%s\n");
665 p(tcps_accepts, "\t%ju connection accept%s\n");
666 p(tcps_badsyn, "\t%ju bad connection attempt%s\n");
667 p(tcps_listendrop, "\t%ju listen queue overflow%s\n");
668 p(tcps_badrst, "\t%ju ignored RSTs in the window%s\n");
669 p(tcps_connects, "\t%ju connection%s established (including accepts)\n");
670 p2(tcps_closed, tcps_drops,
671 "\t%ju connection%s closed (including %ju drop%s)\n");
672 p(tcps_cachedrtt, "\t\t%ju connection%s updated cached RTT on close\n");
674 "\t\t%ju connection%s updated cached RTT variance on close\n");
675 p(tcps_cachedssthresh,
676 "\t\t%ju connection%s updated cached ssthresh on close\n");
677 p(tcps_conndrops, "\t%ju embryonic connection%s dropped\n");
678 p2(tcps_rttupdated, tcps_segstimed,
679 "\t%ju segment%s updated rtt (of %ju attempt%s)\n");
680 p(tcps_rexmttimeo, "\t%ju retransmit timeout%s\n");
681 p(tcps_timeoutdrop, "\t\t%ju connection%s dropped by rexmit timeout\n");
682 p(tcps_persisttimeo, "\t%ju persist timeout%s\n");
683 p(tcps_persistdrop, "\t\t%ju connection%s dropped by persist timeout\n");
684 p(tcps_finwait2_drops,
685 "\t%ju Connection%s (fin_wait_2) dropped because of timeout\n");
686 p(tcps_keeptimeo, "\t%ju keepalive timeout%s\n");
687 p(tcps_keepprobe, "\t\t%ju keepalive probe%s sent\n");
688 p(tcps_keepdrops, "\t\t%ju connection%s dropped by keepalive\n");
689 p(tcps_predack, "\t%ju correct ACK header prediction%s\n");
690 p(tcps_preddat, "\t%ju correct data packet header prediction%s\n");
692 p3(tcps_sc_added, "\t%ju syncache entr%s added\n");
693 p1a(tcps_sc_retransmitted, "\t\t%ju retransmitted\n");
694 p1a(tcps_sc_dupsyn, "\t\t%ju dupsyn\n");
695 p1a(tcps_sc_dropped, "\t\t%ju dropped\n");
696 p1a(tcps_sc_completed, "\t\t%ju completed\n");
697 p1a(tcps_sc_bucketoverflow, "\t\t%ju bucket overflow\n");
698 p1a(tcps_sc_cacheoverflow, "\t\t%ju cache overflow\n");
699 p1a(tcps_sc_reset, "\t\t%ju reset\n");
700 p1a(tcps_sc_stale, "\t\t%ju stale\n");
701 p1a(tcps_sc_aborted, "\t\t%ju aborted\n");
702 p1a(tcps_sc_badack, "\t\t%ju badack\n");
703 p1a(tcps_sc_unreach, "\t\t%ju unreach\n");
704 p(tcps_sc_zonefail, "\t\t%ju zone failure%s\n");
705 p(tcps_sc_sendcookie, "\t%ju cookie%s sent\n");
706 p(tcps_sc_recvcookie, "\t%ju cookie%s received\n");
708 p3(tcps_hc_added, "\t%ju hostcache entr%s added\n");
709 p1a(tcps_hc_bucketoverflow, "\t\t%ju bucket overflow\n");
711 p(tcps_sack_recovery_episode, "\t%ju SACK recovery episode%s\n");
713 "\t%ju segment rexmit%s in SACK recovery episodes\n");
714 p(tcps_sack_rexmit_bytes,
715 "\t%ju byte rexmit%s in SACK recovery episodes\n");
716 p(tcps_sack_rcv_blocks,
717 "\t%ju SACK option%s (SACK blocks) received\n");
718 p(tcps_sack_send_blocks, "\t%ju SACK option%s (SACK blocks) sent\n");
719 p1a(tcps_sack_sboverflow, "\t%ju SACK scoreboard overflow\n");
721 p(tcps_ecn_ce, "\t%ju packet%s with ECN CE bit set\n");
722 p(tcps_ecn_ect0, "\t%ju packet%s with ECN ECT(0) bit set\n");
723 p(tcps_ecn_ect1, "\t%ju packet%s with ECN ECT(1) bit set\n");
724 p(tcps_ecn_shs, "\t%ju successful ECN handshake%s\n");
725 p(tcps_ecn_rcwnd, "\t%ju time%s ECN reduced the congestion window\n");
727 p(tcps_sig_rcvgoodsig,
728 "\t%ju packet%s with valid tcp-md5 signature received\n");
729 p(tcps_sig_rcvbadsig,
730 "\t%ju packet%s with invalid tcp-md5 signature received\n");
731 p(tcps_sig_err_buildsig,
732 "\t%ju packet%s with tcp-md5 signature mismatch\n");
733 p(tcps_sig_err_sigopt,
734 "\t%ju packet%s with unexpected tcp-md5 signature received\n");
735 p(tcps_sig_err_nosigopt,
736 "\t%ju packet%s without expected tcp-md5 signature received\n");
745 * Dump UDP statistics structure.
748 udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
750 struct udpstat udpstat, zerostat;
751 size_t len = sizeof udpstat;
763 memset(&zerostat, 0, len);
764 if (sysctlbyname("net.inet.udp.stats", &udpstat, &len,
765 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
766 warn("sysctl: net.inet.udp.stats");
770 kread_counters(off, &udpstat, len);
772 printf("%s:\n", name);
773 #define p(f, m) if (udpstat.f || sflag <= 1) \
774 printf("\t%ju " m, (uintmax_t)udpstat.f, plural(udpstat.f))
775 #define p1a(f, m) if (udpstat.f || sflag <= 1) \
776 printf("\t%ju " m, (uintmax_t)udpstat.f)
777 p(udps_ipackets, "datagram%s received\n");
778 p1a(udps_hdrops, "with incomplete header\n");
779 p1a(udps_badlen, "with bad data length field\n");
780 p1a(udps_badsum, "with bad checksum\n");
781 p1a(udps_nosum, "with no checksum\n");
782 p1a(udps_noport, "dropped due to no socket\n");
784 "broadcast/multicast datagram%s undelivered\n");
785 p1a(udps_fullsock, "dropped due to full socket buffers\n");
786 p1a(udpps_pcbhashmiss, "not for hashed pcb\n");
787 delivered = udpstat.udps_ipackets -
788 udpstat.udps_hdrops -
789 udpstat.udps_badlen -
790 udpstat.udps_badsum -
791 udpstat.udps_noport -
792 udpstat.udps_noportbcast -
793 udpstat.udps_fullsock;
794 if (delivered || sflag <= 1)
795 printf("\t%ju delivered\n", (uint64_t)delivered);
796 p(udps_opackets, "datagram%s output\n");
797 /* the next statistic is cumulative in udps_noportbcast */
799 "time%s multicast source filter matched\n");
805 * Dump CARP statistics structure.
808 carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
810 struct carpstats carpstat, zerostat;
811 size_t len = sizeof(struct carpstats);
815 memset(&zerostat, 0, len);
816 if (sysctlbyname("net.inet.carp.stats", &carpstat, &len,
817 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
819 warn("sysctl: net.inet.carp.stats");
825 kread_counters(off, &carpstat, len);
828 printf("%s:\n", name);
830 #define p(f, m) if (carpstat.f || sflag <= 1) \
831 printf(m, (uintmax_t)carpstat.f, plural(carpstat.f))
832 #define p2(f, m) if (carpstat.f || sflag <= 1) \
833 printf(m, (uintmax_t)carpstat.f)
835 p(carps_ipackets, "\t%ju packet%s received (IPv4)\n");
836 p(carps_ipackets6, "\t%ju packet%s received (IPv6)\n");
837 p(carps_badttl, "\t\t%ju packet%s discarded for wrong TTL\n");
838 p(carps_hdrops, "\t\t%ju packet%s shorter than header\n");
839 p(carps_badsum, "\t\t%ju discarded for bad checksum%s\n");
840 p(carps_badver, "\t\t%ju discarded packet%s with a bad version\n");
841 p2(carps_badlen, "\t\t%ju discarded because packet too short\n");
842 p2(carps_badauth, "\t\t%ju discarded for bad authentication\n");
843 p2(carps_badvhid, "\t\t%ju discarded for bad vhid\n");
844 p2(carps_badaddrs, "\t\t%ju discarded because of a bad address list\n");
845 p(carps_opackets, "\t%ju packet%s sent (IPv4)\n");
846 p(carps_opackets6, "\t%ju packet%s sent (IPv6)\n");
847 p2(carps_onomem, "\t\t%ju send failed due to mbuf memory error\n");
849 p(carps_ostates, "\t\t%s state update%s sent\n");
856 * Dump IP statistics structure.
859 ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
861 struct ipstat ipstat, zerostat;
862 size_t len = sizeof ipstat;
866 memset(&zerostat, 0, len);
867 if (sysctlbyname("net.inet.ip.stats", &ipstat, &len,
868 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
869 warn("sysctl: net.inet.ip.stats");
873 kread_counters(off, &ipstat, len);
875 printf("%s:\n", name);
877 #define p(f, m) if (ipstat.f || sflag <= 1) \
878 printf(m, (uintmax_t )ipstat.f, plural(ipstat.f))
879 #define p1a(f, m) if (ipstat.f || sflag <= 1) \
880 printf(m, (uintmax_t )ipstat.f)
882 p(ips_total, "\t%ju total packet%s received\n");
883 p(ips_badsum, "\t%ju bad header checksum%s\n");
884 p1a(ips_toosmall, "\t%ju with size smaller than minimum\n");
885 p1a(ips_tooshort, "\t%ju with data size < data length\n");
886 p1a(ips_toolong, "\t%ju with ip length > max ip packet size\n");
887 p1a(ips_badhlen, "\t%ju with header length < data size\n");
888 p1a(ips_badlen, "\t%ju with data length < header length\n");
889 p1a(ips_badoptions, "\t%ju with bad options\n");
890 p1a(ips_badvers, "\t%ju with incorrect version number\n");
891 p(ips_fragments, "\t%ju fragment%s received\n");
892 p(ips_fragdropped, "\t%ju fragment%s dropped (dup or out of space)\n");
893 p(ips_fragtimeout, "\t%ju fragment%s dropped after timeout\n");
894 p(ips_reassembled, "\t%ju packet%s reassembled ok\n");
895 p(ips_delivered, "\t%ju packet%s for this host\n");
896 p(ips_noproto, "\t%ju packet%s for unknown/unsupported protocol\n");
897 p(ips_forward, "\t%ju packet%s forwarded");
898 p(ips_fastforward, " (%ju packet%s fast forwarded)");
899 if (ipstat.ips_forward || sflag <= 1)
901 p(ips_cantforward, "\t%ju packet%s not forwardable\n");
903 "\t%ju packet%s received for unknown multicast group\n");
904 p(ips_redirectsent, "\t%ju redirect%s sent\n");
905 p(ips_localout, "\t%ju packet%s sent from this host\n");
906 p(ips_rawout, "\t%ju packet%s sent with fabricated ip header\n");
908 "\t%ju output packet%s dropped due to no bufs, etc.\n");
909 p(ips_noroute, "\t%ju output packet%s discarded due to no route\n");
910 p(ips_fragmented, "\t%ju output datagram%s fragmented\n");
911 p(ips_ofragments, "\t%ju fragment%s created\n");
912 p(ips_cantfrag, "\t%ju datagram%s that can't be fragmented\n");
913 p(ips_nogif, "\t%ju tunneling packet%s that can't find gif\n");
914 p(ips_badaddr, "\t%ju datagram%s with bad address in header\n");
920 * Dump ARP statistics structure.
923 arp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
925 struct arpstat arpstat, zerostat;
926 size_t len = sizeof(arpstat);
930 memset(&zerostat, 0, len);
931 if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len,
932 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
933 warn("sysctl: net.link.ether.arp.stats");
937 kread_counters(off, &arpstat, len);
939 printf("%s:\n", name);
941 #define p(f, m) if (arpstat.f || sflag <= 1) \
942 printf("\t%ju " m, (uintmax_t)arpstat.f, plural(arpstat.f))
943 #define p2(f, m) if (arpstat.f || sflag <= 1) \
944 printf("\t%ju " m, (uintmax_t)arpstat.f, pluralies(arpstat.f))
946 p(txrequests, "ARP request%s sent\n");
947 p2(txreplies, "ARP repl%s sent\n");
948 p(rxrequests, "ARP request%s received\n");
949 p2(rxreplies, "ARP repl%s received\n");
950 p(received, "ARP packet%s received\n");
951 p(dropped, "total packet%s dropped due to no ARP entry\n");
952 p(timeouts, "ARP entry%s timed out\n");
953 p(dupips, "Duplicate IP%s seen\n");
960 static const char *icmpnames[ICMP_MAXTYPE + 1] = {
961 "echo reply", /* RFC 792 */
964 "destination unreachable", /* RFC 792 */
965 "source quench", /* RFC 792 */
966 "routing redirect", /* RFC 792 */
969 "echo", /* RFC 792 */
970 "router advertisement", /* RFC 1256 */
971 "router solicitation", /* RFC 1256 */
972 "time exceeded", /* RFC 792 */
973 "parameter problem", /* RFC 792 */
974 "time stamp", /* RFC 792 */
975 "time stamp reply", /* RFC 792 */
976 "information request", /* RFC 792 */
977 "information request reply", /* RFC 792 */
978 "address mask request", /* RFC 950 */
979 "address mask reply", /* RFC 950 */
991 "icmp traceroute", /* RFC 1393 */
992 "datagram conversion error", /* RFC 1475 */
993 "mobile host redirect",
994 "IPv6 where-are-you",
996 "mobile registration req",
997 "mobile registration reply",
998 "domain name request", /* RFC 1788 */
999 "domain name reply", /* RFC 1788 */
1001 "icmp photuris", /* RFC 2521 */
1005 * Dump ICMP statistics.
1008 icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1010 struct icmpstat icmpstat, zerostat;
1014 len = sizeof icmpstat;
1017 memset(&zerostat, 0, len);
1018 if (sysctlbyname("net.inet.icmp.stats", &icmpstat, &len,
1019 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1020 warn("sysctl: net.inet.icmp.stats");
1024 kread_counters(off, &icmpstat, len);
1026 printf("%s:\n", name);
1028 #define p(f, m) if (icmpstat.f || sflag <= 1) \
1029 printf(m, icmpstat.f, plural(icmpstat.f))
1030 #define p1a(f, m) if (icmpstat.f || sflag <= 1) \
1031 printf(m, icmpstat.f)
1032 #define p2(f, m) if (icmpstat.f || sflag <= 1) \
1033 printf(m, icmpstat.f, plurales(icmpstat.f))
1035 p(icps_error, "\t%lu call%s to icmp_error\n");
1037 "\t%lu error%s not generated in response to an icmp message\n");
1038 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
1039 if (icmpstat.icps_outhist[i] != 0) {
1041 printf("\tOutput histogram:\n");
1044 if (icmpnames[i] != NULL)
1045 printf("\t\t%s: %lu\n", icmpnames[i],
1046 icmpstat.icps_outhist[i]);
1048 printf("\t\tunknown ICMP #%d: %lu\n", i,
1049 icmpstat.icps_outhist[i]);
1051 p(icps_badcode, "\t%lu message%s with bad code fields\n");
1052 p(icps_tooshort, "\t%lu message%s less than the minimum length\n");
1053 p(icps_checksum, "\t%lu message%s with bad checksum\n");
1054 p(icps_badlen, "\t%lu message%s with bad length\n");
1055 p1a(icps_bmcastecho, "\t%lu multicast echo requests ignored\n");
1056 p1a(icps_bmcasttstamp, "\t%lu multicast timestamp requests ignored\n");
1057 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
1058 if (icmpstat.icps_inhist[i] != 0) {
1060 printf("\tInput histogram:\n");
1063 if (icmpnames[i] != NULL)
1064 printf("\t\t%s: %lu\n", icmpnames[i],
1065 icmpstat.icps_inhist[i]);
1067 printf("\t\tunknown ICMP #%d: %lu\n", i,
1068 icmpstat.icps_inhist[i]);
1070 p(icps_reflect, "\t%lu message response%s generated\n");
1071 p2(icps_badaddr, "\t%lu invalid return address%s\n");
1072 p(icps_noroute, "\t%lu no return route%s\n");
1078 if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) <
1081 printf("\tICMP address mask responses are %sabled\n",
1086 #ifndef BURN_BRIDGES
1088 * Dump IGMP statistics structure (pre 8.x kernel).
1091 igmp_stats_live_old(const char *name)
1093 struct oigmpstat oigmpstat, zerostat;
1094 size_t len = sizeof(oigmpstat);
1097 memset(&zerostat, 0, len);
1098 if (sysctlbyname("net.inet.igmp.stats", &oigmpstat, &len,
1099 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1100 warn("sysctl: net.inet.igmp.stats");
1104 printf("%s:\n", name);
1106 #define p(f, m) if (oigmpstat.f || sflag <= 1) \
1107 printf(m, oigmpstat.f, plural(oigmpstat.f))
1108 #define py(f, m) if (oigmpstat.f || sflag <= 1) \
1109 printf(m, oigmpstat.f, oigmpstat.f != 1 ? "ies" : "y")
1110 p(igps_rcv_total, "\t%u message%s received\n");
1111 p(igps_rcv_tooshort, "\t%u message%s received with too few bytes\n");
1112 p(igps_rcv_badsum, "\t%u message%s received with bad checksum\n");
1113 py(igps_rcv_queries, "\t%u membership quer%s received\n");
1114 py(igps_rcv_badqueries,
1115 "\t%u membership quer%s received with invalid field(s)\n");
1116 p(igps_rcv_reports, "\t%u membership report%s received\n");
1117 p(igps_rcv_badreports,
1118 "\t%u membership report%s received with invalid field(s)\n");
1119 p(igps_rcv_ourreports,
1120 "\t%u membership report%s received for groups to which we belong\n");
1121 p(igps_snd_reports, "\t%u membership report%s sent\n");
1125 #endif /* !BURN_BRIDGES */
1128 * Dump IGMP statistics structure.
1131 igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1133 struct igmpstat igmpstat, zerostat;
1136 #ifndef BURN_BRIDGES
1139 * Detect if we are being run against a pre-IGMPv3 kernel.
1140 * We cannot do this for a core file as the legacy
1141 * struct igmpstat has no size field, nor does it
1142 * export it in any readily-available symbols.
1145 if (sysctlbyname("net.inet.igmp.stats", NULL, &len, NULL,
1147 warn("sysctl: net.inet.igmp.stats");
1150 if (len < sizeof(igmpstat)) {
1151 igmp_stats_live_old(name);
1155 #endif /* !BURN_BRIDGES */
1157 len = sizeof(igmpstat);
1160 memset(&zerostat, 0, len);
1161 if (sysctlbyname("net.inet.igmp.stats", &igmpstat, &len,
1162 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1163 warn("sysctl: net.inet.igmp.stats");
1167 len = sizeof(igmpstat);
1168 kread(off, &igmpstat, len);
1171 if (igmpstat.igps_version != IGPS_VERSION_3) {
1172 warnx("%s: version mismatch (%d != %d)", __func__,
1173 igmpstat.igps_version, IGPS_VERSION_3);
1175 if (igmpstat.igps_len != IGPS_VERSION3_LEN) {
1176 warnx("%s: size mismatch (%d != %d)", __func__,
1177 igmpstat.igps_len, IGPS_VERSION3_LEN);
1180 printf("%s:\n", name);
1182 #define p64(f, m) if (igmpstat.f || sflag <= 1) \
1183 printf(m, (uintmax_t) igmpstat.f, plural(igmpstat.f))
1184 #define py64(f, m) if (igmpstat.f || sflag <= 1) \
1185 printf(m, (uintmax_t) igmpstat.f, pluralies(igmpstat.f))
1186 p64(igps_rcv_total, "\t%ju message%s received\n");
1187 p64(igps_rcv_tooshort, "\t%ju message%s received with too few bytes\n");
1188 p64(igps_rcv_badttl, "\t%ju message%s received with wrong TTL\n");
1189 p64(igps_rcv_badsum, "\t%ju message%s received with bad checksum\n");
1190 py64(igps_rcv_v1v2_queries, "\t%ju V1/V2 membership quer%s received\n");
1191 py64(igps_rcv_v3_queries, "\t%ju V3 membership quer%s received\n");
1192 py64(igps_rcv_badqueries,
1193 "\t%ju membership quer%s received with invalid field(s)\n");
1194 py64(igps_rcv_gen_queries, "\t%ju general quer%s received\n");
1195 py64(igps_rcv_group_queries, "\t%ju group quer%s received\n");
1196 py64(igps_rcv_gsr_queries, "\t%ju group-source quer%s received\n");
1197 py64(igps_drop_gsr_queries, "\t%ju group-source quer%s dropped\n");
1198 p64(igps_rcv_reports, "\t%ju membership report%s received\n");
1199 p64(igps_rcv_badreports,
1200 "\t%ju membership report%s received with invalid field(s)\n");
1201 p64(igps_rcv_ourreports,
1202 "\t%ju membership report%s received for groups to which we belong\n");
1203 p64(igps_rcv_nora, "\t%ju V3 report%s received without Router Alert\n");
1204 p64(igps_snd_reports, "\t%ju membership report%s sent\n");
1210 * Dump PIM statistics structure.
1213 pim_stats(u_long off __unused, const char *name, int af1 __unused,
1216 struct pimstat pimstat, zerostat;
1217 size_t len = sizeof pimstat;
1221 memset(&zerostat, 0, len);
1222 if (sysctlbyname("net.inet.pim.stats", &pimstat, &len,
1223 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1224 if (errno != ENOENT)
1225 warn("sysctl: net.inet.pim.stats");
1231 kread_counters(off, &pimstat, len);
1234 printf("%s:\n", name);
1236 #define p(f, m) if (pimstat.f || sflag <= 1) \
1237 printf(m, (uintmax_t)pimstat.f, plural(pimstat.f))
1238 #define py(f, m) if (pimstat.f || sflag <= 1) \
1239 printf(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
1240 p(pims_rcv_total_msgs, "\t%ju message%s received\n");
1241 p(pims_rcv_total_bytes, "\t%ju byte%s received\n");
1242 p(pims_rcv_tooshort, "\t%ju message%s received with too few bytes\n");
1243 p(pims_rcv_badsum, "\t%ju message%s received with bad checksum\n");
1244 p(pims_rcv_badversion, "\t%ju message%s received with bad version\n");
1245 p(pims_rcv_registers_msgs, "\t%ju data register message%s received\n");
1246 p(pims_rcv_registers_bytes, "\t%ju data register byte%s received\n");
1247 p(pims_rcv_registers_wrongiif,
1248 "\t%ju data register message%s received on wrong iif\n");
1249 p(pims_rcv_badregisters, "\t%ju bad register%s received\n");
1250 p(pims_snd_registers_msgs, "\t%ju data register message%s sent\n");
1251 p(pims_snd_registers_bytes, "\t%ju data register byte%s sent\n");
1257 * Pretty print an Internet address (net address + port).
1260 inetprint(struct in_addr *in, int port, const char *proto, int num_port)
1262 struct servent *sp = 0;
1267 sprintf(line, "%s.", inetname(in));
1269 sprintf(line, "%.*s.", (Aflag && !num_port) ? 12 : 16, inetname(in));
1270 cp = strchr(line, '\0');
1271 if (!num_port && port)
1272 sp = getservbyport((int)port, proto);
1273 if (sp || port == 0)
1274 sprintf(cp, "%.15s ", sp ? sp->s_name : "*");
1276 sprintf(cp, "%d ", ntohs((u_short)port));
1277 width = (Aflag && !Wflag) ? 18 : 22;
1279 printf("%-*s ", width, line);
1281 printf("%-*.*s ", width, width, line);
1285 * Construct an Internet address representation.
1286 * If numeric_addr has been supplied, give
1287 * numeric value, otherwise try for symbolic name.
1290 inetname(struct in_addr *inp)
1293 static char line[MAXHOSTNAMELEN];
1298 if (!numeric_addr && inp->s_addr != INADDR_ANY) {
1299 int net = inet_netof(*inp);
1300 int lna = inet_lnaof(*inp);
1302 if (lna == INADDR_ANY) {
1303 np = getnetbyaddr(net, AF_INET);
1308 hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
1311 trimdomain(cp, strlen(cp));
1315 if (inp->s_addr == INADDR_ANY)
1318 strlcpy(line, cp, sizeof(line));
1320 inp->s_addr = ntohl(inp->s_addr);
1321 #define C(x) ((u_int)((x) & 0xff))
1322 sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
1323 C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));