]> CyberLeo.Net >> Repos - FreeBSD/releng/9.1.git/blob - usr.sbin/ifmcstat/ifmcstat.c
MFC: r241679
[FreeBSD/releng/9.1.git] / usr.sbin / ifmcstat / ifmcstat.c
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 *);
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 in6_ifinfo(struct mld_ifinfo *mli)
444 {
445
446         printf("\t");
447         switch (mli->mli_version) {
448         case MLD_VERSION_1:
449         case MLD_VERSION_2:
450                 printf("mldv%d", mli->mli_version);
451                 break;
452         default:
453                 printf("mldv?(%d)", mli->mli_version);
454                 break;
455         }
456         printb(" flags", mli->mli_flags, "\020\1SILENT");
457         if (mli->mli_version == MLD_VERSION_2) {
458                 printf(" rv %u qi %u qri %u uri %u",
459                     mli->mli_rv, mli->mli_qi, mli->mli_qri, mli->mli_uri);
460         }
461         if (vflag >= 2) {
462                 printf(" v1timer %u v2timer %u", mli->mli_v1_timer,
463                    mli->mli_v2_timer);
464         }
465         printf("\n");
466 }
467
468 static void
469 if6_addrlist(struct ifaddr *ifap)
470 {
471         struct ifnet ifnet;
472         struct ifaddr ifa;
473         struct sockaddr sa;
474         struct in6_ifaddr if6a;
475         struct ifaddr *ifap0;
476
477         if (af && af != AF_INET6)
478                 return;
479         ifap0 = ifap;
480         while (ifap) {
481                 KREAD(ifap, &ifa, struct ifaddr);
482                 if (ifa.ifa_addr == NULL)
483                         goto nextifap;
484                 KREAD(ifa.ifa_addr, &sa, struct sockaddr);
485                 if (sa.sa_family != PF_INET6)
486                         goto nextifap;
487                 KREAD(ifap, &if6a, struct in6_ifaddr);
488                 printf("\tinet6 %s\n", inet6_n2a(&if6a.ia_addr.sin6_addr));
489                 /*
490                  * Print per-link MLD information, if available.
491                  */
492                 if (ifa.ifa_ifp != NULL) {
493                         struct in6_ifextra ie;
494                         struct mld_ifinfo mli;
495
496                         KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
497                         KREAD(ifnet.if_afdata[AF_INET6], &ie,
498                             struct in6_ifextra);
499                         if (ie.mld_ifinfo != NULL) {
500                                 KREAD(ie.mld_ifinfo, &mli, struct mld_ifinfo);
501                                 in6_ifinfo(&mli);
502                         }
503                 }
504         nextifap:
505                 ifap = ifa.ifa_link.tqe_next;
506         }
507         if (ifap0) {
508                 struct ifnet ifnet;
509                 struct ifmultiaddr ifm, *ifmp = 0;
510                 struct sockaddr_dl sdl;
511
512                 KREAD(ifap0, &ifa, struct ifaddr);
513                 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
514                 if (TAILQ_FIRST(&ifnet.if_multiaddrs))
515                         ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
516                 while (ifmp) {
517                         KREAD(ifmp, &ifm, struct ifmultiaddr);
518                         if (ifm.ifma_addr == NULL)
519                                 goto nextmulti;
520                         KREAD(ifm.ifma_addr, &sa, struct sockaddr);
521                         if (sa.sa_family != AF_INET6)
522                                 goto nextmulti;
523                         (void)in6_multientry((struct in6_multi *)
524                                              ifm.ifma_protospec);
525                         if (ifm.ifma_lladdr == 0)
526                                 goto nextmulti;
527                         KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
528                         printf("\t\t\tmcast-macaddr %s refcnt %d\n",
529                                ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
530                                ifm.ifma_refcount);
531                     nextmulti:
532                         ifmp = TAILQ_NEXT(&ifm, ifma_link);
533                 }
534         }
535 }
536
537 static struct in6_multi *
538 in6_multientry(struct in6_multi *mc)
539 {
540         struct in6_multi multi;
541
542         KREAD(mc, &multi, struct in6_multi);
543         printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr));
544         printf(" refcnt %u\n", multi.in6m_refcount);
545
546         return (multi.in6m_entry.le_next);
547 }
548
549 #endif /* INET6 */
550
551 #ifdef INET
552
553 static void
554 if_addrlist(struct ifaddr *ifap)
555 {
556         struct ifaddr ifa;
557         struct ifnet ifnet;
558         struct sockaddr sa;
559         struct in_ifaddr ia;
560         struct ifaddr *ifap0;
561
562         if (af && af != AF_INET)
563                 return;
564         ifap0 = ifap;
565         while (ifap) {
566                 KREAD(ifap, &ifa, struct ifaddr);
567                 if (ifa.ifa_addr == NULL)
568                         goto nextifap;
569                 KREAD(ifa.ifa_addr, &sa, struct sockaddr);
570                 if (sa.sa_family != PF_INET)
571                         goto nextifap;
572                 KREAD(ifap, &ia, struct in_ifaddr);
573                 printf("\tinet %s\n", inet_ntoa(ia.ia_addr.sin_addr));
574                 /*
575                  * Print per-link IGMP information, if available.
576                  */
577                 if (ifa.ifa_ifp != NULL) {
578                         struct in_ifinfo ii;
579                         struct igmp_ifinfo igi;
580
581                         KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
582                         KREAD(ifnet.if_afdata[AF_INET], &ii, struct in_ifinfo);
583                         if (ii.ii_igmp != NULL) {
584                                 KREAD(ii.ii_igmp, &igi, struct igmp_ifinfo);
585                                 in_ifinfo(&igi);
586                         }
587                 }
588         nextifap:
589                 ifap = ifa.ifa_link.tqe_next;
590         }
591         if (ifap0) {
592                 struct ifmultiaddr ifm, *ifmp = 0;
593                 struct sockaddr_dl sdl;
594
595                 KREAD(ifap0, &ifa, struct ifaddr);
596                 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
597                 if (TAILQ_FIRST(&ifnet.if_multiaddrs))
598                         ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
599                 while (ifmp) {
600                         KREAD(ifmp, &ifm, struct ifmultiaddr);
601                         if (ifm.ifma_addr == NULL)
602                                 goto nextmulti;
603                         KREAD(ifm.ifma_addr, &sa, struct sockaddr);
604                         if (sa.sa_family != AF_INET)
605                                 goto nextmulti;
606                         (void)in_multientry((struct in_multi *)
607                                             ifm.ifma_protospec);
608                         if (ifm.ifma_lladdr == 0)
609                                 goto nextmulti;
610                         KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
611                         printf("\t\t\tmcast-macaddr %s refcnt %d\n",
612                                ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
613                                ifm.ifma_refcount);
614                     nextmulti:
615                         ifmp = TAILQ_NEXT(&ifm, ifma_link);
616                 }
617         }
618 }
619
620 static const char *inm_states[] = {
621         "not-member",
622         "silent",
623         "idle",
624         "lazy",
625         "sleeping",
626         "awakening",
627         "query-pending",
628         "sg-query-pending",
629         "leaving"
630 };
631
632 static const char *
633 inm_state(u_int state)
634 {
635
636         if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER)
637                 return (inm_states[state]);
638         return (NULL);
639 }
640
641 #if 0
642 static struct ip_msource *
643 ims_min_kvm(struct in_multi *pinm)
644 {
645         struct ip_msource ims0;
646         struct ip_msource *tmp, *parent;
647
648         parent = NULL;
649         tmp = RB_ROOT(&pinm->inm_srcs);
650         while (tmp) {
651                 parent = tmp;
652                 KREAD(tmp, &ims0, struct ip_msource);
653                 tmp = RB_LEFT(&ims0, ims_link);
654         }
655         return (parent); /* kva */
656 }
657
658 /* XXX This routine is buggy. See RB_NEXT in sys/tree.h. */
659 static struct ip_msource *
660 ims_next_kvm(struct ip_msource *ims)
661 {
662         struct ip_msource ims0, ims1;
663         struct ip_msource *tmp;
664
665         KREAD(ims, &ims0, struct ip_msource);
666         if (RB_RIGHT(&ims0, ims_link)) {
667                 ims = RB_RIGHT(&ims0, ims_link);
668                 KREAD(ims, &ims1, struct ip_msource);
669                 while ((tmp = RB_LEFT(&ims1, ims_link))) {
670                         KREAD(tmp, &ims0, struct ip_msource);
671                         ims = RB_LEFT(&ims0, ims_link);
672                 }
673         } else {
674                 tmp = RB_PARENT(&ims0, ims_link);
675                 if (tmp) {
676                         KREAD(tmp, &ims1, struct ip_msource);
677                         if (ims == RB_LEFT(&ims1, ims_link))
678                                 ims = tmp;
679                 } else {
680                         while ((tmp = RB_PARENT(&ims0, ims_link))) {
681                                 KREAD(tmp, &ims1, struct ip_msource);
682                                 if (ims == RB_RIGHT(&ims1, ims_link)) {
683                                         ims = tmp;
684                                         KREAD(ims, &ims0, struct ip_msource);
685                                 } else
686                                         break;
687                         }
688                         ims = RB_PARENT(&ims0, ims_link);
689                 }
690         }
691         return (ims); /* kva */
692 }
693
694 static void
695 inm_print_sources_kvm(struct in_multi *pinm)
696 {
697         struct ip_msource ims0;
698         struct ip_msource *ims;
699         struct in_addr src;
700         int cnt;
701         uint8_t fmode;
702
703         cnt = 0;
704         fmode = pinm->inm_st[1].iss_fmode;
705         if (fmode == MCAST_UNDEFINED)
706                 return;
707         for (ims = ims_min_kvm(pinm); ims != NULL; ims = ims_next_kvm(ims)) {
708                 if (cnt == 0)
709                         printf(" srcs ");
710                 KREAD(ims, &ims0, struct ip_msource);
711                 /* Only print sources in-mode at t1. */
712                 if (fmode != ims_get_mode(pinm, ims, 1))
713                         continue;
714                 src.s_addr = htonl(ims0.ims_haddr);
715                 printf("%s%s", (cnt++ == 0 ? "" : ","), inet_ntoa(src));
716         }
717 }
718 #endif
719
720 static struct in_multi *
721 in_multientry(struct in_multi *pinm)
722 {
723         struct in_multi inm;
724         const char *state, *mode;
725
726         KREAD(pinm, &inm, struct in_multi);
727         printf("\t\tgroup %s", inet_ntoa(inm.inm_addr));
728         printf(" refcnt %u", inm.inm_refcount);
729
730         state = inm_state(inm.inm_state);
731         if (state)
732                 printf(" state %s", state);
733         else
734                 printf(" state (%d)", inm.inm_state);
735
736         mode = inm_mode(inm.inm_st[1].iss_fmode);
737         if (mode)
738                 printf(" mode %s", mode);
739         else
740                 printf(" mode (%d)", inm.inm_st[1].iss_fmode);
741
742         if (vflag >= 2) {
743                 printf(" asm %u ex %u in %u rec %u",
744                     (u_int)inm.inm_st[1].iss_asm,
745                     (u_int)inm.inm_st[1].iss_ex,
746                     (u_int)inm.inm_st[1].iss_in,
747                     (u_int)inm.inm_st[1].iss_rec);
748         }
749
750 #if 0
751         /* Buggy. */
752         if (vflag)
753                 inm_print_sources_kvm(&inm);
754 #endif
755
756         printf("\n");
757         return (NULL);
758 }
759
760 #endif /* INET */
761
762 #endif /* WITH_KVM */
763
764 #ifdef INET6
765 static const char *
766 inet6_n2a(struct in6_addr *p)
767 {
768         static char buf[NI_MAXHOST];
769         struct sockaddr_in6 sin6;
770         u_int32_t scopeid;
771         const int niflags = NI_NUMERICHOST;
772
773         memset(&sin6, 0, sizeof(sin6));
774         sin6.sin6_family = AF_INET6;
775         sin6.sin6_len = sizeof(struct sockaddr_in6);
776         sin6.sin6_addr = *p;
777         if (IN6_IS_ADDR_LINKLOCAL(p) || IN6_IS_ADDR_MC_LINKLOCAL(p) ||
778             IN6_IS_ADDR_MC_NODELOCAL(p)) {
779                 scopeid = ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
780                 if (scopeid) {
781                         sin6.sin6_scope_id = scopeid;
782                         sin6.sin6_addr.s6_addr[2] = 0;
783                         sin6.sin6_addr.s6_addr[3] = 0;
784                 }
785         }
786         if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
787             buf, sizeof(buf), NULL, 0, niflags) == 0) {
788                 return (buf);
789         } else {
790                 return ("(invalid)");
791         }
792 }
793 #endif /* INET6 */
794
795 #ifdef INET
796 /*
797  * Retrieve per-group source filter mode and lists via sysctl.
798  */
799 static void
800 inm_print_sources_sysctl(uint32_t ifindex, struct in_addr gina)
801 {
802 #define MAX_SYSCTL_TRY  5
803         int mib[7];
804         int ntry = 0;
805         size_t mibsize;
806         size_t len;
807         size_t needed;
808         size_t cnt;
809         int i;
810         char *buf;
811         struct in_addr *pina;
812         uint32_t *p;
813         uint32_t fmode;
814         const char *modestr;
815
816         mibsize = sizeof(mib) / sizeof(mib[0]);
817         if (sysctlnametomib("net.inet.ip.mcast.filters", mib, &mibsize) == -1) {
818                 perror("sysctlnametomib");
819                 return;
820         }
821
822         needed = 0;
823         mib[5] = ifindex;
824         mib[6] = gina.s_addr;   /* 32 bits wide */
825         mibsize = sizeof(mib) / sizeof(mib[0]);
826         do {
827                 if (sysctl(mib, mibsize, NULL, &needed, NULL, 0) == -1) {
828                         perror("sysctl net.inet.ip.mcast.filters");
829                         return;
830                 }
831                 if ((buf = malloc(needed)) == NULL) {
832                         perror("malloc");
833                         return;
834                 }
835                 if (sysctl(mib, mibsize, buf, &needed, NULL, 0) == -1) {
836                         if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
837                                 perror("sysctl");
838                                 goto out_free;
839                         }
840                         free(buf);
841                         buf = NULL;
842                 } 
843         } while (buf == NULL);
844
845         len = needed;
846         if (len < sizeof(uint32_t)) {
847                 perror("sysctl");
848                 goto out_free;
849         }
850
851         p = (uint32_t *)buf;
852         fmode = *p++;
853         len -= sizeof(uint32_t);
854
855         modestr = inm_mode(fmode);
856         if (modestr)
857                 printf(" mode %s", modestr);
858         else
859                 printf(" mode (%u)", fmode);
860
861         if (vflag == 0)
862                 goto out_free;
863
864         cnt = len / sizeof(struct in_addr);
865         pina = (struct in_addr *)p;
866
867         for (i = 0; i < cnt; i++) {
868                 if (i == 0)
869                         printf(" srcs ");
870                 fprintf(stdout, "%s%s", (i == 0 ? "" : ","),
871                     inet_ntoa(*pina++));
872                 len -= sizeof(struct in_addr);
873         }
874         if (len > 0) {
875                 fprintf(stderr, "warning: %u trailing bytes from %s\n",
876                     (unsigned int)len, "net.inet.ip.mcast.filters");
877         }
878
879 out_free:
880         free(buf);
881 #undef  MAX_SYSCTL_TRY
882 }
883
884 #endif /* INET */
885
886 #ifdef INET6
887 /*
888  * Retrieve MLD per-group source filter mode and lists via sysctl.
889  *
890  * Note: The 128-bit IPv6 group addres needs to be segmented into
891  * 32-bit pieces for marshaling to sysctl. So the MIB name ends
892  * up looking like this:
893  *  a.b.c.d.e.ifindex.g[0].g[1].g[2].g[3]
894  * Assumes that pgroup originated from the kernel, so its components
895  * are already in network-byte order.
896  */
897 static void
898 in6m_print_sources_sysctl(uint32_t ifindex, struct in6_addr *pgroup)
899 {
900 #define MAX_SYSCTL_TRY  5
901         char addrbuf[INET6_ADDRSTRLEN];
902         int mib[10];
903         int ntry = 0;
904         int *pi;
905         size_t mibsize;
906         size_t len;
907         size_t needed;
908         size_t cnt;
909         int i;
910         char *buf;
911         struct in6_addr *pina;
912         uint32_t *p;
913         uint32_t fmode;
914         const char *modestr;
915
916         mibsize = sizeof(mib) / sizeof(mib[0]);
917         if (sysctlnametomib("net.inet6.ip6.mcast.filters", mib,
918             &mibsize) == -1) {
919                 perror("sysctlnametomib");
920                 return;
921         }
922
923         needed = 0;
924         mib[5] = ifindex;
925         pi = (int *)pgroup;
926         for (i = 0; i < 4; i++)
927                 mib[6 + i] = *pi++;
928
929         mibsize = sizeof(mib) / sizeof(mib[0]);
930         do {
931                 if (sysctl(mib, mibsize, NULL, &needed, NULL, 0) == -1) {
932                         perror("sysctl net.inet6.ip6.mcast.filters");
933                         return;
934                 }
935                 if ((buf = malloc(needed)) == NULL) {
936                         perror("malloc");
937                         return;
938                 }
939                 if (sysctl(mib, mibsize, buf, &needed, NULL, 0) == -1) {
940                         if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
941                                 perror("sysctl");
942                                 goto out_free;
943                         }
944                         free(buf);
945                         buf = NULL;
946                 } 
947         } while (buf == NULL);
948
949         len = needed;
950         if (len < sizeof(uint32_t)) {
951                 perror("sysctl");
952                 goto out_free;
953         }
954
955         p = (uint32_t *)buf;
956         fmode = *p++;
957         len -= sizeof(uint32_t);
958
959         modestr = inm_mode(fmode);
960         if (modestr)
961                 printf(" mode %s", modestr);
962         else
963                 printf(" mode (%u)", fmode);
964
965         if (vflag == 0)
966                 goto out_free;
967
968         cnt = len / sizeof(struct in6_addr);
969         pina = (struct in6_addr *)p;
970
971         for (i = 0; i < cnt; i++) {
972                 if (i == 0)
973                         printf(" srcs ");
974                 inet_ntop(AF_INET6, (const char *)pina++, addrbuf,
975                     INET6_ADDRSTRLEN);
976                 fprintf(stdout, "%s%s", (i == 0 ? "" : ","), addrbuf);
977                 len -= sizeof(struct in6_addr);
978         }
979         if (len > 0) {
980                 fprintf(stderr, "warning: %u trailing bytes from %s\n",
981                     (unsigned int)len, "net.inet6.ip6.mcast.filters");
982         }
983
984 out_free:
985         free(buf);
986 #undef  MAX_SYSCTL_TRY
987 }
988 #endif /* INET6 */
989
990 static int
991 ifmcstat_getifmaddrs(void)
992 {
993         char                     thisifname[IFNAMSIZ];
994         char                     addrbuf[NI_MAXHOST];
995         struct ifaddrs          *ifap, *ifa;
996         struct ifmaddrs         *ifmap, *ifma;
997         sockunion_t              lastifasa;
998         sockunion_t             *psa, *pgsa, *pllsa, *pifasa;
999         char                    *pcolon;
1000         char                    *pafname;
1001         uint32_t                 lastifindex, thisifindex;
1002         int                      error;
1003
1004         error = 0;
1005         ifap = NULL;
1006         ifmap = NULL;
1007         lastifindex = 0;
1008         thisifindex = 0;
1009         lastifasa.ss.ss_family = AF_UNSPEC;
1010
1011         if (getifaddrs(&ifap) != 0) {
1012                 warn("getifmaddrs");
1013                 return (-1);
1014         }
1015
1016         if (getifmaddrs(&ifmap) != 0) {
1017                 warn("getifmaddrs");
1018                 error = -1;
1019                 goto out;
1020         }
1021
1022         for (ifma = ifmap; ifma; ifma = ifma->ifma_next) {
1023                 error = 0;
1024                 if (ifma->ifma_name == NULL || ifma->ifma_addr == NULL)
1025                         continue;
1026
1027                 psa = (sockunion_t *)ifma->ifma_name;
1028                 if (psa->sa.sa_family != AF_LINK) {
1029                         fprintf(stderr,
1030                             "WARNING: Kernel returned invalid data.\n");
1031                         error = -1;
1032                         break;
1033                 }
1034
1035                 /* Filter on interface name. */
1036                 thisifindex = psa->sdl.sdl_index;
1037                 if (ifindex != 0 && thisifindex != ifindex)
1038                         continue;
1039
1040                 /* Filter on address family. */
1041                 pgsa = (sockunion_t *)ifma->ifma_addr;
1042                 if (af != 0 && pgsa->sa.sa_family != af)
1043                         continue;
1044
1045                 strlcpy(thisifname, link_ntoa(&psa->sdl), IFNAMSIZ);
1046                 pcolon = strchr(thisifname, ':');
1047                 if (pcolon)
1048                         *pcolon = '\0';
1049
1050                 /* Only print the banner for the first ifmaddrs entry. */
1051                 if (lastifindex == 0 || lastifindex != thisifindex) {
1052                         lastifindex = thisifindex;
1053                         fprintf(stdout, "%s:\n", thisifname);
1054                 }
1055
1056                 /*
1057                  * Currently, multicast joins only take place on the
1058                  * primary IPv4 address, and only on the link-local IPv6
1059                  * address, as per IGMPv2/3 and MLDv1/2 semantics.
1060                  * Therefore, we only look up the primary address on
1061                  * the first pass.
1062                  */
1063                 pifasa = NULL;
1064                 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1065                         if ((strcmp(ifa->ifa_name, thisifname) != 0) ||
1066                             (ifa->ifa_addr == NULL) ||
1067                             (ifa->ifa_addr->sa_family != pgsa->sa.sa_family))
1068                                 continue;
1069                         /*
1070                          * For AF_INET6 only the link-local address should
1071                          * be returned. If built without IPv6 support,
1072                          * skip this address entirely.
1073                          */
1074                         pifasa = (sockunion_t *)ifa->ifa_addr;
1075                         if (pifasa->sa.sa_family == AF_INET6
1076 #ifdef INET6
1077                             && !IN6_IS_ADDR_LINKLOCAL(&pifasa->sin6.sin6_addr)
1078 #endif
1079                         ) {
1080                                 pifasa = NULL;
1081                                 continue;
1082                         }
1083                         break;
1084                 }
1085                 if (pifasa == NULL)
1086                         continue;       /* primary address not found */
1087
1088                 if (!vflag && pifasa->sa.sa_family == AF_LINK)
1089                         continue;
1090
1091                 /* Parse and print primary address, if not already printed. */
1092                 if (lastifasa.ss.ss_family == AF_UNSPEC ||
1093                     ((lastifasa.ss.ss_family == AF_LINK &&
1094                       !sa_dl_equal(&lastifasa.sa, &pifasa->sa)) ||
1095                      !sa_equal(&lastifasa.sa, &pifasa->sa))) {
1096
1097                         switch (pifasa->sa.sa_family) {
1098                         case AF_INET:
1099                                 pafname = "inet";
1100                                 break;
1101                         case AF_INET6:
1102                                 pafname = "inet6";
1103                                 break;
1104                         case AF_LINK:
1105                                 pafname = "link";
1106                                 break;
1107                         default:
1108                                 pafname = "unknown";
1109                                 break;
1110                         }
1111
1112                         switch (pifasa->sa.sa_family) {
1113                         case AF_INET6:
1114 #ifdef INET6
1115                         {
1116                                 const char *p =
1117                                     inet6_n2a(&pifasa->sin6.sin6_addr);
1118                                 strlcpy(addrbuf, p, sizeof(addrbuf));
1119                                 break;
1120                         }
1121 #else
1122                         /* FALLTHROUGH */
1123 #endif
1124                         case AF_INET:
1125                         case AF_LINK:
1126                                 error = getnameinfo(&pifasa->sa,
1127                                     pifasa->sa.sa_len,
1128                                     addrbuf, sizeof(addrbuf), NULL, 0,
1129                                     NI_NUMERICHOST);
1130                                 if (error)
1131                                         perror("getnameinfo");
1132                                 break;
1133                         default:
1134                                 addrbuf[0] = '\0';
1135                                 break;
1136                         }
1137
1138                         fprintf(stdout, "\t%s %s\n", pafname, addrbuf);
1139 #ifdef INET
1140                         /*
1141                          * Print per-link IGMP information, if available.
1142                          */
1143                         if (pifasa->sa.sa_family == AF_INET) {
1144                                 struct igmp_ifinfo igi;
1145                                 size_t mibsize, len;
1146                                 int mib[5];
1147
1148                                 mibsize = sizeof(mib) / sizeof(mib[0]);
1149                                 if (sysctlnametomib("net.inet.igmp.ifinfo",
1150                                     mib, &mibsize) == -1) {
1151                                         perror("sysctlnametomib");
1152                                         goto next_ifnet;
1153                                 }
1154                                 mib[mibsize] = thisifindex;
1155                                 len = sizeof(struct igmp_ifinfo);
1156                                 if (sysctl(mib, mibsize + 1, &igi, &len, NULL,
1157                                     0) == -1) {
1158                                         perror("sysctl net.inet.igmp.ifinfo");
1159                                         goto next_ifnet;
1160                                 }
1161                                 in_ifinfo(&igi);
1162                         }
1163 #endif /* INET */
1164 #ifdef INET6
1165                         /*
1166                          * Print per-link MLD information, if available.
1167                          */
1168                         if (pifasa->sa.sa_family == AF_INET6) {
1169                                 struct mld_ifinfo mli;
1170                                 size_t mibsize, len;
1171                                 int mib[5];
1172
1173                                 mibsize = sizeof(mib) / sizeof(mib[0]);
1174                                 if (sysctlnametomib("net.inet6.mld.ifinfo",
1175                                     mib, &mibsize) == -1) {
1176                                         perror("sysctlnametomib");
1177                                         goto next_ifnet;
1178                                 }
1179                                 mib[mibsize] = thisifindex;
1180                                 len = sizeof(struct mld_ifinfo);
1181                                 if (sysctl(mib, mibsize + 1, &mli, &len, NULL,
1182                                     0) == -1) {
1183                                         perror("sysctl net.inet6.mld.ifinfo");
1184                                         goto next_ifnet;
1185                                 }
1186                                 in6_ifinfo(&mli);
1187                         }
1188 #endif /* INET6 */
1189 #if defined(INET) || defined(INET6)
1190 next_ifnet:
1191 #endif
1192                         lastifasa = *pifasa;
1193                 }
1194
1195                 /* Print this group address. */
1196 #ifdef INET6
1197                 if (pgsa->sa.sa_family == AF_INET6) {
1198                         const char *p = inet6_n2a(&pgsa->sin6.sin6_addr);
1199                         strlcpy(addrbuf, p, sizeof(addrbuf));
1200                 } else
1201 #endif
1202                 {
1203                         error = getnameinfo(&pgsa->sa, pgsa->sa.sa_len,
1204                             addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
1205                         if (error)
1206                                 perror("getnameinfo");
1207                 }
1208
1209                 fprintf(stdout, "\t\tgroup %s", addrbuf);
1210 #ifdef INET
1211                 if (pgsa->sa.sa_family == AF_INET) {
1212                         inm_print_sources_sysctl(thisifindex,
1213                             pgsa->sin.sin_addr);
1214                 }
1215 #endif
1216 #ifdef INET6
1217                 if (pgsa->sa.sa_family == AF_INET6) {
1218                         in6m_print_sources_sysctl(thisifindex,
1219                             &pgsa->sin6.sin6_addr);
1220                 }
1221 #endif
1222                 fprintf(stdout, "\n");
1223
1224                 /* Link-layer mapping, if present. */
1225                 pllsa = (sockunion_t *)ifma->ifma_lladdr;
1226                 if (pllsa != NULL) {
1227                         error = getnameinfo(&pllsa->sa, pllsa->sa.sa_len,
1228                             addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
1229                         fprintf(stdout, "\t\t\tmcast-macaddr %s\n", addrbuf);
1230                 }
1231         }
1232 out:
1233         if (ifmap != NULL)
1234                 freeifmaddrs(ifmap);
1235         if (ifap != NULL)
1236                 freeifaddrs(ifap);
1237
1238         return (error);
1239 }