Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / mld6query / mld6.c
1 /*      $KAME: mld6.c,v 1.11 2001/05/13 15:45:07 suz Exp $      */
2
3 /*
4  * Copyright (C) 1998 WIDE Project.
5  * All rights reserved.
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  * 
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD: src/usr.sbin/mld6query/mld6.c,v 1.1.1.1.2.2 2001/07/03 11:02:06 ume Exp $
32  */
33 #include <sys/param.h>
34 #include <sys/uio.h>
35 #include <sys/socket.h>
36 #include <sys/types.h>
37 #include <sys/time.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 struct msghdr m;
56 struct sockaddr_in6 dst;
57 struct mld6_hdr mldh;
58 struct in6_addr maddr = IN6ADDR_ANY_INIT, any = IN6ADDR_ANY_INIT;
59 struct ipv6_mreq mreq;
60 u_short ifindex;
61 int s;
62
63 #define QUERY_RESPONSE_INTERVAL 10000
64
65 void make_msg(int index, struct in6_addr *addr, u_int type);
66 void usage(void);
67 void dump(int);
68 void quit(int);
69
70 int
71 main(int argc, char *argv[])
72 {
73         int i;
74         struct icmp6_filter filt;
75         u_int hlim = 1;
76         fd_set fdset;
77         struct itimerval itimer;
78         u_int type;
79         int ch;
80
81         type = MLD6_LISTENER_QUERY;
82         while ((ch = getopt(argc, argv, "dr")) != -1) {
83                 switch (ch) {
84                 case 'd':
85                         type = MLD6_LISTENER_DONE;
86                         break;
87                 case 'r':
88                         type = MLD6_LISTENER_REPORT;
89                         break;
90                 default:
91                         usage();
92                         /*NOTREACHED*/
93                 }
94         }
95
96         argv += optind;
97         argc -= optind;
98         
99         if (argc != 1 && argc != 2)
100                 usage();
101
102         ifindex = (u_short)if_nametoindex(argv[0]);
103         if (ifindex == 0)
104                 usage();
105         if (argc == 2 && inet_pton(AF_INET6, argv[1], &maddr) != 1)
106                 usage();
107
108         if ((s = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
109                 err(1, "socket");
110
111         if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hlim,
112                        sizeof(hlim)) == -1)
113                 err(1, "setsockopt(IPV6_MULTICAST_HOPS)");
114
115         mreq.ipv6mr_multiaddr = any;
116         mreq.ipv6mr_interface = ifindex;
117         if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
118                        sizeof(mreq)) == -1)
119                 err(1, "setsockopt(IPV6_JOIN_GROUP)");
120
121         ICMP6_FILTER_SETBLOCKALL(&filt);
122         ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_QUERY, &filt);
123         ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REPORT, &filt);
124         ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REDUCTION, &filt);
125         if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
126                         sizeof(filt)) < 0)
127                 err(1, "setsockopt(ICMP6_FILTER)");
128
129         make_msg(ifindex, &maddr, type);
130
131         if (sendmsg(s, &m, 0) < 0)
132                 err(1, "sendmsg");
133
134         itimer.it_value.tv_sec =  QUERY_RESPONSE_INTERVAL / 1000;
135         itimer.it_interval.tv_sec = 0;
136         itimer.it_interval.tv_usec = 0;
137         itimer.it_value.tv_usec = 0;
138
139         (void)signal(SIGALRM, quit);
140         (void)setitimer(ITIMER_REAL, &itimer, NULL);
141
142         FD_ZERO(&fdset);
143         for (;;) {
144                 FD_SET(s, &fdset);
145                 if ((i = select(s + 1, &fdset, NULL, NULL, NULL)) < 0)
146                         perror("select");
147                 if (i == 0)
148                         continue;
149                 else
150                         dump(s);
151         }
152 }
153
154 void
155 make_msg(int index, struct in6_addr *addr, u_int type)
156 {
157         static struct iovec iov[2];
158         static u_char *cmsgbuf;
159         int cmsglen, hbhlen = 0;
160         u_int8_t raopt[IP6OPT_RTALERT_LEN];
161         struct in6_pktinfo *pi;
162         struct cmsghdr *cmsgp;
163         u_short rtalert_code = htons(IP6OPT_RTALERT_MLD);
164
165         dst.sin6_len = sizeof(dst);
166         dst.sin6_family = AF_INET6;
167         if (IN6_IS_ADDR_UNSPECIFIED(addr)) {
168                 if (inet_pton(AF_INET6, "ff02::1", &dst.sin6_addr) != 1)
169                         errx(1, "inet_pton failed");
170         }
171         else
172                 dst.sin6_addr = *addr;
173         m.msg_name = (caddr_t)&dst;
174         m.msg_namelen = dst.sin6_len;
175         iov[0].iov_base = (caddr_t)&mldh;
176         iov[0].iov_len = sizeof(mldh);
177         m.msg_iov = iov;
178         m.msg_iovlen = 1;
179
180         bzero(&mldh, sizeof(mldh));
181         mldh.mld6_type = type & 0xff;
182         mldh.mld6_maxdelay = htons(QUERY_RESPONSE_INTERVAL);
183         mldh.mld6_addr = *addr;
184
185         hbhlen = sizeof(raopt); 
186         cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
187             inet6_option_space(hbhlen);
188
189         if ((cmsgbuf = malloc(cmsglen)) == NULL)
190                 errx(1, "can't allocate enough memory for cmsg");
191         cmsgp = (struct cmsghdr *)cmsgbuf;
192         m.msg_control = (caddr_t)cmsgbuf;
193         m.msg_controllen = cmsglen;
194         /* specify the outgoing interface */
195         cmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
196         cmsgp->cmsg_level = IPPROTO_IPV6;
197         cmsgp->cmsg_type = IPV6_PKTINFO;
198         pi = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
199         pi->ipi6_ifindex = index;
200         memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr));
201         /* specifiy to insert router alert option in a hop-by-hop opt hdr. */
202         cmsgp = CMSG_NXTHDR(&m, cmsgp);
203         if (inet6_option_init((void *)cmsgp, &cmsgp, IPV6_HOPOPTS))
204                 errx(1, "inet6_option_init failed\n");
205         raopt[0] = IP6OPT_RTALERT;
206         raopt[1] = IP6OPT_RTALERT_LEN - 2;
207         memcpy(&raopt[2], (caddr_t)&rtalert_code, sizeof(u_short));
208         if (inet6_option_append(cmsgp, raopt, 4, 0))
209                 errx(1, "inet6_option_append failed\n");
210 }
211
212 void
213 dump(int s)
214 {
215         int i;
216         struct mld6_hdr *mld;
217         u_char buf[1024];
218         struct sockaddr_in6 from;
219         int from_len = sizeof(from);
220         char ntop_buf[256];
221
222         if ((i = recvfrom(s, buf, sizeof(buf), 0,
223                           (struct sockaddr *)&from,
224                           &from_len)) < 0)
225                 return;
226
227         if (i < sizeof(struct mld6_hdr)) {
228                 printf("too short!\n");
229                 return;
230         }
231
232         mld = (struct mld6_hdr *)buf;
233
234         printf("from %s, ", inet_ntop(AF_INET6, &from.sin6_addr,
235                                       ntop_buf, sizeof(ntop_buf)));
236
237         switch (mld->mld6_type) {
238         case ICMP6_MEMBERSHIP_QUERY:
239                 printf("type=Multicast Listener Query, ");
240                 break;
241         case ICMP6_MEMBERSHIP_REPORT:
242                 printf("type=Multicast Listener Report, ");
243                 break;
244         case ICMP6_MEMBERSHIP_REDUCTION:
245                 printf("type=Multicast Listener Done, ");
246                 break;
247         }
248         printf("addr=%s\n", inet_ntop(AF_INET6, &mld->mld6_addr,
249                                     ntop_buf, sizeof(ntop_buf)));
250         
251         fflush(stdout);
252 }
253
254 /* ARGSUSED */
255 void
256 quit(int signum) {
257         mreq.ipv6mr_multiaddr = any;
258         mreq.ipv6mr_interface = ifindex;
259         if (setsockopt(s, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq,
260                        sizeof(mreq)) == -1)
261                 err(1, "setsockopt(IPV6_LEAVE_GROUP)");
262
263         exit(0);
264 }
265
266 void
267 usage()
268 {
269         (void)fprintf(stderr, "usage: mld6query ifname [addr]\n");
270         exit(1);
271 }