blob: 8992ff262789976ab3d1a75ce666f1e3c4b3e2b4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Multicast support for IPv6
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09008 * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15
16/* Changes:
17 *
18 * yoshfuji : fix format of router-alert option
19 * YOSHIFUJI Hideaki @USAGI:
20 * Fixed source address for MLD message based on
21 * <draft-ietf-magma-mld-source-05.txt>.
22 * YOSHIFUJI Hideaki @USAGI:
23 * - Ignore Queries for invalid addresses.
24 * - MLD for link-local addresses.
25 * David L Stevens <dlstevens@us.ibm.com>:
26 * - MLDv2 support
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30#include <linux/errno.h>
31#include <linux/types.h>
32#include <linux/string.h>
33#include <linux/socket.h>
34#include <linux/sockios.h>
35#include <linux/jiffies.h>
36#include <linux/times.h>
37#include <linux/net.h>
38#include <linux/in.h>
39#include <linux/in6.h>
40#include <linux/netdevice.h>
41#include <linux/if_arp.h>
42#include <linux/route.h>
43#include <linux/init.h>
44#include <linux/proc_fs.h>
45#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +020047#include <linux/pkt_sched.h>
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +090048#include <net/mld.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <linux/netfilter.h>
51#include <linux/netfilter_ipv6.h>
52
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020053#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <net/sock.h>
55#include <net/snmp.h>
56
57#include <net/ipv6.h>
58#include <net/protocol.h>
59#include <net/if_inet6.h>
60#include <net/ndisc.h>
61#include <net/addrconf.h>
62#include <net/ip6_route.h>
Denis V. Lunev1ed85162008-04-03 14:31:03 -070063#include <net/inet_common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#include <net/ip6_checksum.h>
66
67/* Set to 3 to get tracing... */
68#define MCAST_DEBUG 2
69
70#if MCAST_DEBUG >= 3
71#define MDBG(x) printk x
72#else
73#define MDBG(x)
74#endif
75
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +090076/* Ensure that we have struct in6_addr aligned on 32bit word. */
77static void *__mld2_query_bugs[] __attribute__((__unused__)) = {
78 BUILD_BUG_ON_NULL(offsetof(struct mld2_query, mld2q_srcs) % 4),
79 BUILD_BUG_ON_NULL(offsetof(struct mld2_report, mld2r_grec) % 4),
80 BUILD_BUG_ON_NULL(offsetof(struct mld2_grec, grec_mca) % 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081};
82
83static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
84
85/* Big mc list lock for all the sockets */
Eric Dumazet456b61b2010-11-23 13:12:15 +000086static DEFINE_SPINLOCK(ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static void igmp6_join_group(struct ifmcaddr6 *ma);
89static void igmp6_leave_group(struct ifmcaddr6 *ma);
90static void igmp6_timer_handler(unsigned long data);
91
92static void mld_gq_timer_expire(unsigned long data);
93static void mld_ifc_timer_expire(unsigned long data);
94static void mld_ifc_event(struct inet6_dev *idev);
95static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
Eric Dumazetb71d1d42011-04-22 04:53:02 +000096static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static void mld_clear_delrec(struct inet6_dev *idev);
98static int sf_setstate(struct ifmcaddr6 *pmc);
99static void sf_markstate(struct ifmcaddr6 *pmc);
100static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000101static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
102 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 int delta);
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000104static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
105 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 int delta);
107static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
108 struct inet6_dev *idev);
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#define MLD_QRV_DEFAULT 2
Daniel Borkmann89225d12013-09-04 00:19:37 +0200111/* RFC3810, 9.2. Query Interval */
112#define MLD_QI_DEFAULT (125 * HZ)
113/* RFC3810, 9.3. Query Response Interval */
114#define MLD_QRI_DEFAULT (10 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Daniel Borkmann9fd07842013-08-20 12:22:02 +0200116/* RFC3810, 8.1 Query Version Distinctions */
117#define MLD_V1_QUERY_LEN 24
118#define MLD_V2_QUERY_LEN_MIN 28
119
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700120#define MLD_V1_SEEN(idev) (dev_net((idev)->dev)->ipv6.devconf_all->force_mld_version == 1 || \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 (idev)->cnf.force_mld_version == 1 || \
122 ((idev)->mc_v1_seen && \
123 time_before(jiffies, (idev)->mc_v1_seen)))
124
David L Stevens6f4353d2005-12-26 17:03:46 -0800125#define IPV6_MLD_MAX_MSF 64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Brian Haleyab32ea52006-09-22 14:15:41 -0700127int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129/*
130 * socket join on multicast group
131 */
132
Eric Dumazet456b61b2010-11-23 13:12:15 +0000133#define for_each_pmc_rcu(np, pmc) \
134 for (pmc = rcu_dereference(np->ipv6_mc_list); \
135 pmc != NULL; \
136 pmc = rcu_dereference(pmc->next))
137
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200138static int unsolicited_report_interval(struct inet6_dev *idev)
139{
140 int iv;
141
142 if (MLD_V1_SEEN(idev))
143 iv = idev->cnf.mldv1_unsolicited_report_interval;
144 else
145 iv = idev->cnf.mldv2_unsolicited_report_interval;
146
147 return iv > 0 ? iv : 1;
148}
149
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900150int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 struct net_device *dev = NULL;
153 struct ipv6_mc_socklist *mc_lst;
154 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900155 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 int err;
157
158 if (!ipv6_addr_is_multicast(addr))
159 return -EINVAL;
160
Eric Dumazet456b61b2010-11-23 13:12:15 +0000161 rcu_read_lock();
162 for_each_pmc_rcu(np, mc_lst) {
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700163 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
164 ipv6_addr_equal(&mc_lst->addr, addr)) {
Eric Dumazet456b61b2010-11-23 13:12:15 +0000165 rcu_read_unlock();
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700166 return -EADDRINUSE;
167 }
168 }
Eric Dumazet456b61b2010-11-23 13:12:15 +0000169 rcu_read_unlock();
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
172
173 if (mc_lst == NULL)
174 return -ENOMEM;
175
176 mc_lst->next = NULL;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000177 mc_lst->addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Eric Dumazet96b52e62010-06-07 21:05:02 +0000179 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (ifindex == 0) {
181 struct rt6_info *rt;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800182 rt = rt6_lookup(net, addr, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (rt) {
David S. Millerd1918542011-12-28 20:19:20 -0500184 dev = rt->dst.dev;
Amerigo Wang94e187c2012-10-29 00:13:19 +0000185 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187 } else
Eric Dumazet96b52e62010-06-07 21:05:02 +0000188 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 if (dev == NULL) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000191 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
193 return -ENODEV;
194 }
195
196 mc_lst->ifindex = dev->ifindex;
197 mc_lst->sfmode = MCAST_EXCLUDE;
YOSHIFUJI Hideaki196433c2006-01-04 13:56:31 -0800198 rwlock_init(&mc_lst->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 mc_lst->sflist = NULL;
200
201 /*
202 * now add/increase the group membership on the device
203 */
204
205 err = ipv6_dev_mc_inc(dev, addr);
206
207 if (err) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000208 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return err;
211 }
212
Eric Dumazet456b61b2010-11-23 13:12:15 +0000213 spin_lock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 mc_lst->next = np->ipv6_mc_list;
Eric Dumazet456b61b2010-11-23 13:12:15 +0000215 rcu_assign_pointer(np->ipv6_mc_list, mc_lst);
216 spin_unlock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Eric Dumazet96b52e62010-06-07 21:05:02 +0000218 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 return 0;
221}
222
223/*
224 * socket leave on multicast group
225 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900226int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 struct ipv6_pinfo *np = inet6_sk(sk);
Eric Dumazet456b61b2010-11-23 13:12:15 +0000229 struct ipv6_mc_socklist *mc_lst;
230 struct ipv6_mc_socklist __rcu **lnk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900231 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Li Weia858d642012-07-17 15:28:59 +0800233 if (!ipv6_addr_is_multicast(addr))
234 return -EINVAL;
235
Eric Dumazet456b61b2010-11-23 13:12:15 +0000236 spin_lock(&ipv6_sk_mc_lock);
237 for (lnk = &np->ipv6_mc_list;
238 (mc_lst = rcu_dereference_protected(*lnk,
239 lockdep_is_held(&ipv6_sk_mc_lock))) !=NULL ;
240 lnk = &mc_lst->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
242 ipv6_addr_equal(&mc_lst->addr, addr)) {
243 struct net_device *dev;
244
245 *lnk = mc_lst->next;
Eric Dumazet456b61b2010-11-23 13:12:15 +0000246 spin_unlock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Eric Dumazet96b52e62010-06-07 21:05:02 +0000248 rcu_read_lock();
249 dev = dev_get_by_index_rcu(net, mc_lst->ifindex);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800250 if (dev != NULL) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000251 struct inet6_dev *idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
David L Stevensacd6e002006-08-17 16:27:39 -0700253 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000254 if (idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
David L Stevensacd6e002006-08-17 16:27:39 -0700256 } else
257 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000258 rcu_read_unlock();
Eric Dumazet456b61b2010-11-23 13:12:15 +0000259 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
Lai Jiangshane3cbf282011-03-18 12:00:50 +0800260 kfree_rcu(mc_lst, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return 0;
262 }
263 }
Eric Dumazet456b61b2010-11-23 13:12:15 +0000264 spin_unlock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
David L Stevens9951f032005-07-08 17:47:28 -0700266 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Eric Dumazet96b52e62010-06-07 21:05:02 +0000269/* called with rcu_read_lock() */
270static struct inet6_dev *ip6_mc_find_dev_rcu(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000271 const struct in6_addr *group,
Eric Dumazet96b52e62010-06-07 21:05:02 +0000272 int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct net_device *dev = NULL;
275 struct inet6_dev *idev = NULL;
276
277 if (ifindex == 0) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000278 struct rt6_info *rt = rt6_lookup(net, group, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (rt) {
David S. Millerd1918542011-12-28 20:19:20 -0500281 dev = rt->dst.dev;
Amerigo Wang94e187c2012-10-29 00:13:19 +0000282 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284 } else
Eric Dumazet96b52e62010-06-07 21:05:02 +0000285 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 if (!dev)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000288 return NULL;
289 idev = __in6_dev_get(dev);
Ilpo Järvinen448eb712008-12-14 23:15:21 -0800290 if (!idev)
Joe Perches8a22c992010-11-14 17:05:00 +0000291 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 read_lock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000293 if (idev->dead) {
294 read_unlock_bh(&idev->lock);
295 return NULL;
296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return idev;
298}
299
300void ipv6_sock_mc_close(struct sock *sk)
301{
302 struct ipv6_pinfo *np = inet6_sk(sk);
303 struct ipv6_mc_socklist *mc_lst;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900304 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Eric Dumazet0e1efe92012-12-05 09:18:10 +0000306 if (!rcu_access_pointer(np->ipv6_mc_list))
307 return;
308
Eric Dumazet456b61b2010-11-23 13:12:15 +0000309 spin_lock(&ipv6_sk_mc_lock);
310 while ((mc_lst = rcu_dereference_protected(np->ipv6_mc_list,
311 lockdep_is_held(&ipv6_sk_mc_lock))) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 struct net_device *dev;
313
314 np->ipv6_mc_list = mc_lst->next;
Eric Dumazet456b61b2010-11-23 13:12:15 +0000315 spin_unlock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Eric Dumazet96b52e62010-06-07 21:05:02 +0000317 rcu_read_lock();
318 dev = dev_get_by_index_rcu(net, mc_lst->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (dev) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000320 struct inet6_dev *idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
David L Stevensacd6e002006-08-17 16:27:39 -0700322 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000323 if (idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
David L Stevensacd6e002006-08-17 16:27:39 -0700325 } else
326 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000327 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Eric Dumazet456b61b2010-11-23 13:12:15 +0000329 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
Lai Jiangshane3cbf282011-03-18 12:00:50 +0800330 kfree_rcu(mc_lst, rcu);
Eric Dumazet456b61b2010-11-23 13:12:15 +0000331
332 spin_lock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
Eric Dumazet456b61b2010-11-23 13:12:15 +0000334 spin_unlock(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337int ip6_mc_source(int add, int omode, struct sock *sk,
338 struct group_source_req *pgsr)
339{
340 struct in6_addr *source, *group;
341 struct ipv6_mc_socklist *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 struct inet6_dev *idev;
343 struct ipv6_pinfo *inet6 = inet6_sk(sk);
344 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900345 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 int i, j, rv;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700347 int leavegroup = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800348 int pmclocked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 int err;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
352 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
353
354 if (!ipv6_addr_is_multicast(group))
355 return -EINVAL;
356
Eric Dumazet96b52e62010-06-07 21:05:02 +0000357 rcu_read_lock();
358 idev = ip6_mc_find_dev_rcu(net, group, pgsr->gsr_interface);
359 if (!idev) {
360 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return -ENODEV;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 err = -EADDRNOTAVAIL;
365
Eric Dumazet456b61b2010-11-23 13:12:15 +0000366 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
368 continue;
369 if (ipv6_addr_equal(&pmc->addr, group))
370 break;
371 }
David L Stevens917f2f12005-07-08 17:45:16 -0700372 if (!pmc) { /* must have a prior join */
373 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 /* if a source filter was set, must be the same mode as before */
377 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -0700378 if (pmc->sfmode != omode) {
379 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 } else if (pmc->sfmode != omode) {
383 /* allow mode switches for empty-set filters */
384 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
385 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
386 pmc->sfmode = omode;
387 }
388
Eric Dumazet96b52e62010-06-07 21:05:02 +0000389 write_lock(&pmc->sflock);
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800390 pmclocked = 1;
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 psl = pmc->sflist;
393 if (!add) {
394 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -0700395 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 rv = !0;
397 for (i=0; i<psl->sl_count; i++) {
YOSHIFUJI Hideaki / 吉藤英明07c2fec2013-01-29 12:48:23 +0000398 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (rv == 0)
400 break;
401 }
402 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -0700403 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700405 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
406 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
407 leavegroup = 1;
408 goto done;
409 }
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /* update the interface filter */
412 ip6_mc_del_src(idev, group, omode, 1, source, 1);
413
414 for (j=i+1; j<psl->sl_count; j++)
415 psl->sl_addr[j-1] = psl->sl_addr[j];
416 psl->sl_count--;
417 err = 0;
418 goto done;
419 }
420 /* else, add a new source to the filter */
421
422 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
423 err = -ENOBUFS;
424 goto done;
425 }
426 if (!psl || psl->sl_count == psl->sl_max) {
427 struct ip6_sf_socklist *newpsl;
428 int count = IP6_SFBLOCK;
429
430 if (psl)
431 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800432 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (!newpsl) {
434 err = -ENOBUFS;
435 goto done;
436 }
437 newpsl->sl_max = count;
438 newpsl->sl_count = count - IP6_SFBLOCK;
439 if (psl) {
440 for (i=0; i<psl->sl_count; i++)
441 newpsl->sl_addr[i] = psl->sl_addr[i];
442 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
443 }
444 pmc->sflist = psl = newpsl;
445 }
446 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
447 for (i=0; i<psl->sl_count; i++) {
YOSHIFUJI Hideaki / 吉藤英明07c2fec2013-01-29 12:48:23 +0000448 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
Jean Sacren56db1c52013-02-03 21:34:10 +0000449 if (rv == 0) /* There is an error in the address. */
450 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 for (j=psl->sl_count-1; j>=i; j--)
453 psl->sl_addr[j+1] = psl->sl_addr[j];
454 psl->sl_addr[i] = *source;
455 psl->sl_count++;
456 err = 0;
457 /* update the interface list */
458 ip6_mc_add_src(idev, group, omode, 1, source, 1);
459done:
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800460 if (pmclocked)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000461 write_unlock(&pmc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000463 rcu_read_unlock();
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700464 if (leavegroup)
465 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return err;
467}
468
469int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
470{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000471 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 struct ipv6_mc_socklist *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 struct inet6_dev *idev;
474 struct ipv6_pinfo *inet6 = inet6_sk(sk);
475 struct ip6_sf_socklist *newpsl, *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900476 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -0700477 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 int i, err;
479
480 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
481
482 if (!ipv6_addr_is_multicast(group))
483 return -EINVAL;
484 if (gsf->gf_fmode != MCAST_INCLUDE &&
485 gsf->gf_fmode != MCAST_EXCLUDE)
486 return -EINVAL;
487
Eric Dumazet96b52e62010-06-07 21:05:02 +0000488 rcu_read_lock();
489 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Eric Dumazet96b52e62010-06-07 21:05:02 +0000491 if (!idev) {
492 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return -ENODEV;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
David S. Miller9c059892005-07-08 21:44:39 -0700496 err = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800497
David L Stevens9951f032005-07-08 17:47:28 -0700498 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
499 leavegroup = 1;
500 goto done;
501 }
502
Eric Dumazet456b61b2010-11-23 13:12:15 +0000503 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (pmc->ifindex != gsf->gf_interface)
505 continue;
506 if (ipv6_addr_equal(&pmc->addr, group))
507 break;
508 }
David L Stevens917f2f12005-07-08 17:45:16 -0700509 if (!pmc) { /* must have a prior join */
510 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (gsf->gf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800514 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
515 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (!newpsl) {
517 err = -ENOBUFS;
518 goto done;
519 }
520 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
521 for (i=0; i<newpsl->sl_count; ++i) {
522 struct sockaddr_in6 *psin6;
523
524 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
525 newpsl->sl_addr[i] = psin6->sin6_addr;
526 }
527 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
528 newpsl->sl_count, newpsl->sl_addr, 0);
529 if (err) {
530 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
531 goto done;
532 }
Yan Zheng8713dbf2005-10-28 08:02:08 +0800533 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +0800535 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
536 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800537
Eric Dumazet96b52e62010-06-07 21:05:02 +0000538 write_lock(&pmc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 psl = pmc->sflist;
540 if (psl) {
541 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
542 psl->sl_count, psl->sl_addr, 0);
543 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
544 } else
545 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
546 pmc->sflist = newpsl;
547 pmc->sfmode = gsf->gf_fmode;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000548 write_unlock(&pmc->sflock);
David L Stevens917f2f12005-07-08 17:45:16 -0700549 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550done:
551 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000552 rcu_read_unlock();
David L Stevens9951f032005-07-08 17:47:28 -0700553 if (leavegroup)
554 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return err;
556}
557
558int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
559 struct group_filter __user *optval, int __user *optlen)
560{
561 int err, i, count, copycount;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000562 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 struct ipv6_mc_socklist *pmc;
564 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct ipv6_pinfo *inet6 = inet6_sk(sk);
566 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900567 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
570
571 if (!ipv6_addr_is_multicast(group))
572 return -EINVAL;
573
Eric Dumazet96b52e62010-06-07 21:05:02 +0000574 rcu_read_lock();
575 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Eric Dumazet96b52e62010-06-07 21:05:02 +0000577 if (!idev) {
578 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return -ENODEV;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 err = -EADDRNOTAVAIL;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800583 /*
584 * changes to the ipv6_mc_list require the socket lock and
585 * a read lock on ip6_sk_mc_lock. We have the socket lock,
586 * so reading the list is safe.
587 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Eric Dumazet456b61b2010-11-23 13:12:15 +0000589 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (pmc->ifindex != gsf->gf_interface)
591 continue;
592 if (ipv6_addr_equal(group, &pmc->addr))
593 break;
594 }
595 if (!pmc) /* must have a prior join */
596 goto done;
597 gsf->gf_fmode = pmc->sfmode;
598 psl = pmc->sflist;
599 count = psl ? psl->sl_count : 0;
600 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000601 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
604 gsf->gf_numsrc = count;
605 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
606 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
607 return -EFAULT;
608 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800609 /* changes to psl require the socket lock, a read lock on
610 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
611 * have the socket lock, so reading here is safe.
612 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 for (i=0; i<copycount; i++) {
614 struct sockaddr_in6 *psin6;
615 struct sockaddr_storage ss;
616
617 psin6 = (struct sockaddr_in6 *)&ss;
618 memset(&ss, 0, sizeof(ss));
619 psin6->sin6_family = AF_INET6;
620 psin6->sin6_addr = psl->sl_addr[i];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900621 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return -EFAULT;
623 }
624 return 0;
625done:
626 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000627 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return err;
629}
630
Eric Dumazeta50feda2012-05-18 18:57:34 +0000631bool inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
632 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 struct ipv6_pinfo *np = inet6_sk(sk);
635 struct ipv6_mc_socklist *mc;
636 struct ip6_sf_socklist *psl;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000637 bool rv = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Eric Dumazet456b61b2010-11-23 13:12:15 +0000639 rcu_read_lock();
640 for_each_pmc_rcu(np, mc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (ipv6_addr_equal(&mc->addr, mc_addr))
642 break;
643 }
644 if (!mc) {
Eric Dumazet456b61b2010-11-23 13:12:15 +0000645 rcu_read_unlock();
Eric Dumazeta50feda2012-05-18 18:57:34 +0000646 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800648 read_lock(&mc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 psl = mc->sflist;
650 if (!psl) {
651 rv = mc->sfmode == MCAST_EXCLUDE;
652 } else {
653 int i;
654
655 for (i=0; i<psl->sl_count; i++) {
656 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
657 break;
658 }
659 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000660 rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000662 rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800664 read_unlock(&mc->sflock);
Eric Dumazet456b61b2010-11-23 13:12:15 +0000665 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 return rv;
668}
669
670static void ma_put(struct ifmcaddr6 *mc)
671{
672 if (atomic_dec_and_test(&mc->mca_refcnt)) {
673 in6_dev_put(mc->idev);
674 kfree(mc);
675 }
676}
677
678static void igmp6_group_added(struct ifmcaddr6 *mc)
679{
680 struct net_device *dev = mc->idev->dev;
681 char buf[MAX_ADDR_LEN];
682
YOSHIFUJI Hideaki / 吉藤英明ec16ef22013-02-09 04:29:58 +0000683 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
684 IPV6_ADDR_SCOPE_LINKLOCAL)
685 return;
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 spin_lock_bh(&mc->mca_lock);
688 if (!(mc->mca_flags&MAF_LOADED)) {
689 mc->mca_flags |= MAF_LOADED;
690 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000691 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693 spin_unlock_bh(&mc->mca_lock);
694
695 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
696 return;
697
698 if (MLD_V1_SEEN(mc->idev)) {
699 igmp6_join_group(mc);
700 return;
701 }
702 /* else v2 */
703
704 mc->mca_crcount = mc->idev->mc_qrv;
705 mld_ifc_event(mc->idev);
706}
707
708static void igmp6_group_dropped(struct ifmcaddr6 *mc)
709{
710 struct net_device *dev = mc->idev->dev;
711 char buf[MAX_ADDR_LEN];
712
YOSHIFUJI Hideaki / 吉藤英明ec16ef22013-02-09 04:29:58 +0000713 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
714 IPV6_ADDR_SCOPE_LINKLOCAL)
715 return;
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 spin_lock_bh(&mc->mca_lock);
718 if (mc->mca_flags&MAF_LOADED) {
719 mc->mca_flags &= ~MAF_LOADED;
720 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000721 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723
724 if (mc->mca_flags & MAF_NOREPORT)
725 goto done;
726 spin_unlock_bh(&mc->mca_lock);
727
728 if (!mc->idev->dead)
729 igmp6_leave_group(mc);
730
731 spin_lock_bh(&mc->mca_lock);
732 if (del_timer(&mc->mca_timer))
733 atomic_dec(&mc->mca_refcnt);
734done:
735 ip6_mc_clear_src(mc);
736 spin_unlock_bh(&mc->mca_lock);
737}
738
739/*
740 * deleted ifmcaddr6 manipulation
741 */
742static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
743{
744 struct ifmcaddr6 *pmc;
745
746 /* this is an "ifmcaddr6" for convenience; only the fields below
747 * are actually used. In particular, the refcnt and users are not
748 * used for management of the delete list. Using the same structure
749 * for deleted items allows change reports to use common code with
750 * non-deleted or query-response MCA's.
751 */
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800752 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (!pmc)
754 return;
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 spin_lock_bh(&im->mca_lock);
757 spin_lock_init(&pmc->mca_lock);
758 pmc->idev = im->idev;
759 in6_dev_hold(idev);
760 pmc->mca_addr = im->mca_addr;
761 pmc->mca_crcount = idev->mc_qrv;
762 pmc->mca_sfmode = im->mca_sfmode;
763 if (pmc->mca_sfmode == MCAST_INCLUDE) {
764 struct ip6_sf_list *psf;
765
766 pmc->mca_tomb = im->mca_tomb;
767 pmc->mca_sources = im->mca_sources;
768 im->mca_tomb = im->mca_sources = NULL;
769 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
770 psf->sf_crcount = pmc->mca_crcount;
771 }
772 spin_unlock_bh(&im->mca_lock);
773
Stephen Hemminger6457d262010-02-17 18:48:44 -0800774 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 pmc->next = idev->mc_tomb;
776 idev->mc_tomb = pmc;
Stephen Hemminger6457d262010-02-17 18:48:44 -0800777 spin_unlock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000780static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *pmca)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 struct ifmcaddr6 *pmc, *pmc_prev;
783 struct ip6_sf_list *psf, *psf_next;
784
Stephen Hemminger6457d262010-02-17 18:48:44 -0800785 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 pmc_prev = NULL;
787 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
788 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
789 break;
790 pmc_prev = pmc;
791 }
792 if (pmc) {
793 if (pmc_prev)
794 pmc_prev->next = pmc->next;
795 else
796 idev->mc_tomb = pmc->next;
797 }
Stephen Hemminger6457d262010-02-17 18:48:44 -0800798 spin_unlock_bh(&idev->mc_lock);
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (pmc) {
801 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
802 psf_next = psf->sf_next;
803 kfree(psf);
804 }
805 in6_dev_put(pmc->idev);
806 kfree(pmc);
807 }
808}
809
810static void mld_clear_delrec(struct inet6_dev *idev)
811{
812 struct ifmcaddr6 *pmc, *nextpmc;
813
Stephen Hemminger6457d262010-02-17 18:48:44 -0800814 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 pmc = idev->mc_tomb;
816 idev->mc_tomb = NULL;
Stephen Hemminger6457d262010-02-17 18:48:44 -0800817 spin_unlock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 for (; pmc; pmc = nextpmc) {
820 nextpmc = pmc->next;
821 ip6_mc_clear_src(pmc);
822 in6_dev_put(pmc->idev);
823 kfree(pmc);
824 }
825
826 /* clear dead sources, too */
827 read_lock_bh(&idev->lock);
828 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
829 struct ip6_sf_list *psf, *psf_next;
830
831 spin_lock_bh(&pmc->mca_lock);
832 psf = pmc->mca_tomb;
833 pmc->mca_tomb = NULL;
834 spin_unlock_bh(&pmc->mca_lock);
835 for (; psf; psf=psf_next) {
836 psf_next = psf->sf_next;
837 kfree(psf);
838 }
839 }
840 read_unlock_bh(&idev->lock);
841}
842
843
844/*
845 * device multicast group inc (add if not found)
846 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900847int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 struct ifmcaddr6 *mc;
850 struct inet6_dev *idev;
851
Eric Dumazet96b52e62010-06-07 21:05:02 +0000852 /* we need to take a reference on idev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 idev = in6_dev_get(dev);
854
855 if (idev == NULL)
856 return -EINVAL;
857
858 write_lock_bh(&idev->lock);
859 if (idev->dead) {
860 write_unlock_bh(&idev->lock);
861 in6_dev_put(idev);
862 return -ENODEV;
863 }
864
865 for (mc = idev->mc_list; mc; mc = mc->next) {
866 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
867 mc->mca_users++;
868 write_unlock_bh(&idev->lock);
869 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
870 NULL, 0);
871 in6_dev_put(idev);
872 return 0;
873 }
874 }
875
876 /*
877 * not found: create a new one.
878 */
879
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800880 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 if (mc == NULL) {
883 write_unlock_bh(&idev->lock);
884 in6_dev_put(idev);
885 return -ENOMEM;
886 }
887
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800888 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000890 mc->mca_addr = *addr;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000891 mc->idev = idev; /* (reference taken) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 mc->mca_users = 1;
893 /* mca_stamp should be updated upon changes */
894 mc->mca_cstamp = mc->mca_tstamp = jiffies;
895 atomic_set(&mc->mca_refcnt, 2);
896 spin_lock_init(&mc->mca_lock);
897
898 /* initial mode is (EX, empty) */
899 mc->mca_sfmode = MCAST_EXCLUDE;
900 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
901
902 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
903 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
904 mc->mca_flags |= MAF_NOREPORT;
905
906 mc->next = idev->mc_list;
907 idev->mc_list = mc;
908 write_unlock_bh(&idev->lock);
909
910 mld_del_delrec(idev, &mc->mca_addr);
911 igmp6_group_added(mc);
912 ma_put(mc);
913 return 0;
914}
915
916/*
917 * device multicast group del
918 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900919int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 struct ifmcaddr6 *ma, **map;
922
923 write_lock_bh(&idev->lock);
924 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
925 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
926 if (--ma->mca_users == 0) {
927 *map = ma->next;
928 write_unlock_bh(&idev->lock);
929
930 igmp6_group_dropped(ma);
931
932 ma_put(ma);
933 return 0;
934 }
935 write_unlock_bh(&idev->lock);
936 return 0;
937 }
938 }
939 write_unlock_bh(&idev->lock);
940
941 return -ENOENT;
942}
943
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900944int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Eric Dumazet96b52e62010-06-07 21:05:02 +0000946 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 int err;
948
Eric Dumazet96b52e62010-06-07 21:05:02 +0000949 rcu_read_lock();
950
951 idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (!idev)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000953 err = -ENODEV;
954 else
955 err = __ipv6_dev_mc_dec(idev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Eric Dumazet96b52e62010-06-07 21:05:02 +0000957 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return err;
959}
960
961/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 * check if the interface/address pair is valid
963 */
Eric Dumazeta50feda2012-05-18 18:57:34 +0000964bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
965 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
967 struct inet6_dev *idev;
968 struct ifmcaddr6 *mc;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000969 bool rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Eric Dumazet96b52e62010-06-07 21:05:02 +0000971 rcu_read_lock();
972 idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (idev) {
974 read_lock_bh(&idev->lock);
975 for (mc = idev->mc_list; mc; mc=mc->next) {
976 if (ipv6_addr_equal(&mc->mca_addr, group))
977 break;
978 }
979 if (mc) {
980 if (src_addr && !ipv6_addr_any(src_addr)) {
981 struct ip6_sf_list *psf;
982
983 spin_lock_bh(&mc->mca_lock);
984 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
985 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
986 break;
987 }
988 if (psf)
989 rv = psf->sf_count[MCAST_INCLUDE] ||
990 psf->sf_count[MCAST_EXCLUDE] !=
991 mc->mca_sfcount[MCAST_EXCLUDE];
992 else
993 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
994 spin_unlock_bh(&mc->mca_lock);
995 } else
Eric Dumazeta50feda2012-05-18 18:57:34 +0000996 rv = true; /* don't filter unspecified source */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
Eric Dumazet96b52e62010-06-07 21:05:02 +00001000 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return rv;
1002}
1003
1004static void mld_gq_start_timer(struct inet6_dev *idev)
1005{
Daniel Borkmannc2cef4e2013-08-20 12:22:01 +02001006 unsigned long tv = net_random() % idev->mc_maxdelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 idev->mc_gq_running = 1;
1009 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1010 in6_dev_hold(idev);
1011}
1012
Daniel Borkmannc2cef4e2013-08-20 12:22:01 +02001013static void mld_ifc_start_timer(struct inet6_dev *idev, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Daniel Borkmannc2cef4e2013-08-20 12:22:01 +02001015 unsigned long tv = net_random() % delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1018 in6_dev_hold(idev);
1019}
1020
Daniel Borkmannc2cef4e2013-08-20 12:22:01 +02001021static void mld_dad_start_timer(struct inet6_dev *idev, unsigned long delay)
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02001022{
Daniel Borkmannc2cef4e2013-08-20 12:22:01 +02001023 unsigned long tv = net_random() % delay;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02001024
1025 if (!mod_timer(&idev->mc_dad_timer, jiffies+tv+2))
1026 in6_dev_hold(idev);
1027}
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029/*
1030 * IGMP handling (alias multicast ICMPv6 messages)
1031 */
1032
1033static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1034{
1035 unsigned long delay = resptime;
1036
1037 /* Do not start timer for these addresses */
1038 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1039 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1040 return;
1041
1042 if (del_timer(&ma->mca_timer)) {
1043 atomic_dec(&ma->mca_refcnt);
1044 delay = ma->mca_timer.expires - jiffies;
1045 }
1046
1047 if (delay >= resptime) {
1048 if (resptime)
1049 delay = net_random() % resptime;
1050 else
1051 delay = 1;
1052 }
1053 ma->mca_timer.expires = jiffies + delay;
1054 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1055 atomic_inc(&ma->mca_refcnt);
1056 ma->mca_flags |= MAF_TIMER_RUNNING;
1057}
1058
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001059/* mark EXCLUDE-mode sources */
Eric Dumazeta50feda2012-05-18 18:57:34 +00001060static bool mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1061 const struct in6_addr *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
1063 struct ip6_sf_list *psf;
1064 int i, scount;
1065
1066 scount = 0;
1067 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1068 if (scount == nsrcs)
1069 break;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001070 for (i=0; i<nsrcs; i++) {
1071 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +00001072 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001073 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1074 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +08001075 break;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001076 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1077 scount++;
1078 break;
1079 }
1080 }
1081 }
1082 pmc->mca_flags &= ~MAF_GSQUERY;
1083 if (scount == nsrcs) /* all sources excluded */
Eric Dumazeta50feda2012-05-18 18:57:34 +00001084 return false;
1085 return true;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001086}
1087
Eric Dumazeta50feda2012-05-18 18:57:34 +00001088static bool mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1089 const struct in6_addr *srcs)
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001090{
1091 struct ip6_sf_list *psf;
1092 int i, scount;
1093
1094 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1095 return mld_xmarksources(pmc, nsrcs, srcs);
1096
1097 /* mark INCLUDE-mode sources */
1098
1099 scount = 0;
1100 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1101 if (scount == nsrcs)
1102 break;
1103 for (i=0; i<nsrcs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1105 psf->sf_gsresp = 1;
1106 scount++;
1107 break;
1108 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001111 if (!scount) {
1112 pmc->mca_flags &= ~MAF_GSQUERY;
Eric Dumazeta50feda2012-05-18 18:57:34 +00001113 return false;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001114 }
1115 pmc->mca_flags |= MAF_GSQUERY;
Eric Dumazeta50feda2012-05-18 18:57:34 +00001116 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118
Daniel Borkmann89225d12013-09-04 00:19:37 +02001119static void mld_set_v1_mode(struct inet6_dev *idev)
1120{
1121 /* RFC3810, relevant sections:
1122 * - 9.1. Robustness Variable
1123 * - 9.2. Query Interval
1124 * - 9.3. Query Response Interval
1125 * - 9.12. Older Version Querier Present Timeout
1126 */
1127 unsigned long switchback;
1128
1129 switchback = (idev->mc_qrv * idev->mc_qi) + idev->mc_qri;
1130
1131 idev->mc_v1_seen = jiffies + switchback;
1132}
1133
1134static void mld_update_qrv(struct inet6_dev *idev,
1135 const struct mld2_query *mlh2)
1136{
1137 /* RFC3810, relevant sections:
1138 * - 5.1.8. QRV (Querier's Robustness Variable)
1139 * - 9.1. Robustness Variable
1140 */
1141
1142 /* The value of the Robustness Variable MUST NOT be zero,
1143 * and SHOULD NOT be one. Catch this here if we ever run
1144 * into such a case in future.
1145 */
1146 WARN_ON(idev->mc_qrv == 0);
1147
1148 if (mlh2->mld2q_qrv > 0)
1149 idev->mc_qrv = mlh2->mld2q_qrv;
1150
1151 if (unlikely(idev->mc_qrv < 2)) {
1152 net_warn_ratelimited("IPv6: MLD: clamping QRV from %u to %u!\n",
1153 idev->mc_qrv, MLD_QRV_DEFAULT);
1154 idev->mc_qrv = MLD_QRV_DEFAULT;
1155 }
1156}
1157
1158static void mld_update_qi(struct inet6_dev *idev,
1159 const struct mld2_query *mlh2)
1160{
1161 /* RFC3810, relevant sections:
1162 * - 5.1.9. QQIC (Querier's Query Interval Code)
1163 * - 9.2. Query Interval
1164 * - 9.12. Older Version Querier Present Timeout
1165 * (the [Query Interval] in the last Query received)
1166 */
1167 unsigned long mc_qqi;
1168
1169 if (mlh2->mld2q_qqic < 128) {
1170 mc_qqi = mlh2->mld2q_qqic;
1171 } else {
1172 unsigned long mc_man, mc_exp;
1173
1174 mc_exp = MLDV2_QQIC_EXP(mlh2->mld2q_qqic);
1175 mc_man = MLDV2_QQIC_MAN(mlh2->mld2q_qqic);
1176
1177 mc_qqi = (mc_man | 0x10) << (mc_exp + 3);
1178 }
1179
1180 idev->mc_qi = mc_qqi * HZ;
1181}
1182
1183static void mld_update_qri(struct inet6_dev *idev,
1184 const struct mld2_query *mlh2)
1185{
1186 /* RFC3810, relevant sections:
1187 * - 5.1.3. Maximum Response Code
1188 * - 9.3. Query Response Interval
1189 */
1190 unsigned long mc_qri, mc_mrc = ntohs(mlh2->mld2q_mrc);
1191
1192 if (mc_mrc < 32768) {
1193 mc_qri = mc_mrc;
1194 } else {
1195 unsigned long mc_man, mc_exp;
1196
1197 mc_exp = MLDV2_MRC_EXP(mc_mrc);
1198 mc_man = MLDV2_MRC_MAN(mc_mrc);
1199
1200 mc_qri = (mc_man | 0x1000) << (mc_exp + 3);
1201 }
1202
1203 idev->mc_qri = msecs_to_jiffies(mc_qri);
1204}
1205
Eric Dumazet96b52e62010-06-07 21:05:02 +00001206/* called with rcu_read_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207int igmp6_event_query(struct sk_buff *skb)
1208{
Yan Zheng97300b52005-10-31 20:09:45 +08001209 struct mld2_query *mlh2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 struct ifmcaddr6 *ma;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001211 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 unsigned long max_delay;
1213 struct inet6_dev *idev;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001214 struct mld_msg *mld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 int group_type;
1216 int mark = 0;
1217 int len;
1218
1219 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1220 return -EINVAL;
1221
1222 /* compute payload length excluding extension headers */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001223 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001224 len -= skb_network_header_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 /* Drop queries with not link local source */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001227 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return -EINVAL;
1229
Eric Dumazet96b52e62010-06-07 21:05:02 +00001230 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 if (idev == NULL)
1233 return 0;
1234
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001235 mld = (struct mld_msg *)icmp6_hdr(skb);
1236 group = &mld->mld_mca;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 group_type = ipv6_addr_type(group);
1238
1239 if (group_type != IPV6_ADDR_ANY &&
Eric Dumazet96b52e62010-06-07 21:05:02 +00001240 !(group_type&IPV6_ADDR_MULTICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Daniel Borkmann9fd07842013-08-20 12:22:02 +02001243 if (len == MLD_V1_QUERY_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 /* MLDv1 router present */
Daniel Borkmann84698962013-08-20 12:22:00 +02001245 max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay));
Daniel Borkmann89225d12013-09-04 00:19:37 +02001246
1247 mld_set_v1_mode(idev);
1248
1249 /* cancel MLDv2 report timer */
1250 idev->mc_gq_running = 0;
1251 if (del_timer(&idev->mc_gq_timer))
1252 __in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 /* cancel the interface change timer */
1255 idev->mc_ifc_count = 0;
1256 if (del_timer(&idev->mc_ifc_timer))
1257 __in6_dev_put(idev);
1258 /* clear deleted report items */
1259 mld_clear_delrec(idev);
Daniel Borkmann9fd07842013-08-20 12:22:02 +02001260 } else if (len >= MLD_V2_QUERY_LEN_MIN) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001261 int srcs_offset = sizeof(struct mld2_query) -
Yan Zheng97300b52005-10-31 20:09:45 +08001262 sizeof(struct icmp6hdr);
Daniel Borkmann89225d12013-09-04 00:19:37 +02001263
1264 /* hosts need to stay in MLDv1 mode, discard MLDv2 queries */
1265 if (MLD_V1_SEEN(idev))
1266 return 0;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001267 if (!pskb_may_pull(skb, srcs_offset))
Yan Zheng97300b52005-10-31 20:09:45 +08001268 return -EINVAL;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001269
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001270 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Daniel Borkmann84698962013-08-20 12:22:00 +02001271
1272 max_delay = max(msecs_to_jiffies(MLDV2_MRC(ntohs(mlh2->mld2q_mrc))), 1UL);
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 idev->mc_maxdelay = max_delay;
Daniel Borkmann84698962013-08-20 12:22:00 +02001275
Daniel Borkmann89225d12013-09-04 00:19:37 +02001276 mld_update_qrv(idev, mlh2);
1277 mld_update_qi(idev, mlh2);
1278 mld_update_qri(idev, mlh2);
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (group_type == IPV6_ADDR_ANY) { /* general query */
Eric Dumazet96b52e62010-06-07 21:05:02 +00001281 if (mlh2->mld2q_nsrcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return -EINVAL; /* no sources allowed */
Eric Dumazet96b52e62010-06-07 21:05:02 +00001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 mld_gq_start_timer(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return 0;
1286 }
1287 /* mark sources to include, if group & source-specific */
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001288 if (mlh2->mld2q_nsrcs != 0) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001289 if (!pskb_may_pull(skb, srcs_offset +
Eric Dumazet96b52e62010-06-07 21:05:02 +00001290 ntohs(mlh2->mld2q_nsrcs) * sizeof(struct in6_addr)))
Yan Zheng97300b52005-10-31 20:09:45 +08001291 return -EINVAL;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001292
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001293 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Yan Zheng97300b52005-10-31 20:09:45 +08001294 mark = 1;
1295 }
Eric Dumazet96b52e62010-06-07 21:05:02 +00001296 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 read_lock_bh(&idev->lock);
1300 if (group_type == IPV6_ADDR_ANY) {
1301 for (ma = idev->mc_list; ma; ma=ma->next) {
1302 spin_lock_bh(&ma->mca_lock);
1303 igmp6_group_queried(ma, max_delay);
1304 spin_unlock_bh(&ma->mca_lock);
1305 }
1306 } else {
1307 for (ma = idev->mc_list; ma; ma=ma->next) {
David L Stevens7add2a42006-01-24 13:06:39 -08001308 if (!ipv6_addr_equal(group, &ma->mca_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 continue;
1310 spin_lock_bh(&ma->mca_lock);
1311 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1312 /* gsquery <- gsquery && mark */
1313 if (!mark)
1314 ma->mca_flags &= ~MAF_GSQUERY;
1315 } else {
1316 /* gsquery <- mark */
1317 if (mark)
1318 ma->mca_flags |= MAF_GSQUERY;
1319 else
1320 ma->mca_flags &= ~MAF_GSQUERY;
1321 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001322 if (!(ma->mca_flags & MAF_GSQUERY) ||
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001323 mld_marksources(ma, ntohs(mlh2->mld2q_nsrcs), mlh2->mld2q_srcs))
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001324 igmp6_group_queried(ma, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 spin_unlock_bh(&ma->mca_lock);
David L Stevens7add2a42006-01-24 13:06:39 -08001326 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328 }
1329 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 return 0;
1332}
1333
Eric Dumazet96b52e62010-06-07 21:05:02 +00001334/* called with rcu_read_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335int igmp6_event_report(struct sk_buff *skb)
1336{
1337 struct ifmcaddr6 *ma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 struct inet6_dev *idev;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001339 struct mld_msg *mld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 int addr_type;
1341
1342 /* Our own report looped back. Ignore it. */
1343 if (skb->pkt_type == PACKET_LOOPBACK)
1344 return 0;
1345
David Stevens24c69272005-12-02 20:32:59 -08001346 /* send our report if the MC router may not have heard this report */
1347 if (skb->pkt_type != PACKET_MULTICAST &&
1348 skb->pkt_type != PACKET_BROADCAST)
1349 return 0;
1350
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001351 if (!pskb_may_pull(skb, sizeof(*mld) - sizeof(struct icmp6hdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return -EINVAL;
1353
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001354 mld = (struct mld_msg *)icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 /* Drop reports with not link local source */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001357 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001358 if (addr_type != IPV6_ADDR_ANY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 !(addr_type&IPV6_ADDR_LINKLOCAL))
1360 return -EINVAL;
1361
Eric Dumazet96b52e62010-06-07 21:05:02 +00001362 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (idev == NULL)
1364 return -ENODEV;
1365
1366 /*
1367 * Cancel the timer for this group
1368 */
1369
1370 read_lock_bh(&idev->lock);
1371 for (ma = idev->mc_list; ma; ma=ma->next) {
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001372 if (ipv6_addr_equal(&ma->mca_addr, &mld->mld_mca)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 spin_lock(&ma->mca_lock);
1374 if (del_timer(&ma->mca_timer))
1375 atomic_dec(&ma->mca_refcnt);
1376 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1377 spin_unlock(&ma->mca_lock);
1378 break;
1379 }
1380 }
1381 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return 0;
1383}
1384
Eric Dumazeta50feda2012-05-18 18:57:34 +00001385static bool is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1386 int gdeleted, int sdeleted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 switch (type) {
1389 case MLD2_MODE_IS_INCLUDE:
1390 case MLD2_MODE_IS_EXCLUDE:
1391 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001392 return false;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001393 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1394 if (pmc->mca_sfmode == MCAST_INCLUDE)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001395 return true;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001396 /* don't include if this source is excluded
1397 * in all filters
1398 */
1399 if (psf->sf_count[MCAST_INCLUDE])
David L Stevens7add2a42006-01-24 13:06:39 -08001400 return type == MLD2_MODE_IS_INCLUDE;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001401 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1402 psf->sf_count[MCAST_EXCLUDE];
1403 }
Eric Dumazeta50feda2012-05-18 18:57:34 +00001404 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 case MLD2_CHANGE_TO_INCLUDE:
1406 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001407 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return psf->sf_count[MCAST_INCLUDE] != 0;
1409 case MLD2_CHANGE_TO_EXCLUDE:
1410 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001411 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1413 psf->sf_count[MCAST_INCLUDE])
Eric Dumazeta50feda2012-05-18 18:57:34 +00001414 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1416 psf->sf_count[MCAST_EXCLUDE];
1417 case MLD2_ALLOW_NEW_SOURCES:
1418 if (gdeleted || !psf->sf_crcount)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001419 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1421 case MLD2_BLOCK_OLD_SOURCES:
1422 if (pmc->mca_sfmode == MCAST_INCLUDE)
1423 return gdeleted || (psf->sf_crcount && sdeleted);
1424 return psf->sf_crcount && !gdeleted && !sdeleted;
1425 }
Eric Dumazeta50feda2012-05-18 18:57:34 +00001426 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
1428
1429static int
1430mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1431{
1432 struct ip6_sf_list *psf;
1433 int scount = 0;
1434
1435 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1436 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1437 continue;
1438 scount++;
1439 }
1440 return scount;
1441}
1442
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +00001443static void ip6_mc_hdr(struct sock *sk, struct sk_buff *skb,
1444 struct net_device *dev,
1445 const struct in6_addr *saddr,
1446 const struct in6_addr *daddr,
1447 int proto, int len)
1448{
1449 struct ipv6hdr *hdr;
1450
1451 skb->protocol = htons(ETH_P_IPV6);
1452 skb->dev = dev;
1453
1454 skb_reset_network_header(skb);
1455 skb_put(skb, sizeof(struct ipv6hdr));
1456 hdr = ipv6_hdr(skb);
1457
1458 ip6_flow_hdr(hdr, 0, 0);
1459
1460 hdr->payload_len = htons(len);
1461 hdr->nexthdr = proto;
1462 hdr->hop_limit = inet6_sk(sk)->hop_limit;
1463
1464 hdr->saddr = *saddr;
1465 hdr->daddr = *daddr;
1466}
1467
Amerigo Wang89657792013-06-29 21:30:49 +08001468static struct sk_buff *mld_newpack(struct inet6_dev *idev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Amerigo Wang89657792013-06-29 21:30:49 +08001470 struct net_device *dev = idev->dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001471 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001472 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 struct sk_buff *skb;
1474 struct mld2_report *pmr;
1475 struct in6_addr addr_buf;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001476 const struct in6_addr *saddr;
Herbert Xua7ae1992011-11-18 02:20:04 +00001477 int hlen = LL_RESERVED_SPACE(dev);
1478 int tlen = dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 int err;
1480 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1481 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1482 IPV6_TLV_PADN, 0 };
1483
1484 /* we assume size > sizeof(ra) here */
Herbert Xua7ae1992011-11-18 02:20:04 +00001485 size += hlen + tlen;
Eric Dumazet72e09ad2010-06-05 03:03:30 -07001486 /* limit our allocations to order-0 page */
1487 size = min_t(int, size, SKB_MAX_ORDER(0, 0));
1488 skb = sock_alloc_send_skb(sk, size, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001490 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return NULL;
1492
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +02001493 skb->priority = TC_PRIO_CONTROL;
Herbert Xua7ae1992011-11-18 02:20:04 +00001494 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Amerigo Wang89657792013-06-29 21:30:49 +08001496 if (__ipv6_get_lladdr(idev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001498 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 * when a valid link-local address is not available.
1500 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001501 saddr = &in6addr_any;
1502 } else
1503 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +00001505 ip6_mc_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1508
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001509 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -03001510 skb_put(skb, sizeof(*pmr));
1511 pmr = (struct mld2_report *)skb_transport_header(skb);
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001512 pmr->mld2r_type = ICMPV6_MLD2_REPORT;
1513 pmr->mld2r_resv1 = 0;
1514 pmr->mld2r_cksum = 0;
1515 pmr->mld2r_resv2 = 0;
1516 pmr->mld2r_ngrec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return skb;
1518}
1519
1520static void mld_sendpack(struct sk_buff *skb)
1521{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001522 struct ipv6hdr *pip6 = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001523 struct mld2_report *pmr =
1524 (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 int payload_len, mldlen;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001526 struct inet6_dev *idev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001527 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 int err;
David S. Miller4c9483b2011-03-12 16:22:43 -05001529 struct flowi6 fl6;
Eric Dumazetadf30902009-06-02 05:19:30 +00001530 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Eric Dumazet96b52e62010-06-07 21:05:02 +00001532 rcu_read_lock();
1533 idev = __in6_dev_get(skb->dev);
Neil Hormanedf391f2009-04-27 02:45:02 -07001534 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
1535
Simon Horman29a3cad2013-05-28 20:34:26 +00001536 payload_len = (skb_tail_pointer(skb) - skb_network_header(skb)) -
1537 sizeof(*pip6);
1538 mldlen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 pip6->payload_len = htons(payload_len);
1540
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001541 pmr->mld2r_cksum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1542 IPPROTO_ICMPV6,
1543 csum_partial(skb_transport_header(skb),
1544 mldlen, 0));
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001545
David S. Miller4c9483b2011-03-12 16:22:43 -05001546 icmpv6_flow_init(net->ipv6.igmp_sk, &fl6, ICMPV6_MLD2_REPORT,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001547 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1548 skb->dev->ifindex);
YOSHIFUJI Hideaki / 吉藤英明12fd84f2013-01-18 02:00:24 +00001549 dst = icmp6_dst_alloc(skb->dev, &fl6);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001550
David S. Miller452edd52011-03-02 13:27:41 -08001551 err = 0;
1552 if (IS_ERR(dst)) {
1553 err = PTR_ERR(dst);
1554 dst = NULL;
1555 }
Eric Dumazetadf30902009-06-02 05:19:30 +00001556 skb_dst_set(skb, dst);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001557 if (err)
1558 goto err_out;
1559
Neil Hormanedf391f2009-04-27 02:45:02 -07001560 payload_len = skb->len;
1561
Jan Engelhardtb2e0b382010-03-23 04:09:07 +01001562 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001563 dst_output);
1564out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (!err) {
Denis V. Lunev5a57d4c2008-10-08 10:34:14 -07001566 ICMP6MSGOUT_INC_STATS_BH(net, idev, ICMPV6_MLD2_REPORT);
Denis V. Luneve41b5362008-10-08 10:33:26 -07001567 ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
Neil Hormanedf391f2009-04-27 02:45:02 -07001568 IP6_UPD_PO_STATS_BH(net, idev, IPSTATS_MIB_OUTMCAST, payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 } else
Denis V. Lunev483a47d2008-10-08 11:09:27 -07001570 IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Eric Dumazet96b52e62010-06-07 21:05:02 +00001572 rcu_read_unlock();
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001573 return;
1574
1575err_out:
1576 kfree_skb(skb);
1577 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579
1580static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1581{
Yan Zhengfab10fe2005-10-05 12:08:13 -07001582 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
1585static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1586 int type, struct mld2_grec **ppgr)
1587{
1588 struct net_device *dev = pmc->idev->dev;
1589 struct mld2_report *pmr;
1590 struct mld2_grec *pgr;
1591
1592 if (!skb)
Amerigo Wang89657792013-06-29 21:30:49 +08001593 skb = mld_newpack(pmc->idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (!skb)
1595 return NULL;
1596 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1597 pgr->grec_type = type;
1598 pgr->grec_auxwords = 0;
1599 pgr->grec_nsrcs = 0;
1600 pgr->grec_mca = pmc->mca_addr; /* structure copy */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001601 pmr = (struct mld2_report *)skb_transport_header(skb);
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001602 pmr->mld2r_ngrec = htons(ntohs(pmr->mld2r_ngrec)+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 *ppgr = pgr;
1604 return skb;
1605}
1606
1607#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1608 skb_tailroom(skb)) : 0)
1609
1610static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1611 int type, int gdeleted, int sdeleted)
1612{
Amerigo Wang89657792013-06-29 21:30:49 +08001613 struct inet6_dev *idev = pmc->idev;
1614 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 struct mld2_report *pmr;
1616 struct mld2_grec *pgr = NULL;
1617 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001618 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620 if (pmc->mca_flags & MAF_NOREPORT)
1621 return skb;
1622
1623 isquery = type == MLD2_MODE_IS_INCLUDE ||
1624 type == MLD2_MODE_IS_EXCLUDE;
1625 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1626 type == MLD2_CHANGE_TO_EXCLUDE;
1627
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001628 stotal = scount = 0;
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1631
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001632 if (!*psf_list)
1633 goto empty_source;
1634
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001635 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 /* EX and TO_EX get a fresh packet, if needed */
1638 if (truncate) {
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001639 if (pmr && pmr->mld2r_ngrec &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1641 if (skb)
1642 mld_sendpack(skb);
Amerigo Wang89657792013-06-29 21:30:49 +08001643 skb = mld_newpack(idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
1645 }
1646 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 psf_prev = NULL;
1648 for (psf=*psf_list; psf; psf=psf_next) {
1649 struct in6_addr *psrc;
1650
1651 psf_next = psf->sf_next;
1652
1653 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1654 psf_prev = psf;
1655 continue;
1656 }
1657
1658 /* clear marks on query responses */
1659 if (isquery)
1660 psf->sf_gsresp = 0;
1661
1662 if (AVAILABLE(skb) < sizeof(*psrc) +
1663 first*sizeof(struct mld2_grec)) {
1664 if (truncate && !first)
1665 break; /* truncate these */
1666 if (pgr)
1667 pgr->grec_nsrcs = htons(scount);
1668 if (skb)
1669 mld_sendpack(skb);
Amerigo Wang89657792013-06-29 21:30:49 +08001670 skb = mld_newpack(idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 first = 1;
1672 scount = 0;
1673 }
1674 if (first) {
1675 skb = add_grhead(skb, pmc, type, &pgr);
1676 first = 0;
1677 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -08001678 if (!skb)
1679 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1681 *psrc = psf->sf_addr;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001682 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1684 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1685 psf->sf_crcount--;
1686 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1687 if (psf_prev)
1688 psf_prev->sf_next = psf->sf_next;
1689 else
1690 *psf_list = psf->sf_next;
1691 kfree(psf);
1692 continue;
1693 }
1694 }
1695 psf_prev = psf;
1696 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001697
1698empty_source:
1699 if (!stotal) {
1700 if (type == MLD2_ALLOW_NEW_SOURCES ||
1701 type == MLD2_BLOCK_OLD_SOURCES)
1702 return skb;
1703 if (pmc->mca_crcount || isquery) {
1704 /* make sure we have room for group header */
1705 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1706 mld_sendpack(skb);
1707 skb = NULL; /* add_grhead will get a new one */
1708 }
1709 skb = add_grhead(skb, pmc, type, &pgr);
1710 }
1711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (pgr)
1713 pgr->grec_nsrcs = htons(scount);
1714
1715 if (isquery)
1716 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1717 return skb;
1718}
1719
1720static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1721{
1722 struct sk_buff *skb = NULL;
1723 int type;
1724
Amerigo Wang89657792013-06-29 21:30:49 +08001725 read_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (!pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1728 if (pmc->mca_flags & MAF_NOREPORT)
1729 continue;
1730 spin_lock_bh(&pmc->mca_lock);
1731 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1732 type = MLD2_MODE_IS_EXCLUDE;
1733 else
1734 type = MLD2_MODE_IS_INCLUDE;
1735 skb = add_grec(skb, pmc, type, 0, 0);
1736 spin_unlock_bh(&pmc->mca_lock);
1737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 } else {
1739 spin_lock_bh(&pmc->mca_lock);
1740 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1741 type = MLD2_MODE_IS_EXCLUDE;
1742 else
1743 type = MLD2_MODE_IS_INCLUDE;
1744 skb = add_grec(skb, pmc, type, 0, 0);
1745 spin_unlock_bh(&pmc->mca_lock);
1746 }
Amerigo Wang89657792013-06-29 21:30:49 +08001747 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (skb)
1749 mld_sendpack(skb);
1750}
1751
1752/*
1753 * remove zero-count source records from a source filter list
1754 */
1755static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1756{
1757 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1758
1759 psf_prev = NULL;
1760 for (psf=*ppsf; psf; psf = psf_next) {
1761 psf_next = psf->sf_next;
1762 if (psf->sf_crcount == 0) {
1763 if (psf_prev)
1764 psf_prev->sf_next = psf->sf_next;
1765 else
1766 *ppsf = psf->sf_next;
1767 kfree(psf);
1768 } else
1769 psf_prev = psf;
1770 }
1771}
1772
1773static void mld_send_cr(struct inet6_dev *idev)
1774{
1775 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1776 struct sk_buff *skb = NULL;
1777 int type, dtype;
1778
1779 read_lock_bh(&idev->lock);
Stephen Hemminger6457d262010-02-17 18:48:44 -08001780 spin_lock(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 /* deleted MCA's */
1783 pmc_prev = NULL;
1784 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1785 pmc_next = pmc->next;
1786 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1787 type = MLD2_BLOCK_OLD_SOURCES;
1788 dtype = MLD2_BLOCK_OLD_SOURCES;
1789 skb = add_grec(skb, pmc, type, 1, 0);
1790 skb = add_grec(skb, pmc, dtype, 1, 1);
1791 }
1792 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1794 type = MLD2_CHANGE_TO_INCLUDE;
1795 skb = add_grec(skb, pmc, type, 1, 0);
1796 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001797 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (pmc->mca_crcount == 0) {
1799 mld_clear_zeros(&pmc->mca_tomb);
1800 mld_clear_zeros(&pmc->mca_sources);
1801 }
1802 }
1803 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1804 !pmc->mca_sources) {
1805 if (pmc_prev)
1806 pmc_prev->next = pmc_next;
1807 else
1808 idev->mc_tomb = pmc_next;
1809 in6_dev_put(pmc->idev);
1810 kfree(pmc);
1811 } else
1812 pmc_prev = pmc;
1813 }
Stephen Hemminger6457d262010-02-17 18:48:44 -08001814 spin_unlock(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 /* change recs */
1817 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1818 spin_lock_bh(&pmc->mca_lock);
1819 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1820 type = MLD2_BLOCK_OLD_SOURCES;
1821 dtype = MLD2_ALLOW_NEW_SOURCES;
1822 } else {
1823 type = MLD2_ALLOW_NEW_SOURCES;
1824 dtype = MLD2_BLOCK_OLD_SOURCES;
1825 }
1826 skb = add_grec(skb, pmc, type, 0, 0);
1827 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1828
1829 /* filter mode changes */
1830 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1832 type = MLD2_CHANGE_TO_EXCLUDE;
1833 else
1834 type = MLD2_CHANGE_TO_INCLUDE;
1835 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001836 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
1838 spin_unlock_bh(&pmc->mca_lock);
1839 }
1840 read_unlock_bh(&idev->lock);
1841 if (!skb)
1842 return;
1843 (void) mld_sendpack(skb);
1844}
1845
1846static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1847{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001848 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001849 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 struct inet6_dev *idev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001851 struct sk_buff *skb;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001852 struct mld_msg *hdr;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001853 const struct in6_addr *snd_addr, *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 struct in6_addr addr_buf;
Herbert Xua7ae1992011-11-18 02:20:04 +00001855 int hlen = LL_RESERVED_SPACE(dev);
1856 int tlen = dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 int err, len, payload_len, full_len;
1858 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1859 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1860 IPV6_TLV_PADN, 0 };
David S. Miller4c9483b2011-03-12 16:22:43 -05001861 struct flowi6 fl6;
Eric Dumazetadf30902009-06-02 05:19:30 +00001862 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09001864 if (type == ICMPV6_MGM_REDUCTION)
1865 snd_addr = &in6addr_linklocal_allrouters;
1866 else
1867 snd_addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1870 payload_len = len + sizeof(ra);
1871 full_len = sizeof(struct ipv6hdr) + payload_len;
1872
Neil Hormanedf391f2009-04-27 02:45:02 -07001873 rcu_read_lock();
1874 IP6_UPD_PO_STATS(net, __in6_dev_get(dev),
1875 IPSTATS_MIB_OUT, full_len);
1876 rcu_read_unlock();
1877
Herbert Xua7ae1992011-11-18 02:20:04 +00001878 skb = sock_alloc_send_skb(sk, hlen + tlen + full_len, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 if (skb == NULL) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001881 rcu_read_lock();
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001882 IP6_INC_STATS(net, __in6_dev_get(dev),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001883 IPSTATS_MIB_OUTDISCARDS);
1884 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return;
1886 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +02001887 skb->priority = TC_PRIO_CONTROL;
Herbert Xua7ae1992011-11-18 02:20:04 +00001888 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Neil Horman95c385b2007-04-25 17:08:10 -07001890 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001892 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 * when a valid link-local address is not available.
1894 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001895 saddr = &in6addr_any;
1896 } else
1897 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +00001899 ip6_mc_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1902
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001903 hdr = (struct mld_msg *) skb_put(skb, sizeof(struct mld_msg));
1904 memset(hdr, 0, sizeof(struct mld_msg));
1905 hdr->mld_type = type;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001906 hdr->mld_mca = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001908 hdr->mld_cksum = csum_ipv6_magic(saddr, snd_addr, len,
1909 IPPROTO_ICMPV6,
1910 csum_partial(hdr, len, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Eric Dumazet96b52e62010-06-07 21:05:02 +00001912 rcu_read_lock();
1913 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
David S. Miller4c9483b2011-03-12 16:22:43 -05001915 icmpv6_flow_init(sk, &fl6, type,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001916 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1917 skb->dev->ifindex);
YOSHIFUJI Hideaki / 吉藤英明12fd84f2013-01-18 02:00:24 +00001918 dst = icmp6_dst_alloc(skb->dev, &fl6);
David S. Miller452edd52011-03-02 13:27:41 -08001919 if (IS_ERR(dst)) {
1920 err = PTR_ERR(dst);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001921 goto err_out;
David S. Miller452edd52011-03-02 13:27:41 -08001922 }
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001923
Eric Dumazetadf30902009-06-02 05:19:30 +00001924 skb_dst_set(skb, dst);
Jan Engelhardtb2e0b382010-03-23 04:09:07 +01001925 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001926 dst_output);
1927out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 if (!err) {
Denis V. Lunev5c5d2442008-10-08 10:33:50 -07001929 ICMP6MSGOUT_INC_STATS(net, idev, type);
Denis V. Luneva862f6a2008-10-08 10:33:06 -07001930 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
Neil Hormanedf391f2009-04-27 02:45:02 -07001931 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, full_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 } else
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001933 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Eric Dumazet96b52e62010-06-07 21:05:02 +00001935 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return;
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001937
1938err_out:
1939 kfree_skb(skb);
1940 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02001943static void mld_resend_report(struct inet6_dev *idev)
1944{
1945 if (MLD_V1_SEEN(idev)) {
1946 struct ifmcaddr6 *mcaddr;
1947 read_lock_bh(&idev->lock);
1948 for (mcaddr = idev->mc_list; mcaddr; mcaddr = mcaddr->next) {
1949 if (!(mcaddr->mca_flags & MAF_NOREPORT))
1950 igmp6_send(&mcaddr->mca_addr, idev->dev,
1951 ICMPV6_MGM_REPORT);
1952 }
1953 read_unlock_bh(&idev->lock);
1954 } else {
1955 mld_send_report(idev, NULL);
1956 }
1957}
1958
1959void ipv6_mc_dad_complete(struct inet6_dev *idev)
1960{
1961 idev->mc_dad_count = idev->mc_qrv;
1962 if (idev->mc_dad_count) {
1963 mld_resend_report(idev);
1964 idev->mc_dad_count--;
1965 if (idev->mc_dad_count)
1966 mld_dad_start_timer(idev, idev->mc_maxdelay);
1967 }
1968}
1969
1970static void mld_dad_timer_expire(unsigned long data)
1971{
1972 struct inet6_dev *idev = (struct inet6_dev *)data;
1973
1974 mld_resend_report(idev);
1975 if (idev->mc_dad_count) {
1976 idev->mc_dad_count--;
1977 if (idev->mc_dad_count)
1978 mld_dad_start_timer(idev, idev->mc_maxdelay);
1979 }
1980 __in6_dev_put(idev);
1981}
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001984 const struct in6_addr *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
1986 struct ip6_sf_list *psf, *psf_prev;
1987 int rv = 0;
1988
1989 psf_prev = NULL;
1990 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1991 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1992 break;
1993 psf_prev = psf;
1994 }
1995 if (!psf || psf->sf_count[sfmode] == 0) {
1996 /* source filter not found, or count wrong => bug */
1997 return -ESRCH;
1998 }
1999 psf->sf_count[sfmode]--;
2000 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
2001 struct inet6_dev *idev = pmc->idev;
2002
2003 /* no more filters for this source */
2004 if (psf_prev)
2005 psf_prev->sf_next = psf->sf_next;
2006 else
2007 pmc->mca_sources = psf->sf_next;
2008 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
2009 !MLD_V1_SEEN(idev)) {
2010 psf->sf_crcount = idev->mc_qrv;
2011 psf->sf_next = pmc->mca_tomb;
2012 pmc->mca_tomb = psf;
2013 rv = 1;
2014 } else
2015 kfree(psf);
2016 }
2017 return rv;
2018}
2019
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002020static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2021 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 int delta)
2023{
2024 struct ifmcaddr6 *pmc;
2025 int changerec = 0;
2026 int i, err;
2027
2028 if (!idev)
2029 return -ENODEV;
2030 read_lock_bh(&idev->lock);
2031 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2032 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2033 break;
2034 }
2035 if (!pmc) {
2036 /* MCA not found?? bug */
2037 read_unlock_bh(&idev->lock);
2038 return -ESRCH;
2039 }
2040 spin_lock_bh(&pmc->mca_lock);
2041 sf_markstate(pmc);
2042 if (!delta) {
2043 if (!pmc->mca_sfcount[sfmode]) {
2044 spin_unlock_bh(&pmc->mca_lock);
2045 read_unlock_bh(&idev->lock);
2046 return -EINVAL;
2047 }
2048 pmc->mca_sfcount[sfmode]--;
2049 }
2050 err = 0;
2051 for (i=0; i<sfcount; i++) {
2052 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2053
2054 changerec |= rv > 0;
2055 if (!err && rv < 0)
2056 err = rv;
2057 }
2058 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
2059 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
2060 pmc->mca_sfcount[MCAST_INCLUDE]) {
2061 struct ip6_sf_list *psf;
2062
2063 /* filter mode change */
2064 pmc->mca_sfmode = MCAST_INCLUDE;
2065 pmc->mca_crcount = idev->mc_qrv;
2066 idev->mc_ifc_count = pmc->mca_crcount;
2067 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2068 psf->sf_crcount = 0;
2069 mld_ifc_event(pmc->idev);
2070 } else if (sf_setstate(pmc) || changerec)
2071 mld_ifc_event(pmc->idev);
2072 spin_unlock_bh(&pmc->mca_lock);
2073 read_unlock_bh(&idev->lock);
2074 return err;
2075}
2076
2077/*
2078 * Add multicast single-source filter to the interface list
2079 */
2080static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
Jun Zhao99d2f472011-11-30 06:21:05 +00002081 const struct in6_addr *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
2083 struct ip6_sf_list *psf, *psf_prev;
2084
2085 psf_prev = NULL;
2086 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
2087 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
2088 break;
2089 psf_prev = psf;
2090 }
2091 if (!psf) {
Ingo Oeser0c600ed2006-03-20 23:01:32 -08002092 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if (!psf)
2094 return -ENOBUFS;
Ingo Oeser0c600ed2006-03-20 23:01:32 -08002095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 psf->sf_addr = *psfsrc;
2097 if (psf_prev) {
2098 psf_prev->sf_next = psf;
2099 } else
2100 pmc->mca_sources = psf;
2101 }
2102 psf->sf_count[sfmode]++;
2103 return 0;
2104}
2105
2106static void sf_markstate(struct ifmcaddr6 *pmc)
2107{
2108 struct ip6_sf_list *psf;
2109 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2110
2111 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
2112 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2113 psf->sf_oldin = mca_xcount ==
2114 psf->sf_count[MCAST_EXCLUDE] &&
2115 !psf->sf_count[MCAST_INCLUDE];
2116 } else
2117 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2118}
2119
2120static int sf_setstate(struct ifmcaddr6 *pmc)
2121{
David L Stevens7add2a42006-01-24 13:06:39 -08002122 struct ip6_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2124 int qrv = pmc->idev->mc_qrv;
2125 int new_in, rv;
2126
2127 rv = 0;
2128 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
2129 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2130 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2131 !psf->sf_count[MCAST_INCLUDE];
2132 } else
2133 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevens7add2a42006-01-24 13:06:39 -08002134 if (new_in) {
2135 if (!psf->sf_oldin) {
Al Viroe80e28b2006-02-03 20:10:03 -05002136 struct ip6_sf_list *prev = NULL;
David L Stevens7add2a42006-01-24 13:06:39 -08002137
2138 for (dpsf=pmc->mca_tomb; dpsf;
2139 dpsf=dpsf->sf_next) {
2140 if (ipv6_addr_equal(&dpsf->sf_addr,
2141 &psf->sf_addr))
2142 break;
2143 prev = dpsf;
2144 }
2145 if (dpsf) {
2146 if (prev)
2147 prev->sf_next = dpsf->sf_next;
2148 else
2149 pmc->mca_tomb = dpsf->sf_next;
2150 kfree(dpsf);
2151 }
2152 psf->sf_crcount = qrv;
2153 rv++;
2154 }
2155 } else if (psf->sf_oldin) {
2156 psf->sf_crcount = 0;
2157 /*
2158 * add or update "delete" records if an active filter
2159 * is now inactive
2160 */
2161 for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
2162 if (ipv6_addr_equal(&dpsf->sf_addr,
2163 &psf->sf_addr))
2164 break;
2165 if (!dpsf) {
Joe Perches5d553542010-05-31 17:23:22 +00002166 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevens7add2a42006-01-24 13:06:39 -08002167 if (!dpsf)
2168 continue;
2169 *dpsf = *psf;
2170 /* pmc->mca_lock held by callers */
2171 dpsf->sf_next = pmc->mca_tomb;
2172 pmc->mca_tomb = dpsf;
2173 }
2174 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 rv++;
2176 }
2177 }
2178 return rv;
2179}
2180
2181/*
2182 * Add multicast source filter list to the interface list
2183 */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002184static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2185 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 int delta)
2187{
2188 struct ifmcaddr6 *pmc;
2189 int isexclude;
2190 int i, err;
2191
2192 if (!idev)
2193 return -ENODEV;
2194 read_lock_bh(&idev->lock);
2195 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2196 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2197 break;
2198 }
2199 if (!pmc) {
2200 /* MCA not found?? bug */
2201 read_unlock_bh(&idev->lock);
2202 return -ESRCH;
2203 }
2204 spin_lock_bh(&pmc->mca_lock);
2205
2206 sf_markstate(pmc);
2207 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2208 if (!delta)
2209 pmc->mca_sfcount[sfmode]++;
2210 err = 0;
2211 for (i=0; i<sfcount; i++) {
Jun Zhao99d2f472011-11-30 06:21:05 +00002212 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if (err)
2214 break;
2215 }
2216 if (err) {
2217 int j;
2218
2219 if (!delta)
2220 pmc->mca_sfcount[sfmode]--;
2221 for (j=0; j<i; j++)
RongQing.Li78d50212012-04-04 16:47:04 +00002222 ip6_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 struct ip6_sf_list *psf;
2225
2226 /* filter mode change */
2227 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2228 pmc->mca_sfmode = MCAST_EXCLUDE;
2229 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2230 pmc->mca_sfmode = MCAST_INCLUDE;
2231 /* else no filters; keep old mode for reports */
2232
2233 pmc->mca_crcount = idev->mc_qrv;
2234 idev->mc_ifc_count = pmc->mca_crcount;
2235 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2236 psf->sf_crcount = 0;
2237 mld_ifc_event(idev);
2238 } else if (sf_setstate(pmc))
2239 mld_ifc_event(idev);
2240 spin_unlock_bh(&pmc->mca_lock);
2241 read_unlock_bh(&idev->lock);
2242 return err;
2243}
2244
2245static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2246{
2247 struct ip6_sf_list *psf, *nextpsf;
2248
2249 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2250 nextpsf = psf->sf_next;
2251 kfree(psf);
2252 }
2253 pmc->mca_tomb = NULL;
2254 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2255 nextpsf = psf->sf_next;
2256 kfree(psf);
2257 }
2258 pmc->mca_sources = NULL;
2259 pmc->mca_sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002260 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2262}
2263
2264
2265static void igmp6_join_group(struct ifmcaddr6 *ma)
2266{
2267 unsigned long delay;
2268
2269 if (ma->mca_flags & MAF_NOREPORT)
2270 return;
2271
2272 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2273
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02002274 delay = net_random() % unsolicited_report_interval(ma->idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
2276 spin_lock_bh(&ma->mca_lock);
2277 if (del_timer(&ma->mca_timer)) {
2278 atomic_dec(&ma->mca_refcnt);
2279 delay = ma->mca_timer.expires - jiffies;
2280 }
2281
2282 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2283 atomic_inc(&ma->mca_refcnt);
2284 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2285 spin_unlock_bh(&ma->mca_lock);
2286}
2287
2288static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2289 struct inet6_dev *idev)
2290{
2291 int err;
2292
David L Stevens5ab4a6c2005-12-27 14:03:00 -08002293 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2294 * so no other readers or writers of iml or its sflist
2295 */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002296 if (!iml->sflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 /* any-source empty exclude case */
2298 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2299 }
2300 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2301 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2302 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2303 iml->sflist = NULL;
2304 return err;
2305}
2306
2307static void igmp6_leave_group(struct ifmcaddr6 *ma)
2308{
2309 if (MLD_V1_SEEN(ma->idev)) {
2310 if (ma->mca_flags & MAF_LAST_REPORTER)
2311 igmp6_send(&ma->mca_addr, ma->idev->dev,
2312 ICMPV6_MGM_REDUCTION);
2313 } else {
2314 mld_add_delrec(ma->idev, ma);
2315 mld_ifc_event(ma->idev);
2316 }
2317}
2318
2319static void mld_gq_timer_expire(unsigned long data)
2320{
2321 struct inet6_dev *idev = (struct inet6_dev *)data;
2322
2323 idev->mc_gq_running = 0;
2324 mld_send_report(idev, NULL);
2325 __in6_dev_put(idev);
2326}
2327
2328static void mld_ifc_timer_expire(unsigned long data)
2329{
2330 struct inet6_dev *idev = (struct inet6_dev *)data;
2331
2332 mld_send_cr(idev);
2333 if (idev->mc_ifc_count) {
2334 idev->mc_ifc_count--;
2335 if (idev->mc_ifc_count)
2336 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2337 }
2338 __in6_dev_put(idev);
2339}
2340
2341static void mld_ifc_event(struct inet6_dev *idev)
2342{
2343 if (MLD_V1_SEEN(idev))
2344 return;
2345 idev->mc_ifc_count = idev->mc_qrv;
2346 mld_ifc_start_timer(idev, 1);
2347}
2348
2349
2350static void igmp6_timer_handler(unsigned long data)
2351{
2352 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2353
2354 if (MLD_V1_SEEN(ma->idev))
2355 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2356 else
2357 mld_send_report(ma->idev, ma);
2358
2359 spin_lock(&ma->mca_lock);
2360 ma->mca_flags |= MAF_LAST_REPORTER;
2361 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2362 spin_unlock(&ma->mca_lock);
2363 ma_put(ma);
2364}
2365
Moni Shoua75c78502009-09-15 02:37:40 -07002366/* Device changing type */
2367
2368void ipv6_mc_unmap(struct inet6_dev *idev)
2369{
2370 struct ifmcaddr6 *i;
2371
2372 /* Install multicast list, except for all-nodes (already installed) */
2373
2374 read_lock_bh(&idev->lock);
2375 for (i = idev->mc_list; i; i = i->next)
2376 igmp6_group_dropped(i);
2377 read_unlock_bh(&idev->lock);
2378}
2379
2380void ipv6_mc_remap(struct inet6_dev *idev)
2381{
2382 ipv6_mc_up(idev);
2383}
2384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385/* Device going down */
2386
2387void ipv6_mc_down(struct inet6_dev *idev)
2388{
2389 struct ifmcaddr6 *i;
2390
2391 /* Withdraw multicast list */
2392
2393 read_lock_bh(&idev->lock);
2394 idev->mc_ifc_count = 0;
2395 if (del_timer(&idev->mc_ifc_timer))
2396 __in6_dev_put(idev);
2397 idev->mc_gq_running = 0;
2398 if (del_timer(&idev->mc_gq_timer))
2399 __in6_dev_put(idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002400 if (del_timer(&idev->mc_dad_timer))
2401 __in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403 for (i = idev->mc_list; i; i=i->next)
2404 igmp6_group_dropped(i);
2405 read_unlock_bh(&idev->lock);
2406
2407 mld_clear_delrec(idev);
2408}
2409
2410
2411/* Device going up */
2412
2413void ipv6_mc_up(struct inet6_dev *idev)
2414{
2415 struct ifmcaddr6 *i;
2416
2417 /* Install multicast list, except for all-nodes (already installed) */
2418
2419 read_lock_bh(&idev->lock);
2420 for (i = idev->mc_list; i; i=i->next)
2421 igmp6_group_added(i);
2422 read_unlock_bh(&idev->lock);
2423}
2424
2425/* IPv6 device initialization. */
2426
2427void ipv6_mc_init_dev(struct inet6_dev *idev)
2428{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 write_lock_bh(&idev->lock);
Stephen Hemminger6457d262010-02-17 18:48:44 -08002430 spin_lock_init(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 idev->mc_gq_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002432 setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2433 (unsigned long)idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 idev->mc_tomb = NULL;
2435 idev->mc_ifc_count = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002436 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2437 (unsigned long)idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002438 setup_timer(&idev->mc_dad_timer, mld_dad_timer_expire,
2439 (unsigned long)idev);
Daniel Borkmann89225d12013-09-04 00:19:37 +02002440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 idev->mc_qrv = MLD_QRV_DEFAULT;
Daniel Borkmann89225d12013-09-04 00:19:37 +02002442 idev->mc_qi = MLD_QI_DEFAULT;
2443 idev->mc_qri = MLD_QRI_DEFAULT;
2444
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02002445 idev->mc_maxdelay = unsolicited_report_interval(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 idev->mc_v1_seen = 0;
2447 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448}
2449
2450/*
2451 * Device is about to be destroyed: clean up.
2452 */
2453
2454void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2455{
2456 struct ifmcaddr6 *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
2458 /* Deactivate timers */
2459 ipv6_mc_down(idev);
2460
2461 /* Delete all-nodes address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2463 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2464 * fail.
2465 */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002466 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002468 if (idev->cnf.forwarding)
2469 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471 write_lock_bh(&idev->lock);
2472 while ((i = idev->mc_list) != NULL) {
2473 idev->mc_list = i->next;
2474 write_unlock_bh(&idev->lock);
2475
2476 igmp6_group_dropped(i);
2477 ma_put(i);
2478
2479 write_lock_bh(&idev->lock);
2480 }
2481 write_unlock_bh(&idev->lock);
2482}
2483
2484#ifdef CONFIG_PROC_FS
2485struct igmp6_mc_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002486 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 struct net_device *dev;
2488 struct inet6_dev *idev;
2489};
2490
2491#define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2492
2493static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2494{
2495 struct ifmcaddr6 *im = NULL;
2496 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002497 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
Pavel Emelianov7562f872007-05-03 15:13:45 -07002499 state->idev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002500 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 struct inet6_dev *idev;
Eric Dumazetce81b762009-11-11 17:34:30 +00002502 idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 if (!idev)
2504 continue;
2505 read_lock_bh(&idev->lock);
2506 im = idev->mc_list;
2507 if (im) {
2508 state->idev = idev;
2509 break;
2510 }
2511 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 }
2513 return im;
2514}
2515
2516static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2517{
2518 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2519
2520 im = im->next;
2521 while (!im) {
Eric Dumazetce81b762009-11-11 17:34:30 +00002522 if (likely(state->idev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 read_unlock_bh(&state->idev->lock);
Eric Dumazetce81b762009-11-11 17:34:30 +00002524
2525 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 if (!state->dev) {
2527 state->idev = NULL;
2528 break;
2529 }
Eric Dumazetce81b762009-11-11 17:34:30 +00002530 state->idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 if (!state->idev)
2532 continue;
2533 read_lock_bh(&state->idev->lock);
2534 im = state->idev->mc_list;
2535 }
2536 return im;
2537}
2538
2539static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2540{
2541 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2542 if (im)
2543 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2544 --pos;
2545 return pos ? NULL : im;
2546}
2547
2548static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetce81b762009-11-11 17:34:30 +00002549 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
Eric Dumazetce81b762009-11-11 17:34:30 +00002551 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 return igmp6_mc_get_idx(seq, *pos);
2553}
2554
2555static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2556{
Eric Dumazetce81b762009-11-11 17:34:30 +00002557 struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);
2558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 ++*pos;
2560 return im;
2561}
2562
2563static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetce81b762009-11-11 17:34:30 +00002564 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
2566 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
Eric Dumazetce81b762009-11-11 17:34:30 +00002567
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 if (likely(state->idev != NULL)) {
2569 read_unlock_bh(&state->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 state->idev = NULL;
2571 }
2572 state->dev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002573 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574}
2575
2576static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2577{
2578 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2579 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2580
2581 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002582 "%-4d %-15s %pi6 %5d %08X %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 state->dev->ifindex, state->dev->name,
Harvey Harrisonb0711952008-10-28 16:05:40 -07002584 &im->mca_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 im->mca_users, im->mca_flags,
2586 (im->mca_flags&MAF_TIMER_RUNNING) ?
2587 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2588 return 0;
2589}
2590
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002591static const struct seq_operations igmp6_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 .start = igmp6_mc_seq_start,
2593 .next = igmp6_mc_seq_next,
2594 .stop = igmp6_mc_seq_stop,
2595 .show = igmp6_mc_seq_show,
2596};
2597
2598static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2599{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002600 return seq_open_net(inode, file, &igmp6_mc_seq_ops,
2601 sizeof(struct igmp6_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602}
2603
Arjan van de Ven9a321442007-02-12 00:55:35 -08002604static const struct file_operations igmp6_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 .owner = THIS_MODULE,
2606 .open = igmp6_mc_seq_open,
2607 .read = seq_read,
2608 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002609 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610};
2611
2612struct igmp6_mcf_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002613 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 struct net_device *dev;
2615 struct inet6_dev *idev;
2616 struct ifmcaddr6 *im;
2617};
2618
2619#define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2620
2621static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2622{
2623 struct ip6_sf_list *psf = NULL;
2624 struct ifmcaddr6 *im = NULL;
2625 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002626 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Pavel Emelianov7562f872007-05-03 15:13:45 -07002628 state->idev = NULL;
2629 state->im = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002630 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 struct inet6_dev *idev;
Eric Dumazetce81b762009-11-11 17:34:30 +00002632 idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 if (unlikely(idev == NULL))
2634 continue;
2635 read_lock_bh(&idev->lock);
2636 im = idev->mc_list;
2637 if (likely(im != NULL)) {
2638 spin_lock_bh(&im->mca_lock);
2639 psf = im->mca_sources;
2640 if (likely(psf != NULL)) {
2641 state->im = im;
2642 state->idev = idev;
2643 break;
2644 }
2645 spin_unlock_bh(&im->mca_lock);
2646 }
2647 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 }
2649 return psf;
2650}
2651
2652static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2653{
2654 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2655
2656 psf = psf->sf_next;
2657 while (!psf) {
2658 spin_unlock_bh(&state->im->mca_lock);
2659 state->im = state->im->next;
2660 while (!state->im) {
Eric Dumazetce81b762009-11-11 17:34:30 +00002661 if (likely(state->idev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 read_unlock_bh(&state->idev->lock);
Eric Dumazetce81b762009-11-11 17:34:30 +00002663
2664 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 if (!state->dev) {
2666 state->idev = NULL;
2667 goto out;
2668 }
Eric Dumazetce81b762009-11-11 17:34:30 +00002669 state->idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 if (!state->idev)
2671 continue;
2672 read_lock_bh(&state->idev->lock);
2673 state->im = state->idev->mc_list;
2674 }
2675 if (!state->im)
2676 break;
2677 spin_lock_bh(&state->im->mca_lock);
2678 psf = state->im->mca_sources;
2679 }
2680out:
2681 return psf;
2682}
2683
2684static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2685{
2686 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2687 if (psf)
2688 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2689 --pos;
2690 return pos ? NULL : psf;
2691}
2692
2693static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetce81b762009-11-11 17:34:30 +00002694 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695{
Eric Dumazetce81b762009-11-11 17:34:30 +00002696 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2698}
2699
2700static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2701{
2702 struct ip6_sf_list *psf;
2703 if (v == SEQ_START_TOKEN)
2704 psf = igmp6_mcf_get_first(seq);
2705 else
2706 psf = igmp6_mcf_get_next(seq, v);
2707 ++*pos;
2708 return psf;
2709}
2710
2711static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetce81b762009-11-11 17:34:30 +00002712 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
2714 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2715 if (likely(state->im != NULL)) {
2716 spin_unlock_bh(&state->im->mca_lock);
2717 state->im = NULL;
2718 }
2719 if (likely(state->idev != NULL)) {
2720 read_unlock_bh(&state->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 state->idev = NULL;
2722 }
2723 state->dev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002724 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725}
2726
2727static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2728{
2729 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2730 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2731
2732 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002733 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 "%3s %6s "
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002735 "%32s %32s %6s %6s\n", "Idx",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 "Device", "Multicast Address",
2737 "Source Address", "INC", "EXC");
2738 } else {
2739 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002740 "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 state->dev->ifindex, state->dev->name,
Harvey Harrisonb0711952008-10-28 16:05:40 -07002742 &state->im->mca_addr,
2743 &psf->sf_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 psf->sf_count[MCAST_INCLUDE],
2745 psf->sf_count[MCAST_EXCLUDE]);
2746 }
2747 return 0;
2748}
2749
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002750static const struct seq_operations igmp6_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 .start = igmp6_mcf_seq_start,
2752 .next = igmp6_mcf_seq_next,
2753 .stop = igmp6_mcf_seq_stop,
2754 .show = igmp6_mcf_seq_show,
2755};
2756
2757static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2758{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002759 return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
2760 sizeof(struct igmp6_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761}
2762
Arjan van de Ven9a321442007-02-12 00:55:35 -08002763static const struct file_operations igmp6_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 .owner = THIS_MODULE,
2765 .open = igmp6_mcf_seq_open,
2766 .read = seq_read,
2767 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002768 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769};
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002770
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002771static int __net_init igmp6_proc_init(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002772{
2773 int err;
2774
2775 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002776 if (!proc_create("igmp6", S_IRUGO, net->proc_net, &igmp6_mc_seq_fops))
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002777 goto out;
Gao fengd4beaa62013-02-18 01:34:54 +00002778 if (!proc_create("mcfilter6", S_IRUGO, net->proc_net,
2779 &igmp6_mcf_seq_fops))
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002780 goto out_proc_net_igmp6;
2781
2782 err = 0;
2783out:
2784 return err;
2785
2786out_proc_net_igmp6:
Gao fengece31ff2013-02-18 01:34:56 +00002787 remove_proc_entry("igmp6", net->proc_net);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002788 goto out;
2789}
2790
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002791static void __net_exit igmp6_proc_exit(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002792{
Gao fengece31ff2013-02-18 01:34:56 +00002793 remove_proc_entry("mcfilter6", net->proc_net);
2794 remove_proc_entry("igmp6", net->proc_net);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002795}
2796#else
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002797static inline int igmp6_proc_init(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002798{
2799 return 0;
2800}
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002801static inline void igmp6_proc_exit(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002802{
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002803}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804#endif
2805
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002806static int __net_init igmp6_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 int err;
2809
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002810 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2811 SOCK_RAW, IPPROTO_ICMPV6, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00002813 pr_err("Failed to initialize the IGMP6 control socket (err %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 err);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002815 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 }
2817
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002818 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002820 err = igmp6_proc_init(net);
2821 if (err)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002822 goto out_sock_create;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002823out:
2824 return err;
2825
2826out_sock_create:
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002827 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002828 goto out;
2829}
2830
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002831static void __net_exit igmp6_net_exit(struct net *net)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002832{
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002833 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002834 igmp6_proc_exit(net);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002835}
2836
2837static struct pernet_operations igmp6_net_ops = {
2838 .init = igmp6_net_init,
2839 .exit = igmp6_net_exit,
2840};
2841
2842int __init igmp6_init(void)
2843{
2844 return register_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845}
2846
2847void igmp6_cleanup(void)
2848{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002849 unregister_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850}