Exemple #1
0
void
make_msg(int index, struct in6_addr *addr, u_int type)
{
	static struct iovec iov[2];
	static u_char *cmsgbuf;
	int cmsglen, hbhlen = 0;
	u_int8_t raopt[IP6OPT_RTALERT_LEN];
	struct in6_pktinfo *pi;
	struct cmsghdr *cmsgp;
	u_short rtalert_code = htons(IP6OPT_RTALERT_MLD);

	dst.sin6_len = sizeof(dst);
	dst.sin6_family = AF_INET6;
	if (IN6_IS_ADDR_UNSPECIFIED(addr)) {
		if (inet_pton(AF_INET6, "ff02::1", &dst.sin6_addr) != 1)
			errx(1, "inet_pton failed");
	}
	else
		dst.sin6_addr = *addr;
	m.msg_name = (caddr_t)&dst;
	m.msg_namelen = dst.sin6_len;
	iov[0].iov_base = (caddr_t)&mldh;
	iov[0].iov_len = sizeof(mldh);
	m.msg_iov = iov;
	m.msg_iovlen = 1;

	bzero(&mldh, sizeof(mldh));
	mldh.mld6_type = type & 0xff;
	mldh.mld6_maxdelay = htons(QUERY_RESPONSE_INTERVAL);
	mldh.mld6_addr = *addr;

	hbhlen = sizeof(raopt); 
	cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
	    inet6_option_space(hbhlen);

	if ((cmsgbuf = malloc(cmsglen)) == NULL)
		errx(1, "can't allocate enough memory for cmsg");
	cmsgp = (struct cmsghdr *)cmsgbuf;
	m.msg_control = (caddr_t)cmsgbuf;
	m.msg_controllen = cmsglen;
	/* specify the outgoing interface */
	cmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
	cmsgp->cmsg_level = IPPROTO_IPV6;
	cmsgp->cmsg_type = IPV6_PKTINFO;
	pi = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
	pi->ipi6_ifindex = index;
	memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr));
	/* specifiy to insert router alert option in a hop-by-hop opt hdr. */
	cmsgp = CMSG_NXTHDR(&m, cmsgp);
	if (inet6_option_init((void *)cmsgp, &cmsgp, IPV6_HOPOPTS))
		errx(1, "inet6_option_init failed\n");
	raopt[0] = IP6OPT_RTALERT;
	raopt[1] = IP6OPT_RTALERT_LEN - 2;
	memcpy(&raopt[2], (caddr_t)&rtalert_code, sizeof(u_short));
	if (inet6_option_append(cmsgp, raopt, 4, 0))
		errx(1, "inet6_option_append failed\n");
}
Exemple #2
0
void runFailure1() {
    int len = 2;
    int room = inet6_option_space(len);
    char* bp = my_malloc(room);
    struct cmsghdr * cmsg;
    if (!inet6_option_init(bp, &cmsg, anyint())) {
        inet6_option_alloc(cmsg, len+1, anyint(), anyint());
    }
}
Exemple #3
0
void testValues() {
    f = 2;
    
    int len = 2;
    int room = inet6_option_space(len);
    char* bp = my_malloc(room);
    struct cmsghdr * cmsg;
    if (!inet6_option_init(bp, &cmsg, anyint())) {
        inet6_option_alloc(cmsg, len, anyint(), anyint());
    }

    //@ assert f == 2;
    //@ assert vacuous: \false;
}
Exemple #4
0
void
make_msg(int index, struct in6_addr *addr, u_int type)
{
	static struct iovec iov[2];
	static u_char *cmsgbuf;
	int cmsglen, hbhlen = 0;
#ifdef USE_RFC2292BIS
	void *hbhbuf = NULL, *optp = NULL;
	int currentlen;
#else
	u_int8_t raopt[IP6OPT_RTALERT_LEN];
#endif 
	struct in6_pktinfo *pi;
	struct cmsghdr *cmsgp;
	u_short rtalert_code = htons(IP6OPT_RTALERT_MLD);
	struct ifaddrs *ifa, *ifap;
	struct in6_addr src;

	dst.sin6_len = sizeof(dst);
	dst.sin6_family = AF_INET6;
	if (IN6_IS_ADDR_UNSPECIFIED(addr)) {
		if (inet_pton(AF_INET6, "ff02::1", &dst.sin6_addr) != 1)
			errx(1, "inet_pton failed");
	}
	else
		dst.sin6_addr = *addr;
	m.msg_name = (caddr_t)&dst;
	m.msg_namelen = dst.sin6_len;
	iov[0].iov_base = (caddr_t)&mldh;
	iov[0].iov_len = sizeof(mldh);
	m.msg_iov = iov;
	m.msg_iovlen = 1;

	bzero(&mldh, sizeof(mldh));
	mldh.mld_type = type & 0xff;
	mldh.mld_maxdelay = htons(QUERY_RESPONSE_INTERVAL);
	mldh.mld_addr = *addr;

	/* MLD packet should be advertised from linklocal address */
	getifaddrs(&ifa);
	for (ifap = ifa; ifap; ifap = ifap->ifa_next) {
		if (index != if_nametoindex(ifap->ifa_name))
			continue;

		if (ifap->ifa_addr->sa_family != AF_INET6)
			continue;
		if (!IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)
					    ifap->ifa_addr)->sin6_addr))
			continue;
		break;
	}
	if (ifap == NULL)
		errx(1, "no linkocal address is available");
	memcpy(&src, &((struct sockaddr_in6 *)ifap->ifa_addr)->sin6_addr,
	       sizeof(src));
	freeifaddrs(ifa);
#ifdef __KAME__
	/* remove embedded ifindex */
	src.s6_addr[2] = src.s6_addr[3] = 0;
#endif

#ifdef USE_RFC2292BIS
	if ((hbhlen = inet6_opt_init(NULL, 0)) == -1)
		errx(1, "inet6_opt_init(0) failed");
	if ((hbhlen = inet6_opt_append(NULL, 0, hbhlen, IP6OPT_ROUTER_ALERT, 2,
				       2, NULL)) == -1)
		errx(1, "inet6_opt_append(0) failed");
	if ((hbhlen = inet6_opt_finish(NULL, 0, hbhlen)) == -1)
		errx(1, "inet6_opt_finish(0) failed");
	cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(hbhlen);
#else
	hbhlen = sizeof(raopt); 
	cmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
	    inet6_option_space(hbhlen);
#endif 

	if ((cmsgbuf = malloc(cmsglen)) == NULL)
		errx(1, "can't allocate enough memory for cmsg");
	cmsgp = (struct cmsghdr *)cmsgbuf;
	m.msg_control = (caddr_t)cmsgbuf;
	m.msg_controllen = cmsglen;
	/* specify the outgoing interface */
	cmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
	cmsgp->cmsg_level = IPPROTO_IPV6;
	cmsgp->cmsg_type = IPV6_PKTINFO;
	pi = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
	pi->ipi6_ifindex = index;
	memcpy(&pi->ipi6_addr, &src, sizeof(pi->ipi6_addr));
	/* specifiy to insert router alert option in a hop-by-hop opt hdr. */
	cmsgp = CMSG_NXTHDR(&m, cmsgp);
#ifdef USE_RFC2292BIS
	cmsgp->cmsg_len = CMSG_LEN(hbhlen);
	cmsgp->cmsg_level = IPPROTO_IPV6;
	cmsgp->cmsg_type = IPV6_HOPOPTS;
	hbhbuf = CMSG_DATA(cmsgp);
	if ((currentlen = inet6_opt_init(hbhbuf, hbhlen)) == -1)
		errx(1, "inet6_opt_init(len = %d) failed", hbhlen);
	if ((currentlen = inet6_opt_append(hbhbuf, hbhlen, currentlen,
					   IP6OPT_ROUTER_ALERT, 2,
					   2, &optp)) == -1)
		errx(1, "inet6_opt_append(currentlen = %d, hbhlen = %d) failed",
		     currentlen, hbhlen);
	(void)inet6_opt_set_val(optp, 0, &rtalert_code, sizeof(rtalert_code));
	if ((currentlen = inet6_opt_finish(hbhbuf, hbhlen, currentlen)) == -1)
		errx(1, "inet6_opt_finish(buf) failed");
#else  /* old advanced API */
	if (inet6_option_init((void *)cmsgp, &cmsgp, IPV6_HOPOPTS))
		errx(1, "inet6_option_init failed\n");
	raopt[0] = IP6OPT_ROUTER_ALERT;
	raopt[1] = IP6OPT_RTALERT_LEN - 2;
	memcpy(&raopt[2], (caddr_t)&rtalert_code, sizeof(u_short));
	if (inet6_option_append(cmsgp, raopt, 4, 0))
		errx(1, "inet6_option_append failed\n");
#endif 
}