1 /* $KAME: mld6.c,v 1.11 2001/05/13 15:45:07 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>
41 #include <net/if_var.h>
43 #include <netinet/in.h>
44 #include <netinet/ip6.h>
45 #include <netinet/icmp6.h>
47 #include <arpa/inet.h>
55 struct sockaddr_in6 dst;
57 struct in6_addr maddr = IN6ADDR_ANY_INIT, any = IN6ADDR_ANY_INIT;
58 struct ipv6_mreq mreq;
62 #define QUERY_RESPONSE_INTERVAL 10000
64 void make_msg(int index, struct in6_addr *addr, u_int type);
70 main(int argc, char *argv[])
73 struct icmp6_filter filt;
76 struct itimerval itimer;
80 type = MLD6_LISTENER_QUERY;
81 while ((ch = getopt(argc, argv, "dr")) != -1) {
84 type = MLD6_LISTENER_DONE;
87 type = MLD6_LISTENER_REPORT;
98 if (argc != 1 && argc != 2)
101 ifindex = (u_short)if_nametoindex(argv[0]);
104 if (argc == 2 && inet_pton(AF_INET6, argv[1], &maddr) != 1)
107 if ((s = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
110 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hlim,
112 err(1, "setsockopt(IPV6_MULTICAST_HOPS)");
114 mreq.ipv6mr_multiaddr = any;
115 mreq.ipv6mr_interface = ifindex;
116 if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
118 err(1, "setsockopt(IPV6_JOIN_GROUP)");
120 ICMP6_FILTER_SETBLOCKALL(&filt);
121 ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_QUERY, &filt);
122 ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REPORT, &filt);
123 ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REDUCTION, &filt);
124 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
126 err(1, "setsockopt(ICMP6_FILTER)");
128 make_msg(ifindex, &maddr, type);
130 if (sendmsg(s, &m, 0) < 0)
133 itimer.it_value.tv_sec = QUERY_RESPONSE_INTERVAL / 1000;
134 itimer.it_interval.tv_sec = 0;
135 itimer.it_interval.tv_usec = 0;
136 itimer.it_value.tv_usec = 0;
138 (void)signal(SIGALRM, quit);
139 (void)setitimer(ITIMER_REAL, &itimer, NULL);
144 if ((i = select(s + 1, &fdset, NULL, NULL, NULL)) < 0)
154 make_msg(int index, struct in6_addr *addr, u_int type)
156 static struct iovec iov[2];
157 static u_char *cmsgbuf;
158 int cmsglen, hbhlen = 0;
159 u_int8_t raopt[IP6OPT_RTALERT_LEN];
160 struct in6_pktinfo *pi;
161 struct cmsghdr *cmsgp;
162 u_short rtalert_code = htons(IP6OPT_RTALERT_MLD);
164 dst.sin6_len = sizeof(dst);
165 dst.sin6_family = AF_INET6;
166 if (IN6_IS_ADDR_UNSPECIFIED(addr)) {
167 if (inet_pton(AF_INET6, "ff02::1", &dst.sin6_addr) != 1)
168 errx(1, "inet_pton failed");
171 dst.sin6_addr = *addr;
172 m.msg_name = (caddr_t)&dst;
173 m.msg_namelen = dst.sin6_len;
174 iov[0].iov_base = (caddr_t)&mldh;
175 iov[0].iov_len = sizeof(mldh);
179 bzero(&mldh, sizeof(mldh));
180 mldh.mld6_type = type & 0xff;
181 mldh.mld6_maxdelay = htons(QUERY_RESPONSE_INTERVAL);
182 mldh.mld6_addr = *addr;
184 hbhlen = sizeof(raopt);
185 cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
186 inet6_option_space(hbhlen);
188 if ((cmsgbuf = malloc(cmsglen)) == NULL)
189 errx(1, "can't allocate enough memory for cmsg");
190 cmsgp = (struct cmsghdr *)cmsgbuf;
191 m.msg_control = (caddr_t)cmsgbuf;
192 m.msg_controllen = cmsglen;
193 /* specify the outgoing interface */
194 cmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
195 cmsgp->cmsg_level = IPPROTO_IPV6;
196 cmsgp->cmsg_type = IPV6_PKTINFO;
197 pi = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
198 pi->ipi6_ifindex = index;
199 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr));
200 /* specifiy to insert router alert option in a hop-by-hop opt hdr. */
201 cmsgp = CMSG_NXTHDR(&m, cmsgp);
202 if (inet6_option_init((void *)cmsgp, &cmsgp, IPV6_HOPOPTS))
203 errx(1, "inet6_option_init failed\n");
204 raopt[0] = IP6OPT_RTALERT;
205 raopt[1] = IP6OPT_RTALERT_LEN - 2;
206 memcpy(&raopt[2], (caddr_t)&rtalert_code, sizeof(u_short));
207 if (inet6_option_append(cmsgp, raopt, 4, 0))
208 errx(1, "inet6_option_append failed\n");
215 struct mld6_hdr *mld;
217 struct sockaddr_in6 from;
218 int from_len = sizeof(from);
221 if ((i = recvfrom(s, buf, sizeof(buf), 0,
222 (struct sockaddr *)&from,
226 if (i < sizeof(struct mld6_hdr)) {
227 printf("too short!\n");
231 mld = (struct mld6_hdr *)buf;
233 printf("from %s, ", inet_ntop(AF_INET6, &from.sin6_addr,
234 ntop_buf, sizeof(ntop_buf)));
236 switch (mld->mld6_type) {
237 case ICMP6_MEMBERSHIP_QUERY:
238 printf("type=Multicast Listener Query, ");
240 case ICMP6_MEMBERSHIP_REPORT:
241 printf("type=Multicast Listener Report, ");
243 case ICMP6_MEMBERSHIP_REDUCTION:
244 printf("type=Multicast Listener Done, ");
247 printf("addr=%s\n", inet_ntop(AF_INET6, &mld->mld6_addr,
248 ntop_buf, sizeof(ntop_buf)));
256 mreq.ipv6mr_multiaddr = any;
257 mreq.ipv6mr_interface = ifindex;
258 if (setsockopt(s, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq,
260 err(1, "setsockopt(IPV6_LEAVE_GROUP)");
268 (void)fprintf(stderr, "usage: mld6query ifname [addr]\n");