xref: /netbsd/usr.sbin/ifmcstat/ifmcstat.c (revision 6550d01e)
1 /*	$NetBSD: ifmcstat.c,v 1.10 2009/04/19 07:49:07 lukem 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 	struct sockaddr_dl sdl;
144 #endif
145 
146 	if ((kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, buf)) == NULL) {
147 		perror("kvm_openfiles");
148 		exit(1);
149 	}
150 	if (kvm_nlist(kvmd, nl) < 0) {
151 		perror("kvm_nlist");
152 		exit(1);
153 	}
154 	if (nl[N_IFNET].n_value == 0) {
155 		printf("symbol %s not found\n", nl[N_IFNET].n_name);
156 		exit(1);
157 	}
158 	KREAD(nl[N_IFNET].n_value, &ifp, struct ifnet *);
159 	while (ifp) {
160 		KREAD(ifp, &ifnet, struct ifnet);
161 		printf("%s:\n", if_indextoname(ifnet.if_index, ifnam));
162 
163 #if defined(__NetBSD__) || defined(__OpenBSD__)
164 		if6_addrlist(ifnet.if_addrlist.tqh_first);
165 		nifp = ifnet.if_list.tqe_next;
166 #elif defined(__FreeBSD__) && __FreeBSD__ >= 3
167 		if6_addrlist(TAILQ_FIRST(&ifnet.if_addrhead));
168 		nifp = ifnet.if_link.tqe_next;
169 #else
170 		if6_addrlist(ifnet.if_addrlist);
171 		nifp = ifnet.if_next;
172 #endif
173 
174 #ifdef __NetBSD__
175 		KREAD(ifnet.if_sadl, &sdl, struct sockaddr_dl);
176 		if (sdl.sdl_type == IFT_ETHER) {
177 			printf("\tenaddr %s",
178 			       ether_ntoa((struct ether_addr *)LLADDR(&sdl)));
179 			KREAD(ifp, &ec, struct ethercom);
180 			printf(" multicnt %d", ec.ec_multicnt);
181 			acmc(ec.ec_multiaddrs.lh_first);
182 			printf("\n");
183 		}
184 #elif defined(__FreeBSD__) && __FreeBSD__ >= 3
185 		/* not supported */
186 #else
187 		if (ifnet.if_type == IFT_ETHER) {
188 			KREAD(ifp, &arpcom, struct arpcom);
189 			printf("\tenaddr %s",
190 			    ether_ntoa((struct ether_addr *)arpcom.ac_enaddr));
191 			KREAD(ifp, &arpcom, struct arpcom);
192 			printf(" multicnt %d", arpcom.ac_multicnt);
193 #ifdef __OpenBSD__
194 			acmc(arpcom.ac_multiaddrs.lh_first);
195 #else
196 			acmc(arpcom.ac_multiaddrs);
197 #endif
198 			printf("\n");
199 		}
200 #endif
201 
202 		ifp = nifp;
203 	}
204 
205 	exit(0);
206 	/*NOTREACHED*/
207 }
208 
209 char *ifname(ifp)
210 	struct ifnet *ifp;
211 {
212 	static char buf[BUFSIZ];
213 #if defined(__NetBSD__) || defined(__OpenBSD__)
214 	struct ifnet ifnet;
215 #endif
216 
217 #if defined(__NetBSD__) || defined(__OpenBSD__)
218 	KREAD(ifp, &ifnet, struct ifnet);
219 	strncpy(buf, ifnet.if_xname, BUFSIZ);
220 #else
221 	KREAD(ifp->if_name, buf, IFNAMSIZ);
222 #endif
223 	return buf;
224 }
225 
226 void kread(addr, buf, len)
227 	u_long addr;
228 	void *buf;
229 	int len;
230 {
231 	if (kvm_read(kvmd, addr, buf, len) != len) {
232 		perror("kvm_read");
233 		exit(1);
234 	}
235 }
236 
237 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
238 void acmc(am)
239 	struct ether_multi *am;
240 {
241 	struct ether_multi em;
242 
243 	while (am) {
244 		KREAD(am, &em, struct ether_multi);
245 
246 		printf("\n\t\t");
247 		printf("%s -- ", ether_ntoa((struct ether_addr *)em.enm_addrlo));
248 		printf("%s ", ether_ntoa((struct ether_addr *)&em.enm_addrhi));
249 		printf("%d", em.enm_refcount);
250 #if !defined(__NetBSD__) && !defined(__OpenBSD__)
251 		am = em.enm_next;
252 #else
253 		am = em.enm_list.le_next;
254 #endif
255 	}
256 }
257 #endif
258 
259 void
260 if6_addrlist(ifap)
261 	struct ifaddr *ifap;
262 {
263 	struct ifaddr ifa;
264 	struct sockaddr sa;
265 	struct in6_ifaddr if6a;
266 	struct in6_multi *mc = 0;
267 	struct ifaddr *ifap0;
268 
269 	ifap0 = ifap;
270 	while (ifap) {
271 		KREAD(ifap, &ifa, struct ifaddr);
272 		if (ifa.ifa_addr == NULL)
273 			goto nextifap;
274 		KREAD(ifa.ifa_addr, &sa, struct sockaddr);
275 		if (sa.sa_family != PF_INET6)
276 			goto nextifap;
277 		KREAD(ifap, &if6a, struct in6_ifaddr);
278 		printf("\tinet6 %s\n", inet6_n2a(&if6a.ia_addr.sin6_addr));
279 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
280 		mc = mc ? mc : if6a.ia6_multiaddrs.lh_first;
281 #endif
282 	nextifap:
283 #if defined(__NetBSD__) || defined(__OpenBSD__)
284 		ifap = ifa.ifa_list.tqe_next;
285 #elif defined(__FreeBSD__) && __FreeBSD__ >= 3
286 		ifap = ifa.ifa_link.tqe_next;
287 #else
288 		ifap = ifa.ifa_next;
289 #endif /* __FreeBSD__ >= 3 */
290 	}
291 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
292 	if (ifap0) {
293 		struct ifnet ifnet;
294 		struct ifmultiaddr ifm, *ifmp = 0;
295 		struct sockaddr_in6 sin6;
296 		struct in6_multi in6m;
297 		struct sockaddr_dl sdl;
298 		int in6_multilist_done = 0;
299 
300 		KREAD(ifap0, &ifa, struct ifaddr);
301 		KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
302 		if (ifnet.if_multiaddrs.lh_first)
303 			ifmp = ifnet.if_multiaddrs.lh_first;
304 		while (ifmp) {
305 			KREAD(ifmp, &ifm, struct ifmultiaddr);
306 			if (ifm.ifma_addr == NULL)
307 				goto nextmulti;
308 			KREAD(ifm.ifma_addr, &sa, struct sockaddr);
309 			if (sa.sa_family != AF_INET6)
310 				goto nextmulti;
311 			(void)in6_multientry((struct in6_multi *)
312 					     ifm.ifma_protospec);
313 			if (ifm.ifma_lladdr == 0)
314 				goto nextmulti;
315 			KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
316 			printf("\t\t\tmcast-macaddr %s multicnt %d\n",
317 			       ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
318 			       ifm.ifma_refcount);
319 		    nextmulti:
320 			ifmp = ifm.ifma_link.le_next;
321 		}
322 	}
323 #else
324 	if (mc)
325 		in6_multilist(mc);
326 #endif
327 #ifdef N_IN6_MK
328 	if (nl[N_IN6_MK].n_value != 0) {
329 		LIST_HEAD(in6_mktype, multi6_kludge) in6_mk;
330 		struct multi6_kludge *mkp, mk;
331 		char *nam;
332 
333 		KREAD(nl[N_IN6_MK].n_value, &in6_mk, struct in6_mktype);
334 		KREAD(ifap0, &ifa, struct ifaddr);
335 
336 		nam = strdup(ifname(ifa.ifa_ifp));
337 
338 		for (mkp = in6_mk.lh_first; mkp; mkp = mk.mk_entry.le_next) {
339 			KREAD(mkp, &mk, struct multi6_kludge);
340 			if (strcmp(nam, ifname(mk.mk_ifp)) == 0 &&
341 			    mk.mk_head.lh_first) {
342 				printf("\t(on kludge entry for %s)\n", nam);
343 				in6_multilist(mk.mk_head.lh_first);
344 			}
345 		}
346 
347 		free(nam);
348 	}
349 #endif
350 }
351 
352 struct in6_multi *
353 in6_multientry(mc)
354 	struct in6_multi *mc;
355 {
356 	struct in6_multi multi;
357 
358 	KREAD(mc, &multi, struct in6_multi);
359 	printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr));
360 	printf(" refcnt %u\n", multi.in6m_refcount);
361 	return(multi.in6m_entry.le_next);
362 }
363 
364 void
365 in6_multilist(mc)
366 	struct in6_multi *mc;
367 {
368 	while (mc)
369 		mc = in6_multientry(mc);
370 }
371 
372 #if !defined(__NetBSD__) && !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined(__OpenBSD__)
373 static char *
374 ether_ntoa(e)
375 	struct ether_addr *e;
376 {
377 	static char buf[20];
378 	u_char *p;
379 
380 	p = (u_char *)e;
381 
382 	snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
383 		p[0], p[1], p[2], p[3], p[4], p[5]);
384 	return buf;
385 }
386 #endif
387