DragonFlyBSD Kernel Audit
DF-2610 / ic6stat.c
← back to finding ↓ download raw
/* ic6stat.c -- decode selected fields of net.inet6.icmp6.stats (u_quad_t
 * array) to observe ICMPv6 type-139 (ND_REDIRECT) input/output counters.
 * struct icmp6stat (sys/netinet/icmp6.h:590-617):
 *   [0] error, [1] canterror, [2] toofreq, [3..258] outhist[256],
 *   [259] badcode, [260] tooshort, [261] checksum, [262] badlen,
 *   [263] reflect, [264..519] inhist[256], [520] nd_toomanyopt,
 *   ... icp6s_badredirect is a later member; print by struct order. */
#include <sys/types.h>
#include <sys/sysctl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define ND_REDIRECT_T 137

int
main(void)
{
	uint64_t buf[4096];
	size_t len = sizeof(buf);
	unsigned long i;
	uint64_t *v = buf;

	if (sysctlbyname("net.inet6.icmp6.stats", buf, &len, NULL, 0) < 0) {
		perror("sysctl");
		return 2;
	}
	printf("stats len=%zu quadwords=%zu\n", len, len / 8);
	printf("icp6s_error=%llu canterror=%llu toofreq=%llu\n",
	    (unsigned long long)v[0], (unsigned long long)v[1],
	    (unsigned long long)v[2]);
	printf("outhist[139]=%llu outhist[140]=%llu\n",
	    (unsigned long long)v[3 + ND_REDIRECT_T],
	    (unsigned long long)v[3 + 140]);
	printf("badcode=%llu tooshort=%llu checksum=%llu badlen=%llu "
	    "reflect=%llu\n",
	    (unsigned long long)v[259], (unsigned long long)v[260],
	    (unsigned long long)v[261], (unsigned long long)v[262],
	    (unsigned long long)v[263]);
	printf("inhist[139]=%llu inhist[140]=%llu inhist[128]=%llu "
	    "inhist[133]=%llu inhist[135]=%llu inhist[136]=%llu "
	    "inhist[137]=%llu\n",
	    (unsigned long long)v[264 + ND_REDIRECT_T],
	    (unsigned long long)v[264 + 140],
	    (unsigned long long)v[264 + 128],
	    (unsigned long long)v[264 + 133],
	    (unsigned long long)v[264 + 135],
	    (unsigned long long)v[264 + 136],
	    (unsigned long long)v[264 + 137]);
	/* find icp6s_badredirect: after nd_toomanyopt[520] + outerrhist
	 * (15 members 521..535) + pmtuchg[536] + nd_badopt[537] + badns[538]
	 * + badna[539] + badrs[540] + badra[541] -> badredirect[542] */
	i = 542;
	if (len / 8 > i)
		printf("badredirect(guessed idx %lu)=%llu\n", i,
		    (unsigned long long)v[i]);
	return 0;
}