1/*	$KAME: mld6.c,v 1.15 2003/04/02 11:29:54 suz Exp $	*/
2
3/*
4 * Copyright (C) 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD$");
34
35#include <sys/param.h>
36#include <sys/uio.h>
37#include <sys/socket.h>
38#include <sys/types.h>
39#include <sys/time.h>
40#include <ifaddrs.h>
41#include <unistd.h>
42#include <signal.h>
43
44#include <net/if.h>
45#include <net/if_var.h>
46
47#include <netinet/in.h>
48#include <netinet/ip6.h>
49#include <netinet/icmp6.h>
50
51#include  <arpa/inet.h>
52
53#include <stdlib.h>
54#include <stdio.h>
55#include <string.h>
56#include <err.h>
57
58/* portability with older KAME headers */
59#ifndef MLD_LISTENER_QUERY
60#define MLD_LISTENER_QUERY	MLD6_LISTENER_QUERY
61#define MLD_LISTENER_REPORT	MLD6_LISTENER_REPORT
62#define MLD_LISTENER_DONE	MLD6_LISTENER_DONE
63#define MLD_MTRACE_RESP		MLD6_MTRACE_RESP
64#define MLD_MTRACE		MLD6_MTRACE
65#define mld_hdr		mld6_hdr
66#define mld_type	mld6_type
67#define mld_code	mld6_code
68#define mld_cksum	mld6_cksum
69#define mld_maxdelay	mld6_maxdelay
70#define mld_reserved	mld6_reserved
71#define mld_addr	mld6_addr
72#endif
73#ifndef IP6OPT_ROUTER_ALERT
74#define IP6OPT_ROUTER_ALERT	IP6OPT_RTALERT
75#endif
76
77struct msghdr m;
78struct sockaddr_in6 dst;
79struct mld_hdr mldh;
80struct in6_addr maddr = IN6ADDR_ANY_INIT, any = IN6ADDR_ANY_INIT;
81struct ipv6_mreq mreq;
82u_short ifindex;
83int s;
84
85#define QUERY_RESPONSE_INTERVAL 10000
86
87void make_msg(int index, struct in6_addr *addr, u_int type);
88void usage(void);
89void dump(int);
90void quit(int);
91
92int
93main(int argc, char *argv[])
94{
95	int i;
96	struct icmp6_filter filt;
97	u_int hlim = 1;
98	fd_set fdset;
99	struct itimerval itimer;
100	u_int type;
101	int ch;
102
103	type = MLD_LISTENER_QUERY;
104	while ((ch = getopt(argc, argv, "dr")) != -1) {
105		switch (ch) {
106		case 'd':
107			type = MLD_LISTENER_DONE;
108			break;
109		case 'r':
110			type = MLD_LISTENER_REPORT;
111			break;
112		default:
113			usage();
114			/*NOTREACHED*/
115		}
116	}
117
118	argv += optind;
119	argc -= optind;
120
121	if (argc != 1 && argc != 2)
122		usage();
123
124	ifindex = (u_short)if_nametoindex(argv[0]);
125	if (ifindex == 0)
126		usage();
127	if (argc == 2 && inet_pton(AF_INET6, argv[1], &maddr) != 1)
128		usage();
129
130	if ((s = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
131		err(1, "socket");
132
133	if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hlim,
134		       sizeof(hlim)) == -1)
135		err(1, "setsockopt(IPV6_MULTICAST_HOPS)");
136
137	mreq.ipv6mr_multiaddr = any;
138	mreq.ipv6mr_interface = ifindex;
139	if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
140		       sizeof(mreq)) == -1)
141		err(1, "setsockopt(IPV6_JOIN_GROUP)");
142
143	ICMP6_FILTER_SETBLOCKALL(&filt);
144	ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_QUERY, &filt);
145	ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REPORT, &filt);
146	ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REDUCTION, &filt);
147	if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
148			sizeof(filt)) < 0)
149		err(1, "setsockopt(ICMP6_FILTER)");
150
151	make_msg(ifindex, &maddr, type);
152
153	if (sendmsg(s, &m, 0) < 0)
154		err(1, "sendmsg");
155
156	itimer.it_value.tv_sec =  QUERY_RESPONSE_INTERVAL / 1000;
157	itimer.it_interval.tv_sec = 0;
158	itimer.it_interval.tv_usec = 0;
159	itimer.it_value.tv_usec = 0;
160
161	(void)signal(SIGALRM, quit);
162	(void)setitimer(ITIMER_REAL, &itimer, NULL);
163
164	FD_ZERO(&fdset);
165	if (s >= FD_SETSIZE)
166		errx(1, "descriptor too big");
167	for (;;) {
168		FD_SET(s, &fdset);
169		if ((i = select(s + 1, &fdset, NULL, NULL, NULL)) < 0)
170			perror("select");
171		if (i == 0)
172			continue;
173		else
174			dump(s);
175	}
176}
177
178void
179make_msg(int index, struct in6_addr *addr, u_int type)
180{
181	static struct iovec iov[2];
182	static u_char *cmsgbuf;
183	int cmsglen, hbhlen = 0;
184#ifdef USE_RFC2292BIS
185	void *hbhbuf = NULL, *optp = NULL;
186	int currentlen;
187#else
188	u_int8_t raopt[IP6OPT_RTALERT_LEN];
189#endif
190	struct in6_pktinfo *pi;
191	struct cmsghdr *cmsgp;
192	u_short rtalert_code = htons(IP6OPT_RTALERT_MLD);
193	struct ifaddrs *ifa, *ifap;
194	struct in6_addr src;
195
196	dst.sin6_len = sizeof(dst);
197	dst.sin6_family = AF_INET6;
198	if (IN6_IS_ADDR_UNSPECIFIED(addr)) {
199		if (inet_pton(AF_INET6, "ff02::1", &dst.sin6_addr) != 1)
200			errx(1, "inet_pton failed");
201	}
202	else
203		dst.sin6_addr = *addr;
204	m.msg_name = (caddr_t)&dst;
205	m.msg_namelen = dst.sin6_len;
206	iov[0].iov_base = (caddr_t)&mldh;
207	iov[0].iov_len = sizeof(mldh);
208	m.msg_iov = iov;
209	m.msg_iovlen = 1;
210
211	bzero(&mldh, sizeof(mldh));
212	mldh.mld_type = type & 0xff;
213	mldh.mld_maxdelay = htons(QUERY_RESPONSE_INTERVAL);
214	mldh.mld_addr = *addr;
215
216	/* MLD packet should be advertised from linklocal address */
217	getifaddrs(&ifa);
218	for (ifap = ifa; ifap; ifap = ifap->ifa_next) {
219		if (index != if_nametoindex(ifap->ifa_name))
220			continue;
221
222		if (ifap->ifa_addr->sa_family != AF_INET6)
223			continue;
224		if (!IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)
225					    ifap->ifa_addr)->sin6_addr))
226			continue;
227		break;
228	}
229	if (ifap == NULL)
230		errx(1, "no linkocal address is available");
231	memcpy(&src, &((struct sockaddr_in6 *)ifap->ifa_addr)->sin6_addr,
232	       sizeof(src));
233	freeifaddrs(ifa);
234#ifdef __KAME__
235	/* remove embedded ifindex */
236	src.s6_addr[2] = src.s6_addr[3] = 0;
237#endif
238
239#ifdef USE_RFC2292BIS
240	if ((hbhlen = inet6_opt_init(NULL, 0)) == -1)
241		errx(1, "inet6_opt_init(0) failed");
242	if ((hbhlen = inet6_opt_append(NULL, 0, hbhlen, IP6OPT_ROUTER_ALERT, 2,
243				       2, NULL)) == -1)
244		errx(1, "inet6_opt_append(0) failed");
245	if ((hbhlen = inet6_opt_finish(NULL, 0, hbhlen)) == -1)
246		errx(1, "inet6_opt_finish(0) failed");
247	cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(hbhlen);
248#else
249	hbhlen = sizeof(raopt);
250	cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
251	    inet6_option_space(hbhlen);
252#endif
253
254	if ((cmsgbuf = malloc(cmsglen)) == NULL)
255		errx(1, "can't allocate enough memory for cmsg");
256	cmsgp = (struct cmsghdr *)cmsgbuf;
257	m.msg_control = (caddr_t)cmsgbuf;
258	m.msg_controllen = cmsglen;
259	/* specify the outgoing interface */
260	cmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
261	cmsgp->cmsg_level = IPPROTO_IPV6;
262	cmsgp->cmsg_type = IPV6_PKTINFO;
263	pi = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
264	pi->ipi6_ifindex = index;
265	memcpy(&pi->ipi6_addr, &src, sizeof(pi->ipi6_addr));
266	/* specifiy to insert router alert option in a hop-by-hop opt hdr. */
267	cmsgp = CMSG_NXTHDR(&m, cmsgp);
268#ifdef USE_RFC2292BIS
269	cmsgp->cmsg_len = CMSG_LEN(hbhlen);
270	cmsgp->cmsg_level = IPPROTO_IPV6;
271	cmsgp->cmsg_type = IPV6_HOPOPTS;
272	hbhbuf = CMSG_DATA(cmsgp);
273	if ((currentlen = inet6_opt_init(hbhbuf, hbhlen)) == -1)
274		errx(1, "inet6_opt_init(len = %d) failed", hbhlen);
275	if ((currentlen = inet6_opt_append(hbhbuf, hbhlen, currentlen,
276					   IP6OPT_ROUTER_ALERT, 2,
277					   2, &optp)) == -1)
278		errx(1, "inet6_opt_append(currentlen = %d, hbhlen = %d) failed",
279		     currentlen, hbhlen);
280	(void)inet6_opt_set_val(optp, 0, &rtalert_code, sizeof(rtalert_code));
281	if ((currentlen = inet6_opt_finish(hbhbuf, hbhlen, currentlen)) == -1)
282		errx(1, "inet6_opt_finish(buf) failed");
283#else  /* old advanced API */
284	if (inet6_option_init((void *)cmsgp, &cmsgp, IPV6_HOPOPTS))
285		errx(1, "inet6_option_init failed\n");
286	raopt[0] = IP6OPT_ROUTER_ALERT;
287	raopt[1] = IP6OPT_RTALERT_LEN - 2;
288	memcpy(&raopt[2], (caddr_t)&rtalert_code, sizeof(u_short));
289	if (inet6_option_append(cmsgp, raopt, 4, 0))
290		errx(1, "inet6_option_append failed\n");
291#endif
292}
293
294void
295dump(int s)
296{
297	int i;
298	struct mld_hdr *mld;
299	u_char buf[1024];
300	struct sockaddr_in6 from;
301	int from_len = sizeof(from);
302	char ntop_buf[256];
303
304	if ((i = recvfrom(s, buf, sizeof(buf), 0,
305			  (struct sockaddr *)&from,
306			  &from_len)) < 0)
307		return;
308
309	if (i < sizeof(struct mld_hdr)) {
310		printf("too short!\n");
311		return;
312	}
313
314	mld = (struct mld_hdr *)buf;
315
316	printf("from %s, ", inet_ntop(AF_INET6, &from.sin6_addr,
317				      ntop_buf, sizeof(ntop_buf)));
318
319	switch (mld->mld_type) {
320	case ICMP6_MEMBERSHIP_QUERY:
321		printf("type=Multicast Listener Query, ");
322		break;
323	case ICMP6_MEMBERSHIP_REPORT:
324		printf("type=Multicast Listener Report, ");
325		break;
326	case ICMP6_MEMBERSHIP_REDUCTION:
327		printf("type=Multicast Listener Done, ");
328		break;
329	}
330	printf("addr=%s\n", inet_ntop(AF_INET6, &mld->mld_addr,
331				    ntop_buf, sizeof(ntop_buf)));
332
333	fflush(stdout);
334}
335
336void
337quit(int signum __unused)
338{
339	mreq.ipv6mr_multiaddr = any;
340	mreq.ipv6mr_interface = ifindex;
341	if (setsockopt(s, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq,
342		       sizeof(mreq)) == -1)
343		err(1, "setsockopt(IPV6_LEAVE_GROUP)");
344
345	exit(0);
346}
347
348void
349usage(void)
350{
351	(void)fprintf(stderr, "usage: mld6query ifname [addr]\n");
352	exit(1);
353}
354