ifmcstat.c revision 1.11 1 1.11 seanb /* $NetBSD: ifmcstat.c,v 1.11 2012/10/26 16:52:52 seanb 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.1 itojun
32 1.1 itojun #include <stdio.h>
33 1.1 itojun #include <stdlib.h>
34 1.1 itojun #include <fcntl.h>
35 1.1 itojun #include <kvm.h>
36 1.1 itojun #include <nlist.h>
37 1.1 itojun #include <string.h>
38 1.3 itojun #include <limits.h>
39 1.1 itojun
40 1.1 itojun #include <sys/types.h>
41 1.1 itojun #include <sys/socket.h>
42 1.1 itojun #include <net/if.h>
43 1.1 itojun #if defined(__FreeBSD__) && __FreeBSD__ >= 3
44 1.5 itojun # include <net/if_var.h>
45 1.5 itojun #endif
46 1.5 itojun #include <net/if_types.h>
47 1.1 itojun #include <net/if_dl.h>
48 1.1 itojun #include <netinet/in.h>
49 1.1 itojun #ifndef __NetBSD__
50 1.5 itojun # ifdef __FreeBSD__
51 1.5 itojun # define KERNEL
52 1.5 itojun # endif
53 1.5 itojun # include <netinet/if_ether.h>
54 1.5 itojun # ifdef __FreeBSD__
55 1.5 itojun # undef KERNEL
56 1.5 itojun # endif
57 1.5 itojun #else
58 1.5 itojun # include <net/if_ether.h>
59 1.1 itojun #endif
60 1.1 itojun #include <netinet/in_var.h>
61 1.1 itojun #include <arpa/inet.h>
62 1.1 itojun
63 1.8 itojun #include <netdb.h>
64 1.8 itojun
65 1.1 itojun kvm_t *kvmd;
66 1.1 itojun
67 1.1 itojun struct nlist nl[] = {
68 1.1 itojun #define N_IFNET 0
69 1.10 lukem { "_ifnet", 0, 0, 0, 0 },
70 1.6 itojun #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
71 1.6 itojun #define N_IN6_MK 1
72 1.10 lukem { "_in6_mk", 0, 0, 0, 0 },
73 1.6 itojun #endif
74 1.10 lukem { "", 0, 0, 0, 0 },
75 1.1 itojun };
76 1.1 itojun
77 1.1 itojun const char *inet6_n2a __P((struct in6_addr *));
78 1.1 itojun int main __P((void));
79 1.1 itojun char *ifname __P((struct ifnet *));
80 1.1 itojun void kread __P((u_long, void *, int));
81 1.5 itojun #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
82 1.1 itojun void acmc __P((struct ether_multi *));
83 1.5 itojun #endif
84 1.1 itojun void if6_addrlist __P((struct ifaddr *));
85 1.1 itojun void in6_multilist __P((struct in6_multi *));
86 1.1 itojun struct in6_multi * in6_multientry __P((struct in6_multi *));
87 1.1 itojun
88 1.5 itojun #if !defined(__NetBSD__) && !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined(__OpenBSD__)
89 1.5 itojun #ifdef __bsdi__
90 1.5 itojun struct ether_addr {
91 1.5 itojun u_int8_t ether_addr_octet[6];
92 1.5 itojun };
93 1.5 itojun #endif
94 1.5 itojun static char *ether_ntoa __P((struct ether_addr *));
95 1.5 itojun #endif
96 1.5 itojun
97 1.1 itojun #define KREAD(addr, buf, type) \
98 1.1 itojun kread((u_long)addr, (void *)buf, sizeof(type))
99 1.1 itojun
100 1.6 itojun #ifdef N_IN6_MK
101 1.6 itojun struct multi6_kludge {
102 1.6 itojun LIST_ENTRY(multi6_kludge) mk_entry;
103 1.6 itojun struct ifnet *mk_ifp;
104 1.6 itojun struct in6_multihead mk_head;
105 1.6 itojun };
106 1.6 itojun #endif
107 1.6 itojun
108 1.1 itojun const char *inet6_n2a(p)
109 1.1 itojun struct in6_addr *p;
110 1.1 itojun {
111 1.8 itojun static char buf[NI_MAXHOST];
112 1.8 itojun struct sockaddr_in6 sin6;
113 1.8 itojun u_int32_t scopeid;
114 1.8 itojun const int niflags = NI_NUMERICHOST;
115 1.1 itojun
116 1.8 itojun memset(&sin6, 0, sizeof(sin6));
117 1.8 itojun sin6.sin6_family = AF_INET6;
118 1.8 itojun sin6.sin6_len = sizeof(struct sockaddr_in6);
119 1.8 itojun sin6.sin6_addr = *p;
120 1.8 itojun if (IN6_IS_ADDR_LINKLOCAL(p) || IN6_IS_ADDR_MC_LINKLOCAL(p)) {
121 1.8 itojun scopeid = ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
122 1.8 itojun if (scopeid) {
123 1.8 itojun sin6.sin6_scope_id = scopeid;
124 1.8 itojun sin6.sin6_addr.s6_addr[2] = 0;
125 1.8 itojun sin6.sin6_addr.s6_addr[3] = 0;
126 1.8 itojun }
127 1.8 itojun }
128 1.8 itojun if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
129 1.8 itojun buf, sizeof(buf), NULL, 0, niflags) == 0)
130 1.8 itojun return buf;
131 1.8 itojun else
132 1.8 itojun return "(invalid)";
133 1.1 itojun }
134 1.1 itojun
135 1.1 itojun int main()
136 1.1 itojun {
137 1.10 lukem char buf[_POSIX2_LINE_MAX], ifnam[IFNAMSIZ];
138 1.5 itojun struct ifnet *ifp, *nifp, ifnet;
139 1.1 itojun #ifndef __NetBSD__
140 1.1 itojun struct arpcom arpcom;
141 1.1 itojun #else
142 1.1 itojun struct ethercom ec;
143 1.11 seanb union {
144 1.11 seanb struct sockaddr_storage st;
145 1.11 seanb struct sockaddr_dl sdl;
146 1.11 seanb } su;
147 1.11 seanb struct sockaddr_dl *sdlp;
148 1.11 seanb sdlp = &su.sdl;
149 1.1 itojun #endif
150 1.1 itojun
151 1.1 itojun if ((kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, buf)) == NULL) {
152 1.1 itojun perror("kvm_openfiles");
153 1.1 itojun exit(1);
154 1.1 itojun }
155 1.1 itojun if (kvm_nlist(kvmd, nl) < 0) {
156 1.1 itojun perror("kvm_nlist");
157 1.1 itojun exit(1);
158 1.1 itojun }
159 1.1 itojun if (nl[N_IFNET].n_value == 0) {
160 1.1 itojun printf("symbol %s not found\n", nl[N_IFNET].n_name);
161 1.1 itojun exit(1);
162 1.1 itojun }
163 1.1 itojun KREAD(nl[N_IFNET].n_value, &ifp, struct ifnet *);
164 1.1 itojun while (ifp) {
165 1.1 itojun KREAD(ifp, &ifnet, struct ifnet);
166 1.10 lukem printf("%s:\n", if_indextoname(ifnet.if_index, ifnam));
167 1.1 itojun
168 1.5 itojun #if defined(__NetBSD__) || defined(__OpenBSD__)
169 1.5 itojun if6_addrlist(ifnet.if_addrlist.tqh_first);
170 1.5 itojun nifp = ifnet.if_list.tqe_next;
171 1.5 itojun #elif defined(__FreeBSD__) && __FreeBSD__ >= 3
172 1.1 itojun if6_addrlist(TAILQ_FIRST(&ifnet.if_addrhead));
173 1.5 itojun nifp = ifnet.if_link.tqe_next;
174 1.5 itojun #else
175 1.1 itojun if6_addrlist(ifnet.if_addrlist);
176 1.5 itojun nifp = ifnet.if_next;
177 1.5 itojun #endif
178 1.1 itojun
179 1.5 itojun #ifdef __NetBSD__
180 1.11 seanb KREAD(ifnet.if_sadl, sdlp, struct sockaddr_dl);
181 1.11 seanb if (sdlp->sdl_type == IFT_ETHER) {
182 1.11 seanb /* If we didn't get all of it, try again */
183 1.11 seanb if (sdlp->sdl_len > sizeof(struct sockaddr_dl))
184 1.11 seanb kread((u_long)ifnet.if_sadl, (void *)sdlp, sdlp->sdl_len);
185 1.1 itojun printf("\tenaddr %s",
186 1.11 seanb ether_ntoa((struct ether_addr *)LLADDR(sdlp)));
187 1.1 itojun KREAD(ifp, &ec, struct ethercom);
188 1.1 itojun printf(" multicnt %d", ec.ec_multicnt);
189 1.1 itojun acmc(ec.ec_multiaddrs.lh_first);
190 1.1 itojun printf("\n");
191 1.1 itojun }
192 1.5 itojun #elif defined(__FreeBSD__) && __FreeBSD__ >= 3
193 1.5 itojun /* not supported */
194 1.5 itojun #else
195 1.5 itojun if (ifnet.if_type == IFT_ETHER) {
196 1.5 itojun KREAD(ifp, &arpcom, struct arpcom);
197 1.5 itojun printf("\tenaddr %s",
198 1.5 itojun ether_ntoa((struct ether_addr *)arpcom.ac_enaddr));
199 1.5 itojun KREAD(ifp, &arpcom, struct arpcom);
200 1.5 itojun printf(" multicnt %d", arpcom.ac_multicnt);
201 1.5 itojun #ifdef __OpenBSD__
202 1.5 itojun acmc(arpcom.ac_multiaddrs.lh_first);
203 1.5 itojun #else
204 1.5 itojun acmc(arpcom.ac_multiaddrs);
205 1.5 itojun #endif
206 1.5 itojun printf("\n");
207 1.5 itojun }
208 1.5 itojun #endif
209 1.1 itojun
210 1.5 itojun ifp = nifp;
211 1.1 itojun }
212 1.1 itojun
213 1.1 itojun exit(0);
214 1.1 itojun /*NOTREACHED*/
215 1.1 itojun }
216 1.1 itojun
217 1.1 itojun char *ifname(ifp)
218 1.1 itojun struct ifnet *ifp;
219 1.1 itojun {
220 1.1 itojun static char buf[BUFSIZ];
221 1.6 itojun #if defined(__NetBSD__) || defined(__OpenBSD__)
222 1.6 itojun struct ifnet ifnet;
223 1.6 itojun #endif
224 1.1 itojun
225 1.5 itojun #if defined(__NetBSD__) || defined(__OpenBSD__)
226 1.6 itojun KREAD(ifp, &ifnet, struct ifnet);
227 1.6 itojun strncpy(buf, ifnet.if_xname, BUFSIZ);
228 1.5 itojun #else
229 1.1 itojun KREAD(ifp->if_name, buf, IFNAMSIZ);
230 1.1 itojun #endif
231 1.1 itojun return buf;
232 1.1 itojun }
233 1.1 itojun
234 1.1 itojun void kread(addr, buf, len)
235 1.1 itojun u_long addr;
236 1.1 itojun void *buf;
237 1.1 itojun int len;
238 1.1 itojun {
239 1.1 itojun if (kvm_read(kvmd, addr, buf, len) != len) {
240 1.1 itojun perror("kvm_read");
241 1.1 itojun exit(1);
242 1.1 itojun }
243 1.1 itojun }
244 1.1 itojun
245 1.5 itojun #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
246 1.1 itojun void acmc(am)
247 1.1 itojun struct ether_multi *am;
248 1.1 itojun {
249 1.1 itojun struct ether_multi em;
250 1.1 itojun
251 1.1 itojun while (am) {
252 1.1 itojun KREAD(am, &em, struct ether_multi);
253 1.1 itojun
254 1.1 itojun printf("\n\t\t");
255 1.5 itojun printf("%s -- ", ether_ntoa((struct ether_addr *)em.enm_addrlo));
256 1.5 itojun printf("%s ", ether_ntoa((struct ether_addr *)&em.enm_addrhi));
257 1.5 itojun printf("%d", em.enm_refcount);
258 1.5 itojun #if !defined(__NetBSD__) && !defined(__OpenBSD__)
259 1.1 itojun am = em.enm_next;
260 1.1 itojun #else
261 1.1 itojun am = em.enm_list.le_next;
262 1.1 itojun #endif
263 1.1 itojun }
264 1.1 itojun }
265 1.5 itojun #endif
266 1.1 itojun
267 1.1 itojun void
268 1.1 itojun if6_addrlist(ifap)
269 1.1 itojun struct ifaddr *ifap;
270 1.1 itojun {
271 1.1 itojun struct ifaddr ifa;
272 1.1 itojun struct sockaddr sa;
273 1.1 itojun struct in6_ifaddr if6a;
274 1.1 itojun struct in6_multi *mc = 0;
275 1.1 itojun struct ifaddr *ifap0;
276 1.1 itojun
277 1.1 itojun ifap0 = ifap;
278 1.1 itojun while (ifap) {
279 1.1 itojun KREAD(ifap, &ifa, struct ifaddr);
280 1.1 itojun if (ifa.ifa_addr == NULL)
281 1.1 itojun goto nextifap;
282 1.1 itojun KREAD(ifa.ifa_addr, &sa, struct sockaddr);
283 1.1 itojun if (sa.sa_family != PF_INET6)
284 1.1 itojun goto nextifap;
285 1.1 itojun KREAD(ifap, &if6a, struct in6_ifaddr);
286 1.8 itojun printf("\tinet6 %s\n", inet6_n2a(&if6a.ia_addr.sin6_addr));
287 1.5 itojun #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
288 1.1 itojun mc = mc ? mc : if6a.ia6_multiaddrs.lh_first;
289 1.5 itojun #endif
290 1.1 itojun nextifap:
291 1.5 itojun #if defined(__NetBSD__) || defined(__OpenBSD__)
292 1.5 itojun ifap = ifa.ifa_list.tqe_next;
293 1.5 itojun #elif defined(__FreeBSD__) && __FreeBSD__ >= 3
294 1.1 itojun ifap = ifa.ifa_link.tqe_next;
295 1.1 itojun #else
296 1.1 itojun ifap = ifa.ifa_next;
297 1.1 itojun #endif /* __FreeBSD__ >= 3 */
298 1.1 itojun }
299 1.1 itojun #if defined(__FreeBSD__) && __FreeBSD__ >= 3
300 1.1 itojun if (ifap0) {
301 1.1 itojun struct ifnet ifnet;
302 1.1 itojun struct ifmultiaddr ifm, *ifmp = 0;
303 1.1 itojun struct sockaddr_in6 sin6;
304 1.1 itojun struct in6_multi in6m;
305 1.1 itojun struct sockaddr_dl sdl;
306 1.1 itojun int in6_multilist_done = 0;
307 1.1 itojun
308 1.1 itojun KREAD(ifap0, &ifa, struct ifaddr);
309 1.1 itojun KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
310 1.1 itojun if (ifnet.if_multiaddrs.lh_first)
311 1.1 itojun ifmp = ifnet.if_multiaddrs.lh_first;
312 1.1 itojun while (ifmp) {
313 1.1 itojun KREAD(ifmp, &ifm, struct ifmultiaddr);
314 1.1 itojun if (ifm.ifma_addr == NULL)
315 1.1 itojun goto nextmulti;
316 1.1 itojun KREAD(ifm.ifma_addr, &sa, struct sockaddr);
317 1.1 itojun if (sa.sa_family != AF_INET6)
318 1.1 itojun goto nextmulti;
319 1.1 itojun (void)in6_multientry((struct in6_multi *)
320 1.1 itojun ifm.ifma_protospec);
321 1.1 itojun if (ifm.ifma_lladdr == 0)
322 1.1 itojun goto nextmulti;
323 1.1 itojun KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
324 1.1 itojun printf("\t\t\tmcast-macaddr %s multicnt %d\n",
325 1.1 itojun ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
326 1.1 itojun ifm.ifma_refcount);
327 1.1 itojun nextmulti:
328 1.1 itojun ifmp = ifm.ifma_link.le_next;
329 1.1 itojun }
330 1.1 itojun }
331 1.1 itojun #else
332 1.1 itojun if (mc)
333 1.1 itojun in6_multilist(mc);
334 1.6 itojun #endif
335 1.6 itojun #ifdef N_IN6_MK
336 1.8 itojun if (nl[N_IN6_MK].n_value != 0) {
337 1.8 itojun LIST_HEAD(in6_mktype, multi6_kludge) in6_mk;
338 1.8 itojun struct multi6_kludge *mkp, mk;
339 1.8 itojun char *nam;
340 1.6 itojun
341 1.8 itojun KREAD(nl[N_IN6_MK].n_value, &in6_mk, struct in6_mktype);
342 1.8 itojun KREAD(ifap0, &ifa, struct ifaddr);
343 1.6 itojun
344 1.8 itojun nam = strdup(ifname(ifa.ifa_ifp));
345 1.6 itojun
346 1.8 itojun for (mkp = in6_mk.lh_first; mkp; mkp = mk.mk_entry.le_next) {
347 1.8 itojun KREAD(mkp, &mk, struct multi6_kludge);
348 1.8 itojun if (strcmp(nam, ifname(mk.mk_ifp)) == 0 &&
349 1.8 itojun mk.mk_head.lh_first) {
350 1.8 itojun printf("\t(on kludge entry for %s)\n", nam);
351 1.8 itojun in6_multilist(mk.mk_head.lh_first);
352 1.8 itojun }
353 1.6 itojun }
354 1.8 itojun
355 1.8 itojun free(nam);
356 1.6 itojun }
357 1.5 itojun #endif
358 1.1 itojun }
359 1.1 itojun
360 1.1 itojun struct in6_multi *
361 1.1 itojun in6_multientry(mc)
362 1.1 itojun struct in6_multi *mc;
363 1.1 itojun {
364 1.1 itojun struct in6_multi multi;
365 1.1 itojun
366 1.1 itojun KREAD(mc, &multi, struct in6_multi);
367 1.8 itojun printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr));
368 1.8 itojun printf(" refcnt %u\n", multi.in6m_refcount);
369 1.1 itojun return(multi.in6m_entry.le_next);
370 1.1 itojun }
371 1.1 itojun
372 1.1 itojun void
373 1.1 itojun in6_multilist(mc)
374 1.1 itojun struct in6_multi *mc;
375 1.1 itojun {
376 1.1 itojun while (mc)
377 1.1 itojun mc = in6_multientry(mc);
378 1.1 itojun }
379 1.5 itojun
380 1.5 itojun #if !defined(__NetBSD__) && !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined(__OpenBSD__)
381 1.5 itojun static char *
382 1.5 itojun ether_ntoa(e)
383 1.5 itojun struct ether_addr *e;
384 1.5 itojun {
385 1.5 itojun static char buf[20];
386 1.5 itojun u_char *p;
387 1.5 itojun
388 1.5 itojun p = (u_char *)e;
389 1.5 itojun
390 1.5 itojun snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
391 1.5 itojun p[0], p[1], p[2], p[3], p[4], p[5]);
392 1.5 itojun return buf;
393 1.5 itojun }
394 1.5 itojun #endif
395