2 * Copyright (c) 2007-2009 Bruce Simpson.
3 * Copyright (c) 2000 Wilbert De Graaf.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Diagnostic and test utility for multicast sockets.
33 * XXX: This file currently assumes INET support in the base system.
34 * TODO: Support embedded KAME Scope ID in IPv6 group addresses.
35 * TODO: Use IPv4 link-local address when source address selection
36 * is implemented; use MCAST_JOIN_SOURCE for IPv4.
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/errno.h>
45 #include <sys/socket.h>
47 #include <sys/ioctl.h>
50 #include <net/if_dl.h>
51 #include <net/ethernet.h>
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
56 #include <netinet/ip_var.h>
59 #include <netinet/in.h>
60 #include <netinet/ip6.h>
72 #include <arpa/inet.h>
77 struct sockaddr_storage ss;
79 struct sockaddr_dl sdl;
81 struct sockaddr_in sin;
84 struct sockaddr_in6 sin6;
87 typedef union sockunion sockunion_t;
92 struct ip_mreq_source mrs;
96 struct group_source_req gr;
99 typedef union mrequnion mrequnion_t;
103 #define LINE_LENGTH 80
106 static int __ifindex_to_primary_ip(const uint32_t, struct in_addr *);
108 static uint32_t parse_cmd_args(sockunion_t *, sockunion_t *,
109 const char *, const char *, const char *);
110 static void process_file(char *, int, int);
111 static void process_cmd(char*, int, int, FILE *);
112 static int su_cmp(const void *, const void *);
113 static void usage(void);
116 * Ordering predicate for qsort().
119 su_cmp(const void *a, const void *b)
121 const sockunion_t *sua = (const sockunion_t *)a;
122 const sockunion_t *sub = (const sockunion_t *)b;
124 assert(sua->sa.sa_family == sub->sa.sa_family);
126 switch (sua->sa.sa_family) {
129 return ((int)(sua->sin.sin_addr.s_addr -
130 sub->sin.sin_addr.s_addr));
135 return (memcmp(&sua->sin6.sin6_addr, &sub->sin6.sin6_addr,
136 sizeof(struct in6_addr)));
143 assert(sua->sa.sa_len == sub->sa.sa_len);
144 return (memcmp(sua, sub, sua->sa.sa_len));
149 * Internal: Map an interface index to primary IPv4 address.
150 * This is somewhat inefficient. This is a useful enough operation
151 * that it probably belongs in the C library.
152 * Return zero if found, -1 on error, 1 on not found.
155 __ifindex_to_primary_ip(const uint32_t ifindex, struct in_addr *pina)
157 char ifname[IFNAMSIZ];
159 struct ifaddrs *ifaddrs;
163 assert(ifindex != 0);
166 if (if_indextoname(ifindex, ifname) == NULL)
168 if (getifaddrs(&ifaddrs) < 0)
172 * Find the ifaddr entry corresponding to the interface name,
173 * and return the first matching IPv4 address.
176 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
177 if (strcmp(ifa->ifa_name, ifname) != 0)
179 psu = (sockunion_t *)ifa->ifa_addr;
180 if (psu && psu->sa.sa_family == AF_INET) {
182 memcpy(pina, &psu->sin.sin_addr,
183 sizeof(struct in_addr));
189 errno = EADDRNOTAVAIL; /* XXX */
191 freeifaddrs(ifaddrs);
197 main(int argc, char **argv)
199 char line[LINE_LENGTH];
206 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
207 if (s == -1 && errno != EAFNOSUPPORT)
208 err(1, "can't open IPv4 socket");
211 s6 = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
212 if (s6 == -1 && errno != EAFNOSUPPORT)
213 err(1, "can't open IPv6 socket");
215 if (s == -1 && s6 == -1)
216 errc(1, EPROTONOSUPPORT, "can't open socket");
219 if (isatty(STDIN_FILENO)) {
220 printf("multicast membership test program; "
221 "enter ? for list of commands\n");
224 if (fgets(line, sizeof(line), stdin) != NULL) {
226 process_cmd(line, s, s6, stdin);
228 /* Get the filename */
229 for (i = 1; isblank(line[i]); i++);
230 if ((p = (char*)strchr(line, '\n'))
233 process_file(&line[i], s, s6);
236 } while (!feof(stdin));
238 for (i = 1; i < argc; i++) {
239 process_file(argv[i], s, s6);
252 process_file(char *fname, int s, int s6)
258 fp = fopen(fname, "r");
264 /* Skip comments and empty lines. */
265 while (fgets(line, sizeof(line), fp) != NULL) {
267 while (isblank(*lineptr))
269 if (*lineptr != '#' && *lineptr != '\n')
270 process_cmd(lineptr, s, s6, fp);
277 * Parse join/leave/allow/block arguments, given:
278 * str1: group (as AF_INET or AF_INET6 printable)
280 * str3: optional source address (may be NULL).
281 * This argument must have the same parsed address family as str1.
282 * Return the ifindex of ifname, or 0 if any parse element failed.
285 parse_cmd_args(sockunion_t *psu, sockunion_t *psu2,
286 const char *str1, const char *str2, const char *str3)
288 struct addrinfo hints;
289 struct addrinfo *res;
294 assert(str1 != NULL);
295 assert(str2 != NULL);
299 ifindex = if_nametoindex(str2);
303 memset(&hints, 0, sizeof(struct addrinfo));
304 hints.ai_flags = AI_NUMERICHOST;
305 hints.ai_family = PF_UNSPEC;
306 hints.ai_socktype = SOCK_DGRAM;
308 memset(psu, 0, sizeof(sockunion_t));
309 psu->sa.sa_family = AF_UNSPEC;
311 error = getaddrinfo(str1, "0", &hints, &res);
313 warnx("getaddrinfo: %s", gai_strerror(error));
318 memcpy(psu, res->ai_addr, res->ai_addrlen);
321 /* sscanf() may pass the empty string. */
322 if (psu2 != NULL && str3 != NULL && *str3 != '\0') {
323 memset(psu2, 0, sizeof(sockunion_t));
324 psu2->sa.sa_family = AF_UNSPEC;
326 /* look for following address family; str3 is *optional*. */
327 hints.ai_family = af;
328 error = getaddrinfo(str3, "0", &hints, &res);
330 warnx("getaddrinfo: %s", gai_strerror(error));
333 if (af != res->ai_family) {
334 errno = EINVAL; /* XXX */
337 memcpy(psu2, res->ai_addr, res->ai_addrlen);
346 af2sock(const int af, int s, int s6)
361 af2socklen(const int af)
366 return (sizeof(struct sockaddr_in));
370 return (sizeof(struct sockaddr_in6));
376 process_cmd(char *cmd, int s, int s6, FILE *fp __unused)
387 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. */
409 if ((sscanf(line, "%d", &n) != 1) || (n < 1)) {
421 sscanf(line, "%s %s %s", str1, str2, str3);
422 ifindex = parse_cmd_args(&su, &su2, str1, str2, str3);
427 af = su.sa.sa_family;
432 error = __ifindex_to_primary_ip(ifindex, &ina);
434 warn("primary_ip_lookup %s", str2);
440 if (su2.sa.sa_family != AF_UNSPEC) {
441 mr.mrs.imr_multiaddr = su.sin.sin_addr;
442 mr.mrs.imr_sourceaddr = su2.sin.sin_addr;
443 mr.mrs.imr_interface = ina;
444 optname = (*cmd == 'j') ?
445 IP_ADD_SOURCE_MEMBERSHIP :
446 IP_DROP_SOURCE_MEMBERSHIP;
447 toptname = (*cmd == 'j') ?
448 "IP_ADD_SOURCE_MEMBERSHIP" :
449 "IP_DROP_SOURCE_MEMBERSHIP";
450 optval = (void *)&mr.mrs;
451 optlen = sizeof(mr.mrs);
453 mr.mr.imr_multiaddr = su.sin.sin_addr;
454 mr.mr.imr_interface = ina;
455 optname = (*cmd == 'j') ?
456 IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP;
457 toptname = (*cmd == 'j') ?
458 "IP_ADD_MEMBERSHIP" : "IP_DROP_MEMBERSHIP";
459 optval = (void *)&mr.mr;
460 optlen = sizeof(mr.mr);
463 warnc(EPROTONOSUPPORT, "setsockopt %s",
465 } else if (setsockopt(s, level, optname, optval,
470 warn("setsockopt %s", toptname);
475 #endif /* INET with INET6 */
478 if (af == AF_INET6) {
479 level = IPPROTO_IPV6;
480 if (su2.sa.sa_family != AF_UNSPEC) {
481 mr.gr.gsr_interface = ifindex;
482 mr.gr.gsr_group = su.ss;
483 mr.gr.gsr_source = su2.ss;
484 optname = (*cmd == 'j') ?
485 MCAST_JOIN_SOURCE_GROUP:
486 MCAST_LEAVE_SOURCE_GROUP;
487 toptname = (*cmd == 'j') ?
488 "MCAST_JOIN_SOURCE_GROUP":
489 "MCAST_LEAVE_SOURCE_GROUP";
490 optval = (void *)&mr.gr;
491 optlen = sizeof(mr.gr);
493 mr.mr6.ipv6mr_multiaddr = su.sin6.sin6_addr;
494 mr.mr6.ipv6mr_interface = ifindex;
495 optname = (*cmd == 'j') ?
498 toptname = (*cmd == 'j') ?
501 optval = (void *)&mr.mr6;
502 optlen = sizeof(mr.mr6);
505 warnc(EPROTONOSUPPORT, "setsockopt %s",
507 } else if (setsockopt(s6, level, optname, optval,
512 warn("setsockopt %s", toptname);
521 * Set the socket to include or exclude filter mode, and
522 * add some sources to the filterlist, using the full-state API.
526 sockunion_t sources[MAX_ADDRS];
527 struct addrinfo hints;
528 struct addrinfo *res;
533 fmode = (*cmd == 'i') ? MCAST_INCLUDE : MCAST_EXCLUDE;
534 if ((sscanf(line, "%s %s %d", str1, str2, &n)) != 3) {
539 ifindex = parse_cmd_args(&su, NULL, str1, str2, NULL);
540 if (ifindex == 0 || n < 0 || n > MAX_ADDRS) {
544 af = su.sa.sa_family;
545 if (af2sock(af, s, s6) == -1) {
546 warnc(EPROTONOSUPPORT, "setsourcefilter");
550 memset(&hints, 0, sizeof(struct addrinfo));
551 hints.ai_flags = AI_NUMERICHOST;
552 hints.ai_family = af;
553 hints.ai_socktype = SOCK_DGRAM;
555 for (i = 0; i < n; i++) {
556 sockunion_t *psu = (sockunion_t *)&sources[i];
558 * Trim trailing whitespace, as getaddrinfo()
559 * can't cope with it.
561 fgets(str1, sizeof(str1), fp);
562 cp = strchr(str1, '\n');
567 error = getaddrinfo(str1, "0", &hints, &res);
572 memset(psu, 0, sizeof(sockunion_t));
573 af1 = res->ai_family;
575 memcpy(psu, res->ai_addr, res->ai_addrlen);
582 warnx("getaddrinfo: %s", gai_strerror(error));
586 if (setsourcefilter(af2sock(af, s, s6), ifindex,
587 &su.sa, su.sa.sa_len, fmode, n, &sources[0].ss) != 0)
588 warn("setsourcefilter");
594 * Allow or block traffic from a source, using the
601 sscanf(line, "%s %s %s", str1, str2, str3);
602 ifindex = parse_cmd_args(&su, &su2, str1, str2, str3);
603 if (ifindex == 0 || su2.sa.sa_family == AF_UNSPEC) {
607 af = su.sa.sa_family;
608 if (af2sock(af, s, s6) == -1) {
609 warnc(EPROTONOSUPPORT, "getsourcefilter");
613 /* 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 (i = 0; i < (nsrc * sizeof(sources[0])); i++) {
738 fprintf(stderr, "%02x", bp[i]);
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");