blob: 4fb761d4002d1279205bb8a152bd8efd7b8728b4 [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;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800559 /*
560 * changes to the ipv6_mc_list require the socket lock and
561 * a read lock on ip6_sk_mc_lock. We have the socket lock,
562 * so reading the list is safe.
563 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Eric Dumazet456b61b2010-11-23 13:12:15 +0000565 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (pmc->ifindex != gsf->gf_interface)
567 continue;
568 if (ipv6_addr_equal(group, &pmc->addr))
569 break;
570 }
571 if (!pmc) /* must have a prior join */
572 goto done;
573 gsf->gf_fmode = pmc->sfmode;
574 psl = pmc->sflist;
575 count = psl ? psl->sl_count : 0;
576 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000577 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
580 gsf->gf_numsrc = count;
581 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
582 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
583 return -EFAULT;
584 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800585 /* changes to psl require the socket lock, a read lock on
586 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
587 * have the socket lock, so reading here is safe.
588 */
Ian Morris67ba4152014-08-24 21:53:10 +0100589 for (i = 0; i < copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 struct sockaddr_in6 *psin6;
591 struct sockaddr_storage ss;
592
593 psin6 = (struct sockaddr_in6 *)&ss;
594 memset(&ss, 0, sizeof(ss));
595 psin6->sin6_family = AF_INET6;
596 psin6->sin6_addr = psl->sl_addr[i];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900597 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return -EFAULT;
599 }
600 return 0;
601done:
602 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000603 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return err;
605}
606
Eric Dumazeta50feda2012-05-18 18:57:34 +0000607bool inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
608 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 struct ipv6_pinfo *np = inet6_sk(sk);
611 struct ipv6_mc_socklist *mc;
612 struct ip6_sf_socklist *psl;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000613 bool rv = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Eric Dumazet456b61b2010-11-23 13:12:15 +0000615 rcu_read_lock();
616 for_each_pmc_rcu(np, mc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (ipv6_addr_equal(&mc->addr, mc_addr))
618 break;
619 }
620 if (!mc) {
Eric Dumazet456b61b2010-11-23 13:12:15 +0000621 rcu_read_unlock();
Eric Dumazeta50feda2012-05-18 18:57:34 +0000622 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800624 read_lock(&mc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 psl = mc->sflist;
626 if (!psl) {
627 rv = mc->sfmode == MCAST_EXCLUDE;
628 } else {
629 int i;
630
Ian Morris67ba4152014-08-24 21:53:10 +0100631 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
633 break;
634 }
635 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000636 rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000638 rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800640 read_unlock(&mc->sflock);
Eric Dumazet456b61b2010-11-23 13:12:15 +0000641 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 return rv;
644}
645
646static void ma_put(struct ifmcaddr6 *mc)
647{
648 if (atomic_dec_and_test(&mc->mca_refcnt)) {
649 in6_dev_put(mc->idev);
650 kfree(mc);
651 }
652}
653
654static void igmp6_group_added(struct ifmcaddr6 *mc)
655{
656 struct net_device *dev = mc->idev->dev;
657 char buf[MAX_ADDR_LEN];
658
YOSHIFUJI Hideaki / 吉藤英明ec16ef22013-02-09 04:29:58 +0000659 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
660 IPV6_ADDR_SCOPE_LINKLOCAL)
661 return;
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 spin_lock_bh(&mc->mca_lock);
664 if (!(mc->mca_flags&MAF_LOADED)) {
665 mc->mca_flags |= MAF_LOADED;
666 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000667 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669 spin_unlock_bh(&mc->mca_lock);
670
671 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
672 return;
673
Daniel Borkmann6c567b72013-09-04 00:19:38 +0200674 if (mld_in_v1_mode(mc->idev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 igmp6_join_group(mc);
676 return;
677 }
678 /* else v2 */
679
680 mc->mca_crcount = mc->idev->mc_qrv;
681 mld_ifc_event(mc->idev);
682}
683
684static void igmp6_group_dropped(struct ifmcaddr6 *mc)
685{
686 struct net_device *dev = mc->idev->dev;
687 char buf[MAX_ADDR_LEN];
688
YOSHIFUJI Hideaki / 吉藤英明ec16ef22013-02-09 04:29:58 +0000689 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
690 IPV6_ADDR_SCOPE_LINKLOCAL)
691 return;
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 spin_lock_bh(&mc->mca_lock);
694 if (mc->mca_flags&MAF_LOADED) {
695 mc->mca_flags &= ~MAF_LOADED;
696 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000697 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699
700 if (mc->mca_flags & MAF_NOREPORT)
701 goto done;
702 spin_unlock_bh(&mc->mca_lock);
703
704 if (!mc->idev->dead)
705 igmp6_leave_group(mc);
706
707 spin_lock_bh(&mc->mca_lock);
708 if (del_timer(&mc->mca_timer))
709 atomic_dec(&mc->mca_refcnt);
710done:
711 ip6_mc_clear_src(mc);
712 spin_unlock_bh(&mc->mca_lock);
713}
714
715/*
716 * deleted ifmcaddr6 manipulation
717 */
718static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
719{
720 struct ifmcaddr6 *pmc;
721
722 /* this is an "ifmcaddr6" for convenience; only the fields below
723 * are actually used. In particular, the refcnt and users are not
724 * used for management of the delete list. Using the same structure
725 * for deleted items allows change reports to use common code with
726 * non-deleted or query-response MCA's.
727 */
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800728 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (!pmc)
730 return;
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 spin_lock_bh(&im->mca_lock);
733 spin_lock_init(&pmc->mca_lock);
734 pmc->idev = im->idev;
735 in6_dev_hold(idev);
736 pmc->mca_addr = im->mca_addr;
737 pmc->mca_crcount = idev->mc_qrv;
738 pmc->mca_sfmode = im->mca_sfmode;
739 if (pmc->mca_sfmode == MCAST_INCLUDE) {
740 struct ip6_sf_list *psf;
741
742 pmc->mca_tomb = im->mca_tomb;
743 pmc->mca_sources = im->mca_sources;
744 im->mca_tomb = im->mca_sources = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +0100745 for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 psf->sf_crcount = pmc->mca_crcount;
747 }
748 spin_unlock_bh(&im->mca_lock);
749
Stephen Hemminger6457d262010-02-17 18:48:44 -0800750 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 pmc->next = idev->mc_tomb;
752 idev->mc_tomb = pmc;
Stephen Hemminger6457d262010-02-17 18:48:44 -0800753 spin_unlock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000756static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *pmca)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 struct ifmcaddr6 *pmc, *pmc_prev;
759 struct ip6_sf_list *psf, *psf_next;
760
Stephen Hemminger6457d262010-02-17 18:48:44 -0800761 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 pmc_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +0100763 for (pmc = idev->mc_tomb; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
765 break;
766 pmc_prev = pmc;
767 }
768 if (pmc) {
769 if (pmc_prev)
770 pmc_prev->next = pmc->next;
771 else
772 idev->mc_tomb = pmc->next;
773 }
Stephen Hemminger6457d262010-02-17 18:48:44 -0800774 spin_unlock_bh(&idev->mc_lock);
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (pmc) {
Ian Morris67ba4152014-08-24 21:53:10 +0100777 for (psf = pmc->mca_tomb; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 psf_next = psf->sf_next;
779 kfree(psf);
780 }
781 in6_dev_put(pmc->idev);
782 kfree(pmc);
783 }
784}
785
786static void mld_clear_delrec(struct inet6_dev *idev)
787{
788 struct ifmcaddr6 *pmc, *nextpmc;
789
Stephen Hemminger6457d262010-02-17 18:48:44 -0800790 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 pmc = idev->mc_tomb;
792 idev->mc_tomb = NULL;
Stephen Hemminger6457d262010-02-17 18:48:44 -0800793 spin_unlock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 for (; pmc; pmc = nextpmc) {
796 nextpmc = pmc->next;
797 ip6_mc_clear_src(pmc);
798 in6_dev_put(pmc->idev);
799 kfree(pmc);
800 }
801
802 /* clear dead sources, too */
803 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100804 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 struct ip6_sf_list *psf, *psf_next;
806
807 spin_lock_bh(&pmc->mca_lock);
808 psf = pmc->mca_tomb;
809 pmc->mca_tomb = NULL;
810 spin_unlock_bh(&pmc->mca_lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100811 for (; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 psf_next = psf->sf_next;
813 kfree(psf);
814 }
815 }
816 read_unlock_bh(&idev->lock);
817}
818
819
820/*
821 * device multicast group inc (add if not found)
822 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900823int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 struct ifmcaddr6 *mc;
826 struct inet6_dev *idev;
827
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200828 ASSERT_RTNL();
829
Eric Dumazet96b52e62010-06-07 21:05:02 +0000830 /* we need to take a reference on idev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 idev = in6_dev_get(dev);
832
833 if (idev == NULL)
834 return -EINVAL;
835
836 write_lock_bh(&idev->lock);
837 if (idev->dead) {
838 write_unlock_bh(&idev->lock);
839 in6_dev_put(idev);
840 return -ENODEV;
841 }
842
843 for (mc = idev->mc_list; mc; mc = mc->next) {
844 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
845 mc->mca_users++;
846 write_unlock_bh(&idev->lock);
847 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
848 NULL, 0);
849 in6_dev_put(idev);
850 return 0;
851 }
852 }
853
854 /*
855 * not found: create a new one.
856 */
857
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800858 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 if (mc == NULL) {
861 write_unlock_bh(&idev->lock);
862 in6_dev_put(idev);
863 return -ENOMEM;
864 }
865
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800866 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000868 mc->mca_addr = *addr;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000869 mc->idev = idev; /* (reference taken) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 mc->mca_users = 1;
871 /* mca_stamp should be updated upon changes */
872 mc->mca_cstamp = mc->mca_tstamp = jiffies;
873 atomic_set(&mc->mca_refcnt, 2);
874 spin_lock_init(&mc->mca_lock);
875
876 /* initial mode is (EX, empty) */
877 mc->mca_sfmode = MCAST_EXCLUDE;
878 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
879
880 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
881 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
882 mc->mca_flags |= MAF_NOREPORT;
883
884 mc->next = idev->mc_list;
885 idev->mc_list = mc;
886 write_unlock_bh(&idev->lock);
887
888 mld_del_delrec(idev, &mc->mca_addr);
889 igmp6_group_added(mc);
890 ma_put(mc);
891 return 0;
892}
893
894/*
895 * device multicast group del
896 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900897int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
899 struct ifmcaddr6 *ma, **map;
900
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200901 ASSERT_RTNL();
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 write_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100904 for (map = &idev->mc_list; (ma = *map) != NULL; map = &ma->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
906 if (--ma->mca_users == 0) {
907 *map = ma->next;
908 write_unlock_bh(&idev->lock);
909
910 igmp6_group_dropped(ma);
911
912 ma_put(ma);
913 return 0;
914 }
915 write_unlock_bh(&idev->lock);
916 return 0;
917 }
918 }
919 write_unlock_bh(&idev->lock);
920
921 return -ENOENT;
922}
923
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900924int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Eric Dumazet96b52e62010-06-07 21:05:02 +0000926 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 int err;
928
WANG Cong414b6c92014-09-11 15:35:14 -0700929 ASSERT_RTNL();
Eric Dumazet96b52e62010-06-07 21:05:02 +0000930
931 idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (!idev)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000933 err = -ENODEV;
934 else
935 err = __ipv6_dev_mc_dec(idev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return err;
938}
939
940/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 * check if the interface/address pair is valid
942 */
Eric Dumazeta50feda2012-05-18 18:57:34 +0000943bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
944 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
946 struct inet6_dev *idev;
947 struct ifmcaddr6 *mc;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000948 bool rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Eric Dumazet96b52e62010-06-07 21:05:02 +0000950 rcu_read_lock();
951 idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (idev) {
953 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100954 for (mc = idev->mc_list; mc; mc = mc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (ipv6_addr_equal(&mc->mca_addr, group))
956 break;
957 }
958 if (mc) {
959 if (src_addr && !ipv6_addr_any(src_addr)) {
960 struct ip6_sf_list *psf;
961
962 spin_lock_bh(&mc->mca_lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100963 for (psf = mc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
965 break;
966 }
967 if (psf)
968 rv = psf->sf_count[MCAST_INCLUDE] ||
969 psf->sf_count[MCAST_EXCLUDE] !=
970 mc->mca_sfcount[MCAST_EXCLUDE];
971 else
Ian Morris67ba4152014-08-24 21:53:10 +0100972 rv = mc->mca_sfcount[MCAST_EXCLUDE] != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 spin_unlock_bh(&mc->mca_lock);
974 } else
Eric Dumazeta50feda2012-05-18 18:57:34 +0000975 rv = true; /* don't filter unspecified source */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
Eric Dumazet96b52e62010-06-07 21:05:02 +0000979 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return rv;
981}
982
983static void mld_gq_start_timer(struct inet6_dev *idev)
984{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500985 unsigned long tv = prandom_u32() % idev->mc_maxdelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 idev->mc_gq_running = 1;
988 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
989 in6_dev_hold(idev);
990}
991
Daniel Borkmannb4af8de2013-09-04 00:19:43 +0200992static void mld_gq_stop_timer(struct inet6_dev *idev)
993{
994 idev->mc_gq_running = 0;
995 if (del_timer(&idev->mc_gq_timer))
996 __in6_dev_put(idev);
997}
998
Daniel Borkmannc2cef4e2013-08-20 12:22:01 +0200999static void mld_ifc_start_timer(struct inet6_dev *idev, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001001 unsigned long tv = prandom_u32() % delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1004 in6_dev_hold(idev);
1005}
1006
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001007static void mld_ifc_stop_timer(struct inet6_dev *idev)
1008{
1009 idev->mc_ifc_count = 0;
1010 if (del_timer(&idev->mc_ifc_timer))
1011 __in6_dev_put(idev);
1012}
1013
Daniel Borkmannc2cef4e2013-08-20 12:22:01 +02001014static void mld_dad_start_timer(struct inet6_dev *idev, unsigned long delay)
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02001015{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001016 unsigned long tv = prandom_u32() % delay;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02001017
1018 if (!mod_timer(&idev->mc_dad_timer, jiffies+tv+2))
1019 in6_dev_hold(idev);
1020}
1021
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001022static void mld_dad_stop_timer(struct inet6_dev *idev)
1023{
1024 if (del_timer(&idev->mc_dad_timer))
1025 __in6_dev_put(idev);
1026}
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028/*
1029 * IGMP handling (alias multicast ICMPv6 messages)
1030 */
1031
1032static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1033{
1034 unsigned long delay = resptime;
1035
1036 /* Do not start timer for these addresses */
1037 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1038 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1039 return;
1040
1041 if (del_timer(&ma->mca_timer)) {
1042 atomic_dec(&ma->mca_refcnt);
1043 delay = ma->mca_timer.expires - jiffies;
1044 }
1045
Daniel Borkmanncc7f7ab2013-09-04 00:19:41 +02001046 if (delay >= resptime)
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001047 delay = prandom_u32() % resptime;
Daniel Borkmanncc7f7ab2013-09-04 00:19:41 +02001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 ma->mca_timer.expires = jiffies + delay;
1050 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1051 atomic_inc(&ma->mca_refcnt);
1052 ma->mca_flags |= MAF_TIMER_RUNNING;
1053}
1054
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001055/* mark EXCLUDE-mode sources */
Eric Dumazeta50feda2012-05-18 18:57:34 +00001056static bool mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1057 const struct in6_addr *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
1059 struct ip6_sf_list *psf;
1060 int i, scount;
1061
1062 scount = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01001063 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (scount == nsrcs)
1065 break;
Ian Morris67ba4152014-08-24 21:53:10 +01001066 for (i = 0; i < nsrcs; i++) {
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001067 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +00001068 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001069 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1070 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +08001071 break;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001072 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1073 scount++;
1074 break;
1075 }
1076 }
1077 }
1078 pmc->mca_flags &= ~MAF_GSQUERY;
1079 if (scount == nsrcs) /* all sources excluded */
Eric Dumazeta50feda2012-05-18 18:57:34 +00001080 return false;
1081 return true;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001082}
1083
Eric Dumazeta50feda2012-05-18 18:57:34 +00001084static bool mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1085 const struct in6_addr *srcs)
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001086{
1087 struct ip6_sf_list *psf;
1088 int i, scount;
1089
1090 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1091 return mld_xmarksources(pmc, nsrcs, srcs);
1092
1093 /* mark INCLUDE-mode sources */
1094
1095 scount = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01001096 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001097 if (scount == nsrcs)
1098 break;
Ian Morris67ba4152014-08-24 21:53:10 +01001099 for (i = 0; i < nsrcs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1101 psf->sf_gsresp = 1;
1102 scount++;
1103 break;
1104 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001107 if (!scount) {
1108 pmc->mca_flags &= ~MAF_GSQUERY;
Eric Dumazeta50feda2012-05-18 18:57:34 +00001109 return false;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001110 }
1111 pmc->mca_flags |= MAF_GSQUERY;
Eric Dumazeta50feda2012-05-18 18:57:34 +00001112 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
Daniel Borkmann58c0ecf2013-09-04 00:19:40 +02001115static int mld_force_mld_version(const struct inet6_dev *idev)
1116{
1117 /* Normally, both are 0 here. If enforcement to a particular is
1118 * being used, individual device enforcement will have a lower
1119 * precedence over 'all' device (.../conf/all/force_mld_version).
1120 */
1121
1122 if (dev_net(idev->dev)->ipv6.devconf_all->force_mld_version != 0)
1123 return dev_net(idev->dev)->ipv6.devconf_all->force_mld_version;
1124 else
1125 return idev->cnf.force_mld_version;
1126}
1127
1128static bool mld_in_v2_mode_only(const struct inet6_dev *idev)
1129{
1130 return mld_force_mld_version(idev) == 2;
1131}
1132
1133static bool mld_in_v1_mode_only(const struct inet6_dev *idev)
1134{
1135 return mld_force_mld_version(idev) == 1;
1136}
1137
Daniel Borkmann6c567b72013-09-04 00:19:38 +02001138static bool mld_in_v1_mode(const struct inet6_dev *idev)
1139{
Daniel Borkmann58c0ecf2013-09-04 00:19:40 +02001140 if (mld_in_v2_mode_only(idev))
1141 return false;
1142 if (mld_in_v1_mode_only(idev))
Daniel Borkmann6c567b72013-09-04 00:19:38 +02001143 return true;
1144 if (idev->mc_v1_seen && time_before(jiffies, idev->mc_v1_seen))
1145 return true;
1146
1147 return false;
1148}
1149
Daniel Borkmann89225d12013-09-04 00:19:37 +02001150static void mld_set_v1_mode(struct inet6_dev *idev)
1151{
1152 /* RFC3810, relevant sections:
1153 * - 9.1. Robustness Variable
1154 * - 9.2. Query Interval
1155 * - 9.3. Query Response Interval
1156 * - 9.12. Older Version Querier Present Timeout
1157 */
1158 unsigned long switchback;
1159
1160 switchback = (idev->mc_qrv * idev->mc_qi) + idev->mc_qri;
1161
1162 idev->mc_v1_seen = jiffies + switchback;
1163}
1164
1165static void mld_update_qrv(struct inet6_dev *idev,
1166 const struct mld2_query *mlh2)
1167{
1168 /* RFC3810, relevant sections:
1169 * - 5.1.8. QRV (Querier's Robustness Variable)
1170 * - 9.1. Robustness Variable
1171 */
1172
1173 /* The value of the Robustness Variable MUST NOT be zero,
1174 * and SHOULD NOT be one. Catch this here if we ever run
1175 * into such a case in future.
1176 */
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02001177 const int min_qrv = min(MLD_QRV_DEFAULT, sysctl_mld_qrv);
Daniel Borkmann89225d12013-09-04 00:19:37 +02001178 WARN_ON(idev->mc_qrv == 0);
1179
1180 if (mlh2->mld2q_qrv > 0)
1181 idev->mc_qrv = mlh2->mld2q_qrv;
1182
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02001183 if (unlikely(idev->mc_qrv < min_qrv)) {
Daniel Borkmann89225d12013-09-04 00:19:37 +02001184 net_warn_ratelimited("IPv6: MLD: clamping QRV from %u to %u!\n",
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02001185 idev->mc_qrv, min_qrv);
1186 idev->mc_qrv = min_qrv;
Daniel Borkmann89225d12013-09-04 00:19:37 +02001187 }
1188}
1189
1190static void mld_update_qi(struct inet6_dev *idev,
1191 const struct mld2_query *mlh2)
1192{
1193 /* RFC3810, relevant sections:
1194 * - 5.1.9. QQIC (Querier's Query Interval Code)
1195 * - 9.2. Query Interval
1196 * - 9.12. Older Version Querier Present Timeout
1197 * (the [Query Interval] in the last Query received)
1198 */
1199 unsigned long mc_qqi;
1200
1201 if (mlh2->mld2q_qqic < 128) {
1202 mc_qqi = mlh2->mld2q_qqic;
1203 } else {
1204 unsigned long mc_man, mc_exp;
1205
1206 mc_exp = MLDV2_QQIC_EXP(mlh2->mld2q_qqic);
1207 mc_man = MLDV2_QQIC_MAN(mlh2->mld2q_qqic);
1208
1209 mc_qqi = (mc_man | 0x10) << (mc_exp + 3);
1210 }
1211
1212 idev->mc_qi = mc_qqi * HZ;
1213}
1214
1215static void mld_update_qri(struct inet6_dev *idev,
1216 const struct mld2_query *mlh2)
1217{
1218 /* RFC3810, relevant sections:
1219 * - 5.1.3. Maximum Response Code
1220 * - 9.3. Query Response Interval
1221 */
Daniel Borkmanne3f5b172013-09-04 00:19:39 +02001222 idev->mc_qri = msecs_to_jiffies(mldv2_mrc(mlh2));
Daniel Borkmann89225d12013-09-04 00:19:37 +02001223}
1224
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001225static int mld_process_v1(struct inet6_dev *idev, struct mld_msg *mld,
1226 unsigned long *max_delay)
1227{
1228 unsigned long mldv1_md;
1229
1230 /* Ignore v1 queries */
1231 if (mld_in_v2_mode_only(idev))
1232 return -EINVAL;
1233
1234 /* MLDv1 router present */
1235 mldv1_md = ntohs(mld->mld_maxdelay);
1236 *max_delay = max(msecs_to_jiffies(mldv1_md), 1UL);
1237
1238 mld_set_v1_mode(idev);
1239
1240 /* cancel MLDv2 report timer */
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001241 mld_gq_stop_timer(idev);
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001242 /* cancel the interface change timer */
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001243 mld_ifc_stop_timer(idev);
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001244 /* clear deleted report items */
1245 mld_clear_delrec(idev);
1246
1247 return 0;
1248}
1249
1250static int mld_process_v2(struct inet6_dev *idev, struct mld2_query *mld,
1251 unsigned long *max_delay)
1252{
1253 /* hosts need to stay in MLDv1 mode, discard MLDv2 queries */
1254 if (mld_in_v1_mode(idev))
1255 return -EINVAL;
1256
1257 *max_delay = max(msecs_to_jiffies(mldv2_mrc(mld)), 1UL);
1258
1259 mld_update_qrv(idev, mld);
1260 mld_update_qi(idev, mld);
1261 mld_update_qri(idev, mld);
1262
1263 idev->mc_maxdelay = *max_delay;
1264
1265 return 0;
1266}
1267
Eric Dumazet96b52e62010-06-07 21:05:02 +00001268/* called with rcu_read_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269int igmp6_event_query(struct sk_buff *skb)
1270{
Yan Zheng97300b52005-10-31 20:09:45 +08001271 struct mld2_query *mlh2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 struct ifmcaddr6 *ma;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001273 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 unsigned long max_delay;
1275 struct inet6_dev *idev;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001276 struct mld_msg *mld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 int group_type;
1278 int mark = 0;
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001279 int len, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1282 return -EINVAL;
1283
1284 /* compute payload length excluding extension headers */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001285 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001286 len -= skb_network_header_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Hangbin Liue940f5d2014-06-27 09:57:53 +08001288 /* RFC3810 6.2
1289 * Upon reception of an MLD message that contains a Query, the node
1290 * checks if the source address of the message is a valid link-local
1291 * address, if the Hop Limit is set to 1, and if the Router Alert
1292 * option is present in the Hop-By-Hop Options header of the IPv6
1293 * packet. If any of these checks fails, the packet is dropped.
1294 */
1295 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL) ||
1296 ipv6_hdr(skb)->hop_limit != 1 ||
1297 !(IP6CB(skb)->flags & IP6SKB_ROUTERALERT) ||
1298 IP6CB(skb)->ra != htons(IPV6_OPT_ROUTERALERT_MLD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return -EINVAL;
1300
Eric Dumazet96b52e62010-06-07 21:05:02 +00001301 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (idev == NULL)
1303 return 0;
1304
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001305 mld = (struct mld_msg *)icmp6_hdr(skb);
1306 group = &mld->mld_mca;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 group_type = ipv6_addr_type(group);
1308
1309 if (group_type != IPV6_ADDR_ANY &&
Eric Dumazet96b52e62010-06-07 21:05:02 +00001310 !(group_type&IPV6_ADDR_MULTICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Daniel Borkmann9fd07842013-08-20 12:22:02 +02001313 if (len == MLD_V1_QUERY_LEN) {
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001314 err = mld_process_v1(idev, mld, &max_delay);
1315 if (err < 0)
1316 return err;
Daniel Borkmann9fd07842013-08-20 12:22:02 +02001317 } else if (len >= MLD_V2_QUERY_LEN_MIN) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001318 int srcs_offset = sizeof(struct mld2_query) -
Yan Zheng97300b52005-10-31 20:09:45 +08001319 sizeof(struct icmp6hdr);
Daniel Borkmann89225d12013-09-04 00:19:37 +02001320
Eric Dumazet96b52e62010-06-07 21:05:02 +00001321 if (!pskb_may_pull(skb, srcs_offset))
Yan Zheng97300b52005-10-31 20:09:45 +08001322 return -EINVAL;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001323
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001324 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Daniel Borkmann84698962013-08-20 12:22:00 +02001325
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001326 err = mld_process_v2(idev, mlh2, &max_delay);
1327 if (err < 0)
1328 return err;
Daniel Borkmann89225d12013-09-04 00:19:37 +02001329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (group_type == IPV6_ADDR_ANY) { /* general query */
Eric Dumazet96b52e62010-06-07 21:05:02 +00001331 if (mlh2->mld2q_nsrcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return -EINVAL; /* no sources allowed */
Eric Dumazet96b52e62010-06-07 21:05:02 +00001333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 mld_gq_start_timer(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return 0;
1336 }
1337 /* mark sources to include, if group & source-specific */
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001338 if (mlh2->mld2q_nsrcs != 0) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001339 if (!pskb_may_pull(skb, srcs_offset +
Eric Dumazet96b52e62010-06-07 21:05:02 +00001340 ntohs(mlh2->mld2q_nsrcs) * sizeof(struct in6_addr)))
Yan Zheng97300b52005-10-31 20:09:45 +08001341 return -EINVAL;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001342
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001343 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Yan Zheng97300b52005-10-31 20:09:45 +08001344 mark = 1;
1345 }
Eric Dumazet96b52e62010-06-07 21:05:02 +00001346 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 read_lock_bh(&idev->lock);
1350 if (group_type == IPV6_ADDR_ANY) {
Ian Morris67ba4152014-08-24 21:53:10 +01001351 for (ma = idev->mc_list; ma; ma = ma->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 spin_lock_bh(&ma->mca_lock);
1353 igmp6_group_queried(ma, max_delay);
1354 spin_unlock_bh(&ma->mca_lock);
1355 }
1356 } else {
Ian Morris67ba4152014-08-24 21:53:10 +01001357 for (ma = idev->mc_list; ma; ma = ma->next) {
David L Stevens7add2a42006-01-24 13:06:39 -08001358 if (!ipv6_addr_equal(group, &ma->mca_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 continue;
1360 spin_lock_bh(&ma->mca_lock);
1361 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1362 /* gsquery <- gsquery && mark */
1363 if (!mark)
1364 ma->mca_flags &= ~MAF_GSQUERY;
1365 } else {
1366 /* gsquery <- mark */
1367 if (mark)
1368 ma->mca_flags |= MAF_GSQUERY;
1369 else
1370 ma->mca_flags &= ~MAF_GSQUERY;
1371 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001372 if (!(ma->mca_flags & MAF_GSQUERY) ||
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001373 mld_marksources(ma, ntohs(mlh2->mld2q_nsrcs), mlh2->mld2q_srcs))
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001374 igmp6_group_queried(ma, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 spin_unlock_bh(&ma->mca_lock);
David L Stevens7add2a42006-01-24 13:06:39 -08001376 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378 }
1379 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 return 0;
1382}
1383
Eric Dumazet96b52e62010-06-07 21:05:02 +00001384/* called with rcu_read_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385int igmp6_event_report(struct sk_buff *skb)
1386{
1387 struct ifmcaddr6 *ma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 struct inet6_dev *idev;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001389 struct mld_msg *mld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 int addr_type;
1391
1392 /* Our own report looped back. Ignore it. */
1393 if (skb->pkt_type == PACKET_LOOPBACK)
1394 return 0;
1395
David Stevens24c69272005-12-02 20:32:59 -08001396 /* send our report if the MC router may not have heard this report */
1397 if (skb->pkt_type != PACKET_MULTICAST &&
1398 skb->pkt_type != PACKET_BROADCAST)
1399 return 0;
1400
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001401 if (!pskb_may_pull(skb, sizeof(*mld) - sizeof(struct icmp6hdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return -EINVAL;
1403
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001404 mld = (struct mld_msg *)icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 /* Drop reports with not link local source */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001407 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001408 if (addr_type != IPV6_ADDR_ANY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 !(addr_type&IPV6_ADDR_LINKLOCAL))
1410 return -EINVAL;
1411
Eric Dumazet96b52e62010-06-07 21:05:02 +00001412 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (idev == NULL)
1414 return -ENODEV;
1415
1416 /*
1417 * Cancel the timer for this group
1418 */
1419
1420 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +01001421 for (ma = idev->mc_list; ma; ma = ma->next) {
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001422 if (ipv6_addr_equal(&ma->mca_addr, &mld->mld_mca)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 spin_lock(&ma->mca_lock);
1424 if (del_timer(&ma->mca_timer))
1425 atomic_dec(&ma->mca_refcnt);
1426 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1427 spin_unlock(&ma->mca_lock);
1428 break;
1429 }
1430 }
1431 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return 0;
1433}
1434
Eric Dumazeta50feda2012-05-18 18:57:34 +00001435static bool is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1436 int gdeleted, int sdeleted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
1438 switch (type) {
1439 case MLD2_MODE_IS_INCLUDE:
1440 case MLD2_MODE_IS_EXCLUDE:
1441 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001442 return false;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001443 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1444 if (pmc->mca_sfmode == MCAST_INCLUDE)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001445 return true;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001446 /* don't include if this source is excluded
1447 * in all filters
1448 */
1449 if (psf->sf_count[MCAST_INCLUDE])
David L Stevens7add2a42006-01-24 13:06:39 -08001450 return type == MLD2_MODE_IS_INCLUDE;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001451 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1452 psf->sf_count[MCAST_EXCLUDE];
1453 }
Eric Dumazeta50feda2012-05-18 18:57:34 +00001454 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 case MLD2_CHANGE_TO_INCLUDE:
1456 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001457 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return psf->sf_count[MCAST_INCLUDE] != 0;
1459 case MLD2_CHANGE_TO_EXCLUDE:
1460 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001461 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1463 psf->sf_count[MCAST_INCLUDE])
Eric Dumazeta50feda2012-05-18 18:57:34 +00001464 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1466 psf->sf_count[MCAST_EXCLUDE];
1467 case MLD2_ALLOW_NEW_SOURCES:
1468 if (gdeleted || !psf->sf_crcount)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001469 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1471 case MLD2_BLOCK_OLD_SOURCES:
1472 if (pmc->mca_sfmode == MCAST_INCLUDE)
1473 return gdeleted || (psf->sf_crcount && sdeleted);
1474 return psf->sf_crcount && !gdeleted && !sdeleted;
1475 }
Eric Dumazeta50feda2012-05-18 18:57:34 +00001476 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
1479static int
1480mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1481{
1482 struct ip6_sf_list *psf;
1483 int scount = 0;
1484
Ian Morris67ba4152014-08-24 21:53:10 +01001485 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1487 continue;
1488 scount++;
1489 }
1490 return scount;
1491}
1492
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +00001493static void ip6_mc_hdr(struct sock *sk, struct sk_buff *skb,
1494 struct net_device *dev,
1495 const struct in6_addr *saddr,
1496 const struct in6_addr *daddr,
1497 int proto, int len)
1498{
1499 struct ipv6hdr *hdr;
1500
1501 skb->protocol = htons(ETH_P_IPV6);
1502 skb->dev = dev;
1503
1504 skb_reset_network_header(skb);
1505 skb_put(skb, sizeof(struct ipv6hdr));
1506 hdr = ipv6_hdr(skb);
1507
1508 ip6_flow_hdr(hdr, 0, 0);
1509
1510 hdr->payload_len = htons(len);
1511 hdr->nexthdr = proto;
1512 hdr->hop_limit = inet6_sk(sk)->hop_limit;
1513
1514 hdr->saddr = *saddr;
1515 hdr->daddr = *daddr;
1516}
1517
Amerigo Wang89657792013-06-29 21:30:49 +08001518static struct sk_buff *mld_newpack(struct inet6_dev *idev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Amerigo Wang89657792013-06-29 21:30:49 +08001520 struct net_device *dev = idev->dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001521 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001522 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 struct sk_buff *skb;
1524 struct mld2_report *pmr;
1525 struct in6_addr addr_buf;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001526 const struct in6_addr *saddr;
Herbert Xua7ae1992011-11-18 02:20:04 +00001527 int hlen = LL_RESERVED_SPACE(dev);
1528 int tlen = dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 int err;
1530 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1531 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1532 IPV6_TLV_PADN, 0 };
1533
1534 /* we assume size > sizeof(ra) here */
Herbert Xua7ae1992011-11-18 02:20:04 +00001535 size += hlen + tlen;
Eric Dumazet72e09ad2010-06-05 03:03:30 -07001536 /* limit our allocations to order-0 page */
1537 size = min_t(int, size, SKB_MAX_ORDER(0, 0));
1538 skb = sock_alloc_send_skb(sk, size, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001540 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return NULL;
1542
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +02001543 skb->priority = TC_PRIO_CONTROL;
Herbert Xua7ae1992011-11-18 02:20:04 +00001544 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Amerigo Wang89657792013-06-29 21:30:49 +08001546 if (__ipv6_get_lladdr(idev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001548 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 * when a valid link-local address is not available.
1550 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001551 saddr = &in6addr_any;
1552 } else
1553 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +00001555 ip6_mc_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1558
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001559 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -03001560 skb_put(skb, sizeof(*pmr));
1561 pmr = (struct mld2_report *)skb_transport_header(skb);
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001562 pmr->mld2r_type = ICMPV6_MLD2_REPORT;
1563 pmr->mld2r_resv1 = 0;
1564 pmr->mld2r_cksum = 0;
1565 pmr->mld2r_resv2 = 0;
1566 pmr->mld2r_ngrec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 return skb;
1568}
1569
1570static void mld_sendpack(struct sk_buff *skb)
1571{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001572 struct ipv6hdr *pip6 = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001573 struct mld2_report *pmr =
1574 (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 int payload_len, mldlen;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001576 struct inet6_dev *idev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001577 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 int err;
David S. Miller4c9483b2011-03-12 16:22:43 -05001579 struct flowi6 fl6;
Eric Dumazetadf30902009-06-02 05:19:30 +00001580 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Eric Dumazet96b52e62010-06-07 21:05:02 +00001582 rcu_read_lock();
1583 idev = __in6_dev_get(skb->dev);
Neil Hormanedf391f2009-04-27 02:45:02 -07001584 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
1585
Simon Horman29a3cad2013-05-28 20:34:26 +00001586 payload_len = (skb_tail_pointer(skb) - skb_network_header(skb)) -
1587 sizeof(*pip6);
1588 mldlen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 pip6->payload_len = htons(payload_len);
1590
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001591 pmr->mld2r_cksum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1592 IPPROTO_ICMPV6,
1593 csum_partial(skb_transport_header(skb),
1594 mldlen, 0));
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001595
David S. Miller4c9483b2011-03-12 16:22:43 -05001596 icmpv6_flow_init(net->ipv6.igmp_sk, &fl6, ICMPV6_MLD2_REPORT,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001597 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1598 skb->dev->ifindex);
YOSHIFUJI Hideaki / 吉藤英明12fd84f2013-01-18 02:00:24 +00001599 dst = icmp6_dst_alloc(skb->dev, &fl6);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001600
David S. Miller452edd52011-03-02 13:27:41 -08001601 err = 0;
1602 if (IS_ERR(dst)) {
1603 err = PTR_ERR(dst);
1604 dst = NULL;
1605 }
Eric Dumazetadf30902009-06-02 05:19:30 +00001606 skb_dst_set(skb, dst);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001607 if (err)
1608 goto err_out;
1609
Neil Hormanedf391f2009-04-27 02:45:02 -07001610 payload_len = skb->len;
1611
Jan Engelhardtb2e0b382010-03-23 04:09:07 +01001612 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001613 dst_output);
1614out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (!err) {
Hannes Frederic Sowa43a43b62014-03-31 20:14:10 +02001616 ICMP6MSGOUT_INC_STATS(net, idev, ICMPV6_MLD2_REPORT);
1617 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
1618 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, payload_len);
1619 } else {
1620 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
1621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Eric Dumazet96b52e62010-06-07 21:05:02 +00001623 rcu_read_unlock();
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001624 return;
1625
1626err_out:
1627 kfree_skb(skb);
1628 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
1631static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1632{
Yan Zhengfab10fe2005-10-05 12:08:13 -07001633 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
1636static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1637 int type, struct mld2_grec **ppgr)
1638{
1639 struct net_device *dev = pmc->idev->dev;
1640 struct mld2_report *pmr;
1641 struct mld2_grec *pgr;
1642
1643 if (!skb)
Amerigo Wang89657792013-06-29 21:30:49 +08001644 skb = mld_newpack(pmc->idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (!skb)
1646 return NULL;
1647 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1648 pgr->grec_type = type;
1649 pgr->grec_auxwords = 0;
1650 pgr->grec_nsrcs = 0;
1651 pgr->grec_mca = pmc->mca_addr; /* structure copy */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001652 pmr = (struct mld2_report *)skb_transport_header(skb);
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001653 pmr->mld2r_ngrec = htons(ntohs(pmr->mld2r_ngrec)+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 *ppgr = pgr;
1655 return skb;
1656}
1657
1658#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1659 skb_tailroom(skb)) : 0)
1660
1661static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001662 int type, int gdeleted, int sdeleted, int crsend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Amerigo Wang89657792013-06-29 21:30:49 +08001664 struct inet6_dev *idev = pmc->idev;
1665 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 struct mld2_report *pmr;
1667 struct mld2_grec *pgr = NULL;
1668 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001669 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 if (pmc->mca_flags & MAF_NOREPORT)
1672 return skb;
1673
1674 isquery = type == MLD2_MODE_IS_INCLUDE ||
1675 type == MLD2_MODE_IS_EXCLUDE;
1676 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1677 type == MLD2_CHANGE_TO_EXCLUDE;
1678
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001679 stotal = scount = 0;
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1682
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001683 if (!*psf_list)
1684 goto empty_source;
1685
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001686 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 /* EX and TO_EX get a fresh packet, if needed */
1689 if (truncate) {
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001690 if (pmr && pmr->mld2r_ngrec &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1692 if (skb)
1693 mld_sendpack(skb);
Amerigo Wang89657792013-06-29 21:30:49 +08001694 skb = mld_newpack(idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696 }
1697 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 psf_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01001699 for (psf = *psf_list; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 struct in6_addr *psrc;
1701
1702 psf_next = psf->sf_next;
1703
1704 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1705 psf_prev = psf;
1706 continue;
1707 }
1708
1709 /* clear marks on query responses */
1710 if (isquery)
1711 psf->sf_gsresp = 0;
1712
1713 if (AVAILABLE(skb) < sizeof(*psrc) +
1714 first*sizeof(struct mld2_grec)) {
1715 if (truncate && !first)
1716 break; /* truncate these */
1717 if (pgr)
1718 pgr->grec_nsrcs = htons(scount);
1719 if (skb)
1720 mld_sendpack(skb);
Amerigo Wang89657792013-06-29 21:30:49 +08001721 skb = mld_newpack(idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 first = 1;
1723 scount = 0;
1724 }
1725 if (first) {
1726 skb = add_grhead(skb, pmc, type, &pgr);
1727 first = 0;
1728 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -08001729 if (!skb)
1730 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1732 *psrc = psf->sf_addr;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001733 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1735 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1736 psf->sf_crcount--;
1737 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1738 if (psf_prev)
1739 psf_prev->sf_next = psf->sf_next;
1740 else
1741 *psf_list = psf->sf_next;
1742 kfree(psf);
1743 continue;
1744 }
1745 }
1746 psf_prev = psf;
1747 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001748
1749empty_source:
1750 if (!stotal) {
1751 if (type == MLD2_ALLOW_NEW_SOURCES ||
1752 type == MLD2_BLOCK_OLD_SOURCES)
1753 return skb;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001754 if (pmc->mca_crcount || isquery || crsend) {
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001755 /* make sure we have room for group header */
1756 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1757 mld_sendpack(skb);
1758 skb = NULL; /* add_grhead will get a new one */
1759 }
1760 skb = add_grhead(skb, pmc, type, &pgr);
1761 }
1762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 if (pgr)
1764 pgr->grec_nsrcs = htons(scount);
1765
1766 if (isquery)
1767 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1768 return skb;
1769}
1770
1771static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1772{
1773 struct sk_buff *skb = NULL;
1774 int type;
1775
Amerigo Wang89657792013-06-29 21:30:49 +08001776 read_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (!pmc) {
Ian Morris67ba4152014-08-24 21:53:10 +01001778 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 if (pmc->mca_flags & MAF_NOREPORT)
1780 continue;
1781 spin_lock_bh(&pmc->mca_lock);
1782 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1783 type = MLD2_MODE_IS_EXCLUDE;
1784 else
1785 type = MLD2_MODE_IS_INCLUDE;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001786 skb = add_grec(skb, pmc, type, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 spin_unlock_bh(&pmc->mca_lock);
1788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 } else {
1790 spin_lock_bh(&pmc->mca_lock);
1791 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1792 type = MLD2_MODE_IS_EXCLUDE;
1793 else
1794 type = MLD2_MODE_IS_INCLUDE;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001795 skb = add_grec(skb, pmc, type, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 spin_unlock_bh(&pmc->mca_lock);
1797 }
Amerigo Wang89657792013-06-29 21:30:49 +08001798 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (skb)
1800 mld_sendpack(skb);
1801}
1802
1803/*
1804 * remove zero-count source records from a source filter list
1805 */
1806static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1807{
1808 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1809
1810 psf_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01001811 for (psf = *ppsf; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 psf_next = psf->sf_next;
1813 if (psf->sf_crcount == 0) {
1814 if (psf_prev)
1815 psf_prev->sf_next = psf->sf_next;
1816 else
1817 *ppsf = psf->sf_next;
1818 kfree(psf);
1819 } else
1820 psf_prev = psf;
1821 }
1822}
1823
1824static void mld_send_cr(struct inet6_dev *idev)
1825{
1826 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1827 struct sk_buff *skb = NULL;
1828 int type, dtype;
1829
1830 read_lock_bh(&idev->lock);
Stephen Hemminger6457d262010-02-17 18:48:44 -08001831 spin_lock(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 /* deleted MCA's */
1834 pmc_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01001835 for (pmc = idev->mc_tomb; pmc; pmc = pmc_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 pmc_next = pmc->next;
1837 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1838 type = MLD2_BLOCK_OLD_SOURCES;
1839 dtype = MLD2_BLOCK_OLD_SOURCES;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001840 skb = add_grec(skb, pmc, type, 1, 0, 0);
1841 skb = add_grec(skb, pmc, dtype, 1, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 }
1843 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1845 type = MLD2_CHANGE_TO_INCLUDE;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001846 skb = add_grec(skb, pmc, type, 1, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001848 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 if (pmc->mca_crcount == 0) {
1850 mld_clear_zeros(&pmc->mca_tomb);
1851 mld_clear_zeros(&pmc->mca_sources);
1852 }
1853 }
1854 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1855 !pmc->mca_sources) {
1856 if (pmc_prev)
1857 pmc_prev->next = pmc_next;
1858 else
1859 idev->mc_tomb = pmc_next;
1860 in6_dev_put(pmc->idev);
1861 kfree(pmc);
1862 } else
1863 pmc_prev = pmc;
1864 }
Stephen Hemminger6457d262010-02-17 18:48:44 -08001865 spin_unlock(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 /* change recs */
Ian Morris67ba4152014-08-24 21:53:10 +01001868 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 spin_lock_bh(&pmc->mca_lock);
1870 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1871 type = MLD2_BLOCK_OLD_SOURCES;
1872 dtype = MLD2_ALLOW_NEW_SOURCES;
1873 } else {
1874 type = MLD2_ALLOW_NEW_SOURCES;
1875 dtype = MLD2_BLOCK_OLD_SOURCES;
1876 }
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001877 skb = add_grec(skb, pmc, type, 0, 0, 0);
1878 skb = add_grec(skb, pmc, dtype, 0, 1, 0); /* deleted sources */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 /* filter mode changes */
1881 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1883 type = MLD2_CHANGE_TO_EXCLUDE;
1884 else
1885 type = MLD2_CHANGE_TO_INCLUDE;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001886 skb = add_grec(skb, pmc, type, 0, 0, 0);
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001887 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
1889 spin_unlock_bh(&pmc->mca_lock);
1890 }
1891 read_unlock_bh(&idev->lock);
1892 if (!skb)
1893 return;
1894 (void) mld_sendpack(skb);
1895}
1896
1897static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1898{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001899 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001900 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 struct inet6_dev *idev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001902 struct sk_buff *skb;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001903 struct mld_msg *hdr;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001904 const struct in6_addr *snd_addr, *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 struct in6_addr addr_buf;
Herbert Xua7ae1992011-11-18 02:20:04 +00001906 int hlen = LL_RESERVED_SPACE(dev);
1907 int tlen = dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 int err, len, payload_len, full_len;
1909 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1910 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1911 IPV6_TLV_PADN, 0 };
David S. Miller4c9483b2011-03-12 16:22:43 -05001912 struct flowi6 fl6;
Eric Dumazetadf30902009-06-02 05:19:30 +00001913 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09001915 if (type == ICMPV6_MGM_REDUCTION)
1916 snd_addr = &in6addr_linklocal_allrouters;
1917 else
1918 snd_addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1921 payload_len = len + sizeof(ra);
1922 full_len = sizeof(struct ipv6hdr) + payload_len;
1923
Neil Hormanedf391f2009-04-27 02:45:02 -07001924 rcu_read_lock();
1925 IP6_UPD_PO_STATS(net, __in6_dev_get(dev),
1926 IPSTATS_MIB_OUT, full_len);
1927 rcu_read_unlock();
1928
Herbert Xua7ae1992011-11-18 02:20:04 +00001929 skb = sock_alloc_send_skb(sk, hlen + tlen + full_len, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 if (skb == NULL) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001932 rcu_read_lock();
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001933 IP6_INC_STATS(net, __in6_dev_get(dev),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001934 IPSTATS_MIB_OUTDISCARDS);
1935 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return;
1937 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +02001938 skb->priority = TC_PRIO_CONTROL;
Herbert Xua7ae1992011-11-18 02:20:04 +00001939 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Neil Horman95c385b2007-04-25 17:08:10 -07001941 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001943 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 * when a valid link-local address is not available.
1945 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001946 saddr = &in6addr_any;
1947 } else
1948 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +00001950 ip6_mc_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1953
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001954 hdr = (struct mld_msg *) skb_put(skb, sizeof(struct mld_msg));
1955 memset(hdr, 0, sizeof(struct mld_msg));
1956 hdr->mld_type = type;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001957 hdr->mld_mca = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001959 hdr->mld_cksum = csum_ipv6_magic(saddr, snd_addr, len,
1960 IPPROTO_ICMPV6,
1961 csum_partial(hdr, len, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Eric Dumazet96b52e62010-06-07 21:05:02 +00001963 rcu_read_lock();
1964 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
David S. Miller4c9483b2011-03-12 16:22:43 -05001966 icmpv6_flow_init(sk, &fl6, type,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001967 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1968 skb->dev->ifindex);
YOSHIFUJI Hideaki / 吉藤英明12fd84f2013-01-18 02:00:24 +00001969 dst = icmp6_dst_alloc(skb->dev, &fl6);
David S. Miller452edd52011-03-02 13:27:41 -08001970 if (IS_ERR(dst)) {
1971 err = PTR_ERR(dst);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001972 goto err_out;
David S. Miller452edd52011-03-02 13:27:41 -08001973 }
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001974
Eric Dumazetadf30902009-06-02 05:19:30 +00001975 skb_dst_set(skb, dst);
Jan Engelhardtb2e0b382010-03-23 04:09:07 +01001976 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001977 dst_output);
1978out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 if (!err) {
Denis V. Lunev5c5d2442008-10-08 10:33:50 -07001980 ICMP6MSGOUT_INC_STATS(net, idev, type);
Denis V. Luneva862f6a2008-10-08 10:33:06 -07001981 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
Neil Hormanedf391f2009-04-27 02:45:02 -07001982 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, full_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 } else
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001984 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Eric Dumazet96b52e62010-06-07 21:05:02 +00001986 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 return;
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001988
1989err_out:
1990 kfree_skb(skb);
1991 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
1993
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001994static void mld_send_initial_cr(struct inet6_dev *idev)
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02001995{
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001996 struct sk_buff *skb;
1997 struct ifmcaddr6 *pmc;
1998 int type;
1999
2000 if (mld_in_v1_mode(idev))
2001 return;
2002
2003 skb = NULL;
2004 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002005 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002006 spin_lock_bh(&pmc->mca_lock);
2007 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2008 type = MLD2_CHANGE_TO_EXCLUDE;
2009 else
2010 type = MLD2_CHANGE_TO_INCLUDE;
2011 skb = add_grec(skb, pmc, type, 0, 0, 1);
2012 spin_unlock_bh(&pmc->mca_lock);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002013 }
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002014 read_unlock_bh(&idev->lock);
2015 if (skb)
2016 mld_sendpack(skb);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002017}
2018
2019void ipv6_mc_dad_complete(struct inet6_dev *idev)
2020{
2021 idev->mc_dad_count = idev->mc_qrv;
2022 if (idev->mc_dad_count) {
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002023 mld_send_initial_cr(idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002024 idev->mc_dad_count--;
2025 if (idev->mc_dad_count)
2026 mld_dad_start_timer(idev, idev->mc_maxdelay);
2027 }
2028}
2029
2030static void mld_dad_timer_expire(unsigned long data)
2031{
2032 struct inet6_dev *idev = (struct inet6_dev *)data;
2033
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002034 mld_send_initial_cr(idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002035 if (idev->mc_dad_count) {
2036 idev->mc_dad_count--;
2037 if (idev->mc_dad_count)
2038 mld_dad_start_timer(idev, idev->mc_maxdelay);
2039 }
Salam Noureddine9260d3e2013-09-29 13:41:34 -07002040 in6_dev_put(idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002041}
2042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002044 const struct in6_addr *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
2046 struct ip6_sf_list *psf, *psf_prev;
2047 int rv = 0;
2048
2049 psf_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01002050 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
2052 break;
2053 psf_prev = psf;
2054 }
2055 if (!psf || psf->sf_count[sfmode] == 0) {
2056 /* source filter not found, or count wrong => bug */
2057 return -ESRCH;
2058 }
2059 psf->sf_count[sfmode]--;
2060 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
2061 struct inet6_dev *idev = pmc->idev;
2062
2063 /* no more filters for this source */
2064 if (psf_prev)
2065 psf_prev->sf_next = psf->sf_next;
2066 else
2067 pmc->mca_sources = psf->sf_next;
2068 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
Daniel Borkmann6c567b72013-09-04 00:19:38 +02002069 !mld_in_v1_mode(idev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 psf->sf_crcount = idev->mc_qrv;
2071 psf->sf_next = pmc->mca_tomb;
2072 pmc->mca_tomb = psf;
2073 rv = 1;
2074 } else
2075 kfree(psf);
2076 }
2077 return rv;
2078}
2079
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002080static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2081 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 int delta)
2083{
2084 struct ifmcaddr6 *pmc;
2085 int changerec = 0;
2086 int i, err;
2087
2088 if (!idev)
2089 return -ENODEV;
2090 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002091 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2093 break;
2094 }
2095 if (!pmc) {
2096 /* MCA not found?? bug */
2097 read_unlock_bh(&idev->lock);
2098 return -ESRCH;
2099 }
2100 spin_lock_bh(&pmc->mca_lock);
2101 sf_markstate(pmc);
2102 if (!delta) {
2103 if (!pmc->mca_sfcount[sfmode]) {
2104 spin_unlock_bh(&pmc->mca_lock);
2105 read_unlock_bh(&idev->lock);
2106 return -EINVAL;
2107 }
2108 pmc->mca_sfcount[sfmode]--;
2109 }
2110 err = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01002111 for (i = 0; i < sfcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2113
2114 changerec |= rv > 0;
2115 if (!err && rv < 0)
2116 err = rv;
2117 }
2118 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
2119 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
2120 pmc->mca_sfcount[MCAST_INCLUDE]) {
2121 struct ip6_sf_list *psf;
2122
2123 /* filter mode change */
2124 pmc->mca_sfmode = MCAST_INCLUDE;
2125 pmc->mca_crcount = idev->mc_qrv;
2126 idev->mc_ifc_count = pmc->mca_crcount;
Ian Morris67ba4152014-08-24 21:53:10 +01002127 for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 psf->sf_crcount = 0;
2129 mld_ifc_event(pmc->idev);
2130 } else if (sf_setstate(pmc) || changerec)
2131 mld_ifc_event(pmc->idev);
2132 spin_unlock_bh(&pmc->mca_lock);
2133 read_unlock_bh(&idev->lock);
2134 return err;
2135}
2136
2137/*
2138 * Add multicast single-source filter to the interface list
2139 */
2140static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
Jun Zhao99d2f472011-11-30 06:21:05 +00002141 const struct in6_addr *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
2143 struct ip6_sf_list *psf, *psf_prev;
2144
2145 psf_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01002146 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
2148 break;
2149 psf_prev = psf;
2150 }
2151 if (!psf) {
Ingo Oeser0c600ed2006-03-20 23:01:32 -08002152 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (!psf)
2154 return -ENOBUFS;
Ingo Oeser0c600ed2006-03-20 23:01:32 -08002155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 psf->sf_addr = *psfsrc;
2157 if (psf_prev) {
2158 psf_prev->sf_next = psf;
2159 } else
2160 pmc->mca_sources = psf;
2161 }
2162 psf->sf_count[sfmode]++;
2163 return 0;
2164}
2165
2166static void sf_markstate(struct ifmcaddr6 *pmc)
2167{
2168 struct ip6_sf_list *psf;
2169 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2170
Ian Morris67ba4152014-08-24 21:53:10 +01002171 for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2173 psf->sf_oldin = mca_xcount ==
2174 psf->sf_count[MCAST_EXCLUDE] &&
2175 !psf->sf_count[MCAST_INCLUDE];
2176 } else
2177 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2178}
2179
2180static int sf_setstate(struct ifmcaddr6 *pmc)
2181{
David L Stevens7add2a42006-01-24 13:06:39 -08002182 struct ip6_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2184 int qrv = pmc->idev->mc_qrv;
2185 int new_in, rv;
2186
2187 rv = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01002188 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2190 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2191 !psf->sf_count[MCAST_INCLUDE];
2192 } else
2193 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevens7add2a42006-01-24 13:06:39 -08002194 if (new_in) {
2195 if (!psf->sf_oldin) {
Al Viroe80e28b2006-02-03 20:10:03 -05002196 struct ip6_sf_list *prev = NULL;
David L Stevens7add2a42006-01-24 13:06:39 -08002197
Ian Morris67ba4152014-08-24 21:53:10 +01002198 for (dpsf = pmc->mca_tomb; dpsf;
2199 dpsf = dpsf->sf_next) {
David L Stevens7add2a42006-01-24 13:06:39 -08002200 if (ipv6_addr_equal(&dpsf->sf_addr,
2201 &psf->sf_addr))
2202 break;
2203 prev = dpsf;
2204 }
2205 if (dpsf) {
2206 if (prev)
2207 prev->sf_next = dpsf->sf_next;
2208 else
2209 pmc->mca_tomb = dpsf->sf_next;
2210 kfree(dpsf);
2211 }
2212 psf->sf_crcount = qrv;
2213 rv++;
2214 }
2215 } else if (psf->sf_oldin) {
2216 psf->sf_crcount = 0;
2217 /*
2218 * add or update "delete" records if an active filter
2219 * is now inactive
2220 */
Ian Morris67ba4152014-08-24 21:53:10 +01002221 for (dpsf = pmc->mca_tomb; dpsf; dpsf = dpsf->sf_next)
David L Stevens7add2a42006-01-24 13:06:39 -08002222 if (ipv6_addr_equal(&dpsf->sf_addr,
2223 &psf->sf_addr))
2224 break;
2225 if (!dpsf) {
Joe Perches5d553542010-05-31 17:23:22 +00002226 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevens7add2a42006-01-24 13:06:39 -08002227 if (!dpsf)
2228 continue;
2229 *dpsf = *psf;
2230 /* pmc->mca_lock held by callers */
2231 dpsf->sf_next = pmc->mca_tomb;
2232 pmc->mca_tomb = dpsf;
2233 }
2234 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 rv++;
2236 }
2237 }
2238 return rv;
2239}
2240
2241/*
2242 * Add multicast source filter list to the interface list
2243 */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002244static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2245 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 int delta)
2247{
2248 struct ifmcaddr6 *pmc;
2249 int isexclude;
2250 int i, err;
2251
2252 if (!idev)
2253 return -ENODEV;
2254 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002255 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2257 break;
2258 }
2259 if (!pmc) {
2260 /* MCA not found?? bug */
2261 read_unlock_bh(&idev->lock);
2262 return -ESRCH;
2263 }
2264 spin_lock_bh(&pmc->mca_lock);
2265
2266 sf_markstate(pmc);
2267 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2268 if (!delta)
2269 pmc->mca_sfcount[sfmode]++;
2270 err = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01002271 for (i = 0; i < sfcount; i++) {
Jun Zhao99d2f472011-11-30 06:21:05 +00002272 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (err)
2274 break;
2275 }
2276 if (err) {
2277 int j;
2278
2279 if (!delta)
2280 pmc->mca_sfcount[sfmode]--;
Ian Morris67ba4152014-08-24 21:53:10 +01002281 for (j = 0; j < i; j++)
RongQing.Li78d50212012-04-04 16:47:04 +00002282 ip6_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 struct ip6_sf_list *psf;
2285
2286 /* filter mode change */
2287 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2288 pmc->mca_sfmode = MCAST_EXCLUDE;
2289 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2290 pmc->mca_sfmode = MCAST_INCLUDE;
2291 /* else no filters; keep old mode for reports */
2292
2293 pmc->mca_crcount = idev->mc_qrv;
2294 idev->mc_ifc_count = pmc->mca_crcount;
Ian Morris67ba4152014-08-24 21:53:10 +01002295 for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 psf->sf_crcount = 0;
2297 mld_ifc_event(idev);
2298 } else if (sf_setstate(pmc))
2299 mld_ifc_event(idev);
2300 spin_unlock_bh(&pmc->mca_lock);
2301 read_unlock_bh(&idev->lock);
2302 return err;
2303}
2304
2305static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2306{
2307 struct ip6_sf_list *psf, *nextpsf;
2308
Ian Morris67ba4152014-08-24 21:53:10 +01002309 for (psf = pmc->mca_tomb; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 nextpsf = psf->sf_next;
2311 kfree(psf);
2312 }
2313 pmc->mca_tomb = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01002314 for (psf = pmc->mca_sources; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 nextpsf = psf->sf_next;
2316 kfree(psf);
2317 }
2318 pmc->mca_sources = NULL;
2319 pmc->mca_sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002320 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2322}
2323
2324
2325static void igmp6_join_group(struct ifmcaddr6 *ma)
2326{
2327 unsigned long delay;
2328
2329 if (ma->mca_flags & MAF_NOREPORT)
2330 return;
2331
2332 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2333
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05002334 delay = prandom_u32() % unsolicited_report_interval(ma->idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
2336 spin_lock_bh(&ma->mca_lock);
2337 if (del_timer(&ma->mca_timer)) {
2338 atomic_dec(&ma->mca_refcnt);
2339 delay = ma->mca_timer.expires - jiffies;
2340 }
2341
2342 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2343 atomic_inc(&ma->mca_refcnt);
2344 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2345 spin_unlock_bh(&ma->mca_lock);
2346}
2347
2348static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2349 struct inet6_dev *idev)
2350{
2351 int err;
2352
David L Stevens5ab4a6c2005-12-27 14:03:00 -08002353 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2354 * so no other readers or writers of iml or its sflist
2355 */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002356 if (!iml->sflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 /* any-source empty exclude case */
2358 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2359 }
2360 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2361 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2362 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2363 iml->sflist = NULL;
2364 return err;
2365}
2366
2367static void igmp6_leave_group(struct ifmcaddr6 *ma)
2368{
Daniel Borkmann6c567b72013-09-04 00:19:38 +02002369 if (mld_in_v1_mode(ma->idev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (ma->mca_flags & MAF_LAST_REPORTER)
2371 igmp6_send(&ma->mca_addr, ma->idev->dev,
2372 ICMPV6_MGM_REDUCTION);
2373 } else {
2374 mld_add_delrec(ma->idev, ma);
2375 mld_ifc_event(ma->idev);
2376 }
2377}
2378
2379static void mld_gq_timer_expire(unsigned long data)
2380{
2381 struct inet6_dev *idev = (struct inet6_dev *)data;
2382
2383 idev->mc_gq_running = 0;
2384 mld_send_report(idev, NULL);
Salam Noureddine9260d3e2013-09-29 13:41:34 -07002385 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386}
2387
2388static void mld_ifc_timer_expire(unsigned long data)
2389{
2390 struct inet6_dev *idev = (struct inet6_dev *)data;
2391
2392 mld_send_cr(idev);
2393 if (idev->mc_ifc_count) {
2394 idev->mc_ifc_count--;
2395 if (idev->mc_ifc_count)
2396 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2397 }
Salam Noureddine9260d3e2013-09-29 13:41:34 -07002398 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399}
2400
2401static void mld_ifc_event(struct inet6_dev *idev)
2402{
Daniel Borkmann6c567b72013-09-04 00:19:38 +02002403 if (mld_in_v1_mode(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 return;
2405 idev->mc_ifc_count = idev->mc_qrv;
2406 mld_ifc_start_timer(idev, 1);
2407}
2408
2409
2410static void igmp6_timer_handler(unsigned long data)
2411{
2412 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2413
Daniel Borkmann6c567b72013-09-04 00:19:38 +02002414 if (mld_in_v1_mode(ma->idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2416 else
2417 mld_send_report(ma->idev, ma);
2418
2419 spin_lock(&ma->mca_lock);
2420 ma->mca_flags |= MAF_LAST_REPORTER;
2421 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2422 spin_unlock(&ma->mca_lock);
2423 ma_put(ma);
2424}
2425
Moni Shoua75c78502009-09-15 02:37:40 -07002426/* Device changing type */
2427
2428void ipv6_mc_unmap(struct inet6_dev *idev)
2429{
2430 struct ifmcaddr6 *i;
2431
2432 /* Install multicast list, except for all-nodes (already installed) */
2433
2434 read_lock_bh(&idev->lock);
2435 for (i = idev->mc_list; i; i = i->next)
2436 igmp6_group_dropped(i);
2437 read_unlock_bh(&idev->lock);
2438}
2439
2440void ipv6_mc_remap(struct inet6_dev *idev)
2441{
2442 ipv6_mc_up(idev);
2443}
2444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445/* Device going down */
2446
2447void ipv6_mc_down(struct inet6_dev *idev)
2448{
2449 struct ifmcaddr6 *i;
2450
2451 /* Withdraw multicast list */
2452
2453 read_lock_bh(&idev->lock);
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02002454 mld_ifc_stop_timer(idev);
2455 mld_gq_stop_timer(idev);
2456 mld_dad_stop_timer(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Ian Morris67ba4152014-08-24 21:53:10 +01002458 for (i = idev->mc_list; i; i = i->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 igmp6_group_dropped(i);
2460 read_unlock_bh(&idev->lock);
2461
2462 mld_clear_delrec(idev);
2463}
2464
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02002465static void ipv6_mc_reset(struct inet6_dev *idev)
2466{
2467 idev->mc_qrv = sysctl_mld_qrv;
2468 idev->mc_qi = MLD_QI_DEFAULT;
2469 idev->mc_qri = MLD_QRI_DEFAULT;
2470 idev->mc_v1_seen = 0;
2471 idev->mc_maxdelay = unsolicited_report_interval(idev);
2472}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
2474/* Device going up */
2475
2476void ipv6_mc_up(struct inet6_dev *idev)
2477{
2478 struct ifmcaddr6 *i;
2479
2480 /* Install multicast list, except for all-nodes (already installed) */
2481
2482 read_lock_bh(&idev->lock);
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02002483 ipv6_mc_reset(idev);
Ian Morris67ba4152014-08-24 21:53:10 +01002484 for (i = idev->mc_list; i; i = i->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 igmp6_group_added(i);
2486 read_unlock_bh(&idev->lock);
2487}
2488
2489/* IPv6 device initialization. */
2490
2491void ipv6_mc_init_dev(struct inet6_dev *idev)
2492{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 write_lock_bh(&idev->lock);
Stephen Hemminger6457d262010-02-17 18:48:44 -08002494 spin_lock_init(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 idev->mc_gq_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002496 setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2497 (unsigned long)idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 idev->mc_tomb = NULL;
2499 idev->mc_ifc_count = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002500 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2501 (unsigned long)idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002502 setup_timer(&idev->mc_dad_timer, mld_dad_timer_expire,
2503 (unsigned long)idev);
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02002504 ipv6_mc_reset(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
2508/*
2509 * Device is about to be destroyed: clean up.
2510 */
2511
2512void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2513{
2514 struct ifmcaddr6 *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
2516 /* Deactivate timers */
2517 ipv6_mc_down(idev);
2518
2519 /* Delete all-nodes address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2521 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2522 * fail.
2523 */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002524 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002526 if (idev->cnf.forwarding)
2527 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
2529 write_lock_bh(&idev->lock);
2530 while ((i = idev->mc_list) != NULL) {
2531 idev->mc_list = i->next;
2532 write_unlock_bh(&idev->lock);
2533
2534 igmp6_group_dropped(i);
2535 ma_put(i);
2536
2537 write_lock_bh(&idev->lock);
2538 }
2539 write_unlock_bh(&idev->lock);
2540}
2541
2542#ifdef CONFIG_PROC_FS
2543struct igmp6_mc_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002544 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 struct net_device *dev;
2546 struct inet6_dev *idev;
2547};
2548
2549#define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2550
2551static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2552{
2553 struct ifmcaddr6 *im = NULL;
2554 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002555 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Pavel Emelianov7562f872007-05-03 15:13:45 -07002557 state->idev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002558 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 struct inet6_dev *idev;
Eric Dumazetce81b762009-11-11 17:34:30 +00002560 idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 if (!idev)
2562 continue;
2563 read_lock_bh(&idev->lock);
2564 im = idev->mc_list;
2565 if (im) {
2566 state->idev = idev;
2567 break;
2568 }
2569 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
2571 return im;
2572}
2573
2574static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2575{
2576 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2577
2578 im = im->next;
2579 while (!im) {
Eric Dumazetce81b762009-11-11 17:34:30 +00002580 if (likely(state->idev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 read_unlock_bh(&state->idev->lock);
Eric Dumazetce81b762009-11-11 17:34:30 +00002582
2583 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 if (!state->dev) {
2585 state->idev = NULL;
2586 break;
2587 }
Eric Dumazetce81b762009-11-11 17:34:30 +00002588 state->idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 if (!state->idev)
2590 continue;
2591 read_lock_bh(&state->idev->lock);
2592 im = state->idev->mc_list;
2593 }
2594 return im;
2595}
2596
2597static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2598{
2599 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2600 if (im)
2601 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2602 --pos;
2603 return pos ? NULL : im;
2604}
2605
2606static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetce81b762009-11-11 17:34:30 +00002607 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
Eric Dumazetce81b762009-11-11 17:34:30 +00002609 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 return igmp6_mc_get_idx(seq, *pos);
2611}
2612
2613static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2614{
Eric Dumazetce81b762009-11-11 17:34:30 +00002615 struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 ++*pos;
2618 return im;
2619}
2620
2621static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetce81b762009-11-11 17:34:30 +00002622 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623{
2624 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
Eric Dumazetce81b762009-11-11 17:34:30 +00002625
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 if (likely(state->idev != NULL)) {
2627 read_unlock_bh(&state->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 state->idev = NULL;
2629 }
2630 state->dev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002631 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632}
2633
2634static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2635{
2636 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2637 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2638
2639 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002640 "%-4d %-15s %pi6 %5d %08X %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 state->dev->ifindex, state->dev->name,
Harvey Harrisonb0711952008-10-28 16:05:40 -07002642 &im->mca_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 im->mca_users, im->mca_flags,
2644 (im->mca_flags&MAF_TIMER_RUNNING) ?
2645 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2646 return 0;
2647}
2648
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002649static const struct seq_operations igmp6_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 .start = igmp6_mc_seq_start,
2651 .next = igmp6_mc_seq_next,
2652 .stop = igmp6_mc_seq_stop,
2653 .show = igmp6_mc_seq_show,
2654};
2655
2656static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2657{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002658 return seq_open_net(inode, file, &igmp6_mc_seq_ops,
2659 sizeof(struct igmp6_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660}
2661
Arjan van de Ven9a321442007-02-12 00:55:35 -08002662static const struct file_operations igmp6_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 .owner = THIS_MODULE,
2664 .open = igmp6_mc_seq_open,
2665 .read = seq_read,
2666 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002667 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668};
2669
2670struct igmp6_mcf_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002671 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 struct net_device *dev;
2673 struct inet6_dev *idev;
2674 struct ifmcaddr6 *im;
2675};
2676
2677#define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2678
2679static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2680{
2681 struct ip6_sf_list *psf = NULL;
2682 struct ifmcaddr6 *im = NULL;
2683 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002684 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Pavel Emelianov7562f872007-05-03 15:13:45 -07002686 state->idev = NULL;
2687 state->im = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002688 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 struct inet6_dev *idev;
Eric Dumazetce81b762009-11-11 17:34:30 +00002690 idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 if (unlikely(idev == NULL))
2692 continue;
2693 read_lock_bh(&idev->lock);
2694 im = idev->mc_list;
2695 if (likely(im != NULL)) {
2696 spin_lock_bh(&im->mca_lock);
2697 psf = im->mca_sources;
2698 if (likely(psf != NULL)) {
2699 state->im = im;
2700 state->idev = idev;
2701 break;
2702 }
2703 spin_unlock_bh(&im->mca_lock);
2704 }
2705 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 }
2707 return psf;
2708}
2709
2710static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2711{
2712 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2713
2714 psf = psf->sf_next;
2715 while (!psf) {
2716 spin_unlock_bh(&state->im->mca_lock);
2717 state->im = state->im->next;
2718 while (!state->im) {
Eric Dumazetce81b762009-11-11 17:34:30 +00002719 if (likely(state->idev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 read_unlock_bh(&state->idev->lock);
Eric Dumazetce81b762009-11-11 17:34:30 +00002721
2722 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 if (!state->dev) {
2724 state->idev = NULL;
2725 goto out;
2726 }
Eric Dumazetce81b762009-11-11 17:34:30 +00002727 state->idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 if (!state->idev)
2729 continue;
2730 read_lock_bh(&state->idev->lock);
2731 state->im = state->idev->mc_list;
2732 }
2733 if (!state->im)
2734 break;
2735 spin_lock_bh(&state->im->mca_lock);
2736 psf = state->im->mca_sources;
2737 }
2738out:
2739 return psf;
2740}
2741
2742static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2743{
2744 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2745 if (psf)
2746 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2747 --pos;
2748 return pos ? NULL : psf;
2749}
2750
2751static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetce81b762009-11-11 17:34:30 +00002752 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753{
Eric Dumazetce81b762009-11-11 17:34:30 +00002754 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2756}
2757
2758static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2759{
2760 struct ip6_sf_list *psf;
2761 if (v == SEQ_START_TOKEN)
2762 psf = igmp6_mcf_get_first(seq);
2763 else
2764 psf = igmp6_mcf_get_next(seq, v);
2765 ++*pos;
2766 return psf;
2767}
2768
2769static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetce81b762009-11-11 17:34:30 +00002770 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
2772 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2773 if (likely(state->im != NULL)) {
2774 spin_unlock_bh(&state->im->mca_lock);
2775 state->im = NULL;
2776 }
2777 if (likely(state->idev != NULL)) {
2778 read_unlock_bh(&state->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 state->idev = NULL;
2780 }
2781 state->dev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002782 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783}
2784
2785static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2786{
2787 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2788 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2789
2790 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002791 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 "%3s %6s "
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002793 "%32s %32s %6s %6s\n", "Idx",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 "Device", "Multicast Address",
2795 "Source Address", "INC", "EXC");
2796 } else {
2797 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002798 "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 state->dev->ifindex, state->dev->name,
Harvey Harrisonb0711952008-10-28 16:05:40 -07002800 &state->im->mca_addr,
2801 &psf->sf_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 psf->sf_count[MCAST_INCLUDE],
2803 psf->sf_count[MCAST_EXCLUDE]);
2804 }
2805 return 0;
2806}
2807
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002808static const struct seq_operations igmp6_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 .start = igmp6_mcf_seq_start,
2810 .next = igmp6_mcf_seq_next,
2811 .stop = igmp6_mcf_seq_stop,
2812 .show = igmp6_mcf_seq_show,
2813};
2814
2815static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2816{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002817 return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
2818 sizeof(struct igmp6_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819}
2820
Arjan van de Ven9a321442007-02-12 00:55:35 -08002821static const struct file_operations igmp6_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 .owner = THIS_MODULE,
2823 .open = igmp6_mcf_seq_open,
2824 .read = seq_read,
2825 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002826 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827};
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002828
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002829static int __net_init igmp6_proc_init(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002830{
2831 int err;
2832
2833 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002834 if (!proc_create("igmp6", S_IRUGO, net->proc_net, &igmp6_mc_seq_fops))
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002835 goto out;
Gao fengd4beaa62013-02-18 01:34:54 +00002836 if (!proc_create("mcfilter6", S_IRUGO, net->proc_net,
2837 &igmp6_mcf_seq_fops))
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002838 goto out_proc_net_igmp6;
2839
2840 err = 0;
2841out:
2842 return err;
2843
2844out_proc_net_igmp6:
Gao fengece31ff2013-02-18 01:34:56 +00002845 remove_proc_entry("igmp6", net->proc_net);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002846 goto out;
2847}
2848
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002849static void __net_exit igmp6_proc_exit(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002850{
Gao fengece31ff2013-02-18 01:34:56 +00002851 remove_proc_entry("mcfilter6", net->proc_net);
2852 remove_proc_entry("igmp6", net->proc_net);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002853}
2854#else
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002855static inline int igmp6_proc_init(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002856{
2857 return 0;
2858}
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002859static inline void igmp6_proc_exit(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002860{
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002861}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862#endif
2863
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002864static int __net_init igmp6_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 int err;
2867
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002868 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2869 SOCK_RAW, IPPROTO_ICMPV6, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00002871 pr_err("Failed to initialize the IGMP6 control socket (err %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 err);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002873 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 }
2875
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002876 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002878 err = igmp6_proc_init(net);
2879 if (err)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002880 goto out_sock_create;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002881out:
2882 return err;
2883
2884out_sock_create:
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002885 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002886 goto out;
2887}
2888
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002889static void __net_exit igmp6_net_exit(struct net *net)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002890{
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002891 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002892 igmp6_proc_exit(net);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002893}
2894
2895static struct pernet_operations igmp6_net_ops = {
2896 .init = igmp6_net_init,
2897 .exit = igmp6_net_exit,
2898};
2899
2900int __init igmp6_init(void)
2901{
2902 return register_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903}
2904
2905void igmp6_cleanup(void)
2906{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002907 unregister_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908}