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