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