1 /* $KAME: mld6.c,v 1.15 2003/04/02 11:29:54 suz Exp $ */
4 * Copyright (C) 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
37 #include <sys/socket.h>
38 #include <sys/types.h>
46 #include <netinet/in.h>
47 #include <netinet/ip6.h>
48 #include <netinet/icmp6.h>
50 #include <arpa/inet.h>
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
72 #ifndef IP6OPT_ROUTER_ALERT
73 #define IP6OPT_ROUTER_ALERT IP6OPT_RTALERT
77 struct sockaddr_in6 dst;
79 struct in6_addr maddr = IN6ADDR_ANY_INIT, any = IN6ADDR_ANY_INIT;
80 struct ipv6_mreq mreq;
84 #define QUERY_RESPONSE_INTERVAL 10000
86 void make_msg(int index, struct in6_addr *addr, u_int type);
92 main(int argc, char *argv[])
95 struct icmp6_filter filt;
98 struct itimerval itimer;
102 type = MLD_LISTENER_QUERY;
103 while ((ch = getopt(argc, argv, "dr")) != -1) {
106 type = MLD_LISTENER_DONE;
109 type = MLD_LISTENER_REPORT;
120 if (argc != 1 && argc != 2)
123 ifindex = (u_short)if_nametoindex(argv[0]);
126 if (argc == 2 && inet_pton(AF_INET6, argv[1], &maddr) != 1)
129 if ((s = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
132 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hlim,
134 err(1, "setsockopt(IPV6_MULTICAST_HOPS)");
136 mreq.ipv6mr_multiaddr = any;
137 mreq.ipv6mr_interface = ifindex;
138 if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
140 err(1, "setsockopt(IPV6_JOIN_GROUP)");
142 ICMP6_FILTER_SETBLOCKALL(&filt);
143 ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_QUERY, &filt);
144 ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REPORT, &filt);
145 ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REDUCTION, &filt);
146 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
148 err(1, "setsockopt(ICMP6_FILTER)");
150 make_msg(ifindex, &maddr, type);
152 if (sendmsg(s, &m, 0) < 0)
155 itimer.it_value.tv_sec = QUERY_RESPONSE_INTERVAL / 1000;
156 itimer.it_interval.tv_sec = 0;
157 itimer.it_interval.tv_usec = 0;
158 itimer.it_value.tv_usec = 0;
160 (void)signal(SIGALRM, quit);
161 (void)setitimer(ITIMER_REAL, &itimer, NULL);
165 errx(1, "descriptor too big");
168 if ((i = select(s + 1, &fdset, NULL, NULL, NULL)) < 0)
178 make_msg(int index, struct in6_addr *addr, u_int type)
180 static struct iovec iov[2];
181 static u_char *cmsgbuf;
182 int cmsglen, hbhlen = 0;
183 #ifdef USE_RFC2292BIS
184 void *hbhbuf = NULL, *optp = NULL;
187 u_int8_t raopt[IP6OPT_RTALERT_LEN];
189 struct in6_pktinfo *pi;
190 struct cmsghdr *cmsgp;
191 u_short rtalert_code = htons(IP6OPT_RTALERT_MLD);
192 struct ifaddrs *ifa, *ifap;
195 dst.sin6_len = sizeof(dst);
196 dst.sin6_family = AF_INET6;
197 if (IN6_IS_ADDR_UNSPECIFIED(addr)) {
198 if (inet_pton(AF_INET6, "ff02::1", &dst.sin6_addr) != 1)
199 errx(1, "inet_pton failed");
202 dst.sin6_addr = *addr;
203 m.msg_name = (caddr_t)&dst;
204 m.msg_namelen = dst.sin6_len;
205 iov[0].iov_base = (caddr_t)&mldh;
206 iov[0].iov_len = sizeof(mldh);
210 bzero(&mldh, sizeof(mldh));
211 mldh.mld_type = type & 0xff;
212 mldh.mld_maxdelay = htons(QUERY_RESPONSE_INTERVAL);
213 mldh.mld_addr = *addr;
215 /* MLD packet should be advertised from linklocal address */
217 for (ifap = ifa; ifap; ifap = ifap->ifa_next) {
218 if (index != if_nametoindex(ifap->ifa_name))
221 if (ifap->ifa_addr->sa_family != AF_INET6)
223 if (!IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)
224 ifap->ifa_addr)->sin6_addr))
229 errx(1, "no linkocal address is available");
230 memcpy(&src, &((struct sockaddr_in6 *)ifap->ifa_addr)->sin6_addr,
234 /* remove embedded ifindex */
235 src.s6_addr[2] = src.s6_addr[3] = 0;
238 #ifdef USE_RFC2292BIS
239 if ((hbhlen = inet6_opt_init(NULL, 0)) == -1)
240 errx(1, "inet6_opt_init(0) failed");
241 if ((hbhlen = inet6_opt_append(NULL, 0, hbhlen, IP6OPT_ROUTER_ALERT, 2,
243 errx(1, "inet6_opt_append(0) failed");
244 if ((hbhlen = inet6_opt_finish(NULL, 0, hbhlen)) == -1)
245 errx(1, "inet6_opt_finish(0) failed");
246 cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(hbhlen);
248 hbhlen = sizeof(raopt);
249 cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
250 inet6_option_space(hbhlen);
253 if ((cmsgbuf = malloc(cmsglen)) == NULL)
254 errx(1, "can't allocate enough memory for cmsg");
255 cmsgp = (struct cmsghdr *)cmsgbuf;
256 m.msg_control = (caddr_t)cmsgbuf;
257 m.msg_controllen = cmsglen;
258 /* specify the outgoing interface */
259 cmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
260 cmsgp->cmsg_level = IPPROTO_IPV6;
261 cmsgp->cmsg_type = IPV6_PKTINFO;
262 pi = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
263 pi->ipi6_ifindex = index;
264 memcpy(&pi->ipi6_addr, &src, sizeof(pi->ipi6_addr));
265 /* specifiy to insert router alert option in a hop-by-hop opt hdr. */
266 cmsgp = CMSG_NXTHDR(&m, cmsgp);
267 #ifdef USE_RFC2292BIS
268 cmsgp->cmsg_len = CMSG_LEN(hbhlen);
269 cmsgp->cmsg_level = IPPROTO_IPV6;
270 cmsgp->cmsg_type = IPV6_HOPOPTS;
271 hbhbuf = CMSG_DATA(cmsgp);
272 if ((currentlen = inet6_opt_init(hbhbuf, hbhlen)) == -1)
273 errx(1, "inet6_opt_init(len = %d) failed", hbhlen);
274 if ((currentlen = inet6_opt_append(hbhbuf, hbhlen, currentlen,
275 IP6OPT_ROUTER_ALERT, 2,
277 errx(1, "inet6_opt_append(currentlen = %d, hbhlen = %d) failed",
279 (void)inet6_opt_set_val(optp, 0, &rtalert_code, sizeof(rtalert_code));
280 if ((currentlen = inet6_opt_finish(hbhbuf, hbhlen, currentlen)) == -1)
281 errx(1, "inet6_opt_finish(buf) failed");
282 #else /* old advanced API */
283 if (inet6_option_init((void *)cmsgp, &cmsgp, IPV6_HOPOPTS))
284 errx(1, "inet6_option_init failed\n");
285 raopt[0] = IP6OPT_ROUTER_ALERT;
286 raopt[1] = IP6OPT_RTALERT_LEN - 2;
287 memcpy(&raopt[2], (caddr_t)&rtalert_code, sizeof(u_short));
288 if (inet6_option_append(cmsgp, raopt, 4, 0))
289 errx(1, "inet6_option_append failed\n");
299 struct sockaddr_in6 from;
300 int from_len = sizeof(from);
303 if ((i = recvfrom(s, buf, sizeof(buf), 0,
304 (struct sockaddr *)&from,
308 if (i < sizeof(struct mld_hdr)) {
309 printf("too short!\n");
313 mld = (struct mld_hdr *)buf;
315 printf("from %s, ", inet_ntop(AF_INET6, &from.sin6_addr,
316 ntop_buf, sizeof(ntop_buf)));
318 switch (mld->mld_type) {
319 case ICMP6_MEMBERSHIP_QUERY:
320 printf("type=Multicast Listener Query, ");
322 case ICMP6_MEMBERSHIP_REPORT:
323 printf("type=Multicast Listener Report, ");
325 case ICMP6_MEMBERSHIP_REDUCTION:
326 printf("type=Multicast Listener Done, ");
329 printf("addr=%s\n", inet_ntop(AF_INET6, &mld->mld_addr,
330 ntop_buf, sizeof(ntop_buf)));
336 quit(int signum __unused)
338 mreq.ipv6mr_multiaddr = any;
339 mreq.ipv6mr_interface = ifindex;
340 if (setsockopt(s, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq,
342 err(1, "setsockopt(IPV6_LEAVE_GROUP)");
350 (void)fprintf(stderr, "usage: mld6query ifname [addr]\n");