blob: 33f98d9ac37689e1f3909d30b26c84471c39451e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Multicast support for IPv6
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09008 * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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.
14 */
15
16/* Changes:
17 *
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
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +020047#include <linux/pkt_sched.h>
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +090048#include <net/mld.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <linux/netfilter.h>
51#include <linux/netfilter_ipv6.h>
52
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020053#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <net/sock.h>
55#include <net/snmp.h>
56
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>
Denis V. Lunev1ed85162008-04-03 14:31:03 -070063#include <net/inet_common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#include <net/ip6_checksum.h>
66
67/* Set to 3 to get tracing... */
68#define MCAST_DEBUG 2
69
70#if MCAST_DEBUG >= 3
71#define MDBG(x) printk x
72#else
73#define MDBG(x)
74#endif
75
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +090076/* Ensure that we have struct in6_addr aligned on 32bit word. */
77static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081};
82
83static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
84
85/* Big mc list lock for all the sockets */
Eric Dumazet456b61b2010-11-23 13:12:15 +000086static DEFINE_SPINLOCK(ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static void igmp6_join_group(struct ifmcaddr6 *ma);
89static void igmp6_leave_group(struct ifmcaddr6 *ma);
90static void igmp6_timer_handler(unsigned long data);
91
92static void mld_gq_timer_expire(unsigned long data);
93static void mld_ifc_timer_expire(unsigned long data);
94static void mld_ifc_event(struct inet6_dev *idev);
95static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
Eric Dumazetb71d1d42011-04-22 04:53:02 +000096static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static void mld_clear_delrec(struct inet6_dev *idev);
98static int sf_setstate(struct ifmcaddr6 *pmc);
99static void sf_markstate(struct ifmcaddr6 *pmc);
100static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000101static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
102 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 int delta);
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000104static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
105 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 int delta);
107static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
108 struct inet6_dev *idev);
109
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#define MLD_QRV_DEFAULT 2
112
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700113#define MLD_V1_SEEN(idev) (dev_net((idev)->dev)->ipv6.devconf_all->force_mld_version == 1 || \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 (idev)->cnf.force_mld_version == 1 || \
115 ((idev)->mc_v1_seen && \
116 time_before(jiffies, (idev)->mc_v1_seen)))
117
David L Stevens6f4353d2005-12-26 17:03:46 -0800118#define IPV6_MLD_MAX_MSF 64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Brian Haleyab32ea52006-09-22 14:15:41 -0700120int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122/*
123 * socket join on multicast group
124 */
125
Eric Dumazet456b61b2010-11-23 13:12:15 +0000126#define for_each_pmc_rcu(np, pmc) \
127 for (pmc = rcu_dereference(np->ipv6_mc_list); \
128 pmc != NULL; \
129 pmc = rcu_dereference(pmc->next))
130
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200131static int unsolicited_report_interval(struct inet6_dev *idev)
132{
133 int iv;
134
135 if (MLD_V1_SEEN(idev))
136 iv = idev->cnf.mldv1_unsolicited_report_interval;
137 else
138 iv = idev->cnf.mldv2_unsolicited_report_interval;
139
140 return iv > 0 ? iv : 1;
141}
142
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900143int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 struct net_device *dev = NULL;
146 struct ipv6_mc_socklist *mc_lst;
147 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900148 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 int err;
150
151 if (!ipv6_addr_is_multicast(addr))
152 return -EINVAL;
153
Eric Dumazet456b61b2010-11-23 13:12:15 +0000154 rcu_read_lock();
155 for_each_pmc_rcu(np, mc_lst) {
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700156 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
157 ipv6_addr_equal(&mc_lst->addr, addr)) {
Eric Dumazet456b61b2010-11-23 13:12:15 +0000158 rcu_read_unlock();
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700159 return -EADDRINUSE;
160 }
161 }
Eric Dumazet456b61b2010-11-23 13:12:15 +0000162 rcu_read_unlock();
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
165
166 if (mc_lst == NULL)
167 return -ENOMEM;
168
169 mc_lst->next = NULL;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000170 mc_lst->addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Eric Dumazet96b52e62010-06-07 21:05:02 +0000172 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (ifindex == 0) {
174 struct rt6_info *rt;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800175 rt = rt6_lookup(net, addr, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (rt) {
David S. Millerd1918542011-12-28 20:19:20 -0500177 dev = rt->dst.dev;
Amerigo Wang94e187c2012-10-29 00:13:19 +0000178 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180 } else
Eric Dumazet96b52e62010-06-07 21:05:02 +0000181 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 if (dev == NULL) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000184 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
186 return -ENODEV;
187 }
188
189 mc_lst->ifindex = dev->ifindex;
190 mc_lst->sfmode = MCAST_EXCLUDE;
YOSHIFUJI Hideaki196433c2006-01-04 13:56:31 -0800191 rwlock_init(&mc_lst->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 mc_lst->sflist = NULL;
193
194 /*
195 * now add/increase the group membership on the device
196 */
197
198 err = ipv6_dev_mc_inc(dev, addr);
199
200 if (err) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000201 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return err;
204 }
205
Eric Dumazet456b61b2010-11-23 13:12:15 +0000206 spin_lock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 mc_lst->next = np->ipv6_mc_list;
Eric Dumazet456b61b2010-11-23 13:12:15 +0000208 rcu_assign_pointer(np->ipv6_mc_list, mc_lst);
209 spin_unlock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Eric Dumazet96b52e62010-06-07 21:05:02 +0000211 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 return 0;
214}
215
216/*
217 * socket leave on multicast group
218 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900219int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 struct ipv6_pinfo *np = inet6_sk(sk);
Eric Dumazet456b61b2010-11-23 13:12:15 +0000222 struct ipv6_mc_socklist *mc_lst;
223 struct ipv6_mc_socklist __rcu **lnk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900224 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Li Weia858d642012-07-17 15:28:59 +0800226 if (!ipv6_addr_is_multicast(addr))
227 return -EINVAL;
228
Eric Dumazet456b61b2010-11-23 13:12:15 +0000229 spin_lock(&ipv6_sk_mc_lock);
230 for (lnk = &np->ipv6_mc_list;
231 (mc_lst = rcu_dereference_protected(*lnk,
232 lockdep_is_held(&ipv6_sk_mc_lock))) !=NULL ;
233 lnk = &mc_lst->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
235 ipv6_addr_equal(&mc_lst->addr, addr)) {
236 struct net_device *dev;
237
238 *lnk = mc_lst->next;
Eric Dumazet456b61b2010-11-23 13:12:15 +0000239 spin_unlock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Eric Dumazet96b52e62010-06-07 21:05:02 +0000241 rcu_read_lock();
242 dev = dev_get_by_index_rcu(net, mc_lst->ifindex);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800243 if (dev != NULL) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000244 struct inet6_dev *idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
David L Stevensacd6e002006-08-17 16:27:39 -0700246 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000247 if (idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
David L Stevensacd6e002006-08-17 16:27:39 -0700249 } else
250 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000251 rcu_read_unlock();
Eric Dumazet456b61b2010-11-23 13:12:15 +0000252 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
Lai Jiangshane3cbf282011-03-18 12:00:50 +0800253 kfree_rcu(mc_lst, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return 0;
255 }
256 }
Eric Dumazet456b61b2010-11-23 13:12:15 +0000257 spin_unlock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
David L Stevens9951f032005-07-08 17:47:28 -0700259 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Eric Dumazet96b52e62010-06-07 21:05:02 +0000262/* called with rcu_read_lock() */
263static struct inet6_dev *ip6_mc_find_dev_rcu(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000264 const struct in6_addr *group,
Eric Dumazet96b52e62010-06-07 21:05:02 +0000265 int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 struct net_device *dev = NULL;
268 struct inet6_dev *idev = NULL;
269
270 if (ifindex == 0) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000271 struct rt6_info *rt = rt6_lookup(net, group, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (rt) {
David S. Millerd1918542011-12-28 20:19:20 -0500274 dev = rt->dst.dev;
Amerigo Wang94e187c2012-10-29 00:13:19 +0000275 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277 } else
Eric Dumazet96b52e62010-06-07 21:05:02 +0000278 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 if (!dev)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000281 return NULL;
282 idev = __in6_dev_get(dev);
Ilpo Järvinen448eb712008-12-14 23:15:21 -0800283 if (!idev)
Joe Perches8a22c992010-11-14 17:05:00 +0000284 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 read_lock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000286 if (idev->dead) {
287 read_unlock_bh(&idev->lock);
288 return NULL;
289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return idev;
291}
292
293void ipv6_sock_mc_close(struct sock *sk)
294{
295 struct ipv6_pinfo *np = inet6_sk(sk);
296 struct ipv6_mc_socklist *mc_lst;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900297 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Eric Dumazet0e1efe92012-12-05 09:18:10 +0000299 if (!rcu_access_pointer(np->ipv6_mc_list))
300 return;
301
Eric Dumazet456b61b2010-11-23 13:12:15 +0000302 spin_lock(&ipv6_sk_mc_lock);
303 while ((mc_lst = rcu_dereference_protected(np->ipv6_mc_list,
304 lockdep_is_held(&ipv6_sk_mc_lock))) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct net_device *dev;
306
307 np->ipv6_mc_list = mc_lst->next;
Eric Dumazet456b61b2010-11-23 13:12:15 +0000308 spin_unlock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Eric Dumazet96b52e62010-06-07 21:05:02 +0000310 rcu_read_lock();
311 dev = dev_get_by_index_rcu(net, mc_lst->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (dev) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000313 struct inet6_dev *idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
David L Stevensacd6e002006-08-17 16:27:39 -0700315 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000316 if (idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
David L Stevensacd6e002006-08-17 16:27:39 -0700318 } else
319 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000320 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Eric Dumazet456b61b2010-11-23 13:12:15 +0000322 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
Lai Jiangshane3cbf282011-03-18 12:00:50 +0800323 kfree_rcu(mc_lst, rcu);
Eric Dumazet456b61b2010-11-23 13:12:15 +0000324
325 spin_lock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Eric Dumazet456b61b2010-11-23 13:12:15 +0000327 spin_unlock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330int ip6_mc_source(int add, int omode, struct sock *sk,
331 struct group_source_req *pgsr)
332{
333 struct in6_addr *source, *group;
334 struct ipv6_mc_socklist *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 struct inet6_dev *idev;
336 struct ipv6_pinfo *inet6 = inet6_sk(sk);
337 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900338 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 int i, j, rv;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700340 int leavegroup = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800341 int pmclocked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 int err;
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
345 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
346
347 if (!ipv6_addr_is_multicast(group))
348 return -EINVAL;
349
Eric Dumazet96b52e62010-06-07 21:05:02 +0000350 rcu_read_lock();
351 idev = ip6_mc_find_dev_rcu(net, group, pgsr->gsr_interface);
352 if (!idev) {
353 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return -ENODEV;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 err = -EADDRNOTAVAIL;
358
Eric Dumazet456b61b2010-11-23 13:12:15 +0000359 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
361 continue;
362 if (ipv6_addr_equal(&pmc->addr, group))
363 break;
364 }
David L Stevens917f2f12005-07-08 17:45:16 -0700365 if (!pmc) { /* must have a prior join */
366 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /* if a source filter was set, must be the same mode as before */
370 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -0700371 if (pmc->sfmode != omode) {
372 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 } else if (pmc->sfmode != omode) {
376 /* allow mode switches for empty-set filters */
377 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
378 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
379 pmc->sfmode = omode;
380 }
381
Eric Dumazet96b52e62010-06-07 21:05:02 +0000382 write_lock(&pmc->sflock);
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800383 pmclocked = 1;
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 psl = pmc->sflist;
386 if (!add) {
387 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -0700388 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 rv = !0;
390 for (i=0; i<psl->sl_count; i++) {
YOSHIFUJI Hideaki / 吉藤英明07c2fec2013-01-29 12:48:23 +0000391 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (rv == 0)
393 break;
394 }
395 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -0700396 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700398 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
399 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
400 leavegroup = 1;
401 goto done;
402 }
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /* update the interface filter */
405 ip6_mc_del_src(idev, group, omode, 1, source, 1);
406
407 for (j=i+1; j<psl->sl_count; j++)
408 psl->sl_addr[j-1] = psl->sl_addr[j];
409 psl->sl_count--;
410 err = 0;
411 goto done;
412 }
413 /* else, add a new source to the filter */
414
415 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
416 err = -ENOBUFS;
417 goto done;
418 }
419 if (!psl || psl->sl_count == psl->sl_max) {
420 struct ip6_sf_socklist *newpsl;
421 int count = IP6_SFBLOCK;
422
423 if (psl)
424 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800425 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (!newpsl) {
427 err = -ENOBUFS;
428 goto done;
429 }
430 newpsl->sl_max = count;
431 newpsl->sl_count = count - IP6_SFBLOCK;
432 if (psl) {
433 for (i=0; i<psl->sl_count; i++)
434 newpsl->sl_addr[i] = psl->sl_addr[i];
435 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
436 }
437 pmc->sflist = psl = newpsl;
438 }
439 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
440 for (i=0; i<psl->sl_count; i++) {
YOSHIFUJI Hideaki / 吉藤英明07c2fec2013-01-29 12:48:23 +0000441 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
Jean Sacren56db1c52013-02-03 21:34:10 +0000442 if (rv == 0) /* There is an error in the address. */
443 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 for (j=psl->sl_count-1; j>=i; j--)
446 psl->sl_addr[j+1] = psl->sl_addr[j];
447 psl->sl_addr[i] = *source;
448 psl->sl_count++;
449 err = 0;
450 /* update the interface list */
451 ip6_mc_add_src(idev, group, omode, 1, source, 1);
452done:
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800453 if (pmclocked)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000454 write_unlock(&pmc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000456 rcu_read_unlock();
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700457 if (leavegroup)
458 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return err;
460}
461
462int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
463{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000464 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 struct ipv6_mc_socklist *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 struct inet6_dev *idev;
467 struct ipv6_pinfo *inet6 = inet6_sk(sk);
468 struct ip6_sf_socklist *newpsl, *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900469 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -0700470 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 int i, err;
472
473 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
474
475 if (!ipv6_addr_is_multicast(group))
476 return -EINVAL;
477 if (gsf->gf_fmode != MCAST_INCLUDE &&
478 gsf->gf_fmode != MCAST_EXCLUDE)
479 return -EINVAL;
480
Eric Dumazet96b52e62010-06-07 21:05:02 +0000481 rcu_read_lock();
482 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Eric Dumazet96b52e62010-06-07 21:05:02 +0000484 if (!idev) {
485 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return -ENODEV;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
David S. Miller9c059892005-07-08 21:44:39 -0700489 err = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800490
David L Stevens9951f032005-07-08 17:47:28 -0700491 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
492 leavegroup = 1;
493 goto done;
494 }
495
Eric Dumazet456b61b2010-11-23 13:12:15 +0000496 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (pmc->ifindex != gsf->gf_interface)
498 continue;
499 if (ipv6_addr_equal(&pmc->addr, group))
500 break;
501 }
David L Stevens917f2f12005-07-08 17:45:16 -0700502 if (!pmc) { /* must have a prior join */
503 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (gsf->gf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800507 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
508 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (!newpsl) {
510 err = -ENOBUFS;
511 goto done;
512 }
513 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
514 for (i=0; i<newpsl->sl_count; ++i) {
515 struct sockaddr_in6 *psin6;
516
517 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
518 newpsl->sl_addr[i] = psin6->sin6_addr;
519 }
520 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
521 newpsl->sl_count, newpsl->sl_addr, 0);
522 if (err) {
523 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
524 goto done;
525 }
Yan Zheng8713dbf2005-10-28 08:02:08 +0800526 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +0800528 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
529 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800530
Eric Dumazet96b52e62010-06-07 21:05:02 +0000531 write_lock(&pmc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 psl = pmc->sflist;
533 if (psl) {
534 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
535 psl->sl_count, psl->sl_addr, 0);
536 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
537 } else
538 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
539 pmc->sflist = newpsl;
540 pmc->sfmode = gsf->gf_fmode;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000541 write_unlock(&pmc->sflock);
David L Stevens917f2f12005-07-08 17:45:16 -0700542 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543done:
544 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000545 rcu_read_unlock();
David L Stevens9951f032005-07-08 17:47:28 -0700546 if (leavegroup)
547 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return err;
549}
550
551int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
552 struct group_filter __user *optval, int __user *optlen)
553{
554 int err, i, count, copycount;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000555 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 struct ipv6_mc_socklist *pmc;
557 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 struct ipv6_pinfo *inet6 = inet6_sk(sk);
559 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900560 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
563
564 if (!ipv6_addr_is_multicast(group))
565 return -EINVAL;
566
Eric Dumazet96b52e62010-06-07 21:05:02 +0000567 rcu_read_lock();
568 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Eric Dumazet96b52e62010-06-07 21:05:02 +0000570 if (!idev) {
571 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return -ENODEV;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 err = -EADDRNOTAVAIL;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800576 /*
577 * changes to the ipv6_mc_list require the socket lock and
578 * a read lock on ip6_sk_mc_lock. We have the socket lock,
579 * so reading the list is safe.
580 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Eric Dumazet456b61b2010-11-23 13:12:15 +0000582 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (pmc->ifindex != gsf->gf_interface)
584 continue;
585 if (ipv6_addr_equal(group, &pmc->addr))
586 break;
587 }
588 if (!pmc) /* must have a prior join */
589 goto done;
590 gsf->gf_fmode = pmc->sfmode;
591 psl = pmc->sflist;
592 count = psl ? psl->sl_count : 0;
593 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000594 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
597 gsf->gf_numsrc = count;
598 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
599 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
600 return -EFAULT;
601 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800602 /* changes to psl require the socket lock, a read lock on
603 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
604 * have the socket lock, so reading here is safe.
605 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 for (i=0; i<copycount; i++) {
607 struct sockaddr_in6 *psin6;
608 struct sockaddr_storage ss;
609
610 psin6 = (struct sockaddr_in6 *)&ss;
611 memset(&ss, 0, sizeof(ss));
612 psin6->sin6_family = AF_INET6;
613 psin6->sin6_addr = psl->sl_addr[i];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900614 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return -EFAULT;
616 }
617 return 0;
618done:
619 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000620 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return err;
622}
623
Eric Dumazeta50feda2012-05-18 18:57:34 +0000624bool inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
625 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 struct ipv6_pinfo *np = inet6_sk(sk);
628 struct ipv6_mc_socklist *mc;
629 struct ip6_sf_socklist *psl;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000630 bool rv = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Eric Dumazet456b61b2010-11-23 13:12:15 +0000632 rcu_read_lock();
633 for_each_pmc_rcu(np, mc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (ipv6_addr_equal(&mc->addr, mc_addr))
635 break;
636 }
637 if (!mc) {
Eric Dumazet456b61b2010-11-23 13:12:15 +0000638 rcu_read_unlock();
Eric Dumazeta50feda2012-05-18 18:57:34 +0000639 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800641 read_lock(&mc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 psl = mc->sflist;
643 if (!psl) {
644 rv = mc->sfmode == MCAST_EXCLUDE;
645 } else {
646 int i;
647
648 for (i=0; i<psl->sl_count; i++) {
649 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
650 break;
651 }
652 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000653 rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000655 rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800657 read_unlock(&mc->sflock);
Eric Dumazet456b61b2010-11-23 13:12:15 +0000658 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 return rv;
661}
662
663static void ma_put(struct ifmcaddr6 *mc)
664{
665 if (atomic_dec_and_test(&mc->mca_refcnt)) {
666 in6_dev_put(mc->idev);
667 kfree(mc);
668 }
669}
670
671static void igmp6_group_added(struct ifmcaddr6 *mc)
672{
673 struct net_device *dev = mc->idev->dev;
674 char buf[MAX_ADDR_LEN];
675
YOSHIFUJI Hideaki / 吉藤英明ec16ef22013-02-09 04:29:58 +0000676 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
677 IPV6_ADDR_SCOPE_LINKLOCAL)
678 return;
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 spin_lock_bh(&mc->mca_lock);
681 if (!(mc->mca_flags&MAF_LOADED)) {
682 mc->mca_flags |= MAF_LOADED;
683 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000684 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686 spin_unlock_bh(&mc->mca_lock);
687
688 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
689 return;
690
691 if (MLD_V1_SEEN(mc->idev)) {
692 igmp6_join_group(mc);
693 return;
694 }
695 /* else v2 */
696
697 mc->mca_crcount = mc->idev->mc_qrv;
698 mld_ifc_event(mc->idev);
699}
700
701static void igmp6_group_dropped(struct ifmcaddr6 *mc)
702{
703 struct net_device *dev = mc->idev->dev;
704 char buf[MAX_ADDR_LEN];
705
YOSHIFUJI Hideaki / 吉藤英明ec16ef22013-02-09 04:29:58 +0000706 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
707 IPV6_ADDR_SCOPE_LINKLOCAL)
708 return;
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 spin_lock_bh(&mc->mca_lock);
711 if (mc->mca_flags&MAF_LOADED) {
712 mc->mca_flags &= ~MAF_LOADED;
713 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000714 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716
717 if (mc->mca_flags & MAF_NOREPORT)
718 goto done;
719 spin_unlock_bh(&mc->mca_lock);
720
721 if (!mc->idev->dead)
722 igmp6_leave_group(mc);
723
724 spin_lock_bh(&mc->mca_lock);
725 if (del_timer(&mc->mca_timer))
726 atomic_dec(&mc->mca_refcnt);
727done:
728 ip6_mc_clear_src(mc);
729 spin_unlock_bh(&mc->mca_lock);
730}
731
732/*
733 * deleted ifmcaddr6 manipulation
734 */
735static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
736{
737 struct ifmcaddr6 *pmc;
738
739 /* this is an "ifmcaddr6" for convenience; only the fields below
740 * are actually used. In particular, the refcnt and users are not
741 * used for management of the delete list. Using the same structure
742 * for deleted items allows change reports to use common code with
743 * non-deleted or query-response MCA's.
744 */
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800745 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (!pmc)
747 return;
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 spin_lock_bh(&im->mca_lock);
750 spin_lock_init(&pmc->mca_lock);
751 pmc->idev = im->idev;
752 in6_dev_hold(idev);
753 pmc->mca_addr = im->mca_addr;
754 pmc->mca_crcount = idev->mc_qrv;
755 pmc->mca_sfmode = im->mca_sfmode;
756 if (pmc->mca_sfmode == MCAST_INCLUDE) {
757 struct ip6_sf_list *psf;
758
759 pmc->mca_tomb = im->mca_tomb;
760 pmc->mca_sources = im->mca_sources;
761 im->mca_tomb = im->mca_sources = NULL;
762 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
763 psf->sf_crcount = pmc->mca_crcount;
764 }
765 spin_unlock_bh(&im->mca_lock);
766
Stephen Hemminger6457d262010-02-17 18:48:44 -0800767 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 pmc->next = idev->mc_tomb;
769 idev->mc_tomb = pmc;
Stephen Hemminger6457d262010-02-17 18:48:44 -0800770 spin_unlock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000773static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *pmca)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 struct ifmcaddr6 *pmc, *pmc_prev;
776 struct ip6_sf_list *psf, *psf_next;
777
Stephen Hemminger6457d262010-02-17 18:48:44 -0800778 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 pmc_prev = NULL;
780 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
781 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
782 break;
783 pmc_prev = pmc;
784 }
785 if (pmc) {
786 if (pmc_prev)
787 pmc_prev->next = pmc->next;
788 else
789 idev->mc_tomb = pmc->next;
790 }
Stephen Hemminger6457d262010-02-17 18:48:44 -0800791 spin_unlock_bh(&idev->mc_lock);
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (pmc) {
794 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
795 psf_next = psf->sf_next;
796 kfree(psf);
797 }
798 in6_dev_put(pmc->idev);
799 kfree(pmc);
800 }
801}
802
803static void mld_clear_delrec(struct inet6_dev *idev)
804{
805 struct ifmcaddr6 *pmc, *nextpmc;
806
Stephen Hemminger6457d262010-02-17 18:48:44 -0800807 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 pmc = idev->mc_tomb;
809 idev->mc_tomb = NULL;
Stephen Hemminger6457d262010-02-17 18:48:44 -0800810 spin_unlock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 for (; pmc; pmc = nextpmc) {
813 nextpmc = pmc->next;
814 ip6_mc_clear_src(pmc);
815 in6_dev_put(pmc->idev);
816 kfree(pmc);
817 }
818
819 /* clear dead sources, too */
820 read_lock_bh(&idev->lock);
821 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
822 struct ip6_sf_list *psf, *psf_next;
823
824 spin_lock_bh(&pmc->mca_lock);
825 psf = pmc->mca_tomb;
826 pmc->mca_tomb = NULL;
827 spin_unlock_bh(&pmc->mca_lock);
828 for (; psf; psf=psf_next) {
829 psf_next = psf->sf_next;
830 kfree(psf);
831 }
832 }
833 read_unlock_bh(&idev->lock);
834}
835
836
837/*
838 * device multicast group inc (add if not found)
839 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900840int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 struct ifmcaddr6 *mc;
843 struct inet6_dev *idev;
844
Eric Dumazet96b52e62010-06-07 21:05:02 +0000845 /* we need to take a reference on idev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 idev = in6_dev_get(dev);
847
848 if (idev == NULL)
849 return -EINVAL;
850
851 write_lock_bh(&idev->lock);
852 if (idev->dead) {
853 write_unlock_bh(&idev->lock);
854 in6_dev_put(idev);
855 return -ENODEV;
856 }
857
858 for (mc = idev->mc_list; mc; mc = mc->next) {
859 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
860 mc->mca_users++;
861 write_unlock_bh(&idev->lock);
862 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
863 NULL, 0);
864 in6_dev_put(idev);
865 return 0;
866 }
867 }
868
869 /*
870 * not found: create a new one.
871 */
872
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800873 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 if (mc == NULL) {
876 write_unlock_bh(&idev->lock);
877 in6_dev_put(idev);
878 return -ENOMEM;
879 }
880
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800881 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000883 mc->mca_addr = *addr;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000884 mc->idev = idev; /* (reference taken) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 mc->mca_users = 1;
886 /* mca_stamp should be updated upon changes */
887 mc->mca_cstamp = mc->mca_tstamp = jiffies;
888 atomic_set(&mc->mca_refcnt, 2);
889 spin_lock_init(&mc->mca_lock);
890
891 /* initial mode is (EX, empty) */
892 mc->mca_sfmode = MCAST_EXCLUDE;
893 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
894
895 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
896 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
897 mc->mca_flags |= MAF_NOREPORT;
898
899 mc->next = idev->mc_list;
900 idev->mc_list = mc;
901 write_unlock_bh(&idev->lock);
902
903 mld_del_delrec(idev, &mc->mca_addr);
904 igmp6_group_added(mc);
905 ma_put(mc);
906 return 0;
907}
908
909/*
910 * device multicast group del
911 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900912int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
914 struct ifmcaddr6 *ma, **map;
915
916 write_lock_bh(&idev->lock);
917 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
918 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
919 if (--ma->mca_users == 0) {
920 *map = ma->next;
921 write_unlock_bh(&idev->lock);
922
923 igmp6_group_dropped(ma);
924
925 ma_put(ma);
926 return 0;
927 }
928 write_unlock_bh(&idev->lock);
929 return 0;
930 }
931 }
932 write_unlock_bh(&idev->lock);
933
934 return -ENOENT;
935}
936
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900937int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Eric Dumazet96b52e62010-06-07 21:05:02 +0000939 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 int err;
941
Eric Dumazet96b52e62010-06-07 21:05:02 +0000942 rcu_read_lock();
943
944 idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (!idev)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000946 err = -ENODEV;
947 else
948 err = __ipv6_dev_mc_dec(idev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Eric Dumazet96b52e62010-06-07 21:05:02 +0000950 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return err;
952}
953
954/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 * check if the interface/address pair is valid
956 */
Eric Dumazeta50feda2012-05-18 18:57:34 +0000957bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
958 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 struct inet6_dev *idev;
961 struct ifmcaddr6 *mc;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000962 bool rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Eric Dumazet96b52e62010-06-07 21:05:02 +0000964 rcu_read_lock();
965 idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (idev) {
967 read_lock_bh(&idev->lock);
968 for (mc = idev->mc_list; mc; mc=mc->next) {
969 if (ipv6_addr_equal(&mc->mca_addr, group))
970 break;
971 }
972 if (mc) {
973 if (src_addr && !ipv6_addr_any(src_addr)) {
974 struct ip6_sf_list *psf;
975
976 spin_lock_bh(&mc->mca_lock);
977 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
978 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
979 break;
980 }
981 if (psf)
982 rv = psf->sf_count[MCAST_INCLUDE] ||
983 psf->sf_count[MCAST_EXCLUDE] !=
984 mc->mca_sfcount[MCAST_EXCLUDE];
985 else
986 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
987 spin_unlock_bh(&mc->mca_lock);
988 } else
Eric Dumazeta50feda2012-05-18 18:57:34 +0000989 rv = true; /* don't filter unspecified source */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
Eric Dumazet96b52e62010-06-07 21:05:02 +0000993 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return rv;
995}
996
997static void mld_gq_start_timer(struct inet6_dev *idev)
998{
Daniel Borkmannc2cef4e2013-08-20 12:22:01 +0200999 unsigned long tv = net_random() % idev->mc_maxdelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 idev->mc_gq_running = 1;
1002 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1003 in6_dev_hold(idev);
1004}
1005
Daniel Borkmannc2cef4e2013-08-20 12:22:01 +02001006static void mld_ifc_start_timer(struct inet6_dev *idev, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Daniel Borkmannc2cef4e2013-08-20 12:22:01 +02001008 unsigned long tv = net_random() % delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1011 in6_dev_hold(idev);
1012}
1013
Daniel Borkmannc2cef4e2013-08-20 12:22:01 +02001014static void mld_dad_start_timer(struct inet6_dev *idev, unsigned long delay)
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02001015{
Daniel Borkmannc2cef4e2013-08-20 12:22:01 +02001016 unsigned long tv = net_random() % delay;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02001017
1018 if (!mod_timer(&idev->mc_dad_timer, jiffies+tv+2))
1019 in6_dev_hold(idev);
1020}
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022/*
1023 * IGMP handling (alias multicast ICMPv6 messages)
1024 */
1025
1026static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1027{
1028 unsigned long delay = resptime;
1029
1030 /* Do not start timer for these addresses */
1031 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1032 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1033 return;
1034
1035 if (del_timer(&ma->mca_timer)) {
1036 atomic_dec(&ma->mca_refcnt);
1037 delay = ma->mca_timer.expires - jiffies;
1038 }
1039
1040 if (delay >= resptime) {
1041 if (resptime)
1042 delay = net_random() % resptime;
1043 else
1044 delay = 1;
1045 }
1046 ma->mca_timer.expires = jiffies + delay;
1047 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1048 atomic_inc(&ma->mca_refcnt);
1049 ma->mca_flags |= MAF_TIMER_RUNNING;
1050}
1051
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001052/* mark EXCLUDE-mode sources */
Eric Dumazeta50feda2012-05-18 18:57:34 +00001053static bool mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1054 const struct in6_addr *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
1056 struct ip6_sf_list *psf;
1057 int i, scount;
1058
1059 scount = 0;
1060 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1061 if (scount == nsrcs)
1062 break;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001063 for (i=0; i<nsrcs; i++) {
1064 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +00001065 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001066 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1067 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +08001068 break;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001069 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1070 scount++;
1071 break;
1072 }
1073 }
1074 }
1075 pmc->mca_flags &= ~MAF_GSQUERY;
1076 if (scount == nsrcs) /* all sources excluded */
Eric Dumazeta50feda2012-05-18 18:57:34 +00001077 return false;
1078 return true;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001079}
1080
Eric Dumazeta50feda2012-05-18 18:57:34 +00001081static bool mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1082 const struct in6_addr *srcs)
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001083{
1084 struct ip6_sf_list *psf;
1085 int i, scount;
1086
1087 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1088 return mld_xmarksources(pmc, nsrcs, srcs);
1089
1090 /* mark INCLUDE-mode sources */
1091
1092 scount = 0;
1093 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1094 if (scount == nsrcs)
1095 break;
1096 for (i=0; i<nsrcs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1098 psf->sf_gsresp = 1;
1099 scount++;
1100 break;
1101 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001104 if (!scount) {
1105 pmc->mca_flags &= ~MAF_GSQUERY;
Eric Dumazeta50feda2012-05-18 18:57:34 +00001106 return false;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001107 }
1108 pmc->mca_flags |= MAF_GSQUERY;
Eric Dumazeta50feda2012-05-18 18:57:34 +00001109 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
Eric Dumazet96b52e62010-06-07 21:05:02 +00001112/* called with rcu_read_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113int igmp6_event_query(struct sk_buff *skb)
1114{
Yan Zheng97300b52005-10-31 20:09:45 +08001115 struct mld2_query *mlh2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 struct ifmcaddr6 *ma;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001117 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 unsigned long max_delay;
1119 struct inet6_dev *idev;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001120 struct mld_msg *mld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 int group_type;
1122 int mark = 0;
1123 int len;
1124
1125 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1126 return -EINVAL;
1127
1128 /* compute payload length excluding extension headers */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001129 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001130 len -= skb_network_header_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 /* Drop queries with not link local source */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001133 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return -EINVAL;
1135
Eric Dumazet96b52e62010-06-07 21:05:02 +00001136 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 if (idev == NULL)
1139 return 0;
1140
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001141 mld = (struct mld_msg *)icmp6_hdr(skb);
1142 group = &mld->mld_mca;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 group_type = ipv6_addr_type(group);
1144
1145 if (group_type != IPV6_ADDR_ANY &&
Eric Dumazet96b52e62010-06-07 21:05:02 +00001146 !(group_type&IPV6_ADDR_MULTICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 if (len == 24) {
1150 int switchback;
1151 /* MLDv1 router present */
1152
Daniel Borkmann84698962013-08-20 12:22:00 +02001153 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 switchback = (idev->mc_qrv + 1) * max_delay;
1155 idev->mc_v1_seen = jiffies + switchback;
1156
1157 /* cancel the interface change timer */
1158 idev->mc_ifc_count = 0;
1159 if (del_timer(&idev->mc_ifc_timer))
1160 __in6_dev_put(idev);
1161 /* clear deleted report items */
1162 mld_clear_delrec(idev);
1163 } else if (len >= 28) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001164 int srcs_offset = sizeof(struct mld2_query) -
Yan Zheng97300b52005-10-31 20:09:45 +08001165 sizeof(struct icmp6hdr);
Eric Dumazet96b52e62010-06-07 21:05:02 +00001166 if (!pskb_may_pull(skb, srcs_offset))
Yan Zheng97300b52005-10-31 20:09:45 +08001167 return -EINVAL;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001168
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001169 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Daniel Borkmann84698962013-08-20 12:22:00 +02001170
1171 max_delay = max(msecs_to_jiffies(MLDV2_MRC(ntohs(mlh2->mld2q_mrc))), 1UL);
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 idev->mc_maxdelay = max_delay;
Daniel Borkmann84698962013-08-20 12:22:00 +02001174
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001175 if (mlh2->mld2q_qrv)
1176 idev->mc_qrv = mlh2->mld2q_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (group_type == IPV6_ADDR_ANY) { /* general query */
Eric Dumazet96b52e62010-06-07 21:05:02 +00001178 if (mlh2->mld2q_nsrcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return -EINVAL; /* no sources allowed */
Eric Dumazet96b52e62010-06-07 21:05:02 +00001180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 mld_gq_start_timer(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return 0;
1183 }
1184 /* mark sources to include, if group & source-specific */
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001185 if (mlh2->mld2q_nsrcs != 0) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001186 if (!pskb_may_pull(skb, srcs_offset +
Eric Dumazet96b52e62010-06-07 21:05:02 +00001187 ntohs(mlh2->mld2q_nsrcs) * sizeof(struct in6_addr)))
Yan Zheng97300b52005-10-31 20:09:45 +08001188 return -EINVAL;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001189
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001190 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Yan Zheng97300b52005-10-31 20:09:45 +08001191 mark = 1;
1192 }
Eric Dumazet96b52e62010-06-07 21:05:02 +00001193 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 read_lock_bh(&idev->lock);
1197 if (group_type == IPV6_ADDR_ANY) {
1198 for (ma = idev->mc_list; ma; ma=ma->next) {
1199 spin_lock_bh(&ma->mca_lock);
1200 igmp6_group_queried(ma, max_delay);
1201 spin_unlock_bh(&ma->mca_lock);
1202 }
1203 } else {
1204 for (ma = idev->mc_list; ma; ma=ma->next) {
David L Stevens7add2a42006-01-24 13:06:39 -08001205 if (!ipv6_addr_equal(group, &ma->mca_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 continue;
1207 spin_lock_bh(&ma->mca_lock);
1208 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1209 /* gsquery <- gsquery && mark */
1210 if (!mark)
1211 ma->mca_flags &= ~MAF_GSQUERY;
1212 } else {
1213 /* gsquery <- mark */
1214 if (mark)
1215 ma->mca_flags |= MAF_GSQUERY;
1216 else
1217 ma->mca_flags &= ~MAF_GSQUERY;
1218 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001219 if (!(ma->mca_flags & MAF_GSQUERY) ||
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001220 mld_marksources(ma, ntohs(mlh2->mld2q_nsrcs), mlh2->mld2q_srcs))
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001221 igmp6_group_queried(ma, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 spin_unlock_bh(&ma->mca_lock);
David L Stevens7add2a42006-01-24 13:06:39 -08001223 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225 }
1226 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 return 0;
1229}
1230
Eric Dumazet96b52e62010-06-07 21:05:02 +00001231/* called with rcu_read_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232int igmp6_event_report(struct sk_buff *skb)
1233{
1234 struct ifmcaddr6 *ma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 struct inet6_dev *idev;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001236 struct mld_msg *mld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 int addr_type;
1238
1239 /* Our own report looped back. Ignore it. */
1240 if (skb->pkt_type == PACKET_LOOPBACK)
1241 return 0;
1242
David Stevens24c69272005-12-02 20:32:59 -08001243 /* send our report if the MC router may not have heard this report */
1244 if (skb->pkt_type != PACKET_MULTICAST &&
1245 skb->pkt_type != PACKET_BROADCAST)
1246 return 0;
1247
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001248 if (!pskb_may_pull(skb, sizeof(*mld) - sizeof(struct icmp6hdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return -EINVAL;
1250
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001251 mld = (struct mld_msg *)icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 /* Drop reports with not link local source */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001254 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001255 if (addr_type != IPV6_ADDR_ANY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 !(addr_type&IPV6_ADDR_LINKLOCAL))
1257 return -EINVAL;
1258
Eric Dumazet96b52e62010-06-07 21:05:02 +00001259 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (idev == NULL)
1261 return -ENODEV;
1262
1263 /*
1264 * Cancel the timer for this group
1265 */
1266
1267 read_lock_bh(&idev->lock);
1268 for (ma = idev->mc_list; ma; ma=ma->next) {
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001269 if (ipv6_addr_equal(&ma->mca_addr, &mld->mld_mca)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 spin_lock(&ma->mca_lock);
1271 if (del_timer(&ma->mca_timer))
1272 atomic_dec(&ma->mca_refcnt);
1273 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1274 spin_unlock(&ma->mca_lock);
1275 break;
1276 }
1277 }
1278 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return 0;
1280}
1281
Eric Dumazeta50feda2012-05-18 18:57:34 +00001282static bool is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1283 int gdeleted, int sdeleted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 switch (type) {
1286 case MLD2_MODE_IS_INCLUDE:
1287 case MLD2_MODE_IS_EXCLUDE:
1288 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001289 return false;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001290 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1291 if (pmc->mca_sfmode == MCAST_INCLUDE)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001292 return true;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001293 /* don't include if this source is excluded
1294 * in all filters
1295 */
1296 if (psf->sf_count[MCAST_INCLUDE])
David L Stevens7add2a42006-01-24 13:06:39 -08001297 return type == MLD2_MODE_IS_INCLUDE;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001298 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1299 psf->sf_count[MCAST_EXCLUDE];
1300 }
Eric Dumazeta50feda2012-05-18 18:57:34 +00001301 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 case MLD2_CHANGE_TO_INCLUDE:
1303 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001304 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return psf->sf_count[MCAST_INCLUDE] != 0;
1306 case MLD2_CHANGE_TO_EXCLUDE:
1307 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001308 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1310 psf->sf_count[MCAST_INCLUDE])
Eric Dumazeta50feda2012-05-18 18:57:34 +00001311 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1313 psf->sf_count[MCAST_EXCLUDE];
1314 case MLD2_ALLOW_NEW_SOURCES:
1315 if (gdeleted || !psf->sf_crcount)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001316 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1318 case MLD2_BLOCK_OLD_SOURCES:
1319 if (pmc->mca_sfmode == MCAST_INCLUDE)
1320 return gdeleted || (psf->sf_crcount && sdeleted);
1321 return psf->sf_crcount && !gdeleted && !sdeleted;
1322 }
Eric Dumazeta50feda2012-05-18 18:57:34 +00001323 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
1325
1326static int
1327mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1328{
1329 struct ip6_sf_list *psf;
1330 int scount = 0;
1331
1332 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1333 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1334 continue;
1335 scount++;
1336 }
1337 return scount;
1338}
1339
YOSHIFUJI Hideaki / 吉藤英明2576f17d2013-01-21 06:48:19 +00001340static void ip6_mc_hdr(struct sock *sk, struct sk_buff *skb,
1341 struct net_device *dev,
1342 const struct in6_addr *saddr,
1343 const struct in6_addr *daddr,
1344 int proto, int len)
1345{
1346 struct ipv6hdr *hdr;
1347
1348 skb->protocol = htons(ETH_P_IPV6);
1349 skb->dev = dev;
1350
1351 skb_reset_network_header(skb);
1352 skb_put(skb, sizeof(struct ipv6hdr));
1353 hdr = ipv6_hdr(skb);
1354
1355 ip6_flow_hdr(hdr, 0, 0);
1356
1357 hdr->payload_len = htons(len);
1358 hdr->nexthdr = proto;
1359 hdr->hop_limit = inet6_sk(sk)->hop_limit;
1360
1361 hdr->saddr = *saddr;
1362 hdr->daddr = *daddr;
1363}
1364
Amerigo Wang89657792013-06-29 21:30:49 +08001365static struct sk_buff *mld_newpack(struct inet6_dev *idev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
Amerigo Wang89657792013-06-29 21:30:49 +08001367 struct net_device *dev = idev->dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001368 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001369 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 struct sk_buff *skb;
1371 struct mld2_report *pmr;
1372 struct in6_addr addr_buf;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001373 const struct in6_addr *saddr;
Herbert Xua7ae1992011-11-18 02:20:04 +00001374 int hlen = LL_RESERVED_SPACE(dev);
1375 int tlen = dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 int err;
1377 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1378 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1379 IPV6_TLV_PADN, 0 };
1380
1381 /* we assume size > sizeof(ra) here */
Herbert Xua7ae1992011-11-18 02:20:04 +00001382 size += hlen + tlen;
Eric Dumazet72e09ad2010-06-05 03:03:30 -07001383 /* limit our allocations to order-0 page */
1384 size = min_t(int, size, SKB_MAX_ORDER(0, 0));
1385 skb = sock_alloc_send_skb(sk, size, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001387 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return NULL;
1389
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +02001390 skb->priority = TC_PRIO_CONTROL;
Herbert Xua7ae1992011-11-18 02:20:04 +00001391 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Amerigo Wang89657792013-06-29 21:30:49 +08001393 if (__ipv6_get_lladdr(idev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001395 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 * when a valid link-local address is not available.
1397 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001398 saddr = &in6addr_any;
1399 } else
1400 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
YOSHIFUJI Hideaki / 吉藤英明2576f17d2013-01-21 06:48:19 +00001402 ip6_mc_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1405
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001406 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -03001407 skb_put(skb, sizeof(*pmr));
1408 pmr = (struct mld2_report *)skb_transport_header(skb);
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001409 pmr->mld2r_type = ICMPV6_MLD2_REPORT;
1410 pmr->mld2r_resv1 = 0;
1411 pmr->mld2r_cksum = 0;
1412 pmr->mld2r_resv2 = 0;
1413 pmr->mld2r_ngrec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return skb;
1415}
1416
1417static void mld_sendpack(struct sk_buff *skb)
1418{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001419 struct ipv6hdr *pip6 = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001420 struct mld2_report *pmr =
1421 (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 int payload_len, mldlen;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001423 struct inet6_dev *idev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001424 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 int err;
David S. Miller4c9483b2011-03-12 16:22:43 -05001426 struct flowi6 fl6;
Eric Dumazetadf30902009-06-02 05:19:30 +00001427 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Eric Dumazet96b52e62010-06-07 21:05:02 +00001429 rcu_read_lock();
1430 idev = __in6_dev_get(skb->dev);
Neil Hormanedf391f2009-04-27 02:45:02 -07001431 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
1432
Simon Horman29a3cad2013-05-28 20:34:26 +00001433 payload_len = (skb_tail_pointer(skb) - skb_network_header(skb)) -
1434 sizeof(*pip6);
1435 mldlen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 pip6->payload_len = htons(payload_len);
1437
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001438 pmr->mld2r_cksum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1439 IPPROTO_ICMPV6,
1440 csum_partial(skb_transport_header(skb),
1441 mldlen, 0));
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001442
David S. Miller4c9483b2011-03-12 16:22:43 -05001443 icmpv6_flow_init(net->ipv6.igmp_sk, &fl6, ICMPV6_MLD2_REPORT,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001444 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1445 skb->dev->ifindex);
YOSHIFUJI Hideaki / 吉藤英明12fd84f2013-01-18 02:00:24 +00001446 dst = icmp6_dst_alloc(skb->dev, &fl6);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001447
David S. Miller452edd52011-03-02 13:27:41 -08001448 err = 0;
1449 if (IS_ERR(dst)) {
1450 err = PTR_ERR(dst);
1451 dst = NULL;
1452 }
Eric Dumazetadf30902009-06-02 05:19:30 +00001453 skb_dst_set(skb, dst);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001454 if (err)
1455 goto err_out;
1456
Neil Hormanedf391f2009-04-27 02:45:02 -07001457 payload_len = skb->len;
1458
Jan Engelhardtb2e0b382010-03-23 04:09:07 +01001459 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001460 dst_output);
1461out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (!err) {
Denis V. Lunev5a57d4c2008-10-08 10:34:14 -07001463 ICMP6MSGOUT_INC_STATS_BH(net, idev, ICMPV6_MLD2_REPORT);
Denis V. Luneve41b5362008-10-08 10:33:26 -07001464 ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
Neil Hormanedf391f2009-04-27 02:45:02 -07001465 IP6_UPD_PO_STATS_BH(net, idev, IPSTATS_MIB_OUTMCAST, payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 } else
Denis V. Lunev483a47d2008-10-08 11:09:27 -07001467 IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Eric Dumazet96b52e62010-06-07 21:05:02 +00001469 rcu_read_unlock();
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001470 return;
1471
1472err_out:
1473 kfree_skb(skb);
1474 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
1477static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1478{
Yan Zhengfab10fe2005-10-05 12:08:13 -07001479 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
1482static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1483 int type, struct mld2_grec **ppgr)
1484{
1485 struct net_device *dev = pmc->idev->dev;
1486 struct mld2_report *pmr;
1487 struct mld2_grec *pgr;
1488
1489 if (!skb)
Amerigo Wang89657792013-06-29 21:30:49 +08001490 skb = mld_newpack(pmc->idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (!skb)
1492 return NULL;
1493 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1494 pgr->grec_type = type;
1495 pgr->grec_auxwords = 0;
1496 pgr->grec_nsrcs = 0;
1497 pgr->grec_mca = pmc->mca_addr; /* structure copy */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001498 pmr = (struct mld2_report *)skb_transport_header(skb);
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001499 pmr->mld2r_ngrec = htons(ntohs(pmr->mld2r_ngrec)+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 *ppgr = pgr;
1501 return skb;
1502}
1503
1504#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1505 skb_tailroom(skb)) : 0)
1506
1507static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1508 int type, int gdeleted, int sdeleted)
1509{
Amerigo Wang89657792013-06-29 21:30:49 +08001510 struct inet6_dev *idev = pmc->idev;
1511 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 struct mld2_report *pmr;
1513 struct mld2_grec *pgr = NULL;
1514 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001515 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 if (pmc->mca_flags & MAF_NOREPORT)
1518 return skb;
1519
1520 isquery = type == MLD2_MODE_IS_INCLUDE ||
1521 type == MLD2_MODE_IS_EXCLUDE;
1522 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1523 type == MLD2_CHANGE_TO_EXCLUDE;
1524
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001525 stotal = scount = 0;
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1528
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001529 if (!*psf_list)
1530 goto empty_source;
1531
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001532 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 /* EX and TO_EX get a fresh packet, if needed */
1535 if (truncate) {
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001536 if (pmr && pmr->mld2r_ngrec &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1538 if (skb)
1539 mld_sendpack(skb);
Amerigo Wang89657792013-06-29 21:30:49 +08001540 skb = mld_newpack(idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
1542 }
1543 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 psf_prev = NULL;
1545 for (psf=*psf_list; psf; psf=psf_next) {
1546 struct in6_addr *psrc;
1547
1548 psf_next = psf->sf_next;
1549
1550 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1551 psf_prev = psf;
1552 continue;
1553 }
1554
1555 /* clear marks on query responses */
1556 if (isquery)
1557 psf->sf_gsresp = 0;
1558
1559 if (AVAILABLE(skb) < sizeof(*psrc) +
1560 first*sizeof(struct mld2_grec)) {
1561 if (truncate && !first)
1562 break; /* truncate these */
1563 if (pgr)
1564 pgr->grec_nsrcs = htons(scount);
1565 if (skb)
1566 mld_sendpack(skb);
Amerigo Wang89657792013-06-29 21:30:49 +08001567 skb = mld_newpack(idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 first = 1;
1569 scount = 0;
1570 }
1571 if (first) {
1572 skb = add_grhead(skb, pmc, type, &pgr);
1573 first = 0;
1574 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -08001575 if (!skb)
1576 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1578 *psrc = psf->sf_addr;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001579 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1581 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1582 psf->sf_crcount--;
1583 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1584 if (psf_prev)
1585 psf_prev->sf_next = psf->sf_next;
1586 else
1587 *psf_list = psf->sf_next;
1588 kfree(psf);
1589 continue;
1590 }
1591 }
1592 psf_prev = psf;
1593 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001594
1595empty_source:
1596 if (!stotal) {
1597 if (type == MLD2_ALLOW_NEW_SOURCES ||
1598 type == MLD2_BLOCK_OLD_SOURCES)
1599 return skb;
1600 if (pmc->mca_crcount || isquery) {
1601 /* make sure we have room for group header */
1602 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1603 mld_sendpack(skb);
1604 skb = NULL; /* add_grhead will get a new one */
1605 }
1606 skb = add_grhead(skb, pmc, type, &pgr);
1607 }
1608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (pgr)
1610 pgr->grec_nsrcs = htons(scount);
1611
1612 if (isquery)
1613 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1614 return skb;
1615}
1616
1617static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1618{
1619 struct sk_buff *skb = NULL;
1620 int type;
1621
Amerigo Wang89657792013-06-29 21:30:49 +08001622 read_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (!pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1625 if (pmc->mca_flags & MAF_NOREPORT)
1626 continue;
1627 spin_lock_bh(&pmc->mca_lock);
1628 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1629 type = MLD2_MODE_IS_EXCLUDE;
1630 else
1631 type = MLD2_MODE_IS_INCLUDE;
1632 skb = add_grec(skb, pmc, type, 0, 0);
1633 spin_unlock_bh(&pmc->mca_lock);
1634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 } else {
1636 spin_lock_bh(&pmc->mca_lock);
1637 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1638 type = MLD2_MODE_IS_EXCLUDE;
1639 else
1640 type = MLD2_MODE_IS_INCLUDE;
1641 skb = add_grec(skb, pmc, type, 0, 0);
1642 spin_unlock_bh(&pmc->mca_lock);
1643 }
Amerigo Wang89657792013-06-29 21:30:49 +08001644 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (skb)
1646 mld_sendpack(skb);
1647}
1648
1649/*
1650 * remove zero-count source records from a source filter list
1651 */
1652static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1653{
1654 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1655
1656 psf_prev = NULL;
1657 for (psf=*ppsf; psf; psf = psf_next) {
1658 psf_next = psf->sf_next;
1659 if (psf->sf_crcount == 0) {
1660 if (psf_prev)
1661 psf_prev->sf_next = psf->sf_next;
1662 else
1663 *ppsf = psf->sf_next;
1664 kfree(psf);
1665 } else
1666 psf_prev = psf;
1667 }
1668}
1669
1670static void mld_send_cr(struct inet6_dev *idev)
1671{
1672 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1673 struct sk_buff *skb = NULL;
1674 int type, dtype;
1675
1676 read_lock_bh(&idev->lock);
Stephen Hemminger6457d262010-02-17 18:48:44 -08001677 spin_lock(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679 /* deleted MCA's */
1680 pmc_prev = NULL;
1681 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1682 pmc_next = pmc->next;
1683 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1684 type = MLD2_BLOCK_OLD_SOURCES;
1685 dtype = MLD2_BLOCK_OLD_SOURCES;
1686 skb = add_grec(skb, pmc, type, 1, 0);
1687 skb = add_grec(skb, pmc, dtype, 1, 1);
1688 }
1689 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1691 type = MLD2_CHANGE_TO_INCLUDE;
1692 skb = add_grec(skb, pmc, type, 1, 0);
1693 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001694 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 if (pmc->mca_crcount == 0) {
1696 mld_clear_zeros(&pmc->mca_tomb);
1697 mld_clear_zeros(&pmc->mca_sources);
1698 }
1699 }
1700 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1701 !pmc->mca_sources) {
1702 if (pmc_prev)
1703 pmc_prev->next = pmc_next;
1704 else
1705 idev->mc_tomb = pmc_next;
1706 in6_dev_put(pmc->idev);
1707 kfree(pmc);
1708 } else
1709 pmc_prev = pmc;
1710 }
Stephen Hemminger6457d262010-02-17 18:48:44 -08001711 spin_unlock(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 /* change recs */
1714 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1715 spin_lock_bh(&pmc->mca_lock);
1716 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1717 type = MLD2_BLOCK_OLD_SOURCES;
1718 dtype = MLD2_ALLOW_NEW_SOURCES;
1719 } else {
1720 type = MLD2_ALLOW_NEW_SOURCES;
1721 dtype = MLD2_BLOCK_OLD_SOURCES;
1722 }
1723 skb = add_grec(skb, pmc, type, 0, 0);
1724 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1725
1726 /* filter mode changes */
1727 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1729 type = MLD2_CHANGE_TO_EXCLUDE;
1730 else
1731 type = MLD2_CHANGE_TO_INCLUDE;
1732 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001733 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 }
1735 spin_unlock_bh(&pmc->mca_lock);
1736 }
1737 read_unlock_bh(&idev->lock);
1738 if (!skb)
1739 return;
1740 (void) mld_sendpack(skb);
1741}
1742
1743static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1744{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001745 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001746 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 struct inet6_dev *idev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001748 struct sk_buff *skb;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001749 struct mld_msg *hdr;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001750 const struct in6_addr *snd_addr, *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 struct in6_addr addr_buf;
Herbert Xua7ae1992011-11-18 02:20:04 +00001752 int hlen = LL_RESERVED_SPACE(dev);
1753 int tlen = dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 int err, len, payload_len, full_len;
1755 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1756 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1757 IPV6_TLV_PADN, 0 };
David S. Miller4c9483b2011-03-12 16:22:43 -05001758 struct flowi6 fl6;
Eric Dumazetadf30902009-06-02 05:19:30 +00001759 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09001761 if (type == ICMPV6_MGM_REDUCTION)
1762 snd_addr = &in6addr_linklocal_allrouters;
1763 else
1764 snd_addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1767 payload_len = len + sizeof(ra);
1768 full_len = sizeof(struct ipv6hdr) + payload_len;
1769
Neil Hormanedf391f2009-04-27 02:45:02 -07001770 rcu_read_lock();
1771 IP6_UPD_PO_STATS(net, __in6_dev_get(dev),
1772 IPSTATS_MIB_OUT, full_len);
1773 rcu_read_unlock();
1774
Herbert Xua7ae1992011-11-18 02:20:04 +00001775 skb = sock_alloc_send_skb(sk, hlen + tlen + full_len, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 if (skb == NULL) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001778 rcu_read_lock();
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001779 IP6_INC_STATS(net, __in6_dev_get(dev),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001780 IPSTATS_MIB_OUTDISCARDS);
1781 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return;
1783 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +02001784 skb->priority = TC_PRIO_CONTROL;
Herbert Xua7ae1992011-11-18 02:20:04 +00001785 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Neil Horman95c385b2007-04-25 17:08:10 -07001787 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001789 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 * when a valid link-local address is not available.
1791 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001792 saddr = &in6addr_any;
1793 } else
1794 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
YOSHIFUJI Hideaki / 吉藤英明2576f17d2013-01-21 06:48:19 +00001796 ip6_mc_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1799
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001800 hdr = (struct mld_msg *) skb_put(skb, sizeof(struct mld_msg));
1801 memset(hdr, 0, sizeof(struct mld_msg));
1802 hdr->mld_type = type;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001803 hdr->mld_mca = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001805 hdr->mld_cksum = csum_ipv6_magic(saddr, snd_addr, len,
1806 IPPROTO_ICMPV6,
1807 csum_partial(hdr, len, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Eric Dumazet96b52e62010-06-07 21:05:02 +00001809 rcu_read_lock();
1810 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
David S. Miller4c9483b2011-03-12 16:22:43 -05001812 icmpv6_flow_init(sk, &fl6, type,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001813 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1814 skb->dev->ifindex);
YOSHIFUJI Hideaki / 吉藤英明12fd84f2013-01-18 02:00:24 +00001815 dst = icmp6_dst_alloc(skb->dev, &fl6);
David S. Miller452edd52011-03-02 13:27:41 -08001816 if (IS_ERR(dst)) {
1817 err = PTR_ERR(dst);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001818 goto err_out;
David S. Miller452edd52011-03-02 13:27:41 -08001819 }
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001820
Eric Dumazetadf30902009-06-02 05:19:30 +00001821 skb_dst_set(skb, dst);
Jan Engelhardtb2e0b382010-03-23 04:09:07 +01001822 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001823 dst_output);
1824out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (!err) {
Denis V. Lunev5c5d2442008-10-08 10:33:50 -07001826 ICMP6MSGOUT_INC_STATS(net, idev, type);
Denis V. Luneva862f6a2008-10-08 10:33:06 -07001827 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
Neil Hormanedf391f2009-04-27 02:45:02 -07001828 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, full_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 } else
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001830 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Eric Dumazet96b52e62010-06-07 21:05:02 +00001832 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return;
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001834
1835err_out:
1836 kfree_skb(skb);
1837 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02001840static void mld_resend_report(struct inet6_dev *idev)
1841{
1842 if (MLD_V1_SEEN(idev)) {
1843 struct ifmcaddr6 *mcaddr;
1844 read_lock_bh(&idev->lock);
1845 for (mcaddr = idev->mc_list; mcaddr; mcaddr = mcaddr->next) {
1846 if (!(mcaddr->mca_flags & MAF_NOREPORT))
1847 igmp6_send(&mcaddr->mca_addr, idev->dev,
1848 ICMPV6_MGM_REPORT);
1849 }
1850 read_unlock_bh(&idev->lock);
1851 } else {
1852 mld_send_report(idev, NULL);
1853 }
1854}
1855
1856void ipv6_mc_dad_complete(struct inet6_dev *idev)
1857{
1858 idev->mc_dad_count = idev->mc_qrv;
1859 if (idev->mc_dad_count) {
1860 mld_resend_report(idev);
1861 idev->mc_dad_count--;
1862 if (idev->mc_dad_count)
1863 mld_dad_start_timer(idev, idev->mc_maxdelay);
1864 }
1865}
1866
1867static void mld_dad_timer_expire(unsigned long data)
1868{
1869 struct inet6_dev *idev = (struct inet6_dev *)data;
1870
1871 mld_resend_report(idev);
1872 if (idev->mc_dad_count) {
1873 idev->mc_dad_count--;
1874 if (idev->mc_dad_count)
1875 mld_dad_start_timer(idev, idev->mc_maxdelay);
1876 }
1877 __in6_dev_put(idev);
1878}
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001881 const struct in6_addr *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
1883 struct ip6_sf_list *psf, *psf_prev;
1884 int rv = 0;
1885
1886 psf_prev = NULL;
1887 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1888 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1889 break;
1890 psf_prev = psf;
1891 }
1892 if (!psf || psf->sf_count[sfmode] == 0) {
1893 /* source filter not found, or count wrong => bug */
1894 return -ESRCH;
1895 }
1896 psf->sf_count[sfmode]--;
1897 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1898 struct inet6_dev *idev = pmc->idev;
1899
1900 /* no more filters for this source */
1901 if (psf_prev)
1902 psf_prev->sf_next = psf->sf_next;
1903 else
1904 pmc->mca_sources = psf->sf_next;
1905 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1906 !MLD_V1_SEEN(idev)) {
1907 psf->sf_crcount = idev->mc_qrv;
1908 psf->sf_next = pmc->mca_tomb;
1909 pmc->mca_tomb = psf;
1910 rv = 1;
1911 } else
1912 kfree(psf);
1913 }
1914 return rv;
1915}
1916
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001917static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
1918 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 int delta)
1920{
1921 struct ifmcaddr6 *pmc;
1922 int changerec = 0;
1923 int i, err;
1924
1925 if (!idev)
1926 return -ENODEV;
1927 read_lock_bh(&idev->lock);
1928 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1929 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1930 break;
1931 }
1932 if (!pmc) {
1933 /* MCA not found?? bug */
1934 read_unlock_bh(&idev->lock);
1935 return -ESRCH;
1936 }
1937 spin_lock_bh(&pmc->mca_lock);
1938 sf_markstate(pmc);
1939 if (!delta) {
1940 if (!pmc->mca_sfcount[sfmode]) {
1941 spin_unlock_bh(&pmc->mca_lock);
1942 read_unlock_bh(&idev->lock);
1943 return -EINVAL;
1944 }
1945 pmc->mca_sfcount[sfmode]--;
1946 }
1947 err = 0;
1948 for (i=0; i<sfcount; i++) {
1949 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1950
1951 changerec |= rv > 0;
1952 if (!err && rv < 0)
1953 err = rv;
1954 }
1955 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1956 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1957 pmc->mca_sfcount[MCAST_INCLUDE]) {
1958 struct ip6_sf_list *psf;
1959
1960 /* filter mode change */
1961 pmc->mca_sfmode = MCAST_INCLUDE;
1962 pmc->mca_crcount = idev->mc_qrv;
1963 idev->mc_ifc_count = pmc->mca_crcount;
1964 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1965 psf->sf_crcount = 0;
1966 mld_ifc_event(pmc->idev);
1967 } else if (sf_setstate(pmc) || changerec)
1968 mld_ifc_event(pmc->idev);
1969 spin_unlock_bh(&pmc->mca_lock);
1970 read_unlock_bh(&idev->lock);
1971 return err;
1972}
1973
1974/*
1975 * Add multicast single-source filter to the interface list
1976 */
1977static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
Jun Zhao99d2f472011-11-30 06:21:05 +00001978 const struct in6_addr *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
1980 struct ip6_sf_list *psf, *psf_prev;
1981
1982 psf_prev = NULL;
1983 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1984 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1985 break;
1986 psf_prev = psf;
1987 }
1988 if (!psf) {
Ingo Oeser0c600ed2006-03-20 23:01:32 -08001989 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (!psf)
1991 return -ENOBUFS;
Ingo Oeser0c600ed2006-03-20 23:01:32 -08001992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 psf->sf_addr = *psfsrc;
1994 if (psf_prev) {
1995 psf_prev->sf_next = psf;
1996 } else
1997 pmc->mca_sources = psf;
1998 }
1999 psf->sf_count[sfmode]++;
2000 return 0;
2001}
2002
2003static void sf_markstate(struct ifmcaddr6 *pmc)
2004{
2005 struct ip6_sf_list *psf;
2006 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2007
2008 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
2009 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2010 psf->sf_oldin = mca_xcount ==
2011 psf->sf_count[MCAST_EXCLUDE] &&
2012 !psf->sf_count[MCAST_INCLUDE];
2013 } else
2014 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2015}
2016
2017static int sf_setstate(struct ifmcaddr6 *pmc)
2018{
David L Stevens7add2a42006-01-24 13:06:39 -08002019 struct ip6_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2021 int qrv = pmc->idev->mc_qrv;
2022 int new_in, rv;
2023
2024 rv = 0;
2025 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
2026 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2027 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2028 !psf->sf_count[MCAST_INCLUDE];
2029 } else
2030 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevens7add2a42006-01-24 13:06:39 -08002031 if (new_in) {
2032 if (!psf->sf_oldin) {
Al Viroe80e28b2006-02-03 20:10:03 -05002033 struct ip6_sf_list *prev = NULL;
David L Stevens7add2a42006-01-24 13:06:39 -08002034
2035 for (dpsf=pmc->mca_tomb; dpsf;
2036 dpsf=dpsf->sf_next) {
2037 if (ipv6_addr_equal(&dpsf->sf_addr,
2038 &psf->sf_addr))
2039 break;
2040 prev = dpsf;
2041 }
2042 if (dpsf) {
2043 if (prev)
2044 prev->sf_next = dpsf->sf_next;
2045 else
2046 pmc->mca_tomb = dpsf->sf_next;
2047 kfree(dpsf);
2048 }
2049 psf->sf_crcount = qrv;
2050 rv++;
2051 }
2052 } else if (psf->sf_oldin) {
2053 psf->sf_crcount = 0;
2054 /*
2055 * add or update "delete" records if an active filter
2056 * is now inactive
2057 */
2058 for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
2059 if (ipv6_addr_equal(&dpsf->sf_addr,
2060 &psf->sf_addr))
2061 break;
2062 if (!dpsf) {
Joe Perches5d553542010-05-31 17:23:22 +00002063 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevens7add2a42006-01-24 13:06:39 -08002064 if (!dpsf)
2065 continue;
2066 *dpsf = *psf;
2067 /* pmc->mca_lock held by callers */
2068 dpsf->sf_next = pmc->mca_tomb;
2069 pmc->mca_tomb = dpsf;
2070 }
2071 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 rv++;
2073 }
2074 }
2075 return rv;
2076}
2077
2078/*
2079 * Add multicast source filter list to the interface list
2080 */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002081static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2082 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 int delta)
2084{
2085 struct ifmcaddr6 *pmc;
2086 int isexclude;
2087 int i, err;
2088
2089 if (!idev)
2090 return -ENODEV;
2091 read_lock_bh(&idev->lock);
2092 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2093 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2094 break;
2095 }
2096 if (!pmc) {
2097 /* MCA not found?? bug */
2098 read_unlock_bh(&idev->lock);
2099 return -ESRCH;
2100 }
2101 spin_lock_bh(&pmc->mca_lock);
2102
2103 sf_markstate(pmc);
2104 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2105 if (!delta)
2106 pmc->mca_sfcount[sfmode]++;
2107 err = 0;
2108 for (i=0; i<sfcount; i++) {
Jun Zhao99d2f472011-11-30 06:21:05 +00002109 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (err)
2111 break;
2112 }
2113 if (err) {
2114 int j;
2115
2116 if (!delta)
2117 pmc->mca_sfcount[sfmode]--;
2118 for (j=0; j<i; j++)
RongQing.Li78d50212012-04-04 16:47:04 +00002119 ip6_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 struct ip6_sf_list *psf;
2122
2123 /* filter mode change */
2124 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2125 pmc->mca_sfmode = MCAST_EXCLUDE;
2126 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2127 pmc->mca_sfmode = MCAST_INCLUDE;
2128 /* else no filters; keep old mode for reports */
2129
2130 pmc->mca_crcount = idev->mc_qrv;
2131 idev->mc_ifc_count = pmc->mca_crcount;
2132 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2133 psf->sf_crcount = 0;
2134 mld_ifc_event(idev);
2135 } else if (sf_setstate(pmc))
2136 mld_ifc_event(idev);
2137 spin_unlock_bh(&pmc->mca_lock);
2138 read_unlock_bh(&idev->lock);
2139 return err;
2140}
2141
2142static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2143{
2144 struct ip6_sf_list *psf, *nextpsf;
2145
2146 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2147 nextpsf = psf->sf_next;
2148 kfree(psf);
2149 }
2150 pmc->mca_tomb = NULL;
2151 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2152 nextpsf = psf->sf_next;
2153 kfree(psf);
2154 }
2155 pmc->mca_sources = NULL;
2156 pmc->mca_sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002157 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2159}
2160
2161
2162static void igmp6_join_group(struct ifmcaddr6 *ma)
2163{
2164 unsigned long delay;
2165
2166 if (ma->mca_flags & MAF_NOREPORT)
2167 return;
2168
2169 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2170
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02002171 delay = net_random() % unsolicited_report_interval(ma->idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 spin_lock_bh(&ma->mca_lock);
2174 if (del_timer(&ma->mca_timer)) {
2175 atomic_dec(&ma->mca_refcnt);
2176 delay = ma->mca_timer.expires - jiffies;
2177 }
2178
2179 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2180 atomic_inc(&ma->mca_refcnt);
2181 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2182 spin_unlock_bh(&ma->mca_lock);
2183}
2184
2185static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2186 struct inet6_dev *idev)
2187{
2188 int err;
2189
David L Stevens5ab4a6c2005-12-27 14:03:00 -08002190 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2191 * so no other readers or writers of iml or its sflist
2192 */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002193 if (!iml->sflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 /* any-source empty exclude case */
2195 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2196 }
2197 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2198 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2199 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2200 iml->sflist = NULL;
2201 return err;
2202}
2203
2204static void igmp6_leave_group(struct ifmcaddr6 *ma)
2205{
2206 if (MLD_V1_SEEN(ma->idev)) {
2207 if (ma->mca_flags & MAF_LAST_REPORTER)
2208 igmp6_send(&ma->mca_addr, ma->idev->dev,
2209 ICMPV6_MGM_REDUCTION);
2210 } else {
2211 mld_add_delrec(ma->idev, ma);
2212 mld_ifc_event(ma->idev);
2213 }
2214}
2215
2216static void mld_gq_timer_expire(unsigned long data)
2217{
2218 struct inet6_dev *idev = (struct inet6_dev *)data;
2219
2220 idev->mc_gq_running = 0;
2221 mld_send_report(idev, NULL);
2222 __in6_dev_put(idev);
2223}
2224
2225static void mld_ifc_timer_expire(unsigned long data)
2226{
2227 struct inet6_dev *idev = (struct inet6_dev *)data;
2228
2229 mld_send_cr(idev);
2230 if (idev->mc_ifc_count) {
2231 idev->mc_ifc_count--;
2232 if (idev->mc_ifc_count)
2233 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2234 }
2235 __in6_dev_put(idev);
2236}
2237
2238static void mld_ifc_event(struct inet6_dev *idev)
2239{
2240 if (MLD_V1_SEEN(idev))
2241 return;
2242 idev->mc_ifc_count = idev->mc_qrv;
2243 mld_ifc_start_timer(idev, 1);
2244}
2245
2246
2247static void igmp6_timer_handler(unsigned long data)
2248{
2249 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2250
2251 if (MLD_V1_SEEN(ma->idev))
2252 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2253 else
2254 mld_send_report(ma->idev, ma);
2255
2256 spin_lock(&ma->mca_lock);
2257 ma->mca_flags |= MAF_LAST_REPORTER;
2258 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2259 spin_unlock(&ma->mca_lock);
2260 ma_put(ma);
2261}
2262
Moni Shoua75c78502009-09-15 02:37:40 -07002263/* Device changing type */
2264
2265void ipv6_mc_unmap(struct inet6_dev *idev)
2266{
2267 struct ifmcaddr6 *i;
2268
2269 /* Install multicast list, except for all-nodes (already installed) */
2270
2271 read_lock_bh(&idev->lock);
2272 for (i = idev->mc_list; i; i = i->next)
2273 igmp6_group_dropped(i);
2274 read_unlock_bh(&idev->lock);
2275}
2276
2277void ipv6_mc_remap(struct inet6_dev *idev)
2278{
2279 ipv6_mc_up(idev);
2280}
2281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282/* Device going down */
2283
2284void ipv6_mc_down(struct inet6_dev *idev)
2285{
2286 struct ifmcaddr6 *i;
2287
2288 /* Withdraw multicast list */
2289
2290 read_lock_bh(&idev->lock);
2291 idev->mc_ifc_count = 0;
2292 if (del_timer(&idev->mc_ifc_timer))
2293 __in6_dev_put(idev);
2294 idev->mc_gq_running = 0;
2295 if (del_timer(&idev->mc_gq_timer))
2296 __in6_dev_put(idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002297 if (del_timer(&idev->mc_dad_timer))
2298 __in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 for (i = idev->mc_list; i; i=i->next)
2301 igmp6_group_dropped(i);
2302 read_unlock_bh(&idev->lock);
2303
2304 mld_clear_delrec(idev);
2305}
2306
2307
2308/* Device going up */
2309
2310void ipv6_mc_up(struct inet6_dev *idev)
2311{
2312 struct ifmcaddr6 *i;
2313
2314 /* Install multicast list, except for all-nodes (already installed) */
2315
2316 read_lock_bh(&idev->lock);
2317 for (i = idev->mc_list; i; i=i->next)
2318 igmp6_group_added(i);
2319 read_unlock_bh(&idev->lock);
2320}
2321
2322/* IPv6 device initialization. */
2323
2324void ipv6_mc_init_dev(struct inet6_dev *idev)
2325{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 write_lock_bh(&idev->lock);
Stephen Hemminger6457d262010-02-17 18:48:44 -08002327 spin_lock_init(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 idev->mc_gq_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002329 setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2330 (unsigned long)idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 idev->mc_tomb = NULL;
2332 idev->mc_ifc_count = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002333 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2334 (unsigned long)idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002335 setup_timer(&idev->mc_dad_timer, mld_dad_timer_expire,
2336 (unsigned long)idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 idev->mc_qrv = MLD_QRV_DEFAULT;
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02002338 idev->mc_maxdelay = unsolicited_report_interval(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 idev->mc_v1_seen = 0;
2340 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341}
2342
2343/*
2344 * Device is about to be destroyed: clean up.
2345 */
2346
2347void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2348{
2349 struct ifmcaddr6 *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
2351 /* Deactivate timers */
2352 ipv6_mc_down(idev);
2353
2354 /* Delete all-nodes address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2356 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2357 * fail.
2358 */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002359 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002361 if (idev->cnf.forwarding)
2362 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
2364 write_lock_bh(&idev->lock);
2365 while ((i = idev->mc_list) != NULL) {
2366 idev->mc_list = i->next;
2367 write_unlock_bh(&idev->lock);
2368
2369 igmp6_group_dropped(i);
2370 ma_put(i);
2371
2372 write_lock_bh(&idev->lock);
2373 }
2374 write_unlock_bh(&idev->lock);
2375}
2376
2377#ifdef CONFIG_PROC_FS
2378struct igmp6_mc_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002379 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 struct net_device *dev;
2381 struct inet6_dev *idev;
2382};
2383
2384#define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2385
2386static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2387{
2388 struct ifmcaddr6 *im = NULL;
2389 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002390 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Pavel Emelianov7562f872007-05-03 15:13:45 -07002392 state->idev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002393 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 struct inet6_dev *idev;
Eric Dumazetce81b762009-11-11 17:34:30 +00002395 idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 if (!idev)
2397 continue;
2398 read_lock_bh(&idev->lock);
2399 im = idev->mc_list;
2400 if (im) {
2401 state->idev = idev;
2402 break;
2403 }
2404 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 }
2406 return im;
2407}
2408
2409static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2410{
2411 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2412
2413 im = im->next;
2414 while (!im) {
Eric Dumazetce81b762009-11-11 17:34:30 +00002415 if (likely(state->idev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 read_unlock_bh(&state->idev->lock);
Eric Dumazetce81b762009-11-11 17:34:30 +00002417
2418 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 if (!state->dev) {
2420 state->idev = NULL;
2421 break;
2422 }
Eric Dumazetce81b762009-11-11 17:34:30 +00002423 state->idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 if (!state->idev)
2425 continue;
2426 read_lock_bh(&state->idev->lock);
2427 im = state->idev->mc_list;
2428 }
2429 return im;
2430}
2431
2432static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2433{
2434 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2435 if (im)
2436 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2437 --pos;
2438 return pos ? NULL : im;
2439}
2440
2441static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetce81b762009-11-11 17:34:30 +00002442 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
Eric Dumazetce81b762009-11-11 17:34:30 +00002444 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 return igmp6_mc_get_idx(seq, *pos);
2446}
2447
2448static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2449{
Eric Dumazetce81b762009-11-11 17:34:30 +00002450 struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);
2451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 ++*pos;
2453 return im;
2454}
2455
2456static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetce81b762009-11-11 17:34:30 +00002457 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458{
2459 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
Eric Dumazetce81b762009-11-11 17:34:30 +00002460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 if (likely(state->idev != NULL)) {
2462 read_unlock_bh(&state->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 state->idev = NULL;
2464 }
2465 state->dev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002466 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467}
2468
2469static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2470{
2471 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2472 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2473
2474 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002475 "%-4d %-15s %pi6 %5d %08X %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 state->dev->ifindex, state->dev->name,
Harvey Harrisonb0711952008-10-28 16:05:40 -07002477 &im->mca_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 im->mca_users, im->mca_flags,
2479 (im->mca_flags&MAF_TIMER_RUNNING) ?
2480 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2481 return 0;
2482}
2483
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002484static const struct seq_operations igmp6_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 .start = igmp6_mc_seq_start,
2486 .next = igmp6_mc_seq_next,
2487 .stop = igmp6_mc_seq_stop,
2488 .show = igmp6_mc_seq_show,
2489};
2490
2491static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2492{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002493 return seq_open_net(inode, file, &igmp6_mc_seq_ops,
2494 sizeof(struct igmp6_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495}
2496
Arjan van de Ven9a321442007-02-12 00:55:35 -08002497static const struct file_operations igmp6_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 .owner = THIS_MODULE,
2499 .open = igmp6_mc_seq_open,
2500 .read = seq_read,
2501 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002502 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503};
2504
2505struct igmp6_mcf_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002506 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 struct net_device *dev;
2508 struct inet6_dev *idev;
2509 struct ifmcaddr6 *im;
2510};
2511
2512#define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2513
2514static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2515{
2516 struct ip6_sf_list *psf = NULL;
2517 struct ifmcaddr6 *im = NULL;
2518 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002519 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Pavel Emelianov7562f872007-05-03 15:13:45 -07002521 state->idev = NULL;
2522 state->im = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002523 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 struct inet6_dev *idev;
Eric Dumazetce81b762009-11-11 17:34:30 +00002525 idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 if (unlikely(idev == NULL))
2527 continue;
2528 read_lock_bh(&idev->lock);
2529 im = idev->mc_list;
2530 if (likely(im != NULL)) {
2531 spin_lock_bh(&im->mca_lock);
2532 psf = im->mca_sources;
2533 if (likely(psf != NULL)) {
2534 state->im = im;
2535 state->idev = idev;
2536 break;
2537 }
2538 spin_unlock_bh(&im->mca_lock);
2539 }
2540 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 }
2542 return psf;
2543}
2544
2545static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2546{
2547 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2548
2549 psf = psf->sf_next;
2550 while (!psf) {
2551 spin_unlock_bh(&state->im->mca_lock);
2552 state->im = state->im->next;
2553 while (!state->im) {
Eric Dumazetce81b762009-11-11 17:34:30 +00002554 if (likely(state->idev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 read_unlock_bh(&state->idev->lock);
Eric Dumazetce81b762009-11-11 17:34:30 +00002556
2557 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 if (!state->dev) {
2559 state->idev = NULL;
2560 goto out;
2561 }
Eric Dumazetce81b762009-11-11 17:34:30 +00002562 state->idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if (!state->idev)
2564 continue;
2565 read_lock_bh(&state->idev->lock);
2566 state->im = state->idev->mc_list;
2567 }
2568 if (!state->im)
2569 break;
2570 spin_lock_bh(&state->im->mca_lock);
2571 psf = state->im->mca_sources;
2572 }
2573out:
2574 return psf;
2575}
2576
2577static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2578{
2579 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2580 if (psf)
2581 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2582 --pos;
2583 return pos ? NULL : psf;
2584}
2585
2586static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetce81b762009-11-11 17:34:30 +00002587 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
Eric Dumazetce81b762009-11-11 17:34:30 +00002589 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2591}
2592
2593static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2594{
2595 struct ip6_sf_list *psf;
2596 if (v == SEQ_START_TOKEN)
2597 psf = igmp6_mcf_get_first(seq);
2598 else
2599 psf = igmp6_mcf_get_next(seq, v);
2600 ++*pos;
2601 return psf;
2602}
2603
2604static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetce81b762009-11-11 17:34:30 +00002605 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
2607 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2608 if (likely(state->im != NULL)) {
2609 spin_unlock_bh(&state->im->mca_lock);
2610 state->im = NULL;
2611 }
2612 if (likely(state->idev != NULL)) {
2613 read_unlock_bh(&state->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 state->idev = NULL;
2615 }
2616 state->dev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002617 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618}
2619
2620static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2621{
2622 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2623 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2624
2625 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002626 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 "%3s %6s "
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002628 "%32s %32s %6s %6s\n", "Idx",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 "Device", "Multicast Address",
2630 "Source Address", "INC", "EXC");
2631 } else {
2632 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002633 "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 state->dev->ifindex, state->dev->name,
Harvey Harrisonb0711952008-10-28 16:05:40 -07002635 &state->im->mca_addr,
2636 &psf->sf_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 psf->sf_count[MCAST_INCLUDE],
2638 psf->sf_count[MCAST_EXCLUDE]);
2639 }
2640 return 0;
2641}
2642
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002643static const struct seq_operations igmp6_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 .start = igmp6_mcf_seq_start,
2645 .next = igmp6_mcf_seq_next,
2646 .stop = igmp6_mcf_seq_stop,
2647 .show = igmp6_mcf_seq_show,
2648};
2649
2650static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2651{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002652 return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
2653 sizeof(struct igmp6_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654}
2655
Arjan van de Ven9a321442007-02-12 00:55:35 -08002656static const struct file_operations igmp6_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 .owner = THIS_MODULE,
2658 .open = igmp6_mcf_seq_open,
2659 .read = seq_read,
2660 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002661 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662};
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002663
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002664static int __net_init igmp6_proc_init(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002665{
2666 int err;
2667
2668 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002669 if (!proc_create("igmp6", S_IRUGO, net->proc_net, &igmp6_mc_seq_fops))
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002670 goto out;
Gao fengd4beaa62013-02-18 01:34:54 +00002671 if (!proc_create("mcfilter6", S_IRUGO, net->proc_net,
2672 &igmp6_mcf_seq_fops))
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002673 goto out_proc_net_igmp6;
2674
2675 err = 0;
2676out:
2677 return err;
2678
2679out_proc_net_igmp6:
Gao fengece31ff2013-02-18 01:34:56 +00002680 remove_proc_entry("igmp6", net->proc_net);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002681 goto out;
2682}
2683
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002684static void __net_exit igmp6_proc_exit(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002685{
Gao fengece31ff2013-02-18 01:34:56 +00002686 remove_proc_entry("mcfilter6", net->proc_net);
2687 remove_proc_entry("igmp6", net->proc_net);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002688}
2689#else
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002690static inline int igmp6_proc_init(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002691{
2692 return 0;
2693}
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002694static inline void igmp6_proc_exit(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002695{
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002696}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697#endif
2698
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002699static int __net_init igmp6_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 int err;
2702
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002703 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2704 SOCK_RAW, IPPROTO_ICMPV6, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00002706 pr_err("Failed to initialize the IGMP6 control socket (err %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 err);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002708 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 }
2710
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002711 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002713 err = igmp6_proc_init(net);
2714 if (err)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002715 goto out_sock_create;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002716out:
2717 return err;
2718
2719out_sock_create:
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002720 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002721 goto out;
2722}
2723
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002724static void __net_exit igmp6_net_exit(struct net *net)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002725{
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002726 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002727 igmp6_proc_exit(net);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002728}
2729
2730static struct pernet_operations igmp6_net_ops = {
2731 .init = igmp6_net_init,
2732 .exit = igmp6_net_exit,
2733};
2734
2735int __init igmp6_init(void)
2736{
2737 return register_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
2739
2740void igmp6_cleanup(void)
2741{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002742 unregister_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743}