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