xref: /freebsd/usr.sbin/mtest/mtest.c (revision 076ad2f8)
1 /*-
2  * Copyright (c) 2007-2009 Bruce Simpson.
3  * Copyright (c) 2000 Wilbert De Graaf.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
28  * SUCH DAMAGE.
29  */
30 
31 /*
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.
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/errno.h>
45 #include <sys/socket.h>
46 #include <sys/time.h>
47 #include <sys/ioctl.h>
48 
49 #include <net/if.h>
50 #include <net/if_dl.h>
51 #include <net/ethernet.h>
52 #ifdef INET
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
56 #include <netinet/ip_var.h>
57 #endif
58 #ifdef INET6
59 #include <netinet/in.h>
60 #include <netinet/ip6.h>
61 #endif
62 
63 #include <assert.h>
64 #include <stdlib.h>
65 #include <stdio.h>
66 #include <string.h>
67 #include <ctype.h>
68 #include <errno.h>
69 #include <err.h>
70 #include <unistd.h>
71 
72 #include <arpa/inet.h>
73 #include <netdb.h>
74 #include <ifaddrs.h>
75 
76 union sockunion {
77 	struct sockaddr_storage	ss;
78 	struct sockaddr		sa;
79 	struct sockaddr_dl	sdl;
80 #ifdef INET
81 	struct sockaddr_in	sin;
82 #endif
83 #ifdef INET6
84 	struct sockaddr_in6	sin6;
85 #endif
86 };
87 typedef union sockunion sockunion_t;
88 
89 union mrequnion {
90 #ifdef INET
91 	struct ip_mreq	 	 mr;
92 	struct ip_mreq_source	 mrs;
93 #endif
94 #ifdef INET6
95 	struct ipv6_mreq	 mr6;
96 	struct group_source_req	 gr;
97 #endif
98 };
99 typedef union mrequnion mrequnion_t;
100 
101 #define	MAX_ADDRS	20
102 #define	STR_SIZE	20
103 #define	LINE_LENGTH	80
104 
105 #ifdef INET
106 static int	__ifindex_to_primary_ip(const uint32_t, struct in_addr *);
107 #endif
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);
114 
115 /*
116  * Ordering predicate for qsort().
117  */
118 static int
119 su_cmp(const void *a, const void *b)
120 {
121 	const sockunion_t	*sua = (const sockunion_t *)a;
122 	const sockunion_t	*sub = (const sockunion_t *)b;
123 
124 	assert(sua->sa.sa_family == sub->sa.sa_family);
125 
126 	switch (sua->sa.sa_family) {
127 #ifdef INET
128 	case AF_INET:
129 		return ((int)(sua->sin.sin_addr.s_addr -
130 		    sub->sin.sin_addr.s_addr));
131 		break;
132 #endif
133 #ifdef INET6
134 	case AF_INET6:
135 		return (memcmp(&sua->sin6.sin6_addr, &sub->sin6.sin6_addr,
136 		    sizeof(struct in6_addr)));
137 		break;
138 #endif
139 	default:
140 		break;
141 	}
142 
143 	assert(sua->sa.sa_len == sub->sa.sa_len);
144 	return (memcmp(sua, sub, sua->sa.sa_len));
145 }
146 
147 #ifdef INET
148 /*
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.
153  */
154 static int
155 __ifindex_to_primary_ip(const uint32_t ifindex, struct in_addr *pina)
156 {
157 	char		 ifname[IFNAMSIZ];
158 	struct ifaddrs	*ifa;
159 	struct ifaddrs	*ifaddrs;
160 	sockunion_t	*psu;
161 	int		 retval;
162 
163 	assert(ifindex != 0);
164 
165 	retval = -1;
166 	if (if_indextoname(ifindex, ifname) == NULL)
167 		return (retval);
168 	if (getifaddrs(&ifaddrs) < 0)
169 		return (retval);
170 
171 	/*
172 	 * Find the ifaddr entry corresponding to the interface name,
173 	 * and return the first matching IPv4 address.
174 	 */
175 	retval = 1;
176 	for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
177 		if (strcmp(ifa->ifa_name, ifname) != 0)
178 			continue;
179 		psu = (sockunion_t *)ifa->ifa_addr;
180 		if (psu && psu->sa.sa_family == AF_INET) {
181 			retval = 0;
182 			memcpy(pina, &psu->sin.sin_addr,
183 			    sizeof(struct in_addr));
184 			break;
185 		}
186 	}
187 
188 	if (retval != 0)
189 		errno = EADDRNOTAVAIL;	/* XXX */
190 
191 	freeifaddrs(ifaddrs);
192 	return (retval);
193 }
194 #endif /* INET */
195 
196 int
197 main(int argc, char **argv)
198 {
199 	char	 line[LINE_LENGTH];
200 	char	*p;
201 	int	 i, s, s6;
202 
203 	s = -1;
204 	s6 = -1;
205 #ifdef INET
206 	s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
207 	if (s == -1 && errno != EAFNOSUPPORT)
208 		err(1, "can't open IPv4 socket");
209 #endif
210 #ifdef INET6
211 	s6 = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
212 	if (s6 == -1 && errno != EAFNOSUPPORT)
213 		err(1, "can't open IPv6 socket");
214 #endif
215 	if (s == -1 && s6 == -1)
216 		errc(1, EPROTONOSUPPORT, "can't open socket");
217 
218 	if (argc < 2) {
219 		if (isatty(STDIN_FILENO)) {
220 			printf("multicast membership test program; "
221 			    "enter ? for list of commands\n");
222 		}
223 		do {
224 			if (fgets(line, sizeof(line), stdin) != NULL) {
225 				if (line[0] != 'f')
226 					process_cmd(line, s, s6, stdin);
227 				else {
228 					/* Get the filename */
229 					for (i = 1; isblank(line[i]); i++);
230 					if ((p = (char*)strchr(line, '\n'))
231 					    != NULL)
232 						*p = '\0';
233 					process_file(&line[i], s, s6);
234 				}
235 			}
236 		} while (!feof(stdin));
237 	} else {
238 		for (i = 1; i < argc; i++) {
239 			process_file(argv[i], s, s6);
240 		}
241 	}
242 
243 	if (s != -1)
244 		close(s);
245 	if (s6 != -1)
246 		close(s6);
247 
248 	exit (0);
249 }
250 
251 static void
252 process_file(char *fname, int s, int s6)
253 {
254 	char line[80];
255 	FILE *fp;
256 	char *lineptr;
257 
258 	fp = fopen(fname, "r");
259 	if (fp == NULL) {
260 		warn("fopen");
261 		return;
262 	}
263 
264 	/* Skip comments and empty lines. */
265 	while (fgets(line, sizeof(line), fp) != NULL) {
266 		lineptr = line;
267 		while (isblank(*lineptr))
268 			lineptr++;
269 		if (*lineptr != '#' && *lineptr != '\n')
270 			process_cmd(lineptr, s, s6, fp);
271 	}
272 
273 	fclose(fp);
274 }
275 
276 /*
277  * Parse join/leave/allow/block arguments, given:
278  *  str1: group (as AF_INET or AF_INET6 printable)
279  *  str2: ifname
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.
283  */
284 static uint32_t
285 parse_cmd_args(sockunion_t *psu, sockunion_t *psu2,
286     const char *str1, const char *str2, const char *str3)
287 {
288 	struct addrinfo		 hints;
289 	struct addrinfo		*res;
290 	uint32_t		 ifindex;
291 	int			 af, error;
292 
293 	assert(psu != NULL);
294 	assert(str1 != NULL);
295 	assert(str2 != NULL);
296 
297 	af = AF_UNSPEC;
298 
299 	ifindex = if_nametoindex(str2);
300 	if (ifindex == 0)
301 		return (0);
302 
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;
307 
308 	memset(psu, 0, sizeof(sockunion_t));
309 	psu->sa.sa_family = AF_UNSPEC;
310 
311 	error = getaddrinfo(str1, "0", &hints, &res);
312 	if (error) {
313 		warnx("getaddrinfo: %s", gai_strerror(error));
314 		return (0);
315 	}
316 	assert(res != NULL);
317 	af = res->ai_family;
318 	memcpy(psu, res->ai_addr, res->ai_addrlen);
319 	freeaddrinfo(res);
320 
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;
325 
326 		/* look for following address family; str3 is *optional*. */
327 		hints.ai_family = af;
328 		error = getaddrinfo(str3, "0", &hints, &res);
329 		if (error) {
330 			warnx("getaddrinfo: %s", gai_strerror(error));
331 			ifindex = 0;
332 		} else {
333 			if (af != res->ai_family) {
334 				errno = EINVAL; /* XXX */
335 				ifindex = 0;
336 			}
337 			memcpy(psu2, res->ai_addr, res->ai_addrlen);
338 			freeaddrinfo(res);
339 		}
340 	}
341 
342 	return (ifindex);
343 }
344 
345 static __inline int
346 af2sock(const int af, int s, int s6)
347 {
348 
349 #ifdef INET
350 	if (af == AF_INET)
351 		return (s);
352 #endif
353 #ifdef INET6
354 	if (af == AF_INET6)
355 		return (s6);
356 #endif
357 	return (-1);
358 }
359 
360 static __inline int
361 af2socklen(const int af)
362 {
363 
364 #ifdef INET
365 	if (af == AF_INET)
366 		return (sizeof(struct sockaddr_in));
367 #endif
368 #ifdef INET6
369 	if (af == AF_INET6)
370 		return (sizeof(struct sockaddr_in6));
371 #endif
372 	return (-1);
373 }
374 
375 static void
376 process_cmd(char *cmd, int s, int s6, FILE *fp __unused)
377 {
378 	char			 str1[STR_SIZE];
379 	char			 str2[STR_SIZE];
380 	char			 str3[STR_SIZE];
381 	mrequnion_t		 mr;
382 	sockunion_t		 su, su2;
383 	struct ifreq		 ifr;
384 	char			*line;
385 	char			*toptname;
386 	void			*optval;
387 	uint32_t		 fmode, ifindex;
388 	socklen_t		 optlen;
389 	int			 af, error, f, flags, i, level, n, optname;
390 
391 	af = AF_UNSPEC;
392 	su.sa.sa_family = AF_UNSPEC;
393 	su2.sa.sa_family = AF_UNSPEC;
394 
395 	line = cmd;
396 	while (isblank(*++line))
397 		;	/* Skip whitespace. */
398 
399 	n = 0;
400 	switch (*cmd) {
401 	case '?':
402 		usage();
403 		break;
404 
405 	case 'q':
406 		close(s);
407 		exit(0);
408 
409 	case 's':
410 		if ((sscanf(line, "%d", &n) != 1) || (n < 1)) {
411 			printf("-1\n");
412 			break;
413 		}
414 		sleep(n);
415 		printf("ok\n");
416 		break;
417 
418 	case 'j':
419 	case 'l':
420 		str3[0] = '\0';
421 		toptname = "";
422 		sscanf(line, "%s %s %s", str1, str2, str3);
423 		ifindex = parse_cmd_args(&su, &su2, str1, str2, str3);
424 		if (ifindex == 0) {
425 			printf("-1\n");
426 			break;
427 		}
428 		af = su.sa.sa_family;
429 #ifdef INET
430 		if (af == AF_INET) {
431 			struct in_addr ina;
432 
433 			error = __ifindex_to_primary_ip(ifindex, &ina);
434 			if (error != 0) {
435 				warn("primary_ip_lookup %s", str2);
436 				printf("-1\n");
437 				break;
438 			}
439 			level = IPPROTO_IP;
440 
441 			if (su2.sa.sa_family != AF_UNSPEC) {
442 				mr.mrs.imr_multiaddr = su.sin.sin_addr;
443 				mr.mrs.imr_sourceaddr = su2.sin.sin_addr;
444 				mr.mrs.imr_interface = ina;
445 				optname = (*cmd == 'j') ?
446 				    IP_ADD_SOURCE_MEMBERSHIP :
447 				    IP_DROP_SOURCE_MEMBERSHIP;
448 				toptname = (*cmd == 'j') ?
449 				    "IP_ADD_SOURCE_MEMBERSHIP" :
450 				    "IP_DROP_SOURCE_MEMBERSHIP";
451 				optval = (void *)&mr.mrs;
452 				optlen = sizeof(mr.mrs);
453 			} else {
454 				mr.mr.imr_multiaddr = su.sin.sin_addr;
455 				mr.mr.imr_interface = ina;
456 				optname = (*cmd == 'j') ?
457 				    IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP;
458 				toptname = (*cmd == 'j') ?
459 				    "IP_ADD_MEMBERSHIP" : "IP_DROP_MEMBERSHIP";
460 				optval = (void *)&mr.mr;
461 				optlen = sizeof(mr.mr);
462 			}
463 			if (s < 0) {
464 				warnc(EPROTONOSUPPORT, "setsockopt %s",
465 				    toptname);
466 			} else if (setsockopt(s, level, optname, optval,
467 			    optlen) == 0) {
468 				printf("ok\n");
469 				break;
470 			} else {
471 				warn("setsockopt %s", toptname);
472 			}
473 		}
474 #ifdef INET6
475 		else
476 #endif /* INET with INET6 */
477 #endif /* INET */
478 #ifdef INET6
479 		if (af == AF_INET6) {
480 			level = IPPROTO_IPV6;
481 			if (su2.sa.sa_family != AF_UNSPEC) {
482 				mr.gr.gsr_interface = ifindex;
483 				mr.gr.gsr_group = su.ss;
484 				mr.gr.gsr_source = su2.ss;
485 				optname = (*cmd == 'j') ?
486 				    MCAST_JOIN_SOURCE_GROUP:
487 				    MCAST_LEAVE_SOURCE_GROUP;
488 				toptname = (*cmd == 'j') ?
489 				    "MCAST_JOIN_SOURCE_GROUP":
490 				    "MCAST_LEAVE_SOURCE_GROUP";
491 				optval = (void *)&mr.gr;
492 				optlen = sizeof(mr.gr);
493 			} else {
494 				mr.mr6.ipv6mr_multiaddr = su.sin6.sin6_addr;
495 				mr.mr6.ipv6mr_interface = ifindex;
496 				optname = (*cmd == 'j') ?
497 				    IPV6_JOIN_GROUP :
498 				    IPV6_LEAVE_GROUP;
499 				toptname = (*cmd == 'j') ?
500 				    "IPV6_JOIN_GROUP" :
501 				    "IPV6_LEAVE_GROUP";
502 				optval = (void *)&mr.mr6;
503 				optlen = sizeof(mr.mr6);
504 			}
505 			if (s6 < 0) {
506 				warnc(EPROTONOSUPPORT, "setsockopt %s",
507 				    toptname);
508 			} else if (setsockopt(s6, level, optname, optval,
509 			    optlen) == 0) {
510 				printf("ok\n");
511 				break;
512 			} else {
513 				warn("setsockopt %s", toptname);
514 			}
515 		}
516 #endif /* INET6 */
517 		/* FALLTHROUGH */
518 		printf("-1\n");
519 		break;
520 
521 	/*
522 	 * Set the socket to include or exclude filter mode, and
523 	 * add some sources to the filterlist, using the full-state API.
524 	 */
525 	case 'i':
526 	case 'e': {
527 		sockunion_t	 sources[MAX_ADDRS];
528 		struct addrinfo	 hints;
529 		struct addrinfo	*res;
530 		char		*cp;
531 		int		 af1;
532 
533 		n = 0;
534 		fmode = (*cmd == 'i') ? MCAST_INCLUDE : MCAST_EXCLUDE;
535 		if ((sscanf(line, "%s %s %d", str1, str2, &n)) != 3) {
536 			printf("-1\n");
537 			break;
538 		}
539 
540 		ifindex = parse_cmd_args(&su, NULL, str1, str2, NULL);
541 		if (ifindex == 0 || n < 0 || n > MAX_ADDRS) {
542 			printf("-1\n");
543 			break;
544 		}
545 		af = su.sa.sa_family;
546 		if (af2sock(af, s, s6) == -1) {
547 			warnc(EPROTONOSUPPORT, "setsourcefilter");
548 			break;
549 		}
550 
551 		memset(&hints, 0, sizeof(struct addrinfo));
552 		hints.ai_flags = AI_NUMERICHOST;
553 		hints.ai_family = af;
554 		hints.ai_socktype = SOCK_DGRAM;
555 
556 		for (i = 0; i < n; i++) {
557 			sockunion_t *psu = (sockunion_t *)&sources[i];
558 			/*
559 			 * Trim trailing whitespace, as getaddrinfo()
560 			 * can't cope with it.
561 			 */
562 			fgets(str1, sizeof(str1), fp);
563 			cp = strchr(str1, '\n');
564 			if (cp != NULL)
565 				*cp = '\0';
566 
567 			res = NULL;
568 			error = getaddrinfo(str1, "0", &hints, &res);
569 			if (error)
570 				break;
571 			assert(res != NULL);
572 
573 			memset(psu, 0, sizeof(sockunion_t));
574 			af1 = res->ai_family;
575 			if (af1 == af)
576 				memcpy(psu, res->ai_addr, res->ai_addrlen);
577 			freeaddrinfo(res);
578 			if (af1 != af)
579 				break;
580 		}
581 		if (i < n) {
582 			if (error)
583 				warnx("getaddrinfo: %s", gai_strerror(error));
584 			printf("-1\n");
585 			break;
586 		}
587 		if (setsourcefilter(af2sock(af, s, s6), ifindex,
588 		    &su.sa, su.sa.sa_len, fmode, n, &sources[0].ss) != 0)
589 			warn("setsourcefilter");
590 		else
591 			printf("ok\n");
592 	} break;
593 
594 	/*
595 	 * Allow or block traffic from a source, using the
596 	 * delta based api.
597 	 */
598 	case 't':
599 	case 'b': {
600 		str3[0] = '\0';
601 		toptname = "";
602 		sscanf(line, "%s %s %s", str1, str2, str3);
603 		ifindex = parse_cmd_args(&su, &su2, str1, str2, str3);
604 		if (ifindex == 0 || su2.sa.sa_family == AF_UNSPEC) {
605 			printf("-1\n");
606 			break;
607 		}
608 		af = su.sa.sa_family;
609 		if (af2sock(af, s, s6) == -1) {
610 			warnc(EPROTONOSUPPORT, "getsourcefilter");
611 			break;
612 		}
613 
614 		/* 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");
618 			break;
619 		}
620 #ifdef INET
621 		if (af == AF_INET) {
622 			struct in_addr ina;
623 
624 			error = __ifindex_to_primary_ip(ifindex, &ina);
625 			if (error != 0) {
626 				warn("primary_ip_lookup %s", str2);
627 				printf("-1\n");
628 				break;
629 			}
630 			level = IPPROTO_IP;
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') ?
639 				    IP_UNBLOCK_SOURCE :
640 				    IP_BLOCK_SOURCE;
641 				toptname = (*cmd == 't') ?
642 				    "IP_UNBLOCK_SOURCE" :
643 				    "IP_BLOCK_SOURCE";
644 			} else {
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";
652 			}
653 			if (setsockopt(s, level, optname, optval,
654 			    optlen) == 0) {
655 				printf("ok\n");
656 				break;
657 			} else {
658 				warn("setsockopt %s", toptname);
659 			}
660 		}
661 #ifdef INET6
662 		else
663 #endif /* INET with INET6 */
664 #endif /* INET */
665 #ifdef 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 :
675 				    MCAST_BLOCK_SOURCE;
676 				toptname = (*cmd == 't') ?
677 				    "MCAST_UNBLOCK_SOURCE" :
678 				    "MCAST_BLOCK_SOURCE";
679 			} else {
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";
687 			}
688 			optval = (void *)&mr.gr;
689 			optlen = sizeof(mr.gr);
690 			if (setsockopt(s6, level, optname, optval,
691 			    optlen) == 0) {
692 				printf("ok\n");
693 				break;
694 			} else {
695 				warn("setsockopt %s", toptname);
696 			}
697 		}
698 #endif /* INET6 */
699 		/* FALLTHROUGH */
700 		printf("-1\n");
701 	} break;
702 
703 	case 'g': {
704 		sockunion_t	 sources[MAX_ADDRS];
705 		char		 addrbuf[NI_MAXHOST];
706 		int		 nreqsrc, nsrc;
707 
708 		if ((sscanf(line, "%s %s %d", str1, str2, &nreqsrc)) != 3) {
709 			printf("-1\n");
710 			break;
711 		}
712 		ifindex = parse_cmd_args(&su, NULL, str1, str2, NULL);
713 		if (ifindex == 0 || (n < 0 || n > MAX_ADDRS)) {
714 			printf("-1\n");
715 			break;
716 		}
717 
718 		af = su.sa.sa_family;
719 		if (af2sock(af, s, s6) == -1) {
720 			warnc(EPROTONOSUPPORT, "getsourcefilter");
721 			break;
722 		}
723 		nsrc = nreqsrc;
724 		if (getsourcefilter(af2sock(af, s, s6), ifindex, &su.sa,
725 		    su.sa.sa_len, &fmode, &nsrc, &sources[0].ss) != 0) {
726 			warn("getsourcefilter");
727 			printf("-1\n");
728 			break;
729 		}
730 		printf("%s\n", (fmode == MCAST_INCLUDE) ? "include" :
731 		    "exclude");
732 		printf("%d\n", nsrc);
733 
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]);
739 		}
740 		fprintf(stderr, "\nend hexdump\n");
741 
742 		qsort(sources, nsrc, af2socklen(af), su_cmp);
743 		for (i = 0; i < nsrc; i++) {
744 			sockunion_t *psu = (sockunion_t *)&sources[i];
745 			addrbuf[0] = '\0';
746 			error = getnameinfo(&psu->sa, psu->sa.sa_len,
747 			    addrbuf, sizeof(addrbuf), NULL, 0,
748 			    NI_NUMERICHOST);
749 			if (error)
750 				warnx("getnameinfo: %s", gai_strerror(error));
751 			else
752 				printf("%s\n", addrbuf);
753 		}
754 		printf("ok\n");
755 	} break;
756 
757 	/* link-layer stuff follows. */
758 
759 	case 'a':
760 	case 'd': {
761 		struct sockaddr_dl	*dlp;
762 		struct ether_addr	*ep;
763 
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;
768 		dlp->sdl_index = 0;
769 		dlp->sdl_nlen = 0;
770 		dlp->sdl_alen = ETHER_ADDR_LEN;
771 		dlp->sdl_slen = 0;
772 		if (sscanf(line, "%s %s", str1, str2) != 2) {
773 			warnc(EINVAL, "sscanf");
774 			break;
775 		}
776 		ep = ether_aton(str2);
777 		if (ep == NULL) {
778 			warnc(EINVAL, "ether_aton");
779 			break;
780 		}
781 		strlcpy(ifr.ifr_name, str1, IF_NAMESIZE);
782 		memcpy(LLADDR(dlp), ep, ETHER_ADDR_LEN);
783 		if (ioctl(s, (*cmd == 'a') ? SIOCADDMULTI : SIOCDELMULTI,
784 		    &ifr) == -1) {
785 			warn("ioctl SIOCADDMULTI/SIOCDELMULTI");
786 			printf("-1\n");
787 		} else
788 			printf("ok\n");
789 		break;
790 	}
791 
792 	case 'm':
793 		fprintf(stderr,
794 		    "warning: IFF_ALLMULTI cannot be set from userland "
795 		    "in FreeBSD; command ignored.\n");
796 		printf("-1\n");
797 		break;
798 
799 	case 'p':
800 		if (sscanf(line, "%s %u", ifr.ifr_name, &f) != 2) {
801 			printf("-1\n");
802 			break;
803 		}
804 		if (ioctl(s, SIOCGIFFLAGS, &ifr) == -1) {
805 			warn("ioctl SIOCGIFFLAGS");
806 			break;
807 		}
808 		flags = (ifr.ifr_flags & 0xffff) | (ifr.ifr_flagshigh << 16);
809 		if (f == 0) {
810 			flags &= ~IFF_PPROMISC;
811 		} else {
812 			flags |= IFF_PPROMISC;
813 		}
814 		ifr.ifr_flags = flags & 0xffff;
815 		ifr.ifr_flagshigh = flags >> 16;
816 		if (ioctl(s, SIOCSIFFLAGS, &ifr) == -1)
817 			warn("ioctl SIOCGIFFLAGS");
818 		else
819 			printf( "changed to 0x%08x\n", flags );
820 		break;
821 
822 	case '\n':
823 		break;
824 	default:
825 		printf("invalid command\n");
826 		break;
827 	}
828 }
829 
830 static void
831 usage(void)
832 {
833 
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");
836 	printf(
837 "i mcast-addr ifname n          - set n include mode src filter\n");
838 	printf(
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");
850 }
851 
852