pktgen: add needed include file
[linux/fpc-iii.git] / net / ipv6 / mcast.c
blobdb25b8eb62bda9f7e5a0a352ca79a83a7aca00eb
1 /*
2 * Multicast support for IPv6
3 * Linux INET6 implementation
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
16 /* Changes:
18 * yoshfuji : fix format of router-alert option
19 * YOSHIFUJI Hideaki @USAGI:
20 * Fixed source address for MLD message based on
21 * <draft-ietf-magma-mld-source-05.txt>.
22 * YOSHIFUJI Hideaki @USAGI:
23 * - Ignore Queries for invalid addresses.
24 * - MLD for link-local addresses.
25 * David L Stevens <dlstevens@us.ibm.com>:
26 * - MLDv2 support
29 #include <linux/module.h>
30 #include <linux/errno.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
33 #include <linux/socket.h>
34 #include <linux/sockios.h>
35 #include <linux/jiffies.h>
36 #include <linux/times.h>
37 #include <linux/net.h>
38 #include <linux/in.h>
39 #include <linux/in6.h>
40 #include <linux/netdevice.h>
41 #include <linux/if_arp.h>
42 #include <linux/route.h>
43 #include <linux/init.h>
44 #include <linux/proc_fs.h>
45 #include <linux/seq_file.h>
46 #include <linux/slab.h>
47 #include <linux/pkt_sched.h>
48 #include <net/mld.h>
50 #include <linux/netfilter.h>
51 #include <linux/netfilter_ipv6.h>
53 #include <net/net_namespace.h>
54 #include <net/sock.h>
55 #include <net/snmp.h>
57 #include <net/ipv6.h>
58 #include <net/protocol.h>
59 #include <net/if_inet6.h>
60 #include <net/ndisc.h>
61 #include <net/addrconf.h>
62 #include <net/ip6_route.h>
63 #include <net/inet_common.h>
65 #include <net/ip6_checksum.h>
67 /* Set to 3 to get tracing... */
68 #define MCAST_DEBUG 2
70 #if MCAST_DEBUG >= 3
71 #define MDBG(x) printk x
72 #else
73 #define MDBG(x)
74 #endif
76 /* Ensure that we have struct in6_addr aligned on 32bit word. */
77 static void *__mld2_query_bugs[] __attribute__((__unused__)) = {
78 BUILD_BUG_ON_NULL(offsetof(struct mld2_query, mld2q_srcs) % 4),
79 BUILD_BUG_ON_NULL(offsetof(struct mld2_report, mld2r_grec) % 4),
80 BUILD_BUG_ON_NULL(offsetof(struct mld2_grec, grec_mca) % 4)
83 static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
85 /* Big mc list lock for all the sockets */
86 static DEFINE_SPINLOCK(ipv6_sk_mc_lock);
88 static void igmp6_join_group(struct ifmcaddr6 *ma);
89 static void igmp6_leave_group(struct ifmcaddr6 *ma);
90 static void igmp6_timer_handler(unsigned long data);
92 static void mld_gq_timer_expire(unsigned long data);
93 static void mld_ifc_timer_expire(unsigned long data);
94 static void mld_ifc_event(struct inet6_dev *idev);
95 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
96 static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *addr);
97 static void mld_clear_delrec(struct inet6_dev *idev);
98 static int sf_setstate(struct ifmcaddr6 *pmc);
99 static void sf_markstate(struct ifmcaddr6 *pmc);
100 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
101 static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
102 int sfmode, int sfcount, const struct in6_addr *psfsrc,
103 int delta);
104 static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
105 int sfmode, int sfcount, const struct in6_addr *psfsrc,
106 int delta);
107 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
108 struct inet6_dev *idev);
111 #define IGMP6_UNSOLICITED_IVAL (10*HZ)
112 #define MLD_QRV_DEFAULT 2
114 #define MLD_V1_SEEN(idev) (dev_net((idev)->dev)->ipv6.devconf_all->force_mld_version == 1 || \
115 (idev)->cnf.force_mld_version == 1 || \
116 ((idev)->mc_v1_seen && \
117 time_before(jiffies, (idev)->mc_v1_seen)))
119 #define IPV6_MLD_MAX_MSF 64
121 int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
124 * socket join on multicast group
127 #define for_each_pmc_rcu(np, pmc) \
128 for (pmc = rcu_dereference(np->ipv6_mc_list); \
129 pmc != NULL; \
130 pmc = rcu_dereference(pmc->next))
132 int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
134 struct net_device *dev = NULL;
135 struct ipv6_mc_socklist *mc_lst;
136 struct ipv6_pinfo *np = inet6_sk(sk);
137 struct net *net = sock_net(sk);
138 int err;
140 if (!ipv6_addr_is_multicast(addr))
141 return -EINVAL;
143 rcu_read_lock();
144 for_each_pmc_rcu(np, mc_lst) {
145 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
146 ipv6_addr_equal(&mc_lst->addr, addr)) {
147 rcu_read_unlock();
148 return -EADDRINUSE;
151 rcu_read_unlock();
153 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
155 if (mc_lst == NULL)
156 return -ENOMEM;
158 mc_lst->next = NULL;
159 mc_lst->addr = *addr;
161 rcu_read_lock();
162 if (ifindex == 0) {
163 struct rt6_info *rt;
164 rt = rt6_lookup(net, addr, NULL, 0, 0);
165 if (rt) {
166 dev = rt->dst.dev;
167 ip6_rt_put(rt);
169 } else
170 dev = dev_get_by_index_rcu(net, ifindex);
172 if (dev == NULL) {
173 rcu_read_unlock();
174 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
175 return -ENODEV;
178 mc_lst->ifindex = dev->ifindex;
179 mc_lst->sfmode = MCAST_EXCLUDE;
180 rwlock_init(&mc_lst->sflock);
181 mc_lst->sflist = NULL;
184 * now add/increase the group membership on the device
187 err = ipv6_dev_mc_inc(dev, addr);
189 if (err) {
190 rcu_read_unlock();
191 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
192 return err;
195 spin_lock(&ipv6_sk_mc_lock);
196 mc_lst->next = np->ipv6_mc_list;
197 rcu_assign_pointer(np->ipv6_mc_list, mc_lst);
198 spin_unlock(&ipv6_sk_mc_lock);
200 rcu_read_unlock();
202 return 0;
206 * socket leave on multicast group
208 int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
210 struct ipv6_pinfo *np = inet6_sk(sk);
211 struct ipv6_mc_socklist *mc_lst;
212 struct ipv6_mc_socklist __rcu **lnk;
213 struct net *net = sock_net(sk);
215 if (!ipv6_addr_is_multicast(addr))
216 return -EINVAL;
218 spin_lock(&ipv6_sk_mc_lock);
219 for (lnk = &np->ipv6_mc_list;
220 (mc_lst = rcu_dereference_protected(*lnk,
221 lockdep_is_held(&ipv6_sk_mc_lock))) !=NULL ;
222 lnk = &mc_lst->next) {
223 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
224 ipv6_addr_equal(&mc_lst->addr, addr)) {
225 struct net_device *dev;
227 *lnk = mc_lst->next;
228 spin_unlock(&ipv6_sk_mc_lock);
230 rcu_read_lock();
231 dev = dev_get_by_index_rcu(net, mc_lst->ifindex);
232 if (dev != NULL) {
233 struct inet6_dev *idev = __in6_dev_get(dev);
235 (void) ip6_mc_leave_src(sk, mc_lst, idev);
236 if (idev)
237 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
238 } else
239 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
240 rcu_read_unlock();
241 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
242 kfree_rcu(mc_lst, rcu);
243 return 0;
246 spin_unlock(&ipv6_sk_mc_lock);
248 return -EADDRNOTAVAIL;
251 /* called with rcu_read_lock() */
252 static struct inet6_dev *ip6_mc_find_dev_rcu(struct net *net,
253 const struct in6_addr *group,
254 int ifindex)
256 struct net_device *dev = NULL;
257 struct inet6_dev *idev = NULL;
259 if (ifindex == 0) {
260 struct rt6_info *rt = rt6_lookup(net, group, NULL, 0, 0);
262 if (rt) {
263 dev = rt->dst.dev;
264 ip6_rt_put(rt);
266 } else
267 dev = dev_get_by_index_rcu(net, ifindex);
269 if (!dev)
270 return NULL;
271 idev = __in6_dev_get(dev);
272 if (!idev)
273 return NULL;
274 read_lock_bh(&idev->lock);
275 if (idev->dead) {
276 read_unlock_bh(&idev->lock);
277 return NULL;
279 return idev;
282 void ipv6_sock_mc_close(struct sock *sk)
284 struct ipv6_pinfo *np = inet6_sk(sk);
285 struct ipv6_mc_socklist *mc_lst;
286 struct net *net = sock_net(sk);
288 if (!rcu_access_pointer(np->ipv6_mc_list))
289 return;
291 spin_lock(&ipv6_sk_mc_lock);
292 while ((mc_lst = rcu_dereference_protected(np->ipv6_mc_list,
293 lockdep_is_held(&ipv6_sk_mc_lock))) != NULL) {
294 struct net_device *dev;
296 np->ipv6_mc_list = mc_lst->next;
297 spin_unlock(&ipv6_sk_mc_lock);
299 rcu_read_lock();
300 dev = dev_get_by_index_rcu(net, mc_lst->ifindex);
301 if (dev) {
302 struct inet6_dev *idev = __in6_dev_get(dev);
304 (void) ip6_mc_leave_src(sk, mc_lst, idev);
305 if (idev)
306 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
307 } else
308 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
309 rcu_read_unlock();
311 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
312 kfree_rcu(mc_lst, rcu);
314 spin_lock(&ipv6_sk_mc_lock);
316 spin_unlock(&ipv6_sk_mc_lock);
319 int ip6_mc_source(int add, int omode, struct sock *sk,
320 struct group_source_req *pgsr)
322 struct in6_addr *source, *group;
323 struct ipv6_mc_socklist *pmc;
324 struct inet6_dev *idev;
325 struct ipv6_pinfo *inet6 = inet6_sk(sk);
326 struct ip6_sf_socklist *psl;
327 struct net *net = sock_net(sk);
328 int i, j, rv;
329 int leavegroup = 0;
330 int pmclocked = 0;
331 int err;
333 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
334 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
336 if (!ipv6_addr_is_multicast(group))
337 return -EINVAL;
339 rcu_read_lock();
340 idev = ip6_mc_find_dev_rcu(net, group, pgsr->gsr_interface);
341 if (!idev) {
342 rcu_read_unlock();
343 return -ENODEV;
346 err = -EADDRNOTAVAIL;
348 for_each_pmc_rcu(inet6, pmc) {
349 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
350 continue;
351 if (ipv6_addr_equal(&pmc->addr, group))
352 break;
354 if (!pmc) { /* must have a prior join */
355 err = -EINVAL;
356 goto done;
358 /* if a source filter was set, must be the same mode as before */
359 if (pmc->sflist) {
360 if (pmc->sfmode != omode) {
361 err = -EINVAL;
362 goto done;
364 } else if (pmc->sfmode != omode) {
365 /* allow mode switches for empty-set filters */
366 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
367 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
368 pmc->sfmode = omode;
371 write_lock(&pmc->sflock);
372 pmclocked = 1;
374 psl = pmc->sflist;
375 if (!add) {
376 if (!psl)
377 goto done; /* err = -EADDRNOTAVAIL */
378 rv = !0;
379 for (i=0; i<psl->sl_count; i++) {
380 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
381 if (rv == 0)
382 break;
384 if (rv) /* source not found */
385 goto done; /* err = -EADDRNOTAVAIL */
387 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
388 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
389 leavegroup = 1;
390 goto done;
393 /* update the interface filter */
394 ip6_mc_del_src(idev, group, omode, 1, source, 1);
396 for (j=i+1; j<psl->sl_count; j++)
397 psl->sl_addr[j-1] = psl->sl_addr[j];
398 psl->sl_count--;
399 err = 0;
400 goto done;
402 /* else, add a new source to the filter */
404 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
405 err = -ENOBUFS;
406 goto done;
408 if (!psl || psl->sl_count == psl->sl_max) {
409 struct ip6_sf_socklist *newpsl;
410 int count = IP6_SFBLOCK;
412 if (psl)
413 count += psl->sl_max;
414 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
415 if (!newpsl) {
416 err = -ENOBUFS;
417 goto done;
419 newpsl->sl_max = count;
420 newpsl->sl_count = count - IP6_SFBLOCK;
421 if (psl) {
422 for (i=0; i<psl->sl_count; i++)
423 newpsl->sl_addr[i] = psl->sl_addr[i];
424 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
426 pmc->sflist = psl = newpsl;
428 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
429 for (i=0; i<psl->sl_count; i++) {
430 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
431 if (rv == 0) /* There is an error in the address. */
432 goto done;
434 for (j=psl->sl_count-1; j>=i; j--)
435 psl->sl_addr[j+1] = psl->sl_addr[j];
436 psl->sl_addr[i] = *source;
437 psl->sl_count++;
438 err = 0;
439 /* update the interface list */
440 ip6_mc_add_src(idev, group, omode, 1, source, 1);
441 done:
442 if (pmclocked)
443 write_unlock(&pmc->sflock);
444 read_unlock_bh(&idev->lock);
445 rcu_read_unlock();
446 if (leavegroup)
447 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
448 return err;
451 int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
453 const struct in6_addr *group;
454 struct ipv6_mc_socklist *pmc;
455 struct inet6_dev *idev;
456 struct ipv6_pinfo *inet6 = inet6_sk(sk);
457 struct ip6_sf_socklist *newpsl, *psl;
458 struct net *net = sock_net(sk);
459 int leavegroup = 0;
460 int i, err;
462 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
464 if (!ipv6_addr_is_multicast(group))
465 return -EINVAL;
466 if (gsf->gf_fmode != MCAST_INCLUDE &&
467 gsf->gf_fmode != MCAST_EXCLUDE)
468 return -EINVAL;
470 rcu_read_lock();
471 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
473 if (!idev) {
474 rcu_read_unlock();
475 return -ENODEV;
478 err = 0;
480 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
481 leavegroup = 1;
482 goto done;
485 for_each_pmc_rcu(inet6, pmc) {
486 if (pmc->ifindex != gsf->gf_interface)
487 continue;
488 if (ipv6_addr_equal(&pmc->addr, group))
489 break;
491 if (!pmc) { /* must have a prior join */
492 err = -EINVAL;
493 goto done;
495 if (gsf->gf_numsrc) {
496 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
497 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 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
520 write_lock(&pmc->sflock);
521 psl = pmc->sflist;
522 if (psl) {
523 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
524 psl->sl_count, psl->sl_addr, 0);
525 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
526 } else
527 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
528 pmc->sflist = newpsl;
529 pmc->sfmode = gsf->gf_fmode;
530 write_unlock(&pmc->sflock);
531 err = 0;
532 done:
533 read_unlock_bh(&idev->lock);
534 rcu_read_unlock();
535 if (leavegroup)
536 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
537 return err;
540 int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
541 struct group_filter __user *optval, int __user *optlen)
543 int err, i, count, copycount;
544 const struct in6_addr *group;
545 struct ipv6_mc_socklist *pmc;
546 struct inet6_dev *idev;
547 struct ipv6_pinfo *inet6 = inet6_sk(sk);
548 struct ip6_sf_socklist *psl;
549 struct net *net = sock_net(sk);
551 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
553 if (!ipv6_addr_is_multicast(group))
554 return -EINVAL;
556 rcu_read_lock();
557 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
559 if (!idev) {
560 rcu_read_unlock();
561 return -ENODEV;
564 err = -EADDRNOTAVAIL;
566 * changes to the ipv6_mc_list require the socket lock and
567 * a read lock on ip6_sk_mc_lock. We have the socket lock,
568 * so reading the list is safe.
571 for_each_pmc_rcu(inet6, pmc) {
572 if (pmc->ifindex != gsf->gf_interface)
573 continue;
574 if (ipv6_addr_equal(group, &pmc->addr))
575 break;
577 if (!pmc) /* must have a prior join */
578 goto done;
579 gsf->gf_fmode = pmc->sfmode;
580 psl = pmc->sflist;
581 count = psl ? psl->sl_count : 0;
582 read_unlock_bh(&idev->lock);
583 rcu_read_unlock();
585 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
586 gsf->gf_numsrc = count;
587 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
588 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
589 return -EFAULT;
591 /* changes to psl require the socket lock, a read lock on
592 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
593 * have the socket lock, so reading here is safe.
595 for (i=0; i<copycount; i++) {
596 struct sockaddr_in6 *psin6;
597 struct sockaddr_storage ss;
599 psin6 = (struct sockaddr_in6 *)&ss;
600 memset(&ss, 0, sizeof(ss));
601 psin6->sin6_family = AF_INET6;
602 psin6->sin6_addr = psl->sl_addr[i];
603 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
604 return -EFAULT;
606 return 0;
607 done:
608 read_unlock_bh(&idev->lock);
609 rcu_read_unlock();
610 return err;
613 bool inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
614 const struct in6_addr *src_addr)
616 struct ipv6_pinfo *np = inet6_sk(sk);
617 struct ipv6_mc_socklist *mc;
618 struct ip6_sf_socklist *psl;
619 bool rv = true;
621 rcu_read_lock();
622 for_each_pmc_rcu(np, mc) {
623 if (ipv6_addr_equal(&mc->addr, mc_addr))
624 break;
626 if (!mc) {
627 rcu_read_unlock();
628 return true;
630 read_lock(&mc->sflock);
631 psl = mc->sflist;
632 if (!psl) {
633 rv = mc->sfmode == MCAST_EXCLUDE;
634 } else {
635 int i;
637 for (i=0; i<psl->sl_count; i++) {
638 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
639 break;
641 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
642 rv = false;
643 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
644 rv = false;
646 read_unlock(&mc->sflock);
647 rcu_read_unlock();
649 return rv;
652 static void ma_put(struct ifmcaddr6 *mc)
654 if (atomic_dec_and_test(&mc->mca_refcnt)) {
655 in6_dev_put(mc->idev);
656 kfree(mc);
660 static void igmp6_group_added(struct ifmcaddr6 *mc)
662 struct net_device *dev = mc->idev->dev;
663 char buf[MAX_ADDR_LEN];
665 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
666 IPV6_ADDR_SCOPE_LINKLOCAL)
667 return;
669 spin_lock_bh(&mc->mca_lock);
670 if (!(mc->mca_flags&MAF_LOADED)) {
671 mc->mca_flags |= MAF_LOADED;
672 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
673 dev_mc_add(dev, buf);
675 spin_unlock_bh(&mc->mca_lock);
677 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
678 return;
680 if (MLD_V1_SEEN(mc->idev)) {
681 igmp6_join_group(mc);
682 return;
684 /* else v2 */
686 mc->mca_crcount = mc->idev->mc_qrv;
687 mld_ifc_event(mc->idev);
690 static void igmp6_group_dropped(struct ifmcaddr6 *mc)
692 struct net_device *dev = mc->idev->dev;
693 char buf[MAX_ADDR_LEN];
695 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
696 IPV6_ADDR_SCOPE_LINKLOCAL)
697 return;
699 spin_lock_bh(&mc->mca_lock);
700 if (mc->mca_flags&MAF_LOADED) {
701 mc->mca_flags &= ~MAF_LOADED;
702 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
703 dev_mc_del(dev, buf);
706 if (mc->mca_flags & MAF_NOREPORT)
707 goto done;
708 spin_unlock_bh(&mc->mca_lock);
710 if (!mc->idev->dead)
711 igmp6_leave_group(mc);
713 spin_lock_bh(&mc->mca_lock);
714 if (del_timer(&mc->mca_timer))
715 atomic_dec(&mc->mca_refcnt);
716 done:
717 ip6_mc_clear_src(mc);
718 spin_unlock_bh(&mc->mca_lock);
722 * deleted ifmcaddr6 manipulation
724 static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
726 struct ifmcaddr6 *pmc;
728 /* this is an "ifmcaddr6" for convenience; only the fields below
729 * are actually used. In particular, the refcnt and users are not
730 * used for management of the delete list. Using the same structure
731 * for deleted items allows change reports to use common code with
732 * non-deleted or query-response MCA's.
734 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
735 if (!pmc)
736 return;
738 spin_lock_bh(&im->mca_lock);
739 spin_lock_init(&pmc->mca_lock);
740 pmc->idev = im->idev;
741 in6_dev_hold(idev);
742 pmc->mca_addr = im->mca_addr;
743 pmc->mca_crcount = idev->mc_qrv;
744 pmc->mca_sfmode = im->mca_sfmode;
745 if (pmc->mca_sfmode == MCAST_INCLUDE) {
746 struct ip6_sf_list *psf;
748 pmc->mca_tomb = im->mca_tomb;
749 pmc->mca_sources = im->mca_sources;
750 im->mca_tomb = im->mca_sources = NULL;
751 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
752 psf->sf_crcount = pmc->mca_crcount;
754 spin_unlock_bh(&im->mca_lock);
756 spin_lock_bh(&idev->mc_lock);
757 pmc->next = idev->mc_tomb;
758 idev->mc_tomb = pmc;
759 spin_unlock_bh(&idev->mc_lock);
762 static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *pmca)
764 struct ifmcaddr6 *pmc, *pmc_prev;
765 struct ip6_sf_list *psf, *psf_next;
767 spin_lock_bh(&idev->mc_lock);
768 pmc_prev = NULL;
769 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
770 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
771 break;
772 pmc_prev = pmc;
774 if (pmc) {
775 if (pmc_prev)
776 pmc_prev->next = pmc->next;
777 else
778 idev->mc_tomb = pmc->next;
780 spin_unlock_bh(&idev->mc_lock);
782 if (pmc) {
783 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
784 psf_next = psf->sf_next;
785 kfree(psf);
787 in6_dev_put(pmc->idev);
788 kfree(pmc);
792 static void mld_clear_delrec(struct inet6_dev *idev)
794 struct ifmcaddr6 *pmc, *nextpmc;
796 spin_lock_bh(&idev->mc_lock);
797 pmc = idev->mc_tomb;
798 idev->mc_tomb = NULL;
799 spin_unlock_bh(&idev->mc_lock);
801 for (; pmc; pmc = nextpmc) {
802 nextpmc = pmc->next;
803 ip6_mc_clear_src(pmc);
804 in6_dev_put(pmc->idev);
805 kfree(pmc);
808 /* clear dead sources, too */
809 read_lock_bh(&idev->lock);
810 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
811 struct ip6_sf_list *psf, *psf_next;
813 spin_lock_bh(&pmc->mca_lock);
814 psf = pmc->mca_tomb;
815 pmc->mca_tomb = NULL;
816 spin_unlock_bh(&pmc->mca_lock);
817 for (; psf; psf=psf_next) {
818 psf_next = psf->sf_next;
819 kfree(psf);
822 read_unlock_bh(&idev->lock);
827 * device multicast group inc (add if not found)
829 int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
831 struct ifmcaddr6 *mc;
832 struct inet6_dev *idev;
834 /* we need to take a reference on idev */
835 idev = in6_dev_get(dev);
837 if (idev == NULL)
838 return -EINVAL;
840 write_lock_bh(&idev->lock);
841 if (idev->dead) {
842 write_unlock_bh(&idev->lock);
843 in6_dev_put(idev);
844 return -ENODEV;
847 for (mc = idev->mc_list; mc; mc = mc->next) {
848 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
849 mc->mca_users++;
850 write_unlock_bh(&idev->lock);
851 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
852 NULL, 0);
853 in6_dev_put(idev);
854 return 0;
859 * not found: create a new one.
862 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
864 if (mc == NULL) {
865 write_unlock_bh(&idev->lock);
866 in6_dev_put(idev);
867 return -ENOMEM;
870 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
872 mc->mca_addr = *addr;
873 mc->idev = idev; /* (reference taken) */
874 mc->mca_users = 1;
875 /* mca_stamp should be updated upon changes */
876 mc->mca_cstamp = mc->mca_tstamp = jiffies;
877 atomic_set(&mc->mca_refcnt, 2);
878 spin_lock_init(&mc->mca_lock);
880 /* initial mode is (EX, empty) */
881 mc->mca_sfmode = MCAST_EXCLUDE;
882 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
884 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
885 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
886 mc->mca_flags |= MAF_NOREPORT;
888 mc->next = idev->mc_list;
889 idev->mc_list = mc;
890 write_unlock_bh(&idev->lock);
892 mld_del_delrec(idev, &mc->mca_addr);
893 igmp6_group_added(mc);
894 ma_put(mc);
895 return 0;
899 * device multicast group del
901 int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
903 struct ifmcaddr6 *ma, **map;
905 write_lock_bh(&idev->lock);
906 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
907 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
908 if (--ma->mca_users == 0) {
909 *map = ma->next;
910 write_unlock_bh(&idev->lock);
912 igmp6_group_dropped(ma);
914 ma_put(ma);
915 return 0;
917 write_unlock_bh(&idev->lock);
918 return 0;
921 write_unlock_bh(&idev->lock);
923 return -ENOENT;
926 int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
928 struct inet6_dev *idev;
929 int err;
931 rcu_read_lock();
933 idev = __in6_dev_get(dev);
934 if (!idev)
935 err = -ENODEV;
936 else
937 err = __ipv6_dev_mc_dec(idev, addr);
939 rcu_read_unlock();
940 return err;
944 * check if the interface/address pair is valid
946 bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
947 const struct in6_addr *src_addr)
949 struct inet6_dev *idev;
950 struct ifmcaddr6 *mc;
951 bool rv = false;
953 rcu_read_lock();
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 = true; /* don't filter unspecified source */
980 read_unlock_bh(&idev->lock);
982 rcu_read_unlock();
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);
1003 static void mld_dad_start_timer(struct inet6_dev *idev, int delay)
1005 int tv = net_random() % delay;
1007 if (!mod_timer(&idev->mc_dad_timer, jiffies+tv+2))
1008 in6_dev_hold(idev);
1012 * IGMP handling (alias multicast ICMPv6 messages)
1015 static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1017 unsigned long delay = resptime;
1019 /* Do not start timer for these addresses */
1020 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1021 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1022 return;
1024 if (del_timer(&ma->mca_timer)) {
1025 atomic_dec(&ma->mca_refcnt);
1026 delay = ma->mca_timer.expires - jiffies;
1029 if (delay >= resptime) {
1030 if (resptime)
1031 delay = net_random() % resptime;
1032 else
1033 delay = 1;
1035 ma->mca_timer.expires = jiffies + delay;
1036 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1037 atomic_inc(&ma->mca_refcnt);
1038 ma->mca_flags |= MAF_TIMER_RUNNING;
1041 /* mark EXCLUDE-mode sources */
1042 static bool mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1043 const struct in6_addr *srcs)
1045 struct ip6_sf_list *psf;
1046 int i, scount;
1048 scount = 0;
1049 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1050 if (scount == nsrcs)
1051 break;
1052 for (i=0; i<nsrcs; i++) {
1053 /* skip inactive filters */
1054 if (psf->sf_count[MCAST_INCLUDE] ||
1055 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1056 psf->sf_count[MCAST_EXCLUDE])
1057 break;
1058 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1059 scount++;
1060 break;
1064 pmc->mca_flags &= ~MAF_GSQUERY;
1065 if (scount == nsrcs) /* all sources excluded */
1066 return false;
1067 return true;
1070 static bool mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1071 const struct in6_addr *srcs)
1073 struct ip6_sf_list *psf;
1074 int i, scount;
1076 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1077 return mld_xmarksources(pmc, nsrcs, srcs);
1079 /* mark INCLUDE-mode sources */
1081 scount = 0;
1082 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1083 if (scount == nsrcs)
1084 break;
1085 for (i=0; i<nsrcs; i++) {
1086 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1087 psf->sf_gsresp = 1;
1088 scount++;
1089 break;
1093 if (!scount) {
1094 pmc->mca_flags &= ~MAF_GSQUERY;
1095 return false;
1097 pmc->mca_flags |= MAF_GSQUERY;
1098 return true;
1101 /* called with rcu_read_lock() */
1102 int igmp6_event_query(struct sk_buff *skb)
1104 struct mld2_query *mlh2 = NULL;
1105 struct ifmcaddr6 *ma;
1106 const struct in6_addr *group;
1107 unsigned long max_delay;
1108 struct inet6_dev *idev;
1109 struct mld_msg *mld;
1110 int group_type;
1111 int mark = 0;
1112 int len;
1114 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1115 return -EINVAL;
1117 /* compute payload length excluding extension headers */
1118 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
1119 len -= skb_network_header_len(skb);
1121 /* Drop queries with not link local source */
1122 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
1123 return -EINVAL;
1125 idev = __in6_dev_get(skb->dev);
1127 if (idev == NULL)
1128 return 0;
1130 mld = (struct mld_msg *)icmp6_hdr(skb);
1131 group = &mld->mld_mca;
1132 group_type = ipv6_addr_type(group);
1134 if (group_type != IPV6_ADDR_ANY &&
1135 !(group_type&IPV6_ADDR_MULTICAST))
1136 return -EINVAL;
1138 if (len == 24) {
1139 int switchback;
1140 /* MLDv1 router present */
1142 /* Translate milliseconds to jiffies */
1143 max_delay = (ntohs(mld->mld_maxdelay)*HZ)/1000;
1145 switchback = (idev->mc_qrv + 1) * max_delay;
1146 idev->mc_v1_seen = jiffies + switchback;
1148 /* cancel the interface change timer */
1149 idev->mc_ifc_count = 0;
1150 if (del_timer(&idev->mc_ifc_timer))
1151 __in6_dev_put(idev);
1152 /* clear deleted report items */
1153 mld_clear_delrec(idev);
1154 } else if (len >= 28) {
1155 int srcs_offset = sizeof(struct mld2_query) -
1156 sizeof(struct icmp6hdr);
1157 if (!pskb_may_pull(skb, srcs_offset))
1158 return -EINVAL;
1160 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1161 max_delay = (MLDV2_MRC(ntohs(mlh2->mld2q_mrc))*HZ)/1000;
1162 if (!max_delay)
1163 max_delay = 1;
1164 idev->mc_maxdelay = max_delay;
1165 if (mlh2->mld2q_qrv)
1166 idev->mc_qrv = mlh2->mld2q_qrv;
1167 if (group_type == IPV6_ADDR_ANY) { /* general query */
1168 if (mlh2->mld2q_nsrcs)
1169 return -EINVAL; /* no sources allowed */
1171 mld_gq_start_timer(idev);
1172 return 0;
1174 /* mark sources to include, if group & source-specific */
1175 if (mlh2->mld2q_nsrcs != 0) {
1176 if (!pskb_may_pull(skb, srcs_offset +
1177 ntohs(mlh2->mld2q_nsrcs) * sizeof(struct in6_addr)))
1178 return -EINVAL;
1180 mlh2 = (struct mld2_query *)skb_transport_header(skb);
1181 mark = 1;
1183 } else
1184 return -EINVAL;
1186 read_lock_bh(&idev->lock);
1187 if (group_type == IPV6_ADDR_ANY) {
1188 for (ma = idev->mc_list; ma; ma=ma->next) {
1189 spin_lock_bh(&ma->mca_lock);
1190 igmp6_group_queried(ma, max_delay);
1191 spin_unlock_bh(&ma->mca_lock);
1193 } else {
1194 for (ma = idev->mc_list; ma; ma=ma->next) {
1195 if (!ipv6_addr_equal(group, &ma->mca_addr))
1196 continue;
1197 spin_lock_bh(&ma->mca_lock);
1198 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1199 /* gsquery <- gsquery && mark */
1200 if (!mark)
1201 ma->mca_flags &= ~MAF_GSQUERY;
1202 } else {
1203 /* gsquery <- mark */
1204 if (mark)
1205 ma->mca_flags |= MAF_GSQUERY;
1206 else
1207 ma->mca_flags &= ~MAF_GSQUERY;
1209 if (!(ma->mca_flags & MAF_GSQUERY) ||
1210 mld_marksources(ma, ntohs(mlh2->mld2q_nsrcs), mlh2->mld2q_srcs))
1211 igmp6_group_queried(ma, max_delay);
1212 spin_unlock_bh(&ma->mca_lock);
1213 break;
1216 read_unlock_bh(&idev->lock);
1218 return 0;
1221 /* called with rcu_read_lock() */
1222 int igmp6_event_report(struct sk_buff *skb)
1224 struct ifmcaddr6 *ma;
1225 struct inet6_dev *idev;
1226 struct mld_msg *mld;
1227 int addr_type;
1229 /* Our own report looped back. Ignore it. */
1230 if (skb->pkt_type == PACKET_LOOPBACK)
1231 return 0;
1233 /* send our report if the MC router may not have heard this report */
1234 if (skb->pkt_type != PACKET_MULTICAST &&
1235 skb->pkt_type != PACKET_BROADCAST)
1236 return 0;
1238 if (!pskb_may_pull(skb, sizeof(*mld) - sizeof(struct icmp6hdr)))
1239 return -EINVAL;
1241 mld = (struct mld_msg *)icmp6_hdr(skb);
1243 /* Drop reports with not link local source */
1244 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
1245 if (addr_type != IPV6_ADDR_ANY &&
1246 !(addr_type&IPV6_ADDR_LINKLOCAL))
1247 return -EINVAL;
1249 idev = __in6_dev_get(skb->dev);
1250 if (idev == NULL)
1251 return -ENODEV;
1254 * Cancel the timer for this group
1257 read_lock_bh(&idev->lock);
1258 for (ma = idev->mc_list; ma; ma=ma->next) {
1259 if (ipv6_addr_equal(&ma->mca_addr, &mld->mld_mca)) {
1260 spin_lock(&ma->mca_lock);
1261 if (del_timer(&ma->mca_timer))
1262 atomic_dec(&ma->mca_refcnt);
1263 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1264 spin_unlock(&ma->mca_lock);
1265 break;
1268 read_unlock_bh(&idev->lock);
1269 return 0;
1272 static bool is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1273 int gdeleted, int sdeleted)
1275 switch (type) {
1276 case MLD2_MODE_IS_INCLUDE:
1277 case MLD2_MODE_IS_EXCLUDE:
1278 if (gdeleted || sdeleted)
1279 return false;
1280 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1281 if (pmc->mca_sfmode == MCAST_INCLUDE)
1282 return true;
1283 /* don't include if this source is excluded
1284 * in all filters
1286 if (psf->sf_count[MCAST_INCLUDE])
1287 return type == MLD2_MODE_IS_INCLUDE;
1288 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1289 psf->sf_count[MCAST_EXCLUDE];
1291 return false;
1292 case MLD2_CHANGE_TO_INCLUDE:
1293 if (gdeleted || sdeleted)
1294 return false;
1295 return psf->sf_count[MCAST_INCLUDE] != 0;
1296 case MLD2_CHANGE_TO_EXCLUDE:
1297 if (gdeleted || sdeleted)
1298 return false;
1299 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1300 psf->sf_count[MCAST_INCLUDE])
1301 return false;
1302 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1303 psf->sf_count[MCAST_EXCLUDE];
1304 case MLD2_ALLOW_NEW_SOURCES:
1305 if (gdeleted || !psf->sf_crcount)
1306 return false;
1307 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1308 case MLD2_BLOCK_OLD_SOURCES:
1309 if (pmc->mca_sfmode == MCAST_INCLUDE)
1310 return gdeleted || (psf->sf_crcount && sdeleted);
1311 return psf->sf_crcount && !gdeleted && !sdeleted;
1313 return false;
1316 static int
1317 mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1319 struct ip6_sf_list *psf;
1320 int scount = 0;
1322 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1323 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1324 continue;
1325 scount++;
1327 return scount;
1330 static void ip6_mc_hdr(struct sock *sk, struct sk_buff *skb,
1331 struct net_device *dev,
1332 const struct in6_addr *saddr,
1333 const struct in6_addr *daddr,
1334 int proto, int len)
1336 struct ipv6hdr *hdr;
1338 skb->protocol = htons(ETH_P_IPV6);
1339 skb->dev = dev;
1341 skb_reset_network_header(skb);
1342 skb_put(skb, sizeof(struct ipv6hdr));
1343 hdr = ipv6_hdr(skb);
1345 ip6_flow_hdr(hdr, 0, 0);
1347 hdr->payload_len = htons(len);
1348 hdr->nexthdr = proto;
1349 hdr->hop_limit = inet6_sk(sk)->hop_limit;
1351 hdr->saddr = *saddr;
1352 hdr->daddr = *daddr;
1355 static struct sk_buff *mld_newpack(struct inet6_dev *idev, int size)
1357 struct net_device *dev = idev->dev;
1358 struct net *net = dev_net(dev);
1359 struct sock *sk = net->ipv6.igmp_sk;
1360 struct sk_buff *skb;
1361 struct mld2_report *pmr;
1362 struct in6_addr addr_buf;
1363 const struct in6_addr *saddr;
1364 int hlen = LL_RESERVED_SPACE(dev);
1365 int tlen = dev->needed_tailroom;
1366 int err;
1367 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1368 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1369 IPV6_TLV_PADN, 0 };
1371 /* we assume size > sizeof(ra) here */
1372 size += hlen + tlen;
1373 /* limit our allocations to order-0 page */
1374 size = min_t(int, size, SKB_MAX_ORDER(0, 0));
1375 skb = sock_alloc_send_skb(sk, size, 1, &err);
1377 if (!skb)
1378 return NULL;
1380 skb->priority = TC_PRIO_CONTROL;
1381 skb_reserve(skb, hlen);
1383 if (__ipv6_get_lladdr(idev, &addr_buf, IFA_F_TENTATIVE)) {
1384 /* <draft-ietf-magma-mld-source-05.txt>:
1385 * use unspecified address as the source address
1386 * when a valid link-local address is not available.
1388 saddr = &in6addr_any;
1389 } else
1390 saddr = &addr_buf;
1392 ip6_mc_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
1394 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1396 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
1397 skb_put(skb, sizeof(*pmr));
1398 pmr = (struct mld2_report *)skb_transport_header(skb);
1399 pmr->mld2r_type = ICMPV6_MLD2_REPORT;
1400 pmr->mld2r_resv1 = 0;
1401 pmr->mld2r_cksum = 0;
1402 pmr->mld2r_resv2 = 0;
1403 pmr->mld2r_ngrec = 0;
1404 return skb;
1407 static void mld_sendpack(struct sk_buff *skb)
1409 struct ipv6hdr *pip6 = ipv6_hdr(skb);
1410 struct mld2_report *pmr =
1411 (struct mld2_report *)skb_transport_header(skb);
1412 int payload_len, mldlen;
1413 struct inet6_dev *idev;
1414 struct net *net = dev_net(skb->dev);
1415 int err;
1416 struct flowi6 fl6;
1417 struct dst_entry *dst;
1419 rcu_read_lock();
1420 idev = __in6_dev_get(skb->dev);
1421 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
1423 payload_len = (skb_tail_pointer(skb) - skb_network_header(skb)) -
1424 sizeof(*pip6);
1425 mldlen = skb_tail_pointer(skb) - skb_transport_header(skb);
1426 pip6->payload_len = htons(payload_len);
1428 pmr->mld2r_cksum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1429 IPPROTO_ICMPV6,
1430 csum_partial(skb_transport_header(skb),
1431 mldlen, 0));
1433 icmpv6_flow_init(net->ipv6.igmp_sk, &fl6, ICMPV6_MLD2_REPORT,
1434 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1435 skb->dev->ifindex);
1436 dst = icmp6_dst_alloc(skb->dev, &fl6);
1438 err = 0;
1439 if (IS_ERR(dst)) {
1440 err = PTR_ERR(dst);
1441 dst = NULL;
1443 skb_dst_set(skb, dst);
1444 if (err)
1445 goto err_out;
1447 payload_len = skb->len;
1449 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
1450 dst_output);
1451 out:
1452 if (!err) {
1453 ICMP6MSGOUT_INC_STATS_BH(net, idev, ICMPV6_MLD2_REPORT);
1454 ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
1455 IP6_UPD_PO_STATS_BH(net, idev, IPSTATS_MIB_OUTMCAST, payload_len);
1456 } else
1457 IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_OUTDISCARDS);
1459 rcu_read_unlock();
1460 return;
1462 err_out:
1463 kfree_skb(skb);
1464 goto out;
1467 static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1469 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
1472 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1473 int type, struct mld2_grec **ppgr)
1475 struct net_device *dev = pmc->idev->dev;
1476 struct mld2_report *pmr;
1477 struct mld2_grec *pgr;
1479 if (!skb)
1480 skb = mld_newpack(pmc->idev, dev->mtu);
1481 if (!skb)
1482 return NULL;
1483 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1484 pgr->grec_type = type;
1485 pgr->grec_auxwords = 0;
1486 pgr->grec_nsrcs = 0;
1487 pgr->grec_mca = pmc->mca_addr; /* structure copy */
1488 pmr = (struct mld2_report *)skb_transport_header(skb);
1489 pmr->mld2r_ngrec = htons(ntohs(pmr->mld2r_ngrec)+1);
1490 *ppgr = pgr;
1491 return skb;
1494 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1495 skb_tailroom(skb)) : 0)
1497 static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1498 int type, int gdeleted, int sdeleted)
1500 struct inet6_dev *idev = pmc->idev;
1501 struct net_device *dev = idev->dev;
1502 struct mld2_report *pmr;
1503 struct mld2_grec *pgr = NULL;
1504 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1505 int scount, stotal, first, isquery, truncate;
1507 if (pmc->mca_flags & MAF_NOREPORT)
1508 return skb;
1510 isquery = type == MLD2_MODE_IS_INCLUDE ||
1511 type == MLD2_MODE_IS_EXCLUDE;
1512 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1513 type == MLD2_CHANGE_TO_EXCLUDE;
1515 stotal = scount = 0;
1517 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1519 if (!*psf_list)
1520 goto empty_source;
1522 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
1524 /* EX and TO_EX get a fresh packet, if needed */
1525 if (truncate) {
1526 if (pmr && pmr->mld2r_ngrec &&
1527 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1528 if (skb)
1529 mld_sendpack(skb);
1530 skb = mld_newpack(idev, dev->mtu);
1533 first = 1;
1534 psf_prev = NULL;
1535 for (psf=*psf_list; psf; psf=psf_next) {
1536 struct in6_addr *psrc;
1538 psf_next = psf->sf_next;
1540 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1541 psf_prev = psf;
1542 continue;
1545 /* clear marks on query responses */
1546 if (isquery)
1547 psf->sf_gsresp = 0;
1549 if (AVAILABLE(skb) < sizeof(*psrc) +
1550 first*sizeof(struct mld2_grec)) {
1551 if (truncate && !first)
1552 break; /* truncate these */
1553 if (pgr)
1554 pgr->grec_nsrcs = htons(scount);
1555 if (skb)
1556 mld_sendpack(skb);
1557 skb = mld_newpack(idev, dev->mtu);
1558 first = 1;
1559 scount = 0;
1561 if (first) {
1562 skb = add_grhead(skb, pmc, type, &pgr);
1563 first = 0;
1565 if (!skb)
1566 return NULL;
1567 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1568 *psrc = psf->sf_addr;
1569 scount++; stotal++;
1570 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1571 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1572 psf->sf_crcount--;
1573 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1574 if (psf_prev)
1575 psf_prev->sf_next = psf->sf_next;
1576 else
1577 *psf_list = psf->sf_next;
1578 kfree(psf);
1579 continue;
1582 psf_prev = psf;
1585 empty_source:
1586 if (!stotal) {
1587 if (type == MLD2_ALLOW_NEW_SOURCES ||
1588 type == MLD2_BLOCK_OLD_SOURCES)
1589 return skb;
1590 if (pmc->mca_crcount || isquery) {
1591 /* make sure we have room for group header */
1592 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1593 mld_sendpack(skb);
1594 skb = NULL; /* add_grhead will get a new one */
1596 skb = add_grhead(skb, pmc, type, &pgr);
1599 if (pgr)
1600 pgr->grec_nsrcs = htons(scount);
1602 if (isquery)
1603 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1604 return skb;
1607 static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1609 struct sk_buff *skb = NULL;
1610 int type;
1612 read_lock_bh(&idev->lock);
1613 if (!pmc) {
1614 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1615 if (pmc->mca_flags & MAF_NOREPORT)
1616 continue;
1617 spin_lock_bh(&pmc->mca_lock);
1618 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1619 type = MLD2_MODE_IS_EXCLUDE;
1620 else
1621 type = MLD2_MODE_IS_INCLUDE;
1622 skb = add_grec(skb, pmc, type, 0, 0);
1623 spin_unlock_bh(&pmc->mca_lock);
1625 } else {
1626 spin_lock_bh(&pmc->mca_lock);
1627 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1628 type = MLD2_MODE_IS_EXCLUDE;
1629 else
1630 type = MLD2_MODE_IS_INCLUDE;
1631 skb = add_grec(skb, pmc, type, 0, 0);
1632 spin_unlock_bh(&pmc->mca_lock);
1634 read_unlock_bh(&idev->lock);
1635 if (skb)
1636 mld_sendpack(skb);
1640 * remove zero-count source records from a source filter list
1642 static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1644 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1646 psf_prev = NULL;
1647 for (psf=*ppsf; psf; psf = psf_next) {
1648 psf_next = psf->sf_next;
1649 if (psf->sf_crcount == 0) {
1650 if (psf_prev)
1651 psf_prev->sf_next = psf->sf_next;
1652 else
1653 *ppsf = psf->sf_next;
1654 kfree(psf);
1655 } else
1656 psf_prev = psf;
1660 static void mld_send_cr(struct inet6_dev *idev)
1662 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1663 struct sk_buff *skb = NULL;
1664 int type, dtype;
1666 read_lock_bh(&idev->lock);
1667 spin_lock(&idev->mc_lock);
1669 /* deleted MCA's */
1670 pmc_prev = NULL;
1671 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1672 pmc_next = pmc->next;
1673 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1674 type = MLD2_BLOCK_OLD_SOURCES;
1675 dtype = MLD2_BLOCK_OLD_SOURCES;
1676 skb = add_grec(skb, pmc, type, 1, 0);
1677 skb = add_grec(skb, pmc, dtype, 1, 1);
1679 if (pmc->mca_crcount) {
1680 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1681 type = MLD2_CHANGE_TO_INCLUDE;
1682 skb = add_grec(skb, pmc, type, 1, 0);
1684 pmc->mca_crcount--;
1685 if (pmc->mca_crcount == 0) {
1686 mld_clear_zeros(&pmc->mca_tomb);
1687 mld_clear_zeros(&pmc->mca_sources);
1690 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1691 !pmc->mca_sources) {
1692 if (pmc_prev)
1693 pmc_prev->next = pmc_next;
1694 else
1695 idev->mc_tomb = pmc_next;
1696 in6_dev_put(pmc->idev);
1697 kfree(pmc);
1698 } else
1699 pmc_prev = pmc;
1701 spin_unlock(&idev->mc_lock);
1703 /* change recs */
1704 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1705 spin_lock_bh(&pmc->mca_lock);
1706 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1707 type = MLD2_BLOCK_OLD_SOURCES;
1708 dtype = MLD2_ALLOW_NEW_SOURCES;
1709 } else {
1710 type = MLD2_ALLOW_NEW_SOURCES;
1711 dtype = MLD2_BLOCK_OLD_SOURCES;
1713 skb = add_grec(skb, pmc, type, 0, 0);
1714 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1716 /* filter mode changes */
1717 if (pmc->mca_crcount) {
1718 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1719 type = MLD2_CHANGE_TO_EXCLUDE;
1720 else
1721 type = MLD2_CHANGE_TO_INCLUDE;
1722 skb = add_grec(skb, pmc, type, 0, 0);
1723 pmc->mca_crcount--;
1725 spin_unlock_bh(&pmc->mca_lock);
1727 read_unlock_bh(&idev->lock);
1728 if (!skb)
1729 return;
1730 (void) mld_sendpack(skb);
1733 static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1735 struct net *net = dev_net(dev);
1736 struct sock *sk = net->ipv6.igmp_sk;
1737 struct inet6_dev *idev;
1738 struct sk_buff *skb;
1739 struct mld_msg *hdr;
1740 const struct in6_addr *snd_addr, *saddr;
1741 struct in6_addr addr_buf;
1742 int hlen = LL_RESERVED_SPACE(dev);
1743 int tlen = dev->needed_tailroom;
1744 int err, len, payload_len, full_len;
1745 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1746 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1747 IPV6_TLV_PADN, 0 };
1748 struct flowi6 fl6;
1749 struct dst_entry *dst;
1751 if (type == ICMPV6_MGM_REDUCTION)
1752 snd_addr = &in6addr_linklocal_allrouters;
1753 else
1754 snd_addr = addr;
1756 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1757 payload_len = len + sizeof(ra);
1758 full_len = sizeof(struct ipv6hdr) + payload_len;
1760 rcu_read_lock();
1761 IP6_UPD_PO_STATS(net, __in6_dev_get(dev),
1762 IPSTATS_MIB_OUT, full_len);
1763 rcu_read_unlock();
1765 skb = sock_alloc_send_skb(sk, hlen + tlen + full_len, 1, &err);
1767 if (skb == NULL) {
1768 rcu_read_lock();
1769 IP6_INC_STATS(net, __in6_dev_get(dev),
1770 IPSTATS_MIB_OUTDISCARDS);
1771 rcu_read_unlock();
1772 return;
1774 skb->priority = TC_PRIO_CONTROL;
1775 skb_reserve(skb, hlen);
1777 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
1778 /* <draft-ietf-magma-mld-source-05.txt>:
1779 * use unspecified address as the source address
1780 * when a valid link-local address is not available.
1782 saddr = &in6addr_any;
1783 } else
1784 saddr = &addr_buf;
1786 ip6_mc_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
1788 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1790 hdr = (struct mld_msg *) skb_put(skb, sizeof(struct mld_msg));
1791 memset(hdr, 0, sizeof(struct mld_msg));
1792 hdr->mld_type = type;
1793 hdr->mld_mca = *addr;
1795 hdr->mld_cksum = csum_ipv6_magic(saddr, snd_addr, len,
1796 IPPROTO_ICMPV6,
1797 csum_partial(hdr, len, 0));
1799 rcu_read_lock();
1800 idev = __in6_dev_get(skb->dev);
1802 icmpv6_flow_init(sk, &fl6, type,
1803 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1804 skb->dev->ifindex);
1805 dst = icmp6_dst_alloc(skb->dev, &fl6);
1806 if (IS_ERR(dst)) {
1807 err = PTR_ERR(dst);
1808 goto err_out;
1811 skb_dst_set(skb, dst);
1812 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
1813 dst_output);
1814 out:
1815 if (!err) {
1816 ICMP6MSGOUT_INC_STATS(net, idev, type);
1817 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
1818 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, full_len);
1819 } else
1820 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
1822 rcu_read_unlock();
1823 return;
1825 err_out:
1826 kfree_skb(skb);
1827 goto out;
1830 static void mld_resend_report(struct inet6_dev *idev)
1832 if (MLD_V1_SEEN(idev)) {
1833 struct ifmcaddr6 *mcaddr;
1834 read_lock_bh(&idev->lock);
1835 for (mcaddr = idev->mc_list; mcaddr; mcaddr = mcaddr->next) {
1836 if (!(mcaddr->mca_flags & MAF_NOREPORT))
1837 igmp6_send(&mcaddr->mca_addr, idev->dev,
1838 ICMPV6_MGM_REPORT);
1840 read_unlock_bh(&idev->lock);
1841 } else {
1842 mld_send_report(idev, NULL);
1846 void ipv6_mc_dad_complete(struct inet6_dev *idev)
1848 idev->mc_dad_count = idev->mc_qrv;
1849 if (idev->mc_dad_count) {
1850 mld_resend_report(idev);
1851 idev->mc_dad_count--;
1852 if (idev->mc_dad_count)
1853 mld_dad_start_timer(idev, idev->mc_maxdelay);
1857 static void mld_dad_timer_expire(unsigned long data)
1859 struct inet6_dev *idev = (struct inet6_dev *)data;
1861 mld_resend_report(idev);
1862 if (idev->mc_dad_count) {
1863 idev->mc_dad_count--;
1864 if (idev->mc_dad_count)
1865 mld_dad_start_timer(idev, idev->mc_maxdelay);
1867 __in6_dev_put(idev);
1870 static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1871 const struct in6_addr *psfsrc)
1873 struct ip6_sf_list *psf, *psf_prev;
1874 int rv = 0;
1876 psf_prev = NULL;
1877 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1878 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1879 break;
1880 psf_prev = psf;
1882 if (!psf || psf->sf_count[sfmode] == 0) {
1883 /* source filter not found, or count wrong => bug */
1884 return -ESRCH;
1886 psf->sf_count[sfmode]--;
1887 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1888 struct inet6_dev *idev = pmc->idev;
1890 /* no more filters for this source */
1891 if (psf_prev)
1892 psf_prev->sf_next = psf->sf_next;
1893 else
1894 pmc->mca_sources = psf->sf_next;
1895 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1896 !MLD_V1_SEEN(idev)) {
1897 psf->sf_crcount = idev->mc_qrv;
1898 psf->sf_next = pmc->mca_tomb;
1899 pmc->mca_tomb = psf;
1900 rv = 1;
1901 } else
1902 kfree(psf);
1904 return rv;
1907 static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
1908 int sfmode, int sfcount, const struct in6_addr *psfsrc,
1909 int delta)
1911 struct ifmcaddr6 *pmc;
1912 int changerec = 0;
1913 int i, err;
1915 if (!idev)
1916 return -ENODEV;
1917 read_lock_bh(&idev->lock);
1918 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1919 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1920 break;
1922 if (!pmc) {
1923 /* MCA not found?? bug */
1924 read_unlock_bh(&idev->lock);
1925 return -ESRCH;
1927 spin_lock_bh(&pmc->mca_lock);
1928 sf_markstate(pmc);
1929 if (!delta) {
1930 if (!pmc->mca_sfcount[sfmode]) {
1931 spin_unlock_bh(&pmc->mca_lock);
1932 read_unlock_bh(&idev->lock);
1933 return -EINVAL;
1935 pmc->mca_sfcount[sfmode]--;
1937 err = 0;
1938 for (i=0; i<sfcount; i++) {
1939 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1941 changerec |= rv > 0;
1942 if (!err && rv < 0)
1943 err = rv;
1945 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1946 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1947 pmc->mca_sfcount[MCAST_INCLUDE]) {
1948 struct ip6_sf_list *psf;
1950 /* filter mode change */
1951 pmc->mca_sfmode = MCAST_INCLUDE;
1952 pmc->mca_crcount = idev->mc_qrv;
1953 idev->mc_ifc_count = pmc->mca_crcount;
1954 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1955 psf->sf_crcount = 0;
1956 mld_ifc_event(pmc->idev);
1957 } else if (sf_setstate(pmc) || changerec)
1958 mld_ifc_event(pmc->idev);
1959 spin_unlock_bh(&pmc->mca_lock);
1960 read_unlock_bh(&idev->lock);
1961 return err;
1965 * Add multicast single-source filter to the interface list
1967 static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1968 const struct in6_addr *psfsrc)
1970 struct ip6_sf_list *psf, *psf_prev;
1972 psf_prev = NULL;
1973 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1974 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1975 break;
1976 psf_prev = psf;
1978 if (!psf) {
1979 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1980 if (!psf)
1981 return -ENOBUFS;
1983 psf->sf_addr = *psfsrc;
1984 if (psf_prev) {
1985 psf_prev->sf_next = psf;
1986 } else
1987 pmc->mca_sources = psf;
1989 psf->sf_count[sfmode]++;
1990 return 0;
1993 static void sf_markstate(struct ifmcaddr6 *pmc)
1995 struct ip6_sf_list *psf;
1996 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1998 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1999 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2000 psf->sf_oldin = mca_xcount ==
2001 psf->sf_count[MCAST_EXCLUDE] &&
2002 !psf->sf_count[MCAST_INCLUDE];
2003 } else
2004 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2007 static int sf_setstate(struct ifmcaddr6 *pmc)
2009 struct ip6_sf_list *psf, *dpsf;
2010 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2011 int qrv = pmc->idev->mc_qrv;
2012 int new_in, rv;
2014 rv = 0;
2015 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
2016 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2017 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2018 !psf->sf_count[MCAST_INCLUDE];
2019 } else
2020 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2021 if (new_in) {
2022 if (!psf->sf_oldin) {
2023 struct ip6_sf_list *prev = NULL;
2025 for (dpsf=pmc->mca_tomb; dpsf;
2026 dpsf=dpsf->sf_next) {
2027 if (ipv6_addr_equal(&dpsf->sf_addr,
2028 &psf->sf_addr))
2029 break;
2030 prev = dpsf;
2032 if (dpsf) {
2033 if (prev)
2034 prev->sf_next = dpsf->sf_next;
2035 else
2036 pmc->mca_tomb = dpsf->sf_next;
2037 kfree(dpsf);
2039 psf->sf_crcount = qrv;
2040 rv++;
2042 } else if (psf->sf_oldin) {
2043 psf->sf_crcount = 0;
2045 * add or update "delete" records if an active filter
2046 * is now inactive
2048 for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
2049 if (ipv6_addr_equal(&dpsf->sf_addr,
2050 &psf->sf_addr))
2051 break;
2052 if (!dpsf) {
2053 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2054 if (!dpsf)
2055 continue;
2056 *dpsf = *psf;
2057 /* pmc->mca_lock held by callers */
2058 dpsf->sf_next = pmc->mca_tomb;
2059 pmc->mca_tomb = dpsf;
2061 dpsf->sf_crcount = qrv;
2062 rv++;
2065 return rv;
2069 * Add multicast source filter list to the interface list
2071 static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2072 int sfmode, int sfcount, const struct in6_addr *psfsrc,
2073 int delta)
2075 struct ifmcaddr6 *pmc;
2076 int isexclude;
2077 int i, err;
2079 if (!idev)
2080 return -ENODEV;
2081 read_lock_bh(&idev->lock);
2082 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2083 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2084 break;
2086 if (!pmc) {
2087 /* MCA not found?? bug */
2088 read_unlock_bh(&idev->lock);
2089 return -ESRCH;
2091 spin_lock_bh(&pmc->mca_lock);
2093 sf_markstate(pmc);
2094 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2095 if (!delta)
2096 pmc->mca_sfcount[sfmode]++;
2097 err = 0;
2098 for (i=0; i<sfcount; i++) {
2099 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2100 if (err)
2101 break;
2103 if (err) {
2104 int j;
2106 if (!delta)
2107 pmc->mca_sfcount[sfmode]--;
2108 for (j=0; j<i; j++)
2109 ip6_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2110 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2111 struct ip6_sf_list *psf;
2113 /* filter mode change */
2114 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2115 pmc->mca_sfmode = MCAST_EXCLUDE;
2116 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2117 pmc->mca_sfmode = MCAST_INCLUDE;
2118 /* else no filters; keep old mode for reports */
2120 pmc->mca_crcount = idev->mc_qrv;
2121 idev->mc_ifc_count = pmc->mca_crcount;
2122 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2123 psf->sf_crcount = 0;
2124 mld_ifc_event(idev);
2125 } else if (sf_setstate(pmc))
2126 mld_ifc_event(idev);
2127 spin_unlock_bh(&pmc->mca_lock);
2128 read_unlock_bh(&idev->lock);
2129 return err;
2132 static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2134 struct ip6_sf_list *psf, *nextpsf;
2136 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2137 nextpsf = psf->sf_next;
2138 kfree(psf);
2140 pmc->mca_tomb = NULL;
2141 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2142 nextpsf = psf->sf_next;
2143 kfree(psf);
2145 pmc->mca_sources = NULL;
2146 pmc->mca_sfmode = MCAST_EXCLUDE;
2147 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
2148 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2152 static void igmp6_join_group(struct ifmcaddr6 *ma)
2154 unsigned long delay;
2156 if (ma->mca_flags & MAF_NOREPORT)
2157 return;
2159 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2161 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2163 spin_lock_bh(&ma->mca_lock);
2164 if (del_timer(&ma->mca_timer)) {
2165 atomic_dec(&ma->mca_refcnt);
2166 delay = ma->mca_timer.expires - jiffies;
2169 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2170 atomic_inc(&ma->mca_refcnt);
2171 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2172 spin_unlock_bh(&ma->mca_lock);
2175 static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2176 struct inet6_dev *idev)
2178 int err;
2180 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2181 * so no other readers or writers of iml or its sflist
2183 if (!iml->sflist) {
2184 /* any-source empty exclude case */
2185 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2187 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2188 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2189 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2190 iml->sflist = NULL;
2191 return err;
2194 static void igmp6_leave_group(struct ifmcaddr6 *ma)
2196 if (MLD_V1_SEEN(ma->idev)) {
2197 if (ma->mca_flags & MAF_LAST_REPORTER)
2198 igmp6_send(&ma->mca_addr, ma->idev->dev,
2199 ICMPV6_MGM_REDUCTION);
2200 } else {
2201 mld_add_delrec(ma->idev, ma);
2202 mld_ifc_event(ma->idev);
2206 static void mld_gq_timer_expire(unsigned long data)
2208 struct inet6_dev *idev = (struct inet6_dev *)data;
2210 idev->mc_gq_running = 0;
2211 mld_send_report(idev, NULL);
2212 __in6_dev_put(idev);
2215 static void mld_ifc_timer_expire(unsigned long data)
2217 struct inet6_dev *idev = (struct inet6_dev *)data;
2219 mld_send_cr(idev);
2220 if (idev->mc_ifc_count) {
2221 idev->mc_ifc_count--;
2222 if (idev->mc_ifc_count)
2223 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2225 __in6_dev_put(idev);
2228 static void mld_ifc_event(struct inet6_dev *idev)
2230 if (MLD_V1_SEEN(idev))
2231 return;
2232 idev->mc_ifc_count = idev->mc_qrv;
2233 mld_ifc_start_timer(idev, 1);
2237 static void igmp6_timer_handler(unsigned long data)
2239 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2241 if (MLD_V1_SEEN(ma->idev))
2242 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2243 else
2244 mld_send_report(ma->idev, ma);
2246 spin_lock(&ma->mca_lock);
2247 ma->mca_flags |= MAF_LAST_REPORTER;
2248 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2249 spin_unlock(&ma->mca_lock);
2250 ma_put(ma);
2253 /* Device changing type */
2255 void ipv6_mc_unmap(struct inet6_dev *idev)
2257 struct ifmcaddr6 *i;
2259 /* Install multicast list, except for all-nodes (already installed) */
2261 read_lock_bh(&idev->lock);
2262 for (i = idev->mc_list; i; i = i->next)
2263 igmp6_group_dropped(i);
2264 read_unlock_bh(&idev->lock);
2267 void ipv6_mc_remap(struct inet6_dev *idev)
2269 ipv6_mc_up(idev);
2272 /* Device going down */
2274 void ipv6_mc_down(struct inet6_dev *idev)
2276 struct ifmcaddr6 *i;
2278 /* Withdraw multicast list */
2280 read_lock_bh(&idev->lock);
2281 idev->mc_ifc_count = 0;
2282 if (del_timer(&idev->mc_ifc_timer))
2283 __in6_dev_put(idev);
2284 idev->mc_gq_running = 0;
2285 if (del_timer(&idev->mc_gq_timer))
2286 __in6_dev_put(idev);
2287 if (del_timer(&idev->mc_dad_timer))
2288 __in6_dev_put(idev);
2290 for (i = idev->mc_list; i; i=i->next)
2291 igmp6_group_dropped(i);
2292 read_unlock_bh(&idev->lock);
2294 mld_clear_delrec(idev);
2298 /* Device going up */
2300 void ipv6_mc_up(struct inet6_dev *idev)
2302 struct ifmcaddr6 *i;
2304 /* Install multicast list, except for all-nodes (already installed) */
2306 read_lock_bh(&idev->lock);
2307 for (i = idev->mc_list; i; i=i->next)
2308 igmp6_group_added(i);
2309 read_unlock_bh(&idev->lock);
2312 /* IPv6 device initialization. */
2314 void ipv6_mc_init_dev(struct inet6_dev *idev)
2316 write_lock_bh(&idev->lock);
2317 spin_lock_init(&idev->mc_lock);
2318 idev->mc_gq_running = 0;
2319 setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2320 (unsigned long)idev);
2321 idev->mc_tomb = NULL;
2322 idev->mc_ifc_count = 0;
2323 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2324 (unsigned long)idev);
2325 setup_timer(&idev->mc_dad_timer, mld_dad_timer_expire,
2326 (unsigned long)idev);
2327 idev->mc_qrv = MLD_QRV_DEFAULT;
2328 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2329 idev->mc_v1_seen = 0;
2330 write_unlock_bh(&idev->lock);
2334 * Device is about to be destroyed: clean up.
2337 void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2339 struct ifmcaddr6 *i;
2341 /* Deactivate timers */
2342 ipv6_mc_down(idev);
2344 /* Delete all-nodes address. */
2345 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2346 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2347 * fail.
2349 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
2351 if (idev->cnf.forwarding)
2352 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
2354 write_lock_bh(&idev->lock);
2355 while ((i = idev->mc_list) != NULL) {
2356 idev->mc_list = i->next;
2357 write_unlock_bh(&idev->lock);
2359 igmp6_group_dropped(i);
2360 ma_put(i);
2362 write_lock_bh(&idev->lock);
2364 write_unlock_bh(&idev->lock);
2367 #ifdef CONFIG_PROC_FS
2368 struct igmp6_mc_iter_state {
2369 struct seq_net_private p;
2370 struct net_device *dev;
2371 struct inet6_dev *idev;
2374 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2376 static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2378 struct ifmcaddr6 *im = NULL;
2379 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2380 struct net *net = seq_file_net(seq);
2382 state->idev = NULL;
2383 for_each_netdev_rcu(net, state->dev) {
2384 struct inet6_dev *idev;
2385 idev = __in6_dev_get(state->dev);
2386 if (!idev)
2387 continue;
2388 read_lock_bh(&idev->lock);
2389 im = idev->mc_list;
2390 if (im) {
2391 state->idev = idev;
2392 break;
2394 read_unlock_bh(&idev->lock);
2396 return im;
2399 static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2401 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2403 im = im->next;
2404 while (!im) {
2405 if (likely(state->idev != NULL))
2406 read_unlock_bh(&state->idev->lock);
2408 state->dev = next_net_device_rcu(state->dev);
2409 if (!state->dev) {
2410 state->idev = NULL;
2411 break;
2413 state->idev = __in6_dev_get(state->dev);
2414 if (!state->idev)
2415 continue;
2416 read_lock_bh(&state->idev->lock);
2417 im = state->idev->mc_list;
2419 return im;
2422 static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2424 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2425 if (im)
2426 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2427 --pos;
2428 return pos ? NULL : im;
2431 static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
2432 __acquires(RCU)
2434 rcu_read_lock();
2435 return igmp6_mc_get_idx(seq, *pos);
2438 static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2440 struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);
2442 ++*pos;
2443 return im;
2446 static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2447 __releases(RCU)
2449 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2451 if (likely(state->idev != NULL)) {
2452 read_unlock_bh(&state->idev->lock);
2453 state->idev = NULL;
2455 state->dev = NULL;
2456 rcu_read_unlock();
2459 static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2461 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2462 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2464 seq_printf(seq,
2465 "%-4d %-15s %pi6 %5d %08X %ld\n",
2466 state->dev->ifindex, state->dev->name,
2467 &im->mca_addr,
2468 im->mca_users, im->mca_flags,
2469 (im->mca_flags&MAF_TIMER_RUNNING) ?
2470 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2471 return 0;
2474 static const struct seq_operations igmp6_mc_seq_ops = {
2475 .start = igmp6_mc_seq_start,
2476 .next = igmp6_mc_seq_next,
2477 .stop = igmp6_mc_seq_stop,
2478 .show = igmp6_mc_seq_show,
2481 static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2483 return seq_open_net(inode, file, &igmp6_mc_seq_ops,
2484 sizeof(struct igmp6_mc_iter_state));
2487 static const struct file_operations igmp6_mc_seq_fops = {
2488 .owner = THIS_MODULE,
2489 .open = igmp6_mc_seq_open,
2490 .read = seq_read,
2491 .llseek = seq_lseek,
2492 .release = seq_release_net,
2495 struct igmp6_mcf_iter_state {
2496 struct seq_net_private p;
2497 struct net_device *dev;
2498 struct inet6_dev *idev;
2499 struct ifmcaddr6 *im;
2502 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2504 static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2506 struct ip6_sf_list *psf = NULL;
2507 struct ifmcaddr6 *im = NULL;
2508 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2509 struct net *net = seq_file_net(seq);
2511 state->idev = NULL;
2512 state->im = NULL;
2513 for_each_netdev_rcu(net, state->dev) {
2514 struct inet6_dev *idev;
2515 idev = __in6_dev_get(state->dev);
2516 if (unlikely(idev == NULL))
2517 continue;
2518 read_lock_bh(&idev->lock);
2519 im = idev->mc_list;
2520 if (likely(im != NULL)) {
2521 spin_lock_bh(&im->mca_lock);
2522 psf = im->mca_sources;
2523 if (likely(psf != NULL)) {
2524 state->im = im;
2525 state->idev = idev;
2526 break;
2528 spin_unlock_bh(&im->mca_lock);
2530 read_unlock_bh(&idev->lock);
2532 return psf;
2535 static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2537 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2539 psf = psf->sf_next;
2540 while (!psf) {
2541 spin_unlock_bh(&state->im->mca_lock);
2542 state->im = state->im->next;
2543 while (!state->im) {
2544 if (likely(state->idev != NULL))
2545 read_unlock_bh(&state->idev->lock);
2547 state->dev = next_net_device_rcu(state->dev);
2548 if (!state->dev) {
2549 state->idev = NULL;
2550 goto out;
2552 state->idev = __in6_dev_get(state->dev);
2553 if (!state->idev)
2554 continue;
2555 read_lock_bh(&state->idev->lock);
2556 state->im = state->idev->mc_list;
2558 if (!state->im)
2559 break;
2560 spin_lock_bh(&state->im->mca_lock);
2561 psf = state->im->mca_sources;
2563 out:
2564 return psf;
2567 static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2569 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2570 if (psf)
2571 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2572 --pos;
2573 return pos ? NULL : psf;
2576 static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2577 __acquires(RCU)
2579 rcu_read_lock();
2580 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2583 static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2585 struct ip6_sf_list *psf;
2586 if (v == SEQ_START_TOKEN)
2587 psf = igmp6_mcf_get_first(seq);
2588 else
2589 psf = igmp6_mcf_get_next(seq, v);
2590 ++*pos;
2591 return psf;
2594 static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2595 __releases(RCU)
2597 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2598 if (likely(state->im != NULL)) {
2599 spin_unlock_bh(&state->im->mca_lock);
2600 state->im = NULL;
2602 if (likely(state->idev != NULL)) {
2603 read_unlock_bh(&state->idev->lock);
2604 state->idev = NULL;
2606 state->dev = NULL;
2607 rcu_read_unlock();
2610 static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2612 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2613 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2615 if (v == SEQ_START_TOKEN) {
2616 seq_printf(seq,
2617 "%3s %6s "
2618 "%32s %32s %6s %6s\n", "Idx",
2619 "Device", "Multicast Address",
2620 "Source Address", "INC", "EXC");
2621 } else {
2622 seq_printf(seq,
2623 "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
2624 state->dev->ifindex, state->dev->name,
2625 &state->im->mca_addr,
2626 &psf->sf_addr,
2627 psf->sf_count[MCAST_INCLUDE],
2628 psf->sf_count[MCAST_EXCLUDE]);
2630 return 0;
2633 static const struct seq_operations igmp6_mcf_seq_ops = {
2634 .start = igmp6_mcf_seq_start,
2635 .next = igmp6_mcf_seq_next,
2636 .stop = igmp6_mcf_seq_stop,
2637 .show = igmp6_mcf_seq_show,
2640 static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2642 return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
2643 sizeof(struct igmp6_mcf_iter_state));
2646 static const struct file_operations igmp6_mcf_seq_fops = {
2647 .owner = THIS_MODULE,
2648 .open = igmp6_mcf_seq_open,
2649 .read = seq_read,
2650 .llseek = seq_lseek,
2651 .release = seq_release_net,
2654 static int __net_init igmp6_proc_init(struct net *net)
2656 int err;
2658 err = -ENOMEM;
2659 if (!proc_create("igmp6", S_IRUGO, net->proc_net, &igmp6_mc_seq_fops))
2660 goto out;
2661 if (!proc_create("mcfilter6", S_IRUGO, net->proc_net,
2662 &igmp6_mcf_seq_fops))
2663 goto out_proc_net_igmp6;
2665 err = 0;
2666 out:
2667 return err;
2669 out_proc_net_igmp6:
2670 remove_proc_entry("igmp6", net->proc_net);
2671 goto out;
2674 static void __net_exit igmp6_proc_exit(struct net *net)
2676 remove_proc_entry("mcfilter6", net->proc_net);
2677 remove_proc_entry("igmp6", net->proc_net);
2679 #else
2680 static inline int igmp6_proc_init(struct net *net)
2682 return 0;
2684 static inline void igmp6_proc_exit(struct net *net)
2687 #endif
2689 static int __net_init igmp6_net_init(struct net *net)
2691 int err;
2693 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2694 SOCK_RAW, IPPROTO_ICMPV6, net);
2695 if (err < 0) {
2696 pr_err("Failed to initialize the IGMP6 control socket (err %d)\n",
2697 err);
2698 goto out;
2701 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
2703 err = igmp6_proc_init(net);
2704 if (err)
2705 goto out_sock_create;
2706 out:
2707 return err;
2709 out_sock_create:
2710 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2711 goto out;
2714 static void __net_exit igmp6_net_exit(struct net *net)
2716 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2717 igmp6_proc_exit(net);
2720 static struct pernet_operations igmp6_net_ops = {
2721 .init = igmp6_net_init,
2722 .exit = igmp6_net_exit,
2725 int __init igmp6_init(void)
2727 return register_pernet_subsys(&igmp6_net_ops);
2730 void igmp6_cleanup(void)
2732 unregister_pernet_subsys(&igmp6_net_ops);