blob: 64919425f1abf3a17df5be25e1ceb52143aeb044 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Multicast support for IPv6
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09008 * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15
16/* Changes:
17 *
18 * yoshfuji : fix format of router-alert option
19 * YOSHIFUJI Hideaki @USAGI:
20 * Fixed source address for MLD message based on
21 * <draft-ietf-magma-mld-source-05.txt>.
22 * YOSHIFUJI Hideaki @USAGI:
23 * - Ignore Queries for invalid addresses.
24 * - MLD for link-local addresses.
25 * David L Stevens <dlstevens@us.ibm.com>:
26 * - MLDv2 support
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30#include <linux/errno.h>
31#include <linux/types.h>
32#include <linux/string.h>
33#include <linux/socket.h>
34#include <linux/sockios.h>
35#include <linux/jiffies.h>
36#include <linux/times.h>
37#include <linux/net.h>
38#include <linux/in.h>
39#include <linux/in6.h>
40#include <linux/netdevice.h>
41#include <linux/if_arp.h>
42#include <linux/route.h>
43#include <linux/init.h>
44#include <linux/proc_fs.h>
45#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +020047#include <linux/pkt_sched.h>
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +090048#include <net/mld.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <linux/netfilter.h>
51#include <linux/netfilter_ipv6.h>
52
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020053#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <net/sock.h>
55#include <net/snmp.h>
56
57#include <net/ipv6.h>
58#include <net/protocol.h>
59#include <net/if_inet6.h>
60#include <net/ndisc.h>
61#include <net/addrconf.h>
62#include <net/ip6_route.h>
Denis V. Lunev1ed85162008-04-03 14:31:03 -070063#include <net/inet_common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#include <net/ip6_checksum.h>
66
67/* Set to 3 to get tracing... */
68#define MCAST_DEBUG 2
69
70#if MCAST_DEBUG >= 3
71#define MDBG(x) printk x
72#else
73#define MDBG(x)
74#endif
75
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +090076/* Ensure that we have struct in6_addr aligned on 32bit word. */
77static void *__mld2_query_bugs[] __attribute__((__unused__)) = {
78 BUILD_BUG_ON_NULL(offsetof(struct mld2_query, mld2q_srcs) % 4),
79 BUILD_BUG_ON_NULL(offsetof(struct mld2_report, mld2r_grec) % 4),
80 BUILD_BUG_ON_NULL(offsetof(struct mld2_grec, grec_mca) % 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081};
82
83static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
84
85/* Big mc list lock for all the sockets */
Eric Dumazet456b61b2010-11-23 13:12:15 +000086static DEFINE_SPINLOCK(ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static void igmp6_join_group(struct ifmcaddr6 *ma);
89static void igmp6_leave_group(struct ifmcaddr6 *ma);
90static void igmp6_timer_handler(unsigned long data);
91
92static void mld_gq_timer_expire(unsigned long data);
93static void mld_ifc_timer_expire(unsigned long data);
94static void mld_ifc_event(struct inet6_dev *idev);
95static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
Eric Dumazetb71d1d42011-04-22 04:53:02 +000096static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static void mld_clear_delrec(struct inet6_dev *idev);
Daniel Borkmann6c567b72013-09-04 00:19:38 +020098static bool mld_in_v1_mode(const struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static int sf_setstate(struct ifmcaddr6 *pmc);
100static void sf_markstate(struct ifmcaddr6 *pmc);
101static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000102static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
103 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 int delta);
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000105static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
106 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 int delta);
108static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
109 struct inet6_dev *idev);
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#define MLD_QRV_DEFAULT 2
Daniel Borkmann89225d12013-09-04 00:19:37 +0200112/* RFC3810, 9.2. Query Interval */
113#define MLD_QI_DEFAULT (125 * HZ)
114/* RFC3810, 9.3. Query Response Interval */
115#define MLD_QRI_DEFAULT (10 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Daniel Borkmann9fd07842013-08-20 12:22:02 +0200117/* RFC3810, 8.1 Query Version Distinctions */
118#define MLD_V1_QUERY_LEN 24
119#define MLD_V2_QUERY_LEN_MIN 28
120
David L Stevens6f4353d2005-12-26 17:03:46 -0800121#define IPV6_MLD_MAX_MSF 64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Brian Haleyab32ea52006-09-22 14:15:41 -0700123int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +0200124int sysctl_mld_qrv __read_mostly = MLD_QRV_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/*
127 * socket join on multicast group
128 */
129
Eric Dumazet456b61b2010-11-23 13:12:15 +0000130#define for_each_pmc_rcu(np, pmc) \
131 for (pmc = rcu_dereference(np->ipv6_mc_list); \
132 pmc != NULL; \
133 pmc = rcu_dereference(pmc->next))
134
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200135static int unsolicited_report_interval(struct inet6_dev *idev)
136{
137 int iv;
138
Daniel Borkmann6c567b72013-09-04 00:19:38 +0200139 if (mld_in_v1_mode(idev))
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200140 iv = idev->cnf.mldv1_unsolicited_report_interval;
141 else
142 iv = idev->cnf.mldv2_unsolicited_report_interval;
143
144 return iv > 0 ? iv : 1;
145}
146
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900147int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 struct net_device *dev = NULL;
150 struct ipv6_mc_socklist *mc_lst;
151 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900152 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int err;
154
155 if (!ipv6_addr_is_multicast(addr))
156 return -EINVAL;
157
Eric Dumazet456b61b2010-11-23 13:12:15 +0000158 rcu_read_lock();
159 for_each_pmc_rcu(np, mc_lst) {
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700160 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
161 ipv6_addr_equal(&mc_lst->addr, addr)) {
Eric Dumazet456b61b2010-11-23 13:12:15 +0000162 rcu_read_unlock();
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700163 return -EADDRINUSE;
164 }
165 }
Eric Dumazet456b61b2010-11-23 13:12:15 +0000166 rcu_read_unlock();
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
169
170 if (mc_lst == NULL)
171 return -ENOMEM;
172
173 mc_lst->next = NULL;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000174 mc_lst->addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Eric Dumazet96b52e62010-06-07 21:05:02 +0000176 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (ifindex == 0) {
178 struct rt6_info *rt;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800179 rt = rt6_lookup(net, addr, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (rt) {
David S. Millerd1918542011-12-28 20:19:20 -0500181 dev = rt->dst.dev;
Amerigo Wang94e187c2012-10-29 00:13:19 +0000182 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184 } else
Eric Dumazet96b52e62010-06-07 21:05:02 +0000185 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 if (dev == NULL) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000188 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
190 return -ENODEV;
191 }
192
193 mc_lst->ifindex = dev->ifindex;
194 mc_lst->sfmode = MCAST_EXCLUDE;
YOSHIFUJI Hideaki196433c2006-01-04 13:56:31 -0800195 rwlock_init(&mc_lst->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 mc_lst->sflist = NULL;
197
198 /*
199 * now add/increase the group membership on the device
200 */
201
202 err = ipv6_dev_mc_inc(dev, addr);
203
204 if (err) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000205 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return err;
208 }
209
Eric Dumazet456b61b2010-11-23 13:12:15 +0000210 spin_lock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 mc_lst->next = np->ipv6_mc_list;
Eric Dumazet456b61b2010-11-23 13:12:15 +0000212 rcu_assign_pointer(np->ipv6_mc_list, mc_lst);
213 spin_unlock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Eric Dumazet96b52e62010-06-07 21:05:02 +0000215 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 return 0;
218}
219
220/*
221 * socket leave on multicast group
222 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900223int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 struct ipv6_pinfo *np = inet6_sk(sk);
Eric Dumazet456b61b2010-11-23 13:12:15 +0000226 struct ipv6_mc_socklist *mc_lst;
227 struct ipv6_mc_socklist __rcu **lnk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900228 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Li Weia858d642012-07-17 15:28:59 +0800230 if (!ipv6_addr_is_multicast(addr))
231 return -EINVAL;
232
Eric Dumazet456b61b2010-11-23 13:12:15 +0000233 spin_lock(&ipv6_sk_mc_lock);
234 for (lnk = &np->ipv6_mc_list;
235 (mc_lst = rcu_dereference_protected(*lnk,
Ian Morris67ba4152014-08-24 21:53:10 +0100236 lockdep_is_held(&ipv6_sk_mc_lock))) != NULL;
Eric Dumazet456b61b2010-11-23 13:12:15 +0000237 lnk = &mc_lst->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
239 ipv6_addr_equal(&mc_lst->addr, addr)) {
240 struct net_device *dev;
241
242 *lnk = mc_lst->next;
Eric Dumazet456b61b2010-11-23 13:12:15 +0000243 spin_unlock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Eric Dumazet96b52e62010-06-07 21:05:02 +0000245 rcu_read_lock();
246 dev = dev_get_by_index_rcu(net, mc_lst->ifindex);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800247 if (dev != NULL) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000248 struct inet6_dev *idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
David L Stevensacd6e002006-08-17 16:27:39 -0700250 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000251 if (idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
David L Stevensacd6e002006-08-17 16:27:39 -0700253 } else
254 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000255 rcu_read_unlock();
Eric Dumazet456b61b2010-11-23 13:12:15 +0000256 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
Lai Jiangshane3cbf282011-03-18 12:00:50 +0800257 kfree_rcu(mc_lst, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return 0;
259 }
260 }
Eric Dumazet456b61b2010-11-23 13:12:15 +0000261 spin_unlock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
David L Stevens9951f032005-07-08 17:47:28 -0700263 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Eric Dumazet96b52e62010-06-07 21:05:02 +0000266/* called with rcu_read_lock() */
267static struct inet6_dev *ip6_mc_find_dev_rcu(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000268 const struct in6_addr *group,
Eric Dumazet96b52e62010-06-07 21:05:02 +0000269 int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 struct net_device *dev = NULL;
272 struct inet6_dev *idev = NULL;
273
274 if (ifindex == 0) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000275 struct rt6_info *rt = rt6_lookup(net, group, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (rt) {
David S. Millerd1918542011-12-28 20:19:20 -0500278 dev = rt->dst.dev;
Amerigo Wang94e187c2012-10-29 00:13:19 +0000279 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281 } else
Eric Dumazet96b52e62010-06-07 21:05:02 +0000282 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 if (!dev)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000285 return NULL;
286 idev = __in6_dev_get(dev);
Ilpo Järvinen448eb712008-12-14 23:15:21 -0800287 if (!idev)
Joe Perches8a22c992010-11-14 17:05:00 +0000288 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 read_lock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000290 if (idev->dead) {
291 read_unlock_bh(&idev->lock);
292 return NULL;
293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return idev;
295}
296
297void ipv6_sock_mc_close(struct sock *sk)
298{
299 struct ipv6_pinfo *np = inet6_sk(sk);
300 struct ipv6_mc_socklist *mc_lst;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900301 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Eric Dumazet0e1efe92012-12-05 09:18:10 +0000303 if (!rcu_access_pointer(np->ipv6_mc_list))
304 return;
305
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
334int ip6_mc_source(int add, int omode, struct sock *sk,
335 struct group_source_req *pgsr)
336{
337 struct in6_addr *source, *group;
338 struct ipv6_mc_socklist *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 struct inet6_dev *idev;
340 struct ipv6_pinfo *inet6 = inet6_sk(sk);
341 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900342 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 int i, j, rv;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700344 int leavegroup = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800345 int pmclocked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 int err;
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
349 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
350
351 if (!ipv6_addr_is_multicast(group))
352 return -EINVAL;
353
Eric Dumazet96b52e62010-06-07 21:05:02 +0000354 rcu_read_lock();
355 idev = ip6_mc_find_dev_rcu(net, group, pgsr->gsr_interface);
356 if (!idev) {
357 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return -ENODEV;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 err = -EADDRNOTAVAIL;
362
Eric Dumazet456b61b2010-11-23 13:12:15 +0000363 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
365 continue;
366 if (ipv6_addr_equal(&pmc->addr, group))
367 break;
368 }
David L Stevens917f2f12005-07-08 17:45:16 -0700369 if (!pmc) { /* must have a prior join */
370 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /* if a source filter was set, must be the same mode as before */
374 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -0700375 if (pmc->sfmode != omode) {
376 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 } else if (pmc->sfmode != omode) {
380 /* allow mode switches for empty-set filters */
381 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
382 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
383 pmc->sfmode = omode;
384 }
385
Eric Dumazet96b52e62010-06-07 21:05:02 +0000386 write_lock(&pmc->sflock);
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800387 pmclocked = 1;
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 psl = pmc->sflist;
390 if (!add) {
391 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -0700392 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 rv = !0;
Ian Morris67ba4152014-08-24 21:53:10 +0100394 for (i = 0; i < psl->sl_count; i++) {
YOSHIFUJI Hideaki / 吉藤英明07c2fec2013-01-29 12:48:23 +0000395 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (rv == 0)
397 break;
398 }
399 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -0700400 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700402 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
403 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
404 leavegroup = 1;
405 goto done;
406 }
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /* update the interface filter */
409 ip6_mc_del_src(idev, group, omode, 1, source, 1);
410
Ian Morris67ba4152014-08-24 21:53:10 +0100411 for (j = i+1; j < psl->sl_count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 psl->sl_addr[j-1] = psl->sl_addr[j];
413 psl->sl_count--;
414 err = 0;
415 goto done;
416 }
417 /* else, add a new source to the filter */
418
419 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
420 err = -ENOBUFS;
421 goto done;
422 }
423 if (!psl || psl->sl_count == psl->sl_max) {
424 struct ip6_sf_socklist *newpsl;
425 int count = IP6_SFBLOCK;
426
427 if (psl)
428 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800429 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (!newpsl) {
431 err = -ENOBUFS;
432 goto done;
433 }
434 newpsl->sl_max = count;
435 newpsl->sl_count = count - IP6_SFBLOCK;
436 if (psl) {
Ian Morris67ba4152014-08-24 21:53:10 +0100437 for (i = 0; i < psl->sl_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 newpsl->sl_addr[i] = psl->sl_addr[i];
439 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
440 }
441 pmc->sflist = psl = newpsl;
442 }
443 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
Ian Morris67ba4152014-08-24 21:53:10 +0100444 for (i = 0; i < psl->sl_count; i++) {
YOSHIFUJI Hideaki / 吉藤英明07c2fec2013-01-29 12:48:23 +0000445 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
Jean Sacren56db1c52013-02-03 21:34:10 +0000446 if (rv == 0) /* There is an error in the address. */
447 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
Ian Morris67ba4152014-08-24 21:53:10 +0100449 for (j = psl->sl_count-1; j >= i; j--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 psl->sl_addr[j+1] = psl->sl_addr[j];
451 psl->sl_addr[i] = *source;
452 psl->sl_count++;
453 err = 0;
454 /* update the interface list */
455 ip6_mc_add_src(idev, group, omode, 1, source, 1);
456done:
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800457 if (pmclocked)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000458 write_unlock(&pmc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000460 rcu_read_unlock();
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700461 if (leavegroup)
462 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return err;
464}
465
466int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
467{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000468 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 struct ipv6_mc_socklist *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 struct inet6_dev *idev;
471 struct ipv6_pinfo *inet6 = inet6_sk(sk);
472 struct ip6_sf_socklist *newpsl, *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900473 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -0700474 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 int i, err;
476
477 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
478
479 if (!ipv6_addr_is_multicast(group))
480 return -EINVAL;
481 if (gsf->gf_fmode != MCAST_INCLUDE &&
482 gsf->gf_fmode != MCAST_EXCLUDE)
483 return -EINVAL;
484
Eric Dumazet96b52e62010-06-07 21:05:02 +0000485 rcu_read_lock();
486 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Eric Dumazet96b52e62010-06-07 21:05:02 +0000488 if (!idev) {
489 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return -ENODEV;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
David S. Miller9c059892005-07-08 21:44:39 -0700493 err = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800494
David L Stevens9951f032005-07-08 17:47:28 -0700495 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
496 leavegroup = 1;
497 goto done;
498 }
499
Eric Dumazet456b61b2010-11-23 13:12:15 +0000500 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (pmc->ifindex != gsf->gf_interface)
502 continue;
503 if (ipv6_addr_equal(&pmc->addr, group))
504 break;
505 }
David L Stevens917f2f12005-07-08 17:45:16 -0700506 if (!pmc) { /* must have a prior join */
507 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (gsf->gf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800511 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
512 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (!newpsl) {
514 err = -ENOBUFS;
515 goto done;
516 }
517 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
Ian Morris67ba4152014-08-24 21:53:10 +0100518 for (i = 0; i < newpsl->sl_count; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 struct sockaddr_in6 *psin6;
520
521 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
522 newpsl->sl_addr[i] = psin6->sin6_addr;
523 }
524 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
525 newpsl->sl_count, newpsl->sl_addr, 0);
526 if (err) {
527 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
528 goto done;
529 }
Yan Zheng8713dbf2005-10-28 08:02:08 +0800530 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +0800532 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
533 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800534
Eric Dumazet96b52e62010-06-07 21:05:02 +0000535 write_lock(&pmc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 psl = pmc->sflist;
537 if (psl) {
538 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
539 psl->sl_count, psl->sl_addr, 0);
540 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
541 } else
542 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
543 pmc->sflist = newpsl;
544 pmc->sfmode = gsf->gf_fmode;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000545 write_unlock(&pmc->sflock);
David L Stevens917f2f12005-07-08 17:45:16 -0700546 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547done:
548 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000549 rcu_read_unlock();
David L Stevens9951f032005-07-08 17:47:28 -0700550 if (leavegroup)
551 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return err;
553}
554
555int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
556 struct group_filter __user *optval, int __user *optlen)
557{
558 int err, i, count, copycount;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000559 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 struct ipv6_mc_socklist *pmc;
561 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 struct ipv6_pinfo *inet6 = inet6_sk(sk);
563 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900564 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
567
568 if (!ipv6_addr_is_multicast(group))
569 return -EINVAL;
570
Eric Dumazet96b52e62010-06-07 21:05:02 +0000571 rcu_read_lock();
572 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Eric Dumazet96b52e62010-06-07 21:05:02 +0000574 if (!idev) {
575 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return -ENODEV;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 err = -EADDRNOTAVAIL;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800580 /*
581 * changes to the ipv6_mc_list require the socket lock and
582 * a read lock on ip6_sk_mc_lock. We have the socket lock,
583 * so reading the list is safe.
584 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Eric Dumazet456b61b2010-11-23 13:12:15 +0000586 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (pmc->ifindex != gsf->gf_interface)
588 continue;
589 if (ipv6_addr_equal(group, &pmc->addr))
590 break;
591 }
592 if (!pmc) /* must have a prior join */
593 goto done;
594 gsf->gf_fmode = pmc->sfmode;
595 psl = pmc->sflist;
596 count = psl ? psl->sl_count : 0;
597 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000598 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
601 gsf->gf_numsrc = count;
602 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
603 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
604 return -EFAULT;
605 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800606 /* changes to psl require the socket lock, a read lock on
607 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
608 * have the socket lock, so reading here is safe.
609 */
Ian Morris67ba4152014-08-24 21:53:10 +0100610 for (i = 0; i < copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 struct sockaddr_in6 *psin6;
612 struct sockaddr_storage ss;
613
614 psin6 = (struct sockaddr_in6 *)&ss;
615 memset(&ss, 0, sizeof(ss));
616 psin6->sin6_family = AF_INET6;
617 psin6->sin6_addr = psl->sl_addr[i];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900618 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return -EFAULT;
620 }
621 return 0;
622done:
623 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000624 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return err;
626}
627
Eric Dumazeta50feda2012-05-18 18:57:34 +0000628bool inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
629 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 struct ipv6_pinfo *np = inet6_sk(sk);
632 struct ipv6_mc_socklist *mc;
633 struct ip6_sf_socklist *psl;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000634 bool rv = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Eric Dumazet456b61b2010-11-23 13:12:15 +0000636 rcu_read_lock();
637 for_each_pmc_rcu(np, mc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (ipv6_addr_equal(&mc->addr, mc_addr))
639 break;
640 }
641 if (!mc) {
Eric Dumazet456b61b2010-11-23 13:12:15 +0000642 rcu_read_unlock();
Eric Dumazeta50feda2012-05-18 18:57:34 +0000643 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800645 read_lock(&mc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 psl = mc->sflist;
647 if (!psl) {
648 rv = mc->sfmode == MCAST_EXCLUDE;
649 } else {
650 int i;
651
Ian Morris67ba4152014-08-24 21:53:10 +0100652 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
654 break;
655 }
656 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000657 rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000659 rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800661 read_unlock(&mc->sflock);
Eric Dumazet456b61b2010-11-23 13:12:15 +0000662 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 return rv;
665}
666
667static void ma_put(struct ifmcaddr6 *mc)
668{
669 if (atomic_dec_and_test(&mc->mca_refcnt)) {
670 in6_dev_put(mc->idev);
671 kfree(mc);
672 }
673}
674
675static void igmp6_group_added(struct ifmcaddr6 *mc)
676{
677 struct net_device *dev = mc->idev->dev;
678 char buf[MAX_ADDR_LEN];
679
YOSHIFUJI Hideaki / 吉藤英明ec16ef22013-02-09 04:29:58 +0000680 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
681 IPV6_ADDR_SCOPE_LINKLOCAL)
682 return;
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 spin_lock_bh(&mc->mca_lock);
685 if (!(mc->mca_flags&MAF_LOADED)) {
686 mc->mca_flags |= MAF_LOADED;
687 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000688 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690 spin_unlock_bh(&mc->mca_lock);
691
692 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
693 return;
694
Daniel Borkmann6c567b72013-09-04 00:19:38 +0200695 if (mld_in_v1_mode(mc->idev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 igmp6_join_group(mc);
697 return;
698 }
699 /* else v2 */
700
701 mc->mca_crcount = mc->idev->mc_qrv;
702 mld_ifc_event(mc->idev);
703}
704
705static void igmp6_group_dropped(struct ifmcaddr6 *mc)
706{
707 struct net_device *dev = mc->idev->dev;
708 char buf[MAX_ADDR_LEN];
709
YOSHIFUJI Hideaki / 吉藤英明ec16ef22013-02-09 04:29:58 +0000710 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
711 IPV6_ADDR_SCOPE_LINKLOCAL)
712 return;
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 spin_lock_bh(&mc->mca_lock);
715 if (mc->mca_flags&MAF_LOADED) {
716 mc->mca_flags &= ~MAF_LOADED;
717 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000718 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720
721 if (mc->mca_flags & MAF_NOREPORT)
722 goto done;
723 spin_unlock_bh(&mc->mca_lock);
724
725 if (!mc->idev->dead)
726 igmp6_leave_group(mc);
727
728 spin_lock_bh(&mc->mca_lock);
729 if (del_timer(&mc->mca_timer))
730 atomic_dec(&mc->mca_refcnt);
731done:
732 ip6_mc_clear_src(mc);
733 spin_unlock_bh(&mc->mca_lock);
734}
735
736/*
737 * deleted ifmcaddr6 manipulation
738 */
739static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
740{
741 struct ifmcaddr6 *pmc;
742
743 /* this is an "ifmcaddr6" for convenience; only the fields below
744 * are actually used. In particular, the refcnt and users are not
745 * used for management of the delete list. Using the same structure
746 * for deleted items allows change reports to use common code with
747 * non-deleted or query-response MCA's.
748 */
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800749 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (!pmc)
751 return;
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 spin_lock_bh(&im->mca_lock);
754 spin_lock_init(&pmc->mca_lock);
755 pmc->idev = im->idev;
756 in6_dev_hold(idev);
757 pmc->mca_addr = im->mca_addr;
758 pmc->mca_crcount = idev->mc_qrv;
759 pmc->mca_sfmode = im->mca_sfmode;
760 if (pmc->mca_sfmode == MCAST_INCLUDE) {
761 struct ip6_sf_list *psf;
762
763 pmc->mca_tomb = im->mca_tomb;
764 pmc->mca_sources = im->mca_sources;
765 im->mca_tomb = im->mca_sources = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +0100766 for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 psf->sf_crcount = pmc->mca_crcount;
768 }
769 spin_unlock_bh(&im->mca_lock);
770
Stephen Hemminger6457d262010-02-17 18:48:44 -0800771 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 pmc->next = idev->mc_tomb;
773 idev->mc_tomb = pmc;
Stephen Hemminger6457d262010-02-17 18:48:44 -0800774 spin_unlock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000777static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *pmca)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 struct ifmcaddr6 *pmc, *pmc_prev;
780 struct ip6_sf_list *psf, *psf_next;
781
Stephen Hemminger6457d262010-02-17 18:48:44 -0800782 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 pmc_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +0100784 for (pmc = idev->mc_tomb; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
786 break;
787 pmc_prev = pmc;
788 }
789 if (pmc) {
790 if (pmc_prev)
791 pmc_prev->next = pmc->next;
792 else
793 idev->mc_tomb = pmc->next;
794 }
Stephen Hemminger6457d262010-02-17 18:48:44 -0800795 spin_unlock_bh(&idev->mc_lock);
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (pmc) {
Ian Morris67ba4152014-08-24 21:53:10 +0100798 for (psf = pmc->mca_tomb; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 psf_next = psf->sf_next;
800 kfree(psf);
801 }
802 in6_dev_put(pmc->idev);
803 kfree(pmc);
804 }
805}
806
807static void mld_clear_delrec(struct inet6_dev *idev)
808{
809 struct ifmcaddr6 *pmc, *nextpmc;
810
Stephen Hemminger6457d262010-02-17 18:48:44 -0800811 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 pmc = idev->mc_tomb;
813 idev->mc_tomb = NULL;
Stephen Hemminger6457d262010-02-17 18:48:44 -0800814 spin_unlock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 for (; pmc; pmc = nextpmc) {
817 nextpmc = pmc->next;
818 ip6_mc_clear_src(pmc);
819 in6_dev_put(pmc->idev);
820 kfree(pmc);
821 }
822
823 /* clear dead sources, too */
824 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100825 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 struct ip6_sf_list *psf, *psf_next;
827
828 spin_lock_bh(&pmc->mca_lock);
829 psf = pmc->mca_tomb;
830 pmc->mca_tomb = NULL;
831 spin_unlock_bh(&pmc->mca_lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100832 for (; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 psf_next = psf->sf_next;
834 kfree(psf);
835 }
836 }
837 read_unlock_bh(&idev->lock);
838}
839
840
841/*
842 * device multicast group inc (add if not found)
843 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900844int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
846 struct ifmcaddr6 *mc;
847 struct inet6_dev *idev;
848
Eric Dumazet96b52e62010-06-07 21:05:02 +0000849 /* we need to take a reference on idev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 idev = in6_dev_get(dev);
851
852 if (idev == NULL)
853 return -EINVAL;
854
855 write_lock_bh(&idev->lock);
856 if (idev->dead) {
857 write_unlock_bh(&idev->lock);
858 in6_dev_put(idev);
859 return -ENODEV;
860 }
861
862 for (mc = idev->mc_list; mc; mc = mc->next) {
863 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
864 mc->mca_users++;
865 write_unlock_bh(&idev->lock);
866 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
867 NULL, 0);
868 in6_dev_put(idev);
869 return 0;
870 }
871 }
872
873 /*
874 * not found: create a new one.
875 */
876
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800877 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 if (mc == NULL) {
880 write_unlock_bh(&idev->lock);
881 in6_dev_put(idev);
882 return -ENOMEM;
883 }
884
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800885 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000887 mc->mca_addr = *addr;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000888 mc->idev = idev; /* (reference taken) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 mc->mca_users = 1;
890 /* mca_stamp should be updated upon changes */
891 mc->mca_cstamp = mc->mca_tstamp = jiffies;
892 atomic_set(&mc->mca_refcnt, 2);
893 spin_lock_init(&mc->mca_lock);
894
895 /* initial mode is (EX, empty) */
896 mc->mca_sfmode = MCAST_EXCLUDE;
897 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
898
899 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
900 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
901 mc->mca_flags |= MAF_NOREPORT;
902
903 mc->next = idev->mc_list;
904 idev->mc_list = mc;
905 write_unlock_bh(&idev->lock);
906
907 mld_del_delrec(idev, &mc->mca_addr);
908 igmp6_group_added(mc);
909 ma_put(mc);
910 return 0;
911}
912
913/*
914 * device multicast group del
915 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900916int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 struct ifmcaddr6 *ma, **map;
919
920 write_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100921 for (map = &idev->mc_list; (ma = *map) != NULL; map = &ma->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
923 if (--ma->mca_users == 0) {
924 *map = ma->next;
925 write_unlock_bh(&idev->lock);
926
927 igmp6_group_dropped(ma);
928
929 ma_put(ma);
930 return 0;
931 }
932 write_unlock_bh(&idev->lock);
933 return 0;
934 }
935 }
936 write_unlock_bh(&idev->lock);
937
938 return -ENOENT;
939}
940
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900941int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Eric Dumazet96b52e62010-06-07 21:05:02 +0000943 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 int err;
945
Eric Dumazet96b52e62010-06-07 21:05:02 +0000946 rcu_read_lock();
947
948 idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (!idev)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000950 err = -ENODEV;
951 else
952 err = __ipv6_dev_mc_dec(idev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Eric Dumazet96b52e62010-06-07 21:05:02 +0000954 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return err;
956}
957
958/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 * check if the interface/address pair is valid
960 */
Eric Dumazeta50feda2012-05-18 18:57:34 +0000961bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
962 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
964 struct inet6_dev *idev;
965 struct ifmcaddr6 *mc;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000966 bool rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Eric Dumazet96b52e62010-06-07 21:05:02 +0000968 rcu_read_lock();
969 idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (idev) {
971 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100972 for (mc = idev->mc_list; mc; mc = mc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (ipv6_addr_equal(&mc->mca_addr, group))
974 break;
975 }
976 if (mc) {
977 if (src_addr && !ipv6_addr_any(src_addr)) {
978 struct ip6_sf_list *psf;
979
980 spin_lock_bh(&mc->mca_lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100981 for (psf = mc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
983 break;
984 }
985 if (psf)
986 rv = psf->sf_count[MCAST_INCLUDE] ||
987 psf->sf_count[MCAST_EXCLUDE] !=
988 mc->mca_sfcount[MCAST_EXCLUDE];
989 else
Ian Morris67ba4152014-08-24 21:53:10 +0100990 rv = mc->mca_sfcount[MCAST_EXCLUDE] != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 spin_unlock_bh(&mc->mca_lock);
992 } else
Eric Dumazeta50feda2012-05-18 18:57:34 +0000993 rv = true; /* don't filter unspecified source */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
Eric Dumazet96b52e62010-06-07 21:05:02 +0000997 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return rv;
999}
1000
1001static void mld_gq_start_timer(struct inet6_dev *idev)
1002{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001003 unsigned long tv = prandom_u32() % idev->mc_maxdelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 idev->mc_gq_running = 1;
1006 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1007 in6_dev_hold(idev);
1008}
1009
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001010static void mld_gq_stop_timer(struct inet6_dev *idev)
1011{
1012 idev->mc_gq_running = 0;
1013 if (del_timer(&idev->mc_gq_timer))
1014 __in6_dev_put(idev);
1015}
1016
Daniel Borkmannc2cef4e2013-08-20 12:22:01 +02001017static void mld_ifc_start_timer(struct inet6_dev *idev, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001019 unsigned long tv = prandom_u32() % delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1022 in6_dev_hold(idev);
1023}
1024
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001025static void mld_ifc_stop_timer(struct inet6_dev *idev)
1026{
1027 idev->mc_ifc_count = 0;
1028 if (del_timer(&idev->mc_ifc_timer))
1029 __in6_dev_put(idev);
1030}
1031
Daniel Borkmannc2cef4e2013-08-20 12:22:01 +02001032static void mld_dad_start_timer(struct inet6_dev *idev, unsigned long delay)
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02001033{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001034 unsigned long tv = prandom_u32() % delay;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02001035
1036 if (!mod_timer(&idev->mc_dad_timer, jiffies+tv+2))
1037 in6_dev_hold(idev);
1038}
1039
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001040static void mld_dad_stop_timer(struct inet6_dev *idev)
1041{
1042 if (del_timer(&idev->mc_dad_timer))
1043 __in6_dev_put(idev);
1044}
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046/*
1047 * IGMP handling (alias multicast ICMPv6 messages)
1048 */
1049
1050static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1051{
1052 unsigned long delay = resptime;
1053
1054 /* Do not start timer for these addresses */
1055 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1056 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1057 return;
1058
1059 if (del_timer(&ma->mca_timer)) {
1060 atomic_dec(&ma->mca_refcnt);
1061 delay = ma->mca_timer.expires - jiffies;
1062 }
1063
Daniel Borkmanncc7f7ab2013-09-04 00:19:41 +02001064 if (delay >= resptime)
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001065 delay = prandom_u32() % resptime;
Daniel Borkmanncc7f7ab2013-09-04 00:19:41 +02001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 ma->mca_timer.expires = jiffies + delay;
1068 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1069 atomic_inc(&ma->mca_refcnt);
1070 ma->mca_flags |= MAF_TIMER_RUNNING;
1071}
1072
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001073/* mark EXCLUDE-mode sources */
Eric Dumazeta50feda2012-05-18 18:57:34 +00001074static bool mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1075 const struct in6_addr *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 struct ip6_sf_list *psf;
1078 int i, scount;
1079
1080 scount = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01001081 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (scount == nsrcs)
1083 break;
Ian Morris67ba4152014-08-24 21:53:10 +01001084 for (i = 0; i < nsrcs; i++) {
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001085 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +00001086 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001087 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1088 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +08001089 break;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001090 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1091 scount++;
1092 break;
1093 }
1094 }
1095 }
1096 pmc->mca_flags &= ~MAF_GSQUERY;
1097 if (scount == nsrcs) /* all sources excluded */
Eric Dumazeta50feda2012-05-18 18:57:34 +00001098 return false;
1099 return true;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001100}
1101
Eric Dumazeta50feda2012-05-18 18:57:34 +00001102static bool mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1103 const struct in6_addr *srcs)
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001104{
1105 struct ip6_sf_list *psf;
1106 int i, scount;
1107
1108 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1109 return mld_xmarksources(pmc, nsrcs, srcs);
1110
1111 /* mark INCLUDE-mode sources */
1112
1113 scount = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01001114 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001115 if (scount == nsrcs)
1116 break;
Ian Morris67ba4152014-08-24 21:53:10 +01001117 for (i = 0; i < nsrcs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1119 psf->sf_gsresp = 1;
1120 scount++;
1121 break;
1122 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001125 if (!scount) {
1126 pmc->mca_flags &= ~MAF_GSQUERY;
Eric Dumazeta50feda2012-05-18 18:57:34 +00001127 return false;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001128 }
1129 pmc->mca_flags |= MAF_GSQUERY;
Eric Dumazeta50feda2012-05-18 18:57:34 +00001130 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
Daniel Borkmann58c0ecf2013-09-04 00:19:40 +02001133static int mld_force_mld_version(const struct inet6_dev *idev)
1134{
1135 /* Normally, both are 0 here. If enforcement to a particular is
1136 * being used, individual device enforcement will have a lower
1137 * precedence over 'all' device (.../conf/all/force_mld_version).
1138 */
1139
1140 if (dev_net(idev->dev)->ipv6.devconf_all->force_mld_version != 0)
1141 return dev_net(idev->dev)->ipv6.devconf_all->force_mld_version;
1142 else
1143 return idev->cnf.force_mld_version;
1144}
1145
1146static bool mld_in_v2_mode_only(const struct inet6_dev *idev)
1147{
1148 return mld_force_mld_version(idev) == 2;
1149}
1150
1151static bool mld_in_v1_mode_only(const struct inet6_dev *idev)
1152{
1153 return mld_force_mld_version(idev) == 1;
1154}
1155
Daniel Borkmann6c567b72013-09-04 00:19:38 +02001156static bool mld_in_v1_mode(const struct inet6_dev *idev)
1157{
Daniel Borkmann58c0ecf2013-09-04 00:19:40 +02001158 if (mld_in_v2_mode_only(idev))
1159 return false;
1160 if (mld_in_v1_mode_only(idev))
Daniel Borkmann6c567b72013-09-04 00:19:38 +02001161 return true;
1162 if (idev->mc_v1_seen && time_before(jiffies, idev->mc_v1_seen))
1163 return true;
1164
1165 return false;
1166}
1167
Daniel Borkmann89225d12013-09-04 00:19:37 +02001168static void mld_set_v1_mode(struct inet6_dev *idev)
1169{
1170 /* RFC3810, relevant sections:
1171 * - 9.1. Robustness Variable
1172 * - 9.2. Query Interval
1173 * - 9.3. Query Response Interval
1174 * - 9.12. Older Version Querier Present Timeout
1175 */
1176 unsigned long switchback;
1177
1178 switchback = (idev->mc_qrv * idev->mc_qi) + idev->mc_qri;
1179
1180 idev->mc_v1_seen = jiffies + switchback;
1181}
1182
1183static void mld_update_qrv(struct inet6_dev *idev,
1184 const struct mld2_query *mlh2)
1185{
1186 /* RFC3810, relevant sections:
1187 * - 5.1.8. QRV (Querier's Robustness Variable)
1188 * - 9.1. Robustness Variable
1189 */
1190
1191 /* The value of the Robustness Variable MUST NOT be zero,
1192 * and SHOULD NOT be one. Catch this here if we ever run
1193 * into such a case in future.
1194 */
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02001195 const int min_qrv = min(MLD_QRV_DEFAULT, sysctl_mld_qrv);
Daniel Borkmann89225d12013-09-04 00:19:37 +02001196 WARN_ON(idev->mc_qrv == 0);
1197
1198 if (mlh2->mld2q_qrv > 0)
1199 idev->mc_qrv = mlh2->mld2q_qrv;
1200
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02001201 if (unlikely(idev->mc_qrv < min_qrv)) {
Daniel Borkmann89225d12013-09-04 00:19:37 +02001202 net_warn_ratelimited("IPv6: MLD: clamping QRV from %u to %u!\n",
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02001203 idev->mc_qrv, min_qrv);
1204 idev->mc_qrv = min_qrv;
Daniel Borkmann89225d12013-09-04 00:19:37 +02001205 }
1206}
1207
1208static void mld_update_qi(struct inet6_dev *idev,
1209 const struct mld2_query *mlh2)
1210{
1211 /* RFC3810, relevant sections:
1212 * - 5.1.9. QQIC (Querier's Query Interval Code)
1213 * - 9.2. Query Interval
1214 * - 9.12. Older Version Querier Present Timeout
1215 * (the [Query Interval] in the last Query received)
1216 */
1217 unsigned long mc_qqi;
1218
1219 if (mlh2->mld2q_qqic < 128) {
1220 mc_qqi = mlh2->mld2q_qqic;
1221 } else {
1222 unsigned long mc_man, mc_exp;
1223
1224 mc_exp = MLDV2_QQIC_EXP(mlh2->mld2q_qqic);
1225 mc_man = MLDV2_QQIC_MAN(mlh2->mld2q_qqic);
1226
1227 mc_qqi = (mc_man | 0x10) << (mc_exp + 3);
1228 }
1229
1230 idev->mc_qi = mc_qqi * HZ;
1231}
1232
1233static void mld_update_qri(struct inet6_dev *idev,
1234 const struct mld2_query *mlh2)
1235{
1236 /* RFC3810, relevant sections:
1237 * - 5.1.3. Maximum Response Code
1238 * - 9.3. Query Response Interval
1239 */
Daniel Borkmanne3f5b172013-09-04 00:19:39 +02001240 idev->mc_qri = msecs_to_jiffies(mldv2_mrc(mlh2));
Daniel Borkmann89225d12013-09-04 00:19:37 +02001241}
1242
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001243static int mld_process_v1(struct inet6_dev *idev, struct mld_msg *mld,
1244 unsigned long *max_delay)
1245{
1246 unsigned long mldv1_md;
1247
1248 /* Ignore v1 queries */
1249 if (mld_in_v2_mode_only(idev))
1250 return -EINVAL;
1251
1252 /* MLDv1 router present */
1253 mldv1_md = ntohs(mld->mld_maxdelay);
1254 *max_delay = max(msecs_to_jiffies(mldv1_md), 1UL);
1255
1256 mld_set_v1_mode(idev);
1257
1258 /* cancel MLDv2 report timer */
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001259 mld_gq_stop_timer(idev);
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001260 /* cancel the interface change timer */
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001261 mld_ifc_stop_timer(idev);
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001262 /* clear deleted report items */
1263 mld_clear_delrec(idev);
1264
1265 return 0;
1266}
1267
1268static int mld_process_v2(struct inet6_dev *idev, struct mld2_query *mld,
1269 unsigned long *max_delay)
1270{
1271 /* hosts need to stay in MLDv1 mode, discard MLDv2 queries */
1272 if (mld_in_v1_mode(idev))
1273 return -EINVAL;
1274
1275 *max_delay = max(msecs_to_jiffies(mldv2_mrc(mld)), 1UL);
1276
1277 mld_update_qrv(idev, mld);
1278 mld_update_qi(idev, mld);
1279 mld_update_qri(idev, mld);
1280
1281 idev->mc_maxdelay = *max_delay;
1282
1283 return 0;
1284}
1285
Eric Dumazet96b52e62010-06-07 21:05:02 +00001286/* called with rcu_read_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287int igmp6_event_query(struct sk_buff *skb)
1288{
Yan Zheng97300b52005-10-31 20:09:45 +08001289 struct mld2_query *mlh2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 struct ifmcaddr6 *ma;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001291 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 unsigned long max_delay;
1293 struct inet6_dev *idev;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001294 struct mld_msg *mld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 int group_type;
1296 int mark = 0;
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001297 int len, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1300 return -EINVAL;
1301
1302 /* compute payload length excluding extension headers */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001303 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001304 len -= skb_network_header_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Hangbin Liue940f5d2014-06-27 09:57:53 +08001306 /* RFC3810 6.2
1307 * Upon reception of an MLD message that contains a Query, the node
1308 * checks if the source address of the message is a valid link-local
1309 * address, if the Hop Limit is set to 1, and if the Router Alert
1310 * option is present in the Hop-By-Hop Options header of the IPv6
1311 * packet. If any of these checks fails, the packet is dropped.
1312 */
1313 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL) ||
1314 ipv6_hdr(skb)->hop_limit != 1 ||
1315 !(IP6CB(skb)->flags & IP6SKB_ROUTERALERT) ||
1316 IP6CB(skb)->ra != htons(IPV6_OPT_ROUTERALERT_MLD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return -EINVAL;
1318
Eric Dumazet96b52e62010-06-07 21:05:02 +00001319 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (idev == NULL)
1321 return 0;
1322
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001323 mld = (struct mld_msg *)icmp6_hdr(skb);
1324 group = &mld->mld_mca;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 group_type = ipv6_addr_type(group);
1326
1327 if (group_type != IPV6_ADDR_ANY &&
Eric Dumazet96b52e62010-06-07 21:05:02 +00001328 !(group_type&IPV6_ADDR_MULTICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Daniel Borkmann9fd07842013-08-20 12:22:02 +02001331 if (len == MLD_V1_QUERY_LEN) {
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001332 err = mld_process_v1(idev, mld, &max_delay);
1333 if (err < 0)
1334 return err;
Daniel Borkmann9fd07842013-08-20 12:22:02 +02001335 } else if (len >= MLD_V2_QUERY_LEN_MIN) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001336 int srcs_offset = sizeof(struct mld2_query) -
Yan Zheng97300b52005-10-31 20:09:45 +08001337 sizeof(struct icmp6hdr);
Daniel Borkmann89225d12013-09-04 00:19:37 +02001338
Eric Dumazet96b52e62010-06-07 21:05:02 +00001339 if (!pskb_may_pull(skb, srcs_offset))
Yan Zheng97300b52005-10-31 20:09:45 +08001340 return -EINVAL;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001341
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001342 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Daniel Borkmann84698962013-08-20 12:22:00 +02001343
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001344 err = mld_process_v2(idev, mlh2, &max_delay);
1345 if (err < 0)
1346 return err;
Daniel Borkmann89225d12013-09-04 00:19:37 +02001347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 if (group_type == IPV6_ADDR_ANY) { /* general query */
Eric Dumazet96b52e62010-06-07 21:05:02 +00001349 if (mlh2->mld2q_nsrcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return -EINVAL; /* no sources allowed */
Eric Dumazet96b52e62010-06-07 21:05:02 +00001351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 mld_gq_start_timer(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return 0;
1354 }
1355 /* mark sources to include, if group & source-specific */
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001356 if (mlh2->mld2q_nsrcs != 0) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001357 if (!pskb_may_pull(skb, srcs_offset +
Eric Dumazet96b52e62010-06-07 21:05:02 +00001358 ntohs(mlh2->mld2q_nsrcs) * sizeof(struct in6_addr)))
Yan Zheng97300b52005-10-31 20:09:45 +08001359 return -EINVAL;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001360
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001361 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Yan Zheng97300b52005-10-31 20:09:45 +08001362 mark = 1;
1363 }
Eric Dumazet96b52e62010-06-07 21:05:02 +00001364 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 read_lock_bh(&idev->lock);
1368 if (group_type == IPV6_ADDR_ANY) {
Ian Morris67ba4152014-08-24 21:53:10 +01001369 for (ma = idev->mc_list; ma; ma = ma->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 spin_lock_bh(&ma->mca_lock);
1371 igmp6_group_queried(ma, max_delay);
1372 spin_unlock_bh(&ma->mca_lock);
1373 }
1374 } else {
Ian Morris67ba4152014-08-24 21:53:10 +01001375 for (ma = idev->mc_list; ma; ma = ma->next) {
David L Stevens7add2a42006-01-24 13:06:39 -08001376 if (!ipv6_addr_equal(group, &ma->mca_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 continue;
1378 spin_lock_bh(&ma->mca_lock);
1379 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1380 /* gsquery <- gsquery && mark */
1381 if (!mark)
1382 ma->mca_flags &= ~MAF_GSQUERY;
1383 } else {
1384 /* gsquery <- mark */
1385 if (mark)
1386 ma->mca_flags |= MAF_GSQUERY;
1387 else
1388 ma->mca_flags &= ~MAF_GSQUERY;
1389 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001390 if (!(ma->mca_flags & MAF_GSQUERY) ||
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001391 mld_marksources(ma, ntohs(mlh2->mld2q_nsrcs), mlh2->mld2q_srcs))
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001392 igmp6_group_queried(ma, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 spin_unlock_bh(&ma->mca_lock);
David L Stevens7add2a42006-01-24 13:06:39 -08001394 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
1396 }
1397 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 return 0;
1400}
1401
Eric Dumazet96b52e62010-06-07 21:05:02 +00001402/* called with rcu_read_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403int igmp6_event_report(struct sk_buff *skb)
1404{
1405 struct ifmcaddr6 *ma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 struct inet6_dev *idev;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001407 struct mld_msg *mld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 int addr_type;
1409
1410 /* Our own report looped back. Ignore it. */
1411 if (skb->pkt_type == PACKET_LOOPBACK)
1412 return 0;
1413
David Stevens24c69272005-12-02 20:32:59 -08001414 /* send our report if the MC router may not have heard this report */
1415 if (skb->pkt_type != PACKET_MULTICAST &&
1416 skb->pkt_type != PACKET_BROADCAST)
1417 return 0;
1418
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001419 if (!pskb_may_pull(skb, sizeof(*mld) - sizeof(struct icmp6hdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return -EINVAL;
1421
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001422 mld = (struct mld_msg *)icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 /* Drop reports with not link local source */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001425 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001426 if (addr_type != IPV6_ADDR_ANY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 !(addr_type&IPV6_ADDR_LINKLOCAL))
1428 return -EINVAL;
1429
Eric Dumazet96b52e62010-06-07 21:05:02 +00001430 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (idev == NULL)
1432 return -ENODEV;
1433
1434 /*
1435 * Cancel the timer for this group
1436 */
1437
1438 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +01001439 for (ma = idev->mc_list; ma; ma = ma->next) {
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001440 if (ipv6_addr_equal(&ma->mca_addr, &mld->mld_mca)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 spin_lock(&ma->mca_lock);
1442 if (del_timer(&ma->mca_timer))
1443 atomic_dec(&ma->mca_refcnt);
1444 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1445 spin_unlock(&ma->mca_lock);
1446 break;
1447 }
1448 }
1449 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return 0;
1451}
1452
Eric Dumazeta50feda2012-05-18 18:57:34 +00001453static bool is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1454 int gdeleted, int sdeleted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
1456 switch (type) {
1457 case MLD2_MODE_IS_INCLUDE:
1458 case MLD2_MODE_IS_EXCLUDE:
1459 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001460 return false;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001461 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1462 if (pmc->mca_sfmode == MCAST_INCLUDE)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001463 return true;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001464 /* don't include if this source is excluded
1465 * in all filters
1466 */
1467 if (psf->sf_count[MCAST_INCLUDE])
David L Stevens7add2a42006-01-24 13:06:39 -08001468 return type == MLD2_MODE_IS_INCLUDE;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001469 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1470 psf->sf_count[MCAST_EXCLUDE];
1471 }
Eric Dumazeta50feda2012-05-18 18:57:34 +00001472 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 case MLD2_CHANGE_TO_INCLUDE:
1474 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001475 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return psf->sf_count[MCAST_INCLUDE] != 0;
1477 case MLD2_CHANGE_TO_EXCLUDE:
1478 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001479 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1481 psf->sf_count[MCAST_INCLUDE])
Eric Dumazeta50feda2012-05-18 18:57:34 +00001482 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1484 psf->sf_count[MCAST_EXCLUDE];
1485 case MLD2_ALLOW_NEW_SOURCES:
1486 if (gdeleted || !psf->sf_crcount)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001487 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1489 case MLD2_BLOCK_OLD_SOURCES:
1490 if (pmc->mca_sfmode == MCAST_INCLUDE)
1491 return gdeleted || (psf->sf_crcount && sdeleted);
1492 return psf->sf_crcount && !gdeleted && !sdeleted;
1493 }
Eric Dumazeta50feda2012-05-18 18:57:34 +00001494 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
1497static int
1498mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1499{
1500 struct ip6_sf_list *psf;
1501 int scount = 0;
1502
Ian Morris67ba4152014-08-24 21:53:10 +01001503 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1505 continue;
1506 scount++;
1507 }
1508 return scount;
1509}
1510
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +00001511static void ip6_mc_hdr(struct sock *sk, struct sk_buff *skb,
1512 struct net_device *dev,
1513 const struct in6_addr *saddr,
1514 const struct in6_addr *daddr,
1515 int proto, int len)
1516{
1517 struct ipv6hdr *hdr;
1518
1519 skb->protocol = htons(ETH_P_IPV6);
1520 skb->dev = dev;
1521
1522 skb_reset_network_header(skb);
1523 skb_put(skb, sizeof(struct ipv6hdr));
1524 hdr = ipv6_hdr(skb);
1525
1526 ip6_flow_hdr(hdr, 0, 0);
1527
1528 hdr->payload_len = htons(len);
1529 hdr->nexthdr = proto;
1530 hdr->hop_limit = inet6_sk(sk)->hop_limit;
1531
1532 hdr->saddr = *saddr;
1533 hdr->daddr = *daddr;
1534}
1535
Amerigo Wang89657792013-06-29 21:30:49 +08001536static struct sk_buff *mld_newpack(struct inet6_dev *idev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Amerigo Wang89657792013-06-29 21:30:49 +08001538 struct net_device *dev = idev->dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001539 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001540 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 struct sk_buff *skb;
1542 struct mld2_report *pmr;
1543 struct in6_addr addr_buf;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001544 const struct in6_addr *saddr;
Herbert Xua7ae1992011-11-18 02:20:04 +00001545 int hlen = LL_RESERVED_SPACE(dev);
1546 int tlen = dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 int err;
1548 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1549 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1550 IPV6_TLV_PADN, 0 };
1551
1552 /* we assume size > sizeof(ra) here */
Herbert Xua7ae1992011-11-18 02:20:04 +00001553 size += hlen + tlen;
Eric Dumazet72e09ad2010-06-05 03:03:30 -07001554 /* limit our allocations to order-0 page */
1555 size = min_t(int, size, SKB_MAX_ORDER(0, 0));
1556 skb = sock_alloc_send_skb(sk, size, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001558 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return NULL;
1560
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +02001561 skb->priority = TC_PRIO_CONTROL;
Herbert Xua7ae1992011-11-18 02:20:04 +00001562 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Amerigo Wang89657792013-06-29 21:30:49 +08001564 if (__ipv6_get_lladdr(idev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001566 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 * when a valid link-local address is not available.
1568 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001569 saddr = &in6addr_any;
1570 } else
1571 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +00001573 ip6_mc_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1576
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001577 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -03001578 skb_put(skb, sizeof(*pmr));
1579 pmr = (struct mld2_report *)skb_transport_header(skb);
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001580 pmr->mld2r_type = ICMPV6_MLD2_REPORT;
1581 pmr->mld2r_resv1 = 0;
1582 pmr->mld2r_cksum = 0;
1583 pmr->mld2r_resv2 = 0;
1584 pmr->mld2r_ngrec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return skb;
1586}
1587
1588static void mld_sendpack(struct sk_buff *skb)
1589{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001590 struct ipv6hdr *pip6 = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001591 struct mld2_report *pmr =
1592 (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 int payload_len, mldlen;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001594 struct inet6_dev *idev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001595 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 int err;
David S. Miller4c9483b2011-03-12 16:22:43 -05001597 struct flowi6 fl6;
Eric Dumazetadf30902009-06-02 05:19:30 +00001598 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Eric Dumazet96b52e62010-06-07 21:05:02 +00001600 rcu_read_lock();
1601 idev = __in6_dev_get(skb->dev);
Neil Hormanedf391f2009-04-27 02:45:02 -07001602 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
1603
Simon Horman29a3cad2013-05-28 20:34:26 +00001604 payload_len = (skb_tail_pointer(skb) - skb_network_header(skb)) -
1605 sizeof(*pip6);
1606 mldlen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 pip6->payload_len = htons(payload_len);
1608
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001609 pmr->mld2r_cksum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1610 IPPROTO_ICMPV6,
1611 csum_partial(skb_transport_header(skb),
1612 mldlen, 0));
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001613
David S. Miller4c9483b2011-03-12 16:22:43 -05001614 icmpv6_flow_init(net->ipv6.igmp_sk, &fl6, ICMPV6_MLD2_REPORT,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001615 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1616 skb->dev->ifindex);
YOSHIFUJI Hideaki / 吉藤英明12fd84f2013-01-18 02:00:24 +00001617 dst = icmp6_dst_alloc(skb->dev, &fl6);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001618
David S. Miller452edd52011-03-02 13:27:41 -08001619 err = 0;
1620 if (IS_ERR(dst)) {
1621 err = PTR_ERR(dst);
1622 dst = NULL;
1623 }
Eric Dumazetadf30902009-06-02 05:19:30 +00001624 skb_dst_set(skb, dst);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001625 if (err)
1626 goto err_out;
1627
Neil Hormanedf391f2009-04-27 02:45:02 -07001628 payload_len = skb->len;
1629
Jan Engelhardtb2e0b382010-03-23 04:09:07 +01001630 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001631 dst_output);
1632out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 if (!err) {
Hannes Frederic Sowa43a43b62014-03-31 20:14:10 +02001634 ICMP6MSGOUT_INC_STATS(net, idev, ICMPV6_MLD2_REPORT);
1635 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
1636 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, payload_len);
1637 } else {
1638 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
1639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Eric Dumazet96b52e62010-06-07 21:05:02 +00001641 rcu_read_unlock();
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001642 return;
1643
1644err_out:
1645 kfree_skb(skb);
1646 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
1649static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1650{
Yan Zhengfab10fe2005-10-05 12:08:13 -07001651 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
1654static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1655 int type, struct mld2_grec **ppgr)
1656{
1657 struct net_device *dev = pmc->idev->dev;
1658 struct mld2_report *pmr;
1659 struct mld2_grec *pgr;
1660
1661 if (!skb)
Amerigo Wang89657792013-06-29 21:30:49 +08001662 skb = mld_newpack(pmc->idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 if (!skb)
1664 return NULL;
1665 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1666 pgr->grec_type = type;
1667 pgr->grec_auxwords = 0;
1668 pgr->grec_nsrcs = 0;
1669 pgr->grec_mca = pmc->mca_addr; /* structure copy */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001670 pmr = (struct mld2_report *)skb_transport_header(skb);
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001671 pmr->mld2r_ngrec = htons(ntohs(pmr->mld2r_ngrec)+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 *ppgr = pgr;
1673 return skb;
1674}
1675
1676#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1677 skb_tailroom(skb)) : 0)
1678
1679static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001680 int type, int gdeleted, int sdeleted, int crsend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Amerigo Wang89657792013-06-29 21:30:49 +08001682 struct inet6_dev *idev = pmc->idev;
1683 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 struct mld2_report *pmr;
1685 struct mld2_grec *pgr = NULL;
1686 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001687 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 if (pmc->mca_flags & MAF_NOREPORT)
1690 return skb;
1691
1692 isquery = type == MLD2_MODE_IS_INCLUDE ||
1693 type == MLD2_MODE_IS_EXCLUDE;
1694 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1695 type == MLD2_CHANGE_TO_EXCLUDE;
1696
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001697 stotal = scount = 0;
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1700
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001701 if (!*psf_list)
1702 goto empty_source;
1703
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001704 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 /* EX and TO_EX get a fresh packet, if needed */
1707 if (truncate) {
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001708 if (pmr && pmr->mld2r_ngrec &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1710 if (skb)
1711 mld_sendpack(skb);
Amerigo Wang89657792013-06-29 21:30:49 +08001712 skb = mld_newpack(idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
1714 }
1715 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 psf_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01001717 for (psf = *psf_list; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 struct in6_addr *psrc;
1719
1720 psf_next = psf->sf_next;
1721
1722 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1723 psf_prev = psf;
1724 continue;
1725 }
1726
1727 /* clear marks on query responses */
1728 if (isquery)
1729 psf->sf_gsresp = 0;
1730
1731 if (AVAILABLE(skb) < sizeof(*psrc) +
1732 first*sizeof(struct mld2_grec)) {
1733 if (truncate && !first)
1734 break; /* truncate these */
1735 if (pgr)
1736 pgr->grec_nsrcs = htons(scount);
1737 if (skb)
1738 mld_sendpack(skb);
Amerigo Wang89657792013-06-29 21:30:49 +08001739 skb = mld_newpack(idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 first = 1;
1741 scount = 0;
1742 }
1743 if (first) {
1744 skb = add_grhead(skb, pmc, type, &pgr);
1745 first = 0;
1746 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -08001747 if (!skb)
1748 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1750 *psrc = psf->sf_addr;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001751 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1753 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1754 psf->sf_crcount--;
1755 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1756 if (psf_prev)
1757 psf_prev->sf_next = psf->sf_next;
1758 else
1759 *psf_list = psf->sf_next;
1760 kfree(psf);
1761 continue;
1762 }
1763 }
1764 psf_prev = psf;
1765 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001766
1767empty_source:
1768 if (!stotal) {
1769 if (type == MLD2_ALLOW_NEW_SOURCES ||
1770 type == MLD2_BLOCK_OLD_SOURCES)
1771 return skb;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001772 if (pmc->mca_crcount || isquery || crsend) {
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001773 /* make sure we have room for group header */
1774 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1775 mld_sendpack(skb);
1776 skb = NULL; /* add_grhead will get a new one */
1777 }
1778 skb = add_grhead(skb, pmc, type, &pgr);
1779 }
1780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 if (pgr)
1782 pgr->grec_nsrcs = htons(scount);
1783
1784 if (isquery)
1785 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1786 return skb;
1787}
1788
1789static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1790{
1791 struct sk_buff *skb = NULL;
1792 int type;
1793
Amerigo Wang89657792013-06-29 21:30:49 +08001794 read_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (!pmc) {
Ian Morris67ba4152014-08-24 21:53:10 +01001796 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 if (pmc->mca_flags & MAF_NOREPORT)
1798 continue;
1799 spin_lock_bh(&pmc->mca_lock);
1800 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1801 type = MLD2_MODE_IS_EXCLUDE;
1802 else
1803 type = MLD2_MODE_IS_INCLUDE;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001804 skb = add_grec(skb, pmc, type, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 spin_unlock_bh(&pmc->mca_lock);
1806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 } else {
1808 spin_lock_bh(&pmc->mca_lock);
1809 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1810 type = MLD2_MODE_IS_EXCLUDE;
1811 else
1812 type = MLD2_MODE_IS_INCLUDE;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001813 skb = add_grec(skb, pmc, type, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 spin_unlock_bh(&pmc->mca_lock);
1815 }
Amerigo Wang89657792013-06-29 21:30:49 +08001816 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 if (skb)
1818 mld_sendpack(skb);
1819}
1820
1821/*
1822 * remove zero-count source records from a source filter list
1823 */
1824static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1825{
1826 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1827
1828 psf_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01001829 for (psf = *ppsf; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 psf_next = psf->sf_next;
1831 if (psf->sf_crcount == 0) {
1832 if (psf_prev)
1833 psf_prev->sf_next = psf->sf_next;
1834 else
1835 *ppsf = psf->sf_next;
1836 kfree(psf);
1837 } else
1838 psf_prev = psf;
1839 }
1840}
1841
1842static void mld_send_cr(struct inet6_dev *idev)
1843{
1844 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1845 struct sk_buff *skb = NULL;
1846 int type, dtype;
1847
1848 read_lock_bh(&idev->lock);
Stephen Hemminger6457d262010-02-17 18:48:44 -08001849 spin_lock(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 /* deleted MCA's */
1852 pmc_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01001853 for (pmc = idev->mc_tomb; pmc; pmc = pmc_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 pmc_next = pmc->next;
1855 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1856 type = MLD2_BLOCK_OLD_SOURCES;
1857 dtype = MLD2_BLOCK_OLD_SOURCES;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001858 skb = add_grec(skb, pmc, type, 1, 0, 0);
1859 skb = add_grec(skb, pmc, dtype, 1, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 }
1861 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1863 type = MLD2_CHANGE_TO_INCLUDE;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001864 skb = add_grec(skb, pmc, type, 1, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001866 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (pmc->mca_crcount == 0) {
1868 mld_clear_zeros(&pmc->mca_tomb);
1869 mld_clear_zeros(&pmc->mca_sources);
1870 }
1871 }
1872 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1873 !pmc->mca_sources) {
1874 if (pmc_prev)
1875 pmc_prev->next = pmc_next;
1876 else
1877 idev->mc_tomb = pmc_next;
1878 in6_dev_put(pmc->idev);
1879 kfree(pmc);
1880 } else
1881 pmc_prev = pmc;
1882 }
Stephen Hemminger6457d262010-02-17 18:48:44 -08001883 spin_unlock(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885 /* change recs */
Ian Morris67ba4152014-08-24 21:53:10 +01001886 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 spin_lock_bh(&pmc->mca_lock);
1888 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1889 type = MLD2_BLOCK_OLD_SOURCES;
1890 dtype = MLD2_ALLOW_NEW_SOURCES;
1891 } else {
1892 type = MLD2_ALLOW_NEW_SOURCES;
1893 dtype = MLD2_BLOCK_OLD_SOURCES;
1894 }
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001895 skb = add_grec(skb, pmc, type, 0, 0, 0);
1896 skb = add_grec(skb, pmc, dtype, 0, 1, 0); /* deleted sources */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 /* filter mode changes */
1899 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1901 type = MLD2_CHANGE_TO_EXCLUDE;
1902 else
1903 type = MLD2_CHANGE_TO_INCLUDE;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001904 skb = add_grec(skb, pmc, type, 0, 0, 0);
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001905 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
1907 spin_unlock_bh(&pmc->mca_lock);
1908 }
1909 read_unlock_bh(&idev->lock);
1910 if (!skb)
1911 return;
1912 (void) mld_sendpack(skb);
1913}
1914
1915static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1916{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001917 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001918 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 struct inet6_dev *idev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001920 struct sk_buff *skb;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001921 struct mld_msg *hdr;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001922 const struct in6_addr *snd_addr, *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 struct in6_addr addr_buf;
Herbert Xua7ae1992011-11-18 02:20:04 +00001924 int hlen = LL_RESERVED_SPACE(dev);
1925 int tlen = dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 int err, len, payload_len, full_len;
1927 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1928 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1929 IPV6_TLV_PADN, 0 };
David S. Miller4c9483b2011-03-12 16:22:43 -05001930 struct flowi6 fl6;
Eric Dumazetadf30902009-06-02 05:19:30 +00001931 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09001933 if (type == ICMPV6_MGM_REDUCTION)
1934 snd_addr = &in6addr_linklocal_allrouters;
1935 else
1936 snd_addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1939 payload_len = len + sizeof(ra);
1940 full_len = sizeof(struct ipv6hdr) + payload_len;
1941
Neil Hormanedf391f2009-04-27 02:45:02 -07001942 rcu_read_lock();
1943 IP6_UPD_PO_STATS(net, __in6_dev_get(dev),
1944 IPSTATS_MIB_OUT, full_len);
1945 rcu_read_unlock();
1946
Herbert Xua7ae1992011-11-18 02:20:04 +00001947 skb = sock_alloc_send_skb(sk, hlen + tlen + full_len, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 if (skb == NULL) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001950 rcu_read_lock();
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001951 IP6_INC_STATS(net, __in6_dev_get(dev),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001952 IPSTATS_MIB_OUTDISCARDS);
1953 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 return;
1955 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +02001956 skb->priority = TC_PRIO_CONTROL;
Herbert Xua7ae1992011-11-18 02:20:04 +00001957 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Neil Horman95c385b2007-04-25 17:08:10 -07001959 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001961 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 * when a valid link-local address is not available.
1963 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001964 saddr = &in6addr_any;
1965 } else
1966 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +00001968 ip6_mc_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1971
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001972 hdr = (struct mld_msg *) skb_put(skb, sizeof(struct mld_msg));
1973 memset(hdr, 0, sizeof(struct mld_msg));
1974 hdr->mld_type = type;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001975 hdr->mld_mca = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001977 hdr->mld_cksum = csum_ipv6_magic(saddr, snd_addr, len,
1978 IPPROTO_ICMPV6,
1979 csum_partial(hdr, len, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Eric Dumazet96b52e62010-06-07 21:05:02 +00001981 rcu_read_lock();
1982 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
David S. Miller4c9483b2011-03-12 16:22:43 -05001984 icmpv6_flow_init(sk, &fl6, type,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001985 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1986 skb->dev->ifindex);
YOSHIFUJI Hideaki / 吉藤英明12fd84f2013-01-18 02:00:24 +00001987 dst = icmp6_dst_alloc(skb->dev, &fl6);
David S. Miller452edd52011-03-02 13:27:41 -08001988 if (IS_ERR(dst)) {
1989 err = PTR_ERR(dst);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001990 goto err_out;
David S. Miller452edd52011-03-02 13:27:41 -08001991 }
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001992
Eric Dumazetadf30902009-06-02 05:19:30 +00001993 skb_dst_set(skb, dst);
Jan Engelhardtb2e0b382010-03-23 04:09:07 +01001994 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001995 dst_output);
1996out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 if (!err) {
Denis V. Lunev5c5d2442008-10-08 10:33:50 -07001998 ICMP6MSGOUT_INC_STATS(net, idev, type);
Denis V. Luneva862f6a2008-10-08 10:33:06 -07001999 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
Neil Hormanedf391f2009-04-27 02:45:02 -07002000 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, full_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 } else
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07002002 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Eric Dumazet96b52e62010-06-07 21:05:02 +00002004 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 return;
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08002006
2007err_out:
2008 kfree_skb(skb);
2009 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
2011
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002012static void mld_send_initial_cr(struct inet6_dev *idev)
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002013{
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002014 struct sk_buff *skb;
2015 struct ifmcaddr6 *pmc;
2016 int type;
2017
2018 if (mld_in_v1_mode(idev))
2019 return;
2020
2021 skb = NULL;
2022 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002023 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002024 spin_lock_bh(&pmc->mca_lock);
2025 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2026 type = MLD2_CHANGE_TO_EXCLUDE;
2027 else
2028 type = MLD2_CHANGE_TO_INCLUDE;
2029 skb = add_grec(skb, pmc, type, 0, 0, 1);
2030 spin_unlock_bh(&pmc->mca_lock);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002031 }
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002032 read_unlock_bh(&idev->lock);
2033 if (skb)
2034 mld_sendpack(skb);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002035}
2036
2037void ipv6_mc_dad_complete(struct inet6_dev *idev)
2038{
2039 idev->mc_dad_count = idev->mc_qrv;
2040 if (idev->mc_dad_count) {
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002041 mld_send_initial_cr(idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002042 idev->mc_dad_count--;
2043 if (idev->mc_dad_count)
2044 mld_dad_start_timer(idev, idev->mc_maxdelay);
2045 }
2046}
2047
2048static void mld_dad_timer_expire(unsigned long data)
2049{
2050 struct inet6_dev *idev = (struct inet6_dev *)data;
2051
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002052 mld_send_initial_cr(idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002053 if (idev->mc_dad_count) {
2054 idev->mc_dad_count--;
2055 if (idev->mc_dad_count)
2056 mld_dad_start_timer(idev, idev->mc_maxdelay);
2057 }
Salam Noureddine9260d3e2013-09-29 13:41:34 -07002058 in6_dev_put(idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002059}
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002062 const struct in6_addr *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
2064 struct ip6_sf_list *psf, *psf_prev;
2065 int rv = 0;
2066
2067 psf_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01002068 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
2070 break;
2071 psf_prev = psf;
2072 }
2073 if (!psf || psf->sf_count[sfmode] == 0) {
2074 /* source filter not found, or count wrong => bug */
2075 return -ESRCH;
2076 }
2077 psf->sf_count[sfmode]--;
2078 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
2079 struct inet6_dev *idev = pmc->idev;
2080
2081 /* no more filters for this source */
2082 if (psf_prev)
2083 psf_prev->sf_next = psf->sf_next;
2084 else
2085 pmc->mca_sources = psf->sf_next;
2086 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
Daniel Borkmann6c567b72013-09-04 00:19:38 +02002087 !mld_in_v1_mode(idev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 psf->sf_crcount = idev->mc_qrv;
2089 psf->sf_next = pmc->mca_tomb;
2090 pmc->mca_tomb = psf;
2091 rv = 1;
2092 } else
2093 kfree(psf);
2094 }
2095 return rv;
2096}
2097
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002098static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2099 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 int delta)
2101{
2102 struct ifmcaddr6 *pmc;
2103 int changerec = 0;
2104 int i, err;
2105
2106 if (!idev)
2107 return -ENODEV;
2108 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002109 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2111 break;
2112 }
2113 if (!pmc) {
2114 /* MCA not found?? bug */
2115 read_unlock_bh(&idev->lock);
2116 return -ESRCH;
2117 }
2118 spin_lock_bh(&pmc->mca_lock);
2119 sf_markstate(pmc);
2120 if (!delta) {
2121 if (!pmc->mca_sfcount[sfmode]) {
2122 spin_unlock_bh(&pmc->mca_lock);
2123 read_unlock_bh(&idev->lock);
2124 return -EINVAL;
2125 }
2126 pmc->mca_sfcount[sfmode]--;
2127 }
2128 err = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01002129 for (i = 0; i < sfcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2131
2132 changerec |= rv > 0;
2133 if (!err && rv < 0)
2134 err = rv;
2135 }
2136 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
2137 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
2138 pmc->mca_sfcount[MCAST_INCLUDE]) {
2139 struct ip6_sf_list *psf;
2140
2141 /* filter mode change */
2142 pmc->mca_sfmode = MCAST_INCLUDE;
2143 pmc->mca_crcount = idev->mc_qrv;
2144 idev->mc_ifc_count = pmc->mca_crcount;
Ian Morris67ba4152014-08-24 21:53:10 +01002145 for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 psf->sf_crcount = 0;
2147 mld_ifc_event(pmc->idev);
2148 } else if (sf_setstate(pmc) || changerec)
2149 mld_ifc_event(pmc->idev);
2150 spin_unlock_bh(&pmc->mca_lock);
2151 read_unlock_bh(&idev->lock);
2152 return err;
2153}
2154
2155/*
2156 * Add multicast single-source filter to the interface list
2157 */
2158static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
Jun Zhao99d2f472011-11-30 06:21:05 +00002159 const struct in6_addr *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
2161 struct ip6_sf_list *psf, *psf_prev;
2162
2163 psf_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01002164 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
2166 break;
2167 psf_prev = psf;
2168 }
2169 if (!psf) {
Ingo Oeser0c600ed2006-03-20 23:01:32 -08002170 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 if (!psf)
2172 return -ENOBUFS;
Ingo Oeser0c600ed2006-03-20 23:01:32 -08002173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 psf->sf_addr = *psfsrc;
2175 if (psf_prev) {
2176 psf_prev->sf_next = psf;
2177 } else
2178 pmc->mca_sources = psf;
2179 }
2180 psf->sf_count[sfmode]++;
2181 return 0;
2182}
2183
2184static void sf_markstate(struct ifmcaddr6 *pmc)
2185{
2186 struct ip6_sf_list *psf;
2187 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2188
Ian Morris67ba4152014-08-24 21:53:10 +01002189 for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2191 psf->sf_oldin = mca_xcount ==
2192 psf->sf_count[MCAST_EXCLUDE] &&
2193 !psf->sf_count[MCAST_INCLUDE];
2194 } else
2195 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2196}
2197
2198static int sf_setstate(struct ifmcaddr6 *pmc)
2199{
David L Stevens7add2a42006-01-24 13:06:39 -08002200 struct ip6_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2202 int qrv = pmc->idev->mc_qrv;
2203 int new_in, rv;
2204
2205 rv = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01002206 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2208 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2209 !psf->sf_count[MCAST_INCLUDE];
2210 } else
2211 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevens7add2a42006-01-24 13:06:39 -08002212 if (new_in) {
2213 if (!psf->sf_oldin) {
Al Viroe80e28b2006-02-03 20:10:03 -05002214 struct ip6_sf_list *prev = NULL;
David L Stevens7add2a42006-01-24 13:06:39 -08002215
Ian Morris67ba4152014-08-24 21:53:10 +01002216 for (dpsf = pmc->mca_tomb; dpsf;
2217 dpsf = dpsf->sf_next) {
David L Stevens7add2a42006-01-24 13:06:39 -08002218 if (ipv6_addr_equal(&dpsf->sf_addr,
2219 &psf->sf_addr))
2220 break;
2221 prev = dpsf;
2222 }
2223 if (dpsf) {
2224 if (prev)
2225 prev->sf_next = dpsf->sf_next;
2226 else
2227 pmc->mca_tomb = dpsf->sf_next;
2228 kfree(dpsf);
2229 }
2230 psf->sf_crcount = qrv;
2231 rv++;
2232 }
2233 } else if (psf->sf_oldin) {
2234 psf->sf_crcount = 0;
2235 /*
2236 * add or update "delete" records if an active filter
2237 * is now inactive
2238 */
Ian Morris67ba4152014-08-24 21:53:10 +01002239 for (dpsf = pmc->mca_tomb; dpsf; dpsf = dpsf->sf_next)
David L Stevens7add2a42006-01-24 13:06:39 -08002240 if (ipv6_addr_equal(&dpsf->sf_addr,
2241 &psf->sf_addr))
2242 break;
2243 if (!dpsf) {
Joe Perches5d553542010-05-31 17:23:22 +00002244 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevens7add2a42006-01-24 13:06:39 -08002245 if (!dpsf)
2246 continue;
2247 *dpsf = *psf;
2248 /* pmc->mca_lock held by callers */
2249 dpsf->sf_next = pmc->mca_tomb;
2250 pmc->mca_tomb = dpsf;
2251 }
2252 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 rv++;
2254 }
2255 }
2256 return rv;
2257}
2258
2259/*
2260 * Add multicast source filter list to the interface list
2261 */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002262static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2263 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 int delta)
2265{
2266 struct ifmcaddr6 *pmc;
2267 int isexclude;
2268 int i, err;
2269
2270 if (!idev)
2271 return -ENODEV;
2272 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002273 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2275 break;
2276 }
2277 if (!pmc) {
2278 /* MCA not found?? bug */
2279 read_unlock_bh(&idev->lock);
2280 return -ESRCH;
2281 }
2282 spin_lock_bh(&pmc->mca_lock);
2283
2284 sf_markstate(pmc);
2285 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2286 if (!delta)
2287 pmc->mca_sfcount[sfmode]++;
2288 err = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01002289 for (i = 0; i < sfcount; i++) {
Jun Zhao99d2f472011-11-30 06:21:05 +00002290 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if (err)
2292 break;
2293 }
2294 if (err) {
2295 int j;
2296
2297 if (!delta)
2298 pmc->mca_sfcount[sfmode]--;
Ian Morris67ba4152014-08-24 21:53:10 +01002299 for (j = 0; j < i; j++)
RongQing.Li78d50212012-04-04 16:47:04 +00002300 ip6_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 struct ip6_sf_list *psf;
2303
2304 /* filter mode change */
2305 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2306 pmc->mca_sfmode = MCAST_EXCLUDE;
2307 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2308 pmc->mca_sfmode = MCAST_INCLUDE;
2309 /* else no filters; keep old mode for reports */
2310
2311 pmc->mca_crcount = idev->mc_qrv;
2312 idev->mc_ifc_count = pmc->mca_crcount;
Ian Morris67ba4152014-08-24 21:53:10 +01002313 for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 psf->sf_crcount = 0;
2315 mld_ifc_event(idev);
2316 } else if (sf_setstate(pmc))
2317 mld_ifc_event(idev);
2318 spin_unlock_bh(&pmc->mca_lock);
2319 read_unlock_bh(&idev->lock);
2320 return err;
2321}
2322
2323static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2324{
2325 struct ip6_sf_list *psf, *nextpsf;
2326
Ian Morris67ba4152014-08-24 21:53:10 +01002327 for (psf = pmc->mca_tomb; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 nextpsf = psf->sf_next;
2329 kfree(psf);
2330 }
2331 pmc->mca_tomb = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01002332 for (psf = pmc->mca_sources; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 nextpsf = psf->sf_next;
2334 kfree(psf);
2335 }
2336 pmc->mca_sources = NULL;
2337 pmc->mca_sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002338 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2340}
2341
2342
2343static void igmp6_join_group(struct ifmcaddr6 *ma)
2344{
2345 unsigned long delay;
2346
2347 if (ma->mca_flags & MAF_NOREPORT)
2348 return;
2349
2350 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2351
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05002352 delay = prandom_u32() % unsolicited_report_interval(ma->idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
2354 spin_lock_bh(&ma->mca_lock);
2355 if (del_timer(&ma->mca_timer)) {
2356 atomic_dec(&ma->mca_refcnt);
2357 delay = ma->mca_timer.expires - jiffies;
2358 }
2359
2360 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2361 atomic_inc(&ma->mca_refcnt);
2362 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2363 spin_unlock_bh(&ma->mca_lock);
2364}
2365
2366static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2367 struct inet6_dev *idev)
2368{
2369 int err;
2370
David L Stevens5ab4a6c2005-12-27 14:03:00 -08002371 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2372 * so no other readers or writers of iml or its sflist
2373 */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002374 if (!iml->sflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 /* any-source empty exclude case */
2376 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2377 }
2378 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2379 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2380 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2381 iml->sflist = NULL;
2382 return err;
2383}
2384
2385static void igmp6_leave_group(struct ifmcaddr6 *ma)
2386{
Daniel Borkmann6c567b72013-09-04 00:19:38 +02002387 if (mld_in_v1_mode(ma->idev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if (ma->mca_flags & MAF_LAST_REPORTER)
2389 igmp6_send(&ma->mca_addr, ma->idev->dev,
2390 ICMPV6_MGM_REDUCTION);
2391 } else {
2392 mld_add_delrec(ma->idev, ma);
2393 mld_ifc_event(ma->idev);
2394 }
2395}
2396
2397static void mld_gq_timer_expire(unsigned long data)
2398{
2399 struct inet6_dev *idev = (struct inet6_dev *)data;
2400
2401 idev->mc_gq_running = 0;
2402 mld_send_report(idev, NULL);
Salam Noureddine9260d3e2013-09-29 13:41:34 -07002403 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404}
2405
2406static void mld_ifc_timer_expire(unsigned long data)
2407{
2408 struct inet6_dev *idev = (struct inet6_dev *)data;
2409
2410 mld_send_cr(idev);
2411 if (idev->mc_ifc_count) {
2412 idev->mc_ifc_count--;
2413 if (idev->mc_ifc_count)
2414 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2415 }
Salam Noureddine9260d3e2013-09-29 13:41:34 -07002416 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417}
2418
2419static void mld_ifc_event(struct inet6_dev *idev)
2420{
Daniel Borkmann6c567b72013-09-04 00:19:38 +02002421 if (mld_in_v1_mode(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return;
2423 idev->mc_ifc_count = idev->mc_qrv;
2424 mld_ifc_start_timer(idev, 1);
2425}
2426
2427
2428static void igmp6_timer_handler(unsigned long data)
2429{
2430 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2431
Daniel Borkmann6c567b72013-09-04 00:19:38 +02002432 if (mld_in_v1_mode(ma->idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2434 else
2435 mld_send_report(ma->idev, ma);
2436
2437 spin_lock(&ma->mca_lock);
2438 ma->mca_flags |= MAF_LAST_REPORTER;
2439 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2440 spin_unlock(&ma->mca_lock);
2441 ma_put(ma);
2442}
2443
Moni Shoua75c78502009-09-15 02:37:40 -07002444/* Device changing type */
2445
2446void ipv6_mc_unmap(struct inet6_dev *idev)
2447{
2448 struct ifmcaddr6 *i;
2449
2450 /* Install multicast list, except for all-nodes (already installed) */
2451
2452 read_lock_bh(&idev->lock);
2453 for (i = idev->mc_list; i; i = i->next)
2454 igmp6_group_dropped(i);
2455 read_unlock_bh(&idev->lock);
2456}
2457
2458void ipv6_mc_remap(struct inet6_dev *idev)
2459{
2460 ipv6_mc_up(idev);
2461}
2462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463/* Device going down */
2464
2465void ipv6_mc_down(struct inet6_dev *idev)
2466{
2467 struct ifmcaddr6 *i;
2468
2469 /* Withdraw multicast list */
2470
2471 read_lock_bh(&idev->lock);
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02002472 mld_ifc_stop_timer(idev);
2473 mld_gq_stop_timer(idev);
2474 mld_dad_stop_timer(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Ian Morris67ba4152014-08-24 21:53:10 +01002476 for (i = idev->mc_list; i; i = i->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 igmp6_group_dropped(i);
2478 read_unlock_bh(&idev->lock);
2479
2480 mld_clear_delrec(idev);
2481}
2482
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02002483static void ipv6_mc_reset(struct inet6_dev *idev)
2484{
2485 idev->mc_qrv = sysctl_mld_qrv;
2486 idev->mc_qi = MLD_QI_DEFAULT;
2487 idev->mc_qri = MLD_QRI_DEFAULT;
2488 idev->mc_v1_seen = 0;
2489 idev->mc_maxdelay = unsolicited_report_interval(idev);
2490}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492/* Device going up */
2493
2494void ipv6_mc_up(struct inet6_dev *idev)
2495{
2496 struct ifmcaddr6 *i;
2497
2498 /* Install multicast list, except for all-nodes (already installed) */
2499
2500 read_lock_bh(&idev->lock);
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02002501 ipv6_mc_reset(idev);
Ian Morris67ba4152014-08-24 21:53:10 +01002502 for (i = idev->mc_list; i; i = i->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 igmp6_group_added(i);
2504 read_unlock_bh(&idev->lock);
2505}
2506
2507/* IPv6 device initialization. */
2508
2509void ipv6_mc_init_dev(struct inet6_dev *idev)
2510{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 write_lock_bh(&idev->lock);
Stephen Hemminger6457d262010-02-17 18:48:44 -08002512 spin_lock_init(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 idev->mc_gq_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002514 setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2515 (unsigned long)idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 idev->mc_tomb = NULL;
2517 idev->mc_ifc_count = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002518 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2519 (unsigned long)idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002520 setup_timer(&idev->mc_dad_timer, mld_dad_timer_expire,
2521 (unsigned long)idev);
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02002522 ipv6_mc_reset(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524}
2525
2526/*
2527 * Device is about to be destroyed: clean up.
2528 */
2529
2530void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2531{
2532 struct ifmcaddr6 *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534 /* Deactivate timers */
2535 ipv6_mc_down(idev);
2536
2537 /* Delete all-nodes address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2539 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2540 * fail.
2541 */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002542 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002544 if (idev->cnf.forwarding)
2545 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
2547 write_lock_bh(&idev->lock);
2548 while ((i = idev->mc_list) != NULL) {
2549 idev->mc_list = i->next;
2550 write_unlock_bh(&idev->lock);
2551
2552 igmp6_group_dropped(i);
2553 ma_put(i);
2554
2555 write_lock_bh(&idev->lock);
2556 }
2557 write_unlock_bh(&idev->lock);
2558}
2559
2560#ifdef CONFIG_PROC_FS
2561struct igmp6_mc_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002562 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 struct net_device *dev;
2564 struct inet6_dev *idev;
2565};
2566
2567#define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2568
2569static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2570{
2571 struct ifmcaddr6 *im = NULL;
2572 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002573 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Pavel Emelianov7562f872007-05-03 15:13:45 -07002575 state->idev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002576 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 struct inet6_dev *idev;
Eric Dumazetce81b762009-11-11 17:34:30 +00002578 idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 if (!idev)
2580 continue;
2581 read_lock_bh(&idev->lock);
2582 im = idev->mc_list;
2583 if (im) {
2584 state->idev = idev;
2585 break;
2586 }
2587 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 }
2589 return im;
2590}
2591
2592static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2593{
2594 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2595
2596 im = im->next;
2597 while (!im) {
Eric Dumazetce81b762009-11-11 17:34:30 +00002598 if (likely(state->idev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 read_unlock_bh(&state->idev->lock);
Eric Dumazetce81b762009-11-11 17:34:30 +00002600
2601 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 if (!state->dev) {
2603 state->idev = NULL;
2604 break;
2605 }
Eric Dumazetce81b762009-11-11 17:34:30 +00002606 state->idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 if (!state->idev)
2608 continue;
2609 read_lock_bh(&state->idev->lock);
2610 im = state->idev->mc_list;
2611 }
2612 return im;
2613}
2614
2615static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2616{
2617 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2618 if (im)
2619 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2620 --pos;
2621 return pos ? NULL : im;
2622}
2623
2624static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetce81b762009-11-11 17:34:30 +00002625 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
Eric Dumazetce81b762009-11-11 17:34:30 +00002627 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 return igmp6_mc_get_idx(seq, *pos);
2629}
2630
2631static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2632{
Eric Dumazetce81b762009-11-11 17:34:30 +00002633 struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);
2634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 ++*pos;
2636 return im;
2637}
2638
2639static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetce81b762009-11-11 17:34:30 +00002640 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641{
2642 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
Eric Dumazetce81b762009-11-11 17:34:30 +00002643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 if (likely(state->idev != NULL)) {
2645 read_unlock_bh(&state->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 state->idev = NULL;
2647 }
2648 state->dev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002649 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650}
2651
2652static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2653{
2654 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2655 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2656
2657 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002658 "%-4d %-15s %pi6 %5d %08X %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 state->dev->ifindex, state->dev->name,
Harvey Harrisonb0711952008-10-28 16:05:40 -07002660 &im->mca_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 im->mca_users, im->mca_flags,
2662 (im->mca_flags&MAF_TIMER_RUNNING) ?
2663 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2664 return 0;
2665}
2666
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002667static const struct seq_operations igmp6_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 .start = igmp6_mc_seq_start,
2669 .next = igmp6_mc_seq_next,
2670 .stop = igmp6_mc_seq_stop,
2671 .show = igmp6_mc_seq_show,
2672};
2673
2674static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2675{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002676 return seq_open_net(inode, file, &igmp6_mc_seq_ops,
2677 sizeof(struct igmp6_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678}
2679
Arjan van de Ven9a321442007-02-12 00:55:35 -08002680static const struct file_operations igmp6_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 .owner = THIS_MODULE,
2682 .open = igmp6_mc_seq_open,
2683 .read = seq_read,
2684 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002685 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686};
2687
2688struct igmp6_mcf_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002689 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 struct net_device *dev;
2691 struct inet6_dev *idev;
2692 struct ifmcaddr6 *im;
2693};
2694
2695#define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2696
2697static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2698{
2699 struct ip6_sf_list *psf = NULL;
2700 struct ifmcaddr6 *im = NULL;
2701 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002702 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Pavel Emelianov7562f872007-05-03 15:13:45 -07002704 state->idev = NULL;
2705 state->im = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002706 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 struct inet6_dev *idev;
Eric Dumazetce81b762009-11-11 17:34:30 +00002708 idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 if (unlikely(idev == NULL))
2710 continue;
2711 read_lock_bh(&idev->lock);
2712 im = idev->mc_list;
2713 if (likely(im != NULL)) {
2714 spin_lock_bh(&im->mca_lock);
2715 psf = im->mca_sources;
2716 if (likely(psf != NULL)) {
2717 state->im = im;
2718 state->idev = idev;
2719 break;
2720 }
2721 spin_unlock_bh(&im->mca_lock);
2722 }
2723 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 }
2725 return psf;
2726}
2727
2728static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2729{
2730 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2731
2732 psf = psf->sf_next;
2733 while (!psf) {
2734 spin_unlock_bh(&state->im->mca_lock);
2735 state->im = state->im->next;
2736 while (!state->im) {
Eric Dumazetce81b762009-11-11 17:34:30 +00002737 if (likely(state->idev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 read_unlock_bh(&state->idev->lock);
Eric Dumazetce81b762009-11-11 17:34:30 +00002739
2740 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 if (!state->dev) {
2742 state->idev = NULL;
2743 goto out;
2744 }
Eric Dumazetce81b762009-11-11 17:34:30 +00002745 state->idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 if (!state->idev)
2747 continue;
2748 read_lock_bh(&state->idev->lock);
2749 state->im = state->idev->mc_list;
2750 }
2751 if (!state->im)
2752 break;
2753 spin_lock_bh(&state->im->mca_lock);
2754 psf = state->im->mca_sources;
2755 }
2756out:
2757 return psf;
2758}
2759
2760static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2761{
2762 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2763 if (psf)
2764 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2765 --pos;
2766 return pos ? NULL : psf;
2767}
2768
2769static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetce81b762009-11-11 17:34:30 +00002770 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
Eric Dumazetce81b762009-11-11 17:34:30 +00002772 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2774}
2775
2776static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2777{
2778 struct ip6_sf_list *psf;
2779 if (v == SEQ_START_TOKEN)
2780 psf = igmp6_mcf_get_first(seq);
2781 else
2782 psf = igmp6_mcf_get_next(seq, v);
2783 ++*pos;
2784 return psf;
2785}
2786
2787static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetce81b762009-11-11 17:34:30 +00002788 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789{
2790 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2791 if (likely(state->im != NULL)) {
2792 spin_unlock_bh(&state->im->mca_lock);
2793 state->im = NULL;
2794 }
2795 if (likely(state->idev != NULL)) {
2796 read_unlock_bh(&state->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 state->idev = NULL;
2798 }
2799 state->dev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002800 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801}
2802
2803static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2804{
2805 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2806 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2807
2808 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002809 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 "%3s %6s "
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002811 "%32s %32s %6s %6s\n", "Idx",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 "Device", "Multicast Address",
2813 "Source Address", "INC", "EXC");
2814 } else {
2815 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002816 "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 state->dev->ifindex, state->dev->name,
Harvey Harrisonb0711952008-10-28 16:05:40 -07002818 &state->im->mca_addr,
2819 &psf->sf_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 psf->sf_count[MCAST_INCLUDE],
2821 psf->sf_count[MCAST_EXCLUDE]);
2822 }
2823 return 0;
2824}
2825
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002826static const struct seq_operations igmp6_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 .start = igmp6_mcf_seq_start,
2828 .next = igmp6_mcf_seq_next,
2829 .stop = igmp6_mcf_seq_stop,
2830 .show = igmp6_mcf_seq_show,
2831};
2832
2833static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2834{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002835 return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
2836 sizeof(struct igmp6_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837}
2838
Arjan van de Ven9a321442007-02-12 00:55:35 -08002839static const struct file_operations igmp6_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 .owner = THIS_MODULE,
2841 .open = igmp6_mcf_seq_open,
2842 .read = seq_read,
2843 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002844 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845};
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002846
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002847static int __net_init igmp6_proc_init(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002848{
2849 int err;
2850
2851 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002852 if (!proc_create("igmp6", S_IRUGO, net->proc_net, &igmp6_mc_seq_fops))
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002853 goto out;
Gao fengd4beaa62013-02-18 01:34:54 +00002854 if (!proc_create("mcfilter6", S_IRUGO, net->proc_net,
2855 &igmp6_mcf_seq_fops))
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002856 goto out_proc_net_igmp6;
2857
2858 err = 0;
2859out:
2860 return err;
2861
2862out_proc_net_igmp6:
Gao fengece31ff2013-02-18 01:34:56 +00002863 remove_proc_entry("igmp6", net->proc_net);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002864 goto out;
2865}
2866
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002867static void __net_exit igmp6_proc_exit(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002868{
Gao fengece31ff2013-02-18 01:34:56 +00002869 remove_proc_entry("mcfilter6", net->proc_net);
2870 remove_proc_entry("igmp6", net->proc_net);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002871}
2872#else
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002873static inline int igmp6_proc_init(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002874{
2875 return 0;
2876}
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002877static inline void igmp6_proc_exit(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002878{
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002879}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880#endif
2881
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002882static int __net_init igmp6_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 int err;
2885
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002886 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2887 SOCK_RAW, IPPROTO_ICMPV6, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00002889 pr_err("Failed to initialize the IGMP6 control socket (err %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 err);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002891 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 }
2893
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002894 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002896 err = igmp6_proc_init(net);
2897 if (err)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002898 goto out_sock_create;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002899out:
2900 return err;
2901
2902out_sock_create:
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002903 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002904 goto out;
2905}
2906
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002907static void __net_exit igmp6_net_exit(struct net *net)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002908{
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002909 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002910 igmp6_proc_exit(net);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002911}
2912
2913static struct pernet_operations igmp6_net_ops = {
2914 .init = igmp6_net_init,
2915 .exit = igmp6_net_exit,
2916};
2917
2918int __init igmp6_init(void)
2919{
2920 return register_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921}
2922
2923void igmp6_cleanup(void)
2924{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002925 unregister_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926}