xref: /freebsd/usr.sbin/mld6query/mld6.c (revision 61ba55bcf70f2340f9c943c9571113b3fd8eda69)
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 #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 
46 #include <netinet/in.h>
47 #include <netinet/ip6.h>
48 #include <netinet/icmp6.h>
49 
50 #include  <arpa/inet.h>
51 
52 #include <stdlib.h>
53 #include <stdio.h>
54 #include <string.h>
55 #include <err.h>
56 
57 /* portability with older KAME headers */
58 #ifndef MLD_LISTENER_QUERY
59 #define MLD_LISTENER_QUERY	MLD6_LISTENER_QUERY
60 #define MLD_LISTENER_REPORT	MLD6_LISTENER_REPORT
61 #define MLD_LISTENER_DONE	MLD6_LISTENER_DONE
62 #define MLD_MTRACE_RESP		MLD6_MTRACE_RESP
63 #define MLD_MTRACE		MLD6_MTRACE
64 #define mld_hdr		mld6_hdr
65 #define mld_type	mld6_type
66 #define mld_code	mld6_code
67 #define mld_cksum	mld6_cksum
68 #define mld_maxdelay	mld6_maxdelay
69 #define mld_reserved	mld6_reserved
70 #define mld_addr	mld6_addr
71 #endif
72 #ifndef IP6OPT_ROUTER_ALERT
73 #define IP6OPT_ROUTER_ALERT	IP6OPT_RTALERT
74 #endif
75 
76 struct msghdr m;
77 struct sockaddr_in6 dst;
78 struct mld_hdr mldh;
79 struct in6_addr maddr = IN6ADDR_ANY_INIT, any = IN6ADDR_ANY_INIT;
80 struct ipv6_mreq mreq;
81 u_short ifindex;
82 int s;
83 
84 #define QUERY_RESPONSE_INTERVAL 10000
85 
86 void make_msg(int index, struct in6_addr *addr, u_int type, struct in6_addr *qaddr);
87 void usage(void);
88 void dump(int);
89 void quit(int);
90 
91 int
92 main(int argc, char *argv[])
93 {
94 	int i;
95 	struct icmp6_filter filt;
96 	u_int hlim = 1;
97 	fd_set fdset;
98 	struct itimerval itimer;
99 	u_int type;
100 	int ch;
101 	struct in6_addr *qaddr = &maddr;
102 
103 	type = MLD_LISTENER_QUERY;
104 	while ((ch = getopt(argc, argv, "dgr")) != -1) {
105 		switch (ch) {
106 		case 'd':
107 			if (type != MLD_LISTENER_QUERY) {
108 				printf("Can not specify -d with -r\n");
109 				return 1;
110 			}
111 			type = MLD_LISTENER_DONE;
112 			break;
113 		case 'g':
114 			qaddr = &any;
115 			break;
116 		case 'r':
117 			if (type != MLD_LISTENER_QUERY) {
118 				printf("Can not specify -r with -d\n");
119 				return 1;
120 			}
121 			type = MLD_LISTENER_REPORT;
122 			break;
123 		default:
124 			usage();
125 			/*NOTREACHED*/
126 		}
127 	}
128 
129 	argv += optind;
130 	argc -= optind;
131 
132 	if (argc != 1 && argc != 2)
133 		usage();
134 
135 	ifindex = (u_short)if_nametoindex(argv[0]);
136 	if (ifindex == 0)
137 		usage();
138 	if (argc == 2 && inet_pton(AF_INET6, argv[1], &maddr) != 1)
139 		usage();
140 	if (type != MLD_LISTENER_QUERY && qaddr != &maddr) {
141 		printf("Can not specify -g with -d or -r\n");
142 		return 1;
143 	}
144 
145 	if ((s = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
146 		err(1, "socket");
147 
148 	if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hlim,
149 		       sizeof(hlim)) == -1)
150 		err(1, "setsockopt(IPV6_MULTICAST_HOPS)");
151 
152 	if (IN6_IS_ADDR_UNSPECIFIED(&maddr)) {
153 		if (inet_pton(AF_INET6, "ff02::1", &maddr) != 1)
154 			errx(1, "inet_pton failed");
155 	}
156 
157 	mreq.ipv6mr_multiaddr = maddr;
158 	mreq.ipv6mr_interface = ifindex;
159 	if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
160 		       sizeof(mreq)) == -1)
161 		err(1, "setsockopt(IPV6_JOIN_GROUP)");
162 
163 	ICMP6_FILTER_SETBLOCKALL(&filt);
164 	ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_QUERY, &filt);
165 	ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REPORT, &filt);
166 	ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REDUCTION, &filt);
167 	if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
168 			sizeof(filt)) < 0)
169 		err(1, "setsockopt(ICMP6_FILTER)");
170 
171 	make_msg(ifindex, &maddr, type, qaddr);
172 
173 	if (sendmsg(s, &m, 0) < 0)
174 		err(1, "sendmsg");
175 
176 	itimer.it_value.tv_sec =  QUERY_RESPONSE_INTERVAL / 1000;
177 	itimer.it_interval.tv_sec = 0;
178 	itimer.it_interval.tv_usec = 0;
179 	itimer.it_value.tv_usec = 0;
180 
181 	(void)signal(SIGALRM, quit);
182 	(void)setitimer(ITIMER_REAL, &itimer, NULL);
183 
184 	FD_ZERO(&fdset);
185 	if (s >= FD_SETSIZE)
186 		errx(1, "descriptor too big");
187 	for (;;) {
188 		FD_SET(s, &fdset);
189 		if ((i = select(s + 1, &fdset, NULL, NULL, NULL)) < 0)
190 			perror("select");
191 		if (i == 0)
192 			continue;
193 		else
194 			dump(s);
195 	}
196 }
197 
198 void
199 make_msg(int index, struct in6_addr *addr, u_int type, struct in6_addr *qaddr)
200 {
201 	static struct iovec iov[2];
202 	static u_char *cmsgbuf;
203 	int cmsglen, hbhlen = 0;
204 	void *hbhbuf = NULL, *optp = NULL;
205 	int currentlen;
206 	struct in6_pktinfo *pi;
207 	struct cmsghdr *cmsgp;
208 	u_short rtalert_code = htons(IP6OPT_RTALERT_MLD);
209 	struct ifaddrs *ifa, *ifap;
210 	struct in6_addr src;
211 
212 	dst.sin6_len = sizeof(dst);
213 	dst.sin6_family = AF_INET6;
214 	dst.sin6_addr = *addr;
215 	m.msg_name = (caddr_t)&dst;
216 	m.msg_namelen = dst.sin6_len;
217 	iov[0].iov_base = (caddr_t)&mldh;
218 	iov[0].iov_len = sizeof(mldh);
219 	m.msg_iov = iov;
220 	m.msg_iovlen = 1;
221 
222 	bzero(&mldh, sizeof(mldh));
223 	mldh.mld_type = type & 0xff;
224 	mldh.mld_maxdelay = htons(QUERY_RESPONSE_INTERVAL);
225 	mldh.mld_addr = *qaddr;
226 
227 	/* MLD packet should be advertised from linklocal address */
228 	getifaddrs(&ifa);
229 	for (ifap = ifa; ifap; ifap = ifap->ifa_next) {
230 		if (index != if_nametoindex(ifap->ifa_name))
231 			continue;
232 
233 		if (ifap->ifa_addr->sa_family != AF_INET6)
234 			continue;
235 		if (!IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)
236 					    ifap->ifa_addr)->sin6_addr))
237 			continue;
238 		break;
239 	}
240 	if (ifap == NULL)
241 		errx(1, "no linklocal address is available");
242 	memcpy(&src, &((struct sockaddr_in6 *)ifap->ifa_addr)->sin6_addr,
243 	       sizeof(src));
244 	freeifaddrs(ifa);
245 #ifdef __KAME__
246 	/* remove embedded ifindex */
247 	src.s6_addr[2] = src.s6_addr[3] = 0;
248 #endif
249 
250 	if ((hbhlen = inet6_opt_init(NULL, 0)) == -1)
251 		errx(1, "inet6_opt_init(0) failed");
252 	if ((hbhlen = inet6_opt_append(NULL, 0, hbhlen, IP6OPT_ROUTER_ALERT, 2,
253 				       2, NULL)) == -1)
254 		errx(1, "inet6_opt_append(0) failed");
255 	if ((hbhlen = inet6_opt_finish(NULL, 0, hbhlen)) == -1)
256 		errx(1, "inet6_opt_finish(0) failed");
257 	cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(hbhlen);
258 
259 	if ((cmsgbuf = malloc(cmsglen)) == NULL)
260 		errx(1, "can't allocate enough memory for cmsg");
261 	cmsgp = (struct cmsghdr *)cmsgbuf;
262 	m.msg_control = (caddr_t)cmsgbuf;
263 	m.msg_controllen = cmsglen;
264 	/* specify the outgoing interface */
265 	cmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
266 	cmsgp->cmsg_level = IPPROTO_IPV6;
267 	cmsgp->cmsg_type = IPV6_PKTINFO;
268 	pi = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
269 	pi->ipi6_ifindex = index;
270 	memcpy(&pi->ipi6_addr, &src, sizeof(pi->ipi6_addr));
271 	/* specify to insert router alert option in a hop-by-hop opt hdr. */
272 	cmsgp = CMSG_NXTHDR(&m, cmsgp);
273 	cmsgp->cmsg_len = CMSG_LEN(hbhlen);
274 	cmsgp->cmsg_level = IPPROTO_IPV6;
275 	cmsgp->cmsg_type = IPV6_HOPOPTS;
276 	hbhbuf = CMSG_DATA(cmsgp);
277 	if ((currentlen = inet6_opt_init(hbhbuf, hbhlen)) == -1)
278 		errx(1, "inet6_opt_init(len = %d) failed", hbhlen);
279 	if ((currentlen = inet6_opt_append(hbhbuf, hbhlen, currentlen,
280 					   IP6OPT_ROUTER_ALERT, 2,
281 					   2, &optp)) == -1)
282 		errx(1, "inet6_opt_append(currentlen = %d, hbhlen = %d) failed",
283 		     currentlen, hbhlen);
284 	(void)inet6_opt_set_val(optp, 0, &rtalert_code, sizeof(rtalert_code));
285 	if ((currentlen = inet6_opt_finish(hbhbuf, hbhlen, currentlen)) == -1)
286 		errx(1, "inet6_opt_finish(buf) failed");
287 }
288 
289 void
290 dump(int s)
291 {
292 	int i;
293 	struct mld_hdr *mld;
294 	u_char buf[1024];
295 	struct sockaddr_in6 from;
296 	int from_len = sizeof(from);
297 	char ntop_buf[256];
298 
299 	if ((i = recvfrom(s, buf, sizeof(buf), 0,
300 			  (struct sockaddr *)&from,
301 			  &from_len)) < 0)
302 		return;
303 
304 	if (i < sizeof(struct mld_hdr)) {
305 		printf("too short!\n");
306 		return;
307 	}
308 
309 	mld = (struct mld_hdr *)buf;
310 
311 	printf("from %s, ", inet_ntop(AF_INET6, &from.sin6_addr,
312 				      ntop_buf, sizeof(ntop_buf)));
313 
314 	switch (mld->mld_type) {
315 	case ICMP6_MEMBERSHIP_QUERY:
316 		printf("type=Multicast Listener Query, ");
317 		break;
318 	case ICMP6_MEMBERSHIP_REPORT:
319 		printf("type=Multicast Listener Report, ");
320 		break;
321 	case ICMP6_MEMBERSHIP_REDUCTION:
322 		printf("type=Multicast Listener Done, ");
323 		break;
324 	}
325 	printf("addr=%s\n", inet_ntop(AF_INET6, &mld->mld_addr,
326 				    ntop_buf, sizeof(ntop_buf)));
327 
328 	fflush(stdout);
329 }
330 
331 void
332 quit(int signum __unused)
333 {
334 	mreq.ipv6mr_multiaddr = maddr;
335 	mreq.ipv6mr_interface = ifindex;
336 	if (setsockopt(s, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq,
337 		       sizeof(mreq)) == -1)
338 		err(1, "setsockopt(IPV6_LEAVE_GROUP)");
339 
340 	exit(0);
341 }
342 
343 void
344 usage(void)
345 {
346 	(void)fprintf(stderr, "usage: mld6query [-dgr] ifname [addr]\n");
347 	exit(1);
348 }
349