Home | History | Annotate | Line # | Download | only in ifmcstat
ifmcstat.c revision 1.17
      1 /*	$NetBSD: ifmcstat.c,v 1.17 2014/06/10 09:38:30 joerg Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 #include <sys/cdefs.h>
     32 __RCSID("$NetBSD: ifmcstat.c,v 1.17 2014/06/10 09:38:30 joerg Exp $");
     33 
     34 #include <err.h>
     35 #include <errno.h>
     36 #include <stdio.h>
     37 #include <stdlib.h>
     38 #include <fcntl.h>
     39 #include <kvm.h>
     40 #include <nlist.h>
     41 #include <string.h>
     42 #include <limits.h>
     43 #include <util.h>
     44 
     45 #include <sys/types.h>
     46 #include <sys/socket.h>
     47 #include <sys/sysctl.h>
     48 #include <net/if.h>
     49 #include <net/if_types.h>
     50 #include <net/if_dl.h>
     51 #include <netinet/in.h>
     52 #include <net/if_ether.h>
     53 #include <netinet/in_var.h>
     54 #include <arpa/inet.h>
     55 
     56 #include <netdb.h>
     57 
     58 static const char *inet6_n2a(void *);
     59 static void print_ether_mcast(u_short);
     60 static void print_inet6_mcast(u_short, const char *);
     61 
     62 static const char *
     63 inet6_n2a(void *p)
     64 {
     65 	static char buf[NI_MAXHOST];
     66 	struct sockaddr_in6 sin6;
     67 	const int niflags = NI_NUMERICHOST;
     68 
     69 	memset(&sin6, 0, sizeof(sin6));
     70 	sin6.sin6_family = AF_INET6;
     71 	sin6.sin6_len = sizeof(struct sockaddr_in6);
     72 	memcpy(&sin6.sin6_addr, p, sizeof(sin6.sin6_addr));
     73 	inet6_getscopeid(&sin6, INET6_IS_ADDR_LINKLOCAL|
     74 	    INET6_IS_ADDR_MC_LINKLOCAL);
     75 	if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
     76 			buf, sizeof(buf), NULL, 0, niflags) == 0)
     77 		return buf;
     78 	else
     79 		return "(invalid)";
     80 }
     81 
     82 int
     83 main(void)
     84 {
     85 	struct if_nameindex  *ifps;
     86 	size_t i;
     87 
     88 
     89 	ifps = if_nameindex();
     90 	if (ifps == NULL)
     91 		errx(1, "failed to obtain list of interfaces");
     92 	for (i = 0; ifps[i].if_name != NULL; ++i) {
     93 		printf("%s:\n", ifps[i].if_name);
     94 		print_inet6_mcast(ifps[i].if_index, ifps[i].if_name);
     95 		print_ether_mcast(ifps[i].if_index);
     96 	}
     97 	if_freenameindex(ifps);
     98 
     99 	exit(0);
    100 	/*NOTREACHED*/
    101 }
    102 
    103 static void *
    104 fetch_sysctl(size_t *len, int oids[], size_t oidlen, const char *msg)
    105 {
    106 	void *data;
    107 
    108 	*len = 0;
    109 	data = NULL;
    110 
    111 	for (;;) {
    112 		if (sysctl(oids, oidlen, data, len, NULL, 0) == 0) {
    113 			if (data != NULL || len == 0)
    114 				return data;
    115 			errno = ENOMEM;
    116 		}
    117 		free(data);
    118 		if (errno == ENOMEM) {
    119 			data = emalloc(*len);
    120 			continue;
    121 		}
    122 		if (errno != ENODEV)
    123 			warn("%s", msg);
    124 		return NULL;
    125 	}
    126 }
    127 
    128 static void
    129 print_hwaddr(const uint8_t *hwaddr, size_t len)
    130 {
    131 	while (len)
    132 		printf("%02x%s", *hwaddr++, len-- == 0 ? "" : ":");
    133 }
    134 
    135 static void
    136 print_ether_mcast(u_short ifindex)
    137 {
    138 	static int ems_oids[4], sdl_oids[3];
    139 	size_t i, ems_len, sdl_len;
    140 	void *hwaddr;
    141 	struct ether_multi_sysctl *ems;
    142 
    143 	if (ems_oids[0] == 0) {
    144 		size_t oidlen = __arraycount(ems_oids);
    145 		if (sysctlnametomib("net.ether.multicast", ems_oids, &oidlen) == -1)
    146 			errx(1, "net.ether.multicast not found");
    147 		if (oidlen != 3)
    148 			errx(1, "Wrong OID path for net.ether.multicast");
    149 	}
    150 
    151 	if (sdl_oids[0] == 0) {
    152 		size_t oidlen = __arraycount(sdl_oids);
    153 		if (sysctlnametomib("net.sdl", sdl_oids, &oidlen) == -1)
    154 			errx(1, "net.sdl not found");
    155 		if (oidlen != 2)
    156 			errx(1, "Wrong OID path for net.sdl");
    157 	}
    158 
    159 	sdl_oids[2] = ifindex;
    160 	hwaddr = fetch_sysctl(&sdl_len, sdl_oids, 3, "failed to read net.sdl");
    161 
    162 	if (sdl_len == 0) {
    163 		free(hwaddr);
    164 		return;
    165 	}
    166 
    167 	ems_oids[3] = ifindex;
    168 	ems = fetch_sysctl(&ems_len, ems_oids, 4,
    169 	    "failed to read net.ether.multicast");
    170 	ems_len /= sizeof(*ems);
    171 
    172 	printf("\tenaddr ");
    173 	print_hwaddr(hwaddr, sdl_len);
    174 	printf(" multicnt %zu\n", ems_len);
    175 
    176 	for (i = 0; i < ems_len; ++i) {
    177 		printf("\t\t");
    178 		print_hwaddr(ems[i].enm_addrlo, sizeof(ems[i].enm_addrlo));
    179 		printf(" -- ");
    180 		print_hwaddr(ems[i].enm_addrhi, sizeof(ems[i].enm_addrhi));
    181 		printf(" %d\n", ems[i].enm_refcount);
    182 	}
    183 	free(ems);
    184 	free(hwaddr);
    185 }
    186 
    187 static void
    188 print_inet6_mcast(u_short ifindex, const char *ifname)
    189 {
    190 	static int mcast_oids[4], kludge_oids[4];
    191 	const char *addr;
    192 	uint8_t *mcast_addrs, *kludge_addrs, *p, *last_p;
    193 	uint32_t refcnt;
    194 	size_t len;
    195 
    196 	if (mcast_oids[0] == 0) {
    197 		size_t oidlen = __arraycount(mcast_oids);
    198 		if (sysctlnametomib("net.inet6.multicast", mcast_oids,
    199 		    &oidlen) == -1)
    200 			errx(1, "net.inet6.multicast not found");
    201 		if (oidlen != 3)
    202 			errx(1, "Wrong OID path for net.inet6.multicast");
    203 	}
    204 
    205 	if (kludge_oids[0] == 0) {
    206 		size_t oidlen = __arraycount(kludge_oids);
    207 		if (sysctlnametomib("net.inet6.multicast_kludge", kludge_oids,
    208 		    &oidlen) == -1)
    209 			errx(1, "net.inet6.multicast_kludge not found");
    210 		if (oidlen != 3)
    211 			errx(1, "Wrong OID path for net.inet6.multicast_kludge");
    212 	}
    213 
    214 	mcast_oids[3] = ifindex;
    215 	kludge_oids[3] = ifindex;
    216 
    217 	mcast_addrs = fetch_sysctl(&len, mcast_oids, 4,
    218 	    "failed to read net.inet6.multicast");
    219 	if (len) {
    220 		p = mcast_addrs;
    221 		last_p = NULL;
    222 		while (len >= 2 * sizeof(struct in6_addr) + sizeof(uint32_t)) {
    223 			if (last_p == NULL ||
    224 			    memcmp(p, last_p, sizeof(struct in6_addr)))
    225 				printf("\tinet6 %s\n", inet6_n2a(p));
    226 			last_p = p;
    227 			p += sizeof(struct in6_addr);
    228 			addr = inet6_n2a(p);
    229 			p += sizeof(struct in6_addr);
    230 			memcpy(&refcnt, p, sizeof(refcnt));
    231 			p += sizeof(refcnt);
    232 			printf("\t\tgroup %s refcount %" PRIu32 "\n", addr,
    233 			    refcnt);
    234 			len -= 2 * sizeof(struct in6_addr) + sizeof(uint32_t);
    235 		}
    236 	}
    237 	free(mcast_addrs);
    238 
    239 	kludge_addrs = fetch_sysctl(&len, kludge_oids, 4,
    240 	    "failed to read net.inet6.multicast_kludge");
    241 	if (len) {
    242 		printf("\t(on kludge entry for %s)\n", ifname);
    243 		p = kludge_addrs;
    244 		while (len >= sizeof(struct in6_addr) + sizeof(uint32_t)) {
    245 			addr = inet6_n2a(p);
    246 			p += sizeof(struct in6_addr);
    247 			memcpy(&refcnt, p, sizeof(refcnt));
    248 			p += sizeof(refcnt);
    249 			printf("\t\tgroup %s refcount %" PRIu32 "\n", addr,
    250 			    refcnt);
    251 			len -= sizeof(struct in6_addr) + sizeof(uint32_t);
    252 		}
    253 	}
    254 	free(kludge_addrs);
    255 }
    256