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