ifmcstat.c revision 1.1 1 1.1 itojun /*
2 1.1 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 1.1 itojun * All rights reserved.
4 1.1 itojun *
5 1.1 itojun * Redistribution and use in source and binary forms, with or without
6 1.1 itojun * modification, are permitted provided that the following conditions
7 1.1 itojun * are met:
8 1.1 itojun * 1. Redistributions of source code must retain the above copyright
9 1.1 itojun * notice, this list of conditions and the following disclaimer.
10 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 itojun * notice, this list of conditions and the following disclaimer in the
12 1.1 itojun * documentation and/or other materials provided with the distribution.
13 1.1 itojun * 3. Neither the name of the project nor the names of its contributors
14 1.1 itojun * may be used to endorse or promote products derived from this software
15 1.1 itojun * without specific prior written permission.
16 1.1 itojun *
17 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 itojun * SUCH DAMAGE.
28 1.1 itojun */
29 1.1 itojun
30 1.1 itojun #include <stdio.h>
31 1.1 itojun #include <stdlib.h>
32 1.1 itojun #include <fcntl.h>
33 1.1 itojun #include <kvm.h>
34 1.1 itojun #include <nlist.h>
35 1.1 itojun #include <string.h>
36 1.1 itojun
37 1.1 itojun #include <sys/types.h>
38 1.1 itojun #include <sys/socket.h>
39 1.1 itojun #include <net/if.h>
40 1.1 itojun #if defined(__FreeBSD__) && __FreeBSD__ >= 3
41 1.1 itojun #include <net/if_var.h>
42 1.1 itojun #include <net/if_dl.h>
43 1.1 itojun #endif /* __FreeBSD__ >= 3 */
44 1.1 itojun #include <netinet/in.h>
45 1.1 itojun #ifndef __NetBSD__
46 1.1 itojun #ifdef __FreeBSD__
47 1.1 itojun #define KERNEL
48 1.1 itojun #endif
49 1.1 itojun #include <netinet/if_ether.h>
50 1.1 itojun #ifdef __FreeBSD__
51 1.1 itojun #undef KERNEL
52 1.1 itojun #endif
53 1.1 itojun #else /* __NetBSD__ */
54 1.1 itojun #include <net/if_ether.h>
55 1.1 itojun #include <net/if_dl.h>
56 1.1 itojun #include <net/if_types.h>
57 1.1 itojun #endif /* __NetBSD__ */
58 1.1 itojun #include <netinet/in_var.h>
59 1.1 itojun #include <arpa/inet.h>
60 1.1 itojun
61 1.1 itojun kvm_t *kvmd;
62 1.1 itojun
63 1.1 itojun struct nlist nl[] = {
64 1.1 itojun #define N_IFNET 0
65 1.1 itojun { "_ifnet" },
66 1.1 itojun { "" },
67 1.1 itojun };
68 1.1 itojun
69 1.1 itojun const char *inet6_n2a __P((struct in6_addr *));
70 1.1 itojun int main __P((void));
71 1.1 itojun char *ifname __P((struct ifnet *));
72 1.1 itojun #ifndef __NetBSD__
73 1.1 itojun int havearpcom __P((char *));
74 1.1 itojun #endif /* __NetBSD__ */
75 1.1 itojun void kread __P((u_long, void *, int));
76 1.1 itojun #if !defined(__FreeBSD__) || __FreeBSD__ < 3
77 1.1 itojun void acmc __P((struct ether_multi *));
78 1.1 itojun #else
79 1.1 itojun void ifmamc __P((struct ifmultiaddr *ifma_p));
80 1.1 itojun #endif /* __FreeBSD__ < 3 */
81 1.1 itojun void if6_addrlist __P((struct ifaddr *));
82 1.1 itojun void in6_multilist __P((struct in6_multi *));
83 1.1 itojun struct in6_multi * in6_multientry __P((struct in6_multi *));
84 1.1 itojun
85 1.1 itojun #define KREAD(addr, buf, type) \
86 1.1 itojun kread((u_long)addr, (void *)buf, sizeof(type))
87 1.1 itojun
88 1.1 itojun const char *inet6_n2a(p)
89 1.1 itojun struct in6_addr *p;
90 1.1 itojun {
91 1.1 itojun static char buf[BUFSIZ];
92 1.1 itojun
93 1.1 itojun if (IN6_IS_ADDR_ANY(p))
94 1.1 itojun return "*";
95 1.1 itojun return inet_ntop(AF_INET6, (void *)p, buf, sizeof(buf));
96 1.1 itojun }
97 1.1 itojun
98 1.1 itojun int main()
99 1.1 itojun {
100 1.1 itojun char buf[BUFSIZ], ifname[IFNAMSIZ];
101 1.1 itojun struct ifnet *ifp, ifnet;
102 1.1 itojun #ifndef __NetBSD__
103 1.1 itojun struct arpcom arpcom;
104 1.1 itojun #else
105 1.1 itojun struct ethercom ec;
106 1.1 itojun struct sockaddr_dl sdl;
107 1.1 itojun #endif
108 1.1 itojun
109 1.1 itojun if ((kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, buf)) == NULL) {
110 1.1 itojun perror("kvm_openfiles");
111 1.1 itojun exit(1);
112 1.1 itojun }
113 1.1 itojun if (kvm_nlist(kvmd, nl) < 0) {
114 1.1 itojun perror("kvm_nlist");
115 1.1 itojun exit(1);
116 1.1 itojun }
117 1.1 itojun if (nl[N_IFNET].n_value == 0) {
118 1.1 itojun printf("symbol %s not found\n", nl[N_IFNET].n_name);
119 1.1 itojun exit(1);
120 1.1 itojun }
121 1.1 itojun KREAD(nl[N_IFNET].n_value, &ifp, struct ifnet *);
122 1.1 itojun while (ifp) {
123 1.1 itojun KREAD(ifp, &ifnet, struct ifnet);
124 1.1 itojun printf("%s:\n", if_indextoname(ifnet.if_index, ifname));
125 1.1 itojun
126 1.1 itojun #ifndef __NetBSD__
127 1.1 itojun #if defined(__FreeBSD__) && __FreeBSD__ >= 3
128 1.1 itojun if6_addrlist(TAILQ_FIRST(&ifnet.if_addrhead));
129 1.1 itojun ifp = ifnet.if_link.tqe_next;
130 1.1 itojun #else /* __FreeBSD__ >= 3 */
131 1.1 itojun if6_addrlist(ifnet.if_addrlist);
132 1.1 itojun
133 1.1 itojun if (havearpcom(ifname)) {
134 1.1 itojun KREAD(ifp, &arpcom, struct arpcom);
135 1.1 itojun printf("\tenaddr %02x:%02x:%02x:%02x:%02x:%02x",
136 1.1 itojun arpcom.ac_enaddr[0], arpcom.ac_enaddr[1],
137 1.1 itojun arpcom.ac_enaddr[2], arpcom.ac_enaddr[3],
138 1.1 itojun arpcom.ac_enaddr[4], arpcom.ac_enaddr[5]);
139 1.1 itojun printf(" multicnt %d", arpcom.ac_multicnt);
140 1.1 itojun acmc(arpcom.ac_multiaddrs);
141 1.1 itojun printf("\n");
142 1.1 itojun }
143 1.1 itojun
144 1.1 itojun ifp = ifnet.if_next;
145 1.1 itojun #endif /* __FreeBSD__ >= 3 */
146 1.1 itojun #else
147 1.1 itojun if6_addrlist(ifnet.if_addrlist.tqh_first);
148 1.1 itojun
149 1.1 itojun KREAD(ifnet.if_sadl, &sdl, struct sockaddr_dl);
150 1.1 itojun if (sdl.sdl_type == IFT_ETHER) {
151 1.1 itojun printf("\tenaddr %s",
152 1.1 itojun ether_ntoa((struct ether_addr *)LLADDR(&sdl)));
153 1.1 itojun KREAD(ifp, &ec, struct ethercom);
154 1.1 itojun printf(" multicnt %d", ec.ec_multicnt);
155 1.1 itojun acmc(ec.ec_multiaddrs.lh_first);
156 1.1 itojun printf("\n");
157 1.1 itojun }
158 1.1 itojun
159 1.1 itojun ifp = ifnet.if_list.tqe_next;
160 1.1 itojun #endif
161 1.1 itojun }
162 1.1 itojun
163 1.1 itojun exit(0);
164 1.1 itojun /*NOTREACHED*/
165 1.1 itojun }
166 1.1 itojun
167 1.1 itojun char *ifname(ifp)
168 1.1 itojun struct ifnet *ifp;
169 1.1 itojun {
170 1.1 itojun static char buf[BUFSIZ];
171 1.1 itojun
172 1.1 itojun #ifndef __NetBSD__
173 1.1 itojun KREAD(ifp->if_name, buf, IFNAMSIZ);
174 1.1 itojun #else
175 1.1 itojun KREAD(ifp->if_xname, buf, IFNAMSIZ);
176 1.1 itojun #endif
177 1.1 itojun return buf;
178 1.1 itojun }
179 1.1 itojun
180 1.1 itojun void kread(addr, buf, len)
181 1.1 itojun u_long addr;
182 1.1 itojun void *buf;
183 1.1 itojun int len;
184 1.1 itojun {
185 1.1 itojun if (kvm_read(kvmd, addr, buf, len) != len) {
186 1.1 itojun perror("kvm_read");
187 1.1 itojun exit(1);
188 1.1 itojun }
189 1.1 itojun }
190 1.1 itojun
191 1.1 itojun #ifndef __NetBSD__
192 1.1 itojun int havearpcom(p)
193 1.1 itojun char *p;
194 1.1 itojun {
195 1.1 itojun if (strncmp(p, "ne", 2) == 0) return 1;
196 1.1 itojun if (strncmp(p, "eo", 2) == 0) return 1;
197 1.1 itojun if (strncmp(p, "ep", 2) == 0) return 1;
198 1.1 itojun if (strncmp(p, "tn", 2) == 0) return 1;
199 1.1 itojun if (strncmp(p, "de", 2) == 0) return 1;
200 1.1 itojun if (strncmp(p, "hpp", 3) == 0) return 1;
201 1.1 itojun if (strncmp(p, "re", 2) == 0) return 1;
202 1.1 itojun if (strncmp(p, "ef", 2) == 0) return 1;
203 1.1 itojun if (strncmp(p, "di", 2) == 0) return 1;
204 1.1 itojun if (strncmp(p, "we", 2) == 0) return 1;
205 1.1 itojun if (strncmp(p, "el", 2) == 0) return 1;
206 1.1 itojun if (strncmp(p, "ex", 2) == 0) return 1;
207 1.1 itojun if (strncmp(p, "pe", 2) == 0) return 1;
208 1.1 itojun if (strncmp(p, "xir", 3) == 0) return 1;
209 1.1 itojun if (strncmp(p, "fxp", 2) == 0) return 1;
210 1.1 itojun if (strncmp(p, "fe", 2) == 0) return 1;
211 1.1 itojun if (strncmp(p, "cnw", 2) == 0) return 1;
212 1.1 itojun /* if (strncmp(p, "tr", 2) == 0) return 1; ??? */
213 1.1 itojun /* if (strncmp(p, "fea", 3) == 0) return 1; ??? */
214 1.1 itojun /* if (strncmp(p, "fpa", 3) == 0) return 1; ??? */
215 1.1 itojun /* if (strncmp(p, "te", 2) == 0) return 1; ??? */
216 1.1 itojun return 0;
217 1.1 itojun }
218 1.1 itojun #endif
219 1.1 itojun
220 1.1 itojun #if !defined(__FreeBSD__) || __FreeBSD__ < 3
221 1.1 itojun void acmc(am)
222 1.1 itojun struct ether_multi *am;
223 1.1 itojun {
224 1.1 itojun struct ether_multi em;
225 1.1 itojun
226 1.1 itojun while (am) {
227 1.1 itojun KREAD(am, &em, struct ether_multi);
228 1.1 itojun
229 1.1 itojun printf("\n\t\t");
230 1.1 itojun #ifndef __NetBSD__
231 1.1 itojun printf("%02x:%02x:%02x:%02x:%02x:%02x--",
232 1.1 itojun em.enm_addrlo[0], em.enm_addrlo[1],
233 1.1 itojun em.enm_addrlo[2], em.enm_addrlo[3],
234 1.1 itojun em.enm_addrlo[4], em.enm_addrlo[5]);
235 1.1 itojun printf("%02x:%02x:%02x:%02x:%02x:%02x",
236 1.1 itojun em.enm_addrhi[0], em.enm_addrhi[1],
237 1.1 itojun em.enm_addrhi[2], em.enm_addrhi[3],
238 1.1 itojun em.enm_addrhi[4], em.enm_addrhi[5]);
239 1.1 itojun printf(" %d", em.enm_refcount);
240 1.1 itojun am = em.enm_next;
241 1.1 itojun #else
242 1.1 itojun printf("%s -- ", ether_ntoa((struct ether_addr *)&em.enm_addrlo));
243 1.1 itojun printf("%s ", ether_ntoa((struct ether_addr *)&em.enm_addrhi));
244 1.1 itojun printf("%d", em.enm_refcount);
245 1.1 itojun am = em.enm_list.le_next;
246 1.1 itojun #endif
247 1.1 itojun }
248 1.1 itojun }
249 1.1 itojun #endif /* __FreeBSD__ < 3 */
250 1.1 itojun
251 1.1 itojun void
252 1.1 itojun if6_addrlist(ifap)
253 1.1 itojun struct ifaddr *ifap;
254 1.1 itojun {
255 1.1 itojun static char in6buf[BUFSIZ];
256 1.1 itojun struct ifaddr ifa;
257 1.1 itojun struct sockaddr sa;
258 1.1 itojun struct in6_ifaddr if6a;
259 1.1 itojun struct in6_multi *mc = 0;
260 1.1 itojun #if defined(__FreeBSD__) && __FreeBSD__ >= 3
261 1.1 itojun struct ifaddr *ifap0;
262 1.1 itojun #endif /* __FreeBSD__ >= 3 */
263 1.1 itojun
264 1.1 itojun #if defined(__FreeBSD__) && __FreeBSD__ >= 3
265 1.1 itojun ifap0 = ifap;
266 1.1 itojun #endif /* __FreeBSD__ >= 3 */
267 1.1 itojun while (ifap) {
268 1.1 itojun KREAD(ifap, &ifa, struct ifaddr);
269 1.1 itojun if (ifa.ifa_addr == NULL)
270 1.1 itojun goto nextifap;
271 1.1 itojun KREAD(ifa.ifa_addr, &sa, struct sockaddr);
272 1.1 itojun if (sa.sa_family != PF_INET6)
273 1.1 itojun goto nextifap;
274 1.1 itojun KREAD(ifap, &if6a, struct in6_ifaddr);
275 1.1 itojun printf("\tinet6 %s\n",
276 1.1 itojun inet_ntop(AF_INET6,
277 1.1 itojun (const void *)&if6a.ia_addr.sin6_addr,
278 1.1 itojun in6buf, sizeof(in6buf)));
279 1.1 itojun #if !defined(__FreeBSD__) || __FreeBSD__ < 3
280 1.1 itojun mc = mc ? mc : if6a.ia6_multiaddrs.lh_first;
281 1.1 itojun #endif /* !__FreeBSD__ || __FreeBSD__ < 3 */
282 1.1 itojun nextifap:
283 1.1 itojun #ifndef __NetBSD__
284 1.1 itojun #if defined(__FreeBSD__) && __FreeBSD__ >= 3
285 1.1 itojun ifap = ifa.ifa_link.tqe_next;
286 1.1 itojun #else
287 1.1 itojun ifap = ifa.ifa_next;
288 1.1 itojun #endif /* __FreeBSD__ >= 3 */
289 1.1 itojun #else
290 1.1 itojun ifap = ifa.ifa_list.tqe_next;
291 1.1 itojun #endif
292 1.1 itojun }
293 1.1 itojun #if defined(__FreeBSD__) && __FreeBSD__ >= 3
294 1.1 itojun if (ifap0) {
295 1.1 itojun struct ifnet ifnet;
296 1.1 itojun struct ifmultiaddr ifm, *ifmp = 0;
297 1.1 itojun struct sockaddr_in6 sin6;
298 1.1 itojun struct in6_multi in6m;
299 1.1 itojun struct sockaddr_dl sdl;
300 1.1 itojun int in6_multilist_done = 0;
301 1.1 itojun
302 1.1 itojun KREAD(ifap0, &ifa, struct ifaddr);
303 1.1 itojun KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
304 1.1 itojun if (ifnet.if_multiaddrs.lh_first)
305 1.1 itojun ifmp = ifnet.if_multiaddrs.lh_first;
306 1.1 itojun while (ifmp) {
307 1.1 itojun KREAD(ifmp, &ifm, struct ifmultiaddr);
308 1.1 itojun if (ifm.ifma_addr == NULL)
309 1.1 itojun goto nextmulti;
310 1.1 itojun KREAD(ifm.ifma_addr, &sa, struct sockaddr);
311 1.1 itojun if (sa.sa_family != AF_INET6)
312 1.1 itojun goto nextmulti;
313 1.1 itojun (void)in6_multientry((struct in6_multi *)
314 1.1 itojun ifm.ifma_protospec);
315 1.1 itojun if (ifm.ifma_lladdr == 0)
316 1.1 itojun goto nextmulti;
317 1.1 itojun KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
318 1.1 itojun printf("\t\t\tmcast-macaddr %s multicnt %d\n",
319 1.1 itojun ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
320 1.1 itojun ifm.ifma_refcount);
321 1.1 itojun nextmulti:
322 1.1 itojun ifmp = ifm.ifma_link.le_next;
323 1.1 itojun }
324 1.1 itojun }
325 1.1 itojun #else
326 1.1 itojun if (mc)
327 1.1 itojun in6_multilist(mc);
328 1.1 itojun #endif /* !__FreeBSD__ || __FreeBSD__ < 3 */
329 1.1 itojun }
330 1.1 itojun
331 1.1 itojun struct in6_multi *
332 1.1 itojun in6_multientry(mc)
333 1.1 itojun struct in6_multi *mc;
334 1.1 itojun {
335 1.1 itojun static char mcbuf[BUFSIZ];
336 1.1 itojun struct in6_multi multi;
337 1.1 itojun
338 1.1 itojun KREAD(mc, &multi, struct in6_multi);
339 1.1 itojun printf("\t\tgroup %s\n", inet_ntop(AF_INET6,
340 1.1 itojun (const void *)&multi.in6m_addr,
341 1.1 itojun mcbuf, sizeof(mcbuf)));
342 1.1 itojun return(multi.in6m_entry.le_next);
343 1.1 itojun }
344 1.1 itojun
345 1.1 itojun void
346 1.1 itojun in6_multilist(mc)
347 1.1 itojun struct in6_multi *mc;
348 1.1 itojun {
349 1.1 itojun while (mc)
350 1.1 itojun mc = in6_multientry(mc);
351 1.1 itojun }
352