#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet6/ip6_mroute.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <util.h>
#include "netstat.h"
#define WID_ORG (lflag ? 39 : (nflag ? 29 : 18))
#define WID_GRP (lflag ? 18 : (nflag ? 16 : 18))
void
mroute6pr(void)
{
char *buf = NULL;
char fmtbuf[FMT_SCALED_STRSIZE];
struct mf6cinfo *mfc;
struct mif6info *mif;
size_t needed, mifi, nummifs, mfci, nummfcs;
int banner_printed, saved_nflag;
u_int mrtproto;
int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_MRTPROTO };
size_t len = sizeof(int);
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
&mrtproto, &len, NULL, 0) == -1) {
if (errno != EOPNOTSUPP)
warn("mroute");
printf("no IPv6 multicast routing compiled into this system\n");
return;
}
saved_nflag = nflag;
nflag = 1;
mib[3] = IPV6CTL_MRTMIF;
needed = get_sysctl(mib, sizeof(mib) / sizeof(mib[0]), &buf);
nummifs = needed / sizeof(*mif);
mif = (struct mif6info *)buf;
banner_printed = 0;
for (mifi = 0; mifi < nummifs; ++mifi, ++mif) {
char ifname[IFNAMSIZ];
if (mif->m6_ifindex == 0)
continue;
if (!banner_printed) {
printf("\nIPv6 Multicast Interface Table\n"
" Mif Rate PhyIF "
"Pkts-In Pkts-Out\n");
banner_printed = 1;
}
printf(" %2u %4d",
mif->m6_mifi, mif->m6_rate_limit);
printf(" %5s", (mif->m6_flags & MIFF_REGISTER) ?
"reg0" : if_indextoname(mif->m6_ifindex, ifname));
printf(" %9llu %9llu\n", mif->m6_pkt_in, mif->m6_pkt_out);
}
if (!banner_printed)
printf("IPv6 Multicast Interface Table is empty\n");
mib[3] = IPV6CTL_MRTMFC;
needed = get_sysctl(mib, sizeof(mib) / sizeof(mib[0]), &buf);
nummfcs = needed / sizeof(*mfc);
mfc = (struct mf6cinfo *)buf;
banner_printed = 0;
for (mfci = 0; mfci < nummfcs; ++mfci, ++mfc) {
if (!banner_printed) {
printf("\nIPv6 Multicast Forwarding Cache\n");
printf(" %-*.*s %-*.*s %s",
WID_ORG, WID_ORG, "Origin",
WID_GRP, WID_GRP, "Group",
" Packets Waits In-Mif Out-Mifs\n");
banner_printed = 1;
}
printf(" %-*.*s", WID_ORG, WID_ORG,
routename6(&mfc->mf6c_origin));
printf(" %-*.*s", WID_GRP, WID_GRP,
routename6(&mfc->mf6c_mcastgrp));
fmt_scaled(mfc->mf6c_pkt_cnt, fmtbuf);
printf(" %9s", fmtbuf);
printf(" %3llu", mfc->mf6c_stall_cnt);
if (mfc->mf6c_parent == MF6C_INCOMPLETE_PARENT)
printf(" --- ");
else
printf(" %3d ", mfc->mf6c_parent);
for (mifi = 0; mifi <= MAXMIFS; mifi++) {
if (IF_ISSET(mifi, &mfc->mf6c_ifset))
printf(" %zu", mifi);
}
printf("\n");
}
if (!banner_printed)
printf("IPv6 Multicast Routing Table is empty");
printf("\n");
nflag = saved_nflag;
free(buf);
}
void
mrt6_stats(void)
{
struct mrt6stat mrt6stat;
int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_MRTSTATS };
size_t len = sizeof(mrt6stat);
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
&mrt6stat, &len, NULL, 0) == -1) {
if (errno != EOPNOTSUPP)
warn("mroute6");
printf("no IPv6 multicast routing compiled into this system\n");
return;
}
printf("ip6 multicast forwarding:\n");
printf("\t%llu multicast forwarding cache lookup%s\n",
mrt6stat.mrt6s_mfc_lookups, plural(mrt6stat.mrt6s_mfc_lookups));
printf("\t%llu multicast forwarding cache miss%s\n",
mrt6stat.mrt6s_mfc_misses, plurales(mrt6stat.mrt6s_mfc_misses));
printf("\t%llu upcall%s to mrouted\n",
mrt6stat.mrt6s_upcalls, plural(mrt6stat.mrt6s_upcalls));
printf("\t%llu datagram%s with no route for origin\n",
mrt6stat.mrt6s_no_route, plural(mrt6stat.mrt6s_no_route));
printf("\t%llu datagram%s with malformed tunnel options\n",
mrt6stat.mrt6s_bad_tunnel, plural(mrt6stat.mrt6s_bad_tunnel));
printf("\t%llu datagram%s could not be tunneled\n",
mrt6stat.mrt6s_cant_tunnel, plural(mrt6stat.mrt6s_cant_tunnel));
printf("\t%llu datagram%s arrived on wrong interface\n",
mrt6stat.mrt6s_wrong_if, plural(mrt6stat.mrt6s_wrong_if));
printf("\t%llu datagram%s dropped due to upcall queue overflow\n",
mrt6stat.mrt6s_upq_ovflw, plural(mrt6stat.mrt6s_upq_ovflw));
printf("\t%llu datagram%s cleaned up by the cache\n",
mrt6stat.mrt6s_cache_cleanups, plural(mrt6stat.mrt6s_cache_cleanups));
printf("\t%llu datagram%s dropped selectively by ratelimiter\n",
mrt6stat.mrt6s_drop_sel, plural(mrt6stat.mrt6s_drop_sel));
printf("\t%llu datagram%s dropped due to queue overflow\n",
mrt6stat.mrt6s_q_overflow, plural(mrt6stat.mrt6s_q_overflow));
printf("\t%llu datagram%s dropped for being too large\n",
mrt6stat.mrt6s_pkt2large, plural(mrt6stat.mrt6s_pkt2large));
printf("\t%llu datagram%s dropped due to full socket buffer\n",
mrt6stat.mrt6s_upq_sockfull, plural(mrt6stat.mrt6s_upq_sockfull));
}