2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/errno.h>
47 #include <sys/socket.h>
49 #include <sys/ioctl.h>
52 #include <net/if_dl.h>
53 #include <net/ethernet.h>
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/ip.h>
58 #include <netinet/ip_var.h>
61 #include <netinet/in.h>
62 #include <netinet/ip6.h>
74 #include <arpa/inet.h>
79 struct sockaddr_storage ss;
81 struct sockaddr_dl sdl;
83 struct sockaddr_in sin;
86 struct sockaddr_in6 sin6;
89 typedef union sockunion sockunion_t;
94 struct ip_mreq_source mrs;
98 struct group_source_req gr;
101 typedef union mrequnion mrequnion_t;
105 #define LINE_LENGTH 80
108 static int __ifindex_to_primary_ip(const uint32_t, struct in_addr *);
110 static uint32_t parse_cmd_args(sockunion_t *, sockunion_t *,
111 const char *, const char *, const char *);
112 static void process_file(char *, int, int);
113 static void process_cmd(char*, int, int, FILE *);
114 static int su_cmp(const void *, const void *);
115 static void usage(void);
118 * Ordering predicate for qsort().
121 su_cmp(const void *a, const void *b)
123 const sockunion_t *sua = (const sockunion_t *)a;
124 const sockunion_t *sub = (const sockunion_t *)b;
126 assert(sua->sa.sa_family == sub->sa.sa_family);
128 switch (sua->sa.sa_family) {
131 return ((int)(sua->sin.sin_addr.s_addr -
132 sub->sin.sin_addr.s_addr));
137 return (memcmp(&sua->sin6.sin6_addr, &sub->sin6.sin6_addr,
138 sizeof(struct in6_addr)));
145 assert(sua->sa.sa_len == sub->sa.sa_len);
146 return (memcmp(sua, sub, sua->sa.sa_len));
151 * Internal: Map an interface index to primary IPv4 address.
152 * This is somewhat inefficient. This is a useful enough operation
153 * that it probably belongs in the C library.
154 * Return zero if found, -1 on error, 1 on not found.
157 __ifindex_to_primary_ip(const uint32_t ifindex, struct in_addr *pina)
159 char ifname[IFNAMSIZ];
161 struct ifaddrs *ifaddrs;
165 assert(ifindex != 0);
168 if (if_indextoname(ifindex, ifname) == NULL)
170 if (getifaddrs(&ifaddrs) < 0)
174 * Find the ifaddr entry corresponding to the interface name,
175 * and return the first matching IPv4 address.
178 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
179 if (strcmp(ifa->ifa_name, ifname) != 0)
181 psu = (sockunion_t *)ifa->ifa_addr;
182 if (psu && psu->sa.sa_family == AF_INET) {
184 memcpy(pina, &psu->sin.sin_addr,
185 sizeof(struct in_addr));
191 errno = EADDRNOTAVAIL; /* XXX */
193 freeifaddrs(ifaddrs);
199 main(int argc, char **argv)
201 char line[LINE_LENGTH];
208 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
209 if (s == -1 && errno != EAFNOSUPPORT)
210 err(1, "can't open IPv4 socket");
213 s6 = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
214 if (s6 == -1 && errno != EAFNOSUPPORT)
215 err(1, "can't open IPv6 socket");
217 if (s == -1 && s6 == -1)
218 errc(1, EPROTONOSUPPORT, "can't open socket");
221 if (isatty(STDIN_FILENO)) {
222 printf("multicast membership test program; "
223 "enter ? for list of commands\n");
226 if (fgets(line, sizeof(line), stdin) != NULL) {
228 process_cmd(line, s, s6, stdin);
230 /* Get the filename */
231 for (i = 1; isblank(line[i]); i++);
232 if ((p = (char*)strchr(line, '\n'))
235 process_file(&line[i], s, s6);
238 } while (!feof(stdin));
240 for (i = 1; i < argc; i++) {
241 process_file(argv[i], s, s6);
254 process_file(char *fname, int s, int s6)
260 fp = fopen(fname, "r");
266 /* Skip comments and empty lines. */
267 while (fgets(line, sizeof(line), fp) != NULL) {
269 while (isblank(*lineptr))
271 if (*lineptr != '#' && *lineptr != '\n')
272 process_cmd(lineptr, s, s6, fp);
279 * Parse join/leave/allow/block arguments, given:
280 * str1: group (as AF_INET or AF_INET6 printable)
282 * str3: optional source address (may be NULL).
283 * This argument must have the same parsed address family as str1.
284 * Return the ifindex of ifname, or 0 if any parse element failed.
287 parse_cmd_args(sockunion_t *psu, sockunion_t *psu2,
288 const char *str1, const char *str2, const char *str3)
290 struct addrinfo hints;
291 struct addrinfo *res;
296 assert(str1 != NULL);
297 assert(str2 != NULL);
301 ifindex = if_nametoindex(str2);
305 memset(&hints, 0, sizeof(struct addrinfo));
306 hints.ai_flags = AI_NUMERICHOST;
307 hints.ai_family = PF_UNSPEC;
308 hints.ai_socktype = SOCK_DGRAM;
310 memset(psu, 0, sizeof(sockunion_t));
311 psu->sa.sa_family = AF_UNSPEC;
313 error = getaddrinfo(str1, "0", &hints, &res);
315 warnx("getaddrinfo: %s", gai_strerror(error));
320 memcpy(psu, res->ai_addr, res->ai_addrlen);
323 /* sscanf() may pass the empty string. */
324 if (psu2 != NULL && str3 != NULL && *str3 != '\0') {
325 memset(psu2, 0, sizeof(sockunion_t));
326 psu2->sa.sa_family = AF_UNSPEC;
328 /* look for following address family; str3 is *optional*. */
329 hints.ai_family = af;
330 error = getaddrinfo(str3, "0", &hints, &res);
332 warnx("getaddrinfo: %s", gai_strerror(error));
335 if (af != res->ai_family) {
336 errno = EINVAL; /* XXX */
339 memcpy(psu2, res->ai_addr, res->ai_addrlen);
348 af2sock(const int af, int s, int s6)
363 af2socklen(const int af)
368 return (sizeof(struct sockaddr_in));
372 return (sizeof(struct sockaddr_in6));
378 process_cmd(char *cmd, int s, int s6, FILE *fp __unused)
389 uint32_t fmode, ifindex;
391 int af, error, f, flags, i, level, n, optname;
394 su.sa.sa_family = AF_UNSPEC;
395 su2.sa.sa_family = AF_UNSPEC;
398 while (isblank(*++line))
399 ; /* Skip whitespace. */
412 if ((sscanf(line, "%d", &n) != 1) || (n < 1)) {
424 sscanf(line, "%s %s %s", str1, str2, str3);
425 ifindex = parse_cmd_args(&su, &su2, str1, str2, str3);
430 af = su.sa.sa_family;
435 error = __ifindex_to_primary_ip(ifindex, &ina);
437 warn("primary_ip_lookup %s", str2);
443 if (su2.sa.sa_family != AF_UNSPEC) {
444 mr.mrs.imr_multiaddr = su.sin.sin_addr;
445 mr.mrs.imr_sourceaddr = su2.sin.sin_addr;
446 mr.mrs.imr_interface = ina;
447 optname = (*cmd == 'j') ?
448 IP_ADD_SOURCE_MEMBERSHIP :
449 IP_DROP_SOURCE_MEMBERSHIP;
450 toptname = (*cmd == 'j') ?
451 "IP_ADD_SOURCE_MEMBERSHIP" :
452 "IP_DROP_SOURCE_MEMBERSHIP";
453 optval = (void *)&mr.mrs;
454 optlen = sizeof(mr.mrs);
456 mr.mr.imr_multiaddr = su.sin.sin_addr;
457 mr.mr.imr_interface = ina;
458 optname = (*cmd == 'j') ?
459 IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP;
460 toptname = (*cmd == 'j') ?
461 "IP_ADD_MEMBERSHIP" : "IP_DROP_MEMBERSHIP";
462 optval = (void *)&mr.mr;
463 optlen = sizeof(mr.mr);
466 warnc(EPROTONOSUPPORT, "setsockopt %s",
468 } else if (setsockopt(s, level, optname, optval,
473 warn("setsockopt %s", toptname);
478 #endif /* INET with INET6 */
481 if (af == AF_INET6) {
482 level = IPPROTO_IPV6;
483 if (su2.sa.sa_family != AF_UNSPEC) {
484 mr.gr.gsr_interface = ifindex;
485 mr.gr.gsr_group = su.ss;
486 mr.gr.gsr_source = su2.ss;
487 optname = (*cmd == 'j') ?
488 MCAST_JOIN_SOURCE_GROUP:
489 MCAST_LEAVE_SOURCE_GROUP;
490 toptname = (*cmd == 'j') ?
491 "MCAST_JOIN_SOURCE_GROUP":
492 "MCAST_LEAVE_SOURCE_GROUP";
493 optval = (void *)&mr.gr;
494 optlen = sizeof(mr.gr);
496 mr.mr6.ipv6mr_multiaddr = su.sin6.sin6_addr;
497 mr.mr6.ipv6mr_interface = ifindex;
498 optname = (*cmd == 'j') ?
501 toptname = (*cmd == 'j') ?
504 optval = (void *)&mr.mr6;
505 optlen = sizeof(mr.mr6);
508 warnc(EPROTONOSUPPORT, "setsockopt %s",
510 } else if (setsockopt(s6, level, optname, optval,
515 warn("setsockopt %s", toptname);
524 * Set the socket to include or exclude filter mode, and
525 * add some sources to the filterlist, using the full-state API.
529 sockunion_t sources[MAX_ADDRS];
530 struct addrinfo hints;
531 struct addrinfo *res;
536 fmode = (*cmd == 'i') ? MCAST_INCLUDE : MCAST_EXCLUDE;
537 if ((sscanf(line, "%s %s %d", str1, str2, &n)) != 3) {
542 ifindex = parse_cmd_args(&su, NULL, str1, str2, NULL);
543 if (ifindex == 0 || n < 0 || n > MAX_ADDRS) {
547 af = su.sa.sa_family;
548 if (af2sock(af, s, s6) == -1) {
549 warnc(EPROTONOSUPPORT, "setsourcefilter");
553 memset(&hints, 0, sizeof(struct addrinfo));
554 hints.ai_flags = AI_NUMERICHOST;
555 hints.ai_family = af;
556 hints.ai_socktype = SOCK_DGRAM;
558 for (i = 0; i < n; i++) {
559 sockunion_t *psu = (sockunion_t *)&sources[i];
561 * Trim trailing whitespace, as getaddrinfo()
562 * can't cope with it.
564 fgets(str1, sizeof(str1), fp);
565 cp = strchr(str1, '\n');
570 error = getaddrinfo(str1, "0", &hints, &res);
575 memset(psu, 0, sizeof(sockunion_t));
576 af1 = res->ai_family;
578 memcpy(psu, res->ai_addr, res->ai_addrlen);
585 warnx("getaddrinfo: %s", gai_strerror(error));
589 if (setsourcefilter(af2sock(af, s, s6), ifindex,
590 &su.sa, su.sa.sa_len, fmode, n, &sources[0].ss) != 0)
591 warn("setsourcefilter");
597 * Allow or block traffic from a source, using the
604 sscanf(line, "%s %s %s", str1, str2, str3);
605 ifindex = parse_cmd_args(&su, &su2, str1, str2, str3);
606 if (ifindex == 0 || su2.sa.sa_family == AF_UNSPEC) {
610 af = su.sa.sa_family;
611 if (af2sock(af, s, s6) == -1) {
612 warnc(EPROTONOSUPPORT, "getsourcefilter");
616 /* First determine our current filter mode. */
617 if (getsourcefilter(af2sock(af, s, s6), ifindex,
618 &su.sa, su.sa.sa_len, &fmode, &n, NULL) != 0) {
619 warn("getsourcefilter");
626 error = __ifindex_to_primary_ip(ifindex, &ina);
628 warn("primary_ip_lookup %s", str2);
633 optval = (void *)&mr.mrs;
634 optlen = sizeof(mr.mrs);
635 mr.mrs.imr_multiaddr = su.sin.sin_addr;
636 mr.mrs.imr_sourceaddr = su2.sin.sin_addr;
637 mr.mrs.imr_interface = ina;
638 if (fmode == MCAST_EXCLUDE) {
639 /* Any-source mode socket membership. */
640 optname = (*cmd == 't') ?
643 toptname = (*cmd == 't') ?
644 "IP_UNBLOCK_SOURCE" :
647 /* Source-specific mode socket membership. */
648 optname = (*cmd == 't') ?
649 IP_ADD_SOURCE_MEMBERSHIP :
650 IP_DROP_SOURCE_MEMBERSHIP;
651 toptname = (*cmd == 't') ?
652 "IP_ADD_SOURCE_MEMBERSHIP" :
653 "IP_DROP_SOURCE_MEMBERSHIP";
655 if (setsockopt(s, level, optname, optval,
660 warn("setsockopt %s", toptname);
665 #endif /* INET with INET6 */
668 if (af == AF_INET6) {
669 level = IPPROTO_IPV6;
670 mr.gr.gsr_interface = ifindex;
671 mr.gr.gsr_group = su.ss;
672 mr.gr.gsr_source = su2.ss;
673 if (fmode == MCAST_EXCLUDE) {
674 /* Any-source mode socket membership. */
675 optname = (*cmd == 't') ?
676 MCAST_UNBLOCK_SOURCE :
678 toptname = (*cmd == 't') ?
679 "MCAST_UNBLOCK_SOURCE" :
680 "MCAST_BLOCK_SOURCE";
682 /* Source-specific mode socket membership. */
683 optname = (*cmd == 't') ?
684 MCAST_JOIN_SOURCE_GROUP :
685 MCAST_LEAVE_SOURCE_GROUP;
686 toptname = (*cmd == 't') ?
687 "MCAST_JOIN_SOURCE_GROUP":
688 "MCAST_LEAVE_SOURCE_GROUP";
690 optval = (void *)&mr.gr;
691 optlen = sizeof(mr.gr);
692 if (setsockopt(s6, level, optname, optval,
697 warn("setsockopt %s", toptname);
706 sockunion_t sources[MAX_ADDRS];
707 char addrbuf[NI_MAXHOST];
710 if ((sscanf(line, "%s %s %d", str1, str2, &nreqsrc)) != 3) {
714 ifindex = parse_cmd_args(&su, NULL, str1, str2, NULL);
715 if (ifindex == 0 || (n < 0 || n > MAX_ADDRS)) {
720 af = su.sa.sa_family;
721 if (af2sock(af, s, s6) == -1) {
722 warnc(EPROTONOSUPPORT, "getsourcefilter");
726 if (getsourcefilter(af2sock(af, s, s6), ifindex, &su.sa,
727 su.sa.sa_len, &fmode, &nsrc, &sources[0].ss) != 0) {
728 warn("getsourcefilter");
732 printf("%s\n", (fmode == MCAST_INCLUDE) ? "include" :
734 printf("%d\n", nsrc);
736 nsrc = MIN(nreqsrc, nsrc);
737 fprintf(stderr, "hexdump of sources:\n");
738 uint8_t *bp = (uint8_t *)&sources[0];
739 for (i = 0; i < (nsrc * sizeof(sources[0])); i++) {
740 fprintf(stderr, "%02x", bp[i]);
742 fprintf(stderr, "\nend hexdump\n");
744 qsort(sources, nsrc, af2socklen(af), su_cmp);
745 for (i = 0; i < nsrc; i++) {
746 sockunion_t *psu = (sockunion_t *)&sources[i];
748 error = getnameinfo(&psu->sa, psu->sa.sa_len,
749 addrbuf, sizeof(addrbuf), NULL, 0,
752 warnx("getnameinfo: %s", gai_strerror(error));
754 printf("%s\n", addrbuf);
759 /* link-layer stuff follows. */
763 struct sockaddr_dl *dlp;
764 struct ether_addr *ep;
766 memset(&ifr, 0, sizeof(struct ifreq));
767 dlp = (struct sockaddr_dl *)&ifr.ifr_addr;
768 dlp->sdl_len = sizeof(struct sockaddr_dl);
769 dlp->sdl_family = AF_LINK;
772 dlp->sdl_alen = ETHER_ADDR_LEN;
774 if (sscanf(line, "%s %s", str1, str2) != 2) {
775 warnc(EINVAL, "sscanf");
778 ep = ether_aton(str2);
780 warnc(EINVAL, "ether_aton");
783 strlcpy(ifr.ifr_name, str1, IF_NAMESIZE);
784 memcpy(LLADDR(dlp), ep, ETHER_ADDR_LEN);
785 if (ioctl(s, (*cmd == 'a') ? SIOCADDMULTI : SIOCDELMULTI,
787 warn("ioctl SIOCADDMULTI/SIOCDELMULTI");
796 "warning: IFF_ALLMULTI cannot be set from userland "
797 "in FreeBSD; command ignored.\n");
802 if (sscanf(line, "%s %u", ifr.ifr_name, &f) != 2) {
806 if (ioctl(s, SIOCGIFFLAGS, &ifr) == -1) {
807 warn("ioctl SIOCGIFFLAGS");
810 flags = (ifr.ifr_flags & 0xffff) | (ifr.ifr_flagshigh << 16);
812 flags &= ~IFF_PPROMISC;
814 flags |= IFF_PPROMISC;
816 ifr.ifr_flags = flags & 0xffff;
817 ifr.ifr_flagshigh = flags >> 16;
818 if (ioctl(s, SIOCSIFFLAGS, &ifr) == -1)
819 warn("ioctl SIOCGIFFLAGS");
821 printf( "changed to 0x%08x\n", flags );
827 printf("invalid command\n");
836 printf("j mcast-addr ifname [src-addr] - join IP multicast group\n");
837 printf("l mcast-addr ifname [src-addr] - leave IP multicast group\n");
839 "i mcast-addr ifname n - set n include mode src filter\n");
841 "e mcast-addr ifname n - set n exclude mode src filter\n");
842 printf("t mcast-addr ifname src-addr - allow traffic from src\n");
843 printf("b mcast-addr ifname src-addr - block traffic from src\n");
844 printf("g mcast-addr ifname n - get and show n src filters\n");
845 printf("a ifname mac-addr - add link multicast filter\n");
846 printf("d ifname mac-addr - delete link multicast filter\n");
847 printf("m ifname 1/0 - set/clear ether allmulti flag\n");
848 printf("p ifname 1/0 - set/clear ether promisc flag\n");
849 printf("f filename - read command(s) from file\n");
850 printf("s seconds - sleep for some time\n");
851 printf("q - quit\n");