ifmcstat.c revision 1.20 1 1.20 ozaki /* $NetBSD: ifmcstat.c,v 1.20 2017/01/10 05:43:27 ozaki-r 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.20 ozaki __RCSID("$NetBSD: ifmcstat.c,v 1.20 2017/01/10 05:43:27 ozaki-r 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.20 ozaki static bool
83 1.20 ozaki check_inet6_availability(void)
84 1.20 ozaki {
85 1.20 ozaki size_t dummy;
86 1.20 ozaki
87 1.20 ozaki return sysctlnametomib("net.inet6.multicast", NULL, &dummy) == 0;
88 1.20 ozaki }
89 1.20 ozaki
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.20 ozaki 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.20 ozaki
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.20 ozaki if (inet6_available)
104 1.20 ozaki 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.17 joerg static int mcast_oids[4], kludge_oids[4];
183 1.17 joerg const char *addr;
184 1.17 joerg uint8_t *mcast_addrs, *kludge_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 if (kludge_oids[0] == 0) {
198 1.17 joerg size_t oidlen = __arraycount(kludge_oids);
199 1.17 joerg if (sysctlnametomib("net.inet6.multicast_kludge", kludge_oids,
200 1.17 joerg &oidlen) == -1)
201 1.17 joerg errx(1, "net.inet6.multicast_kludge not found");
202 1.17 joerg if (oidlen != 3)
203 1.17 joerg errx(1, "Wrong OID path for net.inet6.multicast_kludge");
204 1.17 joerg }
205 1.17 joerg
206 1.17 joerg mcast_oids[3] = ifindex;
207 1.17 joerg kludge_oids[3] = ifindex;
208 1.17 joerg
209 1.19 joerg mcast_addrs = asysctl(mcast_oids, 4, &len);
210 1.19 joerg if (mcast_addrs == NULL && len != 0) {
211 1.19 joerg warn("failed to read net.inet6.multicast");
212 1.19 joerg return;
213 1.19 joerg }
214 1.17 joerg if (len) {
215 1.17 joerg p = mcast_addrs;
216 1.17 joerg last_p = NULL;
217 1.17 joerg while (len >= 2 * sizeof(struct in6_addr) + sizeof(uint32_t)) {
218 1.17 joerg if (last_p == NULL ||
219 1.17 joerg memcmp(p, last_p, sizeof(struct in6_addr)))
220 1.17 joerg printf("\tinet6 %s\n", inet6_n2a(p));
221 1.17 joerg last_p = p;
222 1.17 joerg p += sizeof(struct in6_addr);
223 1.17 joerg addr = inet6_n2a(p);
224 1.17 joerg p += sizeof(struct in6_addr);
225 1.17 joerg memcpy(&refcnt, p, sizeof(refcnt));
226 1.17 joerg p += sizeof(refcnt);
227 1.17 joerg printf("\t\tgroup %s refcount %" PRIu32 "\n", addr,
228 1.17 joerg refcnt);
229 1.17 joerg len -= 2 * sizeof(struct in6_addr) + sizeof(uint32_t);
230 1.6 itojun }
231 1.6 itojun }
232 1.17 joerg free(mcast_addrs);
233 1.1 itojun
234 1.19 joerg kludge_addrs = asysctl(kludge_oids, 4, &len);
235 1.19 joerg if (kludge_addrs == NULL && len != 0) {
236 1.19 joerg warn("failed to read net.inet6.multicast_kludge");
237 1.19 joerg return;
238 1.19 joerg }
239 1.17 joerg if (len) {
240 1.17 joerg printf("\t(on kludge entry for %s)\n", ifname);
241 1.17 joerg p = kludge_addrs;
242 1.17 joerg while (len >= sizeof(struct in6_addr) + sizeof(uint32_t)) {
243 1.17 joerg addr = inet6_n2a(p);
244 1.17 joerg p += sizeof(struct in6_addr);
245 1.17 joerg memcpy(&refcnt, p, sizeof(refcnt));
246 1.17 joerg p += sizeof(refcnt);
247 1.17 joerg printf("\t\tgroup %s refcount %" PRIu32 "\n", addr,
248 1.17 joerg refcnt);
249 1.17 joerg len -= sizeof(struct in6_addr) + sizeof(uint32_t);
250 1.17 joerg }
251 1.16 joerg }
252 1.17 joerg free(kludge_addrs);
253 1.1 itojun }
254