[PATCH] I2C: Separate non-i2c hwmon drivers from i2c-core (9/9)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv6 / mcast.c
blob29fed6e58d0aa631c19d55c967d0bb9ff909c5f4
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 -EADDRNOTAVAIL;
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 err = -EINVAL;
391 goto done;
393 /* if a source filter was set, must be the same mode as before */
394 if (pmc->sflist) {
395 if (pmc->sfmode != omode) {
396 err = -EINVAL;
397 goto done;
399 } else if (pmc->sfmode != omode) {
400 /* allow mode switches for empty-set filters */
401 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
402 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
403 pmc->sfmode = omode;
406 psl = pmc->sflist;
407 if (!add) {
408 if (!psl)
409 goto done; /* err = -EADDRNOTAVAIL */
410 rv = !0;
411 for (i=0; i<psl->sl_count; i++) {
412 rv = memcmp(&psl->sl_addr[i], source,
413 sizeof(struct in6_addr));
414 if (rv == 0)
415 break;
417 if (rv) /* source not found */
418 goto done; /* err = -EADDRNOTAVAIL */
420 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
421 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
422 leavegroup = 1;
423 goto done;
426 /* update the interface filter */
427 ip6_mc_del_src(idev, group, omode, 1, source, 1);
429 for (j=i+1; j<psl->sl_count; j++)
430 psl->sl_addr[j-1] = psl->sl_addr[j];
431 psl->sl_count--;
432 err = 0;
433 goto done;
435 /* else, add a new source to the filter */
437 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
438 err = -ENOBUFS;
439 goto done;
441 if (!psl || psl->sl_count == psl->sl_max) {
442 struct ip6_sf_socklist *newpsl;
443 int count = IP6_SFBLOCK;
445 if (psl)
446 count += psl->sl_max;
447 newpsl = (struct ip6_sf_socklist *)sock_kmalloc(sk,
448 IP6_SFLSIZE(count), GFP_ATOMIC);
449 if (!newpsl) {
450 err = -ENOBUFS;
451 goto done;
453 newpsl->sl_max = count;
454 newpsl->sl_count = count - IP6_SFBLOCK;
455 if (psl) {
456 for (i=0; i<psl->sl_count; i++)
457 newpsl->sl_addr[i] = psl->sl_addr[i];
458 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
460 pmc->sflist = psl = newpsl;
462 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
463 for (i=0; i<psl->sl_count; i++) {
464 rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
465 if (rv == 0)
466 break;
468 if (rv == 0) /* address already there is an error */
469 goto done;
470 for (j=psl->sl_count-1; j>=i; j--)
471 psl->sl_addr[j+1] = psl->sl_addr[j];
472 psl->sl_addr[i] = *source;
473 psl->sl_count++;
474 err = 0;
475 /* update the interface list */
476 ip6_mc_add_src(idev, group, omode, 1, source, 1);
477 done:
478 read_unlock_bh(&ipv6_sk_mc_lock);
479 read_unlock_bh(&idev->lock);
480 in6_dev_put(idev);
481 dev_put(dev);
482 if (leavegroup)
483 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
484 return err;
487 int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
489 struct in6_addr *group;
490 struct ipv6_mc_socklist *pmc;
491 struct net_device *dev;
492 struct inet6_dev *idev;
493 struct ipv6_pinfo *inet6 = inet6_sk(sk);
494 struct ip6_sf_socklist *newpsl, *psl;
495 int leavegroup = 0;
496 int i, err;
498 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
500 if (!ipv6_addr_is_multicast(group))
501 return -EINVAL;
502 if (gsf->gf_fmode != MCAST_INCLUDE &&
503 gsf->gf_fmode != MCAST_EXCLUDE)
504 return -EINVAL;
506 idev = ip6_mc_find_dev(group, gsf->gf_interface);
508 if (!idev)
509 return -ENODEV;
510 dev = idev->dev;
512 err = 0;
513 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
514 leavegroup = 1;
515 goto done;
518 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
519 if (pmc->ifindex != gsf->gf_interface)
520 continue;
521 if (ipv6_addr_equal(&pmc->addr, group))
522 break;
524 if (!pmc) { /* must have a prior join */
525 err = -EINVAL;
526 goto done;
528 if (gsf->gf_numsrc) {
529 newpsl = (struct ip6_sf_socklist *)sock_kmalloc(sk,
530 IP6_SFLSIZE(gsf->gf_numsrc), GFP_ATOMIC);
531 if (!newpsl) {
532 err = -ENOBUFS;
533 goto done;
535 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
536 for (i=0; i<newpsl->sl_count; ++i) {
537 struct sockaddr_in6 *psin6;
539 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
540 newpsl->sl_addr[i] = psin6->sin6_addr;
542 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
543 newpsl->sl_count, newpsl->sl_addr, 0);
544 if (err) {
545 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
546 goto done;
548 } else
549 newpsl = NULL;
550 psl = pmc->sflist;
551 if (psl) {
552 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
553 psl->sl_count, psl->sl_addr, 0);
554 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
555 } else
556 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
557 pmc->sflist = newpsl;
558 pmc->sfmode = gsf->gf_fmode;
559 err = 0;
560 done:
561 read_unlock_bh(&idev->lock);
562 in6_dev_put(idev);
563 dev_put(dev);
564 if (leavegroup)
565 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
566 return err;
569 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
570 struct group_filter __user *optval, int __user *optlen)
572 int err, i, count, copycount;
573 struct in6_addr *group;
574 struct ipv6_mc_socklist *pmc;
575 struct inet6_dev *idev;
576 struct net_device *dev;
577 struct ipv6_pinfo *inet6 = inet6_sk(sk);
578 struct ip6_sf_socklist *psl;
580 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
582 if (!ipv6_addr_is_multicast(group))
583 return -EINVAL;
585 idev = ip6_mc_find_dev(group, gsf->gf_interface);
587 if (!idev)
588 return -ENODEV;
590 dev = idev->dev;
592 err = -EADDRNOTAVAIL;
594 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
595 if (pmc->ifindex != gsf->gf_interface)
596 continue;
597 if (ipv6_addr_equal(group, &pmc->addr))
598 break;
600 if (!pmc) /* must have a prior join */
601 goto done;
602 gsf->gf_fmode = pmc->sfmode;
603 psl = pmc->sflist;
604 count = psl ? psl->sl_count : 0;
605 read_unlock_bh(&idev->lock);
606 in6_dev_put(idev);
607 dev_put(dev);
609 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
610 gsf->gf_numsrc = count;
611 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
612 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
613 return -EFAULT;
615 for (i=0; i<copycount; i++) {
616 struct sockaddr_in6 *psin6;
617 struct sockaddr_storage ss;
619 psin6 = (struct sockaddr_in6 *)&ss;
620 memset(&ss, 0, sizeof(ss));
621 psin6->sin6_family = AF_INET6;
622 psin6->sin6_addr = psl->sl_addr[i];
623 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
624 return -EFAULT;
626 return 0;
627 done:
628 read_unlock_bh(&idev->lock);
629 in6_dev_put(idev);
630 dev_put(dev);
631 return err;
634 int inet6_mc_check(struct sock *sk, struct in6_addr *mc_addr,
635 struct in6_addr *src_addr)
637 struct ipv6_pinfo *np = inet6_sk(sk);
638 struct ipv6_mc_socklist *mc;
639 struct ip6_sf_socklist *psl;
640 int rv = 1;
642 read_lock(&ipv6_sk_mc_lock);
643 for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
644 if (ipv6_addr_equal(&mc->addr, mc_addr))
645 break;
647 if (!mc) {
648 read_unlock(&ipv6_sk_mc_lock);
649 return 1;
651 psl = mc->sflist;
652 if (!psl) {
653 rv = mc->sfmode == MCAST_EXCLUDE;
654 } else {
655 int i;
657 for (i=0; i<psl->sl_count; i++) {
658 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
659 break;
661 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
662 rv = 0;
663 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
664 rv = 0;
666 read_unlock(&ipv6_sk_mc_lock);
668 return rv;
671 static void ma_put(struct ifmcaddr6 *mc)
673 if (atomic_dec_and_test(&mc->mca_refcnt)) {
674 in6_dev_put(mc->idev);
675 kfree(mc);
679 static void igmp6_group_added(struct ifmcaddr6 *mc)
681 struct net_device *dev = mc->idev->dev;
682 char buf[MAX_ADDR_LEN];
684 spin_lock_bh(&mc->mca_lock);
685 if (!(mc->mca_flags&MAF_LOADED)) {
686 mc->mca_flags |= MAF_LOADED;
687 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
688 dev_mc_add(dev, buf, dev->addr_len, 0);
690 spin_unlock_bh(&mc->mca_lock);
692 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
693 return;
695 if (MLD_V1_SEEN(mc->idev)) {
696 igmp6_join_group(mc);
697 return;
699 /* else v2 */
701 mc->mca_crcount = mc->idev->mc_qrv;
702 mld_ifc_event(mc->idev);
705 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
707 struct net_device *dev = mc->idev->dev;
708 char buf[MAX_ADDR_LEN];
710 spin_lock_bh(&mc->mca_lock);
711 if (mc->mca_flags&MAF_LOADED) {
712 mc->mca_flags &= ~MAF_LOADED;
713 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
714 dev_mc_delete(dev, buf, dev->addr_len, 0);
717 if (mc->mca_flags & MAF_NOREPORT)
718 goto done;
719 spin_unlock_bh(&mc->mca_lock);
721 if (!mc->idev->dead)
722 igmp6_leave_group(mc);
724 spin_lock_bh(&mc->mca_lock);
725 if (del_timer(&mc->mca_timer))
726 atomic_dec(&mc->mca_refcnt);
727 done:
728 ip6_mc_clear_src(mc);
729 spin_unlock_bh(&mc->mca_lock);
733 * deleted ifmcaddr6 manipulation
735 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
737 struct ifmcaddr6 *pmc;
739 /* this is an "ifmcaddr6" for convenience; only the fields below
740 * are actually used. In particular, the refcnt and users are not
741 * used for management of the delete list. Using the same structure
742 * for deleted items allows change reports to use common code with
743 * non-deleted or query-response MCA's.
745 pmc = (struct ifmcaddr6 *)kmalloc(sizeof(*pmc), GFP_ATOMIC);
746 if (!pmc)
747 return;
748 memset(pmc, 0, sizeof(*pmc));
749 spin_lock_bh(&im->mca_lock);
750 spin_lock_init(&pmc->mca_lock);
751 pmc->idev = im->idev;
752 in6_dev_hold(idev);
753 pmc->mca_addr = im->mca_addr;
754 pmc->mca_crcount = idev->mc_qrv;
755 pmc->mca_sfmode = im->mca_sfmode;
756 if (pmc->mca_sfmode == MCAST_INCLUDE) {
757 struct ip6_sf_list *psf;
759 pmc->mca_tomb = im->mca_tomb;
760 pmc->mca_sources = im->mca_sources;
761 im->mca_tomb = im->mca_sources = NULL;
762 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
763 psf->sf_crcount = pmc->mca_crcount;
765 spin_unlock_bh(&im->mca_lock);
767 write_lock_bh(&idev->mc_lock);
768 pmc->next = idev->mc_tomb;
769 idev->mc_tomb = pmc;
770 write_unlock_bh(&idev->mc_lock);
773 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
775 struct ifmcaddr6 *pmc, *pmc_prev;
776 struct ip6_sf_list *psf, *psf_next;
778 write_lock_bh(&idev->mc_lock);
779 pmc_prev = NULL;
780 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
781 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
782 break;
783 pmc_prev = pmc;
785 if (pmc) {
786 if (pmc_prev)
787 pmc_prev->next = pmc->next;
788 else
789 idev->mc_tomb = pmc->next;
791 write_unlock_bh(&idev->mc_lock);
792 if (pmc) {
793 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
794 psf_next = psf->sf_next;
795 kfree(psf);
797 in6_dev_put(pmc->idev);
798 kfree(pmc);
802 static void mld_clear_delrec(struct inet6_dev *idev)
804 struct ifmcaddr6 *pmc, *nextpmc;
806 write_lock_bh(&idev->mc_lock);
807 pmc = idev->mc_tomb;
808 idev->mc_tomb = NULL;
809 write_unlock_bh(&idev->mc_lock);
811 for (; pmc; pmc = nextpmc) {
812 nextpmc = pmc->next;
813 ip6_mc_clear_src(pmc);
814 in6_dev_put(pmc->idev);
815 kfree(pmc);
818 /* clear dead sources, too */
819 read_lock_bh(&idev->lock);
820 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
821 struct ip6_sf_list *psf, *psf_next;
823 spin_lock_bh(&pmc->mca_lock);
824 psf = pmc->mca_tomb;
825 pmc->mca_tomb = NULL;
826 spin_unlock_bh(&pmc->mca_lock);
827 for (; psf; psf=psf_next) {
828 psf_next = psf->sf_next;
829 kfree(psf);
832 read_unlock_bh(&idev->lock);
837 * device multicast group inc (add if not found)
839 int ipv6_dev_mc_inc(struct net_device *dev, struct in6_addr *addr)
841 struct ifmcaddr6 *mc;
842 struct inet6_dev *idev;
844 idev = in6_dev_get(dev);
846 if (idev == NULL)
847 return -EINVAL;
849 write_lock_bh(&idev->lock);
850 if (idev->dead) {
851 write_unlock_bh(&idev->lock);
852 in6_dev_put(idev);
853 return -ENODEV;
856 for (mc = idev->mc_list; mc; mc = mc->next) {
857 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
858 mc->mca_users++;
859 write_unlock_bh(&idev->lock);
860 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
861 NULL, 0);
862 in6_dev_put(idev);
863 return 0;
868 * not found: create a new one.
871 mc = kmalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
873 if (mc == NULL) {
874 write_unlock_bh(&idev->lock);
875 in6_dev_put(idev);
876 return -ENOMEM;
879 memset(mc, 0, sizeof(struct ifmcaddr6));
880 init_timer(&mc->mca_timer);
881 mc->mca_timer.function = igmp6_timer_handler;
882 mc->mca_timer.data = (unsigned long) mc;
884 ipv6_addr_copy(&mc->mca_addr, addr);
885 mc->idev = idev;
886 mc->mca_users = 1;
887 /* mca_stamp should be updated upon changes */
888 mc->mca_cstamp = mc->mca_tstamp = jiffies;
889 atomic_set(&mc->mca_refcnt, 2);
890 spin_lock_init(&mc->mca_lock);
892 /* initial mode is (EX, empty) */
893 mc->mca_sfmode = MCAST_EXCLUDE;
894 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
896 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
897 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
898 mc->mca_flags |= MAF_NOREPORT;
900 mc->next = idev->mc_list;
901 idev->mc_list = mc;
902 write_unlock_bh(&idev->lock);
904 mld_del_delrec(idev, &mc->mca_addr);
905 igmp6_group_added(mc);
906 ma_put(mc);
907 return 0;
911 * device multicast group del
913 int __ipv6_dev_mc_dec(struct inet6_dev *idev, struct in6_addr *addr)
915 struct ifmcaddr6 *ma, **map;
917 write_lock_bh(&idev->lock);
918 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
919 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
920 if (--ma->mca_users == 0) {
921 *map = ma->next;
922 write_unlock_bh(&idev->lock);
924 igmp6_group_dropped(ma);
926 ma_put(ma);
927 return 0;
929 write_unlock_bh(&idev->lock);
930 return 0;
933 write_unlock_bh(&idev->lock);
935 return -ENOENT;
938 int ipv6_dev_mc_dec(struct net_device *dev, struct in6_addr *addr)
940 struct inet6_dev *idev = in6_dev_get(dev);
941 int err;
943 if (!idev)
944 return -ENODEV;
946 err = __ipv6_dev_mc_dec(idev, addr);
948 in6_dev_put(idev);
950 return err;
954 * identify MLD packets for MLD filter exceptions
956 int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
958 struct icmp6hdr *pic;
960 if (nexthdr != IPPROTO_ICMPV6)
961 return 0;
963 if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
964 return 0;
966 pic = (struct icmp6hdr *)skb->h.raw;
968 switch (pic->icmp6_type) {
969 case ICMPV6_MGM_QUERY:
970 case ICMPV6_MGM_REPORT:
971 case ICMPV6_MGM_REDUCTION:
972 case ICMPV6_MLD2_REPORT:
973 return 1;
974 default:
975 break;
977 return 0;
981 * check if the interface/address pair is valid
983 int ipv6_chk_mcast_addr(struct net_device *dev, struct in6_addr *group,
984 struct in6_addr *src_addr)
986 struct inet6_dev *idev;
987 struct ifmcaddr6 *mc;
988 int rv = 0;
990 idev = in6_dev_get(dev);
991 if (idev) {
992 read_lock_bh(&idev->lock);
993 for (mc = idev->mc_list; mc; mc=mc->next) {
994 if (ipv6_addr_equal(&mc->mca_addr, group))
995 break;
997 if (mc) {
998 if (src_addr && !ipv6_addr_any(src_addr)) {
999 struct ip6_sf_list *psf;
1001 spin_lock_bh(&mc->mca_lock);
1002 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
1003 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1004 break;
1006 if (psf)
1007 rv = psf->sf_count[MCAST_INCLUDE] ||
1008 psf->sf_count[MCAST_EXCLUDE] !=
1009 mc->mca_sfcount[MCAST_EXCLUDE];
1010 else
1011 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
1012 spin_unlock_bh(&mc->mca_lock);
1013 } else
1014 rv = 1; /* don't filter unspecified source */
1016 read_unlock_bh(&idev->lock);
1017 in6_dev_put(idev);
1019 return rv;
1022 static void mld_gq_start_timer(struct inet6_dev *idev)
1024 int tv = net_random() % idev->mc_maxdelay;
1026 idev->mc_gq_running = 1;
1027 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1028 in6_dev_hold(idev);
1031 static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
1033 int tv = net_random() % delay;
1035 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1036 in6_dev_hold(idev);
1040 * IGMP handling (alias multicast ICMPv6 messages)
1043 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1045 unsigned long delay = resptime;
1047 /* Do not start timer for these addresses */
1048 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1049 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1050 return;
1052 if (del_timer(&ma->mca_timer)) {
1053 atomic_dec(&ma->mca_refcnt);
1054 delay = ma->mca_timer.expires - jiffies;
1057 if (delay >= resptime) {
1058 if (resptime)
1059 delay = net_random() % resptime;
1060 else
1061 delay = 1;
1063 ma->mca_timer.expires = jiffies + delay;
1064 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1065 atomic_inc(&ma->mca_refcnt);
1066 ma->mca_flags |= MAF_TIMER_RUNNING;
1069 static void mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1070 struct in6_addr *srcs)
1072 struct ip6_sf_list *psf;
1073 int i, scount;
1075 scount = 0;
1076 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1077 if (scount == nsrcs)
1078 break;
1079 for (i=0; i<nsrcs; i++)
1080 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1081 psf->sf_gsresp = 1;
1082 scount++;
1083 break;
1088 int igmp6_event_query(struct sk_buff *skb)
1090 struct mld2_query *mlh2 = (struct mld2_query *) skb->h.raw;
1091 struct ifmcaddr6 *ma;
1092 struct in6_addr *group;
1093 unsigned long max_delay;
1094 struct inet6_dev *idev;
1095 struct icmp6hdr *hdr;
1096 int group_type;
1097 int mark = 0;
1098 int len;
1100 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1101 return -EINVAL;
1103 /* compute payload length excluding extension headers */
1104 len = ntohs(skb->nh.ipv6h->payload_len) + sizeof(struct ipv6hdr);
1105 len -= (char *)skb->h.raw - (char *)skb->nh.ipv6h;
1107 /* Drop queries with not link local source */
1108 if (!(ipv6_addr_type(&skb->nh.ipv6h->saddr)&IPV6_ADDR_LINKLOCAL))
1109 return -EINVAL;
1111 idev = in6_dev_get(skb->dev);
1113 if (idev == NULL)
1114 return 0;
1116 hdr = (struct icmp6hdr *) skb->h.raw;
1117 group = (struct in6_addr *) (hdr + 1);
1118 group_type = ipv6_addr_type(group);
1120 if (group_type != IPV6_ADDR_ANY &&
1121 !(group_type&IPV6_ADDR_MULTICAST)) {
1122 in6_dev_put(idev);
1123 return -EINVAL;
1126 if (len == 24) {
1127 int switchback;
1128 /* MLDv1 router present */
1130 /* Translate milliseconds to jiffies */
1131 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
1133 switchback = (idev->mc_qrv + 1) * max_delay;
1134 idev->mc_v1_seen = jiffies + switchback;
1136 /* cancel the interface change timer */
1137 idev->mc_ifc_count = 0;
1138 if (del_timer(&idev->mc_ifc_timer))
1139 __in6_dev_put(idev);
1140 /* clear deleted report items */
1141 mld_clear_delrec(idev);
1142 } else if (len >= 28) {
1143 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
1144 if (!max_delay)
1145 max_delay = 1;
1146 idev->mc_maxdelay = max_delay;
1147 if (mlh2->qrv)
1148 idev->mc_qrv = mlh2->qrv;
1149 if (group_type == IPV6_ADDR_ANY) { /* general query */
1150 if (mlh2->nsrcs) {
1151 in6_dev_put(idev);
1152 return -EINVAL; /* no sources allowed */
1154 mld_gq_start_timer(idev);
1155 in6_dev_put(idev);
1156 return 0;
1158 /* mark sources to include, if group & source-specific */
1159 mark = mlh2->nsrcs != 0;
1160 } else {
1161 in6_dev_put(idev);
1162 return -EINVAL;
1165 read_lock_bh(&idev->lock);
1166 if (group_type == IPV6_ADDR_ANY) {
1167 for (ma = idev->mc_list; ma; ma=ma->next) {
1168 spin_lock_bh(&ma->mca_lock);
1169 igmp6_group_queried(ma, max_delay);
1170 spin_unlock_bh(&ma->mca_lock);
1172 } else {
1173 for (ma = idev->mc_list; ma; ma=ma->next) {
1174 if (group_type != IPV6_ADDR_ANY &&
1175 !ipv6_addr_equal(group, &ma->mca_addr))
1176 continue;
1177 spin_lock_bh(&ma->mca_lock);
1178 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1179 /* gsquery <- gsquery && mark */
1180 if (!mark)
1181 ma->mca_flags &= ~MAF_GSQUERY;
1182 } else {
1183 /* gsquery <- mark */
1184 if (mark)
1185 ma->mca_flags |= MAF_GSQUERY;
1186 else
1187 ma->mca_flags &= ~MAF_GSQUERY;
1189 if (ma->mca_flags & MAF_GSQUERY)
1190 mld_marksources(ma, ntohs(mlh2->nsrcs),
1191 mlh2->srcs);
1192 igmp6_group_queried(ma, max_delay);
1193 spin_unlock_bh(&ma->mca_lock);
1194 if (group_type != IPV6_ADDR_ANY)
1195 break;
1198 read_unlock_bh(&idev->lock);
1199 in6_dev_put(idev);
1201 return 0;
1205 int igmp6_event_report(struct sk_buff *skb)
1207 struct ifmcaddr6 *ma;
1208 struct in6_addr *addrp;
1209 struct inet6_dev *idev;
1210 struct icmp6hdr *hdr;
1211 int addr_type;
1213 /* Our own report looped back. Ignore it. */
1214 if (skb->pkt_type == PACKET_LOOPBACK)
1215 return 0;
1217 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1218 return -EINVAL;
1220 hdr = (struct icmp6hdr*) skb->h.raw;
1222 /* Drop reports with not link local source */
1223 addr_type = ipv6_addr_type(&skb->nh.ipv6h->saddr);
1224 if (addr_type != IPV6_ADDR_ANY &&
1225 !(addr_type&IPV6_ADDR_LINKLOCAL))
1226 return -EINVAL;
1228 addrp = (struct in6_addr *) (hdr + 1);
1230 idev = in6_dev_get(skb->dev);
1231 if (idev == NULL)
1232 return -ENODEV;
1235 * Cancel the timer for this group
1238 read_lock_bh(&idev->lock);
1239 for (ma = idev->mc_list; ma; ma=ma->next) {
1240 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1241 spin_lock(&ma->mca_lock);
1242 if (del_timer(&ma->mca_timer))
1243 atomic_dec(&ma->mca_refcnt);
1244 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1245 spin_unlock(&ma->mca_lock);
1246 break;
1249 read_unlock_bh(&idev->lock);
1250 in6_dev_put(idev);
1251 return 0;
1254 static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1255 int gdeleted, int sdeleted)
1257 switch (type) {
1258 case MLD2_MODE_IS_INCLUDE:
1259 case MLD2_MODE_IS_EXCLUDE:
1260 if (gdeleted || sdeleted)
1261 return 0;
1262 return !((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp);
1263 case MLD2_CHANGE_TO_INCLUDE:
1264 if (gdeleted || sdeleted)
1265 return 0;
1266 return psf->sf_count[MCAST_INCLUDE] != 0;
1267 case MLD2_CHANGE_TO_EXCLUDE:
1268 if (gdeleted || sdeleted)
1269 return 0;
1270 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1271 psf->sf_count[MCAST_INCLUDE])
1272 return 0;
1273 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1274 psf->sf_count[MCAST_EXCLUDE];
1275 case MLD2_ALLOW_NEW_SOURCES:
1276 if (gdeleted || !psf->sf_crcount)
1277 return 0;
1278 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1279 case MLD2_BLOCK_OLD_SOURCES:
1280 if (pmc->mca_sfmode == MCAST_INCLUDE)
1281 return gdeleted || (psf->sf_crcount && sdeleted);
1282 return psf->sf_crcount && !gdeleted && !sdeleted;
1284 return 0;
1287 static int
1288 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1290 struct ip6_sf_list *psf;
1291 int scount = 0;
1293 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1294 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1295 continue;
1296 scount++;
1298 return scount;
1301 static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1303 struct sock *sk = igmp6_socket->sk;
1304 struct sk_buff *skb;
1305 struct mld2_report *pmr;
1306 struct in6_addr addr_buf;
1307 int err;
1308 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1309 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1310 IPV6_TLV_PADN, 0 };
1312 /* we assume size > sizeof(ra) here */
1313 skb = sock_alloc_send_skb(sk, size + LL_RESERVED_SPACE(dev), 1, &err);
1315 if (skb == 0)
1316 return NULL;
1318 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1320 if (ipv6_get_lladdr(dev, &addr_buf)) {
1321 /* <draft-ietf-magma-mld-source-05.txt>:
1322 * use unspecified address as the source address
1323 * when a valid link-local address is not available.
1325 memset(&addr_buf, 0, sizeof(addr_buf));
1328 ip6_nd_hdr(sk, skb, dev, &addr_buf, &mld2_all_mcr, NEXTHDR_HOP, 0);
1330 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1332 pmr =(struct mld2_report *)skb_put(skb, sizeof(*pmr));
1333 skb->h.raw = (unsigned char *)pmr;
1334 pmr->type = ICMPV6_MLD2_REPORT;
1335 pmr->resv1 = 0;
1336 pmr->csum = 0;
1337 pmr->resv2 = 0;
1338 pmr->ngrec = 0;
1339 return skb;
1342 static inline int mld_dev_queue_xmit2(struct sk_buff *skb)
1344 struct net_device *dev = skb->dev;
1346 if (dev->hard_header) {
1347 unsigned char ha[MAX_ADDR_LEN];
1348 int err;
1350 ndisc_mc_map(&skb->nh.ipv6h->daddr, ha, dev, 1);
1351 err = dev->hard_header(skb, dev, ETH_P_IPV6, ha, NULL, skb->len);
1352 if (err < 0) {
1353 kfree_skb(skb);
1354 return err;
1357 return dev_queue_xmit(skb);
1360 static inline int mld_dev_queue_xmit(struct sk_buff *skb)
1362 return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb, NULL, skb->dev,
1363 mld_dev_queue_xmit2);
1366 static void mld_sendpack(struct sk_buff *skb)
1368 struct ipv6hdr *pip6 = skb->nh.ipv6h;
1369 struct mld2_report *pmr = (struct mld2_report *)skb->h.raw;
1370 int payload_len, mldlen;
1371 struct inet6_dev *idev = in6_dev_get(skb->dev);
1372 int err;
1374 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1375 payload_len = skb->tail - (unsigned char *)skb->nh.ipv6h -
1376 sizeof(struct ipv6hdr);
1377 mldlen = skb->tail - skb->h.raw;
1378 pip6->payload_len = htons(payload_len);
1380 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1381 IPPROTO_ICMPV6, csum_partial(skb->h.raw, mldlen, 0));
1382 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1383 mld_dev_queue_xmit);
1384 if (!err) {
1385 ICMP6_INC_STATS(idev,ICMP6_MIB_OUTMSGS);
1386 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1387 } else
1388 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1390 if (likely(idev != NULL))
1391 in6_dev_put(idev);
1394 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1396 return sizeof(struct mld2_grec) + 4*mld_scount(pmc,type,gdel,sdel);
1399 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1400 int type, struct mld2_grec **ppgr)
1402 struct net_device *dev = pmc->idev->dev;
1403 struct mld2_report *pmr;
1404 struct mld2_grec *pgr;
1406 if (!skb)
1407 skb = mld_newpack(dev, dev->mtu);
1408 if (!skb)
1409 return NULL;
1410 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1411 pgr->grec_type = type;
1412 pgr->grec_auxwords = 0;
1413 pgr->grec_nsrcs = 0;
1414 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1415 pmr = (struct mld2_report *)skb->h.raw;
1416 pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1417 *ppgr = pgr;
1418 return skb;
1421 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1422 skb_tailroom(skb)) : 0)
1424 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1425 int type, int gdeleted, int sdeleted)
1427 struct net_device *dev = pmc->idev->dev;
1428 struct mld2_report *pmr;
1429 struct mld2_grec *pgr = NULL;
1430 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1431 int scount, first, isquery, truncate;
1433 if (pmc->mca_flags & MAF_NOREPORT)
1434 return skb;
1436 isquery = type == MLD2_MODE_IS_INCLUDE ||
1437 type == MLD2_MODE_IS_EXCLUDE;
1438 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1439 type == MLD2_CHANGE_TO_EXCLUDE;
1441 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1443 if (!*psf_list) {
1444 if (type == MLD2_ALLOW_NEW_SOURCES ||
1445 type == MLD2_BLOCK_OLD_SOURCES)
1446 return skb;
1447 if (pmc->mca_crcount || isquery) {
1448 /* make sure we have room for group header and at
1449 * least one source.
1451 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)+
1452 sizeof(struct in6_addr)) {
1453 mld_sendpack(skb);
1454 skb = NULL; /* add_grhead will get a new one */
1456 skb = add_grhead(skb, pmc, type, &pgr);
1458 return skb;
1460 pmr = skb ? (struct mld2_report *)skb->h.raw : NULL;
1462 /* EX and TO_EX get a fresh packet, if needed */
1463 if (truncate) {
1464 if (pmr && pmr->ngrec &&
1465 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1466 if (skb)
1467 mld_sendpack(skb);
1468 skb = mld_newpack(dev, dev->mtu);
1471 first = 1;
1472 scount = 0;
1473 psf_prev = NULL;
1474 for (psf=*psf_list; psf; psf=psf_next) {
1475 struct in6_addr *psrc;
1477 psf_next = psf->sf_next;
1479 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1480 psf_prev = psf;
1481 continue;
1484 /* clear marks on query responses */
1485 if (isquery)
1486 psf->sf_gsresp = 0;
1488 if (AVAILABLE(skb) < sizeof(*psrc) +
1489 first*sizeof(struct mld2_grec)) {
1490 if (truncate && !first)
1491 break; /* truncate these */
1492 if (pgr)
1493 pgr->grec_nsrcs = htons(scount);
1494 if (skb)
1495 mld_sendpack(skb);
1496 skb = mld_newpack(dev, dev->mtu);
1497 first = 1;
1498 scount = 0;
1500 if (first) {
1501 skb = add_grhead(skb, pmc, type, &pgr);
1502 first = 0;
1504 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1505 *psrc = psf->sf_addr;
1506 scount++;
1507 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1508 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1509 psf->sf_crcount--;
1510 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1511 if (psf_prev)
1512 psf_prev->sf_next = psf->sf_next;
1513 else
1514 *psf_list = psf->sf_next;
1515 kfree(psf);
1516 continue;
1519 psf_prev = psf;
1521 if (pgr)
1522 pgr->grec_nsrcs = htons(scount);
1524 if (isquery)
1525 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1526 return skb;
1529 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1531 struct sk_buff *skb = NULL;
1532 int type;
1534 if (!pmc) {
1535 read_lock_bh(&idev->lock);
1536 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1537 if (pmc->mca_flags & MAF_NOREPORT)
1538 continue;
1539 spin_lock_bh(&pmc->mca_lock);
1540 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1541 type = MLD2_MODE_IS_EXCLUDE;
1542 else
1543 type = MLD2_MODE_IS_INCLUDE;
1544 skb = add_grec(skb, pmc, type, 0, 0);
1545 spin_unlock_bh(&pmc->mca_lock);
1547 read_unlock_bh(&idev->lock);
1548 } else {
1549 spin_lock_bh(&pmc->mca_lock);
1550 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1551 type = MLD2_MODE_IS_EXCLUDE;
1552 else
1553 type = MLD2_MODE_IS_INCLUDE;
1554 skb = add_grec(skb, pmc, type, 0, 0);
1555 spin_unlock_bh(&pmc->mca_lock);
1557 if (skb)
1558 mld_sendpack(skb);
1562 * remove zero-count source records from a source filter list
1564 static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1566 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1568 psf_prev = NULL;
1569 for (psf=*ppsf; psf; psf = psf_next) {
1570 psf_next = psf->sf_next;
1571 if (psf->sf_crcount == 0) {
1572 if (psf_prev)
1573 psf_prev->sf_next = psf->sf_next;
1574 else
1575 *ppsf = psf->sf_next;
1576 kfree(psf);
1577 } else
1578 psf_prev = psf;
1582 static void mld_send_cr(struct inet6_dev *idev)
1584 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1585 struct sk_buff *skb = NULL;
1586 int type, dtype;
1588 read_lock_bh(&idev->lock);
1589 write_lock_bh(&idev->mc_lock);
1591 /* deleted MCA's */
1592 pmc_prev = NULL;
1593 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1594 pmc_next = pmc->next;
1595 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1596 type = MLD2_BLOCK_OLD_SOURCES;
1597 dtype = MLD2_BLOCK_OLD_SOURCES;
1598 skb = add_grec(skb, pmc, type, 1, 0);
1599 skb = add_grec(skb, pmc, dtype, 1, 1);
1601 if (pmc->mca_crcount) {
1602 pmc->mca_crcount--;
1603 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1604 type = MLD2_CHANGE_TO_INCLUDE;
1605 skb = add_grec(skb, pmc, type, 1, 0);
1607 if (pmc->mca_crcount == 0) {
1608 mld_clear_zeros(&pmc->mca_tomb);
1609 mld_clear_zeros(&pmc->mca_sources);
1612 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1613 !pmc->mca_sources) {
1614 if (pmc_prev)
1615 pmc_prev->next = pmc_next;
1616 else
1617 idev->mc_tomb = pmc_next;
1618 in6_dev_put(pmc->idev);
1619 kfree(pmc);
1620 } else
1621 pmc_prev = pmc;
1623 write_unlock_bh(&idev->mc_lock);
1625 /* change recs */
1626 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1627 spin_lock_bh(&pmc->mca_lock);
1628 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1629 type = MLD2_BLOCK_OLD_SOURCES;
1630 dtype = MLD2_ALLOW_NEW_SOURCES;
1631 } else {
1632 type = MLD2_ALLOW_NEW_SOURCES;
1633 dtype = MLD2_BLOCK_OLD_SOURCES;
1635 skb = add_grec(skb, pmc, type, 0, 0);
1636 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1638 /* filter mode changes */
1639 if (pmc->mca_crcount) {
1640 pmc->mca_crcount--;
1641 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1642 type = MLD2_CHANGE_TO_EXCLUDE;
1643 else
1644 type = MLD2_CHANGE_TO_INCLUDE;
1645 skb = add_grec(skb, pmc, type, 0, 0);
1647 spin_unlock_bh(&pmc->mca_lock);
1649 read_unlock_bh(&idev->lock);
1650 if (!skb)
1651 return;
1652 (void) mld_sendpack(skb);
1655 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1657 struct sock *sk = igmp6_socket->sk;
1658 struct inet6_dev *idev;
1659 struct sk_buff *skb;
1660 struct icmp6hdr *hdr;
1661 struct in6_addr *snd_addr;
1662 struct in6_addr *addrp;
1663 struct in6_addr addr_buf;
1664 struct in6_addr all_routers;
1665 int err, len, payload_len, full_len;
1666 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1667 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1668 IPV6_TLV_PADN, 0 };
1670 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1671 snd_addr = addr;
1672 if (type == ICMPV6_MGM_REDUCTION) {
1673 snd_addr = &all_routers;
1674 ipv6_addr_all_routers(&all_routers);
1677 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1678 payload_len = len + sizeof(ra);
1679 full_len = sizeof(struct ipv6hdr) + payload_len;
1681 skb = sock_alloc_send_skb(sk, LL_RESERVED_SPACE(dev) + full_len, 1, &err);
1683 if (skb == NULL) {
1684 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1685 return;
1688 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1690 if (ipv6_get_lladdr(dev, &addr_buf)) {
1691 /* <draft-ietf-magma-mld-source-05.txt>:
1692 * use unspecified address as the source address
1693 * when a valid link-local address is not available.
1695 memset(&addr_buf, 0, sizeof(addr_buf));
1698 ip6_nd_hdr(sk, skb, dev, &addr_buf, snd_addr, NEXTHDR_HOP, payload_len);
1700 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1702 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1703 memset(hdr, 0, sizeof(struct icmp6hdr));
1704 hdr->icmp6_type = type;
1706 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1707 ipv6_addr_copy(addrp, addr);
1709 hdr->icmp6_cksum = csum_ipv6_magic(&addr_buf, snd_addr, len,
1710 IPPROTO_ICMPV6,
1711 csum_partial((__u8 *) hdr, len, 0));
1713 idev = in6_dev_get(skb->dev);
1715 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1716 mld_dev_queue_xmit);
1717 if (!err) {
1718 if (type == ICMPV6_MGM_REDUCTION)
1719 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBREDUCTIONS);
1720 else
1721 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBRESPONSES);
1722 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
1723 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1724 } else
1725 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1727 if (likely(idev != NULL))
1728 in6_dev_put(idev);
1729 return;
1732 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1733 struct in6_addr *psfsrc)
1735 struct ip6_sf_list *psf, *psf_prev;
1736 int rv = 0;
1738 psf_prev = NULL;
1739 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1740 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1741 break;
1742 psf_prev = psf;
1744 if (!psf || psf->sf_count[sfmode] == 0) {
1745 /* source filter not found, or count wrong => bug */
1746 return -ESRCH;
1748 psf->sf_count[sfmode]--;
1749 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1750 struct inet6_dev *idev = pmc->idev;
1752 /* no more filters for this source */
1753 if (psf_prev)
1754 psf_prev->sf_next = psf->sf_next;
1755 else
1756 pmc->mca_sources = psf->sf_next;
1757 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1758 !MLD_V1_SEEN(idev)) {
1759 psf->sf_crcount = idev->mc_qrv;
1760 psf->sf_next = pmc->mca_tomb;
1761 pmc->mca_tomb = psf;
1762 rv = 1;
1763 } else
1764 kfree(psf);
1766 return rv;
1769 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1770 int sfmode, int sfcount, struct in6_addr *psfsrc,
1771 int delta)
1773 struct ifmcaddr6 *pmc;
1774 int changerec = 0;
1775 int i, err;
1777 if (!idev)
1778 return -ENODEV;
1779 read_lock_bh(&idev->lock);
1780 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1781 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1782 break;
1784 if (!pmc) {
1785 /* MCA not found?? bug */
1786 read_unlock_bh(&idev->lock);
1787 return -ESRCH;
1789 spin_lock_bh(&pmc->mca_lock);
1790 sf_markstate(pmc);
1791 if (!delta) {
1792 if (!pmc->mca_sfcount[sfmode]) {
1793 spin_unlock_bh(&pmc->mca_lock);
1794 read_unlock_bh(&idev->lock);
1795 return -EINVAL;
1797 pmc->mca_sfcount[sfmode]--;
1799 err = 0;
1800 for (i=0; i<sfcount; i++) {
1801 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1803 changerec |= rv > 0;
1804 if (!err && rv < 0)
1805 err = rv;
1807 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1808 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1809 pmc->mca_sfcount[MCAST_INCLUDE]) {
1810 struct ip6_sf_list *psf;
1812 /* filter mode change */
1813 pmc->mca_sfmode = MCAST_INCLUDE;
1814 pmc->mca_crcount = idev->mc_qrv;
1815 idev->mc_ifc_count = pmc->mca_crcount;
1816 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1817 psf->sf_crcount = 0;
1818 mld_ifc_event(pmc->idev);
1819 } else if (sf_setstate(pmc) || changerec)
1820 mld_ifc_event(pmc->idev);
1821 spin_unlock_bh(&pmc->mca_lock);
1822 read_unlock_bh(&idev->lock);
1823 return err;
1827 * Add multicast single-source filter to the interface list
1829 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1830 struct in6_addr *psfsrc, int delta)
1832 struct ip6_sf_list *psf, *psf_prev;
1834 psf_prev = NULL;
1835 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1836 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1837 break;
1838 psf_prev = psf;
1840 if (!psf) {
1841 psf = (struct ip6_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
1842 if (!psf)
1843 return -ENOBUFS;
1844 memset(psf, 0, sizeof(*psf));
1845 psf->sf_addr = *psfsrc;
1846 if (psf_prev) {
1847 psf_prev->sf_next = psf;
1848 } else
1849 pmc->mca_sources = psf;
1851 psf->sf_count[sfmode]++;
1852 return 0;
1855 static void sf_markstate(struct ifmcaddr6 *pmc)
1857 struct ip6_sf_list *psf;
1858 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1860 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1861 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1862 psf->sf_oldin = mca_xcount ==
1863 psf->sf_count[MCAST_EXCLUDE] &&
1864 !psf->sf_count[MCAST_INCLUDE];
1865 } else
1866 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1869 static int sf_setstate(struct ifmcaddr6 *pmc)
1871 struct ip6_sf_list *psf;
1872 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1873 int qrv = pmc->idev->mc_qrv;
1874 int new_in, rv;
1876 rv = 0;
1877 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1878 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1879 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1880 !psf->sf_count[MCAST_INCLUDE];
1881 } else
1882 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1883 if (new_in != psf->sf_oldin) {
1884 psf->sf_crcount = qrv;
1885 rv++;
1888 return rv;
1892 * Add multicast source filter list to the interface list
1894 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
1895 int sfmode, int sfcount, struct in6_addr *psfsrc,
1896 int delta)
1898 struct ifmcaddr6 *pmc;
1899 int isexclude;
1900 int i, err;
1902 if (!idev)
1903 return -ENODEV;
1904 read_lock_bh(&idev->lock);
1905 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1906 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1907 break;
1909 if (!pmc) {
1910 /* MCA not found?? bug */
1911 read_unlock_bh(&idev->lock);
1912 return -ESRCH;
1914 spin_lock_bh(&pmc->mca_lock);
1916 sf_markstate(pmc);
1917 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
1918 if (!delta)
1919 pmc->mca_sfcount[sfmode]++;
1920 err = 0;
1921 for (i=0; i<sfcount; i++) {
1922 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1923 if (err)
1924 break;
1926 if (err) {
1927 int j;
1929 if (!delta)
1930 pmc->mca_sfcount[sfmode]--;
1931 for (j=0; j<i; j++)
1932 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1933 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
1934 struct inet6_dev *idev = pmc->idev;
1935 struct ip6_sf_list *psf;
1937 /* filter mode change */
1938 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1939 pmc->mca_sfmode = MCAST_EXCLUDE;
1940 else if (pmc->mca_sfcount[MCAST_INCLUDE])
1941 pmc->mca_sfmode = MCAST_INCLUDE;
1942 /* else no filters; keep old mode for reports */
1944 pmc->mca_crcount = idev->mc_qrv;
1945 idev->mc_ifc_count = pmc->mca_crcount;
1946 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1947 psf->sf_crcount = 0;
1948 mld_ifc_event(idev);
1949 } else if (sf_setstate(pmc))
1950 mld_ifc_event(idev);
1951 spin_unlock_bh(&pmc->mca_lock);
1952 read_unlock_bh(&idev->lock);
1953 return err;
1956 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
1958 struct ip6_sf_list *psf, *nextpsf;
1960 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
1961 nextpsf = psf->sf_next;
1962 kfree(psf);
1964 pmc->mca_tomb = NULL;
1965 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
1966 nextpsf = psf->sf_next;
1967 kfree(psf);
1969 pmc->mca_sources = NULL;
1970 pmc->mca_sfmode = MCAST_EXCLUDE;
1971 pmc->mca_sfcount[MCAST_EXCLUDE] = 0;
1972 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
1976 static void igmp6_join_group(struct ifmcaddr6 *ma)
1978 unsigned long delay;
1980 if (ma->mca_flags & MAF_NOREPORT)
1981 return;
1983 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
1985 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
1987 spin_lock_bh(&ma->mca_lock);
1988 if (del_timer(&ma->mca_timer)) {
1989 atomic_dec(&ma->mca_refcnt);
1990 delay = ma->mca_timer.expires - jiffies;
1993 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1994 atomic_inc(&ma->mca_refcnt);
1995 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
1996 spin_unlock_bh(&ma->mca_lock);
1999 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2000 struct inet6_dev *idev)
2002 int err;
2004 if (iml->sflist == 0) {
2005 /* any-source empty exclude case */
2006 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2008 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2009 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2010 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2011 iml->sflist = NULL;
2012 return err;
2015 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2017 if (MLD_V1_SEEN(ma->idev)) {
2018 if (ma->mca_flags & MAF_LAST_REPORTER)
2019 igmp6_send(&ma->mca_addr, ma->idev->dev,
2020 ICMPV6_MGM_REDUCTION);
2021 } else {
2022 mld_add_delrec(ma->idev, ma);
2023 mld_ifc_event(ma->idev);
2027 static void mld_gq_timer_expire(unsigned long data)
2029 struct inet6_dev *idev = (struct inet6_dev *)data;
2031 idev->mc_gq_running = 0;
2032 mld_send_report(idev, NULL);
2033 __in6_dev_put(idev);
2036 static void mld_ifc_timer_expire(unsigned long data)
2038 struct inet6_dev *idev = (struct inet6_dev *)data;
2040 mld_send_cr(idev);
2041 if (idev->mc_ifc_count) {
2042 idev->mc_ifc_count--;
2043 if (idev->mc_ifc_count)
2044 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2046 __in6_dev_put(idev);
2049 static void mld_ifc_event(struct inet6_dev *idev)
2051 if (MLD_V1_SEEN(idev))
2052 return;
2053 idev->mc_ifc_count = idev->mc_qrv;
2054 mld_ifc_start_timer(idev, 1);
2058 static void igmp6_timer_handler(unsigned long data)
2060 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2062 if (MLD_V1_SEEN(ma->idev))
2063 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2064 else
2065 mld_send_report(ma->idev, ma);
2067 spin_lock(&ma->mca_lock);
2068 ma->mca_flags |= MAF_LAST_REPORTER;
2069 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2070 spin_unlock(&ma->mca_lock);
2071 ma_put(ma);
2074 /* Device going down */
2076 void ipv6_mc_down(struct inet6_dev *idev)
2078 struct ifmcaddr6 *i;
2080 /* Withdraw multicast list */
2082 read_lock_bh(&idev->lock);
2083 idev->mc_ifc_count = 0;
2084 if (del_timer(&idev->mc_ifc_timer))
2085 __in6_dev_put(idev);
2086 idev->mc_gq_running = 0;
2087 if (del_timer(&idev->mc_gq_timer))
2088 __in6_dev_put(idev);
2090 for (i = idev->mc_list; i; i=i->next)
2091 igmp6_group_dropped(i);
2092 read_unlock_bh(&idev->lock);
2094 mld_clear_delrec(idev);
2098 /* Device going up */
2100 void ipv6_mc_up(struct inet6_dev *idev)
2102 struct ifmcaddr6 *i;
2104 /* Install multicast list, except for all-nodes (already installed) */
2106 read_lock_bh(&idev->lock);
2107 for (i = idev->mc_list; i; i=i->next)
2108 igmp6_group_added(i);
2109 read_unlock_bh(&idev->lock);
2112 /* IPv6 device initialization. */
2114 void ipv6_mc_init_dev(struct inet6_dev *idev)
2116 struct in6_addr maddr;
2118 write_lock_bh(&idev->lock);
2119 rwlock_init(&idev->mc_lock);
2120 idev->mc_gq_running = 0;
2121 init_timer(&idev->mc_gq_timer);
2122 idev->mc_gq_timer.data = (unsigned long) idev;
2123 idev->mc_gq_timer.function = &mld_gq_timer_expire;
2124 idev->mc_tomb = NULL;
2125 idev->mc_ifc_count = 0;
2126 init_timer(&idev->mc_ifc_timer);
2127 idev->mc_ifc_timer.data = (unsigned long) idev;
2128 idev->mc_ifc_timer.function = &mld_ifc_timer_expire;
2129 idev->mc_qrv = MLD_QRV_DEFAULT;
2130 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2131 idev->mc_v1_seen = 0;
2132 write_unlock_bh(&idev->lock);
2134 /* Add all-nodes address. */
2135 ipv6_addr_all_nodes(&maddr);
2136 ipv6_dev_mc_inc(idev->dev, &maddr);
2140 * Device is about to be destroyed: clean up.
2143 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2145 struct ifmcaddr6 *i;
2146 struct in6_addr maddr;
2148 /* Deactivate timers */
2149 ipv6_mc_down(idev);
2151 /* Delete all-nodes address. */
2152 ipv6_addr_all_nodes(&maddr);
2154 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2155 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2156 * fail.
2158 __ipv6_dev_mc_dec(idev, &maddr);
2160 if (idev->cnf.forwarding) {
2161 ipv6_addr_all_routers(&maddr);
2162 __ipv6_dev_mc_dec(idev, &maddr);
2165 write_lock_bh(&idev->lock);
2166 while ((i = idev->mc_list) != NULL) {
2167 idev->mc_list = i->next;
2168 write_unlock_bh(&idev->lock);
2170 igmp6_group_dropped(i);
2171 ma_put(i);
2173 write_lock_bh(&idev->lock);
2175 write_unlock_bh(&idev->lock);
2178 #ifdef CONFIG_PROC_FS
2179 struct igmp6_mc_iter_state {
2180 struct net_device *dev;
2181 struct inet6_dev *idev;
2184 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2186 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2188 struct ifmcaddr6 *im = NULL;
2189 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2191 for (state->dev = dev_base, state->idev = NULL;
2192 state->dev;
2193 state->dev = state->dev->next) {
2194 struct inet6_dev *idev;
2195 idev = in6_dev_get(state->dev);
2196 if (!idev)
2197 continue;
2198 read_lock_bh(&idev->lock);
2199 im = idev->mc_list;
2200 if (im) {
2201 state->idev = idev;
2202 break;
2204 read_unlock_bh(&idev->lock);
2205 in6_dev_put(idev);
2207 return im;
2210 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2212 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2214 im = im->next;
2215 while (!im) {
2216 if (likely(state->idev != NULL)) {
2217 read_unlock_bh(&state->idev->lock);
2218 in6_dev_put(state->idev);
2220 state->dev = state->dev->next;
2221 if (!state->dev) {
2222 state->idev = NULL;
2223 break;
2225 state->idev = in6_dev_get(state->dev);
2226 if (!state->idev)
2227 continue;
2228 read_lock_bh(&state->idev->lock);
2229 im = state->idev->mc_list;
2231 return im;
2234 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2236 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2237 if (im)
2238 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2239 --pos;
2240 return pos ? NULL : im;
2243 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2245 read_lock(&dev_base_lock);
2246 return igmp6_mc_get_idx(seq, *pos);
2249 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2251 struct ifmcaddr6 *im;
2252 im = igmp6_mc_get_next(seq, v);
2253 ++*pos;
2254 return im;
2257 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2259 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2260 if (likely(state->idev != NULL)) {
2261 read_unlock_bh(&state->idev->lock);
2262 in6_dev_put(state->idev);
2263 state->idev = NULL;
2265 state->dev = NULL;
2266 read_unlock(&dev_base_lock);
2269 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2271 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2272 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2274 seq_printf(seq,
2275 "%-4d %-15s %04x%04x%04x%04x%04x%04x%04x%04x %5d %08X %ld\n",
2276 state->dev->ifindex, state->dev->name,
2277 NIP6(im->mca_addr),
2278 im->mca_users, im->mca_flags,
2279 (im->mca_flags&MAF_TIMER_RUNNING) ?
2280 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2281 return 0;
2284 static struct seq_operations igmp6_mc_seq_ops = {
2285 .start = igmp6_mc_seq_start,
2286 .next = igmp6_mc_seq_next,
2287 .stop = igmp6_mc_seq_stop,
2288 .show = igmp6_mc_seq_show,
2291 static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2293 struct seq_file *seq;
2294 int rc = -ENOMEM;
2295 struct igmp6_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2297 if (!s)
2298 goto out;
2300 rc = seq_open(file, &igmp6_mc_seq_ops);
2301 if (rc)
2302 goto out_kfree;
2304 seq = file->private_data;
2305 seq->private = s;
2306 memset(s, 0, sizeof(*s));
2307 out:
2308 return rc;
2309 out_kfree:
2310 kfree(s);
2311 goto out;
2314 static struct file_operations igmp6_mc_seq_fops = {
2315 .owner = THIS_MODULE,
2316 .open = igmp6_mc_seq_open,
2317 .read = seq_read,
2318 .llseek = seq_lseek,
2319 .release = seq_release_private,
2322 struct igmp6_mcf_iter_state {
2323 struct net_device *dev;
2324 struct inet6_dev *idev;
2325 struct ifmcaddr6 *im;
2328 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2330 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2332 struct ip6_sf_list *psf = NULL;
2333 struct ifmcaddr6 *im = NULL;
2334 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2336 for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2337 state->dev;
2338 state->dev = state->dev->next) {
2339 struct inet6_dev *idev;
2340 idev = in6_dev_get(state->dev);
2341 if (unlikely(idev == NULL))
2342 continue;
2343 read_lock_bh(&idev->lock);
2344 im = idev->mc_list;
2345 if (likely(im != NULL)) {
2346 spin_lock_bh(&im->mca_lock);
2347 psf = im->mca_sources;
2348 if (likely(psf != NULL)) {
2349 state->im = im;
2350 state->idev = idev;
2351 break;
2353 spin_unlock_bh(&im->mca_lock);
2355 read_unlock_bh(&idev->lock);
2356 in6_dev_put(idev);
2358 return psf;
2361 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2363 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2365 psf = psf->sf_next;
2366 while (!psf) {
2367 spin_unlock_bh(&state->im->mca_lock);
2368 state->im = state->im->next;
2369 while (!state->im) {
2370 if (likely(state->idev != NULL)) {
2371 read_unlock_bh(&state->idev->lock);
2372 in6_dev_put(state->idev);
2374 state->dev = state->dev->next;
2375 if (!state->dev) {
2376 state->idev = NULL;
2377 goto out;
2379 state->idev = in6_dev_get(state->dev);
2380 if (!state->idev)
2381 continue;
2382 read_lock_bh(&state->idev->lock);
2383 state->im = state->idev->mc_list;
2385 if (!state->im)
2386 break;
2387 spin_lock_bh(&state->im->mca_lock);
2388 psf = state->im->mca_sources;
2390 out:
2391 return psf;
2394 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2396 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2397 if (psf)
2398 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2399 --pos;
2400 return pos ? NULL : psf;
2403 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2405 read_lock(&dev_base_lock);
2406 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2409 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2411 struct ip6_sf_list *psf;
2412 if (v == SEQ_START_TOKEN)
2413 psf = igmp6_mcf_get_first(seq);
2414 else
2415 psf = igmp6_mcf_get_next(seq, v);
2416 ++*pos;
2417 return psf;
2420 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2422 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2423 if (likely(state->im != NULL)) {
2424 spin_unlock_bh(&state->im->mca_lock);
2425 state->im = NULL;
2427 if (likely(state->idev != NULL)) {
2428 read_unlock_bh(&state->idev->lock);
2429 in6_dev_put(state->idev);
2430 state->idev = NULL;
2432 state->dev = NULL;
2433 read_unlock(&dev_base_lock);
2436 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2438 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2439 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2441 if (v == SEQ_START_TOKEN) {
2442 seq_printf(seq,
2443 "%3s %6s "
2444 "%32s %32s %6s %6s\n", "Idx",
2445 "Device", "Multicast Address",
2446 "Source Address", "INC", "EXC");
2447 } else {
2448 seq_printf(seq,
2449 "%3d %6.6s "
2450 "%04x%04x%04x%04x%04x%04x%04x%04x "
2451 "%04x%04x%04x%04x%04x%04x%04x%04x "
2452 "%6lu %6lu\n",
2453 state->dev->ifindex, state->dev->name,
2454 NIP6(state->im->mca_addr),
2455 NIP6(psf->sf_addr),
2456 psf->sf_count[MCAST_INCLUDE],
2457 psf->sf_count[MCAST_EXCLUDE]);
2459 return 0;
2462 static struct seq_operations igmp6_mcf_seq_ops = {
2463 .start = igmp6_mcf_seq_start,
2464 .next = igmp6_mcf_seq_next,
2465 .stop = igmp6_mcf_seq_stop,
2466 .show = igmp6_mcf_seq_show,
2469 static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2471 struct seq_file *seq;
2472 int rc = -ENOMEM;
2473 struct igmp6_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2475 if (!s)
2476 goto out;
2478 rc = seq_open(file, &igmp6_mcf_seq_ops);
2479 if (rc)
2480 goto out_kfree;
2482 seq = file->private_data;
2483 seq->private = s;
2484 memset(s, 0, sizeof(*s));
2485 out:
2486 return rc;
2487 out_kfree:
2488 kfree(s);
2489 goto out;
2492 static struct file_operations igmp6_mcf_seq_fops = {
2493 .owner = THIS_MODULE,
2494 .open = igmp6_mcf_seq_open,
2495 .read = seq_read,
2496 .llseek = seq_lseek,
2497 .release = seq_release_private,
2499 #endif
2501 int __init igmp6_init(struct net_proto_family *ops)
2503 struct ipv6_pinfo *np;
2504 struct sock *sk;
2505 int err;
2507 err = sock_create_kern(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6, &igmp6_socket);
2508 if (err < 0) {
2509 printk(KERN_ERR
2510 "Failed to initialize the IGMP6 control socket (err %d).\n",
2511 err);
2512 igmp6_socket = NULL; /* For safety. */
2513 return err;
2516 sk = igmp6_socket->sk;
2517 sk->sk_allocation = GFP_ATOMIC;
2518 sk->sk_prot->unhash(sk);
2520 np = inet6_sk(sk);
2521 np->hop_limit = 1;
2523 #ifdef CONFIG_PROC_FS
2524 proc_net_fops_create("igmp6", S_IRUGO, &igmp6_mc_seq_fops);
2525 proc_net_fops_create("mcfilter6", S_IRUGO, &igmp6_mcf_seq_fops);
2526 #endif
2528 return 0;
2531 void igmp6_cleanup(void)
2533 sock_release(igmp6_socket);
2534 igmp6_socket = NULL; /* for safety */
2536 #ifdef CONFIG_PROC_FS
2537 proc_net_remove("mcfilter6");
2538 proc_net_remove("igmp6");
2539 #endif