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