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