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