ifmcstat.c revision 1.17 1 1.17 joerg /* $NetBSD: ifmcstat.c,v 1.17 2014/06/10 09:38:30 joerg 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.17 joerg __RCSID("$NetBSD: ifmcstat.c,v 1.17 2014/06/10 09:38:30 joerg 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.15 joerg int
83 1.15 joerg main(void)
84 1.1 itojun {
85 1.17 joerg struct if_nameindex *ifps;
86 1.17 joerg size_t i;
87 1.17 joerg
88 1.1 itojun
89 1.17 joerg ifps = if_nameindex();
90 1.17 joerg if (ifps == NULL)
91 1.17 joerg errx(1, "failed to obtain list of interfaces");
92 1.17 joerg for (i = 0; ifps[i].if_name != NULL; ++i) {
93 1.17 joerg printf("%s:\n", ifps[i].if_name);
94 1.17 joerg print_inet6_mcast(ifps[i].if_index, ifps[i].if_name);
95 1.17 joerg print_ether_mcast(ifps[i].if_index);
96 1.1 itojun }
97 1.17 joerg 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.17 joerg static void *
104 1.17 joerg fetch_sysctl(size_t *len, int oids[], size_t oidlen, const char *msg)
105 1.1 itojun {
106 1.17 joerg void *data;
107 1.17 joerg
108 1.17 joerg *len = 0;
109 1.17 joerg data = NULL;
110 1.1 itojun
111 1.17 joerg for (;;) {
112 1.17 joerg if (sysctl(oids, oidlen, data, len, NULL, 0) == 0) {
113 1.17 joerg if (data != NULL || len == 0)
114 1.17 joerg return data;
115 1.17 joerg errno = ENOMEM;
116 1.17 joerg }
117 1.17 joerg free(data);
118 1.17 joerg if (errno == ENOMEM) {
119 1.17 joerg data = emalloc(*len);
120 1.17 joerg continue;
121 1.17 joerg }
122 1.17 joerg if (errno != ENODEV)
123 1.17 joerg warn("%s", msg);
124 1.17 joerg return NULL;
125 1.17 joerg }
126 1.1 itojun }
127 1.1 itojun
128 1.15 joerg static void
129 1.17 joerg print_hwaddr(const uint8_t *hwaddr, size_t len)
130 1.1 itojun {
131 1.17 joerg while (len)
132 1.17 joerg printf("%02x%s", *hwaddr++, len-- == 0 ? "" : ":");
133 1.1 itojun }
134 1.1 itojun
135 1.15 joerg static void
136 1.17 joerg print_ether_mcast(u_short ifindex)
137 1.1 itojun {
138 1.17 joerg static int ems_oids[4], sdl_oids[3];
139 1.17 joerg size_t i, ems_len, sdl_len;
140 1.17 joerg void *hwaddr;
141 1.17 joerg struct ether_multi_sysctl *ems;
142 1.17 joerg
143 1.17 joerg if (ems_oids[0] == 0) {
144 1.17 joerg size_t oidlen = __arraycount(ems_oids);
145 1.17 joerg if (sysctlnametomib("net.ether.multicast", ems_oids, &oidlen) == -1)
146 1.17 joerg errx(1, "net.ether.multicast not found");
147 1.17 joerg if (oidlen != 3)
148 1.17 joerg errx(1, "Wrong OID path for net.ether.multicast");
149 1.17 joerg }
150 1.17 joerg
151 1.17 joerg if (sdl_oids[0] == 0) {
152 1.17 joerg size_t oidlen = __arraycount(sdl_oids);
153 1.17 joerg if (sysctlnametomib("net.sdl", sdl_oids, &oidlen) == -1)
154 1.17 joerg errx(1, "net.sdl not found");
155 1.17 joerg if (oidlen != 2)
156 1.17 joerg errx(1, "Wrong OID path for net.sdl");
157 1.17 joerg }
158 1.17 joerg
159 1.17 joerg sdl_oids[2] = ifindex;
160 1.17 joerg hwaddr = fetch_sysctl(&sdl_len, sdl_oids, 3, "failed to read net.sdl");
161 1.17 joerg
162 1.17 joerg if (sdl_len == 0) {
163 1.17 joerg free(hwaddr);
164 1.17 joerg return;
165 1.17 joerg }
166 1.17 joerg
167 1.17 joerg ems_oids[3] = ifindex;
168 1.17 joerg ems = fetch_sysctl(&ems_len, ems_oids, 4,
169 1.17 joerg "failed to read net.ether.multicast");
170 1.17 joerg ems_len /= sizeof(*ems);
171 1.17 joerg
172 1.17 joerg printf("\tenaddr ");
173 1.17 joerg print_hwaddr(hwaddr, sdl_len);
174 1.17 joerg printf(" multicnt %zu\n", ems_len);
175 1.17 joerg
176 1.17 joerg for (i = 0; i < ems_len; ++i) {
177 1.17 joerg printf("\t\t");
178 1.17 joerg print_hwaddr(ems[i].enm_addrlo, sizeof(ems[i].enm_addrlo));
179 1.17 joerg printf(" -- ");
180 1.17 joerg print_hwaddr(ems[i].enm_addrhi, sizeof(ems[i].enm_addrhi));
181 1.17 joerg printf(" %d\n", ems[i].enm_refcount);
182 1.1 itojun }
183 1.17 joerg free(ems);
184 1.17 joerg free(hwaddr);
185 1.1 itojun }
186 1.1 itojun
187 1.15 joerg static void
188 1.17 joerg print_inet6_mcast(u_short ifindex, const char *ifname)
189 1.1 itojun {
190 1.17 joerg static int mcast_oids[4], kludge_oids[4];
191 1.17 joerg const char *addr;
192 1.17 joerg uint8_t *mcast_addrs, *kludge_addrs, *p, *last_p;
193 1.17 joerg uint32_t refcnt;
194 1.17 joerg size_t len;
195 1.17 joerg
196 1.17 joerg if (mcast_oids[0] == 0) {
197 1.17 joerg size_t oidlen = __arraycount(mcast_oids);
198 1.17 joerg if (sysctlnametomib("net.inet6.multicast", mcast_oids,
199 1.17 joerg &oidlen) == -1)
200 1.17 joerg errx(1, "net.inet6.multicast not found");
201 1.17 joerg if (oidlen != 3)
202 1.17 joerg errx(1, "Wrong OID path for net.inet6.multicast");
203 1.17 joerg }
204 1.17 joerg
205 1.17 joerg if (kludge_oids[0] == 0) {
206 1.17 joerg size_t oidlen = __arraycount(kludge_oids);
207 1.17 joerg if (sysctlnametomib("net.inet6.multicast_kludge", kludge_oids,
208 1.17 joerg &oidlen) == -1)
209 1.17 joerg errx(1, "net.inet6.multicast_kludge not found");
210 1.17 joerg if (oidlen != 3)
211 1.17 joerg errx(1, "Wrong OID path for net.inet6.multicast_kludge");
212 1.17 joerg }
213 1.17 joerg
214 1.17 joerg mcast_oids[3] = ifindex;
215 1.17 joerg kludge_oids[3] = ifindex;
216 1.17 joerg
217 1.17 joerg mcast_addrs = fetch_sysctl(&len, mcast_oids, 4,
218 1.17 joerg "failed to read net.inet6.multicast");
219 1.17 joerg if (len) {
220 1.17 joerg p = mcast_addrs;
221 1.17 joerg last_p = NULL;
222 1.17 joerg while (len >= 2 * sizeof(struct in6_addr) + sizeof(uint32_t)) {
223 1.17 joerg if (last_p == NULL ||
224 1.17 joerg memcmp(p, last_p, sizeof(struct in6_addr)))
225 1.17 joerg printf("\tinet6 %s\n", inet6_n2a(p));
226 1.17 joerg last_p = p;
227 1.17 joerg p += sizeof(struct in6_addr);
228 1.17 joerg addr = inet6_n2a(p);
229 1.17 joerg p += sizeof(struct in6_addr);
230 1.17 joerg memcpy(&refcnt, p, sizeof(refcnt));
231 1.17 joerg p += sizeof(refcnt);
232 1.17 joerg printf("\t\tgroup %s refcount %" PRIu32 "\n", addr,
233 1.17 joerg refcnt);
234 1.17 joerg len -= 2 * sizeof(struct in6_addr) + sizeof(uint32_t);
235 1.6 itojun }
236 1.6 itojun }
237 1.17 joerg free(mcast_addrs);
238 1.1 itojun
239 1.17 joerg kludge_addrs = fetch_sysctl(&len, kludge_oids, 4,
240 1.17 joerg "failed to read net.inet6.multicast_kludge");
241 1.17 joerg if (len) {
242 1.17 joerg printf("\t(on kludge entry for %s)\n", ifname);
243 1.17 joerg p = kludge_addrs;
244 1.17 joerg while (len >= sizeof(struct in6_addr) + sizeof(uint32_t)) {
245 1.17 joerg addr = inet6_n2a(p);
246 1.17 joerg p += sizeof(struct in6_addr);
247 1.17 joerg memcpy(&refcnt, p, sizeof(refcnt));
248 1.17 joerg p += sizeof(refcnt);
249 1.17 joerg printf("\t\tgroup %s refcount %" PRIu32 "\n", addr,
250 1.17 joerg refcnt);
251 1.17 joerg len -= sizeof(struct in6_addr) + sizeof(uint32_t);
252 1.17 joerg }
253 1.16 joerg }
254 1.17 joerg free(kludge_addrs);
255 1.1 itojun }
256