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);
208 err(1, "can't open IPv4 socket");
211 s6 = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
213 err(1, "can't open IPv6 socket");
217 if (isatty(STDIN_FILENO)) {
218 printf("multicast membership test program; "
219 "enter ? for list of commands\n");
222 if (fgets(line, sizeof(line), stdin) != NULL) {
224 process_cmd(line, s, s6, stdin);
226 /* Get the filename */
227 for (i = 1; isblank(line[i]); i++);
228 if ((p = (char*)strchr(line, '\n'))
231 process_file(&line[i], s, s6);
234 } while (!feof(stdin));
236 for (i = 1; i < argc; i++) {
237 process_file(argv[i], s, s6);
250 process_file(char *fname, int s, int s6)
256 fp = fopen(fname, "r");
262 /* Skip comments and empty lines. */
263 while (fgets(line, sizeof(line), fp) != NULL) {
265 while (isblank(*lineptr))
267 if (*lineptr != '#' && *lineptr != '\n')
268 process_cmd(lineptr, s, s6, fp);
275 * Parse join/leave/allow/block arguments, given:
276 * str1: group (as AF_INET or AF_INET6 printable)
278 * str3: optional source address (may be NULL).
279 * This argument must have the same parsed address family as str1.
280 * Return the ifindex of ifname, or 0 if any parse element failed.
283 parse_cmd_args(sockunion_t *psu, sockunion_t *psu2,
284 const char *str1, const char *str2, const char *str3)
286 struct addrinfo hints;
287 struct addrinfo *res;
292 assert(str1 != NULL);
293 assert(str2 != NULL);
297 ifindex = if_nametoindex(str2);
301 memset(&hints, 0, sizeof(struct addrinfo));
302 hints.ai_flags = AI_NUMERICHOST;
303 hints.ai_family = PF_UNSPEC;
304 hints.ai_socktype = SOCK_DGRAM;
306 memset(psu, 0, sizeof(sockunion_t));
307 psu->sa.sa_family = AF_UNSPEC;
309 error = getaddrinfo(str1, "0", &hints, &res);
311 warnx("getaddrinfo: %s", gai_strerror(error));
316 memcpy(psu, res->ai_addr, res->ai_addrlen);
319 /* sscanf() may pass the empty string. */
320 if (psu2 != NULL && str3 != NULL && *str3 != '\0') {
321 memset(psu2, 0, sizeof(sockunion_t));
322 psu2->sa.sa_family = AF_UNSPEC;
324 /* look for following address family; str3 is *optional*. */
325 hints.ai_family = af;
326 error = getaddrinfo(str3, "0", &hints, &res);
328 warnx("getaddrinfo: %s", gai_strerror(error));
331 if (af != res->ai_family) {
332 errno = EINVAL; /* XXX */
335 memcpy(psu2, res->ai_addr, res->ai_addrlen);
344 af2sock(const int af, int s, int s6)
359 af2socklen(const int af)
364 return (sizeof(struct sockaddr_in));
368 return (sizeof(struct sockaddr_in6));
374 process_cmd(char *cmd, int s, int s6 __unused, FILE *fp __unused)
385 uint32_t fmode, ifindex;
387 int af, error, f, flags, i, level, n, optname;
390 su.sa.sa_family = AF_UNSPEC;
391 su2.sa.sa_family = AF_UNSPEC;
394 while (isblank(*++line))
395 ; /* Skip whitespace. */
407 if ((sscanf(line, "%d", &n) != 1) || (n < 1)) {
419 sscanf(line, "%s %s %s", str1, str2, str3);
420 ifindex = parse_cmd_args(&su, &su2, str1, str2, str3);
425 af = su.sa.sa_family;
430 error = __ifindex_to_primary_ip(ifindex, &ina);
432 warn("primary_ip_lookup %s", str2);
438 if (su2.sa.sa_family != AF_UNSPEC) {
439 mr.mrs.imr_multiaddr = su.sin.sin_addr;
440 mr.mrs.imr_sourceaddr = su2.sin.sin_addr;
441 mr.mrs.imr_interface = ina;
442 optname = (*cmd == 'j') ?
443 IP_ADD_SOURCE_MEMBERSHIP :
444 IP_DROP_SOURCE_MEMBERSHIP;
445 toptname = (*cmd == 'j') ?
446 "IP_ADD_SOURCE_MEMBERSHIP" :
447 "IP_DROP_SOURCE_MEMBERSHIP";
448 optval = (void *)&mr.mrs;
449 optlen = sizeof(mr.mrs);
451 mr.mr.imr_multiaddr = su.sin.sin_addr;
452 mr.mr.imr_interface = ina;
453 optname = (*cmd == 'j') ?
454 IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP;
455 toptname = (*cmd == 'j') ?
456 "IP_ADD_MEMBERSHIP" : "IP_DROP_MEMBERSHIP";
457 optval = (void *)&mr.mr;
458 optlen = sizeof(mr.mr);
460 if (setsockopt(s, level, optname, optval,
465 warn("setsockopt %s", toptname);
470 #endif /* INET with INET6 */
473 if (af == AF_INET6) {
474 level = IPPROTO_IPV6;
475 if (su2.sa.sa_family != AF_UNSPEC) {
476 mr.gr.gsr_interface = ifindex;
477 mr.gr.gsr_group = su.ss;
478 mr.gr.gsr_source = su2.ss;
479 optname = (*cmd == 'j') ?
480 MCAST_JOIN_SOURCE_GROUP:
481 MCAST_LEAVE_SOURCE_GROUP;
482 toptname = (*cmd == 'j') ?
483 "MCAST_JOIN_SOURCE_GROUP":
484 "MCAST_LEAVE_SOURCE_GROUP";
485 optval = (void *)&mr.gr;
486 optlen = sizeof(mr.gr);
488 mr.mr6.ipv6mr_multiaddr = su.sin6.sin6_addr;
489 mr.mr6.ipv6mr_interface = ifindex;
490 optname = (*cmd == 'j') ?
493 toptname = (*cmd == 'j') ?
496 optval = (void *)&mr.mr6;
497 optlen = sizeof(mr.mr6);
499 if (setsockopt(s6, level, optname, optval,
504 warn("setsockopt %s", toptname);
513 * Set the socket to include or exclude filter mode, and
514 * add some sources to the filterlist, using the full-state API.
518 sockunion_t sources[MAX_ADDRS];
519 struct addrinfo hints;
520 struct addrinfo *res;
525 fmode = (*cmd == 'i') ? MCAST_INCLUDE : MCAST_EXCLUDE;
526 if ((sscanf(line, "%s %s %d", str1, str2, &n)) != 3) {
531 ifindex = parse_cmd_args(&su, NULL, str1, str2, NULL);
532 if (ifindex == 0 || n < 0 || n > MAX_ADDRS) {
536 af = su.sa.sa_family;
538 memset(&hints, 0, sizeof(struct addrinfo));
539 hints.ai_flags = AI_NUMERICHOST;
540 hints.ai_family = af;
541 hints.ai_socktype = SOCK_DGRAM;
543 for (i = 0; i < n; i++) {
544 sockunion_t *psu = (sockunion_t *)&sources[i];
546 * Trim trailing whitespace, as getaddrinfo()
547 * can't cope with it.
549 fgets(str1, sizeof(str1), fp);
550 cp = strchr(str1, '\n');
555 error = getaddrinfo(str1, "0", &hints, &res);
560 memset(psu, 0, sizeof(sockunion_t));
561 af1 = res->ai_family;
563 memcpy(psu, res->ai_addr, res->ai_addrlen);
570 warnx("getaddrinfo: %s", gai_strerror(error));
574 if (setsourcefilter(af2sock(af, s, s6), ifindex,
575 &su.sa, su.sa.sa_len, fmode, n, &sources[0].ss) != 0)
576 warn("setsourcefilter");
582 * Allow or block traffic from a source, using the
589 sscanf(line, "%s %s %s", str1, str2, str3);
590 ifindex = parse_cmd_args(&su, &su2, str1, str2, str3);
591 if (ifindex == 0 || su2.sa.sa_family == AF_UNSPEC) {
595 af = su.sa.sa_family;
597 /* First determine our current filter mode. */
599 if (getsourcefilter(af2sock(af, s, s6), ifindex,
600 &su.sa, su.sa.sa_len, &fmode, &n, NULL) != 0) {
601 warn("getsourcefilter");
608 error = __ifindex_to_primary_ip(ifindex, &ina);
610 warn("primary_ip_lookup %s", str2);
615 optval = (void *)&mr.mrs;
616 optlen = sizeof(mr.mrs);
617 mr.mrs.imr_multiaddr = su.sin.sin_addr;
618 mr.mrs.imr_sourceaddr = su2.sin.sin_addr;
619 mr.mrs.imr_interface = ina;
620 if (fmode == MCAST_EXCLUDE) {
621 /* Any-source mode socket membership. */
622 optname = (*cmd == 't') ?
625 toptname = (*cmd == 't') ?
626 "IP_UNBLOCK_SOURCE" :
629 /* Source-specific mode socket membership. */
630 optname = (*cmd == 't') ?
631 IP_ADD_SOURCE_MEMBERSHIP :
632 IP_DROP_SOURCE_MEMBERSHIP;
633 toptname = (*cmd == 't') ?
634 "IP_ADD_SOURCE_MEMBERSHIP" :
635 "IP_DROP_SOURCE_MEMBERSHIP";
637 if (setsockopt(s, level, optname, optval,
642 warn("setsockopt %s", toptname);
647 #endif /* INET with INET6 */
650 if (af == AF_INET6) {
651 level = IPPROTO_IPV6;
652 mr.gr.gsr_interface = ifindex;
653 mr.gr.gsr_group = su.ss;
654 mr.gr.gsr_source = su2.ss;
655 if (fmode == MCAST_EXCLUDE) {
656 /* Any-source mode socket membership. */
657 optname = (*cmd == 't') ?
658 MCAST_UNBLOCK_SOURCE :
660 toptname = (*cmd == 't') ?
661 "MCAST_UNBLOCK_SOURCE" :
662 "MCAST_BLOCK_SOURCE";
664 /* Source-specific mode socket membership. */
665 optname = (*cmd == 't') ?
666 MCAST_JOIN_SOURCE_GROUP :
667 MCAST_LEAVE_SOURCE_GROUP;
668 toptname = (*cmd == 't') ?
669 "MCAST_JOIN_SOURCE_GROUP":
670 "MCAST_LEAVE_SOURCE_GROUP";
672 optval = (void *)&mr.gr;
673 optlen = sizeof(mr.gr);
674 if (setsockopt(s6, level, optname, optval,
679 warn("setsockopt %s", toptname);
688 sockunion_t sources[MAX_ADDRS];
689 char addrbuf[NI_MAXHOST];
692 if ((sscanf(line, "%s %s %d", str1, str2, &nreqsrc)) != 3) {
696 ifindex = parse_cmd_args(&su, NULL, str1, str2, NULL);
697 if (ifindex == 0 || (n < 0 || n > MAX_ADDRS)) {
702 af = su.sa.sa_family;
704 if (getsourcefilter(af2sock(af, s, s6), ifindex, &su.sa,
705 su.sa.sa_len, &fmode, &nsrc, &sources[0].ss) != 0) {
706 warn("getsourcefilter");
710 printf("%s\n", (fmode == MCAST_INCLUDE) ? "include" :
712 printf("%d\n", nsrc);
714 nsrc = MIN(nreqsrc, nsrc);
715 fprintf(stderr, "hexdump of sources:\n");
716 uint8_t *bp = (uint8_t *)&sources[0];
717 for (i = 0; i < (nsrc * sizeof(sources[0])); i++) {
718 fprintf(stderr, "%02x", bp[i]);
720 fprintf(stderr, "\nend hexdump\n");
722 qsort(sources, nsrc, af2socklen(af), su_cmp);
723 for (i = 0; i < nsrc; i++) {
724 sockunion_t *psu = (sockunion_t *)&sources[i];
726 error = getnameinfo(&psu->sa, psu->sa.sa_len,
727 addrbuf, sizeof(addrbuf), NULL, 0,
730 warnx("getnameinfo: %s", gai_strerror(error));
732 printf("%s\n", addrbuf);
737 /* link-layer stuff follows. */
741 struct sockaddr_dl *dlp;
742 struct ether_addr *ep;
744 memset(&ifr, 0, sizeof(struct ifreq));
745 dlp = (struct sockaddr_dl *)&ifr.ifr_addr;
746 dlp->sdl_len = sizeof(struct sockaddr_dl);
747 dlp->sdl_family = AF_LINK;
750 dlp->sdl_alen = ETHER_ADDR_LEN;
752 if (sscanf(line, "%s %s", str1, str2) != 2) {
753 warnc(EINVAL, "sscanf");
756 ep = ether_aton(str2);
758 warnc(EINVAL, "ether_aton");
761 strlcpy(ifr.ifr_name, str1, IF_NAMESIZE);
762 memcpy(LLADDR(dlp), ep, ETHER_ADDR_LEN);
763 if (ioctl(s, (*cmd == 'a') ? SIOCADDMULTI : SIOCDELMULTI,
765 warn("ioctl SIOCADDMULTI/SIOCDELMULTI");
774 "warning: IFF_ALLMULTI cannot be set from userland "
775 "in FreeBSD; command ignored.\n");
780 if (sscanf(line, "%s %u", ifr.ifr_name, &f) != 2) {
784 if (ioctl(s, SIOCGIFFLAGS, &ifr) == -1) {
785 warn("ioctl SIOCGIFFLAGS");
788 flags = (ifr.ifr_flags & 0xffff) | (ifr.ifr_flagshigh << 16);
790 flags &= ~IFF_PPROMISC;
792 flags |= IFF_PPROMISC;
794 ifr.ifr_flags = flags & 0xffff;
795 ifr.ifr_flagshigh = flags >> 16;
796 if (ioctl(s, SIOCSIFFLAGS, &ifr) == -1)
797 warn("ioctl SIOCGIFFLAGS");
799 printf( "changed to 0x%08x\n", flags );
805 printf("invalid command\n");
814 printf("j mcast-addr ifname [src-addr] - join IP multicast group\n");
815 printf("l mcast-addr ifname [src-addr] - leave IP multicast group\n");
817 "i mcast-addr ifname n - set n include mode src filter\n");
819 "e mcast-addr ifname n - set n exclude mode src filter\n");
820 printf("t mcast-addr ifname src-addr - allow traffic from src\n");
821 printf("b mcast-addr ifname src-addr - block traffic from src\n");
822 printf("g mcast-addr ifname n - get and show n src filters\n");
823 printf("a ifname mac-addr - add link multicast filter\n");
824 printf("d ifname mac-addr - delete link multicast filter\n");
825 printf("m ifname 1/0 - set/clear ether allmulti flag\n");
826 printf("p ifname 1/0 - set/clear ether promisc flag\n");
827 printf("f filename - read command(s) from file\n");
828 printf("s seconds - sleep for some time\n");
829 printf("q - quit\n");