blob: 27ca0b741ccc362b2ab3ae419bb6a6ded367c465 [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();
Eric Dumazet96b52e62010-06-07 21:05:02 +0000165 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (ifindex == 0) {
167 struct rt6_info *rt;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800168 rt = rt6_lookup(net, addr, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (rt) {
David S. Millerd1918542011-12-28 20:19:20 -0500170 dev = rt->dst.dev;
Amerigo Wang94e187c2012-10-29 00:13:19 +0000171 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173 } else
Eric Dumazet96b52e62010-06-07 21:05:02 +0000174 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 if (dev == NULL) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000177 rcu_read_unlock();
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200178 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
180 return -ENODEV;
181 }
182
183 mc_lst->ifindex = dev->ifindex;
184 mc_lst->sfmode = MCAST_EXCLUDE;
YOSHIFUJI Hideaki196433c2006-01-04 13:56:31 -0800185 rwlock_init(&mc_lst->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 mc_lst->sflist = NULL;
187
188 /*
189 * now add/increase the group membership on the device
190 */
191
192 err = ipv6_dev_mc_inc(dev, addr);
193
194 if (err) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000195 rcu_read_unlock();
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200196 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return err;
199 }
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 mc_lst->next = np->ipv6_mc_list;
Eric Dumazet456b61b2010-11-23 13:12:15 +0000202 rcu_assign_pointer(np->ipv6_mc_list, mc_lst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Eric Dumazet96b52e62010-06-07 21:05:02 +0000204 rcu_read_unlock();
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200205 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 return 0;
208}
209
210/*
211 * socket leave on multicast group
212 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900213int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 struct ipv6_pinfo *np = inet6_sk(sk);
Eric Dumazet456b61b2010-11-23 13:12:15 +0000216 struct ipv6_mc_socklist *mc_lst;
217 struct ipv6_mc_socklist __rcu **lnk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900218 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Li Weia858d642012-07-17 15:28:59 +0800220 if (!ipv6_addr_is_multicast(addr))
221 return -EINVAL;
222
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200223 rtnl_lock();
Eric Dumazet456b61b2010-11-23 13:12:15 +0000224 for (lnk = &np->ipv6_mc_list;
WANG Congb5350912014-09-11 15:35:13 -0700225 (mc_lst = rtnl_dereference(*lnk)) != NULL;
Eric Dumazet456b61b2010-11-23 13:12:15 +0000226 lnk = &mc_lst->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
228 ipv6_addr_equal(&mc_lst->addr, addr)) {
229 struct net_device *dev;
230
231 *lnk = mc_lst->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Eric Dumazet96b52e62010-06-07 21:05:02 +0000233 rcu_read_lock();
234 dev = dev_get_by_index_rcu(net, mc_lst->ifindex);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800235 if (dev != NULL) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000236 struct inet6_dev *idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
David L Stevensacd6e002006-08-17 16:27:39 -0700238 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000239 if (idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
David L Stevensacd6e002006-08-17 16:27:39 -0700241 } else
242 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000243 rcu_read_unlock();
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200244 rtnl_unlock();
245
Eric Dumazet456b61b2010-11-23 13:12:15 +0000246 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
Lai Jiangshane3cbf282011-03-18 12:00:50 +0800247 kfree_rcu(mc_lst, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return 0;
249 }
250 }
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200251 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
David L Stevens9951f032005-07-08 17:47:28 -0700253 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Eric Dumazet96b52e62010-06-07 21:05:02 +0000256/* called with rcu_read_lock() */
257static struct inet6_dev *ip6_mc_find_dev_rcu(struct net *net,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000258 const struct in6_addr *group,
Eric Dumazet96b52e62010-06-07 21:05:02 +0000259 int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct net_device *dev = NULL;
262 struct inet6_dev *idev = NULL;
263
264 if (ifindex == 0) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000265 struct rt6_info *rt = rt6_lookup(net, group, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (rt) {
David S. Millerd1918542011-12-28 20:19:20 -0500268 dev = rt->dst.dev;
Amerigo Wang94e187c2012-10-29 00:13:19 +0000269 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271 } else
Eric Dumazet96b52e62010-06-07 21:05:02 +0000272 dev = dev_get_by_index_rcu(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 if (!dev)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000275 return NULL;
276 idev = __in6_dev_get(dev);
Ilpo Järvinen448eb712008-12-14 23:15:21 -0800277 if (!idev)
Joe Perches8a22c992010-11-14 17:05:00 +0000278 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 read_lock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000280 if (idev->dead) {
281 read_unlock_bh(&idev->lock);
282 return NULL;
283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return idev;
285}
286
287void ipv6_sock_mc_close(struct sock *sk)
288{
289 struct ipv6_pinfo *np = inet6_sk(sk);
290 struct ipv6_mc_socklist *mc_lst;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900291 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Eric Dumazet0e1efe92012-12-05 09:18:10 +0000293 if (!rcu_access_pointer(np->ipv6_mc_list))
294 return;
295
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200296 rtnl_lock();
WANG Congb5350912014-09-11 15:35:13 -0700297 while ((mc_lst = rtnl_dereference(np->ipv6_mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 struct net_device *dev;
299
300 np->ipv6_mc_list = mc_lst->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Eric Dumazet96b52e62010-06-07 21:05:02 +0000302 rcu_read_lock();
303 dev = dev_get_by_index_rcu(net, mc_lst->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (dev) {
Eric Dumazet96b52e62010-06-07 21:05:02 +0000305 struct inet6_dev *idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
David L Stevensacd6e002006-08-17 16:27:39 -0700307 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000308 if (idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
David L Stevensacd6e002006-08-17 16:27:39 -0700310 } else
311 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000312 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Eric Dumazet456b61b2010-11-23 13:12:15 +0000314 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
Lai Jiangshane3cbf282011-03-18 12:00:50 +0800315 kfree_rcu(mc_lst, rcu);
Eric Dumazet456b61b2010-11-23 13:12:15 +0000316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200318 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321int ip6_mc_source(int add, int omode, struct sock *sk,
322 struct group_source_req *pgsr)
323{
324 struct in6_addr *source, *group;
325 struct ipv6_mc_socklist *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 struct inet6_dev *idev;
327 struct ipv6_pinfo *inet6 = inet6_sk(sk);
328 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900329 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 int i, j, rv;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700331 int leavegroup = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800332 int pmclocked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 int err;
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
336 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
337
338 if (!ipv6_addr_is_multicast(group))
339 return -EINVAL;
340
Eric Dumazet96b52e62010-06-07 21:05:02 +0000341 rcu_read_lock();
342 idev = ip6_mc_find_dev_rcu(net, group, pgsr->gsr_interface);
343 if (!idev) {
344 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return -ENODEV;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 err = -EADDRNOTAVAIL;
349
Eric Dumazet456b61b2010-11-23 13:12:15 +0000350 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
352 continue;
353 if (ipv6_addr_equal(&pmc->addr, group))
354 break;
355 }
David L Stevens917f2f12005-07-08 17:45:16 -0700356 if (!pmc) { /* must have a prior join */
357 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /* if a source filter was set, must be the same mode as before */
361 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -0700362 if (pmc->sfmode != omode) {
363 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 } else if (pmc->sfmode != omode) {
367 /* allow mode switches for empty-set filters */
368 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
369 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
370 pmc->sfmode = omode;
371 }
372
Eric Dumazet96b52e62010-06-07 21:05:02 +0000373 write_lock(&pmc->sflock);
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800374 pmclocked = 1;
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 psl = pmc->sflist;
377 if (!add) {
378 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -0700379 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 rv = !0;
Ian Morris67ba4152014-08-24 21:53:10 +0100381 for (i = 0; i < psl->sl_count; i++) {
YOSHIFUJI Hideaki / 吉藤英明07c2fec2013-01-29 12:48:23 +0000382 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (rv == 0)
384 break;
385 }
386 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -0700387 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700389 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
390 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
391 leavegroup = 1;
392 goto done;
393 }
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* update the interface filter */
396 ip6_mc_del_src(idev, group, omode, 1, source, 1);
397
Ian Morris67ba4152014-08-24 21:53:10 +0100398 for (j = i+1; j < psl->sl_count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 psl->sl_addr[j-1] = psl->sl_addr[j];
400 psl->sl_count--;
401 err = 0;
402 goto done;
403 }
404 /* else, add a new source to the filter */
405
406 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
407 err = -ENOBUFS;
408 goto done;
409 }
410 if (!psl || psl->sl_count == psl->sl_max) {
411 struct ip6_sf_socklist *newpsl;
412 int count = IP6_SFBLOCK;
413
414 if (psl)
415 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800416 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (!newpsl) {
418 err = -ENOBUFS;
419 goto done;
420 }
421 newpsl->sl_max = count;
422 newpsl->sl_count = count - IP6_SFBLOCK;
423 if (psl) {
Ian Morris67ba4152014-08-24 21:53:10 +0100424 for (i = 0; i < psl->sl_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 newpsl->sl_addr[i] = psl->sl_addr[i];
426 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
427 }
428 pmc->sflist = psl = newpsl;
429 }
430 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
Ian Morris67ba4152014-08-24 21:53:10 +0100431 for (i = 0; i < psl->sl_count; i++) {
YOSHIFUJI Hideaki / 吉藤英明07c2fec2013-01-29 12:48:23 +0000432 rv = !ipv6_addr_equal(&psl->sl_addr[i], source);
Jean Sacren56db1c52013-02-03 21:34:10 +0000433 if (rv == 0) /* There is an error in the address. */
434 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Ian Morris67ba4152014-08-24 21:53:10 +0100436 for (j = psl->sl_count-1; j >= i; j--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 psl->sl_addr[j+1] = psl->sl_addr[j];
438 psl->sl_addr[i] = *source;
439 psl->sl_count++;
440 err = 0;
441 /* update the interface list */
442 ip6_mc_add_src(idev, group, omode, 1, source, 1);
443done:
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800444 if (pmclocked)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000445 write_unlock(&pmc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000447 rcu_read_unlock();
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700448 if (leavegroup)
449 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return err;
451}
452
453int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
454{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000455 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct ipv6_mc_socklist *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 struct inet6_dev *idev;
458 struct ipv6_pinfo *inet6 = inet6_sk(sk);
459 struct ip6_sf_socklist *newpsl, *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900460 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -0700461 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 int i, err;
463
464 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
465
466 if (!ipv6_addr_is_multicast(group))
467 return -EINVAL;
468 if (gsf->gf_fmode != MCAST_INCLUDE &&
469 gsf->gf_fmode != MCAST_EXCLUDE)
470 return -EINVAL;
471
Eric Dumazet96b52e62010-06-07 21:05:02 +0000472 rcu_read_lock();
473 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Eric Dumazet96b52e62010-06-07 21:05:02 +0000475 if (!idev) {
476 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return -ENODEV;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
David S. Miller9c059892005-07-08 21:44:39 -0700480 err = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800481
David L Stevens9951f032005-07-08 17:47:28 -0700482 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
483 leavegroup = 1;
484 goto done;
485 }
486
Eric Dumazet456b61b2010-11-23 13:12:15 +0000487 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (pmc->ifindex != gsf->gf_interface)
489 continue;
490 if (ipv6_addr_equal(&pmc->addr, group))
491 break;
492 }
David L Stevens917f2f12005-07-08 17:45:16 -0700493 if (!pmc) { /* must have a prior join */
494 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (gsf->gf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800498 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
499 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (!newpsl) {
501 err = -ENOBUFS;
502 goto done;
503 }
504 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
Ian Morris67ba4152014-08-24 21:53:10 +0100505 for (i = 0; i < newpsl->sl_count; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 struct sockaddr_in6 *psin6;
507
508 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
509 newpsl->sl_addr[i] = psin6->sin6_addr;
510 }
511 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
512 newpsl->sl_count, newpsl->sl_addr, 0);
513 if (err) {
514 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
515 goto done;
516 }
Yan Zheng8713dbf2005-10-28 08:02:08 +0800517 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +0800519 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
520 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800521
Eric Dumazet96b52e62010-06-07 21:05:02 +0000522 write_lock(&pmc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 psl = pmc->sflist;
524 if (psl) {
525 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
526 psl->sl_count, psl->sl_addr, 0);
527 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
528 } else
529 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
530 pmc->sflist = newpsl;
531 pmc->sfmode = gsf->gf_fmode;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000532 write_unlock(&pmc->sflock);
David L Stevens917f2f12005-07-08 17:45:16 -0700533 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534done:
535 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000536 rcu_read_unlock();
David L Stevens9951f032005-07-08 17:47:28 -0700537 if (leavegroup)
538 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return err;
540}
541
542int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
543 struct group_filter __user *optval, int __user *optlen)
544{
545 int err, i, count, copycount;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000546 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 struct ipv6_mc_socklist *pmc;
548 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct ipv6_pinfo *inet6 = inet6_sk(sk);
550 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900551 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
554
555 if (!ipv6_addr_is_multicast(group))
556 return -EINVAL;
557
Eric Dumazet96b52e62010-06-07 21:05:02 +0000558 rcu_read_lock();
559 idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Eric Dumazet96b52e62010-06-07 21:05:02 +0000561 if (!idev) {
562 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return -ENODEV;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 err = -EADDRNOTAVAIL;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800567 /*
568 * changes to the ipv6_mc_list require the socket lock and
569 * a read lock on ip6_sk_mc_lock. We have the socket lock,
570 * so reading the list is safe.
571 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Eric Dumazet456b61b2010-11-23 13:12:15 +0000573 for_each_pmc_rcu(inet6, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (pmc->ifindex != gsf->gf_interface)
575 continue;
576 if (ipv6_addr_equal(group, &pmc->addr))
577 break;
578 }
579 if (!pmc) /* must have a prior join */
580 goto done;
581 gsf->gf_fmode = pmc->sfmode;
582 psl = pmc->sflist;
583 count = psl ? psl->sl_count : 0;
584 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000585 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
588 gsf->gf_numsrc = count;
589 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
590 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
591 return -EFAULT;
592 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800593 /* changes to psl require the socket lock, a read lock on
594 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
595 * have the socket lock, so reading here is safe.
596 */
Ian Morris67ba4152014-08-24 21:53:10 +0100597 for (i = 0; i < copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 struct sockaddr_in6 *psin6;
599 struct sockaddr_storage ss;
600
601 psin6 = (struct sockaddr_in6 *)&ss;
602 memset(&ss, 0, sizeof(ss));
603 psin6->sin6_family = AF_INET6;
604 psin6->sin6_addr = psl->sl_addr[i];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900605 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return -EFAULT;
607 }
608 return 0;
609done:
610 read_unlock_bh(&idev->lock);
Eric Dumazet96b52e62010-06-07 21:05:02 +0000611 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return err;
613}
614
Eric Dumazeta50feda2012-05-18 18:57:34 +0000615bool inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
616 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 struct ipv6_pinfo *np = inet6_sk(sk);
619 struct ipv6_mc_socklist *mc;
620 struct ip6_sf_socklist *psl;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000621 bool rv = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Eric Dumazet456b61b2010-11-23 13:12:15 +0000623 rcu_read_lock();
624 for_each_pmc_rcu(np, mc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (ipv6_addr_equal(&mc->addr, mc_addr))
626 break;
627 }
628 if (!mc) {
Eric Dumazet456b61b2010-11-23 13:12:15 +0000629 rcu_read_unlock();
Eric Dumazeta50feda2012-05-18 18:57:34 +0000630 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800632 read_lock(&mc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 psl = mc->sflist;
634 if (!psl) {
635 rv = mc->sfmode == MCAST_EXCLUDE;
636 } else {
637 int i;
638
Ian Morris67ba4152014-08-24 21:53:10 +0100639 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
641 break;
642 }
643 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000644 rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000646 rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800648 read_unlock(&mc->sflock);
Eric Dumazet456b61b2010-11-23 13:12:15 +0000649 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 return rv;
652}
653
654static void ma_put(struct ifmcaddr6 *mc)
655{
656 if (atomic_dec_and_test(&mc->mca_refcnt)) {
657 in6_dev_put(mc->idev);
658 kfree(mc);
659 }
660}
661
662static void igmp6_group_added(struct ifmcaddr6 *mc)
663{
664 struct net_device *dev = mc->idev->dev;
665 char buf[MAX_ADDR_LEN];
666
YOSHIFUJI Hideaki / 吉藤英明ec16ef22013-02-09 04:29:58 +0000667 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
668 IPV6_ADDR_SCOPE_LINKLOCAL)
669 return;
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 spin_lock_bh(&mc->mca_lock);
672 if (!(mc->mca_flags&MAF_LOADED)) {
673 mc->mca_flags |= MAF_LOADED;
674 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000675 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677 spin_unlock_bh(&mc->mca_lock);
678
679 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
680 return;
681
Daniel Borkmann6c567b72013-09-04 00:19:38 +0200682 if (mld_in_v1_mode(mc->idev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 igmp6_join_group(mc);
684 return;
685 }
686 /* else v2 */
687
688 mc->mca_crcount = mc->idev->mc_qrv;
689 mld_ifc_event(mc->idev);
690}
691
692static void igmp6_group_dropped(struct ifmcaddr6 *mc)
693{
694 struct net_device *dev = mc->idev->dev;
695 char buf[MAX_ADDR_LEN];
696
YOSHIFUJI Hideaki / 吉藤英明ec16ef22013-02-09 04:29:58 +0000697 if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) <
698 IPV6_ADDR_SCOPE_LINKLOCAL)
699 return;
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 spin_lock_bh(&mc->mca_lock);
702 if (mc->mca_flags&MAF_LOADED) {
703 mc->mca_flags &= ~MAF_LOADED;
704 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +0000705 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707
708 if (mc->mca_flags & MAF_NOREPORT)
709 goto done;
710 spin_unlock_bh(&mc->mca_lock);
711
712 if (!mc->idev->dead)
713 igmp6_leave_group(mc);
714
715 spin_lock_bh(&mc->mca_lock);
716 if (del_timer(&mc->mca_timer))
717 atomic_dec(&mc->mca_refcnt);
718done:
719 ip6_mc_clear_src(mc);
720 spin_unlock_bh(&mc->mca_lock);
721}
722
723/*
724 * deleted ifmcaddr6 manipulation
725 */
726static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
727{
728 struct ifmcaddr6 *pmc;
729
730 /* this is an "ifmcaddr6" for convenience; only the fields below
731 * are actually used. In particular, the refcnt and users are not
732 * used for management of the delete list. Using the same structure
733 * for deleted items allows change reports to use common code with
734 * non-deleted or query-response MCA's.
735 */
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800736 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (!pmc)
738 return;
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 spin_lock_bh(&im->mca_lock);
741 spin_lock_init(&pmc->mca_lock);
742 pmc->idev = im->idev;
743 in6_dev_hold(idev);
744 pmc->mca_addr = im->mca_addr;
745 pmc->mca_crcount = idev->mc_qrv;
746 pmc->mca_sfmode = im->mca_sfmode;
747 if (pmc->mca_sfmode == MCAST_INCLUDE) {
748 struct ip6_sf_list *psf;
749
750 pmc->mca_tomb = im->mca_tomb;
751 pmc->mca_sources = im->mca_sources;
752 im->mca_tomb = im->mca_sources = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +0100753 for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 psf->sf_crcount = pmc->mca_crcount;
755 }
756 spin_unlock_bh(&im->mca_lock);
757
Stephen Hemminger6457d262010-02-17 18:48:44 -0800758 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 pmc->next = idev->mc_tomb;
760 idev->mc_tomb = pmc;
Stephen Hemminger6457d262010-02-17 18:48:44 -0800761 spin_unlock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000764static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *pmca)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 struct ifmcaddr6 *pmc, *pmc_prev;
767 struct ip6_sf_list *psf, *psf_next;
768
Stephen Hemminger6457d262010-02-17 18:48:44 -0800769 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 pmc_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +0100771 for (pmc = idev->mc_tomb; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
773 break;
774 pmc_prev = pmc;
775 }
776 if (pmc) {
777 if (pmc_prev)
778 pmc_prev->next = pmc->next;
779 else
780 idev->mc_tomb = pmc->next;
781 }
Stephen Hemminger6457d262010-02-17 18:48:44 -0800782 spin_unlock_bh(&idev->mc_lock);
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (pmc) {
Ian Morris67ba4152014-08-24 21:53:10 +0100785 for (psf = pmc->mca_tomb; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 psf_next = psf->sf_next;
787 kfree(psf);
788 }
789 in6_dev_put(pmc->idev);
790 kfree(pmc);
791 }
792}
793
794static void mld_clear_delrec(struct inet6_dev *idev)
795{
796 struct ifmcaddr6 *pmc, *nextpmc;
797
Stephen Hemminger6457d262010-02-17 18:48:44 -0800798 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 pmc = idev->mc_tomb;
800 idev->mc_tomb = NULL;
Stephen Hemminger6457d262010-02-17 18:48:44 -0800801 spin_unlock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 for (; pmc; pmc = nextpmc) {
804 nextpmc = pmc->next;
805 ip6_mc_clear_src(pmc);
806 in6_dev_put(pmc->idev);
807 kfree(pmc);
808 }
809
810 /* clear dead sources, too */
811 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100812 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 struct ip6_sf_list *psf, *psf_next;
814
815 spin_lock_bh(&pmc->mca_lock);
816 psf = pmc->mca_tomb;
817 pmc->mca_tomb = NULL;
818 spin_unlock_bh(&pmc->mca_lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100819 for (; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 psf_next = psf->sf_next;
821 kfree(psf);
822 }
823 }
824 read_unlock_bh(&idev->lock);
825}
826
827
828/*
829 * device multicast group inc (add if not found)
830 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900831int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 struct ifmcaddr6 *mc;
834 struct inet6_dev *idev;
835
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200836 ASSERT_RTNL();
837
Eric Dumazet96b52e62010-06-07 21:05:02 +0000838 /* we need to take a reference on idev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 idev = in6_dev_get(dev);
840
841 if (idev == NULL)
842 return -EINVAL;
843
844 write_lock_bh(&idev->lock);
845 if (idev->dead) {
846 write_unlock_bh(&idev->lock);
847 in6_dev_put(idev);
848 return -ENODEV;
849 }
850
851 for (mc = idev->mc_list; mc; mc = mc->next) {
852 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
853 mc->mca_users++;
854 write_unlock_bh(&idev->lock);
855 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
856 NULL, 0);
857 in6_dev_put(idev);
858 return 0;
859 }
860 }
861
862 /*
863 * not found: create a new one.
864 */
865
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800866 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 if (mc == NULL) {
869 write_unlock_bh(&idev->lock);
870 in6_dev_put(idev);
871 return -ENOMEM;
872 }
873
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800874 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000876 mc->mca_addr = *addr;
Eric Dumazet96b52e62010-06-07 21:05:02 +0000877 mc->idev = idev; /* (reference taken) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 mc->mca_users = 1;
879 /* mca_stamp should be updated upon changes */
880 mc->mca_cstamp = mc->mca_tstamp = jiffies;
881 atomic_set(&mc->mca_refcnt, 2);
882 spin_lock_init(&mc->mca_lock);
883
884 /* initial mode is (EX, empty) */
885 mc->mca_sfmode = MCAST_EXCLUDE;
886 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
887
888 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
889 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
890 mc->mca_flags |= MAF_NOREPORT;
891
892 mc->next = idev->mc_list;
893 idev->mc_list = mc;
894 write_unlock_bh(&idev->lock);
895
896 mld_del_delrec(idev, &mc->mca_addr);
897 igmp6_group_added(mc);
898 ma_put(mc);
899 return 0;
900}
901
902/*
903 * device multicast group del
904 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900905int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 struct ifmcaddr6 *ma, **map;
908
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +0200909 ASSERT_RTNL();
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 write_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100912 for (map = &idev->mc_list; (ma = *map) != NULL; map = &ma->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
914 if (--ma->mca_users == 0) {
915 *map = ma->next;
916 write_unlock_bh(&idev->lock);
917
918 igmp6_group_dropped(ma);
919
920 ma_put(ma);
921 return 0;
922 }
923 write_unlock_bh(&idev->lock);
924 return 0;
925 }
926 }
927 write_unlock_bh(&idev->lock);
928
929 return -ENOENT;
930}
931
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900932int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Eric Dumazet96b52e62010-06-07 21:05:02 +0000934 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 int err;
936
Eric Dumazet96b52e62010-06-07 21:05:02 +0000937 rcu_read_lock();
938
939 idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (!idev)
Eric Dumazet96b52e62010-06-07 21:05:02 +0000941 err = -ENODEV;
942 else
943 err = __ipv6_dev_mc_dec(idev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Eric Dumazet96b52e62010-06-07 21:05:02 +0000945 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return err;
947}
948
949/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 * check if the interface/address pair is valid
951 */
Eric Dumazeta50feda2012-05-18 18:57:34 +0000952bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
953 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 struct inet6_dev *idev;
956 struct ifmcaddr6 *mc;
Eric Dumazeta50feda2012-05-18 18:57:34 +0000957 bool rv = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Eric Dumazet96b52e62010-06-07 21:05:02 +0000959 rcu_read_lock();
960 idev = __in6_dev_get(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (idev) {
962 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100963 for (mc = idev->mc_list; mc; mc = mc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (ipv6_addr_equal(&mc->mca_addr, group))
965 break;
966 }
967 if (mc) {
968 if (src_addr && !ipv6_addr_any(src_addr)) {
969 struct ip6_sf_list *psf;
970
971 spin_lock_bh(&mc->mca_lock);
Ian Morris67ba4152014-08-24 21:53:10 +0100972 for (psf = mc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
974 break;
975 }
976 if (psf)
977 rv = psf->sf_count[MCAST_INCLUDE] ||
978 psf->sf_count[MCAST_EXCLUDE] !=
979 mc->mca_sfcount[MCAST_EXCLUDE];
980 else
Ian Morris67ba4152014-08-24 21:53:10 +0100981 rv = mc->mca_sfcount[MCAST_EXCLUDE] != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 spin_unlock_bh(&mc->mca_lock);
983 } else
Eric Dumazeta50feda2012-05-18 18:57:34 +0000984 rv = true; /* don't filter unspecified source */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
Eric Dumazet96b52e62010-06-07 21:05:02 +0000988 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return rv;
990}
991
992static void mld_gq_start_timer(struct inet6_dev *idev)
993{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500994 unsigned long tv = prandom_u32() % idev->mc_maxdelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 idev->mc_gq_running = 1;
997 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
998 in6_dev_hold(idev);
999}
1000
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001001static void mld_gq_stop_timer(struct inet6_dev *idev)
1002{
1003 idev->mc_gq_running = 0;
1004 if (del_timer(&idev->mc_gq_timer))
1005 __in6_dev_put(idev);
1006}
1007
Daniel Borkmannc2cef4e2013-08-20 12:22:01 +02001008static void mld_ifc_start_timer(struct inet6_dev *idev, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001010 unsigned long tv = prandom_u32() % delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1013 in6_dev_hold(idev);
1014}
1015
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001016static void mld_ifc_stop_timer(struct inet6_dev *idev)
1017{
1018 idev->mc_ifc_count = 0;
1019 if (del_timer(&idev->mc_ifc_timer))
1020 __in6_dev_put(idev);
1021}
1022
Daniel Borkmannc2cef4e2013-08-20 12:22:01 +02001023static void mld_dad_start_timer(struct inet6_dev *idev, unsigned long delay)
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02001024{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001025 unsigned long tv = prandom_u32() % delay;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02001026
1027 if (!mod_timer(&idev->mc_dad_timer, jiffies+tv+2))
1028 in6_dev_hold(idev);
1029}
1030
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001031static void mld_dad_stop_timer(struct inet6_dev *idev)
1032{
1033 if (del_timer(&idev->mc_dad_timer))
1034 __in6_dev_put(idev);
1035}
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037/*
1038 * IGMP handling (alias multicast ICMPv6 messages)
1039 */
1040
1041static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1042{
1043 unsigned long delay = resptime;
1044
1045 /* Do not start timer for these addresses */
1046 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1047 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1048 return;
1049
1050 if (del_timer(&ma->mca_timer)) {
1051 atomic_dec(&ma->mca_refcnt);
1052 delay = ma->mca_timer.expires - jiffies;
1053 }
1054
Daniel Borkmanncc7f7ab2013-09-04 00:19:41 +02001055 if (delay >= resptime)
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001056 delay = prandom_u32() % resptime;
Daniel Borkmanncc7f7ab2013-09-04 00:19:41 +02001057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 ma->mca_timer.expires = jiffies + delay;
1059 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1060 atomic_inc(&ma->mca_refcnt);
1061 ma->mca_flags |= MAF_TIMER_RUNNING;
1062}
1063
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001064/* mark EXCLUDE-mode sources */
Eric Dumazeta50feda2012-05-18 18:57:34 +00001065static bool mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
1066 const struct in6_addr *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 struct ip6_sf_list *psf;
1069 int i, scount;
1070
1071 scount = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01001072 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (scount == nsrcs)
1074 break;
Ian Morris67ba4152014-08-24 21:53:10 +01001075 for (i = 0; i < nsrcs; i++) {
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001076 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +00001077 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001078 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1079 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +08001080 break;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001081 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1082 scount++;
1083 break;
1084 }
1085 }
1086 }
1087 pmc->mca_flags &= ~MAF_GSQUERY;
1088 if (scount == nsrcs) /* all sources excluded */
Eric Dumazeta50feda2012-05-18 18:57:34 +00001089 return false;
1090 return true;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001091}
1092
Eric Dumazeta50feda2012-05-18 18:57:34 +00001093static bool mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1094 const struct in6_addr *srcs)
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001095{
1096 struct ip6_sf_list *psf;
1097 int i, scount;
1098
1099 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1100 return mld_xmarksources(pmc, nsrcs, srcs);
1101
1102 /* mark INCLUDE-mode sources */
1103
1104 scount = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01001105 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001106 if (scount == nsrcs)
1107 break;
Ian Morris67ba4152014-08-24 21:53:10 +01001108 for (i = 0; i < nsrcs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1110 psf->sf_gsresp = 1;
1111 scount++;
1112 break;
1113 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001116 if (!scount) {
1117 pmc->mca_flags &= ~MAF_GSQUERY;
Eric Dumazeta50feda2012-05-18 18:57:34 +00001118 return false;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001119 }
1120 pmc->mca_flags |= MAF_GSQUERY;
Eric Dumazeta50feda2012-05-18 18:57:34 +00001121 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
Daniel Borkmann58c0ecf2013-09-04 00:19:40 +02001124static int mld_force_mld_version(const struct inet6_dev *idev)
1125{
1126 /* Normally, both are 0 here. If enforcement to a particular is
1127 * being used, individual device enforcement will have a lower
1128 * precedence over 'all' device (.../conf/all/force_mld_version).
1129 */
1130
1131 if (dev_net(idev->dev)->ipv6.devconf_all->force_mld_version != 0)
1132 return dev_net(idev->dev)->ipv6.devconf_all->force_mld_version;
1133 else
1134 return idev->cnf.force_mld_version;
1135}
1136
1137static bool mld_in_v2_mode_only(const struct inet6_dev *idev)
1138{
1139 return mld_force_mld_version(idev) == 2;
1140}
1141
1142static bool mld_in_v1_mode_only(const struct inet6_dev *idev)
1143{
1144 return mld_force_mld_version(idev) == 1;
1145}
1146
Daniel Borkmann6c567b72013-09-04 00:19:38 +02001147static bool mld_in_v1_mode(const struct inet6_dev *idev)
1148{
Daniel Borkmann58c0ecf2013-09-04 00:19:40 +02001149 if (mld_in_v2_mode_only(idev))
1150 return false;
1151 if (mld_in_v1_mode_only(idev))
Daniel Borkmann6c567b72013-09-04 00:19:38 +02001152 return true;
1153 if (idev->mc_v1_seen && time_before(jiffies, idev->mc_v1_seen))
1154 return true;
1155
1156 return false;
1157}
1158
Daniel Borkmann89225d12013-09-04 00:19:37 +02001159static void mld_set_v1_mode(struct inet6_dev *idev)
1160{
1161 /* RFC3810, relevant sections:
1162 * - 9.1. Robustness Variable
1163 * - 9.2. Query Interval
1164 * - 9.3. Query Response Interval
1165 * - 9.12. Older Version Querier Present Timeout
1166 */
1167 unsigned long switchback;
1168
1169 switchback = (idev->mc_qrv * idev->mc_qi) + idev->mc_qri;
1170
1171 idev->mc_v1_seen = jiffies + switchback;
1172}
1173
1174static void mld_update_qrv(struct inet6_dev *idev,
1175 const struct mld2_query *mlh2)
1176{
1177 /* RFC3810, relevant sections:
1178 * - 5.1.8. QRV (Querier's Robustness Variable)
1179 * - 9.1. Robustness Variable
1180 */
1181
1182 /* The value of the Robustness Variable MUST NOT be zero,
1183 * and SHOULD NOT be one. Catch this here if we ever run
1184 * into such a case in future.
1185 */
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02001186 const int min_qrv = min(MLD_QRV_DEFAULT, sysctl_mld_qrv);
Daniel Borkmann89225d12013-09-04 00:19:37 +02001187 WARN_ON(idev->mc_qrv == 0);
1188
1189 if (mlh2->mld2q_qrv > 0)
1190 idev->mc_qrv = mlh2->mld2q_qrv;
1191
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02001192 if (unlikely(idev->mc_qrv < min_qrv)) {
Daniel Borkmann89225d12013-09-04 00:19:37 +02001193 net_warn_ratelimited("IPv6: MLD: clamping QRV from %u to %u!\n",
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02001194 idev->mc_qrv, min_qrv);
1195 idev->mc_qrv = min_qrv;
Daniel Borkmann89225d12013-09-04 00:19:37 +02001196 }
1197}
1198
1199static void mld_update_qi(struct inet6_dev *idev,
1200 const struct mld2_query *mlh2)
1201{
1202 /* RFC3810, relevant sections:
1203 * - 5.1.9. QQIC (Querier's Query Interval Code)
1204 * - 9.2. Query Interval
1205 * - 9.12. Older Version Querier Present Timeout
1206 * (the [Query Interval] in the last Query received)
1207 */
1208 unsigned long mc_qqi;
1209
1210 if (mlh2->mld2q_qqic < 128) {
1211 mc_qqi = mlh2->mld2q_qqic;
1212 } else {
1213 unsigned long mc_man, mc_exp;
1214
1215 mc_exp = MLDV2_QQIC_EXP(mlh2->mld2q_qqic);
1216 mc_man = MLDV2_QQIC_MAN(mlh2->mld2q_qqic);
1217
1218 mc_qqi = (mc_man | 0x10) << (mc_exp + 3);
1219 }
1220
1221 idev->mc_qi = mc_qqi * HZ;
1222}
1223
1224static void mld_update_qri(struct inet6_dev *idev,
1225 const struct mld2_query *mlh2)
1226{
1227 /* RFC3810, relevant sections:
1228 * - 5.1.3. Maximum Response Code
1229 * - 9.3. Query Response Interval
1230 */
Daniel Borkmanne3f5b172013-09-04 00:19:39 +02001231 idev->mc_qri = msecs_to_jiffies(mldv2_mrc(mlh2));
Daniel Borkmann89225d12013-09-04 00:19:37 +02001232}
1233
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001234static int mld_process_v1(struct inet6_dev *idev, struct mld_msg *mld,
1235 unsigned long *max_delay)
1236{
1237 unsigned long mldv1_md;
1238
1239 /* Ignore v1 queries */
1240 if (mld_in_v2_mode_only(idev))
1241 return -EINVAL;
1242
1243 /* MLDv1 router present */
1244 mldv1_md = ntohs(mld->mld_maxdelay);
1245 *max_delay = max(msecs_to_jiffies(mldv1_md), 1UL);
1246
1247 mld_set_v1_mode(idev);
1248
1249 /* cancel MLDv2 report timer */
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001250 mld_gq_stop_timer(idev);
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001251 /* cancel the interface change timer */
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02001252 mld_ifc_stop_timer(idev);
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001253 /* clear deleted report items */
1254 mld_clear_delrec(idev);
1255
1256 return 0;
1257}
1258
1259static int mld_process_v2(struct inet6_dev *idev, struct mld2_query *mld,
1260 unsigned long *max_delay)
1261{
1262 /* hosts need to stay in MLDv1 mode, discard MLDv2 queries */
1263 if (mld_in_v1_mode(idev))
1264 return -EINVAL;
1265
1266 *max_delay = max(msecs_to_jiffies(mldv2_mrc(mld)), 1UL);
1267
1268 mld_update_qrv(idev, mld);
1269 mld_update_qi(idev, mld);
1270 mld_update_qri(idev, mld);
1271
1272 idev->mc_maxdelay = *max_delay;
1273
1274 return 0;
1275}
1276
Eric Dumazet96b52e62010-06-07 21:05:02 +00001277/* called with rcu_read_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278int igmp6_event_query(struct sk_buff *skb)
1279{
Yan Zheng97300b52005-10-31 20:09:45 +08001280 struct mld2_query *mlh2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 struct ifmcaddr6 *ma;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001282 const struct in6_addr *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 unsigned long max_delay;
1284 struct inet6_dev *idev;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001285 struct mld_msg *mld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 int group_type;
1287 int mark = 0;
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001288 int len, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1291 return -EINVAL;
1292
1293 /* compute payload length excluding extension headers */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001294 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001295 len -= skb_network_header_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Hangbin Liue940f5d2014-06-27 09:57:53 +08001297 /* RFC3810 6.2
1298 * Upon reception of an MLD message that contains a Query, the node
1299 * checks if the source address of the message is a valid link-local
1300 * address, if the Hop Limit is set to 1, and if the Router Alert
1301 * option is present in the Hop-By-Hop Options header of the IPv6
1302 * packet. If any of these checks fails, the packet is dropped.
1303 */
1304 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL) ||
1305 ipv6_hdr(skb)->hop_limit != 1 ||
1306 !(IP6CB(skb)->flags & IP6SKB_ROUTERALERT) ||
1307 IP6CB(skb)->ra != htons(IPV6_OPT_ROUTERALERT_MLD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return -EINVAL;
1309
Eric Dumazet96b52e62010-06-07 21:05:02 +00001310 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (idev == NULL)
1312 return 0;
1313
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001314 mld = (struct mld_msg *)icmp6_hdr(skb);
1315 group = &mld->mld_mca;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 group_type = ipv6_addr_type(group);
1317
1318 if (group_type != IPV6_ADDR_ANY &&
Eric Dumazet96b52e62010-06-07 21:05:02 +00001319 !(group_type&IPV6_ADDR_MULTICAST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Daniel Borkmann9fd07842013-08-20 12:22:02 +02001322 if (len == MLD_V1_QUERY_LEN) {
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001323 err = mld_process_v1(idev, mld, &max_delay);
1324 if (err < 0)
1325 return err;
Daniel Borkmann9fd07842013-08-20 12:22:02 +02001326 } else if (len >= MLD_V2_QUERY_LEN_MIN) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001327 int srcs_offset = sizeof(struct mld2_query) -
Yan Zheng97300b52005-10-31 20:09:45 +08001328 sizeof(struct icmp6hdr);
Daniel Borkmann89225d12013-09-04 00:19:37 +02001329
Eric Dumazet96b52e62010-06-07 21:05:02 +00001330 if (!pskb_may_pull(skb, srcs_offset))
Yan Zheng97300b52005-10-31 20:09:45 +08001331 return -EINVAL;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001332
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001333 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Daniel Borkmann84698962013-08-20 12:22:00 +02001334
Daniel Borkmann2b7c1212013-09-04 00:19:42 +02001335 err = mld_process_v2(idev, mlh2, &max_delay);
1336 if (err < 0)
1337 return err;
Daniel Borkmann89225d12013-09-04 00:19:37 +02001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (group_type == IPV6_ADDR_ANY) { /* general query */
Eric Dumazet96b52e62010-06-07 21:05:02 +00001340 if (mlh2->mld2q_nsrcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return -EINVAL; /* no sources allowed */
Eric Dumazet96b52e62010-06-07 21:05:02 +00001342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 mld_gq_start_timer(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return 0;
1345 }
1346 /* mark sources to include, if group & source-specific */
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001347 if (mlh2->mld2q_nsrcs != 0) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001348 if (!pskb_may_pull(skb, srcs_offset +
Eric Dumazet96b52e62010-06-07 21:05:02 +00001349 ntohs(mlh2->mld2q_nsrcs) * sizeof(struct in6_addr)))
Yan Zheng97300b52005-10-31 20:09:45 +08001350 return -EINVAL;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001351
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001352 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Yan Zheng97300b52005-10-31 20:09:45 +08001353 mark = 1;
1354 }
Eric Dumazet96b52e62010-06-07 21:05:02 +00001355 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 read_lock_bh(&idev->lock);
1359 if (group_type == IPV6_ADDR_ANY) {
Ian Morris67ba4152014-08-24 21:53:10 +01001360 for (ma = idev->mc_list; ma; ma = ma->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 spin_lock_bh(&ma->mca_lock);
1362 igmp6_group_queried(ma, max_delay);
1363 spin_unlock_bh(&ma->mca_lock);
1364 }
1365 } else {
Ian Morris67ba4152014-08-24 21:53:10 +01001366 for (ma = idev->mc_list; ma; ma = ma->next) {
David L Stevens7add2a42006-01-24 13:06:39 -08001367 if (!ipv6_addr_equal(group, &ma->mca_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 continue;
1369 spin_lock_bh(&ma->mca_lock);
1370 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1371 /* gsquery <- gsquery && mark */
1372 if (!mark)
1373 ma->mca_flags &= ~MAF_GSQUERY;
1374 } else {
1375 /* gsquery <- mark */
1376 if (mark)
1377 ma->mca_flags |= MAF_GSQUERY;
1378 else
1379 ma->mca_flags &= ~MAF_GSQUERY;
1380 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001381 if (!(ma->mca_flags & MAF_GSQUERY) ||
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001382 mld_marksources(ma, ntohs(mlh2->mld2q_nsrcs), mlh2->mld2q_srcs))
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001383 igmp6_group_queried(ma, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 spin_unlock_bh(&ma->mca_lock);
David L Stevens7add2a42006-01-24 13:06:39 -08001385 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387 }
1388 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 return 0;
1391}
1392
Eric Dumazet96b52e62010-06-07 21:05:02 +00001393/* called with rcu_read_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394int igmp6_event_report(struct sk_buff *skb)
1395{
1396 struct ifmcaddr6 *ma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 struct inet6_dev *idev;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001398 struct mld_msg *mld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 int addr_type;
1400
1401 /* Our own report looped back. Ignore it. */
1402 if (skb->pkt_type == PACKET_LOOPBACK)
1403 return 0;
1404
David Stevens24c69272005-12-02 20:32:59 -08001405 /* send our report if the MC router may not have heard this report */
1406 if (skb->pkt_type != PACKET_MULTICAST &&
1407 skb->pkt_type != PACKET_BROADCAST)
1408 return 0;
1409
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001410 if (!pskb_may_pull(skb, sizeof(*mld) - sizeof(struct icmp6hdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return -EINVAL;
1412
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001413 mld = (struct mld_msg *)icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 /* Drop reports with not link local source */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001416 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001417 if (addr_type != IPV6_ADDR_ANY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 !(addr_type&IPV6_ADDR_LINKLOCAL))
1419 return -EINVAL;
1420
Eric Dumazet96b52e62010-06-07 21:05:02 +00001421 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (idev == NULL)
1423 return -ENODEV;
1424
1425 /*
1426 * Cancel the timer for this group
1427 */
1428
1429 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +01001430 for (ma = idev->mc_list; ma; ma = ma->next) {
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001431 if (ipv6_addr_equal(&ma->mca_addr, &mld->mld_mca)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 spin_lock(&ma->mca_lock);
1433 if (del_timer(&ma->mca_timer))
1434 atomic_dec(&ma->mca_refcnt);
1435 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1436 spin_unlock(&ma->mca_lock);
1437 break;
1438 }
1439 }
1440 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return 0;
1442}
1443
Eric Dumazeta50feda2012-05-18 18:57:34 +00001444static bool is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1445 int gdeleted, int sdeleted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 switch (type) {
1448 case MLD2_MODE_IS_INCLUDE:
1449 case MLD2_MODE_IS_EXCLUDE:
1450 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001451 return false;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001452 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1453 if (pmc->mca_sfmode == MCAST_INCLUDE)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001454 return true;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001455 /* don't include if this source is excluded
1456 * in all filters
1457 */
1458 if (psf->sf_count[MCAST_INCLUDE])
David L Stevens7add2a42006-01-24 13:06:39 -08001459 return type == MLD2_MODE_IS_INCLUDE;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001460 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1461 psf->sf_count[MCAST_EXCLUDE];
1462 }
Eric Dumazeta50feda2012-05-18 18:57:34 +00001463 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 case MLD2_CHANGE_TO_INCLUDE:
1465 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001466 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return psf->sf_count[MCAST_INCLUDE] != 0;
1468 case MLD2_CHANGE_TO_EXCLUDE:
1469 if (gdeleted || sdeleted)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001470 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1472 psf->sf_count[MCAST_INCLUDE])
Eric Dumazeta50feda2012-05-18 18:57:34 +00001473 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1475 psf->sf_count[MCAST_EXCLUDE];
1476 case MLD2_ALLOW_NEW_SOURCES:
1477 if (gdeleted || !psf->sf_crcount)
Eric Dumazeta50feda2012-05-18 18:57:34 +00001478 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1480 case MLD2_BLOCK_OLD_SOURCES:
1481 if (pmc->mca_sfmode == MCAST_INCLUDE)
1482 return gdeleted || (psf->sf_crcount && sdeleted);
1483 return psf->sf_crcount && !gdeleted && !sdeleted;
1484 }
Eric Dumazeta50feda2012-05-18 18:57:34 +00001485 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
1488static int
1489mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1490{
1491 struct ip6_sf_list *psf;
1492 int scount = 0;
1493
Ian Morris67ba4152014-08-24 21:53:10 +01001494 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1496 continue;
1497 scount++;
1498 }
1499 return scount;
1500}
1501
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +00001502static void ip6_mc_hdr(struct sock *sk, struct sk_buff *skb,
1503 struct net_device *dev,
1504 const struct in6_addr *saddr,
1505 const struct in6_addr *daddr,
1506 int proto, int len)
1507{
1508 struct ipv6hdr *hdr;
1509
1510 skb->protocol = htons(ETH_P_IPV6);
1511 skb->dev = dev;
1512
1513 skb_reset_network_header(skb);
1514 skb_put(skb, sizeof(struct ipv6hdr));
1515 hdr = ipv6_hdr(skb);
1516
1517 ip6_flow_hdr(hdr, 0, 0);
1518
1519 hdr->payload_len = htons(len);
1520 hdr->nexthdr = proto;
1521 hdr->hop_limit = inet6_sk(sk)->hop_limit;
1522
1523 hdr->saddr = *saddr;
1524 hdr->daddr = *daddr;
1525}
1526
Amerigo Wang89657792013-06-29 21:30:49 +08001527static struct sk_buff *mld_newpack(struct inet6_dev *idev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
Amerigo Wang89657792013-06-29 21:30:49 +08001529 struct net_device *dev = idev->dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001530 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001531 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 struct sk_buff *skb;
1533 struct mld2_report *pmr;
1534 struct in6_addr addr_buf;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001535 const struct in6_addr *saddr;
Herbert Xua7ae1992011-11-18 02:20:04 +00001536 int hlen = LL_RESERVED_SPACE(dev);
1537 int tlen = dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 int err;
1539 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1540 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1541 IPV6_TLV_PADN, 0 };
1542
1543 /* we assume size > sizeof(ra) here */
Herbert Xua7ae1992011-11-18 02:20:04 +00001544 size += hlen + tlen;
Eric Dumazet72e09ad2010-06-05 03:03:30 -07001545 /* limit our allocations to order-0 page */
1546 size = min_t(int, size, SKB_MAX_ORDER(0, 0));
1547 skb = sock_alloc_send_skb(sk, size, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001549 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 return NULL;
1551
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +02001552 skb->priority = TC_PRIO_CONTROL;
Herbert Xua7ae1992011-11-18 02:20:04 +00001553 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Amerigo Wang89657792013-06-29 21:30:49 +08001555 if (__ipv6_get_lladdr(idev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001557 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 * when a valid link-local address is not available.
1559 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001560 saddr = &in6addr_any;
1561 } else
1562 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +00001564 ip6_mc_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1567
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001568 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -03001569 skb_put(skb, sizeof(*pmr));
1570 pmr = (struct mld2_report *)skb_transport_header(skb);
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001571 pmr->mld2r_type = ICMPV6_MLD2_REPORT;
1572 pmr->mld2r_resv1 = 0;
1573 pmr->mld2r_cksum = 0;
1574 pmr->mld2r_resv2 = 0;
1575 pmr->mld2r_ngrec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return skb;
1577}
1578
1579static void mld_sendpack(struct sk_buff *skb)
1580{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001581 struct ipv6hdr *pip6 = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001582 struct mld2_report *pmr =
1583 (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 int payload_len, mldlen;
Eric Dumazet96b52e62010-06-07 21:05:02 +00001585 struct inet6_dev *idev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001586 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 int err;
David S. Miller4c9483b2011-03-12 16:22:43 -05001588 struct flowi6 fl6;
Eric Dumazetadf30902009-06-02 05:19:30 +00001589 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Eric Dumazet96b52e62010-06-07 21:05:02 +00001591 rcu_read_lock();
1592 idev = __in6_dev_get(skb->dev);
Neil Hormanedf391f2009-04-27 02:45:02 -07001593 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
1594
Simon Horman29a3cad2013-05-28 20:34:26 +00001595 payload_len = (skb_tail_pointer(skb) - skb_network_header(skb)) -
1596 sizeof(*pip6);
1597 mldlen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 pip6->payload_len = htons(payload_len);
1599
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001600 pmr->mld2r_cksum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
1601 IPPROTO_ICMPV6,
1602 csum_partial(skb_transport_header(skb),
1603 mldlen, 0));
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001604
David S. Miller4c9483b2011-03-12 16:22:43 -05001605 icmpv6_flow_init(net->ipv6.igmp_sk, &fl6, ICMPV6_MLD2_REPORT,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001606 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1607 skb->dev->ifindex);
YOSHIFUJI Hideaki / 吉藤英明12fd84f2013-01-18 02:00:24 +00001608 dst = icmp6_dst_alloc(skb->dev, &fl6);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001609
David S. Miller452edd52011-03-02 13:27:41 -08001610 err = 0;
1611 if (IS_ERR(dst)) {
1612 err = PTR_ERR(dst);
1613 dst = NULL;
1614 }
Eric Dumazetadf30902009-06-02 05:19:30 +00001615 skb_dst_set(skb, dst);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001616 if (err)
1617 goto err_out;
1618
Neil Hormanedf391f2009-04-27 02:45:02 -07001619 payload_len = skb->len;
1620
Jan Engelhardtb2e0b382010-03-23 04:09:07 +01001621 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001622 dst_output);
1623out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (!err) {
Hannes Frederic Sowa43a43b62014-03-31 20:14:10 +02001625 ICMP6MSGOUT_INC_STATS(net, idev, ICMPV6_MLD2_REPORT);
1626 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
1627 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, payload_len);
1628 } else {
1629 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
1630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Eric Dumazet96b52e62010-06-07 21:05:02 +00001632 rcu_read_unlock();
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001633 return;
1634
1635err_out:
1636 kfree_skb(skb);
1637 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
1640static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1641{
Yan Zhengfab10fe2005-10-05 12:08:13 -07001642 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643}
1644
1645static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1646 int type, struct mld2_grec **ppgr)
1647{
1648 struct net_device *dev = pmc->idev->dev;
1649 struct mld2_report *pmr;
1650 struct mld2_grec *pgr;
1651
1652 if (!skb)
Amerigo Wang89657792013-06-29 21:30:49 +08001653 skb = mld_newpack(pmc->idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (!skb)
1655 return NULL;
1656 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1657 pgr->grec_type = type;
1658 pgr->grec_auxwords = 0;
1659 pgr->grec_nsrcs = 0;
1660 pgr->grec_mca = pmc->mca_addr; /* structure copy */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001661 pmr = (struct mld2_report *)skb_transport_header(skb);
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001662 pmr->mld2r_ngrec = htons(ntohs(pmr->mld2r_ngrec)+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 *ppgr = pgr;
1664 return skb;
1665}
1666
1667#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1668 skb_tailroom(skb)) : 0)
1669
1670static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001671 int type, int gdeleted, int sdeleted, int crsend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
Amerigo Wang89657792013-06-29 21:30:49 +08001673 struct inet6_dev *idev = pmc->idev;
1674 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 struct mld2_report *pmr;
1676 struct mld2_grec *pgr = NULL;
1677 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001678 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 if (pmc->mca_flags & MAF_NOREPORT)
1681 return skb;
1682
1683 isquery = type == MLD2_MODE_IS_INCLUDE ||
1684 type == MLD2_MODE_IS_EXCLUDE;
1685 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1686 type == MLD2_CHANGE_TO_EXCLUDE;
1687
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001688 stotal = scount = 0;
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1691
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001692 if (!*psf_list)
1693 goto empty_source;
1694
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001695 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 /* EX and TO_EX get a fresh packet, if needed */
1698 if (truncate) {
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001699 if (pmr && pmr->mld2r_ngrec &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1701 if (skb)
1702 mld_sendpack(skb);
Amerigo Wang89657792013-06-29 21:30:49 +08001703 skb = mld_newpack(idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
1705 }
1706 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 psf_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01001708 for (psf = *psf_list; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 struct in6_addr *psrc;
1710
1711 psf_next = psf->sf_next;
1712
1713 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1714 psf_prev = psf;
1715 continue;
1716 }
1717
1718 /* clear marks on query responses */
1719 if (isquery)
1720 psf->sf_gsresp = 0;
1721
1722 if (AVAILABLE(skb) < sizeof(*psrc) +
1723 first*sizeof(struct mld2_grec)) {
1724 if (truncate && !first)
1725 break; /* truncate these */
1726 if (pgr)
1727 pgr->grec_nsrcs = htons(scount);
1728 if (skb)
1729 mld_sendpack(skb);
Amerigo Wang89657792013-06-29 21:30:49 +08001730 skb = mld_newpack(idev, dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 first = 1;
1732 scount = 0;
1733 }
1734 if (first) {
1735 skb = add_grhead(skb, pmc, type, &pgr);
1736 first = 0;
1737 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -08001738 if (!skb)
1739 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1741 *psrc = psf->sf_addr;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001742 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1744 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1745 psf->sf_crcount--;
1746 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1747 if (psf_prev)
1748 psf_prev->sf_next = psf->sf_next;
1749 else
1750 *psf_list = psf->sf_next;
1751 kfree(psf);
1752 continue;
1753 }
1754 }
1755 psf_prev = psf;
1756 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001757
1758empty_source:
1759 if (!stotal) {
1760 if (type == MLD2_ALLOW_NEW_SOURCES ||
1761 type == MLD2_BLOCK_OLD_SOURCES)
1762 return skb;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001763 if (pmc->mca_crcount || isquery || crsend) {
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001764 /* make sure we have room for group header */
1765 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1766 mld_sendpack(skb);
1767 skb = NULL; /* add_grhead will get a new one */
1768 }
1769 skb = add_grhead(skb, pmc, type, &pgr);
1770 }
1771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 if (pgr)
1773 pgr->grec_nsrcs = htons(scount);
1774
1775 if (isquery)
1776 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1777 return skb;
1778}
1779
1780static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1781{
1782 struct sk_buff *skb = NULL;
1783 int type;
1784
Amerigo Wang89657792013-06-29 21:30:49 +08001785 read_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if (!pmc) {
Ian Morris67ba4152014-08-24 21:53:10 +01001787 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 if (pmc->mca_flags & MAF_NOREPORT)
1789 continue;
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 } else {
1799 spin_lock_bh(&pmc->mca_lock);
1800 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1801 type = MLD2_MODE_IS_EXCLUDE;
1802 else
1803 type = MLD2_MODE_IS_INCLUDE;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001804 skb = add_grec(skb, pmc, type, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 spin_unlock_bh(&pmc->mca_lock);
1806 }
Amerigo Wang89657792013-06-29 21:30:49 +08001807 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 if (skb)
1809 mld_sendpack(skb);
1810}
1811
1812/*
1813 * remove zero-count source records from a source filter list
1814 */
1815static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1816{
1817 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1818
1819 psf_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01001820 for (psf = *ppsf; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 psf_next = psf->sf_next;
1822 if (psf->sf_crcount == 0) {
1823 if (psf_prev)
1824 psf_prev->sf_next = psf->sf_next;
1825 else
1826 *ppsf = psf->sf_next;
1827 kfree(psf);
1828 } else
1829 psf_prev = psf;
1830 }
1831}
1832
1833static void mld_send_cr(struct inet6_dev *idev)
1834{
1835 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1836 struct sk_buff *skb = NULL;
1837 int type, dtype;
1838
1839 read_lock_bh(&idev->lock);
Stephen Hemminger6457d262010-02-17 18:48:44 -08001840 spin_lock(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 /* deleted MCA's */
1843 pmc_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01001844 for (pmc = idev->mc_tomb; pmc; pmc = pmc_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 pmc_next = pmc->next;
1846 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1847 type = MLD2_BLOCK_OLD_SOURCES;
1848 dtype = MLD2_BLOCK_OLD_SOURCES;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001849 skb = add_grec(skb, pmc, type, 1, 0, 0);
1850 skb = add_grec(skb, pmc, dtype, 1, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
1852 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1854 type = MLD2_CHANGE_TO_INCLUDE;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001855 skb = add_grec(skb, pmc, type, 1, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001857 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 if (pmc->mca_crcount == 0) {
1859 mld_clear_zeros(&pmc->mca_tomb);
1860 mld_clear_zeros(&pmc->mca_sources);
1861 }
1862 }
1863 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1864 !pmc->mca_sources) {
1865 if (pmc_prev)
1866 pmc_prev->next = pmc_next;
1867 else
1868 idev->mc_tomb = pmc_next;
1869 in6_dev_put(pmc->idev);
1870 kfree(pmc);
1871 } else
1872 pmc_prev = pmc;
1873 }
Stephen Hemminger6457d262010-02-17 18:48:44 -08001874 spin_unlock(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 /* change recs */
Ian Morris67ba4152014-08-24 21:53:10 +01001877 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 spin_lock_bh(&pmc->mca_lock);
1879 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1880 type = MLD2_BLOCK_OLD_SOURCES;
1881 dtype = MLD2_ALLOW_NEW_SOURCES;
1882 } else {
1883 type = MLD2_ALLOW_NEW_SOURCES;
1884 dtype = MLD2_BLOCK_OLD_SOURCES;
1885 }
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001886 skb = add_grec(skb, pmc, type, 0, 0, 0);
1887 skb = add_grec(skb, pmc, dtype, 0, 1, 0); /* deleted sources */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889 /* filter mode changes */
1890 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1892 type = MLD2_CHANGE_TO_EXCLUDE;
1893 else
1894 type = MLD2_CHANGE_TO_INCLUDE;
Flavio Leitner6a7cc412014-01-16 19:27:59 -02001895 skb = add_grec(skb, pmc, type, 0, 0, 0);
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001896 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
1898 spin_unlock_bh(&pmc->mca_lock);
1899 }
1900 read_unlock_bh(&idev->lock);
1901 if (!skb)
1902 return;
1903 (void) mld_sendpack(skb);
1904}
1905
1906static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1907{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001908 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001909 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 struct inet6_dev *idev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001911 struct sk_buff *skb;
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001912 struct mld_msg *hdr;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001913 const struct in6_addr *snd_addr, *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 struct in6_addr addr_buf;
Herbert Xua7ae1992011-11-18 02:20:04 +00001915 int hlen = LL_RESERVED_SPACE(dev);
1916 int tlen = dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 int err, len, payload_len, full_len;
1918 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1919 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1920 IPV6_TLV_PADN, 0 };
David S. Miller4c9483b2011-03-12 16:22:43 -05001921 struct flowi6 fl6;
Eric Dumazetadf30902009-06-02 05:19:30 +00001922 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09001924 if (type == ICMPV6_MGM_REDUCTION)
1925 snd_addr = &in6addr_linklocal_allrouters;
1926 else
1927 snd_addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1930 payload_len = len + sizeof(ra);
1931 full_len = sizeof(struct ipv6hdr) + payload_len;
1932
Neil Hormanedf391f2009-04-27 02:45:02 -07001933 rcu_read_lock();
1934 IP6_UPD_PO_STATS(net, __in6_dev_get(dev),
1935 IPSTATS_MIB_OUT, full_len);
1936 rcu_read_unlock();
1937
Herbert Xua7ae1992011-11-18 02:20:04 +00001938 skb = sock_alloc_send_skb(sk, hlen + tlen + full_len, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 if (skb == NULL) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001941 rcu_read_lock();
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001942 IP6_INC_STATS(net, __in6_dev_get(dev),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001943 IPSTATS_MIB_OUTDISCARDS);
1944 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 return;
1946 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +02001947 skb->priority = TC_PRIO_CONTROL;
Herbert Xua7ae1992011-11-18 02:20:04 +00001948 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Neil Horman95c385b2007-04-25 17:08:10 -07001950 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001952 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 * when a valid link-local address is not available.
1954 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001955 saddr = &in6addr_any;
1956 } else
1957 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
YOSHIFUJI Hideaki / 吉藤英明2576f172013-01-21 06:48:19 +00001959 ip6_mc_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1962
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001963 hdr = (struct mld_msg *) skb_put(skb, sizeof(struct mld_msg));
1964 memset(hdr, 0, sizeof(struct mld_msg));
1965 hdr->mld_type = type;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001966 hdr->mld_mca = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
YOSHIFUJI Hideaki6e7cb832010-04-18 12:42:05 +09001968 hdr->mld_cksum = csum_ipv6_magic(saddr, snd_addr, len,
1969 IPPROTO_ICMPV6,
1970 csum_partial(hdr, len, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Eric Dumazet96b52e62010-06-07 21:05:02 +00001972 rcu_read_lock();
1973 idev = __in6_dev_get(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
David S. Miller4c9483b2011-03-12 16:22:43 -05001975 icmpv6_flow_init(sk, &fl6, type,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001976 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1977 skb->dev->ifindex);
YOSHIFUJI Hideaki / 吉藤英明12fd84f2013-01-18 02:00:24 +00001978 dst = icmp6_dst_alloc(skb->dev, &fl6);
David S. Miller452edd52011-03-02 13:27:41 -08001979 if (IS_ERR(dst)) {
1980 err = PTR_ERR(dst);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001981 goto err_out;
David S. Miller452edd52011-03-02 13:27:41 -08001982 }
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001983
Eric Dumazetadf30902009-06-02 05:19:30 +00001984 skb_dst_set(skb, dst);
Jan Engelhardtb2e0b382010-03-23 04:09:07 +01001985 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001986 dst_output);
1987out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 if (!err) {
Denis V. Lunev5c5d2442008-10-08 10:33:50 -07001989 ICMP6MSGOUT_INC_STATS(net, idev, type);
Denis V. Luneva862f6a2008-10-08 10:33:06 -07001990 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
Neil Hormanedf391f2009-04-27 02:45:02 -07001991 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, full_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 } else
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001993 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Eric Dumazet96b52e62010-06-07 21:05:02 +00001995 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return;
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001997
1998err_out:
1999 kfree_skb(skb);
2000 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001}
2002
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002003static void mld_send_initial_cr(struct inet6_dev *idev)
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002004{
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002005 struct sk_buff *skb;
2006 struct ifmcaddr6 *pmc;
2007 int type;
2008
2009 if (mld_in_v1_mode(idev))
2010 return;
2011
2012 skb = NULL;
2013 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002014 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002015 spin_lock_bh(&pmc->mca_lock);
2016 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2017 type = MLD2_CHANGE_TO_EXCLUDE;
2018 else
2019 type = MLD2_CHANGE_TO_INCLUDE;
2020 skb = add_grec(skb, pmc, type, 0, 0, 1);
2021 spin_unlock_bh(&pmc->mca_lock);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002022 }
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002023 read_unlock_bh(&idev->lock);
2024 if (skb)
2025 mld_sendpack(skb);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002026}
2027
2028void ipv6_mc_dad_complete(struct inet6_dev *idev)
2029{
2030 idev->mc_dad_count = idev->mc_qrv;
2031 if (idev->mc_dad_count) {
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002032 mld_send_initial_cr(idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002033 idev->mc_dad_count--;
2034 if (idev->mc_dad_count)
2035 mld_dad_start_timer(idev, idev->mc_maxdelay);
2036 }
2037}
2038
2039static void mld_dad_timer_expire(unsigned long data)
2040{
2041 struct inet6_dev *idev = (struct inet6_dev *)data;
2042
Flavio Leitner6a7cc412014-01-16 19:27:59 -02002043 mld_send_initial_cr(idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002044 if (idev->mc_dad_count) {
2045 idev->mc_dad_count--;
2046 if (idev->mc_dad_count)
2047 mld_dad_start_timer(idev, idev->mc_maxdelay);
2048 }
Salam Noureddine9260d3e2013-09-29 13:41:34 -07002049 in6_dev_put(idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002050}
2051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002053 const struct in6_addr *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
2055 struct ip6_sf_list *psf, *psf_prev;
2056 int rv = 0;
2057
2058 psf_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01002059 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
2061 break;
2062 psf_prev = psf;
2063 }
2064 if (!psf || psf->sf_count[sfmode] == 0) {
2065 /* source filter not found, or count wrong => bug */
2066 return -ESRCH;
2067 }
2068 psf->sf_count[sfmode]--;
2069 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
2070 struct inet6_dev *idev = pmc->idev;
2071
2072 /* no more filters for this source */
2073 if (psf_prev)
2074 psf_prev->sf_next = psf->sf_next;
2075 else
2076 pmc->mca_sources = psf->sf_next;
2077 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
Daniel Borkmann6c567b72013-09-04 00:19:38 +02002078 !mld_in_v1_mode(idev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 psf->sf_crcount = idev->mc_qrv;
2080 psf->sf_next = pmc->mca_tomb;
2081 pmc->mca_tomb = psf;
2082 rv = 1;
2083 } else
2084 kfree(psf);
2085 }
2086 return rv;
2087}
2088
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002089static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2090 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 int delta)
2092{
2093 struct ifmcaddr6 *pmc;
2094 int changerec = 0;
2095 int i, err;
2096
2097 if (!idev)
2098 return -ENODEV;
2099 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002100 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2102 break;
2103 }
2104 if (!pmc) {
2105 /* MCA not found?? bug */
2106 read_unlock_bh(&idev->lock);
2107 return -ESRCH;
2108 }
2109 spin_lock_bh(&pmc->mca_lock);
2110 sf_markstate(pmc);
2111 if (!delta) {
2112 if (!pmc->mca_sfcount[sfmode]) {
2113 spin_unlock_bh(&pmc->mca_lock);
2114 read_unlock_bh(&idev->lock);
2115 return -EINVAL;
2116 }
2117 pmc->mca_sfcount[sfmode]--;
2118 }
2119 err = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01002120 for (i = 0; i < sfcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2122
2123 changerec |= rv > 0;
2124 if (!err && rv < 0)
2125 err = rv;
2126 }
2127 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
2128 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
2129 pmc->mca_sfcount[MCAST_INCLUDE]) {
2130 struct ip6_sf_list *psf;
2131
2132 /* filter mode change */
2133 pmc->mca_sfmode = MCAST_INCLUDE;
2134 pmc->mca_crcount = idev->mc_qrv;
2135 idev->mc_ifc_count = pmc->mca_crcount;
Ian Morris67ba4152014-08-24 21:53:10 +01002136 for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 psf->sf_crcount = 0;
2138 mld_ifc_event(pmc->idev);
2139 } else if (sf_setstate(pmc) || changerec)
2140 mld_ifc_event(pmc->idev);
2141 spin_unlock_bh(&pmc->mca_lock);
2142 read_unlock_bh(&idev->lock);
2143 return err;
2144}
2145
2146/*
2147 * Add multicast single-source filter to the interface list
2148 */
2149static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
Jun Zhao99d2f472011-11-30 06:21:05 +00002150 const struct in6_addr *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
2152 struct ip6_sf_list *psf, *psf_prev;
2153
2154 psf_prev = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01002155 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
2157 break;
2158 psf_prev = psf;
2159 }
2160 if (!psf) {
Ingo Oeser0c600ed2006-03-20 23:01:32 -08002161 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 if (!psf)
2163 return -ENOBUFS;
Ingo Oeser0c600ed2006-03-20 23:01:32 -08002164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 psf->sf_addr = *psfsrc;
2166 if (psf_prev) {
2167 psf_prev->sf_next = psf;
2168 } else
2169 pmc->mca_sources = psf;
2170 }
2171 psf->sf_count[sfmode]++;
2172 return 0;
2173}
2174
2175static void sf_markstate(struct ifmcaddr6 *pmc)
2176{
2177 struct ip6_sf_list *psf;
2178 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2179
Ian Morris67ba4152014-08-24 21:53:10 +01002180 for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2182 psf->sf_oldin = mca_xcount ==
2183 psf->sf_count[MCAST_EXCLUDE] &&
2184 !psf->sf_count[MCAST_INCLUDE];
2185 } else
2186 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2187}
2188
2189static int sf_setstate(struct ifmcaddr6 *pmc)
2190{
David L Stevens7add2a42006-01-24 13:06:39 -08002191 struct ip6_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2193 int qrv = pmc->idev->mc_qrv;
2194 int new_in, rv;
2195
2196 rv = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01002197 for (psf = pmc->mca_sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2199 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2200 !psf->sf_count[MCAST_INCLUDE];
2201 } else
2202 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevens7add2a42006-01-24 13:06:39 -08002203 if (new_in) {
2204 if (!psf->sf_oldin) {
Al Viroe80e28b2006-02-03 20:10:03 -05002205 struct ip6_sf_list *prev = NULL;
David L Stevens7add2a42006-01-24 13:06:39 -08002206
Ian Morris67ba4152014-08-24 21:53:10 +01002207 for (dpsf = pmc->mca_tomb; dpsf;
2208 dpsf = dpsf->sf_next) {
David L Stevens7add2a42006-01-24 13:06:39 -08002209 if (ipv6_addr_equal(&dpsf->sf_addr,
2210 &psf->sf_addr))
2211 break;
2212 prev = dpsf;
2213 }
2214 if (dpsf) {
2215 if (prev)
2216 prev->sf_next = dpsf->sf_next;
2217 else
2218 pmc->mca_tomb = dpsf->sf_next;
2219 kfree(dpsf);
2220 }
2221 psf->sf_crcount = qrv;
2222 rv++;
2223 }
2224 } else if (psf->sf_oldin) {
2225 psf->sf_crcount = 0;
2226 /*
2227 * add or update "delete" records if an active filter
2228 * is now inactive
2229 */
Ian Morris67ba4152014-08-24 21:53:10 +01002230 for (dpsf = pmc->mca_tomb; dpsf; dpsf = dpsf->sf_next)
David L Stevens7add2a42006-01-24 13:06:39 -08002231 if (ipv6_addr_equal(&dpsf->sf_addr,
2232 &psf->sf_addr))
2233 break;
2234 if (!dpsf) {
Joe Perches5d553542010-05-31 17:23:22 +00002235 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevens7add2a42006-01-24 13:06:39 -08002236 if (!dpsf)
2237 continue;
2238 *dpsf = *psf;
2239 /* pmc->mca_lock held by callers */
2240 dpsf->sf_next = pmc->mca_tomb;
2241 pmc->mca_tomb = dpsf;
2242 }
2243 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 rv++;
2245 }
2246 }
2247 return rv;
2248}
2249
2250/*
2251 * Add multicast source filter list to the interface list
2252 */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002253static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
2254 int sfmode, int sfcount, const struct in6_addr *psfsrc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 int delta)
2256{
2257 struct ifmcaddr6 *pmc;
2258 int isexclude;
2259 int i, err;
2260
2261 if (!idev)
2262 return -ENODEV;
2263 read_lock_bh(&idev->lock);
Ian Morris67ba4152014-08-24 21:53:10 +01002264 for (pmc = idev->mc_list; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2266 break;
2267 }
2268 if (!pmc) {
2269 /* MCA not found?? bug */
2270 read_unlock_bh(&idev->lock);
2271 return -ESRCH;
2272 }
2273 spin_lock_bh(&pmc->mca_lock);
2274
2275 sf_markstate(pmc);
2276 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2277 if (!delta)
2278 pmc->mca_sfcount[sfmode]++;
2279 err = 0;
Ian Morris67ba4152014-08-24 21:53:10 +01002280 for (i = 0; i < sfcount; i++) {
Jun Zhao99d2f472011-11-30 06:21:05 +00002281 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (err)
2283 break;
2284 }
2285 if (err) {
2286 int j;
2287
2288 if (!delta)
2289 pmc->mca_sfcount[sfmode]--;
Ian Morris67ba4152014-08-24 21:53:10 +01002290 for (j = 0; j < i; j++)
RongQing.Li78d50212012-04-04 16:47:04 +00002291 ip6_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 struct ip6_sf_list *psf;
2294
2295 /* filter mode change */
2296 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2297 pmc->mca_sfmode = MCAST_EXCLUDE;
2298 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2299 pmc->mca_sfmode = MCAST_INCLUDE;
2300 /* else no filters; keep old mode for reports */
2301
2302 pmc->mca_crcount = idev->mc_qrv;
2303 idev->mc_ifc_count = pmc->mca_crcount;
Ian Morris67ba4152014-08-24 21:53:10 +01002304 for (psf = pmc->mca_sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 psf->sf_crcount = 0;
2306 mld_ifc_event(idev);
2307 } else if (sf_setstate(pmc))
2308 mld_ifc_event(idev);
2309 spin_unlock_bh(&pmc->mca_lock);
2310 read_unlock_bh(&idev->lock);
2311 return err;
2312}
2313
2314static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2315{
2316 struct ip6_sf_list *psf, *nextpsf;
2317
Ian Morris67ba4152014-08-24 21:53:10 +01002318 for (psf = pmc->mca_tomb; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 nextpsf = psf->sf_next;
2320 kfree(psf);
2321 }
2322 pmc->mca_tomb = NULL;
Ian Morris67ba4152014-08-24 21:53:10 +01002323 for (psf = pmc->mca_sources; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 nextpsf = psf->sf_next;
2325 kfree(psf);
2326 }
2327 pmc->mca_sources = NULL;
2328 pmc->mca_sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002329 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2331}
2332
2333
2334static void igmp6_join_group(struct ifmcaddr6 *ma)
2335{
2336 unsigned long delay;
2337
2338 if (ma->mca_flags & MAF_NOREPORT)
2339 return;
2340
2341 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2342
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05002343 delay = prandom_u32() % unsolicited_report_interval(ma->idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
2345 spin_lock_bh(&ma->mca_lock);
2346 if (del_timer(&ma->mca_timer)) {
2347 atomic_dec(&ma->mca_refcnt);
2348 delay = ma->mca_timer.expires - jiffies;
2349 }
2350
2351 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2352 atomic_inc(&ma->mca_refcnt);
2353 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2354 spin_unlock_bh(&ma->mca_lock);
2355}
2356
2357static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2358 struct inet6_dev *idev)
2359{
2360 int err;
2361
David L Stevens5ab4a6c2005-12-27 14:03:00 -08002362 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2363 * so no other readers or writers of iml or its sflist
2364 */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002365 if (!iml->sflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 /* any-source empty exclude case */
2367 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2368 }
2369 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2370 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2371 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2372 iml->sflist = NULL;
2373 return err;
2374}
2375
2376static void igmp6_leave_group(struct ifmcaddr6 *ma)
2377{
Daniel Borkmann6c567b72013-09-04 00:19:38 +02002378 if (mld_in_v1_mode(ma->idev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if (ma->mca_flags & MAF_LAST_REPORTER)
2380 igmp6_send(&ma->mca_addr, ma->idev->dev,
2381 ICMPV6_MGM_REDUCTION);
2382 } else {
2383 mld_add_delrec(ma->idev, ma);
2384 mld_ifc_event(ma->idev);
2385 }
2386}
2387
2388static void mld_gq_timer_expire(unsigned long data)
2389{
2390 struct inet6_dev *idev = (struct inet6_dev *)data;
2391
2392 idev->mc_gq_running = 0;
2393 mld_send_report(idev, NULL);
Salam Noureddine9260d3e2013-09-29 13:41:34 -07002394 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395}
2396
2397static void mld_ifc_timer_expire(unsigned long data)
2398{
2399 struct inet6_dev *idev = (struct inet6_dev *)data;
2400
2401 mld_send_cr(idev);
2402 if (idev->mc_ifc_count) {
2403 idev->mc_ifc_count--;
2404 if (idev->mc_ifc_count)
2405 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2406 }
Salam Noureddine9260d3e2013-09-29 13:41:34 -07002407 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
2410static void mld_ifc_event(struct inet6_dev *idev)
2411{
Daniel Borkmann6c567b72013-09-04 00:19:38 +02002412 if (mld_in_v1_mode(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 return;
2414 idev->mc_ifc_count = idev->mc_qrv;
2415 mld_ifc_start_timer(idev, 1);
2416}
2417
2418
2419static void igmp6_timer_handler(unsigned long data)
2420{
2421 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2422
Daniel Borkmann6c567b72013-09-04 00:19:38 +02002423 if (mld_in_v1_mode(ma->idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2425 else
2426 mld_send_report(ma->idev, ma);
2427
2428 spin_lock(&ma->mca_lock);
2429 ma->mca_flags |= MAF_LAST_REPORTER;
2430 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2431 spin_unlock(&ma->mca_lock);
2432 ma_put(ma);
2433}
2434
Moni Shoua75c78502009-09-15 02:37:40 -07002435/* Device changing type */
2436
2437void ipv6_mc_unmap(struct inet6_dev *idev)
2438{
2439 struct ifmcaddr6 *i;
2440
2441 /* Install multicast list, except for all-nodes (already installed) */
2442
2443 read_lock_bh(&idev->lock);
2444 for (i = idev->mc_list; i; i = i->next)
2445 igmp6_group_dropped(i);
2446 read_unlock_bh(&idev->lock);
2447}
2448
2449void ipv6_mc_remap(struct inet6_dev *idev)
2450{
2451 ipv6_mc_up(idev);
2452}
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454/* Device going down */
2455
2456void ipv6_mc_down(struct inet6_dev *idev)
2457{
2458 struct ifmcaddr6 *i;
2459
2460 /* Withdraw multicast list */
2461
2462 read_lock_bh(&idev->lock);
Daniel Borkmannb4af8de2013-09-04 00:19:43 +02002463 mld_ifc_stop_timer(idev);
2464 mld_gq_stop_timer(idev);
2465 mld_dad_stop_timer(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Ian Morris67ba4152014-08-24 21:53:10 +01002467 for (i = idev->mc_list; i; i = i->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 igmp6_group_dropped(i);
2469 read_unlock_bh(&idev->lock);
2470
2471 mld_clear_delrec(idev);
2472}
2473
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02002474static void ipv6_mc_reset(struct inet6_dev *idev)
2475{
2476 idev->mc_qrv = sysctl_mld_qrv;
2477 idev->mc_qi = MLD_QI_DEFAULT;
2478 idev->mc_qri = MLD_QRI_DEFAULT;
2479 idev->mc_v1_seen = 0;
2480 idev->mc_maxdelay = unsolicited_report_interval(idev);
2481}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483/* Device going up */
2484
2485void ipv6_mc_up(struct inet6_dev *idev)
2486{
2487 struct ifmcaddr6 *i;
2488
2489 /* Install multicast list, except for all-nodes (already installed) */
2490
2491 read_lock_bh(&idev->lock);
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02002492 ipv6_mc_reset(idev);
Ian Morris67ba4152014-08-24 21:53:10 +01002493 for (i = idev->mc_list; i; i = i->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 igmp6_group_added(i);
2495 read_unlock_bh(&idev->lock);
2496}
2497
2498/* IPv6 device initialization. */
2499
2500void ipv6_mc_init_dev(struct inet6_dev *idev)
2501{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 write_lock_bh(&idev->lock);
Stephen Hemminger6457d262010-02-17 18:48:44 -08002503 spin_lock_init(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 idev->mc_gq_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002505 setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2506 (unsigned long)idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 idev->mc_tomb = NULL;
2508 idev->mc_ifc_count = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002509 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2510 (unsigned long)idev);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02002511 setup_timer(&idev->mc_dad_timer, mld_dad_timer_expire,
2512 (unsigned long)idev);
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +02002513 ipv6_mc_reset(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515}
2516
2517/*
2518 * Device is about to be destroyed: clean up.
2519 */
2520
2521void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2522{
2523 struct ifmcaddr6 *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
2525 /* Deactivate timers */
2526 ipv6_mc_down(idev);
2527
2528 /* Delete all-nodes address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2530 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2531 * fail.
2532 */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002533 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002535 if (idev->cnf.forwarding)
2536 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
2538 write_lock_bh(&idev->lock);
2539 while ((i = idev->mc_list) != NULL) {
2540 idev->mc_list = i->next;
2541 write_unlock_bh(&idev->lock);
2542
2543 igmp6_group_dropped(i);
2544 ma_put(i);
2545
2546 write_lock_bh(&idev->lock);
2547 }
2548 write_unlock_bh(&idev->lock);
2549}
2550
2551#ifdef CONFIG_PROC_FS
2552struct igmp6_mc_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002553 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 struct net_device *dev;
2555 struct inet6_dev *idev;
2556};
2557
2558#define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2559
2560static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2561{
2562 struct ifmcaddr6 *im = NULL;
2563 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002564 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Pavel Emelianov7562f872007-05-03 15:13:45 -07002566 state->idev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002567 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 struct inet6_dev *idev;
Eric Dumazetce81b762009-11-11 17:34:30 +00002569 idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 if (!idev)
2571 continue;
2572 read_lock_bh(&idev->lock);
2573 im = idev->mc_list;
2574 if (im) {
2575 state->idev = idev;
2576 break;
2577 }
2578 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 }
2580 return im;
2581}
2582
2583static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2584{
2585 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2586
2587 im = im->next;
2588 while (!im) {
Eric Dumazetce81b762009-11-11 17:34:30 +00002589 if (likely(state->idev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 read_unlock_bh(&state->idev->lock);
Eric Dumazetce81b762009-11-11 17:34:30 +00002591
2592 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 if (!state->dev) {
2594 state->idev = NULL;
2595 break;
2596 }
Eric Dumazetce81b762009-11-11 17:34:30 +00002597 state->idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 if (!state->idev)
2599 continue;
2600 read_lock_bh(&state->idev->lock);
2601 im = state->idev->mc_list;
2602 }
2603 return im;
2604}
2605
2606static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2607{
2608 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2609 if (im)
2610 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2611 --pos;
2612 return pos ? NULL : im;
2613}
2614
2615static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetce81b762009-11-11 17:34:30 +00002616 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617{
Eric Dumazetce81b762009-11-11 17:34:30 +00002618 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 return igmp6_mc_get_idx(seq, *pos);
2620}
2621
2622static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2623{
Eric Dumazetce81b762009-11-11 17:34:30 +00002624 struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);
2625
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 ++*pos;
2627 return im;
2628}
2629
2630static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetce81b762009-11-11 17:34:30 +00002631 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
2633 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
Eric Dumazetce81b762009-11-11 17:34:30 +00002634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 if (likely(state->idev != NULL)) {
2636 read_unlock_bh(&state->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 state->idev = NULL;
2638 }
2639 state->dev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002640 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
2642
2643static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2644{
2645 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2646 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2647
2648 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002649 "%-4d %-15s %pi6 %5d %08X %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 state->dev->ifindex, state->dev->name,
Harvey Harrisonb0711952008-10-28 16:05:40 -07002651 &im->mca_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 im->mca_users, im->mca_flags,
2653 (im->mca_flags&MAF_TIMER_RUNNING) ?
2654 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2655 return 0;
2656}
2657
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002658static const struct seq_operations igmp6_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 .start = igmp6_mc_seq_start,
2660 .next = igmp6_mc_seq_next,
2661 .stop = igmp6_mc_seq_stop,
2662 .show = igmp6_mc_seq_show,
2663};
2664
2665static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2666{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002667 return seq_open_net(inode, file, &igmp6_mc_seq_ops,
2668 sizeof(struct igmp6_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669}
2670
Arjan van de Ven9a321442007-02-12 00:55:35 -08002671static const struct file_operations igmp6_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 .owner = THIS_MODULE,
2673 .open = igmp6_mc_seq_open,
2674 .read = seq_read,
2675 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002676 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677};
2678
2679struct igmp6_mcf_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002680 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 struct net_device *dev;
2682 struct inet6_dev *idev;
2683 struct ifmcaddr6 *im;
2684};
2685
2686#define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2687
2688static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2689{
2690 struct ip6_sf_list *psf = NULL;
2691 struct ifmcaddr6 *im = NULL;
2692 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002693 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Pavel Emelianov7562f872007-05-03 15:13:45 -07002695 state->idev = NULL;
2696 state->im = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002697 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 struct inet6_dev *idev;
Eric Dumazetce81b762009-11-11 17:34:30 +00002699 idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 if (unlikely(idev == NULL))
2701 continue;
2702 read_lock_bh(&idev->lock);
2703 im = idev->mc_list;
2704 if (likely(im != NULL)) {
2705 spin_lock_bh(&im->mca_lock);
2706 psf = im->mca_sources;
2707 if (likely(psf != NULL)) {
2708 state->im = im;
2709 state->idev = idev;
2710 break;
2711 }
2712 spin_unlock_bh(&im->mca_lock);
2713 }
2714 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 }
2716 return psf;
2717}
2718
2719static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2720{
2721 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2722
2723 psf = psf->sf_next;
2724 while (!psf) {
2725 spin_unlock_bh(&state->im->mca_lock);
2726 state->im = state->im->next;
2727 while (!state->im) {
Eric Dumazetce81b762009-11-11 17:34:30 +00002728 if (likely(state->idev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 read_unlock_bh(&state->idev->lock);
Eric Dumazetce81b762009-11-11 17:34:30 +00002730
2731 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 if (!state->dev) {
2733 state->idev = NULL;
2734 goto out;
2735 }
Eric Dumazetce81b762009-11-11 17:34:30 +00002736 state->idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 if (!state->idev)
2738 continue;
2739 read_lock_bh(&state->idev->lock);
2740 state->im = state->idev->mc_list;
2741 }
2742 if (!state->im)
2743 break;
2744 spin_lock_bh(&state->im->mca_lock);
2745 psf = state->im->mca_sources;
2746 }
2747out:
2748 return psf;
2749}
2750
2751static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2752{
2753 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2754 if (psf)
2755 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2756 --pos;
2757 return pos ? NULL : psf;
2758}
2759
2760static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetce81b762009-11-11 17:34:30 +00002761 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762{
Eric Dumazetce81b762009-11-11 17:34:30 +00002763 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2765}
2766
2767static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2768{
2769 struct ip6_sf_list *psf;
2770 if (v == SEQ_START_TOKEN)
2771 psf = igmp6_mcf_get_first(seq);
2772 else
2773 psf = igmp6_mcf_get_next(seq, v);
2774 ++*pos;
2775 return psf;
2776}
2777
2778static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetce81b762009-11-11 17:34:30 +00002779 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780{
2781 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2782 if (likely(state->im != NULL)) {
2783 spin_unlock_bh(&state->im->mca_lock);
2784 state->im = NULL;
2785 }
2786 if (likely(state->idev != NULL)) {
2787 read_unlock_bh(&state->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 state->idev = NULL;
2789 }
2790 state->dev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002791 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792}
2793
2794static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2795{
2796 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2797 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2798
2799 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002800 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 "%3s %6s "
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002802 "%32s %32s %6s %6s\n", "Idx",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 "Device", "Multicast Address",
2804 "Source Address", "INC", "EXC");
2805 } else {
2806 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002807 "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 state->dev->ifindex, state->dev->name,
Harvey Harrisonb0711952008-10-28 16:05:40 -07002809 &state->im->mca_addr,
2810 &psf->sf_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 psf->sf_count[MCAST_INCLUDE],
2812 psf->sf_count[MCAST_EXCLUDE]);
2813 }
2814 return 0;
2815}
2816
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002817static const struct seq_operations igmp6_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 .start = igmp6_mcf_seq_start,
2819 .next = igmp6_mcf_seq_next,
2820 .stop = igmp6_mcf_seq_stop,
2821 .show = igmp6_mcf_seq_show,
2822};
2823
2824static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2825{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002826 return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
2827 sizeof(struct igmp6_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828}
2829
Arjan van de Ven9a321442007-02-12 00:55:35 -08002830static const struct file_operations igmp6_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 .owner = THIS_MODULE,
2832 .open = igmp6_mcf_seq_open,
2833 .read = seq_read,
2834 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002835 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836};
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002837
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002838static int __net_init igmp6_proc_init(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002839{
2840 int err;
2841
2842 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002843 if (!proc_create("igmp6", S_IRUGO, net->proc_net, &igmp6_mc_seq_fops))
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002844 goto out;
Gao fengd4beaa62013-02-18 01:34:54 +00002845 if (!proc_create("mcfilter6", S_IRUGO, net->proc_net,
2846 &igmp6_mcf_seq_fops))
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002847 goto out_proc_net_igmp6;
2848
2849 err = 0;
2850out:
2851 return err;
2852
2853out_proc_net_igmp6:
Gao fengece31ff2013-02-18 01:34:56 +00002854 remove_proc_entry("igmp6", net->proc_net);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002855 goto out;
2856}
2857
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002858static void __net_exit igmp6_proc_exit(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002859{
Gao fengece31ff2013-02-18 01:34:56 +00002860 remove_proc_entry("mcfilter6", net->proc_net);
2861 remove_proc_entry("igmp6", net->proc_net);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002862}
2863#else
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002864static inline int igmp6_proc_init(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002865{
2866 return 0;
2867}
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002868static inline void igmp6_proc_exit(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002869{
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002870}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871#endif
2872
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002873static int __net_init igmp6_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 int err;
2876
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002877 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2878 SOCK_RAW, IPPROTO_ICMPV6, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00002880 pr_err("Failed to initialize the IGMP6 control socket (err %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 err);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002882 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 }
2884
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002885 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002887 err = igmp6_proc_init(net);
2888 if (err)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002889 goto out_sock_create;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002890out:
2891 return err;
2892
2893out_sock_create:
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002894 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002895 goto out;
2896}
2897
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002898static void __net_exit igmp6_net_exit(struct net *net)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002899{
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002900 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002901 igmp6_proc_exit(net);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002902}
2903
2904static struct pernet_operations igmp6_net_ops = {
2905 .init = igmp6_net_init,
2906 .exit = igmp6_net_exit,
2907};
2908
2909int __init igmp6_init(void)
2910{
2911 return register_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912}
2913
2914void igmp6_cleanup(void)
2915{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002916 unregister_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917}