xref: /netbsd/usr.sbin/mld6query/mld6.c (revision 547f654f)
1 /*	$NetBSD: mld6.c,v 1.16 2023/06/24 05:15:42 msaitoh 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 <poll.h>
38 #include <unistd.h>
39 #include <signal.h>
40 
41 #include <net/if.h>
42 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
43 #include <net/if_var.h>
44 #endif /* __FreeBSD__ >= 3 */
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 static struct msghdr m;
58 static struct sockaddr_in6 dst;
59 static struct mld6_hdr mldh;
60 static struct in6_addr maddr = IN6ADDR_ANY_INIT, any = IN6ADDR_ANY_INIT;
61 static struct ipv6_mreq mreq;
62 static u_short ifindex;
63 static int sock;
64 
65 #define QUERY_RESPONSE_INTERVAL 10000
66 
67 static void make_msg(int index, struct in6_addr *addr, u_int type);
68 __dead static void usage(void);
69 static void dump(int);
70 __dead static void quit(int);
71 
72 int
main(int argc,char * argv[])73 main(int argc, char *argv[])
74 {
75 	int i;
76 	struct icmp6_filter filt;
77 	u_int hlim = 1;
78 	struct pollfd set[1];
79 	struct itimerval itimer;
80 	u_int type;
81 	int ch;
82 
83 	type = MLD_LISTENER_QUERY;
84 	while ((ch = getopt(argc, argv, "dr")) != -1) {
85 		switch (ch) {
86 		case 'd':
87 			type = MLD_LISTENER_DONE;
88 			break;
89 		case 'r':
90 			type = MLD_LISTENER_REPORT;
91 			break;
92 		default:
93 			usage();
94 			/*NOTREACHED*/
95 		}
96 	}
97 
98 	argv += optind;
99 	argc -= optind;
100 
101 	if (argc != 1 && argc != 2)
102 		usage();
103 
104 	ifindex = (u_short)if_nametoindex(argv[0]);
105 	if (ifindex == 0)
106 		usage();
107 	if (argc == 3 && inet_pton(AF_INET6, argv[1], &maddr) != 1)
108 		usage();
109 
110 	if ((sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
111 		err(1, "socket");
112 
113 	if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hlim,
114 		       sizeof(hlim)) == -1)
115 		err(1, "setsockopt(IPV6_MULTICAST_HOPS)");
116 
117 	mreq.ipv6mr_multiaddr = any;
118 	mreq.ipv6mr_interface = ifindex;
119 	if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
120 		       sizeof(mreq)) == -1)
121 		err(1, "setsockopt(IPV6_JOIN_GROUP)");
122 
123 	ICMP6_FILTER_SETBLOCKALL(&filt);
124 	ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_QUERY, &filt);
125 	ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REPORT, &filt);
126 	ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REDUCTION, &filt);
127 	if (setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
128 			sizeof(filt)) < 0)
129 		err(1, "setsockopt(ICMP6_FILTER)");
130 
131 	make_msg(ifindex, &maddr, type);
132 
133 	if (sendmsg(sock, &m, 0) < 0)
134 		err(1, "sendmsg");
135 
136 	itimer.it_value.tv_sec =  QUERY_RESPONSE_INTERVAL / 1000;
137 	itimer.it_interval.tv_sec = 0;
138 	itimer.it_interval.tv_usec = 0;
139 	itimer.it_value.tv_usec = 0;
140 
141 	(void)signal(SIGALRM, quit);
142 	(void)setitimer(ITIMER_REAL, &itimer, NULL);
143 
144 	set[0].fd = sock;
145 	set[0].events = POLLIN;
146 	for (;;) {
147 		if ((i = poll(set, 1, INFTIM)) < 0)
148 			perror("poll");
149 		if (i == 0)
150 			continue;
151 		else
152 			dump(sock);
153 	}
154 }
155 
156 static void
make_msg(int idx,struct in6_addr * addr,u_int type)157 make_msg(int idx, struct in6_addr *addr, u_int type)
158 {
159 	static struct iovec iov[2];
160 	static u_char *cmsgbuf;
161 	int cmsglen, hbhlen = 0;
162 #ifdef USE_RFC2292BIS
163 	void *hbhbuf = NULL, *optp = NULL;
164 	int currentlen;
165 #else
166 	u_int8_t raopt[IP6OPT_RTALERT_LEN];
167 #endif
168 	struct in6_pktinfo *pi;
169 	struct cmsghdr *cmsgp;
170 	u_short rtalert_code = htons(IP6OPT_RTALERT_MLD);
171 
172 	dst.sin6_len = sizeof(dst);
173 	dst.sin6_family = AF_INET6;
174 	if (IN6_IS_ADDR_UNSPECIFIED(addr)) {
175 		if (inet_pton(AF_INET6, "ff02::1", &dst.sin6_addr) != 1)
176 			errx(1, "inet_pton failed");
177 	}
178 	else
179 		dst.sin6_addr = *addr;
180 	m.msg_name = (caddr_t)&dst;
181 	m.msg_namelen = dst.sin6_len;
182 	iov[0].iov_base = (caddr_t)&mldh;
183 	iov[0].iov_len = sizeof(mldh);
184 	m.msg_iov = iov;
185 	m.msg_iovlen = 1;
186 
187 	bzero(&mldh, sizeof(mldh));
188 	mldh.mld6_type = type & 0xff;
189 	mldh.mld6_maxdelay = htons(QUERY_RESPONSE_INTERVAL);
190 	mldh.mld6_addr = *addr;
191 
192 #ifdef USE_RFC2292BIS
193 	if ((hbhlen = inet6_opt_init(NULL, 0)) == -1)
194 		errx(1, "inet6_opt_init(0) failed");
195 	if ((hbhlen = inet6_opt_append(NULL, 0, hbhlen, IP6OPT_ROUTER_ALERT, 2,
196 				       2, NULL)) == -1)
197 		errx(1, "inet6_opt_append(0) failed");
198 	if ((hbhlen = inet6_opt_finish(NULL, 0, hbhlen)) == -1)
199 		errx(1, "inet6_opt_finish(0) failed");
200 	cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(hbhlen);
201 #else
202 	hbhlen = sizeof(raopt);
203 	cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
204 	    inet6_option_space(hbhlen);
205 #endif
206 
207 	if ((cmsgbuf = malloc(cmsglen)) == NULL)
208 		errx(1, "can't allocate enough memory for cmsg");
209 	cmsgp = (struct cmsghdr *)cmsgbuf;
210 	m.msg_control = (caddr_t)cmsgbuf;
211 	m.msg_controllen = cmsglen;
212 	/* specify the outgoing interface */
213 	cmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
214 	cmsgp->cmsg_level = IPPROTO_IPV6;
215 	cmsgp->cmsg_type = IPV6_PKTINFO;
216 	pi = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
217 	pi->ipi6_ifindex = idx;
218 	memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr));
219 	/* specify to insert router alert option in a hop-by-hop opt hdr. */
220 	cmsgp = CMSG_NXTHDR(&m, cmsgp);
221 #ifdef USE_RFC2292BIS
222 	cmsgp->cmsg_len = CMSG_LEN(hbhlen);
223 	cmsgp->cmsg_level = IPPROTO_IPV6;
224 	cmsgp->cmsg_type = IPV6_HOPOPTS;
225 	hbhbuf = CMSG_DATA(cmsgp);
226 	if ((currentlen = inet6_opt_init(hbhbuf, hbhlen)) == -1)
227 		errx(1, "inet6_opt_init(len = %d) failed", hbhlen);
228 	if ((currentlen = inet6_opt_append(hbhbuf, hbhlen, currentlen,
229 					   IP6OPT_ROUTER_ALERT, 2,
230 					   2, &optp)) == -1)
231 		errx(1, "inet6_opt_append(currentlen = %d, hbhlen = %d) failed",
232 		     currentlen, hbhlen);
233 	(void)inet6_opt_set_val(optp, 0, &rtalert_code, sizeof(rtalert_code));
234 	if ((currentlen = inet6_opt_finish(hbhbuf, hbhlen, currentlen)) == -1)
235 		errx(1, "inet6_opt_finish(buf) failed");
236 #else  /* old advanced API */
237 	if (inet6_option_init((void *)cmsgp, &cmsgp, IPV6_HOPOPTS))
238 		errx(1, "inet6_option_init failed");
239 	raopt[0] = IP6OPT_RTALERT;
240 	raopt[1] = IP6OPT_RTALERT_LEN - 2;
241 	memcpy(&raopt[2], (caddr_t)&rtalert_code, sizeof(u_short));
242 	if (inet6_option_append(cmsgp, raopt, 4, 0))
243 		errx(1, "inet6_option_append failed");
244 #endif
245 }
246 
247 static void
dump(int s)248 dump(int s)
249 {
250 	int i;
251 	struct mld6_hdr *mld;
252 	u_char buf[1024];
253 	struct sockaddr_in6 from;
254 	socklen_t from_len = sizeof(from);
255 	char ntop_buf[256];
256 
257 	if ((i = recvfrom(s, buf, sizeof(buf), 0,
258 			  (struct sockaddr *)&from,
259 			  &from_len)) < 0)
260 		return;
261 
262 	if (i < (int)sizeof(struct mld6_hdr)) {
263 		printf("too short!\n");
264 		return;
265 	}
266 
267 	mld = (struct mld6_hdr *)buf;
268 
269 	printf("from %s, ", inet_ntop(AF_INET6, &from.sin6_addr,
270 				      ntop_buf, sizeof(ntop_buf)));
271 
272 	switch (mld->mld6_type) {
273 	case ICMP6_MEMBERSHIP_QUERY:
274 		printf("type=Multicast Listener Query, ");
275 		break;
276 	case ICMP6_MEMBERSHIP_REPORT:
277 		printf("type=Multicast Listener Report, ");
278 		break;
279 	case ICMP6_MEMBERSHIP_REDUCTION:
280 		printf("type=Multicast Listener Done, ");
281 		break;
282 	}
283 	printf("addr=%s\n", inet_ntop(AF_INET6, &mld->mld6_addr,
284 				    ntop_buf, sizeof(ntop_buf)));
285 
286 	fflush(stdout);
287 }
288 
289 /* ARGSUSED */
290 static void
quit(int signum)291 quit(int signum) {
292 	mreq.ipv6mr_multiaddr = any;
293 	mreq.ipv6mr_interface = ifindex;
294 	if (setsockopt(sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq,
295 		       sizeof(mreq)) == -1)
296 		err(1, "setsockopt(IPV6_LEAVE_GROUP)");
297 
298 	exit(0);
299 }
300 
301 static void
usage(void)302 usage(void)
303 {
304 	(void)fprintf(stderr, "usage: %s [-dr] ifname [addr]\n", getprogname());
305 	exit(1);
306 }
307