blob: db25b8eb62bda9f7e5a0a352ca79a83a7aca00eb [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
111#define IGMP6_UNSOLICITED_IVAL (10*HZ)
112#define MLD_QRV_DEFAULT 2
113
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700114#define MLD_V1_SEEN(idev) (dev_net((idev)->dev)->ipv6.devconf_all->force_mld_version == 1 || \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 (idev)->cnf.force_mld_version == 1 || \
116 ((idev)->mc_v1_seen && \
117 time_before(jiffies, (idev)->mc_v1_seen)))
118
David L Stevens6f4353d2005-12-26 17:03:46 -0800119#define IPV6_MLD_MAX_MSF 64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Brian Haleyab32ea52006-09-22 14:15:41 -0700121int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123/*
124 * socket join on multicast group
125 */
126
Eric Dumazet456b61b2010-11-23 13:12:15 +0000127#define for_each_pmc_rcu(np, pmc) \
128 for (pmc = rcu_dereference(np->ipv6_mc_list); \
129 pmc != NULL; \
130 pmc = rcu_dereference(pmc->next))
131
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900132int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 struct net_device *dev = NULL;
135 struct ipv6_mc_socklist *mc_lst;
136 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900137 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 int err;
139
140 if (!ipv6_addr_is_multicast(addr))
141 return -EINVAL;
142
Eric Dumazet456b61b2010-11-23 13:12:15 +0000143 rcu_read_lock();
144 for_each_pmc_rcu(np, mc_lst) {
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700145 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
146 ipv6_addr_equal(&mc_lst->addr, addr)) {
Eric Dumazet456b61b2010-11-23 13:12:15 +0000147 rcu_read_unlock();
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700148 return -EADDRINUSE;
149 }
150 }
Eric Dumazet456b61b2010-11-23 13:12:15 +0000151 rcu_read_unlock();
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
154
155 if (mc_lst == NULL)
156 return -ENOMEM;
157
158 mc_lst->next = NULL;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000159 mc_lst->addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Eric Dumazet96b52e62010-06-07 21:05:02 +0000161 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (ifindex == 0) {
163 struct rt6_info *rt;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800164 rt = rt6_lookup(net, addr, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (rt) {
David S. Millerd1918542011-12-28 20:19:20 -0500166 dev = rt->dst.dev;
Amerigo Wang94e187c2012-10-29 00:13:19 +0000167 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169 } else
Eric Dumazet96b52e62010-06-07 21:05:02 +0000170 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 if (dev == NULL) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000173 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
175 return -ENODEV;
176 }
177
178 mc_lst->ifindex = dev->ifindex;
179 mc_lst->sfmode = MCAST_EXCLUDE;
YOSHIFUJI Hideaki196433c2006-01-04 13:56:31 -0800180 rwlock_init(&mc_lst->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 mc_lst->sflist = NULL;
182
183 /*
184 * now add/increase the group membership on the device
185 */
186
187 err = ipv6_dev_mc_inc(dev, addr);
188
189 if (err) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000190 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return err;
193 }
194
Eric Dumazet456b61b2010-11-23 13:12:15 +0000195 spin_lock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 mc_lst->next = np->ipv6_mc_list;
Eric Dumazet456b61b2010-11-23 13:12:15 +0000197 rcu_assign_pointer(np->ipv6_mc_list, mc_lst);
198 spin_unlock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Eric Dumazet96b52e62010-06-07 21:05:02 +0000200 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 return 0;
203}
204
205/*
206 * socket leave on multicast group
207 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900208int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 struct ipv6_pinfo *np = inet6_sk(sk);
Eric Dumazet456b61b2010-11-23 13:12:15 +0000211 struct ipv6_mc_socklist *mc_lst;
212 struct ipv6_mc_socklist __rcu **lnk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900213 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Li Weia858d642012-07-17 15:28:59 +0800215 if (!ipv6_addr_is_multicast(addr))
216 return -EINVAL;
217
Eric Dumazet456b61b2010-11-23 13:12:15 +0000218 spin_lock(&ipv6_sk_mc_lock);
219 for (lnk = &np->ipv6_mc_list;
220 (mc_lst = rcu_dereference_protected(*lnk,
221 lockdep_is_held(&ipv6_sk_mc_lock))) !=NULL ;
222 lnk = &mc_lst->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
224 ipv6_addr_equal(&mc_lst->addr, addr)) {
225 struct net_device *dev;
226
227 *lnk = mc_lst->next;
Eric Dumazet456b61b2010-11-23 13:12:15 +0000228 spin_unlock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Eric Dumazet96b52e62010-06-07 21:05:02 +0000230 rcu_read_lock();
231 dev = dev_get_by_index_rcu(net, mc_lst->ifindex);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800232 if (dev != NULL) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000233 struct inet6_dev *idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
David L Stevensacd6e002006-08-17 16:27:39 -0700235 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000236 if (idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
David L Stevensacd6e002006-08-17 16:27:39 -0700238 } else
239 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000240 rcu_read_unlock();
Eric Dumazet456b61b2010-11-23 13:12:15 +0000241 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
Lai Jiangshane3cbf282011-03-18 12:00:50 +0800242 kfree_rcu(mc_lst, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return 0;
244 }
245 }
Eric Dumazet456b61b2010-11-23 13:12:15 +0000246 spin_unlock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
David L Stevens9951f032005-07-08 17:47:28 -0700248 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Eric Dumazet96b52e62010-06-07 21:05:02 +0000251/* called with rcu_read_lock() */
252static struct inet6_dev *ip6_mc_find_dev_rcu(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000253 const struct in6_addr *group,
Eric Dumazet96b52e62010-06-07 21:05:02 +0000254 int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 struct net_device *dev = NULL;
257 struct inet6_dev *idev = NULL;
258
259 if (ifindex == 0) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000260 struct rt6_info *rt = rt6_lookup(net, group, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (rt) {
David S. Millerd1918542011-12-28 20:19:20 -0500263 dev = rt->dst.dev;
Amerigo Wang94e187c2012-10-29 00:13:19 +0000264 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266 } else
Eric Dumazet96b52e62010-06-07 21:05:02 +0000267 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 if (!dev)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000270 return NULL;
271 idev = __in6_dev_get(dev);
Ilpo Järvinen448eb712008-12-14 23:15:21 -0800272 if (!idev)
Joe Perches8a22c992010-11-14 17:05:00 +0000273 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 read_lock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000275 if (idev->dead) {
276 read_unlock_bh(&idev->lock);
277 return NULL;
278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return idev;
280}
281
282void ipv6_sock_mc_close(struct sock *sk)
283{
284 struct ipv6_pinfo *np = inet6_sk(sk);
285 struct ipv6_mc_socklist *mc_lst;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900286 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Eric Dumazet0e1efe92012-12-05 09:18:10 +0000288 if (!rcu_access_pointer(np->ipv6_mc_list))
289 return;
290
Eric Dumazet456b61b2010-11-23 13:12:15 +0000291 spin_lock(&ipv6_sk_mc_lock);
292 while ((mc_lst = rcu_dereference_protected(np->ipv6_mc_list,
293 lockdep_is_held(&ipv6_sk_mc_lock))) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 struct net_device *dev;
295
296 np->ipv6_mc_list = mc_lst->next;
Eric Dumazet456b61b2010-11-23 13:12:15 +0000297 spin_unlock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Eric Dumazet96b52e62010-06-07 21:05:02 +0000299 rcu_read_lock();
300 dev = dev_get_by_index_rcu(net, mc_lst->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (dev) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000302 struct inet6_dev *idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
David L Stevensacd6e002006-08-17 16:27:39 -0700304 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000305 if (idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
David L Stevensacd6e002006-08-17 16:27:39 -0700307 } else
308 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000309 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Eric Dumazet456b61b2010-11-23 13:12:15 +0000311 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
Lai Jiangshane3cbf282011-03-18 12:00:50 +0800312 kfree_rcu(mc_lst, rcu);
Eric Dumazet456b61b2010-11-23 13:12:15 +0000313
314 spin_lock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
Eric Dumazet456b61b2010-11-23 13:12:15 +0000316 spin_unlock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
319int ip6_mc_source(int add, int omode, struct sock *sk,
320 struct group_source_req *pgsr)
321{
322 struct in6_addr *source, *group;
323 struct ipv6_mc_socklist *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 struct inet6_dev *idev;
325 struct ipv6_pinfo *inet6 = inet6_sk(sk);
326 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900327 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 int i, j, rv;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700329 int leavegroup = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800330 int pmclocked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 int err;
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
334 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
335
336 if (!ipv6_addr_is_multicast(group))
337 return -EINVAL;
338
Eric Dumazet96b52e62010-06-07 21:05:02 +0000339 rcu_read_lock();
340 idev = ip6_mc_find_dev_rcu(net, group, pgsr->gsr_interface);
341 if (!idev) {
342 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return -ENODEV;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 err = -EADDRNOTAVAIL;
347
Eric Dumazet456b61b2010-11-23 13:12:15 +0000348 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
350 continue;
351 if (ipv6_addr_equal(&pmc->addr, group))
352 break;
353 }
David L Stevens917f2f12005-07-08 17:45:16 -0700354 if (!pmc) { /* must have a prior join */
355 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /* if a source filter was set, must be the same mode as before */
359 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -0700360 if (pmc->sfmode != omode) {
361 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 } else if (pmc->sfmode != omode) {
365 /* allow mode switches for empty-set filters */
366 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
367 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
368 pmc->sfmode = omode;
369 }
370
Eric Dumazet96b52e62010-06-07 21:05:02 +0000371 write_lock(&pmc->sflock);
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800372 pmclocked = 1;
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 psl = pmc->sflist;
375 if (!add) {
376 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -0700377 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 rv = !0;
379 for (i=0; i<psl->sl_count; i++) {
YOSHIFUJI Hideaki / 吉藤英明07c2fec2013-01-29 12:48:23 +0000380 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (rv == 0)
382 break;
383 }
384 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -0700385 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700387 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
388 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
389 leavegroup = 1;
390 goto done;
391 }
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /* update the interface filter */
394 ip6_mc_del_src(idev, group, omode, 1, source, 1);
395
396 for (j=i+1; j<psl->sl_count; j++)
397 psl->sl_addr[j-1] = psl->sl_addr[j];
398 psl->sl_count--;
399 err = 0;
400 goto done;
401 }
402 /* else, add a new source to the filter */
403
404 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
405 err = -ENOBUFS;
406 goto done;
407 }
408 if (!psl || psl->sl_count == psl->sl_max) {
409 struct ip6_sf_socklist *newpsl;
410 int count = IP6_SFBLOCK;
411
412 if (psl)
413 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800414 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (!newpsl) {
416 err = -ENOBUFS;
417 goto done;
418 }
419 newpsl->sl_max = count;
420 newpsl->sl_count = count - IP6_SFBLOCK;
421 if (psl) {
422 for (i=0; i<psl->sl_count; i++)
423 newpsl->sl_addr[i] = psl->sl_addr[i];
424 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
425 }
426 pmc->sflist = psl = newpsl;
427 }
428 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
429 for (i=0; i<psl->sl_count; i++) {
YOSHIFUJI Hideaki / 吉藤英明07c2fec2013-01-29 12:48:23 +0000430 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
Jean Sacren56db1c52013-02-03 21:34:10 +0000431 if (rv == 0) /* There is an error in the address. */
432 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 for (j=psl->sl_count-1; j>=i; j--)
435 psl->sl_addr[j+1] = psl->sl_addr[j];
436 psl->sl_addr[i] = *source;
437 psl->sl_count++;
438 err = 0;
439 /* update the interface list */
440 ip6_mc_add_src(idev, group, omode, 1, source, 1);
441done:
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800442 if (pmclocked)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000443 write_unlock(&pmc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000445 rcu_read_unlock();
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700446 if (leavegroup)
447 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return err;
449}
450
451int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
452{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000453 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 struct ipv6_mc_socklist *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 struct inet6_dev *idev;
456 struct ipv6_pinfo *inet6 = inet6_sk(sk);
457 struct ip6_sf_socklist *newpsl, *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900458 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -0700459 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 int i, err;
461
462 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
463
464 if (!ipv6_addr_is_multicast(group))
465 return -EINVAL;
466 if (gsf->gf_fmode != MCAST_INCLUDE &&
467 gsf->gf_fmode != MCAST_EXCLUDE)
468 return -EINVAL;
469
Eric Dumazet96b52e62010-06-07 21:05:02 +0000470 rcu_read_lock();
471 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Eric Dumazet96b52e62010-06-07 21:05:02 +0000473 if (!idev) {
474 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return -ENODEV;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
David S. Miller9c059892005-07-08 21:44:39 -0700478 err = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800479
David L Stevens9951f032005-07-08 17:47:28 -0700480 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
481 leavegroup = 1;
482 goto done;
483 }
484
Eric Dumazet456b61b2010-11-23 13:12:15 +0000485 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (pmc->ifindex != gsf->gf_interface)
487 continue;
488 if (ipv6_addr_equal(&pmc->addr, group))
489 break;
490 }
David L Stevens917f2f12005-07-08 17:45:16 -0700491 if (!pmc) { /* must have a prior join */
492 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (gsf->gf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800496 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
497 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (!newpsl) {
499 err = -ENOBUFS;
500 goto done;
501 }
502 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
503 for (i=0; i<newpsl->sl_count; ++i) {
504 struct sockaddr_in6 *psin6;
505
506 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
507 newpsl->sl_addr[i] = psin6->sin6_addr;
508 }
509 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
510 newpsl->sl_count, newpsl->sl_addr, 0);
511 if (err) {
512 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
513 goto done;
514 }
Yan Zheng8713dbf2005-10-28 08:02:08 +0800515 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +0800517 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
518 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800519
Eric Dumazet96b52e62010-06-07 21:05:02 +0000520 write_lock(&pmc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 psl = pmc->sflist;
522 if (psl) {
523 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
524 psl->sl_count, psl->sl_addr, 0);
525 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
526 } else
527 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
528 pmc->sflist = newpsl;
529 pmc->sfmode = gsf->gf_fmode;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000530 write_unlock(&pmc->sflock);
David L Stevens917f2f12005-07-08 17:45:16 -0700531 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532done:
533 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000534 rcu_read_unlock();
David L Stevens9951f032005-07-08 17:47:28 -0700535 if (leavegroup)
536 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return err;
538}
539
540int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
541 struct group_filter __user *optval, int __user *optlen)
542{
543 int err, i, count, copycount;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000544 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 struct ipv6_mc_socklist *pmc;
546 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 struct ipv6_pinfo *inet6 = inet6_sk(sk);
548 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900549 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
552
553 if (!ipv6_addr_is_multicast(group))
554 return -EINVAL;
555
Eric Dumazet96b52e62010-06-07 21:05:02 +0000556 rcu_read_lock();
557 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Eric Dumazet96b52e62010-06-07 21:05:02 +0000559 if (!idev) {
560 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return -ENODEV;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 err = -EADDRNOTAVAIL;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800565 /*
566 * changes to the ipv6_mc_list require the socket lock and
567 * a read lock on ip6_sk_mc_lock. We have the socket lock,
568 * so reading the list is safe.
569 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Eric Dumazet456b61b2010-11-23 13:12:15 +0000571 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (pmc->ifindex != gsf->gf_interface)
573 continue;
574 if (ipv6_addr_equal(group, &pmc->addr))
575 break;
576 }
577 if (!pmc) /* must have a prior join */
578 goto done;
579 gsf->gf_fmode = pmc->sfmode;
580 psl = pmc->sflist;
581 count = psl ? psl->sl_count : 0;
582 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000583 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
586 gsf->gf_numsrc = count;
587 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
588 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
589 return -EFAULT;
590 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800591 /* changes to psl require the socket lock, a read lock on
592 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
593 * have the socket lock, so reading here is safe.
594 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 for (i=0; i<copycount; i++) {
596 struct sockaddr_in6 *psin6;
597 struct sockaddr_storage ss;
598
599 psin6 = (struct sockaddr_in6 *)&ss;
600 memset(&ss, 0, sizeof(ss));
601 psin6->sin6_family = AF_INET6;
602 psin6->sin6_addr = psl->sl_addr[i];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900603 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return -EFAULT;
605 }
606 return 0;
607done:
608 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000609 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return err;
611}
612
Eric Dumazeta50feda2012-05-18 18:57:34 +0000613bool inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
614 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 struct ipv6_pinfo *np = inet6_sk(sk);
617 struct ipv6_mc_socklist *mc;
618 struct ip6_sf_socklist *psl;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000619 bool rv = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Eric Dumazet456b61b2010-11-23 13:12:15 +0000621 rcu_read_lock();
622 for_each_pmc_rcu(np, mc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (ipv6_addr_equal(&mc->addr, mc_addr))
624 break;
625 }
626 if (!mc) {
Eric Dumazet456b61b2010-11-23 13:12:15 +0000627 rcu_read_unlock();
Eric Dumazeta50feda2012-05-18 18:57:34 +0000628 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800630 read_lock(&mc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 psl = mc->sflist;
632 if (!psl) {
633 rv = mc->sfmode == MCAST_EXCLUDE;
634 } else {
635 int i;
636
637 for (i=0; i<psl->sl_count; i++) {
638 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
639 break;
640 }
641 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000642 rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000644 rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800646 read_unlock(&mc->sflock);
Eric Dumazet456b61b2010-11-23 13:12:15 +0000647 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 return rv;
650}
651
652static void ma_put(struct ifmcaddr6 *mc)
653{
654 if (atomic_dec_and_test(&mc->mca_refcnt)) {
655 in6_dev_put(mc->idev);
656 kfree(mc);
657 }
658}
659
660static void igmp6_group_added(struct ifmcaddr6 *mc)
661{
662 struct net_device *dev = mc->idev->dev;
663 char buf[MAX_ADDR_LEN];
664
YOSHIFUJI Hideaki / 吉藤英明ec16ef22013-02-09 04:29:58 +0000665 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
666 IPV6_ADDR_SCOPE_LINKLOCAL)
667 return;
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 spin_lock_bh(&mc->mca_lock);
670 if (!(mc->mca_flags&MAF_LOADED)) {
671 mc->mca_flags |= MAF_LOADED;
672 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000673 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675 spin_unlock_bh(&mc->mca_lock);
676
677 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
678 return;
679
680 if (MLD_V1_SEEN(mc->idev)) {
681 igmp6_join_group(mc);
682 return;
683 }
684 /* else v2 */
685
686 mc->mca_crcount = mc->idev->mc_qrv;
687 mld_ifc_event(mc->idev);
688}
689
690static void igmp6_group_dropped(struct ifmcaddr6 *mc)
691{
692 struct net_device *dev = mc->idev->dev;
693 char buf[MAX_ADDR_LEN];
694
YOSHIFUJI Hideaki / 吉藤英明ec16ef22013-02-09 04:29:58 +0000695 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
696 IPV6_ADDR_SCOPE_LINKLOCAL)
697 return;
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 spin_lock_bh(&mc->mca_lock);
700 if (mc->mca_flags&MAF_LOADED) {
701 mc->mca_flags &= ~MAF_LOADED;
702 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000703 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705
706 if (mc->mca_flags & MAF_NOREPORT)
707 goto done;
708 spin_unlock_bh(&mc->mca_lock);
709
710 if (!mc->idev->dead)
711 igmp6_leave_group(mc);
712
713 spin_lock_bh(&mc->mca_lock);
714 if (del_timer(&mc->mca_timer))
715 atomic_dec(&mc->mca_refcnt);
716done:
717 ip6_mc_clear_src(mc);
718 spin_unlock_bh(&mc->mca_lock);
719}
720
721/*
722 * deleted ifmcaddr6 manipulation
723 */
724static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
725{
726 struct ifmcaddr6 *pmc;
727
728 /* this is an "ifmcaddr6" for convenience; only the fields below
729 * are actually used. In particular, the refcnt and users are not
730 * used for management of the delete list. Using the same structure
731 * for deleted items allows change reports to use common code with
732 * non-deleted or query-response MCA's.
733 */
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800734 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (!pmc)
736 return;
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 spin_lock_bh(&im->mca_lock);
739 spin_lock_init(&pmc->mca_lock);
740 pmc->idev = im->idev;
741 in6_dev_hold(idev);
742 pmc->mca_addr = im->mca_addr;
743 pmc->mca_crcount = idev->mc_qrv;
744 pmc->mca_sfmode = im->mca_sfmode;
745 if (pmc->mca_sfmode == MCAST_INCLUDE) {
746 struct ip6_sf_list *psf;
747
748 pmc->mca_tomb = im->mca_tomb;
749 pmc->mca_sources = im->mca_sources;
750 im->mca_tomb = im->mca_sources = NULL;
751 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
752 psf->sf_crcount = pmc->mca_crcount;
753 }
754 spin_unlock_bh(&im->mca_lock);
755
Stephen Hemminger6457d262010-02-17 18:48:44 -0800756 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 pmc->next = idev->mc_tomb;
758 idev->mc_tomb = pmc;
Stephen Hemminger6457d262010-02-17 18:48:44 -0800759 spin_unlock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000762static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *pmca)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 struct ifmcaddr6 *pmc, *pmc_prev;
765 struct ip6_sf_list *psf, *psf_next;
766
Stephen Hemminger6457d262010-02-17 18:48:44 -0800767 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 pmc_prev = NULL;
769 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
770 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
771 break;
772 pmc_prev = pmc;
773 }
774 if (pmc) {
775 if (pmc_prev)
776 pmc_prev->next = pmc->next;
777 else
778 idev->mc_tomb = pmc->next;
779 }
Stephen Hemminger6457d262010-02-17 18:48:44 -0800780 spin_unlock_bh(&idev->mc_lock);
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (pmc) {
783 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
784 psf_next = psf->sf_next;
785 kfree(psf);
786 }
787 in6_dev_put(pmc->idev);
788 kfree(pmc);
789 }
790}
791
792static void mld_clear_delrec(struct inet6_dev *idev)
793{
794 struct ifmcaddr6 *pmc, *nextpmc;
795
Stephen Hemminger6457d262010-02-17 18:48:44 -0800796 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 pmc = idev->mc_tomb;
798 idev->mc_tomb = NULL;
Stephen Hemminger6457d262010-02-17 18:48:44 -0800799 spin_unlock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 for (; pmc; pmc = nextpmc) {
802 nextpmc = pmc->next;
803 ip6_mc_clear_src(pmc);
804 in6_dev_put(pmc->idev);
805 kfree(pmc);
806 }
807
808 /* clear dead sources, too */
809 read_lock_bh(&idev->lock);
810 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
811 struct ip6_sf_list *psf, *psf_next;
812
813 spin_lock_bh(&pmc->mca_lock);
814 psf = pmc->mca_tomb;
815 pmc->mca_tomb = NULL;
816 spin_unlock_bh(&pmc->mca_lock);
817 for (; psf; psf=psf_next) {
818 psf_next = psf->sf_next;
819 kfree(psf);
820 }
821 }
822 read_unlock_bh(&idev->lock);
823}
824
825
826/*
827 * device multicast group inc (add if not found)
828 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900829int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 struct ifmcaddr6 *mc;
832 struct inet6_dev *idev;
833
Eric Dumazet96b52e62010-06-07 21:05:02 +0000834 /* we need to take a reference on idev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 idev = in6_dev_get(dev);
836
837 if (idev == NULL)
838 return -EINVAL;
839
840 write_lock_bh(&idev->lock);
841 if (idev->dead) {
842 write_unlock_bh(&idev->lock);
843 in6_dev_put(idev);
844 return -ENODEV;
845 }
846
847 for (mc = idev->mc_list; mc; mc = mc->next) {
848 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
849 mc->mca_users++;
850 write_unlock_bh(&idev->lock);
851 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
852 NULL, 0);
853 in6_dev_put(idev);
854 return 0;
855 }
856 }
857
858 /*
859 * not found: create a new one.
860 */
861
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800862 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 if (mc == NULL) {
865 write_unlock_bh(&idev->lock);
866 in6_dev_put(idev);
867 return -ENOMEM;
868 }
869
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800870 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000872 mc->mca_addr = *addr;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000873 mc->idev = idev; /* (reference taken) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 mc->mca_users = 1;
875 /* mca_stamp should be updated upon changes */
876 mc->mca_cstamp = mc->mca_tstamp = jiffies;
877 atomic_set(&mc->mca_refcnt, 2);
878 spin_lock_init(&mc->mca_lock);
879
880 /* initial mode is (EX, empty) */
881 mc->mca_sfmode = MCAST_EXCLUDE;
882 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
883
884 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
885 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
886 mc->mca_flags |= MAF_NOREPORT;
887
888 mc->next = idev->mc_list;
889 idev->mc_list = mc;
890 write_unlock_bh(&idev->lock);
891
892 mld_del_delrec(idev, &mc->mca_addr);
893 igmp6_group_added(mc);
894 ma_put(mc);
895 return 0;
896}
897
898/*
899 * device multicast group del
900 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900901int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 struct ifmcaddr6 *ma, **map;
904
905 write_lock_bh(&idev->lock);
906 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
907 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
908 if (--ma->mca_users == 0) {
909 *map = ma->next;
910 write_unlock_bh(&idev->lock);
911
912 igmp6_group_dropped(ma);
913
914 ma_put(ma);
915 return 0;
916 }
917 write_unlock_bh(&idev->lock);
918 return 0;
919 }
920 }
921 write_unlock_bh(&idev->lock);
922
923 return -ENOENT;
924}
925
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900926int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Eric Dumazet96b52e62010-06-07 21:05:02 +0000928 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 int err;
930
Eric Dumazet96b52e62010-06-07 21:05:02 +0000931 rcu_read_lock();
932
933 idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (!idev)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000935 err = -ENODEV;
936 else
937 err = __ipv6_dev_mc_dec(idev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Eric Dumazet96b52e62010-06-07 21:05:02 +0000939 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return err;
941}
942
943/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 * check if the interface/address pair is valid
945 */
Eric Dumazeta50feda2012-05-18 18:57:34 +0000946bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
947 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 struct inet6_dev *idev;
950 struct ifmcaddr6 *mc;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000951 bool rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Eric Dumazet96b52e62010-06-07 21:05:02 +0000953 rcu_read_lock();
954 idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (idev) {
956 read_lock_bh(&idev->lock);
957 for (mc = idev->mc_list; mc; mc=mc->next) {
958 if (ipv6_addr_equal(&mc->mca_addr, group))
959 break;
960 }
961 if (mc) {
962 if (src_addr && !ipv6_addr_any(src_addr)) {
963 struct ip6_sf_list *psf;
964
965 spin_lock_bh(&mc->mca_lock);
966 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
967 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
968 break;
969 }
970 if (psf)
971 rv = psf->sf_count[MCAST_INCLUDE] ||
972 psf->sf_count[MCAST_EXCLUDE] !=
973 mc->mca_sfcount[MCAST_EXCLUDE];
974 else
975 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
976 spin_unlock_bh(&mc->mca_lock);
977 } else
Eric Dumazeta50feda2012-05-18 18:57:34 +0000978 rv = true; /* don't filter unspecified source */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
Eric Dumazet96b52e62010-06-07 21:05:02 +0000982 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return rv;
984}
985
986static void mld_gq_start_timer(struct inet6_dev *idev)
987{
988 int tv = net_random() % idev->mc_maxdelay;
989
990 idev->mc_gq_running = 1;
991 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
992 in6_dev_hold(idev);
993}
994
995static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
996{
997 int tv = net_random() % delay;
998
999 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1000 in6_dev_hold(idev);
1001}
1002
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02001003static void mld_dad_start_timer(struct inet6_dev *idev, int delay)
1004{
1005 int tv = net_random() % delay;
1006
1007 if (!mod_timer(&idev->mc_dad_timer, jiffies+tv+2))
1008 in6_dev_hold(idev);
1009}
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011/*
1012 * IGMP handling (alias multicast ICMPv6 messages)
1013 */
1014
1015static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1016{
1017 unsigned long delay = resptime;
1018
1019 /* Do not start timer for these addresses */
1020 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1021 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1022 return;
1023
1024 if (del_timer(&ma->mca_timer)) {
1025 atomic_dec(&ma->mca_refcnt);
1026 delay = ma->mca_timer.expires - jiffies;
1027 }
1028
1029 if (delay >= resptime) {
1030 if (resptime)
1031 delay = net_random() % resptime;
1032 else
1033 delay = 1;
1034 }
1035 ma->mca_timer.expires = jiffies + delay;
1036 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1037 atomic_inc(&ma->mca_refcnt);
1038 ma->mca_flags |= MAF_TIMER_RUNNING;
1039}
1040
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001041/* mark EXCLUDE-mode sources */
Eric Dumazeta50feda2012-05-18 18:57:34 +00001042static bool mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1043 const struct in6_addr *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 struct ip6_sf_list *psf;
1046 int i, scount;
1047
1048 scount = 0;
1049 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1050 if (scount == nsrcs)
1051 break;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001052 for (i=0; i<nsrcs; i++) {
1053 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +00001054 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001055 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1056 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +08001057 break;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001058 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1059 scount++;
1060 break;
1061 }
1062 }
1063 }
1064 pmc->mca_flags &= ~MAF_GSQUERY;
1065 if (scount == nsrcs) /* all sources excluded */
Eric Dumazeta50feda2012-05-18 18:57:34 +00001066 return false;
1067 return true;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001068}
1069
Eric Dumazeta50feda2012-05-18 18:57:34 +00001070static bool mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1071 const struct in6_addr *srcs)
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001072{
1073 struct ip6_sf_list *psf;
1074 int i, scount;
1075
1076 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1077 return mld_xmarksources(pmc, nsrcs, srcs);
1078
1079 /* mark INCLUDE-mode sources */
1080
1081 scount = 0;
1082 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1083 if (scount == nsrcs)
1084 break;
1085 for (i=0; i<nsrcs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1087 psf->sf_gsresp = 1;
1088 scount++;
1089 break;
1090 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001093 if (!scount) {
1094 pmc->mca_flags &= ~MAF_GSQUERY;
Eric Dumazeta50feda2012-05-18 18:57:34 +00001095 return false;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001096 }
1097 pmc->mca_flags |= MAF_GSQUERY;
Eric Dumazeta50feda2012-05-18 18:57:34 +00001098 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
Eric Dumazet96b52e62010-06-07 21:05:02 +00001101/* called with rcu_read_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102int igmp6_event_query(struct sk_buff *skb)
1103{
Yan Zheng97300b52005-10-31 20:09:45 +08001104 struct mld2_query *mlh2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 struct ifmcaddr6 *ma;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001106 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 unsigned long max_delay;
1108 struct inet6_dev *idev;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001109 struct mld_msg *mld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 int group_type;
1111 int mark = 0;
1112 int len;
1113
1114 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1115 return -EINVAL;
1116
1117 /* compute payload length excluding extension headers */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001118 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001119 len -= skb_network_header_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 /* Drop queries with not link local source */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001122 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return -EINVAL;
1124
Eric Dumazet96b52e62010-06-07 21:05:02 +00001125 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 if (idev == NULL)
1128 return 0;
1129
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001130 mld = (struct mld_msg *)icmp6_hdr(skb);
1131 group = &mld->mld_mca;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 group_type = ipv6_addr_type(group);
1133
1134 if (group_type != IPV6_ADDR_ANY &&
Eric Dumazet96b52e62010-06-07 21:05:02 +00001135 !(group_type&IPV6_ADDR_MULTICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 if (len == 24) {
1139 int switchback;
1140 /* MLDv1 router present */
1141
1142 /* Translate milliseconds to jiffies */
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001143 max_delay = (ntohs(mld->mld_maxdelay)*HZ)/1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 switchback = (idev->mc_qrv + 1) * max_delay;
1146 idev->mc_v1_seen = jiffies + switchback;
1147
1148 /* cancel the interface change timer */
1149 idev->mc_ifc_count = 0;
1150 if (del_timer(&idev->mc_ifc_timer))
1151 __in6_dev_put(idev);
1152 /* clear deleted report items */
1153 mld_clear_delrec(idev);
1154 } else if (len >= 28) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001155 int srcs_offset = sizeof(struct mld2_query) -
Yan Zheng97300b52005-10-31 20:09:45 +08001156 sizeof(struct icmp6hdr);
Eric Dumazet96b52e62010-06-07 21:05:02 +00001157 if (!pskb_may_pull(skb, srcs_offset))
Yan Zheng97300b52005-10-31 20:09:45 +08001158 return -EINVAL;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001159
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001160 mlh2 = (struct mld2_query *)skb_transport_header(skb);
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001161 max_delay = (MLDV2_MRC(ntohs(mlh2->mld2q_mrc))*HZ)/1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (!max_delay)
1163 max_delay = 1;
1164 idev->mc_maxdelay = max_delay;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001165 if (mlh2->mld2q_qrv)
1166 idev->mc_qrv = mlh2->mld2q_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (group_type == IPV6_ADDR_ANY) { /* general query */
Eric Dumazet96b52e62010-06-07 21:05:02 +00001168 if (mlh2->mld2q_nsrcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return -EINVAL; /* no sources allowed */
Eric Dumazet96b52e62010-06-07 21:05:02 +00001170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 mld_gq_start_timer(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return 0;
1173 }
1174 /* mark sources to include, if group & source-specific */
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001175 if (mlh2->mld2q_nsrcs != 0) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001176 if (!pskb_may_pull(skb, srcs_offset +
Eric Dumazet96b52e62010-06-07 21:05:02 +00001177 ntohs(mlh2->mld2q_nsrcs) * sizeof(struct in6_addr)))
Yan Zheng97300b52005-10-31 20:09:45 +08001178 return -EINVAL;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001179
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001180 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Yan Zheng97300b52005-10-31 20:09:45 +08001181 mark = 1;
1182 }
Eric Dumazet96b52e62010-06-07 21:05:02 +00001183 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 read_lock_bh(&idev->lock);
1187 if (group_type == IPV6_ADDR_ANY) {
1188 for (ma = idev->mc_list; ma; ma=ma->next) {
1189 spin_lock_bh(&ma->mca_lock);
1190 igmp6_group_queried(ma, max_delay);
1191 spin_unlock_bh(&ma->mca_lock);
1192 }
1193 } else {
1194 for (ma = idev->mc_list; ma; ma=ma->next) {
David L Stevens7add2a42006-01-24 13:06:39 -08001195 if (!ipv6_addr_equal(group, &ma->mca_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 continue;
1197 spin_lock_bh(&ma->mca_lock);
1198 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1199 /* gsquery <- gsquery && mark */
1200 if (!mark)
1201 ma->mca_flags &= ~MAF_GSQUERY;
1202 } else {
1203 /* gsquery <- mark */
1204 if (mark)
1205 ma->mca_flags |= MAF_GSQUERY;
1206 else
1207 ma->mca_flags &= ~MAF_GSQUERY;
1208 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001209 if (!(ma->mca_flags & MAF_GSQUERY) ||
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001210 mld_marksources(ma, ntohs(mlh2->mld2q_nsrcs), mlh2->mld2q_srcs))
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001211 igmp6_group_queried(ma, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 spin_unlock_bh(&ma->mca_lock);
David L Stevens7add2a42006-01-24 13:06:39 -08001213 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215 }
1216 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 return 0;
1219}
1220
Eric Dumazet96b52e62010-06-07 21:05:02 +00001221/* called with rcu_read_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222int igmp6_event_report(struct sk_buff *skb)
1223{
1224 struct ifmcaddr6 *ma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 struct inet6_dev *idev;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001226 struct mld_msg *mld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 int addr_type;
1228
1229 /* Our own report looped back. Ignore it. */
1230 if (skb->pkt_type == PACKET_LOOPBACK)
1231 return 0;
1232
David Stevens24c69272005-12-02 20:32:59 -08001233 /* send our report if the MC router may not have heard this report */
1234 if (skb->pkt_type != PACKET_MULTICAST &&
1235 skb->pkt_type != PACKET_BROADCAST)
1236 return 0;
1237
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001238 if (!pskb_may_pull(skb, sizeof(*mld) - sizeof(struct icmp6hdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return -EINVAL;
1240
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001241 mld = (struct mld_msg *)icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 /* Drop reports with not link local source */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001244 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001245 if (addr_type != IPV6_ADDR_ANY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 !(addr_type&IPV6_ADDR_LINKLOCAL))
1247 return -EINVAL;
1248
Eric Dumazet96b52e62010-06-07 21:05:02 +00001249 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (idev == NULL)
1251 return -ENODEV;
1252
1253 /*
1254 * Cancel the timer for this group
1255 */
1256
1257 read_lock_bh(&idev->lock);
1258 for (ma = idev->mc_list; ma; ma=ma->next) {
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001259 if (ipv6_addr_equal(&ma->mca_addr, &mld->mld_mca)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 spin_lock(&ma->mca_lock);
1261 if (del_timer(&ma->mca_timer))
1262 atomic_dec(&ma->mca_refcnt);
1263 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1264 spin_unlock(&ma->mca_lock);
1265 break;
1266 }
1267 }
1268 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return 0;
1270}
1271
Eric Dumazeta50feda2012-05-18 18:57:34 +00001272static bool is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1273 int gdeleted, int sdeleted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
1275 switch (type) {
1276 case MLD2_MODE_IS_INCLUDE:
1277 case MLD2_MODE_IS_EXCLUDE:
1278 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001279 return false;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001280 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1281 if (pmc->mca_sfmode == MCAST_INCLUDE)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001282 return true;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001283 /* don't include if this source is excluded
1284 * in all filters
1285 */
1286 if (psf->sf_count[MCAST_INCLUDE])
David L Stevens7add2a42006-01-24 13:06:39 -08001287 return type == MLD2_MODE_IS_INCLUDE;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001288 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1289 psf->sf_count[MCAST_EXCLUDE];
1290 }
Eric Dumazeta50feda2012-05-18 18:57:34 +00001291 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 case MLD2_CHANGE_TO_INCLUDE:
1293 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001294 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return psf->sf_count[MCAST_INCLUDE] != 0;
1296 case MLD2_CHANGE_TO_EXCLUDE:
1297 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001298 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1300 psf->sf_count[MCAST_INCLUDE])
Eric Dumazeta50feda2012-05-18 18:57:34 +00001301 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1303 psf->sf_count[MCAST_EXCLUDE];
1304 case MLD2_ALLOW_NEW_SOURCES:
1305 if (gdeleted || !psf->sf_crcount)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001306 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1308 case MLD2_BLOCK_OLD_SOURCES:
1309 if (pmc->mca_sfmode == MCAST_INCLUDE)
1310 return gdeleted || (psf->sf_crcount && sdeleted);
1311 return psf->sf_crcount && !gdeleted && !sdeleted;
1312 }
Eric Dumazeta50feda2012-05-18 18:57:34 +00001313 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
1316static int
1317mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1318{
1319 struct ip6_sf_list *psf;
1320 int scount = 0;
1321
1322 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1323 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1324 continue;
1325 scount++;
1326 }
1327 return scount;
1328}
1329
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +00001330static void ip6_mc_hdr(struct sock *sk, struct sk_buff *skb,
1331 struct net_device *dev,
1332 const struct in6_addr *saddr,
1333 const struct in6_addr *daddr,
1334 int proto, int len)
1335{
1336 struct ipv6hdr *hdr;
1337
1338 skb->protocol = htons(ETH_P_IPV6);
1339 skb->dev = dev;
1340
1341 skb_reset_network_header(skb);
1342 skb_put(skb, sizeof(struct ipv6hdr));
1343 hdr = ipv6_hdr(skb);
1344
1345 ip6_flow_hdr(hdr, 0, 0);
1346
1347 hdr->payload_len = htons(len);
1348 hdr->nexthdr = proto;
1349 hdr->hop_limit = inet6_sk(sk)->hop_limit;
1350
1351 hdr->saddr = *saddr;
1352 hdr->daddr = *daddr;
1353}
1354
Amerigo Wang89657792013-06-29 21:30:49 +08001355static struct sk_buff *mld_newpack(struct inet6_dev *idev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Amerigo Wang89657792013-06-29 21:30:49 +08001357 struct net_device *dev = idev->dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001358 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001359 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 struct sk_buff *skb;
1361 struct mld2_report *pmr;
1362 struct in6_addr addr_buf;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001363 const struct in6_addr *saddr;
Herbert Xua7ae1992011-11-18 02:20:04 +00001364 int hlen = LL_RESERVED_SPACE(dev);
1365 int tlen = dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 int err;
1367 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1368 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1369 IPV6_TLV_PADN, 0 };
1370
1371 /* we assume size > sizeof(ra) here */
Herbert Xua7ae1992011-11-18 02:20:04 +00001372 size += hlen + tlen;
Eric Dumazet72e09ad2010-06-05 03:03:30 -07001373 /* limit our allocations to order-0 page */
1374 size = min_t(int, size, SKB_MAX_ORDER(0, 0));
1375 skb = sock_alloc_send_skb(sk, size, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001377 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return NULL;
1379
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +02001380 skb->priority = TC_PRIO_CONTROL;
Herbert Xua7ae1992011-11-18 02:20:04 +00001381 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Amerigo Wang89657792013-06-29 21:30:49 +08001383 if (__ipv6_get_lladdr(idev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001385 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 * when a valid link-local address is not available.
1387 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001388 saddr = &in6addr_any;
1389 } else
1390 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +00001392 ip6_mc_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1395
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001396 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -03001397 skb_put(skb, sizeof(*pmr));
1398 pmr = (struct mld2_report *)skb_transport_header(skb);
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001399 pmr->mld2r_type = ICMPV6_MLD2_REPORT;
1400 pmr->mld2r_resv1 = 0;
1401 pmr->mld2r_cksum = 0;
1402 pmr->mld2r_resv2 = 0;
1403 pmr->mld2r_ngrec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return skb;
1405}
1406
1407static void mld_sendpack(struct sk_buff *skb)
1408{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001409 struct ipv6hdr *pip6 = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001410 struct mld2_report *pmr =
1411 (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 int payload_len, mldlen;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001413 struct inet6_dev *idev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001414 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 int err;
David S. Miller4c9483b2011-03-12 16:22:43 -05001416 struct flowi6 fl6;
Eric Dumazetadf30902009-06-02 05:19:30 +00001417 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Eric Dumazet96b52e62010-06-07 21:05:02 +00001419 rcu_read_lock();
1420 idev = __in6_dev_get(skb->dev);
Neil Hormanedf391f2009-04-27 02:45:02 -07001421 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
1422
Simon Horman29a3cad2013-05-28 20:34:26 +00001423 payload_len = (skb_tail_pointer(skb) - skb_network_header(skb)) -
1424 sizeof(*pip6);
1425 mldlen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 pip6->payload_len = htons(payload_len);
1427
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001428 pmr->mld2r_cksum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1429 IPPROTO_ICMPV6,
1430 csum_partial(skb_transport_header(skb),
1431 mldlen, 0));
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001432
David S. Miller4c9483b2011-03-12 16:22:43 -05001433 icmpv6_flow_init(net->ipv6.igmp_sk, &fl6, ICMPV6_MLD2_REPORT,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001434 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1435 skb->dev->ifindex);
YOSHIFUJI Hideaki / 吉藤英明12fd84f2013-01-18 02:00:24 +00001436 dst = icmp6_dst_alloc(skb->dev, &fl6);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001437
David S. Miller452edd52011-03-02 13:27:41 -08001438 err = 0;
1439 if (IS_ERR(dst)) {
1440 err = PTR_ERR(dst);
1441 dst = NULL;
1442 }
Eric Dumazetadf30902009-06-02 05:19:30 +00001443 skb_dst_set(skb, dst);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001444 if (err)
1445 goto err_out;
1446
Neil Hormanedf391f2009-04-27 02:45:02 -07001447 payload_len = skb->len;
1448
Jan Engelhardtb2e0b382010-03-23 04:09:07 +01001449 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001450 dst_output);
1451out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (!err) {
Denis V. Lunev5a57d4c2008-10-08 10:34:14 -07001453 ICMP6MSGOUT_INC_STATS_BH(net, idev, ICMPV6_MLD2_REPORT);
Denis V. Luneve41b5362008-10-08 10:33:26 -07001454 ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
Neil Hormanedf391f2009-04-27 02:45:02 -07001455 IP6_UPD_PO_STATS_BH(net, idev, IPSTATS_MIB_OUTMCAST, payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 } else
Denis V. Lunev483a47d2008-10-08 11:09:27 -07001457 IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Eric Dumazet96b52e62010-06-07 21:05:02 +00001459 rcu_read_unlock();
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001460 return;
1461
1462err_out:
1463 kfree_skb(skb);
1464 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466
1467static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1468{
Yan Zhengfab10fe2005-10-05 12:08:13 -07001469 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
1472static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1473 int type, struct mld2_grec **ppgr)
1474{
1475 struct net_device *dev = pmc->idev->dev;
1476 struct mld2_report *pmr;
1477 struct mld2_grec *pgr;
1478
1479 if (!skb)
Amerigo Wang89657792013-06-29 21:30:49 +08001480 skb = mld_newpack(pmc->idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 if (!skb)
1482 return NULL;
1483 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1484 pgr->grec_type = type;
1485 pgr->grec_auxwords = 0;
1486 pgr->grec_nsrcs = 0;
1487 pgr->grec_mca = pmc->mca_addr; /* structure copy */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001488 pmr = (struct mld2_report *)skb_transport_header(skb);
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001489 pmr->mld2r_ngrec = htons(ntohs(pmr->mld2r_ngrec)+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 *ppgr = pgr;
1491 return skb;
1492}
1493
1494#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1495 skb_tailroom(skb)) : 0)
1496
1497static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1498 int type, int gdeleted, int sdeleted)
1499{
Amerigo Wang89657792013-06-29 21:30:49 +08001500 struct inet6_dev *idev = pmc->idev;
1501 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 struct mld2_report *pmr;
1503 struct mld2_grec *pgr = NULL;
1504 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001505 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 if (pmc->mca_flags & MAF_NOREPORT)
1508 return skb;
1509
1510 isquery = type == MLD2_MODE_IS_INCLUDE ||
1511 type == MLD2_MODE_IS_EXCLUDE;
1512 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1513 type == MLD2_CHANGE_TO_EXCLUDE;
1514
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001515 stotal = scount = 0;
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1518
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001519 if (!*psf_list)
1520 goto empty_source;
1521
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001522 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 /* EX and TO_EX get a fresh packet, if needed */
1525 if (truncate) {
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001526 if (pmr && pmr->mld2r_ngrec &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1528 if (skb)
1529 mld_sendpack(skb);
Amerigo Wang89657792013-06-29 21:30:49 +08001530 skb = mld_newpack(idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532 }
1533 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 psf_prev = NULL;
1535 for (psf=*psf_list; psf; psf=psf_next) {
1536 struct in6_addr *psrc;
1537
1538 psf_next = psf->sf_next;
1539
1540 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1541 psf_prev = psf;
1542 continue;
1543 }
1544
1545 /* clear marks on query responses */
1546 if (isquery)
1547 psf->sf_gsresp = 0;
1548
1549 if (AVAILABLE(skb) < sizeof(*psrc) +
1550 first*sizeof(struct mld2_grec)) {
1551 if (truncate && !first)
1552 break; /* truncate these */
1553 if (pgr)
1554 pgr->grec_nsrcs = htons(scount);
1555 if (skb)
1556 mld_sendpack(skb);
Amerigo Wang89657792013-06-29 21:30:49 +08001557 skb = mld_newpack(idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 first = 1;
1559 scount = 0;
1560 }
1561 if (first) {
1562 skb = add_grhead(skb, pmc, type, &pgr);
1563 first = 0;
1564 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -08001565 if (!skb)
1566 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1568 *psrc = psf->sf_addr;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001569 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1571 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1572 psf->sf_crcount--;
1573 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1574 if (psf_prev)
1575 psf_prev->sf_next = psf->sf_next;
1576 else
1577 *psf_list = psf->sf_next;
1578 kfree(psf);
1579 continue;
1580 }
1581 }
1582 psf_prev = psf;
1583 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001584
1585empty_source:
1586 if (!stotal) {
1587 if (type == MLD2_ALLOW_NEW_SOURCES ||
1588 type == MLD2_BLOCK_OLD_SOURCES)
1589 return skb;
1590 if (pmc->mca_crcount || isquery) {
1591 /* make sure we have room for group header */
1592 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1593 mld_sendpack(skb);
1594 skb = NULL; /* add_grhead will get a new one */
1595 }
1596 skb = add_grhead(skb, pmc, type, &pgr);
1597 }
1598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if (pgr)
1600 pgr->grec_nsrcs = htons(scount);
1601
1602 if (isquery)
1603 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1604 return skb;
1605}
1606
1607static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1608{
1609 struct sk_buff *skb = NULL;
1610 int type;
1611
Amerigo Wang89657792013-06-29 21:30:49 +08001612 read_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (!pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1615 if (pmc->mca_flags & MAF_NOREPORT)
1616 continue;
1617 spin_lock_bh(&pmc->mca_lock);
1618 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1619 type = MLD2_MODE_IS_EXCLUDE;
1620 else
1621 type = MLD2_MODE_IS_INCLUDE;
1622 skb = add_grec(skb, pmc, type, 0, 0);
1623 spin_unlock_bh(&pmc->mca_lock);
1624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 } else {
1626 spin_lock_bh(&pmc->mca_lock);
1627 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1628 type = MLD2_MODE_IS_EXCLUDE;
1629 else
1630 type = MLD2_MODE_IS_INCLUDE;
1631 skb = add_grec(skb, pmc, type, 0, 0);
1632 spin_unlock_bh(&pmc->mca_lock);
1633 }
Amerigo Wang89657792013-06-29 21:30:49 +08001634 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if (skb)
1636 mld_sendpack(skb);
1637}
1638
1639/*
1640 * remove zero-count source records from a source filter list
1641 */
1642static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1643{
1644 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1645
1646 psf_prev = NULL;
1647 for (psf=*ppsf; psf; psf = psf_next) {
1648 psf_next = psf->sf_next;
1649 if (psf->sf_crcount == 0) {
1650 if (psf_prev)
1651 psf_prev->sf_next = psf->sf_next;
1652 else
1653 *ppsf = psf->sf_next;
1654 kfree(psf);
1655 } else
1656 psf_prev = psf;
1657 }
1658}
1659
1660static void mld_send_cr(struct inet6_dev *idev)
1661{
1662 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1663 struct sk_buff *skb = NULL;
1664 int type, dtype;
1665
1666 read_lock_bh(&idev->lock);
Stephen Hemminger6457d262010-02-17 18:48:44 -08001667 spin_lock(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 /* deleted MCA's */
1670 pmc_prev = NULL;
1671 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1672 pmc_next = pmc->next;
1673 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1674 type = MLD2_BLOCK_OLD_SOURCES;
1675 dtype = MLD2_BLOCK_OLD_SOURCES;
1676 skb = add_grec(skb, pmc, type, 1, 0);
1677 skb = add_grec(skb, pmc, dtype, 1, 1);
1678 }
1679 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1681 type = MLD2_CHANGE_TO_INCLUDE;
1682 skb = add_grec(skb, pmc, type, 1, 0);
1683 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001684 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 if (pmc->mca_crcount == 0) {
1686 mld_clear_zeros(&pmc->mca_tomb);
1687 mld_clear_zeros(&pmc->mca_sources);
1688 }
1689 }
1690 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1691 !pmc->mca_sources) {
1692 if (pmc_prev)
1693 pmc_prev->next = pmc_next;
1694 else
1695 idev->mc_tomb = pmc_next;
1696 in6_dev_put(pmc->idev);
1697 kfree(pmc);
1698 } else
1699 pmc_prev = pmc;
1700 }
Stephen Hemminger6457d262010-02-17 18:48:44 -08001701 spin_unlock(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 /* change recs */
1704 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1705 spin_lock_bh(&pmc->mca_lock);
1706 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1707 type = MLD2_BLOCK_OLD_SOURCES;
1708 dtype = MLD2_ALLOW_NEW_SOURCES;
1709 } else {
1710 type = MLD2_ALLOW_NEW_SOURCES;
1711 dtype = MLD2_BLOCK_OLD_SOURCES;
1712 }
1713 skb = add_grec(skb, pmc, type, 0, 0);
1714 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1715
1716 /* filter mode changes */
1717 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1719 type = MLD2_CHANGE_TO_EXCLUDE;
1720 else
1721 type = MLD2_CHANGE_TO_INCLUDE;
1722 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001723 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
1725 spin_unlock_bh(&pmc->mca_lock);
1726 }
1727 read_unlock_bh(&idev->lock);
1728 if (!skb)
1729 return;
1730 (void) mld_sendpack(skb);
1731}
1732
1733static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1734{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001735 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001736 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 struct inet6_dev *idev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001738 struct sk_buff *skb;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001739 struct mld_msg *hdr;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001740 const struct in6_addr *snd_addr, *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 struct in6_addr addr_buf;
Herbert Xua7ae1992011-11-18 02:20:04 +00001742 int hlen = LL_RESERVED_SPACE(dev);
1743 int tlen = dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 int err, len, payload_len, full_len;
1745 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1746 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1747 IPV6_TLV_PADN, 0 };
David S. Miller4c9483b2011-03-12 16:22:43 -05001748 struct flowi6 fl6;
Eric Dumazetadf30902009-06-02 05:19:30 +00001749 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09001751 if (type == ICMPV6_MGM_REDUCTION)
1752 snd_addr = &in6addr_linklocal_allrouters;
1753 else
1754 snd_addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1757 payload_len = len + sizeof(ra);
1758 full_len = sizeof(struct ipv6hdr) + payload_len;
1759
Neil Hormanedf391f2009-04-27 02:45:02 -07001760 rcu_read_lock();
1761 IP6_UPD_PO_STATS(net, __in6_dev_get(dev),
1762 IPSTATS_MIB_OUT, full_len);
1763 rcu_read_unlock();
1764
Herbert Xua7ae1992011-11-18 02:20:04 +00001765 skb = sock_alloc_send_skb(sk, hlen + tlen + full_len, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 if (skb == NULL) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001768 rcu_read_lock();
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001769 IP6_INC_STATS(net, __in6_dev_get(dev),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001770 IPSTATS_MIB_OUTDISCARDS);
1771 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return;
1773 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +02001774 skb->priority = TC_PRIO_CONTROL;
Herbert Xua7ae1992011-11-18 02:20:04 +00001775 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Neil Horman95c385b2007-04-25 17:08:10 -07001777 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001779 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 * when a valid link-local address is not available.
1781 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001782 saddr = &in6addr_any;
1783 } else
1784 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +00001786 ip6_mc_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1789
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001790 hdr = (struct mld_msg *) skb_put(skb, sizeof(struct mld_msg));
1791 memset(hdr, 0, sizeof(struct mld_msg));
1792 hdr->mld_type = type;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001793 hdr->mld_mca = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001795 hdr->mld_cksum = csum_ipv6_magic(saddr, snd_addr, len,
1796 IPPROTO_ICMPV6,
1797 csum_partial(hdr, len, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Eric Dumazet96b52e62010-06-07 21:05:02 +00001799 rcu_read_lock();
1800 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
David S. Miller4c9483b2011-03-12 16:22:43 -05001802 icmpv6_flow_init(sk, &fl6, type,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001803 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1804 skb->dev->ifindex);
YOSHIFUJI Hideaki / 吉藤英明12fd84f2013-01-18 02:00:24 +00001805 dst = icmp6_dst_alloc(skb->dev, &fl6);
David S. Miller452edd52011-03-02 13:27:41 -08001806 if (IS_ERR(dst)) {
1807 err = PTR_ERR(dst);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001808 goto err_out;
David S. Miller452edd52011-03-02 13:27:41 -08001809 }
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001810
Eric Dumazetadf30902009-06-02 05:19:30 +00001811 skb_dst_set(skb, dst);
Jan Engelhardtb2e0b382010-03-23 04:09:07 +01001812 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001813 dst_output);
1814out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (!err) {
Denis V. Lunev5c5d2442008-10-08 10:33:50 -07001816 ICMP6MSGOUT_INC_STATS(net, idev, type);
Denis V. Luneva862f6a2008-10-08 10:33:06 -07001817 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
Neil Hormanedf391f2009-04-27 02:45:02 -07001818 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, full_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 } else
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001820 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Eric Dumazet96b52e62010-06-07 21:05:02 +00001822 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 return;
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001824
1825err_out:
1826 kfree_skb(skb);
1827 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02001830static void mld_resend_report(struct inet6_dev *idev)
1831{
1832 if (MLD_V1_SEEN(idev)) {
1833 struct ifmcaddr6 *mcaddr;
1834 read_lock_bh(&idev->lock);
1835 for (mcaddr = idev->mc_list; mcaddr; mcaddr = mcaddr->next) {
1836 if (!(mcaddr->mca_flags & MAF_NOREPORT))
1837 igmp6_send(&mcaddr->mca_addr, idev->dev,
1838 ICMPV6_MGM_REPORT);
1839 }
1840 read_unlock_bh(&idev->lock);
1841 } else {
1842 mld_send_report(idev, NULL);
1843 }
1844}
1845
1846void ipv6_mc_dad_complete(struct inet6_dev *idev)
1847{
1848 idev->mc_dad_count = idev->mc_qrv;
1849 if (idev->mc_dad_count) {
1850 mld_resend_report(idev);
1851 idev->mc_dad_count--;
1852 if (idev->mc_dad_count)
1853 mld_dad_start_timer(idev, idev->mc_maxdelay);
1854 }
1855}
1856
1857static void mld_dad_timer_expire(unsigned long data)
1858{
1859 struct inet6_dev *idev = (struct inet6_dev *)data;
1860
1861 mld_resend_report(idev);
1862 if (idev->mc_dad_count) {
1863 idev->mc_dad_count--;
1864 if (idev->mc_dad_count)
1865 mld_dad_start_timer(idev, idev->mc_maxdelay);
1866 }
1867 __in6_dev_put(idev);
1868}
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001871 const struct in6_addr *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
1873 struct ip6_sf_list *psf, *psf_prev;
1874 int rv = 0;
1875
1876 psf_prev = NULL;
1877 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1878 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1879 break;
1880 psf_prev = psf;
1881 }
1882 if (!psf || psf->sf_count[sfmode] == 0) {
1883 /* source filter not found, or count wrong => bug */
1884 return -ESRCH;
1885 }
1886 psf->sf_count[sfmode]--;
1887 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1888 struct inet6_dev *idev = pmc->idev;
1889
1890 /* no more filters for this source */
1891 if (psf_prev)
1892 psf_prev->sf_next = psf->sf_next;
1893 else
1894 pmc->mca_sources = psf->sf_next;
1895 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1896 !MLD_V1_SEEN(idev)) {
1897 psf->sf_crcount = idev->mc_qrv;
1898 psf->sf_next = pmc->mca_tomb;
1899 pmc->mca_tomb = psf;
1900 rv = 1;
1901 } else
1902 kfree(psf);
1903 }
1904 return rv;
1905}
1906
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001907static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
1908 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 int delta)
1910{
1911 struct ifmcaddr6 *pmc;
1912 int changerec = 0;
1913 int i, err;
1914
1915 if (!idev)
1916 return -ENODEV;
1917 read_lock_bh(&idev->lock);
1918 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1919 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1920 break;
1921 }
1922 if (!pmc) {
1923 /* MCA not found?? bug */
1924 read_unlock_bh(&idev->lock);
1925 return -ESRCH;
1926 }
1927 spin_lock_bh(&pmc->mca_lock);
1928 sf_markstate(pmc);
1929 if (!delta) {
1930 if (!pmc->mca_sfcount[sfmode]) {
1931 spin_unlock_bh(&pmc->mca_lock);
1932 read_unlock_bh(&idev->lock);
1933 return -EINVAL;
1934 }
1935 pmc->mca_sfcount[sfmode]--;
1936 }
1937 err = 0;
1938 for (i=0; i<sfcount; i++) {
1939 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1940
1941 changerec |= rv > 0;
1942 if (!err && rv < 0)
1943 err = rv;
1944 }
1945 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1946 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1947 pmc->mca_sfcount[MCAST_INCLUDE]) {
1948 struct ip6_sf_list *psf;
1949
1950 /* filter mode change */
1951 pmc->mca_sfmode = MCAST_INCLUDE;
1952 pmc->mca_crcount = idev->mc_qrv;
1953 idev->mc_ifc_count = pmc->mca_crcount;
1954 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1955 psf->sf_crcount = 0;
1956 mld_ifc_event(pmc->idev);
1957 } else if (sf_setstate(pmc) || changerec)
1958 mld_ifc_event(pmc->idev);
1959 spin_unlock_bh(&pmc->mca_lock);
1960 read_unlock_bh(&idev->lock);
1961 return err;
1962}
1963
1964/*
1965 * Add multicast single-source filter to the interface list
1966 */
1967static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
Jun Zhao99d2f472011-11-30 06:21:05 +00001968 const struct in6_addr *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
1970 struct ip6_sf_list *psf, *psf_prev;
1971
1972 psf_prev = NULL;
1973 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1974 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1975 break;
1976 psf_prev = psf;
1977 }
1978 if (!psf) {
Ingo Oeser0c600ed2006-03-20 23:01:32 -08001979 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (!psf)
1981 return -ENOBUFS;
Ingo Oeser0c600ed2006-03-20 23:01:32 -08001982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 psf->sf_addr = *psfsrc;
1984 if (psf_prev) {
1985 psf_prev->sf_next = psf;
1986 } else
1987 pmc->mca_sources = psf;
1988 }
1989 psf->sf_count[sfmode]++;
1990 return 0;
1991}
1992
1993static void sf_markstate(struct ifmcaddr6 *pmc)
1994{
1995 struct ip6_sf_list *psf;
1996 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1997
1998 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1999 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2000 psf->sf_oldin = mca_xcount ==
2001 psf->sf_count[MCAST_EXCLUDE] &&
2002 !psf->sf_count[MCAST_INCLUDE];
2003 } else
2004 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2005}
2006
2007static int sf_setstate(struct ifmcaddr6 *pmc)
2008{
David L Stevens7add2a42006-01-24 13:06:39 -08002009 struct ip6_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2011 int qrv = pmc->idev->mc_qrv;
2012 int new_in, rv;
2013
2014 rv = 0;
2015 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
2016 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2017 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2018 !psf->sf_count[MCAST_INCLUDE];
2019 } else
2020 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevens7add2a42006-01-24 13:06:39 -08002021 if (new_in) {
2022 if (!psf->sf_oldin) {
Al Viroe80e28b2006-02-03 20:10:03 -05002023 struct ip6_sf_list *prev = NULL;
David L Stevens7add2a42006-01-24 13:06:39 -08002024
2025 for (dpsf=pmc->mca_tomb; dpsf;
2026 dpsf=dpsf->sf_next) {
2027 if (ipv6_addr_equal(&dpsf->sf_addr,
2028 &psf->sf_addr))
2029 break;
2030 prev = dpsf;
2031 }
2032 if (dpsf) {
2033 if (prev)
2034 prev->sf_next = dpsf->sf_next;
2035 else
2036 pmc->mca_tomb = dpsf->sf_next;
2037 kfree(dpsf);
2038 }
2039 psf->sf_crcount = qrv;
2040 rv++;
2041 }
2042 } else if (psf->sf_oldin) {
2043 psf->sf_crcount = 0;
2044 /*
2045 * add or update "delete" records if an active filter
2046 * is now inactive
2047 */
2048 for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
2049 if (ipv6_addr_equal(&dpsf->sf_addr,
2050 &psf->sf_addr))
2051 break;
2052 if (!dpsf) {
Joe Perches5d553542010-05-31 17:23:22 +00002053 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevens7add2a42006-01-24 13:06:39 -08002054 if (!dpsf)
2055 continue;
2056 *dpsf = *psf;
2057 /* pmc->mca_lock held by callers */
2058 dpsf->sf_next = pmc->mca_tomb;
2059 pmc->mca_tomb = dpsf;
2060 }
2061 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 rv++;
2063 }
2064 }
2065 return rv;
2066}
2067
2068/*
2069 * Add multicast source filter list to the interface list
2070 */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002071static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2072 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 int delta)
2074{
2075 struct ifmcaddr6 *pmc;
2076 int isexclude;
2077 int i, err;
2078
2079 if (!idev)
2080 return -ENODEV;
2081 read_lock_bh(&idev->lock);
2082 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2083 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2084 break;
2085 }
2086 if (!pmc) {
2087 /* MCA not found?? bug */
2088 read_unlock_bh(&idev->lock);
2089 return -ESRCH;
2090 }
2091 spin_lock_bh(&pmc->mca_lock);
2092
2093 sf_markstate(pmc);
2094 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2095 if (!delta)
2096 pmc->mca_sfcount[sfmode]++;
2097 err = 0;
2098 for (i=0; i<sfcount; i++) {
Jun Zhao99d2f472011-11-30 06:21:05 +00002099 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (err)
2101 break;
2102 }
2103 if (err) {
2104 int j;
2105
2106 if (!delta)
2107 pmc->mca_sfcount[sfmode]--;
2108 for (j=0; j<i; j++)
RongQing.Li78d50212012-04-04 16:47:04 +00002109 ip6_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 struct ip6_sf_list *psf;
2112
2113 /* filter mode change */
2114 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2115 pmc->mca_sfmode = MCAST_EXCLUDE;
2116 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2117 pmc->mca_sfmode = MCAST_INCLUDE;
2118 /* else no filters; keep old mode for reports */
2119
2120 pmc->mca_crcount = idev->mc_qrv;
2121 idev->mc_ifc_count = pmc->mca_crcount;
2122 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2123 psf->sf_crcount = 0;
2124 mld_ifc_event(idev);
2125 } else if (sf_setstate(pmc))
2126 mld_ifc_event(idev);
2127 spin_unlock_bh(&pmc->mca_lock);
2128 read_unlock_bh(&idev->lock);
2129 return err;
2130}
2131
2132static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2133{
2134 struct ip6_sf_list *psf, *nextpsf;
2135
2136 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2137 nextpsf = psf->sf_next;
2138 kfree(psf);
2139 }
2140 pmc->mca_tomb = NULL;
2141 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2142 nextpsf = psf->sf_next;
2143 kfree(psf);
2144 }
2145 pmc->mca_sources = NULL;
2146 pmc->mca_sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002147 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2149}
2150
2151
2152static void igmp6_join_group(struct ifmcaddr6 *ma)
2153{
2154 unsigned long delay;
2155
2156 if (ma->mca_flags & MAF_NOREPORT)
2157 return;
2158
2159 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2160
2161 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2162
2163 spin_lock_bh(&ma->mca_lock);
2164 if (del_timer(&ma->mca_timer)) {
2165 atomic_dec(&ma->mca_refcnt);
2166 delay = ma->mca_timer.expires - jiffies;
2167 }
2168
2169 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2170 atomic_inc(&ma->mca_refcnt);
2171 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2172 spin_unlock_bh(&ma->mca_lock);
2173}
2174
2175static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2176 struct inet6_dev *idev)
2177{
2178 int err;
2179
David L Stevens5ab4a6c2005-12-27 14:03:00 -08002180 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2181 * so no other readers or writers of iml or its sflist
2182 */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002183 if (!iml->sflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 /* any-source empty exclude case */
2185 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2186 }
2187 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2188 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2189 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2190 iml->sflist = NULL;
2191 return err;
2192}
2193
2194static void igmp6_leave_group(struct ifmcaddr6 *ma)
2195{
2196 if (MLD_V1_SEEN(ma->idev)) {
2197 if (ma->mca_flags & MAF_LAST_REPORTER)
2198 igmp6_send(&ma->mca_addr, ma->idev->dev,
2199 ICMPV6_MGM_REDUCTION);
2200 } else {
2201 mld_add_delrec(ma->idev, ma);
2202 mld_ifc_event(ma->idev);
2203 }
2204}
2205
2206static void mld_gq_timer_expire(unsigned long data)
2207{
2208 struct inet6_dev *idev = (struct inet6_dev *)data;
2209
2210 idev->mc_gq_running = 0;
2211 mld_send_report(idev, NULL);
2212 __in6_dev_put(idev);
2213}
2214
2215static void mld_ifc_timer_expire(unsigned long data)
2216{
2217 struct inet6_dev *idev = (struct inet6_dev *)data;
2218
2219 mld_send_cr(idev);
2220 if (idev->mc_ifc_count) {
2221 idev->mc_ifc_count--;
2222 if (idev->mc_ifc_count)
2223 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2224 }
2225 __in6_dev_put(idev);
2226}
2227
2228static void mld_ifc_event(struct inet6_dev *idev)
2229{
2230 if (MLD_V1_SEEN(idev))
2231 return;
2232 idev->mc_ifc_count = idev->mc_qrv;
2233 mld_ifc_start_timer(idev, 1);
2234}
2235
2236
2237static void igmp6_timer_handler(unsigned long data)
2238{
2239 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2240
2241 if (MLD_V1_SEEN(ma->idev))
2242 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2243 else
2244 mld_send_report(ma->idev, ma);
2245
2246 spin_lock(&ma->mca_lock);
2247 ma->mca_flags |= MAF_LAST_REPORTER;
2248 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2249 spin_unlock(&ma->mca_lock);
2250 ma_put(ma);
2251}
2252
Moni Shoua75c78502009-09-15 02:37:40 -07002253/* Device changing type */
2254
2255void ipv6_mc_unmap(struct inet6_dev *idev)
2256{
2257 struct ifmcaddr6 *i;
2258
2259 /* Install multicast list, except for all-nodes (already installed) */
2260
2261 read_lock_bh(&idev->lock);
2262 for (i = idev->mc_list; i; i = i->next)
2263 igmp6_group_dropped(i);
2264 read_unlock_bh(&idev->lock);
2265}
2266
2267void ipv6_mc_remap(struct inet6_dev *idev)
2268{
2269 ipv6_mc_up(idev);
2270}
2271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272/* Device going down */
2273
2274void ipv6_mc_down(struct inet6_dev *idev)
2275{
2276 struct ifmcaddr6 *i;
2277
2278 /* Withdraw multicast list */
2279
2280 read_lock_bh(&idev->lock);
2281 idev->mc_ifc_count = 0;
2282 if (del_timer(&idev->mc_ifc_timer))
2283 __in6_dev_put(idev);
2284 idev->mc_gq_running = 0;
2285 if (del_timer(&idev->mc_gq_timer))
2286 __in6_dev_put(idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002287 if (del_timer(&idev->mc_dad_timer))
2288 __in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
2290 for (i = idev->mc_list; i; i=i->next)
2291 igmp6_group_dropped(i);
2292 read_unlock_bh(&idev->lock);
2293
2294 mld_clear_delrec(idev);
2295}
2296
2297
2298/* Device going up */
2299
2300void ipv6_mc_up(struct inet6_dev *idev)
2301{
2302 struct ifmcaddr6 *i;
2303
2304 /* Install multicast list, except for all-nodes (already installed) */
2305
2306 read_lock_bh(&idev->lock);
2307 for (i = idev->mc_list; i; i=i->next)
2308 igmp6_group_added(i);
2309 read_unlock_bh(&idev->lock);
2310}
2311
2312/* IPv6 device initialization. */
2313
2314void ipv6_mc_init_dev(struct inet6_dev *idev)
2315{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 write_lock_bh(&idev->lock);
Stephen Hemminger6457d262010-02-17 18:48:44 -08002317 spin_lock_init(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 idev->mc_gq_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002319 setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2320 (unsigned long)idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 idev->mc_tomb = NULL;
2322 idev->mc_ifc_count = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002323 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2324 (unsigned long)idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002325 setup_timer(&idev->mc_dad_timer, mld_dad_timer_expire,
2326 (unsigned long)idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 idev->mc_qrv = MLD_QRV_DEFAULT;
2328 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2329 idev->mc_v1_seen = 0;
2330 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331}
2332
2333/*
2334 * Device is about to be destroyed: clean up.
2335 */
2336
2337void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2338{
2339 struct ifmcaddr6 *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341 /* Deactivate timers */
2342 ipv6_mc_down(idev);
2343
2344 /* Delete all-nodes address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2346 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2347 * fail.
2348 */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002349 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002351 if (idev->cnf.forwarding)
2352 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
2354 write_lock_bh(&idev->lock);
2355 while ((i = idev->mc_list) != NULL) {
2356 idev->mc_list = i->next;
2357 write_unlock_bh(&idev->lock);
2358
2359 igmp6_group_dropped(i);
2360 ma_put(i);
2361
2362 write_lock_bh(&idev->lock);
2363 }
2364 write_unlock_bh(&idev->lock);
2365}
2366
2367#ifdef CONFIG_PROC_FS
2368struct igmp6_mc_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002369 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 struct net_device *dev;
2371 struct inet6_dev *idev;
2372};
2373
2374#define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2375
2376static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2377{
2378 struct ifmcaddr6 *im = NULL;
2379 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002380 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Pavel Emelianov7562f872007-05-03 15:13:45 -07002382 state->idev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002383 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 struct inet6_dev *idev;
Eric Dumazetce81b762009-11-11 17:34:30 +00002385 idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 if (!idev)
2387 continue;
2388 read_lock_bh(&idev->lock);
2389 im = idev->mc_list;
2390 if (im) {
2391 state->idev = idev;
2392 break;
2393 }
2394 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 }
2396 return im;
2397}
2398
2399static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2400{
2401 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2402
2403 im = im->next;
2404 while (!im) {
Eric Dumazetce81b762009-11-11 17:34:30 +00002405 if (likely(state->idev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 read_unlock_bh(&state->idev->lock);
Eric Dumazetce81b762009-11-11 17:34:30 +00002407
2408 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 if (!state->dev) {
2410 state->idev = NULL;
2411 break;
2412 }
Eric Dumazetce81b762009-11-11 17:34:30 +00002413 state->idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 if (!state->idev)
2415 continue;
2416 read_lock_bh(&state->idev->lock);
2417 im = state->idev->mc_list;
2418 }
2419 return im;
2420}
2421
2422static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2423{
2424 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2425 if (im)
2426 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2427 --pos;
2428 return pos ? NULL : im;
2429}
2430
2431static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetce81b762009-11-11 17:34:30 +00002432 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433{
Eric Dumazetce81b762009-11-11 17:34:30 +00002434 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return igmp6_mc_get_idx(seq, *pos);
2436}
2437
2438static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2439{
Eric Dumazetce81b762009-11-11 17:34:30 +00002440 struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);
2441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 ++*pos;
2443 return im;
2444}
2445
2446static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetce81b762009-11-11 17:34:30 +00002447 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
2449 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
Eric Dumazetce81b762009-11-11 17:34:30 +00002450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 if (likely(state->idev != NULL)) {
2452 read_unlock_bh(&state->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 state->idev = NULL;
2454 }
2455 state->dev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002456 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
2458
2459static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2460{
2461 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2462 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2463
2464 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002465 "%-4d %-15s %pi6 %5d %08X %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 state->dev->ifindex, state->dev->name,
Harvey Harrisonb0711952008-10-28 16:05:40 -07002467 &im->mca_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 im->mca_users, im->mca_flags,
2469 (im->mca_flags&MAF_TIMER_RUNNING) ?
2470 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2471 return 0;
2472}
2473
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002474static const struct seq_operations igmp6_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 .start = igmp6_mc_seq_start,
2476 .next = igmp6_mc_seq_next,
2477 .stop = igmp6_mc_seq_stop,
2478 .show = igmp6_mc_seq_show,
2479};
2480
2481static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2482{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002483 return seq_open_net(inode, file, &igmp6_mc_seq_ops,
2484 sizeof(struct igmp6_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485}
2486
Arjan van de Ven9a321442007-02-12 00:55:35 -08002487static const struct file_operations igmp6_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 .owner = THIS_MODULE,
2489 .open = igmp6_mc_seq_open,
2490 .read = seq_read,
2491 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002492 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493};
2494
2495struct igmp6_mcf_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002496 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 struct net_device *dev;
2498 struct inet6_dev *idev;
2499 struct ifmcaddr6 *im;
2500};
2501
2502#define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2503
2504static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2505{
2506 struct ip6_sf_list *psf = NULL;
2507 struct ifmcaddr6 *im = NULL;
2508 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002509 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Pavel Emelianov7562f872007-05-03 15:13:45 -07002511 state->idev = NULL;
2512 state->im = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002513 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 struct inet6_dev *idev;
Eric Dumazetce81b762009-11-11 17:34:30 +00002515 idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 if (unlikely(idev == NULL))
2517 continue;
2518 read_lock_bh(&idev->lock);
2519 im = idev->mc_list;
2520 if (likely(im != NULL)) {
2521 spin_lock_bh(&im->mca_lock);
2522 psf = im->mca_sources;
2523 if (likely(psf != NULL)) {
2524 state->im = im;
2525 state->idev = idev;
2526 break;
2527 }
2528 spin_unlock_bh(&im->mca_lock);
2529 }
2530 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 }
2532 return psf;
2533}
2534
2535static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2536{
2537 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2538
2539 psf = psf->sf_next;
2540 while (!psf) {
2541 spin_unlock_bh(&state->im->mca_lock);
2542 state->im = state->im->next;
2543 while (!state->im) {
Eric Dumazetce81b762009-11-11 17:34:30 +00002544 if (likely(state->idev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 read_unlock_bh(&state->idev->lock);
Eric Dumazetce81b762009-11-11 17:34:30 +00002546
2547 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 if (!state->dev) {
2549 state->idev = NULL;
2550 goto out;
2551 }
Eric Dumazetce81b762009-11-11 17:34:30 +00002552 state->idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 if (!state->idev)
2554 continue;
2555 read_lock_bh(&state->idev->lock);
2556 state->im = state->idev->mc_list;
2557 }
2558 if (!state->im)
2559 break;
2560 spin_lock_bh(&state->im->mca_lock);
2561 psf = state->im->mca_sources;
2562 }
2563out:
2564 return psf;
2565}
2566
2567static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2568{
2569 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2570 if (psf)
2571 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2572 --pos;
2573 return pos ? NULL : psf;
2574}
2575
2576static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetce81b762009-11-11 17:34:30 +00002577 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
Eric Dumazetce81b762009-11-11 17:34:30 +00002579 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2581}
2582
2583static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2584{
2585 struct ip6_sf_list *psf;
2586 if (v == SEQ_START_TOKEN)
2587 psf = igmp6_mcf_get_first(seq);
2588 else
2589 psf = igmp6_mcf_get_next(seq, v);
2590 ++*pos;
2591 return psf;
2592}
2593
2594static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetce81b762009-11-11 17:34:30 +00002595 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
2597 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2598 if (likely(state->im != NULL)) {
2599 spin_unlock_bh(&state->im->mca_lock);
2600 state->im = NULL;
2601 }
2602 if (likely(state->idev != NULL)) {
2603 read_unlock_bh(&state->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 state->idev = NULL;
2605 }
2606 state->dev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002607 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608}
2609
2610static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2611{
2612 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2613 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2614
2615 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002616 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 "%3s %6s "
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002618 "%32s %32s %6s %6s\n", "Idx",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 "Device", "Multicast Address",
2620 "Source Address", "INC", "EXC");
2621 } else {
2622 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002623 "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 state->dev->ifindex, state->dev->name,
Harvey Harrisonb0711952008-10-28 16:05:40 -07002625 &state->im->mca_addr,
2626 &psf->sf_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 psf->sf_count[MCAST_INCLUDE],
2628 psf->sf_count[MCAST_EXCLUDE]);
2629 }
2630 return 0;
2631}
2632
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002633static const struct seq_operations igmp6_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 .start = igmp6_mcf_seq_start,
2635 .next = igmp6_mcf_seq_next,
2636 .stop = igmp6_mcf_seq_stop,
2637 .show = igmp6_mcf_seq_show,
2638};
2639
2640static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2641{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002642 return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
2643 sizeof(struct igmp6_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644}
2645
Arjan van de Ven9a321442007-02-12 00:55:35 -08002646static const struct file_operations igmp6_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 .owner = THIS_MODULE,
2648 .open = igmp6_mcf_seq_open,
2649 .read = seq_read,
2650 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002651 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652};
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002653
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002654static int __net_init igmp6_proc_init(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002655{
2656 int err;
2657
2658 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002659 if (!proc_create("igmp6", S_IRUGO, net->proc_net, &igmp6_mc_seq_fops))
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002660 goto out;
Gao fengd4beaa62013-02-18 01:34:54 +00002661 if (!proc_create("mcfilter6", S_IRUGO, net->proc_net,
2662 &igmp6_mcf_seq_fops))
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002663 goto out_proc_net_igmp6;
2664
2665 err = 0;
2666out:
2667 return err;
2668
2669out_proc_net_igmp6:
Gao fengece31ff2013-02-18 01:34:56 +00002670 remove_proc_entry("igmp6", net->proc_net);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002671 goto out;
2672}
2673
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002674static void __net_exit igmp6_proc_exit(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002675{
Gao fengece31ff2013-02-18 01:34:56 +00002676 remove_proc_entry("mcfilter6", net->proc_net);
2677 remove_proc_entry("igmp6", net->proc_net);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002678}
2679#else
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002680static inline int igmp6_proc_init(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002681{
2682 return 0;
2683}
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002684static inline void igmp6_proc_exit(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002685{
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002686}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687#endif
2688
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002689static int __net_init igmp6_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 int err;
2692
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002693 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2694 SOCK_RAW, IPPROTO_ICMPV6, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00002696 pr_err("Failed to initialize the IGMP6 control socket (err %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 err);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002698 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 }
2700
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002701 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002703 err = igmp6_proc_init(net);
2704 if (err)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002705 goto out_sock_create;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002706out:
2707 return err;
2708
2709out_sock_create:
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002710 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002711 goto out;
2712}
2713
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002714static void __net_exit igmp6_net_exit(struct net *net)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002715{
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002716 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002717 igmp6_proc_exit(net);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002718}
2719
2720static struct pernet_operations igmp6_net_ops = {
2721 .init = igmp6_net_init,
2722 .exit = igmp6_net_exit,
2723};
2724
2725int __init igmp6_init(void)
2726{
2727 return register_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728}
2729
2730void igmp6_cleanup(void)
2731{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002732 unregister_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733}