Home | History | Annotate | Line # | Download | only in ifmcstat
ifmcstat.c revision 1.19.6.1
      1  1.19.6.1  pgoyette /*	$NetBSD: ifmcstat.c,v 1.19.6.1 2017/03/20 06:58:07 pgoyette 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.17     joerg #include <sys/cdefs.h>
     32  1.19.6.1  pgoyette __RCSID("$NetBSD: ifmcstat.c,v 1.19.6.1 2017/03/20 06:58:07 pgoyette Exp $");
     33       1.1    itojun 
     34      1.17     joerg #include <err.h>
     35      1.17     joerg #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.17     joerg #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.17     joerg #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.14     joerg #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.17     joerg static const char *inet6_n2a(void *);
     59      1.17     joerg static void print_ether_mcast(u_short);
     60      1.17     joerg static void print_inet6_mcast(u_short, const char *);
     61       1.6    itojun 
     62      1.15     joerg static const char *
     63      1.17     joerg 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.17     joerg 	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.19.6.1  pgoyette static bool
     83  1.19.6.1  pgoyette check_inet6_availability(void)
     84  1.19.6.1  pgoyette {
     85  1.19.6.1  pgoyette 	size_t dummy;
     86  1.19.6.1  pgoyette 
     87  1.19.6.1  pgoyette 	return sysctlnametomib("net.inet6.multicast", NULL, &dummy) == 0;
     88  1.19.6.1  pgoyette }
     89  1.19.6.1  pgoyette 
     90      1.15     joerg int
     91      1.15     joerg main(void)
     92       1.1    itojun {
     93      1.17     joerg 	struct if_nameindex  *ifps;
     94      1.17     joerg 	size_t i;
     95  1.19.6.1  pgoyette 	bool inet6_available = check_inet6_availability();
     96       1.1    itojun 
     97      1.17     joerg 	ifps = if_nameindex();
     98      1.17     joerg 	if (ifps == NULL)
     99      1.17     joerg 		errx(1, "failed to obtain list of interfaces");
    100  1.19.6.1  pgoyette 
    101      1.17     joerg 	for (i = 0; ifps[i].if_name != NULL; ++i) {
    102      1.17     joerg 		printf("%s:\n", ifps[i].if_name);
    103  1.19.6.1  pgoyette 		if (inet6_available)
    104  1.19.6.1  pgoyette 			print_inet6_mcast(ifps[i].if_index, ifps[i].if_name);
    105      1.17     joerg 		print_ether_mcast(ifps[i].if_index);
    106       1.1    itojun 	}
    107      1.17     joerg 	if_freenameindex(ifps);
    108       1.1    itojun 
    109       1.1    itojun 	exit(0);
    110       1.1    itojun 	/*NOTREACHED*/
    111       1.1    itojun }
    112       1.1    itojun 
    113      1.15     joerg static void
    114      1.17     joerg print_hwaddr(const uint8_t *hwaddr, size_t len)
    115       1.1    itojun {
    116      1.17     joerg 	while (len)
    117      1.17     joerg 		printf("%02x%s", *hwaddr++, len-- == 0 ? "" : ":");
    118       1.1    itojun }
    119       1.1    itojun 
    120      1.15     joerg static void
    121      1.17     joerg print_ether_mcast(u_short ifindex)
    122       1.1    itojun {
    123      1.17     joerg 	static int ems_oids[4], sdl_oids[3];
    124      1.17     joerg 	size_t i, ems_len, sdl_len;
    125      1.17     joerg 	void *hwaddr;
    126      1.17     joerg 	struct ether_multi_sysctl *ems;
    127      1.17     joerg 
    128      1.17     joerg 	if (ems_oids[0] == 0) {
    129      1.17     joerg 		size_t oidlen = __arraycount(ems_oids);
    130      1.17     joerg 		if (sysctlnametomib("net.ether.multicast", ems_oids, &oidlen) == -1)
    131      1.17     joerg 			errx(1, "net.ether.multicast not found");
    132      1.17     joerg 		if (oidlen != 3)
    133      1.17     joerg 			errx(1, "Wrong OID path for net.ether.multicast");
    134      1.17     joerg 	}
    135      1.17     joerg 
    136      1.17     joerg 	if (sdl_oids[0] == 0) {
    137      1.17     joerg 		size_t oidlen = __arraycount(sdl_oids);
    138      1.17     joerg 		if (sysctlnametomib("net.sdl", sdl_oids, &oidlen) == -1)
    139      1.17     joerg 			errx(1, "net.sdl not found");
    140      1.17     joerg 		if (oidlen != 2)
    141      1.17     joerg 			errx(1, "Wrong OID path for net.sdl");
    142      1.17     joerg 	}
    143      1.17     joerg 
    144      1.17     joerg 	sdl_oids[2] = ifindex;
    145      1.19     joerg 	hwaddr = asysctl(sdl_oids, 3, &sdl_len);
    146      1.17     joerg 
    147      1.17     joerg 	if (sdl_len == 0) {
    148      1.17     joerg 		free(hwaddr);
    149      1.17     joerg 		return;
    150      1.17     joerg 	}
    151      1.19     joerg 	if (hwaddr == NULL) {
    152      1.19     joerg 		warn("failed to read net.sdl");
    153      1.19     joerg 	}
    154      1.17     joerg 
    155      1.17     joerg 	ems_oids[3] = ifindex;
    156      1.19     joerg 	ems = asysctl(ems_oids, 4, &ems_len);
    157      1.17     joerg 	ems_len /= sizeof(*ems);
    158      1.17     joerg 
    159      1.19     joerg 	if (ems == NULL && ems_len != 0) {
    160      1.19     joerg 		warn("failed to read net.ether.multicast");
    161      1.19     joerg 		return;
    162      1.19     joerg 	}
    163      1.19     joerg 
    164      1.17     joerg 	printf("\tenaddr ");
    165      1.17     joerg 	print_hwaddr(hwaddr, sdl_len);
    166      1.17     joerg 	printf(" multicnt %zu\n", ems_len);
    167      1.17     joerg 
    168      1.17     joerg 	for (i = 0; i < ems_len; ++i) {
    169      1.17     joerg 		printf("\t\t");
    170      1.17     joerg 		print_hwaddr(ems[i].enm_addrlo, sizeof(ems[i].enm_addrlo));
    171      1.17     joerg 		printf(" -- ");
    172      1.17     joerg 		print_hwaddr(ems[i].enm_addrhi, sizeof(ems[i].enm_addrhi));
    173      1.17     joerg 		printf(" %d\n", ems[i].enm_refcount);
    174       1.1    itojun 	}
    175      1.17     joerg 	free(ems);
    176      1.17     joerg 	free(hwaddr);
    177       1.1    itojun }
    178       1.1    itojun 
    179      1.15     joerg static void
    180      1.17     joerg print_inet6_mcast(u_short ifindex, const char *ifname)
    181       1.1    itojun {
    182  1.19.6.1  pgoyette 	static int mcast_oids[4];
    183      1.17     joerg 	const char *addr;
    184  1.19.6.1  pgoyette 	uint8_t *mcast_addrs, *p, *last_p;
    185      1.17     joerg 	uint32_t refcnt;
    186      1.17     joerg 	size_t len;
    187      1.17     joerg 
    188      1.17     joerg 	if (mcast_oids[0] == 0) {
    189      1.17     joerg 		size_t oidlen = __arraycount(mcast_oids);
    190      1.17     joerg 		if (sysctlnametomib("net.inet6.multicast", mcast_oids,
    191      1.17     joerg 		    &oidlen) == -1)
    192      1.17     joerg 			errx(1, "net.inet6.multicast not found");
    193      1.17     joerg 		if (oidlen != 3)
    194      1.17     joerg 			errx(1, "Wrong OID path for net.inet6.multicast");
    195      1.17     joerg 	}
    196      1.17     joerg 
    197      1.17     joerg 	mcast_oids[3] = ifindex;
    198      1.17     joerg 
    199      1.19     joerg 	mcast_addrs = asysctl(mcast_oids, 4, &len);
    200      1.19     joerg 	if (mcast_addrs == NULL && len != 0) {
    201      1.19     joerg 		warn("failed to read net.inet6.multicast");
    202      1.19     joerg 		return;
    203      1.19     joerg 	}
    204      1.17     joerg 	if (len) {
    205      1.17     joerg 		p = mcast_addrs;
    206      1.17     joerg 		last_p = NULL;
    207      1.17     joerg 		while (len >= 2 * sizeof(struct in6_addr) + sizeof(uint32_t)) {
    208      1.17     joerg 			if (last_p == NULL ||
    209      1.17     joerg 			    memcmp(p, last_p, sizeof(struct in6_addr)))
    210      1.17     joerg 				printf("\tinet6 %s\n", inet6_n2a(p));
    211      1.17     joerg 			last_p = p;
    212      1.17     joerg 			p += sizeof(struct in6_addr);
    213      1.17     joerg 			addr = inet6_n2a(p);
    214      1.17     joerg 			p += sizeof(struct in6_addr);
    215      1.17     joerg 			memcpy(&refcnt, p, sizeof(refcnt));
    216      1.17     joerg 			p += sizeof(refcnt);
    217      1.17     joerg 			printf("\t\tgroup %s refcount %" PRIu32 "\n", addr,
    218      1.17     joerg 			    refcnt);
    219      1.17     joerg 			len -= 2 * sizeof(struct in6_addr) + sizeof(uint32_t);
    220       1.6    itojun 		}
    221       1.6    itojun 	}
    222      1.17     joerg 	free(mcast_addrs);
    223       1.1    itojun }
    224