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