blob: d64e26366cb8e6ec9cacb301e43e8e3c5661a4cd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Multicast support for IPv6
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09008 * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15
16/* Changes:
17 *
18 * yoshfuji : fix format of router-alert option
19 * YOSHIFUJI Hideaki @USAGI:
20 * Fixed source address for MLD message based on
21 * <draft-ietf-magma-mld-source-05.txt>.
22 * YOSHIFUJI Hideaki @USAGI:
23 * - Ignore Queries for invalid addresses.
24 * - MLD for link-local addresses.
25 * David L Stevens <dlstevens@us.ibm.com>:
26 * - MLDv2 support
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30#include <linux/errno.h>
31#include <linux/types.h>
32#include <linux/string.h>
33#include <linux/socket.h>
34#include <linux/sockios.h>
35#include <linux/jiffies.h>
36#include <linux/times.h>
37#include <linux/net.h>
38#include <linux/in.h>
39#include <linux/in6.h>
40#include <linux/netdevice.h>
41#include <linux/if_arp.h>
42#include <linux/route.h>
43#include <linux/init.h>
44#include <linux/proc_fs.h>
45#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +020047#include <linux/pkt_sched.h>
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +090048#include <net/mld.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <linux/netfilter.h>
51#include <linux/netfilter_ipv6.h>
52
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020053#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <net/sock.h>
55#include <net/snmp.h>
56
57#include <net/ipv6.h>
58#include <net/protocol.h>
59#include <net/if_inet6.h>
60#include <net/ndisc.h>
61#include <net/addrconf.h>
62#include <net/ip6_route.h>
Denis V. Lunev1ed85162008-04-03 14:31:03 -070063#include <net/inet_common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#include <net/ip6_checksum.h>
66
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +090067/* Ensure that we have struct in6_addr aligned on 32bit word. */
68static void *__mld2_query_bugs[] __attribute__((__unused__)) = {
69 BUILD_BUG_ON_NULL(offsetof(struct mld2_query, mld2q_srcs) % 4),
70 BUILD_BUG_ON_NULL(offsetof(struct mld2_report, mld2r_grec) % 4),
71 BUILD_BUG_ON_NULL(offsetof(struct mld2_grec, grec_mca) % 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
74static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static void igmp6_join_group(struct ifmcaddr6 *ma);
77static void igmp6_leave_group(struct ifmcaddr6 *ma);
78static void igmp6_timer_handler(unsigned long data);
79
80static void mld_gq_timer_expire(unsigned long data);
81static void mld_ifc_timer_expire(unsigned long data);
82static void mld_ifc_event(struct inet6_dev *idev);
83static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
Eric Dumazetb71d1d42011-04-22 04:53:02 +000084static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static void mld_clear_delrec(struct inet6_dev *idev);
Daniel Borkmann6c567b72013-09-04 00:19:38 +020086static bool mld_in_v1_mode(const struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static int sf_setstate(struct ifmcaddr6 *pmc);
88static void sf_markstate(struct ifmcaddr6 *pmc);
89static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
Eric Dumazetb71d1d42011-04-22 04:53:02 +000090static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
91 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 int delta);
Eric Dumazetb71d1d42011-04-22 04:53:02 +000093static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
94 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 int delta);
96static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
97 struct inet6_dev *idev);
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#define MLD_QRV_DEFAULT 2
Daniel Borkmann89225d12013-09-04 00:19:37 +0200100/* RFC3810, 9.2. Query Interval */
101#define MLD_QI_DEFAULT (125 * HZ)
102/* RFC3810, 9.3. Query Response Interval */
103#define MLD_QRI_DEFAULT (10 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Daniel Borkmann9fd07842013-08-20 12:22:02 +0200105/* RFC3810, 8.1 Query Version Distinctions */
106#define MLD_V1_QUERY_LEN 24
107#define MLD_V2_QUERY_LEN_MIN 28
108
David L Stevens6f4353d2005-12-26 17:03:46 -0800109#define IPV6_MLD_MAX_MSF 64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Brian Haleyab32ea52006-09-22 14:15:41 -0700111int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +0200112int sysctl_mld_qrv __read_mostly = MLD_QRV_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/*
115 * socket join on multicast group
116 */
117
Eric Dumazet456b61b2010-11-23 13:12:15 +0000118#define for_each_pmc_rcu(np, pmc) \
119 for (pmc = rcu_dereference(np->ipv6_mc_list); \
120 pmc != NULL; \
121 pmc = rcu_dereference(pmc->next))
122
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200123static int unsolicited_report_interval(struct inet6_dev *idev)
124{
125 int iv;
126
Daniel Borkmann6c567b72013-09-04 00:19:38 +0200127 if (mld_in_v1_mode(idev))
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200128 iv = idev->cnf.mldv1_unsolicited_report_interval;
129 else
130 iv = idev->cnf.mldv2_unsolicited_report_interval;
131
132 return iv > 0 ? iv : 1;
133}
134
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900135int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 struct net_device *dev = NULL;
138 struct ipv6_mc_socklist *mc_lst;
139 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900140 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 int err;
142
143 if (!ipv6_addr_is_multicast(addr))
144 return -EINVAL;
145
Eric Dumazet456b61b2010-11-23 13:12:15 +0000146 rcu_read_lock();
147 for_each_pmc_rcu(np, mc_lst) {
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700148 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
149 ipv6_addr_equal(&mc_lst->addr, addr)) {
Eric Dumazet456b61b2010-11-23 13:12:15 +0000150 rcu_read_unlock();
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700151 return -EADDRINUSE;
152 }
153 }
Eric Dumazet456b61b2010-11-23 13:12:15 +0000154 rcu_read_unlock();
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
157
158 if (mc_lst == NULL)
159 return -ENOMEM;
160
161 mc_lst->next = NULL;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000162 mc_lst->addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200164 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (ifindex == 0) {
166 struct rt6_info *rt;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800167 rt = rt6_lookup(net, addr, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (rt) {
David S. Millerd1918542011-12-28 20:19:20 -0500169 dev = rt->dst.dev;
Amerigo Wang94e187c2012-10-29 00:13:19 +0000170 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172 } else
WANG Cong414b6c92014-09-11 15:35:14 -0700173 dev = __dev_get_by_index(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 if (dev == NULL) {
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200176 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
178 return -ENODEV;
179 }
180
181 mc_lst->ifindex = dev->ifindex;
182 mc_lst->sfmode = MCAST_EXCLUDE;
YOSHIFUJI Hideaki196433c2006-01-04 13:56:31 -0800183 rwlock_init(&mc_lst->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 mc_lst->sflist = NULL;
185
186 /*
187 * now add/increase the group membership on the device
188 */
189
190 err = ipv6_dev_mc_inc(dev, addr);
191
192 if (err) {
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200193 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return err;
196 }
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 mc_lst->next = np->ipv6_mc_list;
Eric Dumazet456b61b2010-11-23 13:12:15 +0000199 rcu_assign_pointer(np->ipv6_mc_list, mc_lst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200201 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 return 0;
204}
205
206/*
207 * socket leave on multicast group
208 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900209int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 struct ipv6_pinfo *np = inet6_sk(sk);
Eric Dumazet456b61b2010-11-23 13:12:15 +0000212 struct ipv6_mc_socklist *mc_lst;
213 struct ipv6_mc_socklist __rcu **lnk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900214 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Li Weia858d642012-07-17 15:28:59 +0800216 if (!ipv6_addr_is_multicast(addr))
217 return -EINVAL;
218
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200219 rtnl_lock();
Eric Dumazet456b61b2010-11-23 13:12:15 +0000220 for (lnk = &np->ipv6_mc_list;
WANG Congb5350912014-09-11 15:35:13 -0700221 (mc_lst = rtnl_dereference(*lnk)) != NULL;
Eric Dumazet456b61b2010-11-23 13:12:15 +0000222 lnk = &mc_lst->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
224 ipv6_addr_equal(&mc_lst->addr, addr)) {
225 struct net_device *dev;
226
227 *lnk = mc_lst->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
WANG Cong414b6c92014-09-11 15:35:14 -0700229 dev = __dev_get_by_index(net, mc_lst->ifindex);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800230 if (dev != NULL) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000231 struct inet6_dev *idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
David L Stevensacd6e002006-08-17 16:27:39 -0700233 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000234 if (idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
David L Stevensacd6e002006-08-17 16:27:39 -0700236 } else
237 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200238 rtnl_unlock();
239
Eric Dumazet456b61b2010-11-23 13:12:15 +0000240 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
Lai Jiangshane3cbf282011-03-18 12:00:50 +0800241 kfree_rcu(mc_lst, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return 0;
243 }
244 }
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200245 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
David L Stevens9951f032005-07-08 17:47:28 -0700247 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Eric Dumazet96b52e62010-06-07 21:05:02 +0000250/* called with rcu_read_lock() */
251static struct inet6_dev *ip6_mc_find_dev_rcu(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000252 const struct in6_addr *group,
Eric Dumazet96b52e62010-06-07 21:05:02 +0000253 int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 struct net_device *dev = NULL;
256 struct inet6_dev *idev = NULL;
257
258 if (ifindex == 0) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000259 struct rt6_info *rt = rt6_lookup(net, group, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (rt) {
David S. Millerd1918542011-12-28 20:19:20 -0500262 dev = rt->dst.dev;
Amerigo Wang94e187c2012-10-29 00:13:19 +0000263 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265 } else
Eric Dumazet96b52e62010-06-07 21:05:02 +0000266 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 if (!dev)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000269 return NULL;
270 idev = __in6_dev_get(dev);
Ilpo Järvinen448eb712008-12-14 23:15:21 -0800271 if (!idev)
Joe Perches8a22c992010-11-14 17:05:00 +0000272 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 read_lock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000274 if (idev->dead) {
275 read_unlock_bh(&idev->lock);
276 return NULL;
277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return idev;
279}
280
281void ipv6_sock_mc_close(struct sock *sk)
282{
283 struct ipv6_pinfo *np = inet6_sk(sk);
284 struct ipv6_mc_socklist *mc_lst;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900285 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Eric Dumazet0e1efe92012-12-05 09:18:10 +0000287 if (!rcu_access_pointer(np->ipv6_mc_list))
288 return;
289
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200290 rtnl_lock();
WANG Congb5350912014-09-11 15:35:13 -0700291 while ((mc_lst = rtnl_dereference(np->ipv6_mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 struct net_device *dev;
293
294 np->ipv6_mc_list = mc_lst->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
WANG Cong414b6c92014-09-11 15:35:14 -0700296 dev = __dev_get_by_index(net, mc_lst->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (dev) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000298 struct inet6_dev *idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
David L Stevensacd6e002006-08-17 16:27:39 -0700300 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000301 if (idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
David L Stevensacd6e002006-08-17 16:27:39 -0700303 } else
304 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Eric Dumazet456b61b2010-11-23 13:12:15 +0000306 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
Lai Jiangshane3cbf282011-03-18 12:00:50 +0800307 kfree_rcu(mc_lst, rcu);
Eric Dumazet456b61b2010-11-23 13:12:15 +0000308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200310 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313int ip6_mc_source(int add, int omode, struct sock *sk,
314 struct group_source_req *pgsr)
315{
316 struct in6_addr *source, *group;
317 struct ipv6_mc_socklist *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 struct inet6_dev *idev;
319 struct ipv6_pinfo *inet6 = inet6_sk(sk);
320 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900321 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 int i, j, rv;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700323 int leavegroup = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800324 int pmclocked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 int err;
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
328 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
329
330 if (!ipv6_addr_is_multicast(group))
331 return -EINVAL;
332
Eric Dumazet96b52e62010-06-07 21:05:02 +0000333 rcu_read_lock();
334 idev = ip6_mc_find_dev_rcu(net, group, pgsr->gsr_interface);
335 if (!idev) {
336 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return -ENODEV;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 err = -EADDRNOTAVAIL;
341
Eric Dumazet456b61b2010-11-23 13:12:15 +0000342 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
344 continue;
345 if (ipv6_addr_equal(&pmc->addr, group))
346 break;
347 }
David L Stevens917f2f12005-07-08 17:45:16 -0700348 if (!pmc) { /* must have a prior join */
349 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /* if a source filter was set, must be the same mode as before */
353 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -0700354 if (pmc->sfmode != omode) {
355 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 } else if (pmc->sfmode != omode) {
359 /* allow mode switches for empty-set filters */
360 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
361 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
362 pmc->sfmode = omode;
363 }
364
Eric Dumazet96b52e62010-06-07 21:05:02 +0000365 write_lock(&pmc->sflock);
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800366 pmclocked = 1;
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 psl = pmc->sflist;
369 if (!add) {
370 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -0700371 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 rv = !0;
Ian Morris67ba4152014-08-24 21:53:10 +0100373 for (i = 0; i < psl->sl_count; i++) {
YOSHIFUJI Hideaki / 吉藤英明07c2fec2013-01-29 12:48:23 +0000374 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (rv == 0)
376 break;
377 }
378 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -0700379 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700381 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
382 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
383 leavegroup = 1;
384 goto done;
385 }
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /* update the interface filter */
388 ip6_mc_del_src(idev, group, omode, 1, source, 1);
389
Ian Morris67ba4152014-08-24 21:53:10 +0100390 for (j = i+1; j < psl->sl_count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 psl->sl_addr[j-1] = psl->sl_addr[j];
392 psl->sl_count--;
393 err = 0;
394 goto done;
395 }
396 /* else, add a new source to the filter */
397
398 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
399 err = -ENOBUFS;
400 goto done;
401 }
402 if (!psl || psl->sl_count == psl->sl_max) {
403 struct ip6_sf_socklist *newpsl;
404 int count = IP6_SFBLOCK;
405
406 if (psl)
407 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800408 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (!newpsl) {
410 err = -ENOBUFS;
411 goto done;
412 }
413 newpsl->sl_max = count;
414 newpsl->sl_count = count - IP6_SFBLOCK;
415 if (psl) {
Ian Morris67ba4152014-08-24 21:53:10 +0100416 for (i = 0; i < psl->sl_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 newpsl->sl_addr[i] = psl->sl_addr[i];
418 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
419 }
420 pmc->sflist = psl = newpsl;
421 }
422 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
Ian Morris67ba4152014-08-24 21:53:10 +0100423 for (i = 0; i < psl->sl_count; i++) {
YOSHIFUJI Hideaki / 吉藤英明07c2fec2013-01-29 12:48:23 +0000424 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
Jean Sacren56db1c52013-02-03 21:34:10 +0000425 if (rv == 0) /* There is an error in the address. */
426 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Ian Morris67ba4152014-08-24 21:53:10 +0100428 for (j = psl->sl_count-1; j >= i; j--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 psl->sl_addr[j+1] = psl->sl_addr[j];
430 psl->sl_addr[i] = *source;
431 psl->sl_count++;
432 err = 0;
433 /* update the interface list */
434 ip6_mc_add_src(idev, group, omode, 1, source, 1);
435done:
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800436 if (pmclocked)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000437 write_unlock(&pmc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000439 rcu_read_unlock();
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700440 if (leavegroup)
441 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return err;
443}
444
445int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
446{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000447 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 struct ipv6_mc_socklist *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct inet6_dev *idev;
450 struct ipv6_pinfo *inet6 = inet6_sk(sk);
451 struct ip6_sf_socklist *newpsl, *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900452 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -0700453 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 int i, err;
455
456 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
457
458 if (!ipv6_addr_is_multicast(group))
459 return -EINVAL;
460 if (gsf->gf_fmode != MCAST_INCLUDE &&
461 gsf->gf_fmode != MCAST_EXCLUDE)
462 return -EINVAL;
463
Eric Dumazet96b52e62010-06-07 21:05:02 +0000464 rcu_read_lock();
465 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Eric Dumazet96b52e62010-06-07 21:05:02 +0000467 if (!idev) {
468 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return -ENODEV;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
David S. Miller9c059892005-07-08 21:44:39 -0700472 err = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800473
David L Stevens9951f032005-07-08 17:47:28 -0700474 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
475 leavegroup = 1;
476 goto done;
477 }
478
Eric Dumazet456b61b2010-11-23 13:12:15 +0000479 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (pmc->ifindex != gsf->gf_interface)
481 continue;
482 if (ipv6_addr_equal(&pmc->addr, group))
483 break;
484 }
David L Stevens917f2f12005-07-08 17:45:16 -0700485 if (!pmc) { /* must have a prior join */
486 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (gsf->gf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800490 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
491 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (!newpsl) {
493 err = -ENOBUFS;
494 goto done;
495 }
496 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
Ian Morris67ba4152014-08-24 21:53:10 +0100497 for (i = 0; i < newpsl->sl_count; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct sockaddr_in6 *psin6;
499
500 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
501 newpsl->sl_addr[i] = psin6->sin6_addr;
502 }
503 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
504 newpsl->sl_count, newpsl->sl_addr, 0);
505 if (err) {
506 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
507 goto done;
508 }
Yan Zheng8713dbf2005-10-28 08:02:08 +0800509 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +0800511 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
512 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800513
Eric Dumazet96b52e62010-06-07 21:05:02 +0000514 write_lock(&pmc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 psl = pmc->sflist;
516 if (psl) {
517 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
518 psl->sl_count, psl->sl_addr, 0);
519 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
520 } else
521 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
522 pmc->sflist = newpsl;
523 pmc->sfmode = gsf->gf_fmode;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000524 write_unlock(&pmc->sflock);
David L Stevens917f2f12005-07-08 17:45:16 -0700525 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526done:
527 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000528 rcu_read_unlock();
David L Stevens9951f032005-07-08 17:47:28 -0700529 if (leavegroup)
530 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return err;
532}
533
534int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
535 struct group_filter __user *optval, int __user *optlen)
536{
537 int err, i, count, copycount;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000538 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 struct ipv6_mc_socklist *pmc;
540 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct ipv6_pinfo *inet6 = inet6_sk(sk);
542 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900543 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
546
547 if (!ipv6_addr_is_multicast(group))
548 return -EINVAL;
549
Eric Dumazet96b52e62010-06-07 21:05:02 +0000550 rcu_read_lock();
551 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Eric Dumazet96b52e62010-06-07 21:05:02 +0000553 if (!idev) {
554 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return -ENODEV;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 err = -EADDRNOTAVAIL;
WANG Congf7ed9252014-09-11 15:35:15 -0700559 /* changes to the ipv6_mc_list require the socket lock and
560 * rtnl lock. We have the socket lock and rcu read lock,
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800561 * so reading the list is safe.
562 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Eric Dumazet456b61b2010-11-23 13:12:15 +0000564 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (pmc->ifindex != gsf->gf_interface)
566 continue;
567 if (ipv6_addr_equal(group, &pmc->addr))
568 break;
569 }
570 if (!pmc) /* must have a prior join */
571 goto done;
572 gsf->gf_fmode = pmc->sfmode;
573 psl = pmc->sflist;
574 count = psl ? psl->sl_count : 0;
575 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000576 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
579 gsf->gf_numsrc = count;
580 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
581 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
582 return -EFAULT;
583 }
WANG Congf7ed9252014-09-11 15:35:15 -0700584 /* changes to psl require the socket lock, and a write lock
585 * on pmc->sflock. We have the socket lock so reading here is safe.
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800586 */
Ian Morris67ba4152014-08-24 21:53:10 +0100587 for (i = 0; i < copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 struct sockaddr_in6 *psin6;
589 struct sockaddr_storage ss;
590
591 psin6 = (struct sockaddr_in6 *)&ss;
592 memset(&ss, 0, sizeof(ss));
593 psin6->sin6_family = AF_INET6;
594 psin6->sin6_addr = psl->sl_addr[i];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900595 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return -EFAULT;
597 }
598 return 0;
599done:
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 return err;
603}
604
Eric Dumazeta50feda2012-05-18 18:57:34 +0000605bool inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
606 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 struct ipv6_pinfo *np = inet6_sk(sk);
609 struct ipv6_mc_socklist *mc;
610 struct ip6_sf_socklist *psl;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000611 bool rv = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Eric Dumazet456b61b2010-11-23 13:12:15 +0000613 rcu_read_lock();
614 for_each_pmc_rcu(np, mc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (ipv6_addr_equal(&mc->addr, mc_addr))
616 break;
617 }
618 if (!mc) {
Eric Dumazet456b61b2010-11-23 13:12:15 +0000619 rcu_read_unlock();
Eric Dumazeta50feda2012-05-18 18:57:34 +0000620 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800622 read_lock(&mc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 psl = mc->sflist;
624 if (!psl) {
625 rv = mc->sfmode == MCAST_EXCLUDE;
626 } else {
627 int i;
628
Ian Morris67ba4152014-08-24 21:53:10 +0100629 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
631 break;
632 }
633 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000634 rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000636 rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800638 read_unlock(&mc->sflock);
Eric Dumazet456b61b2010-11-23 13:12:15 +0000639 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 return rv;
642}
643
644static void ma_put(struct ifmcaddr6 *mc)
645{
646 if (atomic_dec_and_test(&mc->mca_refcnt)) {
647 in6_dev_put(mc->idev);
648 kfree(mc);
649 }
650}
651
652static void igmp6_group_added(struct ifmcaddr6 *mc)
653{
654 struct net_device *dev = mc->idev->dev;
655 char buf[MAX_ADDR_LEN];
656
YOSHIFUJI Hideaki / 吉藤英明ec16ef22013-02-09 04:29:58 +0000657 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
658 IPV6_ADDR_SCOPE_LINKLOCAL)
659 return;
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 spin_lock_bh(&mc->mca_lock);
662 if (!(mc->mca_flags&MAF_LOADED)) {
663 mc->mca_flags |= MAF_LOADED;
664 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000665 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667 spin_unlock_bh(&mc->mca_lock);
668
669 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
670 return;
671
Daniel Borkmann6c567b72013-09-04 00:19:38 +0200672 if (mld_in_v1_mode(mc->idev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 igmp6_join_group(mc);
674 return;
675 }
676 /* else v2 */
677
678 mc->mca_crcount = mc->idev->mc_qrv;
679 mld_ifc_event(mc->idev);
680}
681
682static void igmp6_group_dropped(struct ifmcaddr6 *mc)
683{
684 struct net_device *dev = mc->idev->dev;
685 char buf[MAX_ADDR_LEN];
686
YOSHIFUJI Hideaki / 吉藤英明ec16ef22013-02-09 04:29:58 +0000687 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
688 IPV6_ADDR_SCOPE_LINKLOCAL)
689 return;
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 spin_lock_bh(&mc->mca_lock);
692 if (mc->mca_flags&MAF_LOADED) {
693 mc->mca_flags &= ~MAF_LOADED;
694 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000695 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697
698 if (mc->mca_flags & MAF_NOREPORT)
699 goto done;
700 spin_unlock_bh(&mc->mca_lock);
701
702 if (!mc->idev->dead)
703 igmp6_leave_group(mc);
704
705 spin_lock_bh(&mc->mca_lock);
706 if (del_timer(&mc->mca_timer))
707 atomic_dec(&mc->mca_refcnt);
708done:
709 ip6_mc_clear_src(mc);
710 spin_unlock_bh(&mc->mca_lock);
711}
712
713/*
714 * deleted ifmcaddr6 manipulation
715 */
716static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
717{
718 struct ifmcaddr6 *pmc;
719
720 /* this is an "ifmcaddr6" for convenience; only the fields below
721 * are actually used. In particular, the refcnt and users are not
722 * used for management of the delete list. Using the same structure
723 * for deleted items allows change reports to use common code with
724 * non-deleted or query-response MCA's.
725 */
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800726 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (!pmc)
728 return;
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 spin_lock_bh(&im->mca_lock);
731 spin_lock_init(&pmc->mca_lock);
732 pmc->idev = im->idev;
733 in6_dev_hold(idev);
734 pmc->mca_addr = im->mca_addr;
735 pmc->mca_crcount = idev->mc_qrv;
736 pmc->mca_sfmode = im->mca_sfmode;
737 if (pmc->mca_sfmode == MCAST_INCLUDE) {
738 struct ip6_sf_list *psf;
739
740 pmc->mca_tomb = im->mca_tomb;
741 pmc->mca_sources = im->mca_sources;
742 im->mca_tomb = im->mca_sources = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +0100743 for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 psf->sf_crcount = pmc->mca_crcount;
745 }
746 spin_unlock_bh(&im->mca_lock);
747
Stephen Hemminger6457d262010-02-17 18:48:44 -0800748 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 pmc->next = idev->mc_tomb;
750 idev->mc_tomb = pmc;
Stephen Hemminger6457d262010-02-17 18:48:44 -0800751 spin_unlock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000754static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *pmca)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 struct ifmcaddr6 *pmc, *pmc_prev;
757 struct ip6_sf_list *psf, *psf_next;
758
Stephen Hemminger6457d262010-02-17 18:48:44 -0800759 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 pmc_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +0100761 for (pmc = idev->mc_tomb; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
763 break;
764 pmc_prev = pmc;
765 }
766 if (pmc) {
767 if (pmc_prev)
768 pmc_prev->next = pmc->next;
769 else
770 idev->mc_tomb = pmc->next;
771 }
Stephen Hemminger6457d262010-02-17 18:48:44 -0800772 spin_unlock_bh(&idev->mc_lock);
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (pmc) {
Ian Morris67ba4152014-08-24 21:53:10 +0100775 for (psf = pmc->mca_tomb; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 psf_next = psf->sf_next;
777 kfree(psf);
778 }
779 in6_dev_put(pmc->idev);
780 kfree(pmc);
781 }
782}
783
784static void mld_clear_delrec(struct inet6_dev *idev)
785{
786 struct ifmcaddr6 *pmc, *nextpmc;
787
Stephen Hemminger6457d262010-02-17 18:48:44 -0800788 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 pmc = idev->mc_tomb;
790 idev->mc_tomb = NULL;
Stephen Hemminger6457d262010-02-17 18:48:44 -0800791 spin_unlock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 for (; pmc; pmc = nextpmc) {
794 nextpmc = pmc->next;
795 ip6_mc_clear_src(pmc);
796 in6_dev_put(pmc->idev);
797 kfree(pmc);
798 }
799
800 /* clear dead sources, too */
801 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100802 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 struct ip6_sf_list *psf, *psf_next;
804
805 spin_lock_bh(&pmc->mca_lock);
806 psf = pmc->mca_tomb;
807 pmc->mca_tomb = NULL;
808 spin_unlock_bh(&pmc->mca_lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100809 for (; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 psf_next = psf->sf_next;
811 kfree(psf);
812 }
813 }
814 read_unlock_bh(&idev->lock);
815}
816
817
818/*
819 * device multicast group inc (add if not found)
820 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900821int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 struct ifmcaddr6 *mc;
824 struct inet6_dev *idev;
825
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200826 ASSERT_RTNL();
827
Eric Dumazet96b52e62010-06-07 21:05:02 +0000828 /* we need to take a reference on idev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 idev = in6_dev_get(dev);
830
831 if (idev == NULL)
832 return -EINVAL;
833
834 write_lock_bh(&idev->lock);
835 if (idev->dead) {
836 write_unlock_bh(&idev->lock);
837 in6_dev_put(idev);
838 return -ENODEV;
839 }
840
841 for (mc = idev->mc_list; mc; mc = mc->next) {
842 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
843 mc->mca_users++;
844 write_unlock_bh(&idev->lock);
845 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
846 NULL, 0);
847 in6_dev_put(idev);
848 return 0;
849 }
850 }
851
852 /*
853 * not found: create a new one.
854 */
855
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800856 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 if (mc == NULL) {
859 write_unlock_bh(&idev->lock);
860 in6_dev_put(idev);
861 return -ENOMEM;
862 }
863
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800864 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000866 mc->mca_addr = *addr;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000867 mc->idev = idev; /* (reference taken) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 mc->mca_users = 1;
869 /* mca_stamp should be updated upon changes */
870 mc->mca_cstamp = mc->mca_tstamp = jiffies;
871 atomic_set(&mc->mca_refcnt, 2);
872 spin_lock_init(&mc->mca_lock);
873
874 /* initial mode is (EX, empty) */
875 mc->mca_sfmode = MCAST_EXCLUDE;
876 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
877
878 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
879 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
880 mc->mca_flags |= MAF_NOREPORT;
881
882 mc->next = idev->mc_list;
883 idev->mc_list = mc;
884 write_unlock_bh(&idev->lock);
885
886 mld_del_delrec(idev, &mc->mca_addr);
887 igmp6_group_added(mc);
888 ma_put(mc);
889 return 0;
890}
891
892/*
893 * device multicast group del
894 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900895int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
897 struct ifmcaddr6 *ma, **map;
898
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200899 ASSERT_RTNL();
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 write_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100902 for (map = &idev->mc_list; (ma = *map) != NULL; map = &ma->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
904 if (--ma->mca_users == 0) {
905 *map = ma->next;
906 write_unlock_bh(&idev->lock);
907
908 igmp6_group_dropped(ma);
909
910 ma_put(ma);
911 return 0;
912 }
913 write_unlock_bh(&idev->lock);
914 return 0;
915 }
916 }
917 write_unlock_bh(&idev->lock);
918
919 return -ENOENT;
920}
921
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900922int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Eric Dumazet96b52e62010-06-07 21:05:02 +0000924 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 int err;
926
WANG Cong414b6c92014-09-11 15:35:14 -0700927 ASSERT_RTNL();
Eric Dumazet96b52e62010-06-07 21:05:02 +0000928
929 idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (!idev)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000931 err = -ENODEV;
932 else
933 err = __ipv6_dev_mc_dec(idev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return err;
936}
937
938/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 * check if the interface/address pair is valid
940 */
Eric Dumazeta50feda2012-05-18 18:57:34 +0000941bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
942 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 struct inet6_dev *idev;
945 struct ifmcaddr6 *mc;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000946 bool rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Eric Dumazet96b52e62010-06-07 21:05:02 +0000948 rcu_read_lock();
949 idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (idev) {
951 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100952 for (mc = idev->mc_list; mc; mc = mc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (ipv6_addr_equal(&mc->mca_addr, group))
954 break;
955 }
956 if (mc) {
957 if (src_addr && !ipv6_addr_any(src_addr)) {
958 struct ip6_sf_list *psf;
959
960 spin_lock_bh(&mc->mca_lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100961 for (psf = mc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
963 break;
964 }
965 if (psf)
966 rv = psf->sf_count[MCAST_INCLUDE] ||
967 psf->sf_count[MCAST_EXCLUDE] !=
968 mc->mca_sfcount[MCAST_EXCLUDE];
969 else
Ian Morris67ba4152014-08-24 21:53:10 +0100970 rv = mc->mca_sfcount[MCAST_EXCLUDE] != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 spin_unlock_bh(&mc->mca_lock);
972 } else
Eric Dumazeta50feda2012-05-18 18:57:34 +0000973 rv = true; /* don't filter unspecified source */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
975 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
Eric Dumazet96b52e62010-06-07 21:05:02 +0000977 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return rv;
979}
980
981static void mld_gq_start_timer(struct inet6_dev *idev)
982{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500983 unsigned long tv = prandom_u32() % idev->mc_maxdelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 idev->mc_gq_running = 1;
986 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
987 in6_dev_hold(idev);
988}
989
Daniel Borkmannb4af8de2013-09-04 00:19:43 +0200990static void mld_gq_stop_timer(struct inet6_dev *idev)
991{
992 idev->mc_gq_running = 0;
993 if (del_timer(&idev->mc_gq_timer))
994 __in6_dev_put(idev);
995}
996
Daniel Borkmannc2cef4e2013-08-20 12:22:01 +0200997static void mld_ifc_start_timer(struct inet6_dev *idev, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500999 unsigned long tv = prandom_u32() % delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1002 in6_dev_hold(idev);
1003}
1004
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001005static void mld_ifc_stop_timer(struct inet6_dev *idev)
1006{
1007 idev->mc_ifc_count = 0;
1008 if (del_timer(&idev->mc_ifc_timer))
1009 __in6_dev_put(idev);
1010}
1011
Daniel Borkmannc2cef4e2013-08-20 12:22:01 +02001012static void mld_dad_start_timer(struct inet6_dev *idev, unsigned long delay)
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02001013{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001014 unsigned long tv = prandom_u32() % delay;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02001015
1016 if (!mod_timer(&idev->mc_dad_timer, jiffies+tv+2))
1017 in6_dev_hold(idev);
1018}
1019
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001020static void mld_dad_stop_timer(struct inet6_dev *idev)
1021{
1022 if (del_timer(&idev->mc_dad_timer))
1023 __in6_dev_put(idev);
1024}
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026/*
1027 * IGMP handling (alias multicast ICMPv6 messages)
1028 */
1029
1030static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1031{
1032 unsigned long delay = resptime;
1033
1034 /* Do not start timer for these addresses */
1035 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1036 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1037 return;
1038
1039 if (del_timer(&ma->mca_timer)) {
1040 atomic_dec(&ma->mca_refcnt);
1041 delay = ma->mca_timer.expires - jiffies;
1042 }
1043
Daniel Borkmanncc7f7ab2013-09-04 00:19:41 +02001044 if (delay >= resptime)
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001045 delay = prandom_u32() % resptime;
Daniel Borkmanncc7f7ab2013-09-04 00:19:41 +02001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 ma->mca_timer.expires = jiffies + delay;
1048 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1049 atomic_inc(&ma->mca_refcnt);
1050 ma->mca_flags |= MAF_TIMER_RUNNING;
1051}
1052
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001053/* mark EXCLUDE-mode sources */
Eric Dumazeta50feda2012-05-18 18:57:34 +00001054static bool mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1055 const struct in6_addr *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 struct ip6_sf_list *psf;
1058 int i, scount;
1059
1060 scount = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01001061 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (scount == nsrcs)
1063 break;
Ian Morris67ba4152014-08-24 21:53:10 +01001064 for (i = 0; i < nsrcs; i++) {
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001065 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +00001066 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001067 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1068 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +08001069 break;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001070 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1071 scount++;
1072 break;
1073 }
1074 }
1075 }
1076 pmc->mca_flags &= ~MAF_GSQUERY;
1077 if (scount == nsrcs) /* all sources excluded */
Eric Dumazeta50feda2012-05-18 18:57:34 +00001078 return false;
1079 return true;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001080}
1081
Eric Dumazeta50feda2012-05-18 18:57:34 +00001082static bool mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1083 const struct in6_addr *srcs)
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001084{
1085 struct ip6_sf_list *psf;
1086 int i, scount;
1087
1088 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1089 return mld_xmarksources(pmc, nsrcs, srcs);
1090
1091 /* mark INCLUDE-mode sources */
1092
1093 scount = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01001094 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001095 if (scount == nsrcs)
1096 break;
Ian Morris67ba4152014-08-24 21:53:10 +01001097 for (i = 0; i < nsrcs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1099 psf->sf_gsresp = 1;
1100 scount++;
1101 break;
1102 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001105 if (!scount) {
1106 pmc->mca_flags &= ~MAF_GSQUERY;
Eric Dumazeta50feda2012-05-18 18:57:34 +00001107 return false;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001108 }
1109 pmc->mca_flags |= MAF_GSQUERY;
Eric Dumazeta50feda2012-05-18 18:57:34 +00001110 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
Daniel Borkmann58c0ecf2013-09-04 00:19:40 +02001113static int mld_force_mld_version(const struct inet6_dev *idev)
1114{
1115 /* Normally, both are 0 here. If enforcement to a particular is
1116 * being used, individual device enforcement will have a lower
1117 * precedence over 'all' device (.../conf/all/force_mld_version).
1118 */
1119
1120 if (dev_net(idev->dev)->ipv6.devconf_all->force_mld_version != 0)
1121 return dev_net(idev->dev)->ipv6.devconf_all->force_mld_version;
1122 else
1123 return idev->cnf.force_mld_version;
1124}
1125
1126static bool mld_in_v2_mode_only(const struct inet6_dev *idev)
1127{
1128 return mld_force_mld_version(idev) == 2;
1129}
1130
1131static bool mld_in_v1_mode_only(const struct inet6_dev *idev)
1132{
1133 return mld_force_mld_version(idev) == 1;
1134}
1135
Daniel Borkmann6c567b72013-09-04 00:19:38 +02001136static bool mld_in_v1_mode(const struct inet6_dev *idev)
1137{
Daniel Borkmann58c0ecf2013-09-04 00:19:40 +02001138 if (mld_in_v2_mode_only(idev))
1139 return false;
1140 if (mld_in_v1_mode_only(idev))
Daniel Borkmann6c567b72013-09-04 00:19:38 +02001141 return true;
1142 if (idev->mc_v1_seen && time_before(jiffies, idev->mc_v1_seen))
1143 return true;
1144
1145 return false;
1146}
1147
Daniel Borkmann89225d12013-09-04 00:19:37 +02001148static void mld_set_v1_mode(struct inet6_dev *idev)
1149{
1150 /* RFC3810, relevant sections:
1151 * - 9.1. Robustness Variable
1152 * - 9.2. Query Interval
1153 * - 9.3. Query Response Interval
1154 * - 9.12. Older Version Querier Present Timeout
1155 */
1156 unsigned long switchback;
1157
1158 switchback = (idev->mc_qrv * idev->mc_qi) + idev->mc_qri;
1159
1160 idev->mc_v1_seen = jiffies + switchback;
1161}
1162
1163static void mld_update_qrv(struct inet6_dev *idev,
1164 const struct mld2_query *mlh2)
1165{
1166 /* RFC3810, relevant sections:
1167 * - 5.1.8. QRV (Querier's Robustness Variable)
1168 * - 9.1. Robustness Variable
1169 */
1170
1171 /* The value of the Robustness Variable MUST NOT be zero,
1172 * and SHOULD NOT be one. Catch this here if we ever run
1173 * into such a case in future.
1174 */
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02001175 const int min_qrv = min(MLD_QRV_DEFAULT, sysctl_mld_qrv);
Daniel Borkmann89225d12013-09-04 00:19:37 +02001176 WARN_ON(idev->mc_qrv == 0);
1177
1178 if (mlh2->mld2q_qrv > 0)
1179 idev->mc_qrv = mlh2->mld2q_qrv;
1180
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02001181 if (unlikely(idev->mc_qrv < min_qrv)) {
Daniel Borkmann89225d12013-09-04 00:19:37 +02001182 net_warn_ratelimited("IPv6: MLD: clamping QRV from %u to %u!\n",
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02001183 idev->mc_qrv, min_qrv);
1184 idev->mc_qrv = min_qrv;
Daniel Borkmann89225d12013-09-04 00:19:37 +02001185 }
1186}
1187
1188static void mld_update_qi(struct inet6_dev *idev,
1189 const struct mld2_query *mlh2)
1190{
1191 /* RFC3810, relevant sections:
1192 * - 5.1.9. QQIC (Querier's Query Interval Code)
1193 * - 9.2. Query Interval
1194 * - 9.12. Older Version Querier Present Timeout
1195 * (the [Query Interval] in the last Query received)
1196 */
1197 unsigned long mc_qqi;
1198
1199 if (mlh2->mld2q_qqic < 128) {
1200 mc_qqi = mlh2->mld2q_qqic;
1201 } else {
1202 unsigned long mc_man, mc_exp;
1203
1204 mc_exp = MLDV2_QQIC_EXP(mlh2->mld2q_qqic);
1205 mc_man = MLDV2_QQIC_MAN(mlh2->mld2q_qqic);
1206
1207 mc_qqi = (mc_man | 0x10) << (mc_exp + 3);
1208 }
1209
1210 idev->mc_qi = mc_qqi * HZ;
1211}
1212
1213static void mld_update_qri(struct inet6_dev *idev,
1214 const struct mld2_query *mlh2)
1215{
1216 /* RFC3810, relevant sections:
1217 * - 5.1.3. Maximum Response Code
1218 * - 9.3. Query Response Interval
1219 */
Daniel Borkmanne3f5b172013-09-04 00:19:39 +02001220 idev->mc_qri = msecs_to_jiffies(mldv2_mrc(mlh2));
Daniel Borkmann89225d12013-09-04 00:19:37 +02001221}
1222
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001223static int mld_process_v1(struct inet6_dev *idev, struct mld_msg *mld,
1224 unsigned long *max_delay)
1225{
1226 unsigned long mldv1_md;
1227
1228 /* Ignore v1 queries */
1229 if (mld_in_v2_mode_only(idev))
1230 return -EINVAL;
1231
1232 /* MLDv1 router present */
1233 mldv1_md = ntohs(mld->mld_maxdelay);
1234 *max_delay = max(msecs_to_jiffies(mldv1_md), 1UL);
1235
1236 mld_set_v1_mode(idev);
1237
1238 /* cancel MLDv2 report timer */
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001239 mld_gq_stop_timer(idev);
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001240 /* cancel the interface change timer */
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001241 mld_ifc_stop_timer(idev);
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001242 /* clear deleted report items */
1243 mld_clear_delrec(idev);
1244
1245 return 0;
1246}
1247
1248static int mld_process_v2(struct inet6_dev *idev, struct mld2_query *mld,
1249 unsigned long *max_delay)
1250{
1251 /* hosts need to stay in MLDv1 mode, discard MLDv2 queries */
1252 if (mld_in_v1_mode(idev))
1253 return -EINVAL;
1254
1255 *max_delay = max(msecs_to_jiffies(mldv2_mrc(mld)), 1UL);
1256
1257 mld_update_qrv(idev, mld);
1258 mld_update_qi(idev, mld);
1259 mld_update_qri(idev, mld);
1260
1261 idev->mc_maxdelay = *max_delay;
1262
1263 return 0;
1264}
1265
Eric Dumazet96b52e62010-06-07 21:05:02 +00001266/* called with rcu_read_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267int igmp6_event_query(struct sk_buff *skb)
1268{
Yan Zheng97300b52005-10-31 20:09:45 +08001269 struct mld2_query *mlh2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 struct ifmcaddr6 *ma;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001271 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 unsigned long max_delay;
1273 struct inet6_dev *idev;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001274 struct mld_msg *mld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int group_type;
1276 int mark = 0;
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001277 int len, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1280 return -EINVAL;
1281
1282 /* compute payload length excluding extension headers */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001283 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001284 len -= skb_network_header_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Hangbin Liue940f5d2014-06-27 09:57:53 +08001286 /* RFC3810 6.2
1287 * Upon reception of an MLD message that contains a Query, the node
1288 * checks if the source address of the message is a valid link-local
1289 * address, if the Hop Limit is set to 1, and if the Router Alert
1290 * option is present in the Hop-By-Hop Options header of the IPv6
1291 * packet. If any of these checks fails, the packet is dropped.
1292 */
1293 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL) ||
1294 ipv6_hdr(skb)->hop_limit != 1 ||
1295 !(IP6CB(skb)->flags & IP6SKB_ROUTERALERT) ||
1296 IP6CB(skb)->ra != htons(IPV6_OPT_ROUTERALERT_MLD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return -EINVAL;
1298
Eric Dumazet96b52e62010-06-07 21:05:02 +00001299 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (idev == NULL)
1301 return 0;
1302
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001303 mld = (struct mld_msg *)icmp6_hdr(skb);
1304 group = &mld->mld_mca;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 group_type = ipv6_addr_type(group);
1306
1307 if (group_type != IPV6_ADDR_ANY &&
Eric Dumazet96b52e62010-06-07 21:05:02 +00001308 !(group_type&IPV6_ADDR_MULTICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Daniel Borkmann9fd07842013-08-20 12:22:02 +02001311 if (len == MLD_V1_QUERY_LEN) {
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001312 err = mld_process_v1(idev, mld, &max_delay);
1313 if (err < 0)
1314 return err;
Daniel Borkmann9fd07842013-08-20 12:22:02 +02001315 } else if (len >= MLD_V2_QUERY_LEN_MIN) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001316 int srcs_offset = sizeof(struct mld2_query) -
Yan Zheng97300b52005-10-31 20:09:45 +08001317 sizeof(struct icmp6hdr);
Daniel Borkmann89225d12013-09-04 00:19:37 +02001318
Eric Dumazet96b52e62010-06-07 21:05:02 +00001319 if (!pskb_may_pull(skb, srcs_offset))
Yan Zheng97300b52005-10-31 20:09:45 +08001320 return -EINVAL;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001321
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001322 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Daniel Borkmann84698962013-08-20 12:22:00 +02001323
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001324 err = mld_process_v2(idev, mlh2, &max_delay);
1325 if (err < 0)
1326 return err;
Daniel Borkmann89225d12013-09-04 00:19:37 +02001327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (group_type == IPV6_ADDR_ANY) { /* general query */
Eric Dumazet96b52e62010-06-07 21:05:02 +00001329 if (mlh2->mld2q_nsrcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return -EINVAL; /* no sources allowed */
Eric Dumazet96b52e62010-06-07 21:05:02 +00001331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 mld_gq_start_timer(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return 0;
1334 }
1335 /* mark sources to include, if group & source-specific */
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001336 if (mlh2->mld2q_nsrcs != 0) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001337 if (!pskb_may_pull(skb, srcs_offset +
Eric Dumazet96b52e62010-06-07 21:05:02 +00001338 ntohs(mlh2->mld2q_nsrcs) * sizeof(struct in6_addr)))
Yan Zheng97300b52005-10-31 20:09:45 +08001339 return -EINVAL;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001340
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001341 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Yan Zheng97300b52005-10-31 20:09:45 +08001342 mark = 1;
1343 }
Eric Dumazet96b52e62010-06-07 21:05:02 +00001344 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 read_lock_bh(&idev->lock);
1348 if (group_type == IPV6_ADDR_ANY) {
Ian Morris67ba4152014-08-24 21:53:10 +01001349 for (ma = idev->mc_list; ma; ma = ma->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 spin_lock_bh(&ma->mca_lock);
1351 igmp6_group_queried(ma, max_delay);
1352 spin_unlock_bh(&ma->mca_lock);
1353 }
1354 } else {
Ian Morris67ba4152014-08-24 21:53:10 +01001355 for (ma = idev->mc_list; ma; ma = ma->next) {
David L Stevens7add2a42006-01-24 13:06:39 -08001356 if (!ipv6_addr_equal(group, &ma->mca_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 continue;
1358 spin_lock_bh(&ma->mca_lock);
1359 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1360 /* gsquery <- gsquery && mark */
1361 if (!mark)
1362 ma->mca_flags &= ~MAF_GSQUERY;
1363 } else {
1364 /* gsquery <- mark */
1365 if (mark)
1366 ma->mca_flags |= MAF_GSQUERY;
1367 else
1368 ma->mca_flags &= ~MAF_GSQUERY;
1369 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001370 if (!(ma->mca_flags & MAF_GSQUERY) ||
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001371 mld_marksources(ma, ntohs(mlh2->mld2q_nsrcs), mlh2->mld2q_srcs))
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001372 igmp6_group_queried(ma, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 spin_unlock_bh(&ma->mca_lock);
David L Stevens7add2a42006-01-24 13:06:39 -08001374 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
1376 }
1377 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 return 0;
1380}
1381
Eric Dumazet96b52e62010-06-07 21:05:02 +00001382/* called with rcu_read_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383int igmp6_event_report(struct sk_buff *skb)
1384{
1385 struct ifmcaddr6 *ma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 struct inet6_dev *idev;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001387 struct mld_msg *mld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 int addr_type;
1389
1390 /* Our own report looped back. Ignore it. */
1391 if (skb->pkt_type == PACKET_LOOPBACK)
1392 return 0;
1393
David Stevens24c69272005-12-02 20:32:59 -08001394 /* send our report if the MC router may not have heard this report */
1395 if (skb->pkt_type != PACKET_MULTICAST &&
1396 skb->pkt_type != PACKET_BROADCAST)
1397 return 0;
1398
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001399 if (!pskb_may_pull(skb, sizeof(*mld) - sizeof(struct icmp6hdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return -EINVAL;
1401
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001402 mld = (struct mld_msg *)icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 /* Drop reports with not link local source */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001405 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001406 if (addr_type != IPV6_ADDR_ANY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 !(addr_type&IPV6_ADDR_LINKLOCAL))
1408 return -EINVAL;
1409
Eric Dumazet96b52e62010-06-07 21:05:02 +00001410 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (idev == NULL)
1412 return -ENODEV;
1413
1414 /*
1415 * Cancel the timer for this group
1416 */
1417
1418 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +01001419 for (ma = idev->mc_list; ma; ma = ma->next) {
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001420 if (ipv6_addr_equal(&ma->mca_addr, &mld->mld_mca)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 spin_lock(&ma->mca_lock);
1422 if (del_timer(&ma->mca_timer))
1423 atomic_dec(&ma->mca_refcnt);
1424 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1425 spin_unlock(&ma->mca_lock);
1426 break;
1427 }
1428 }
1429 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return 0;
1431}
1432
Eric Dumazeta50feda2012-05-18 18:57:34 +00001433static bool is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1434 int gdeleted, int sdeleted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
1436 switch (type) {
1437 case MLD2_MODE_IS_INCLUDE:
1438 case MLD2_MODE_IS_EXCLUDE:
1439 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001440 return false;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001441 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1442 if (pmc->mca_sfmode == MCAST_INCLUDE)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001443 return true;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001444 /* don't include if this source is excluded
1445 * in all filters
1446 */
1447 if (psf->sf_count[MCAST_INCLUDE])
David L Stevens7add2a42006-01-24 13:06:39 -08001448 return type == MLD2_MODE_IS_INCLUDE;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001449 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1450 psf->sf_count[MCAST_EXCLUDE];
1451 }
Eric Dumazeta50feda2012-05-18 18:57:34 +00001452 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 case MLD2_CHANGE_TO_INCLUDE:
1454 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001455 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return psf->sf_count[MCAST_INCLUDE] != 0;
1457 case MLD2_CHANGE_TO_EXCLUDE:
1458 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001459 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1461 psf->sf_count[MCAST_INCLUDE])
Eric Dumazeta50feda2012-05-18 18:57:34 +00001462 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1464 psf->sf_count[MCAST_EXCLUDE];
1465 case MLD2_ALLOW_NEW_SOURCES:
1466 if (gdeleted || !psf->sf_crcount)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001467 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1469 case MLD2_BLOCK_OLD_SOURCES:
1470 if (pmc->mca_sfmode == MCAST_INCLUDE)
1471 return gdeleted || (psf->sf_crcount && sdeleted);
1472 return psf->sf_crcount && !gdeleted && !sdeleted;
1473 }
Eric Dumazeta50feda2012-05-18 18:57:34 +00001474 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
1477static int
1478mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1479{
1480 struct ip6_sf_list *psf;
1481 int scount = 0;
1482
Ian Morris67ba4152014-08-24 21:53:10 +01001483 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1485 continue;
1486 scount++;
1487 }
1488 return scount;
1489}
1490
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +00001491static void ip6_mc_hdr(struct sock *sk, struct sk_buff *skb,
1492 struct net_device *dev,
1493 const struct in6_addr *saddr,
1494 const struct in6_addr *daddr,
1495 int proto, int len)
1496{
1497 struct ipv6hdr *hdr;
1498
1499 skb->protocol = htons(ETH_P_IPV6);
1500 skb->dev = dev;
1501
1502 skb_reset_network_header(skb);
1503 skb_put(skb, sizeof(struct ipv6hdr));
1504 hdr = ipv6_hdr(skb);
1505
1506 ip6_flow_hdr(hdr, 0, 0);
1507
1508 hdr->payload_len = htons(len);
1509 hdr->nexthdr = proto;
1510 hdr->hop_limit = inet6_sk(sk)->hop_limit;
1511
1512 hdr->saddr = *saddr;
1513 hdr->daddr = *daddr;
1514}
1515
Amerigo Wang89657792013-06-29 21:30:49 +08001516static struct sk_buff *mld_newpack(struct inet6_dev *idev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Amerigo Wang89657792013-06-29 21:30:49 +08001518 struct net_device *dev = idev->dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001519 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001520 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 struct sk_buff *skb;
1522 struct mld2_report *pmr;
1523 struct in6_addr addr_buf;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001524 const struct in6_addr *saddr;
Herbert Xua7ae1992011-11-18 02:20:04 +00001525 int hlen = LL_RESERVED_SPACE(dev);
1526 int tlen = dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 int err;
1528 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1529 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1530 IPV6_TLV_PADN, 0 };
1531
1532 /* we assume size > sizeof(ra) here */
Herbert Xua7ae1992011-11-18 02:20:04 +00001533 size += hlen + tlen;
Eric Dumazet72e09ad2010-06-05 03:03:30 -07001534 /* limit our allocations to order-0 page */
1535 size = min_t(int, size, SKB_MAX_ORDER(0, 0));
1536 skb = sock_alloc_send_skb(sk, size, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001538 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return NULL;
1540
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +02001541 skb->priority = TC_PRIO_CONTROL;
Herbert Xua7ae1992011-11-18 02:20:04 +00001542 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Amerigo Wang89657792013-06-29 21:30:49 +08001544 if (__ipv6_get_lladdr(idev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001546 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 * when a valid link-local address is not available.
1548 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001549 saddr = &in6addr_any;
1550 } else
1551 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +00001553 ip6_mc_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1556
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001557 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -03001558 skb_put(skb, sizeof(*pmr));
1559 pmr = (struct mld2_report *)skb_transport_header(skb);
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001560 pmr->mld2r_type = ICMPV6_MLD2_REPORT;
1561 pmr->mld2r_resv1 = 0;
1562 pmr->mld2r_cksum = 0;
1563 pmr->mld2r_resv2 = 0;
1564 pmr->mld2r_ngrec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return skb;
1566}
1567
1568static void mld_sendpack(struct sk_buff *skb)
1569{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001570 struct ipv6hdr *pip6 = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001571 struct mld2_report *pmr =
1572 (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 int payload_len, mldlen;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001574 struct inet6_dev *idev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001575 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 int err;
David S. Miller4c9483b2011-03-12 16:22:43 -05001577 struct flowi6 fl6;
Eric Dumazetadf30902009-06-02 05:19:30 +00001578 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Eric Dumazet96b52e62010-06-07 21:05:02 +00001580 rcu_read_lock();
1581 idev = __in6_dev_get(skb->dev);
Neil Hormanedf391f2009-04-27 02:45:02 -07001582 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
1583
Simon Horman29a3cad2013-05-28 20:34:26 +00001584 payload_len = (skb_tail_pointer(skb) - skb_network_header(skb)) -
1585 sizeof(*pip6);
1586 mldlen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 pip6->payload_len = htons(payload_len);
1588
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001589 pmr->mld2r_cksum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1590 IPPROTO_ICMPV6,
1591 csum_partial(skb_transport_header(skb),
1592 mldlen, 0));
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001593
David S. Miller4c9483b2011-03-12 16:22:43 -05001594 icmpv6_flow_init(net->ipv6.igmp_sk, &fl6, ICMPV6_MLD2_REPORT,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001595 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1596 skb->dev->ifindex);
YOSHIFUJI Hideaki / 吉藤英明12fd84f2013-01-18 02:00:24 +00001597 dst = icmp6_dst_alloc(skb->dev, &fl6);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001598
David S. Miller452edd52011-03-02 13:27:41 -08001599 err = 0;
1600 if (IS_ERR(dst)) {
1601 err = PTR_ERR(dst);
1602 dst = NULL;
1603 }
Eric Dumazetadf30902009-06-02 05:19:30 +00001604 skb_dst_set(skb, dst);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001605 if (err)
1606 goto err_out;
1607
Neil Hormanedf391f2009-04-27 02:45:02 -07001608 payload_len = skb->len;
1609
Jan Engelhardtb2e0b382010-03-23 04:09:07 +01001610 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001611 dst_output);
1612out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (!err) {
Hannes Frederic Sowa43a43b62014-03-31 20:14:10 +02001614 ICMP6MSGOUT_INC_STATS(net, idev, ICMPV6_MLD2_REPORT);
1615 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
1616 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, payload_len);
1617 } else {
1618 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
1619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Eric Dumazet96b52e62010-06-07 21:05:02 +00001621 rcu_read_unlock();
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001622 return;
1623
1624err_out:
1625 kfree_skb(skb);
1626 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
1628
1629static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1630{
Yan Zhengfab10fe2005-10-05 12:08:13 -07001631 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633
1634static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1635 int type, struct mld2_grec **ppgr)
1636{
1637 struct net_device *dev = pmc->idev->dev;
1638 struct mld2_report *pmr;
1639 struct mld2_grec *pgr;
1640
1641 if (!skb)
Amerigo Wang89657792013-06-29 21:30:49 +08001642 skb = mld_newpack(pmc->idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (!skb)
1644 return NULL;
1645 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1646 pgr->grec_type = type;
1647 pgr->grec_auxwords = 0;
1648 pgr->grec_nsrcs = 0;
1649 pgr->grec_mca = pmc->mca_addr; /* structure copy */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001650 pmr = (struct mld2_report *)skb_transport_header(skb);
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001651 pmr->mld2r_ngrec = htons(ntohs(pmr->mld2r_ngrec)+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 *ppgr = pgr;
1653 return skb;
1654}
1655
1656#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1657 skb_tailroom(skb)) : 0)
1658
1659static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001660 int type, int gdeleted, int sdeleted, int crsend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
Amerigo Wang89657792013-06-29 21:30:49 +08001662 struct inet6_dev *idev = pmc->idev;
1663 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 struct mld2_report *pmr;
1665 struct mld2_grec *pgr = NULL;
1666 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001667 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 if (pmc->mca_flags & MAF_NOREPORT)
1670 return skb;
1671
1672 isquery = type == MLD2_MODE_IS_INCLUDE ||
1673 type == MLD2_MODE_IS_EXCLUDE;
1674 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1675 type == MLD2_CHANGE_TO_EXCLUDE;
1676
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001677 stotal = scount = 0;
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1680
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001681 if (!*psf_list)
1682 goto empty_source;
1683
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001684 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 /* EX and TO_EX get a fresh packet, if needed */
1687 if (truncate) {
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001688 if (pmr && pmr->mld2r_ngrec &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1690 if (skb)
1691 mld_sendpack(skb);
Amerigo Wang89657792013-06-29 21:30:49 +08001692 skb = mld_newpack(idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
1694 }
1695 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 psf_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01001697 for (psf = *psf_list; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 struct in6_addr *psrc;
1699
1700 psf_next = psf->sf_next;
1701
1702 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1703 psf_prev = psf;
1704 continue;
1705 }
1706
1707 /* clear marks on query responses */
1708 if (isquery)
1709 psf->sf_gsresp = 0;
1710
1711 if (AVAILABLE(skb) < sizeof(*psrc) +
1712 first*sizeof(struct mld2_grec)) {
1713 if (truncate && !first)
1714 break; /* truncate these */
1715 if (pgr)
1716 pgr->grec_nsrcs = htons(scount);
1717 if (skb)
1718 mld_sendpack(skb);
Amerigo Wang89657792013-06-29 21:30:49 +08001719 skb = mld_newpack(idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 first = 1;
1721 scount = 0;
1722 }
1723 if (first) {
1724 skb = add_grhead(skb, pmc, type, &pgr);
1725 first = 0;
1726 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -08001727 if (!skb)
1728 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1730 *psrc = psf->sf_addr;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001731 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1733 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1734 psf->sf_crcount--;
1735 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1736 if (psf_prev)
1737 psf_prev->sf_next = psf->sf_next;
1738 else
1739 *psf_list = psf->sf_next;
1740 kfree(psf);
1741 continue;
1742 }
1743 }
1744 psf_prev = psf;
1745 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001746
1747empty_source:
1748 if (!stotal) {
1749 if (type == MLD2_ALLOW_NEW_SOURCES ||
1750 type == MLD2_BLOCK_OLD_SOURCES)
1751 return skb;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001752 if (pmc->mca_crcount || isquery || crsend) {
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001753 /* make sure we have room for group header */
1754 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1755 mld_sendpack(skb);
1756 skb = NULL; /* add_grhead will get a new one */
1757 }
1758 skb = add_grhead(skb, pmc, type, &pgr);
1759 }
1760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (pgr)
1762 pgr->grec_nsrcs = htons(scount);
1763
1764 if (isquery)
1765 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1766 return skb;
1767}
1768
1769static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1770{
1771 struct sk_buff *skb = NULL;
1772 int type;
1773
Amerigo Wang89657792013-06-29 21:30:49 +08001774 read_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 if (!pmc) {
Ian Morris67ba4152014-08-24 21:53:10 +01001776 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (pmc->mca_flags & MAF_NOREPORT)
1778 continue;
1779 spin_lock_bh(&pmc->mca_lock);
1780 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1781 type = MLD2_MODE_IS_EXCLUDE;
1782 else
1783 type = MLD2_MODE_IS_INCLUDE;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001784 skb = add_grec(skb, pmc, type, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 spin_unlock_bh(&pmc->mca_lock);
1786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 } else {
1788 spin_lock_bh(&pmc->mca_lock);
1789 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1790 type = MLD2_MODE_IS_EXCLUDE;
1791 else
1792 type = MLD2_MODE_IS_INCLUDE;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001793 skb = add_grec(skb, pmc, type, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 spin_unlock_bh(&pmc->mca_lock);
1795 }
Amerigo Wang89657792013-06-29 21:30:49 +08001796 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 if (skb)
1798 mld_sendpack(skb);
1799}
1800
1801/*
1802 * remove zero-count source records from a source filter list
1803 */
1804static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1805{
1806 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1807
1808 psf_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01001809 for (psf = *ppsf; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 psf_next = psf->sf_next;
1811 if (psf->sf_crcount == 0) {
1812 if (psf_prev)
1813 psf_prev->sf_next = psf->sf_next;
1814 else
1815 *ppsf = psf->sf_next;
1816 kfree(psf);
1817 } else
1818 psf_prev = psf;
1819 }
1820}
1821
1822static void mld_send_cr(struct inet6_dev *idev)
1823{
1824 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1825 struct sk_buff *skb = NULL;
1826 int type, dtype;
1827
1828 read_lock_bh(&idev->lock);
Stephen Hemminger6457d262010-02-17 18:48:44 -08001829 spin_lock(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 /* deleted MCA's */
1832 pmc_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01001833 for (pmc = idev->mc_tomb; pmc; pmc = pmc_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 pmc_next = pmc->next;
1835 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1836 type = MLD2_BLOCK_OLD_SOURCES;
1837 dtype = MLD2_BLOCK_OLD_SOURCES;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001838 skb = add_grec(skb, pmc, type, 1, 0, 0);
1839 skb = add_grec(skb, pmc, dtype, 1, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
1841 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1843 type = MLD2_CHANGE_TO_INCLUDE;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001844 skb = add_grec(skb, pmc, type, 1, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001846 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (pmc->mca_crcount == 0) {
1848 mld_clear_zeros(&pmc->mca_tomb);
1849 mld_clear_zeros(&pmc->mca_sources);
1850 }
1851 }
1852 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1853 !pmc->mca_sources) {
1854 if (pmc_prev)
1855 pmc_prev->next = pmc_next;
1856 else
1857 idev->mc_tomb = pmc_next;
1858 in6_dev_put(pmc->idev);
1859 kfree(pmc);
1860 } else
1861 pmc_prev = pmc;
1862 }
Stephen Hemminger6457d262010-02-17 18:48:44 -08001863 spin_unlock(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 /* change recs */
Ian Morris67ba4152014-08-24 21:53:10 +01001866 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 spin_lock_bh(&pmc->mca_lock);
1868 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1869 type = MLD2_BLOCK_OLD_SOURCES;
1870 dtype = MLD2_ALLOW_NEW_SOURCES;
1871 } else {
1872 type = MLD2_ALLOW_NEW_SOURCES;
1873 dtype = MLD2_BLOCK_OLD_SOURCES;
1874 }
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001875 skb = add_grec(skb, pmc, type, 0, 0, 0);
1876 skb = add_grec(skb, pmc, dtype, 0, 1, 0); /* deleted sources */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878 /* filter mode changes */
1879 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1881 type = MLD2_CHANGE_TO_EXCLUDE;
1882 else
1883 type = MLD2_CHANGE_TO_INCLUDE;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001884 skb = add_grec(skb, pmc, type, 0, 0, 0);
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001885 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
1887 spin_unlock_bh(&pmc->mca_lock);
1888 }
1889 read_unlock_bh(&idev->lock);
1890 if (!skb)
1891 return;
1892 (void) mld_sendpack(skb);
1893}
1894
1895static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1896{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001897 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001898 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 struct inet6_dev *idev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001900 struct sk_buff *skb;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001901 struct mld_msg *hdr;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001902 const struct in6_addr *snd_addr, *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 struct in6_addr addr_buf;
Herbert Xua7ae1992011-11-18 02:20:04 +00001904 int hlen = LL_RESERVED_SPACE(dev);
1905 int tlen = dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 int err, len, payload_len, full_len;
1907 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1908 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1909 IPV6_TLV_PADN, 0 };
David S. Miller4c9483b2011-03-12 16:22:43 -05001910 struct flowi6 fl6;
Eric Dumazetadf30902009-06-02 05:19:30 +00001911 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09001913 if (type == ICMPV6_MGM_REDUCTION)
1914 snd_addr = &in6addr_linklocal_allrouters;
1915 else
1916 snd_addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1919 payload_len = len + sizeof(ra);
1920 full_len = sizeof(struct ipv6hdr) + payload_len;
1921
Neil Hormanedf391f2009-04-27 02:45:02 -07001922 rcu_read_lock();
1923 IP6_UPD_PO_STATS(net, __in6_dev_get(dev),
1924 IPSTATS_MIB_OUT, full_len);
1925 rcu_read_unlock();
1926
Herbert Xua7ae1992011-11-18 02:20:04 +00001927 skb = sock_alloc_send_skb(sk, hlen + tlen + full_len, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 if (skb == NULL) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001930 rcu_read_lock();
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001931 IP6_INC_STATS(net, __in6_dev_get(dev),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001932 IPSTATS_MIB_OUTDISCARDS);
1933 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 return;
1935 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +02001936 skb->priority = TC_PRIO_CONTROL;
Herbert Xua7ae1992011-11-18 02:20:04 +00001937 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Neil Horman95c385b2007-04-25 17:08:10 -07001939 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001941 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 * when a valid link-local address is not available.
1943 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001944 saddr = &in6addr_any;
1945 } else
1946 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +00001948 ip6_mc_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1951
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001952 hdr = (struct mld_msg *) skb_put(skb, sizeof(struct mld_msg));
1953 memset(hdr, 0, sizeof(struct mld_msg));
1954 hdr->mld_type = type;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001955 hdr->mld_mca = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001957 hdr->mld_cksum = csum_ipv6_magic(saddr, snd_addr, len,
1958 IPPROTO_ICMPV6,
1959 csum_partial(hdr, len, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Eric Dumazet96b52e62010-06-07 21:05:02 +00001961 rcu_read_lock();
1962 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
David S. Miller4c9483b2011-03-12 16:22:43 -05001964 icmpv6_flow_init(sk, &fl6, type,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001965 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1966 skb->dev->ifindex);
YOSHIFUJI Hideaki / 吉藤英明12fd84f2013-01-18 02:00:24 +00001967 dst = icmp6_dst_alloc(skb->dev, &fl6);
David S. Miller452edd52011-03-02 13:27:41 -08001968 if (IS_ERR(dst)) {
1969 err = PTR_ERR(dst);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001970 goto err_out;
David S. Miller452edd52011-03-02 13:27:41 -08001971 }
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001972
Eric Dumazetadf30902009-06-02 05:19:30 +00001973 skb_dst_set(skb, dst);
Jan Engelhardtb2e0b382010-03-23 04:09:07 +01001974 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001975 dst_output);
1976out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 if (!err) {
Denis V. Lunev5c5d2442008-10-08 10:33:50 -07001978 ICMP6MSGOUT_INC_STATS(net, idev, type);
Denis V. Luneva862f6a2008-10-08 10:33:06 -07001979 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
Neil Hormanedf391f2009-04-27 02:45:02 -07001980 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, full_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 } else
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001982 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Eric Dumazet96b52e62010-06-07 21:05:02 +00001984 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 return;
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001986
1987err_out:
1988 kfree_skb(skb);
1989 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990}
1991
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001992static void mld_send_initial_cr(struct inet6_dev *idev)
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02001993{
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001994 struct sk_buff *skb;
1995 struct ifmcaddr6 *pmc;
1996 int type;
1997
1998 if (mld_in_v1_mode(idev))
1999 return;
2000
2001 skb = NULL;
2002 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002003 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002004 spin_lock_bh(&pmc->mca_lock);
2005 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2006 type = MLD2_CHANGE_TO_EXCLUDE;
2007 else
2008 type = MLD2_CHANGE_TO_INCLUDE;
2009 skb = add_grec(skb, pmc, type, 0, 0, 1);
2010 spin_unlock_bh(&pmc->mca_lock);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002011 }
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002012 read_unlock_bh(&idev->lock);
2013 if (skb)
2014 mld_sendpack(skb);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002015}
2016
2017void ipv6_mc_dad_complete(struct inet6_dev *idev)
2018{
2019 idev->mc_dad_count = idev->mc_qrv;
2020 if (idev->mc_dad_count) {
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002021 mld_send_initial_cr(idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002022 idev->mc_dad_count--;
2023 if (idev->mc_dad_count)
2024 mld_dad_start_timer(idev, idev->mc_maxdelay);
2025 }
2026}
2027
2028static void mld_dad_timer_expire(unsigned long data)
2029{
2030 struct inet6_dev *idev = (struct inet6_dev *)data;
2031
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002032 mld_send_initial_cr(idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002033 if (idev->mc_dad_count) {
2034 idev->mc_dad_count--;
2035 if (idev->mc_dad_count)
2036 mld_dad_start_timer(idev, idev->mc_maxdelay);
2037 }
Salam Noureddine9260d3e2013-09-29 13:41:34 -07002038 in6_dev_put(idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002039}
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002042 const struct in6_addr *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
2044 struct ip6_sf_list *psf, *psf_prev;
2045 int rv = 0;
2046
2047 psf_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01002048 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
2050 break;
2051 psf_prev = psf;
2052 }
2053 if (!psf || psf->sf_count[sfmode] == 0) {
2054 /* source filter not found, or count wrong => bug */
2055 return -ESRCH;
2056 }
2057 psf->sf_count[sfmode]--;
2058 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
2059 struct inet6_dev *idev = pmc->idev;
2060
2061 /* no more filters for this source */
2062 if (psf_prev)
2063 psf_prev->sf_next = psf->sf_next;
2064 else
2065 pmc->mca_sources = psf->sf_next;
2066 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
Daniel Borkmann6c567b72013-09-04 00:19:38 +02002067 !mld_in_v1_mode(idev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 psf->sf_crcount = idev->mc_qrv;
2069 psf->sf_next = pmc->mca_tomb;
2070 pmc->mca_tomb = psf;
2071 rv = 1;
2072 } else
2073 kfree(psf);
2074 }
2075 return rv;
2076}
2077
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002078static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2079 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 int delta)
2081{
2082 struct ifmcaddr6 *pmc;
2083 int changerec = 0;
2084 int i, err;
2085
2086 if (!idev)
2087 return -ENODEV;
2088 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002089 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2091 break;
2092 }
2093 if (!pmc) {
2094 /* MCA not found?? bug */
2095 read_unlock_bh(&idev->lock);
2096 return -ESRCH;
2097 }
2098 spin_lock_bh(&pmc->mca_lock);
2099 sf_markstate(pmc);
2100 if (!delta) {
2101 if (!pmc->mca_sfcount[sfmode]) {
2102 spin_unlock_bh(&pmc->mca_lock);
2103 read_unlock_bh(&idev->lock);
2104 return -EINVAL;
2105 }
2106 pmc->mca_sfcount[sfmode]--;
2107 }
2108 err = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01002109 for (i = 0; i < sfcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2111
2112 changerec |= rv > 0;
2113 if (!err && rv < 0)
2114 err = rv;
2115 }
2116 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
2117 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
2118 pmc->mca_sfcount[MCAST_INCLUDE]) {
2119 struct ip6_sf_list *psf;
2120
2121 /* filter mode change */
2122 pmc->mca_sfmode = MCAST_INCLUDE;
2123 pmc->mca_crcount = idev->mc_qrv;
2124 idev->mc_ifc_count = pmc->mca_crcount;
Ian Morris67ba4152014-08-24 21:53:10 +01002125 for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 psf->sf_crcount = 0;
2127 mld_ifc_event(pmc->idev);
2128 } else if (sf_setstate(pmc) || changerec)
2129 mld_ifc_event(pmc->idev);
2130 spin_unlock_bh(&pmc->mca_lock);
2131 read_unlock_bh(&idev->lock);
2132 return err;
2133}
2134
2135/*
2136 * Add multicast single-source filter to the interface list
2137 */
2138static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
Jun Zhao99d2f472011-11-30 06:21:05 +00002139 const struct in6_addr *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
2141 struct ip6_sf_list *psf, *psf_prev;
2142
2143 psf_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01002144 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
2146 break;
2147 psf_prev = psf;
2148 }
2149 if (!psf) {
Ingo Oeser0c600ed2006-03-20 23:01:32 -08002150 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 if (!psf)
2152 return -ENOBUFS;
Ingo Oeser0c600ed2006-03-20 23:01:32 -08002153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 psf->sf_addr = *psfsrc;
2155 if (psf_prev) {
2156 psf_prev->sf_next = psf;
2157 } else
2158 pmc->mca_sources = psf;
2159 }
2160 psf->sf_count[sfmode]++;
2161 return 0;
2162}
2163
2164static void sf_markstate(struct ifmcaddr6 *pmc)
2165{
2166 struct ip6_sf_list *psf;
2167 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2168
Ian Morris67ba4152014-08-24 21:53:10 +01002169 for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2171 psf->sf_oldin = mca_xcount ==
2172 psf->sf_count[MCAST_EXCLUDE] &&
2173 !psf->sf_count[MCAST_INCLUDE];
2174 } else
2175 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2176}
2177
2178static int sf_setstate(struct ifmcaddr6 *pmc)
2179{
David L Stevens7add2a42006-01-24 13:06:39 -08002180 struct ip6_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2182 int qrv = pmc->idev->mc_qrv;
2183 int new_in, rv;
2184
2185 rv = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01002186 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2188 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2189 !psf->sf_count[MCAST_INCLUDE];
2190 } else
2191 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevens7add2a42006-01-24 13:06:39 -08002192 if (new_in) {
2193 if (!psf->sf_oldin) {
Al Viroe80e28b2006-02-03 20:10:03 -05002194 struct ip6_sf_list *prev = NULL;
David L Stevens7add2a42006-01-24 13:06:39 -08002195
Ian Morris67ba4152014-08-24 21:53:10 +01002196 for (dpsf = pmc->mca_tomb; dpsf;
2197 dpsf = dpsf->sf_next) {
David L Stevens7add2a42006-01-24 13:06:39 -08002198 if (ipv6_addr_equal(&dpsf->sf_addr,
2199 &psf->sf_addr))
2200 break;
2201 prev = dpsf;
2202 }
2203 if (dpsf) {
2204 if (prev)
2205 prev->sf_next = dpsf->sf_next;
2206 else
2207 pmc->mca_tomb = dpsf->sf_next;
2208 kfree(dpsf);
2209 }
2210 psf->sf_crcount = qrv;
2211 rv++;
2212 }
2213 } else if (psf->sf_oldin) {
2214 psf->sf_crcount = 0;
2215 /*
2216 * add or update "delete" records if an active filter
2217 * is now inactive
2218 */
Ian Morris67ba4152014-08-24 21:53:10 +01002219 for (dpsf = pmc->mca_tomb; dpsf; dpsf = dpsf->sf_next)
David L Stevens7add2a42006-01-24 13:06:39 -08002220 if (ipv6_addr_equal(&dpsf->sf_addr,
2221 &psf->sf_addr))
2222 break;
2223 if (!dpsf) {
Joe Perches5d553542010-05-31 17:23:22 +00002224 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevens7add2a42006-01-24 13:06:39 -08002225 if (!dpsf)
2226 continue;
2227 *dpsf = *psf;
2228 /* pmc->mca_lock held by callers */
2229 dpsf->sf_next = pmc->mca_tomb;
2230 pmc->mca_tomb = dpsf;
2231 }
2232 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 rv++;
2234 }
2235 }
2236 return rv;
2237}
2238
2239/*
2240 * Add multicast source filter list to the interface list
2241 */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002242static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2243 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 int delta)
2245{
2246 struct ifmcaddr6 *pmc;
2247 int isexclude;
2248 int i, err;
2249
2250 if (!idev)
2251 return -ENODEV;
2252 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002253 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2255 break;
2256 }
2257 if (!pmc) {
2258 /* MCA not found?? bug */
2259 read_unlock_bh(&idev->lock);
2260 return -ESRCH;
2261 }
2262 spin_lock_bh(&pmc->mca_lock);
2263
2264 sf_markstate(pmc);
2265 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2266 if (!delta)
2267 pmc->mca_sfcount[sfmode]++;
2268 err = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01002269 for (i = 0; i < sfcount; i++) {
Jun Zhao99d2f472011-11-30 06:21:05 +00002270 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 if (err)
2272 break;
2273 }
2274 if (err) {
2275 int j;
2276
2277 if (!delta)
2278 pmc->mca_sfcount[sfmode]--;
Ian Morris67ba4152014-08-24 21:53:10 +01002279 for (j = 0; j < i; j++)
RongQing.Li78d50212012-04-04 16:47:04 +00002280 ip6_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 struct ip6_sf_list *psf;
2283
2284 /* filter mode change */
2285 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2286 pmc->mca_sfmode = MCAST_EXCLUDE;
2287 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2288 pmc->mca_sfmode = MCAST_INCLUDE;
2289 /* else no filters; keep old mode for reports */
2290
2291 pmc->mca_crcount = idev->mc_qrv;
2292 idev->mc_ifc_count = pmc->mca_crcount;
Ian Morris67ba4152014-08-24 21:53:10 +01002293 for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 psf->sf_crcount = 0;
2295 mld_ifc_event(idev);
2296 } else if (sf_setstate(pmc))
2297 mld_ifc_event(idev);
2298 spin_unlock_bh(&pmc->mca_lock);
2299 read_unlock_bh(&idev->lock);
2300 return err;
2301}
2302
2303static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2304{
2305 struct ip6_sf_list *psf, *nextpsf;
2306
Ian Morris67ba4152014-08-24 21:53:10 +01002307 for (psf = pmc->mca_tomb; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 nextpsf = psf->sf_next;
2309 kfree(psf);
2310 }
2311 pmc->mca_tomb = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01002312 for (psf = pmc->mca_sources; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 nextpsf = psf->sf_next;
2314 kfree(psf);
2315 }
2316 pmc->mca_sources = NULL;
2317 pmc->mca_sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002318 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2320}
2321
2322
2323static void igmp6_join_group(struct ifmcaddr6 *ma)
2324{
2325 unsigned long delay;
2326
2327 if (ma->mca_flags & MAF_NOREPORT)
2328 return;
2329
2330 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2331
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05002332 delay = prandom_u32() % unsolicited_report_interval(ma->idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334 spin_lock_bh(&ma->mca_lock);
2335 if (del_timer(&ma->mca_timer)) {
2336 atomic_dec(&ma->mca_refcnt);
2337 delay = ma->mca_timer.expires - jiffies;
2338 }
2339
2340 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2341 atomic_inc(&ma->mca_refcnt);
2342 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2343 spin_unlock_bh(&ma->mca_lock);
2344}
2345
2346static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2347 struct inet6_dev *idev)
2348{
2349 int err;
2350
WANG Congf7ed9252014-09-11 15:35:15 -07002351 /* callers have the socket lock and rtnl lock
David L Stevens5ab4a6c2005-12-27 14:03:00 -08002352 * so no other readers or writers of iml or its sflist
2353 */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002354 if (!iml->sflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 /* any-source empty exclude case */
2356 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2357 }
2358 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2359 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2360 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2361 iml->sflist = NULL;
2362 return err;
2363}
2364
2365static void igmp6_leave_group(struct ifmcaddr6 *ma)
2366{
Daniel Borkmann6c567b72013-09-04 00:19:38 +02002367 if (mld_in_v1_mode(ma->idev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (ma->mca_flags & MAF_LAST_REPORTER)
2369 igmp6_send(&ma->mca_addr, ma->idev->dev,
2370 ICMPV6_MGM_REDUCTION);
2371 } else {
2372 mld_add_delrec(ma->idev, ma);
2373 mld_ifc_event(ma->idev);
2374 }
2375}
2376
2377static void mld_gq_timer_expire(unsigned long data)
2378{
2379 struct inet6_dev *idev = (struct inet6_dev *)data;
2380
2381 idev->mc_gq_running = 0;
2382 mld_send_report(idev, NULL);
Salam Noureddine9260d3e2013-09-29 13:41:34 -07002383 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384}
2385
2386static void mld_ifc_timer_expire(unsigned long data)
2387{
2388 struct inet6_dev *idev = (struct inet6_dev *)data;
2389
2390 mld_send_cr(idev);
2391 if (idev->mc_ifc_count) {
2392 idev->mc_ifc_count--;
2393 if (idev->mc_ifc_count)
2394 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2395 }
Salam Noureddine9260d3e2013-09-29 13:41:34 -07002396 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
2399static void mld_ifc_event(struct inet6_dev *idev)
2400{
Daniel Borkmann6c567b72013-09-04 00:19:38 +02002401 if (mld_in_v1_mode(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return;
2403 idev->mc_ifc_count = idev->mc_qrv;
2404 mld_ifc_start_timer(idev, 1);
2405}
2406
2407
2408static void igmp6_timer_handler(unsigned long data)
2409{
2410 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2411
Daniel Borkmann6c567b72013-09-04 00:19:38 +02002412 if (mld_in_v1_mode(ma->idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2414 else
2415 mld_send_report(ma->idev, ma);
2416
2417 spin_lock(&ma->mca_lock);
2418 ma->mca_flags |= MAF_LAST_REPORTER;
2419 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2420 spin_unlock(&ma->mca_lock);
2421 ma_put(ma);
2422}
2423
Moni Shoua75c78502009-09-15 02:37:40 -07002424/* Device changing type */
2425
2426void ipv6_mc_unmap(struct inet6_dev *idev)
2427{
2428 struct ifmcaddr6 *i;
2429
2430 /* Install multicast list, except for all-nodes (already installed) */
2431
2432 read_lock_bh(&idev->lock);
2433 for (i = idev->mc_list; i; i = i->next)
2434 igmp6_group_dropped(i);
2435 read_unlock_bh(&idev->lock);
2436}
2437
2438void ipv6_mc_remap(struct inet6_dev *idev)
2439{
2440 ipv6_mc_up(idev);
2441}
2442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443/* Device going down */
2444
2445void ipv6_mc_down(struct inet6_dev *idev)
2446{
2447 struct ifmcaddr6 *i;
2448
2449 /* Withdraw multicast list */
2450
2451 read_lock_bh(&idev->lock);
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02002452 mld_ifc_stop_timer(idev);
2453 mld_gq_stop_timer(idev);
2454 mld_dad_stop_timer(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Ian Morris67ba4152014-08-24 21:53:10 +01002456 for (i = idev->mc_list; i; i = i->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 igmp6_group_dropped(i);
2458 read_unlock_bh(&idev->lock);
2459
2460 mld_clear_delrec(idev);
2461}
2462
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02002463static void ipv6_mc_reset(struct inet6_dev *idev)
2464{
2465 idev->mc_qrv = sysctl_mld_qrv;
2466 idev->mc_qi = MLD_QI_DEFAULT;
2467 idev->mc_qri = MLD_QRI_DEFAULT;
2468 idev->mc_v1_seen = 0;
2469 idev->mc_maxdelay = unsolicited_report_interval(idev);
2470}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472/* Device going up */
2473
2474void ipv6_mc_up(struct inet6_dev *idev)
2475{
2476 struct ifmcaddr6 *i;
2477
2478 /* Install multicast list, except for all-nodes (already installed) */
2479
2480 read_lock_bh(&idev->lock);
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02002481 ipv6_mc_reset(idev);
Ian Morris67ba4152014-08-24 21:53:10 +01002482 for (i = idev->mc_list; i; i = i->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 igmp6_group_added(i);
2484 read_unlock_bh(&idev->lock);
2485}
2486
2487/* IPv6 device initialization. */
2488
2489void ipv6_mc_init_dev(struct inet6_dev *idev)
2490{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 write_lock_bh(&idev->lock);
Stephen Hemminger6457d262010-02-17 18:48:44 -08002492 spin_lock_init(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 idev->mc_gq_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002494 setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2495 (unsigned long)idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 idev->mc_tomb = NULL;
2497 idev->mc_ifc_count = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002498 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2499 (unsigned long)idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002500 setup_timer(&idev->mc_dad_timer, mld_dad_timer_expire,
2501 (unsigned long)idev);
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02002502 ipv6_mc_reset(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
2505
2506/*
2507 * Device is about to be destroyed: clean up.
2508 */
2509
2510void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2511{
2512 struct ifmcaddr6 *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
2514 /* Deactivate timers */
2515 ipv6_mc_down(idev);
2516
2517 /* Delete all-nodes address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2519 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2520 * fail.
2521 */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002522 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002524 if (idev->cnf.forwarding)
2525 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527 write_lock_bh(&idev->lock);
2528 while ((i = idev->mc_list) != NULL) {
2529 idev->mc_list = i->next;
2530 write_unlock_bh(&idev->lock);
2531
2532 igmp6_group_dropped(i);
2533 ma_put(i);
2534
2535 write_lock_bh(&idev->lock);
2536 }
2537 write_unlock_bh(&idev->lock);
2538}
2539
2540#ifdef CONFIG_PROC_FS
2541struct igmp6_mc_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002542 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 struct net_device *dev;
2544 struct inet6_dev *idev;
2545};
2546
2547#define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2548
2549static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2550{
2551 struct ifmcaddr6 *im = NULL;
2552 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002553 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Pavel Emelianov7562f872007-05-03 15:13:45 -07002555 state->idev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002556 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 struct inet6_dev *idev;
Eric Dumazetce81b762009-11-11 17:34:30 +00002558 idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 if (!idev)
2560 continue;
2561 read_lock_bh(&idev->lock);
2562 im = idev->mc_list;
2563 if (im) {
2564 state->idev = idev;
2565 break;
2566 }
2567 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 }
2569 return im;
2570}
2571
2572static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2573{
2574 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2575
2576 im = im->next;
2577 while (!im) {
Eric Dumazetce81b762009-11-11 17:34:30 +00002578 if (likely(state->idev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 read_unlock_bh(&state->idev->lock);
Eric Dumazetce81b762009-11-11 17:34:30 +00002580
2581 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 if (!state->dev) {
2583 state->idev = NULL;
2584 break;
2585 }
Eric Dumazetce81b762009-11-11 17:34:30 +00002586 state->idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 if (!state->idev)
2588 continue;
2589 read_lock_bh(&state->idev->lock);
2590 im = state->idev->mc_list;
2591 }
2592 return im;
2593}
2594
2595static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2596{
2597 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2598 if (im)
2599 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2600 --pos;
2601 return pos ? NULL : im;
2602}
2603
2604static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetce81b762009-11-11 17:34:30 +00002605 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
Eric Dumazetce81b762009-11-11 17:34:30 +00002607 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 return igmp6_mc_get_idx(seq, *pos);
2609}
2610
2611static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2612{
Eric Dumazetce81b762009-11-11 17:34:30 +00002613 struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);
2614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 ++*pos;
2616 return im;
2617}
2618
2619static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetce81b762009-11-11 17:34:30 +00002620 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621{
2622 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
Eric Dumazetce81b762009-11-11 17:34:30 +00002623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 if (likely(state->idev != NULL)) {
2625 read_unlock_bh(&state->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 state->idev = NULL;
2627 }
2628 state->dev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002629 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
2631
2632static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2633{
2634 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2635 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2636
2637 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002638 "%-4d %-15s %pi6 %5d %08X %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 state->dev->ifindex, state->dev->name,
Harvey Harrisonb0711952008-10-28 16:05:40 -07002640 &im->mca_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 im->mca_users, im->mca_flags,
2642 (im->mca_flags&MAF_TIMER_RUNNING) ?
2643 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2644 return 0;
2645}
2646
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002647static const struct seq_operations igmp6_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 .start = igmp6_mc_seq_start,
2649 .next = igmp6_mc_seq_next,
2650 .stop = igmp6_mc_seq_stop,
2651 .show = igmp6_mc_seq_show,
2652};
2653
2654static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2655{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002656 return seq_open_net(inode, file, &igmp6_mc_seq_ops,
2657 sizeof(struct igmp6_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
2659
Arjan van de Ven9a321442007-02-12 00:55:35 -08002660static const struct file_operations igmp6_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 .owner = THIS_MODULE,
2662 .open = igmp6_mc_seq_open,
2663 .read = seq_read,
2664 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002665 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666};
2667
2668struct igmp6_mcf_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002669 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 struct net_device *dev;
2671 struct inet6_dev *idev;
2672 struct ifmcaddr6 *im;
2673};
2674
2675#define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2676
2677static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2678{
2679 struct ip6_sf_list *psf = NULL;
2680 struct ifmcaddr6 *im = NULL;
2681 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002682 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Pavel Emelianov7562f872007-05-03 15:13:45 -07002684 state->idev = NULL;
2685 state->im = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002686 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 struct inet6_dev *idev;
Eric Dumazetce81b762009-11-11 17:34:30 +00002688 idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 if (unlikely(idev == NULL))
2690 continue;
2691 read_lock_bh(&idev->lock);
2692 im = idev->mc_list;
2693 if (likely(im != NULL)) {
2694 spin_lock_bh(&im->mca_lock);
2695 psf = im->mca_sources;
2696 if (likely(psf != NULL)) {
2697 state->im = im;
2698 state->idev = idev;
2699 break;
2700 }
2701 spin_unlock_bh(&im->mca_lock);
2702 }
2703 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 }
2705 return psf;
2706}
2707
2708static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2709{
2710 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2711
2712 psf = psf->sf_next;
2713 while (!psf) {
2714 spin_unlock_bh(&state->im->mca_lock);
2715 state->im = state->im->next;
2716 while (!state->im) {
Eric Dumazetce81b762009-11-11 17:34:30 +00002717 if (likely(state->idev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 read_unlock_bh(&state->idev->lock);
Eric Dumazetce81b762009-11-11 17:34:30 +00002719
2720 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 if (!state->dev) {
2722 state->idev = NULL;
2723 goto out;
2724 }
Eric Dumazetce81b762009-11-11 17:34:30 +00002725 state->idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 if (!state->idev)
2727 continue;
2728 read_lock_bh(&state->idev->lock);
2729 state->im = state->idev->mc_list;
2730 }
2731 if (!state->im)
2732 break;
2733 spin_lock_bh(&state->im->mca_lock);
2734 psf = state->im->mca_sources;
2735 }
2736out:
2737 return psf;
2738}
2739
2740static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2741{
2742 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2743 if (psf)
2744 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2745 --pos;
2746 return pos ? NULL : psf;
2747}
2748
2749static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetce81b762009-11-11 17:34:30 +00002750 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751{
Eric Dumazetce81b762009-11-11 17:34:30 +00002752 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2754}
2755
2756static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2757{
2758 struct ip6_sf_list *psf;
2759 if (v == SEQ_START_TOKEN)
2760 psf = igmp6_mcf_get_first(seq);
2761 else
2762 psf = igmp6_mcf_get_next(seq, v);
2763 ++*pos;
2764 return psf;
2765}
2766
2767static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetce81b762009-11-11 17:34:30 +00002768 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769{
2770 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2771 if (likely(state->im != NULL)) {
2772 spin_unlock_bh(&state->im->mca_lock);
2773 state->im = NULL;
2774 }
2775 if (likely(state->idev != NULL)) {
2776 read_unlock_bh(&state->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 state->idev = NULL;
2778 }
2779 state->dev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002780 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781}
2782
2783static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2784{
2785 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2786 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2787
2788 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002789 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 "%3s %6s "
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002791 "%32s %32s %6s %6s\n", "Idx",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 "Device", "Multicast Address",
2793 "Source Address", "INC", "EXC");
2794 } else {
2795 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002796 "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 state->dev->ifindex, state->dev->name,
Harvey Harrisonb0711952008-10-28 16:05:40 -07002798 &state->im->mca_addr,
2799 &psf->sf_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 psf->sf_count[MCAST_INCLUDE],
2801 psf->sf_count[MCAST_EXCLUDE]);
2802 }
2803 return 0;
2804}
2805
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002806static const struct seq_operations igmp6_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 .start = igmp6_mcf_seq_start,
2808 .next = igmp6_mcf_seq_next,
2809 .stop = igmp6_mcf_seq_stop,
2810 .show = igmp6_mcf_seq_show,
2811};
2812
2813static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2814{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002815 return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
2816 sizeof(struct igmp6_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817}
2818
Arjan van de Ven9a321442007-02-12 00:55:35 -08002819static const struct file_operations igmp6_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 .owner = THIS_MODULE,
2821 .open = igmp6_mcf_seq_open,
2822 .read = seq_read,
2823 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002824 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825};
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002826
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002827static int __net_init igmp6_proc_init(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002828{
2829 int err;
2830
2831 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002832 if (!proc_create("igmp6", S_IRUGO, net->proc_net, &igmp6_mc_seq_fops))
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002833 goto out;
Gao fengd4beaa62013-02-18 01:34:54 +00002834 if (!proc_create("mcfilter6", S_IRUGO, net->proc_net,
2835 &igmp6_mcf_seq_fops))
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002836 goto out_proc_net_igmp6;
2837
2838 err = 0;
2839out:
2840 return err;
2841
2842out_proc_net_igmp6:
Gao fengece31ff2013-02-18 01:34:56 +00002843 remove_proc_entry("igmp6", net->proc_net);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002844 goto out;
2845}
2846
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002847static void __net_exit igmp6_proc_exit(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002848{
Gao fengece31ff2013-02-18 01:34:56 +00002849 remove_proc_entry("mcfilter6", net->proc_net);
2850 remove_proc_entry("igmp6", net->proc_net);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002851}
2852#else
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002853static inline int igmp6_proc_init(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002854{
2855 return 0;
2856}
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002857static inline void igmp6_proc_exit(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002858{
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002859}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860#endif
2861
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002862static int __net_init igmp6_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 int err;
2865
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002866 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2867 SOCK_RAW, IPPROTO_ICMPV6, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00002869 pr_err("Failed to initialize the IGMP6 control socket (err %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 err);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002871 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 }
2873
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002874 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002876 err = igmp6_proc_init(net);
2877 if (err)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002878 goto out_sock_create;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002879out:
2880 return err;
2881
2882out_sock_create:
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002883 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002884 goto out;
2885}
2886
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002887static void __net_exit igmp6_net_exit(struct net *net)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002888{
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002889 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002890 igmp6_proc_exit(net);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002891}
2892
2893static struct pernet_operations igmp6_net_ops = {
2894 .init = igmp6_net_init,
2895 .exit = igmp6_net_exit,
2896};
2897
2898int __init igmp6_init(void)
2899{
2900 return register_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901}
2902
2903void igmp6_cleanup(void)
2904{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002905 unregister_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906}