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/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/sysctl.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_mcnt = sb->sb_mcnt;
146 xsb->sb_ccnt = sb->sb_ccnt;
147 xsb->sb_mbmax = sb->sb_mbmax;
148 xsb->sb_lowat = sb->sb_lowat;
149 xsb->sb_flags = sb->sb_flags;
150 xsb->sb_timeo = sb->sb_timeo;
154 sotoxsocket(struct socket *so, struct xsocket *xso)
156 struct protosw proto;
157 struct domain domain;
159 bzero(xso, sizeof *xso);
160 xso->xso_len = sizeof *xso;
162 xso->so_type = so->so_type;
163 xso->so_options = so->so_options;
164 xso->so_linger = so->so_linger;
165 xso->so_state = so->so_state;
166 xso->so_pcb = so->so_pcb;
167 if (kread((uintptr_t)so->so_proto, &proto, sizeof(proto)) != 0)
169 xso->xso_protocol = proto.pr_protocol;
170 if (kread((uintptr_t)proto.pr_domain, &domain, sizeof(domain)) != 0)
172 xso->xso_family = domain.dom_family;
173 xso->so_qlen = so->so_qlen;
174 xso->so_incqlen = so->so_incqlen;
175 xso->so_qlimit = so->so_qlimit;
176 xso->so_timeo = so->so_timeo;
177 xso->so_error = so->so_error;
178 xso->so_oobmark = so->so_oobmark;
179 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
180 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
185 pcblist_kvm(u_long off, char **bufp, int istcp)
187 struct inpcbinfo pcbinfo;
188 struct inpcbhead listhead;
200 kread(off, &pcbinfo, sizeof(pcbinfo));
202 len = 2 * sizeof(xig) +
203 (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) *
204 sizeof(struct xtcpcb);
206 len = 2 * sizeof(xig) +
207 (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) *
208 sizeof(struct xinpcb);
209 if ((buf = malloc(len)) == 0) {
210 warnx("malloc %lu bytes", (u_long)len);
215 #define COPYOUT(obj, size) do { \
216 if (len < (size)) { \
217 warnx("buffer size exceeded"); \
220 bcopy((obj), p, (size)); \
225 #define KREAD(off, buf, len) do { \
226 if (kread((uintptr_t)(off), (buf), (len)) != 0) \
230 /* Write out header. */
231 xig.xig_len = sizeof xig;
232 xig.xig_count = pcbinfo.ipi_count;
233 xig.xig_gen = pcbinfo.ipi_gencnt;
235 COPYOUT(&xig, sizeof xig);
237 /* Walk the PCB list. */
238 xt.xt_len = sizeof xt;
239 xi.xi_len = sizeof xi;
244 KREAD(pcbinfo.ipi_listhead, &listhead, sizeof(listhead));
245 LIST_FOREACH(inp, &listhead, inp_list) {
247 KREAD(inp, &xt.xt_inp, sizeof(*inp));
250 KREAD(inp, &xi.xi_inp, sizeof(*inp));
254 if (inp->inp_gencnt > pcbinfo.ipi_gencnt)
258 if (inp->inp_ppcb == NULL)
259 bzero(&xt.xt_tp, sizeof xt.xt_tp);
260 else if (inp->inp_flags & INP_TIMEWAIT) {
261 bzero(&xt.xt_tp, sizeof xt.xt_tp);
262 xt.xt_tp.t_state = TCPS_TIME_WAIT;
264 KREAD(inp->inp_ppcb, &xt.xt_tp,
267 if (inp->inp_socket) {
268 KREAD(inp->inp_socket, &so, sizeof(so));
269 if (sotoxsocket(&so, xso) != 0)
272 bzero(xso, sizeof(*xso));
274 xso->xso_protocol = IPPROTO_TCP;
277 COPYOUT(&xt, sizeof xt);
279 COPYOUT(&xi, sizeof xi);
282 /* Reread the pcbinfo and write out the footer. */
283 kread(off, &pcbinfo, sizeof(pcbinfo));
284 xig.xig_count = pcbinfo.ipi_count;
285 xig.xig_gen = pcbinfo.ipi_gencnt;
286 COPYOUT(&xig, sizeof xig);
299 * Print a summary of connections related to an Internet
300 * protocol. For TCP, also give state of connection.
301 * Listening processes (aflag) are suppressed unless the
302 * -a (all) flag is specified.
305 protopr(u_long off, const char *name, int af1, int proto)
308 static int first = 1;
311 struct tcpcb *tp = NULL;
313 struct xinpgen *xig, *oxig;
337 if (!pcblist_sysctl(proto, &buf, istcp))
340 if (!pcblist_kvm(off, &buf, istcp))
344 oxig = xig = (struct xinpgen *)buf;
345 for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
346 xig->xig_len > sizeof(struct xinpgen);
347 xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
349 tp = &((struct xtcpcb *)xig)->xt_tp;
350 inp = &((struct xtcpcb *)xig)->xt_inp;
351 so = &((struct xtcpcb *)xig)->xt_socket;
353 inp = &((struct xinpcb *)xig)->xi_inp;
354 so = &((struct xinpcb *)xig)->xi_socket;
357 /* Ignore sockets for protocols other than the desired one. */
358 if (so->xso_protocol != proto)
361 /* Ignore PCBs which were freed during copyout. */
362 if (inp->inp_gencnt > oxig->xig_gen)
365 if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
367 || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
369 || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
371 && (inp->inp_vflag & INP_IPV6) == 0
378 (istcp && tp->t_state == TCPS_LISTEN)
379 || (af1 == AF_INET &&
380 inet_lnaof(inp->inp_laddr) == INADDR_ANY)
382 || (af1 == AF_INET6 &&
383 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
385 || (af1 == AF_UNSPEC &&
386 (((inp->inp_vflag & INP_IPV4) != 0 &&
387 inet_lnaof(inp->inp_laddr) == INADDR_ANY)
389 || ((inp->inp_vflag & INP_IPV6) != 0 &&
390 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
398 printf("Active Internet connections");
400 printf(" (including servers)");
403 "Current listen queue sizes (qlen/incqlen/maxqlen)");
406 printf("%-8.8s ", "Tcpcb");
408 printf("%-5.5s %-14.14s %-22.22s\n",
409 "Proto", "Listen", "Local Address");
411 printf((Aflag && !Wflag) ?
412 "%-5.5s %-6.6s %-6.6s %-18.18s %-18.18s" :
413 "%-5.5s %-6.6s %-6.6s %-22.22s %-22.22s",
414 "Proto", "Recv-Q", "Send-Q",
415 "Local Address", "Foreign Address");
417 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 %s\n",
418 "R-MBUF", "S-MBUF", "R-CLUS",
419 "S-CLUS", "R-HIWA", "S-HIWA",
420 "R-LOWA", "S-LOWA", "R-BCNT",
421 "S-BCNT", "R-BMAX", "S-BMAX",
428 if (Lflag && so->so_qlimit == 0)
432 printf("%8lx ", (u_long)inp->inp_ppcb);
434 printf("%8lx ", (u_long)so->so_pcb);
437 if ((inp->inp_vflag & INP_IPV6) != 0)
438 vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
442 vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
444 printf("%-3.3s%-2.2s ", name, vchar);
448 snprintf(buf1, 15, "%d/%d/%d", so->so_qlen,
449 so->so_incqlen, so->so_qlimit);
450 printf("%-14.14s ", buf1);
452 printf("%6u %6u ", so->so_rcv.sb_cc, so->so_snd.sb_cc);
455 if (inp->inp_vflag & INP_IPV4) {
456 inetprint(&inp->inp_laddr, (int)inp->inp_lport,
459 inetprint(&inp->inp_faddr,
460 (int)inp->inp_fport, name, 1);
463 else if (inp->inp_vflag & INP_IPV6) {
464 inet6print(&inp->in6p_laddr,
465 (int)inp->inp_lport, name, 1);
467 inet6print(&inp->in6p_faddr,
468 (int)inp->inp_fport, name, 1);
469 } /* else nothing printed now */
471 } else if (inp->inp_flags & INP_ANONPORT) {
472 if (inp->inp_vflag & INP_IPV4) {
473 inetprint(&inp->inp_laddr, (int)inp->inp_lport,
476 inetprint(&inp->inp_faddr,
477 (int)inp->inp_fport, name, 0);
480 else if (inp->inp_vflag & INP_IPV6) {
481 inet6print(&inp->in6p_laddr,
482 (int)inp->inp_lport, name, 1);
484 inet6print(&inp->in6p_faddr,
485 (int)inp->inp_fport, name, 0);
486 } /* else nothing printed now */
489 if (inp->inp_vflag & INP_IPV4) {
490 inetprint(&inp->inp_laddr, (int)inp->inp_lport,
493 inetprint(&inp->inp_faddr,
494 (int)inp->inp_fport, name,
495 inp->inp_lport != inp->inp_fport);
498 else if (inp->inp_vflag & INP_IPV6) {
499 inet6print(&inp->in6p_laddr,
500 (int)inp->inp_lport, name, 0);
502 inet6print(&inp->in6p_faddr,
503 (int)inp->inp_fport, name,
504 inp->inp_lport != inp->inp_fport);
505 } /* else nothing printed now */
510 printf("%21s %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u ",
512 so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt,
513 so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt,
514 so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
515 so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
516 so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
517 so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
519 printf("%6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u ",
520 so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt,
521 so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt,
522 so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
523 so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
524 so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
525 so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
527 if (istcp && !Lflag) {
528 if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
529 printf("%d", tp->t_state);
531 printf("%s", tcpstates[tp->t_state]);
532 #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
533 /* Show T/TCP `hidden state' */
534 if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
536 #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
541 if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
542 if (oxig->xig_count > xig->xig_count) {
543 printf("Some %s sockets may have been deleted.\n",
545 } else if (oxig->xig_count < xig->xig_count) {
546 printf("Some %s sockets may have been created.\n",
550 "Some %s sockets may have been created or deleted.\n",
558 * Dump TCP statistics structure.
561 tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
563 struct tcpstat tcpstat, zerostat;
564 size_t len = sizeof tcpstat;
575 memset(&zerostat, 0, len);
576 if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &len,
577 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
578 warn("sysctl: net.inet.tcp.stats");
582 kread(off, &tcpstat, len);
584 printf ("%s:\n", name);
586 #define p(f, m) if (tcpstat.f || sflag <= 1) \
587 printf(m, tcpstat.f, plural(tcpstat.f))
588 #define p1a(f, m) if (tcpstat.f || sflag <= 1) \
590 #define p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
591 printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2))
592 #define p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
593 printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2)
594 #define p3(f, m) if (tcpstat.f || sflag <= 1) \
595 printf(m, tcpstat.f, pluralies(tcpstat.f))
597 p(tcps_sndtotal, "\t%lu packet%s sent\n");
598 p2(tcps_sndpack,tcps_sndbyte, "\t\t%lu data packet%s (%lu byte%s)\n");
599 p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
600 "\t\t%lu data packet%s (%lu byte%s) retransmitted\n");
602 "\t\t%lu data packet%s unnecessarily retransmitted\n");
603 p(tcps_mturesent, "\t\t%lu resend%s initiated by MTU discovery\n");
604 p2a(tcps_sndacks, tcps_delack,
605 "\t\t%lu ack-only packet%s (%lu delayed)\n");
606 p(tcps_sndurg, "\t\t%lu URG only packet%s\n");
607 p(tcps_sndprobe, "\t\t%lu window probe packet%s\n");
608 p(tcps_sndwinup, "\t\t%lu window update packet%s\n");
609 p(tcps_sndctrl, "\t\t%lu control packet%s\n");
610 p(tcps_rcvtotal, "\t%lu packet%s received\n");
611 p2(tcps_rcvackpack, tcps_rcvackbyte,
612 "\t\t%lu ack%s (for %lu byte%s)\n");
613 p(tcps_rcvdupack, "\t\t%lu duplicate ack%s\n");
614 p(tcps_rcvacktoomuch, "\t\t%lu ack%s for unsent data\n");
615 p2(tcps_rcvpack, tcps_rcvbyte,
616 "\t\t%lu packet%s (%lu byte%s) received in-sequence\n");
617 p2(tcps_rcvduppack, tcps_rcvdupbyte,
618 "\t\t%lu completely duplicate packet%s (%lu byte%s)\n");
619 p(tcps_pawsdrop, "\t\t%lu old duplicate packet%s\n");
620 p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
621 "\t\t%lu packet%s with some dup. data (%lu byte%s duped)\n");
622 p2(tcps_rcvoopack, tcps_rcvoobyte,
623 "\t\t%lu out-of-order packet%s (%lu byte%s)\n");
624 p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
625 "\t\t%lu packet%s (%lu byte%s) of data after window\n");
626 p(tcps_rcvwinprobe, "\t\t%lu window probe%s\n");
627 p(tcps_rcvwinupd, "\t\t%lu window update packet%s\n");
628 p(tcps_rcvafterclose, "\t\t%lu packet%s received after close\n");
629 p(tcps_rcvbadsum, "\t\t%lu discarded for bad checksum%s\n");
630 p(tcps_rcvbadoff, "\t\t%lu discarded for bad header offset field%s\n");
631 p1a(tcps_rcvshort, "\t\t%lu discarded because packet too short\n");
632 p1a(tcps_rcvmemdrop, "\t\t%lu discarded due to memory problems\n");
633 p(tcps_connattempt, "\t%lu connection request%s\n");
634 p(tcps_accepts, "\t%lu connection accept%s\n");
635 p(tcps_badsyn, "\t%lu bad connection attempt%s\n");
636 p(tcps_listendrop, "\t%lu listen queue overflow%s\n");
637 p(tcps_badrst, "\t%lu ignored RSTs in the window%s\n");
638 p(tcps_connects, "\t%lu connection%s established (including accepts)\n");
639 p2(tcps_closed, tcps_drops,
640 "\t%lu connection%s closed (including %lu drop%s)\n");
641 p(tcps_cachedrtt, "\t\t%lu connection%s updated cached RTT on close\n");
643 "\t\t%lu connection%s updated cached RTT variance on close\n");
644 p(tcps_cachedssthresh,
645 "\t\t%lu connection%s updated cached ssthresh on close\n");
646 p(tcps_conndrops, "\t%lu embryonic connection%s dropped\n");
647 p2(tcps_rttupdated, tcps_segstimed,
648 "\t%lu segment%s updated rtt (of %lu attempt%s)\n");
649 p(tcps_rexmttimeo, "\t%lu retransmit timeout%s\n");
650 p(tcps_timeoutdrop, "\t\t%lu connection%s dropped by rexmit timeout\n");
651 p(tcps_persisttimeo, "\t%lu persist timeout%s\n");
652 p(tcps_persistdrop, "\t\t%lu connection%s dropped by persist timeout\n");
653 p(tcps_finwait2_drops,
654 "\t%lu Connection%s (fin_wait_2) dropped because of timeout\n");
655 p(tcps_keeptimeo, "\t%lu keepalive timeout%s\n");
656 p(tcps_keepprobe, "\t\t%lu keepalive probe%s sent\n");
657 p(tcps_keepdrops, "\t\t%lu connection%s dropped by keepalive\n");
658 p(tcps_predack, "\t%lu correct ACK header prediction%s\n");
659 p(tcps_preddat, "\t%lu correct data packet header prediction%s\n");
661 p3(tcps_sc_added, "\t%lu syncache entr%s added\n");
662 p1a(tcps_sc_retransmitted, "\t\t%lu retransmitted\n");
663 p1a(tcps_sc_dupsyn, "\t\t%lu dupsyn\n");
664 p1a(tcps_sc_dropped, "\t\t%lu dropped\n");
665 p1a(tcps_sc_completed, "\t\t%lu completed\n");
666 p1a(tcps_sc_bucketoverflow, "\t\t%lu bucket overflow\n");
667 p1a(tcps_sc_cacheoverflow, "\t\t%lu cache overflow\n");
668 p1a(tcps_sc_reset, "\t\t%lu reset\n");
669 p1a(tcps_sc_stale, "\t\t%lu stale\n");
670 p1a(tcps_sc_aborted, "\t\t%lu aborted\n");
671 p1a(tcps_sc_badack, "\t\t%lu badack\n");
672 p1a(tcps_sc_unreach, "\t\t%lu unreach\n");
673 p(tcps_sc_zonefail, "\t\t%lu zone failure%s\n");
674 p(tcps_sc_sendcookie, "\t%lu cookie%s sent\n");
675 p(tcps_sc_recvcookie, "\t%lu cookie%s received\n");
677 p(tcps_sack_recovery_episode, "\t%lu SACK recovery episode%s\n");
679 "\t%lu segment rexmit%s in SACK recovery episodes\n");
680 p(tcps_sack_rexmit_bytes,
681 "\t%lu byte rexmit%s in SACK recovery episodes\n");
682 p(tcps_sack_rcv_blocks,
683 "\t%lu SACK option%s (SACK blocks) received\n");
684 p(tcps_sack_send_blocks, "\t%lu SACK option%s (SACK blocks) sent\n");
685 p1a(tcps_sack_sboverflow, "\t%lu SACK scoreboard overflow\n");
687 p(tcps_ecn_ce, "\t%lu packet%s with ECN CE bit set\n");
688 p(tcps_ecn_ect0, "\t%lu packet%s with ECN ECT(0) bit set\n");
689 p(tcps_ecn_ect1, "\t%lu packet%s with ECN ECT(1) bit set\n");
690 p(tcps_ecn_shs, "\t%lu successful ECN handshake%s\n");
691 p(tcps_ecn_rcwnd, "\t%lu time%s ECN reduced the congestion window\n");
700 * Dump UDP statistics structure.
703 udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
705 struct udpstat udpstat, zerostat;
706 size_t len = sizeof udpstat;
718 memset(&zerostat, 0, len);
719 if (sysctlbyname("net.inet.udp.stats", &udpstat, &len,
720 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
721 warn("sysctl: net.inet.udp.stats");
725 kread(off, &udpstat, len);
727 printf("%s:\n", name);
728 #define p(f, m) if (udpstat.f || sflag <= 1) \
729 printf(m, udpstat.f, plural(udpstat.f))
730 #define p1a(f, m) if (udpstat.f || sflag <= 1) \
732 p(udps_ipackets, "\t%lu datagram%s received\n");
733 p1a(udps_hdrops, "\t%lu with incomplete header\n");
734 p1a(udps_badlen, "\t%lu with bad data length field\n");
735 p1a(udps_badsum, "\t%lu with bad checksum\n");
736 p1a(udps_nosum, "\t%lu with no checksum\n");
737 p1a(udps_noport, "\t%lu dropped due to no socket\n");
739 "\t%lu broadcast/multicast datagram%s undelivered\n");
740 p1a(udps_fullsock, "\t%lu dropped due to full socket buffers\n");
741 p1a(udpps_pcbhashmiss, "\t%lu not for hashed pcb\n");
742 delivered = udpstat.udps_ipackets -
743 udpstat.udps_hdrops -
744 udpstat.udps_badlen -
745 udpstat.udps_badsum -
746 udpstat.udps_noport -
747 udpstat.udps_noportbcast -
748 udpstat.udps_fullsock;
749 if (delivered || sflag <= 1)
750 printf("\t%lu delivered\n", delivered);
751 p(udps_opackets, "\t%lu datagram%s output\n");
752 /* the next statistic is cumulative in udps_noportbcast */
754 "\t%lu time%s multicast source filter matched\n");
760 * Dump CARP statistics structure.
763 carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
765 struct carpstats carpstat, zerostat;
766 size_t len = sizeof(struct carpstats);
770 memset(&zerostat, 0, len);
771 if (sysctlbyname("net.inet.carp.stats", &carpstat, &len,
772 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
774 warn("sysctl: net.inet.carp.stats");
780 kread(off, &carpstat, len);
783 printf("%s:\n", name);
785 #define p(f, m) if (carpstat.f || sflag <= 1) \
786 printf(m, (uintmax_t)carpstat.f, plural(carpstat.f))
787 #define p2(f, m) if (carpstat.f || sflag <= 1) \
788 printf(m, (uintmax_t)carpstat.f)
790 p(carps_ipackets, "\t%ju packet%s received (IPv4)\n");
791 p(carps_ipackets6, "\t%ju packet%s received (IPv6)\n");
792 p(carps_badttl, "\t\t%ju packet%s discarded for wrong TTL\n");
793 p(carps_hdrops, "\t\t%ju packet%s shorter than header\n");
794 p(carps_badsum, "\t\t%ju discarded for bad checksum%s\n");
795 p(carps_badver, "\t\t%ju discarded packet%s with a bad version\n");
796 p2(carps_badlen, "\t\t%ju discarded because packet too short\n");
797 p2(carps_badauth, "\t\t%ju discarded for bad authentication\n");
798 p2(carps_badvhid, "\t\t%ju discarded for bad vhid\n");
799 p2(carps_badaddrs, "\t\t%ju discarded because of a bad address list\n");
800 p(carps_opackets, "\t%ju packet%s sent (IPv4)\n");
801 p(carps_opackets6, "\t%ju packet%s sent (IPv6)\n");
802 p2(carps_onomem, "\t\t%ju send failed due to mbuf memory error\n");
804 p(carps_ostates, "\t\t%s state update%s sent\n");
811 * Dump IP statistics structure.
814 ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
816 struct ipstat ipstat, zerostat;
817 size_t len = sizeof ipstat;
821 memset(&zerostat, 0, len);
822 if (sysctlbyname("net.inet.ip.stats", &ipstat, &len,
823 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
824 warn("sysctl: net.inet.ip.stats");
828 kread(off, &ipstat, len);
830 printf("%s:\n", name);
832 #define p(f, m) if (ipstat.f || sflag <= 1) \
833 printf(m, ipstat.f, plural(ipstat.f))
834 #define p1a(f, m) if (ipstat.f || sflag <= 1) \
837 p(ips_total, "\t%lu total packet%s received\n");
838 p(ips_badsum, "\t%lu bad header checksum%s\n");
839 p1a(ips_toosmall, "\t%lu with size smaller than minimum\n");
840 p1a(ips_tooshort, "\t%lu with data size < data length\n");
841 p1a(ips_toolong, "\t%lu with ip length > max ip packet size\n");
842 p1a(ips_badhlen, "\t%lu with header length < data size\n");
843 p1a(ips_badlen, "\t%lu with data length < header length\n");
844 p1a(ips_badoptions, "\t%lu with bad options\n");
845 p1a(ips_badvers, "\t%lu with incorrect version number\n");
846 p(ips_fragments, "\t%lu fragment%s received\n");
847 p(ips_fragdropped, "\t%lu fragment%s dropped (dup or out of space)\n");
848 p(ips_fragtimeout, "\t%lu fragment%s dropped after timeout\n");
849 p(ips_reassembled, "\t%lu packet%s reassembled ok\n");
850 p(ips_delivered, "\t%lu packet%s for this host\n");
851 p(ips_noproto, "\t%lu packet%s for unknown/unsupported protocol\n");
852 p(ips_forward, "\t%lu packet%s forwarded");
853 p(ips_fastforward, " (%lu packet%s fast forwarded)");
854 if (ipstat.ips_forward || sflag <= 1)
856 p(ips_cantforward, "\t%lu packet%s not forwardable\n");
858 "\t%lu packet%s received for unknown multicast group\n");
859 p(ips_redirectsent, "\t%lu redirect%s sent\n");
860 p(ips_localout, "\t%lu packet%s sent from this host\n");
861 p(ips_rawout, "\t%lu packet%s sent with fabricated ip header\n");
863 "\t%lu output packet%s dropped due to no bufs, etc.\n");
864 p(ips_noroute, "\t%lu output packet%s discarded due to no route\n");
865 p(ips_fragmented, "\t%lu output datagram%s fragmented\n");
866 p(ips_ofragments, "\t%lu fragment%s created\n");
867 p(ips_cantfrag, "\t%lu datagram%s that can't be fragmented\n");
868 p(ips_nogif, "\t%lu tunneling packet%s that can't find gif\n");
869 p(ips_badaddr, "\t%lu datagram%s with bad address in header\n");
874 static const char *icmpnames[ICMP_MAXTYPE + 1] = {
875 "echo reply", /* RFC 792 */
878 "destination unreachable", /* RFC 792 */
879 "source quench", /* RFC 792 */
880 "routing redirect", /* RFC 792 */
883 "echo", /* RFC 792 */
884 "router advertisement", /* RFC 1256 */
885 "router solicitation", /* RFC 1256 */
886 "time exceeded", /* RFC 792 */
887 "parameter problem", /* RFC 792 */
888 "time stamp", /* RFC 792 */
889 "time stamp reply", /* RFC 792 */
890 "information request", /* RFC 792 */
891 "information request reply", /* RFC 792 */
892 "address mask request", /* RFC 950 */
893 "address mask reply", /* RFC 950 */
905 "icmp traceroute", /* RFC 1393 */
906 "datagram conversion error", /* RFC 1475 */
907 "mobile host redirect",
908 "IPv6 where-are-you",
910 "mobile registration req",
911 "mobile registration reply",
912 "domain name request", /* RFC 1788 */
913 "domain name reply", /* RFC 1788 */
915 "icmp photuris", /* RFC 2521 */
919 * Dump ICMP statistics.
922 icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
924 struct icmpstat icmpstat, zerostat;
928 len = sizeof icmpstat;
931 memset(&zerostat, 0, len);
932 if (sysctlbyname("net.inet.icmp.stats", &icmpstat, &len,
933 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
934 warn("sysctl: net.inet.icmp.stats");
938 kread(off, &icmpstat, len);
940 printf("%s:\n", name);
942 #define p(f, m) if (icmpstat.f || sflag <= 1) \
943 printf(m, icmpstat.f, plural(icmpstat.f))
944 #define p1a(f, m) if (icmpstat.f || sflag <= 1) \
945 printf(m, icmpstat.f)
946 #define p2(f, m) if (icmpstat.f || sflag <= 1) \
947 printf(m, icmpstat.f, plurales(icmpstat.f))
949 p(icps_error, "\t%lu call%s to icmp_error\n");
951 "\t%lu error%s not generated in response to an icmp message\n");
952 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
953 if (icmpstat.icps_outhist[i] != 0) {
955 printf("\tOutput histogram:\n");
958 if (icmpnames[i] != NULL)
959 printf("\t\t%s: %lu\n", icmpnames[i],
960 icmpstat.icps_outhist[i]);
962 printf("\t\tunknown ICMP #%d: %lu\n", i,
963 icmpstat.icps_outhist[i]);
965 p(icps_badcode, "\t%lu message%s with bad code fields\n");
966 p(icps_tooshort, "\t%lu message%s less than the minimum length\n");
967 p(icps_checksum, "\t%lu message%s with bad checksum\n");
968 p(icps_badlen, "\t%lu message%s with bad length\n");
969 p1a(icps_bmcastecho, "\t%lu multicast echo requests ignored\n");
970 p1a(icps_bmcasttstamp, "\t%lu multicast timestamp requests ignored\n");
971 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
972 if (icmpstat.icps_inhist[i] != 0) {
974 printf("\tInput histogram:\n");
977 if (icmpnames[i] != NULL)
978 printf("\t\t%s: %lu\n", icmpnames[i],
979 icmpstat.icps_inhist[i]);
981 printf("\t\tunknown ICMP #%d: %lu\n", i,
982 icmpstat.icps_inhist[i]);
984 p(icps_reflect, "\t%lu message response%s generated\n");
985 p2(icps_badaddr, "\t%lu invalid return address%s\n");
986 p(icps_noroute, "\t%lu no return route%s\n");
992 if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) <
995 printf("\tICMP address mask responses are %sabled\n",
1000 #ifndef BURN_BRIDGES
1002 * Dump IGMP statistics structure (pre 8.x kernel).
1005 igmp_stats_live_old(u_long off, const char *name)
1007 struct oigmpstat oigmpstat, zerostat;
1008 size_t len = sizeof(oigmpstat);
1011 memset(&zerostat, 0, len);
1012 if (sysctlbyname("net.inet.igmp.stats", &oigmpstat, &len,
1013 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1014 warn("sysctl: net.inet.igmp.stats");
1018 printf("%s:\n", name);
1020 #define p(f, m) if (oigmpstat.f || sflag <= 1) \
1021 printf(m, oigmpstat.f, plural(oigmpstat.f))
1022 #define py(f, m) if (oigmpstat.f || sflag <= 1) \
1023 printf(m, oigmpstat.f, oigmpstat.f != 1 ? "ies" : "y")
1024 p(igps_rcv_total, "\t%u message%s received\n");
1025 p(igps_rcv_tooshort, "\t%u message%s received with too few bytes\n");
1026 p(igps_rcv_badsum, "\t%u message%s received with bad checksum\n");
1027 py(igps_rcv_queries, "\t%u membership quer%s received\n");
1028 py(igps_rcv_badqueries,
1029 "\t%u membership quer%s received with invalid field(s)\n");
1030 p(igps_rcv_reports, "\t%u membership report%s received\n");
1031 p(igps_rcv_badreports,
1032 "\t%u membership report%s received with invalid field(s)\n");
1033 p(igps_rcv_ourreports,
1034 "\t%u membership report%s received for groups to which we belong\n");
1035 p(igps_snd_reports, "\t%u membership report%s sent\n");
1039 #endif /* !BURN_BRIDGES */
1042 * Dump IGMP statistics structure.
1045 igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1047 struct igmpstat igmpstat, zerostat;
1050 #ifndef BURN_BRIDGES
1053 * Detect if we are being run against a pre-IGMPv3 kernel.
1054 * We cannot do this for a core file as the legacy
1055 * struct igmpstat has no size field, nor does it
1056 * export it in any readily-available symbols.
1059 if (sysctlbyname("net.inet.igmp.stats", NULL, &len, NULL,
1061 warn("sysctl: net.inet.igmp.stats");
1064 if (len < sizeof(igmpstat)) {
1065 igmp_stats_live_old(off, name);
1069 #endif /* !BURN_BRIDGES */
1071 len = sizeof(igmpstat);
1074 memset(&zerostat, 0, len);
1075 if (sysctlbyname("net.inet.igmp.stats", &igmpstat, &len,
1076 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1077 warn("sysctl: net.inet.igmp.stats");
1081 len = sizeof(igmpstat);
1082 kread(off, &igmpstat, len);
1085 if (igmpstat.igps_version != IGPS_VERSION_3) {
1086 warnx("%s: version mismatch (%d != %d)", __func__,
1087 igmpstat.igps_version, IGPS_VERSION_3);
1089 if (igmpstat.igps_len != IGPS_VERSION3_LEN) {
1090 warnx("%s: size mismatch (%d != %d)", __func__,
1091 igmpstat.igps_len, IGPS_VERSION3_LEN);
1094 printf("%s:\n", name);
1096 #define p64(f, m) if (igmpstat.f || sflag <= 1) \
1097 printf(m, (uintmax_t) igmpstat.f, plural(igmpstat.f))
1098 #define py64(f, m) if (igmpstat.f || sflag <= 1) \
1099 printf(m, (uintmax_t) igmpstat.f, pluralies(igmpstat.f))
1100 p64(igps_rcv_total, "\t%ju message%s received\n");
1101 p64(igps_rcv_tooshort, "\t%ju message%s received with too few bytes\n");
1102 p64(igps_rcv_badttl, "\t%ju message%s received with wrong TTL\n");
1103 p64(igps_rcv_badsum, "\t%ju message%s received with bad checksum\n");
1104 py64(igps_rcv_v1v2_queries, "\t%ju V1/V2 membership quer%s received\n");
1105 py64(igps_rcv_v3_queries, "\t%ju V3 membership quer%s received\n");
1106 py64(igps_rcv_badqueries,
1107 "\t%ju membership quer%s received with invalid field(s)\n");
1108 py64(igps_rcv_gen_queries, "\t%ju general quer%s received\n");
1109 py64(igps_rcv_group_queries, "\t%ju group quer%s received\n");
1110 py64(igps_rcv_gsr_queries, "\t%ju group-source quer%s received\n");
1111 py64(igps_drop_gsr_queries, "\t%ju group-source quer%s dropped\n");
1112 p64(igps_rcv_reports, "\t%ju membership report%s received\n");
1113 p64(igps_rcv_badreports,
1114 "\t%ju membership report%s received with invalid field(s)\n");
1115 p64(igps_rcv_ourreports,
1116 "\t%ju membership report%s received for groups to which we belong\n");
1117 p64(igps_rcv_nora, "\t%ju V3 report%s received without Router Alert\n");
1118 p64(igps_snd_reports, "\t%ju membership report%s sent\n");
1124 * Dump PIM statistics structure.
1127 pim_stats(u_long off __unused, const char *name, int af1 __unused,
1130 struct pimstat pimstat, zerostat;
1131 size_t len = sizeof pimstat;
1135 memset(&zerostat, 0, len);
1136 if (sysctlbyname("net.inet.pim.stats", &pimstat, &len,
1137 zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1138 if (errno != ENOENT)
1139 warn("sysctl: net.inet.pim.stats");
1145 kread(off, &pimstat, len);
1148 printf("%s:\n", name);
1150 #define p(f, m) if (pimstat.f || sflag <= 1) \
1151 printf(m, (uintmax_t)pimstat.f, plural(pimstat.f))
1152 #define py(f, m) if (pimstat.f || sflag <= 1) \
1153 printf(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
1154 p(pims_rcv_total_msgs, "\t%ju message%s received\n");
1155 p(pims_rcv_total_bytes, "\t%ju byte%s received\n");
1156 p(pims_rcv_tooshort, "\t%ju message%s received with too few bytes\n");
1157 p(pims_rcv_badsum, "\t%ju message%s received with bad checksum\n");
1158 p(pims_rcv_badversion, "\t%ju message%s received with bad version\n");
1159 p(pims_rcv_registers_msgs, "\t%ju data register message%s received\n");
1160 p(pims_rcv_registers_bytes, "\t%ju data register byte%s received\n");
1161 p(pims_rcv_registers_wrongiif,
1162 "\t%ju data register message%s received on wrong iif\n");
1163 p(pims_rcv_badregisters, "\t%ju bad register%s received\n");
1164 p(pims_snd_registers_msgs, "\t%ju data register message%s sent\n");
1165 p(pims_snd_registers_bytes, "\t%ju data register byte%s sent\n");
1171 * Pretty print an Internet address (net address + port).
1174 inetprint(struct in_addr *in, int port, const char *proto, int num_port)
1176 struct servent *sp = 0;
1181 sprintf(line, "%s.", inetname(in));
1183 sprintf(line, "%.*s.", (Aflag && !num_port) ? 12 : 16, inetname(in));
1184 cp = index(line, '\0');
1185 if (!num_port && port)
1186 sp = getservbyport((int)port, proto);
1187 if (sp || port == 0)
1188 sprintf(cp, "%.15s ", sp ? sp->s_name : "*");
1190 sprintf(cp, "%d ", ntohs((u_short)port));
1191 width = (Aflag && !Wflag) ? 18 : 22;
1193 printf("%-*s ", width, line);
1195 printf("%-*.*s ", width, width, line);
1199 * Construct an Internet address representation.
1200 * If numeric_addr has been supplied, give
1201 * numeric value, otherwise try for symbolic name.
1204 inetname(struct in_addr *inp)
1207 static char line[MAXHOSTNAMELEN];
1212 if (!numeric_addr && inp->s_addr != INADDR_ANY) {
1213 int net = inet_netof(*inp);
1214 int lna = inet_lnaof(*inp);
1216 if (lna == INADDR_ANY) {
1217 np = getnetbyaddr(net, AF_INET);
1222 hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
1225 trimdomain(cp, strlen(cp));
1229 if (inp->s_addr == INADDR_ANY)
1232 strlcpy(line, cp, sizeof(line));
1234 inp->s_addr = ntohl(inp->s_addr);
1235 #define C(x) ((u_int)((x) & 0xff))
1236 sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
1237 C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));