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