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>
45 #include <net/if_var.h>
47 #include <netinet/in.h>
48 #include <netinet/ip6.h>
49 #include <netinet/icmp6.h>
51 #include <arpa/inet.h>
58 /* portability with older KAME headers */
59 #ifndef MLD_LISTENER_QUERY
60 #define MLD_LISTENER_QUERY MLD6_LISTENER_QUERY
61 #define MLD_LISTENER_REPORT MLD6_LISTENER_REPORT
62 #define MLD_LISTENER_DONE MLD6_LISTENER_DONE
63 #define MLD_MTRACE_RESP MLD6_MTRACE_RESP
64 #define MLD_MTRACE MLD6_MTRACE
65 #define mld_hdr mld6_hdr
66 #define mld_type mld6_type
67 #define mld_code mld6_code
68 #define mld_cksum mld6_cksum
69 #define mld_maxdelay mld6_maxdelay
70 #define mld_reserved mld6_reserved
71 #define mld_addr mld6_addr
73 #ifndef IP6OPT_ROUTER_ALERT
74 #define IP6OPT_ROUTER_ALERT IP6OPT_RTALERT
78 struct sockaddr_in6 dst;
80 struct in6_addr maddr = IN6ADDR_ANY_INIT, any = IN6ADDR_ANY_INIT;
81 struct ipv6_mreq mreq;
85 #define QUERY_RESPONSE_INTERVAL 10000
87 void make_msg(int index, struct in6_addr *addr, u_int type);
93 main(int argc, char *argv[])
96 struct icmp6_filter filt;
99 struct itimerval itimer;
103 type = MLD_LISTENER_QUERY;
104 while ((ch = getopt(argc, argv, "dr")) != -1) {
107 type = MLD_LISTENER_DONE;
110 type = MLD_LISTENER_REPORT;
121 if (argc != 1 && argc != 2)
124 ifindex = (u_short)if_nametoindex(argv[0]);
127 if (argc == 2 && inet_pton(AF_INET6, argv[1], &maddr) != 1)
130 if ((s = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
133 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hlim,
135 err(1, "setsockopt(IPV6_MULTICAST_HOPS)");
137 mreq.ipv6mr_multiaddr = any;
138 mreq.ipv6mr_interface = ifindex;
139 if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
141 err(1, "setsockopt(IPV6_JOIN_GROUP)");
143 ICMP6_FILTER_SETBLOCKALL(&filt);
144 ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_QUERY, &filt);
145 ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REPORT, &filt);
146 ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REDUCTION, &filt);
147 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
149 err(1, "setsockopt(ICMP6_FILTER)");
151 make_msg(ifindex, &maddr, type);
153 if (sendmsg(s, &m, 0) < 0)
156 itimer.it_value.tv_sec = QUERY_RESPONSE_INTERVAL / 1000;
157 itimer.it_interval.tv_sec = 0;
158 itimer.it_interval.tv_usec = 0;
159 itimer.it_value.tv_usec = 0;
161 (void)signal(SIGALRM, quit);
162 (void)setitimer(ITIMER_REAL, &itimer, NULL);
166 errx(1, "descriptor too big");
169 if ((i = select(s + 1, &fdset, NULL, NULL, NULL)) < 0)
179 make_msg(int index, struct in6_addr *addr, u_int type)
181 static struct iovec iov[2];
182 static u_char *cmsgbuf;
183 int cmsglen, hbhlen = 0;
184 #ifdef USE_RFC2292BIS
185 void *hbhbuf = NULL, *optp = NULL;
188 u_int8_t raopt[IP6OPT_RTALERT_LEN];
190 struct in6_pktinfo *pi;
191 struct cmsghdr *cmsgp;
192 u_short rtalert_code = htons(IP6OPT_RTALERT_MLD);
193 struct ifaddrs *ifa, *ifap;
196 dst.sin6_len = sizeof(dst);
197 dst.sin6_family = AF_INET6;
198 if (IN6_IS_ADDR_UNSPECIFIED(addr)) {
199 if (inet_pton(AF_INET6, "ff02::1", &dst.sin6_addr) != 1)
200 errx(1, "inet_pton failed");
203 dst.sin6_addr = *addr;
204 m.msg_name = (caddr_t)&dst;
205 m.msg_namelen = dst.sin6_len;
206 iov[0].iov_base = (caddr_t)&mldh;
207 iov[0].iov_len = sizeof(mldh);
211 bzero(&mldh, sizeof(mldh));
212 mldh.mld_type = type & 0xff;
213 mldh.mld_maxdelay = htons(QUERY_RESPONSE_INTERVAL);
214 mldh.mld_addr = *addr;
216 /* MLD packet should be advertised from linklocal address */
218 for (ifap = ifa; ifap; ifap = ifap->ifa_next) {
219 if (index != if_nametoindex(ifap->ifa_name))
222 if (ifap->ifa_addr->sa_family != AF_INET6)
224 if (!IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)
225 ifap->ifa_addr)->sin6_addr))
230 errx(1, "no linkocal address is available");
231 memcpy(&src, &((struct sockaddr_in6 *)ifap->ifa_addr)->sin6_addr,
235 /* remove embedded ifindex */
236 src.s6_addr[2] = src.s6_addr[3] = 0;
239 #ifdef USE_RFC2292BIS
240 if ((hbhlen = inet6_opt_init(NULL, 0)) == -1)
241 errx(1, "inet6_opt_init(0) failed");
242 if ((hbhlen = inet6_opt_append(NULL, 0, hbhlen, IP6OPT_ROUTER_ALERT, 2,
244 errx(1, "inet6_opt_append(0) failed");
245 if ((hbhlen = inet6_opt_finish(NULL, 0, hbhlen)) == -1)
246 errx(1, "inet6_opt_finish(0) failed");
247 cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(hbhlen);
249 hbhlen = sizeof(raopt);
250 cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
251 inet6_option_space(hbhlen);
254 if ((cmsgbuf = malloc(cmsglen)) == NULL)
255 errx(1, "can't allocate enough memory for cmsg");
256 cmsgp = (struct cmsghdr *)cmsgbuf;
257 m.msg_control = (caddr_t)cmsgbuf;
258 m.msg_controllen = cmsglen;
259 /* specify the outgoing interface */
260 cmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
261 cmsgp->cmsg_level = IPPROTO_IPV6;
262 cmsgp->cmsg_type = IPV6_PKTINFO;
263 pi = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
264 pi->ipi6_ifindex = index;
265 memcpy(&pi->ipi6_addr, &src, sizeof(pi->ipi6_addr));
266 /* specifiy to insert router alert option in a hop-by-hop opt hdr. */
267 cmsgp = CMSG_NXTHDR(&m, cmsgp);
268 #ifdef USE_RFC2292BIS
269 cmsgp->cmsg_len = CMSG_LEN(hbhlen);
270 cmsgp->cmsg_level = IPPROTO_IPV6;
271 cmsgp->cmsg_type = IPV6_HOPOPTS;
272 hbhbuf = CMSG_DATA(cmsgp);
273 if ((currentlen = inet6_opt_init(hbhbuf, hbhlen)) == -1)
274 errx(1, "inet6_opt_init(len = %d) failed", hbhlen);
275 if ((currentlen = inet6_opt_append(hbhbuf, hbhlen, currentlen,
276 IP6OPT_ROUTER_ALERT, 2,
278 errx(1, "inet6_opt_append(currentlen = %d, hbhlen = %d) failed",
280 (void)inet6_opt_set_val(optp, 0, &rtalert_code, sizeof(rtalert_code));
281 if ((currentlen = inet6_opt_finish(hbhbuf, hbhlen, currentlen)) == -1)
282 errx(1, "inet6_opt_finish(buf) failed");
283 #else /* old advanced API */
284 if (inet6_option_init((void *)cmsgp, &cmsgp, IPV6_HOPOPTS))
285 errx(1, "inet6_option_init failed\n");
286 raopt[0] = IP6OPT_ROUTER_ALERT;
287 raopt[1] = IP6OPT_RTALERT_LEN - 2;
288 memcpy(&raopt[2], (caddr_t)&rtalert_code, sizeof(u_short));
289 if (inet6_option_append(cmsgp, raopt, 4, 0))
290 errx(1, "inet6_option_append failed\n");
300 struct sockaddr_in6 from;
301 int from_len = sizeof(from);
304 if ((i = recvfrom(s, buf, sizeof(buf), 0,
305 (struct sockaddr *)&from,
309 if (i < sizeof(struct mld_hdr)) {
310 printf("too short!\n");
314 mld = (struct mld_hdr *)buf;
316 printf("from %s, ", inet_ntop(AF_INET6, &from.sin6_addr,
317 ntop_buf, sizeof(ntop_buf)));
319 switch (mld->mld_type) {
320 case ICMP6_MEMBERSHIP_QUERY:
321 printf("type=Multicast Listener Query, ");
323 case ICMP6_MEMBERSHIP_REPORT:
324 printf("type=Multicast Listener Report, ");
326 case ICMP6_MEMBERSHIP_REDUCTION:
327 printf("type=Multicast Listener Done, ");
330 printf("addr=%s\n", inet_ntop(AF_INET6, &mld->mld_addr,
331 ntop_buf, sizeof(ntop_buf)));
337 quit(int signum __unused)
339 mreq.ipv6mr_multiaddr = any;
340 mreq.ipv6mr_interface = ifindex;
341 if (setsockopt(s, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq,
343 err(1, "setsockopt(IPV6_LEAVE_GROUP)");
351 (void)fprintf(stderr, "usage: mld6query ifname [addr]\n");