[PATCH] Documentation/networking/dmfe.txt: Make documentation nicer
[linux-2.6/linux-loongson.git] / net / ipv6 / mcast.c
blob562fcd14fdeab0acd75afd019d822ee79968ede1
1 /*
2 * Multicast support for IPv6
3 * Linux INET6 implementation
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * $Id: mcast.c,v 1.40 2002/02/08 03:57:19 davem Exp $
10 * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
18 /* Changes:
20 * yoshfuji : fix format of router-alert option
21 * YOSHIFUJI Hideaki @USAGI:
22 * Fixed source address for MLD message based on
23 * <draft-ietf-magma-mld-source-05.txt>.
24 * YOSHIFUJI Hideaki @USAGI:
25 * - Ignore Queries for invalid addresses.
26 * - MLD for link-local addresses.
27 * David L Stevens <dlstevens@us.ibm.com>:
28 * - MLDv2 support
31 #include <linux/config.h>
32 #include <linux/module.h>
33 #include <linux/errno.h>
34 #include <linux/types.h>
35 #include <linux/string.h>
36 #include <linux/socket.h>
37 #include <linux/sockios.h>
38 #include <linux/jiffies.h>
39 #include <linux/times.h>
40 #include <linux/net.h>
41 #include <linux/in.h>
42 #include <linux/in6.h>
43 #include <linux/netdevice.h>
44 #include <linux/if_arp.h>
45 #include <linux/route.h>
46 #include <linux/init.h>
47 #include <linux/proc_fs.h>
48 #include <linux/seq_file.h>
50 #include <linux/netfilter.h>
51 #include <linux/netfilter_ipv6.h>
53 #include <net/sock.h>
54 #include <net/snmp.h>
56 #include <net/ipv6.h>
57 #include <net/protocol.h>
58 #include <net/if_inet6.h>
59 #include <net/ndisc.h>
60 #include <net/addrconf.h>
61 #include <net/ip6_route.h>
63 #include <net/ip6_checksum.h>
65 /* Set to 3 to get tracing... */
66 #define MCAST_DEBUG 2
68 #if MCAST_DEBUG >= 3
69 #define MDBG(x) printk x
70 #else
71 #define MDBG(x)
72 #endif
75 * These header formats should be in a separate include file, but icmpv6.h
76 * doesn't have in6_addr defined in all cases, there is no __u128, and no
77 * other files reference these.
79 * +-DLS 4/14/03
82 /* Multicast Listener Discovery version 2 headers */
84 struct mld2_grec {
85 __u8 grec_type;
86 __u8 grec_auxwords;
87 __u16 grec_nsrcs;
88 struct in6_addr grec_mca;
89 struct in6_addr grec_src[0];
92 struct mld2_report {
93 __u8 type;
94 __u8 resv1;
95 __u16 csum;
96 __u16 resv2;
97 __u16 ngrec;
98 struct mld2_grec grec[0];
101 struct mld2_query {
102 __u8 type;
103 __u8 code;
104 __u16 csum;
105 __u16 mrc;
106 __u16 resv1;
107 struct in6_addr mca;
108 #if defined(__LITTLE_ENDIAN_BITFIELD)
109 __u8 qrv:3,
110 suppress:1,
111 resv2:4;
112 #elif defined(__BIG_ENDIAN_BITFIELD)
113 __u8 resv2:4,
114 suppress:1,
115 qrv:3;
116 #else
117 #error "Please fix <asm/byteorder.h>"
118 #endif
119 __u8 qqic;
120 __u16 nsrcs;
121 struct in6_addr srcs[0];
124 static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
126 /* Big mc list lock for all the sockets */
127 static DEFINE_RWLOCK(ipv6_sk_mc_lock);
129 static struct socket *igmp6_socket;
131 int __ipv6_dev_mc_dec(struct inet6_dev *idev, struct in6_addr *addr);
133 static void igmp6_join_group(struct ifmcaddr6 *ma);
134 static void igmp6_leave_group(struct ifmcaddr6 *ma);
135 static void igmp6_timer_handler(unsigned long data);
137 static void mld_gq_timer_expire(unsigned long data);
138 static void mld_ifc_timer_expire(unsigned long data);
139 static void mld_ifc_event(struct inet6_dev *idev);
140 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
141 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *addr);
142 static void mld_clear_delrec(struct inet6_dev *idev);
143 static int sf_setstate(struct ifmcaddr6 *pmc);
144 static void sf_markstate(struct ifmcaddr6 *pmc);
145 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
146 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
147 int sfmode, int sfcount, struct in6_addr *psfsrc,
148 int delta);
149 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
150 int sfmode, int sfcount, struct in6_addr *psfsrc,
151 int delta);
152 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
153 struct inet6_dev *idev);
156 #define IGMP6_UNSOLICITED_IVAL (10*HZ)
157 #define MLD_QRV_DEFAULT 2
159 #define MLD_V1_SEEN(idev) (ipv6_devconf.force_mld_version == 1 || \
160 (idev)->cnf.force_mld_version == 1 || \
161 ((idev)->mc_v1_seen && \
162 time_before(jiffies, (idev)->mc_v1_seen)))
164 #define MLDV2_MASK(value, nb) ((nb)>=32 ? (value) : ((1<<(nb))-1) & (value))
165 #define MLDV2_EXP(thresh, nbmant, nbexp, value) \
166 ((value) < (thresh) ? (value) : \
167 ((MLDV2_MASK(value, nbmant) | (1<<(nbmant+nbexp))) << \
168 (MLDV2_MASK((value) >> (nbmant), nbexp) + (nbexp))))
170 #define MLDV2_QQIC(value) MLDV2_EXP(0x80, 4, 3, value)
171 #define MLDV2_MRC(value) MLDV2_EXP(0x8000, 12, 3, value)
173 #define IPV6_MLD_MAX_MSF 10
175 int sysctl_mld_max_msf = IPV6_MLD_MAX_MSF;
178 * socket join on multicast group
181 int ipv6_sock_mc_join(struct sock *sk, int ifindex, struct in6_addr *addr)
183 struct net_device *dev = NULL;
184 struct ipv6_mc_socklist *mc_lst;
185 struct ipv6_pinfo *np = inet6_sk(sk);
186 int err;
188 if (!ipv6_addr_is_multicast(addr))
189 return -EINVAL;
191 read_lock_bh(&ipv6_sk_mc_lock);
192 for (mc_lst=np->ipv6_mc_list; mc_lst; mc_lst=mc_lst->next) {
193 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
194 ipv6_addr_equal(&mc_lst->addr, addr)) {
195 read_unlock_bh(&ipv6_sk_mc_lock);
196 return -EADDRINUSE;
199 read_unlock_bh(&ipv6_sk_mc_lock);
201 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
203 if (mc_lst == NULL)
204 return -ENOMEM;
206 mc_lst->next = NULL;
207 ipv6_addr_copy(&mc_lst->addr, addr);
209 if (ifindex == 0) {
210 struct rt6_info *rt;
211 rt = rt6_lookup(addr, NULL, 0, 0);
212 if (rt) {
213 dev = rt->rt6i_dev;
214 dev_hold(dev);
215 dst_release(&rt->u.dst);
217 } else
218 dev = dev_get_by_index(ifindex);
220 if (dev == NULL) {
221 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
222 return -ENODEV;
225 mc_lst->ifindex = dev->ifindex;
226 mc_lst->sfmode = MCAST_EXCLUDE;
227 mc_lst->sflist = NULL;
230 * now add/increase the group membership on the device
233 err = ipv6_dev_mc_inc(dev, addr);
235 if (err) {
236 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
237 dev_put(dev);
238 return err;
241 write_lock_bh(&ipv6_sk_mc_lock);
242 mc_lst->next = np->ipv6_mc_list;
243 np->ipv6_mc_list = mc_lst;
244 write_unlock_bh(&ipv6_sk_mc_lock);
246 dev_put(dev);
248 return 0;
252 * socket leave on multicast group
254 int ipv6_sock_mc_drop(struct sock *sk, int ifindex, struct in6_addr *addr)
256 struct ipv6_pinfo *np = inet6_sk(sk);
257 struct ipv6_mc_socklist *mc_lst, **lnk;
259 write_lock_bh(&ipv6_sk_mc_lock);
260 for (lnk = &np->ipv6_mc_list; (mc_lst = *lnk) !=NULL ; lnk = &mc_lst->next) {
261 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
262 ipv6_addr_equal(&mc_lst->addr, addr)) {
263 struct net_device *dev;
265 *lnk = mc_lst->next;
266 write_unlock_bh(&ipv6_sk_mc_lock);
268 if ((dev = dev_get_by_index(mc_lst->ifindex)) != NULL) {
269 struct inet6_dev *idev = in6_dev_get(dev);
271 if (idev) {
272 (void) ip6_mc_leave_src(sk,mc_lst,idev);
273 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
274 in6_dev_put(idev);
276 dev_put(dev);
278 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
279 return 0;
282 write_unlock_bh(&ipv6_sk_mc_lock);
284 return -ENOENT;
287 static struct inet6_dev *ip6_mc_find_dev(struct in6_addr *group, int ifindex)
289 struct net_device *dev = NULL;
290 struct inet6_dev *idev = NULL;
292 if (ifindex == 0) {
293 struct rt6_info *rt;
295 rt = rt6_lookup(group, NULL, 0, 0);
296 if (rt) {
297 dev = rt->rt6i_dev;
298 dev_hold(dev);
299 dst_release(&rt->u.dst);
301 } else
302 dev = dev_get_by_index(ifindex);
304 if (!dev)
305 return NULL;
306 idev = in6_dev_get(dev);
307 if (!idev) {
308 dev_put(dev);
309 return NULL;
311 read_lock_bh(&idev->lock);
312 if (idev->dead) {
313 read_unlock_bh(&idev->lock);
314 in6_dev_put(idev);
315 dev_put(dev);
316 return NULL;
318 return idev;
321 void ipv6_sock_mc_close(struct sock *sk)
323 struct ipv6_pinfo *np = inet6_sk(sk);
324 struct ipv6_mc_socklist *mc_lst;
326 write_lock_bh(&ipv6_sk_mc_lock);
327 while ((mc_lst = np->ipv6_mc_list) != NULL) {
328 struct net_device *dev;
330 np->ipv6_mc_list = mc_lst->next;
331 write_unlock_bh(&ipv6_sk_mc_lock);
333 dev = dev_get_by_index(mc_lst->ifindex);
334 if (dev) {
335 struct inet6_dev *idev = in6_dev_get(dev);
337 if (idev) {
338 (void) ip6_mc_leave_src(sk, mc_lst, idev);
339 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
340 in6_dev_put(idev);
342 dev_put(dev);
345 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
347 write_lock_bh(&ipv6_sk_mc_lock);
349 write_unlock_bh(&ipv6_sk_mc_lock);
352 int ip6_mc_source(int add, int omode, struct sock *sk,
353 struct group_source_req *pgsr)
355 struct in6_addr *source, *group;
356 struct ipv6_mc_socklist *pmc;
357 struct net_device *dev;
358 struct inet6_dev *idev;
359 struct ipv6_pinfo *inet6 = inet6_sk(sk);
360 struct ip6_sf_socklist *psl;
361 int i, j, rv;
362 int leavegroup = 0;
363 int err;
365 if (pgsr->gsr_group.ss_family != AF_INET6 ||
366 pgsr->gsr_source.ss_family != AF_INET6)
367 return -EINVAL;
369 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
370 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
372 if (!ipv6_addr_is_multicast(group))
373 return -EINVAL;
375 idev = ip6_mc_find_dev(group, pgsr->gsr_interface);
376 if (!idev)
377 return -ENODEV;
378 dev = idev->dev;
380 err = -EADDRNOTAVAIL;
382 read_lock_bh(&ipv6_sk_mc_lock);
383 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
384 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
385 continue;
386 if (ipv6_addr_equal(&pmc->addr, group))
387 break;
389 if (!pmc) /* must have a prior join */
390 goto done;
391 /* if a source filter was set, must be the same mode as before */
392 if (pmc->sflist) {
393 if (pmc->sfmode != omode)
394 goto done;
395 } else if (pmc->sfmode != omode) {
396 /* allow mode switches for empty-set filters */
397 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
398 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
399 pmc->sfmode = omode;
402 psl = pmc->sflist;
403 if (!add) {
404 if (!psl)
405 goto done;
406 rv = !0;
407 for (i=0; i<psl->sl_count; i++) {
408 rv = memcmp(&psl->sl_addr[i], source,
409 sizeof(struct in6_addr));
410 if (rv == 0)
411 break;
413 if (rv) /* source not found */
414 goto done;
416 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
417 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
418 leavegroup = 1;
419 goto done;
422 /* update the interface filter */
423 ip6_mc_del_src(idev, group, omode, 1, source, 1);
425 for (j=i+1; j<psl->sl_count; j++)
426 psl->sl_addr[j-1] = psl->sl_addr[j];
427 psl->sl_count--;
428 err = 0;
429 goto done;
431 /* else, add a new source to the filter */
433 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
434 err = -ENOBUFS;
435 goto done;
437 if (!psl || psl->sl_count == psl->sl_max) {
438 struct ip6_sf_socklist *newpsl;
439 int count = IP6_SFBLOCK;
441 if (psl)
442 count += psl->sl_max;
443 newpsl = (struct ip6_sf_socklist *)sock_kmalloc(sk,
444 IP6_SFLSIZE(count), GFP_ATOMIC);
445 if (!newpsl) {
446 err = -ENOBUFS;
447 goto done;
449 newpsl->sl_max = count;
450 newpsl->sl_count = count - IP6_SFBLOCK;
451 if (psl) {
452 for (i=0; i<psl->sl_count; i++)
453 newpsl->sl_addr[i] = psl->sl_addr[i];
454 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
456 pmc->sflist = psl = newpsl;
458 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
459 for (i=0; i<psl->sl_count; i++) {
460 rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
461 if (rv == 0)
462 break;
464 if (rv == 0) /* address already there is an error */
465 goto done;
466 for (j=psl->sl_count-1; j>=i; j--)
467 psl->sl_addr[j+1] = psl->sl_addr[j];
468 psl->sl_addr[i] = *source;
469 psl->sl_count++;
470 err = 0;
471 /* update the interface list */
472 ip6_mc_add_src(idev, group, omode, 1, source, 1);
473 done:
474 read_unlock_bh(&ipv6_sk_mc_lock);
475 read_unlock_bh(&idev->lock);
476 in6_dev_put(idev);
477 dev_put(dev);
478 if (leavegroup)
479 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
480 return err;
483 int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
485 struct in6_addr *group;
486 struct ipv6_mc_socklist *pmc;
487 struct net_device *dev;
488 struct inet6_dev *idev;
489 struct ipv6_pinfo *inet6 = inet6_sk(sk);
490 struct ip6_sf_socklist *newpsl, *psl;
491 int i, err;
493 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
495 if (!ipv6_addr_is_multicast(group))
496 return -EINVAL;
497 if (gsf->gf_fmode != MCAST_INCLUDE &&
498 gsf->gf_fmode != MCAST_EXCLUDE)
499 return -EINVAL;
501 idev = ip6_mc_find_dev(group, gsf->gf_interface);
503 if (!idev)
504 return -ENODEV;
505 dev = idev->dev;
506 err = -EADDRNOTAVAIL;
508 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
509 if (pmc->ifindex != gsf->gf_interface)
510 continue;
511 if (ipv6_addr_equal(&pmc->addr, group))
512 break;
514 if (!pmc) /* must have a prior join */
515 goto done;
516 if (gsf->gf_numsrc) {
517 newpsl = (struct ip6_sf_socklist *)sock_kmalloc(sk,
518 IP6_SFLSIZE(gsf->gf_numsrc), GFP_ATOMIC);
519 if (!newpsl) {
520 err = -ENOBUFS;
521 goto done;
523 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
524 for (i=0; i<newpsl->sl_count; ++i) {
525 struct sockaddr_in6 *psin6;
527 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
528 newpsl->sl_addr[i] = psin6->sin6_addr;
530 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
531 newpsl->sl_count, newpsl->sl_addr, 0);
532 if (err) {
533 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
534 goto done;
536 } else
537 newpsl = NULL;
538 psl = pmc->sflist;
539 if (psl) {
540 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
541 psl->sl_count, psl->sl_addr, 0);
542 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
543 } else
544 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
545 pmc->sflist = newpsl;
546 pmc->sfmode = gsf->gf_fmode;
547 done:
548 read_unlock_bh(&idev->lock);
549 in6_dev_put(idev);
550 dev_put(dev);
551 return err;
554 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
555 struct group_filter __user *optval, int __user *optlen)
557 int err, i, count, copycount;
558 struct in6_addr *group;
559 struct ipv6_mc_socklist *pmc;
560 struct inet6_dev *idev;
561 struct net_device *dev;
562 struct ipv6_pinfo *inet6 = inet6_sk(sk);
563 struct ip6_sf_socklist *psl;
565 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
567 if (!ipv6_addr_is_multicast(group))
568 return -EINVAL;
570 idev = ip6_mc_find_dev(group, gsf->gf_interface);
572 if (!idev)
573 return -ENODEV;
575 dev = idev->dev;
577 err = -EADDRNOTAVAIL;
579 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
580 if (pmc->ifindex != gsf->gf_interface)
581 continue;
582 if (ipv6_addr_equal(group, &pmc->addr))
583 break;
585 if (!pmc) /* must have a prior join */
586 goto done;
587 gsf->gf_fmode = pmc->sfmode;
588 psl = pmc->sflist;
589 count = psl ? psl->sl_count : 0;
590 read_unlock_bh(&idev->lock);
591 in6_dev_put(idev);
592 dev_put(dev);
594 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
595 gsf->gf_numsrc = count;
596 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
597 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
598 return -EFAULT;
600 for (i=0; i<copycount; i++) {
601 struct sockaddr_in6 *psin6;
602 struct sockaddr_storage ss;
604 psin6 = (struct sockaddr_in6 *)&ss;
605 memset(&ss, 0, sizeof(ss));
606 psin6->sin6_family = AF_INET6;
607 psin6->sin6_addr = psl->sl_addr[i];
608 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
609 return -EFAULT;
611 return 0;
612 done:
613 read_unlock_bh(&idev->lock);
614 in6_dev_put(idev);
615 dev_put(dev);
616 return err;
619 int inet6_mc_check(struct sock *sk, struct in6_addr *mc_addr,
620 struct in6_addr *src_addr)
622 struct ipv6_pinfo *np = inet6_sk(sk);
623 struct ipv6_mc_socklist *mc;
624 struct ip6_sf_socklist *psl;
625 int rv = 1;
627 read_lock(&ipv6_sk_mc_lock);
628 for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
629 if (ipv6_addr_equal(&mc->addr, mc_addr))
630 break;
632 if (!mc) {
633 read_unlock(&ipv6_sk_mc_lock);
634 return 1;
636 psl = mc->sflist;
637 if (!psl) {
638 rv = mc->sfmode == MCAST_EXCLUDE;
639 } else {
640 int i;
642 for (i=0; i<psl->sl_count; i++) {
643 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
644 break;
646 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
647 rv = 0;
648 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
649 rv = 0;
651 read_unlock(&ipv6_sk_mc_lock);
653 return rv;
656 static void ma_put(struct ifmcaddr6 *mc)
658 if (atomic_dec_and_test(&mc->mca_refcnt)) {
659 in6_dev_put(mc->idev);
660 kfree(mc);
664 static void igmp6_group_added(struct ifmcaddr6 *mc)
666 struct net_device *dev = mc->idev->dev;
667 char buf[MAX_ADDR_LEN];
669 spin_lock_bh(&mc->mca_lock);
670 if (!(mc->mca_flags&MAF_LOADED)) {
671 mc->mca_flags |= MAF_LOADED;
672 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
673 dev_mc_add(dev, buf, dev->addr_len, 0);
675 spin_unlock_bh(&mc->mca_lock);
677 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
678 return;
680 if (MLD_V1_SEEN(mc->idev)) {
681 igmp6_join_group(mc);
682 return;
684 /* else v2 */
686 mc->mca_crcount = mc->idev->mc_qrv;
687 mld_ifc_event(mc->idev);
690 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
692 struct net_device *dev = mc->idev->dev;
693 char buf[MAX_ADDR_LEN];
695 spin_lock_bh(&mc->mca_lock);
696 if (mc->mca_flags&MAF_LOADED) {
697 mc->mca_flags &= ~MAF_LOADED;
698 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
699 dev_mc_delete(dev, buf, dev->addr_len, 0);
702 if (mc->mca_flags & MAF_NOREPORT)
703 goto done;
704 spin_unlock_bh(&mc->mca_lock);
706 if (!mc->idev->dead)
707 igmp6_leave_group(mc);
709 spin_lock_bh(&mc->mca_lock);
710 if (del_timer(&mc->mca_timer))
711 atomic_dec(&mc->mca_refcnt);
712 done:
713 ip6_mc_clear_src(mc);
714 spin_unlock_bh(&mc->mca_lock);
718 * deleted ifmcaddr6 manipulation
720 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
722 struct ifmcaddr6 *pmc;
724 /* this is an "ifmcaddr6" for convenience; only the fields below
725 * are actually used. In particular, the refcnt and users are not
726 * used for management of the delete list. Using the same structure
727 * for deleted items allows change reports to use common code with
728 * non-deleted or query-response MCA's.
730 pmc = (struct ifmcaddr6 *)kmalloc(sizeof(*pmc), GFP_ATOMIC);
731 if (!pmc)
732 return;
733 memset(pmc, 0, sizeof(*pmc));
734 spin_lock_bh(&im->mca_lock);
735 spin_lock_init(&pmc->mca_lock);
736 pmc->idev = im->idev;
737 in6_dev_hold(idev);
738 pmc->mca_addr = im->mca_addr;
739 pmc->mca_crcount = idev->mc_qrv;
740 pmc->mca_sfmode = im->mca_sfmode;
741 if (pmc->mca_sfmode == MCAST_INCLUDE) {
742 struct ip6_sf_list *psf;
744 pmc->mca_tomb = im->mca_tomb;
745 pmc->mca_sources = im->mca_sources;
746 im->mca_tomb = im->mca_sources = NULL;
747 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
748 psf->sf_crcount = pmc->mca_crcount;
750 spin_unlock_bh(&im->mca_lock);
752 write_lock_bh(&idev->mc_lock);
753 pmc->next = idev->mc_tomb;
754 idev->mc_tomb = pmc;
755 write_unlock_bh(&idev->mc_lock);
758 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
760 struct ifmcaddr6 *pmc, *pmc_prev;
761 struct ip6_sf_list *psf, *psf_next;
763 write_lock_bh(&idev->mc_lock);
764 pmc_prev = NULL;
765 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
766 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
767 break;
768 pmc_prev = pmc;
770 if (pmc) {
771 if (pmc_prev)
772 pmc_prev->next = pmc->next;
773 else
774 idev->mc_tomb = pmc->next;
776 write_unlock_bh(&idev->mc_lock);
777 if (pmc) {
778 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
779 psf_next = psf->sf_next;
780 kfree(psf);
782 in6_dev_put(pmc->idev);
783 kfree(pmc);
787 static void mld_clear_delrec(struct inet6_dev *idev)
789 struct ifmcaddr6 *pmc, *nextpmc;
791 write_lock_bh(&idev->mc_lock);
792 pmc = idev->mc_tomb;
793 idev->mc_tomb = NULL;
794 write_unlock_bh(&idev->mc_lock);
796 for (; pmc; pmc = nextpmc) {
797 nextpmc = pmc->next;
798 ip6_mc_clear_src(pmc);
799 in6_dev_put(pmc->idev);
800 kfree(pmc);
803 /* clear dead sources, too */
804 read_lock_bh(&idev->lock);
805 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
806 struct ip6_sf_list *psf, *psf_next;
808 spin_lock_bh(&pmc->mca_lock);
809 psf = pmc->mca_tomb;
810 pmc->mca_tomb = NULL;
811 spin_unlock_bh(&pmc->mca_lock);
812 for (; psf; psf=psf_next) {
813 psf_next = psf->sf_next;
814 kfree(psf);
817 read_unlock_bh(&idev->lock);
822 * device multicast group inc (add if not found)
824 int ipv6_dev_mc_inc(struct net_device *dev, struct in6_addr *addr)
826 struct ifmcaddr6 *mc;
827 struct inet6_dev *idev;
829 idev = in6_dev_get(dev);
831 if (idev == NULL)
832 return -EINVAL;
834 write_lock_bh(&idev->lock);
835 if (idev->dead) {
836 write_unlock_bh(&idev->lock);
837 in6_dev_put(idev);
838 return -ENODEV;
841 for (mc = idev->mc_list; mc; mc = mc->next) {
842 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
843 mc->mca_users++;
844 write_unlock_bh(&idev->lock);
845 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
846 NULL, 0);
847 in6_dev_put(idev);
848 return 0;
853 * not found: create a new one.
856 mc = kmalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
858 if (mc == NULL) {
859 write_unlock_bh(&idev->lock);
860 in6_dev_put(idev);
861 return -ENOMEM;
864 memset(mc, 0, sizeof(struct ifmcaddr6));
865 init_timer(&mc->mca_timer);
866 mc->mca_timer.function = igmp6_timer_handler;
867 mc->mca_timer.data = (unsigned long) mc;
869 ipv6_addr_copy(&mc->mca_addr, addr);
870 mc->idev = idev;
871 mc->mca_users = 1;
872 /* mca_stamp should be updated upon changes */
873 mc->mca_cstamp = mc->mca_tstamp = jiffies;
874 atomic_set(&mc->mca_refcnt, 2);
875 spin_lock_init(&mc->mca_lock);
877 /* initial mode is (EX, empty) */
878 mc->mca_sfmode = MCAST_EXCLUDE;
879 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
881 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
882 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
883 mc->mca_flags |= MAF_NOREPORT;
885 mc->next = idev->mc_list;
886 idev->mc_list = mc;
887 write_unlock_bh(&idev->lock);
889 mld_del_delrec(idev, &mc->mca_addr);
890 igmp6_group_added(mc);
891 ma_put(mc);
892 return 0;
896 * device multicast group del
898 int __ipv6_dev_mc_dec(struct inet6_dev *idev, struct in6_addr *addr)
900 struct ifmcaddr6 *ma, **map;
902 write_lock_bh(&idev->lock);
903 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
904 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
905 if (--ma->mca_users == 0) {
906 *map = ma->next;
907 write_unlock_bh(&idev->lock);
909 igmp6_group_dropped(ma);
911 ma_put(ma);
912 return 0;
914 write_unlock_bh(&idev->lock);
915 return 0;
918 write_unlock_bh(&idev->lock);
920 return -ENOENT;
923 int ipv6_dev_mc_dec(struct net_device *dev, struct in6_addr *addr)
925 struct inet6_dev *idev = in6_dev_get(dev);
926 int err;
928 if (!idev)
929 return -ENODEV;
931 err = __ipv6_dev_mc_dec(idev, addr);
933 in6_dev_put(idev);
935 return err;
939 * identify MLD packets for MLD filter exceptions
941 int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
943 struct icmp6hdr *pic;
945 if (nexthdr != IPPROTO_ICMPV6)
946 return 0;
948 if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
949 return 0;
951 pic = (struct icmp6hdr *)skb->h.raw;
953 switch (pic->icmp6_type) {
954 case ICMPV6_MGM_QUERY:
955 case ICMPV6_MGM_REPORT:
956 case ICMPV6_MGM_REDUCTION:
957 case ICMPV6_MLD2_REPORT:
958 return 1;
959 default:
960 break;
962 return 0;
966 * check if the interface/address pair is valid
968 int ipv6_chk_mcast_addr(struct net_device *dev, struct in6_addr *group,
969 struct in6_addr *src_addr)
971 struct inet6_dev *idev;
972 struct ifmcaddr6 *mc;
973 int rv = 0;
975 idev = in6_dev_get(dev);
976 if (idev) {
977 read_lock_bh(&idev->lock);
978 for (mc = idev->mc_list; mc; mc=mc->next) {
979 if (ipv6_addr_equal(&mc->mca_addr, group))
980 break;
982 if (mc) {
983 if (src_addr && !ipv6_addr_any(src_addr)) {
984 struct ip6_sf_list *psf;
986 spin_lock_bh(&mc->mca_lock);
987 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
988 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
989 break;
991 if (psf)
992 rv = psf->sf_count[MCAST_INCLUDE] ||
993 psf->sf_count[MCAST_EXCLUDE] !=
994 mc->mca_sfcount[MCAST_EXCLUDE];
995 else
996 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
997 spin_unlock_bh(&mc->mca_lock);
998 } else
999 rv = 1; /* don't filter unspecified source */
1001 read_unlock_bh(&idev->lock);
1002 in6_dev_put(idev);
1004 return rv;
1007 static void mld_gq_start_timer(struct inet6_dev *idev)
1009 int tv = net_random() % idev->mc_maxdelay;
1011 idev->mc_gq_running = 1;
1012 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1013 in6_dev_hold(idev);
1016 static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
1018 int tv = net_random() % delay;
1020 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1021 in6_dev_hold(idev);
1025 * IGMP handling (alias multicast ICMPv6 messages)
1028 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1030 unsigned long delay = resptime;
1032 /* Do not start timer for these addresses */
1033 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1034 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1035 return;
1037 if (del_timer(&ma->mca_timer)) {
1038 atomic_dec(&ma->mca_refcnt);
1039 delay = ma->mca_timer.expires - jiffies;
1042 if (delay >= resptime) {
1043 if (resptime)
1044 delay = net_random() % resptime;
1045 else
1046 delay = 1;
1048 ma->mca_timer.expires = jiffies + delay;
1049 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1050 atomic_inc(&ma->mca_refcnt);
1051 ma->mca_flags |= MAF_TIMER_RUNNING;
1054 static void mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1055 struct in6_addr *srcs)
1057 struct ip6_sf_list *psf;
1058 int i, scount;
1060 scount = 0;
1061 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1062 if (scount == nsrcs)
1063 break;
1064 for (i=0; i<nsrcs; i++)
1065 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1066 psf->sf_gsresp = 1;
1067 scount++;
1068 break;
1073 int igmp6_event_query(struct sk_buff *skb)
1075 struct mld2_query *mlh2 = (struct mld2_query *) skb->h.raw;
1076 struct ifmcaddr6 *ma;
1077 struct in6_addr *group;
1078 unsigned long max_delay;
1079 struct inet6_dev *idev;
1080 struct icmp6hdr *hdr;
1081 int group_type;
1082 int mark = 0;
1083 int len;
1085 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1086 return -EINVAL;
1088 /* compute payload length excluding extension headers */
1089 len = ntohs(skb->nh.ipv6h->payload_len) + sizeof(struct ipv6hdr);
1090 len -= (char *)skb->h.raw - (char *)skb->nh.ipv6h;
1092 /* Drop queries with not link local source */
1093 if (!(ipv6_addr_type(&skb->nh.ipv6h->saddr)&IPV6_ADDR_LINKLOCAL))
1094 return -EINVAL;
1096 idev = in6_dev_get(skb->dev);
1098 if (idev == NULL)
1099 return 0;
1101 hdr = (struct icmp6hdr *) skb->h.raw;
1102 group = (struct in6_addr *) (hdr + 1);
1103 group_type = ipv6_addr_type(group);
1105 if (group_type != IPV6_ADDR_ANY &&
1106 !(group_type&IPV6_ADDR_MULTICAST)) {
1107 in6_dev_put(idev);
1108 return -EINVAL;
1111 if (len == 24) {
1112 int switchback;
1113 /* MLDv1 router present */
1115 /* Translate milliseconds to jiffies */
1116 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
1118 switchback = (idev->mc_qrv + 1) * max_delay;
1119 idev->mc_v1_seen = jiffies + switchback;
1121 /* cancel the interface change timer */
1122 idev->mc_ifc_count = 0;
1123 if (del_timer(&idev->mc_ifc_timer))
1124 __in6_dev_put(idev);
1125 /* clear deleted report items */
1126 mld_clear_delrec(idev);
1127 } else if (len >= 28) {
1128 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
1129 if (!max_delay)
1130 max_delay = 1;
1131 idev->mc_maxdelay = max_delay;
1132 if (mlh2->qrv)
1133 idev->mc_qrv = mlh2->qrv;
1134 if (group_type == IPV6_ADDR_ANY) { /* general query */
1135 if (mlh2->nsrcs) {
1136 in6_dev_put(idev);
1137 return -EINVAL; /* no sources allowed */
1139 mld_gq_start_timer(idev);
1140 in6_dev_put(idev);
1141 return 0;
1143 /* mark sources to include, if group & source-specific */
1144 mark = mlh2->nsrcs != 0;
1145 } else {
1146 in6_dev_put(idev);
1147 return -EINVAL;
1150 read_lock_bh(&idev->lock);
1151 if (group_type == IPV6_ADDR_ANY) {
1152 for (ma = idev->mc_list; ma; ma=ma->next) {
1153 spin_lock_bh(&ma->mca_lock);
1154 igmp6_group_queried(ma, max_delay);
1155 spin_unlock_bh(&ma->mca_lock);
1157 } else {
1158 for (ma = idev->mc_list; ma; ma=ma->next) {
1159 if (group_type != IPV6_ADDR_ANY &&
1160 !ipv6_addr_equal(group, &ma->mca_addr))
1161 continue;
1162 spin_lock_bh(&ma->mca_lock);
1163 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1164 /* gsquery <- gsquery && mark */
1165 if (!mark)
1166 ma->mca_flags &= ~MAF_GSQUERY;
1167 } else {
1168 /* gsquery <- mark */
1169 if (mark)
1170 ma->mca_flags |= MAF_GSQUERY;
1171 else
1172 ma->mca_flags &= ~MAF_GSQUERY;
1174 if (ma->mca_flags & MAF_GSQUERY)
1175 mld_marksources(ma, ntohs(mlh2->nsrcs),
1176 mlh2->srcs);
1177 igmp6_group_queried(ma, max_delay);
1178 spin_unlock_bh(&ma->mca_lock);
1179 if (group_type != IPV6_ADDR_ANY)
1180 break;
1183 read_unlock_bh(&idev->lock);
1184 in6_dev_put(idev);
1186 return 0;
1190 int igmp6_event_report(struct sk_buff *skb)
1192 struct ifmcaddr6 *ma;
1193 struct in6_addr *addrp;
1194 struct inet6_dev *idev;
1195 struct icmp6hdr *hdr;
1196 int addr_type;
1198 /* Our own report looped back. Ignore it. */
1199 if (skb->pkt_type == PACKET_LOOPBACK)
1200 return 0;
1202 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1203 return -EINVAL;
1205 hdr = (struct icmp6hdr*) skb->h.raw;
1207 /* Drop reports with not link local source */
1208 addr_type = ipv6_addr_type(&skb->nh.ipv6h->saddr);
1209 if (addr_type != IPV6_ADDR_ANY &&
1210 !(addr_type&IPV6_ADDR_LINKLOCAL))
1211 return -EINVAL;
1213 addrp = (struct in6_addr *) (hdr + 1);
1215 idev = in6_dev_get(skb->dev);
1216 if (idev == NULL)
1217 return -ENODEV;
1220 * Cancel the timer for this group
1223 read_lock_bh(&idev->lock);
1224 for (ma = idev->mc_list; ma; ma=ma->next) {
1225 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1226 spin_lock(&ma->mca_lock);
1227 if (del_timer(&ma->mca_timer))
1228 atomic_dec(&ma->mca_refcnt);
1229 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1230 spin_unlock(&ma->mca_lock);
1231 break;
1234 read_unlock_bh(&idev->lock);
1235 in6_dev_put(idev);
1236 return 0;
1239 static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1240 int gdeleted, int sdeleted)
1242 switch (type) {
1243 case MLD2_MODE_IS_INCLUDE:
1244 case MLD2_MODE_IS_EXCLUDE:
1245 if (gdeleted || sdeleted)
1246 return 0;
1247 return !((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp);
1248 case MLD2_CHANGE_TO_INCLUDE:
1249 if (gdeleted || sdeleted)
1250 return 0;
1251 return psf->sf_count[MCAST_INCLUDE] != 0;
1252 case MLD2_CHANGE_TO_EXCLUDE:
1253 if (gdeleted || sdeleted)
1254 return 0;
1255 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1256 psf->sf_count[MCAST_INCLUDE])
1257 return 0;
1258 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1259 psf->sf_count[MCAST_EXCLUDE];
1260 case MLD2_ALLOW_NEW_SOURCES:
1261 if (gdeleted || !psf->sf_crcount)
1262 return 0;
1263 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1264 case MLD2_BLOCK_OLD_SOURCES:
1265 if (pmc->mca_sfmode == MCAST_INCLUDE)
1266 return gdeleted || (psf->sf_crcount && sdeleted);
1267 return psf->sf_crcount && !gdeleted && !sdeleted;
1269 return 0;
1272 static int
1273 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1275 struct ip6_sf_list *psf;
1276 int scount = 0;
1278 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1279 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1280 continue;
1281 scount++;
1283 return scount;
1286 static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1288 struct sock *sk = igmp6_socket->sk;
1289 struct sk_buff *skb;
1290 struct mld2_report *pmr;
1291 struct in6_addr addr_buf;
1292 int err;
1293 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1294 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1295 IPV6_TLV_PADN, 0 };
1297 /* we assume size > sizeof(ra) here */
1298 skb = sock_alloc_send_skb(sk, size + LL_RESERVED_SPACE(dev), 1, &err);
1300 if (skb == 0)
1301 return NULL;
1303 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1305 if (ipv6_get_lladdr(dev, &addr_buf)) {
1306 /* <draft-ietf-magma-mld-source-05.txt>:
1307 * use unspecified address as the source address
1308 * when a valid link-local address is not available.
1310 memset(&addr_buf, 0, sizeof(addr_buf));
1313 ip6_nd_hdr(sk, skb, dev, &addr_buf, &mld2_all_mcr, NEXTHDR_HOP, 0);
1315 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1317 pmr =(struct mld2_report *)skb_put(skb, sizeof(*pmr));
1318 skb->h.raw = (unsigned char *)pmr;
1319 pmr->type = ICMPV6_MLD2_REPORT;
1320 pmr->resv1 = 0;
1321 pmr->csum = 0;
1322 pmr->resv2 = 0;
1323 pmr->ngrec = 0;
1324 return skb;
1327 static inline int mld_dev_queue_xmit2(struct sk_buff *skb)
1329 struct net_device *dev = skb->dev;
1331 if (dev->hard_header) {
1332 unsigned char ha[MAX_ADDR_LEN];
1333 int err;
1335 ndisc_mc_map(&skb->nh.ipv6h->daddr, ha, dev, 1);
1336 err = dev->hard_header(skb, dev, ETH_P_IPV6, ha, NULL, skb->len);
1337 if (err < 0) {
1338 kfree_skb(skb);
1339 return err;
1342 return dev_queue_xmit(skb);
1345 static inline int mld_dev_queue_xmit(struct sk_buff *skb)
1347 return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dev,
1348 mld_dev_queue_xmit2);
1351 static void mld_sendpack(struct sk_buff *skb)
1353 struct ipv6hdr *pip6 = skb->nh.ipv6h;
1354 struct mld2_report *pmr = (struct mld2_report *)skb->h.raw;
1355 int payload_len, mldlen;
1356 struct inet6_dev *idev = in6_dev_get(skb->dev);
1357 int err;
1359 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1360 payload_len = skb->tail - (unsigned char *)skb->nh.ipv6h -
1361 sizeof(struct ipv6hdr);
1362 mldlen = skb->tail - skb->h.raw;
1363 pip6->payload_len = htons(payload_len);
1365 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1366 IPPROTO_ICMPV6, csum_partial(skb->h.raw, mldlen, 0));
1367 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1368 mld_dev_queue_xmit);
1369 if (!err) {
1370 ICMP6_INC_STATS(idev,ICMP6_MIB_OUTMSGS);
1371 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1372 } else
1373 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1375 if (likely(idev != NULL))
1376 in6_dev_put(idev);
1379 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1381 return sizeof(struct mld2_grec) + 4*mld_scount(pmc,type,gdel,sdel);
1384 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1385 int type, struct mld2_grec **ppgr)
1387 struct net_device *dev = pmc->idev->dev;
1388 struct mld2_report *pmr;
1389 struct mld2_grec *pgr;
1391 if (!skb)
1392 skb = mld_newpack(dev, dev->mtu);
1393 if (!skb)
1394 return NULL;
1395 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1396 pgr->grec_type = type;
1397 pgr->grec_auxwords = 0;
1398 pgr->grec_nsrcs = 0;
1399 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1400 pmr = (struct mld2_report *)skb->h.raw;
1401 pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1402 *ppgr = pgr;
1403 return skb;
1406 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1407 skb_tailroom(skb)) : 0)
1409 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1410 int type, int gdeleted, int sdeleted)
1412 struct net_device *dev = pmc->idev->dev;
1413 struct mld2_report *pmr;
1414 struct mld2_grec *pgr = NULL;
1415 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1416 int scount, first, isquery, truncate;
1418 if (pmc->mca_flags & MAF_NOREPORT)
1419 return skb;
1421 isquery = type == MLD2_MODE_IS_INCLUDE ||
1422 type == MLD2_MODE_IS_EXCLUDE;
1423 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1424 type == MLD2_CHANGE_TO_EXCLUDE;
1426 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1428 if (!*psf_list) {
1429 if (type == MLD2_ALLOW_NEW_SOURCES ||
1430 type == MLD2_BLOCK_OLD_SOURCES)
1431 return skb;
1432 if (pmc->mca_crcount || isquery) {
1433 /* make sure we have room for group header and at
1434 * least one source.
1436 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)+
1437 sizeof(struct in6_addr)) {
1438 mld_sendpack(skb);
1439 skb = NULL; /* add_grhead will get a new one */
1441 skb = add_grhead(skb, pmc, type, &pgr);
1443 return skb;
1445 pmr = skb ? (struct mld2_report *)skb->h.raw : NULL;
1447 /* EX and TO_EX get a fresh packet, if needed */
1448 if (truncate) {
1449 if (pmr && pmr->ngrec &&
1450 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1451 if (skb)
1452 mld_sendpack(skb);
1453 skb = mld_newpack(dev, dev->mtu);
1456 first = 1;
1457 scount = 0;
1458 psf_prev = NULL;
1459 for (psf=*psf_list; psf; psf=psf_next) {
1460 struct in6_addr *psrc;
1462 psf_next = psf->sf_next;
1464 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1465 psf_prev = psf;
1466 continue;
1469 /* clear marks on query responses */
1470 if (isquery)
1471 psf->sf_gsresp = 0;
1473 if (AVAILABLE(skb) < sizeof(*psrc) +
1474 first*sizeof(struct mld2_grec)) {
1475 if (truncate && !first)
1476 break; /* truncate these */
1477 if (pgr)
1478 pgr->grec_nsrcs = htons(scount);
1479 if (skb)
1480 mld_sendpack(skb);
1481 skb = mld_newpack(dev, dev->mtu);
1482 first = 1;
1483 scount = 0;
1485 if (first) {
1486 skb = add_grhead(skb, pmc, type, &pgr);
1487 first = 0;
1489 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1490 *psrc = psf->sf_addr;
1491 scount++;
1492 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1493 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1494 psf->sf_crcount--;
1495 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1496 if (psf_prev)
1497 psf_prev->sf_next = psf->sf_next;
1498 else
1499 *psf_list = psf->sf_next;
1500 kfree(psf);
1501 continue;
1504 psf_prev = psf;
1506 if (pgr)
1507 pgr->grec_nsrcs = htons(scount);
1509 if (isquery)
1510 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1511 return skb;
1514 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1516 struct sk_buff *skb = NULL;
1517 int type;
1519 if (!pmc) {
1520 read_lock_bh(&idev->lock);
1521 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1522 if (pmc->mca_flags & MAF_NOREPORT)
1523 continue;
1524 spin_lock_bh(&pmc->mca_lock);
1525 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1526 type = MLD2_MODE_IS_EXCLUDE;
1527 else
1528 type = MLD2_MODE_IS_INCLUDE;
1529 skb = add_grec(skb, pmc, type, 0, 0);
1530 spin_unlock_bh(&pmc->mca_lock);
1532 read_unlock_bh(&idev->lock);
1533 } else {
1534 spin_lock_bh(&pmc->mca_lock);
1535 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1536 type = MLD2_MODE_IS_EXCLUDE;
1537 else
1538 type = MLD2_MODE_IS_INCLUDE;
1539 skb = add_grec(skb, pmc, type, 0, 0);
1540 spin_unlock_bh(&pmc->mca_lock);
1542 if (skb)
1543 mld_sendpack(skb);
1547 * remove zero-count source records from a source filter list
1549 static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1551 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1553 psf_prev = NULL;
1554 for (psf=*ppsf; psf; psf = psf_next) {
1555 psf_next = psf->sf_next;
1556 if (psf->sf_crcount == 0) {
1557 if (psf_prev)
1558 psf_prev->sf_next = psf->sf_next;
1559 else
1560 *ppsf = psf->sf_next;
1561 kfree(psf);
1562 } else
1563 psf_prev = psf;
1567 static void mld_send_cr(struct inet6_dev *idev)
1569 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1570 struct sk_buff *skb = NULL;
1571 int type, dtype;
1573 read_lock_bh(&idev->lock);
1574 write_lock_bh(&idev->mc_lock);
1576 /* deleted MCA's */
1577 pmc_prev = NULL;
1578 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1579 pmc_next = pmc->next;
1580 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1581 type = MLD2_BLOCK_OLD_SOURCES;
1582 dtype = MLD2_BLOCK_OLD_SOURCES;
1583 skb = add_grec(skb, pmc, type, 1, 0);
1584 skb = add_grec(skb, pmc, dtype, 1, 1);
1586 if (pmc->mca_crcount) {
1587 pmc->mca_crcount--;
1588 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1589 type = MLD2_CHANGE_TO_INCLUDE;
1590 skb = add_grec(skb, pmc, type, 1, 0);
1592 if (pmc->mca_crcount == 0) {
1593 mld_clear_zeros(&pmc->mca_tomb);
1594 mld_clear_zeros(&pmc->mca_sources);
1597 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1598 !pmc->mca_sources) {
1599 if (pmc_prev)
1600 pmc_prev->next = pmc_next;
1601 else
1602 idev->mc_tomb = pmc_next;
1603 in6_dev_put(pmc->idev);
1604 kfree(pmc);
1605 } else
1606 pmc_prev = pmc;
1608 write_unlock_bh(&idev->mc_lock);
1610 /* change recs */
1611 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1612 spin_lock_bh(&pmc->mca_lock);
1613 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1614 type = MLD2_BLOCK_OLD_SOURCES;
1615 dtype = MLD2_ALLOW_NEW_SOURCES;
1616 } else {
1617 type = MLD2_ALLOW_NEW_SOURCES;
1618 dtype = MLD2_BLOCK_OLD_SOURCES;
1620 skb = add_grec(skb, pmc, type, 0, 0);
1621 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1623 /* filter mode changes */
1624 if (pmc->mca_crcount) {
1625 pmc->mca_crcount--;
1626 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1627 type = MLD2_CHANGE_TO_EXCLUDE;
1628 else
1629 type = MLD2_CHANGE_TO_INCLUDE;
1630 skb = add_grec(skb, pmc, type, 0, 0);
1632 spin_unlock_bh(&pmc->mca_lock);
1634 read_unlock_bh(&idev->lock);
1635 if (!skb)
1636 return;
1637 (void) mld_sendpack(skb);
1640 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1642 struct sock *sk = igmp6_socket->sk;
1643 struct inet6_dev *idev;
1644 struct sk_buff *skb;
1645 struct icmp6hdr *hdr;
1646 struct in6_addr *snd_addr;
1647 struct in6_addr *addrp;
1648 struct in6_addr addr_buf;
1649 struct in6_addr all_routers;
1650 int err, len, payload_len, full_len;
1651 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1652 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1653 IPV6_TLV_PADN, 0 };
1655 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1656 snd_addr = addr;
1657 if (type == ICMPV6_MGM_REDUCTION) {
1658 snd_addr = &all_routers;
1659 ipv6_addr_all_routers(&all_routers);
1662 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1663 payload_len = len + sizeof(ra);
1664 full_len = sizeof(struct ipv6hdr) + payload_len;
1666 skb = sock_alloc_send_skb(sk, LL_RESERVED_SPACE(dev) + full_len, 1, &err);
1668 if (skb == NULL) {
1669 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1670 return;
1673 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1675 if (ipv6_get_lladdr(dev, &addr_buf)) {
1676 /* <draft-ietf-magma-mld-source-05.txt>:
1677 * use unspecified address as the source address
1678 * when a valid link-local address is not available.
1680 memset(&addr_buf, 0, sizeof(addr_buf));
1683 ip6_nd_hdr(sk, skb, dev, &addr_buf, snd_addr, NEXTHDR_HOP, payload_len);
1685 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1687 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1688 memset(hdr, 0, sizeof(struct icmp6hdr));
1689 hdr->icmp6_type = type;
1691 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1692 ipv6_addr_copy(addrp, addr);
1694 hdr->icmp6_cksum = csum_ipv6_magic(&addr_buf, snd_addr, len,
1695 IPPROTO_ICMPV6,
1696 csum_partial((__u8 *) hdr, len, 0));
1698 idev = in6_dev_get(skb->dev);
1700 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1701 mld_dev_queue_xmit);
1702 if (!err) {
1703 if (type == ICMPV6_MGM_REDUCTION)
1704 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBREDUCTIONS);
1705 else
1706 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBRESPONSES);
1707 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
1708 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1709 } else
1710 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1712 if (likely(idev != NULL))
1713 in6_dev_put(idev);
1714 return;
1717 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1718 struct in6_addr *psfsrc)
1720 struct ip6_sf_list *psf, *psf_prev;
1721 int rv = 0;
1723 psf_prev = NULL;
1724 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1725 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1726 break;
1727 psf_prev = psf;
1729 if (!psf || psf->sf_count[sfmode] == 0) {
1730 /* source filter not found, or count wrong => bug */
1731 return -ESRCH;
1733 psf->sf_count[sfmode]--;
1734 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1735 struct inet6_dev *idev = pmc->idev;
1737 /* no more filters for this source */
1738 if (psf_prev)
1739 psf_prev->sf_next = psf->sf_next;
1740 else
1741 pmc->mca_sources = psf->sf_next;
1742 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1743 !MLD_V1_SEEN(idev)) {
1744 psf->sf_crcount = idev->mc_qrv;
1745 psf->sf_next = pmc->mca_tomb;
1746 pmc->mca_tomb = psf;
1747 rv = 1;
1748 } else
1749 kfree(psf);
1751 return rv;
1754 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1755 int sfmode, int sfcount, struct in6_addr *psfsrc,
1756 int delta)
1758 struct ifmcaddr6 *pmc;
1759 int changerec = 0;
1760 int i, err;
1762 if (!idev)
1763 return -ENODEV;
1764 read_lock_bh(&idev->lock);
1765 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1766 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1767 break;
1769 if (!pmc) {
1770 /* MCA not found?? bug */
1771 read_unlock_bh(&idev->lock);
1772 return -ESRCH;
1774 spin_lock_bh(&pmc->mca_lock);
1775 sf_markstate(pmc);
1776 if (!delta) {
1777 if (!pmc->mca_sfcount[sfmode]) {
1778 spin_unlock_bh(&pmc->mca_lock);
1779 read_unlock_bh(&idev->lock);
1780 return -EINVAL;
1782 pmc->mca_sfcount[sfmode]--;
1784 err = 0;
1785 for (i=0; i<sfcount; i++) {
1786 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1788 changerec |= rv > 0;
1789 if (!err && rv < 0)
1790 err = rv;
1792 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1793 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1794 pmc->mca_sfcount[MCAST_INCLUDE]) {
1795 struct ip6_sf_list *psf;
1797 /* filter mode change */
1798 pmc->mca_sfmode = MCAST_INCLUDE;
1799 pmc->mca_crcount = idev->mc_qrv;
1800 idev->mc_ifc_count = pmc->mca_crcount;
1801 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1802 psf->sf_crcount = 0;
1803 mld_ifc_event(pmc->idev);
1804 } else if (sf_setstate(pmc) || changerec)
1805 mld_ifc_event(pmc->idev);
1806 spin_unlock_bh(&pmc->mca_lock);
1807 read_unlock_bh(&idev->lock);
1808 return err;
1812 * Add multicast single-source filter to the interface list
1814 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1815 struct in6_addr *psfsrc, int delta)
1817 struct ip6_sf_list *psf, *psf_prev;
1819 psf_prev = NULL;
1820 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1821 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1822 break;
1823 psf_prev = psf;
1825 if (!psf) {
1826 psf = (struct ip6_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
1827 if (!psf)
1828 return -ENOBUFS;
1829 memset(psf, 0, sizeof(*psf));
1830 psf->sf_addr = *psfsrc;
1831 if (psf_prev) {
1832 psf_prev->sf_next = psf;
1833 } else
1834 pmc->mca_sources = psf;
1836 psf->sf_count[sfmode]++;
1837 return 0;
1840 static void sf_markstate(struct ifmcaddr6 *pmc)
1842 struct ip6_sf_list *psf;
1843 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1845 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1846 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1847 psf->sf_oldin = mca_xcount ==
1848 psf->sf_count[MCAST_EXCLUDE] &&
1849 !psf->sf_count[MCAST_INCLUDE];
1850 } else
1851 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1854 static int sf_setstate(struct ifmcaddr6 *pmc)
1856 struct ip6_sf_list *psf;
1857 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1858 int qrv = pmc->idev->mc_qrv;
1859 int new_in, rv;
1861 rv = 0;
1862 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1863 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1864 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1865 !psf->sf_count[MCAST_INCLUDE];
1866 } else
1867 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1868 if (new_in != psf->sf_oldin) {
1869 psf->sf_crcount = qrv;
1870 rv++;
1873 return rv;
1877 * Add multicast source filter list to the interface list
1879 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
1880 int sfmode, int sfcount, struct in6_addr *psfsrc,
1881 int delta)
1883 struct ifmcaddr6 *pmc;
1884 int isexclude;
1885 int i, err;
1887 if (!idev)
1888 return -ENODEV;
1889 read_lock_bh(&idev->lock);
1890 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1891 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1892 break;
1894 if (!pmc) {
1895 /* MCA not found?? bug */
1896 read_unlock_bh(&idev->lock);
1897 return -ESRCH;
1899 spin_lock_bh(&pmc->mca_lock);
1901 sf_markstate(pmc);
1902 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
1903 if (!delta)
1904 pmc->mca_sfcount[sfmode]++;
1905 err = 0;
1906 for (i=0; i<sfcount; i++) {
1907 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1908 if (err)
1909 break;
1911 if (err) {
1912 int j;
1914 if (!delta)
1915 pmc->mca_sfcount[sfmode]--;
1916 for (j=0; j<i; j++)
1917 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1918 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
1919 struct inet6_dev *idev = pmc->idev;
1920 struct ip6_sf_list *psf;
1922 /* filter mode change */
1923 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1924 pmc->mca_sfmode = MCAST_EXCLUDE;
1925 else if (pmc->mca_sfcount[MCAST_INCLUDE])
1926 pmc->mca_sfmode = MCAST_INCLUDE;
1927 /* else no filters; keep old mode for reports */
1929 pmc->mca_crcount = idev->mc_qrv;
1930 idev->mc_ifc_count = pmc->mca_crcount;
1931 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1932 psf->sf_crcount = 0;
1933 mld_ifc_event(idev);
1934 } else if (sf_setstate(pmc))
1935 mld_ifc_event(idev);
1936 spin_unlock_bh(&pmc->mca_lock);
1937 read_unlock_bh(&idev->lock);
1938 return err;
1941 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
1943 struct ip6_sf_list *psf, *nextpsf;
1945 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
1946 nextpsf = psf->sf_next;
1947 kfree(psf);
1949 pmc->mca_tomb = NULL;
1950 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
1951 nextpsf = psf->sf_next;
1952 kfree(psf);
1954 pmc->mca_sources = NULL;
1955 pmc->mca_sfmode = MCAST_EXCLUDE;
1956 pmc->mca_sfcount[MCAST_EXCLUDE] = 0;
1957 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
1961 static void igmp6_join_group(struct ifmcaddr6 *ma)
1963 unsigned long delay;
1965 if (ma->mca_flags & MAF_NOREPORT)
1966 return;
1968 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
1970 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
1972 spin_lock_bh(&ma->mca_lock);
1973 if (del_timer(&ma->mca_timer)) {
1974 atomic_dec(&ma->mca_refcnt);
1975 delay = ma->mca_timer.expires - jiffies;
1978 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1979 atomic_inc(&ma->mca_refcnt);
1980 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
1981 spin_unlock_bh(&ma->mca_lock);
1984 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
1985 struct inet6_dev *idev)
1987 int err;
1989 if (iml->sflist == 0) {
1990 /* any-source empty exclude case */
1991 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
1993 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
1994 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
1995 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
1996 iml->sflist = NULL;
1997 return err;
2000 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2002 if (MLD_V1_SEEN(ma->idev)) {
2003 if (ma->mca_flags & MAF_LAST_REPORTER)
2004 igmp6_send(&ma->mca_addr, ma->idev->dev,
2005 ICMPV6_MGM_REDUCTION);
2006 } else {
2007 mld_add_delrec(ma->idev, ma);
2008 mld_ifc_event(ma->idev);
2012 static void mld_gq_timer_expire(unsigned long data)
2014 struct inet6_dev *idev = (struct inet6_dev *)data;
2016 idev->mc_gq_running = 0;
2017 mld_send_report(idev, NULL);
2018 __in6_dev_put(idev);
2021 static void mld_ifc_timer_expire(unsigned long data)
2023 struct inet6_dev *idev = (struct inet6_dev *)data;
2025 mld_send_cr(idev);
2026 if (idev->mc_ifc_count) {
2027 idev->mc_ifc_count--;
2028 if (idev->mc_ifc_count)
2029 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2031 __in6_dev_put(idev);
2034 static void mld_ifc_event(struct inet6_dev *idev)
2036 if (MLD_V1_SEEN(idev))
2037 return;
2038 idev->mc_ifc_count = idev->mc_qrv;
2039 mld_ifc_start_timer(idev, 1);
2043 static void igmp6_timer_handler(unsigned long data)
2045 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2047 if (MLD_V1_SEEN(ma->idev))
2048 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2049 else
2050 mld_send_report(ma->idev, ma);
2052 spin_lock(&ma->mca_lock);
2053 ma->mca_flags |= MAF_LAST_REPORTER;
2054 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2055 spin_unlock(&ma->mca_lock);
2056 ma_put(ma);
2059 /* Device going down */
2061 void ipv6_mc_down(struct inet6_dev *idev)
2063 struct ifmcaddr6 *i;
2065 /* Withdraw multicast list */
2067 read_lock_bh(&idev->lock);
2068 idev->mc_ifc_count = 0;
2069 if (del_timer(&idev->mc_ifc_timer))
2070 __in6_dev_put(idev);
2071 idev->mc_gq_running = 0;
2072 if (del_timer(&idev->mc_gq_timer))
2073 __in6_dev_put(idev);
2075 for (i = idev->mc_list; i; i=i->next)
2076 igmp6_group_dropped(i);
2077 read_unlock_bh(&idev->lock);
2079 mld_clear_delrec(idev);
2083 /* Device going up */
2085 void ipv6_mc_up(struct inet6_dev *idev)
2087 struct ifmcaddr6 *i;
2089 /* Install multicast list, except for all-nodes (already installed) */
2091 read_lock_bh(&idev->lock);
2092 for (i = idev->mc_list; i; i=i->next)
2093 igmp6_group_added(i);
2094 read_unlock_bh(&idev->lock);
2097 /* IPv6 device initialization. */
2099 void ipv6_mc_init_dev(struct inet6_dev *idev)
2101 struct in6_addr maddr;
2103 write_lock_bh(&idev->lock);
2104 rwlock_init(&idev->mc_lock);
2105 idev->mc_gq_running = 0;
2106 init_timer(&idev->mc_gq_timer);
2107 idev->mc_gq_timer.data = (unsigned long) idev;
2108 idev->mc_gq_timer.function = &mld_gq_timer_expire;
2109 idev->mc_tomb = NULL;
2110 idev->mc_ifc_count = 0;
2111 init_timer(&idev->mc_ifc_timer);
2112 idev->mc_ifc_timer.data = (unsigned long) idev;
2113 idev->mc_ifc_timer.function = &mld_ifc_timer_expire;
2114 idev->mc_qrv = MLD_QRV_DEFAULT;
2115 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2116 idev->mc_v1_seen = 0;
2117 write_unlock_bh(&idev->lock);
2119 /* Add all-nodes address. */
2120 ipv6_addr_all_nodes(&maddr);
2121 ipv6_dev_mc_inc(idev->dev, &maddr);
2125 * Device is about to be destroyed: clean up.
2128 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2130 struct ifmcaddr6 *i;
2131 struct in6_addr maddr;
2133 /* Deactivate timers */
2134 ipv6_mc_down(idev);
2136 /* Delete all-nodes address. */
2137 ipv6_addr_all_nodes(&maddr);
2139 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2140 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2141 * fail.
2143 __ipv6_dev_mc_dec(idev, &maddr);
2145 if (idev->cnf.forwarding) {
2146 ipv6_addr_all_routers(&maddr);
2147 __ipv6_dev_mc_dec(idev, &maddr);
2150 write_lock_bh(&idev->lock);
2151 while ((i = idev->mc_list) != NULL) {
2152 idev->mc_list = i->next;
2153 write_unlock_bh(&idev->lock);
2155 igmp6_group_dropped(i);
2156 ma_put(i);
2158 write_lock_bh(&idev->lock);
2160 write_unlock_bh(&idev->lock);
2163 #ifdef CONFIG_PROC_FS
2164 struct igmp6_mc_iter_state {
2165 struct net_device *dev;
2166 struct inet6_dev *idev;
2169 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2171 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2173 struct ifmcaddr6 *im = NULL;
2174 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2176 for (state->dev = dev_base, state->idev = NULL;
2177 state->dev;
2178 state->dev = state->dev->next) {
2179 struct inet6_dev *idev;
2180 idev = in6_dev_get(state->dev);
2181 if (!idev)
2182 continue;
2183 read_lock_bh(&idev->lock);
2184 im = idev->mc_list;
2185 if (im) {
2186 state->idev = idev;
2187 break;
2189 read_unlock_bh(&idev->lock);
2190 in6_dev_put(idev);
2192 return im;
2195 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2197 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2199 im = im->next;
2200 while (!im) {
2201 if (likely(state->idev != NULL)) {
2202 read_unlock_bh(&state->idev->lock);
2203 in6_dev_put(state->idev);
2205 state->dev = state->dev->next;
2206 if (!state->dev) {
2207 state->idev = NULL;
2208 break;
2210 state->idev = in6_dev_get(state->dev);
2211 if (!state->idev)
2212 continue;
2213 read_lock_bh(&state->idev->lock);
2214 im = state->idev->mc_list;
2216 return im;
2219 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2221 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2222 if (im)
2223 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2224 --pos;
2225 return pos ? NULL : im;
2228 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2230 read_lock(&dev_base_lock);
2231 return igmp6_mc_get_idx(seq, *pos);
2234 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2236 struct ifmcaddr6 *im;
2237 im = igmp6_mc_get_next(seq, v);
2238 ++*pos;
2239 return im;
2242 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2244 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2245 if (likely(state->idev != NULL)) {
2246 read_unlock_bh(&state->idev->lock);
2247 in6_dev_put(state->idev);
2248 state->idev = NULL;
2250 state->dev = NULL;
2251 read_unlock(&dev_base_lock);
2254 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2256 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2257 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2259 seq_printf(seq,
2260 "%-4d %-15s %04x%04x%04x%04x%04x%04x%04x%04x %5d %08X %ld\n",
2261 state->dev->ifindex, state->dev->name,
2262 NIP6(im->mca_addr),
2263 im->mca_users, im->mca_flags,
2264 (im->mca_flags&MAF_TIMER_RUNNING) ?
2265 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2266 return 0;
2269 static struct seq_operations igmp6_mc_seq_ops = {
2270 .start = igmp6_mc_seq_start,
2271 .next = igmp6_mc_seq_next,
2272 .stop = igmp6_mc_seq_stop,
2273 .show = igmp6_mc_seq_show,
2276 static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2278 struct seq_file *seq;
2279 int rc = -ENOMEM;
2280 struct igmp6_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2282 if (!s)
2283 goto out;
2285 rc = seq_open(file, &igmp6_mc_seq_ops);
2286 if (rc)
2287 goto out_kfree;
2289 seq = file->private_data;
2290 seq->private = s;
2291 memset(s, 0, sizeof(*s));
2292 out:
2293 return rc;
2294 out_kfree:
2295 kfree(s);
2296 goto out;
2299 static struct file_operations igmp6_mc_seq_fops = {
2300 .owner = THIS_MODULE,
2301 .open = igmp6_mc_seq_open,
2302 .read = seq_read,
2303 .llseek = seq_lseek,
2304 .release = seq_release_private,
2307 struct igmp6_mcf_iter_state {
2308 struct net_device *dev;
2309 struct inet6_dev *idev;
2310 struct ifmcaddr6 *im;
2313 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2315 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2317 struct ip6_sf_list *psf = NULL;
2318 struct ifmcaddr6 *im = NULL;
2319 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2321 for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2322 state->dev;
2323 state->dev = state->dev->next) {
2324 struct inet6_dev *idev;
2325 idev = in6_dev_get(state->dev);
2326 if (unlikely(idev == NULL))
2327 continue;
2328 read_lock_bh(&idev->lock);
2329 im = idev->mc_list;
2330 if (likely(im != NULL)) {
2331 spin_lock_bh(&im->mca_lock);
2332 psf = im->mca_sources;
2333 if (likely(psf != NULL)) {
2334 state->im = im;
2335 state->idev = idev;
2336 break;
2338 spin_unlock_bh(&im->mca_lock);
2340 read_unlock_bh(&idev->lock);
2341 in6_dev_put(idev);
2343 return psf;
2346 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2348 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2350 psf = psf->sf_next;
2351 while (!psf) {
2352 spin_unlock_bh(&state->im->mca_lock);
2353 state->im = state->im->next;
2354 while (!state->im) {
2355 if (likely(state->idev != NULL)) {
2356 read_unlock_bh(&state->idev->lock);
2357 in6_dev_put(state->idev);
2359 state->dev = state->dev->next;
2360 if (!state->dev) {
2361 state->idev = NULL;
2362 goto out;
2364 state->idev = in6_dev_get(state->dev);
2365 if (!state->idev)
2366 continue;
2367 read_lock_bh(&state->idev->lock);
2368 state->im = state->idev->mc_list;
2370 if (!state->im)
2371 break;
2372 spin_lock_bh(&state->im->mca_lock);
2373 psf = state->im->mca_sources;
2375 out:
2376 return psf;
2379 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2381 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2382 if (psf)
2383 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2384 --pos;
2385 return pos ? NULL : psf;
2388 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2390 read_lock(&dev_base_lock);
2391 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2394 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2396 struct ip6_sf_list *psf;
2397 if (v == SEQ_START_TOKEN)
2398 psf = igmp6_mcf_get_first(seq);
2399 else
2400 psf = igmp6_mcf_get_next(seq, v);
2401 ++*pos;
2402 return psf;
2405 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2407 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2408 if (likely(state->im != NULL)) {
2409 spin_unlock_bh(&state->im->mca_lock);
2410 state->im = NULL;
2412 if (likely(state->idev != NULL)) {
2413 read_unlock_bh(&state->idev->lock);
2414 in6_dev_put(state->idev);
2415 state->idev = NULL;
2417 state->dev = NULL;
2418 read_unlock(&dev_base_lock);
2421 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2423 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2424 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2426 if (v == SEQ_START_TOKEN) {
2427 seq_printf(seq,
2428 "%3s %6s "
2429 "%32s %32s %6s %6s\n", "Idx",
2430 "Device", "Multicast Address",
2431 "Source Address", "INC", "EXC");
2432 } else {
2433 seq_printf(seq,
2434 "%3d %6.6s "
2435 "%04x%04x%04x%04x%04x%04x%04x%04x "
2436 "%04x%04x%04x%04x%04x%04x%04x%04x "
2437 "%6lu %6lu\n",
2438 state->dev->ifindex, state->dev->name,
2439 NIP6(state->im->mca_addr),
2440 NIP6(psf->sf_addr),
2441 psf->sf_count[MCAST_INCLUDE],
2442 psf->sf_count[MCAST_EXCLUDE]);
2444 return 0;
2447 static struct seq_operations igmp6_mcf_seq_ops = {
2448 .start = igmp6_mcf_seq_start,
2449 .next = igmp6_mcf_seq_next,
2450 .stop = igmp6_mcf_seq_stop,
2451 .show = igmp6_mcf_seq_show,
2454 static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2456 struct seq_file *seq;
2457 int rc = -ENOMEM;
2458 struct igmp6_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2460 if (!s)
2461 goto out;
2463 rc = seq_open(file, &igmp6_mcf_seq_ops);
2464 if (rc)
2465 goto out_kfree;
2467 seq = file->private_data;
2468 seq->private = s;
2469 memset(s, 0, sizeof(*s));
2470 out:
2471 return rc;
2472 out_kfree:
2473 kfree(s);
2474 goto out;
2477 static struct file_operations igmp6_mcf_seq_fops = {
2478 .owner = THIS_MODULE,
2479 .open = igmp6_mcf_seq_open,
2480 .read = seq_read,
2481 .llseek = seq_lseek,
2482 .release = seq_release_private,
2484 #endif
2486 int __init igmp6_init(struct net_proto_family *ops)
2488 struct ipv6_pinfo *np;
2489 struct sock *sk;
2490 int err;
2492 err = sock_create_kern(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6, &igmp6_socket);
2493 if (err < 0) {
2494 printk(KERN_ERR
2495 "Failed to initialize the IGMP6 control socket (err %d).\n",
2496 err);
2497 igmp6_socket = NULL; /* For safety. */
2498 return err;
2501 sk = igmp6_socket->sk;
2502 sk->sk_allocation = GFP_ATOMIC;
2503 sk->sk_prot->unhash(sk);
2505 np = inet6_sk(sk);
2506 np->hop_limit = 1;
2508 #ifdef CONFIG_PROC_FS
2509 proc_net_fops_create("igmp6", S_IRUGO, &igmp6_mc_seq_fops);
2510 proc_net_fops_create("mcfilter6", S_IRUGO, &igmp6_mcf_seq_fops);
2511 #endif
2513 return 0;
2516 void igmp6_cleanup(void)
2518 sock_release(igmp6_socket);
2519 igmp6_socket = NULL; /* for safety */
2521 #ifdef CONFIG_PROC_FS
2522 proc_net_remove("mcfilter6");
2523 proc_net_remove("igmp6");
2524 #endif