xref: /netbsd-src/usr.sbin/ifmcstat/ifmcstat.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: ifmcstat.c,v 1.19 2014/06/13 16:04:41 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.19 2014/06/13 16:04:41 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 print_hwaddr(const uint8_t *hwaddr, size_t len)
105 {
106 	while (len)
107 		printf("%02x%s", *hwaddr++, len-- == 0 ? "" : ":");
108 }
109 
110 static void
111 print_ether_mcast(u_short ifindex)
112 {
113 	static int ems_oids[4], sdl_oids[3];
114 	size_t i, ems_len, sdl_len;
115 	void *hwaddr;
116 	struct ether_multi_sysctl *ems;
117 
118 	if (ems_oids[0] == 0) {
119 		size_t oidlen = __arraycount(ems_oids);
120 		if (sysctlnametomib("net.ether.multicast", ems_oids, &oidlen) == -1)
121 			errx(1, "net.ether.multicast not found");
122 		if (oidlen != 3)
123 			errx(1, "Wrong OID path for net.ether.multicast");
124 	}
125 
126 	if (sdl_oids[0] == 0) {
127 		size_t oidlen = __arraycount(sdl_oids);
128 		if (sysctlnametomib("net.sdl", sdl_oids, &oidlen) == -1)
129 			errx(1, "net.sdl not found");
130 		if (oidlen != 2)
131 			errx(1, "Wrong OID path for net.sdl");
132 	}
133 
134 	sdl_oids[2] = ifindex;
135 	hwaddr = asysctl(sdl_oids, 3, &sdl_len);
136 
137 	if (sdl_len == 0) {
138 		free(hwaddr);
139 		return;
140 	}
141 	if (hwaddr == NULL) {
142 		warn("failed to read net.sdl");
143 	}
144 
145 	ems_oids[3] = ifindex;
146 	ems = asysctl(ems_oids, 4, &ems_len);
147 	ems_len /= sizeof(*ems);
148 
149 	if (ems == NULL && ems_len != 0) {
150 		warn("failed to read net.ether.multicast");
151 		return;
152 	}
153 
154 	printf("\tenaddr ");
155 	print_hwaddr(hwaddr, sdl_len);
156 	printf(" multicnt %zu\n", ems_len);
157 
158 	for (i = 0; i < ems_len; ++i) {
159 		printf("\t\t");
160 		print_hwaddr(ems[i].enm_addrlo, sizeof(ems[i].enm_addrlo));
161 		printf(" -- ");
162 		print_hwaddr(ems[i].enm_addrhi, sizeof(ems[i].enm_addrhi));
163 		printf(" %d\n", ems[i].enm_refcount);
164 	}
165 	free(ems);
166 	free(hwaddr);
167 }
168 
169 static void
170 print_inet6_mcast(u_short ifindex, const char *ifname)
171 {
172 	static int mcast_oids[4], kludge_oids[4];
173 	const char *addr;
174 	uint8_t *mcast_addrs, *kludge_addrs, *p, *last_p;
175 	uint32_t refcnt;
176 	size_t len;
177 
178 	if (mcast_oids[0] == 0) {
179 		size_t oidlen = __arraycount(mcast_oids);
180 		if (sysctlnametomib("net.inet6.multicast", mcast_oids,
181 		    &oidlen) == -1)
182 			errx(1, "net.inet6.multicast not found");
183 		if (oidlen != 3)
184 			errx(1, "Wrong OID path for net.inet6.multicast");
185 	}
186 
187 	if (kludge_oids[0] == 0) {
188 		size_t oidlen = __arraycount(kludge_oids);
189 		if (sysctlnametomib("net.inet6.multicast_kludge", kludge_oids,
190 		    &oidlen) == -1)
191 			errx(1, "net.inet6.multicast_kludge not found");
192 		if (oidlen != 3)
193 			errx(1, "Wrong OID path for net.inet6.multicast_kludge");
194 	}
195 
196 	mcast_oids[3] = ifindex;
197 	kludge_oids[3] = ifindex;
198 
199 	mcast_addrs = asysctl(mcast_oids, 4, &len);
200 	if (mcast_addrs == NULL && len != 0) {
201 		warn("failed to read net.inet6.multicast");
202 		return;
203 	}
204 	if (len) {
205 		p = mcast_addrs;
206 		last_p = NULL;
207 		while (len >= 2 * sizeof(struct in6_addr) + sizeof(uint32_t)) {
208 			if (last_p == NULL ||
209 			    memcmp(p, last_p, sizeof(struct in6_addr)))
210 				printf("\tinet6 %s\n", inet6_n2a(p));
211 			last_p = p;
212 			p += sizeof(struct in6_addr);
213 			addr = inet6_n2a(p);
214 			p += sizeof(struct in6_addr);
215 			memcpy(&refcnt, p, sizeof(refcnt));
216 			p += sizeof(refcnt);
217 			printf("\t\tgroup %s refcount %" PRIu32 "\n", addr,
218 			    refcnt);
219 			len -= 2 * sizeof(struct in6_addr) + sizeof(uint32_t);
220 		}
221 	}
222 	free(mcast_addrs);
223 
224 	kludge_addrs = asysctl(kludge_oids, 4, &len);
225 	if (kludge_addrs == NULL && len != 0) {
226 		warn("failed to read net.inet6.multicast_kludge");
227 		return;
228 	}
229 	if (len) {
230 		printf("\t(on kludge entry for %s)\n", ifname);
231 		p = kludge_addrs;
232 		while (len >= sizeof(struct in6_addr) + sizeof(uint32_t)) {
233 			addr = inet6_n2a(p);
234 			p += sizeof(struct in6_addr);
235 			memcpy(&refcnt, p, sizeof(refcnt));
236 			p += sizeof(refcnt);
237 			printf("\t\tgroup %s refcount %" PRIu32 "\n", addr,
238 			    refcnt);
239 			len -= sizeof(struct in6_addr) + sizeof(uint32_t);
240 		}
241 	}
242 	free(kludge_addrs);
243 }
244