xref: /freebsd/usr.sbin/ifmcstat/ifmcstat.c (revision a038519e)
1 /*	$KAME: ifmcstat.c,v 1.48 2006/11/15 05:13:59 itojun Exp $	*/
2 
3 /*
4  * Copyright (c) 2007-2009 Bruce Simpson.
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/sysctl.h>
39 #include <sys/socket.h>
40 #include <sys/queue.h>
41 #include <sys/tree.h>
42 
43 #include <net/if.h>
44 #include <net/if_var.h>
45 #include <net/if_types.h>
46 #include <net/if_dl.h>
47 #include <net/route.h>
48 
49 #include <netinet/in.h>
50 #include <netinet/in_var.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53 #include <netinet/igmp.h>
54 #define KERNEL
55 # include <netinet/if_ether.h>
56 #undef KERNEL
57 #define _KERNEL
58 #define SYSCTL_DECL(x)
59 # include <netinet/igmp_var.h>
60 #undef SYSCTL_DECL
61 #undef _KERNEL
62 
63 #ifdef INET6
64 #include <netinet/icmp6.h>
65 #define _KERNEL
66 # include <netinet6/mld6_var.h>
67 #undef _KERNEL
68 #endif /* INET6 */
69 
70 #include <arpa/inet.h>
71 #include <netdb.h>
72 
73 #include <stddef.h>
74 #include <stdarg.h>
75 #include <stdint.h>
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <string.h>
79 
80 #include <ctype.h>
81 #include <err.h>
82 #include <errno.h>
83 #include <fcntl.h>
84 #include <kvm.h>
85 #include <limits.h>
86 #include <ifaddrs.h>
87 #include <nlist.h>
88 #include <sysexits.h>
89 #include <unistd.h>
90 
91 /* XXX: This file currently assumes INET and KVM support in the base system. */
92 #ifndef INET
93 #define INET
94 #endif
95 
96 extern void	printb(const char *, unsigned int, const char *);
97 
98 union sockunion {
99 	struct sockaddr_storage	ss;
100 	struct sockaddr		sa;
101 	struct sockaddr_dl	sdl;
102 #ifdef INET
103 	struct sockaddr_in	sin;
104 #endif
105 #ifdef INET6
106 	struct sockaddr_in6	sin6;
107 #endif
108 };
109 typedef union sockunion sockunion_t;
110 
111 uint32_t	ifindex = 0;
112 int		af = AF_UNSPEC;
113 #ifdef WITH_KVM
114 int		Kflag = 0;
115 #endif
116 int		vflag = 0;
117 
118 #define	sa_equal(a1, a2)	\
119 	(bcmp((a1), (a2), ((a1))->sa_len) == 0)
120 
121 #define	sa_dl_equal(a1, a2)	\
122 	((((struct sockaddr_dl *)(a1))->sdl_len ==			\
123 	 ((struct sockaddr_dl *)(a2))->sdl_len) &&			\
124 	 (bcmp(LLADDR((struct sockaddr_dl *)(a1)),			\
125 	       LLADDR((struct sockaddr_dl *)(a2)),			\
126 	       ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
127 
128 /*
129  * Most of the code in this utility is to support the use of KVM for
130  * post-mortem debugging of the multicast code.
131  */
132 #ifdef WITH_KVM
133 
134 #ifdef INET
135 static void		if_addrlist(struct ifaddr *);
136 static struct in_multi *
137 			in_multientry(struct in_multi *);
138 #endif /* INET */
139 
140 #ifdef INET6
141 static void		if6_addrlist(struct ifaddr *);
142 static struct in6_multi *
143 			in6_multientry(struct in6_multi *);
144 #endif /* INET6 */
145 
146 static void		kread(u_long, void *, int);
147 static void		ll_addrlist(struct ifaddr *);
148 
149 static int		ifmcstat_kvm(const char *kernel, const char *core);
150 
151 #define	KREAD(addr, buf, type) \
152 	kread((u_long)addr, (void *)buf, sizeof(type))
153 
154 kvm_t	*kvmd;
155 struct	nlist nl[] = {
156 	{ "_ifnet", 0, 0, 0, 0, },
157 	{ "", 0, 0, 0, 0, },
158 };
159 #define	N_IFNET	0
160 
161 #endif /* WITH_KVM */
162 
163 static int		ifmcstat_getifmaddrs(void);
164 #ifdef INET
165 static void		in_ifinfo(struct igmp_ifinfo *);
166 static const char *	inm_mode(u_int mode);
167 #endif
168 #ifdef INET6
169 static void		in6_ifinfo(struct mld_ifinfo *);
170 static const char *	inet6_n2a(struct in6_addr *, uint32_t);
171 #endif
172 int			main(int, char **);
173 
174 static void
175 usage()
176 {
177 
178 	fprintf(stderr,
179 	    "usage: ifmcstat [-i interface] [-f address family]"
180 	    " [-v]"
181 #ifdef WITH_KVM
182 	    " [-K] [-M core] [-N system]"
183 #endif
184 	    "\n");
185 	exit(EX_USAGE);
186 }
187 
188 static const char *options = "i:f:vM:N:"
189 #ifdef WITH_KVM
190 	"K"
191 #endif
192 	;
193 
194 int
195 main(int argc, char **argv)
196 {
197 	int c, error;
198 #ifdef WITH_KVM
199 	const char *kernel = NULL;
200 	const char *core = NULL;
201 #endif
202 
203 	while ((c = getopt(argc, argv, options)) != -1) {
204 		switch (c) {
205 		case 'i':
206 			if ((ifindex = if_nametoindex(optarg)) == 0) {
207 				fprintf(stderr, "%s: unknown interface\n",
208 				    optarg);
209 				exit(EX_NOHOST);
210 			}
211 			break;
212 
213 		case 'f':
214 #ifdef INET
215 			if (strcmp(optarg, "inet") == 0) {
216 				af = AF_INET;
217 				break;
218 			}
219 #endif
220 #ifdef INET6
221 			if (strcmp(optarg, "inet6") == 0) {
222 				af = AF_INET6;
223 				break;
224 			}
225 #endif
226 			if (strcmp(optarg, "link") == 0) {
227 				af = AF_LINK;
228 				break;
229 			}
230 			fprintf(stderr, "%s: unknown address family\n", optarg);
231 			exit(EX_USAGE);
232 			/*NOTREACHED*/
233 			break;
234 
235 #ifdef WITH_KVM
236 		case 'K':
237 			++Kflag;
238 			break;
239 #endif
240 
241 		case 'v':
242 			++vflag;
243 			break;
244 
245 #ifdef WITH_KVM
246 		case 'M':
247 			core = strdup(optarg);
248 			break;
249 
250 		case 'N':
251 			kernel = strdup(optarg);
252 			break;
253 #endif
254 
255 		default:
256 			usage();
257 			break;
258 			/*NOTREACHED*/
259 		}
260 	}
261 
262 	if (af == AF_LINK && vflag)
263 		usage();
264 
265 #ifdef WITH_KVM
266 	if (Kflag)
267 		error = ifmcstat_kvm(kernel, core);
268 	/*
269 	 * If KVM failed, and user did not explicitly specify a core file,
270 	 * or force KVM backend to be disabled, try the sysctl backend.
271 	 */
272 	if (!Kflag || (error != 0 && (core == NULL && kernel == NULL)))
273 #endif
274 	error = ifmcstat_getifmaddrs();
275 	if (error != 0)
276 		exit(EX_OSERR);
277 
278 	exit(EX_OK);
279 	/*NOTREACHED*/
280 }
281 
282 #ifdef INET
283 
284 static void
285 in_ifinfo(struct igmp_ifinfo *igi)
286 {
287 
288 	printf("\t");
289 	switch (igi->igi_version) {
290 	case IGMP_VERSION_1:
291 	case IGMP_VERSION_2:
292 	case IGMP_VERSION_3:
293 		printf("igmpv%d", igi->igi_version);
294 		break;
295 	default:
296 		printf("igmpv?(%d)", igi->igi_version);
297 		break;
298 	}
299 	printb(" flags", igi->igi_flags, "\020\1SILENT\2LOOPBACK");
300 	if (igi->igi_version == IGMP_VERSION_3) {
301 		printf(" rv %u qi %u qri %u uri %u",
302 		    igi->igi_rv, igi->igi_qi, igi->igi_qri, igi->igi_uri);
303 	}
304 	if (vflag >= 2) {
305 		printf(" v1timer %u v2timer %u v3timer %u",
306 		    igi->igi_v1_timer, igi->igi_v2_timer, igi->igi_v3_timer);
307 	}
308 	printf("\n");
309 }
310 
311 static const char *inm_modes[] = {
312 	"undefined",
313 	"include",
314 	"exclude",
315 };
316 
317 static const char *
318 inm_mode(u_int mode)
319 {
320 
321 	if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
322 		return (inm_modes[mode]);
323 	return (NULL);
324 }
325 
326 #endif /* INET */
327 
328 #ifdef WITH_KVM
329 
330 static int
331 ifmcstat_kvm(const char *kernel, const char *core)
332 {
333 	char	buf[_POSIX2_LINE_MAX], ifname[IFNAMSIZ];
334 	struct	ifnet	*ifp, *nifp, ifnet;
335 
336 	if ((kvmd = kvm_openfiles(kernel, core, NULL, O_RDONLY, buf)) ==
337 	    NULL) {
338 		perror("kvm_openfiles");
339 		return (-1);
340 	}
341 	if (kvm_nlist(kvmd, nl) < 0) {
342 		perror("kvm_nlist");
343 		return (-1);
344 	}
345 	if (nl[N_IFNET].n_value == 0) {
346 		printf("symbol %s not found\n", nl[N_IFNET].n_name);
347 		return (-1);
348 	}
349 	KREAD(nl[N_IFNET].n_value, &ifp, struct ifnet *);
350 	while (ifp) {
351 		KREAD(ifp, &ifnet, struct ifnet);
352 		nifp = ifnet.if_link.tqe_next;
353 		if (ifindex && ifindex != ifnet.if_index)
354 			goto next;
355 
356 		printf("%s:\n", if_indextoname(ifnet.if_index, ifname));
357 #ifdef INET
358 		if_addrlist(TAILQ_FIRST(&ifnet.if_addrhead));
359 #endif
360 #ifdef INET6
361 		if6_addrlist(TAILQ_FIRST(&ifnet.if_addrhead));
362 #endif
363 		if (vflag)
364 			ll_addrlist(TAILQ_FIRST(&ifnet.if_addrhead));
365 	next:
366 		ifp = nifp;
367 	}
368 
369 	return (0);
370 }
371 
372 static void
373 kread(u_long addr, void *buf, int len)
374 {
375 
376 	if (kvm_read(kvmd, addr, buf, len) != len) {
377 		perror("kvm_read");
378 		exit(EX_OSERR);
379 	}
380 }
381 
382 static void
383 ll_addrlist(struct ifaddr *ifap)
384 {
385 	char addrbuf[NI_MAXHOST];
386 	struct ifaddr ifa;
387 	struct sockaddr sa;
388 	struct sockaddr_dl sdl;
389 	struct ifaddr *ifap0;
390 
391 	if (af && af != AF_LINK)
392 		return;
393 
394 	ifap0 = ifap;
395 	while (ifap) {
396 		KREAD(ifap, &ifa, struct ifaddr);
397 		if (ifa.ifa_addr == NULL)
398 			goto nextifap;
399 		KREAD(ifa.ifa_addr, &sa, struct sockaddr);
400 		if (sa.sa_family != PF_LINK)
401 			goto nextifap;
402 		KREAD(ifa.ifa_addr, &sdl, struct sockaddr_dl);
403 		if (sdl.sdl_alen == 0)
404 			goto nextifap;
405 		addrbuf[0] = '\0';
406 		getnameinfo((struct sockaddr *)&sdl, sdl.sdl_len,
407 		    addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
408 		printf("\tlink %s\n", addrbuf);
409 	nextifap:
410 		ifap = ifa.ifa_link.tqe_next;
411 	}
412 	if (ifap0) {
413 		struct ifnet ifnet;
414 		struct ifmultiaddr ifm, *ifmp = 0;
415 
416 		KREAD(ifap0, &ifa, struct ifaddr);
417 		KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
418 		if (TAILQ_FIRST(&ifnet.if_multiaddrs))
419 			ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
420 		while (ifmp) {
421 			KREAD(ifmp, &ifm, struct ifmultiaddr);
422 			if (ifm.ifma_addr == NULL)
423 				goto nextmulti;
424 			KREAD(ifm.ifma_addr, &sa, struct sockaddr);
425 			if (sa.sa_family != AF_LINK)
426 				goto nextmulti;
427 			KREAD(ifm.ifma_addr, &sdl, struct sockaddr_dl);
428 			addrbuf[0] = '\0';
429 			getnameinfo((struct sockaddr *)&sdl,
430 			    sdl.sdl_len, addrbuf, sizeof(addrbuf),
431 			    NULL, 0, NI_NUMERICHOST);
432 			printf("\t\tgroup %s refcnt %d\n",
433 			    addrbuf, ifm.ifma_refcount);
434 		nextmulti:
435 			ifmp = TAILQ_NEXT(&ifm, ifma_link);
436 		}
437 	}
438 }
439 
440 #ifdef INET6
441 
442 static void
443 if6_addrlist(struct ifaddr *ifap)
444 {
445 	struct ifnet ifnet;
446 	struct ifaddr ifa;
447 	struct sockaddr sa;
448 	struct in6_ifaddr if6a;
449 	struct ifaddr *ifap0;
450 
451 	if (af && af != AF_INET6)
452 		return;
453 	ifap0 = ifap;
454 	while (ifap) {
455 		KREAD(ifap, &ifa, struct ifaddr);
456 		if (ifa.ifa_addr == NULL)
457 			goto nextifap;
458 		KREAD(ifa.ifa_addr, &sa, struct sockaddr);
459 		if (sa.sa_family != PF_INET6)
460 			goto nextifap;
461 		KREAD(ifap, &if6a, struct in6_ifaddr);
462 		printf("\tinet6 %s\n", inet6_n2a(&if6a.ia_addr.sin6_addr,
463 		    if6a.ia_addr.sin6_scope_id));
464 		/*
465 		 * Print per-link MLD information, if available.
466 		 */
467 		if (ifa.ifa_ifp != NULL) {
468 			struct in6_ifextra ie;
469 			struct mld_ifinfo mli;
470 
471 			KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
472 			KREAD(ifnet.if_afdata[AF_INET6], &ie,
473 			    struct in6_ifextra);
474 			if (ie.mld_ifinfo != NULL) {
475 				KREAD(ie.mld_ifinfo, &mli, struct mld_ifinfo);
476 				in6_ifinfo(&mli);
477 			}
478 		}
479 	nextifap:
480 		ifap = ifa.ifa_link.tqe_next;
481 	}
482 	if (ifap0) {
483 		struct ifnet ifnet;
484 		struct ifmultiaddr ifm, *ifmp = 0;
485 		struct sockaddr_dl sdl;
486 
487 		KREAD(ifap0, &ifa, struct ifaddr);
488 		KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
489 		if (TAILQ_FIRST(&ifnet.if_multiaddrs))
490 			ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
491 		while (ifmp) {
492 			KREAD(ifmp, &ifm, struct ifmultiaddr);
493 			if (ifm.ifma_addr == NULL)
494 				goto nextmulti;
495 			KREAD(ifm.ifma_addr, &sa, struct sockaddr);
496 			if (sa.sa_family != AF_INET6)
497 				goto nextmulti;
498 			(void)in6_multientry((struct in6_multi *)
499 					     ifm.ifma_protospec);
500 			if (ifm.ifma_lladdr == 0)
501 				goto nextmulti;
502 			KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
503 			printf("\t\t\tmcast-macaddr %s refcnt %d\n",
504 			       ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
505 			       ifm.ifma_refcount);
506 		    nextmulti:
507 			ifmp = TAILQ_NEXT(&ifm, ifma_link);
508 		}
509 	}
510 }
511 
512 static struct in6_multi *
513 in6_multientry(struct in6_multi *mc)
514 {
515 	struct in6_multi multi;
516 
517 	KREAD(mc, &multi, struct in6_multi);
518 	printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr, 0));
519 	printf(" refcnt %u\n", multi.in6m_refcount);
520 
521 	return (multi.in6m_entry.le_next);
522 }
523 
524 #endif /* INET6 */
525 
526 #ifdef INET
527 
528 static void
529 if_addrlist(struct ifaddr *ifap)
530 {
531 	struct ifaddr ifa;
532 	struct ifnet ifnet;
533 	struct sockaddr sa;
534 	struct in_ifaddr ia;
535 	struct ifaddr *ifap0;
536 
537 	if (af && af != AF_INET)
538 		return;
539 	ifap0 = ifap;
540 	while (ifap) {
541 		KREAD(ifap, &ifa, struct ifaddr);
542 		if (ifa.ifa_addr == NULL)
543 			goto nextifap;
544 		KREAD(ifa.ifa_addr, &sa, struct sockaddr);
545 		if (sa.sa_family != PF_INET)
546 			goto nextifap;
547 		KREAD(ifap, &ia, struct in_ifaddr);
548 		printf("\tinet %s\n", inet_ntoa(ia.ia_addr.sin_addr));
549 		/*
550 		 * Print per-link IGMP information, if available.
551 		 */
552 		if (ifa.ifa_ifp != NULL) {
553 			struct in_ifinfo ii;
554 			struct igmp_ifinfo igi;
555 
556 			KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
557 			KREAD(ifnet.if_afdata[AF_INET], &ii, struct in_ifinfo);
558 			if (ii.ii_igmp != NULL) {
559 				KREAD(ii.ii_igmp, &igi, struct igmp_ifinfo);
560 				in_ifinfo(&igi);
561 			}
562 		}
563 	nextifap:
564 		ifap = ifa.ifa_link.tqe_next;
565 	}
566 	if (ifap0) {
567 		struct ifmultiaddr ifm, *ifmp = 0;
568 		struct sockaddr_dl sdl;
569 
570 		KREAD(ifap0, &ifa, struct ifaddr);
571 		KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
572 		if (TAILQ_FIRST(&ifnet.if_multiaddrs))
573 			ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
574 		while (ifmp) {
575 			KREAD(ifmp, &ifm, struct ifmultiaddr);
576 			if (ifm.ifma_addr == NULL)
577 				goto nextmulti;
578 			KREAD(ifm.ifma_addr, &sa, struct sockaddr);
579 			if (sa.sa_family != AF_INET)
580 				goto nextmulti;
581 			(void)in_multientry((struct in_multi *)
582 					    ifm.ifma_protospec);
583 			if (ifm.ifma_lladdr == 0)
584 				goto nextmulti;
585 			KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
586 			printf("\t\t\tmcast-macaddr %s refcnt %d\n",
587 			       ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
588 			       ifm.ifma_refcount);
589 		    nextmulti:
590 			ifmp = TAILQ_NEXT(&ifm, ifma_link);
591 		}
592 	}
593 }
594 
595 static const char *inm_states[] = {
596 	"not-member",
597 	"silent",
598 	"idle",
599 	"lazy",
600 	"sleeping",
601 	"awakening",
602 	"query-pending",
603 	"sg-query-pending",
604 	"leaving"
605 };
606 
607 static const char *
608 inm_state(u_int state)
609 {
610 
611 	if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER)
612 		return (inm_states[state]);
613 	return (NULL);
614 }
615 
616 #if 0
617 static struct ip_msource *
618 ims_min_kvm(struct in_multi *pinm)
619 {
620 	struct ip_msource ims0;
621 	struct ip_msource *tmp, *parent;
622 
623 	parent = NULL;
624 	tmp = RB_ROOT(&pinm->inm_srcs);
625 	while (tmp) {
626 		parent = tmp;
627 		KREAD(tmp, &ims0, struct ip_msource);
628 		tmp = RB_LEFT(&ims0, ims_link);
629 	}
630 	return (parent); /* kva */
631 }
632 
633 /* XXX This routine is buggy. See RB_NEXT in sys/tree.h. */
634 static struct ip_msource *
635 ims_next_kvm(struct ip_msource *ims)
636 {
637 	struct ip_msource ims0, ims1;
638 	struct ip_msource *tmp;
639 
640 	KREAD(ims, &ims0, struct ip_msource);
641 	if (RB_RIGHT(&ims0, ims_link)) {
642 		ims = RB_RIGHT(&ims0, ims_link);
643 		KREAD(ims, &ims1, struct ip_msource);
644 		while ((tmp = RB_LEFT(&ims1, ims_link))) {
645 			KREAD(tmp, &ims0, struct ip_msource);
646 			ims = RB_LEFT(&ims0, ims_link);
647 		}
648 	} else {
649 		tmp = RB_PARENT(&ims0, ims_link);
650 		if (tmp) {
651 			KREAD(tmp, &ims1, struct ip_msource);
652 			if (ims == RB_LEFT(&ims1, ims_link))
653 				ims = tmp;
654 		} else {
655 			while ((tmp = RB_PARENT(&ims0, ims_link))) {
656 				KREAD(tmp, &ims1, struct ip_msource);
657 				if (ims == RB_RIGHT(&ims1, ims_link)) {
658 					ims = tmp;
659 					KREAD(ims, &ims0, struct ip_msource);
660 				} else
661 					break;
662 			}
663 			ims = RB_PARENT(&ims0, ims_link);
664 		}
665 	}
666 	return (ims); /* kva */
667 }
668 
669 static void
670 inm_print_sources_kvm(struct in_multi *pinm)
671 {
672 	struct ip_msource ims0;
673 	struct ip_msource *ims;
674 	struct in_addr src;
675 	int cnt;
676 	uint8_t fmode;
677 
678 	cnt = 0;
679 	fmode = pinm->inm_st[1].iss_fmode;
680 	if (fmode == MCAST_UNDEFINED)
681 		return;
682 	for (ims = ims_min_kvm(pinm); ims != NULL; ims = ims_next_kvm(ims)) {
683 		if (cnt == 0)
684 			printf(" srcs ");
685 		KREAD(ims, &ims0, struct ip_msource);
686 		/* Only print sources in-mode at t1. */
687 		if (fmode != ims_get_mode(pinm, ims, 1))
688 			continue;
689 		src.s_addr = htonl(ims0.ims_haddr);
690 		printf("%s%s", (cnt++ == 0 ? "" : ","), inet_ntoa(src));
691 	}
692 }
693 #endif
694 
695 static struct in_multi *
696 in_multientry(struct in_multi *pinm)
697 {
698 	struct in_multi inm;
699 	const char *state, *mode;
700 
701 	KREAD(pinm, &inm, struct in_multi);
702 	printf("\t\tgroup %s", inet_ntoa(inm.inm_addr));
703 	printf(" refcnt %u", inm.inm_refcount);
704 
705 	state = inm_state(inm.inm_state);
706 	if (state)
707 		printf(" state %s", state);
708 	else
709 		printf(" state (%d)", inm.inm_state);
710 
711 	mode = inm_mode(inm.inm_st[1].iss_fmode);
712 	if (mode)
713 		printf(" mode %s", mode);
714 	else
715 		printf(" mode (%d)", inm.inm_st[1].iss_fmode);
716 
717 	if (vflag >= 2) {
718 		printf(" asm %u ex %u in %u rec %u",
719 		    (u_int)inm.inm_st[1].iss_asm,
720 		    (u_int)inm.inm_st[1].iss_ex,
721 		    (u_int)inm.inm_st[1].iss_in,
722 		    (u_int)inm.inm_st[1].iss_rec);
723 	}
724 
725 #if 0
726 	/* Buggy. */
727 	if (vflag)
728 		inm_print_sources_kvm(&inm);
729 #endif
730 
731 	printf("\n");
732 	return (NULL);
733 }
734 
735 #endif /* INET */
736 
737 #endif /* WITH_KVM */
738 
739 #ifdef INET6
740 
741 static void
742 in6_ifinfo(struct mld_ifinfo *mli)
743 {
744 
745 	printf("\t");
746 	switch (mli->mli_version) {
747 	case MLD_VERSION_1:
748 	case MLD_VERSION_2:
749 		printf("mldv%d", mli->mli_version);
750 		break;
751 	default:
752 		printf("mldv?(%d)", mli->mli_version);
753 		break;
754 	}
755 	printb(" flags", mli->mli_flags, "\020\1SILENT\2USEALLOW");
756 	if (mli->mli_version == MLD_VERSION_2) {
757 		printf(" rv %u qi %u qri %u uri %u",
758 		    mli->mli_rv, mli->mli_qi, mli->mli_qri, mli->mli_uri);
759 	}
760 	if (vflag >= 2) {
761 		printf(" v1timer %u v2timer %u", mli->mli_v1_timer,
762 		   mli->mli_v2_timer);
763 	}
764 	printf("\n");
765 }
766 
767 static const char *
768 inet6_n2a(struct in6_addr *p, uint32_t scope_id)
769 {
770 	static char buf[NI_MAXHOST];
771 	struct sockaddr_in6 sin6;
772 	const int niflags = NI_NUMERICHOST;
773 
774 	memset(&sin6, 0, sizeof(sin6));
775 	sin6.sin6_family = AF_INET6;
776 	sin6.sin6_len = sizeof(struct sockaddr_in6);
777 	sin6.sin6_addr = *p;
778 	sin6.sin6_scope_id = scope_id;
779 	if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
780 	    buf, sizeof(buf), NULL, 0, niflags) == 0) {
781 		return (buf);
782 	} else {
783 		return ("(invalid)");
784 	}
785 }
786 #endif /* INET6 */
787 
788 #ifdef INET
789 /*
790  * Retrieve per-group source filter mode and lists via sysctl.
791  */
792 static void
793 inm_print_sources_sysctl(uint32_t ifindex, struct in_addr gina)
794 {
795 #define	MAX_SYSCTL_TRY	5
796 	int mib[7];
797 	int ntry = 0;
798 	size_t mibsize;
799 	size_t len;
800 	size_t needed;
801 	size_t cnt;
802 	int i;
803 	char *buf;
804 	struct in_addr *pina;
805 	uint32_t *p;
806 	uint32_t fmode;
807 	const char *modestr;
808 
809 	mibsize = sizeof(mib) / sizeof(mib[0]);
810 	if (sysctlnametomib("net.inet.ip.mcast.filters", mib, &mibsize) == -1) {
811 		perror("sysctlnametomib");
812 		return;
813 	}
814 
815 	needed = 0;
816 	mib[5] = ifindex;
817 	mib[6] = gina.s_addr;	/* 32 bits wide */
818 	mibsize = sizeof(mib) / sizeof(mib[0]);
819 	do {
820 		if (sysctl(mib, mibsize, NULL, &needed, NULL, 0) == -1) {
821 			perror("sysctl net.inet.ip.mcast.filters");
822 			return;
823 		}
824 		if ((buf = malloc(needed)) == NULL) {
825 			perror("malloc");
826 			return;
827 		}
828 		if (sysctl(mib, mibsize, buf, &needed, NULL, 0) == -1) {
829 			if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
830 				perror("sysctl");
831 				goto out_free;
832 			}
833 			free(buf);
834 			buf = NULL;
835 		}
836 	} while (buf == NULL);
837 
838 	len = needed;
839 	if (len < sizeof(uint32_t)) {
840 		perror("sysctl");
841 		goto out_free;
842 	}
843 
844 	p = (uint32_t *)buf;
845 	fmode = *p++;
846 	len -= sizeof(uint32_t);
847 
848 	modestr = inm_mode(fmode);
849 	if (modestr)
850 		printf(" mode %s", modestr);
851 	else
852 		printf(" mode (%u)", fmode);
853 
854 	if (vflag == 0)
855 		goto out_free;
856 
857 	cnt = len / sizeof(struct in_addr);
858 	pina = (struct in_addr *)p;
859 
860 	for (i = 0; i < cnt; i++) {
861 		if (i == 0)
862 			printf(" srcs ");
863 		fprintf(stdout, "%s%s", (i == 0 ? "" : ","),
864 		    inet_ntoa(*pina++));
865 		len -= sizeof(struct in_addr);
866 	}
867 	if (len > 0) {
868 		fprintf(stderr, "warning: %u trailing bytes from %s\n",
869 		    (unsigned int)len, "net.inet.ip.mcast.filters");
870 	}
871 
872 out_free:
873 	free(buf);
874 #undef	MAX_SYSCTL_TRY
875 }
876 
877 #endif /* INET */
878 
879 #ifdef INET6
880 /*
881  * Retrieve MLD per-group source filter mode and lists via sysctl.
882  *
883  * Note: The 128-bit IPv6 group address needs to be segmented into
884  * 32-bit pieces for marshaling to sysctl. So the MIB name ends
885  * up looking like this:
886  *  a.b.c.d.e.ifindex.g[0].g[1].g[2].g[3]
887  * Assumes that pgroup originated from the kernel, so its components
888  * are already in network-byte order.
889  */
890 static void
891 in6m_print_sources_sysctl(uint32_t ifindex, struct in6_addr *pgroup)
892 {
893 #define	MAX_SYSCTL_TRY	5
894 	char addrbuf[INET6_ADDRSTRLEN];
895 	int mib[10];
896 	int ntry = 0;
897 	int *pi;
898 	size_t mibsize;
899 	size_t len;
900 	size_t needed;
901 	size_t cnt;
902 	int i;
903 	char *buf;
904 	struct in6_addr *pina;
905 	uint32_t *p;
906 	uint32_t fmode;
907 	const char *modestr;
908 
909 	mibsize = sizeof(mib) / sizeof(mib[0]);
910 	if (sysctlnametomib("net.inet6.ip6.mcast.filters", mib,
911 	    &mibsize) == -1) {
912 		perror("sysctlnametomib");
913 		return;
914 	}
915 
916 	needed = 0;
917 	mib[5] = ifindex;
918 	pi = (int *)pgroup;
919 	for (i = 0; i < 4; i++)
920 		mib[6 + i] = *pi++;
921 
922 	mibsize = sizeof(mib) / sizeof(mib[0]);
923 	do {
924 		if (sysctl(mib, mibsize, NULL, &needed, NULL, 0) == -1) {
925 			perror("sysctl net.inet6.ip6.mcast.filters");
926 			return;
927 		}
928 		if ((buf = malloc(needed)) == NULL) {
929 			perror("malloc");
930 			return;
931 		}
932 		if (sysctl(mib, mibsize, buf, &needed, NULL, 0) == -1) {
933 			if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
934 				perror("sysctl");
935 				goto out_free;
936 			}
937 			free(buf);
938 			buf = NULL;
939 		}
940 	} while (buf == NULL);
941 
942 	len = needed;
943 	if (len < sizeof(uint32_t)) {
944 		perror("sysctl");
945 		goto out_free;
946 	}
947 
948 	p = (uint32_t *)buf;
949 	fmode = *p++;
950 	len -= sizeof(uint32_t);
951 
952 	modestr = inm_mode(fmode);
953 	if (modestr)
954 		printf(" mode %s", modestr);
955 	else
956 		printf(" mode (%u)", fmode);
957 
958 	if (vflag == 0)
959 		goto out_free;
960 
961 	cnt = len / sizeof(struct in6_addr);
962 	pina = (struct in6_addr *)p;
963 
964 	for (i = 0; i < cnt; i++) {
965 		if (i == 0)
966 			printf(" srcs ");
967 		inet_ntop(AF_INET6, (const char *)pina++, addrbuf,
968 		    INET6_ADDRSTRLEN);
969 		fprintf(stdout, "%s%s", (i == 0 ? "" : ","), addrbuf);
970 		len -= sizeof(struct in6_addr);
971 	}
972 	if (len > 0) {
973 		fprintf(stderr, "warning: %u trailing bytes from %s\n",
974 		    (unsigned int)len, "net.inet6.ip6.mcast.filters");
975 	}
976 
977 out_free:
978 	free(buf);
979 #undef	MAX_SYSCTL_TRY
980 }
981 #endif /* INET6 */
982 
983 static int
984 ifmcstat_getifmaddrs(void)
985 {
986 	char			 thisifname[IFNAMSIZ];
987 	char			 addrbuf[NI_MAXHOST];
988 	struct ifaddrs		*ifap, *ifa;
989 	struct ifmaddrs		*ifmap, *ifma;
990 	sockunion_t		 lastifasa;
991 	sockunion_t		*psa, *pgsa, *pllsa, *pifasa;
992 	char			*pcolon;
993 	char			*pafname;
994 	uint32_t		 lastifindex, thisifindex;
995 	int			 error;
996 
997 	error = 0;
998 	ifap = NULL;
999 	ifmap = NULL;
1000 	lastifindex = 0;
1001 	thisifindex = 0;
1002 	lastifasa.ss.ss_family = AF_UNSPEC;
1003 
1004 	if (getifaddrs(&ifap) != 0) {
1005 		warn("getifmaddrs");
1006 		return (-1);
1007 	}
1008 
1009 	if (getifmaddrs(&ifmap) != 0) {
1010 		warn("getifmaddrs");
1011 		error = -1;
1012 		goto out;
1013 	}
1014 
1015 	for (ifma = ifmap; ifma; ifma = ifma->ifma_next) {
1016 		error = 0;
1017 		if (ifma->ifma_name == NULL || ifma->ifma_addr == NULL)
1018 			continue;
1019 
1020 		psa = (sockunion_t *)ifma->ifma_name;
1021 		if (psa->sa.sa_family != AF_LINK) {
1022 			fprintf(stderr,
1023 			    "WARNING: Kernel returned invalid data.\n");
1024 			error = -1;
1025 			break;
1026 		}
1027 
1028 		/* Filter on interface name. */
1029 		thisifindex = psa->sdl.sdl_index;
1030 		if (ifindex != 0 && thisifindex != ifindex)
1031 			continue;
1032 
1033 		/* Filter on address family. */
1034 		pgsa = (sockunion_t *)ifma->ifma_addr;
1035 		if (af != 0 && pgsa->sa.sa_family != af)
1036 			continue;
1037 
1038 		strlcpy(thisifname, link_ntoa(&psa->sdl), IFNAMSIZ);
1039 		pcolon = strchr(thisifname, ':');
1040 		if (pcolon)
1041 			*pcolon = '\0';
1042 
1043 		/* Only print the banner for the first ifmaddrs entry. */
1044 		if (lastifindex == 0 || lastifindex != thisifindex) {
1045 			lastifindex = thisifindex;
1046 			fprintf(stdout, "%s:\n", thisifname);
1047 		}
1048 
1049 		/*
1050 		 * Currently, multicast joins only take place on the
1051 		 * primary IPv4 address, and only on the link-local IPv6
1052 		 * address, as per IGMPv2/3 and MLDv1/2 semantics.
1053 		 * Therefore, we only look up the primary address on
1054 		 * the first pass.
1055 		 */
1056 		pifasa = NULL;
1057 		for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1058 			if ((strcmp(ifa->ifa_name, thisifname) != 0) ||
1059 			    (ifa->ifa_addr == NULL) ||
1060 			    (ifa->ifa_addr->sa_family != pgsa->sa.sa_family))
1061 				continue;
1062 			/*
1063 			 * For AF_INET6 only the link-local address should
1064 			 * be returned. If built without IPv6 support,
1065 			 * skip this address entirely.
1066 			 */
1067 			pifasa = (sockunion_t *)ifa->ifa_addr;
1068 			if (pifasa->sa.sa_family == AF_INET6
1069 #ifdef INET6
1070 			    && !IN6_IS_ADDR_LINKLOCAL(&pifasa->sin6.sin6_addr)
1071 #endif
1072 			) {
1073 				pifasa = NULL;
1074 				continue;
1075 			}
1076 			break;
1077 		}
1078 		if (pifasa == NULL)
1079 			continue;	/* primary address not found */
1080 
1081 		if (!vflag && pifasa->sa.sa_family == AF_LINK)
1082 			continue;
1083 
1084 		/* Parse and print primary address, if not already printed. */
1085 		if (lastifasa.ss.ss_family == AF_UNSPEC ||
1086 		    ((lastifasa.ss.ss_family == AF_LINK &&
1087 		      !sa_dl_equal(&lastifasa.sa, &pifasa->sa)) ||
1088 		     !sa_equal(&lastifasa.sa, &pifasa->sa))) {
1089 
1090 			switch (pifasa->sa.sa_family) {
1091 			case AF_INET:
1092 				pafname = "inet";
1093 				break;
1094 			case AF_INET6:
1095 				pafname = "inet6";
1096 				break;
1097 			case AF_LINK:
1098 				pafname = "link";
1099 				break;
1100 			default:
1101 				pafname = "unknown";
1102 				break;
1103 			}
1104 
1105 			switch (pifasa->sa.sa_family) {
1106 			case AF_INET6:
1107 #ifdef INET6
1108 			{
1109 				const char *p =
1110 				    inet6_n2a(&pifasa->sin6.sin6_addr,
1111 					pifasa->sin6.sin6_scope_id);
1112 				strlcpy(addrbuf, p, sizeof(addrbuf));
1113 				break;
1114 			}
1115 #else
1116 			/* FALLTHROUGH */
1117 #endif
1118 			case AF_INET:
1119 			case AF_LINK:
1120 				error = getnameinfo(&pifasa->sa,
1121 				    pifasa->sa.sa_len,
1122 				    addrbuf, sizeof(addrbuf), NULL, 0,
1123 				    NI_NUMERICHOST);
1124 				if (error)
1125 					perror("getnameinfo");
1126 				break;
1127 			default:
1128 				addrbuf[0] = '\0';
1129 				break;
1130 			}
1131 
1132 			fprintf(stdout, "\t%s %s\n", pafname, addrbuf);
1133 #ifdef INET
1134 			/*
1135 			 * Print per-link IGMP information, if available.
1136 			 */
1137 			if (pifasa->sa.sa_family == AF_INET) {
1138 				struct igmp_ifinfo igi;
1139 				size_t mibsize, len;
1140 				int mib[5];
1141 
1142 				mibsize = sizeof(mib) / sizeof(mib[0]);
1143 				if (sysctlnametomib("net.inet.igmp.ifinfo",
1144 				    mib, &mibsize) == -1) {
1145 					perror("sysctlnametomib");
1146 					goto next_ifnet;
1147 				}
1148 				mib[mibsize] = thisifindex;
1149 				len = sizeof(struct igmp_ifinfo);
1150 				if (sysctl(mib, mibsize + 1, &igi, &len, NULL,
1151 				    0) == -1) {
1152 					perror("sysctl net.inet.igmp.ifinfo");
1153 					goto next_ifnet;
1154 				}
1155 				in_ifinfo(&igi);
1156 			}
1157 #endif /* INET */
1158 #ifdef INET6
1159 			/*
1160 			 * Print per-link MLD information, if available.
1161 			 */
1162 			if (pifasa->sa.sa_family == AF_INET6) {
1163 				struct mld_ifinfo mli;
1164 				size_t mibsize, len;
1165 				int mib[5];
1166 
1167 				mibsize = sizeof(mib) / sizeof(mib[0]);
1168 				if (sysctlnametomib("net.inet6.mld.ifinfo",
1169 				    mib, &mibsize) == -1) {
1170 					perror("sysctlnametomib");
1171 					goto next_ifnet;
1172 				}
1173 				mib[mibsize] = thisifindex;
1174 				len = sizeof(struct mld_ifinfo);
1175 				if (sysctl(mib, mibsize + 1, &mli, &len, NULL,
1176 				    0) == -1) {
1177 					perror("sysctl net.inet6.mld.ifinfo");
1178 					goto next_ifnet;
1179 				}
1180 				in6_ifinfo(&mli);
1181 			}
1182 #endif /* INET6 */
1183 #if defined(INET) || defined(INET6)
1184 next_ifnet:
1185 #endif
1186 			lastifasa = *pifasa;
1187 		}
1188 
1189 		/* Print this group address. */
1190 #ifdef INET6
1191 		if (pgsa->sa.sa_family == AF_INET6) {
1192 			const char *p = inet6_n2a(&pgsa->sin6.sin6_addr,
1193 			    pgsa->sin6.sin6_scope_id);
1194 			strlcpy(addrbuf, p, sizeof(addrbuf));
1195 		} else
1196 #endif
1197 		{
1198 			error = getnameinfo(&pgsa->sa, pgsa->sa.sa_len,
1199 			    addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
1200 			if (error)
1201 				perror("getnameinfo");
1202 		}
1203 
1204 		fprintf(stdout, "\t\tgroup %s", addrbuf);
1205 #ifdef INET
1206 		if (pgsa->sa.sa_family == AF_INET) {
1207 			inm_print_sources_sysctl(thisifindex,
1208 			    pgsa->sin.sin_addr);
1209 		}
1210 #endif
1211 #ifdef INET6
1212 		if (pgsa->sa.sa_family == AF_INET6) {
1213 			in6m_print_sources_sysctl(thisifindex,
1214 			    &pgsa->sin6.sin6_addr);
1215 		}
1216 #endif
1217 		fprintf(stdout, "\n");
1218 
1219 		/* Link-layer mapping, if present. */
1220 		pllsa = (sockunion_t *)ifma->ifma_lladdr;
1221 		if (pllsa != NULL) {
1222 			error = getnameinfo(&pllsa->sa, pllsa->sa.sa_len,
1223 			    addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
1224 			fprintf(stdout, "\t\t\tmcast-macaddr %s\n", addrbuf);
1225 		}
1226 	}
1227 out:
1228 	if (ifmap != NULL)
1229 		freeifmaddrs(ifmap);
1230 	if (ifap != NULL)
1231 		freeifaddrs(ifap);
1232 
1233 	return (error);
1234 }
1235