2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2007-2009 Bruce Simpson.
5 * Copyright (c) 2000 Wilbert De Graaf.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
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
34 * Diagnostic and test utility for multicast sockets.
35 * XXX: This file currently assumes INET support in the base system.
36 * TODO: Support embedded KAME Scope ID in IPv6 group addresses.
37 * TODO: Use IPv4 link-local address when source address selection
38 * is implemented; use MCAST_JOIN_SOURCE for IPv4.
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/errno.h>
44 #include <sys/socket.h>
46 #include <sys/ioctl.h>
49 #include <net/if_dl.h>
50 #include <net/ethernet.h>
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip_var.h>
58 #include <netinet/in.h>
59 #include <netinet/ip6.h>
71 #include <arpa/inet.h>
76 struct sockaddr_storage ss;
78 struct sockaddr_dl sdl;
80 struct sockaddr_in sin;
83 struct sockaddr_in6 sin6;
86 typedef union sockunion sockunion_t;
91 struct ip_mreq_source mrs;
95 struct group_source_req gr;
98 typedef union mrequnion mrequnion_t;
102 #define LINE_LENGTH 80
105 static int __ifindex_to_primary_ip(const uint32_t, struct in_addr *);
107 static uint32_t parse_cmd_args(sockunion_t *, sockunion_t *,
108 const char *, const char *, const char *);
109 static void process_file(char *, int, int);
110 static void process_cmd(char*, int, int, FILE *);
111 static int su_cmp(const void *, const void *);
112 static void usage(void);
115 * Ordering predicate for qsort().
118 su_cmp(const void *a, const void *b)
120 const sockunion_t *sua = (const sockunion_t *)a;
121 const sockunion_t *sub = (const sockunion_t *)b;
123 assert(sua->sa.sa_family == sub->sa.sa_family);
125 switch (sua->sa.sa_family) {
128 return ((int)(sua->sin.sin_addr.s_addr -
129 sub->sin.sin_addr.s_addr));
134 return (memcmp(&sua->sin6.sin6_addr, &sub->sin6.sin6_addr,
135 sizeof(struct in6_addr)));
142 assert(sua->sa.sa_len == sub->sa.sa_len);
143 return (memcmp(sua, sub, sua->sa.sa_len));
148 * Internal: Map an interface index to primary IPv4 address.
149 * This is somewhat inefficient. This is a useful enough operation
150 * that it probably belongs in the C library.
151 * Return zero if found, -1 on error, 1 on not found.
154 __ifindex_to_primary_ip(const uint32_t ifindex, struct in_addr *pina)
156 char ifname[IFNAMSIZ];
158 struct ifaddrs *ifaddrs;
162 assert(ifindex != 0);
165 if (if_indextoname(ifindex, ifname) == NULL)
167 if (getifaddrs(&ifaddrs) < 0)
171 * Find the ifaddr entry corresponding to the interface name,
172 * and return the first matching IPv4 address.
175 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
176 if (strcmp(ifa->ifa_name, ifname) != 0)
178 psu = (sockunion_t *)ifa->ifa_addr;
179 if (psu && psu->sa.sa_family == AF_INET) {
181 memcpy(pina, &psu->sin.sin_addr,
182 sizeof(struct in_addr));
188 errno = EADDRNOTAVAIL; /* XXX */
190 freeifaddrs(ifaddrs);
196 main(int argc, char **argv)
198 char line[LINE_LENGTH];
205 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
206 if (s == -1 && errno != EAFNOSUPPORT)
207 err(1, "can't open IPv4 socket");
210 s6 = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
211 if (s6 == -1 && errno != EAFNOSUPPORT)
212 err(1, "can't open IPv6 socket");
214 if (s == -1 && s6 == -1)
215 errc(1, EPROTONOSUPPORT, "can't open socket");
218 if (isatty(STDIN_FILENO)) {
219 printf("multicast membership test program; "
220 "enter ? for list of commands\n");
223 if (fgets(line, sizeof(line), stdin) != NULL) {
225 process_cmd(line, s, s6, stdin);
227 /* Get the filename */
228 for (i = 1; isblank(line[i]); i++);
229 if ((p = (char*)strchr(line, '\n'))
232 process_file(&line[i], s, s6);
235 } while (!feof(stdin));
237 for (i = 1; i < argc; i++) {
238 process_file(argv[i], s, s6);
251 process_file(char *fname, int s, int s6)
257 fp = fopen(fname, "r");
263 /* Skip comments and empty lines. */
264 while (fgets(line, sizeof(line), fp) != NULL) {
266 while (isblank(*lineptr))
268 if (*lineptr != '#' && *lineptr != '\n')
269 process_cmd(lineptr, s, s6, fp);
276 * Parse join/leave/allow/block arguments, given:
277 * str1: group (as AF_INET or AF_INET6 printable)
279 * str3: optional source address (may be NULL).
280 * This argument must have the same parsed address family as str1.
281 * Return the ifindex of ifname, or 0 if any parse element failed.
284 parse_cmd_args(sockunion_t *psu, sockunion_t *psu2,
285 const char *str1, const char *str2, const char *str3)
287 struct addrinfo hints;
288 struct addrinfo *res;
293 assert(str1 != NULL);
294 assert(str2 != NULL);
298 ifindex = if_nametoindex(str2);
302 memset(&hints, 0, sizeof(struct addrinfo));
303 hints.ai_flags = AI_NUMERICHOST;
304 hints.ai_family = PF_UNSPEC;
305 hints.ai_socktype = SOCK_DGRAM;
307 memset(psu, 0, sizeof(sockunion_t));
308 psu->sa.sa_family = AF_UNSPEC;
310 error = getaddrinfo(str1, "0", &hints, &res);
312 warnx("getaddrinfo: %s", gai_strerror(error));
317 memcpy(psu, res->ai_addr, res->ai_addrlen);
320 /* sscanf() may pass the empty string. */
321 if (psu2 != NULL && str3 != NULL && *str3 != '\0') {
322 memset(psu2, 0, sizeof(sockunion_t));
323 psu2->sa.sa_family = AF_UNSPEC;
325 /* look for following address family; str3 is *optional*. */
326 hints.ai_family = af;
327 error = getaddrinfo(str3, "0", &hints, &res);
329 warnx("getaddrinfo: %s", gai_strerror(error));
332 if (af != res->ai_family) {
333 errno = EINVAL; /* XXX */
336 memcpy(psu2, res->ai_addr, res->ai_addrlen);
345 af2sock(const int af, int s, int s6)
360 af2socklen(const int af)
365 return (sizeof(struct sockaddr_in));
369 return (sizeof(struct sockaddr_in6));
375 process_cmd(char *cmd, int s, int s6, FILE *fp __unused)
386 uint32_t fmode, ifindex;
389 int af, error, f, flags, i, level, n, optname;
392 su.sa.sa_family = AF_UNSPEC;
393 su2.sa.sa_family = AF_UNSPEC;
396 while (isblank(*++line))
397 ; /* Skip whitespace. */
410 if ((sscanf(line, "%d", &n) != 1) || (n < 1)) {
422 sscanf(line, "%s %s %s", str1, str2, str3);
423 ifindex = parse_cmd_args(&su, &su2, str1, str2, str3);
428 af = su.sa.sa_family;
433 error = __ifindex_to_primary_ip(ifindex, &ina);
435 warn("primary_ip_lookup %s", str2);
441 if (su2.sa.sa_family != AF_UNSPEC) {
442 mr.mrs.imr_multiaddr = su.sin.sin_addr;
443 mr.mrs.imr_sourceaddr = su2.sin.sin_addr;
444 mr.mrs.imr_interface = ina;
445 optname = (*cmd == 'j') ?
446 IP_ADD_SOURCE_MEMBERSHIP :
447 IP_DROP_SOURCE_MEMBERSHIP;
448 toptname = (*cmd == 'j') ?
449 "IP_ADD_SOURCE_MEMBERSHIP" :
450 "IP_DROP_SOURCE_MEMBERSHIP";
451 optval = (void *)&mr.mrs;
452 optlen = sizeof(mr.mrs);
454 mr.mr.imr_multiaddr = su.sin.sin_addr;
455 mr.mr.imr_interface = ina;
456 optname = (*cmd == 'j') ?
457 IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP;
458 toptname = (*cmd == 'j') ?
459 "IP_ADD_MEMBERSHIP" : "IP_DROP_MEMBERSHIP";
460 optval = (void *)&mr.mr;
461 optlen = sizeof(mr.mr);
464 warnc(EPROTONOSUPPORT, "setsockopt %s",
466 } else if (setsockopt(s, level, optname, optval,
471 warn("setsockopt %s", toptname);
476 #endif /* INET with INET6 */
479 if (af == AF_INET6) {
480 level = IPPROTO_IPV6;
481 if (su2.sa.sa_family != AF_UNSPEC) {
482 mr.gr.gsr_interface = ifindex;
483 mr.gr.gsr_group = su.ss;
484 mr.gr.gsr_source = su2.ss;
485 optname = (*cmd == 'j') ?
486 MCAST_JOIN_SOURCE_GROUP:
487 MCAST_LEAVE_SOURCE_GROUP;
488 toptname = (*cmd == 'j') ?
489 "MCAST_JOIN_SOURCE_GROUP":
490 "MCAST_LEAVE_SOURCE_GROUP";
491 optval = (void *)&mr.gr;
492 optlen = sizeof(mr.gr);
494 mr.mr6.ipv6mr_multiaddr = su.sin6.sin6_addr;
495 mr.mr6.ipv6mr_interface = ifindex;
496 optname = (*cmd == 'j') ?
499 toptname = (*cmd == 'j') ?
502 optval = (void *)&mr.mr6;
503 optlen = sizeof(mr.mr6);
506 warnc(EPROTONOSUPPORT, "setsockopt %s",
508 } else if (setsockopt(s6, level, optname, optval,
513 warn("setsockopt %s", toptname);
522 * Set the socket to include or exclude filter mode, and
523 * add some sources to the filterlist, using the full-state API.
527 sockunion_t sources[MAX_ADDRS];
528 struct addrinfo hints;
529 struct addrinfo *res;
534 fmode = (*cmd == 'i') ? MCAST_INCLUDE : MCAST_EXCLUDE;
535 if ((sscanf(line, "%s %s %d", str1, str2, &n)) != 3) {
540 ifindex = parse_cmd_args(&su, NULL, str1, str2, NULL);
541 if (ifindex == 0 || n < 0 || n > MAX_ADDRS) {
545 af = su.sa.sa_family;
546 if (af2sock(af, s, s6) == -1) {
547 warnc(EPROTONOSUPPORT, "setsourcefilter");
551 memset(&hints, 0, sizeof(struct addrinfo));
552 hints.ai_flags = AI_NUMERICHOST;
553 hints.ai_family = af;
554 hints.ai_socktype = SOCK_DGRAM;
556 for (i = 0; i < n; i++) {
557 sockunion_t *psu = (sockunion_t *)&sources[i];
559 * Trim trailing whitespace, as getaddrinfo()
560 * can't cope with it.
562 fgets(str1, sizeof(str1), fp);
563 cp = strchr(str1, '\n');
568 error = getaddrinfo(str1, "0", &hints, &res);
573 memset(psu, 0, sizeof(sockunion_t));
574 af1 = res->ai_family;
576 memcpy(psu, res->ai_addr, res->ai_addrlen);
583 warnx("getaddrinfo: %s", gai_strerror(error));
587 if (setsourcefilter(af2sock(af, s, s6), ifindex,
588 &su.sa, su.sa.sa_len, fmode, n, &sources[0].ss) != 0)
589 warn("setsourcefilter");
595 * Allow or block traffic from a source, using the
602 sscanf(line, "%s %s %s", str1, str2, str3);
603 ifindex = parse_cmd_args(&su, &su2, str1, str2, str3);
604 if (ifindex == 0 || su2.sa.sa_family == AF_UNSPEC) {
608 af = su.sa.sa_family;
609 if (af2sock(af, s, s6) == -1) {
610 warnc(EPROTONOSUPPORT, "getsourcefilter");
614 /* First determine our current filter mode. */
615 if (getsourcefilter(af2sock(af, s, s6), ifindex,
616 &su.sa, su.sa.sa_len, &fmode, &n, NULL) != 0) {
617 warn("getsourcefilter");
624 error = __ifindex_to_primary_ip(ifindex, &ina);
626 warn("primary_ip_lookup %s", str2);
631 optval = (void *)&mr.mrs;
632 optlen = sizeof(mr.mrs);
633 mr.mrs.imr_multiaddr = su.sin.sin_addr;
634 mr.mrs.imr_sourceaddr = su2.sin.sin_addr;
635 mr.mrs.imr_interface = ina;
636 if (fmode == MCAST_EXCLUDE) {
637 /* Any-source mode socket membership. */
638 optname = (*cmd == 't') ?
641 toptname = (*cmd == 't') ?
642 "IP_UNBLOCK_SOURCE" :
645 /* Source-specific mode socket membership. */
646 optname = (*cmd == 't') ?
647 IP_ADD_SOURCE_MEMBERSHIP :
648 IP_DROP_SOURCE_MEMBERSHIP;
649 toptname = (*cmd == 't') ?
650 "IP_ADD_SOURCE_MEMBERSHIP" :
651 "IP_DROP_SOURCE_MEMBERSHIP";
653 if (setsockopt(s, level, optname, optval,
658 warn("setsockopt %s", toptname);
663 #endif /* INET with INET6 */
666 if (af == AF_INET6) {
667 level = IPPROTO_IPV6;
668 mr.gr.gsr_interface = ifindex;
669 mr.gr.gsr_group = su.ss;
670 mr.gr.gsr_source = su2.ss;
671 if (fmode == MCAST_EXCLUDE) {
672 /* Any-source mode socket membership. */
673 optname = (*cmd == 't') ?
674 MCAST_UNBLOCK_SOURCE :
676 toptname = (*cmd == 't') ?
677 "MCAST_UNBLOCK_SOURCE" :
678 "MCAST_BLOCK_SOURCE";
680 /* Source-specific mode socket membership. */
681 optname = (*cmd == 't') ?
682 MCAST_JOIN_SOURCE_GROUP :
683 MCAST_LEAVE_SOURCE_GROUP;
684 toptname = (*cmd == 't') ?
685 "MCAST_JOIN_SOURCE_GROUP":
686 "MCAST_LEAVE_SOURCE_GROUP";
688 optval = (void *)&mr.gr;
689 optlen = sizeof(mr.gr);
690 if (setsockopt(s6, level, optname, optval,
695 warn("setsockopt %s", toptname);
704 sockunion_t sources[MAX_ADDRS];
705 char addrbuf[NI_MAXHOST];
708 if ((sscanf(line, "%s %s %d", str1, str2, &nreqsrc)) != 3) {
712 ifindex = parse_cmd_args(&su, NULL, str1, str2, NULL);
713 if (ifindex == 0 || (n < 0 || n > MAX_ADDRS)) {
718 af = su.sa.sa_family;
719 if (af2sock(af, s, s6) == -1) {
720 warnc(EPROTONOSUPPORT, "getsourcefilter");
724 if (getsourcefilter(af2sock(af, s, s6), ifindex, &su.sa,
725 su.sa.sa_len, &fmode, &nsrc, &sources[0].ss) != 0) {
726 warn("getsourcefilter");
730 printf("%s\n", (fmode == MCAST_INCLUDE) ? "include" :
732 printf("%d\n", nsrc);
734 nsrc = MIN(nreqsrc, nsrc);
735 fprintf(stderr, "hexdump of sources:\n");
736 uint8_t *bp = (uint8_t *)&sources[0];
737 for (j = 0; j < (nsrc * sizeof(sources[0])); j++) {
738 fprintf(stderr, "%02x", bp[j]);
740 fprintf(stderr, "\nend hexdump\n");
742 qsort(sources, nsrc, af2socklen(af), su_cmp);
743 for (i = 0; i < nsrc; i++) {
744 sockunion_t *psu = (sockunion_t *)&sources[i];
746 error = getnameinfo(&psu->sa, psu->sa.sa_len,
747 addrbuf, sizeof(addrbuf), NULL, 0,
750 warnx("getnameinfo: %s", gai_strerror(error));
752 printf("%s\n", addrbuf);
757 /* link-layer stuff follows. */
761 struct sockaddr_dl *dlp;
762 struct ether_addr *ep;
764 memset(&ifr, 0, sizeof(struct ifreq));
765 dlp = (struct sockaddr_dl *)&ifr.ifr_addr;
766 dlp->sdl_len = sizeof(struct sockaddr_dl);
767 dlp->sdl_family = AF_LINK;
770 dlp->sdl_alen = ETHER_ADDR_LEN;
772 if (sscanf(line, "%s %s", str1, str2) != 2) {
773 warnc(EINVAL, "sscanf");
776 ep = ether_aton(str2);
778 warnc(EINVAL, "ether_aton");
781 strlcpy(ifr.ifr_name, str1, IF_NAMESIZE);
782 memcpy(LLADDR(dlp), ep, ETHER_ADDR_LEN);
783 if (ioctl(s, (*cmd == 'a') ? SIOCADDMULTI : SIOCDELMULTI,
785 warn("ioctl SIOCADDMULTI/SIOCDELMULTI");
794 "warning: IFF_ALLMULTI cannot be set from userland "
795 "in FreeBSD; command ignored.\n");
800 if (sscanf(line, "%s %u", ifr.ifr_name, &f) != 2) {
804 if (ioctl(s, SIOCGIFFLAGS, &ifr) == -1) {
805 warn("ioctl SIOCGIFFLAGS");
808 flags = (ifr.ifr_flags & 0xffff) | (ifr.ifr_flagshigh << 16);
810 flags &= ~IFF_PPROMISC;
812 flags |= IFF_PPROMISC;
814 ifr.ifr_flags = flags & 0xffff;
815 ifr.ifr_flagshigh = flags >> 16;
816 if (ioctl(s, SIOCSIFFLAGS, &ifr) == -1)
817 warn("ioctl SIOCGIFFLAGS");
819 printf( "changed to 0x%08x\n", flags );
825 printf("invalid command\n");
834 printf("j mcast-addr ifname [src-addr] - join IP multicast group\n");
835 printf("l mcast-addr ifname [src-addr] - leave IP multicast group\n");
837 "i mcast-addr ifname n - set n include mode src filter\n");
839 "e mcast-addr ifname n - set n exclude mode src filter\n");
840 printf("t mcast-addr ifname src-addr - allow traffic from src\n");
841 printf("b mcast-addr ifname src-addr - block traffic from src\n");
842 printf("g mcast-addr ifname n - get and show n src filters\n");
843 printf("a ifname mac-addr - add link multicast filter\n");
844 printf("d ifname mac-addr - delete link multicast filter\n");
845 printf("m ifname 1/0 - set/clear ether allmulti flag\n");
846 printf("p ifname 1/0 - set/clear ether promisc flag\n");
847 printf("f filename - read command(s) from file\n");
848 printf("s seconds - sleep for some time\n");
849 printf("q - quit\n");