]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/mld6query/mld6.c
stand: TARGET_ARCH is spelled MACHINE_ARCH in Makefiles
[FreeBSD/FreeBSD.git] / usr.sbin / mld6query / mld6.c
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/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/uio.h>
39 #include <sys/socket.h>
40 #include <sys/types.h>
41 #include <sys/time.h>
42 #include <ifaddrs.h>
43 #include <unistd.h>
44 #include <signal.h>
45
46 #include <net/if.h>
47
48 #include <netinet/in.h>
49 #include <netinet/ip6.h>
50 #include <netinet/icmp6.h>
51
52 #include  <arpa/inet.h>
53
54 #include <stdlib.h>
55 #include <stdio.h>
56 #include <string.h>
57 #include <err.h>
58
59 /* portability with older KAME headers */
60 #ifndef MLD_LISTENER_QUERY
61 #define MLD_LISTENER_QUERY      MLD6_LISTENER_QUERY
62 #define MLD_LISTENER_REPORT     MLD6_LISTENER_REPORT
63 #define MLD_LISTENER_DONE       MLD6_LISTENER_DONE
64 #define MLD_MTRACE_RESP         MLD6_MTRACE_RESP
65 #define MLD_MTRACE              MLD6_MTRACE
66 #define mld_hdr         mld6_hdr
67 #define mld_type        mld6_type
68 #define mld_code        mld6_code
69 #define mld_cksum       mld6_cksum
70 #define mld_maxdelay    mld6_maxdelay
71 #define mld_reserved    mld6_reserved
72 #define mld_addr        mld6_addr
73 #endif
74 #ifndef IP6OPT_ROUTER_ALERT
75 #define IP6OPT_ROUTER_ALERT     IP6OPT_RTALERT
76 #endif
77
78 struct msghdr m;
79 struct sockaddr_in6 dst;
80 struct mld_hdr mldh;
81 struct in6_addr maddr = IN6ADDR_ANY_INIT, any = IN6ADDR_ANY_INIT;
82 struct ipv6_mreq mreq;
83 u_short ifindex;
84 int s;
85
86 #define QUERY_RESPONSE_INTERVAL 10000
87
88 void make_msg(int index, struct in6_addr *addr, u_int type, struct in6_addr *qaddr);
89 void usage(void);
90 void dump(int);
91 void quit(int);
92
93 int
94 main(int argc, char *argv[])
95 {
96         int i;
97         struct icmp6_filter filt;
98         u_int hlim = 1;
99         fd_set fdset;
100         struct itimerval itimer;
101         u_int type;
102         int ch;
103         struct in6_addr *qaddr = &maddr;
104
105         type = MLD_LISTENER_QUERY;
106         while ((ch = getopt(argc, argv, "dgr")) != -1) {
107                 switch (ch) {
108                 case 'd':
109                         if (type != MLD_LISTENER_QUERY) {
110                                 printf("Can not specifiy -d with -r\n");
111                                 return 1;
112                         }
113                         type = MLD_LISTENER_DONE;
114                         break;
115                 case 'g':
116                         qaddr = &any;
117                         break;
118                 case 'r':
119                         if (type != MLD_LISTENER_QUERY) {
120                                 printf("Can not specifiy -r with -d\n");
121                                 return 1;
122                         }
123                         type = MLD_LISTENER_REPORT;
124                         break;
125                 default:
126                         usage();
127                         /*NOTREACHED*/
128                 }
129         }
130
131         argv += optind;
132         argc -= optind;
133         
134         if (argc != 1 && argc != 2)
135                 usage();
136
137         ifindex = (u_short)if_nametoindex(argv[0]);
138         if (ifindex == 0)
139                 usage();
140         if (argc == 2 && inet_pton(AF_INET6, argv[1], &maddr) != 1)
141                 usage();
142         if (type != MLD_LISTENER_QUERY && qaddr != &maddr) {
143                 printf("Can not specifiy -g with -d or -r\n");
144                 return 1;
145         }
146
147         if ((s = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
148                 err(1, "socket");
149
150         if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hlim,
151                        sizeof(hlim)) == -1)
152                 err(1, "setsockopt(IPV6_MULTICAST_HOPS)");
153
154         if (IN6_IS_ADDR_UNSPECIFIED(&maddr)) {
155                 if (inet_pton(AF_INET6, "ff02::1", &maddr) != 1)
156                         errx(1, "inet_pton failed");
157         }
158
159         mreq.ipv6mr_multiaddr = maddr;
160         mreq.ipv6mr_interface = ifindex;
161         if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
162                        sizeof(mreq)) == -1)
163                 err(1, "setsockopt(IPV6_JOIN_GROUP)");
164
165         ICMP6_FILTER_SETBLOCKALL(&filt);
166         ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_QUERY, &filt);
167         ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REPORT, &filt);
168         ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REDUCTION, &filt);
169         if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
170                         sizeof(filt)) < 0)
171                 err(1, "setsockopt(ICMP6_FILTER)");
172
173         make_msg(ifindex, &maddr, type, qaddr);
174
175         if (sendmsg(s, &m, 0) < 0)
176                 err(1, "sendmsg");
177
178         itimer.it_value.tv_sec =  QUERY_RESPONSE_INTERVAL / 1000;
179         itimer.it_interval.tv_sec = 0;
180         itimer.it_interval.tv_usec = 0;
181         itimer.it_value.tv_usec = 0;
182
183         (void)signal(SIGALRM, quit);
184         (void)setitimer(ITIMER_REAL, &itimer, NULL);
185
186         FD_ZERO(&fdset);
187         if (s >= FD_SETSIZE)
188                 errx(1, "descriptor too big");
189         for (;;) {
190                 FD_SET(s, &fdset);
191                 if ((i = select(s + 1, &fdset, NULL, NULL, NULL)) < 0)
192                         perror("select");
193                 if (i == 0)
194                         continue;
195                 else
196                         dump(s);
197         }
198 }
199
200 void
201 make_msg(int index, struct in6_addr *addr, u_int type, struct in6_addr *qaddr)
202 {
203         static struct iovec iov[2];
204         static u_char *cmsgbuf;
205         int cmsglen, hbhlen = 0;
206 #ifdef USE_RFC2292BIS
207         void *hbhbuf = NULL, *optp = NULL;
208         int currentlen;
209 #else
210         u_int8_t raopt[IP6OPT_RTALERT_LEN];
211 #endif 
212         struct in6_pktinfo *pi;
213         struct cmsghdr *cmsgp;
214         u_short rtalert_code = htons(IP6OPT_RTALERT_MLD);
215         struct ifaddrs *ifa, *ifap;
216         struct in6_addr src;
217
218         dst.sin6_len = sizeof(dst);
219         dst.sin6_family = AF_INET6;
220         dst.sin6_addr = *addr;
221         m.msg_name = (caddr_t)&dst;
222         m.msg_namelen = dst.sin6_len;
223         iov[0].iov_base = (caddr_t)&mldh;
224         iov[0].iov_len = sizeof(mldh);
225         m.msg_iov = iov;
226         m.msg_iovlen = 1;
227
228         bzero(&mldh, sizeof(mldh));
229         mldh.mld_type = type & 0xff;
230         mldh.mld_maxdelay = htons(QUERY_RESPONSE_INTERVAL);
231         mldh.mld_addr = *qaddr;
232
233         /* MLD packet should be advertised from linklocal address */
234         getifaddrs(&ifa);
235         for (ifap = ifa; ifap; ifap = ifap->ifa_next) {
236                 if (index != if_nametoindex(ifap->ifa_name))
237                         continue;
238
239                 if (ifap->ifa_addr->sa_family != AF_INET6)
240                         continue;
241                 if (!IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)
242                                             ifap->ifa_addr)->sin6_addr))
243                         continue;
244                 break;
245         }
246         if (ifap == NULL)
247                 errx(1, "no linkocal address is available");
248         memcpy(&src, &((struct sockaddr_in6 *)ifap->ifa_addr)->sin6_addr,
249                sizeof(src));
250         freeifaddrs(ifa);
251 #ifdef __KAME__
252         /* remove embedded ifindex */
253         src.s6_addr[2] = src.s6_addr[3] = 0;
254 #endif
255
256 #ifdef USE_RFC2292BIS
257         if ((hbhlen = inet6_opt_init(NULL, 0)) == -1)
258                 errx(1, "inet6_opt_init(0) failed");
259         if ((hbhlen = inet6_opt_append(NULL, 0, hbhlen, IP6OPT_ROUTER_ALERT, 2,
260                                        2, NULL)) == -1)
261                 errx(1, "inet6_opt_append(0) failed");
262         if ((hbhlen = inet6_opt_finish(NULL, 0, hbhlen)) == -1)
263                 errx(1, "inet6_opt_finish(0) failed");
264         cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(hbhlen);
265 #else
266         hbhlen = sizeof(raopt); 
267         cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
268             inet6_option_space(hbhlen);
269 #endif 
270
271         if ((cmsgbuf = malloc(cmsglen)) == NULL)
272                 errx(1, "can't allocate enough memory for cmsg");
273         cmsgp = (struct cmsghdr *)cmsgbuf;
274         m.msg_control = (caddr_t)cmsgbuf;
275         m.msg_controllen = cmsglen;
276         /* specify the outgoing interface */
277         cmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
278         cmsgp->cmsg_level = IPPROTO_IPV6;
279         cmsgp->cmsg_type = IPV6_PKTINFO;
280         pi = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
281         pi->ipi6_ifindex = index;
282         memcpy(&pi->ipi6_addr, &src, sizeof(pi->ipi6_addr));
283         /* specifiy to insert router alert option in a hop-by-hop opt hdr. */
284         cmsgp = CMSG_NXTHDR(&m, cmsgp);
285 #ifdef USE_RFC2292BIS
286         cmsgp->cmsg_len = CMSG_LEN(hbhlen);
287         cmsgp->cmsg_level = IPPROTO_IPV6;
288         cmsgp->cmsg_type = IPV6_HOPOPTS;
289         hbhbuf = CMSG_DATA(cmsgp);
290         if ((currentlen = inet6_opt_init(hbhbuf, hbhlen)) == -1)
291                 errx(1, "inet6_opt_init(len = %d) failed", hbhlen);
292         if ((currentlen = inet6_opt_append(hbhbuf, hbhlen, currentlen,
293                                            IP6OPT_ROUTER_ALERT, 2,
294                                            2, &optp)) == -1)
295                 errx(1, "inet6_opt_append(currentlen = %d, hbhlen = %d) failed",
296                      currentlen, hbhlen);
297         (void)inet6_opt_set_val(optp, 0, &rtalert_code, sizeof(rtalert_code));
298         if ((currentlen = inet6_opt_finish(hbhbuf, hbhlen, currentlen)) == -1)
299                 errx(1, "inet6_opt_finish(buf) failed");
300 #else  /* old advanced API */
301         if (inet6_option_init((void *)cmsgp, &cmsgp, IPV6_HOPOPTS))
302                 errx(1, "inet6_option_init failed\n");
303         raopt[0] = IP6OPT_ROUTER_ALERT;
304         raopt[1] = IP6OPT_RTALERT_LEN - 2;
305         memcpy(&raopt[2], (caddr_t)&rtalert_code, sizeof(u_short));
306         if (inet6_option_append(cmsgp, raopt, 4, 0))
307                 errx(1, "inet6_option_append failed\n");
308 #endif 
309 }
310
311 void
312 dump(int s)
313 {
314         int i;
315         struct mld_hdr *mld;
316         u_char buf[1024];
317         struct sockaddr_in6 from;
318         int from_len = sizeof(from);
319         char ntop_buf[256];
320
321         if ((i = recvfrom(s, buf, sizeof(buf), 0,
322                           (struct sockaddr *)&from,
323                           &from_len)) < 0)
324                 return;
325
326         if (i < sizeof(struct mld_hdr)) {
327                 printf("too short!\n");
328                 return;
329         }
330
331         mld = (struct mld_hdr *)buf;
332
333         printf("from %s, ", inet_ntop(AF_INET6, &from.sin6_addr,
334                                       ntop_buf, sizeof(ntop_buf)));
335
336         switch (mld->mld_type) {
337         case ICMP6_MEMBERSHIP_QUERY:
338                 printf("type=Multicast Listener Query, ");
339                 break;
340         case ICMP6_MEMBERSHIP_REPORT:
341                 printf("type=Multicast Listener Report, ");
342                 break;
343         case ICMP6_MEMBERSHIP_REDUCTION:
344                 printf("type=Multicast Listener Done, ");
345                 break;
346         }
347         printf("addr=%s\n", inet_ntop(AF_INET6, &mld->mld_addr,
348                                     ntop_buf, sizeof(ntop_buf)));
349         
350         fflush(stdout);
351 }
352
353 void
354 quit(int signum __unused)
355 {
356         mreq.ipv6mr_multiaddr = maddr;
357         mreq.ipv6mr_interface = ifindex;
358         if (setsockopt(s, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq,
359                        sizeof(mreq)) == -1)
360                 err(1, "setsockopt(IPV6_LEAVE_GROUP)");
361
362         exit(0);
363 }
364
365 void
366 usage(void)
367 {
368         (void)fprintf(stderr, "usage: mld6query [-dgr] ifname [addr]\n");
369         exit(1);
370 }