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