blob: 484a94215d8897f80551b760e6045b606dd73076 [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
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +090067/* Ensure that we have struct in6_addr aligned on 32bit word. */
68static void *__mld2_query_bugs[] __attribute__((__unused__)) = {
69 BUILD_BUG_ON_NULL(offsetof(struct mld2_query, mld2q_srcs) % 4),
70 BUILD_BUG_ON_NULL(offsetof(struct mld2_report, mld2r_grec) % 4),
71 BUILD_BUG_ON_NULL(offsetof(struct mld2_grec, grec_mca) % 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
74static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
75
76/* Big mc list lock for all the sockets */
Eric Dumazet456b61b2010-11-23 13:12:15 +000077static DEFINE_SPINLOCK(ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static void igmp6_join_group(struct ifmcaddr6 *ma);
80static void igmp6_leave_group(struct ifmcaddr6 *ma);
81static void igmp6_timer_handler(unsigned long data);
82
83static void mld_gq_timer_expire(unsigned long data);
84static void mld_ifc_timer_expire(unsigned long data);
85static void mld_ifc_event(struct inet6_dev *idev);
86static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
Eric Dumazetb71d1d42011-04-22 04:53:02 +000087static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static void mld_clear_delrec(struct inet6_dev *idev);
Daniel Borkmann6c567b72013-09-04 00:19:38 +020089static bool mld_in_v1_mode(const struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int sf_setstate(struct ifmcaddr6 *pmc);
91static void sf_markstate(struct ifmcaddr6 *pmc);
92static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
Eric Dumazetb71d1d42011-04-22 04:53:02 +000093static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
94 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 int delta);
Eric Dumazetb71d1d42011-04-22 04:53:02 +000096static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
97 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 int delta);
99static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
100 struct inet6_dev *idev);
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#define MLD_QRV_DEFAULT 2
Daniel Borkmann89225d12013-09-04 00:19:37 +0200103/* RFC3810, 9.2. Query Interval */
104#define MLD_QI_DEFAULT (125 * HZ)
105/* RFC3810, 9.3. Query Response Interval */
106#define MLD_QRI_DEFAULT (10 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Daniel Borkmann9fd07842013-08-20 12:22:02 +0200108/* RFC3810, 8.1 Query Version Distinctions */
109#define MLD_V1_QUERY_LEN 24
110#define MLD_V2_QUERY_LEN_MIN 28
111
David L Stevens6f4353d2005-12-26 17:03:46 -0800112#define IPV6_MLD_MAX_MSF 64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Brian Haleyab32ea52006-09-22 14:15:41 -0700114int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +0200115int sysctl_mld_qrv __read_mostly = MLD_QRV_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117/*
118 * socket join on multicast group
119 */
120
Eric Dumazet456b61b2010-11-23 13:12:15 +0000121#define for_each_pmc_rcu(np, pmc) \
122 for (pmc = rcu_dereference(np->ipv6_mc_list); \
123 pmc != NULL; \
124 pmc = rcu_dereference(pmc->next))
125
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200126static int unsolicited_report_interval(struct inet6_dev *idev)
127{
128 int iv;
129
Daniel Borkmann6c567b72013-09-04 00:19:38 +0200130 if (mld_in_v1_mode(idev))
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200131 iv = idev->cnf.mldv1_unsolicited_report_interval;
132 else
133 iv = idev->cnf.mldv2_unsolicited_report_interval;
134
135 return iv > 0 ? iv : 1;
136}
137
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900138int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 struct net_device *dev = NULL;
141 struct ipv6_mc_socklist *mc_lst;
142 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900143 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int err;
145
146 if (!ipv6_addr_is_multicast(addr))
147 return -EINVAL;
148
Eric Dumazet456b61b2010-11-23 13:12:15 +0000149 rcu_read_lock();
150 for_each_pmc_rcu(np, mc_lst) {
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700151 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
152 ipv6_addr_equal(&mc_lst->addr, addr)) {
Eric Dumazet456b61b2010-11-23 13:12:15 +0000153 rcu_read_unlock();
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700154 return -EADDRINUSE;
155 }
156 }
Eric Dumazet456b61b2010-11-23 13:12:15 +0000157 rcu_read_unlock();
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
160
161 if (mc_lst == NULL)
162 return -ENOMEM;
163
164 mc_lst->next = NULL;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000165 mc_lst->addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200167 rtnl_lock();
Eric Dumazet96b52e62010-06-07 21:05:02 +0000168 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (ifindex == 0) {
170 struct rt6_info *rt;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800171 rt = rt6_lookup(net, addr, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (rt) {
David S. Millerd1918542011-12-28 20:19:20 -0500173 dev = rt->dst.dev;
Amerigo Wang94e187c2012-10-29 00:13:19 +0000174 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176 } else
Eric Dumazet96b52e62010-06-07 21:05:02 +0000177 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 if (dev == NULL) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000180 rcu_read_unlock();
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200181 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
183 return -ENODEV;
184 }
185
186 mc_lst->ifindex = dev->ifindex;
187 mc_lst->sfmode = MCAST_EXCLUDE;
YOSHIFUJI Hideaki196433c2006-01-04 13:56:31 -0800188 rwlock_init(&mc_lst->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 mc_lst->sflist = NULL;
190
191 /*
192 * now add/increase the group membership on the device
193 */
194
195 err = ipv6_dev_mc_inc(dev, addr);
196
197 if (err) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000198 rcu_read_unlock();
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200199 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return err;
202 }
203
Eric Dumazet456b61b2010-11-23 13:12:15 +0000204 spin_lock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 mc_lst->next = np->ipv6_mc_list;
Eric Dumazet456b61b2010-11-23 13:12:15 +0000206 rcu_assign_pointer(np->ipv6_mc_list, mc_lst);
207 spin_unlock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Eric Dumazet96b52e62010-06-07 21:05:02 +0000209 rcu_read_unlock();
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200210 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 return 0;
213}
214
215/*
216 * socket leave on multicast group
217 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900218int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 struct ipv6_pinfo *np = inet6_sk(sk);
Eric Dumazet456b61b2010-11-23 13:12:15 +0000221 struct ipv6_mc_socklist *mc_lst;
222 struct ipv6_mc_socklist __rcu **lnk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900223 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Li Weia858d642012-07-17 15:28:59 +0800225 if (!ipv6_addr_is_multicast(addr))
226 return -EINVAL;
227
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200228 rtnl_lock();
Eric Dumazet456b61b2010-11-23 13:12:15 +0000229 spin_lock(&ipv6_sk_mc_lock);
230 for (lnk = &np->ipv6_mc_list;
231 (mc_lst = rcu_dereference_protected(*lnk,
Ian Morris67ba4152014-08-24 21:53:10 +0100232 lockdep_is_held(&ipv6_sk_mc_lock))) != NULL;
Eric Dumazet456b61b2010-11-23 13:12:15 +0000233 lnk = &mc_lst->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
235 ipv6_addr_equal(&mc_lst->addr, addr)) {
236 struct net_device *dev;
237
238 *lnk = mc_lst->next;
Eric Dumazet456b61b2010-11-23 13:12:15 +0000239 spin_unlock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Eric Dumazet96b52e62010-06-07 21:05:02 +0000241 rcu_read_lock();
242 dev = dev_get_by_index_rcu(net, mc_lst->ifindex);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800243 if (dev != NULL) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000244 struct inet6_dev *idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
David L Stevensacd6e002006-08-17 16:27:39 -0700246 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000247 if (idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
David L Stevensacd6e002006-08-17 16:27:39 -0700249 } else
250 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000251 rcu_read_unlock();
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200252 rtnl_unlock();
253
Eric Dumazet456b61b2010-11-23 13:12:15 +0000254 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
Lai Jiangshane3cbf282011-03-18 12:00:50 +0800255 kfree_rcu(mc_lst, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return 0;
257 }
258 }
Eric Dumazet456b61b2010-11-23 13:12:15 +0000259 spin_unlock(&ipv6_sk_mc_lock);
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200260 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
David L Stevens9951f032005-07-08 17:47:28 -0700262 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Eric Dumazet96b52e62010-06-07 21:05:02 +0000265/* called with rcu_read_lock() */
266static struct inet6_dev *ip6_mc_find_dev_rcu(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000267 const struct in6_addr *group,
Eric Dumazet96b52e62010-06-07 21:05:02 +0000268 int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 struct net_device *dev = NULL;
271 struct inet6_dev *idev = NULL;
272
273 if (ifindex == 0) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000274 struct rt6_info *rt = rt6_lookup(net, group, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (rt) {
David S. Millerd1918542011-12-28 20:19:20 -0500277 dev = rt->dst.dev;
Amerigo Wang94e187c2012-10-29 00:13:19 +0000278 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280 } else
Eric Dumazet96b52e62010-06-07 21:05:02 +0000281 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 if (!dev)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000284 return NULL;
285 idev = __in6_dev_get(dev);
Ilpo Järvinen448eb712008-12-14 23:15:21 -0800286 if (!idev)
Joe Perches8a22c992010-11-14 17:05:00 +0000287 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 read_lock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000289 if (idev->dead) {
290 read_unlock_bh(&idev->lock);
291 return NULL;
292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return idev;
294}
295
296void ipv6_sock_mc_close(struct sock *sk)
297{
298 struct ipv6_pinfo *np = inet6_sk(sk);
299 struct ipv6_mc_socklist *mc_lst;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900300 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Eric Dumazet0e1efe92012-12-05 09:18:10 +0000302 if (!rcu_access_pointer(np->ipv6_mc_list))
303 return;
304
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200305 rtnl_lock();
Eric Dumazet456b61b2010-11-23 13:12:15 +0000306 spin_lock(&ipv6_sk_mc_lock);
307 while ((mc_lst = rcu_dereference_protected(np->ipv6_mc_list,
308 lockdep_is_held(&ipv6_sk_mc_lock))) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 struct net_device *dev;
310
311 np->ipv6_mc_list = mc_lst->next;
Eric Dumazet456b61b2010-11-23 13:12:15 +0000312 spin_unlock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Eric Dumazet96b52e62010-06-07 21:05:02 +0000314 rcu_read_lock();
315 dev = dev_get_by_index_rcu(net, mc_lst->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (dev) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000317 struct inet6_dev *idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
David L Stevensacd6e002006-08-17 16:27:39 -0700319 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000320 if (idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
David L Stevensacd6e002006-08-17 16:27:39 -0700322 } else
323 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000324 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Eric Dumazet456b61b2010-11-23 13:12:15 +0000326 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
Lai Jiangshane3cbf282011-03-18 12:00:50 +0800327 kfree_rcu(mc_lst, rcu);
Eric Dumazet456b61b2010-11-23 13:12:15 +0000328
329 spin_lock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Eric Dumazet456b61b2010-11-23 13:12:15 +0000331 spin_unlock(&ipv6_sk_mc_lock);
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200332 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
335int ip6_mc_source(int add, int omode, struct sock *sk,
336 struct group_source_req *pgsr)
337{
338 struct in6_addr *source, *group;
339 struct ipv6_mc_socklist *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct inet6_dev *idev;
341 struct ipv6_pinfo *inet6 = inet6_sk(sk);
342 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900343 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 int i, j, rv;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700345 int leavegroup = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800346 int pmclocked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 int err;
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
350 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
351
352 if (!ipv6_addr_is_multicast(group))
353 return -EINVAL;
354
Eric Dumazet96b52e62010-06-07 21:05:02 +0000355 rcu_read_lock();
356 idev = ip6_mc_find_dev_rcu(net, group, pgsr->gsr_interface);
357 if (!idev) {
358 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return -ENODEV;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 err = -EADDRNOTAVAIL;
363
Eric Dumazet456b61b2010-11-23 13:12:15 +0000364 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
366 continue;
367 if (ipv6_addr_equal(&pmc->addr, group))
368 break;
369 }
David L Stevens917f2f12005-07-08 17:45:16 -0700370 if (!pmc) { /* must have a prior join */
371 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /* if a source filter was set, must be the same mode as before */
375 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -0700376 if (pmc->sfmode != omode) {
377 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 } else if (pmc->sfmode != omode) {
381 /* allow mode switches for empty-set filters */
382 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
383 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
384 pmc->sfmode = omode;
385 }
386
Eric Dumazet96b52e62010-06-07 21:05:02 +0000387 write_lock(&pmc->sflock);
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800388 pmclocked = 1;
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 psl = pmc->sflist;
391 if (!add) {
392 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -0700393 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 rv = !0;
Ian Morris67ba4152014-08-24 21:53:10 +0100395 for (i = 0; i < psl->sl_count; i++) {
YOSHIFUJI Hideaki / 吉藤英明07c2fec2013-01-29 12:48:23 +0000396 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (rv == 0)
398 break;
399 }
400 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -0700401 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700403 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
404 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
405 leavegroup = 1;
406 goto done;
407 }
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /* update the interface filter */
410 ip6_mc_del_src(idev, group, omode, 1, source, 1);
411
Ian Morris67ba4152014-08-24 21:53:10 +0100412 for (j = i+1; j < psl->sl_count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 psl->sl_addr[j-1] = psl->sl_addr[j];
414 psl->sl_count--;
415 err = 0;
416 goto done;
417 }
418 /* else, add a new source to the filter */
419
420 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
421 err = -ENOBUFS;
422 goto done;
423 }
424 if (!psl || psl->sl_count == psl->sl_max) {
425 struct ip6_sf_socklist *newpsl;
426 int count = IP6_SFBLOCK;
427
428 if (psl)
429 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800430 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (!newpsl) {
432 err = -ENOBUFS;
433 goto done;
434 }
435 newpsl->sl_max = count;
436 newpsl->sl_count = count - IP6_SFBLOCK;
437 if (psl) {
Ian Morris67ba4152014-08-24 21:53:10 +0100438 for (i = 0; i < psl->sl_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 newpsl->sl_addr[i] = psl->sl_addr[i];
440 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
441 }
442 pmc->sflist = psl = newpsl;
443 }
444 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
Ian Morris67ba4152014-08-24 21:53:10 +0100445 for (i = 0; i < psl->sl_count; i++) {
YOSHIFUJI Hideaki / 吉藤英明07c2fec2013-01-29 12:48:23 +0000446 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
Jean Sacren56db1c52013-02-03 21:34:10 +0000447 if (rv == 0) /* There is an error in the address. */
448 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
Ian Morris67ba4152014-08-24 21:53:10 +0100450 for (j = psl->sl_count-1; j >= i; j--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 psl->sl_addr[j+1] = psl->sl_addr[j];
452 psl->sl_addr[i] = *source;
453 psl->sl_count++;
454 err = 0;
455 /* update the interface list */
456 ip6_mc_add_src(idev, group, omode, 1, source, 1);
457done:
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800458 if (pmclocked)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000459 write_unlock(&pmc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000461 rcu_read_unlock();
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700462 if (leavegroup)
463 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return err;
465}
466
467int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
468{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000469 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 struct ipv6_mc_socklist *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 struct inet6_dev *idev;
472 struct ipv6_pinfo *inet6 = inet6_sk(sk);
473 struct ip6_sf_socklist *newpsl, *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900474 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -0700475 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 int i, err;
477
478 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
479
480 if (!ipv6_addr_is_multicast(group))
481 return -EINVAL;
482 if (gsf->gf_fmode != MCAST_INCLUDE &&
483 gsf->gf_fmode != MCAST_EXCLUDE)
484 return -EINVAL;
485
Eric Dumazet96b52e62010-06-07 21:05:02 +0000486 rcu_read_lock();
487 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Eric Dumazet96b52e62010-06-07 21:05:02 +0000489 if (!idev) {
490 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return -ENODEV;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
David S. Miller9c059892005-07-08 21:44:39 -0700494 err = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800495
David L Stevens9951f032005-07-08 17:47:28 -0700496 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
497 leavegroup = 1;
498 goto done;
499 }
500
Eric Dumazet456b61b2010-11-23 13:12:15 +0000501 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (pmc->ifindex != gsf->gf_interface)
503 continue;
504 if (ipv6_addr_equal(&pmc->addr, group))
505 break;
506 }
David L Stevens917f2f12005-07-08 17:45:16 -0700507 if (!pmc) { /* must have a prior join */
508 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (gsf->gf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800512 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
513 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (!newpsl) {
515 err = -ENOBUFS;
516 goto done;
517 }
518 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
Ian Morris67ba4152014-08-24 21:53:10 +0100519 for (i = 0; i < newpsl->sl_count; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 struct sockaddr_in6 *psin6;
521
522 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
523 newpsl->sl_addr[i] = psin6->sin6_addr;
524 }
525 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
526 newpsl->sl_count, newpsl->sl_addr, 0);
527 if (err) {
528 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
529 goto done;
530 }
Yan Zheng8713dbf2005-10-28 08:02:08 +0800531 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +0800533 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
534 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800535
Eric Dumazet96b52e62010-06-07 21:05:02 +0000536 write_lock(&pmc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 psl = pmc->sflist;
538 if (psl) {
539 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
540 psl->sl_count, psl->sl_addr, 0);
541 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
542 } else
543 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
544 pmc->sflist = newpsl;
545 pmc->sfmode = gsf->gf_fmode;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000546 write_unlock(&pmc->sflock);
David L Stevens917f2f12005-07-08 17:45:16 -0700547 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548done:
549 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000550 rcu_read_unlock();
David L Stevens9951f032005-07-08 17:47:28 -0700551 if (leavegroup)
552 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return err;
554}
555
556int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
557 struct group_filter __user *optval, int __user *optlen)
558{
559 int err, i, count, copycount;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000560 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 struct ipv6_mc_socklist *pmc;
562 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 struct ipv6_pinfo *inet6 = inet6_sk(sk);
564 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900565 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
568
569 if (!ipv6_addr_is_multicast(group))
570 return -EINVAL;
571
Eric Dumazet96b52e62010-06-07 21:05:02 +0000572 rcu_read_lock();
573 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Eric Dumazet96b52e62010-06-07 21:05:02 +0000575 if (!idev) {
576 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return -ENODEV;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 err = -EADDRNOTAVAIL;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800581 /*
582 * changes to the ipv6_mc_list require the socket lock and
583 * a read lock on ip6_sk_mc_lock. We have the socket lock,
584 * so reading the list is safe.
585 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Eric Dumazet456b61b2010-11-23 13:12:15 +0000587 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (pmc->ifindex != gsf->gf_interface)
589 continue;
590 if (ipv6_addr_equal(group, &pmc->addr))
591 break;
592 }
593 if (!pmc) /* must have a prior join */
594 goto done;
595 gsf->gf_fmode = pmc->sfmode;
596 psl = pmc->sflist;
597 count = psl ? psl->sl_count : 0;
598 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000599 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
602 gsf->gf_numsrc = count;
603 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
604 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
605 return -EFAULT;
606 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800607 /* changes to psl require the socket lock, a read lock on
608 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
609 * have the socket lock, so reading here is safe.
610 */
Ian Morris67ba4152014-08-24 21:53:10 +0100611 for (i = 0; i < copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 struct sockaddr_in6 *psin6;
613 struct sockaddr_storage ss;
614
615 psin6 = (struct sockaddr_in6 *)&ss;
616 memset(&ss, 0, sizeof(ss));
617 psin6->sin6_family = AF_INET6;
618 psin6->sin6_addr = psl->sl_addr[i];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900619 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return -EFAULT;
621 }
622 return 0;
623done:
624 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000625 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return err;
627}
628
Eric Dumazeta50feda2012-05-18 18:57:34 +0000629bool inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
630 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 struct ipv6_pinfo *np = inet6_sk(sk);
633 struct ipv6_mc_socklist *mc;
634 struct ip6_sf_socklist *psl;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000635 bool rv = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Eric Dumazet456b61b2010-11-23 13:12:15 +0000637 rcu_read_lock();
638 for_each_pmc_rcu(np, mc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (ipv6_addr_equal(&mc->addr, mc_addr))
640 break;
641 }
642 if (!mc) {
Eric Dumazet456b61b2010-11-23 13:12:15 +0000643 rcu_read_unlock();
Eric Dumazeta50feda2012-05-18 18:57:34 +0000644 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800646 read_lock(&mc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 psl = mc->sflist;
648 if (!psl) {
649 rv = mc->sfmode == MCAST_EXCLUDE;
650 } else {
651 int i;
652
Ian Morris67ba4152014-08-24 21:53:10 +0100653 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
655 break;
656 }
657 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000658 rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000660 rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800662 read_unlock(&mc->sflock);
Eric Dumazet456b61b2010-11-23 13:12:15 +0000663 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 return rv;
666}
667
668static void ma_put(struct ifmcaddr6 *mc)
669{
670 if (atomic_dec_and_test(&mc->mca_refcnt)) {
671 in6_dev_put(mc->idev);
672 kfree(mc);
673 }
674}
675
676static void igmp6_group_added(struct ifmcaddr6 *mc)
677{
678 struct net_device *dev = mc->idev->dev;
679 char buf[MAX_ADDR_LEN];
680
YOSHIFUJI Hideaki / 吉藤英明ec16ef22013-02-09 04:29:58 +0000681 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
682 IPV6_ADDR_SCOPE_LINKLOCAL)
683 return;
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 spin_lock_bh(&mc->mca_lock);
686 if (!(mc->mca_flags&MAF_LOADED)) {
687 mc->mca_flags |= MAF_LOADED;
688 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000689 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691 spin_unlock_bh(&mc->mca_lock);
692
693 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
694 return;
695
Daniel Borkmann6c567b72013-09-04 00:19:38 +0200696 if (mld_in_v1_mode(mc->idev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 igmp6_join_group(mc);
698 return;
699 }
700 /* else v2 */
701
702 mc->mca_crcount = mc->idev->mc_qrv;
703 mld_ifc_event(mc->idev);
704}
705
706static void igmp6_group_dropped(struct ifmcaddr6 *mc)
707{
708 struct net_device *dev = mc->idev->dev;
709 char buf[MAX_ADDR_LEN];
710
YOSHIFUJI Hideaki / 吉藤英明ec16ef22013-02-09 04:29:58 +0000711 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
712 IPV6_ADDR_SCOPE_LINKLOCAL)
713 return;
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 spin_lock_bh(&mc->mca_lock);
716 if (mc->mca_flags&MAF_LOADED) {
717 mc->mca_flags &= ~MAF_LOADED;
718 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000719 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721
722 if (mc->mca_flags & MAF_NOREPORT)
723 goto done;
724 spin_unlock_bh(&mc->mca_lock);
725
726 if (!mc->idev->dead)
727 igmp6_leave_group(mc);
728
729 spin_lock_bh(&mc->mca_lock);
730 if (del_timer(&mc->mca_timer))
731 atomic_dec(&mc->mca_refcnt);
732done:
733 ip6_mc_clear_src(mc);
734 spin_unlock_bh(&mc->mca_lock);
735}
736
737/*
738 * deleted ifmcaddr6 manipulation
739 */
740static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
741{
742 struct ifmcaddr6 *pmc;
743
744 /* this is an "ifmcaddr6" for convenience; only the fields below
745 * are actually used. In particular, the refcnt and users are not
746 * used for management of the delete list. Using the same structure
747 * for deleted items allows change reports to use common code with
748 * non-deleted or query-response MCA's.
749 */
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800750 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (!pmc)
752 return;
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 spin_lock_bh(&im->mca_lock);
755 spin_lock_init(&pmc->mca_lock);
756 pmc->idev = im->idev;
757 in6_dev_hold(idev);
758 pmc->mca_addr = im->mca_addr;
759 pmc->mca_crcount = idev->mc_qrv;
760 pmc->mca_sfmode = im->mca_sfmode;
761 if (pmc->mca_sfmode == MCAST_INCLUDE) {
762 struct ip6_sf_list *psf;
763
764 pmc->mca_tomb = im->mca_tomb;
765 pmc->mca_sources = im->mca_sources;
766 im->mca_tomb = im->mca_sources = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +0100767 for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 psf->sf_crcount = pmc->mca_crcount;
769 }
770 spin_unlock_bh(&im->mca_lock);
771
Stephen Hemminger6457d262010-02-17 18:48:44 -0800772 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 pmc->next = idev->mc_tomb;
774 idev->mc_tomb = pmc;
Stephen Hemminger6457d262010-02-17 18:48:44 -0800775 spin_unlock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000778static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *pmca)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 struct ifmcaddr6 *pmc, *pmc_prev;
781 struct ip6_sf_list *psf, *psf_next;
782
Stephen Hemminger6457d262010-02-17 18:48:44 -0800783 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 pmc_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +0100785 for (pmc = idev->mc_tomb; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
787 break;
788 pmc_prev = pmc;
789 }
790 if (pmc) {
791 if (pmc_prev)
792 pmc_prev->next = pmc->next;
793 else
794 idev->mc_tomb = pmc->next;
795 }
Stephen Hemminger6457d262010-02-17 18:48:44 -0800796 spin_unlock_bh(&idev->mc_lock);
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (pmc) {
Ian Morris67ba4152014-08-24 21:53:10 +0100799 for (psf = pmc->mca_tomb; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 psf_next = psf->sf_next;
801 kfree(psf);
802 }
803 in6_dev_put(pmc->idev);
804 kfree(pmc);
805 }
806}
807
808static void mld_clear_delrec(struct inet6_dev *idev)
809{
810 struct ifmcaddr6 *pmc, *nextpmc;
811
Stephen Hemminger6457d262010-02-17 18:48:44 -0800812 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 pmc = idev->mc_tomb;
814 idev->mc_tomb = NULL;
Stephen Hemminger6457d262010-02-17 18:48:44 -0800815 spin_unlock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 for (; pmc; pmc = nextpmc) {
818 nextpmc = pmc->next;
819 ip6_mc_clear_src(pmc);
820 in6_dev_put(pmc->idev);
821 kfree(pmc);
822 }
823
824 /* clear dead sources, too */
825 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100826 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 struct ip6_sf_list *psf, *psf_next;
828
829 spin_lock_bh(&pmc->mca_lock);
830 psf = pmc->mca_tomb;
831 pmc->mca_tomb = NULL;
832 spin_unlock_bh(&pmc->mca_lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100833 for (; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 psf_next = psf->sf_next;
835 kfree(psf);
836 }
837 }
838 read_unlock_bh(&idev->lock);
839}
840
841
842/*
843 * device multicast group inc (add if not found)
844 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900845int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
847 struct ifmcaddr6 *mc;
848 struct inet6_dev *idev;
849
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200850 ASSERT_RTNL();
851
Eric Dumazet96b52e62010-06-07 21:05:02 +0000852 /* we need to take a reference on idev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 idev = in6_dev_get(dev);
854
855 if (idev == NULL)
856 return -EINVAL;
857
858 write_lock_bh(&idev->lock);
859 if (idev->dead) {
860 write_unlock_bh(&idev->lock);
861 in6_dev_put(idev);
862 return -ENODEV;
863 }
864
865 for (mc = idev->mc_list; mc; mc = mc->next) {
866 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
867 mc->mca_users++;
868 write_unlock_bh(&idev->lock);
869 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
870 NULL, 0);
871 in6_dev_put(idev);
872 return 0;
873 }
874 }
875
876 /*
877 * not found: create a new one.
878 */
879
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800880 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 if (mc == NULL) {
883 write_unlock_bh(&idev->lock);
884 in6_dev_put(idev);
885 return -ENOMEM;
886 }
887
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800888 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000890 mc->mca_addr = *addr;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000891 mc->idev = idev; /* (reference taken) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 mc->mca_users = 1;
893 /* mca_stamp should be updated upon changes */
894 mc->mca_cstamp = mc->mca_tstamp = jiffies;
895 atomic_set(&mc->mca_refcnt, 2);
896 spin_lock_init(&mc->mca_lock);
897
898 /* initial mode is (EX, empty) */
899 mc->mca_sfmode = MCAST_EXCLUDE;
900 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
901
902 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
903 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
904 mc->mca_flags |= MAF_NOREPORT;
905
906 mc->next = idev->mc_list;
907 idev->mc_list = mc;
908 write_unlock_bh(&idev->lock);
909
910 mld_del_delrec(idev, &mc->mca_addr);
911 igmp6_group_added(mc);
912 ma_put(mc);
913 return 0;
914}
915
916/*
917 * device multicast group del
918 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900919int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 struct ifmcaddr6 *ma, **map;
922
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200923 ASSERT_RTNL();
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 write_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100926 for (map = &idev->mc_list; (ma = *map) != NULL; map = &ma->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
928 if (--ma->mca_users == 0) {
929 *map = ma->next;
930 write_unlock_bh(&idev->lock);
931
932 igmp6_group_dropped(ma);
933
934 ma_put(ma);
935 return 0;
936 }
937 write_unlock_bh(&idev->lock);
938 return 0;
939 }
940 }
941 write_unlock_bh(&idev->lock);
942
943 return -ENOENT;
944}
945
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900946int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Eric Dumazet96b52e62010-06-07 21:05:02 +0000948 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 int err;
950
Eric Dumazet96b52e62010-06-07 21:05:02 +0000951 rcu_read_lock();
952
953 idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (!idev)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000955 err = -ENODEV;
956 else
957 err = __ipv6_dev_mc_dec(idev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Eric Dumazet96b52e62010-06-07 21:05:02 +0000959 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return err;
961}
962
963/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 * check if the interface/address pair is valid
965 */
Eric Dumazeta50feda2012-05-18 18:57:34 +0000966bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
967 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 struct inet6_dev *idev;
970 struct ifmcaddr6 *mc;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000971 bool rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Eric Dumazet96b52e62010-06-07 21:05:02 +0000973 rcu_read_lock();
974 idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (idev) {
976 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100977 for (mc = idev->mc_list; mc; mc = mc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (ipv6_addr_equal(&mc->mca_addr, group))
979 break;
980 }
981 if (mc) {
982 if (src_addr && !ipv6_addr_any(src_addr)) {
983 struct ip6_sf_list *psf;
984
985 spin_lock_bh(&mc->mca_lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100986 for (psf = mc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
988 break;
989 }
990 if (psf)
991 rv = psf->sf_count[MCAST_INCLUDE] ||
992 psf->sf_count[MCAST_EXCLUDE] !=
993 mc->mca_sfcount[MCAST_EXCLUDE];
994 else
Ian Morris67ba4152014-08-24 21:53:10 +0100995 rv = mc->mca_sfcount[MCAST_EXCLUDE] != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 spin_unlock_bh(&mc->mca_lock);
997 } else
Eric Dumazeta50feda2012-05-18 18:57:34 +0000998 rv = true; /* don't filter unspecified source */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
Eric Dumazet96b52e62010-06-07 21:05:02 +00001002 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return rv;
1004}
1005
1006static void mld_gq_start_timer(struct inet6_dev *idev)
1007{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001008 unsigned long tv = prandom_u32() % idev->mc_maxdelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 idev->mc_gq_running = 1;
1011 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1012 in6_dev_hold(idev);
1013}
1014
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001015static void mld_gq_stop_timer(struct inet6_dev *idev)
1016{
1017 idev->mc_gq_running = 0;
1018 if (del_timer(&idev->mc_gq_timer))
1019 __in6_dev_put(idev);
1020}
1021
Daniel Borkmannc2cef4e2013-08-20 12:22:01 +02001022static void mld_ifc_start_timer(struct inet6_dev *idev, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001024 unsigned long tv = prandom_u32() % delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1027 in6_dev_hold(idev);
1028}
1029
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001030static void mld_ifc_stop_timer(struct inet6_dev *idev)
1031{
1032 idev->mc_ifc_count = 0;
1033 if (del_timer(&idev->mc_ifc_timer))
1034 __in6_dev_put(idev);
1035}
1036
Daniel Borkmannc2cef4e2013-08-20 12:22:01 +02001037static void mld_dad_start_timer(struct inet6_dev *idev, unsigned long delay)
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02001038{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001039 unsigned long tv = prandom_u32() % delay;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02001040
1041 if (!mod_timer(&idev->mc_dad_timer, jiffies+tv+2))
1042 in6_dev_hold(idev);
1043}
1044
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001045static void mld_dad_stop_timer(struct inet6_dev *idev)
1046{
1047 if (del_timer(&idev->mc_dad_timer))
1048 __in6_dev_put(idev);
1049}
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051/*
1052 * IGMP handling (alias multicast ICMPv6 messages)
1053 */
1054
1055static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1056{
1057 unsigned long delay = resptime;
1058
1059 /* Do not start timer for these addresses */
1060 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1061 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1062 return;
1063
1064 if (del_timer(&ma->mca_timer)) {
1065 atomic_dec(&ma->mca_refcnt);
1066 delay = ma->mca_timer.expires - jiffies;
1067 }
1068
Daniel Borkmanncc7f7ab2013-09-04 00:19:41 +02001069 if (delay >= resptime)
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001070 delay = prandom_u32() % resptime;
Daniel Borkmanncc7f7ab2013-09-04 00:19:41 +02001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 ma->mca_timer.expires = jiffies + delay;
1073 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1074 atomic_inc(&ma->mca_refcnt);
1075 ma->mca_flags |= MAF_TIMER_RUNNING;
1076}
1077
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001078/* mark EXCLUDE-mode sources */
Eric Dumazeta50feda2012-05-18 18:57:34 +00001079static bool mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1080 const struct in6_addr *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
1082 struct ip6_sf_list *psf;
1083 int i, scount;
1084
1085 scount = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01001086 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (scount == nsrcs)
1088 break;
Ian Morris67ba4152014-08-24 21:53:10 +01001089 for (i = 0; i < nsrcs; i++) {
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001090 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +00001091 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001092 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1093 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +08001094 break;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001095 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1096 scount++;
1097 break;
1098 }
1099 }
1100 }
1101 pmc->mca_flags &= ~MAF_GSQUERY;
1102 if (scount == nsrcs) /* all sources excluded */
Eric Dumazeta50feda2012-05-18 18:57:34 +00001103 return false;
1104 return true;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001105}
1106
Eric Dumazeta50feda2012-05-18 18:57:34 +00001107static bool mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1108 const struct in6_addr *srcs)
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001109{
1110 struct ip6_sf_list *psf;
1111 int i, scount;
1112
1113 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1114 return mld_xmarksources(pmc, nsrcs, srcs);
1115
1116 /* mark INCLUDE-mode sources */
1117
1118 scount = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01001119 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001120 if (scount == nsrcs)
1121 break;
Ian Morris67ba4152014-08-24 21:53:10 +01001122 for (i = 0; i < nsrcs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1124 psf->sf_gsresp = 1;
1125 scount++;
1126 break;
1127 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001130 if (!scount) {
1131 pmc->mca_flags &= ~MAF_GSQUERY;
Eric Dumazeta50feda2012-05-18 18:57:34 +00001132 return false;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001133 }
1134 pmc->mca_flags |= MAF_GSQUERY;
Eric Dumazeta50feda2012-05-18 18:57:34 +00001135 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
Daniel Borkmann58c0ecf2013-09-04 00:19:40 +02001138static int mld_force_mld_version(const struct inet6_dev *idev)
1139{
1140 /* Normally, both are 0 here. If enforcement to a particular is
1141 * being used, individual device enforcement will have a lower
1142 * precedence over 'all' device (.../conf/all/force_mld_version).
1143 */
1144
1145 if (dev_net(idev->dev)->ipv6.devconf_all->force_mld_version != 0)
1146 return dev_net(idev->dev)->ipv6.devconf_all->force_mld_version;
1147 else
1148 return idev->cnf.force_mld_version;
1149}
1150
1151static bool mld_in_v2_mode_only(const struct inet6_dev *idev)
1152{
1153 return mld_force_mld_version(idev) == 2;
1154}
1155
1156static bool mld_in_v1_mode_only(const struct inet6_dev *idev)
1157{
1158 return mld_force_mld_version(idev) == 1;
1159}
1160
Daniel Borkmann6c567b72013-09-04 00:19:38 +02001161static bool mld_in_v1_mode(const struct inet6_dev *idev)
1162{
Daniel Borkmann58c0ecf2013-09-04 00:19:40 +02001163 if (mld_in_v2_mode_only(idev))
1164 return false;
1165 if (mld_in_v1_mode_only(idev))
Daniel Borkmann6c567b72013-09-04 00:19:38 +02001166 return true;
1167 if (idev->mc_v1_seen && time_before(jiffies, idev->mc_v1_seen))
1168 return true;
1169
1170 return false;
1171}
1172
Daniel Borkmann89225d12013-09-04 00:19:37 +02001173static void mld_set_v1_mode(struct inet6_dev *idev)
1174{
1175 /* RFC3810, relevant sections:
1176 * - 9.1. Robustness Variable
1177 * - 9.2. Query Interval
1178 * - 9.3. Query Response Interval
1179 * - 9.12. Older Version Querier Present Timeout
1180 */
1181 unsigned long switchback;
1182
1183 switchback = (idev->mc_qrv * idev->mc_qi) + idev->mc_qri;
1184
1185 idev->mc_v1_seen = jiffies + switchback;
1186}
1187
1188static void mld_update_qrv(struct inet6_dev *idev,
1189 const struct mld2_query *mlh2)
1190{
1191 /* RFC3810, relevant sections:
1192 * - 5.1.8. QRV (Querier's Robustness Variable)
1193 * - 9.1. Robustness Variable
1194 */
1195
1196 /* The value of the Robustness Variable MUST NOT be zero,
1197 * and SHOULD NOT be one. Catch this here if we ever run
1198 * into such a case in future.
1199 */
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02001200 const int min_qrv = min(MLD_QRV_DEFAULT, sysctl_mld_qrv);
Daniel Borkmann89225d12013-09-04 00:19:37 +02001201 WARN_ON(idev->mc_qrv == 0);
1202
1203 if (mlh2->mld2q_qrv > 0)
1204 idev->mc_qrv = mlh2->mld2q_qrv;
1205
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02001206 if (unlikely(idev->mc_qrv < min_qrv)) {
Daniel Borkmann89225d12013-09-04 00:19:37 +02001207 net_warn_ratelimited("IPv6: MLD: clamping QRV from %u to %u!\n",
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02001208 idev->mc_qrv, min_qrv);
1209 idev->mc_qrv = min_qrv;
Daniel Borkmann89225d12013-09-04 00:19:37 +02001210 }
1211}
1212
1213static void mld_update_qi(struct inet6_dev *idev,
1214 const struct mld2_query *mlh2)
1215{
1216 /* RFC3810, relevant sections:
1217 * - 5.1.9. QQIC (Querier's Query Interval Code)
1218 * - 9.2. Query Interval
1219 * - 9.12. Older Version Querier Present Timeout
1220 * (the [Query Interval] in the last Query received)
1221 */
1222 unsigned long mc_qqi;
1223
1224 if (mlh2->mld2q_qqic < 128) {
1225 mc_qqi = mlh2->mld2q_qqic;
1226 } else {
1227 unsigned long mc_man, mc_exp;
1228
1229 mc_exp = MLDV2_QQIC_EXP(mlh2->mld2q_qqic);
1230 mc_man = MLDV2_QQIC_MAN(mlh2->mld2q_qqic);
1231
1232 mc_qqi = (mc_man | 0x10) << (mc_exp + 3);
1233 }
1234
1235 idev->mc_qi = mc_qqi * HZ;
1236}
1237
1238static void mld_update_qri(struct inet6_dev *idev,
1239 const struct mld2_query *mlh2)
1240{
1241 /* RFC3810, relevant sections:
1242 * - 5.1.3. Maximum Response Code
1243 * - 9.3. Query Response Interval
1244 */
Daniel Borkmanne3f5b172013-09-04 00:19:39 +02001245 idev->mc_qri = msecs_to_jiffies(mldv2_mrc(mlh2));
Daniel Borkmann89225d12013-09-04 00:19:37 +02001246}
1247
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001248static int mld_process_v1(struct inet6_dev *idev, struct mld_msg *mld,
1249 unsigned long *max_delay)
1250{
1251 unsigned long mldv1_md;
1252
1253 /* Ignore v1 queries */
1254 if (mld_in_v2_mode_only(idev))
1255 return -EINVAL;
1256
1257 /* MLDv1 router present */
1258 mldv1_md = ntohs(mld->mld_maxdelay);
1259 *max_delay = max(msecs_to_jiffies(mldv1_md), 1UL);
1260
1261 mld_set_v1_mode(idev);
1262
1263 /* cancel MLDv2 report timer */
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001264 mld_gq_stop_timer(idev);
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001265 /* cancel the interface change timer */
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001266 mld_ifc_stop_timer(idev);
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001267 /* clear deleted report items */
1268 mld_clear_delrec(idev);
1269
1270 return 0;
1271}
1272
1273static int mld_process_v2(struct inet6_dev *idev, struct mld2_query *mld,
1274 unsigned long *max_delay)
1275{
1276 /* hosts need to stay in MLDv1 mode, discard MLDv2 queries */
1277 if (mld_in_v1_mode(idev))
1278 return -EINVAL;
1279
1280 *max_delay = max(msecs_to_jiffies(mldv2_mrc(mld)), 1UL);
1281
1282 mld_update_qrv(idev, mld);
1283 mld_update_qi(idev, mld);
1284 mld_update_qri(idev, mld);
1285
1286 idev->mc_maxdelay = *max_delay;
1287
1288 return 0;
1289}
1290
Eric Dumazet96b52e62010-06-07 21:05:02 +00001291/* called with rcu_read_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292int igmp6_event_query(struct sk_buff *skb)
1293{
Yan Zheng97300b52005-10-31 20:09:45 +08001294 struct mld2_query *mlh2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 struct ifmcaddr6 *ma;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001296 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 unsigned long max_delay;
1298 struct inet6_dev *idev;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001299 struct mld_msg *mld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 int group_type;
1301 int mark = 0;
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001302 int len, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1305 return -EINVAL;
1306
1307 /* compute payload length excluding extension headers */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001308 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001309 len -= skb_network_header_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Hangbin Liue940f5d2014-06-27 09:57:53 +08001311 /* RFC3810 6.2
1312 * Upon reception of an MLD message that contains a Query, the node
1313 * checks if the source address of the message is a valid link-local
1314 * address, if the Hop Limit is set to 1, and if the Router Alert
1315 * option is present in the Hop-By-Hop Options header of the IPv6
1316 * packet. If any of these checks fails, the packet is dropped.
1317 */
1318 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL) ||
1319 ipv6_hdr(skb)->hop_limit != 1 ||
1320 !(IP6CB(skb)->flags & IP6SKB_ROUTERALERT) ||
1321 IP6CB(skb)->ra != htons(IPV6_OPT_ROUTERALERT_MLD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return -EINVAL;
1323
Eric Dumazet96b52e62010-06-07 21:05:02 +00001324 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 if (idev == NULL)
1326 return 0;
1327
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001328 mld = (struct mld_msg *)icmp6_hdr(skb);
1329 group = &mld->mld_mca;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 group_type = ipv6_addr_type(group);
1331
1332 if (group_type != IPV6_ADDR_ANY &&
Eric Dumazet96b52e62010-06-07 21:05:02 +00001333 !(group_type&IPV6_ADDR_MULTICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Daniel Borkmann9fd07842013-08-20 12:22:02 +02001336 if (len == MLD_V1_QUERY_LEN) {
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001337 err = mld_process_v1(idev, mld, &max_delay);
1338 if (err < 0)
1339 return err;
Daniel Borkmann9fd07842013-08-20 12:22:02 +02001340 } else if (len >= MLD_V2_QUERY_LEN_MIN) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001341 int srcs_offset = sizeof(struct mld2_query) -
Yan Zheng97300b52005-10-31 20:09:45 +08001342 sizeof(struct icmp6hdr);
Daniel Borkmann89225d12013-09-04 00:19:37 +02001343
Eric Dumazet96b52e62010-06-07 21:05:02 +00001344 if (!pskb_may_pull(skb, srcs_offset))
Yan Zheng97300b52005-10-31 20:09:45 +08001345 return -EINVAL;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001346
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001347 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Daniel Borkmann84698962013-08-20 12:22:00 +02001348
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001349 err = mld_process_v2(idev, mlh2, &max_delay);
1350 if (err < 0)
1351 return err;
Daniel Borkmann89225d12013-09-04 00:19:37 +02001352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (group_type == IPV6_ADDR_ANY) { /* general query */
Eric Dumazet96b52e62010-06-07 21:05:02 +00001354 if (mlh2->mld2q_nsrcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return -EINVAL; /* no sources allowed */
Eric Dumazet96b52e62010-06-07 21:05:02 +00001356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 mld_gq_start_timer(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return 0;
1359 }
1360 /* mark sources to include, if group & source-specific */
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001361 if (mlh2->mld2q_nsrcs != 0) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001362 if (!pskb_may_pull(skb, srcs_offset +
Eric Dumazet96b52e62010-06-07 21:05:02 +00001363 ntohs(mlh2->mld2q_nsrcs) * sizeof(struct in6_addr)))
Yan Zheng97300b52005-10-31 20:09:45 +08001364 return -EINVAL;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001365
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001366 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Yan Zheng97300b52005-10-31 20:09:45 +08001367 mark = 1;
1368 }
Eric Dumazet96b52e62010-06-07 21:05:02 +00001369 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 read_lock_bh(&idev->lock);
1373 if (group_type == IPV6_ADDR_ANY) {
Ian Morris67ba4152014-08-24 21:53:10 +01001374 for (ma = idev->mc_list; ma; ma = ma->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 spin_lock_bh(&ma->mca_lock);
1376 igmp6_group_queried(ma, max_delay);
1377 spin_unlock_bh(&ma->mca_lock);
1378 }
1379 } else {
Ian Morris67ba4152014-08-24 21:53:10 +01001380 for (ma = idev->mc_list; ma; ma = ma->next) {
David L Stevens7add2a42006-01-24 13:06:39 -08001381 if (!ipv6_addr_equal(group, &ma->mca_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 continue;
1383 spin_lock_bh(&ma->mca_lock);
1384 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1385 /* gsquery <- gsquery && mark */
1386 if (!mark)
1387 ma->mca_flags &= ~MAF_GSQUERY;
1388 } else {
1389 /* gsquery <- mark */
1390 if (mark)
1391 ma->mca_flags |= MAF_GSQUERY;
1392 else
1393 ma->mca_flags &= ~MAF_GSQUERY;
1394 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001395 if (!(ma->mca_flags & MAF_GSQUERY) ||
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001396 mld_marksources(ma, ntohs(mlh2->mld2q_nsrcs), mlh2->mld2q_srcs))
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001397 igmp6_group_queried(ma, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 spin_unlock_bh(&ma->mca_lock);
David L Stevens7add2a42006-01-24 13:06:39 -08001399 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401 }
1402 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 return 0;
1405}
1406
Eric Dumazet96b52e62010-06-07 21:05:02 +00001407/* called with rcu_read_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408int igmp6_event_report(struct sk_buff *skb)
1409{
1410 struct ifmcaddr6 *ma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 struct inet6_dev *idev;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001412 struct mld_msg *mld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 int addr_type;
1414
1415 /* Our own report looped back. Ignore it. */
1416 if (skb->pkt_type == PACKET_LOOPBACK)
1417 return 0;
1418
David Stevens24c69272005-12-02 20:32:59 -08001419 /* send our report if the MC router may not have heard this report */
1420 if (skb->pkt_type != PACKET_MULTICAST &&
1421 skb->pkt_type != PACKET_BROADCAST)
1422 return 0;
1423
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001424 if (!pskb_may_pull(skb, sizeof(*mld) - sizeof(struct icmp6hdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return -EINVAL;
1426
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001427 mld = (struct mld_msg *)icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 /* Drop reports with not link local source */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001430 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001431 if (addr_type != IPV6_ADDR_ANY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 !(addr_type&IPV6_ADDR_LINKLOCAL))
1433 return -EINVAL;
1434
Eric Dumazet96b52e62010-06-07 21:05:02 +00001435 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (idev == NULL)
1437 return -ENODEV;
1438
1439 /*
1440 * Cancel the timer for this group
1441 */
1442
1443 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +01001444 for (ma = idev->mc_list; ma; ma = ma->next) {
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001445 if (ipv6_addr_equal(&ma->mca_addr, &mld->mld_mca)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 spin_lock(&ma->mca_lock);
1447 if (del_timer(&ma->mca_timer))
1448 atomic_dec(&ma->mca_refcnt);
1449 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1450 spin_unlock(&ma->mca_lock);
1451 break;
1452 }
1453 }
1454 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return 0;
1456}
1457
Eric Dumazeta50feda2012-05-18 18:57:34 +00001458static bool is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1459 int gdeleted, int sdeleted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 switch (type) {
1462 case MLD2_MODE_IS_INCLUDE:
1463 case MLD2_MODE_IS_EXCLUDE:
1464 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001465 return false;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001466 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1467 if (pmc->mca_sfmode == MCAST_INCLUDE)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001468 return true;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001469 /* don't include if this source is excluded
1470 * in all filters
1471 */
1472 if (psf->sf_count[MCAST_INCLUDE])
David L Stevens7add2a42006-01-24 13:06:39 -08001473 return type == MLD2_MODE_IS_INCLUDE;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001474 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1475 psf->sf_count[MCAST_EXCLUDE];
1476 }
Eric Dumazeta50feda2012-05-18 18:57:34 +00001477 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 case MLD2_CHANGE_TO_INCLUDE:
1479 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001480 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return psf->sf_count[MCAST_INCLUDE] != 0;
1482 case MLD2_CHANGE_TO_EXCLUDE:
1483 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001484 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1486 psf->sf_count[MCAST_INCLUDE])
Eric Dumazeta50feda2012-05-18 18:57:34 +00001487 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1489 psf->sf_count[MCAST_EXCLUDE];
1490 case MLD2_ALLOW_NEW_SOURCES:
1491 if (gdeleted || !psf->sf_crcount)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001492 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1494 case MLD2_BLOCK_OLD_SOURCES:
1495 if (pmc->mca_sfmode == MCAST_INCLUDE)
1496 return gdeleted || (psf->sf_crcount && sdeleted);
1497 return psf->sf_crcount && !gdeleted && !sdeleted;
1498 }
Eric Dumazeta50feda2012-05-18 18:57:34 +00001499 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
1502static int
1503mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1504{
1505 struct ip6_sf_list *psf;
1506 int scount = 0;
1507
Ian Morris67ba4152014-08-24 21:53:10 +01001508 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1510 continue;
1511 scount++;
1512 }
1513 return scount;
1514}
1515
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +00001516static void ip6_mc_hdr(struct sock *sk, struct sk_buff *skb,
1517 struct net_device *dev,
1518 const struct in6_addr *saddr,
1519 const struct in6_addr *daddr,
1520 int proto, int len)
1521{
1522 struct ipv6hdr *hdr;
1523
1524 skb->protocol = htons(ETH_P_IPV6);
1525 skb->dev = dev;
1526
1527 skb_reset_network_header(skb);
1528 skb_put(skb, sizeof(struct ipv6hdr));
1529 hdr = ipv6_hdr(skb);
1530
1531 ip6_flow_hdr(hdr, 0, 0);
1532
1533 hdr->payload_len = htons(len);
1534 hdr->nexthdr = proto;
1535 hdr->hop_limit = inet6_sk(sk)->hop_limit;
1536
1537 hdr->saddr = *saddr;
1538 hdr->daddr = *daddr;
1539}
1540
Amerigo Wang89657792013-06-29 21:30:49 +08001541static struct sk_buff *mld_newpack(struct inet6_dev *idev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Amerigo Wang89657792013-06-29 21:30:49 +08001543 struct net_device *dev = idev->dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001544 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001545 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 struct sk_buff *skb;
1547 struct mld2_report *pmr;
1548 struct in6_addr addr_buf;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001549 const struct in6_addr *saddr;
Herbert Xua7ae1992011-11-18 02:20:04 +00001550 int hlen = LL_RESERVED_SPACE(dev);
1551 int tlen = dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 int err;
1553 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1554 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1555 IPV6_TLV_PADN, 0 };
1556
1557 /* we assume size > sizeof(ra) here */
Herbert Xua7ae1992011-11-18 02:20:04 +00001558 size += hlen + tlen;
Eric Dumazet72e09ad2010-06-05 03:03:30 -07001559 /* limit our allocations to order-0 page */
1560 size = min_t(int, size, SKB_MAX_ORDER(0, 0));
1561 skb = sock_alloc_send_skb(sk, size, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001563 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return NULL;
1565
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +02001566 skb->priority = TC_PRIO_CONTROL;
Herbert Xua7ae1992011-11-18 02:20:04 +00001567 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Amerigo Wang89657792013-06-29 21:30:49 +08001569 if (__ipv6_get_lladdr(idev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001571 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 * when a valid link-local address is not available.
1573 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001574 saddr = &in6addr_any;
1575 } else
1576 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +00001578 ip6_mc_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1581
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001582 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -03001583 skb_put(skb, sizeof(*pmr));
1584 pmr = (struct mld2_report *)skb_transport_header(skb);
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001585 pmr->mld2r_type = ICMPV6_MLD2_REPORT;
1586 pmr->mld2r_resv1 = 0;
1587 pmr->mld2r_cksum = 0;
1588 pmr->mld2r_resv2 = 0;
1589 pmr->mld2r_ngrec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return skb;
1591}
1592
1593static void mld_sendpack(struct sk_buff *skb)
1594{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001595 struct ipv6hdr *pip6 = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001596 struct mld2_report *pmr =
1597 (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 int payload_len, mldlen;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001599 struct inet6_dev *idev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001600 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 int err;
David S. Miller4c9483b2011-03-12 16:22:43 -05001602 struct flowi6 fl6;
Eric Dumazetadf30902009-06-02 05:19:30 +00001603 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Eric Dumazet96b52e62010-06-07 21:05:02 +00001605 rcu_read_lock();
1606 idev = __in6_dev_get(skb->dev);
Neil Hormanedf391f2009-04-27 02:45:02 -07001607 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
1608
Simon Horman29a3cad2013-05-28 20:34:26 +00001609 payload_len = (skb_tail_pointer(skb) - skb_network_header(skb)) -
1610 sizeof(*pip6);
1611 mldlen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 pip6->payload_len = htons(payload_len);
1613
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001614 pmr->mld2r_cksum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1615 IPPROTO_ICMPV6,
1616 csum_partial(skb_transport_header(skb),
1617 mldlen, 0));
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001618
David S. Miller4c9483b2011-03-12 16:22:43 -05001619 icmpv6_flow_init(net->ipv6.igmp_sk, &fl6, ICMPV6_MLD2_REPORT,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001620 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1621 skb->dev->ifindex);
YOSHIFUJI Hideaki / 吉藤英明12fd84f2013-01-18 02:00:24 +00001622 dst = icmp6_dst_alloc(skb->dev, &fl6);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001623
David S. Miller452edd52011-03-02 13:27:41 -08001624 err = 0;
1625 if (IS_ERR(dst)) {
1626 err = PTR_ERR(dst);
1627 dst = NULL;
1628 }
Eric Dumazetadf30902009-06-02 05:19:30 +00001629 skb_dst_set(skb, dst);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001630 if (err)
1631 goto err_out;
1632
Neil Hormanedf391f2009-04-27 02:45:02 -07001633 payload_len = skb->len;
1634
Jan Engelhardtb2e0b382010-03-23 04:09:07 +01001635 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001636 dst_output);
1637out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (!err) {
Hannes Frederic Sowa43a43b62014-03-31 20:14:10 +02001639 ICMP6MSGOUT_INC_STATS(net, idev, ICMPV6_MLD2_REPORT);
1640 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
1641 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, payload_len);
1642 } else {
1643 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
1644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Eric Dumazet96b52e62010-06-07 21:05:02 +00001646 rcu_read_unlock();
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001647 return;
1648
1649err_out:
1650 kfree_skb(skb);
1651 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
1654static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1655{
Yan Zhengfab10fe2005-10-05 12:08:13 -07001656 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
1659static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1660 int type, struct mld2_grec **ppgr)
1661{
1662 struct net_device *dev = pmc->idev->dev;
1663 struct mld2_report *pmr;
1664 struct mld2_grec *pgr;
1665
1666 if (!skb)
Amerigo Wang89657792013-06-29 21:30:49 +08001667 skb = mld_newpack(pmc->idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 if (!skb)
1669 return NULL;
1670 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1671 pgr->grec_type = type;
1672 pgr->grec_auxwords = 0;
1673 pgr->grec_nsrcs = 0;
1674 pgr->grec_mca = pmc->mca_addr; /* structure copy */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001675 pmr = (struct mld2_report *)skb_transport_header(skb);
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001676 pmr->mld2r_ngrec = htons(ntohs(pmr->mld2r_ngrec)+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 *ppgr = pgr;
1678 return skb;
1679}
1680
1681#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1682 skb_tailroom(skb)) : 0)
1683
1684static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001685 int type, int gdeleted, int sdeleted, int crsend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
Amerigo Wang89657792013-06-29 21:30:49 +08001687 struct inet6_dev *idev = pmc->idev;
1688 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 struct mld2_report *pmr;
1690 struct mld2_grec *pgr = NULL;
1691 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001692 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 if (pmc->mca_flags & MAF_NOREPORT)
1695 return skb;
1696
1697 isquery = type == MLD2_MODE_IS_INCLUDE ||
1698 type == MLD2_MODE_IS_EXCLUDE;
1699 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1700 type == MLD2_CHANGE_TO_EXCLUDE;
1701
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001702 stotal = scount = 0;
1703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1705
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001706 if (!*psf_list)
1707 goto empty_source;
1708
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001709 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 /* EX and TO_EX get a fresh packet, if needed */
1712 if (truncate) {
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001713 if (pmr && pmr->mld2r_ngrec &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1715 if (skb)
1716 mld_sendpack(skb);
Amerigo Wang89657792013-06-29 21:30:49 +08001717 skb = mld_newpack(idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719 }
1720 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 psf_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01001722 for (psf = *psf_list; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 struct in6_addr *psrc;
1724
1725 psf_next = psf->sf_next;
1726
1727 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1728 psf_prev = psf;
1729 continue;
1730 }
1731
1732 /* clear marks on query responses */
1733 if (isquery)
1734 psf->sf_gsresp = 0;
1735
1736 if (AVAILABLE(skb) < sizeof(*psrc) +
1737 first*sizeof(struct mld2_grec)) {
1738 if (truncate && !first)
1739 break; /* truncate these */
1740 if (pgr)
1741 pgr->grec_nsrcs = htons(scount);
1742 if (skb)
1743 mld_sendpack(skb);
Amerigo Wang89657792013-06-29 21:30:49 +08001744 skb = mld_newpack(idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 first = 1;
1746 scount = 0;
1747 }
1748 if (first) {
1749 skb = add_grhead(skb, pmc, type, &pgr);
1750 first = 0;
1751 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -08001752 if (!skb)
1753 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1755 *psrc = psf->sf_addr;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001756 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1758 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1759 psf->sf_crcount--;
1760 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1761 if (psf_prev)
1762 psf_prev->sf_next = psf->sf_next;
1763 else
1764 *psf_list = psf->sf_next;
1765 kfree(psf);
1766 continue;
1767 }
1768 }
1769 psf_prev = psf;
1770 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001771
1772empty_source:
1773 if (!stotal) {
1774 if (type == MLD2_ALLOW_NEW_SOURCES ||
1775 type == MLD2_BLOCK_OLD_SOURCES)
1776 return skb;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001777 if (pmc->mca_crcount || isquery || crsend) {
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001778 /* make sure we have room for group header */
1779 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1780 mld_sendpack(skb);
1781 skb = NULL; /* add_grhead will get a new one */
1782 }
1783 skb = add_grhead(skb, pmc, type, &pgr);
1784 }
1785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if (pgr)
1787 pgr->grec_nsrcs = htons(scount);
1788
1789 if (isquery)
1790 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1791 return skb;
1792}
1793
1794static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1795{
1796 struct sk_buff *skb = NULL;
1797 int type;
1798
Amerigo Wang89657792013-06-29 21:30:49 +08001799 read_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 if (!pmc) {
Ian Morris67ba4152014-08-24 21:53:10 +01001801 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (pmc->mca_flags & MAF_NOREPORT)
1803 continue;
1804 spin_lock_bh(&pmc->mca_lock);
1805 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1806 type = MLD2_MODE_IS_EXCLUDE;
1807 else
1808 type = MLD2_MODE_IS_INCLUDE;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001809 skb = add_grec(skb, pmc, type, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 spin_unlock_bh(&pmc->mca_lock);
1811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 } else {
1813 spin_lock_bh(&pmc->mca_lock);
1814 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1815 type = MLD2_MODE_IS_EXCLUDE;
1816 else
1817 type = MLD2_MODE_IS_INCLUDE;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001818 skb = add_grec(skb, pmc, type, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 spin_unlock_bh(&pmc->mca_lock);
1820 }
Amerigo Wang89657792013-06-29 21:30:49 +08001821 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (skb)
1823 mld_sendpack(skb);
1824}
1825
1826/*
1827 * remove zero-count source records from a source filter list
1828 */
1829static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1830{
1831 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1832
1833 psf_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01001834 for (psf = *ppsf; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 psf_next = psf->sf_next;
1836 if (psf->sf_crcount == 0) {
1837 if (psf_prev)
1838 psf_prev->sf_next = psf->sf_next;
1839 else
1840 *ppsf = psf->sf_next;
1841 kfree(psf);
1842 } else
1843 psf_prev = psf;
1844 }
1845}
1846
1847static void mld_send_cr(struct inet6_dev *idev)
1848{
1849 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1850 struct sk_buff *skb = NULL;
1851 int type, dtype;
1852
1853 read_lock_bh(&idev->lock);
Stephen Hemminger6457d262010-02-17 18:48:44 -08001854 spin_lock(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 /* deleted MCA's */
1857 pmc_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01001858 for (pmc = idev->mc_tomb; pmc; pmc = pmc_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 pmc_next = pmc->next;
1860 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1861 type = MLD2_BLOCK_OLD_SOURCES;
1862 dtype = MLD2_BLOCK_OLD_SOURCES;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001863 skb = add_grec(skb, pmc, type, 1, 0, 0);
1864 skb = add_grec(skb, pmc, dtype, 1, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 }
1866 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1868 type = MLD2_CHANGE_TO_INCLUDE;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001869 skb = add_grec(skb, pmc, type, 1, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001871 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if (pmc->mca_crcount == 0) {
1873 mld_clear_zeros(&pmc->mca_tomb);
1874 mld_clear_zeros(&pmc->mca_sources);
1875 }
1876 }
1877 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1878 !pmc->mca_sources) {
1879 if (pmc_prev)
1880 pmc_prev->next = pmc_next;
1881 else
1882 idev->mc_tomb = pmc_next;
1883 in6_dev_put(pmc->idev);
1884 kfree(pmc);
1885 } else
1886 pmc_prev = pmc;
1887 }
Stephen Hemminger6457d262010-02-17 18:48:44 -08001888 spin_unlock(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 /* change recs */
Ian Morris67ba4152014-08-24 21:53:10 +01001891 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 spin_lock_bh(&pmc->mca_lock);
1893 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1894 type = MLD2_BLOCK_OLD_SOURCES;
1895 dtype = MLD2_ALLOW_NEW_SOURCES;
1896 } else {
1897 type = MLD2_ALLOW_NEW_SOURCES;
1898 dtype = MLD2_BLOCK_OLD_SOURCES;
1899 }
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001900 skb = add_grec(skb, pmc, type, 0, 0, 0);
1901 skb = add_grec(skb, pmc, dtype, 0, 1, 0); /* deleted sources */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903 /* filter mode changes */
1904 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1906 type = MLD2_CHANGE_TO_EXCLUDE;
1907 else
1908 type = MLD2_CHANGE_TO_INCLUDE;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001909 skb = add_grec(skb, pmc, type, 0, 0, 0);
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001910 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912 spin_unlock_bh(&pmc->mca_lock);
1913 }
1914 read_unlock_bh(&idev->lock);
1915 if (!skb)
1916 return;
1917 (void) mld_sendpack(skb);
1918}
1919
1920static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1921{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001922 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001923 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 struct inet6_dev *idev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001925 struct sk_buff *skb;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001926 struct mld_msg *hdr;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001927 const struct in6_addr *snd_addr, *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 struct in6_addr addr_buf;
Herbert Xua7ae1992011-11-18 02:20:04 +00001929 int hlen = LL_RESERVED_SPACE(dev);
1930 int tlen = dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 int err, len, payload_len, full_len;
1932 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1933 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1934 IPV6_TLV_PADN, 0 };
David S. Miller4c9483b2011-03-12 16:22:43 -05001935 struct flowi6 fl6;
Eric Dumazetadf30902009-06-02 05:19:30 +00001936 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09001938 if (type == ICMPV6_MGM_REDUCTION)
1939 snd_addr = &in6addr_linklocal_allrouters;
1940 else
1941 snd_addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1944 payload_len = len + sizeof(ra);
1945 full_len = sizeof(struct ipv6hdr) + payload_len;
1946
Neil Hormanedf391f2009-04-27 02:45:02 -07001947 rcu_read_lock();
1948 IP6_UPD_PO_STATS(net, __in6_dev_get(dev),
1949 IPSTATS_MIB_OUT, full_len);
1950 rcu_read_unlock();
1951
Herbert Xua7ae1992011-11-18 02:20:04 +00001952 skb = sock_alloc_send_skb(sk, hlen + tlen + full_len, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954 if (skb == NULL) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001955 rcu_read_lock();
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001956 IP6_INC_STATS(net, __in6_dev_get(dev),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001957 IPSTATS_MIB_OUTDISCARDS);
1958 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 return;
1960 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +02001961 skb->priority = TC_PRIO_CONTROL;
Herbert Xua7ae1992011-11-18 02:20:04 +00001962 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Neil Horman95c385b2007-04-25 17:08:10 -07001964 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001966 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 * when a valid link-local address is not available.
1968 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001969 saddr = &in6addr_any;
1970 } else
1971 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +00001973 ip6_mc_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1976
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001977 hdr = (struct mld_msg *) skb_put(skb, sizeof(struct mld_msg));
1978 memset(hdr, 0, sizeof(struct mld_msg));
1979 hdr->mld_type = type;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001980 hdr->mld_mca = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001982 hdr->mld_cksum = csum_ipv6_magic(saddr, snd_addr, len,
1983 IPPROTO_ICMPV6,
1984 csum_partial(hdr, len, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Eric Dumazet96b52e62010-06-07 21:05:02 +00001986 rcu_read_lock();
1987 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
David S. Miller4c9483b2011-03-12 16:22:43 -05001989 icmpv6_flow_init(sk, &fl6, type,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001990 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1991 skb->dev->ifindex);
YOSHIFUJI Hideaki / 吉藤英明12fd84f2013-01-18 02:00:24 +00001992 dst = icmp6_dst_alloc(skb->dev, &fl6);
David S. Miller452edd52011-03-02 13:27:41 -08001993 if (IS_ERR(dst)) {
1994 err = PTR_ERR(dst);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001995 goto err_out;
David S. Miller452edd52011-03-02 13:27:41 -08001996 }
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001997
Eric Dumazetadf30902009-06-02 05:19:30 +00001998 skb_dst_set(skb, dst);
Jan Engelhardtb2e0b382010-03-23 04:09:07 +01001999 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08002000 dst_output);
2001out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (!err) {
Denis V. Lunev5c5d2442008-10-08 10:33:50 -07002003 ICMP6MSGOUT_INC_STATS(net, idev, type);
Denis V. Luneva862f6a2008-10-08 10:33:06 -07002004 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
Neil Hormanedf391f2009-04-27 02:45:02 -07002005 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, full_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 } else
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002007 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Eric Dumazet96b52e62010-06-07 21:05:02 +00002009 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return;
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08002011
2012err_out:
2013 kfree_skb(skb);
2014 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
2016
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002017static void mld_send_initial_cr(struct inet6_dev *idev)
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002018{
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002019 struct sk_buff *skb;
2020 struct ifmcaddr6 *pmc;
2021 int type;
2022
2023 if (mld_in_v1_mode(idev))
2024 return;
2025
2026 skb = NULL;
2027 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002028 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002029 spin_lock_bh(&pmc->mca_lock);
2030 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2031 type = MLD2_CHANGE_TO_EXCLUDE;
2032 else
2033 type = MLD2_CHANGE_TO_INCLUDE;
2034 skb = add_grec(skb, pmc, type, 0, 0, 1);
2035 spin_unlock_bh(&pmc->mca_lock);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002036 }
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002037 read_unlock_bh(&idev->lock);
2038 if (skb)
2039 mld_sendpack(skb);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002040}
2041
2042void ipv6_mc_dad_complete(struct inet6_dev *idev)
2043{
2044 idev->mc_dad_count = idev->mc_qrv;
2045 if (idev->mc_dad_count) {
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002046 mld_send_initial_cr(idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002047 idev->mc_dad_count--;
2048 if (idev->mc_dad_count)
2049 mld_dad_start_timer(idev, idev->mc_maxdelay);
2050 }
2051}
2052
2053static void mld_dad_timer_expire(unsigned long data)
2054{
2055 struct inet6_dev *idev = (struct inet6_dev *)data;
2056
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002057 mld_send_initial_cr(idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002058 if (idev->mc_dad_count) {
2059 idev->mc_dad_count--;
2060 if (idev->mc_dad_count)
2061 mld_dad_start_timer(idev, idev->mc_maxdelay);
2062 }
Salam Noureddine9260d3e2013-09-29 13:41:34 -07002063 in6_dev_put(idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002064}
2065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002067 const struct in6_addr *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
2069 struct ip6_sf_list *psf, *psf_prev;
2070 int rv = 0;
2071
2072 psf_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01002073 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
2075 break;
2076 psf_prev = psf;
2077 }
2078 if (!psf || psf->sf_count[sfmode] == 0) {
2079 /* source filter not found, or count wrong => bug */
2080 return -ESRCH;
2081 }
2082 psf->sf_count[sfmode]--;
2083 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
2084 struct inet6_dev *idev = pmc->idev;
2085
2086 /* no more filters for this source */
2087 if (psf_prev)
2088 psf_prev->sf_next = psf->sf_next;
2089 else
2090 pmc->mca_sources = psf->sf_next;
2091 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
Daniel Borkmann6c567b72013-09-04 00:19:38 +02002092 !mld_in_v1_mode(idev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 psf->sf_crcount = idev->mc_qrv;
2094 psf->sf_next = pmc->mca_tomb;
2095 pmc->mca_tomb = psf;
2096 rv = 1;
2097 } else
2098 kfree(psf);
2099 }
2100 return rv;
2101}
2102
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002103static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2104 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 int delta)
2106{
2107 struct ifmcaddr6 *pmc;
2108 int changerec = 0;
2109 int i, err;
2110
2111 if (!idev)
2112 return -ENODEV;
2113 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002114 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2116 break;
2117 }
2118 if (!pmc) {
2119 /* MCA not found?? bug */
2120 read_unlock_bh(&idev->lock);
2121 return -ESRCH;
2122 }
2123 spin_lock_bh(&pmc->mca_lock);
2124 sf_markstate(pmc);
2125 if (!delta) {
2126 if (!pmc->mca_sfcount[sfmode]) {
2127 spin_unlock_bh(&pmc->mca_lock);
2128 read_unlock_bh(&idev->lock);
2129 return -EINVAL;
2130 }
2131 pmc->mca_sfcount[sfmode]--;
2132 }
2133 err = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01002134 for (i = 0; i < sfcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2136
2137 changerec |= rv > 0;
2138 if (!err && rv < 0)
2139 err = rv;
2140 }
2141 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
2142 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
2143 pmc->mca_sfcount[MCAST_INCLUDE]) {
2144 struct ip6_sf_list *psf;
2145
2146 /* filter mode change */
2147 pmc->mca_sfmode = MCAST_INCLUDE;
2148 pmc->mca_crcount = idev->mc_qrv;
2149 idev->mc_ifc_count = pmc->mca_crcount;
Ian Morris67ba4152014-08-24 21:53:10 +01002150 for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 psf->sf_crcount = 0;
2152 mld_ifc_event(pmc->idev);
2153 } else if (sf_setstate(pmc) || changerec)
2154 mld_ifc_event(pmc->idev);
2155 spin_unlock_bh(&pmc->mca_lock);
2156 read_unlock_bh(&idev->lock);
2157 return err;
2158}
2159
2160/*
2161 * Add multicast single-source filter to the interface list
2162 */
2163static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
Jun Zhao99d2f472011-11-30 06:21:05 +00002164 const struct in6_addr *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
2166 struct ip6_sf_list *psf, *psf_prev;
2167
2168 psf_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01002169 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
2171 break;
2172 psf_prev = psf;
2173 }
2174 if (!psf) {
Ingo Oeser0c600ed2006-03-20 23:01:32 -08002175 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 if (!psf)
2177 return -ENOBUFS;
Ingo Oeser0c600ed2006-03-20 23:01:32 -08002178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 psf->sf_addr = *psfsrc;
2180 if (psf_prev) {
2181 psf_prev->sf_next = psf;
2182 } else
2183 pmc->mca_sources = psf;
2184 }
2185 psf->sf_count[sfmode]++;
2186 return 0;
2187}
2188
2189static void sf_markstate(struct ifmcaddr6 *pmc)
2190{
2191 struct ip6_sf_list *psf;
2192 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2193
Ian Morris67ba4152014-08-24 21:53:10 +01002194 for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2196 psf->sf_oldin = mca_xcount ==
2197 psf->sf_count[MCAST_EXCLUDE] &&
2198 !psf->sf_count[MCAST_INCLUDE];
2199 } else
2200 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2201}
2202
2203static int sf_setstate(struct ifmcaddr6 *pmc)
2204{
David L Stevens7add2a42006-01-24 13:06:39 -08002205 struct ip6_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2207 int qrv = pmc->idev->mc_qrv;
2208 int new_in, rv;
2209
2210 rv = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01002211 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2213 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2214 !psf->sf_count[MCAST_INCLUDE];
2215 } else
2216 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevens7add2a42006-01-24 13:06:39 -08002217 if (new_in) {
2218 if (!psf->sf_oldin) {
Al Viroe80e28b2006-02-03 20:10:03 -05002219 struct ip6_sf_list *prev = NULL;
David L Stevens7add2a42006-01-24 13:06:39 -08002220
Ian Morris67ba4152014-08-24 21:53:10 +01002221 for (dpsf = pmc->mca_tomb; dpsf;
2222 dpsf = dpsf->sf_next) {
David L Stevens7add2a42006-01-24 13:06:39 -08002223 if (ipv6_addr_equal(&dpsf->sf_addr,
2224 &psf->sf_addr))
2225 break;
2226 prev = dpsf;
2227 }
2228 if (dpsf) {
2229 if (prev)
2230 prev->sf_next = dpsf->sf_next;
2231 else
2232 pmc->mca_tomb = dpsf->sf_next;
2233 kfree(dpsf);
2234 }
2235 psf->sf_crcount = qrv;
2236 rv++;
2237 }
2238 } else if (psf->sf_oldin) {
2239 psf->sf_crcount = 0;
2240 /*
2241 * add or update "delete" records if an active filter
2242 * is now inactive
2243 */
Ian Morris67ba4152014-08-24 21:53:10 +01002244 for (dpsf = pmc->mca_tomb; dpsf; dpsf = dpsf->sf_next)
David L Stevens7add2a42006-01-24 13:06:39 -08002245 if (ipv6_addr_equal(&dpsf->sf_addr,
2246 &psf->sf_addr))
2247 break;
2248 if (!dpsf) {
Joe Perches5d553542010-05-31 17:23:22 +00002249 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevens7add2a42006-01-24 13:06:39 -08002250 if (!dpsf)
2251 continue;
2252 *dpsf = *psf;
2253 /* pmc->mca_lock held by callers */
2254 dpsf->sf_next = pmc->mca_tomb;
2255 pmc->mca_tomb = dpsf;
2256 }
2257 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 rv++;
2259 }
2260 }
2261 return rv;
2262}
2263
2264/*
2265 * Add multicast source filter list to the interface list
2266 */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002267static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2268 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 int delta)
2270{
2271 struct ifmcaddr6 *pmc;
2272 int isexclude;
2273 int i, err;
2274
2275 if (!idev)
2276 return -ENODEV;
2277 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002278 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2280 break;
2281 }
2282 if (!pmc) {
2283 /* MCA not found?? bug */
2284 read_unlock_bh(&idev->lock);
2285 return -ESRCH;
2286 }
2287 spin_lock_bh(&pmc->mca_lock);
2288
2289 sf_markstate(pmc);
2290 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2291 if (!delta)
2292 pmc->mca_sfcount[sfmode]++;
2293 err = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01002294 for (i = 0; i < sfcount; i++) {
Jun Zhao99d2f472011-11-30 06:21:05 +00002295 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 if (err)
2297 break;
2298 }
2299 if (err) {
2300 int j;
2301
2302 if (!delta)
2303 pmc->mca_sfcount[sfmode]--;
Ian Morris67ba4152014-08-24 21:53:10 +01002304 for (j = 0; j < i; j++)
RongQing.Li78d50212012-04-04 16:47:04 +00002305 ip6_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 struct ip6_sf_list *psf;
2308
2309 /* filter mode change */
2310 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2311 pmc->mca_sfmode = MCAST_EXCLUDE;
2312 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2313 pmc->mca_sfmode = MCAST_INCLUDE;
2314 /* else no filters; keep old mode for reports */
2315
2316 pmc->mca_crcount = idev->mc_qrv;
2317 idev->mc_ifc_count = pmc->mca_crcount;
Ian Morris67ba4152014-08-24 21:53:10 +01002318 for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 psf->sf_crcount = 0;
2320 mld_ifc_event(idev);
2321 } else if (sf_setstate(pmc))
2322 mld_ifc_event(idev);
2323 spin_unlock_bh(&pmc->mca_lock);
2324 read_unlock_bh(&idev->lock);
2325 return err;
2326}
2327
2328static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2329{
2330 struct ip6_sf_list *psf, *nextpsf;
2331
Ian Morris67ba4152014-08-24 21:53:10 +01002332 for (psf = pmc->mca_tomb; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 nextpsf = psf->sf_next;
2334 kfree(psf);
2335 }
2336 pmc->mca_tomb = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01002337 for (psf = pmc->mca_sources; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 nextpsf = psf->sf_next;
2339 kfree(psf);
2340 }
2341 pmc->mca_sources = NULL;
2342 pmc->mca_sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002343 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2345}
2346
2347
2348static void igmp6_join_group(struct ifmcaddr6 *ma)
2349{
2350 unsigned long delay;
2351
2352 if (ma->mca_flags & MAF_NOREPORT)
2353 return;
2354
2355 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2356
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05002357 delay = prandom_u32() % unsolicited_report_interval(ma->idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
2359 spin_lock_bh(&ma->mca_lock);
2360 if (del_timer(&ma->mca_timer)) {
2361 atomic_dec(&ma->mca_refcnt);
2362 delay = ma->mca_timer.expires - jiffies;
2363 }
2364
2365 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2366 atomic_inc(&ma->mca_refcnt);
2367 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2368 spin_unlock_bh(&ma->mca_lock);
2369}
2370
2371static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2372 struct inet6_dev *idev)
2373{
2374 int err;
2375
David L Stevens5ab4a6c2005-12-27 14:03:00 -08002376 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2377 * so no other readers or writers of iml or its sflist
2378 */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002379 if (!iml->sflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 /* any-source empty exclude case */
2381 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2382 }
2383 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2384 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2385 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2386 iml->sflist = NULL;
2387 return err;
2388}
2389
2390static void igmp6_leave_group(struct ifmcaddr6 *ma)
2391{
Daniel Borkmann6c567b72013-09-04 00:19:38 +02002392 if (mld_in_v1_mode(ma->idev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if (ma->mca_flags & MAF_LAST_REPORTER)
2394 igmp6_send(&ma->mca_addr, ma->idev->dev,
2395 ICMPV6_MGM_REDUCTION);
2396 } else {
2397 mld_add_delrec(ma->idev, ma);
2398 mld_ifc_event(ma->idev);
2399 }
2400}
2401
2402static void mld_gq_timer_expire(unsigned long data)
2403{
2404 struct inet6_dev *idev = (struct inet6_dev *)data;
2405
2406 idev->mc_gq_running = 0;
2407 mld_send_report(idev, NULL);
Salam Noureddine9260d3e2013-09-29 13:41:34 -07002408 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409}
2410
2411static void mld_ifc_timer_expire(unsigned long data)
2412{
2413 struct inet6_dev *idev = (struct inet6_dev *)data;
2414
2415 mld_send_cr(idev);
2416 if (idev->mc_ifc_count) {
2417 idev->mc_ifc_count--;
2418 if (idev->mc_ifc_count)
2419 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2420 }
Salam Noureddine9260d3e2013-09-29 13:41:34 -07002421 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422}
2423
2424static void mld_ifc_event(struct inet6_dev *idev)
2425{
Daniel Borkmann6c567b72013-09-04 00:19:38 +02002426 if (mld_in_v1_mode(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 return;
2428 idev->mc_ifc_count = idev->mc_qrv;
2429 mld_ifc_start_timer(idev, 1);
2430}
2431
2432
2433static void igmp6_timer_handler(unsigned long data)
2434{
2435 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2436
Daniel Borkmann6c567b72013-09-04 00:19:38 +02002437 if (mld_in_v1_mode(ma->idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2439 else
2440 mld_send_report(ma->idev, ma);
2441
2442 spin_lock(&ma->mca_lock);
2443 ma->mca_flags |= MAF_LAST_REPORTER;
2444 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2445 spin_unlock(&ma->mca_lock);
2446 ma_put(ma);
2447}
2448
Moni Shoua75c78502009-09-15 02:37:40 -07002449/* Device changing type */
2450
2451void ipv6_mc_unmap(struct inet6_dev *idev)
2452{
2453 struct ifmcaddr6 *i;
2454
2455 /* Install multicast list, except for all-nodes (already installed) */
2456
2457 read_lock_bh(&idev->lock);
2458 for (i = idev->mc_list; i; i = i->next)
2459 igmp6_group_dropped(i);
2460 read_unlock_bh(&idev->lock);
2461}
2462
2463void ipv6_mc_remap(struct inet6_dev *idev)
2464{
2465 ipv6_mc_up(idev);
2466}
2467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468/* Device going down */
2469
2470void ipv6_mc_down(struct inet6_dev *idev)
2471{
2472 struct ifmcaddr6 *i;
2473
2474 /* Withdraw multicast list */
2475
2476 read_lock_bh(&idev->lock);
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02002477 mld_ifc_stop_timer(idev);
2478 mld_gq_stop_timer(idev);
2479 mld_dad_stop_timer(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Ian Morris67ba4152014-08-24 21:53:10 +01002481 for (i = idev->mc_list; i; i = i->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 igmp6_group_dropped(i);
2483 read_unlock_bh(&idev->lock);
2484
2485 mld_clear_delrec(idev);
2486}
2487
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02002488static void ipv6_mc_reset(struct inet6_dev *idev)
2489{
2490 idev->mc_qrv = sysctl_mld_qrv;
2491 idev->mc_qi = MLD_QI_DEFAULT;
2492 idev->mc_qri = MLD_QRI_DEFAULT;
2493 idev->mc_v1_seen = 0;
2494 idev->mc_maxdelay = unsolicited_report_interval(idev);
2495}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
2497/* Device going up */
2498
2499void ipv6_mc_up(struct inet6_dev *idev)
2500{
2501 struct ifmcaddr6 *i;
2502
2503 /* Install multicast list, except for all-nodes (already installed) */
2504
2505 read_lock_bh(&idev->lock);
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02002506 ipv6_mc_reset(idev);
Ian Morris67ba4152014-08-24 21:53:10 +01002507 for (i = idev->mc_list; i; i = i->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 igmp6_group_added(i);
2509 read_unlock_bh(&idev->lock);
2510}
2511
2512/* IPv6 device initialization. */
2513
2514void ipv6_mc_init_dev(struct inet6_dev *idev)
2515{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 write_lock_bh(&idev->lock);
Stephen Hemminger6457d262010-02-17 18:48:44 -08002517 spin_lock_init(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 idev->mc_gq_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002519 setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2520 (unsigned long)idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 idev->mc_tomb = NULL;
2522 idev->mc_ifc_count = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002523 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2524 (unsigned long)idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002525 setup_timer(&idev->mc_dad_timer, mld_dad_timer_expire,
2526 (unsigned long)idev);
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02002527 ipv6_mc_reset(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
2530
2531/*
2532 * Device is about to be destroyed: clean up.
2533 */
2534
2535void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2536{
2537 struct ifmcaddr6 *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 /* Deactivate timers */
2540 ipv6_mc_down(idev);
2541
2542 /* Delete all-nodes address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2544 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2545 * fail.
2546 */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002547 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002549 if (idev->cnf.forwarding)
2550 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
2552 write_lock_bh(&idev->lock);
2553 while ((i = idev->mc_list) != NULL) {
2554 idev->mc_list = i->next;
2555 write_unlock_bh(&idev->lock);
2556
2557 igmp6_group_dropped(i);
2558 ma_put(i);
2559
2560 write_lock_bh(&idev->lock);
2561 }
2562 write_unlock_bh(&idev->lock);
2563}
2564
2565#ifdef CONFIG_PROC_FS
2566struct igmp6_mc_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002567 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 struct net_device *dev;
2569 struct inet6_dev *idev;
2570};
2571
2572#define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2573
2574static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2575{
2576 struct ifmcaddr6 *im = NULL;
2577 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002578 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Pavel Emelianov7562f872007-05-03 15:13:45 -07002580 state->idev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002581 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 struct inet6_dev *idev;
Eric Dumazetce81b762009-11-11 17:34:30 +00002583 idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 if (!idev)
2585 continue;
2586 read_lock_bh(&idev->lock);
2587 im = idev->mc_list;
2588 if (im) {
2589 state->idev = idev;
2590 break;
2591 }
2592 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
2594 return im;
2595}
2596
2597static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2598{
2599 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2600
2601 im = im->next;
2602 while (!im) {
Eric Dumazetce81b762009-11-11 17:34:30 +00002603 if (likely(state->idev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 read_unlock_bh(&state->idev->lock);
Eric Dumazetce81b762009-11-11 17:34:30 +00002605
2606 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 if (!state->dev) {
2608 state->idev = NULL;
2609 break;
2610 }
Eric Dumazetce81b762009-11-11 17:34:30 +00002611 state->idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 if (!state->idev)
2613 continue;
2614 read_lock_bh(&state->idev->lock);
2615 im = state->idev->mc_list;
2616 }
2617 return im;
2618}
2619
2620static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2621{
2622 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2623 if (im)
2624 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2625 --pos;
2626 return pos ? NULL : im;
2627}
2628
2629static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetce81b762009-11-11 17:34:30 +00002630 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
Eric Dumazetce81b762009-11-11 17:34:30 +00002632 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 return igmp6_mc_get_idx(seq, *pos);
2634}
2635
2636static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2637{
Eric Dumazetce81b762009-11-11 17:34:30 +00002638 struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);
2639
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 ++*pos;
2641 return im;
2642}
2643
2644static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetce81b762009-11-11 17:34:30 +00002645 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646{
2647 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
Eric Dumazetce81b762009-11-11 17:34:30 +00002648
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 if (likely(state->idev != NULL)) {
2650 read_unlock_bh(&state->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 state->idev = NULL;
2652 }
2653 state->dev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002654 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655}
2656
2657static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2658{
2659 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2660 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2661
2662 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002663 "%-4d %-15s %pi6 %5d %08X %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 state->dev->ifindex, state->dev->name,
Harvey Harrisonb0711952008-10-28 16:05:40 -07002665 &im->mca_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 im->mca_users, im->mca_flags,
2667 (im->mca_flags&MAF_TIMER_RUNNING) ?
2668 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2669 return 0;
2670}
2671
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002672static const struct seq_operations igmp6_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 .start = igmp6_mc_seq_start,
2674 .next = igmp6_mc_seq_next,
2675 .stop = igmp6_mc_seq_stop,
2676 .show = igmp6_mc_seq_show,
2677};
2678
2679static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2680{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002681 return seq_open_net(inode, file, &igmp6_mc_seq_ops,
2682 sizeof(struct igmp6_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683}
2684
Arjan van de Ven9a321442007-02-12 00:55:35 -08002685static const struct file_operations igmp6_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 .owner = THIS_MODULE,
2687 .open = igmp6_mc_seq_open,
2688 .read = seq_read,
2689 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002690 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691};
2692
2693struct igmp6_mcf_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002694 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 struct net_device *dev;
2696 struct inet6_dev *idev;
2697 struct ifmcaddr6 *im;
2698};
2699
2700#define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2701
2702static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2703{
2704 struct ip6_sf_list *psf = NULL;
2705 struct ifmcaddr6 *im = NULL;
2706 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002707 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Pavel Emelianov7562f872007-05-03 15:13:45 -07002709 state->idev = NULL;
2710 state->im = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002711 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 struct inet6_dev *idev;
Eric Dumazetce81b762009-11-11 17:34:30 +00002713 idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 if (unlikely(idev == NULL))
2715 continue;
2716 read_lock_bh(&idev->lock);
2717 im = idev->mc_list;
2718 if (likely(im != NULL)) {
2719 spin_lock_bh(&im->mca_lock);
2720 psf = im->mca_sources;
2721 if (likely(psf != NULL)) {
2722 state->im = im;
2723 state->idev = idev;
2724 break;
2725 }
2726 spin_unlock_bh(&im->mca_lock);
2727 }
2728 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 }
2730 return psf;
2731}
2732
2733static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2734{
2735 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2736
2737 psf = psf->sf_next;
2738 while (!psf) {
2739 spin_unlock_bh(&state->im->mca_lock);
2740 state->im = state->im->next;
2741 while (!state->im) {
Eric Dumazetce81b762009-11-11 17:34:30 +00002742 if (likely(state->idev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 read_unlock_bh(&state->idev->lock);
Eric Dumazetce81b762009-11-11 17:34:30 +00002744
2745 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 if (!state->dev) {
2747 state->idev = NULL;
2748 goto out;
2749 }
Eric Dumazetce81b762009-11-11 17:34:30 +00002750 state->idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 if (!state->idev)
2752 continue;
2753 read_lock_bh(&state->idev->lock);
2754 state->im = state->idev->mc_list;
2755 }
2756 if (!state->im)
2757 break;
2758 spin_lock_bh(&state->im->mca_lock);
2759 psf = state->im->mca_sources;
2760 }
2761out:
2762 return psf;
2763}
2764
2765static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2766{
2767 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2768 if (psf)
2769 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2770 --pos;
2771 return pos ? NULL : psf;
2772}
2773
2774static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetce81b762009-11-11 17:34:30 +00002775 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776{
Eric Dumazetce81b762009-11-11 17:34:30 +00002777 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2779}
2780
2781static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2782{
2783 struct ip6_sf_list *psf;
2784 if (v == SEQ_START_TOKEN)
2785 psf = igmp6_mcf_get_first(seq);
2786 else
2787 psf = igmp6_mcf_get_next(seq, v);
2788 ++*pos;
2789 return psf;
2790}
2791
2792static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetce81b762009-11-11 17:34:30 +00002793 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
2795 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2796 if (likely(state->im != NULL)) {
2797 spin_unlock_bh(&state->im->mca_lock);
2798 state->im = NULL;
2799 }
2800 if (likely(state->idev != NULL)) {
2801 read_unlock_bh(&state->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 state->idev = NULL;
2803 }
2804 state->dev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002805 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806}
2807
2808static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2809{
2810 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2811 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2812
2813 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002814 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 "%3s %6s "
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002816 "%32s %32s %6s %6s\n", "Idx",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 "Device", "Multicast Address",
2818 "Source Address", "INC", "EXC");
2819 } else {
2820 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002821 "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 state->dev->ifindex, state->dev->name,
Harvey Harrisonb0711952008-10-28 16:05:40 -07002823 &state->im->mca_addr,
2824 &psf->sf_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 psf->sf_count[MCAST_INCLUDE],
2826 psf->sf_count[MCAST_EXCLUDE]);
2827 }
2828 return 0;
2829}
2830
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002831static const struct seq_operations igmp6_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 .start = igmp6_mcf_seq_start,
2833 .next = igmp6_mcf_seq_next,
2834 .stop = igmp6_mcf_seq_stop,
2835 .show = igmp6_mcf_seq_show,
2836};
2837
2838static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2839{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002840 return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
2841 sizeof(struct igmp6_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842}
2843
Arjan van de Ven9a321442007-02-12 00:55:35 -08002844static const struct file_operations igmp6_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 .owner = THIS_MODULE,
2846 .open = igmp6_mcf_seq_open,
2847 .read = seq_read,
2848 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002849 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850};
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002851
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002852static int __net_init igmp6_proc_init(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002853{
2854 int err;
2855
2856 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002857 if (!proc_create("igmp6", S_IRUGO, net->proc_net, &igmp6_mc_seq_fops))
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002858 goto out;
Gao fengd4beaa62013-02-18 01:34:54 +00002859 if (!proc_create("mcfilter6", S_IRUGO, net->proc_net,
2860 &igmp6_mcf_seq_fops))
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002861 goto out_proc_net_igmp6;
2862
2863 err = 0;
2864out:
2865 return err;
2866
2867out_proc_net_igmp6:
Gao fengece31ff2013-02-18 01:34:56 +00002868 remove_proc_entry("igmp6", net->proc_net);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002869 goto out;
2870}
2871
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002872static void __net_exit igmp6_proc_exit(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002873{
Gao fengece31ff2013-02-18 01:34:56 +00002874 remove_proc_entry("mcfilter6", net->proc_net);
2875 remove_proc_entry("igmp6", net->proc_net);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002876}
2877#else
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002878static inline int igmp6_proc_init(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002879{
2880 return 0;
2881}
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002882static inline void igmp6_proc_exit(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002883{
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002884}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885#endif
2886
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002887static int __net_init igmp6_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 int err;
2890
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002891 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2892 SOCK_RAW, IPPROTO_ICMPV6, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00002894 pr_err("Failed to initialize the IGMP6 control socket (err %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 err);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002896 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 }
2898
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002899 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002901 err = igmp6_proc_init(net);
2902 if (err)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002903 goto out_sock_create;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002904out:
2905 return err;
2906
2907out_sock_create:
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002908 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002909 goto out;
2910}
2911
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002912static void __net_exit igmp6_net_exit(struct net *net)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002913{
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002914 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002915 igmp6_proc_exit(net);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002916}
2917
2918static struct pernet_operations igmp6_net_ops = {
2919 .init = igmp6_net_init,
2920 .exit = igmp6_net_exit,
2921};
2922
2923int __init igmp6_init(void)
2924{
2925 return register_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926}
2927
2928void igmp6_cleanup(void)
2929{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002930 unregister_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931}