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