xref: /dragonfly/usr.sbin/mld6query/mld6query.c (revision 0085a56d)
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 
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 <unistd.h>
40 #include <signal.h>
41 
42 #include <net/if.h>
43 #include <net/if_var.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 static struct msghdr m;
57 static struct sockaddr_in6 dst;
58 static struct mld6_hdr mldh;
59 static struct in6_addr maddr = IN6ADDR_ANY_INIT, any = IN6ADDR_ANY_INIT;
60 static struct ipv6_mreq mreq;
61 static u_short ifindex;
62 static int s;
63 
64 #define QUERY_RESPONSE_INTERVAL 10000
65 
66 static void make_msg(int, struct in6_addr *, u_int);
67 static void usage(void) __dead2;
68 static void dump(int);
69 static void quit(int) __dead2;
70 
71 int
72 main(int argc, char *argv[])
73 {
74 	int i;
75 	struct icmp6_filter filt;
76 	u_int hlim = 1;
77 	fd_set fdset;
78 	struct itimerval itimer;
79 	u_int type;
80 	int ch;
81 
82 	type = MLD6_LISTENER_QUERY;
83 	while ((ch = getopt(argc, argv, "dr")) != -1) {
84 		switch (ch) {
85 		case 'd':
86 			type = MLD6_LISTENER_DONE;
87 			break;
88 		case 'r':
89 			type = MLD6_LISTENER_REPORT;
90 			break;
91 		default:
92 			usage();
93 			/*NOTREACHED*/
94 		}
95 	}
96 
97 	argv += optind;
98 	argc -= optind;
99 
100 	if (argc != 1 && argc != 2)
101 		usage();
102 
103 	ifindex = (u_short)if_nametoindex(argv[0]);
104 	if (ifindex == 0)
105 		usage();
106 	if (argc == 2 && inet_pton(AF_INET6, argv[1], &maddr) != 1)
107 		usage();
108 
109 	if ((s = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
110 		err(1, "socket");
111 
112 	if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hlim,
113 		       sizeof(hlim)) == -1)
114 		err(1, "setsockopt(IPV6_MULTICAST_HOPS)");
115 
116 	mreq.ipv6mr_multiaddr = any;
117 	mreq.ipv6mr_interface = ifindex;
118 	if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
119 		       sizeof(mreq)) == -1)
120 		err(1, "setsockopt(IPV6_JOIN_GROUP)");
121 
122 	ICMP6_FILTER_SETBLOCKALL(&filt);
123 	ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_QUERY, &filt);
124 	ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REPORT, &filt);
125 	ICMP6_FILTER_SETPASS(ICMP6_MEMBERSHIP_REDUCTION, &filt);
126 	if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
127 			sizeof(filt)) < 0)
128 		err(1, "setsockopt(ICMP6_FILTER)");
129 
130 	make_msg(ifindex, &maddr, type);
131 
132 	if (sendmsg(s, &m, 0) < 0)
133 		err(1, "sendmsg");
134 
135 	itimer.it_value.tv_sec =  QUERY_RESPONSE_INTERVAL / 1000;
136 	itimer.it_interval.tv_sec = 0;
137 	itimer.it_interval.tv_usec = 0;
138 	itimer.it_value.tv_usec = 0;
139 
140 	signal(SIGALRM, quit);
141 	setitimer(ITIMER_REAL, &itimer, NULL);
142 
143 	FD_ZERO(&fdset);
144 	for (;;) {
145 		FD_SET(s, &fdset);
146 		if ((i = select(s + 1, &fdset, NULL, NULL, NULL)) < 0)
147 			perror("select");
148 		if (i == 0)
149 			continue;
150 		else
151 			dump(s);
152 	}
153 }
154 
155 static void
156 make_msg(int idx, struct in6_addr *addr, u_int type)
157 {
158 	static struct iovec iov[2];
159 	static u_char *cmsgbuf;
160 	int cmsglen, hbhlen = 0;
161 	u_int8_t raopt[IP6OPT_RTALERT_LEN];
162 	struct in6_pktinfo *pi;
163 	struct cmsghdr *cmsgp;
164 	u_short rtalert_code = htons(IP6OPT_RTALERT_MLD);
165 
166 	dst.sin6_len = sizeof(dst);
167 	dst.sin6_family = AF_INET6;
168 	if (IN6_IS_ADDR_UNSPECIFIED(addr)) {
169 		if (inet_pton(AF_INET6, "ff02::1", &dst.sin6_addr) != 1)
170 			errx(1, "inet_pton failed");
171 	}
172 	else
173 		dst.sin6_addr = *addr;
174 	m.msg_name = (caddr_t)&dst;
175 	m.msg_namelen = dst.sin6_len;
176 	iov[0].iov_base = (caddr_t)&mldh;
177 	iov[0].iov_len = sizeof(mldh);
178 	m.msg_iov = iov;
179 	m.msg_iovlen = 1;
180 
181 	bzero(&mldh, sizeof(mldh));
182 	mldh.mld6_type = type & 0xff;
183 	mldh.mld6_maxdelay = htons(QUERY_RESPONSE_INTERVAL);
184 	mldh.mld6_addr = *addr;
185 
186 	hbhlen = sizeof(raopt);
187 	cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
188 	    inet6_option_space(hbhlen);
189 
190 	if ((cmsgbuf = malloc(cmsglen)) == NULL)
191 		errx(1, "can't allocate enough memory for cmsg");
192 	cmsgp = (struct cmsghdr *)cmsgbuf;
193 	m.msg_control = (caddr_t)cmsgbuf;
194 	m.msg_controllen = cmsglen;
195 	/* specify the outgoing interface */
196 	cmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
197 	cmsgp->cmsg_level = IPPROTO_IPV6;
198 	cmsgp->cmsg_type = IPV6_PKTINFO;
199 	pi = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
200 	pi->ipi6_ifindex = idx;
201 	memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr));
202 	/* specifiy to insert router alert option in a hop-by-hop opt hdr. */
203 	cmsgp = CMSG_NXTHDR(&m, cmsgp);
204 	if (inet6_option_init((void *)cmsgp, &cmsgp, IPV6_HOPOPTS))
205 		errx(1, "inet6_option_init failed\n");
206 	raopt[0] = IP6OPT_RTALERT;
207 	raopt[1] = IP6OPT_RTALERT_LEN - 2;
208 	memcpy(&raopt[2], (caddr_t)&rtalert_code, sizeof(u_short));
209 	if (inet6_option_append(cmsgp, raopt, 4, 0))
210 		errx(1, "inet6_option_append failed\n");
211 }
212 
213 static void
214 dump(int sockdesc)
215 {
216 	ssize_t i;
217 	struct mld6_hdr *mld;
218 	u_char buf[1024];
219 	struct sockaddr_in6 from;
220 	int from_len = sizeof(from);
221 	char ntop_buf[256];
222 
223 	if ((i = recvfrom(sockdesc, buf, sizeof(buf), 0,
224 			  (struct sockaddr *)&from,
225 			  &from_len)) < 0)
226 		return;
227 
228 	if ((size_t)i < sizeof(struct mld6_hdr)) {
229 		printf("too short!\n");
230 		return;
231 	}
232 
233 	mld = (struct mld6_hdr *)buf;
234 
235 	printf("from %s, ", inet_ntop(AF_INET6, &from.sin6_addr,
236 				      ntop_buf, sizeof(ntop_buf)));
237 
238 	switch (mld->mld6_type) {
239 	case ICMP6_MEMBERSHIP_QUERY:
240 		printf("type=Multicast Listener Query, ");
241 		break;
242 	case ICMP6_MEMBERSHIP_REPORT:
243 		printf("type=Multicast Listener Report, ");
244 		break;
245 	case ICMP6_MEMBERSHIP_REDUCTION:
246 		printf("type=Multicast Listener Done, ");
247 		break;
248 	}
249 	printf("addr=%s\n", inet_ntop(AF_INET6, &mld->mld6_addr,
250 				    ntop_buf, sizeof(ntop_buf)));
251 
252 	fflush(stdout);
253 }
254 
255 /* ARGSUSED */
256 static void
257 quit(__unused int signum) {
258 	mreq.ipv6mr_multiaddr = any;
259 	mreq.ipv6mr_interface = ifindex;
260 	if (setsockopt(s, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq,
261 		       sizeof(mreq)) == -1)
262 		err(1, "setsockopt(IPV6_LEAVE_GROUP)");
263 
264 	exit(0);
265 }
266 
267 static void
268 usage(void)
269 {
270 	fprintf(stderr, "usage: mld6query ifname [addr]\n");
271 	exit(1);
272 }
273