1 /* $KAME: mld6.c,v 1.15 2003/04/02 11:29:54 suz Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (C) 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <sys/types.h>
48 #include <netinet/in.h>
49 #include <netinet/ip6.h>
50 #include <netinet/icmp6.h>
52 #include <arpa/inet.h>
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
74 #ifndef IP6OPT_ROUTER_ALERT
75 #define IP6OPT_ROUTER_ALERT IP6OPT_RTALERT
79 struct sockaddr_in6 dst;
81 struct in6_addr maddr = IN6ADDR_ANY_INIT, any = IN6ADDR_ANY_INIT;
82 struct ipv6_mreq mreq;
86 #define QUERY_RESPONSE_INTERVAL 10000
88 void make_msg(int index, struct in6_addr *addr, u_int type, struct in6_addr *qaddr);
94 main(int argc, char *argv[])
97 struct icmp6_filter filt;
100 struct itimerval itimer;
103 struct in6_addr *qaddr = &maddr;
105 type = MLD_LISTENER_QUERY;
106 while ((ch = getopt(argc, argv, "dgr")) != -1) {
109 if (type != MLD_LISTENER_QUERY) {
110 printf("Can not specifiy -d with -r\n");
113 type = MLD_LISTENER_DONE;
119 if (type != MLD_LISTENER_QUERY) {
120 printf("Can not specifiy -r with -d\n");
123 type = MLD_LISTENER_REPORT;
134 if (argc != 1 && argc != 2)
137 ifindex = (u_short)if_nametoindex(argv[0]);
140 if (argc == 2 && inet_pton(AF_INET6, argv[1], &maddr) != 1)
142 if (type != MLD_LISTENER_QUERY && qaddr != &maddr) {
143 printf("Can not specifiy -g with -d or -r\n");
147 if ((s = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
150 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hlim,
152 err(1, "setsockopt(IPV6_MULTICAST_HOPS)");
154 if (IN6_IS_ADDR_UNSPECIFIED(&maddr)) {
155 if (inet_pton(AF_INET6, "ff02::1", &maddr) != 1)
156 errx(1, "inet_pton failed");
159 mreq.ipv6mr_multiaddr = maddr;
160 mreq.ipv6mr_interface = ifindex;
161 if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
163 err(1, "setsockopt(IPV6_JOIN_GROUP)");
165 ICMP6_FILTER_SETBLOCKALL(&filt);
166 ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_QUERY, &filt);
167 ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REPORT, &filt);
168 ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REDUCTION, &filt);
169 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
171 err(1, "setsockopt(ICMP6_FILTER)");
173 make_msg(ifindex, &maddr, type, qaddr);
175 if (sendmsg(s, &m, 0) < 0)
178 itimer.it_value.tv_sec = QUERY_RESPONSE_INTERVAL / 1000;
179 itimer.it_interval.tv_sec = 0;
180 itimer.it_interval.tv_usec = 0;
181 itimer.it_value.tv_usec = 0;
183 (void)signal(SIGALRM, quit);
184 (void)setitimer(ITIMER_REAL, &itimer, NULL);
188 errx(1, "descriptor too big");
191 if ((i = select(s + 1, &fdset, NULL, NULL, NULL)) < 0)
201 make_msg(int index, struct in6_addr *addr, u_int type, struct in6_addr *qaddr)
203 static struct iovec iov[2];
204 static u_char *cmsgbuf;
205 int cmsglen, hbhlen = 0;
206 void *hbhbuf = NULL, *optp = NULL;
208 struct in6_pktinfo *pi;
209 struct cmsghdr *cmsgp;
210 u_short rtalert_code = htons(IP6OPT_RTALERT_MLD);
211 struct ifaddrs *ifa, *ifap;
214 dst.sin6_len = sizeof(dst);
215 dst.sin6_family = AF_INET6;
216 dst.sin6_addr = *addr;
217 m.msg_name = (caddr_t)&dst;
218 m.msg_namelen = dst.sin6_len;
219 iov[0].iov_base = (caddr_t)&mldh;
220 iov[0].iov_len = sizeof(mldh);
224 bzero(&mldh, sizeof(mldh));
225 mldh.mld_type = type & 0xff;
226 mldh.mld_maxdelay = htons(QUERY_RESPONSE_INTERVAL);
227 mldh.mld_addr = *qaddr;
229 /* MLD packet should be advertised from linklocal address */
231 for (ifap = ifa; ifap; ifap = ifap->ifa_next) {
232 if (index != if_nametoindex(ifap->ifa_name))
235 if (ifap->ifa_addr->sa_family != AF_INET6)
237 if (!IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)
238 ifap->ifa_addr)->sin6_addr))
243 errx(1, "no linklocal address is available");
244 memcpy(&src, &((struct sockaddr_in6 *)ifap->ifa_addr)->sin6_addr,
248 /* remove embedded ifindex */
249 src.s6_addr[2] = src.s6_addr[3] = 0;
252 if ((hbhlen = inet6_opt_init(NULL, 0)) == -1)
253 errx(1, "inet6_opt_init(0) failed");
254 if ((hbhlen = inet6_opt_append(NULL, 0, hbhlen, IP6OPT_ROUTER_ALERT, 2,
256 errx(1, "inet6_opt_append(0) failed");
257 if ((hbhlen = inet6_opt_finish(NULL, 0, hbhlen)) == -1)
258 errx(1, "inet6_opt_finish(0) failed");
259 cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(hbhlen);
261 if ((cmsgbuf = malloc(cmsglen)) == NULL)
262 errx(1, "can't allocate enough memory for cmsg");
263 cmsgp = (struct cmsghdr *)cmsgbuf;
264 m.msg_control = (caddr_t)cmsgbuf;
265 m.msg_controllen = cmsglen;
266 /* specify the outgoing interface */
267 cmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
268 cmsgp->cmsg_level = IPPROTO_IPV6;
269 cmsgp->cmsg_type = IPV6_PKTINFO;
270 pi = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
271 pi->ipi6_ifindex = index;
272 memcpy(&pi->ipi6_addr, &src, sizeof(pi->ipi6_addr));
273 /* specifiy to insert router alert option in a hop-by-hop opt hdr. */
274 cmsgp = CMSG_NXTHDR(&m, cmsgp);
275 cmsgp->cmsg_len = CMSG_LEN(hbhlen);
276 cmsgp->cmsg_level = IPPROTO_IPV6;
277 cmsgp->cmsg_type = IPV6_HOPOPTS;
278 hbhbuf = CMSG_DATA(cmsgp);
279 if ((currentlen = inet6_opt_init(hbhbuf, hbhlen)) == -1)
280 errx(1, "inet6_opt_init(len = %d) failed", hbhlen);
281 if ((currentlen = inet6_opt_append(hbhbuf, hbhlen, currentlen,
282 IP6OPT_ROUTER_ALERT, 2,
284 errx(1, "inet6_opt_append(currentlen = %d, hbhlen = %d) failed",
286 (void)inet6_opt_set_val(optp, 0, &rtalert_code, sizeof(rtalert_code));
287 if ((currentlen = inet6_opt_finish(hbhbuf, hbhlen, currentlen)) == -1)
288 errx(1, "inet6_opt_finish(buf) failed");
297 struct sockaddr_in6 from;
298 int from_len = sizeof(from);
301 if ((i = recvfrom(s, buf, sizeof(buf), 0,
302 (struct sockaddr *)&from,
306 if (i < sizeof(struct mld_hdr)) {
307 printf("too short!\n");
311 mld = (struct mld_hdr *)buf;
313 printf("from %s, ", inet_ntop(AF_INET6, &from.sin6_addr,
314 ntop_buf, sizeof(ntop_buf)));
316 switch (mld->mld_type) {
317 case ICMP6_MEMBERSHIP_QUERY:
318 printf("type=Multicast Listener Query, ");
320 case ICMP6_MEMBERSHIP_REPORT:
321 printf("type=Multicast Listener Report, ");
323 case ICMP6_MEMBERSHIP_REDUCTION:
324 printf("type=Multicast Listener Done, ");
327 printf("addr=%s\n", inet_ntop(AF_INET6, &mld->mld_addr,
328 ntop_buf, sizeof(ntop_buf)));
334 quit(int signum __unused)
336 mreq.ipv6mr_multiaddr = maddr;
337 mreq.ipv6mr_interface = ifindex;
338 if (setsockopt(s, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq,
340 err(1, "setsockopt(IPV6_LEAVE_GROUP)");
348 (void)fprintf(stderr, "usage: mld6query [-dgr] ifname [addr]\n");