1 /* $KAME: mld6.c,v 1.15 2003/04/02 11:29:54 suz Exp $ */
5 * Copyright (C) 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <sys/types.h>
42 #include <net/if_var.h>
44 #include <netinet/in.h>
45 #include <netinet/ip6.h>
46 #include <netinet/icmp6.h>
48 #include <arpa/inet.h>
55 /* portability with older KAME headers */
56 #ifndef MLD_LISTENER_QUERY
57 #define MLD_LISTENER_QUERY MLD6_LISTENER_QUERY
58 #define MLD_LISTENER_REPORT MLD6_LISTENER_REPORT
59 #define MLD_LISTENER_DONE MLD6_LISTENER_DONE
60 #define MLD_MTRACE_RESP MLD6_MTRACE_RESP
61 #define MLD_MTRACE MLD6_MTRACE
62 #define mld_hdr mld6_hdr
63 #define mld_type mld6_type
64 #define mld_code mld6_code
65 #define mld_cksum mld6_cksum
66 #define mld_maxdelay mld6_maxdelay
67 #define mld_reserved mld6_reserved
68 #define mld_addr mld6_addr
70 #ifndef IP6OPT_ROUTER_ALERT
71 #define IP6OPT_ROUTER_ALERT IP6OPT_RTALERT
75 struct sockaddr_in6 dst;
77 struct in6_addr maddr = IN6ADDR_ANY_INIT, any = IN6ADDR_ANY_INIT;
78 struct ipv6_mreq mreq;
82 #define QUERY_RESPONSE_INTERVAL 10000
84 void make_msg(int index, struct in6_addr *addr, u_int type);
90 main(int argc, char *argv[])
93 struct icmp6_filter filt;
96 struct itimerval itimer;
100 type = MLD_LISTENER_QUERY;
101 while ((ch = getopt(argc, argv, "dr")) != -1) {
104 type = MLD_LISTENER_DONE;
107 type = MLD_LISTENER_REPORT;
118 if (argc != 1 && argc != 2)
121 ifindex = (u_short)if_nametoindex(argv[0]);
124 if (argc == 2 && inet_pton(AF_INET6, argv[1], &maddr) != 1)
127 if ((s = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
130 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hlim,
132 err(1, "setsockopt(IPV6_MULTICAST_HOPS)");
134 mreq.ipv6mr_multiaddr = any;
135 mreq.ipv6mr_interface = ifindex;
136 if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
138 err(1, "setsockopt(IPV6_JOIN_GROUP)");
140 ICMP6_FILTER_SETBLOCKALL(&filt);
141 ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_QUERY, &filt);
142 ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REPORT, &filt);
143 ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REDUCTION, &filt);
144 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
146 err(1, "setsockopt(ICMP6_FILTER)");
148 make_msg(ifindex, &maddr, type);
150 if (sendmsg(s, &m, 0) < 0)
153 itimer.it_value.tv_sec = QUERY_RESPONSE_INTERVAL / 1000;
154 itimer.it_interval.tv_sec = 0;
155 itimer.it_interval.tv_usec = 0;
156 itimer.it_value.tv_usec = 0;
158 (void)signal(SIGALRM, quit);
159 (void)setitimer(ITIMER_REAL, &itimer, NULL);
163 errx(1, "descriptor too big");
166 if ((i = select(s + 1, &fdset, NULL, NULL, NULL)) < 0)
176 make_msg(int index, struct in6_addr *addr, u_int type)
178 static struct iovec iov[2];
179 static u_char *cmsgbuf;
180 int cmsglen, hbhlen = 0;
181 #ifdef USE_RFC2292BIS
182 void *hbhbuf = NULL, *optp = NULL;
185 u_int8_t raopt[IP6OPT_RTALERT_LEN];
187 struct in6_pktinfo *pi;
188 struct cmsghdr *cmsgp;
189 u_short rtalert_code = htons(IP6OPT_RTALERT_MLD);
190 struct ifaddrs *ifa, *ifap;
193 dst.sin6_len = sizeof(dst);
194 dst.sin6_family = AF_INET6;
195 if (IN6_IS_ADDR_UNSPECIFIED(addr)) {
196 if (inet_pton(AF_INET6, "ff02::1", &dst.sin6_addr) != 1)
197 errx(1, "inet_pton failed");
200 dst.sin6_addr = *addr;
201 m.msg_name = (caddr_t)&dst;
202 m.msg_namelen = dst.sin6_len;
203 iov[0].iov_base = (caddr_t)&mldh;
204 iov[0].iov_len = sizeof(mldh);
208 bzero(&mldh, sizeof(mldh));
209 mldh.mld_type = type & 0xff;
210 mldh.mld_maxdelay = htons(QUERY_RESPONSE_INTERVAL);
211 mldh.mld_addr = *addr;
213 /* MLD packet should be advertised from linklocal address */
215 for (ifap = ifa; ifap; ifap = ifap->ifa_next) {
216 if (index != if_nametoindex(ifap->ifa_name))
219 if (ifap->ifa_addr->sa_family != AF_INET6)
221 if (!IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)
222 ifap->ifa_addr)->sin6_addr))
227 errx(1, "no linkocal address is available");
228 memcpy(&src, &((struct sockaddr_in6 *)ifap->ifa_addr)->sin6_addr,
232 /* remove embedded ifindex */
233 src.s6_addr[2] = src.s6_addr[3] = 0;
236 #ifdef USE_RFC2292BIS
237 if ((hbhlen = inet6_opt_init(NULL, 0)) == -1)
238 errx(1, "inet6_opt_init(0) failed");
239 if ((hbhlen = inet6_opt_append(NULL, 0, hbhlen, IP6OPT_ROUTER_ALERT, 2,
241 errx(1, "inet6_opt_append(0) failed");
242 if ((hbhlen = inet6_opt_finish(NULL, 0, hbhlen)) == -1)
243 errx(1, "inet6_opt_finish(0) failed");
244 cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(hbhlen);
246 hbhlen = sizeof(raopt);
247 cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
248 inet6_option_space(hbhlen);
251 if ((cmsgbuf = malloc(cmsglen)) == NULL)
252 errx(1, "can't allocate enough memory for cmsg");
253 cmsgp = (struct cmsghdr *)cmsgbuf;
254 m.msg_control = (caddr_t)cmsgbuf;
255 m.msg_controllen = cmsglen;
256 /* specify the outgoing interface */
257 cmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
258 cmsgp->cmsg_level = IPPROTO_IPV6;
259 cmsgp->cmsg_type = IPV6_PKTINFO;
260 pi = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
261 pi->ipi6_ifindex = index;
262 memcpy(&pi->ipi6_addr, &src, sizeof(pi->ipi6_addr));
263 /* specifiy to insert router alert option in a hop-by-hop opt hdr. */
264 cmsgp = CMSG_NXTHDR(&m, cmsgp);
265 #ifdef USE_RFC2292BIS
266 cmsgp->cmsg_len = CMSG_LEN(hbhlen);
267 cmsgp->cmsg_level = IPPROTO_IPV6;
268 cmsgp->cmsg_type = IPV6_HOPOPTS;
269 hbhbuf = CMSG_DATA(cmsgp);
270 if ((currentlen = inet6_opt_init(hbhbuf, hbhlen)) == -1)
271 errx(1, "inet6_opt_init(len = %d) failed", hbhlen);
272 if ((currentlen = inet6_opt_append(hbhbuf, hbhlen, currentlen,
273 IP6OPT_ROUTER_ALERT, 2,
275 errx(1, "inet6_opt_append(currentlen = %d, hbhlen = %d) failed",
277 (void)inet6_opt_set_val(optp, 0, &rtalert_code, sizeof(rtalert_code));
278 if ((currentlen = inet6_opt_finish(hbhbuf, hbhlen, currentlen)) == -1)
279 errx(1, "inet6_opt_finish(buf) failed");
280 #else /* old advanced API */
281 if (inet6_option_init((void *)cmsgp, &cmsgp, IPV6_HOPOPTS))
282 errx(1, "inet6_option_init failed\n");
283 raopt[0] = IP6OPT_ROUTER_ALERT;
284 raopt[1] = IP6OPT_RTALERT_LEN - 2;
285 memcpy(&raopt[2], (caddr_t)&rtalert_code, sizeof(u_short));
286 if (inet6_option_append(cmsgp, raopt, 4, 0))
287 errx(1, "inet6_option_append failed\n");
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)));
337 mreq.ipv6mr_multiaddr = any;
338 mreq.ipv6mr_interface = ifindex;
339 if (setsockopt(s, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq,
341 err(1, "setsockopt(IPV6_LEAVE_GROUP)");
349 (void)fprintf(stderr, "usage: mld6query ifname [addr]\n");