mcast.c 51.2 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7
/*
 *	Multicast support for IPv6
 *	Linux INET6 implementation 
 *
 *	Authors:
 *	Pedro Roque		<roque@di.fc.ul.pt>	
 *
8
 *	$Id: mcast.c,v 1.40 2002/02/08 03:57:19 davem Exp $
Linus Torvalds's avatar
Linus Torvalds committed
9 10 11 12 13 14 15 16 17
 *
 *	Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c 
 *
 *	This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      as published by the Free Software Foundation; either version
 *      2 of the License, or (at your option) any later version.
 */

Linus Torvalds's avatar
Linus Torvalds committed
18 19 20
/* Changes:
 *
 *	yoshfuji	: fix format of router-alert option
21 22
 *	YOSHIFUJI Hideaki @USAGI:
 *		Fixed source address for MLD message based on
23
 *		<draft-ietf-magma-mld-source-05.txt>.
24 25 26
 *	YOSHIFUJI Hideaki @USAGI:
 *		- Ignore Queries for invalid addresses.
 *		- MLD for link-local addresses.
David Stevens's avatar
David Stevens committed
27 28
 *	David L Stevens <dlstevens@us.ibm.com>:
 *		- MLDv2 support
Linus Torvalds's avatar
Linus Torvalds committed
29 30
 */

Linus Torvalds's avatar
Linus Torvalds committed
31 32 33 34 35 36 37
#include <linux/config.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/socket.h>
#include <linux/sockios.h>
Rusty Russell's avatar
Rusty Russell committed
38
#include <linux/jiffies.h>
Linus Torvalds's avatar
Linus Torvalds committed
39
#include <linux/net.h>
David Stevens's avatar
David Stevens committed
40
#include <linux/in.h>
Linus Torvalds's avatar
Linus Torvalds committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#include <linux/in6.h>
#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <linux/route.h>
#include <linux/init.h>
#include <linux/proc_fs.h>

#include <net/sock.h>
#include <net/snmp.h>

#include <net/ipv6.h>
#include <net/protocol.h>
#include <net/if_inet6.h>
#include <net/ndisc.h>
#include <net/addrconf.h>
#include <net/ip6_route.h>

#include <net/checksum.h>

/* Set to 3 to get tracing... */
#define MCAST_DEBUG 2

#if MCAST_DEBUG >= 3
#define MDBG(x) printk x
#else
#define MDBG(x)
#endif

David Stevens's avatar
David Stevens committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
/*
 *  These header formats should be in a separate include file, but icmpv6.h
 *  doesn't have in6_addr defined in all cases, there is no __u128, and no
 *  other files reference these.
 *
 *  			+-DLS 4/14/03
 */

/* Multicast Listener Discovery version 2 headers */

struct mld2_grec {
	__u8		grec_type;
	__u8		grec_auxwords;
	__u16		grec_nsrcs;
	struct in6_addr	grec_mca;
	struct in6_addr	grec_src[0];
};

struct mld2_report {
	__u8	type;
	__u8	resv1;
	__u16	csum;
	__u16	resv2;
	__u16	ngrec;
	struct mld2_grec grec[0];
};

struct mld2_query {
	__u8 type;
	__u8 code;
	__u16 csum;
	__u16 mrc;
	__u16 resv1;
	struct in6_addr mca;
#if defined(__LITTLE_ENDIAN_BITFIELD)
	__u8 qrv:3,
	     suppress:1,
	     resv2:4;
#elif defined(__BIG_ENDIAN_BITFIELD)
	__u8 resv2:4,
	     suppress:1,
	     qrv:3;
#else
#error "Please fix <asm/byteorder.h>"
#endif
	__u8 qqic;
	__u16 nsrcs;
	struct in6_addr srcs[0];
};

struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;

Linus Torvalds's avatar
Linus Torvalds committed
121 122 123 124 125 126 127
/* Big mc list lock for all the sockets */
static rwlock_t ipv6_sk_mc_lock = RW_LOCK_UNLOCKED;

static struct socket *igmp6_socket;

static void igmp6_join_group(struct ifmcaddr6 *ma);
static void igmp6_leave_group(struct ifmcaddr6 *ma);
Stephen Rothwell's avatar
Stephen Rothwell committed
128
static void igmp6_timer_handler(unsigned long data);
Linus Torvalds's avatar
Linus Torvalds committed
129

David Stevens's avatar
David Stevens committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
static void mld_gq_timer_expire(unsigned long data);
static void mld_ifc_timer_expire(unsigned long data);
static void mld_ifc_event(struct inet6_dev *idev);
static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *addr);
static void mld_clear_delrec(struct inet6_dev *idev);
static int sf_setstate(struct ifmcaddr6 *pmc);
static void sf_markstate(struct ifmcaddr6 *pmc);
static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca, int sfmode,
	int sfcount, struct in6_addr *psfsrc, int delta);
int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca, int sfmode,
	int sfcount, struct in6_addr *psfsrc, int delta);
int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
	struct inet6_dev *idev);


Linus Torvalds's avatar
Linus Torvalds committed
147
#define IGMP6_UNSOLICITED_IVAL	(10*HZ)
David Stevens's avatar
David Stevens committed
148 149 150 151 152 153 154 155 156 157 158 159 160
#define MLD_QRV_DEFAULT		2

#define MLD_V1_SEEN(idev) ((idev)->mc_v1_seen && \
		time_before(jiffies, (idev)->mc_v1_seen))

#define MLDV2_MASK(value, nb) ((nb)>=32 ? (value) : ((1<<(nb))-1) & (value))
#define MLDV2_EXP(thresh, nbmant, nbexp, value) \
	((value) < (thresh) ? (value) : \
	((MLDV2_MASK(value, nbmant) | (1<<(nbmant+nbexp))) << \
	(MLDV2_MASK((value) >> (nbmant), nbexp) + (nbexp))))

#define MLDV2_QQIC(value) MLDV2_EXP(0x80, 4, 3, value)
#define MLDV2_MRC(value) MLDV2_EXP(0x8000, 12, 3, value)
Linus Torvalds's avatar
Linus Torvalds committed
161 162 163 164 165 166 167 168 169

/*
 *	socket join on multicast group
 */

int ipv6_sock_mc_join(struct sock *sk, int ifindex, struct in6_addr *addr)
{
	struct net_device *dev = NULL;
	struct ipv6_mc_socklist *mc_lst;
170
	struct ipv6_pinfo *np = inet6_sk(sk);
Linus Torvalds's avatar
Linus Torvalds committed
171 172 173 174 175 176 177 178 179 180 181
	int err;

	if (!(ipv6_addr_type(addr) & IPV6_ADDR_MULTICAST))
		return -EINVAL;

	mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);

	if (mc_lst == NULL)
		return -ENOMEM;

	mc_lst->next = NULL;
182
	ipv6_addr_copy(&mc_lst->addr, addr);
Linus Torvalds's avatar
Linus Torvalds committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

	if (ifindex == 0) {
		struct rt6_info *rt;
		rt = rt6_lookup(addr, NULL, 0, 0);
		if (rt) {
			dev = rt->rt6i_dev;
			dev_hold(dev);
			dst_release(&rt->u.dst);
		}
	} else
		dev = dev_get_by_index(ifindex);

	if (dev == NULL) {
		sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
		return -ENODEV;
	}

Linus Torvalds's avatar
Linus Torvalds committed
200
	mc_lst->ifindex = dev->ifindex;
David Stevens's avatar
David Stevens committed
201 202
	mc_lst->sfmode = MCAST_EXCLUDE;
	mc_lst->sflist = 0;
Linus Torvalds's avatar
Linus Torvalds committed
203

Linus Torvalds's avatar
Linus Torvalds committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
	/*
	 *	now add/increase the group membership on the device
	 */

	err = ipv6_dev_mc_inc(dev, addr);

	if (err) {
		sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
		dev_put(dev);
		return err;
	}

	write_lock_bh(&ipv6_sk_mc_lock);
	mc_lst->next = np->ipv6_mc_list;
	np->ipv6_mc_list = mc_lst;
	write_unlock_bh(&ipv6_sk_mc_lock);

	dev_put(dev);

	return 0;
}

/*
 *	socket leave on multicast group
 */
int ipv6_sock_mc_drop(struct sock *sk, int ifindex, struct in6_addr *addr)
{
231
	struct ipv6_pinfo *np = inet6_sk(sk);
Linus Torvalds's avatar
Linus Torvalds committed
232 233 234 235
	struct ipv6_mc_socklist *mc_lst, **lnk;

	write_lock_bh(&ipv6_sk_mc_lock);
	for (lnk = &np->ipv6_mc_list; (mc_lst = *lnk) !=NULL ; lnk = &mc_lst->next) {
236
		if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
Linus Torvalds's avatar
Linus Torvalds committed
237 238 239 240 241 242
		    ipv6_addr_cmp(&mc_lst->addr, addr) == 0) {
			struct net_device *dev;

			*lnk = mc_lst->next;
			write_unlock_bh(&ipv6_sk_mc_lock);

243
			if ((dev = dev_get_by_index(mc_lst->ifindex)) != NULL) {
David Stevens's avatar
David Stevens committed
244 245 246 247 248 249
				struct inet6_dev *idev = in6_dev_get(dev);

				if (idev) {
					(void) ip6_mc_leave_src(sk,mc_lst,idev);
					in6_dev_put(idev);
				}
Linus Torvalds's avatar
Linus Torvalds committed
250 251 252 253 254 255 256 257 258 259 260 261
				ipv6_dev_mc_dec(dev, &mc_lst->addr);
				dev_put(dev);
			}
			sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
			return 0;
		}
	}
	write_unlock_bh(&ipv6_sk_mc_lock);

	return -ENOENT;
}

David Stevens's avatar
David Stevens committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
struct inet6_dev *ip6_mc_find_dev(struct in6_addr *group, int ifindex)
{
	struct net_device *dev = 0;
	struct inet6_dev *idev = 0;

	if (ifindex == 0) {
		struct rt6_info *rt;

		rt = rt6_lookup(group, NULL, 0, 0);
		if (rt) {
			dev = rt->rt6i_dev;
			dev_hold(dev);
			dst_release(&rt->u.dst);
		}
	} else
		dev = dev_get_by_index(ifindex);

	if (!dev)
		return 0;
	idev = in6_dev_get(dev);
	if (!idev) {
		dev_put(dev);
		return 0;
	}
	read_lock_bh(&idev->lock);
	if (idev->dead) {
		read_unlock_bh(&idev->lock);
		in6_dev_put(idev);
		dev_put(dev);
		return 0;
	}
	return idev;
}

Linus Torvalds's avatar
Linus Torvalds committed
296 297
void ipv6_sock_mc_close(struct sock *sk)
{
298
	struct ipv6_pinfo *np = inet6_sk(sk);
Linus Torvalds's avatar
Linus Torvalds committed
299 300 301 302 303 304 305 306 307 308 309
	struct ipv6_mc_socklist *mc_lst;

	write_lock_bh(&ipv6_sk_mc_lock);
	while ((mc_lst = np->ipv6_mc_list) != NULL) {
		struct net_device *dev;

		np->ipv6_mc_list = mc_lst->next;
		write_unlock_bh(&ipv6_sk_mc_lock);

		dev = dev_get_by_index(mc_lst->ifindex);
		if (dev) {
David Stevens's avatar
David Stevens committed
310 311 312 313 314 315
			struct inet6_dev *idev = in6_dev_get(dev);

			if (idev) {
				(void) ip6_mc_leave_src(sk, mc_lst, idev);
				in6_dev_put(idev);
			}
Linus Torvalds's avatar
Linus Torvalds committed
316 317 318 319 320 321 322 323 324 325 326
			ipv6_dev_mc_dec(dev, &mc_lst->addr);
			dev_put(dev);
		}

		sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));

		write_lock_bh(&ipv6_sk_mc_lock);
	}
	write_unlock_bh(&ipv6_sk_mc_lock);
}

David Stevens's avatar
David Stevens committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
int ip6_mc_source(int add, int omode, struct sock *sk,
	struct group_source_req *pgsr)
{
	struct in6_addr *source, *group;
	struct ipv6_mc_socklist *pmc;
	struct net_device *dev;
	struct inet6_dev *idev;
	struct ipv6_pinfo *inet6 = inet6_sk(sk);
	struct ip6_sf_socklist *psl;
	int i, j, rv;
	int err;

	if (pgsr->gsr_group.ss_family != AF_INET6 ||
	    pgsr->gsr_source.ss_family != AF_INET6)
		return -EINVAL;

	source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
	group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;

	if (!(ipv6_addr_type(group) & IPV6_ADDR_MULTICAST))
		return -EINVAL;

	idev = ip6_mc_find_dev(group, pgsr->gsr_interface);
	if (!idev)
		return -ENODEV;
	dev = idev->dev;

	err = -EADDRNOTAVAIL;

	for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
		if (pmc->ifindex != pgsr->gsr_interface)
			continue;
		if (ipv6_addr_cmp(&pmc->addr, group) == 0)
			break;
	}
	if (!pmc)		/* must have a prior join */
		goto done;
	/* if a source filter was set, must be the same mode as before */
	if (pmc->sflist) {
		if (pmc->sfmode != omode)
			goto done;
	} else if (pmc->sfmode != omode) {
		/* allow mode switches for empty-set filters */
		ip6_mc_del_src(idev, group, pmc->sfmode, 0, 0, 0);
		pmc->sfmode = omode;
		ip6_mc_del_src(idev, group, pmc->sfmode, 0, 0, 0);
	}

	psl = pmc->sflist;
	if (!add) {
		if (!psl)
			goto done;
		rv = !0;
		for (i=0; i<psl->sl_count; i++) {
			rv = memcmp(&psl->sl_addr, group,
				sizeof(struct in6_addr));
			if (rv >= 0)
				break;
		}
		if (!rv)	/* source not found */
			goto done;

		/* update the interface filter */
		ip6_mc_del_src(idev, group, omode, 1, source, 1);

		for (j=i+1; j<psl->sl_count; j++)
			psl->sl_addr[j-1] = psl->sl_addr[j];
		psl->sl_count--;
		err = 0;
		goto done;
	}
	/* else, add a new source to the filter */

	if (!psl || psl->sl_count == psl->sl_max) {
		struct ip6_sf_socklist *newpsl;
		int count = IP6_SFBLOCK;

		if (psl)
			count += psl->sl_max;
		newpsl = (struct ip6_sf_socklist *)sock_kmalloc(sk,
			IP6_SFLSIZE(count), GFP_ATOMIC);
		if (!newpsl) {
			err = -ENOBUFS;
			goto done;
		}
		newpsl->sl_max = count;
		newpsl->sl_count = count - IP6_SFBLOCK;
		if (psl) {
			for (i=0; i<psl->sl_count; i++)
				newpsl->sl_addr[i] = psl->sl_addr[i];
			sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
		}
		pmc->sflist = psl = newpsl;
	}
	rv = 1;	/* > 0 for insert logic below if sl_count is 0 */
	for (i=0; i<psl->sl_count; i++) {
		rv = memcmp(&psl->sl_addr, group, sizeof(struct in6_addr));
		if (rv >= 0)
			break;
	}
	if (rv == 0)		/* address already there is an error */
		goto done;
	for (j=psl->sl_count-1; j>=i; j--)
		psl->sl_addr[j+1] = psl->sl_addr[j];
	psl->sl_addr[i] = *source;
	psl->sl_count++;
	err = 0;
	/* update the interface list */
	ip6_mc_add_src(idev, group, omode, 1, source, 1);
done:
	read_unlock_bh(&idev->lock);
	in6_dev_put(idev);
	dev_put(dev);
	return err;
}

int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
{
	struct in6_addr *group;
	struct ipv6_mc_socklist *pmc;
	struct net_device *dev;
	struct inet6_dev *idev;
	struct ipv6_pinfo *inet6 = inet6_sk(sk);
	struct ip6_sf_socklist *newpsl, *psl;
	int i, err;

	group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;

	if (!(ipv6_addr_type(group) & IPV6_ADDR_MULTICAST))
		return -EINVAL;
	if (gsf->gf_fmode != MCAST_INCLUDE &&
	    gsf->gf_fmode != MCAST_EXCLUDE)
		return -EINVAL;

	idev = ip6_mc_find_dev(group, gsf->gf_interface);

	if (!idev)
		return -ENODEV;
	dev = idev->dev;
	err = -EADDRNOTAVAIL;

	for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
		if (pmc->ifindex != gsf->gf_interface)
			continue;
		if (ipv6_addr_cmp(&pmc->addr, group) == 0)
			break;
	}
	if (!pmc)		/* must have a prior join */
		goto done;
	if (gsf->gf_numsrc) {
		newpsl = (struct ip6_sf_socklist *)sock_kmalloc(sk,
				IP6_SFLSIZE(gsf->gf_numsrc), GFP_ATOMIC);
		if (!newpsl) {
			err = -ENOBUFS;
			goto done;
		}
		newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
		for (i=0; i<newpsl->sl_count; ++i) {
			struct sockaddr_in6 *psin6;

			psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
			newpsl->sl_addr[i] = psin6->sin6_addr;
		}
		err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
			newpsl->sl_count, newpsl->sl_addr, 0);
		if (err) {
			sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
			goto done;
		}
	} else
		newpsl = 0;
	psl = pmc->sflist;
	if (psl) {
		(void) ip6_mc_del_src(idev, group, pmc->sfmode,
			psl->sl_count, psl->sl_addr, 0);
		sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
	} else
		(void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, 0, 0);
	pmc->sflist = newpsl;
	pmc->sfmode = gsf->gf_fmode;
done:
	read_unlock_bh(&idev->lock);
	in6_dev_put(idev);
	dev_put(dev);
	return err;
}

int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
	struct group_filter *optval, int *optlen)
{
	int err, i, count, copycount;
	struct in6_addr *group;
	struct ipv6_mc_socklist *pmc;
	struct inet6_dev *idev;
	struct net_device *dev;
	struct ipv6_pinfo *inet6 = inet6_sk(sk);
	struct ip6_sf_socklist *psl;

	group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;

	if (!(ipv6_addr_type(group) & IPV6_ADDR_MULTICAST))
		return -EINVAL;

	idev = ip6_mc_find_dev(group, gsf->gf_interface);

	if (!idev)
		return -ENODEV;

	dev = idev->dev;

	err = -EADDRNOTAVAIL;

	for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
		if (pmc->ifindex != gsf->gf_interface)
			continue;
		if (ipv6_addr_cmp(group, &pmc->addr) == 0)
			break;
	}
	if (!pmc)		/* must have a prior join */
		goto done;
	gsf->gf_fmode = pmc->sfmode;
	psl = pmc->sflist;
	count = psl ? psl->sl_count : 0;
	read_unlock_bh(&idev->lock);
	in6_dev_put(idev);
	dev_put(dev);

	copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
	gsf->gf_numsrc = count;
	if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
	    copy_to_user((void *)optval, gsf, GROUP_FILTER_SIZE(0))) {
		return -EFAULT;
	}
	for (i=0; i<copycount; i++) {
		struct sockaddr_in6 *psin6;
		struct sockaddr_storage ss;

		psin6 = (struct sockaddr_in6 *)&ss;
		memset(&ss, 0, sizeof(ss));
		psin6->sin6_family = AF_INET6;
		psin6->sin6_addr = psl->sl_addr[i];
	    	if (copy_to_user((void *)&optval->gf_slist[i], &ss, sizeof(ss)))
			return -EFAULT;
	}
	return 0;
done:
	read_unlock_bh(&idev->lock);
	in6_dev_put(idev);
	dev_put(dev);
	return err;
}

int inet6_mc_check(struct sock *sk, struct in6_addr *mc_addr,
	struct in6_addr *src_addr)
Linus Torvalds's avatar
Linus Torvalds committed
581
{
582
	struct ipv6_pinfo *np = inet6_sk(sk);
Linus Torvalds's avatar
Linus Torvalds committed
583
	struct ipv6_mc_socklist *mc;
David Stevens's avatar
David Stevens committed
584
	struct ip6_sf_socklist *psl;
585
	int rv = 1;
Linus Torvalds's avatar
Linus Torvalds committed
586 587

	read_lock(&ipv6_sk_mc_lock);
588
	for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
David Stevens's avatar
David Stevens committed
589 590 591 592 593
		if (ipv6_addr_cmp(&mc->addr, mc_addr) == 0)
			break;
	}
	if (!mc) {
		read_unlock(&ipv6_sk_mc_lock);
594
		return 1;
David Stevens's avatar
David Stevens committed
595 596 597 598 599 600 601 602 603 604
	}
	psl = mc->sflist;
	if (!psl) {
		rv = mc->sfmode == MCAST_EXCLUDE;
	} else {
		int i;

		for (i=0; i<psl->sl_count; i++) {
			if (ipv6_addr_cmp(&psl->sl_addr[i], src_addr) == 0)
				break;
Linus Torvalds's avatar
Linus Torvalds committed
605
		}
606 607 608 609
		if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count);
			rv = 0;
		if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count);
			rv = 0;
Linus Torvalds's avatar
Linus Torvalds committed
610 611 612
	}
	read_unlock(&ipv6_sk_mc_lock);

David Stevens's avatar
David Stevens committed
613
	return rv;
Linus Torvalds's avatar
Linus Torvalds committed
614 615 616 617 618 619 620 621 622 623
}

static void ma_put(struct ifmcaddr6 *mc)
{
	if (atomic_dec_and_test(&mc->mca_refcnt)) {
		in6_dev_put(mc->idev);
		kfree(mc);
	}
}

David Stevens's avatar
David Stevens committed
624
static void igmp6_group_added(struct ifmcaddr6 *mc)
Linus Torvalds's avatar
Linus Torvalds committed
625 626 627 628 629 630 631 632 633 634 635 636
{
	struct net_device *dev = mc->idev->dev;
	char buf[MAX_ADDR_LEN];

	spin_lock_bh(&mc->mca_lock);
	if (!(mc->mca_flags&MAF_LOADED)) {
		mc->mca_flags |= MAF_LOADED;
		if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
			dev_mc_add(dev, buf, dev->addr_len, 0);
	}
	spin_unlock_bh(&mc->mca_lock);

David Stevens's avatar
David Stevens committed
637 638 639 640
	if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
		return;

	if (MLD_V1_SEEN(mc->idev)) {
Linus Torvalds's avatar
Linus Torvalds committed
641
		igmp6_join_group(mc);
David Stevens's avatar
David Stevens committed
642 643 644 645 646 647
		return;
	}
	/* else v2 */

	mc->mca_crcount = mc->idev->mc_qrv;
	mld_ifc_event(mc->idev);
Linus Torvalds's avatar
Linus Torvalds committed
648 649
}

David Stevens's avatar
David Stevens committed
650
static void igmp6_group_dropped(struct ifmcaddr6 *mc)
Linus Torvalds's avatar
Linus Torvalds committed
651 652 653 654 655 656 657 658 659 660 661 662
{
	struct net_device *dev = mc->idev->dev;
	char buf[MAX_ADDR_LEN];

	spin_lock_bh(&mc->mca_lock);
	if (mc->mca_flags&MAF_LOADED) {
		mc->mca_flags &= ~MAF_LOADED;
		if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
			dev_mc_delete(dev, buf, dev->addr_len, 0);
	}
	spin_unlock_bh(&mc->mca_lock);

David Stevens's avatar
David Stevens committed
663 664 665
	if (mc->mca_flags & MAF_NOREPORT)
		goto done;

Linus Torvalds's avatar
Linus Torvalds committed
666 667
	if (dev->flags&IFF_UP)
		igmp6_leave_group(mc);
David Stevens's avatar
David Stevens committed
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772

	spin_lock_bh(&mc->mca_lock);
	if (del_timer(&mc->mca_timer))
		atomic_dec(&mc->mca_refcnt);
	spin_unlock_bh(&mc->mca_lock);

done:
	ip6_mc_clear_src(mc);
}

/*
 * deleted ifmcaddr6 manipulation
 */
static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
{
	struct ifmcaddr6 *pmc;

	/* this is an "ifmcaddr6" for convenience; only the fields below
	 * are actually used. In particular, the refcnt and users are not
	 * used for management of the delete list. Using the same structure
	 * for deleted items allows change reports to use common code with
	 * non-deleted or query-response MCA's.
	 */
	pmc = (struct ifmcaddr6 *)kmalloc(sizeof(*pmc), GFP_ATOMIC);
	if (!pmc)
		return;
	memset(pmc, 0, sizeof(*pmc));
	spin_lock_bh(&im->mca_lock);
	pmc->mca_lock = SPIN_LOCK_UNLOCKED;
	pmc->idev = im->idev;
	in6_dev_hold(idev);
	pmc->mca_addr = im->mca_addr;
	pmc->mca_crcount = idev->mc_qrv;
	pmc->mca_sfmode = im->mca_sfmode;
	if (pmc->mca_sfmode == MCAST_INCLUDE) {
		struct ip6_sf_list *psf;

		pmc->mca_tomb = im->mca_tomb;
		pmc->mca_sources = im->mca_sources;
		im->mca_tomb = im->mca_sources = 0;
		for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
			psf->sf_crcount = pmc->mca_crcount;
	}
	spin_unlock_bh(&im->mca_lock);

	write_lock_bh(&idev->mc_lock);
	pmc->next = idev->mc_tomb;
	idev->mc_tomb = pmc;
	write_unlock_bh(&idev->mc_lock);
}

static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
{
	struct ifmcaddr6 *pmc, *pmc_prev;
	struct ip6_sf_list *psf, *psf_next;

	write_lock_bh(&idev->mc_lock);
	pmc_prev = 0;
	for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
		if (ipv6_addr_cmp(&pmc->mca_addr, pmca) == 0)
			break;
		pmc_prev = pmc;
	}
	if (pmc) {
		if (pmc_prev)
			pmc_prev->next = pmc->next;
		else
			idev->mc_tomb = pmc->next;
	}
	write_unlock_bh(&idev->mc_lock);
	if (pmc) {
		for (psf=pmc->mca_tomb; psf; psf=psf_next) {
			psf_next = psf->sf_next;
			kfree(psf);
		}
		in6_dev_put(pmc->idev);
		kfree(pmc);
	}
}

static void mld_clear_delrec(struct inet6_dev *idev)
{
	struct ifmcaddr6 *pmc, *nextpmc;

	write_lock_bh(&idev->mc_lock);
	pmc = idev->mc_tomb;
	idev->mc_tomb = 0;
	write_unlock_bh(&idev->mc_lock);

	for (; pmc; pmc = nextpmc) {
		nextpmc = pmc->next;
		ip6_mc_clear_src(pmc);
		in6_dev_put(pmc->idev);
		kfree(pmc);
	}

	/* clear dead sources, too */
	read_lock_bh(&idev->lock);
	for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
		struct ip6_sf_list *psf, *psf_next;

		spin_lock_bh(&pmc->mca_lock);
		psf = pmc->mca_tomb;
		pmc->mca_tomb = 0;
		spin_unlock_bh(&pmc->mca_lock);
773
		for (; psf; psf=psf_next) {
David Stevens's avatar
David Stevens committed
774 775 776 777 778
			psf_next = psf->sf_next;
			kfree(psf);
		}
	}
	read_unlock_bh(&idev->lock);
Linus Torvalds's avatar
Linus Torvalds committed
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
}


/*
 *	device multicast group inc (add if not found)
 */
int ipv6_dev_mc_inc(struct net_device *dev, struct in6_addr *addr)
{
	struct ifmcaddr6 *mc;
	struct inet6_dev *idev;

	idev = in6_dev_get(dev);

	if (idev == NULL)
		return -EINVAL;

	write_lock_bh(&idev->lock);
	if (idev->dead) {
		write_unlock_bh(&idev->lock);
		in6_dev_put(idev);
		return -ENODEV;
	}

	for (mc = idev->mc_list; mc; mc = mc->next) {
		if (ipv6_addr_cmp(&mc->mca_addr, addr) == 0) {
			mc->mca_users++;
			write_unlock_bh(&idev->lock);
David Stevens's avatar
David Stevens committed
806 807
			ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
				0, 0);
Linus Torvalds's avatar
Linus Torvalds committed
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
			in6_dev_put(idev);
			return 0;
		}
	}

	/*
	 *	not found: create a new one.
	 */

	mc = kmalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);

	if (mc == NULL) {
		write_unlock_bh(&idev->lock);
		in6_dev_put(idev);
		return -ENOMEM;
	}

	memset(mc, 0, sizeof(struct ifmcaddr6));
826
	init_timer(&mc->mca_timer);
Linus Torvalds's avatar
Linus Torvalds committed
827 828 829
	mc->mca_timer.function = igmp6_timer_handler;
	mc->mca_timer.data = (unsigned long) mc;

830
	ipv6_addr_copy(&mc->mca_addr, addr);
Linus Torvalds's avatar
Linus Torvalds committed
831 832 833 834 835
	mc->idev = idev;
	mc->mca_users = 1;
	atomic_set(&mc->mca_refcnt, 2);
	mc->mca_lock = SPIN_LOCK_UNLOCKED;

David Stevens's avatar
David Stevens committed
836 837 838 839 840 841 842 843
	/* initial mode is (EX, empty) */
	mc->mca_sfmode = MCAST_EXCLUDE;
	mc->mca_sfcount[MCAST_EXCLUDE] = 1;

	if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
	    IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
		mc->mca_flags |= MAF_NOREPORT;

Linus Torvalds's avatar
Linus Torvalds committed
844 845 846 847
	mc->next = idev->mc_list;
	idev->mc_list = mc;
	write_unlock_bh(&idev->lock);

David Stevens's avatar
David Stevens committed
848
	mld_del_delrec(idev, &mc->mca_addr);
Linus Torvalds's avatar
Linus Torvalds committed
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
	igmp6_group_added(mc);
	ma_put(mc);
	return 0;
}

/*
 *	device multicast group del
 */
int ipv6_dev_mc_dec(struct net_device *dev, struct in6_addr *addr)
{
	struct inet6_dev *idev;
	struct ifmcaddr6 *ma, **map;

	idev = in6_dev_get(dev);
	if (idev == NULL)
		return -ENODEV;

	write_lock_bh(&idev->lock);
	for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
		if (ipv6_addr_cmp(&ma->mca_addr, addr) == 0) {
			if (--ma->mca_users == 0) {
				*map = ma->next;
				write_unlock_bh(&idev->lock);

				igmp6_group_dropped(ma);

				ma_put(ma);
				in6_dev_put(idev);
				return 0;
			}
			write_unlock_bh(&idev->lock);
			in6_dev_put(idev);
			return 0;
		}
	}
	write_unlock_bh(&idev->lock);
	in6_dev_put(idev);

	return -ENOENT;
}

/*
 *	check if the interface/address pair is valid
 */
David Stevens's avatar
David Stevens committed
893 894
int ipv6_chk_mcast_addr(struct net_device *dev, struct in6_addr *group,
	struct in6_addr *src_addr)
Linus Torvalds's avatar
Linus Torvalds committed
895 896 897
{
	struct inet6_dev *idev;
	struct ifmcaddr6 *mc;
David Stevens's avatar
David Stevens committed
898
	int rv = 0;
Linus Torvalds's avatar
Linus Torvalds committed
899 900 901 902 903

	idev = in6_dev_get(dev);
	if (idev) {
		read_lock_bh(&idev->lock);
		for (mc = idev->mc_list; mc; mc=mc->next) {
David Stevens's avatar
David Stevens committed
904 905 906 907 908 909 910 911 912 913
			if (ipv6_addr_cmp(&mc->mca_addr, group) == 0)
				break;
		}
		if (mc) {
			struct ip6_sf_list *psf;

			spin_lock_bh(&mc->mca_lock);
			for (psf=mc->mca_sources; psf; psf=psf->sf_next) {
				if (ipv6_addr_cmp(&psf->sf_addr, src_addr) == 0)
					break;
Linus Torvalds's avatar
Linus Torvalds committed
914
			}
David Stevens's avatar
David Stevens committed
915 916 917 918 919 920 921
			if (psf)
				rv = psf->sf_count[MCAST_INCLUDE] ||
					psf->sf_count[MCAST_EXCLUDE] !=
					mc->mca_sfcount[MCAST_EXCLUDE];
			else
				rv = mc->mca_sfcount[MCAST_EXCLUDE] != 0;
			spin_unlock_bh(&mc->mca_lock);
Linus Torvalds's avatar
Linus Torvalds committed
922 923 924 925
		}
		read_unlock_bh(&idev->lock);
		in6_dev_put(idev);
	}
David Stevens's avatar
David Stevens committed
926 927 928 929 930 931 932 933 934
	return rv;
}

static void mld_gq_start_timer(struct inet6_dev *idev)
{
	int tv = net_random() % idev->mc_maxdelay;

	idev->mc_gq_running = 1;
	if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
935
		in6_dev_hold(idev);
David Stevens's avatar
David Stevens committed
936 937 938 939 940 941 942
}

static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
{
	int tv = net_random() % delay;

	if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
943
		in6_dev_hold(idev);
Linus Torvalds's avatar
Linus Torvalds committed
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
}

/*
 *	IGMP handling (alias multicast ICMPv6 messages)
 */

static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
{
	unsigned long delay = resptime;

	/* Do not start timer for addresses with link/host scope */
	if (ipv6_addr_type(&ma->mca_addr)&(IPV6_ADDR_LINKLOCAL|IPV6_ADDR_LOOPBACK))
		return;

	if (del_timer(&ma->mca_timer)) {
		atomic_dec(&ma->mca_refcnt);
		delay = ma->mca_timer.expires - jiffies;
	}

	if (delay >= resptime) {
		if (resptime)
			delay = net_random() % resptime;
		else
			delay = 1;
	}
	ma->mca_timer.expires = jiffies + delay;
	if (!mod_timer(&ma->mca_timer, jiffies + delay))
		atomic_inc(&ma->mca_refcnt);
David Stevens's avatar
David Stevens committed
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
}

static void mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
	struct in6_addr *srcs)
{
	struct ip6_sf_list *psf;
	int i, scount;

	scount = 0;
	for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
		if (scount == nsrcs)
			break;
		for (i=0; i<nsrcs; i++)
			if (ipv6_addr_cmp(&srcs[i], &psf->sf_addr) == 0) {
				psf->sf_gsresp = 1;
				scount++;
				break;
			}
	}
Linus Torvalds's avatar
Linus Torvalds committed
991 992
}

Linus Torvalds's avatar
Linus Torvalds committed
993
int igmp6_event_query(struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
994
{
David Stevens's avatar
David Stevens committed
995
	struct mld2_query *mlh2 = (struct mld2_query *) skb->h.raw;
Linus Torvalds's avatar
Linus Torvalds committed
996
	struct ifmcaddr6 *ma;
David Stevens's avatar
David Stevens committed
997 998
	struct in6_addr *group;
	unsigned long max_delay;
Linus Torvalds's avatar
Linus Torvalds committed
999
	struct inet6_dev *idev;
Linus Torvalds's avatar
Linus Torvalds committed
1000
	struct icmp6hdr *hdr;
David Stevens's avatar
David Stevens committed
1001 1002 1003
	int group_type;
	int mark = 0;
	int len;
Linus Torvalds's avatar
Linus Torvalds committed
1004

Linus Torvalds's avatar
Linus Torvalds committed
1005
	if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
Linus Torvalds's avatar
Linus Torvalds committed
1006 1007
		return -EINVAL;

David Stevens's avatar
David Stevens committed
1008
	len = ntohs(skb->nh.ipv6h->payload_len);
Linus Torvalds's avatar
Linus Torvalds committed
1009

Linus Torvalds's avatar
Linus Torvalds committed
1010 1011 1012 1013 1014 1015 1016 1017 1018
	/* Drop queries with not link local source */
	if (!(ipv6_addr_type(&skb->nh.ipv6h->saddr)&IPV6_ADDR_LINKLOCAL))
		return -EINVAL;

	idev = in6_dev_get(skb->dev);

	if (idev == NULL)
		return 0;

David Stevens's avatar
David Stevens committed
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
	hdr = (struct icmp6hdr *) skb->h.raw;
	group = (struct in6_addr *) (hdr + 1);
	group_type = ipv6_addr_type(group);

	if (group_type != IPV6_ADDR_ANY &&
	    !(group_type&IPV6_ADDR_MULTICAST)) {
		in6_dev_put(idev);
		return -EINVAL;
	}

	if (len == 24) {
		int switchback;
		/* MLDv1 router present */

		/* Translate milliseconds to jiffies */
		max_delay = ntohs(hdr->icmp6_maxdelay)*(HZ/10);

		switchback = (idev->mc_qrv + 1) * max_delay;
		idev->mc_v1_seen = jiffies + switchback;

		/* cancel the interface change timer */
		idev->mc_ifc_count = 0;
		if (del_timer(&idev->mc_ifc_timer))
1042
			__in6_dev_put(idev);
David Stevens's avatar
David Stevens committed
1043 1044 1045 1046
		/* clear deleted report items */
		mld_clear_delrec(idev);
	} else if (len >= 28) {
		max_delay = MLDV2_MRC(ntohs(mlh2->mrc))*(HZ/10);
1047 1048
		if (!max_delay)
			max_delay = 1;
David Stevens's avatar
David Stevens committed
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
		idev->mc_maxdelay = max_delay;
		if (mlh2->qrv)
			idev->mc_qrv = mlh2->qrv;
		if (group_type == IPV6_ADDR_ANY) { /* general query */
			if (mlh2->nsrcs) {
				in6_dev_put(idev);
				return -EINVAL; /* no sources allowed */
			}
			mld_gq_start_timer(idev);
			in6_dev_put(idev);
			return 0;
		}
		/* mark sources to include, if group & source-specific */
		mark = mlh2->nsrcs != 0;
	} else {
		in6_dev_put(idev);
		return -EINVAL;
	}

	read_lock_bh(&idev->lock);
	if (group_type == IPV6_ADDR_ANY) {
		for (ma = idev->mc_list; ma; ma=ma->next) {
			spin_lock_bh(&ma->mca_lock);
			igmp6_group_queried(ma, max_delay);
			spin_unlock_bh(&ma->mca_lock);
		}
Linus Torvalds's avatar
Linus Torvalds committed
1075 1076
	} else {
		for (ma = idev->mc_list; ma; ma=ma->next) {
David Stevens's avatar
David Stevens committed
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
			if (group_type != IPV6_ADDR_ANY &&
			    ipv6_addr_cmp(group, &ma->mca_addr) != 0)
				continue;
			spin_lock_bh(&ma->mca_lock);
			if (ma->mca_flags & MAF_TIMER_RUNNING) {
				/* gsquery <- gsquery && mark */
				if (!mark)
					ma->mca_flags &= ~MAF_GSQUERY;
			} else {
				/* gsquery <- mark */
				if (mark)
					ma->mca_flags |= MAF_GSQUERY;
				else
					ma->mca_flags &= ~MAF_GSQUERY;
Linus Torvalds's avatar
Linus Torvalds committed
1091
			}
David Stevens's avatar
David Stevens committed
1092 1093 1094 1095 1096 1097 1098
			if (ma->mca_flags & MAF_GSQUERY)
				mld_marksources(ma, ntohs(mlh2->nsrcs),
					mlh2->srcs);
			igmp6_group_queried(ma, max_delay);
			spin_unlock_bh(&ma->mca_lock);
			if (group_type != IPV6_ADDR_ANY)
				break;
Linus Torvalds's avatar
Linus Torvalds committed
1099 1100
		}
	}
David Stevens's avatar
David Stevens committed
1101
	read_unlock_bh(&idev->lock);
Linus Torvalds's avatar
Linus Torvalds committed
1102 1103 1104 1105 1106 1107
	in6_dev_put(idev);

	return 0;
}


Linus Torvalds's avatar
Linus Torvalds committed
1108
int igmp6_event_report(struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
1109 1110 1111 1112
{
	struct ifmcaddr6 *ma;
	struct in6_addr *addrp;
	struct inet6_dev *idev;
Linus Torvalds's avatar
Linus Torvalds committed
1113
	struct icmp6hdr *hdr;
1114
	int addr_type;
Linus Torvalds's avatar
Linus Torvalds committed
1115 1116 1117 1118 1119

	/* Our own report looped back. Ignore it. */
	if (skb->pkt_type == PACKET_LOOPBACK)
		return 0;

Linus Torvalds's avatar
Linus Torvalds committed
1120
	if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
Linus Torvalds's avatar
Linus Torvalds committed
1121 1122
		return -EINVAL;

Linus Torvalds's avatar
Linus Torvalds committed
1123 1124
	hdr = (struct icmp6hdr*) skb->h.raw;

Linus Torvalds's avatar
Linus Torvalds committed
1125
	/* Drop reports with not link local source */
1126 1127 1128
	addr_type = ipv6_addr_type(&skb->nh.ipv6h->saddr);
	if (addr_type != IPV6_ADDR_ANY && 
	    !(addr_type&IPV6_ADDR_LINKLOCAL))
Linus Torvalds's avatar
Linus Torvalds committed
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
		return -EINVAL;

	addrp = (struct in6_addr *) (hdr + 1);

	idev = in6_dev_get(skb->dev);
	if (idev == NULL)
		return -ENODEV;

	/*
	 *	Cancel the timer for this group
	 */

David Stevens's avatar
David Stevens committed
1141
	read_lock_bh(&idev->lock);
Linus Torvalds's avatar
Linus Torvalds committed
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
	for (ma = idev->mc_list; ma; ma=ma->next) {
		if (ipv6_addr_cmp(&ma->mca_addr, addrp) == 0) {
			spin_lock(&ma->mca_lock);
			if (del_timer(&ma->mca_timer))
				atomic_dec(&ma->mca_refcnt);
			ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
			spin_unlock(&ma->mca_lock);
			break;
		}
	}
David Stevens's avatar
David Stevens committed
1152
	read_unlock_bh(&idev->lock);
Linus Torvalds's avatar
Linus Torvalds committed
1153 1154 1155 1156
	in6_dev_put(idev);
	return 0;
}

David Stevens's avatar
David Stevens committed
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
	int gdeleted, int sdeleted)
{
	switch (type) {
	case MLD2_MODE_IS_INCLUDE:
	case MLD2_MODE_IS_EXCLUDE:
		if (gdeleted || sdeleted)
			return 0;
		return !((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp);
	case MLD2_CHANGE_TO_INCLUDE:
		if (gdeleted || sdeleted)
			return 0;
		return psf->sf_count[MCAST_INCLUDE] != 0;
	case MLD2_CHANGE_TO_EXCLUDE:
		if (gdeleted || sdeleted)
			return 0;
		if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
		    psf->sf_count[MCAST_INCLUDE])
			return 0;
		return pmc->mca_sfcount[MCAST_EXCLUDE] ==
			psf->sf_count[MCAST_EXCLUDE];
	case MLD2_ALLOW_NEW_SOURCES:
		if (gdeleted || !psf->sf_crcount)
			return 0;
		return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
	case MLD2_BLOCK_OLD_SOURCES:
		if (pmc->mca_sfmode == MCAST_INCLUDE)
			return gdeleted || (psf->sf_crcount && sdeleted);
		return psf->sf_crcount && !gdeleted && !sdeleted;
	}
	return 0;
}

static int
mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
{
	struct ip6_sf_list *psf;
	int scount = 0;

	for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
		if (!is_in(pmc, psf, type, gdeleted, sdeleted))
			continue;
		scount++;
	}
	return scount;
}

static struct sk_buff *mld_newpack(struct net_device *dev, int size)
{
	struct sock *sk = igmp6_socket->sk;
	struct sk_buff *skb;
	struct mld2_report *pmr;
	struct in6_addr addr_buf;
	int err;
	u8 ra[8] = { IPPROTO_ICMPV6, 0,
		     IPV6_TLV_ROUTERALERT, 2, 0, 0,
		     IPV6_TLV_PADN, 0 };

1215
	skb = sock_alloc_send_skb(sk, size + dev->hard_header_len+15, 1, &err);
David Stevens's avatar
David Stevens committed
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231

	if (skb == 0)
		return 0;

	skb_reserve(skb, (dev->hard_header_len + 15) & ~15);
	if (dev->hard_header) {
		unsigned char ha[MAX_ADDR_LEN];

		ndisc_mc_map(&mld2_all_mcr, ha, dev, 1);
		if (dev->hard_header(skb, dev, ETH_P_IPV6,ha,NULL,size) < 0) {
			kfree_skb(skb);
			return 0;
		}
	}

	if (ipv6_get_lladdr(dev, &addr_buf)) {
1232
		/* <draft-ietf-magma-mld-source-05.txt>:
David Stevens's avatar
David Stevens committed
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
		 * use unspecified address as the source address 
		 * when a valid link-local address is not available.
		 */
		memset(&addr_buf, 0, sizeof(addr_buf));
	}

	ip6_nd_hdr(sk, skb, dev, &addr_buf, &mld2_all_mcr, NEXTHDR_HOP, 0);

	memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));

	pmr =(struct mld2_report *)skb_put(skb, sizeof(*pmr));
	skb->h.raw = (unsigned char *)pmr;
	pmr->type = ICMPV6_MLD2_REPORT;
	pmr->resv1 = 0;
	pmr->csum = 0;
	pmr->resv2 = 0;
	pmr->ngrec = 0;
	return skb;
}

static void mld_sendpack(struct sk_buff *skb)
{
	struct ipv6hdr *pip6 = skb->nh.ipv6h;
	struct mld2_report *pmr = (struct mld2_report *)skb->h.raw;
	int payload_len, mldlen;
1258
	struct inet6_dev *idev = in6_dev_get(skb->dev);
David Stevens's avatar
David Stevens committed
1259 1260 1261 1262 1263 1264 1265 1266 1267

	payload_len = skb->tail - (unsigned char *)skb->nh.ipv6h -
		sizeof(struct ipv6hdr);
	mldlen = skb->tail - skb->h.raw;
	pip6->payload_len = htons(payload_len);

	pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
		IPPROTO_ICMPV6, csum_partial(skb->h.raw, mldlen, 0));
	dev_queue_xmit(skb);
1268 1269 1270
	ICMP6_INC_STATS(idev,Icmp6OutMsgs);
	if (likely(idev != NULL))
		in6_dev_put(idev);
David Stevens's avatar
David Stevens committed
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
}

static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
{
	return sizeof(struct mld2_grec) + 4*mld_scount(pmc,type,gdel,sdel);
}

static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
	int type, struct mld2_grec **ppgr)
{
	struct net_device *dev = pmc->idev->dev;
	struct mld2_report *pmr;
	struct mld2_grec *pgr;

	if (!skb)
		skb = mld_newpack(dev, dev->mtu);
	if (!skb)
		return 0;
	pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
	pgr->grec_type = type;
	pgr->grec_auxwords = 0;
	pgr->grec_nsrcs = 0;
	pgr->grec_mca = pmc->mca_addr;	/* structure copy */
	pmr = (struct mld2_report *)skb->h.raw;
	pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
	*ppgr = pgr;
	return skb;
}

#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
	skb_tailroom(skb)) : 0)

static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
	int type, int gdeleted, int sdeleted)
{
	struct net_device *dev = pmc->idev->dev;
	struct mld2_report *pmr;
	struct mld2_grec *pgr = 0;
	struct ip6_sf_list *psf, *psf_next, *psf_prev, *psf_list;
	int scount, first, isquery, truncate;

	if (pmc->mca_flags & MAF_NOREPORT)
		return skb;

	isquery = type == MLD2_MODE_IS_INCLUDE ||
		  type == MLD2_MODE_IS_EXCLUDE;
	truncate = type == MLD2_MODE_IS_EXCLUDE ||
		    type == MLD2_CHANGE_TO_EXCLUDE;

	psf_list = sdeleted ? pmc->mca_tomb : pmc->mca_sources;

	if (!psf_list) {
		if (type == MLD2_ALLOW_NEW_SOURCES ||
		    type == MLD2_BLOCK_OLD_SOURCES)
			return skb;
1326 1327 1328 1329 1330 1331 1332 1333 1334
		if (pmc->mca_crcount || isquery) {
			/* make sure we have room for group header and at
			 * least one source.
			 */
			if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)+
			    sizeof(struct in6_addr)) {
				mld_sendpack(skb);
				skb = 0; /* add_grhead will get a new one */
			}
David Stevens's avatar
David Stevens committed
1335
			skb = add_grhead(skb, pmc, type, &pgr);
1336
		}
David Stevens's avatar
David Stevens committed
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
		return skb;
	}
	pmr = skb ? (struct mld2_report *)skb->h.raw : 0;

	/* EX and TO_EX get a fresh packet, if needed */
	if (truncate) {
		if (pmr && pmr->ngrec &&
		    AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
			if (skb)
				mld_sendpack(skb);
			skb = mld_newpack(dev, dev->mtu);
		}
	}
	first = 1;
	scount = 0;
	psf_prev = 0;
	for (psf=psf_list; psf; psf=psf_next) {
		struct in6_addr *psrc;

		psf_next = psf->sf_next;

		if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
			psf_prev = psf;
			continue;
		}

		/* clear marks on query responses */
		if (isquery)
			psf->sf_gsresp = 0;

		if (AVAILABLE(skb) < sizeof(*psrc) +
		    first*sizeof(struct mld2_grec)) {
			if (truncate && !first)
				break;	 /* truncate these */
			if (pgr)
				pgr->grec_nsrcs = htons(scount);
			if (skb)
				mld_sendpack(skb);
			skb = mld_newpack(dev, dev->mtu);
			first = 1;
			scount = 0;
		}
		if (first) {
			skb = add_grhead(skb, pmc, type, &pgr);
			first = 0;
		}
		psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
		*psrc = psf->sf_addr;
		scount++;
		if ((type == MLD2_ALLOW_NEW_SOURCES ||
		     type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
			psf->sf_crcount--;
			if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
				if (psf_prev)
					psf_prev->sf_next = psf->sf_next;
				else
					pmc->mca_tomb = psf->sf_next;
				kfree(psf);
				continue;
			}
		}
		psf_prev = psf;
	}
	if (pgr)
		pgr->grec_nsrcs = htons(scount);

	if (isquery)
		pmc->mca_flags &= ~MAF_GSQUERY;	/* clear query state */
	return skb;
}

static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
{
	struct sk_buff *skb = 0;
	int type;

	if (!pmc) {
		read_lock_bh(&idev->lock);
		for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
			if (pmc->mca_flags & MAF_NOREPORT)
				continue;
			spin_lock_bh(&pmc->mca_lock);
			if (pmc->mca_sfcount[MCAST_EXCLUDE])
				type = MLD2_MODE_IS_EXCLUDE;
			else
				type = MLD2_MODE_IS_INCLUDE;
			skb = add_grec(skb, pmc, type, 0, 0);
			spin_unlock_bh(&pmc->mca_lock);
		}
		read_unlock_bh(&idev->lock);
	} else {
		spin_lock_bh(&pmc->mca_lock);
		if (pmc->mca_sfcount[MCAST_EXCLUDE])
			type = MLD2_MODE_IS_EXCLUDE;
		else
			type = MLD2_MODE_IS_INCLUDE;
		skb = add_grec(skb, pmc, type, 0, 0);
		spin_unlock_bh(&pmc->mca_lock);
	}
	if (skb)
		mld_sendpack(skb);
}

/*
 * remove zero-count source records from a source filter list
 */
static void mld_clear_zeros(struct ip6_sf_list **ppsf)
{
	struct ip6_sf_list *psf_prev, *psf_next, *psf;

	psf_prev = 0;
	for (psf=*ppsf; psf; psf = psf_next) {
		psf_next = psf->sf_next;
		if (psf->sf_crcount == 0) {
			if (psf_prev)
				psf_prev->sf_next = psf->sf_next;
			else
				*ppsf = psf->sf_next;
			kfree(psf);
		} else
			psf_prev = psf;
	}
}

static void mld_send_cr(struct inet6_dev *idev)
{
	struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
	struct sk_buff *skb = 0;
	int type, dtype;

	read_lock_bh(&idev->lock);
	write_lock_bh(&idev->mc_lock);

	/* deleted MCA's */
	pmc_prev = 0;
	for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
		pmc_next = pmc->next;
		if (pmc->mca_sfmode == MCAST_INCLUDE) {
			type = MLD2_BLOCK_OLD_SOURCES;
			dtype = MLD2_BLOCK_OLD_SOURCES;
			skb = add_grec(skb, pmc, type, 1, 0);
			skb = add_grec(skb, pmc, dtype, 1, 1);
		}
		if (pmc->mca_crcount) {
			pmc->mca_crcount--;
			if (pmc->mca_sfmode == MCAST_EXCLUDE) {
				type = MLD2_CHANGE_TO_INCLUDE;
				skb = add_grec(skb, pmc, type, 1, 0);
			}
			if (pmc->mca_crcount == 0) {
				mld_clear_zeros(&pmc->mca_tomb);
				mld_clear_zeros(&pmc->mca_sources);
			}
		}
		if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
		    !pmc->mca_sources) {
			if (pmc_prev)
				pmc_prev->next = pmc_next;
			else
				idev->mc_tomb = pmc_next;
			in6_dev_put(pmc->idev);
			kfree(pmc);
		} else
			pmc_prev = pmc;
	}
	write_unlock_bh(&idev->mc_lock);

	/* change recs */
	for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
		spin_lock_bh(&pmc->mca_lock);
		if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
			type = MLD2_BLOCK_OLD_SOURCES;
			dtype = MLD2_ALLOW_NEW_SOURCES;
		} else {
			type = MLD2_ALLOW_NEW_SOURCES;
			dtype = MLD2_BLOCK_OLD_SOURCES;
		}
		skb = add_grec(skb, pmc, type, 0, 0);
		skb = add_grec(skb, pmc, dtype, 0, 1);	/* deleted sources */

		/* filter mode changes */
		if (pmc->mca_crcount) {
			pmc->mca_crcount--;
			if (pmc->mca_sfmode == MCAST_EXCLUDE)
				type = MLD2_CHANGE_TO_EXCLUDE;
			else
				type = MLD2_CHANGE_TO_INCLUDE;
			skb = add_grec(skb, pmc, type, 0, 0);
		}
		spin_unlock_bh(&pmc->mca_lock);
	}
	read_unlock_bh(&idev->lock);
	if (!skb)
		return;
	(void) mld_sendpack(skb);
}

Stephen Rothwell's avatar
Stephen Rothwell committed
1534
static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
Linus Torvalds's avatar
Linus Torvalds committed
1535 1536
{
	struct sock *sk = igmp6_socket->sk;
1537
	struct inet6_dev *idev;
Linus Torvalds's avatar
Linus Torvalds committed
1538 1539 1540 1541 1542 1543 1544 1545
        struct sk_buff *skb;
        struct icmp6hdr *hdr;
	struct in6_addr *snd_addr;
	struct in6_addr *addrp;
	struct in6_addr addr_buf;
	struct in6_addr all_routers;
	int err, len, payload_len, full_len;
	u8 ra[8] = { IPPROTO_ICMPV6, 0,
Linus Torvalds's avatar
Linus Torvalds committed
1546
		     IPV6_TLV_ROUTERALERT, 2, 0, 0,
Linus Torvalds's avatar
Linus Torvalds committed
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
		     IPV6_TLV_PADN, 0 };

	snd_addr = addr;
	if (type == ICMPV6_MGM_REDUCTION) {
		snd_addr = &all_routers;
		ipv6_addr_all_routers(&all_routers);
	}

	len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
	payload_len = len + sizeof(ra);
	full_len = sizeof(struct ipv6hdr) + payload_len;

1559
	skb = sock_alloc_send_skb(sk, dev->hard_header_len + full_len + 15, 1, &err);
Linus Torvalds's avatar
Linus Torvalds committed
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572

	if (skb == NULL)
		return;

	skb_reserve(skb, (dev->hard_header_len + 15) & ~15);
	if (dev->hard_header) {
		unsigned char ha[MAX_ADDR_LEN];
		ndisc_mc_map(snd_addr, ha, dev, 1);
		if (dev->hard_header(skb, dev, ETH_P_IPV6, ha, NULL, full_len) < 0)
			goto out;
	}

	if (ipv6_get_lladdr(dev, &addr_buf)) {
1573
		/* <draft-ietf-magma-mld-source-05.txt>:
1574 1575 1576 1577
		 * use unspecified address as the source address 
		 * when a valid link-local address is not available.
		 */
		memset(&addr_buf, 0, sizeof(addr_buf));
Linus Torvalds's avatar
Linus Torvalds committed
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
	}

	ip6_nd_hdr(sk, skb, dev, &addr_buf, snd_addr, NEXTHDR_HOP, payload_len);

	memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));

	hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
	memset(hdr, 0, sizeof(struct icmp6hdr));
	hdr->icmp6_type = type;

	addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
	ipv6_addr_copy(addrp, addr);

	hdr->icmp6_cksum = csum_ipv6_magic(&addr_buf, snd_addr, len,
					   IPPROTO_ICMPV6,
					   csum_partial((__u8 *) hdr, len, 0));

1595 1596
	idev = in6_dev_get(skb->dev);

Linus Torvalds's avatar
Linus Torvalds committed
1597 1598
	dev_queue_xmit(skb);
	if (type == ICMPV6_MGM_REDUCTION)
1599
		ICMP6_INC_STATS(idev, Icmp6OutGroupMembReductions);
Linus Torvalds's avatar
Linus Torvalds committed
1600
	else
1601 1602 1603 1604 1605
		ICMP6_INC_STATS(idev, Icmp6OutGroupMembResponses);
	ICMP6_INC_STATS(idev, Icmp6OutMsgs);

	if (likely(idev != NULL))
		in6_dev_put(idev);
Linus Torvalds's avatar
Linus Torvalds committed
1606 1607 1608 1609 1610 1611
	return;

out:
	kfree_skb(skb);
}

David Stevens's avatar
David Stevens committed
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
	struct in6_addr *psfsrc)
{
	struct ip6_sf_list *psf, *psf_prev;
	int rv = 0;

	psf_prev = 0;
	for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
		if (ipv6_addr_cmp(&psf->sf_addr, psfsrc) == 0)
			break;
		psf_prev = psf;
	}
	if (!psf || psf->sf_count[sfmode] == 0) {
		/* source filter not found, or count wrong =>  bug */
		return -ESRCH;
	}
	psf->sf_count[sfmode]--;
	if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
		struct inet6_dev *idev = pmc->idev;

		/* no more filters for this source */
		if (psf_prev)
			psf_prev->sf_next = psf->sf_next;
		else
			pmc->mca_sources = psf->sf_next;
		if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
		    !MLD_V1_SEEN(idev)) {
			psf->sf_crcount = idev->mc_qrv;
			psf->sf_next = pmc->mca_tomb;
			pmc->mca_tomb = psf;
			rv = 1;
		} else
			kfree(psf);
	}
	return rv;
}

int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca, int sfmode,
	int sfcount, struct in6_addr *psfsrc, int delta)
{
	struct ifmcaddr6 *pmc;
	int	changerec = 0;
	int	i, err;

	if (!idev)
		return -ENODEV;
	read_lock_bh(&idev->lock);
	for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
		if (ipv6_addr_cmp(pmca, &pmc->mca_addr) == 0)
			break;
	}
	if (!pmc) {
		/* MCA not found?? bug */
		read_unlock_bh(&idev->lock);
		return -ESRCH;
	}
	spin_lock_bh(&pmc->mca_lock);
	read_unlock_bh(&idev->lock);
	sf_markstate(pmc);
	if (!delta) {
		if (!pmc->mca_sfcount[sfmode]) {
			spin_unlock_bh(&pmc->mca_lock);
			return -EINVAL;
		}
		pmc->mca_sfcount[sfmode]--;
	}
	err = 0;
	for (i=0; i<sfcount; i++) {
		int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);

		changerec |= rv > 0;
		if (!err && rv < 0)
			err = rv;
	}
	if (pmc->mca_sfmode == MCAST_EXCLUDE &&
	    pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
	    pmc->mca_sfcount[MCAST_INCLUDE]) {
		struct ip6_sf_list *psf;

		/* filter mode change */
		pmc->mca_sfmode = MCAST_INCLUDE;
		pmc->mca_crcount = idev->mc_qrv;
		idev->mc_ifc_count = pmc->mca_crcount;
		for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
			psf->sf_crcount = 0;
		mld_ifc_event(pmc->idev);
	} else if (sf_setstate(pmc) || changerec)
		mld_ifc_event(pmc->idev);
	spin_unlock_bh(&pmc->mca_lock);
	return err;
}

/*
 * Add multicast single-source filter to the interface list
 */
static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
	struct in6_addr *psfsrc, int delta)
{
	struct ip6_sf_list *psf, *psf_prev;

	psf_prev = 0;
	for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
		if (ipv6_addr_cmp(&psf->sf_addr, psfsrc) == 0)
			break;
		psf_prev = psf;
	}
	if (!psf) {
		psf = (struct ip6_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
		if (!psf)
			return -ENOBUFS;
		memset(psf, 0, sizeof(*psf));
		psf->sf_addr = *psfsrc;
		if (psf_prev) {
			psf_prev->sf_next = psf;
		} else
			pmc->mca_sources = psf;
	}
	psf->sf_count[sfmode]++;
	return 0;
}

static void sf_markstate(struct ifmcaddr6 *pmc)
{
	struct ip6_sf_list *psf;
	int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];

	for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
		if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
			psf->sf_oldin = mca_xcount ==
				psf->sf_count[MCAST_EXCLUDE] &&
				!psf->sf_count[MCAST_INCLUDE];
		} else
			psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
}

static int sf_setstate(struct ifmcaddr6 *pmc)
{
	struct ip6_sf_list *psf;
	int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
	int qrv = pmc->idev->mc_qrv;
	int new_in, rv;

	rv = 0;
	for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
		if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
			new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
				!psf->sf_count[MCAST_INCLUDE];
		} else
			new_in = psf->sf_count[MCAST_INCLUDE] != 0;
		if (new_in != psf->sf_oldin) {
			psf->sf_crcount = qrv;
			rv++;
		}
	}
	return rv;
}

/*
 * Add multicast source filter list to the interface list
 */
int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca, int sfmode,
	int sfcount, struct in6_addr *psfsrc, int delta)
{
	struct ifmcaddr6 *pmc;
	int	isexclude;
	int	i, err;

	if (!idev)
		return -ENODEV;
	read_lock_bh(&idev->lock);
	for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
		if (ipv6_addr_cmp(pmca, &pmc->mca_addr) == 0)
			break;
	}
	if (!pmc) {
		/* MCA not found?? bug */
		read_unlock_bh(&idev->lock);
		return -ESRCH;
	}
	spin_lock_bh(&pmc->mca_lock);
	read_unlock_bh(&idev->lock);

	sf_markstate(pmc);
	isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
	if (!delta)
		pmc->mca_sfcount[sfmode]++;
	err = 0;
	for (i=0; i<sfcount; i++) {
		err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
		if (err)
			break;
	}
	if (err) {
		int j;

		pmc->mca_sfcount[sfmode]--;
		for (j=0; j<i; j++)
			(void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
	} else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
		struct inet6_dev *idev = pmc->idev;
		struct ip6_sf_list *psf;

		/* filter mode change */
		if (pmc->mca_sfcount[MCAST_EXCLUDE])
			pmc->mca_sfmode = MCAST_EXCLUDE;
		else if (pmc->mca_sfcount[MCAST_INCLUDE])
			pmc->mca_sfmode = MCAST_INCLUDE;
		/* else no filters; keep old mode for reports */

		pmc->mca_crcount = idev->mc_qrv;
		idev->mc_ifc_count = pmc->mca_crcount;
		for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
			psf->sf_crcount = 0;
		mld_ifc_event(idev);
	} else if (sf_setstate(pmc))
		mld_ifc_event(idev);
	spin_unlock_bh(&pmc->mca_lock);
	return err;
}

static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
{
	struct ip6_sf_list *psf, *nextpsf;

	for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
		nextpsf = psf->sf_next;
		kfree(psf);
	}
	pmc->mca_tomb = 0;
	for (psf=pmc->mca_sources; psf; psf=nextpsf) {
		nextpsf = psf->sf_next;
		kfree(psf);
	}
	pmc->mca_sources = 0;
	pmc->mca_sfmode = MCAST_EXCLUDE;
	pmc->mca_sfcount[MCAST_EXCLUDE] = 0;
	pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
}


Linus Torvalds's avatar
Linus Torvalds committed
1852 1853 1854 1855
static void igmp6_join_group(struct ifmcaddr6 *ma)
{
	unsigned long delay;

David Stevens's avatar
David Stevens committed
1856
	if (ma->mca_flags & MAF_NOREPORT)
Linus Torvalds's avatar
Linus Torvalds committed
1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
		return;

	igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);

	delay = net_random() % IGMP6_UNSOLICITED_IVAL;

	spin_lock_bh(&ma->mca_lock);
	if (del_timer(&ma->mca_timer)) {
		atomic_dec(&ma->mca_refcnt);
		delay = ma->mca_timer.expires - jiffies;
	}

	if (!mod_timer(&ma->mca_timer, jiffies + delay))
		atomic_inc(&ma->mca_refcnt);
	ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
	spin_unlock_bh(&ma->mca_lock);
}

David Stevens's avatar
David Stevens committed
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890
int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
	struct inet6_dev *idev)
{
	int err;

	if (iml->sflist == 0) {
		/* any-source empty exclude case */
		return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, 0, 0);
	}
	err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
		iml->sflist->sl_count, iml->sflist->sl_addr, 0);
	sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
	iml->sflist = 0;
	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
1891 1892
static void igmp6_leave_group(struct ifmcaddr6 *ma)
{
David Stevens's avatar
David Stevens committed
1893 1894 1895 1896 1897 1898 1899 1900 1901
	if (MLD_V1_SEEN(ma->idev)) {
		if (ma->mca_flags & MAF_LAST_REPORTER)
			igmp6_send(&ma->mca_addr, ma->idev->dev,
				ICMPV6_MGM_REDUCTION);
	} else {
		mld_add_delrec(ma->idev, ma);
		mld_ifc_event(ma->idev);
	}
}
Linus Torvalds's avatar
Linus Torvalds committed
1902

David Stevens's avatar
David Stevens committed
1903 1904 1905
static void mld_gq_timer_expire(unsigned long data)
{
	struct inet6_dev *idev = (struct inet6_dev *)data;
Linus Torvalds's avatar
Linus Torvalds committed
1906

David Stevens's avatar
David Stevens committed
1907 1908
	idev->mc_gq_running = 0;
	mld_send_report(idev, 0);
1909
	__in6_dev_put(idev);
David Stevens's avatar
David Stevens committed
1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
}

static void mld_ifc_timer_expire(unsigned long data)
{
	struct inet6_dev *idev = (struct inet6_dev *)data;

	mld_send_cr(idev);
	if (idev->mc_ifc_count) {
		idev->mc_ifc_count--;
		if (idev->mc_ifc_count)
			mld_ifc_start_timer(idev, idev->mc_maxdelay);
	}
1922
	__in6_dev_put(idev);
David Stevens's avatar
David Stevens committed
1923 1924 1925 1926 1927 1928 1929 1930
}

static void mld_ifc_event(struct inet6_dev *idev)
{
	if (MLD_V1_SEEN(idev))
		return;
	idev->mc_ifc_count = idev->mc_qrv;
	mld_ifc_start_timer(idev, 1);
Linus Torvalds's avatar
Linus Torvalds committed
1931 1932
}

David Stevens's avatar
David Stevens committed
1933

Stephen Rothwell's avatar
Stephen Rothwell committed
1934
static void igmp6_timer_handler(unsigned long data)
Linus Torvalds's avatar
Linus Torvalds committed
1935 1936 1937
{
	struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;

David Stevens's avatar
David Stevens committed
1938 1939 1940 1941
	if (MLD_V1_SEEN(ma->idev))
		igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
	else
		mld_send_report(ma->idev, ma);
Linus Torvalds's avatar
Linus Torvalds committed
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958

	spin_lock(&ma->mca_lock);
	ma->mca_flags |=  MAF_LAST_REPORTER;
	ma->mca_flags &= ~MAF_TIMER_RUNNING;
	spin_unlock(&ma->mca_lock);
	ma_put(ma);
}

/* Device going down */

void ipv6_mc_down(struct inet6_dev *idev)
{
	struct ifmcaddr6 *i;

	/* Withdraw multicast list */

	read_lock_bh(&idev->lock);
David Stevens's avatar
David Stevens committed
1959 1960
	idev->mc_ifc_count = 0;
	if (del_timer(&idev->mc_ifc_timer))
1961
		__in6_dev_put(idev);
David Stevens's avatar
David Stevens committed
1962 1963
	idev->mc_gq_running = 0;
	if (del_timer(&idev->mc_gq_timer))
1964
		__in6_dev_put(idev);
David Stevens's avatar
David Stevens committed
1965

Linus Torvalds's avatar
Linus Torvalds committed
1966 1967 1968
	for (i = idev->mc_list; i; i=i->next)
		igmp6_group_dropped(i);
	read_unlock_bh(&idev->lock);
David Stevens's avatar
David Stevens committed
1969 1970

	mld_clear_delrec(idev);
Linus Torvalds's avatar
Linus Torvalds committed
1971 1972
}

Linus Torvalds's avatar
Linus Torvalds committed
1973

Linus Torvalds's avatar
Linus Torvalds committed
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
/* Device going up */

void ipv6_mc_up(struct inet6_dev *idev)
{
	struct ifmcaddr6 *i;

	/* Install multicast list, except for all-nodes (already installed) */

	read_lock_bh(&idev->lock);
	for (i = idev->mc_list; i; i=i->next)
		igmp6_group_added(i);
	read_unlock_bh(&idev->lock);
}

Linus Torvalds's avatar
Linus Torvalds committed
1988 1989 1990 1991 1992 1993
/* IPv6 device initialization. */

void ipv6_mc_init_dev(struct inet6_dev *idev)
{
	struct in6_addr maddr;

David Stevens's avatar
David Stevens committed
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
	write_lock_bh(&idev->lock);
	idev->mc_lock = RW_LOCK_UNLOCKED;
	idev->mc_gq_running = 0;
	init_timer(&idev->mc_gq_timer);
	idev->mc_gq_timer.data = (unsigned long) idev;
	idev->mc_gq_timer.function = &mld_gq_timer_expire;
	idev->mc_tomb = 0;
	idev->mc_ifc_count = 0;
	init_timer(&idev->mc_ifc_timer);
	idev->mc_ifc_timer.data = (unsigned long) idev;
	idev->mc_ifc_timer.function = &mld_ifc_timer_expire;
	idev->mc_qrv = MLD_QRV_DEFAULT;
	idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
	idev->mc_v1_seen = 0;
	write_unlock_bh(&idev->lock);

Linus Torvalds's avatar
Linus Torvalds committed
2010 2011 2012 2013 2014
	/* Add all-nodes address. */
	ipv6_addr_all_nodes(&maddr);
	ipv6_dev_mc_inc(idev->dev, &maddr);
}

Linus Torvalds's avatar
Linus Torvalds committed
2015 2016 2017 2018 2019 2020 2021
/*
 *	Device is about to be destroyed: clean up.
 */

void ipv6_mc_destroy_dev(struct inet6_dev *idev)
{
	struct ifmcaddr6 *i;
Linus Torvalds's avatar
Linus Torvalds committed
2022 2023 2024 2025 2026
	struct in6_addr maddr;

	/* Delete all-nodes address. */
	ipv6_addr_all_nodes(&maddr);
	ipv6_dev_mc_dec(idev->dev, &maddr);
Linus Torvalds's avatar
Linus Torvalds committed
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087

	write_lock_bh(&idev->lock);
	while ((i = idev->mc_list) != NULL) {
		idev->mc_list = i->next;
		write_unlock_bh(&idev->lock);

		igmp6_group_dropped(i);
		ma_put(i);

		write_lock_bh(&idev->lock);
	}
	write_unlock_bh(&idev->lock);
}

#ifdef CONFIG_PROC_FS
static int igmp6_read_proc(char *buffer, char **start, off_t offset,
			   int length, int *eof, void *data)
{
	off_t pos=0, begin=0;
	struct ifmcaddr6 *im;
	int len=0;
	struct net_device *dev;
	
	read_lock(&dev_base_lock);
	for (dev = dev_base; dev; dev = dev->next) {
		struct inet6_dev *idev;

		if ((idev = in6_dev_get(dev)) == NULL)
			continue;

		read_lock_bh(&idev->lock);
		for (im = idev->mc_list; im; im = im->next) {
			int i;

			len += sprintf(buffer+len,"%-4d %-15s ", dev->ifindex, dev->name);

			for (i=0; i<16; i++)
				len += sprintf(buffer+len, "%02x", im->mca_addr.s6_addr[i]);

			len+=sprintf(buffer+len,
				     " %5d %08X %ld\n",
				     im->mca_users,
				     im->mca_flags,
				     (im->mca_flags&MAF_TIMER_RUNNING) ? im->mca_timer.expires-jiffies : 0);

			pos=begin+len;
			if (pos < offset) {
				len=0;
				begin=pos;
			}
			if (pos > offset+length) {
				read_unlock_bh(&idev->lock);
				in6_dev_put(idev);
				goto done;
			}
		}
		read_unlock_bh(&idev->lock);
		in6_dev_put(idev);
	}
	*eof = 1;

David Stevens's avatar
David Stevens committed
2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119
done:
	read_unlock(&dev_base_lock);

	*start=buffer+(offset-begin);
	len-=(offset-begin);
	if(len>length)
		len=length;
	if (len<0)
		len=0;
	return len;
}

static int ip6_mcf_read_proc(char *buffer, char **start, off_t offset,
			   int length, int *eof, void *data)
{
	off_t pos=0, begin=0;
	int len=0;
	int first=1;
	struct net_device *dev;
	
	read_lock(&dev_base_lock);
	for (dev=dev_base; dev; dev=dev->next) {
		struct inet6_dev *idev = in6_dev_get(dev);
		struct ifmcaddr6 *imc;

		if (idev == NULL)
			continue;

		read_lock_bh(&idev->lock);

		for (imc=idev->mc_list; imc; imc=imc->next) {
			struct ip6_sf_list *psf;
2120
			unsigned long i;
David Stevens's avatar
David Stevens committed
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162

			spin_lock_bh(&imc->mca_lock);
			for (psf=imc->mca_sources; psf; psf=psf->sf_next) {
				if (first) {
					len += sprintf(buffer+len, "%3s %6s "
						"%32s %32s %6s %6s\n", "Idx",
						"Device", "Multicast Address",
						"Source Address", "INC", "EXC");
					first = 0;
				}
				len += sprintf(buffer+len,"%3d %6.6s ",
					dev->ifindex, dev->name);

				for (i=0; i<16; i++)
					len += sprintf(buffer+len, "%02x",
						imc->mca_addr.s6_addr[i]);
				buffer[len++] = ' ';
				for (i=0; i<16; i++)
					len += sprintf(buffer+len, "%02x",
						psf->sf_addr.s6_addr[i]);
				len += sprintf(buffer+len, " %6lu %6lu\n",
					psf->sf_count[MCAST_INCLUDE],
					psf->sf_count[MCAST_EXCLUDE]);
				pos = begin+len;
				if (pos < offset) {
					len=0;
					begin=pos;
				}
				if (pos > offset+length) {
					spin_unlock_bh(&imc->mca_lock);
					read_unlock_bh(&idev->lock);
					in6_dev_put(idev);
					goto done;
				}
			}
			spin_unlock_bh(&imc->mca_lock);
		}
		read_unlock_bh(&idev->lock);
		in6_dev_put(idev);
	}
	*eof = 1;

Linus Torvalds's avatar
Linus Torvalds committed
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177
done:
	read_unlock(&dev_base_lock);

	*start=buffer+(offset-begin);
	len-=(offset-begin);
	if(len>length)
		len=length;
	if (len<0)
		len=0;
	return len;
}
#endif

int __init igmp6_init(struct net_proto_family *ops)
{
2178
	struct ipv6_pinfo *np;
Linus Torvalds's avatar
Linus Torvalds committed
2179 2180 2181
	struct sock *sk;
	int err;

2182 2183
	err = sock_create(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6, &igmp6_socket);
	if (err < 0) {
Linus Torvalds's avatar
Linus Torvalds committed
2184 2185 2186 2187 2188 2189 2190 2191
		printk(KERN_ERR
		       "Failed to initialize the IGMP6 control socket (err %d).\n",
		       err);
		igmp6_socket = NULL; /* For safety. */
		return err;
	}

	sk = igmp6_socket->sk;
2192 2193
	sk->sk_allocation = GFP_ATOMIC;
	sk->sk_prot->unhash(sk);
Linus Torvalds's avatar
Linus Torvalds committed
2194

2195 2196
	np = inet6_sk(sk);
	np->hop_limit = 1;
Linus Torvalds's avatar
Linus Torvalds committed
2197 2198
#ifdef CONFIG_PROC_FS
	create_proc_read_entry("net/igmp6", 0, 0, igmp6_read_proc, NULL);
David Stevens's avatar
David Stevens committed
2199
	create_proc_read_entry("net/mcfilter6", 0, 0, ip6_mcf_read_proc, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212
#endif

	return 0;
}

void igmp6_cleanup(void)
{
	sock_release(igmp6_socket);
	igmp6_socket = NULL; /* for safety */
#ifdef CONFIG_PROC_FS
	remove_proc_entry("net/igmp6", 0);
#endif
}