2 * Copyright (c) 2007 Bruce M. 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 IPv4 multicast sockets.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/types.h>
39 #include <sys/errno.h>
40 #include <sys/socket.h>
42 #include <sys/ioctl.h>
45 #include <net/if_dl.h>
46 #include <net/ethernet.h>
47 #include <netinet/in.h>
49 #include <arpa/inet.h>
58 static void process_file(char *, int);
59 static void process_cmd(char*, int, FILE *fp);
60 static void usage(void);
62 static int inaddr_cmp(const void *a, const void *b);
67 #define LINE_LENGTH 80
70 main(int argc, char **argv)
72 char line[LINE_LENGTH];
76 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
78 err(1, "can't open socket");
81 if (isatty(STDIN_FILENO)) {
82 printf("multicast membership test program; "
83 "enter ? for list of commands\n");
86 if (fgets(line, sizeof(line), stdin) != NULL) {
88 process_cmd(line, s, stdin);
90 /* Get the filename */
91 for (i = 1; isblank(line[i]); i++);
92 if ((p = (char*)strchr(line, '\n'))
95 process_file(&line[i], s);
98 } while (!feof(stdin));
100 for (i = 1; i < argc; i++) {
101 process_file(argv[i], s);
109 process_file(char *fname, int s)
115 fp = fopen(fname, "r");
121 /* Skip comments and empty lines. */
122 while (fgets(line, sizeof(line), fp) != NULL) {
124 while (isblank(*lineptr))
126 if (*lineptr != '#' && *lineptr != '\n')
127 process_cmd(lineptr, s, fp);
134 process_cmd(char *cmd, int s, FILE *fp __unused)
140 char filtbuf[IP_MSFILTER_SIZE(MAX_ADDRS)];
145 struct ip_mreq_source imrs;
146 struct ip_msfilter *imsfp;
149 int n, opt, f, flags;
152 while (isblank(*++line))
153 ; /* Skip whitespace. */
165 if ((sscanf(line, "%d", &n) != 1) || (n < 1)) {
175 sscanf(line, "%s %s", str1, str2);
176 if (((imr.imr_multiaddr.s_addr = inet_addr(str1)) ==
178 ((imr.imr_interface.s_addr = inet_addr(str2)) ==
183 opt = (*cmd == 'j') ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP;
184 if (setsockopt( s, IPPROTO_IP, opt, &imr,
186 warn("setsockopt IP_ADD_MEMBERSHIP/IP_DROP_MEMBERSHIP");
193 struct sockaddr_dl *dlp;
194 struct ether_addr *ep;
196 memset(&ifr, 0, sizeof(struct ifreq));
197 dlp = (struct sockaddr_dl *)&ifr.ifr_addr;
198 dlp->sdl_len = sizeof(struct sockaddr_dl);
199 dlp->sdl_family = AF_LINK;
202 dlp->sdl_alen = ETHER_ADDR_LEN;
204 if (sscanf(line, "%s %s", str1, str2) != 2) {
205 warnc(EINVAL, "sscanf");
208 ep = ether_aton(str2);
210 warnc(EINVAL, "ether_aton");
213 strlcpy(ifr.ifr_name, str1, IF_NAMESIZE);
214 memcpy(LLADDR(dlp), ep, ETHER_ADDR_LEN);
215 if (ioctl(s, (*cmd == 'a') ? SIOCADDMULTI : SIOCDELMULTI,
217 warn("ioctl SIOCADDMULTI/SIOCDELMULTI");
224 printf("warning: IFF_ALLMULTI cannot be set from userland "
225 "in FreeBSD; command ignored.\n");
228 if (sscanf(line, "%s %u", ifr.ifr_name, &f) != 2) {
232 if (ioctl(s, SIOCGIFFLAGS, &ifr) == -1) {
233 warn("ioctl SIOCGIFFLAGS");
236 flags = (ifr.ifr_flags & 0xffff) | (ifr.ifr_flagshigh << 16);
243 ifr.ifr_flags = flags & 0xffff;
244 ifr.ifr_flagshigh = flags >> 16;
245 if (ioctl(s, SIOCSIFFLAGS, &ifr) == -1)
246 warn("ioctl SIOCGIFFLAGS");
248 printf( "changed to 0x%08x\n", flags );
253 * Set the socket to include or exclude filter mode, and
254 * add some sources to the filterlist, using the full-state,
259 if ((sscanf(line, "%s %s %d", str1, str2, &n)) != 3) {
263 imsfp = (struct ip_msfilter *)filtbuf;
264 if (((imsfp->imsf_multiaddr.s_addr = inet_addr(str1)) ==
266 ((imsfp->imsf_interface.s_addr = inet_addr(str2)) ==
267 INADDR_NONE) || (n > MAX_ADDRS)) {
271 imsfp->imsf_fmode = (*cmd == 'i') ? MCAST_INCLUDE :
273 imsfp->imsf_numsrc = n;
274 for (i = 0; i < n; i++) {
275 fgets(str1, sizeof(str1), fp);
276 if ((imsfp->imsf_slist[i].s_addr = inet_addr(str1)) ==
282 if (ioctl(s, SIOCSIPMSFILTER, imsfp) != 0)
283 warn("setsockopt SIOCSIPMSFILTER");
289 * Allow or block traffic from a source, using the
294 sscanf(line, "%s %s %s", str1, str2, str3);
295 if (((imrs.imr_multiaddr.s_addr = inet_addr(str1)) ==
297 ((imrs.imr_interface.s_addr = inet_addr(str2)) ==
299 ((imrs.imr_sourceaddr.s_addr = inet_addr(str3)) ==
305 /* First determine out current filter mode. */
306 imsfp = (struct ip_msfilter *)filtbuf;
307 imsfp->imsf_multiaddr.s_addr = imrs.imr_multiaddr.s_addr;
308 imsfp->imsf_interface.s_addr = imrs.imr_interface.s_addr;
309 imsfp->imsf_numsrc = 5;
310 if (ioctl(s, SIOCSIPMSFILTER, imsfp) != 0) {
311 /* It's only okay for 't' to fail */
313 warn("ioctl SIOCSIPMSFILTER");
316 imsfp->imsf_fmode = MCAST_INCLUDE;
319 if (imsfp->imsf_fmode == MCAST_EXCLUDE) {
321 opt = (*cmd == 't') ? IP_UNBLOCK_SOURCE :
324 /* Controlled source */
325 opt = (*cmd == 't') ? IP_ADD_SOURCE_MEMBERSHIP :
326 IP_DROP_SOURCE_MEMBERSHIP;
328 if (setsockopt(s, IPPROTO_IP, opt, &imrs, sizeof(imrs)) == -1)
329 warn("ioctl IP_ADD_SOURCE_MEMBERSHIP/IP_DROP_SOURCE_MEMBERSHIP/IP_UNBLOCK_SOURCE/IP_BLOCK_SOURCE");
335 if ((sscanf(line, "%s %s %d", str1, str2, &n)) != 3) {
339 imsfp = (struct ip_msfilter *)filtbuf;
340 if (((imsfp->imsf_multiaddr.s_addr = inet_addr(str1)) ==
342 ((imsfp->imsf_interface.s_addr = inet_addr(str2)) ==
343 INADDR_NONE) || (n < 0 || n > MAX_ADDRS)) {
347 imsfp->imsf_numsrc = n;
348 if (ioctl(s, SIOCSIPMSFILTER, imsfp) != 0) {
349 warn("setsockopt SIOCSIPMSFILTER");
352 printf("%s\n", (imsfp->imsf_fmode == MCAST_INCLUDE) ?
353 "include" : "exclude");
354 printf("%d\n", imsfp->imsf_numsrc);
355 if (n >= imsfp->imsf_numsrc) {
356 n = imsfp->imsf_numsrc;
357 qsort(imsfp->imsf_slist, n, sizeof(struct in_addr),
359 for (i = 0; i < n; i++)
360 printf("%s\n", inet_ntoa(imsfp->imsf_slist[i]));
363 #else /* !WITH_IGMPV3 */
369 printf("warning: IGMPv3 is not supported by this version "
370 "of FreeBSD; command ignored.\n");
372 #endif /* WITH_IGMPV3 */
377 printf("invalid command\n");
386 printf("j g.g.g.g i.i.i.i - join IP multicast group\n");
387 printf("l g.g.g.g i.i.i.i - leave IP multicast group\n");
388 printf("a ifname e.e.e.e.e.e - add ether multicast address\n");
389 printf("d ifname e.e.e.e.e.e - delete ether multicast address\n");
390 printf("m ifname 1/0 - set/clear ether allmulti flag\n");
391 printf("p ifname 1/0 - set/clear ether promisc flag\n");
393 printf("i g.g.g.g i.i.i.i n - set n include mode src filter\n");
394 printf("e g.g.g.g i.i.i.i n - set n exclude mode src filter\n");
395 printf("t g.g.g.g i.i.i.i s.s.s.s - allow traffic from src\n");
396 printf("b g.g.g.g i.i.i.i s.s.s.s - block traffic from src\n");
397 printf("g g.g.g.g i.i.i.i n - get and show n src filters\n");
399 printf("f filename - read command(s) from file\n");
400 printf("s seconds - sleep for some time\n");
401 printf("q - quit\n");
406 inaddr_cmp(const void *a, const void *b)
408 return((int)((const struct in_addr *)a)->s_addr -
409 ((const struct in_addr *)b)->s_addr);