/*
* Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright (c) 1983, 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Print a summary of connections related to an Internet
* protocol. For TCP, also give state of connection.
* Listening processes (aflag) are suppressed unless the
* -a (all) flag is specified.
*/
static int width;
static int compact;
if (prog_sysctlbyname("net.inet6.udp6.stats", udp6stat, &size,
NULL, 0) == -1 && errno != ENOMEM)
return;
} else {
warnx("%s stats not available via KVM.", name);
return;
}
printf("%s:\n", name);
#define p(f, m) if (udp6stat[f] || sflag <= 1) \
printf(m, (unsigned long long)udp6stat[f], plural(udp6stat[f]))
#define p1(f, m) if (udp6stat[f] || sflag <= 1) \
printf(m, (unsigned long long)udp6stat[f])
p(UDP6_STAT_IPACKETS, "\t%llu datagram%s received\n");
p1(UDP6_STAT_HDROPS, "\t%llu with incomplete header\n");
p1(UDP6_STAT_BADLEN, "\t%llu with bad data length field\n");
p1(UDP6_STAT_BADSUM, "\t%llu with bad checksum\n");
p1(UDP6_STAT_NOSUM, "\t%llu with no checksum\n");
p1(UDP6_STAT_NOPORT, "\t%llu dropped due to no socket\n");
p(UDP6_STAT_NOPORTMCAST,
"\t%llu multicast datagram%s dropped due to no socket\n");
p1(UDP6_STAT_FULLSOCK, "\t%llu dropped due to full socket buffers\n");
delivered = udp6stat[UDP6_STAT_IPACKETS] -
udp6stat[UDP6_STAT_HDROPS] -
udp6stat[UDP6_STAT_BADLEN] -
udp6stat[UDP6_STAT_BADSUM] -
udp6stat[UDP6_STAT_NOPORT] -
udp6stat[UDP6_STAT_NOPORTMCAST] -
udp6stat[UDP6_STAT_FULLSOCK];
if (delivered || sflag <= 1)
printf("\t%llu delivered\n", (unsigned long long)delivered);
p(UDP6_STAT_OPACKETS, "\t%llu datagram%s output\n");
#undef p
#undef p1
}
if (prog_sysctlbyname("net.inet6.ip6.stats", ip6stat, &size,
NULL, 0) == -1 && errno != ENOMEM)
return;
} else {
warnx("%s stats not available via KVM.", name);
return;
}
printf("%s:\n", name);
#define p(f, m) if (ip6stat[f] || sflag <= 1) \
printf(m, (unsigned long long)ip6stat[f], plural(ip6stat[f]))
#define p1(f, m) if (ip6stat[f] || sflag <= 1) \
printf(m, (unsigned long long)ip6stat[f])
p(IP6_STAT_TOTAL, "\t%llu total packet%s received\n");
p1(IP6_STAT_TOOSMALL, "\t%llu with size smaller than minimum\n");
p1(IP6_STAT_TOOSHORT, "\t%llu with data size < data length\n");
p1(IP6_STAT_BADOPTIONS, "\t%llu with bad options\n");
p1(IP6_STAT_BADVERS, "\t%llu with incorrect version number\n");
p(IP6_STAT_FRAGMENTS, "\t%llu fragment%s received\n");
p(IP6_STAT_FRAGDROPPED,
"\t%llu fragment%s dropped (dup or out of space)\n");
p(IP6_STAT_FRAGTIMEOUT, "\t%llu fragment%s dropped after timeout\n");
p(IP6_STAT_FRAGOVERFLOW, "\t%llu fragment%s that exceeded limit\n");
p(IP6_STAT_REASSEMBLED, "\t%llu packet%s reassembled ok\n");
p(IP6_STAT_DELIVERED, "\t%llu packet%s for this host\n");
p(IP6_STAT_FORWARD, "\t%llu packet%s forwarded\n");
p(IP6_STAT_FASTFORWARD, "\t%llu packet%s fast forwarded\n");
p1(IP6_STAT_FASTFORWARDFLOWS, "\t%llu fast forward flows\n");
p(IP6_STAT_CANTFORWARD, "\t%llu packet%s not forwardable\n");
p(IP6_STAT_REDIRECTSENT, "\t%llu redirect%s sent\n");
p(IP6_STAT_LOCALOUT, "\t%llu packet%s sent from this host\n");
p(IP6_STAT_RAWOUT, "\t%llu packet%s sent with fabricated ip header\n");
p(IP6_STAT_ODROPPED,
"\t%llu output packet%s dropped due to no bufs, etc.\n");
p(IP6_STAT_NOROUTE,
"\t%llu output packet%s discarded due to no route\n");
p(IP6_STAT_FRAGMENTED, "\t%llu output datagram%s fragmented\n");
p(IP6_STAT_OFRAGMENTS, "\t%llu fragment%s created\n");
p(IP6_STAT_CANTFRAG, "\t%llu datagram%s that can't be fragmented\n");
p(IP6_STAT_BADSCOPE, "\t%llu packet%s that violated scope rules\n");
p(IP6_STAT_NOTMEMBER, "\t%llu multicast packet%s which we don't join\n");
for (first = 1, i = 0; i < 256; i++)
if (ip6stat[IP6_STAT_NXTHIST + i] != 0) {
if (first) {
printf("\tInput packet histogram:\n");
first = 0;
}
n = NULL;
if (ip6nh[i])
n = ip6nh[i];
else if ((ep = getprotobynumber(i)) != NULL)
n = ep->p_name;
if (n)
printf("\t\t%s: %llu\n", n,
(unsigned long long)ip6stat[IP6_STAT_NXTHIST + i]);
else
printf("\t\t#%d: %llu\n", i,
(unsigned long long)ip6stat[IP6_STAT_NXTHIST + i]);
}
printf("\tMbuf statistics:\n");
p(IP6_STAT_M1, "\t\t%llu one mbuf%s\n");
for (first = 1, i = 0; i < 32; i++) {
char ifbuf[IFNAMSIZ];
if (ip6stat[IP6_STAT_M2M + i] != 0) {
if (first) {
printf("\t\ttwo or more mbuf:\n");
first = 0;
}
printf("\t\t\t%s = %llu\n",
if_indextoname(i, ifbuf),
(unsigned long long)ip6stat[IP6_STAT_M2M + i]);
}
}
p(IP6_STAT_MEXT1, "\t\t%llu one ext mbuf%s\n");
p(IP6_STAT_MEXT2M, "\t\t%llu two or more ext mbuf%s\n");
p(IP6_STAT_EXTHDRTOOLONG,
"\t%llu packet%s whose headers are not continuous\n");
p(IP6_STAT_NOGIF, "\t%llu tunneling packet%s that can't find gif\n");
p(IP6_STAT_NOIPSEC,
"\t%llu tunneling packet%s that can't find ipsecif\n");
p(IP6_STAT_TOOMANYHDR,
"\t%llu packet%s discarded due to too many headers\n");
/* for debugging source address selection */
#define PRINT_SCOPESTAT(s, i) do { \
switch (i) { /* XXX hardcoding in each case */ \
case 1: \
p(s, "\t\t%llu node-local%s\n"); \
break; \
case 2: \
p(s, "\t\t%llu link-local%s\n"); \
break; \
case 5: \
p(s, "\t\t%llu site-local%s\n"); \
break; \
case 14: \
p(s, "\t\t%llu global%s\n"); \
break; \
default: \
printf("\t\t%llu addresses scope=%x\n", \
(unsigned long long)ip6stat[s], i); \
} \
} while(0);
p(IP6_STAT_SOURCES_NONE,
"\t%llu failure%s of source address selection\n");
for (first = 1, i = 0; i < 16; i++) {
if (ip6stat[IP6_STAT_SOURCES_SAMEIF + i]) {
if (first) {
printf("\tsource addresses on an outgoing I/F\n");
first = 0;
}
PRINT_SCOPESTAT(IP6_STAT_SOURCES_SAMEIF + i, i);
}
}
for (first = 1, i = 0; i < 16; i++) {
if (ip6stat[IP6_STAT_SOURCES_OTHERIF + i]) {
if (first) {
printf("\tsource addresses on a non-outgoing I/F\n");
first = 0;
}
PRINT_SCOPESTAT(IP6_STAT_SOURCES_OTHERIF + i, i);
}
}
for (first = 1, i = 0; i < 16; i++) {
if (ip6stat[IP6_STAT_SOURCES_SAMESCOPE + i]) {
if (first) {
printf("\tsource addresses of same scope\n");
first = 0;
}
PRINT_SCOPESTAT(IP6_STAT_SOURCES_SAMESCOPE + i, i);
}
}
for (first = 1, i = 0; i < 16; i++) {
if (ip6stat[IP6_STAT_SOURCES_OTHERSCOPE + i]) {
if (first) {
printf("\tsource addresses of a different scope\n");
first = 0;
}
PRINT_SCOPESTAT(IP6_STAT_SOURCES_OTHERSCOPE + i, i);
}
}
for (first = 1, i = 0; i < 16; i++) {
if (ip6stat[IP6_STAT_SOURCES_DEPRECATED + i]) {
if (first) {
printf("\tdeprecated source addresses\n");
first = 0;
}
PRINT_SCOPESTAT(IP6_STAT_SOURCES_DEPRECATED + i, i);
}
}
p1(IP6_STAT_FORWARD_CACHEHIT, "\t%llu forward cache hit\n");
p1(IP6_STAT_FORWARD_CACHEMISS, "\t%llu forward cache miss\n");
p(IP6_STAT_PFILDROP_IN, "\t%llu input packet%s dropped by pfil\n");
p(IP6_STAT_PFILDROP_OUT, "\t%llu output packet%s dropped by pfil\n");
p(IP6_STAT_IPSECDROP_IN, "\t%llu input packet%s dropped by IPsec\n");
p(IP6_STAT_IPSECDROP_OUT, "\t%llu output packet%s dropped by IPsec\n");
p(IP6_STAT_IFDROP,
"\t%llu input packet%s dropped due to interface state\n");
p(IP6_STAT_IDROPPED,
"\t%llu input packet%s dropped due to no bufs, etc.\n");
p(IP6_STAT_TIMXCEED,
"\t%llu packet%s dropped due to hop limit exceeded\n");
p(IP6_STAT_TOOBIG, "\t%llu packet%s dropped (too big)\n");
p(IP6_STAT_RTREJECT,
"\t%llu output packet%s discarded due to reject route\n");
#undef p
#undef p1
}
/*
* Dump IPv6 per-interface statistics based on RFC 2465.
*/
void
ip6_ifstats(const char *ifname)
{
struct in6_ifreq ifr;
int s;
#define p(f, m) if (ifr.ifr_ifru.ifru_stat.f || sflag <= 1) \
printf(m, (unsigned long long)ifr.ifr_ifru.ifru_stat.f, \
plural(ifr.ifr_ifru.ifru_stat.f))
#define p_5(f, m) if (ifr.ifr_ifru.ifru_stat.f || sflag <= 1) \
printf(m, (unsigned long long)ip6stat.f)
/*
* Dump ICMPv6 statistics.
*/
void
icmp6_stats(u_long off, const char *name)
{
uint64_t icmp6stat[ICMP6_NSTATS];
int i, first;
if (use_sysctl) {
size_t size = sizeof(icmp6stat);
if (prog_sysctlbyname("net.inet6.icmp6.stats", icmp6stat,
&size, NULL, 0) == -1 && errno != ENOMEM)
return;
} else {
warnx("%s stats not available via KVM.", name);
return;
}
printf("%s:\n", name);
#define p(f, m) if (icmp6stat[f] || sflag <= 1) \
printf(m, (unsigned long long)icmp6stat[f], \
plural(icmp6stat[f]))
#define p_oerr(f, m) if (icmp6stat[ICMP6_STAT_OUTERRHIST + f] || sflag <= 1) \
printf(m, \
(unsigned long long)icmp6stat[ICMP6_STAT_OUTERRHIST + f])
p(ICMP6_STAT_ERROR, "\t%llu call%s to icmp6_error\n");
p(ICMP6_STAT_CANTERROR,
"\t%llu error%s not generated because old message was icmp6 or so\n");
p(ICMP6_STAT_TOOFREQ,
"\t%llu error%s not generated because of rate limitation\n");
for (first = 1, i = 0; i < 256; i++)
if (icmp6stat[ICMP6_STAT_OUTHIST + i] != 0) {
if (first) {
printf("\tOutput packet histogram:\n");
first = 0;
}
printf("\t\t%s: %llu\n", icmp6names[i],
(unsigned long long)icmp6stat[ICMP6_STAT_OUTHIST + i]);
}
p(ICMP6_STAT_BADCODE, "\t%llu message%s with bad code fields\n");
p(ICMP6_STAT_TOOSHORT, "\t%llu message%s < minimum length\n");
p(ICMP6_STAT_CHECKSUM, "\t%llu bad checksum%s\n");
p(ICMP6_STAT_BADLEN, "\t%llu message%s with bad length\n");
for (first = 1, i = 0; i < ICMP6_MAXTYPE; i++)
if (icmp6stat[ICMP6_STAT_INHIST + i] != 0) {
if (first) {
printf("\tInput packet histogram:\n");
first = 0;
}
printf("\t\t%s: %llu\n", icmp6names[i],
(unsigned long long)icmp6stat[ICMP6_STAT_INHIST + i]);
}
printf("\tHistogram of error messages to be generated:\n");
p_oerr(ICMP6_ERRSTAT_DST_UNREACH_NOROUTE, "\t\t%llu no route\n");
p_oerr(ICMP6_ERRSTAT_DST_UNREACH_ADMIN,
"\t\t%llu administratively prohibited\n");
p_oerr(ICMP6_ERRSTAT_DST_UNREACH_BEYONDSCOPE,
"\t\t%llu beyond scope\n");
p_oerr(ICMP6_ERRSTAT_DST_UNREACH_ADDR,
"\t\t%llu address unreachable\n");
p_oerr(ICMP6_ERRSTAT_DST_UNREACH_NOPORT,
"\t\t%llu port unreachable\n");
p_oerr(ICMP6_ERRSTAT_PACKET_TOO_BIG, "\t\t%llu packet too big\n");
p_oerr(ICMP6_ERRSTAT_TIME_EXCEED_TRANSIT,
"\t\t%llu time exceed transit\n");
p_oerr(ICMP6_ERRSTAT_TIME_EXCEED_REASSEMBLY,
"\t\t%llu time exceed reassembly\n");
p_oerr(ICMP6_ERRSTAT_PARAMPROB_HEADER,
"\t\t%llu erroneous header field\n");
p_oerr(ICMP6_ERRSTAT_PARAMPROB_NEXTHEADER,
"\t\t%llu unrecognized next header\n");
p_oerr(ICMP6_ERRSTAT_PARAMPROB_OPTION,
"\t\t%llu unrecognized option\n");
p_oerr(ICMP6_ERRSTAT_REDIRECT, "\t\t%llu redirect\n");
p_oerr(ICMP6_ERRSTAT_UNKNOWN, "\t\t%llu unknown\n");
p(ICMP6_STAT_REFLECT, "\t%llu message response%s generated\n");
p(ICMP6_STAT_ND_TOOMANYOPT,
"\t%llu message%s with too many ND options\n");
p(ICMP6_STAT_ND_BADOPT, "\t%llu message%s with bad ND options\n");
p(ICMP6_STAT_BADNS, "\t%llu bad neighbor solicitation message%s\n");
p(ICMP6_STAT_BADNA, "\t%llu bad neighbor advertisement message%s\n");
p(ICMP6_STAT_BADRS, "\t%llu bad router solicitation message%s\n");
p(ICMP6_STAT_BADRA, "\t%llu bad router advertisement message%s\n");
p(ICMP6_STAT_DROPPED_RAROUTE,
"\t%llu router advertisement route%s dropped\n");
p(ICMP6_STAT_BADREDIRECT, "\t%llu bad redirect message%s\n");
p(ICMP6_STAT_PMTUCHG, "\t%llu path MTU change%s\n");
#undef p
#undef p_oerr
}
/*
* Dump ICMPv6 per-interface statistics based on RFC 2466.
*/
void
icmp6_ifstats(const char *ifname)
{
struct in6_ifreq ifr;
int s;
#define p(f, m) if (ifr.ifr_ifru.ifru_icmp6stat.f || sflag <= 1) \
printf(m, (unsigned long long)ifr.ifr_ifru.ifru_icmp6stat.f, \
plural(ifr.ifr_ifru.ifru_icmp6stat.f))
if (prog_sysctlbyname("net.inet6.pim6.stats", pim6stat, &size,
NULL, 0) == -1 && errno != ENOMEM)
return;
} else {
warnx("%s stats not available via KVM.", name);
return;
}
printf("%s:\n", name);
#define p(f, m) if (pim6stat[f] || sflag <= 1) \
printf(m, (unsigned long long)pim6stat[f], plural(pim6stat[f]))
p(PIM6_STAT_RCV_TOTAL, "\t%llu message%s received\n");
p(PIM6_STAT_RCV_TOOSHORT,
"\t%llu message%s received with too few bytes\n");
p(PIM6_STAT_RCV_BADSUM,
"\t%llu message%s received with bad checksum\n");
p(PIM6_STAT_RCV_BADVERSION,
"\t%llu message%s received with bad version\n");
p(PIM6_STAT_RCV_REGISTERS, "\t%llu register%s received\n");
p(PIM6_STAT_RCV_BADREGISTERS, "\t%llu bad register%s received\n");
p(PIM6_STAT_SND_REGISTERS, "\t%llu register%s sent\n");
#undef p
}
if (prog_sysctlbyname("net.inet6.raw6.stats", rip6stat, &size,
NULL, 0) == -1 && errno != ENOMEM)
return;
} else {
warnx("%s stats not available via KVM.", name);
return;
}
printf("%s:\n", name);
#define p(f, m) if (rip6stat[f] || sflag <= 1) \
printf(m, (unsigned long long)rip6stat[f], plural(rip6stat[f]))
p(RIP6_STAT_IPACKETS, "\t%llu message%s received\n");
p(RIP6_STAT_ISUM, "\t%llu checksum calculation%s on inbound\n");
p(RIP6_STAT_BADSUM, "\t%llu message%s with bad checksum\n");
p(RIP6_STAT_NOSOCK, "\t%llu message%s dropped due to no socket\n");
p(RIP6_STAT_NOSOCKMCAST,
"\t%llu multicast message%s dropped due to no socket\n");
p(RIP6_STAT_FULLSOCK,
"\t%llu message%s dropped due to full socket buffers\n");
delivered = rip6stat[RIP6_STAT_IPACKETS] -
rip6stat[RIP6_STAT_BADSUM] -
rip6stat[RIP6_STAT_NOSOCK] -
rip6stat[RIP6_STAT_NOSOCKMCAST] -
rip6stat[RIP6_STAT_FULLSOCK];
if (delivered || sflag <= 1)
printf("\t%llu delivered\n", (unsigned long long)delivered);
p(RIP6_STAT_OPACKETS, "\t%llu datagram%s output\n");
#undef p
}
/*
* Pretty print an Internet address (net address + port).
* Take numeric_addr and numeric_port into consideration.
*/
void
inet6print(const struct in6_addr *in6, int port, const char *proto)
{
#define GETSERVBYPORT6(port, proto, ret) \
do { \
if (strcmp((proto), "tcp6") == 0) \
(ret) = getservbyport((int)(port), "tcp"); \
else if (strcmp((proto), "udp6") == 0) \
(ret) = getservbyport((int)(port), "udp"); \
else \
(ret) = getservbyport((int)(port), (proto)); \
} while (0)
struct servent *sp = 0;
char line[80], *cp;
int lwidth;