[PATCH] aic7xxx_osm build fix
[cris-mirror.git] / net / ipv6 / mcast.c
blob393b6e6f50a9626e2894c9a5abd8dafd903e5eba
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 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
193 if (mc_lst == NULL)
194 return -ENOMEM;
196 mc_lst->next = NULL;
197 ipv6_addr_copy(&mc_lst->addr, addr);
199 if (ifindex == 0) {
200 struct rt6_info *rt;
201 rt = rt6_lookup(addr, NULL, 0, 0);
202 if (rt) {
203 dev = rt->rt6i_dev;
204 dev_hold(dev);
205 dst_release(&rt->u.dst);
207 } else
208 dev = dev_get_by_index(ifindex);
210 if (dev == NULL) {
211 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
212 return -ENODEV;
215 mc_lst->ifindex = dev->ifindex;
216 mc_lst->sfmode = MCAST_EXCLUDE;
217 mc_lst->sflist = NULL;
220 * now add/increase the group membership on the device
223 err = ipv6_dev_mc_inc(dev, addr);
225 if (err) {
226 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
227 dev_put(dev);
228 return err;
231 write_lock_bh(&ipv6_sk_mc_lock);
232 mc_lst->next = np->ipv6_mc_list;
233 np->ipv6_mc_list = mc_lst;
234 write_unlock_bh(&ipv6_sk_mc_lock);
236 dev_put(dev);
238 return 0;
242 * socket leave on multicast group
244 int ipv6_sock_mc_drop(struct sock *sk, int ifindex, struct in6_addr *addr)
246 struct ipv6_pinfo *np = inet6_sk(sk);
247 struct ipv6_mc_socklist *mc_lst, **lnk;
249 write_lock_bh(&ipv6_sk_mc_lock);
250 for (lnk = &np->ipv6_mc_list; (mc_lst = *lnk) !=NULL ; lnk = &mc_lst->next) {
251 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
252 ipv6_addr_equal(&mc_lst->addr, addr)) {
253 struct net_device *dev;
255 *lnk = mc_lst->next;
256 write_unlock_bh(&ipv6_sk_mc_lock);
258 if ((dev = dev_get_by_index(mc_lst->ifindex)) != NULL) {
259 struct inet6_dev *idev = in6_dev_get(dev);
261 if (idev) {
262 (void) ip6_mc_leave_src(sk,mc_lst,idev);
263 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
264 in6_dev_put(idev);
266 dev_put(dev);
268 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
269 return 0;
272 write_unlock_bh(&ipv6_sk_mc_lock);
274 return -ENOENT;
277 static struct inet6_dev *ip6_mc_find_dev(struct in6_addr *group, int ifindex)
279 struct net_device *dev = NULL;
280 struct inet6_dev *idev = NULL;
282 if (ifindex == 0) {
283 struct rt6_info *rt;
285 rt = rt6_lookup(group, NULL, 0, 0);
286 if (rt) {
287 dev = rt->rt6i_dev;
288 dev_hold(dev);
289 dst_release(&rt->u.dst);
291 } else
292 dev = dev_get_by_index(ifindex);
294 if (!dev)
295 return NULL;
296 idev = in6_dev_get(dev);
297 if (!idev) {
298 dev_put(dev);
299 return NULL;
301 read_lock_bh(&idev->lock);
302 if (idev->dead) {
303 read_unlock_bh(&idev->lock);
304 in6_dev_put(idev);
305 dev_put(dev);
306 return NULL;
308 return idev;
311 void ipv6_sock_mc_close(struct sock *sk)
313 struct ipv6_pinfo *np = inet6_sk(sk);
314 struct ipv6_mc_socklist *mc_lst;
316 write_lock_bh(&ipv6_sk_mc_lock);
317 while ((mc_lst = np->ipv6_mc_list) != NULL) {
318 struct net_device *dev;
320 np->ipv6_mc_list = mc_lst->next;
321 write_unlock_bh(&ipv6_sk_mc_lock);
323 dev = dev_get_by_index(mc_lst->ifindex);
324 if (dev) {
325 struct inet6_dev *idev = in6_dev_get(dev);
327 if (idev) {
328 (void) ip6_mc_leave_src(sk, mc_lst, idev);
329 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
330 in6_dev_put(idev);
332 dev_put(dev);
335 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
337 write_lock_bh(&ipv6_sk_mc_lock);
339 write_unlock_bh(&ipv6_sk_mc_lock);
342 int ip6_mc_source(int add, int omode, struct sock *sk,
343 struct group_source_req *pgsr)
345 struct in6_addr *source, *group;
346 struct ipv6_mc_socklist *pmc;
347 struct net_device *dev;
348 struct inet6_dev *idev;
349 struct ipv6_pinfo *inet6 = inet6_sk(sk);
350 struct ip6_sf_socklist *psl;
351 int i, j, rv;
352 int err;
354 if (pgsr->gsr_group.ss_family != AF_INET6 ||
355 pgsr->gsr_source.ss_family != AF_INET6)
356 return -EINVAL;
358 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
359 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
361 if (!ipv6_addr_is_multicast(group))
362 return -EINVAL;
364 idev = ip6_mc_find_dev(group, pgsr->gsr_interface);
365 if (!idev)
366 return -ENODEV;
367 dev = idev->dev;
369 err = -EADDRNOTAVAIL;
371 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
372 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
373 continue;
374 if (ipv6_addr_equal(&pmc->addr, group))
375 break;
377 if (!pmc) /* must have a prior join */
378 goto done;
379 /* if a source filter was set, must be the same mode as before */
380 if (pmc->sflist) {
381 if (pmc->sfmode != omode)
382 goto done;
383 } else if (pmc->sfmode != omode) {
384 /* allow mode switches for empty-set filters */
385 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
386 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
387 pmc->sfmode = omode;
390 psl = pmc->sflist;
391 if (!add) {
392 if (!psl)
393 goto done;
394 rv = !0;
395 for (i=0; i<psl->sl_count; i++) {
396 rv = memcmp(&psl->sl_addr[i], source,
397 sizeof(struct in6_addr));
398 if (rv == 0)
399 break;
401 if (rv) /* source not found */
402 goto done;
404 /* update the interface filter */
405 ip6_mc_del_src(idev, group, omode, 1, source, 1);
407 for (j=i+1; j<psl->sl_count; j++)
408 psl->sl_addr[j-1] = psl->sl_addr[j];
409 psl->sl_count--;
410 err = 0;
411 goto done;
413 /* else, add a new source to the filter */
415 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
416 err = -ENOBUFS;
417 goto done;
419 if (!psl || psl->sl_count == psl->sl_max) {
420 struct ip6_sf_socklist *newpsl;
421 int count = IP6_SFBLOCK;
423 if (psl)
424 count += psl->sl_max;
425 newpsl = (struct ip6_sf_socklist *)sock_kmalloc(sk,
426 IP6_SFLSIZE(count), GFP_ATOMIC);
427 if (!newpsl) {
428 err = -ENOBUFS;
429 goto done;
431 newpsl->sl_max = count;
432 newpsl->sl_count = count - IP6_SFBLOCK;
433 if (psl) {
434 for (i=0; i<psl->sl_count; i++)
435 newpsl->sl_addr[i] = psl->sl_addr[i];
436 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
438 pmc->sflist = psl = newpsl;
440 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
441 for (i=0; i<psl->sl_count; i++) {
442 rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
443 if (rv == 0)
444 break;
446 if (rv == 0) /* address already there is an error */
447 goto done;
448 for (j=psl->sl_count-1; j>=i; j--)
449 psl->sl_addr[j+1] = psl->sl_addr[j];
450 psl->sl_addr[i] = *source;
451 psl->sl_count++;
452 err = 0;
453 /* update the interface list */
454 ip6_mc_add_src(idev, group, omode, 1, source, 1);
455 done:
456 read_unlock_bh(&idev->lock);
457 in6_dev_put(idev);
458 dev_put(dev);
459 return err;
462 int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
464 struct in6_addr *group;
465 struct ipv6_mc_socklist *pmc;
466 struct net_device *dev;
467 struct inet6_dev *idev;
468 struct ipv6_pinfo *inet6 = inet6_sk(sk);
469 struct ip6_sf_socklist *newpsl, *psl;
470 int i, err;
472 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
474 if (!ipv6_addr_is_multicast(group))
475 return -EINVAL;
476 if (gsf->gf_fmode != MCAST_INCLUDE &&
477 gsf->gf_fmode != MCAST_EXCLUDE)
478 return -EINVAL;
480 idev = ip6_mc_find_dev(group, gsf->gf_interface);
482 if (!idev)
483 return -ENODEV;
484 dev = idev->dev;
485 err = -EADDRNOTAVAIL;
487 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
488 if (pmc->ifindex != gsf->gf_interface)
489 continue;
490 if (ipv6_addr_equal(&pmc->addr, group))
491 break;
493 if (!pmc) /* must have a prior join */
494 goto done;
495 if (gsf->gf_numsrc) {
496 newpsl = (struct ip6_sf_socklist *)sock_kmalloc(sk,
497 IP6_SFLSIZE(gsf->gf_numsrc), GFP_ATOMIC);
498 if (!newpsl) {
499 err = -ENOBUFS;
500 goto done;
502 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
503 for (i=0; i<newpsl->sl_count; ++i) {
504 struct sockaddr_in6 *psin6;
506 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
507 newpsl->sl_addr[i] = psin6->sin6_addr;
509 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
510 newpsl->sl_count, newpsl->sl_addr, 0);
511 if (err) {
512 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
513 goto done;
515 } else
516 newpsl = NULL;
517 psl = pmc->sflist;
518 if (psl) {
519 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
520 psl->sl_count, psl->sl_addr, 0);
521 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
522 } else
523 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
524 pmc->sflist = newpsl;
525 pmc->sfmode = gsf->gf_fmode;
526 done:
527 read_unlock_bh(&idev->lock);
528 in6_dev_put(idev);
529 dev_put(dev);
530 return err;
533 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
534 struct group_filter __user *optval, int __user *optlen)
536 int err, i, count, copycount;
537 struct in6_addr *group;
538 struct ipv6_mc_socklist *pmc;
539 struct inet6_dev *idev;
540 struct net_device *dev;
541 struct ipv6_pinfo *inet6 = inet6_sk(sk);
542 struct ip6_sf_socklist *psl;
544 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
546 if (!ipv6_addr_is_multicast(group))
547 return -EINVAL;
549 idev = ip6_mc_find_dev(group, gsf->gf_interface);
551 if (!idev)
552 return -ENODEV;
554 dev = idev->dev;
556 err = -EADDRNOTAVAIL;
558 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
559 if (pmc->ifindex != gsf->gf_interface)
560 continue;
561 if (ipv6_addr_equal(group, &pmc->addr))
562 break;
564 if (!pmc) /* must have a prior join */
565 goto done;
566 gsf->gf_fmode = pmc->sfmode;
567 psl = pmc->sflist;
568 count = psl ? psl->sl_count : 0;
569 read_unlock_bh(&idev->lock);
570 in6_dev_put(idev);
571 dev_put(dev);
573 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
574 gsf->gf_numsrc = count;
575 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
576 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
577 return -EFAULT;
579 for (i=0; i<copycount; i++) {
580 struct sockaddr_in6 *psin6;
581 struct sockaddr_storage ss;
583 psin6 = (struct sockaddr_in6 *)&ss;
584 memset(&ss, 0, sizeof(ss));
585 psin6->sin6_family = AF_INET6;
586 psin6->sin6_addr = psl->sl_addr[i];
587 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
588 return -EFAULT;
590 return 0;
591 done:
592 read_unlock_bh(&idev->lock);
593 in6_dev_put(idev);
594 dev_put(dev);
595 return err;
598 int inet6_mc_check(struct sock *sk, struct in6_addr *mc_addr,
599 struct in6_addr *src_addr)
601 struct ipv6_pinfo *np = inet6_sk(sk);
602 struct ipv6_mc_socklist *mc;
603 struct ip6_sf_socklist *psl;
604 int rv = 1;
606 read_lock(&ipv6_sk_mc_lock);
607 for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
608 if (ipv6_addr_equal(&mc->addr, mc_addr))
609 break;
611 if (!mc) {
612 read_unlock(&ipv6_sk_mc_lock);
613 return 1;
615 psl = mc->sflist;
616 if (!psl) {
617 rv = mc->sfmode == MCAST_EXCLUDE;
618 } else {
619 int i;
621 for (i=0; i<psl->sl_count; i++) {
622 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
623 break;
625 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
626 rv = 0;
627 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
628 rv = 0;
630 read_unlock(&ipv6_sk_mc_lock);
632 return rv;
635 static void ma_put(struct ifmcaddr6 *mc)
637 if (atomic_dec_and_test(&mc->mca_refcnt)) {
638 in6_dev_put(mc->idev);
639 kfree(mc);
643 static void igmp6_group_added(struct ifmcaddr6 *mc)
645 struct net_device *dev = mc->idev->dev;
646 char buf[MAX_ADDR_LEN];
648 spin_lock_bh(&mc->mca_lock);
649 if (!(mc->mca_flags&MAF_LOADED)) {
650 mc->mca_flags |= MAF_LOADED;
651 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
652 dev_mc_add(dev, buf, dev->addr_len, 0);
654 spin_unlock_bh(&mc->mca_lock);
656 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
657 return;
659 if (MLD_V1_SEEN(mc->idev)) {
660 igmp6_join_group(mc);
661 return;
663 /* else v2 */
665 mc->mca_crcount = mc->idev->mc_qrv;
666 mld_ifc_event(mc->idev);
669 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
671 struct net_device *dev = mc->idev->dev;
672 char buf[MAX_ADDR_LEN];
674 spin_lock_bh(&mc->mca_lock);
675 if (mc->mca_flags&MAF_LOADED) {
676 mc->mca_flags &= ~MAF_LOADED;
677 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
678 dev_mc_delete(dev, buf, dev->addr_len, 0);
681 if (mc->mca_flags & MAF_NOREPORT)
682 goto done;
683 spin_unlock_bh(&mc->mca_lock);
685 if (!mc->idev->dead)
686 igmp6_leave_group(mc);
688 spin_lock_bh(&mc->mca_lock);
689 if (del_timer(&mc->mca_timer))
690 atomic_dec(&mc->mca_refcnt);
691 done:
692 ip6_mc_clear_src(mc);
693 spin_unlock_bh(&mc->mca_lock);
697 * deleted ifmcaddr6 manipulation
699 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
701 struct ifmcaddr6 *pmc;
703 /* this is an "ifmcaddr6" for convenience; only the fields below
704 * are actually used. In particular, the refcnt and users are not
705 * used for management of the delete list. Using the same structure
706 * for deleted items allows change reports to use common code with
707 * non-deleted or query-response MCA's.
709 pmc = (struct ifmcaddr6 *)kmalloc(sizeof(*pmc), GFP_ATOMIC);
710 if (!pmc)
711 return;
712 memset(pmc, 0, sizeof(*pmc));
713 spin_lock_bh(&im->mca_lock);
714 spin_lock_init(&pmc->mca_lock);
715 pmc->idev = im->idev;
716 in6_dev_hold(idev);
717 pmc->mca_addr = im->mca_addr;
718 pmc->mca_crcount = idev->mc_qrv;
719 pmc->mca_sfmode = im->mca_sfmode;
720 if (pmc->mca_sfmode == MCAST_INCLUDE) {
721 struct ip6_sf_list *psf;
723 pmc->mca_tomb = im->mca_tomb;
724 pmc->mca_sources = im->mca_sources;
725 im->mca_tomb = im->mca_sources = NULL;
726 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
727 psf->sf_crcount = pmc->mca_crcount;
729 spin_unlock_bh(&im->mca_lock);
731 write_lock_bh(&idev->mc_lock);
732 pmc->next = idev->mc_tomb;
733 idev->mc_tomb = pmc;
734 write_unlock_bh(&idev->mc_lock);
737 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
739 struct ifmcaddr6 *pmc, *pmc_prev;
740 struct ip6_sf_list *psf, *psf_next;
742 write_lock_bh(&idev->mc_lock);
743 pmc_prev = NULL;
744 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
745 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
746 break;
747 pmc_prev = pmc;
749 if (pmc) {
750 if (pmc_prev)
751 pmc_prev->next = pmc->next;
752 else
753 idev->mc_tomb = pmc->next;
755 write_unlock_bh(&idev->mc_lock);
756 if (pmc) {
757 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
758 psf_next = psf->sf_next;
759 kfree(psf);
761 in6_dev_put(pmc->idev);
762 kfree(pmc);
766 static void mld_clear_delrec(struct inet6_dev *idev)
768 struct ifmcaddr6 *pmc, *nextpmc;
770 write_lock_bh(&idev->mc_lock);
771 pmc = idev->mc_tomb;
772 idev->mc_tomb = NULL;
773 write_unlock_bh(&idev->mc_lock);
775 for (; pmc; pmc = nextpmc) {
776 nextpmc = pmc->next;
777 ip6_mc_clear_src(pmc);
778 in6_dev_put(pmc->idev);
779 kfree(pmc);
782 /* clear dead sources, too */
783 read_lock_bh(&idev->lock);
784 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
785 struct ip6_sf_list *psf, *psf_next;
787 spin_lock_bh(&pmc->mca_lock);
788 psf = pmc->mca_tomb;
789 pmc->mca_tomb = NULL;
790 spin_unlock_bh(&pmc->mca_lock);
791 for (; psf; psf=psf_next) {
792 psf_next = psf->sf_next;
793 kfree(psf);
796 read_unlock_bh(&idev->lock);
801 * device multicast group inc (add if not found)
803 int ipv6_dev_mc_inc(struct net_device *dev, struct in6_addr *addr)
805 struct ifmcaddr6 *mc;
806 struct inet6_dev *idev;
808 idev = in6_dev_get(dev);
810 if (idev == NULL)
811 return -EINVAL;
813 write_lock_bh(&idev->lock);
814 if (idev->dead) {
815 write_unlock_bh(&idev->lock);
816 in6_dev_put(idev);
817 return -ENODEV;
820 for (mc = idev->mc_list; mc; mc = mc->next) {
821 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
822 mc->mca_users++;
823 write_unlock_bh(&idev->lock);
824 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
825 NULL, 0);
826 in6_dev_put(idev);
827 return 0;
832 * not found: create a new one.
835 mc = kmalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
837 if (mc == NULL) {
838 write_unlock_bh(&idev->lock);
839 in6_dev_put(idev);
840 return -ENOMEM;
843 memset(mc, 0, sizeof(struct ifmcaddr6));
844 init_timer(&mc->mca_timer);
845 mc->mca_timer.function = igmp6_timer_handler;
846 mc->mca_timer.data = (unsigned long) mc;
848 ipv6_addr_copy(&mc->mca_addr, addr);
849 mc->idev = idev;
850 mc->mca_users = 1;
851 /* mca_stamp should be updated upon changes */
852 mc->mca_cstamp = mc->mca_tstamp = jiffies;
853 atomic_set(&mc->mca_refcnt, 2);
854 spin_lock_init(&mc->mca_lock);
856 /* initial mode is (EX, empty) */
857 mc->mca_sfmode = MCAST_EXCLUDE;
858 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
860 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
861 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
862 mc->mca_flags |= MAF_NOREPORT;
864 mc->next = idev->mc_list;
865 idev->mc_list = mc;
866 write_unlock_bh(&idev->lock);
868 mld_del_delrec(idev, &mc->mca_addr);
869 igmp6_group_added(mc);
870 ma_put(mc);
871 return 0;
875 * device multicast group del
877 int __ipv6_dev_mc_dec(struct inet6_dev *idev, struct in6_addr *addr)
879 struct ifmcaddr6 *ma, **map;
881 write_lock_bh(&idev->lock);
882 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
883 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
884 if (--ma->mca_users == 0) {
885 *map = ma->next;
886 write_unlock_bh(&idev->lock);
888 igmp6_group_dropped(ma);
890 ma_put(ma);
891 return 0;
893 write_unlock_bh(&idev->lock);
894 return 0;
897 write_unlock_bh(&idev->lock);
899 return -ENOENT;
902 int ipv6_dev_mc_dec(struct net_device *dev, struct in6_addr *addr)
904 struct inet6_dev *idev = in6_dev_get(dev);
905 int err;
907 if (!idev)
908 return -ENODEV;
910 err = __ipv6_dev_mc_dec(idev, addr);
912 in6_dev_put(idev);
914 return err;
918 * identify MLD packets for MLD filter exceptions
920 int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
922 struct icmp6hdr *pic;
924 if (nexthdr != IPPROTO_ICMPV6)
925 return 0;
927 if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
928 return 0;
930 pic = (struct icmp6hdr *)skb->h.raw;
932 switch (pic->icmp6_type) {
933 case ICMPV6_MGM_QUERY:
934 case ICMPV6_MGM_REPORT:
935 case ICMPV6_MGM_REDUCTION:
936 case ICMPV6_MLD2_REPORT:
937 return 1;
938 default:
939 break;
941 return 0;
945 * check if the interface/address pair is valid
947 int ipv6_chk_mcast_addr(struct net_device *dev, struct in6_addr *group,
948 struct in6_addr *src_addr)
950 struct inet6_dev *idev;
951 struct ifmcaddr6 *mc;
952 int rv = 0;
954 idev = in6_dev_get(dev);
955 if (idev) {
956 read_lock_bh(&idev->lock);
957 for (mc = idev->mc_list; mc; mc=mc->next) {
958 if (ipv6_addr_equal(&mc->mca_addr, group))
959 break;
961 if (mc) {
962 if (src_addr && !ipv6_addr_any(src_addr)) {
963 struct ip6_sf_list *psf;
965 spin_lock_bh(&mc->mca_lock);
966 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
967 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
968 break;
970 if (psf)
971 rv = psf->sf_count[MCAST_INCLUDE] ||
972 psf->sf_count[MCAST_EXCLUDE] !=
973 mc->mca_sfcount[MCAST_EXCLUDE];
974 else
975 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
976 spin_unlock_bh(&mc->mca_lock);
977 } else
978 rv = 1; /* don't filter unspecified source */
980 read_unlock_bh(&idev->lock);
981 in6_dev_put(idev);
983 return rv;
986 static void mld_gq_start_timer(struct inet6_dev *idev)
988 int tv = net_random() % idev->mc_maxdelay;
990 idev->mc_gq_running = 1;
991 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
992 in6_dev_hold(idev);
995 static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
997 int tv = net_random() % delay;
999 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1000 in6_dev_hold(idev);
1004 * IGMP handling (alias multicast ICMPv6 messages)
1007 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1009 unsigned long delay = resptime;
1011 /* Do not start timer for these addresses */
1012 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1013 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1014 return;
1016 if (del_timer(&ma->mca_timer)) {
1017 atomic_dec(&ma->mca_refcnt);
1018 delay = ma->mca_timer.expires - jiffies;
1021 if (delay >= resptime) {
1022 if (resptime)
1023 delay = net_random() % resptime;
1024 else
1025 delay = 1;
1027 ma->mca_timer.expires = jiffies + delay;
1028 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1029 atomic_inc(&ma->mca_refcnt);
1030 ma->mca_flags |= MAF_TIMER_RUNNING;
1033 static void mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1034 struct in6_addr *srcs)
1036 struct ip6_sf_list *psf;
1037 int i, scount;
1039 scount = 0;
1040 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1041 if (scount == nsrcs)
1042 break;
1043 for (i=0; i<nsrcs; i++)
1044 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1045 psf->sf_gsresp = 1;
1046 scount++;
1047 break;
1052 int igmp6_event_query(struct sk_buff *skb)
1054 struct mld2_query *mlh2 = (struct mld2_query *) skb->h.raw;
1055 struct ifmcaddr6 *ma;
1056 struct in6_addr *group;
1057 unsigned long max_delay;
1058 struct inet6_dev *idev;
1059 struct icmp6hdr *hdr;
1060 int group_type;
1061 int mark = 0;
1062 int len;
1064 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1065 return -EINVAL;
1067 /* compute payload length excluding extension headers */
1068 len = ntohs(skb->nh.ipv6h->payload_len) + sizeof(struct ipv6hdr);
1069 len -= (char *)skb->h.raw - (char *)skb->nh.ipv6h;
1071 /* Drop queries with not link local source */
1072 if (!(ipv6_addr_type(&skb->nh.ipv6h->saddr)&IPV6_ADDR_LINKLOCAL))
1073 return -EINVAL;
1075 idev = in6_dev_get(skb->dev);
1077 if (idev == NULL)
1078 return 0;
1080 hdr = (struct icmp6hdr *) skb->h.raw;
1081 group = (struct in6_addr *) (hdr + 1);
1082 group_type = ipv6_addr_type(group);
1084 if (group_type != IPV6_ADDR_ANY &&
1085 !(group_type&IPV6_ADDR_MULTICAST)) {
1086 in6_dev_put(idev);
1087 return -EINVAL;
1090 if (len == 24) {
1091 int switchback;
1092 /* MLDv1 router present */
1094 /* Translate milliseconds to jiffies */
1095 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
1097 switchback = (idev->mc_qrv + 1) * max_delay;
1098 idev->mc_v1_seen = jiffies + switchback;
1100 /* cancel the interface change timer */
1101 idev->mc_ifc_count = 0;
1102 if (del_timer(&idev->mc_ifc_timer))
1103 __in6_dev_put(idev);
1104 /* clear deleted report items */
1105 mld_clear_delrec(idev);
1106 } else if (len >= 28) {
1107 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
1108 if (!max_delay)
1109 max_delay = 1;
1110 idev->mc_maxdelay = max_delay;
1111 if (mlh2->qrv)
1112 idev->mc_qrv = mlh2->qrv;
1113 if (group_type == IPV6_ADDR_ANY) { /* general query */
1114 if (mlh2->nsrcs) {
1115 in6_dev_put(idev);
1116 return -EINVAL; /* no sources allowed */
1118 mld_gq_start_timer(idev);
1119 in6_dev_put(idev);
1120 return 0;
1122 /* mark sources to include, if group & source-specific */
1123 mark = mlh2->nsrcs != 0;
1124 } else {
1125 in6_dev_put(idev);
1126 return -EINVAL;
1129 read_lock_bh(&idev->lock);
1130 if (group_type == IPV6_ADDR_ANY) {
1131 for (ma = idev->mc_list; ma; ma=ma->next) {
1132 spin_lock_bh(&ma->mca_lock);
1133 igmp6_group_queried(ma, max_delay);
1134 spin_unlock_bh(&ma->mca_lock);
1136 } else {
1137 for (ma = idev->mc_list; ma; ma=ma->next) {
1138 if (group_type != IPV6_ADDR_ANY &&
1139 !ipv6_addr_equal(group, &ma->mca_addr))
1140 continue;
1141 spin_lock_bh(&ma->mca_lock);
1142 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1143 /* gsquery <- gsquery && mark */
1144 if (!mark)
1145 ma->mca_flags &= ~MAF_GSQUERY;
1146 } else {
1147 /* gsquery <- mark */
1148 if (mark)
1149 ma->mca_flags |= MAF_GSQUERY;
1150 else
1151 ma->mca_flags &= ~MAF_GSQUERY;
1153 if (ma->mca_flags & MAF_GSQUERY)
1154 mld_marksources(ma, ntohs(mlh2->nsrcs),
1155 mlh2->srcs);
1156 igmp6_group_queried(ma, max_delay);
1157 spin_unlock_bh(&ma->mca_lock);
1158 if (group_type != IPV6_ADDR_ANY)
1159 break;
1162 read_unlock_bh(&idev->lock);
1163 in6_dev_put(idev);
1165 return 0;
1169 int igmp6_event_report(struct sk_buff *skb)
1171 struct ifmcaddr6 *ma;
1172 struct in6_addr *addrp;
1173 struct inet6_dev *idev;
1174 struct icmp6hdr *hdr;
1175 int addr_type;
1177 /* Our own report looped back. Ignore it. */
1178 if (skb->pkt_type == PACKET_LOOPBACK)
1179 return 0;
1181 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1182 return -EINVAL;
1184 hdr = (struct icmp6hdr*) skb->h.raw;
1186 /* Drop reports with not link local source */
1187 addr_type = ipv6_addr_type(&skb->nh.ipv6h->saddr);
1188 if (addr_type != IPV6_ADDR_ANY &&
1189 !(addr_type&IPV6_ADDR_LINKLOCAL))
1190 return -EINVAL;
1192 addrp = (struct in6_addr *) (hdr + 1);
1194 idev = in6_dev_get(skb->dev);
1195 if (idev == NULL)
1196 return -ENODEV;
1199 * Cancel the timer for this group
1202 read_lock_bh(&idev->lock);
1203 for (ma = idev->mc_list; ma; ma=ma->next) {
1204 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1205 spin_lock(&ma->mca_lock);
1206 if (del_timer(&ma->mca_timer))
1207 atomic_dec(&ma->mca_refcnt);
1208 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1209 spin_unlock(&ma->mca_lock);
1210 break;
1213 read_unlock_bh(&idev->lock);
1214 in6_dev_put(idev);
1215 return 0;
1218 static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1219 int gdeleted, int sdeleted)
1221 switch (type) {
1222 case MLD2_MODE_IS_INCLUDE:
1223 case MLD2_MODE_IS_EXCLUDE:
1224 if (gdeleted || sdeleted)
1225 return 0;
1226 return !((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp);
1227 case MLD2_CHANGE_TO_INCLUDE:
1228 if (gdeleted || sdeleted)
1229 return 0;
1230 return psf->sf_count[MCAST_INCLUDE] != 0;
1231 case MLD2_CHANGE_TO_EXCLUDE:
1232 if (gdeleted || sdeleted)
1233 return 0;
1234 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1235 psf->sf_count[MCAST_INCLUDE])
1236 return 0;
1237 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1238 psf->sf_count[MCAST_EXCLUDE];
1239 case MLD2_ALLOW_NEW_SOURCES:
1240 if (gdeleted || !psf->sf_crcount)
1241 return 0;
1242 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1243 case MLD2_BLOCK_OLD_SOURCES:
1244 if (pmc->mca_sfmode == MCAST_INCLUDE)
1245 return gdeleted || (psf->sf_crcount && sdeleted);
1246 return psf->sf_crcount && !gdeleted && !sdeleted;
1248 return 0;
1251 static int
1252 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1254 struct ip6_sf_list *psf;
1255 int scount = 0;
1257 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1258 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1259 continue;
1260 scount++;
1262 return scount;
1265 static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1267 struct sock *sk = igmp6_socket->sk;
1268 struct sk_buff *skb;
1269 struct mld2_report *pmr;
1270 struct in6_addr addr_buf;
1271 int err;
1272 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1273 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1274 IPV6_TLV_PADN, 0 };
1276 /* we assume size > sizeof(ra) here */
1277 skb = sock_alloc_send_skb(sk, size + LL_RESERVED_SPACE(dev), 1, &err);
1279 if (skb == 0)
1280 return NULL;
1282 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1283 if (dev->hard_header) {
1284 unsigned char ha[MAX_ADDR_LEN];
1286 ndisc_mc_map(&mld2_all_mcr, ha, dev, 1);
1287 if (dev->hard_header(skb, dev, ETH_P_IPV6,ha,NULL,size) < 0) {
1288 kfree_skb(skb);
1289 return NULL;
1293 if (ipv6_get_lladdr(dev, &addr_buf)) {
1294 /* <draft-ietf-magma-mld-source-05.txt>:
1295 * use unspecified address as the source address
1296 * when a valid link-local address is not available.
1298 memset(&addr_buf, 0, sizeof(addr_buf));
1301 ip6_nd_hdr(sk, skb, dev, &addr_buf, &mld2_all_mcr, NEXTHDR_HOP, 0);
1303 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1305 pmr =(struct mld2_report *)skb_put(skb, sizeof(*pmr));
1306 skb->h.raw = (unsigned char *)pmr;
1307 pmr->type = ICMPV6_MLD2_REPORT;
1308 pmr->resv1 = 0;
1309 pmr->csum = 0;
1310 pmr->resv2 = 0;
1311 pmr->ngrec = 0;
1312 return skb;
1315 static void mld_sendpack(struct sk_buff *skb)
1317 struct ipv6hdr *pip6 = skb->nh.ipv6h;
1318 struct mld2_report *pmr = (struct mld2_report *)skb->h.raw;
1319 int payload_len, mldlen;
1320 struct inet6_dev *idev = in6_dev_get(skb->dev);
1321 int err;
1323 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1324 payload_len = skb->tail - (unsigned char *)skb->nh.ipv6h -
1325 sizeof(struct ipv6hdr);
1326 mldlen = skb->tail - skb->h.raw;
1327 pip6->payload_len = htons(payload_len);
1329 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1330 IPPROTO_ICMPV6, csum_partial(skb->h.raw, mldlen, 0));
1331 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1332 dev_queue_xmit);
1333 if (!err) {
1334 ICMP6_INC_STATS(idev,ICMP6_MIB_OUTMSGS);
1335 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1336 } else
1337 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1339 if (likely(idev != NULL))
1340 in6_dev_put(idev);
1343 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1345 return sizeof(struct mld2_grec) + 4*mld_scount(pmc,type,gdel,sdel);
1348 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1349 int type, struct mld2_grec **ppgr)
1351 struct net_device *dev = pmc->idev->dev;
1352 struct mld2_report *pmr;
1353 struct mld2_grec *pgr;
1355 if (!skb)
1356 skb = mld_newpack(dev, dev->mtu);
1357 if (!skb)
1358 return NULL;
1359 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1360 pgr->grec_type = type;
1361 pgr->grec_auxwords = 0;
1362 pgr->grec_nsrcs = 0;
1363 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1364 pmr = (struct mld2_report *)skb->h.raw;
1365 pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1366 *ppgr = pgr;
1367 return skb;
1370 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1371 skb_tailroom(skb)) : 0)
1373 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1374 int type, int gdeleted, int sdeleted)
1376 struct net_device *dev = pmc->idev->dev;
1377 struct mld2_report *pmr;
1378 struct mld2_grec *pgr = NULL;
1379 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1380 int scount, first, isquery, truncate;
1382 if (pmc->mca_flags & MAF_NOREPORT)
1383 return skb;
1385 isquery = type == MLD2_MODE_IS_INCLUDE ||
1386 type == MLD2_MODE_IS_EXCLUDE;
1387 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1388 type == MLD2_CHANGE_TO_EXCLUDE;
1390 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1392 if (!*psf_list) {
1393 if (type == MLD2_ALLOW_NEW_SOURCES ||
1394 type == MLD2_BLOCK_OLD_SOURCES)
1395 return skb;
1396 if (pmc->mca_crcount || isquery) {
1397 /* make sure we have room for group header and at
1398 * least one source.
1400 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)+
1401 sizeof(struct in6_addr)) {
1402 mld_sendpack(skb);
1403 skb = NULL; /* add_grhead will get a new one */
1405 skb = add_grhead(skb, pmc, type, &pgr);
1407 return skb;
1409 pmr = skb ? (struct mld2_report *)skb->h.raw : NULL;
1411 /* EX and TO_EX get a fresh packet, if needed */
1412 if (truncate) {
1413 if (pmr && pmr->ngrec &&
1414 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1415 if (skb)
1416 mld_sendpack(skb);
1417 skb = mld_newpack(dev, dev->mtu);
1420 first = 1;
1421 scount = 0;
1422 psf_prev = NULL;
1423 for (psf=*psf_list; psf; psf=psf_next) {
1424 struct in6_addr *psrc;
1426 psf_next = psf->sf_next;
1428 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1429 psf_prev = psf;
1430 continue;
1433 /* clear marks on query responses */
1434 if (isquery)
1435 psf->sf_gsresp = 0;
1437 if (AVAILABLE(skb) < sizeof(*psrc) +
1438 first*sizeof(struct mld2_grec)) {
1439 if (truncate && !first)
1440 break; /* truncate these */
1441 if (pgr)
1442 pgr->grec_nsrcs = htons(scount);
1443 if (skb)
1444 mld_sendpack(skb);
1445 skb = mld_newpack(dev, dev->mtu);
1446 first = 1;
1447 scount = 0;
1449 if (first) {
1450 skb = add_grhead(skb, pmc, type, &pgr);
1451 first = 0;
1453 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1454 *psrc = psf->sf_addr;
1455 scount++;
1456 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1457 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1458 psf->sf_crcount--;
1459 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1460 if (psf_prev)
1461 psf_prev->sf_next = psf->sf_next;
1462 else
1463 *psf_list = psf->sf_next;
1464 kfree(psf);
1465 continue;
1468 psf_prev = psf;
1470 if (pgr)
1471 pgr->grec_nsrcs = htons(scount);
1473 if (isquery)
1474 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1475 return skb;
1478 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1480 struct sk_buff *skb = NULL;
1481 int type;
1483 if (!pmc) {
1484 read_lock_bh(&idev->lock);
1485 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1486 if (pmc->mca_flags & MAF_NOREPORT)
1487 continue;
1488 spin_lock_bh(&pmc->mca_lock);
1489 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1490 type = MLD2_MODE_IS_EXCLUDE;
1491 else
1492 type = MLD2_MODE_IS_INCLUDE;
1493 skb = add_grec(skb, pmc, type, 0, 0);
1494 spin_unlock_bh(&pmc->mca_lock);
1496 read_unlock_bh(&idev->lock);
1497 } else {
1498 spin_lock_bh(&pmc->mca_lock);
1499 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1500 type = MLD2_MODE_IS_EXCLUDE;
1501 else
1502 type = MLD2_MODE_IS_INCLUDE;
1503 skb = add_grec(skb, pmc, type, 0, 0);
1504 spin_unlock_bh(&pmc->mca_lock);
1506 if (skb)
1507 mld_sendpack(skb);
1511 * remove zero-count source records from a source filter list
1513 static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1515 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1517 psf_prev = NULL;
1518 for (psf=*ppsf; psf; psf = psf_next) {
1519 psf_next = psf->sf_next;
1520 if (psf->sf_crcount == 0) {
1521 if (psf_prev)
1522 psf_prev->sf_next = psf->sf_next;
1523 else
1524 *ppsf = psf->sf_next;
1525 kfree(psf);
1526 } else
1527 psf_prev = psf;
1531 static void mld_send_cr(struct inet6_dev *idev)
1533 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1534 struct sk_buff *skb = NULL;
1535 int type, dtype;
1537 read_lock_bh(&idev->lock);
1538 write_lock_bh(&idev->mc_lock);
1540 /* deleted MCA's */
1541 pmc_prev = NULL;
1542 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1543 pmc_next = pmc->next;
1544 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1545 type = MLD2_BLOCK_OLD_SOURCES;
1546 dtype = MLD2_BLOCK_OLD_SOURCES;
1547 skb = add_grec(skb, pmc, type, 1, 0);
1548 skb = add_grec(skb, pmc, dtype, 1, 1);
1550 if (pmc->mca_crcount) {
1551 pmc->mca_crcount--;
1552 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1553 type = MLD2_CHANGE_TO_INCLUDE;
1554 skb = add_grec(skb, pmc, type, 1, 0);
1556 if (pmc->mca_crcount == 0) {
1557 mld_clear_zeros(&pmc->mca_tomb);
1558 mld_clear_zeros(&pmc->mca_sources);
1561 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1562 !pmc->mca_sources) {
1563 if (pmc_prev)
1564 pmc_prev->next = pmc_next;
1565 else
1566 idev->mc_tomb = pmc_next;
1567 in6_dev_put(pmc->idev);
1568 kfree(pmc);
1569 } else
1570 pmc_prev = pmc;
1572 write_unlock_bh(&idev->mc_lock);
1574 /* change recs */
1575 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1576 spin_lock_bh(&pmc->mca_lock);
1577 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1578 type = MLD2_BLOCK_OLD_SOURCES;
1579 dtype = MLD2_ALLOW_NEW_SOURCES;
1580 } else {
1581 type = MLD2_ALLOW_NEW_SOURCES;
1582 dtype = MLD2_BLOCK_OLD_SOURCES;
1584 skb = add_grec(skb, pmc, type, 0, 0);
1585 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1587 /* filter mode changes */
1588 if (pmc->mca_crcount) {
1589 pmc->mca_crcount--;
1590 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1591 type = MLD2_CHANGE_TO_EXCLUDE;
1592 else
1593 type = MLD2_CHANGE_TO_INCLUDE;
1594 skb = add_grec(skb, pmc, type, 0, 0);
1596 spin_unlock_bh(&pmc->mca_lock);
1598 read_unlock_bh(&idev->lock);
1599 if (!skb)
1600 return;
1601 (void) mld_sendpack(skb);
1604 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1606 struct sock *sk = igmp6_socket->sk;
1607 struct inet6_dev *idev;
1608 struct sk_buff *skb;
1609 struct icmp6hdr *hdr;
1610 struct in6_addr *snd_addr;
1611 struct in6_addr *addrp;
1612 struct in6_addr addr_buf;
1613 struct in6_addr all_routers;
1614 int err, len, payload_len, full_len;
1615 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1616 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1617 IPV6_TLV_PADN, 0 };
1619 IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
1620 snd_addr = addr;
1621 if (type == ICMPV6_MGM_REDUCTION) {
1622 snd_addr = &all_routers;
1623 ipv6_addr_all_routers(&all_routers);
1626 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1627 payload_len = len + sizeof(ra);
1628 full_len = sizeof(struct ipv6hdr) + payload_len;
1630 skb = sock_alloc_send_skb(sk, LL_RESERVED_SPACE(dev) + full_len, 1, &err);
1632 if (skb == NULL) {
1633 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1634 return;
1637 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1638 if (dev->hard_header) {
1639 unsigned char ha[MAX_ADDR_LEN];
1640 ndisc_mc_map(snd_addr, ha, dev, 1);
1641 if (dev->hard_header(skb, dev, ETH_P_IPV6, ha, NULL, full_len) < 0)
1642 goto out;
1645 if (ipv6_get_lladdr(dev, &addr_buf)) {
1646 /* <draft-ietf-magma-mld-source-05.txt>:
1647 * use unspecified address as the source address
1648 * when a valid link-local address is not available.
1650 memset(&addr_buf, 0, sizeof(addr_buf));
1653 ip6_nd_hdr(sk, skb, dev, &addr_buf, snd_addr, NEXTHDR_HOP, payload_len);
1655 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1657 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1658 memset(hdr, 0, sizeof(struct icmp6hdr));
1659 hdr->icmp6_type = type;
1661 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1662 ipv6_addr_copy(addrp, addr);
1664 hdr->icmp6_cksum = csum_ipv6_magic(&addr_buf, snd_addr, len,
1665 IPPROTO_ICMPV6,
1666 csum_partial((__u8 *) hdr, len, 0));
1668 idev = in6_dev_get(skb->dev);
1670 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dev,
1671 dev_queue_xmit);
1672 if (!err) {
1673 if (type == ICMPV6_MGM_REDUCTION)
1674 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBREDUCTIONS);
1675 else
1676 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTGROUPMEMBRESPONSES);
1677 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
1678 IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
1679 } else
1680 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1682 if (likely(idev != NULL))
1683 in6_dev_put(idev);
1684 return;
1686 out:
1687 IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
1688 kfree_skb(skb);
1691 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1692 struct in6_addr *psfsrc)
1694 struct ip6_sf_list *psf, *psf_prev;
1695 int rv = 0;
1697 psf_prev = NULL;
1698 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1699 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1700 break;
1701 psf_prev = psf;
1703 if (!psf || psf->sf_count[sfmode] == 0) {
1704 /* source filter not found, or count wrong => bug */
1705 return -ESRCH;
1707 psf->sf_count[sfmode]--;
1708 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1709 struct inet6_dev *idev = pmc->idev;
1711 /* no more filters for this source */
1712 if (psf_prev)
1713 psf_prev->sf_next = psf->sf_next;
1714 else
1715 pmc->mca_sources = psf->sf_next;
1716 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1717 !MLD_V1_SEEN(idev)) {
1718 psf->sf_crcount = idev->mc_qrv;
1719 psf->sf_next = pmc->mca_tomb;
1720 pmc->mca_tomb = psf;
1721 rv = 1;
1722 } else
1723 kfree(psf);
1725 return rv;
1728 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1729 int sfmode, int sfcount, struct in6_addr *psfsrc,
1730 int delta)
1732 struct ifmcaddr6 *pmc;
1733 int changerec = 0;
1734 int i, err;
1736 if (!idev)
1737 return -ENODEV;
1738 read_lock_bh(&idev->lock);
1739 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1740 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1741 break;
1743 if (!pmc) {
1744 /* MCA not found?? bug */
1745 read_unlock_bh(&idev->lock);
1746 return -ESRCH;
1748 spin_lock_bh(&pmc->mca_lock);
1749 sf_markstate(pmc);
1750 if (!delta) {
1751 if (!pmc->mca_sfcount[sfmode]) {
1752 spin_unlock_bh(&pmc->mca_lock);
1753 read_unlock_bh(&idev->lock);
1754 return -EINVAL;
1756 pmc->mca_sfcount[sfmode]--;
1758 err = 0;
1759 for (i=0; i<sfcount; i++) {
1760 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1762 changerec |= rv > 0;
1763 if (!err && rv < 0)
1764 err = rv;
1766 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1767 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1768 pmc->mca_sfcount[MCAST_INCLUDE]) {
1769 struct ip6_sf_list *psf;
1771 /* filter mode change */
1772 pmc->mca_sfmode = MCAST_INCLUDE;
1773 pmc->mca_crcount = idev->mc_qrv;
1774 idev->mc_ifc_count = pmc->mca_crcount;
1775 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1776 psf->sf_crcount = 0;
1777 mld_ifc_event(pmc->idev);
1778 } else if (sf_setstate(pmc) || changerec)
1779 mld_ifc_event(pmc->idev);
1780 spin_unlock_bh(&pmc->mca_lock);
1781 read_unlock_bh(&idev->lock);
1782 return err;
1786 * Add multicast single-source filter to the interface list
1788 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1789 struct in6_addr *psfsrc, int delta)
1791 struct ip6_sf_list *psf, *psf_prev;
1793 psf_prev = NULL;
1794 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1795 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1796 break;
1797 psf_prev = psf;
1799 if (!psf) {
1800 psf = (struct ip6_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
1801 if (!psf)
1802 return -ENOBUFS;
1803 memset(psf, 0, sizeof(*psf));
1804 psf->sf_addr = *psfsrc;
1805 if (psf_prev) {
1806 psf_prev->sf_next = psf;
1807 } else
1808 pmc->mca_sources = psf;
1810 psf->sf_count[sfmode]++;
1811 return 0;
1814 static void sf_markstate(struct ifmcaddr6 *pmc)
1816 struct ip6_sf_list *psf;
1817 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1819 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1820 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1821 psf->sf_oldin = mca_xcount ==
1822 psf->sf_count[MCAST_EXCLUDE] &&
1823 !psf->sf_count[MCAST_INCLUDE];
1824 } else
1825 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1828 static int sf_setstate(struct ifmcaddr6 *pmc)
1830 struct ip6_sf_list *psf;
1831 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1832 int qrv = pmc->idev->mc_qrv;
1833 int new_in, rv;
1835 rv = 0;
1836 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1837 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1838 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1839 !psf->sf_count[MCAST_INCLUDE];
1840 } else
1841 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1842 if (new_in != psf->sf_oldin) {
1843 psf->sf_crcount = qrv;
1844 rv++;
1847 return rv;
1851 * Add multicast source filter list to the interface list
1853 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
1854 int sfmode, int sfcount, struct in6_addr *psfsrc,
1855 int delta)
1857 struct ifmcaddr6 *pmc;
1858 int isexclude;
1859 int i, err;
1861 if (!idev)
1862 return -ENODEV;
1863 read_lock_bh(&idev->lock);
1864 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1865 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1866 break;
1868 if (!pmc) {
1869 /* MCA not found?? bug */
1870 read_unlock_bh(&idev->lock);
1871 return -ESRCH;
1873 spin_lock_bh(&pmc->mca_lock);
1875 sf_markstate(pmc);
1876 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
1877 if (!delta)
1878 pmc->mca_sfcount[sfmode]++;
1879 err = 0;
1880 for (i=0; i<sfcount; i++) {
1881 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1882 if (err)
1883 break;
1885 if (err) {
1886 int j;
1888 if (!delta)
1889 pmc->mca_sfcount[sfmode]--;
1890 for (j=0; j<i; j++)
1891 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1892 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
1893 struct inet6_dev *idev = pmc->idev;
1894 struct ip6_sf_list *psf;
1896 /* filter mode change */
1897 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1898 pmc->mca_sfmode = MCAST_EXCLUDE;
1899 else if (pmc->mca_sfcount[MCAST_INCLUDE])
1900 pmc->mca_sfmode = MCAST_INCLUDE;
1901 /* else no filters; keep old mode for reports */
1903 pmc->mca_crcount = idev->mc_qrv;
1904 idev->mc_ifc_count = pmc->mca_crcount;
1905 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1906 psf->sf_crcount = 0;
1907 mld_ifc_event(idev);
1908 } else if (sf_setstate(pmc))
1909 mld_ifc_event(idev);
1910 spin_unlock_bh(&pmc->mca_lock);
1911 read_unlock_bh(&idev->lock);
1912 return err;
1915 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
1917 struct ip6_sf_list *psf, *nextpsf;
1919 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
1920 nextpsf = psf->sf_next;
1921 kfree(psf);
1923 pmc->mca_tomb = NULL;
1924 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
1925 nextpsf = psf->sf_next;
1926 kfree(psf);
1928 pmc->mca_sources = NULL;
1929 pmc->mca_sfmode = MCAST_EXCLUDE;
1930 pmc->mca_sfcount[MCAST_EXCLUDE] = 0;
1931 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
1935 static void igmp6_join_group(struct ifmcaddr6 *ma)
1937 unsigned long delay;
1939 if (ma->mca_flags & MAF_NOREPORT)
1940 return;
1942 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
1944 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
1946 spin_lock_bh(&ma->mca_lock);
1947 if (del_timer(&ma->mca_timer)) {
1948 atomic_dec(&ma->mca_refcnt);
1949 delay = ma->mca_timer.expires - jiffies;
1952 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1953 atomic_inc(&ma->mca_refcnt);
1954 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
1955 spin_unlock_bh(&ma->mca_lock);
1958 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
1959 struct inet6_dev *idev)
1961 int err;
1963 if (iml->sflist == 0) {
1964 /* any-source empty exclude case */
1965 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
1967 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
1968 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
1969 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
1970 iml->sflist = NULL;
1971 return err;
1974 static void igmp6_leave_group(struct ifmcaddr6 *ma)
1976 if (MLD_V1_SEEN(ma->idev)) {
1977 if (ma->mca_flags & MAF_LAST_REPORTER)
1978 igmp6_send(&ma->mca_addr, ma->idev->dev,
1979 ICMPV6_MGM_REDUCTION);
1980 } else {
1981 mld_add_delrec(ma->idev, ma);
1982 mld_ifc_event(ma->idev);
1986 static void mld_gq_timer_expire(unsigned long data)
1988 struct inet6_dev *idev = (struct inet6_dev *)data;
1990 idev->mc_gq_running = 0;
1991 mld_send_report(idev, NULL);
1992 __in6_dev_put(idev);
1995 static void mld_ifc_timer_expire(unsigned long data)
1997 struct inet6_dev *idev = (struct inet6_dev *)data;
1999 mld_send_cr(idev);
2000 if (idev->mc_ifc_count) {
2001 idev->mc_ifc_count--;
2002 if (idev->mc_ifc_count)
2003 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2005 __in6_dev_put(idev);
2008 static void mld_ifc_event(struct inet6_dev *idev)
2010 if (MLD_V1_SEEN(idev))
2011 return;
2012 idev->mc_ifc_count = idev->mc_qrv;
2013 mld_ifc_start_timer(idev, 1);
2017 static void igmp6_timer_handler(unsigned long data)
2019 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2021 if (MLD_V1_SEEN(ma->idev))
2022 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2023 else
2024 mld_send_report(ma->idev, ma);
2026 spin_lock(&ma->mca_lock);
2027 ma->mca_flags |= MAF_LAST_REPORTER;
2028 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2029 spin_unlock(&ma->mca_lock);
2030 ma_put(ma);
2033 /* Device going down */
2035 void ipv6_mc_down(struct inet6_dev *idev)
2037 struct ifmcaddr6 *i;
2039 /* Withdraw multicast list */
2041 read_lock_bh(&idev->lock);
2042 idev->mc_ifc_count = 0;
2043 if (del_timer(&idev->mc_ifc_timer))
2044 __in6_dev_put(idev);
2045 idev->mc_gq_running = 0;
2046 if (del_timer(&idev->mc_gq_timer))
2047 __in6_dev_put(idev);
2049 for (i = idev->mc_list; i; i=i->next)
2050 igmp6_group_dropped(i);
2051 read_unlock_bh(&idev->lock);
2053 mld_clear_delrec(idev);
2057 /* Device going up */
2059 void ipv6_mc_up(struct inet6_dev *idev)
2061 struct ifmcaddr6 *i;
2063 /* Install multicast list, except for all-nodes (already installed) */
2065 read_lock_bh(&idev->lock);
2066 for (i = idev->mc_list; i; i=i->next)
2067 igmp6_group_added(i);
2068 read_unlock_bh(&idev->lock);
2071 /* IPv6 device initialization. */
2073 void ipv6_mc_init_dev(struct inet6_dev *idev)
2075 struct in6_addr maddr;
2077 write_lock_bh(&idev->lock);
2078 rwlock_init(&idev->mc_lock);
2079 idev->mc_gq_running = 0;
2080 init_timer(&idev->mc_gq_timer);
2081 idev->mc_gq_timer.data = (unsigned long) idev;
2082 idev->mc_gq_timer.function = &mld_gq_timer_expire;
2083 idev->mc_tomb = NULL;
2084 idev->mc_ifc_count = 0;
2085 init_timer(&idev->mc_ifc_timer);
2086 idev->mc_ifc_timer.data = (unsigned long) idev;
2087 idev->mc_ifc_timer.function = &mld_ifc_timer_expire;
2088 idev->mc_qrv = MLD_QRV_DEFAULT;
2089 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2090 idev->mc_v1_seen = 0;
2091 write_unlock_bh(&idev->lock);
2093 /* Add all-nodes address. */
2094 ipv6_addr_all_nodes(&maddr);
2095 ipv6_dev_mc_inc(idev->dev, &maddr);
2099 * Device is about to be destroyed: clean up.
2102 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2104 struct ifmcaddr6 *i;
2105 struct in6_addr maddr;
2107 /* Deactivate timers */
2108 ipv6_mc_down(idev);
2110 /* Delete all-nodes address. */
2111 ipv6_addr_all_nodes(&maddr);
2113 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2114 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2115 * fail.
2117 __ipv6_dev_mc_dec(idev, &maddr);
2119 if (idev->cnf.forwarding) {
2120 ipv6_addr_all_routers(&maddr);
2121 __ipv6_dev_mc_dec(idev, &maddr);
2124 write_lock_bh(&idev->lock);
2125 while ((i = idev->mc_list) != NULL) {
2126 idev->mc_list = i->next;
2127 write_unlock_bh(&idev->lock);
2129 igmp6_group_dropped(i);
2130 ma_put(i);
2132 write_lock_bh(&idev->lock);
2134 write_unlock_bh(&idev->lock);
2137 #ifdef CONFIG_PROC_FS
2138 struct igmp6_mc_iter_state {
2139 struct net_device *dev;
2140 struct inet6_dev *idev;
2143 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2145 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2147 struct ifmcaddr6 *im = NULL;
2148 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2150 for (state->dev = dev_base, state->idev = NULL;
2151 state->dev;
2152 state->dev = state->dev->next) {
2153 struct inet6_dev *idev;
2154 idev = in6_dev_get(state->dev);
2155 if (!idev)
2156 continue;
2157 read_lock_bh(&idev->lock);
2158 im = idev->mc_list;
2159 if (im) {
2160 state->idev = idev;
2161 break;
2163 read_unlock_bh(&idev->lock);
2164 in6_dev_put(idev);
2166 return im;
2169 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2171 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2173 im = im->next;
2174 while (!im) {
2175 if (likely(state->idev != NULL)) {
2176 read_unlock_bh(&state->idev->lock);
2177 in6_dev_put(state->idev);
2179 state->dev = state->dev->next;
2180 if (!state->dev) {
2181 state->idev = NULL;
2182 break;
2184 state->idev = in6_dev_get(state->dev);
2185 if (!state->idev)
2186 continue;
2187 read_lock_bh(&state->idev->lock);
2188 im = state->idev->mc_list;
2190 return im;
2193 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2195 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2196 if (im)
2197 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2198 --pos;
2199 return pos ? NULL : im;
2202 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2204 read_lock(&dev_base_lock);
2205 return igmp6_mc_get_idx(seq, *pos);
2208 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2210 struct ifmcaddr6 *im;
2211 im = igmp6_mc_get_next(seq, v);
2212 ++*pos;
2213 return im;
2216 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2218 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2219 if (likely(state->idev != NULL)) {
2220 read_unlock_bh(&state->idev->lock);
2221 in6_dev_put(state->idev);
2222 state->idev = NULL;
2224 state->dev = NULL;
2225 read_unlock(&dev_base_lock);
2228 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2230 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2231 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2233 seq_printf(seq,
2234 "%-4d %-15s %04x%04x%04x%04x%04x%04x%04x%04x %5d %08X %ld\n",
2235 state->dev->ifindex, state->dev->name,
2236 NIP6(im->mca_addr),
2237 im->mca_users, im->mca_flags,
2238 (im->mca_flags&MAF_TIMER_RUNNING) ?
2239 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2240 return 0;
2243 static struct seq_operations igmp6_mc_seq_ops = {
2244 .start = igmp6_mc_seq_start,
2245 .next = igmp6_mc_seq_next,
2246 .stop = igmp6_mc_seq_stop,
2247 .show = igmp6_mc_seq_show,
2250 static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2252 struct seq_file *seq;
2253 int rc = -ENOMEM;
2254 struct igmp6_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2256 if (!s)
2257 goto out;
2259 rc = seq_open(file, &igmp6_mc_seq_ops);
2260 if (rc)
2261 goto out_kfree;
2263 seq = file->private_data;
2264 seq->private = s;
2265 memset(s, 0, sizeof(*s));
2266 out:
2267 return rc;
2268 out_kfree:
2269 kfree(s);
2270 goto out;
2273 static struct file_operations igmp6_mc_seq_fops = {
2274 .owner = THIS_MODULE,
2275 .open = igmp6_mc_seq_open,
2276 .read = seq_read,
2277 .llseek = seq_lseek,
2278 .release = seq_release_private,
2281 struct igmp6_mcf_iter_state {
2282 struct net_device *dev;
2283 struct inet6_dev *idev;
2284 struct ifmcaddr6 *im;
2287 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2289 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2291 struct ip6_sf_list *psf = NULL;
2292 struct ifmcaddr6 *im = NULL;
2293 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2295 for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2296 state->dev;
2297 state->dev = state->dev->next) {
2298 struct inet6_dev *idev;
2299 idev = in6_dev_get(state->dev);
2300 if (unlikely(idev == NULL))
2301 continue;
2302 read_lock_bh(&idev->lock);
2303 im = idev->mc_list;
2304 if (likely(im != NULL)) {
2305 spin_lock_bh(&im->mca_lock);
2306 psf = im->mca_sources;
2307 if (likely(psf != NULL)) {
2308 state->im = im;
2309 state->idev = idev;
2310 break;
2312 spin_unlock_bh(&im->mca_lock);
2314 read_unlock_bh(&idev->lock);
2315 in6_dev_put(idev);
2317 return psf;
2320 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2322 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2324 psf = psf->sf_next;
2325 while (!psf) {
2326 spin_unlock_bh(&state->im->mca_lock);
2327 state->im = state->im->next;
2328 while (!state->im) {
2329 if (likely(state->idev != NULL)) {
2330 read_unlock_bh(&state->idev->lock);
2331 in6_dev_put(state->idev);
2333 state->dev = state->dev->next;
2334 if (!state->dev) {
2335 state->idev = NULL;
2336 goto out;
2338 state->idev = in6_dev_get(state->dev);
2339 if (!state->idev)
2340 continue;
2341 read_lock_bh(&state->idev->lock);
2342 state->im = state->idev->mc_list;
2344 if (!state->im)
2345 break;
2346 spin_lock_bh(&state->im->mca_lock);
2347 psf = state->im->mca_sources;
2349 out:
2350 return psf;
2353 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2355 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2356 if (psf)
2357 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2358 --pos;
2359 return pos ? NULL : psf;
2362 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2364 read_lock(&dev_base_lock);
2365 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2368 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2370 struct ip6_sf_list *psf;
2371 if (v == SEQ_START_TOKEN)
2372 psf = igmp6_mcf_get_first(seq);
2373 else
2374 psf = igmp6_mcf_get_next(seq, v);
2375 ++*pos;
2376 return psf;
2379 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2381 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2382 if (likely(state->im != NULL)) {
2383 spin_unlock_bh(&state->im->mca_lock);
2384 state->im = NULL;
2386 if (likely(state->idev != NULL)) {
2387 read_unlock_bh(&state->idev->lock);
2388 in6_dev_put(state->idev);
2389 state->idev = NULL;
2391 state->dev = NULL;
2392 read_unlock(&dev_base_lock);
2395 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2397 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2398 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2400 if (v == SEQ_START_TOKEN) {
2401 seq_printf(seq,
2402 "%3s %6s "
2403 "%32s %32s %6s %6s\n", "Idx",
2404 "Device", "Multicast Address",
2405 "Source Address", "INC", "EXC");
2406 } else {
2407 seq_printf(seq,
2408 "%3d %6.6s "
2409 "%04x%04x%04x%04x%04x%04x%04x%04x "
2410 "%04x%04x%04x%04x%04x%04x%04x%04x "
2411 "%6lu %6lu\n",
2412 state->dev->ifindex, state->dev->name,
2413 NIP6(state->im->mca_addr),
2414 NIP6(psf->sf_addr),
2415 psf->sf_count[MCAST_INCLUDE],
2416 psf->sf_count[MCAST_EXCLUDE]);
2418 return 0;
2421 static struct seq_operations igmp6_mcf_seq_ops = {
2422 .start = igmp6_mcf_seq_start,
2423 .next = igmp6_mcf_seq_next,
2424 .stop = igmp6_mcf_seq_stop,
2425 .show = igmp6_mcf_seq_show,
2428 static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2430 struct seq_file *seq;
2431 int rc = -ENOMEM;
2432 struct igmp6_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2434 if (!s)
2435 goto out;
2437 rc = seq_open(file, &igmp6_mcf_seq_ops);
2438 if (rc)
2439 goto out_kfree;
2441 seq = file->private_data;
2442 seq->private = s;
2443 memset(s, 0, sizeof(*s));
2444 out:
2445 return rc;
2446 out_kfree:
2447 kfree(s);
2448 goto out;
2451 static struct file_operations igmp6_mcf_seq_fops = {
2452 .owner = THIS_MODULE,
2453 .open = igmp6_mcf_seq_open,
2454 .read = seq_read,
2455 .llseek = seq_lseek,
2456 .release = seq_release_private,
2458 #endif
2460 int __init igmp6_init(struct net_proto_family *ops)
2462 struct ipv6_pinfo *np;
2463 struct sock *sk;
2464 int err;
2466 err = sock_create_kern(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6, &igmp6_socket);
2467 if (err < 0) {
2468 printk(KERN_ERR
2469 "Failed to initialize the IGMP6 control socket (err %d).\n",
2470 err);
2471 igmp6_socket = NULL; /* For safety. */
2472 return err;
2475 sk = igmp6_socket->sk;
2476 sk->sk_allocation = GFP_ATOMIC;
2477 sk->sk_prot->unhash(sk);
2479 np = inet6_sk(sk);
2480 np->hop_limit = 1;
2482 #ifdef CONFIG_PROC_FS
2483 proc_net_fops_create("igmp6", S_IRUGO, &igmp6_mc_seq_fops);
2484 proc_net_fops_create("mcfilter6", S_IRUGO, &igmp6_mcf_seq_fops);
2485 #endif
2487 return 0;
2490 void igmp6_cleanup(void)
2492 sock_release(igmp6_socket);
2493 igmp6_socket = NULL; /* for safety */
2495 #ifdef CONFIG_PROC_FS
2496 proc_net_remove("mcfilter6");
2497 proc_net_remove("igmp6");
2498 #endif