1 /* $KAME: mld6.c,v 1.15 2003/04/02 11:29:54 suz Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 * Copyright (C) 1998 WIDE Project.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include <sys/param.h>
35 #include <sys/uio.h>
36 #include <sys/socket.h>
37 #include <sys/types.h>
38 #include <sys/time.h>
39 #include <ifaddrs.h>
40 #include <unistd.h>
41 #include <signal.h>
42
43 #include <net/if.h>
44
45 #include <netinet/in.h>
46 #include <netinet/ip6.h>
47 #include <netinet/icmp6.h>
48
49 #include <arpa/inet.h>
50
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <err.h>
55
56 /* portability with older KAME headers */
57 #ifndef MLD_LISTENER_QUERY
58 #define MLD_LISTENER_QUERY MLD6_LISTENER_QUERY
59 #define MLD_LISTENER_REPORT MLD6_LISTENER_REPORT
60 #define MLD_LISTENER_DONE MLD6_LISTENER_DONE
61 #define MLD_MTRACE_RESP MLD6_MTRACE_RESP
62 #define MLD_MTRACE MLD6_MTRACE
63 #define mld_hdr mld6_hdr
64 #define mld_type mld6_type
65 #define mld_code mld6_code
66 #define mld_cksum mld6_cksum
67 #define mld_maxdelay mld6_maxdelay
68 #define mld_reserved mld6_reserved
69 #define mld_addr mld6_addr
70 #endif
71 #ifndef IP6OPT_ROUTER_ALERT
72 #define IP6OPT_ROUTER_ALERT IP6OPT_RTALERT
73 #endif
74
75 struct msghdr m;
76 struct sockaddr_in6 dst;
77 struct mld_hdr mldh;
78 struct in6_addr maddr = IN6ADDR_ANY_INIT, any = IN6ADDR_ANY_INIT;
79 struct ipv6_mreq mreq;
80 u_short ifindex;
81 int s;
82
83 #define QUERY_RESPONSE_INTERVAL 10000
84
85 void make_msg(int index, struct in6_addr *addr, u_int type, struct in6_addr *qaddr);
86 void usage(void);
87 void dump(int);
88 void quit(int);
89
90 int
main(int argc,char * argv[])91 main(int argc, char *argv[])
92 {
93 int i;
94 struct icmp6_filter filt;
95 u_int hlim = 1;
96 fd_set fdset;
97 struct itimerval itimer;
98 u_int type;
99 int ch;
100 struct in6_addr *qaddr = &maddr;
101
102 type = MLD_LISTENER_QUERY;
103 while ((ch = getopt(argc, argv, "dgr")) != -1) {
104 switch (ch) {
105 case 'd':
106 if (type != MLD_LISTENER_QUERY) {
107 printf("Can not specify -d with -r\n");
108 return 1;
109 }
110 type = MLD_LISTENER_DONE;
111 break;
112 case 'g':
113 qaddr = &any;
114 break;
115 case 'r':
116 if (type != MLD_LISTENER_QUERY) {
117 printf("Can not specify -r with -d\n");
118 return 1;
119 }
120 type = MLD_LISTENER_REPORT;
121 break;
122 default:
123 usage();
124 /*NOTREACHED*/
125 }
126 }
127
128 argv += optind;
129 argc -= optind;
130
131 if (argc != 1 && argc != 2)
132 usage();
133
134 ifindex = (u_short)if_nametoindex(argv[0]);
135 if (ifindex == 0)
136 usage();
137 if (argc == 2 && inet_pton(AF_INET6, argv[1], &maddr) != 1)
138 usage();
139 if (type != MLD_LISTENER_QUERY && qaddr != &maddr) {
140 printf("Can not specify -g with -d or -r\n");
141 return 1;
142 }
143
144 if ((s = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
145 err(1, "socket");
146
147 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hlim,
148 sizeof(hlim)) == -1)
149 err(1, "setsockopt(IPV6_MULTICAST_HOPS)");
150
151 if (IN6_IS_ADDR_UNSPECIFIED(&maddr)) {
152 if (inet_pton(AF_INET6, "ff02::1", &maddr) != 1)
153 errx(1, "inet_pton failed");
154 }
155
156 mreq.ipv6mr_multiaddr = maddr;
157 mreq.ipv6mr_interface = ifindex;
158 if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
159 sizeof(mreq)) == -1)
160 err(1, "setsockopt(IPV6_JOIN_GROUP)");
161
162 ICMP6_FILTER_SETBLOCKALL(&filt);
163 ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_QUERY, &filt);
164 ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REPORT, &filt);
165 ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REDUCTION, &filt);
166 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
167 sizeof(filt)) < 0)
168 err(1, "setsockopt(ICMP6_FILTER)");
169
170 make_msg(ifindex, &maddr, type, qaddr);
171
172 if (sendmsg(s, &m, 0) < 0)
173 err(1, "sendmsg");
174
175 itimer.it_value.tv_sec = QUERY_RESPONSE_INTERVAL / 1000;
176 itimer.it_interval.tv_sec = 0;
177 itimer.it_interval.tv_usec = 0;
178 itimer.it_value.tv_usec = 0;
179
180 (void)signal(SIGALRM, quit);
181 (void)setitimer(ITIMER_REAL, &itimer, NULL);
182
183 FD_ZERO(&fdset);
184 if (s >= FD_SETSIZE)
185 errx(1, "descriptor too big");
186 for (;;) {
187 FD_SET(s, &fdset);
188 if ((i = select(s + 1, &fdset, NULL, NULL, NULL)) < 0)
189 perror("select");
190 if (i == 0)
191 continue;
192 else
193 dump(s);
194 }
195 }
196
197 void
make_msg(int index,struct in6_addr * addr,u_int type,struct in6_addr * qaddr)198 make_msg(int index, struct in6_addr *addr, u_int type, struct in6_addr *qaddr)
199 {
200 static struct iovec iov[2];
201 static u_char *cmsgbuf;
202 int cmsglen, hbhlen = 0;
203 void *hbhbuf = NULL, *optp = NULL;
204 int currentlen;
205 struct in6_pktinfo *pi;
206 struct cmsghdr *cmsgp;
207 u_short rtalert_code = htons(IP6OPT_RTALERT_MLD);
208 struct ifaddrs *ifa, *ifap;
209 struct in6_addr src;
210
211 dst.sin6_len = sizeof(dst);
212 dst.sin6_family = AF_INET6;
213 dst.sin6_addr = *addr;
214 m.msg_name = (caddr_t)&dst;
215 m.msg_namelen = dst.sin6_len;
216 iov[0].iov_base = (caddr_t)&mldh;
217 iov[0].iov_len = sizeof(mldh);
218 m.msg_iov = iov;
219 m.msg_iovlen = 1;
220
221 bzero(&mldh, sizeof(mldh));
222 mldh.mld_type = type & 0xff;
223 mldh.mld_maxdelay = htons(QUERY_RESPONSE_INTERVAL);
224 mldh.mld_addr = *qaddr;
225
226 /* MLD packet should be advertised from linklocal address */
227 getifaddrs(&ifa);
228 for (ifap = ifa; ifap; ifap = ifap->ifa_next) {
229 if (index != if_nametoindex(ifap->ifa_name))
230 continue;
231
232 if (ifap->ifa_addr->sa_family != AF_INET6)
233 continue;
234 if (!IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)
235 ifap->ifa_addr)->sin6_addr))
236 continue;
237 break;
238 }
239 if (ifap == NULL)
240 errx(1, "no linklocal address is available");
241 memcpy(&src, &((struct sockaddr_in6 *)ifap->ifa_addr)->sin6_addr,
242 sizeof(src));
243 freeifaddrs(ifa);
244 #ifdef __KAME__
245 /* remove embedded ifindex */
246 src.s6_addr[2] = src.s6_addr[3] = 0;
247 #endif
248
249 if ((hbhlen = inet6_opt_init(NULL, 0)) == -1)
250 errx(1, "inet6_opt_init(0) failed");
251 if ((hbhlen = inet6_opt_append(NULL, 0, hbhlen, IP6OPT_ROUTER_ALERT, 2,
252 2, NULL)) == -1)
253 errx(1, "inet6_opt_append(0) failed");
254 if ((hbhlen = inet6_opt_finish(NULL, 0, hbhlen)) == -1)
255 errx(1, "inet6_opt_finish(0) failed");
256 cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(hbhlen);
257
258 if ((cmsgbuf = malloc(cmsglen)) == NULL)
259 errx(1, "can't allocate enough memory for cmsg");
260 cmsgp = (struct cmsghdr *)cmsgbuf;
261 m.msg_control = (caddr_t)cmsgbuf;
262 m.msg_controllen = cmsglen;
263 /* specify the outgoing interface */
264 cmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
265 cmsgp->cmsg_level = IPPROTO_IPV6;
266 cmsgp->cmsg_type = IPV6_PKTINFO;
267 pi = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
268 pi->ipi6_ifindex = index;
269 memcpy(&pi->ipi6_addr, &src, sizeof(pi->ipi6_addr));
270 /* specify to insert router alert option in a hop-by-hop opt hdr. */
271 cmsgp = CMSG_NXTHDR(&m, cmsgp);
272 cmsgp->cmsg_len = CMSG_LEN(hbhlen);
273 cmsgp->cmsg_level = IPPROTO_IPV6;
274 cmsgp->cmsg_type = IPV6_HOPOPTS;
275 hbhbuf = CMSG_DATA(cmsgp);
276 if ((currentlen = inet6_opt_init(hbhbuf, hbhlen)) == -1)
277 errx(1, "inet6_opt_init(len = %d) failed", hbhlen);
278 if ((currentlen = inet6_opt_append(hbhbuf, hbhlen, currentlen,
279 IP6OPT_ROUTER_ALERT, 2,
280 2, &optp)) == -1)
281 errx(1, "inet6_opt_append(currentlen = %d, hbhlen = %d) failed",
282 currentlen, hbhlen);
283 (void)inet6_opt_set_val(optp, 0, &rtalert_code, sizeof(rtalert_code));
284 if ((currentlen = inet6_opt_finish(hbhbuf, hbhlen, currentlen)) == -1)
285 errx(1, "inet6_opt_finish(buf) failed");
286 }
287
288 void
dump(int s)289 dump(int s)
290 {
291 int i;
292 struct mld_hdr *mld;
293 u_char buf[1024];
294 struct sockaddr_in6 from;
295 int from_len = sizeof(from);
296 char ntop_buf[256];
297
298 if ((i = recvfrom(s, buf, sizeof(buf), 0,
299 (struct sockaddr *)&from,
300 &from_len)) < 0)
301 return;
302
303 if (i < sizeof(struct mld_hdr)) {
304 printf("too short!\n");
305 return;
306 }
307
308 mld = (struct mld_hdr *)buf;
309
310 printf("from %s, ", inet_ntop(AF_INET6, &from.sin6_addr,
311 ntop_buf, sizeof(ntop_buf)));
312
313 switch (mld->mld_type) {
314 case ICMP6_MEMBERSHIP_QUERY:
315 printf("type=Multicast Listener Query, ");
316 break;
317 case ICMP6_MEMBERSHIP_REPORT:
318 printf("type=Multicast Listener Report, ");
319 break;
320 case ICMP6_MEMBERSHIP_REDUCTION:
321 printf("type=Multicast Listener Done, ");
322 break;
323 }
324 printf("addr=%s\n", inet_ntop(AF_INET6, &mld->mld_addr,
325 ntop_buf, sizeof(ntop_buf)));
326
327 fflush(stdout);
328 }
329
330 void
quit(int signum __unused)331 quit(int signum __unused)
332 {
333 mreq.ipv6mr_multiaddr = maddr;
334 mreq.ipv6mr_interface = ifindex;
335 if (setsockopt(s, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq,
336 sizeof(mreq)) == -1)
337 err(1, "setsockopt(IPV6_LEAVE_GROUP)");
338
339 exit(0);
340 }
341
342 void
usage(void)343 usage(void)
344 {
345 (void)fprintf(stderr, "usage: mld6query [-dgr] ifname [addr]\n");
346 exit(1);
347 }
348