ifmcstat.c 28 KB

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