1 /* $NetBSD: mld6.c,v 1.12 2006/05/09 20:18:09 mrg Exp $ */
2 /* $KAME: mld6.c,v 1.9 2000/12/04 06:29:37 itojun 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 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
43 #include <net/if_var.h>
44 #endif /* __FreeBSD__ >= 3 */
46 #include <netinet/in.h>
47 #include <netinet/ip6.h>
48 #include <netinet/icmp6.h>
50 #include <arpa/inet.h>
58 struct sockaddr_in6 dst
;
60 struct in6_addr maddr
= IN6ADDR_ANY_INIT
, any
= IN6ADDR_ANY_INIT
;
61 struct ipv6_mreq mreq
;
65 #define QUERY_RESPONSE_INTERVAL 10000
67 void make_msg(int index
, struct in6_addr
*addr
, u_int type
);
73 main(int argc
, char *argv
[])
76 struct icmp6_filter filt
;
79 struct itimerval itimer
;
83 type
= MLD_LISTENER_QUERY
;
84 while ((ch
= getopt(argc
, argv
, "d")) != -1) {
87 type
= MLD_LISTENER_DONE
;
90 type
= MLD_LISTENER_REPORT
;
101 if (argc
!= 1 && argc
!= 2)
104 ifindex
= (u_short
)if_nametoindex(argv
[0]);
107 if (argc
== 3 && inet_pton(AF_INET6
, argv
[1], &maddr
) != 1)
110 if ((sock
= socket(AF_INET6
, SOCK_RAW
, IPPROTO_ICMPV6
)) < 0)
113 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
, &hlim
,
115 err(1, "setsockopt(IPV6_MULTICAST_HOPS)");
117 mreq
.ipv6mr_multiaddr
= any
;
118 mreq
.ipv6mr_interface
= ifindex
;
119 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
, &mreq
,
121 err(1, "setsockopt(IPV6_JOIN_GROUP)");
123 ICMP6_FILTER_SETBLOCKALL(&filt
);
124 ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_QUERY
, &filt
);
125 ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REPORT
, &filt
);
126 ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REDUCTION
, &filt
);
127 if (setsockopt(sock
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
,
129 err(1, "setsockopt(ICMP6_FILTER)");
131 make_msg(ifindex
, &maddr
, type
);
133 if (sendmsg(sock
, &m
, 0) < 0)
136 itimer
.it_value
.tv_sec
= QUERY_RESPONSE_INTERVAL
/ 1000;
137 itimer
.it_interval
.tv_sec
= 0;
138 itimer
.it_interval
.tv_usec
= 0;
139 itimer
.it_value
.tv_usec
= 0;
141 (void)signal(SIGALRM
, quit
);
142 (void)setitimer(ITIMER_REAL
, &itimer
, NULL
);
145 set
[0].events
= POLLIN
;
147 if ((i
= poll(set
, 1, INFTIM
)) < 0)
157 make_msg(int idx
, struct in6_addr
*addr
, u_int type
)
159 static struct iovec iov
[2];
160 static u_char
*cmsgbuf
;
161 int cmsglen
, hbhlen
= 0;
162 #ifdef USE_RFC2292BIS
163 void *hbhbuf
= NULL
, *optp
= NULL
;
166 u_int8_t raopt
[IP6OPT_RTALERT_LEN
];
168 struct in6_pktinfo
*pi
;
169 struct cmsghdr
*cmsgp
;
170 u_short rtalert_code
= htons(IP6OPT_RTALERT_MLD
);
172 dst
.sin6_len
= sizeof(dst
);
173 dst
.sin6_family
= AF_INET6
;
174 if (IN6_IS_ADDR_UNSPECIFIED(addr
)) {
175 if (inet_pton(AF_INET6
, "ff02::1", &dst
.sin6_addr
) != 1)
176 errx(1, "inet_pton failed");
179 dst
.sin6_addr
= *addr
;
180 m
.msg_name
= (caddr_t
)&dst
;
181 m
.msg_namelen
= dst
.sin6_len
;
182 iov
[0].iov_base
= (caddr_t
)&mldh
;
183 iov
[0].iov_len
= sizeof(mldh
);
187 bzero(&mldh
, sizeof(mldh
));
188 mldh
.mld6_type
= type
& 0xff;
189 mldh
.mld6_maxdelay
= htons(QUERY_RESPONSE_INTERVAL
);
190 mldh
.mld6_addr
= *addr
;
192 #ifdef USE_RFC2292BIS
193 if ((hbhlen
= inet6_opt_init(NULL
, 0)) == -1)
194 errx(1, "inet6_opt_init(0) failed");
195 if ((hbhlen
= inet6_opt_append(NULL
, 0, hbhlen
, IP6OPT_ROUTER_ALERT
, 2,
197 errx(1, "inet6_opt_append(0) failed");
198 if ((hbhlen
= inet6_opt_finish(NULL
, 0, hbhlen
)) == -1)
199 errx(1, "inet6_opt_finish(0) failed");
200 cmsglen
= CMSG_SPACE(sizeof(struct in6_pktinfo
)) + CMSG_SPACE(hbhlen
);
202 hbhlen
= sizeof(raopt
);
203 cmsglen
= CMSG_SPACE(sizeof(struct in6_pktinfo
)) +
204 inet6_option_space(hbhlen
);
207 if ((cmsgbuf
= malloc(cmsglen
)) == NULL
)
208 errx(1, "can't allocate enough memory for cmsg");
209 cmsgp
= (struct cmsghdr
*)cmsgbuf
;
210 m
.msg_control
= (caddr_t
)cmsgbuf
;
211 m
.msg_controllen
= cmsglen
;
212 /* specify the outgoing interface */
213 cmsgp
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
214 cmsgp
->cmsg_level
= IPPROTO_IPV6
;
215 cmsgp
->cmsg_type
= IPV6_PKTINFO
;
216 pi
= (struct in6_pktinfo
*)CMSG_DATA(cmsgp
);
217 pi
->ipi6_ifindex
= idx
;
218 memset(&pi
->ipi6_addr
, 0, sizeof(pi
->ipi6_addr
));
219 /* specifiy to insert router alert option in a hop-by-hop opt hdr. */
220 cmsgp
= CMSG_NXTHDR(&m
, cmsgp
);
221 #ifdef USE_RFC2292BIS
222 cmsgp
->cmsg_len
= CMSG_LEN(hbhlen
);
223 cmsgp
->cmsg_level
= IPPROTO_IPV6
;
224 cmsgp
->cmsg_type
= IPV6_HOPOPTS
;
225 hbhbuf
= CMSG_DATA(cmsgp
);
226 if ((currentlen
= inet6_opt_init(hbhbuf
, hbhlen
)) == -1)
227 errx(1, "inet6_opt_init(len = %d) failed", hbhlen
);
228 if ((currentlen
= inet6_opt_append(hbhbuf
, hbhlen
, currentlen
,
229 IP6OPT_ROUTER_ALERT
, 2,
231 errx(1, "inet6_opt_append(currentlen = %d, hbhlen = %d) failed",
233 (void)inet6_opt_set_val(optp
, 0, &rtalert_code
, sizeof(rtalert_code
));
234 if ((currentlen
= inet6_opt_finish(hbhbuf
, hbhlen
, currentlen
)) == -1)
235 errx(1, "inet6_opt_finish(buf) failed");
236 #else /* old advanced API */
237 if (inet6_option_init((void *)cmsgp
, &cmsgp
, IPV6_HOPOPTS
))
238 errx(1, "inet6_option_init failed");
239 raopt
[0] = IP6OPT_RTALERT
;
240 raopt
[1] = IP6OPT_RTALERT_LEN
- 2;
241 memcpy(&raopt
[2], (caddr_t
)&rtalert_code
, sizeof(u_short
));
242 if (inet6_option_append(cmsgp
, raopt
, 4, 0))
243 errx(1, "inet6_option_append failed");
251 struct mld6_hdr
*mld
;
253 struct sockaddr_in6 from
;
254 socklen_t from_len
= sizeof(from
);
257 if ((i
= recvfrom(s
, buf
, sizeof(buf
), 0,
258 (struct sockaddr
*)&from
,
262 if (i
< (int)sizeof(struct mld6_hdr
)) {
263 printf("too short!\n");
267 mld
= (struct mld6_hdr
*)buf
;
269 printf("from %s, ", inet_ntop(AF_INET6
, &from
.sin6_addr
,
270 ntop_buf
, sizeof(ntop_buf
)));
272 switch (mld
->mld6_type
) {
273 case ICMP6_MEMBERSHIP_QUERY
:
274 printf("type=Multicast Listener Query, ");
276 case ICMP6_MEMBERSHIP_REPORT
:
277 printf("type=Multicast Listener Report, ");
279 case ICMP6_MEMBERSHIP_REDUCTION
:
280 printf("type=Multicast Listener Done, ");
283 printf("addr=%s\n", inet_ntop(AF_INET6
, &mld
->mld6_addr
,
284 ntop_buf
, sizeof(ntop_buf
)));
292 mreq
.ipv6mr_multiaddr
= any
;
293 mreq
.ipv6mr_interface
= ifindex
;
294 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_LEAVE_GROUP
, &mreq
,
296 err(1, "setsockopt(IPV6_LEAVE_GROUP)");
304 (void)fprintf(stderr
, "usage: %s [-dr] ifname [addr]\n", getprogname());