fix IS_I9XX macro in i915 DRM driver
[linux-2.6/mini2440.git] / net / ipv6 / mcast.c
blobab228d1ea11484892a172eb1098195f5977477f2
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/module.h>
32 #include <linux/errno.h>
33 #include <linux/types.h>
34 #include <linux/string.h>
35 #include <linux/socket.h>
36 #include <linux/sockios.h>
37 #include <linux/jiffies.h>
38 #include <linux/times.h>
39 #include <linux/net.h>
40 #include <linux/in.h>
41 #include <linux/in6.h>
42 #include <linux/netdevice.h>
43 #include <linux/if_arp.h>
44 #include <linux/route.h>
45 #include <linux/init.h>
46 #include <linux/proc_fs.h>
47 #include <linux/seq_file.h>
49 #include <linux/netfilter.h>
50 #include <linux/netfilter_ipv6.h>
52 #include <net/net_namespace.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 __be16 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 __sum16 csum;
96 __be16 resv2;
97 __be16 ngrec;
98 struct mld2_grec grec[0];
101 struct mld2_query {
102 __u8 type;
103 __u8 code;
104 __sum16 csum;
105 __be16 mrc;
106 __be16 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 __be16 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))) << \
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 64
175 int sysctl_mld_max_msf __read_mostly = 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(&init_net, 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 rwlock_init(&mc_lst->sflock);
228 mc_lst->sflist = NULL;
231 * now add/increase the group membership on the device
234 err = ipv6_dev_mc_inc(dev, addr);
236 if (err) {
237 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
238 dev_put(dev);
239 return err;
242 write_lock_bh(&ipv6_sk_mc_lock);
243 mc_lst->next = np->ipv6_mc_list;
244 np->ipv6_mc_list = mc_lst;
245 write_unlock_bh(&ipv6_sk_mc_lock);
247 dev_put(dev);
249 return 0;
253 * socket leave on multicast group
255 int ipv6_sock_mc_drop(struct sock *sk, int ifindex, struct in6_addr *addr)
257 struct ipv6_pinfo *np = inet6_sk(sk);
258 struct ipv6_mc_socklist *mc_lst, **lnk;
260 write_lock_bh(&ipv6_sk_mc_lock);
261 for (lnk = &np->ipv6_mc_list; (mc_lst = *lnk) !=NULL ; lnk = &mc_lst->next) {
262 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
263 ipv6_addr_equal(&mc_lst->addr, addr)) {
264 struct net_device *dev;
266 *lnk = mc_lst->next;
267 write_unlock_bh(&ipv6_sk_mc_lock);
269 if ((dev = dev_get_by_index(&init_net, mc_lst->ifindex)) != NULL) {
270 struct inet6_dev *idev = in6_dev_get(dev);
272 (void) ip6_mc_leave_src(sk, mc_lst, idev);
273 if (idev) {
274 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
275 in6_dev_put(idev);
277 dev_put(dev);
278 } else
279 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
280 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
281 return 0;
284 write_unlock_bh(&ipv6_sk_mc_lock);
286 return -EADDRNOTAVAIL;
289 static struct inet6_dev *ip6_mc_find_dev(struct in6_addr *group, int ifindex)
291 struct net_device *dev = NULL;
292 struct inet6_dev *idev = NULL;
294 if (ifindex == 0) {
295 struct rt6_info *rt;
297 rt = rt6_lookup(group, NULL, 0, 0);
298 if (rt) {
299 dev = rt->rt6i_dev;
300 dev_hold(dev);
301 dst_release(&rt->u.dst);
303 } else
304 dev = dev_get_by_index(&init_net, ifindex);
306 if (!dev)
307 return NULL;
308 idev = in6_dev_get(dev);
309 if (!idev) {
310 dev_put(dev);
311 return NULL;
313 read_lock_bh(&idev->lock);
314 if (idev->dead) {
315 read_unlock_bh(&idev->lock);
316 in6_dev_put(idev);
317 dev_put(dev);
318 return NULL;
320 return idev;
323 void ipv6_sock_mc_close(struct sock *sk)
325 struct ipv6_pinfo *np = inet6_sk(sk);
326 struct ipv6_mc_socklist *mc_lst;
328 write_lock_bh(&ipv6_sk_mc_lock);
329 while ((mc_lst = np->ipv6_mc_list) != NULL) {
330 struct net_device *dev;
332 np->ipv6_mc_list = mc_lst->next;
333 write_unlock_bh(&ipv6_sk_mc_lock);
335 dev = dev_get_by_index(&init_net, mc_lst->ifindex);
336 if (dev) {
337 struct inet6_dev *idev = in6_dev_get(dev);
339 (void) ip6_mc_leave_src(sk, mc_lst, idev);
340 if (idev) {
341 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
342 in6_dev_put(idev);
344 dev_put(dev);
345 } else
346 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
348 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
350 write_lock_bh(&ipv6_sk_mc_lock);
352 write_unlock_bh(&ipv6_sk_mc_lock);
355 int ip6_mc_source(int add, int omode, struct sock *sk,
356 struct group_source_req *pgsr)
358 struct in6_addr *source, *group;
359 struct ipv6_mc_socklist *pmc;
360 struct net_device *dev;
361 struct inet6_dev *idev;
362 struct ipv6_pinfo *inet6 = inet6_sk(sk);
363 struct ip6_sf_socklist *psl;
364 int i, j, rv;
365 int leavegroup = 0;
366 int pmclocked = 0;
367 int err;
369 if (pgsr->gsr_group.ss_family != AF_INET6 ||
370 pgsr->gsr_source.ss_family != AF_INET6)
371 return -EINVAL;
373 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
374 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
376 if (!ipv6_addr_is_multicast(group))
377 return -EINVAL;
379 idev = ip6_mc_find_dev(group, pgsr->gsr_interface);
380 if (!idev)
381 return -ENODEV;
382 dev = idev->dev;
384 err = -EADDRNOTAVAIL;
386 read_lock_bh(&ipv6_sk_mc_lock);
387 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
388 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
389 continue;
390 if (ipv6_addr_equal(&pmc->addr, group))
391 break;
393 if (!pmc) { /* must have a prior join */
394 err = -EINVAL;
395 goto done;
397 /* if a source filter was set, must be the same mode as before */
398 if (pmc->sflist) {
399 if (pmc->sfmode != omode) {
400 err = -EINVAL;
401 goto done;
403 } else if (pmc->sfmode != omode) {
404 /* allow mode switches for empty-set filters */
405 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
406 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
407 pmc->sfmode = omode;
410 write_lock_bh(&pmc->sflock);
411 pmclocked = 1;
413 psl = pmc->sflist;
414 if (!add) {
415 if (!psl)
416 goto done; /* err = -EADDRNOTAVAIL */
417 rv = !0;
418 for (i=0; i<psl->sl_count; i++) {
419 rv = memcmp(&psl->sl_addr[i], source,
420 sizeof(struct in6_addr));
421 if (rv == 0)
422 break;
424 if (rv) /* source not found */
425 goto done; /* err = -EADDRNOTAVAIL */
427 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
428 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
429 leavegroup = 1;
430 goto done;
433 /* update the interface filter */
434 ip6_mc_del_src(idev, group, omode, 1, source, 1);
436 for (j=i+1; j<psl->sl_count; j++)
437 psl->sl_addr[j-1] = psl->sl_addr[j];
438 psl->sl_count--;
439 err = 0;
440 goto done;
442 /* else, add a new source to the filter */
444 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
445 err = -ENOBUFS;
446 goto done;
448 if (!psl || psl->sl_count == psl->sl_max) {
449 struct ip6_sf_socklist *newpsl;
450 int count = IP6_SFBLOCK;
452 if (psl)
453 count += psl->sl_max;
454 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
455 if (!newpsl) {
456 err = -ENOBUFS;
457 goto done;
459 newpsl->sl_max = count;
460 newpsl->sl_count = count - IP6_SFBLOCK;
461 if (psl) {
462 for (i=0; i<psl->sl_count; i++)
463 newpsl->sl_addr[i] = psl->sl_addr[i];
464 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
466 pmc->sflist = psl = newpsl;
468 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
469 for (i=0; i<psl->sl_count; i++) {
470 rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
471 if (rv == 0)
472 break;
474 if (rv == 0) /* address already there is an error */
475 goto done;
476 for (j=psl->sl_count-1; j>=i; j--)
477 psl->sl_addr[j+1] = psl->sl_addr[j];
478 psl->sl_addr[i] = *source;
479 psl->sl_count++;
480 err = 0;
481 /* update the interface list */
482 ip6_mc_add_src(idev, group, omode, 1, source, 1);
483 done:
484 if (pmclocked)
485 write_unlock_bh(&pmc->sflock);
486 read_unlock_bh(&ipv6_sk_mc_lock);
487 read_unlock_bh(&idev->lock);
488 in6_dev_put(idev);
489 dev_put(dev);
490 if (leavegroup)
491 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
492 return err;
495 int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
497 struct in6_addr *group;
498 struct ipv6_mc_socklist *pmc;
499 struct net_device *dev;
500 struct inet6_dev *idev;
501 struct ipv6_pinfo *inet6 = inet6_sk(sk);
502 struct ip6_sf_socklist *newpsl, *psl;
503 int leavegroup = 0;
504 int i, err;
506 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
508 if (!ipv6_addr_is_multicast(group))
509 return -EINVAL;
510 if (gsf->gf_fmode != MCAST_INCLUDE &&
511 gsf->gf_fmode != MCAST_EXCLUDE)
512 return -EINVAL;
514 idev = ip6_mc_find_dev(group, gsf->gf_interface);
516 if (!idev)
517 return -ENODEV;
518 dev = idev->dev;
520 err = 0;
521 read_lock_bh(&ipv6_sk_mc_lock);
523 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
524 leavegroup = 1;
525 goto done;
528 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
529 if (pmc->ifindex != gsf->gf_interface)
530 continue;
531 if (ipv6_addr_equal(&pmc->addr, group))
532 break;
534 if (!pmc) { /* must have a prior join */
535 err = -EINVAL;
536 goto done;
538 if (gsf->gf_numsrc) {
539 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
540 GFP_ATOMIC);
541 if (!newpsl) {
542 err = -ENOBUFS;
543 goto done;
545 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
546 for (i=0; i<newpsl->sl_count; ++i) {
547 struct sockaddr_in6 *psin6;
549 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
550 newpsl->sl_addr[i] = psin6->sin6_addr;
552 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
553 newpsl->sl_count, newpsl->sl_addr, 0);
554 if (err) {
555 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
556 goto done;
558 } else {
559 newpsl = NULL;
560 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
563 write_lock_bh(&pmc->sflock);
564 psl = pmc->sflist;
565 if (psl) {
566 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
567 psl->sl_count, psl->sl_addr, 0);
568 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
569 } else
570 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
571 pmc->sflist = newpsl;
572 pmc->sfmode = gsf->gf_fmode;
573 write_unlock_bh(&pmc->sflock);
574 err = 0;
575 done:
576 read_unlock_bh(&ipv6_sk_mc_lock);
577 read_unlock_bh(&idev->lock);
578 in6_dev_put(idev);
579 dev_put(dev);
580 if (leavegroup)
581 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
582 return err;
585 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
586 struct group_filter __user *optval, int __user *optlen)
588 int err, i, count, copycount;
589 struct in6_addr *group;
590 struct ipv6_mc_socklist *pmc;
591 struct inet6_dev *idev;
592 struct net_device *dev;
593 struct ipv6_pinfo *inet6 = inet6_sk(sk);
594 struct ip6_sf_socklist *psl;
596 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
598 if (!ipv6_addr_is_multicast(group))
599 return -EINVAL;
601 idev = ip6_mc_find_dev(group, gsf->gf_interface);
603 if (!idev)
604 return -ENODEV;
606 dev = idev->dev;
608 err = -EADDRNOTAVAIL;
610 * changes to the ipv6_mc_list require the socket lock and
611 * a read lock on ip6_sk_mc_lock. We have the socket lock,
612 * so reading the list is safe.
615 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
616 if (pmc->ifindex != gsf->gf_interface)
617 continue;
618 if (ipv6_addr_equal(group, &pmc->addr))
619 break;
621 if (!pmc) /* must have a prior join */
622 goto done;
623 gsf->gf_fmode = pmc->sfmode;
624 psl = pmc->sflist;
625 count = psl ? psl->sl_count : 0;
626 read_unlock_bh(&idev->lock);
627 in6_dev_put(idev);
628 dev_put(dev);
630 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
631 gsf->gf_numsrc = count;
632 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
633 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
634 return -EFAULT;
636 /* changes to psl require the socket lock, a read lock on
637 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
638 * have the socket lock, so reading here is safe.
640 for (i=0; i<copycount; i++) {
641 struct sockaddr_in6 *psin6;
642 struct sockaddr_storage ss;
644 psin6 = (struct sockaddr_in6 *)&ss;
645 memset(&ss, 0, sizeof(ss));
646 psin6->sin6_family = AF_INET6;
647 psin6->sin6_addr = psl->sl_addr[i];
648 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
649 return -EFAULT;
651 return 0;
652 done:
653 read_unlock_bh(&idev->lock);
654 in6_dev_put(idev);
655 dev_put(dev);
656 return err;
659 int inet6_mc_check(struct sock *sk, struct in6_addr *mc_addr,
660 struct in6_addr *src_addr)
662 struct ipv6_pinfo *np = inet6_sk(sk);
663 struct ipv6_mc_socklist *mc;
664 struct ip6_sf_socklist *psl;
665 int rv = 1;
667 read_lock(&ipv6_sk_mc_lock);
668 for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
669 if (ipv6_addr_equal(&mc->addr, mc_addr))
670 break;
672 if (!mc) {
673 read_unlock(&ipv6_sk_mc_lock);
674 return 1;
676 read_lock(&mc->sflock);
677 psl = mc->sflist;
678 if (!psl) {
679 rv = mc->sfmode == MCAST_EXCLUDE;
680 } else {
681 int i;
683 for (i=0; i<psl->sl_count; i++) {
684 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
685 break;
687 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
688 rv = 0;
689 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
690 rv = 0;
692 read_unlock(&mc->sflock);
693 read_unlock(&ipv6_sk_mc_lock);
695 return rv;
698 static void ma_put(struct ifmcaddr6 *mc)
700 if (atomic_dec_and_test(&mc->mca_refcnt)) {
701 in6_dev_put(mc->idev);
702 kfree(mc);
706 static void igmp6_group_added(struct ifmcaddr6 *mc)
708 struct net_device *dev = mc->idev->dev;
709 char buf[MAX_ADDR_LEN];
711 spin_lock_bh(&mc->mca_lock);
712 if (!(mc->mca_flags&MAF_LOADED)) {
713 mc->mca_flags |= MAF_LOADED;
714 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
715 dev_mc_add(dev, buf, dev->addr_len, 0);
717 spin_unlock_bh(&mc->mca_lock);
719 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
720 return;
722 if (MLD_V1_SEEN(mc->idev)) {
723 igmp6_join_group(mc);
724 return;
726 /* else v2 */
728 mc->mca_crcount = mc->idev->mc_qrv;
729 mld_ifc_event(mc->idev);
732 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
734 struct net_device *dev = mc->idev->dev;
735 char buf[MAX_ADDR_LEN];
737 spin_lock_bh(&mc->mca_lock);
738 if (mc->mca_flags&MAF_LOADED) {
739 mc->mca_flags &= ~MAF_LOADED;
740 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
741 dev_mc_delete(dev, buf, dev->addr_len, 0);
744 if (mc->mca_flags & MAF_NOREPORT)
745 goto done;
746 spin_unlock_bh(&mc->mca_lock);
748 if (!mc->idev->dead)
749 igmp6_leave_group(mc);
751 spin_lock_bh(&mc->mca_lock);
752 if (del_timer(&mc->mca_timer))
753 atomic_dec(&mc->mca_refcnt);
754 done:
755 ip6_mc_clear_src(mc);
756 spin_unlock_bh(&mc->mca_lock);
760 * deleted ifmcaddr6 manipulation
762 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
764 struct ifmcaddr6 *pmc;
766 /* this is an "ifmcaddr6" for convenience; only the fields below
767 * are actually used. In particular, the refcnt and users are not
768 * used for management of the delete list. Using the same structure
769 * for deleted items allows change reports to use common code with
770 * non-deleted or query-response MCA's.
772 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
773 if (!pmc)
774 return;
776 spin_lock_bh(&im->mca_lock);
777 spin_lock_init(&pmc->mca_lock);
778 pmc->idev = im->idev;
779 in6_dev_hold(idev);
780 pmc->mca_addr = im->mca_addr;
781 pmc->mca_crcount = idev->mc_qrv;
782 pmc->mca_sfmode = im->mca_sfmode;
783 if (pmc->mca_sfmode == MCAST_INCLUDE) {
784 struct ip6_sf_list *psf;
786 pmc->mca_tomb = im->mca_tomb;
787 pmc->mca_sources = im->mca_sources;
788 im->mca_tomb = im->mca_sources = NULL;
789 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
790 psf->sf_crcount = pmc->mca_crcount;
792 spin_unlock_bh(&im->mca_lock);
794 write_lock_bh(&idev->mc_lock);
795 pmc->next = idev->mc_tomb;
796 idev->mc_tomb = pmc;
797 write_unlock_bh(&idev->mc_lock);
800 static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
802 struct ifmcaddr6 *pmc, *pmc_prev;
803 struct ip6_sf_list *psf, *psf_next;
805 write_lock_bh(&idev->mc_lock);
806 pmc_prev = NULL;
807 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
808 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
809 break;
810 pmc_prev = pmc;
812 if (pmc) {
813 if (pmc_prev)
814 pmc_prev->next = pmc->next;
815 else
816 idev->mc_tomb = pmc->next;
818 write_unlock_bh(&idev->mc_lock);
819 if (pmc) {
820 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
821 psf_next = psf->sf_next;
822 kfree(psf);
824 in6_dev_put(pmc->idev);
825 kfree(pmc);
829 static void mld_clear_delrec(struct inet6_dev *idev)
831 struct ifmcaddr6 *pmc, *nextpmc;
833 write_lock_bh(&idev->mc_lock);
834 pmc = idev->mc_tomb;
835 idev->mc_tomb = NULL;
836 write_unlock_bh(&idev->mc_lock);
838 for (; pmc; pmc = nextpmc) {
839 nextpmc = pmc->next;
840 ip6_mc_clear_src(pmc);
841 in6_dev_put(pmc->idev);
842 kfree(pmc);
845 /* clear dead sources, too */
846 read_lock_bh(&idev->lock);
847 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
848 struct ip6_sf_list *psf, *psf_next;
850 spin_lock_bh(&pmc->mca_lock);
851 psf = pmc->mca_tomb;
852 pmc->mca_tomb = NULL;
853 spin_unlock_bh(&pmc->mca_lock);
854 for (; psf; psf=psf_next) {
855 psf_next = psf->sf_next;
856 kfree(psf);
859 read_unlock_bh(&idev->lock);
864 * device multicast group inc (add if not found)
866 int ipv6_dev_mc_inc(struct net_device *dev, struct in6_addr *addr)
868 struct ifmcaddr6 *mc;
869 struct inet6_dev *idev;
871 idev = in6_dev_get(dev);
873 if (idev == NULL)
874 return -EINVAL;
876 write_lock_bh(&idev->lock);
877 if (idev->dead) {
878 write_unlock_bh(&idev->lock);
879 in6_dev_put(idev);
880 return -ENODEV;
883 for (mc = idev->mc_list; mc; mc = mc->next) {
884 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
885 mc->mca_users++;
886 write_unlock_bh(&idev->lock);
887 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
888 NULL, 0);
889 in6_dev_put(idev);
890 return 0;
895 * not found: create a new one.
898 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
900 if (mc == NULL) {
901 write_unlock_bh(&idev->lock);
902 in6_dev_put(idev);
903 return -ENOMEM;
906 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
908 ipv6_addr_copy(&mc->mca_addr, addr);
909 mc->idev = idev;
910 mc->mca_users = 1;
911 /* mca_stamp should be updated upon changes */
912 mc->mca_cstamp = mc->mca_tstamp = jiffies;
913 atomic_set(&mc->mca_refcnt, 2);
914 spin_lock_init(&mc->mca_lock);
916 /* initial mode is (EX, empty) */
917 mc->mca_sfmode = MCAST_EXCLUDE;
918 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
920 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
921 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
922 mc->mca_flags |= MAF_NOREPORT;
924 mc->next = idev->mc_list;
925 idev->mc_list = mc;
926 write_unlock_bh(&idev->lock);
928 mld_del_delrec(idev, &mc->mca_addr);
929 igmp6_group_added(mc);
930 ma_put(mc);
931 return 0;
935 * device multicast group del
937 int __ipv6_dev_mc_dec(struct inet6_dev *idev, struct in6_addr *addr)
939 struct ifmcaddr6 *ma, **map;
941 write_lock_bh(&idev->lock);
942 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
943 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
944 if (--ma->mca_users == 0) {
945 *map = ma->next;
946 write_unlock_bh(&idev->lock);
948 igmp6_group_dropped(ma);
950 ma_put(ma);
951 return 0;
953 write_unlock_bh(&idev->lock);
954 return 0;
957 write_unlock_bh(&idev->lock);
959 return -ENOENT;
962 int ipv6_dev_mc_dec(struct net_device *dev, struct in6_addr *addr)
964 struct inet6_dev *idev = in6_dev_get(dev);
965 int err;
967 if (!idev)
968 return -ENODEV;
970 err = __ipv6_dev_mc_dec(idev, addr);
972 in6_dev_put(idev);
974 return err;
978 * identify MLD packets for MLD filter exceptions
980 int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
982 struct icmp6hdr *pic;
984 if (nexthdr != IPPROTO_ICMPV6)
985 return 0;
987 if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
988 return 0;
990 pic = icmp6_hdr(skb);
992 switch (pic->icmp6_type) {
993 case ICMPV6_MGM_QUERY:
994 case ICMPV6_MGM_REPORT:
995 case ICMPV6_MGM_REDUCTION:
996 case ICMPV6_MLD2_REPORT:
997 return 1;
998 default:
999 break;
1001 return 0;
1005 * check if the interface/address pair is valid
1007 int ipv6_chk_mcast_addr(struct net_device *dev, struct in6_addr *group,
1008 struct in6_addr *src_addr)
1010 struct inet6_dev *idev;
1011 struct ifmcaddr6 *mc;
1012 int rv = 0;
1014 idev = in6_dev_get(dev);
1015 if (idev) {
1016 read_lock_bh(&idev->lock);
1017 for (mc = idev->mc_list; mc; mc=mc->next) {
1018 if (ipv6_addr_equal(&mc->mca_addr, group))
1019 break;
1021 if (mc) {
1022 if (src_addr && !ipv6_addr_any(src_addr)) {
1023 struct ip6_sf_list *psf;
1025 spin_lock_bh(&mc->mca_lock);
1026 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
1027 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1028 break;
1030 if (psf)
1031 rv = psf->sf_count[MCAST_INCLUDE] ||
1032 psf->sf_count[MCAST_EXCLUDE] !=
1033 mc->mca_sfcount[MCAST_EXCLUDE];
1034 else
1035 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
1036 spin_unlock_bh(&mc->mca_lock);
1037 } else
1038 rv = 1; /* don't filter unspecified source */
1040 read_unlock_bh(&idev->lock);
1041 in6_dev_put(idev);
1043 return rv;
1046 static void mld_gq_start_timer(struct inet6_dev *idev)
1048 int tv = net_random() % idev->mc_maxdelay;
1050 idev->mc_gq_running = 1;
1051 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1052 in6_dev_hold(idev);
1055 static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
1057 int tv = net_random() % delay;
1059 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1060 in6_dev_hold(idev);
1064 * IGMP handling (alias multicast ICMPv6 messages)
1067 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1069 unsigned long delay = resptime;
1071 /* Do not start timer for these addresses */
1072 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1073 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1074 return;
1076 if (del_timer(&ma->mca_timer)) {
1077 atomic_dec(&ma->mca_refcnt);
1078 delay = ma->mca_timer.expires - jiffies;
1081 if (delay >= resptime) {
1082 if (resptime)
1083 delay = net_random() % resptime;
1084 else
1085 delay = 1;
1087 ma->mca_timer.expires = jiffies + delay;
1088 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1089 atomic_inc(&ma->mca_refcnt);
1090 ma->mca_flags |= MAF_TIMER_RUNNING;
1093 /* mark EXCLUDE-mode sources */
1094 static int mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1095 struct in6_addr *srcs)
1097 struct ip6_sf_list *psf;
1098 int i, scount;
1100 scount = 0;
1101 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1102 if (scount == nsrcs)
1103 break;
1104 for (i=0; i<nsrcs; i++) {
1105 /* skip inactive filters */
1106 if (pmc->mca_sfcount[MCAST_INCLUDE] ||
1107 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1108 psf->sf_count[MCAST_EXCLUDE])
1109 continue;
1110 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1111 scount++;
1112 break;
1116 pmc->mca_flags &= ~MAF_GSQUERY;
1117 if (scount == nsrcs) /* all sources excluded */
1118 return 0;
1119 return 1;
1122 static int mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1123 struct in6_addr *srcs)
1125 struct ip6_sf_list *psf;
1126 int i, scount;
1128 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1129 return mld_xmarksources(pmc, nsrcs, srcs);
1131 /* mark INCLUDE-mode sources */
1133 scount = 0;
1134 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1135 if (scount == nsrcs)
1136 break;
1137 for (i=0; i<nsrcs; i++) {
1138 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1139 psf->sf_gsresp = 1;
1140 scount++;
1141 break;
1145 if (!scount) {
1146 pmc->mca_flags &= ~MAF_GSQUERY;
1147 return 0;
1149 pmc->mca_flags |= MAF_GSQUERY;
1150 return 1;
1153 int igmp6_event_query(struct sk_buff *skb)
1155 struct mld2_query *mlh2 = NULL;
1156 struct ifmcaddr6 *ma;
1157 struct in6_addr *group;
1158 unsigned long max_delay;
1159 struct inet6_dev *idev;
1160 struct icmp6hdr *hdr;
1161 int group_type;
1162 int mark = 0;
1163 int len;
1165 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1166 return -EINVAL;
1168 /* compute payload length excluding extension headers */
1169 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
1170 len -= skb_network_header_len(skb);
1172 /* Drop queries with not link local source */
1173 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
1174 return -EINVAL;
1176 idev = in6_dev_get(skb->dev);
1178 if (idev == NULL)
1179 return 0;
1181 hdr = icmp6_hdr(skb);
1182 group = (struct in6_addr *) (hdr + 1);
1183 group_type = ipv6_addr_type(group);
1185 if (group_type != IPV6_ADDR_ANY &&
1186 !(group_type&IPV6_ADDR_MULTICAST)) {
1187 in6_dev_put(idev);
1188 return -EINVAL;
1191 if (len == 24) {
1192 int switchback;
1193 /* MLDv1 router present */
1195 /* Translate milliseconds to jiffies */
1196 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
1198 switchback = (idev->mc_qrv + 1) * max_delay;
1199 idev->mc_v1_seen = jiffies + switchback;
1201 /* cancel the interface change timer */
1202 idev->mc_ifc_count = 0;
1203 if (del_timer(&idev->mc_ifc_timer))
1204 __in6_dev_put(idev);
1205 /* clear deleted report items */
1206 mld_clear_delrec(idev);
1207 } else if (len >= 28) {
1208 int srcs_offset = sizeof(struct mld2_query) -
1209 sizeof(struct icmp6hdr);
1210 if (!pskb_may_pull(skb, srcs_offset)) {
1211 in6_dev_put(idev);
1212 return -EINVAL;
1214 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1215 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
1216 if (!max_delay)
1217 max_delay = 1;
1218 idev->mc_maxdelay = max_delay;
1219 if (mlh2->qrv)
1220 idev->mc_qrv = mlh2->qrv;
1221 if (group_type == IPV6_ADDR_ANY) { /* general query */
1222 if (mlh2->nsrcs) {
1223 in6_dev_put(idev);
1224 return -EINVAL; /* no sources allowed */
1226 mld_gq_start_timer(idev);
1227 in6_dev_put(idev);
1228 return 0;
1230 /* mark sources to include, if group & source-specific */
1231 if (mlh2->nsrcs != 0) {
1232 if (!pskb_may_pull(skb, srcs_offset +
1233 ntohs(mlh2->nsrcs) * sizeof(struct in6_addr))) {
1234 in6_dev_put(idev);
1235 return -EINVAL;
1237 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1238 mark = 1;
1240 } else {
1241 in6_dev_put(idev);
1242 return -EINVAL;
1245 read_lock_bh(&idev->lock);
1246 if (group_type == IPV6_ADDR_ANY) {
1247 for (ma = idev->mc_list; ma; ma=ma->next) {
1248 spin_lock_bh(&ma->mca_lock);
1249 igmp6_group_queried(ma, max_delay);
1250 spin_unlock_bh(&ma->mca_lock);
1252 } else {
1253 for (ma = idev->mc_list; ma; ma=ma->next) {
1254 if (!ipv6_addr_equal(group, &ma->mca_addr))
1255 continue;
1256 spin_lock_bh(&ma->mca_lock);
1257 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1258 /* gsquery <- gsquery && mark */
1259 if (!mark)
1260 ma->mca_flags &= ~MAF_GSQUERY;
1261 } else {
1262 /* gsquery <- mark */
1263 if (mark)
1264 ma->mca_flags |= MAF_GSQUERY;
1265 else
1266 ma->mca_flags &= ~MAF_GSQUERY;
1268 if (!(ma->mca_flags & MAF_GSQUERY) ||
1269 mld_marksources(ma, ntohs(mlh2->nsrcs), mlh2->srcs))
1270 igmp6_group_queried(ma, max_delay);
1271 spin_unlock_bh(&ma->mca_lock);
1272 break;
1275 read_unlock_bh(&idev->lock);
1276 in6_dev_put(idev);
1278 return 0;
1282 int igmp6_event_report(struct sk_buff *skb)
1284 struct ifmcaddr6 *ma;
1285 struct in6_addr *addrp;
1286 struct inet6_dev *idev;
1287 struct icmp6hdr *hdr;
1288 int addr_type;
1290 /* Our own report looped back. Ignore it. */
1291 if (skb->pkt_type == PACKET_LOOPBACK)
1292 return 0;
1294 /* send our report if the MC router may not have heard this report */
1295 if (skb->pkt_type != PACKET_MULTICAST &&
1296 skb->pkt_type != PACKET_BROADCAST)
1297 return 0;
1299 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1300 return -EINVAL;
1302 hdr = icmp6_hdr(skb);
1304 /* Drop reports with not link local source */
1305 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
1306 if (addr_type != IPV6_ADDR_ANY &&
1307 !(addr_type&IPV6_ADDR_LINKLOCAL))
1308 return -EINVAL;
1310 addrp = (struct in6_addr *) (hdr + 1);
1312 idev = in6_dev_get(skb->dev);
1313 if (idev == NULL)
1314 return -ENODEV;
1317 * Cancel the timer for this group
1320 read_lock_bh(&idev->lock);
1321 for (ma = idev->mc_list; ma; ma=ma->next) {
1322 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1323 spin_lock(&ma->mca_lock);
1324 if (del_timer(&ma->mca_timer))
1325 atomic_dec(&ma->mca_refcnt);
1326 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1327 spin_unlock(&ma->mca_lock);
1328 break;
1331 read_unlock_bh(&idev->lock);
1332 in6_dev_put(idev);
1333 return 0;
1336 static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1337 int gdeleted, int sdeleted)
1339 switch (type) {
1340 case MLD2_MODE_IS_INCLUDE:
1341 case MLD2_MODE_IS_EXCLUDE:
1342 if (gdeleted || sdeleted)
1343 return 0;
1344 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1345 if (pmc->mca_sfmode == MCAST_INCLUDE)
1346 return 1;
1347 /* don't include if this source is excluded
1348 * in all filters
1350 if (psf->sf_count[MCAST_INCLUDE])
1351 return type == MLD2_MODE_IS_INCLUDE;
1352 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1353 psf->sf_count[MCAST_EXCLUDE];
1355 return 0;
1356 case MLD2_CHANGE_TO_INCLUDE:
1357 if (gdeleted || sdeleted)
1358 return 0;
1359 return psf->sf_count[MCAST_INCLUDE] != 0;
1360 case MLD2_CHANGE_TO_EXCLUDE:
1361 if (gdeleted || sdeleted)
1362 return 0;
1363 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1364 psf->sf_count[MCAST_INCLUDE])
1365 return 0;
1366 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1367 psf->sf_count[MCAST_EXCLUDE];
1368 case MLD2_ALLOW_NEW_SOURCES:
1369 if (gdeleted || !psf->sf_crcount)
1370 return 0;
1371 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1372 case MLD2_BLOCK_OLD_SOURCES:
1373 if (pmc->mca_sfmode == MCAST_INCLUDE)
1374 return gdeleted || (psf->sf_crcount && sdeleted);
1375 return psf->sf_crcount && !gdeleted && !sdeleted;
1377 return 0;
1380 static int
1381 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1383 struct ip6_sf_list *psf;
1384 int scount = 0;
1386 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1387 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1388 continue;
1389 scount++;
1391 return scount;
1394 static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1396 struct sock *sk = igmp6_socket->sk;
1397 struct sk_buff *skb;
1398 struct mld2_report *pmr;
1399 struct in6_addr addr_buf;
1400 int err;
1401 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1402 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1403 IPV6_TLV_PADN, 0 };
1405 /* we assume size > sizeof(ra) here */
1406 skb = sock_alloc_send_skb(sk, size + LL_RESERVED_SPACE(dev), 1, &err);
1408 if (!skb)
1409 return NULL;
1411 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1413 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
1414 /* <draft-ietf-magma-mld-source-05.txt>:
1415 * use unspecified address as the source address
1416 * when a valid link-local address is not available.
1418 memset(&addr_buf, 0, sizeof(addr_buf));
1421 ip6_nd_hdr(sk, skb, dev, &addr_buf, &mld2_all_mcr, NEXTHDR_HOP, 0);
1423 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1425 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
1426 skb_put(skb, sizeof(*pmr));
1427 pmr = (struct mld2_report *)skb_transport_header(skb);
1428 pmr->type = ICMPV6_MLD2_REPORT;
1429 pmr->resv1 = 0;
1430 pmr->csum = 0;
1431 pmr->resv2 = 0;
1432 pmr->ngrec = 0;
1433 return skb;
1436 static inline int mld_dev_queue_xmit2(struct sk_buff *skb)
1438 struct net_device *dev = skb->dev;
1439 unsigned char ha[MAX_ADDR_LEN];
1441 ndisc_mc_map(&ipv6_hdr(skb)->daddr, ha, dev, 1);
1442 if (dev_hard_header(skb, dev, ETH_P_IPV6, ha, NULL, skb->len) < 0) {
1443 kfree_skb(skb);
1444 return -EINVAL;
1446 return dev_queue_xmit(skb);
1449 static inline int mld_dev_queue_xmit(struct sk_buff *skb)
1451 return NF_HOOK(PF_INET6, NF_INET_POST_ROUTING, skb, NULL, skb->dev,
1452 mld_dev_queue_xmit2);
1455 static void mld_sendpack(struct sk_buff *skb)
1457 struct ipv6hdr *pip6 = ipv6_hdr(skb);
1458 struct mld2_report *pmr =
1459 (struct mld2_report *)skb_transport_header(skb);
1460 int payload_len, mldlen;
1461 struct inet6_dev *idev = in6_dev_get(skb->dev);
1462 int err;
1464 IP6_INC_STATS(idev, IPSTATS_MIB_OUTREQUESTS);
1465 payload_len = (skb->tail - skb->network_header) - sizeof(*pip6);
1466 mldlen = skb->tail - skb->transport_header;
1467 pip6->payload_len = htons(payload_len);
1469 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1470 IPPROTO_ICMPV6, csum_partial(skb_transport_header(skb),
1471 mldlen, 0));
1472 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
1473 mld_dev_queue_xmit);
1474 if (!err) {
1475 ICMP6MSGOUT_INC_STATS_BH(idev, ICMPV6_MLD2_REPORT);
1476 ICMP6_INC_STATS_BH(idev, ICMP6_MIB_OUTMSGS);
1477 IP6_INC_STATS_BH(idev, IPSTATS_MIB_OUTMCASTPKTS);
1478 } else
1479 IP6_INC_STATS_BH(idev, IPSTATS_MIB_OUTDISCARDS);
1481 if (likely(idev != NULL))
1482 in6_dev_put(idev);
1485 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1487 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
1490 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1491 int type, struct mld2_grec **ppgr)
1493 struct net_device *dev = pmc->idev->dev;
1494 struct mld2_report *pmr;
1495 struct mld2_grec *pgr;
1497 if (!skb)
1498 skb = mld_newpack(dev, dev->mtu);
1499 if (!skb)
1500 return NULL;
1501 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1502 pgr->grec_type = type;
1503 pgr->grec_auxwords = 0;
1504 pgr->grec_nsrcs = 0;
1505 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1506 pmr = (struct mld2_report *)skb_transport_header(skb);
1507 pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1508 *ppgr = pgr;
1509 return skb;
1512 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1513 skb_tailroom(skb)) : 0)
1515 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1516 int type, int gdeleted, int sdeleted)
1518 struct net_device *dev = pmc->idev->dev;
1519 struct mld2_report *pmr;
1520 struct mld2_grec *pgr = NULL;
1521 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1522 int scount, stotal, first, isquery, truncate;
1524 if (pmc->mca_flags & MAF_NOREPORT)
1525 return skb;
1527 isquery = type == MLD2_MODE_IS_INCLUDE ||
1528 type == MLD2_MODE_IS_EXCLUDE;
1529 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1530 type == MLD2_CHANGE_TO_EXCLUDE;
1532 stotal = scount = 0;
1534 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1536 if (!*psf_list)
1537 goto empty_source;
1539 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
1541 /* EX and TO_EX get a fresh packet, if needed */
1542 if (truncate) {
1543 if (pmr && pmr->ngrec &&
1544 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1545 if (skb)
1546 mld_sendpack(skb);
1547 skb = mld_newpack(dev, dev->mtu);
1550 first = 1;
1551 psf_prev = NULL;
1552 for (psf=*psf_list; psf; psf=psf_next) {
1553 struct in6_addr *psrc;
1555 psf_next = psf->sf_next;
1557 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1558 psf_prev = psf;
1559 continue;
1562 /* clear marks on query responses */
1563 if (isquery)
1564 psf->sf_gsresp = 0;
1566 if (AVAILABLE(skb) < sizeof(*psrc) +
1567 first*sizeof(struct mld2_grec)) {
1568 if (truncate && !first)
1569 break; /* truncate these */
1570 if (pgr)
1571 pgr->grec_nsrcs = htons(scount);
1572 if (skb)
1573 mld_sendpack(skb);
1574 skb = mld_newpack(dev, dev->mtu);
1575 first = 1;
1576 scount = 0;
1578 if (first) {
1579 skb = add_grhead(skb, pmc, type, &pgr);
1580 first = 0;
1582 if (!skb)
1583 return NULL;
1584 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1585 *psrc = psf->sf_addr;
1586 scount++; stotal++;
1587 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1588 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1589 psf->sf_crcount--;
1590 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1591 if (psf_prev)
1592 psf_prev->sf_next = psf->sf_next;
1593 else
1594 *psf_list = psf->sf_next;
1595 kfree(psf);
1596 continue;
1599 psf_prev = psf;
1602 empty_source:
1603 if (!stotal) {
1604 if (type == MLD2_ALLOW_NEW_SOURCES ||
1605 type == MLD2_BLOCK_OLD_SOURCES)
1606 return skb;
1607 if (pmc->mca_crcount || isquery) {
1608 /* make sure we have room for group header */
1609 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1610 mld_sendpack(skb);
1611 skb = NULL; /* add_grhead will get a new one */
1613 skb = add_grhead(skb, pmc, type, &pgr);
1616 if (pgr)
1617 pgr->grec_nsrcs = htons(scount);
1619 if (isquery)
1620 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1621 return skb;
1624 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1626 struct sk_buff *skb = NULL;
1627 int type;
1629 if (!pmc) {
1630 read_lock_bh(&idev->lock);
1631 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1632 if (pmc->mca_flags & MAF_NOREPORT)
1633 continue;
1634 spin_lock_bh(&pmc->mca_lock);
1635 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1636 type = MLD2_MODE_IS_EXCLUDE;
1637 else
1638 type = MLD2_MODE_IS_INCLUDE;
1639 skb = add_grec(skb, pmc, type, 0, 0);
1640 spin_unlock_bh(&pmc->mca_lock);
1642 read_unlock_bh(&idev->lock);
1643 } else {
1644 spin_lock_bh(&pmc->mca_lock);
1645 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1646 type = MLD2_MODE_IS_EXCLUDE;
1647 else
1648 type = MLD2_MODE_IS_INCLUDE;
1649 skb = add_grec(skb, pmc, type, 0, 0);
1650 spin_unlock_bh(&pmc->mca_lock);
1652 if (skb)
1653 mld_sendpack(skb);
1657 * remove zero-count source records from a source filter list
1659 static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1661 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1663 psf_prev = NULL;
1664 for (psf=*ppsf; psf; psf = psf_next) {
1665 psf_next = psf->sf_next;
1666 if (psf->sf_crcount == 0) {
1667 if (psf_prev)
1668 psf_prev->sf_next = psf->sf_next;
1669 else
1670 *ppsf = psf->sf_next;
1671 kfree(psf);
1672 } else
1673 psf_prev = psf;
1677 static void mld_send_cr(struct inet6_dev *idev)
1679 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1680 struct sk_buff *skb = NULL;
1681 int type, dtype;
1683 read_lock_bh(&idev->lock);
1684 write_lock_bh(&idev->mc_lock);
1686 /* deleted MCA's */
1687 pmc_prev = NULL;
1688 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1689 pmc_next = pmc->next;
1690 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1691 type = MLD2_BLOCK_OLD_SOURCES;
1692 dtype = MLD2_BLOCK_OLD_SOURCES;
1693 skb = add_grec(skb, pmc, type, 1, 0);
1694 skb = add_grec(skb, pmc, dtype, 1, 1);
1696 if (pmc->mca_crcount) {
1697 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1698 type = MLD2_CHANGE_TO_INCLUDE;
1699 skb = add_grec(skb, pmc, type, 1, 0);
1701 pmc->mca_crcount--;
1702 if (pmc->mca_crcount == 0) {
1703 mld_clear_zeros(&pmc->mca_tomb);
1704 mld_clear_zeros(&pmc->mca_sources);
1707 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1708 !pmc->mca_sources) {
1709 if (pmc_prev)
1710 pmc_prev->next = pmc_next;
1711 else
1712 idev->mc_tomb = pmc_next;
1713 in6_dev_put(pmc->idev);
1714 kfree(pmc);
1715 } else
1716 pmc_prev = pmc;
1718 write_unlock_bh(&idev->mc_lock);
1720 /* change recs */
1721 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1722 spin_lock_bh(&pmc->mca_lock);
1723 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1724 type = MLD2_BLOCK_OLD_SOURCES;
1725 dtype = MLD2_ALLOW_NEW_SOURCES;
1726 } else {
1727 type = MLD2_ALLOW_NEW_SOURCES;
1728 dtype = MLD2_BLOCK_OLD_SOURCES;
1730 skb = add_grec(skb, pmc, type, 0, 0);
1731 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1733 /* filter mode changes */
1734 if (pmc->mca_crcount) {
1735 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1736 type = MLD2_CHANGE_TO_EXCLUDE;
1737 else
1738 type = MLD2_CHANGE_TO_INCLUDE;
1739 skb = add_grec(skb, pmc, type, 0, 0);
1740 pmc->mca_crcount--;
1742 spin_unlock_bh(&pmc->mca_lock);
1744 read_unlock_bh(&idev->lock);
1745 if (!skb)
1746 return;
1747 (void) mld_sendpack(skb);
1750 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1752 struct sock *sk = igmp6_socket->sk;
1753 struct inet6_dev *idev;
1754 struct sk_buff *skb;
1755 struct icmp6hdr *hdr;
1756 struct in6_addr *snd_addr;
1757 struct in6_addr *addrp;
1758 struct in6_addr addr_buf;
1759 struct in6_addr all_routers;
1760 int err, len, payload_len, full_len;
1761 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1762 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1763 IPV6_TLV_PADN, 0 };
1765 rcu_read_lock();
1766 IP6_INC_STATS(__in6_dev_get(dev),
1767 IPSTATS_MIB_OUTREQUESTS);
1768 rcu_read_unlock();
1769 snd_addr = addr;
1770 if (type == ICMPV6_MGM_REDUCTION) {
1771 snd_addr = &all_routers;
1772 ipv6_addr_all_routers(&all_routers);
1775 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1776 payload_len = len + sizeof(ra);
1777 full_len = sizeof(struct ipv6hdr) + payload_len;
1779 skb = sock_alloc_send_skb(sk, LL_RESERVED_SPACE(dev) + full_len, 1, &err);
1781 if (skb == NULL) {
1782 rcu_read_lock();
1783 IP6_INC_STATS(__in6_dev_get(dev),
1784 IPSTATS_MIB_OUTDISCARDS);
1785 rcu_read_unlock();
1786 return;
1789 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1791 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
1792 /* <draft-ietf-magma-mld-source-05.txt>:
1793 * use unspecified address as the source address
1794 * when a valid link-local address is not available.
1796 memset(&addr_buf, 0, sizeof(addr_buf));
1799 ip6_nd_hdr(sk, skb, dev, &addr_buf, snd_addr, NEXTHDR_HOP, payload_len);
1801 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1803 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1804 memset(hdr, 0, sizeof(struct icmp6hdr));
1805 hdr->icmp6_type = type;
1807 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1808 ipv6_addr_copy(addrp, addr);
1810 hdr->icmp6_cksum = csum_ipv6_magic(&addr_buf, snd_addr, len,
1811 IPPROTO_ICMPV6,
1812 csum_partial((__u8 *) hdr, len, 0));
1814 idev = in6_dev_get(skb->dev);
1816 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
1817 mld_dev_queue_xmit);
1818 if (!err) {
1819 ICMP6MSGOUT_INC_STATS(idev, type);
1820 ICMP6_INC_STATS(idev, ICMP6_MIB_OUTMSGS);
1821 IP6_INC_STATS(idev, IPSTATS_MIB_OUTMCASTPKTS);
1822 } else
1823 IP6_INC_STATS(idev, IPSTATS_MIB_OUTDISCARDS);
1825 if (likely(idev != NULL))
1826 in6_dev_put(idev);
1827 return;
1830 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1831 struct in6_addr *psfsrc)
1833 struct ip6_sf_list *psf, *psf_prev;
1834 int rv = 0;
1836 psf_prev = NULL;
1837 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1838 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1839 break;
1840 psf_prev = psf;
1842 if (!psf || psf->sf_count[sfmode] == 0) {
1843 /* source filter not found, or count wrong => bug */
1844 return -ESRCH;
1846 psf->sf_count[sfmode]--;
1847 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1848 struct inet6_dev *idev = pmc->idev;
1850 /* no more filters for this source */
1851 if (psf_prev)
1852 psf_prev->sf_next = psf->sf_next;
1853 else
1854 pmc->mca_sources = psf->sf_next;
1855 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1856 !MLD_V1_SEEN(idev)) {
1857 psf->sf_crcount = idev->mc_qrv;
1858 psf->sf_next = pmc->mca_tomb;
1859 pmc->mca_tomb = psf;
1860 rv = 1;
1861 } else
1862 kfree(psf);
1864 return rv;
1867 static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1868 int sfmode, int sfcount, struct in6_addr *psfsrc,
1869 int delta)
1871 struct ifmcaddr6 *pmc;
1872 int changerec = 0;
1873 int i, err;
1875 if (!idev)
1876 return -ENODEV;
1877 read_lock_bh(&idev->lock);
1878 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1879 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1880 break;
1882 if (!pmc) {
1883 /* MCA not found?? bug */
1884 read_unlock_bh(&idev->lock);
1885 return -ESRCH;
1887 spin_lock_bh(&pmc->mca_lock);
1888 sf_markstate(pmc);
1889 if (!delta) {
1890 if (!pmc->mca_sfcount[sfmode]) {
1891 spin_unlock_bh(&pmc->mca_lock);
1892 read_unlock_bh(&idev->lock);
1893 return -EINVAL;
1895 pmc->mca_sfcount[sfmode]--;
1897 err = 0;
1898 for (i=0; i<sfcount; i++) {
1899 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1901 changerec |= rv > 0;
1902 if (!err && rv < 0)
1903 err = rv;
1905 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1906 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1907 pmc->mca_sfcount[MCAST_INCLUDE]) {
1908 struct ip6_sf_list *psf;
1910 /* filter mode change */
1911 pmc->mca_sfmode = MCAST_INCLUDE;
1912 pmc->mca_crcount = idev->mc_qrv;
1913 idev->mc_ifc_count = pmc->mca_crcount;
1914 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1915 psf->sf_crcount = 0;
1916 mld_ifc_event(pmc->idev);
1917 } else if (sf_setstate(pmc) || changerec)
1918 mld_ifc_event(pmc->idev);
1919 spin_unlock_bh(&pmc->mca_lock);
1920 read_unlock_bh(&idev->lock);
1921 return err;
1925 * Add multicast single-source filter to the interface list
1927 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1928 struct in6_addr *psfsrc, int delta)
1930 struct ip6_sf_list *psf, *psf_prev;
1932 psf_prev = NULL;
1933 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1934 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1935 break;
1936 psf_prev = psf;
1938 if (!psf) {
1939 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1940 if (!psf)
1941 return -ENOBUFS;
1943 psf->sf_addr = *psfsrc;
1944 if (psf_prev) {
1945 psf_prev->sf_next = psf;
1946 } else
1947 pmc->mca_sources = psf;
1949 psf->sf_count[sfmode]++;
1950 return 0;
1953 static void sf_markstate(struct ifmcaddr6 *pmc)
1955 struct ip6_sf_list *psf;
1956 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1958 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1959 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1960 psf->sf_oldin = mca_xcount ==
1961 psf->sf_count[MCAST_EXCLUDE] &&
1962 !psf->sf_count[MCAST_INCLUDE];
1963 } else
1964 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1967 static int sf_setstate(struct ifmcaddr6 *pmc)
1969 struct ip6_sf_list *psf, *dpsf;
1970 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1971 int qrv = pmc->idev->mc_qrv;
1972 int new_in, rv;
1974 rv = 0;
1975 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1976 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1977 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1978 !psf->sf_count[MCAST_INCLUDE];
1979 } else
1980 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1981 if (new_in) {
1982 if (!psf->sf_oldin) {
1983 struct ip6_sf_list *prev = NULL;
1985 for (dpsf=pmc->mca_tomb; dpsf;
1986 dpsf=dpsf->sf_next) {
1987 if (ipv6_addr_equal(&dpsf->sf_addr,
1988 &psf->sf_addr))
1989 break;
1990 prev = dpsf;
1992 if (dpsf) {
1993 if (prev)
1994 prev->sf_next = dpsf->sf_next;
1995 else
1996 pmc->mca_tomb = dpsf->sf_next;
1997 kfree(dpsf);
1999 psf->sf_crcount = qrv;
2000 rv++;
2002 } else if (psf->sf_oldin) {
2003 psf->sf_crcount = 0;
2005 * add or update "delete" records if an active filter
2006 * is now inactive
2008 for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
2009 if (ipv6_addr_equal(&dpsf->sf_addr,
2010 &psf->sf_addr))
2011 break;
2012 if (!dpsf) {
2013 dpsf = (struct ip6_sf_list *)
2014 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2015 if (!dpsf)
2016 continue;
2017 *dpsf = *psf;
2018 /* pmc->mca_lock held by callers */
2019 dpsf->sf_next = pmc->mca_tomb;
2020 pmc->mca_tomb = dpsf;
2022 dpsf->sf_crcount = qrv;
2023 rv++;
2026 return rv;
2030 * Add multicast source filter list to the interface list
2032 static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
2033 int sfmode, int sfcount, struct in6_addr *psfsrc,
2034 int delta)
2036 struct ifmcaddr6 *pmc;
2037 int isexclude;
2038 int i, err;
2040 if (!idev)
2041 return -ENODEV;
2042 read_lock_bh(&idev->lock);
2043 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2044 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2045 break;
2047 if (!pmc) {
2048 /* MCA not found?? bug */
2049 read_unlock_bh(&idev->lock);
2050 return -ESRCH;
2052 spin_lock_bh(&pmc->mca_lock);
2054 sf_markstate(pmc);
2055 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2056 if (!delta)
2057 pmc->mca_sfcount[sfmode]++;
2058 err = 0;
2059 for (i=0; i<sfcount; i++) {
2060 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
2061 if (err)
2062 break;
2064 if (err) {
2065 int j;
2067 if (!delta)
2068 pmc->mca_sfcount[sfmode]--;
2069 for (j=0; j<i; j++)
2070 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2071 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2072 struct inet6_dev *idev = pmc->idev;
2073 struct ip6_sf_list *psf;
2075 /* filter mode change */
2076 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2077 pmc->mca_sfmode = MCAST_EXCLUDE;
2078 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2079 pmc->mca_sfmode = MCAST_INCLUDE;
2080 /* else no filters; keep old mode for reports */
2082 pmc->mca_crcount = idev->mc_qrv;
2083 idev->mc_ifc_count = pmc->mca_crcount;
2084 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2085 psf->sf_crcount = 0;
2086 mld_ifc_event(idev);
2087 } else if (sf_setstate(pmc))
2088 mld_ifc_event(idev);
2089 spin_unlock_bh(&pmc->mca_lock);
2090 read_unlock_bh(&idev->lock);
2091 return err;
2094 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2096 struct ip6_sf_list *psf, *nextpsf;
2098 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2099 nextpsf = psf->sf_next;
2100 kfree(psf);
2102 pmc->mca_tomb = NULL;
2103 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2104 nextpsf = psf->sf_next;
2105 kfree(psf);
2107 pmc->mca_sources = NULL;
2108 pmc->mca_sfmode = MCAST_EXCLUDE;
2109 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
2110 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2114 static void igmp6_join_group(struct ifmcaddr6 *ma)
2116 unsigned long delay;
2118 if (ma->mca_flags & MAF_NOREPORT)
2119 return;
2121 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2123 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2125 spin_lock_bh(&ma->mca_lock);
2126 if (del_timer(&ma->mca_timer)) {
2127 atomic_dec(&ma->mca_refcnt);
2128 delay = ma->mca_timer.expires - jiffies;
2131 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2132 atomic_inc(&ma->mca_refcnt);
2133 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2134 spin_unlock_bh(&ma->mca_lock);
2137 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2138 struct inet6_dev *idev)
2140 int err;
2142 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2143 * so no other readers or writers of iml or its sflist
2145 if (!iml->sflist) {
2146 /* any-source empty exclude case */
2147 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2149 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2150 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2151 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2152 iml->sflist = NULL;
2153 return err;
2156 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2158 if (MLD_V1_SEEN(ma->idev)) {
2159 if (ma->mca_flags & MAF_LAST_REPORTER)
2160 igmp6_send(&ma->mca_addr, ma->idev->dev,
2161 ICMPV6_MGM_REDUCTION);
2162 } else {
2163 mld_add_delrec(ma->idev, ma);
2164 mld_ifc_event(ma->idev);
2168 static void mld_gq_timer_expire(unsigned long data)
2170 struct inet6_dev *idev = (struct inet6_dev *)data;
2172 idev->mc_gq_running = 0;
2173 mld_send_report(idev, NULL);
2174 __in6_dev_put(idev);
2177 static void mld_ifc_timer_expire(unsigned long data)
2179 struct inet6_dev *idev = (struct inet6_dev *)data;
2181 mld_send_cr(idev);
2182 if (idev->mc_ifc_count) {
2183 idev->mc_ifc_count--;
2184 if (idev->mc_ifc_count)
2185 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2187 __in6_dev_put(idev);
2190 static void mld_ifc_event(struct inet6_dev *idev)
2192 if (MLD_V1_SEEN(idev))
2193 return;
2194 idev->mc_ifc_count = idev->mc_qrv;
2195 mld_ifc_start_timer(idev, 1);
2199 static void igmp6_timer_handler(unsigned long data)
2201 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2203 if (MLD_V1_SEEN(ma->idev))
2204 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2205 else
2206 mld_send_report(ma->idev, ma);
2208 spin_lock(&ma->mca_lock);
2209 ma->mca_flags |= MAF_LAST_REPORTER;
2210 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2211 spin_unlock(&ma->mca_lock);
2212 ma_put(ma);
2215 /* Device going down */
2217 void ipv6_mc_down(struct inet6_dev *idev)
2219 struct ifmcaddr6 *i;
2221 /* Withdraw multicast list */
2223 read_lock_bh(&idev->lock);
2224 idev->mc_ifc_count = 0;
2225 if (del_timer(&idev->mc_ifc_timer))
2226 __in6_dev_put(idev);
2227 idev->mc_gq_running = 0;
2228 if (del_timer(&idev->mc_gq_timer))
2229 __in6_dev_put(idev);
2231 for (i = idev->mc_list; i; i=i->next)
2232 igmp6_group_dropped(i);
2233 read_unlock_bh(&idev->lock);
2235 mld_clear_delrec(idev);
2239 /* Device going up */
2241 void ipv6_mc_up(struct inet6_dev *idev)
2243 struct ifmcaddr6 *i;
2245 /* Install multicast list, except for all-nodes (already installed) */
2247 read_lock_bh(&idev->lock);
2248 for (i = idev->mc_list; i; i=i->next)
2249 igmp6_group_added(i);
2250 read_unlock_bh(&idev->lock);
2253 /* IPv6 device initialization. */
2255 void ipv6_mc_init_dev(struct inet6_dev *idev)
2257 write_lock_bh(&idev->lock);
2258 rwlock_init(&idev->mc_lock);
2259 idev->mc_gq_running = 0;
2260 setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2261 (unsigned long)idev);
2262 idev->mc_tomb = NULL;
2263 idev->mc_ifc_count = 0;
2264 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2265 (unsigned long)idev);
2266 idev->mc_qrv = MLD_QRV_DEFAULT;
2267 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2268 idev->mc_v1_seen = 0;
2269 write_unlock_bh(&idev->lock);
2273 * Device is about to be destroyed: clean up.
2276 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2278 struct ifmcaddr6 *i;
2279 struct in6_addr maddr;
2281 /* Deactivate timers */
2282 ipv6_mc_down(idev);
2284 /* Delete all-nodes address. */
2285 ipv6_addr_all_nodes(&maddr);
2287 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2288 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2289 * fail.
2291 __ipv6_dev_mc_dec(idev, &maddr);
2293 if (idev->cnf.forwarding) {
2294 ipv6_addr_all_routers(&maddr);
2295 __ipv6_dev_mc_dec(idev, &maddr);
2298 write_lock_bh(&idev->lock);
2299 while ((i = idev->mc_list) != NULL) {
2300 idev->mc_list = i->next;
2301 write_unlock_bh(&idev->lock);
2303 igmp6_group_dropped(i);
2304 ma_put(i);
2306 write_lock_bh(&idev->lock);
2308 write_unlock_bh(&idev->lock);
2311 #ifdef CONFIG_PROC_FS
2312 struct igmp6_mc_iter_state {
2313 struct net_device *dev;
2314 struct inet6_dev *idev;
2317 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2319 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2321 struct ifmcaddr6 *im = NULL;
2322 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2324 state->idev = NULL;
2325 for_each_netdev(&init_net, state->dev) {
2326 struct inet6_dev *idev;
2327 idev = in6_dev_get(state->dev);
2328 if (!idev)
2329 continue;
2330 read_lock_bh(&idev->lock);
2331 im = idev->mc_list;
2332 if (im) {
2333 state->idev = idev;
2334 break;
2336 read_unlock_bh(&idev->lock);
2337 in6_dev_put(idev);
2339 return im;
2342 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2344 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2346 im = im->next;
2347 while (!im) {
2348 if (likely(state->idev != NULL)) {
2349 read_unlock_bh(&state->idev->lock);
2350 in6_dev_put(state->idev);
2352 state->dev = next_net_device(state->dev);
2353 if (!state->dev) {
2354 state->idev = NULL;
2355 break;
2357 state->idev = in6_dev_get(state->dev);
2358 if (!state->idev)
2359 continue;
2360 read_lock_bh(&state->idev->lock);
2361 im = state->idev->mc_list;
2363 return im;
2366 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2368 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2369 if (im)
2370 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2371 --pos;
2372 return pos ? NULL : im;
2375 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2376 __acquires(dev_base_lock)
2378 read_lock(&dev_base_lock);
2379 return igmp6_mc_get_idx(seq, *pos);
2382 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2384 struct ifmcaddr6 *im;
2385 im = igmp6_mc_get_next(seq, v);
2386 ++*pos;
2387 return im;
2390 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2391 __releases(dev_base_lock)
2393 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2394 if (likely(state->idev != NULL)) {
2395 read_unlock_bh(&state->idev->lock);
2396 in6_dev_put(state->idev);
2397 state->idev = NULL;
2399 state->dev = NULL;
2400 read_unlock(&dev_base_lock);
2403 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2405 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2406 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2408 seq_printf(seq,
2409 "%-4d %-15s " NIP6_SEQFMT " %5d %08X %ld\n",
2410 state->dev->ifindex, state->dev->name,
2411 NIP6(im->mca_addr),
2412 im->mca_users, im->mca_flags,
2413 (im->mca_flags&MAF_TIMER_RUNNING) ?
2414 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2415 return 0;
2418 static const struct seq_operations igmp6_mc_seq_ops = {
2419 .start = igmp6_mc_seq_start,
2420 .next = igmp6_mc_seq_next,
2421 .stop = igmp6_mc_seq_stop,
2422 .show = igmp6_mc_seq_show,
2425 static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2427 return seq_open_private(file, &igmp6_mc_seq_ops,
2428 sizeof(struct igmp6_mc_iter_state));
2431 static const struct file_operations igmp6_mc_seq_fops = {
2432 .owner = THIS_MODULE,
2433 .open = igmp6_mc_seq_open,
2434 .read = seq_read,
2435 .llseek = seq_lseek,
2436 .release = seq_release_private,
2439 struct igmp6_mcf_iter_state {
2440 struct net_device *dev;
2441 struct inet6_dev *idev;
2442 struct ifmcaddr6 *im;
2445 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2447 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2449 struct ip6_sf_list *psf = NULL;
2450 struct ifmcaddr6 *im = NULL;
2451 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2453 state->idev = NULL;
2454 state->im = NULL;
2455 for_each_netdev(&init_net, state->dev) {
2456 struct inet6_dev *idev;
2457 idev = in6_dev_get(state->dev);
2458 if (unlikely(idev == NULL))
2459 continue;
2460 read_lock_bh(&idev->lock);
2461 im = idev->mc_list;
2462 if (likely(im != NULL)) {
2463 spin_lock_bh(&im->mca_lock);
2464 psf = im->mca_sources;
2465 if (likely(psf != NULL)) {
2466 state->im = im;
2467 state->idev = idev;
2468 break;
2470 spin_unlock_bh(&im->mca_lock);
2472 read_unlock_bh(&idev->lock);
2473 in6_dev_put(idev);
2475 return psf;
2478 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2480 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2482 psf = psf->sf_next;
2483 while (!psf) {
2484 spin_unlock_bh(&state->im->mca_lock);
2485 state->im = state->im->next;
2486 while (!state->im) {
2487 if (likely(state->idev != NULL)) {
2488 read_unlock_bh(&state->idev->lock);
2489 in6_dev_put(state->idev);
2491 state->dev = next_net_device(state->dev);
2492 if (!state->dev) {
2493 state->idev = NULL;
2494 goto out;
2496 state->idev = in6_dev_get(state->dev);
2497 if (!state->idev)
2498 continue;
2499 read_lock_bh(&state->idev->lock);
2500 state->im = state->idev->mc_list;
2502 if (!state->im)
2503 break;
2504 spin_lock_bh(&state->im->mca_lock);
2505 psf = state->im->mca_sources;
2507 out:
2508 return psf;
2511 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2513 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2514 if (psf)
2515 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2516 --pos;
2517 return pos ? NULL : psf;
2520 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2521 __acquires(dev_base_lock)
2523 read_lock(&dev_base_lock);
2524 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2527 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2529 struct ip6_sf_list *psf;
2530 if (v == SEQ_START_TOKEN)
2531 psf = igmp6_mcf_get_first(seq);
2532 else
2533 psf = igmp6_mcf_get_next(seq, v);
2534 ++*pos;
2535 return psf;
2538 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2539 __releases(dev_base_lock)
2541 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2542 if (likely(state->im != NULL)) {
2543 spin_unlock_bh(&state->im->mca_lock);
2544 state->im = NULL;
2546 if (likely(state->idev != NULL)) {
2547 read_unlock_bh(&state->idev->lock);
2548 in6_dev_put(state->idev);
2549 state->idev = NULL;
2551 state->dev = NULL;
2552 read_unlock(&dev_base_lock);
2555 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2557 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2558 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2560 if (v == SEQ_START_TOKEN) {
2561 seq_printf(seq,
2562 "%3s %6s "
2563 "%32s %32s %6s %6s\n", "Idx",
2564 "Device", "Multicast Address",
2565 "Source Address", "INC", "EXC");
2566 } else {
2567 seq_printf(seq,
2568 "%3d %6.6s " NIP6_SEQFMT " " NIP6_SEQFMT " %6lu %6lu\n",
2569 state->dev->ifindex, state->dev->name,
2570 NIP6(state->im->mca_addr),
2571 NIP6(psf->sf_addr),
2572 psf->sf_count[MCAST_INCLUDE],
2573 psf->sf_count[MCAST_EXCLUDE]);
2575 return 0;
2578 static const struct seq_operations igmp6_mcf_seq_ops = {
2579 .start = igmp6_mcf_seq_start,
2580 .next = igmp6_mcf_seq_next,
2581 .stop = igmp6_mcf_seq_stop,
2582 .show = igmp6_mcf_seq_show,
2585 static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2587 return seq_open_private(file, &igmp6_mcf_seq_ops,
2588 sizeof(struct igmp6_mcf_iter_state));
2591 static const struct file_operations igmp6_mcf_seq_fops = {
2592 .owner = THIS_MODULE,
2593 .open = igmp6_mcf_seq_open,
2594 .read = seq_read,
2595 .llseek = seq_lseek,
2596 .release = seq_release_private,
2598 #endif
2600 int __init igmp6_init(struct net_proto_family *ops)
2602 struct ipv6_pinfo *np;
2603 struct sock *sk;
2604 int err;
2606 err = sock_create_kern(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6, &igmp6_socket);
2607 if (err < 0) {
2608 printk(KERN_ERR
2609 "Failed to initialize the IGMP6 control socket (err %d).\n",
2610 err);
2611 igmp6_socket = NULL; /* For safety. */
2612 return err;
2615 sk = igmp6_socket->sk;
2616 sk->sk_allocation = GFP_ATOMIC;
2617 sk->sk_prot->unhash(sk);
2619 np = inet6_sk(sk);
2620 np->hop_limit = 1;
2622 #ifdef CONFIG_PROC_FS
2623 proc_net_fops_create(&init_net, "igmp6", S_IRUGO, &igmp6_mc_seq_fops);
2624 proc_net_fops_create(&init_net, "mcfilter6", S_IRUGO, &igmp6_mcf_seq_fops);
2625 #endif
2627 return 0;
2630 void igmp6_cleanup(void)
2632 sock_release(igmp6_socket);
2633 igmp6_socket = NULL; /* for safety */
2635 #ifdef CONFIG_PROC_FS
2636 proc_net_remove(&init_net, "mcfilter6");
2637 proc_net_remove(&init_net, "igmp6");
2638 #endif