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