2 * Copyright (c) 2007 Bruce M. Simpson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Regression test utility for RFC 3678 Advanced Multicast API in FreeBSD.
30 * TODO: Test the SSM paths.
31 * TODO: Support INET6. The code has been written to facilitate this later.
32 * TODO: Merge multicast socket option tests from ipsockopt.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
44 #include <net/if_dl.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
65 #ifndef __SOCKUNION_DECLARED
67 struct sockaddr_storage ss;
69 struct sockaddr_dl sdl;
70 struct sockaddr_in sin;
72 struct sockaddr_in6 sin6;
75 typedef union sockunion sockunion_t;
76 #define __SOCKUNION_DECLARED
77 #endif /* __SOCKUNION_DECLARED */
79 #define ADDRBUF_LEN 16
80 #define DEFAULT_GROUP_STR "238.1.1.0"
81 #define DEFAULT_IFNAME "lo0"
82 #define DEFAULT_IFADDR_STR "127.0.0.1"
83 #define DEFAULT_PORT 6698
84 #define DEFAULT_TIMEOUT 0 /* don't wait for traffic */
85 #define RXBUFSIZE 2048
87 static sockunion_t basegroup;
88 static const char *basegroup_str = NULL;
89 static int dobindaddr = 0;
90 static int dodebug = 1;
91 static int doipv4 = 0;
92 static int domiscopts = 0;
93 static int dorandom = 0;
94 static int doreuseport = 0;
97 static int doverbose = 0;
98 static sockunion_t ifaddr;
99 static const char *ifaddr_str = NULL;
100 static uint32_t ifindex = 0;
101 static const char *ifname = NULL;
102 struct in_addr *ipv4_sources = NULL;
103 static jmp_buf jmpbuf;
104 static size_t nmcastgroups = IP_MAX_MEMBERSHIPS;
105 static size_t nmcastsources = 0;
106 static uint16_t portno = DEFAULT_PORT;
107 static char *progname = NULL;
108 struct sockaddr_storage *ss_sources = NULL;
109 static uint32_t timeout = 0;
111 static int do_asm_ipv4(void);
112 static int do_asm_pim(void);
114 static int do_misc_opts(void);
116 static int do_ssf_ipv4(void);
117 static int do_ssf_pim(void);
118 static int do_ssm_ipv4(void);
119 static int do_ssm_pim(void);
120 static int open_and_bind_socket(sockunion_t *);
121 static int recv_loop_with_match(int, sockunion_t *, sockunion_t *);
122 static void signal_handler(int);
123 static void usage(void);
126 * Test the IPv4 set/getipv4sourcefilter() libc API functions.
127 * Build a single socket.
128 * Join a source group.
129 * Repeatedly change the source filters via setipv4sourcefilter.
130 * Read it back with getipv4sourcefilter up to IP_MAX_SOURCES
131 * and check for inconsistency.
137 fprintf(stderr, "not yet implemented\n");
142 * Test the protocol-independent set/getsourcefilter() functions.
148 fprintf(stderr, "not yet implemented\n");
153 * Test the IPv4 ASM API.
154 * Repeatedly join, block sources, unblock and leave groups.
160 char gaddrbuf[ADDRBUF_LEN];
164 struct ip_mreq_source mreqs;
165 in_addr_t ngroupbase;
166 char saddrbuf[ADDRBUF_LEN];
168 sockunion_t tmpgroup;
169 sockunion_t tmpsource;
171 memset(&mreq, 0, sizeof(struct ip_mreq));
172 memset(&mreqs, 0, sizeof(struct ip_mreq_source));
173 memset(&laddr, 0, sizeof(sockunion_t));
178 laddr.sin.sin_family = AF_INET;
179 laddr.sin.sin_len = sizeof(struct sockaddr_in);
180 laddr.sin.sin_addr.s_addr = INADDR_ANY;
182 laddr.sin.sin_port = htons(portno);
184 tmpgroup = basegroup;
185 ngroupbase = ntohl(basegroup.sin.sin_addr.s_addr) + 1; /* XXX */
186 tmpgroup.sin.sin_addr.s_addr = htonl(ngroupbase);
188 sock = open_and_bind_socket(&laddr);
192 for (i = 0; i < (signed)nmcastgroups; i++) {
193 mreq.imr_multiaddr.s_addr = htonl((ngroupbase + i));
194 mreq.imr_interface = ifaddr.sin.sin_addr;
196 inet_ntop(AF_INET, &mreq.imr_multiaddr, gaddrbuf,
198 fprintf(stderr, "IP_ADD_MEMBERSHIP %s %s\n",
199 gaddrbuf, inet_ntoa(mreq.imr_interface));
201 error = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
202 &mreq, sizeof(struct ip_mreq));
204 warn("setsockopt IP_ADD_MEMBERSHIP");
211 * If no test sources auto-generated or specified on command line,
212 * skip source filter portion of ASM test.
214 if (nmcastsources == 0)
218 * Begin blocking sources on the first group chosen.
220 for (i = 0; i < (signed)nmcastsources; i++) {
221 mreqs.imr_multiaddr = tmpgroup.sin.sin_addr;
222 mreqs.imr_interface = ifaddr.sin.sin_addr;
223 mreqs.imr_sourceaddr = ipv4_sources[i];
225 inet_ntop(AF_INET, &mreqs.imr_multiaddr, gaddrbuf,
227 inet_ntop(AF_INET, &mreqs.imr_sourceaddr, saddrbuf,
229 fprintf(stderr, "IP_BLOCK_SOURCE %s %s %s\n",
230 gaddrbuf, inet_ntoa(mreqs.imr_interface),
233 error = setsockopt(sock, IPPROTO_IP, IP_BLOCK_SOURCE, &mreqs,
234 sizeof(struct ip_mreq_source));
236 warn("setsockopt IP_BLOCK_SOURCE");
243 * Choose the first group and source for a match.
244 * Enter the I/O loop.
246 memset(&tmpsource, 0, sizeof(sockunion_t));
247 tmpsource.sin.sin_family = AF_INET;
248 tmpsource.sin.sin_len = sizeof(struct sockaddr_in);
249 tmpsource.sin.sin_addr = ipv4_sources[0];
251 error = recv_loop_with_match(sock, &tmpgroup, &tmpsource);
256 for (i = nmcastsources-1; i >= 0; i--) {
257 mreqs.imr_multiaddr = tmpgroup.sin.sin_addr;
258 mreqs.imr_interface = ifaddr.sin.sin_addr;
259 mreqs.imr_sourceaddr = ipv4_sources[i];
261 inet_ntop(AF_INET, &mreqs.imr_multiaddr, gaddrbuf,
263 inet_ntop(AF_INET, &mreqs.imr_sourceaddr, saddrbuf,
265 fprintf(stderr, "IP_UNBLOCK_SOURCE %s %s %s\n",
266 gaddrbuf, inet_ntoa(mreqs.imr_interface),
269 error = setsockopt(sock, IPPROTO_IP, IP_UNBLOCK_SOURCE, &mreqs,
270 sizeof(struct ip_mreq_source));
272 warn("setsockopt IP_UNBLOCK_SOURCE");
282 for (i = nmcastgroups-1; i >= 0; i--) {
283 mreq.imr_multiaddr.s_addr = htonl((ngroupbase + i));
284 mreq.imr_interface = ifaddr.sin.sin_addr;
286 inet_ntop(AF_INET, &mreq.imr_multiaddr, gaddrbuf,
288 fprintf(stderr, "IP_DROP_MEMBERSHIP %s %s\n",
289 gaddrbuf, inet_ntoa(mreq.imr_interface));
291 error = setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP,
292 &mreq, sizeof(struct ip_mreq));
294 warn("setsockopt IP_DROP_MEMBERSHIP");
307 fprintf(stderr, "not yet implemented\n");
313 * Test misceallaneous IPv4 options.
320 sock = open_and_bind_socket(NULL);
323 test_ip_uchar(sock, socktypename, IP_MULTICAST_TTL,
324 "IP_MULTICAST_TTL", 1);
327 sock = open_and_bind_socket(NULL);
330 test_ip_boolean(sock, socktypename, IP_MULTICAST_LOOP,
331 "IP_MULTICAST_LOOP", 1, BOOLEAN_ANYONE);
339 * Test the IPv4 SSM API.
345 fprintf(stderr, "not yet implemented\n");
350 * Test the protocol-independent SSM API with IPv4 addresses.
356 fprintf(stderr, "not yet implemented\n");
361 main(int argc, char *argv[])
364 struct addrinfo *aip;
369 struct in_addr *pina;
370 struct sockaddr_storage *pbss;
372 ifname = DEFAULT_IFNAME;
373 ifaddr_str = DEFAULT_IFADDR_STR;
374 basegroup_str = DEFAULT_GROUP_STR;
375 ifname = DEFAULT_IFNAME;
376 portno = DEFAULT_PORT;
377 basegroup.ss.ss_family = AF_UNSPEC;
378 ifaddr.ss.ss_family = AF_UNSPEC;
380 progname = basename(argv[0]);
381 while ((ch = getopt(argc, argv, "4bg:i:I:mM:p:rsS:tT:v")) != -1) {
390 basegroup_str = optarg;
399 usage(); /* notyet */
404 nmcastgroups = atoi(optarg);
407 portno = atoi(optarg);
413 nmcastsources = atoi(optarg);
422 timeout = atoi(optarg);
436 memset(&aih, 0, sizeof(struct addrinfo));
437 aih.ai_flags = AI_NUMERICHOST | AI_PASSIVE;
438 aih.ai_family = PF_INET;
439 aih.ai_socktype = SOCK_DGRAM;
440 aih.ai_protocol = IPPROTO_UDP;
443 * Fill out base group.
446 error = getaddrinfo(basegroup_str, NULL, &aih, &aip);
448 fprintf(stderr, "%s: getaddrinfo: %s\n", progname,
449 gai_strerror(error));
452 memcpy(&basegroup, aip->ai_addr, aip->ai_addrlen);
454 fprintf(stderr, "debug: gai thinks %s is %s\n",
455 basegroup_str, inet_ntoa(basegroup.sin.sin_addr));
459 assert(basegroup.ss.ss_family == AF_INET);
462 * If user specified interface as an address, and protocol
463 * specific APIs were selected, parse it.
464 * Otherwise, parse interface index from name if protocol
465 * independent APIs were selected (the default).
468 if (ifaddr_str == NULL) {
469 warnx("required argument missing: ifaddr");
474 error = getaddrinfo(ifaddr_str, NULL, &aih, &aip);
476 fprintf(stderr, "%s: getaddrinfo: %s\n", progname,
477 gai_strerror(error));
480 memcpy(&ifaddr, aip->ai_addr, aip->ai_addrlen);
482 fprintf(stderr, "debug: gai thinks %s is %s\n",
483 ifaddr_str, inet_ntoa(ifaddr.sin.sin_addr));
489 if (ifname == NULL) {
490 warnx("required argument missing: ifname");
494 ifindex = if_nametoindex(ifname);
496 err(EX_USAGE, "if_nametoindex");
500 * Introduce randomness into group base if specified.
503 in_addr_t ngroupbase;
506 ngroupbase = ntohl(basegroup.sin.sin_addr.s_addr);
507 ngroupbase |= ((random() % ((1 << 11) - 1)) << 16);
508 basegroup.sin.sin_addr.s_addr = htonl(ngroupbase);
512 nmcastsources = argc;
514 ipv4_sources = calloc(nmcastsources,
515 sizeof(struct in_addr));
516 if (ipv4_sources == NULL) {
521 ss_sources = calloc(nmcastsources,
522 sizeof(struct sockaddr_storage));
523 if (ss_sources == NULL) {
531 * Parse source list, if any were specified on the command line.
533 assert(aih.ai_family == PF_INET);
536 for (i = 0; i < (size_t)argc; i++) {
538 error = getaddrinfo(argv[i], NULL, &aih, &aip);
540 fprintf(stderr, "getaddrinfo: %s\n",
541 gai_strerror(error));
546 struct sockaddr_in *sin =
547 (struct sockaddr_in *)aip->ai_addr;
548 *pina++ = sin->sin_addr;
550 memcpy(pbss++, aip->ai_addr, aip->ai_addrlen);
556 * Perform the regression tests which the user requested.
560 exitval = do_misc_opts();
566 /* IPv4 protocol specific API tests */
568 /* Source-specific multicast */
569 exitval = do_ssm_ipv4();
573 /* Do setipvsourcefilter() too */
574 exitval = do_ssf_ipv4();
577 /* Any-source multicast */
578 exitval = do_asm_ipv4();
581 /* Protocol independent API tests */
583 /* Source-specific multicast */
584 exitval = do_ssm_pim();
588 /* Do setsourcefilter() too */
589 exitval = do_ssf_pim();
592 /* Any-source multicast */
593 exitval = do_asm_pim();
598 if (ipv4_sources != NULL)
601 if (ss_sources != NULL)
608 open_and_bind_socket(sockunion_t *bsu)
610 int error, optval, sock;
614 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
622 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &optval,
623 sizeof(optval)) < 0) {
624 warn("setsockopt SO_REUSEPORT");
631 error = bind(sock, &bsu->sa, bsu->sa.sa_len);
643 * Protocol-agnostic multicast I/O loop.
645 * Wait for 'timeout' seconds looking for traffic on group, so that manual
646 * or automated regression tests (possibly running on another host) have an
647 * opportunity to transmit within the group to test source filters.
649 * If the filter failed, this loop will report if we received traffic
650 * from the source we elected to monitor.
653 recv_loop_with_match(int sock, sockunion_t *group, sockunion_t *source)
657 char groupname[NI_MAXHOST];
661 char rxbuf[RXBUFSIZE];
662 char sourcename[NI_MAXHOST];
664 assert(source->sa.sa_family == AF_INET);
667 * Return immediately if we don't need to wait for traffic.
672 error = getnameinfo(&group->sa, group->sa.sa_len, groupname,
673 NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
675 fprintf(stderr, "getnameinfo: %s\n", gai_strerror(error));
679 error = getnameinfo(&source->sa, source->sa.sa_len, sourcename,
680 NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
682 fprintf(stderr, "getnameinfo: %s\n", gai_strerror(error));
687 "Waiting %d seconds for inbound traffic on group %s\n"
688 "Expecting no traffic from blocked source: %s\n",
689 (int)timeout, groupname, sourcename);
691 signal(SIGINT, signal_handler);
692 signal(SIGALRM, signal_handler);
697 while (0 == (jmpretval = setjmp(jmpbuf))) {
698 len = recvfrom(sock, rxbuf, RXBUFSIZE, 0, &from.sa,
699 (socklen_t *)&from.sa.sa_len);
701 fprintf(stderr, "debug: packet received from %s\n",
702 inet_ntoa(from.sin.sin_addr));
705 source->sin.sin_addr.s_addr == from.sin.sin_addr.s_addr)
711 fprintf(stderr, "Number of datagrams received from "
712 "non-blocked sources: %d\n", (int)npackets);
716 case SIGALRM: /* ok */
718 case SIGINT: /* go bye bye */
719 fprintf(stderr, "interrupted\n");
722 case 0: /* Broke out of loop; saw a bad source. */
723 fprintf(stderr, "FAIL: got packet from blocked source\n");
732 signal(SIGINT, SIG_DFL);
733 signal(SIGALRM, SIG_DFL);
739 signal_handler(int signo)
742 longjmp(jmpbuf, signo);
749 fprintf(stderr, "\nIP multicast regression test utility\n");
751 "usage: %s [-4] [-b] [-g groupaddr] [-i ifname] [-I ifaddr] [-m]\n"
752 " [-M ngroups] [-p portno] [-r] [-R] [-s] [-S nsources] [-t] [-T timeout]\n"
753 " [-v] [blockaddr ...]\n\n", progname);
754 fprintf(stderr, "-4: Use IPv4 API "
755 "(default: Use protocol-independent API)\n");
756 fprintf(stderr, "-b: bind listening socket to ifaddr "
757 "(default: INADDR_ANY)\n");
758 fprintf(stderr, "-g: Base IPv4 multicast group to join (default: %s)\n",
760 fprintf(stderr, "-i: interface for multicast joins (default: %s)\n",
762 fprintf(stderr, "-I: IPv4 address to join groups on, if using IPv4 "
763 "API\n (default: %s)\n", DEFAULT_IFADDR_STR);
765 fprintf(stderr, "-m: Test misc IPv4 multicast socket options "
768 fprintf(stderr, "-M: Number of multicast groups to join "
769 "(default: %d)\n", (int)nmcastgroups);
770 fprintf(stderr, "-p: Set local and remote port (default: %d)\n",
772 fprintf(stderr, "-r: Set SO_REUSEPORT on (default: off)\n");
773 fprintf(stderr, "-R: Randomize groups/sources (default: off)\n");
774 fprintf(stderr, "-s: Test source-specific API "
775 "(default: test any-source API)\n");
776 fprintf(stderr, "-S: Number of multicast sources to generate if\n"
777 " none specified on command line (default: %d)\n",
779 fprintf(stderr, "-t: Test get/setNsourcefilter() (default: off)\n");
780 fprintf(stderr, "-T: Timeout to wait for blocked traffic on first "
781 "group (default: %d)\n", DEFAULT_TIMEOUT);
782 fprintf(stderr, "-v: Be verbose (default: off)\n");
783 fprintf(stderr, "\nRemaining arguments are treated as a list of IPv4 "
784 "sources to filter.\n\n");