mld6.c revision 62915
1/*
2 * Copyright (C) 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 * 3. Neither the name of the project nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29#include <sys/param.h>
30#include <sys/uio.h>
31#include <sys/socket.h>
32#include <sys/types.h>
33#include <sys/time.h>
34#include <unistd.h>
35#include <signal.h>
36
37#include <net/if.h>
38#include <net/if_var.h>
39
40#include <netinet/in.h>
41#include <netinet/ip6.h>
42#include <netinet/icmp6.h>
43
44#include  <arpa/inet.h>
45
46#include <stdlib.h>
47#include <stdio.h>
48#include <string.h>
49#include <err.h>
50
51struct msghdr m;
52struct sockaddr_in6 dst;
53struct mld6_hdr mldh;
54struct in6_addr maddr = IN6ADDR_ANY_INIT, any = IN6ADDR_ANY_INIT;
55struct ipv6_mreq mreq;
56u_short ifindex;
57int s;
58
59#define QUERY_RESPONSE_INTERVAL 10000
60
61void make_msg(int index, struct in6_addr *addr, u_int type);
62void usage(void);
63void dump(int);
64void quit(int);
65
66int
67main(int argc, char *argv[])
68{
69	int i;
70	struct icmp6_filter filt;
71	u_int hlim = 1;
72	fd_set fdset;
73	struct itimerval itimer;
74	u_int type;
75	int ch;
76
77	type = MLD6_LISTENER_QUERY;
78	while ((ch = getopt(argc, argv, "d")) != EOF) {
79		switch (ch) {
80		case 'd':
81			type = MLD6_LISTENER_DONE;
82			break;
83		case 'r':
84			type = MLD6_LISTENER_REPORT;
85			break;
86		default:
87			usage();
88			/*NOTREACHED*/
89		}
90	}
91
92	argv += optind;
93	argc -= optind;
94
95	if (argc != 1 && argc != 2)
96		usage();
97
98	ifindex = (u_short)if_nametoindex(argv[0]);
99	if (ifindex == 0)
100		usage();
101	if (argc == 3 && inet_pton(AF_INET6, argv[1], &maddr) != 1)
102		usage();
103
104	if ((s = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
105		err(1, "socket");
106
107	if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hlim,
108		       sizeof(hlim)) == -1)
109		err(1, "setsockopt(IPV6_MULTICAST_HOPS)");
110
111	mreq.ipv6mr_multiaddr = any;
112	mreq.ipv6mr_interface = ifindex;
113	if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
114		       sizeof(mreq)) == -1)
115		err(1, "setsockopt(IPV6_JOIN_GROUP)");
116
117	ICMP6_FILTER_SETBLOCKALL(&filt);
118	ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_QUERY, &filt);
119	ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REPORT, &filt);
120	ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REDUCTION, &filt);
121	if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
122			sizeof(filt)) < 0)
123		err(1, "setsockopt(ICMP6_FILTER)");
124
125	make_msg(ifindex, &maddr, type);
126
127	if (sendmsg(s, &m, 0) < 0)
128		err(1, "sendmsg");
129
130	itimer.it_value.tv_sec =  QUERY_RESPONSE_INTERVAL / 1000;
131	itimer.it_interval.tv_sec = 0;
132	itimer.it_interval.tv_usec = 0;
133	itimer.it_value.tv_usec = 0;
134
135	(void)signal(SIGALRM, quit);
136	(void)setitimer(ITIMER_REAL, &itimer, NULL);
137
138	FD_ZERO(&fdset);
139	for (;;) {
140		FD_SET(s, &fdset);
141		if ((i = select(s + 1, &fdset, NULL, NULL, NULL)) < 0)
142			perror("select");
143		if (i == 0)
144			continue;
145		else
146			dump(s);
147	}
148}
149
150void
151make_msg(int index, struct in6_addr *addr, u_int type)
152{
153	static struct iovec iov[2];
154	static u_char *cmsgbuf;
155	int cmsglen, hbhlen = 0;
156	u_int8_t raopt[IP6OPT_RTALERT_LEN];
157	struct in6_pktinfo *pi;
158	struct cmsghdr *cmsgp;
159	u_short rtalert_code = htons(IP6OPT_RTALERT_MLD);
160
161	dst.sin6_len = sizeof(dst);
162	dst.sin6_family = AF_INET6;
163	if (IN6_IS_ADDR_UNSPECIFIED(addr)) {
164		if (inet_pton(AF_INET6, "ff02::1", &dst.sin6_addr) != 1)
165			errx(1, "inet_pton failed");
166	}
167	else
168		dst.sin6_addr = *addr;
169	m.msg_name = (caddr_t)&dst;
170	m.msg_namelen = dst.sin6_len;
171	iov[0].iov_base = (caddr_t)&mldh;
172	iov[0].iov_len = sizeof(mldh);
173	m.msg_iov = iov;
174	m.msg_iovlen = 1;
175
176	bzero(&mldh, sizeof(mldh));
177	mldh.mld6_type = type & 0xff;
178	mldh.mld6_maxdelay = htons(QUERY_RESPONSE_INTERVAL);
179	mldh.mld6_addr = *addr;
180
181	hbhlen = sizeof(raopt);
182	cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
183	    inet6_option_space(hbhlen);
184
185	if ((cmsgbuf = malloc(cmsglen)) == NULL)
186		errx(1, "can't allocate enough memory for cmsg");
187	cmsgp = (struct cmsghdr *)cmsgbuf;
188	m.msg_control = (caddr_t)cmsgbuf;
189	m.msg_controllen = cmsglen;
190	/* specify the outgoing interface */
191	cmsgp->cmsg_len = CMSG_SPACE(sizeof(struct in6_pktinfo));
192	cmsgp->cmsg_level = IPPROTO_IPV6;
193	cmsgp->cmsg_type = IPV6_PKTINFO;
194	pi = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
195	pi->ipi6_ifindex = index;
196	memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr));
197	/* specifiy to insert router alert option in a hop-by-hop opt hdr. */
198	cmsgp = CMSG_NXTHDR(&m, cmsgp);
199	if (inet6_option_init((void *)cmsgp, &cmsgp, IPV6_HOPOPTS))
200		errx(1, "inet6_option_init failed\n");
201	raopt[0] = IP6OPT_RTALERT;
202	raopt[1] = IP6OPT_RTALERT_LEN - 2;
203	memcpy(&raopt[2], (caddr_t)&rtalert_code, sizeof(u_short));
204	if (inet6_option_append(cmsgp, raopt, 4, 0))
205		errx(1, "inet6_option_append failed\n");
206}
207
208void
209dump(int s)
210{
211	int i;
212	struct mld6_hdr *mld;
213	u_char buf[1024];
214	struct sockaddr_in6 from;
215	int from_len = sizeof(from);
216	char ntop_buf[256];
217
218	if ((i = recvfrom(s, buf, sizeof(buf), 0,
219			  (struct sockaddr *)&from,
220			  &from_len)) < 0)
221		return;
222
223	if (i < sizeof(struct mld6_hdr)) {
224		printf("too short!\n");
225		return;
226	}
227
228	mld = (struct mld6_hdr *)buf;
229
230	printf("from %s, ", inet_ntop(AF_INET6, &from.sin6_addr,
231				      ntop_buf, sizeof(ntop_buf)));
232
233	switch (mld->mld6_type) {
234	case ICMP6_MEMBERSHIP_QUERY:
235		printf("type=Multicast Listener Query, ");
236		break;
237	case ICMP6_MEMBERSHIP_REPORT:
238		printf("type=Multicast Listener Report, ");
239		break;
240	case ICMP6_MEMBERSHIP_REDUCTION:
241		printf("type=Multicast Listener Done, ");
242		break;
243	}
244	printf("addr=%s\n", inet_ntop(AF_INET6, &mld->mld6_addr,
245				    ntop_buf, sizeof(ntop_buf)));
246
247	fflush(stdout);
248}
249
250/* ARGSUSED */
251void
252quit(int signum) {
253	mreq.ipv6mr_multiaddr = any;
254	mreq.ipv6mr_interface = ifindex;
255	if (setsockopt(s, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq,
256		       sizeof(mreq)) == -1)
257		err(1, "setsockopt(IPV6_LEAVE_GROUP)");
258
259	exit(0);
260}
261
262void
263usage()
264{
265	(void)fprintf(stderr, "usage: mld6query ifname [addr]\n");
266	exit(1);
267}
268