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