blob: c483ab9fd67b494286f2dfd7574fed4fa91792a3 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <linux/netfilter.h>
49#include <linux/netfilter_ipv6.h>
50
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020051#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <net/sock.h>
53#include <net/snmp.h>
54
55#include <net/ipv6.h>
56#include <net/protocol.h>
57#include <net/if_inet6.h>
58#include <net/ndisc.h>
59#include <net/addrconf.h>
60#include <net/ip6_route.h>
Denis V. Lunev1ed85162008-04-03 14:31:03 -070061#include <net/inet_common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#include <net/ip6_checksum.h>
64
65/* Set to 3 to get tracing... */
66#define MCAST_DEBUG 2
67
68#if MCAST_DEBUG >= 3
69#define MDBG(x) printk x
70#else
71#define MDBG(x)
72#endif
73
74/*
75 * These header formats should be in a separate include file, but icmpv6.h
76 * doesn't have in6_addr defined in all cases, there is no __u128, and no
77 * other files reference these.
78 *
79 * +-DLS 4/14/03
80 */
81
82/* Multicast Listener Discovery version 2 headers */
83
84struct mld2_grec {
85 __u8 grec_type;
86 __u8 grec_auxwords;
Al Viro8a74ff72006-11-08 00:22:08 -080087 __be16 grec_nsrcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 struct in6_addr grec_mca;
89 struct in6_addr grec_src[0];
90};
91
92struct mld2_report {
93 __u8 type;
94 __u8 resv1;
Al Viro868c86b2006-11-14 21:35:48 -080095 __sum16 csum;
Al Viro8a74ff72006-11-08 00:22:08 -080096 __be16 resv2;
97 __be16 ngrec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 struct mld2_grec grec[0];
99};
100
101struct mld2_query {
102 __u8 type;
103 __u8 code;
Al Viro868c86b2006-11-14 21:35:48 -0800104 __sum16 csum;
Al Viro8a74ff72006-11-08 00:22:08 -0800105 __be16 mrc;
106 __be16 resv1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 struct in6_addr mca;
108#if defined(__LITTLE_ENDIAN_BITFIELD)
109 __u8 qrv:3,
110 suppress:1,
111 resv2:4;
112#elif defined(__BIG_ENDIAN_BITFIELD)
113 __u8 resv2:4,
114 suppress:1,
115 qrv:3;
116#else
117#error "Please fix <asm/byteorder.h>"
118#endif
119 __u8 qqic;
Al Viro8a74ff72006-11-08 00:22:08 -0800120 __be16 nsrcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 struct in6_addr srcs[0];
122};
123
124static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
125
126/* Big mc list lock for all the sockets */
127static DEFINE_RWLOCK(ipv6_sk_mc_lock);
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static void igmp6_join_group(struct ifmcaddr6 *ma);
130static void igmp6_leave_group(struct ifmcaddr6 *ma);
131static void igmp6_timer_handler(unsigned long data);
132
133static void mld_gq_timer_expire(unsigned long data);
134static void mld_ifc_timer_expire(unsigned long data);
135static void mld_ifc_event(struct inet6_dev *idev);
136static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
137static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *addr);
138static void mld_clear_delrec(struct inet6_dev *idev);
139static int sf_setstate(struct ifmcaddr6 *pmc);
140static void sf_markstate(struct ifmcaddr6 *pmc);
141static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
142static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
143 int sfmode, int sfcount, struct in6_addr *psfsrc,
144 int delta);
145static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
146 int sfmode, int sfcount, struct in6_addr *psfsrc,
147 int delta);
148static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
149 struct inet6_dev *idev);
150
151
152#define IGMP6_UNSOLICITED_IVAL (10*HZ)
153#define MLD_QRV_DEFAULT 2
154
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700155#define MLD_V1_SEEN(idev) (dev_net((idev)->dev)->ipv6.devconf_all->force_mld_version == 1 || \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 (idev)->cnf.force_mld_version == 1 || \
157 ((idev)->mc_v1_seen && \
158 time_before(jiffies, (idev)->mc_v1_seen)))
159
160#define MLDV2_MASK(value, nb) ((nb)>=32 ? (value) : ((1<<(nb))-1) & (value))
161#define MLDV2_EXP(thresh, nbmant, nbexp, value) \
162 ((value) < (thresh) ? (value) : \
Yan Zhengf12baea2005-10-29 00:02:32 +0800163 ((MLDV2_MASK(value, nbmant) | (1<<(nbmant))) << \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 (MLDV2_MASK((value) >> (nbmant), nbexp) + (nbexp))))
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#define MLDV2_MRC(value) MLDV2_EXP(0x8000, 12, 3, value)
167
David L Stevens6f4353d2005-12-26 17:03:46 -0800168#define IPV6_MLD_MAX_MSF 64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Brian Haleyab32ea52006-09-22 14:15:41 -0700170int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172/*
173 * socket join on multicast group
174 */
175
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900176int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 struct net_device *dev = NULL;
179 struct ipv6_mc_socklist *mc_lst;
180 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900181 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 int err;
183
184 if (!ipv6_addr_is_multicast(addr))
185 return -EINVAL;
186
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700187 read_lock_bh(&ipv6_sk_mc_lock);
188 for (mc_lst=np->ipv6_mc_list; mc_lst; mc_lst=mc_lst->next) {
189 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
190 ipv6_addr_equal(&mc_lst->addr, addr)) {
191 read_unlock_bh(&ipv6_sk_mc_lock);
192 return -EADDRINUSE;
193 }
194 }
195 read_unlock_bh(&ipv6_sk_mc_lock);
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
198
199 if (mc_lst == NULL)
200 return -ENOMEM;
201
202 mc_lst->next = NULL;
203 ipv6_addr_copy(&mc_lst->addr, addr);
204
205 if (ifindex == 0) {
206 struct rt6_info *rt;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800207 rt = rt6_lookup(net, addr, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (rt) {
209 dev = rt->rt6i_dev;
210 dev_hold(dev);
211 dst_release(&rt->u.dst);
212 }
213 } else
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800214 dev = dev_get_by_index(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 if (dev == NULL) {
217 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
218 return -ENODEV;
219 }
220
221 mc_lst->ifindex = dev->ifindex;
222 mc_lst->sfmode = MCAST_EXCLUDE;
YOSHIFUJI Hideaki196433c2006-01-04 13:56:31 -0800223 rwlock_init(&mc_lst->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 mc_lst->sflist = NULL;
225
226 /*
227 * now add/increase the group membership on the device
228 */
229
230 err = ipv6_dev_mc_inc(dev, addr);
231
232 if (err) {
233 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
234 dev_put(dev);
235 return err;
236 }
237
238 write_lock_bh(&ipv6_sk_mc_lock);
239 mc_lst->next = np->ipv6_mc_list;
240 np->ipv6_mc_list = mc_lst;
241 write_unlock_bh(&ipv6_sk_mc_lock);
242
243 dev_put(dev);
244
245 return 0;
246}
247
248/*
249 * socket leave on multicast group
250 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900251int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 struct ipv6_pinfo *np = inet6_sk(sk);
254 struct ipv6_mc_socklist *mc_lst, **lnk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900255 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 write_lock_bh(&ipv6_sk_mc_lock);
258 for (lnk = &np->ipv6_mc_list; (mc_lst = *lnk) !=NULL ; lnk = &mc_lst->next) {
259 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
260 ipv6_addr_equal(&mc_lst->addr, addr)) {
261 struct net_device *dev;
262
263 *lnk = mc_lst->next;
264 write_unlock_bh(&ipv6_sk_mc_lock);
265
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800266 dev = dev_get_by_index(net, mc_lst->ifindex);
267 if (dev != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct inet6_dev *idev = in6_dev_get(dev);
269
David L Stevensacd6e002006-08-17 16:27:39 -0700270 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
273 in6_dev_put(idev);
274 }
275 dev_put(dev);
David L Stevensacd6e002006-08-17 16:27:39 -0700276 } else
277 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
279 return 0;
280 }
281 }
282 write_unlock_bh(&ipv6_sk_mc_lock);
283
David L Stevens9951f032005-07-08 17:47:28 -0700284 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800287static struct inet6_dev *ip6_mc_find_dev(struct net *net,
288 struct in6_addr *group,
289 int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 struct net_device *dev = NULL;
292 struct inet6_dev *idev = NULL;
293
294 if (ifindex == 0) {
295 struct rt6_info *rt;
296
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800297 rt = rt6_lookup(net, group, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (rt) {
299 dev = rt->rt6i_dev;
300 dev_hold(dev);
301 dst_release(&rt->u.dst);
302 }
303 } else
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800304 dev = dev_get_by_index(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 if (!dev)
Ilpo Järvinen448eb712008-12-14 23:15:21 -0800307 goto nodev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 idev = in6_dev_get(dev);
Ilpo Järvinen448eb712008-12-14 23:15:21 -0800309 if (!idev)
310 goto release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 read_lock_bh(&idev->lock);
Ilpo Järvinen448eb712008-12-14 23:15:21 -0800312 if (idev->dead)
313 goto unlock_release;
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return idev;
Ilpo Järvinen448eb712008-12-14 23:15:21 -0800316
317unlock_release:
318 read_unlock_bh(&idev->lock);
319 in6_dev_put(idev);
320release:
321 dev_put(dev);
322nodev:
323 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326void ipv6_sock_mc_close(struct sock *sk)
327{
328 struct ipv6_pinfo *np = inet6_sk(sk);
329 struct ipv6_mc_socklist *mc_lst;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900330 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 write_lock_bh(&ipv6_sk_mc_lock);
333 while ((mc_lst = np->ipv6_mc_list) != NULL) {
334 struct net_device *dev;
335
336 np->ipv6_mc_list = mc_lst->next;
337 write_unlock_bh(&ipv6_sk_mc_lock);
338
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800339 dev = dev_get_by_index(net, mc_lst->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (dev) {
341 struct inet6_dev *idev = in6_dev_get(dev);
342
David L Stevensacd6e002006-08-17 16:27:39 -0700343 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
346 in6_dev_put(idev);
347 }
348 dev_put(dev);
David L Stevensacd6e002006-08-17 16:27:39 -0700349 } else
350 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
353
354 write_lock_bh(&ipv6_sk_mc_lock);
355 }
356 write_unlock_bh(&ipv6_sk_mc_lock);
357}
358
359int ip6_mc_source(int add, int omode, struct sock *sk,
360 struct group_source_req *pgsr)
361{
362 struct in6_addr *source, *group;
363 struct ipv6_mc_socklist *pmc;
364 struct net_device *dev;
365 struct inet6_dev *idev;
366 struct ipv6_pinfo *inet6 = inet6_sk(sk);
367 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900368 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 int i, j, rv;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700370 int leavegroup = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800371 int pmclocked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 int err;
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
375 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
376
377 if (!ipv6_addr_is_multicast(group))
378 return -EINVAL;
379
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800380 idev = ip6_mc_find_dev(net, group, pgsr->gsr_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (!idev)
382 return -ENODEV;
383 dev = idev->dev;
384
385 err = -EADDRNOTAVAIL;
386
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700387 read_lock_bh(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
389 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
390 continue;
391 if (ipv6_addr_equal(&pmc->addr, group))
392 break;
393 }
David L Stevens917f2f12005-07-08 17:45:16 -0700394 if (!pmc) { /* must have a prior join */
395 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* if a source filter was set, must be the same mode as before */
399 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -0700400 if (pmc->sfmode != omode) {
401 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 } else if (pmc->sfmode != omode) {
405 /* allow mode switches for empty-set filters */
406 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
407 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
408 pmc->sfmode = omode;
409 }
410
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800411 write_lock_bh(&pmc->sflock);
412 pmclocked = 1;
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 psl = pmc->sflist;
415 if (!add) {
416 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -0700417 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 rv = !0;
419 for (i=0; i<psl->sl_count; i++) {
420 rv = memcmp(&psl->sl_addr[i], source,
421 sizeof(struct in6_addr));
422 if (rv == 0)
423 break;
424 }
425 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -0700426 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700428 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
429 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
430 leavegroup = 1;
431 goto done;
432 }
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /* update the interface filter */
435 ip6_mc_del_src(idev, group, omode, 1, source, 1);
436
437 for (j=i+1; j<psl->sl_count; j++)
438 psl->sl_addr[j-1] = psl->sl_addr[j];
439 psl->sl_count--;
440 err = 0;
441 goto done;
442 }
443 /* else, add a new source to the filter */
444
445 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
446 err = -ENOBUFS;
447 goto done;
448 }
449 if (!psl || psl->sl_count == psl->sl_max) {
450 struct ip6_sf_socklist *newpsl;
451 int count = IP6_SFBLOCK;
452
453 if (psl)
454 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800455 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (!newpsl) {
457 err = -ENOBUFS;
458 goto done;
459 }
460 newpsl->sl_max = count;
461 newpsl->sl_count = count - IP6_SFBLOCK;
462 if (psl) {
463 for (i=0; i<psl->sl_count; i++)
464 newpsl->sl_addr[i] = psl->sl_addr[i];
465 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
466 }
467 pmc->sflist = psl = newpsl;
468 }
469 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
470 for (i=0; i<psl->sl_count; i++) {
471 rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
472 if (rv == 0)
473 break;
474 }
475 if (rv == 0) /* address already there is an error */
476 goto done;
477 for (j=psl->sl_count-1; j>=i; j--)
478 psl->sl_addr[j+1] = psl->sl_addr[j];
479 psl->sl_addr[i] = *source;
480 psl->sl_count++;
481 err = 0;
482 /* update the interface list */
483 ip6_mc_add_src(idev, group, omode, 1, source, 1);
484done:
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800485 if (pmclocked)
486 write_unlock_bh(&pmc->sflock);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700487 read_unlock_bh(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 read_unlock_bh(&idev->lock);
489 in6_dev_put(idev);
490 dev_put(dev);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700491 if (leavegroup)
492 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return err;
494}
495
496int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
497{
498 struct in6_addr *group;
499 struct ipv6_mc_socklist *pmc;
500 struct net_device *dev;
501 struct inet6_dev *idev;
502 struct ipv6_pinfo *inet6 = inet6_sk(sk);
503 struct ip6_sf_socklist *newpsl, *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900504 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -0700505 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 int i, err;
507
508 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
509
510 if (!ipv6_addr_is_multicast(group))
511 return -EINVAL;
512 if (gsf->gf_fmode != MCAST_INCLUDE &&
513 gsf->gf_fmode != MCAST_EXCLUDE)
514 return -EINVAL;
515
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800516 idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 if (!idev)
519 return -ENODEV;
520 dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
David S. Miller9c059892005-07-08 21:44:39 -0700522 err = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800523 read_lock_bh(&ipv6_sk_mc_lock);
524
David L Stevens9951f032005-07-08 17:47:28 -0700525 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
526 leavegroup = 1;
527 goto done;
528 }
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
531 if (pmc->ifindex != gsf->gf_interface)
532 continue;
533 if (ipv6_addr_equal(&pmc->addr, group))
534 break;
535 }
David L Stevens917f2f12005-07-08 17:45:16 -0700536 if (!pmc) { /* must have a prior join */
537 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (gsf->gf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800541 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
542 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (!newpsl) {
544 err = -ENOBUFS;
545 goto done;
546 }
547 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
548 for (i=0; i<newpsl->sl_count; ++i) {
549 struct sockaddr_in6 *psin6;
550
551 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
552 newpsl->sl_addr[i] = psin6->sin6_addr;
553 }
554 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
555 newpsl->sl_count, newpsl->sl_addr, 0);
556 if (err) {
557 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
558 goto done;
559 }
Yan Zheng8713dbf2005-10-28 08:02:08 +0800560 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +0800562 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
563 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800564
565 write_lock_bh(&pmc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 psl = pmc->sflist;
567 if (psl) {
568 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
569 psl->sl_count, psl->sl_addr, 0);
570 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
571 } else
572 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
573 pmc->sflist = newpsl;
574 pmc->sfmode = gsf->gf_fmode;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800575 write_unlock_bh(&pmc->sflock);
David L Stevens917f2f12005-07-08 17:45:16 -0700576 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577done:
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800578 read_unlock_bh(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 read_unlock_bh(&idev->lock);
580 in6_dev_put(idev);
581 dev_put(dev);
David L Stevens9951f032005-07-08 17:47:28 -0700582 if (leavegroup)
583 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return err;
585}
586
587int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
588 struct group_filter __user *optval, int __user *optlen)
589{
590 int err, i, count, copycount;
591 struct in6_addr *group;
592 struct ipv6_mc_socklist *pmc;
593 struct inet6_dev *idev;
594 struct net_device *dev;
595 struct ipv6_pinfo *inet6 = inet6_sk(sk);
596 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900597 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
600
601 if (!ipv6_addr_is_multicast(group))
602 return -EINVAL;
603
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800604 idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 if (!idev)
607 return -ENODEV;
608
609 dev = idev->dev;
610
611 err = -EADDRNOTAVAIL;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800612 /*
613 * changes to the ipv6_mc_list require the socket lock and
614 * a read lock on ip6_sk_mc_lock. We have the socket lock,
615 * so reading the list is safe.
616 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
619 if (pmc->ifindex != gsf->gf_interface)
620 continue;
621 if (ipv6_addr_equal(group, &pmc->addr))
622 break;
623 }
624 if (!pmc) /* must have a prior join */
625 goto done;
626 gsf->gf_fmode = pmc->sfmode;
627 psl = pmc->sflist;
628 count = psl ? psl->sl_count : 0;
629 read_unlock_bh(&idev->lock);
630 in6_dev_put(idev);
631 dev_put(dev);
632
633 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
634 gsf->gf_numsrc = count;
635 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
636 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
637 return -EFAULT;
638 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800639 /* changes to psl require the socket lock, a read lock on
640 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
641 * have the socket lock, so reading here is safe.
642 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 for (i=0; i<copycount; i++) {
644 struct sockaddr_in6 *psin6;
645 struct sockaddr_storage ss;
646
647 psin6 = (struct sockaddr_in6 *)&ss;
648 memset(&ss, 0, sizeof(ss));
649 psin6->sin6_family = AF_INET6;
650 psin6->sin6_addr = psl->sl_addr[i];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900651 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return -EFAULT;
653 }
654 return 0;
655done:
656 read_unlock_bh(&idev->lock);
657 in6_dev_put(idev);
658 dev_put(dev);
659 return err;
660}
661
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900662int inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
663 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 struct ipv6_pinfo *np = inet6_sk(sk);
666 struct ipv6_mc_socklist *mc;
667 struct ip6_sf_socklist *psl;
668 int rv = 1;
669
670 read_lock(&ipv6_sk_mc_lock);
671 for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
672 if (ipv6_addr_equal(&mc->addr, mc_addr))
673 break;
674 }
675 if (!mc) {
676 read_unlock(&ipv6_sk_mc_lock);
677 return 1;
678 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800679 read_lock(&mc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 psl = mc->sflist;
681 if (!psl) {
682 rv = mc->sfmode == MCAST_EXCLUDE;
683 } else {
684 int i;
685
686 for (i=0; i<psl->sl_count; i++) {
687 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
688 break;
689 }
690 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
691 rv = 0;
692 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
693 rv = 0;
694 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800695 read_unlock(&mc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 read_unlock(&ipv6_sk_mc_lock);
697
698 return rv;
699}
700
701static void ma_put(struct ifmcaddr6 *mc)
702{
703 if (atomic_dec_and_test(&mc->mca_refcnt)) {
704 in6_dev_put(mc->idev);
705 kfree(mc);
706 }
707}
708
709static void igmp6_group_added(struct ifmcaddr6 *mc)
710{
711 struct net_device *dev = mc->idev->dev;
712 char buf[MAX_ADDR_LEN];
713
714 spin_lock_bh(&mc->mca_lock);
715 if (!(mc->mca_flags&MAF_LOADED)) {
716 mc->mca_flags |= MAF_LOADED;
717 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
718 dev_mc_add(dev, buf, dev->addr_len, 0);
719 }
720 spin_unlock_bh(&mc->mca_lock);
721
722 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
723 return;
724
725 if (MLD_V1_SEEN(mc->idev)) {
726 igmp6_join_group(mc);
727 return;
728 }
729 /* else v2 */
730
731 mc->mca_crcount = mc->idev->mc_qrv;
732 mld_ifc_event(mc->idev);
733}
734
735static void igmp6_group_dropped(struct ifmcaddr6 *mc)
736{
737 struct net_device *dev = mc->idev->dev;
738 char buf[MAX_ADDR_LEN];
739
740 spin_lock_bh(&mc->mca_lock);
741 if (mc->mca_flags&MAF_LOADED) {
742 mc->mca_flags &= ~MAF_LOADED;
743 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
744 dev_mc_delete(dev, buf, dev->addr_len, 0);
745 }
746
747 if (mc->mca_flags & MAF_NOREPORT)
748 goto done;
749 spin_unlock_bh(&mc->mca_lock);
750
751 if (!mc->idev->dead)
752 igmp6_leave_group(mc);
753
754 spin_lock_bh(&mc->mca_lock);
755 if (del_timer(&mc->mca_timer))
756 atomic_dec(&mc->mca_refcnt);
757done:
758 ip6_mc_clear_src(mc);
759 spin_unlock_bh(&mc->mca_lock);
760}
761
762/*
763 * deleted ifmcaddr6 manipulation
764 */
765static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
766{
767 struct ifmcaddr6 *pmc;
768
769 /* this is an "ifmcaddr6" for convenience; only the fields below
770 * are actually used. In particular, the refcnt and users are not
771 * used for management of the delete list. Using the same structure
772 * for deleted items allows change reports to use common code with
773 * non-deleted or query-response MCA's.
774 */
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800775 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (!pmc)
777 return;
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 spin_lock_bh(&im->mca_lock);
780 spin_lock_init(&pmc->mca_lock);
781 pmc->idev = im->idev;
782 in6_dev_hold(idev);
783 pmc->mca_addr = im->mca_addr;
784 pmc->mca_crcount = idev->mc_qrv;
785 pmc->mca_sfmode = im->mca_sfmode;
786 if (pmc->mca_sfmode == MCAST_INCLUDE) {
787 struct ip6_sf_list *psf;
788
789 pmc->mca_tomb = im->mca_tomb;
790 pmc->mca_sources = im->mca_sources;
791 im->mca_tomb = im->mca_sources = NULL;
792 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
793 psf->sf_crcount = pmc->mca_crcount;
794 }
795 spin_unlock_bh(&im->mca_lock);
796
Stephen Hemminger6457d262010-02-17 18:48:44 -0800797 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 pmc->next = idev->mc_tomb;
799 idev->mc_tomb = pmc;
Stephen Hemminger6457d262010-02-17 18:48:44 -0800800 spin_unlock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
803static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
804{
805 struct ifmcaddr6 *pmc, *pmc_prev;
806 struct ip6_sf_list *psf, *psf_next;
807
Stephen Hemminger6457d262010-02-17 18:48:44 -0800808 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 pmc_prev = NULL;
810 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
811 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
812 break;
813 pmc_prev = pmc;
814 }
815 if (pmc) {
816 if (pmc_prev)
817 pmc_prev->next = pmc->next;
818 else
819 idev->mc_tomb = pmc->next;
820 }
Stephen Hemminger6457d262010-02-17 18:48:44 -0800821 spin_unlock_bh(&idev->mc_lock);
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (pmc) {
824 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
825 psf_next = psf->sf_next;
826 kfree(psf);
827 }
828 in6_dev_put(pmc->idev);
829 kfree(pmc);
830 }
831}
832
833static void mld_clear_delrec(struct inet6_dev *idev)
834{
835 struct ifmcaddr6 *pmc, *nextpmc;
836
Stephen Hemminger6457d262010-02-17 18:48:44 -0800837 spin_lock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 pmc = idev->mc_tomb;
839 idev->mc_tomb = NULL;
Stephen Hemminger6457d262010-02-17 18:48:44 -0800840 spin_unlock_bh(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 for (; pmc; pmc = nextpmc) {
843 nextpmc = pmc->next;
844 ip6_mc_clear_src(pmc);
845 in6_dev_put(pmc->idev);
846 kfree(pmc);
847 }
848
849 /* clear dead sources, too */
850 read_lock_bh(&idev->lock);
851 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
852 struct ip6_sf_list *psf, *psf_next;
853
854 spin_lock_bh(&pmc->mca_lock);
855 psf = pmc->mca_tomb;
856 pmc->mca_tomb = NULL;
857 spin_unlock_bh(&pmc->mca_lock);
858 for (; psf; psf=psf_next) {
859 psf_next = psf->sf_next;
860 kfree(psf);
861 }
862 }
863 read_unlock_bh(&idev->lock);
864}
865
866
867/*
868 * device multicast group inc (add if not found)
869 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900870int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 struct ifmcaddr6 *mc;
873 struct inet6_dev *idev;
874
875 idev = in6_dev_get(dev);
876
877 if (idev == NULL)
878 return -EINVAL;
879
880 write_lock_bh(&idev->lock);
881 if (idev->dead) {
882 write_unlock_bh(&idev->lock);
883 in6_dev_put(idev);
884 return -ENODEV;
885 }
886
887 for (mc = idev->mc_list; mc; mc = mc->next) {
888 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
889 mc->mca_users++;
890 write_unlock_bh(&idev->lock);
891 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
892 NULL, 0);
893 in6_dev_put(idev);
894 return 0;
895 }
896 }
897
898 /*
899 * not found: create a new one.
900 */
901
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800902 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 if (mc == NULL) {
905 write_unlock_bh(&idev->lock);
906 in6_dev_put(idev);
907 return -ENOMEM;
908 }
909
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800910 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 ipv6_addr_copy(&mc->mca_addr, addr);
913 mc->idev = idev;
914 mc->mca_users = 1;
915 /* mca_stamp should be updated upon changes */
916 mc->mca_cstamp = mc->mca_tstamp = jiffies;
917 atomic_set(&mc->mca_refcnt, 2);
918 spin_lock_init(&mc->mca_lock);
919
920 /* initial mode is (EX, empty) */
921 mc->mca_sfmode = MCAST_EXCLUDE;
922 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
923
924 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
925 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
926 mc->mca_flags |= MAF_NOREPORT;
927
928 mc->next = idev->mc_list;
929 idev->mc_list = mc;
930 write_unlock_bh(&idev->lock);
931
932 mld_del_delrec(idev, &mc->mca_addr);
933 igmp6_group_added(mc);
934 ma_put(mc);
935 return 0;
936}
937
938/*
939 * device multicast group del
940 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900941int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 struct ifmcaddr6 *ma, **map;
944
945 write_lock_bh(&idev->lock);
946 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
947 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
948 if (--ma->mca_users == 0) {
949 *map = ma->next;
950 write_unlock_bh(&idev->lock);
951
952 igmp6_group_dropped(ma);
953
954 ma_put(ma);
955 return 0;
956 }
957 write_unlock_bh(&idev->lock);
958 return 0;
959 }
960 }
961 write_unlock_bh(&idev->lock);
962
963 return -ENOENT;
964}
965
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900966int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
968 struct inet6_dev *idev = in6_dev_get(dev);
969 int err;
970
971 if (!idev)
972 return -ENODEV;
973
974 err = __ipv6_dev_mc_dec(idev, addr);
975
976 in6_dev_put(idev);
977
978 return err;
979}
980
981/*
982 * identify MLD packets for MLD filter exceptions
983 */
984int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
985{
986 struct icmp6hdr *pic;
987
988 if (nexthdr != IPPROTO_ICMPV6)
989 return 0;
990
991 if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
992 return 0;
993
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -0300994 pic = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 switch (pic->icmp6_type) {
997 case ICMPV6_MGM_QUERY:
998 case ICMPV6_MGM_REPORT:
999 case ICMPV6_MGM_REDUCTION:
1000 case ICMPV6_MLD2_REPORT:
1001 return 1;
1002 default:
1003 break;
1004 }
1005 return 0;
1006}
1007
1008/*
1009 * check if the interface/address pair is valid
1010 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001011int ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
1012 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 struct inet6_dev *idev;
1015 struct ifmcaddr6 *mc;
1016 int rv = 0;
1017
1018 idev = in6_dev_get(dev);
1019 if (idev) {
1020 read_lock_bh(&idev->lock);
1021 for (mc = idev->mc_list; mc; mc=mc->next) {
1022 if (ipv6_addr_equal(&mc->mca_addr, group))
1023 break;
1024 }
1025 if (mc) {
1026 if (src_addr && !ipv6_addr_any(src_addr)) {
1027 struct ip6_sf_list *psf;
1028
1029 spin_lock_bh(&mc->mca_lock);
1030 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
1031 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1032 break;
1033 }
1034 if (psf)
1035 rv = psf->sf_count[MCAST_INCLUDE] ||
1036 psf->sf_count[MCAST_EXCLUDE] !=
1037 mc->mca_sfcount[MCAST_EXCLUDE];
1038 else
1039 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
1040 spin_unlock_bh(&mc->mca_lock);
1041 } else
1042 rv = 1; /* don't filter unspecified source */
1043 }
1044 read_unlock_bh(&idev->lock);
1045 in6_dev_put(idev);
1046 }
1047 return rv;
1048}
1049
1050static void mld_gq_start_timer(struct inet6_dev *idev)
1051{
1052 int tv = net_random() % idev->mc_maxdelay;
1053
1054 idev->mc_gq_running = 1;
1055 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1056 in6_dev_hold(idev);
1057}
1058
1059static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
1060{
1061 int tv = net_random() % delay;
1062
1063 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1064 in6_dev_hold(idev);
1065}
1066
1067/*
1068 * IGMP handling (alias multicast ICMPv6 messages)
1069 */
1070
1071static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1072{
1073 unsigned long delay = resptime;
1074
1075 /* Do not start timer for these addresses */
1076 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1077 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1078 return;
1079
1080 if (del_timer(&ma->mca_timer)) {
1081 atomic_dec(&ma->mca_refcnt);
1082 delay = ma->mca_timer.expires - jiffies;
1083 }
1084
1085 if (delay >= resptime) {
1086 if (resptime)
1087 delay = net_random() % resptime;
1088 else
1089 delay = 1;
1090 }
1091 ma->mca_timer.expires = jiffies + delay;
1092 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1093 atomic_inc(&ma->mca_refcnt);
1094 ma->mca_flags |= MAF_TIMER_RUNNING;
1095}
1096
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001097/* mark EXCLUDE-mode sources */
1098static int mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 struct in6_addr *srcs)
1100{
1101 struct ip6_sf_list *psf;
1102 int i, scount;
1103
1104 scount = 0;
1105 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1106 if (scount == nsrcs)
1107 break;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001108 for (i=0; i<nsrcs; i++) {
1109 /* skip inactive filters */
1110 if (pmc->mca_sfcount[MCAST_INCLUDE] ||
1111 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1112 psf->sf_count[MCAST_EXCLUDE])
1113 continue;
1114 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1115 scount++;
1116 break;
1117 }
1118 }
1119 }
1120 pmc->mca_flags &= ~MAF_GSQUERY;
1121 if (scount == nsrcs) /* all sources excluded */
1122 return 0;
1123 return 1;
1124}
1125
1126static int mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1127 struct in6_addr *srcs)
1128{
1129 struct ip6_sf_list *psf;
1130 int i, scount;
1131
1132 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1133 return mld_xmarksources(pmc, nsrcs, srcs);
1134
1135 /* mark INCLUDE-mode sources */
1136
1137 scount = 0;
1138 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1139 if (scount == nsrcs)
1140 break;
1141 for (i=0; i<nsrcs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1143 psf->sf_gsresp = 1;
1144 scount++;
1145 break;
1146 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001149 if (!scount) {
1150 pmc->mca_flags &= ~MAF_GSQUERY;
1151 return 0;
1152 }
1153 pmc->mca_flags |= MAF_GSQUERY;
1154 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
1156
1157int igmp6_event_query(struct sk_buff *skb)
1158{
Yan Zheng97300b52005-10-31 20:09:45 +08001159 struct mld2_query *mlh2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 struct ifmcaddr6 *ma;
1161 struct in6_addr *group;
1162 unsigned long max_delay;
1163 struct inet6_dev *idev;
1164 struct icmp6hdr *hdr;
1165 int group_type;
1166 int mark = 0;
1167 int len;
1168
1169 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1170 return -EINVAL;
1171
1172 /* compute payload length excluding extension headers */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001173 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001174 len -= skb_network_header_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 /* Drop queries with not link local source */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001177 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return -EINVAL;
1179
1180 idev = in6_dev_get(skb->dev);
1181
1182 if (idev == NULL)
1183 return 0;
1184
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -03001185 hdr = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 group = (struct in6_addr *) (hdr + 1);
1187 group_type = ipv6_addr_type(group);
1188
1189 if (group_type != IPV6_ADDR_ANY &&
1190 !(group_type&IPV6_ADDR_MULTICAST)) {
1191 in6_dev_put(idev);
1192 return -EINVAL;
1193 }
1194
1195 if (len == 24) {
1196 int switchback;
1197 /* MLDv1 router present */
1198
1199 /* Translate milliseconds to jiffies */
1200 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
1201
1202 switchback = (idev->mc_qrv + 1) * max_delay;
1203 idev->mc_v1_seen = jiffies + switchback;
1204
1205 /* cancel the interface change timer */
1206 idev->mc_ifc_count = 0;
1207 if (del_timer(&idev->mc_ifc_timer))
1208 __in6_dev_put(idev);
1209 /* clear deleted report items */
1210 mld_clear_delrec(idev);
1211 } else if (len >= 28) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001212 int srcs_offset = sizeof(struct mld2_query) -
Yan Zheng97300b52005-10-31 20:09:45 +08001213 sizeof(struct icmp6hdr);
1214 if (!pskb_may_pull(skb, srcs_offset)) {
1215 in6_dev_put(idev);
1216 return -EINVAL;
1217 }
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001218 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
1220 if (!max_delay)
1221 max_delay = 1;
1222 idev->mc_maxdelay = max_delay;
1223 if (mlh2->qrv)
1224 idev->mc_qrv = mlh2->qrv;
1225 if (group_type == IPV6_ADDR_ANY) { /* general query */
1226 if (mlh2->nsrcs) {
1227 in6_dev_put(idev);
1228 return -EINVAL; /* no sources allowed */
1229 }
1230 mld_gq_start_timer(idev);
1231 in6_dev_put(idev);
1232 return 0;
1233 }
1234 /* mark sources to include, if group & source-specific */
Yan Zheng97300b52005-10-31 20:09:45 +08001235 if (mlh2->nsrcs != 0) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001236 if (!pskb_may_pull(skb, srcs_offset +
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001237 ntohs(mlh2->nsrcs) * sizeof(struct in6_addr))) {
Yan Zheng97300b52005-10-31 20:09:45 +08001238 in6_dev_put(idev);
1239 return -EINVAL;
1240 }
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001241 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Yan Zheng97300b52005-10-31 20:09:45 +08001242 mark = 1;
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 } else {
1245 in6_dev_put(idev);
1246 return -EINVAL;
1247 }
1248
1249 read_lock_bh(&idev->lock);
1250 if (group_type == IPV6_ADDR_ANY) {
1251 for (ma = idev->mc_list; ma; ma=ma->next) {
1252 spin_lock_bh(&ma->mca_lock);
1253 igmp6_group_queried(ma, max_delay);
1254 spin_unlock_bh(&ma->mca_lock);
1255 }
1256 } else {
1257 for (ma = idev->mc_list; ma; ma=ma->next) {
David L Stevens7add2a42006-01-24 13:06:39 -08001258 if (!ipv6_addr_equal(group, &ma->mca_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 continue;
1260 spin_lock_bh(&ma->mca_lock);
1261 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1262 /* gsquery <- gsquery && mark */
1263 if (!mark)
1264 ma->mca_flags &= ~MAF_GSQUERY;
1265 } else {
1266 /* gsquery <- mark */
1267 if (mark)
1268 ma->mca_flags |= MAF_GSQUERY;
1269 else
1270 ma->mca_flags &= ~MAF_GSQUERY;
1271 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001272 if (!(ma->mca_flags & MAF_GSQUERY) ||
David L Stevens7add2a42006-01-24 13:06:39 -08001273 mld_marksources(ma, ntohs(mlh2->nsrcs), mlh2->srcs))
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001274 igmp6_group_queried(ma, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 spin_unlock_bh(&ma->mca_lock);
David L Stevens7add2a42006-01-24 13:06:39 -08001276 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278 }
1279 read_unlock_bh(&idev->lock);
1280 in6_dev_put(idev);
1281
1282 return 0;
1283}
1284
1285
1286int igmp6_event_report(struct sk_buff *skb)
1287{
1288 struct ifmcaddr6 *ma;
1289 struct in6_addr *addrp;
1290 struct inet6_dev *idev;
1291 struct icmp6hdr *hdr;
1292 int addr_type;
1293
1294 /* Our own report looped back. Ignore it. */
1295 if (skb->pkt_type == PACKET_LOOPBACK)
1296 return 0;
1297
David Stevens24c69272005-12-02 20:32:59 -08001298 /* send our report if the MC router may not have heard this report */
1299 if (skb->pkt_type != PACKET_MULTICAST &&
1300 skb->pkt_type != PACKET_BROADCAST)
1301 return 0;
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1304 return -EINVAL;
1305
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -03001306 hdr = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 /* Drop reports with not link local source */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001309 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001310 if (addr_type != IPV6_ADDR_ANY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 !(addr_type&IPV6_ADDR_LINKLOCAL))
1312 return -EINVAL;
1313
1314 addrp = (struct in6_addr *) (hdr + 1);
1315
1316 idev = in6_dev_get(skb->dev);
1317 if (idev == NULL)
1318 return -ENODEV;
1319
1320 /*
1321 * Cancel the timer for this group
1322 */
1323
1324 read_lock_bh(&idev->lock);
1325 for (ma = idev->mc_list; ma; ma=ma->next) {
1326 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1327 spin_lock(&ma->mca_lock);
1328 if (del_timer(&ma->mca_timer))
1329 atomic_dec(&ma->mca_refcnt);
1330 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1331 spin_unlock(&ma->mca_lock);
1332 break;
1333 }
1334 }
1335 read_unlock_bh(&idev->lock);
1336 in6_dev_put(idev);
1337 return 0;
1338}
1339
1340static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1341 int gdeleted, int sdeleted)
1342{
1343 switch (type) {
1344 case MLD2_MODE_IS_INCLUDE:
1345 case MLD2_MODE_IS_EXCLUDE:
1346 if (gdeleted || sdeleted)
1347 return 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001348 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1349 if (pmc->mca_sfmode == MCAST_INCLUDE)
1350 return 1;
1351 /* don't include if this source is excluded
1352 * in all filters
1353 */
1354 if (psf->sf_count[MCAST_INCLUDE])
David L Stevens7add2a42006-01-24 13:06:39 -08001355 return type == MLD2_MODE_IS_INCLUDE;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001356 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1357 psf->sf_count[MCAST_EXCLUDE];
1358 }
1359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 case MLD2_CHANGE_TO_INCLUDE:
1361 if (gdeleted || sdeleted)
1362 return 0;
1363 return psf->sf_count[MCAST_INCLUDE] != 0;
1364 case MLD2_CHANGE_TO_EXCLUDE:
1365 if (gdeleted || sdeleted)
1366 return 0;
1367 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1368 psf->sf_count[MCAST_INCLUDE])
1369 return 0;
1370 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1371 psf->sf_count[MCAST_EXCLUDE];
1372 case MLD2_ALLOW_NEW_SOURCES:
1373 if (gdeleted || !psf->sf_crcount)
1374 return 0;
1375 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1376 case MLD2_BLOCK_OLD_SOURCES:
1377 if (pmc->mca_sfmode == MCAST_INCLUDE)
1378 return gdeleted || (psf->sf_crcount && sdeleted);
1379 return psf->sf_crcount && !gdeleted && !sdeleted;
1380 }
1381 return 0;
1382}
1383
1384static int
1385mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1386{
1387 struct ip6_sf_list *psf;
1388 int scount = 0;
1389
1390 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1391 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1392 continue;
1393 scount++;
1394 }
1395 return scount;
1396}
1397
1398static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1399{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001400 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001401 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 struct sk_buff *skb;
1403 struct mld2_report *pmr;
1404 struct in6_addr addr_buf;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001405 const struct in6_addr *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 int err;
1407 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1408 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1409 IPV6_TLV_PADN, 0 };
1410
1411 /* we assume size > sizeof(ra) here */
Johannes Bergf5184d22008-05-12 20:48:31 -07001412 skb = sock_alloc_send_skb(sk, size + LL_ALLOCATED_SPACE(dev), 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001414 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return NULL;
1416
1417 skb_reserve(skb, LL_RESERVED_SPACE(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Neil Horman95c385b2007-04-25 17:08:10 -07001419 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001421 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 * when a valid link-local address is not available.
1423 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001424 saddr = &in6addr_any;
1425 } else
1426 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001428 ip6_nd_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1431
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001432 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -03001433 skb_put(skb, sizeof(*pmr));
1434 pmr = (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 pmr->type = ICMPV6_MLD2_REPORT;
1436 pmr->resv1 = 0;
1437 pmr->csum = 0;
1438 pmr->resv2 = 0;
1439 pmr->ngrec = 0;
1440 return skb;
1441}
1442
1443static void mld_sendpack(struct sk_buff *skb)
1444{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001445 struct ipv6hdr *pip6 = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001446 struct mld2_report *pmr =
1447 (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 int payload_len, mldlen;
1449 struct inet6_dev *idev = in6_dev_get(skb->dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001450 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 int err;
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001452 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00001453 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Neil Hormanedf391f2009-04-27 02:45:02 -07001455 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
1456
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001457 payload_len = (skb->tail - skb->network_header) - sizeof(*pip6);
1458 mldlen = skb->tail - skb->transport_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 pip6->payload_len = htons(payload_len);
1460
1461 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001462 IPPROTO_ICMPV6, csum_partial(skb_transport_header(skb),
1463 mldlen, 0));
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001464
Eric Dumazetadf30902009-06-02 05:19:30 +00001465 dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001466
Eric Dumazetadf30902009-06-02 05:19:30 +00001467 if (!dst) {
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001468 err = -ENOMEM;
1469 goto err_out;
1470 }
1471
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001472 icmpv6_flow_init(net->ipv6.igmp_sk, &fl, ICMPV6_MLD2_REPORT,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001473 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1474 skb->dev->ifindex);
1475
Eric Dumazetadf30902009-06-02 05:19:30 +00001476 err = xfrm_lookup(net, &dst, &fl, NULL, 0);
1477 skb_dst_set(skb, dst);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001478 if (err)
1479 goto err_out;
1480
Neil Hormanedf391f2009-04-27 02:45:02 -07001481 payload_len = skb->len;
1482
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001483 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001484 dst_output);
1485out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 if (!err) {
Denis V. Lunev5a57d4c2008-10-08 10:34:14 -07001487 ICMP6MSGOUT_INC_STATS_BH(net, idev, ICMPV6_MLD2_REPORT);
Denis V. Luneve41b5362008-10-08 10:33:26 -07001488 ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
Neil Hormanedf391f2009-04-27 02:45:02 -07001489 IP6_UPD_PO_STATS_BH(net, idev, IPSTATS_MIB_OUTMCAST, payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 } else
Denis V. Lunev483a47d2008-10-08 11:09:27 -07001491 IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 if (likely(idev != NULL))
1494 in6_dev_put(idev);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001495 return;
1496
1497err_out:
1498 kfree_skb(skb);
1499 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
1502static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1503{
Yan Zhengfab10fe2005-10-05 12:08:13 -07001504 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
1506
1507static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1508 int type, struct mld2_grec **ppgr)
1509{
1510 struct net_device *dev = pmc->idev->dev;
1511 struct mld2_report *pmr;
1512 struct mld2_grec *pgr;
1513
1514 if (!skb)
1515 skb = mld_newpack(dev, dev->mtu);
1516 if (!skb)
1517 return NULL;
1518 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1519 pgr->grec_type = type;
1520 pgr->grec_auxwords = 0;
1521 pgr->grec_nsrcs = 0;
1522 pgr->grec_mca = pmc->mca_addr; /* structure copy */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001523 pmr = (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1525 *ppgr = pgr;
1526 return skb;
1527}
1528
1529#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1530 skb_tailroom(skb)) : 0)
1531
1532static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1533 int type, int gdeleted, int sdeleted)
1534{
1535 struct net_device *dev = pmc->idev->dev;
1536 struct mld2_report *pmr;
1537 struct mld2_grec *pgr = NULL;
1538 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001539 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 if (pmc->mca_flags & MAF_NOREPORT)
1542 return skb;
1543
1544 isquery = type == MLD2_MODE_IS_INCLUDE ||
1545 type == MLD2_MODE_IS_EXCLUDE;
1546 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1547 type == MLD2_CHANGE_TO_EXCLUDE;
1548
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001549 stotal = scount = 0;
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1552
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001553 if (!*psf_list)
1554 goto empty_source;
1555
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001556 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 /* EX and TO_EX get a fresh packet, if needed */
1559 if (truncate) {
1560 if (pmr && pmr->ngrec &&
1561 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1562 if (skb)
1563 mld_sendpack(skb);
1564 skb = mld_newpack(dev, dev->mtu);
1565 }
1566 }
1567 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 psf_prev = NULL;
1569 for (psf=*psf_list; psf; psf=psf_next) {
1570 struct in6_addr *psrc;
1571
1572 psf_next = psf->sf_next;
1573
1574 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1575 psf_prev = psf;
1576 continue;
1577 }
1578
1579 /* clear marks on query responses */
1580 if (isquery)
1581 psf->sf_gsresp = 0;
1582
1583 if (AVAILABLE(skb) < sizeof(*psrc) +
1584 first*sizeof(struct mld2_grec)) {
1585 if (truncate && !first)
1586 break; /* truncate these */
1587 if (pgr)
1588 pgr->grec_nsrcs = htons(scount);
1589 if (skb)
1590 mld_sendpack(skb);
1591 skb = mld_newpack(dev, dev->mtu);
1592 first = 1;
1593 scount = 0;
1594 }
1595 if (first) {
1596 skb = add_grhead(skb, pmc, type, &pgr);
1597 first = 0;
1598 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -08001599 if (!skb)
1600 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1602 *psrc = psf->sf_addr;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001603 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1605 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1606 psf->sf_crcount--;
1607 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1608 if (psf_prev)
1609 psf_prev->sf_next = psf->sf_next;
1610 else
1611 *psf_list = psf->sf_next;
1612 kfree(psf);
1613 continue;
1614 }
1615 }
1616 psf_prev = psf;
1617 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001618
1619empty_source:
1620 if (!stotal) {
1621 if (type == MLD2_ALLOW_NEW_SOURCES ||
1622 type == MLD2_BLOCK_OLD_SOURCES)
1623 return skb;
1624 if (pmc->mca_crcount || isquery) {
1625 /* make sure we have room for group header */
1626 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1627 mld_sendpack(skb);
1628 skb = NULL; /* add_grhead will get a new one */
1629 }
1630 skb = add_grhead(skb, pmc, type, &pgr);
1631 }
1632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 if (pgr)
1634 pgr->grec_nsrcs = htons(scount);
1635
1636 if (isquery)
1637 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1638 return skb;
1639}
1640
1641static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1642{
1643 struct sk_buff *skb = NULL;
1644 int type;
1645
1646 if (!pmc) {
1647 read_lock_bh(&idev->lock);
1648 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1649 if (pmc->mca_flags & MAF_NOREPORT)
1650 continue;
1651 spin_lock_bh(&pmc->mca_lock);
1652 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1653 type = MLD2_MODE_IS_EXCLUDE;
1654 else
1655 type = MLD2_MODE_IS_INCLUDE;
1656 skb = add_grec(skb, pmc, type, 0, 0);
1657 spin_unlock_bh(&pmc->mca_lock);
1658 }
1659 read_unlock_bh(&idev->lock);
1660 } else {
1661 spin_lock_bh(&pmc->mca_lock);
1662 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1663 type = MLD2_MODE_IS_EXCLUDE;
1664 else
1665 type = MLD2_MODE_IS_INCLUDE;
1666 skb = add_grec(skb, pmc, type, 0, 0);
1667 spin_unlock_bh(&pmc->mca_lock);
1668 }
1669 if (skb)
1670 mld_sendpack(skb);
1671}
1672
1673/*
1674 * remove zero-count source records from a source filter list
1675 */
1676static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1677{
1678 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1679
1680 psf_prev = NULL;
1681 for (psf=*ppsf; psf; psf = psf_next) {
1682 psf_next = psf->sf_next;
1683 if (psf->sf_crcount == 0) {
1684 if (psf_prev)
1685 psf_prev->sf_next = psf->sf_next;
1686 else
1687 *ppsf = psf->sf_next;
1688 kfree(psf);
1689 } else
1690 psf_prev = psf;
1691 }
1692}
1693
1694static void mld_send_cr(struct inet6_dev *idev)
1695{
1696 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1697 struct sk_buff *skb = NULL;
1698 int type, dtype;
1699
1700 read_lock_bh(&idev->lock);
Stephen Hemminger6457d262010-02-17 18:48:44 -08001701 spin_lock(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 /* deleted MCA's */
1704 pmc_prev = NULL;
1705 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1706 pmc_next = pmc->next;
1707 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1708 type = MLD2_BLOCK_OLD_SOURCES;
1709 dtype = MLD2_BLOCK_OLD_SOURCES;
1710 skb = add_grec(skb, pmc, type, 1, 0);
1711 skb = add_grec(skb, pmc, dtype, 1, 1);
1712 }
1713 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1715 type = MLD2_CHANGE_TO_INCLUDE;
1716 skb = add_grec(skb, pmc, type, 1, 0);
1717 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001718 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 if (pmc->mca_crcount == 0) {
1720 mld_clear_zeros(&pmc->mca_tomb);
1721 mld_clear_zeros(&pmc->mca_sources);
1722 }
1723 }
1724 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1725 !pmc->mca_sources) {
1726 if (pmc_prev)
1727 pmc_prev->next = pmc_next;
1728 else
1729 idev->mc_tomb = pmc_next;
1730 in6_dev_put(pmc->idev);
1731 kfree(pmc);
1732 } else
1733 pmc_prev = pmc;
1734 }
Stephen Hemminger6457d262010-02-17 18:48:44 -08001735 spin_unlock(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 /* change recs */
1738 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1739 spin_lock_bh(&pmc->mca_lock);
1740 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1741 type = MLD2_BLOCK_OLD_SOURCES;
1742 dtype = MLD2_ALLOW_NEW_SOURCES;
1743 } else {
1744 type = MLD2_ALLOW_NEW_SOURCES;
1745 dtype = MLD2_BLOCK_OLD_SOURCES;
1746 }
1747 skb = add_grec(skb, pmc, type, 0, 0);
1748 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1749
1750 /* filter mode changes */
1751 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1753 type = MLD2_CHANGE_TO_EXCLUDE;
1754 else
1755 type = MLD2_CHANGE_TO_INCLUDE;
1756 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001757 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
1759 spin_unlock_bh(&pmc->mca_lock);
1760 }
1761 read_unlock_bh(&idev->lock);
1762 if (!skb)
1763 return;
1764 (void) mld_sendpack(skb);
1765}
1766
1767static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1768{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001769 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001770 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 struct inet6_dev *idev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001772 struct sk_buff *skb;
1773 struct icmp6hdr *hdr;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001774 const struct in6_addr *snd_addr, *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 struct in6_addr *addrp;
1776 struct in6_addr addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 int err, len, payload_len, full_len;
1778 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1779 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1780 IPV6_TLV_PADN, 0 };
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001781 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00001782 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09001784 if (type == ICMPV6_MGM_REDUCTION)
1785 snd_addr = &in6addr_linklocal_allrouters;
1786 else
1787 snd_addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1790 payload_len = len + sizeof(ra);
1791 full_len = sizeof(struct ipv6hdr) + payload_len;
1792
Neil Hormanedf391f2009-04-27 02:45:02 -07001793 rcu_read_lock();
1794 IP6_UPD_PO_STATS(net, __in6_dev_get(dev),
1795 IPSTATS_MIB_OUT, full_len);
1796 rcu_read_unlock();
1797
Johannes Bergf5184d22008-05-12 20:48:31 -07001798 skb = sock_alloc_send_skb(sk, LL_ALLOCATED_SPACE(dev) + full_len, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 if (skb == NULL) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001801 rcu_read_lock();
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001802 IP6_INC_STATS(net, __in6_dev_get(dev),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001803 IPSTATS_MIB_OUTDISCARDS);
1804 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 return;
1806 }
1807
1808 skb_reserve(skb, LL_RESERVED_SPACE(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Neil Horman95c385b2007-04-25 17:08:10 -07001810 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001812 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 * when a valid link-local address is not available.
1814 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001815 saddr = &in6addr_any;
1816 } else
1817 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001819 ip6_nd_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1822
1823 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1824 memset(hdr, 0, sizeof(struct icmp6hdr));
1825 hdr->icmp6_type = type;
1826
1827 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1828 ipv6_addr_copy(addrp, addr);
1829
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001830 hdr->icmp6_cksum = csum_ipv6_magic(saddr, snd_addr, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 IPPROTO_ICMPV6,
Joe Perches07f07572008-11-19 15:44:53 -08001832 csum_partial(hdr, len, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 idev = in6_dev_get(skb->dev);
1835
Eric Dumazetadf30902009-06-02 05:19:30 +00001836 dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
1837 if (!dst) {
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001838 err = -ENOMEM;
1839 goto err_out;
1840 }
1841
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001842 icmpv6_flow_init(sk, &fl, type,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001843 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1844 skb->dev->ifindex);
1845
Eric Dumazetadf30902009-06-02 05:19:30 +00001846 err = xfrm_lookup(net, &dst, &fl, NULL, 0);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001847 if (err)
1848 goto err_out;
1849
Eric Dumazetadf30902009-06-02 05:19:30 +00001850 skb_dst_set(skb, dst);
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001851 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001852 dst_output);
1853out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (!err) {
Denis V. Lunev5c5d2442008-10-08 10:33:50 -07001855 ICMP6MSGOUT_INC_STATS(net, idev, type);
Denis V. Luneva862f6a2008-10-08 10:33:06 -07001856 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
Neil Hormanedf391f2009-04-27 02:45:02 -07001857 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, full_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 } else
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001859 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 if (likely(idev != NULL))
1862 in6_dev_put(idev);
1863 return;
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001864
1865err_out:
1866 kfree_skb(skb);
1867 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
1870static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1871 struct in6_addr *psfsrc)
1872{
1873 struct ip6_sf_list *psf, *psf_prev;
1874 int rv = 0;
1875
1876 psf_prev = NULL;
1877 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1878 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1879 break;
1880 psf_prev = psf;
1881 }
1882 if (!psf || psf->sf_count[sfmode] == 0) {
1883 /* source filter not found, or count wrong => bug */
1884 return -ESRCH;
1885 }
1886 psf->sf_count[sfmode]--;
1887 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1888 struct inet6_dev *idev = pmc->idev;
1889
1890 /* no more filters for this source */
1891 if (psf_prev)
1892 psf_prev->sf_next = psf->sf_next;
1893 else
1894 pmc->mca_sources = psf->sf_next;
1895 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1896 !MLD_V1_SEEN(idev)) {
1897 psf->sf_crcount = idev->mc_qrv;
1898 psf->sf_next = pmc->mca_tomb;
1899 pmc->mca_tomb = psf;
1900 rv = 1;
1901 } else
1902 kfree(psf);
1903 }
1904 return rv;
1905}
1906
1907static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1908 int sfmode, int sfcount, struct in6_addr *psfsrc,
1909 int delta)
1910{
1911 struct ifmcaddr6 *pmc;
1912 int changerec = 0;
1913 int i, err;
1914
1915 if (!idev)
1916 return -ENODEV;
1917 read_lock_bh(&idev->lock);
1918 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1919 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1920 break;
1921 }
1922 if (!pmc) {
1923 /* MCA not found?? bug */
1924 read_unlock_bh(&idev->lock);
1925 return -ESRCH;
1926 }
1927 spin_lock_bh(&pmc->mca_lock);
1928 sf_markstate(pmc);
1929 if (!delta) {
1930 if (!pmc->mca_sfcount[sfmode]) {
1931 spin_unlock_bh(&pmc->mca_lock);
1932 read_unlock_bh(&idev->lock);
1933 return -EINVAL;
1934 }
1935 pmc->mca_sfcount[sfmode]--;
1936 }
1937 err = 0;
1938 for (i=0; i<sfcount; i++) {
1939 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1940
1941 changerec |= rv > 0;
1942 if (!err && rv < 0)
1943 err = rv;
1944 }
1945 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1946 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1947 pmc->mca_sfcount[MCAST_INCLUDE]) {
1948 struct ip6_sf_list *psf;
1949
1950 /* filter mode change */
1951 pmc->mca_sfmode = MCAST_INCLUDE;
1952 pmc->mca_crcount = idev->mc_qrv;
1953 idev->mc_ifc_count = pmc->mca_crcount;
1954 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1955 psf->sf_crcount = 0;
1956 mld_ifc_event(pmc->idev);
1957 } else if (sf_setstate(pmc) || changerec)
1958 mld_ifc_event(pmc->idev);
1959 spin_unlock_bh(&pmc->mca_lock);
1960 read_unlock_bh(&idev->lock);
1961 return err;
1962}
1963
1964/*
1965 * Add multicast single-source filter to the interface list
1966 */
1967static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1968 struct in6_addr *psfsrc, int delta)
1969{
1970 struct ip6_sf_list *psf, *psf_prev;
1971
1972 psf_prev = NULL;
1973 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1974 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1975 break;
1976 psf_prev = psf;
1977 }
1978 if (!psf) {
Ingo Oeser0c600ed2006-03-20 23:01:32 -08001979 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (!psf)
1981 return -ENOBUFS;
Ingo Oeser0c600ed2006-03-20 23:01:32 -08001982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 psf->sf_addr = *psfsrc;
1984 if (psf_prev) {
1985 psf_prev->sf_next = psf;
1986 } else
1987 pmc->mca_sources = psf;
1988 }
1989 psf->sf_count[sfmode]++;
1990 return 0;
1991}
1992
1993static void sf_markstate(struct ifmcaddr6 *pmc)
1994{
1995 struct ip6_sf_list *psf;
1996 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1997
1998 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1999 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2000 psf->sf_oldin = mca_xcount ==
2001 psf->sf_count[MCAST_EXCLUDE] &&
2002 !psf->sf_count[MCAST_INCLUDE];
2003 } else
2004 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2005}
2006
2007static int sf_setstate(struct ifmcaddr6 *pmc)
2008{
David L Stevens7add2a42006-01-24 13:06:39 -08002009 struct ip6_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2011 int qrv = pmc->idev->mc_qrv;
2012 int new_in, rv;
2013
2014 rv = 0;
2015 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
2016 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2017 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2018 !psf->sf_count[MCAST_INCLUDE];
2019 } else
2020 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevens7add2a42006-01-24 13:06:39 -08002021 if (new_in) {
2022 if (!psf->sf_oldin) {
Al Viroe80e28b2006-02-03 20:10:03 -05002023 struct ip6_sf_list *prev = NULL;
David L Stevens7add2a42006-01-24 13:06:39 -08002024
2025 for (dpsf=pmc->mca_tomb; dpsf;
2026 dpsf=dpsf->sf_next) {
2027 if (ipv6_addr_equal(&dpsf->sf_addr,
2028 &psf->sf_addr))
2029 break;
2030 prev = dpsf;
2031 }
2032 if (dpsf) {
2033 if (prev)
2034 prev->sf_next = dpsf->sf_next;
2035 else
2036 pmc->mca_tomb = dpsf->sf_next;
2037 kfree(dpsf);
2038 }
2039 psf->sf_crcount = qrv;
2040 rv++;
2041 }
2042 } else if (psf->sf_oldin) {
2043 psf->sf_crcount = 0;
2044 /*
2045 * add or update "delete" records if an active filter
2046 * is now inactive
2047 */
2048 for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
2049 if (ipv6_addr_equal(&dpsf->sf_addr,
2050 &psf->sf_addr))
2051 break;
2052 if (!dpsf) {
2053 dpsf = (struct ip6_sf_list *)
2054 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2055 if (!dpsf)
2056 continue;
2057 *dpsf = *psf;
2058 /* pmc->mca_lock held by callers */
2059 dpsf->sf_next = pmc->mca_tomb;
2060 pmc->mca_tomb = dpsf;
2061 }
2062 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 rv++;
2064 }
2065 }
2066 return rv;
2067}
2068
2069/*
2070 * Add multicast source filter list to the interface list
2071 */
2072static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
2073 int sfmode, int sfcount, struct in6_addr *psfsrc,
2074 int delta)
2075{
2076 struct ifmcaddr6 *pmc;
2077 int isexclude;
2078 int i, err;
2079
2080 if (!idev)
2081 return -ENODEV;
2082 read_lock_bh(&idev->lock);
2083 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2084 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2085 break;
2086 }
2087 if (!pmc) {
2088 /* MCA not found?? bug */
2089 read_unlock_bh(&idev->lock);
2090 return -ESRCH;
2091 }
2092 spin_lock_bh(&pmc->mca_lock);
2093
2094 sf_markstate(pmc);
2095 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2096 if (!delta)
2097 pmc->mca_sfcount[sfmode]++;
2098 err = 0;
2099 for (i=0; i<sfcount; i++) {
2100 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
2101 if (err)
2102 break;
2103 }
2104 if (err) {
2105 int j;
2106
2107 if (!delta)
2108 pmc->mca_sfcount[sfmode]--;
2109 for (j=0; j<i; j++)
2110 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2111 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 struct ip6_sf_list *psf;
2113
2114 /* filter mode change */
2115 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2116 pmc->mca_sfmode = MCAST_EXCLUDE;
2117 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2118 pmc->mca_sfmode = MCAST_INCLUDE;
2119 /* else no filters; keep old mode for reports */
2120
2121 pmc->mca_crcount = idev->mc_qrv;
2122 idev->mc_ifc_count = pmc->mca_crcount;
2123 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2124 psf->sf_crcount = 0;
2125 mld_ifc_event(idev);
2126 } else if (sf_setstate(pmc))
2127 mld_ifc_event(idev);
2128 spin_unlock_bh(&pmc->mca_lock);
2129 read_unlock_bh(&idev->lock);
2130 return err;
2131}
2132
2133static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2134{
2135 struct ip6_sf_list *psf, *nextpsf;
2136
2137 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2138 nextpsf = psf->sf_next;
2139 kfree(psf);
2140 }
2141 pmc->mca_tomb = NULL;
2142 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2143 nextpsf = psf->sf_next;
2144 kfree(psf);
2145 }
2146 pmc->mca_sources = NULL;
2147 pmc->mca_sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002148 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2150}
2151
2152
2153static void igmp6_join_group(struct ifmcaddr6 *ma)
2154{
2155 unsigned long delay;
2156
2157 if (ma->mca_flags & MAF_NOREPORT)
2158 return;
2159
2160 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2161
2162 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2163
2164 spin_lock_bh(&ma->mca_lock);
2165 if (del_timer(&ma->mca_timer)) {
2166 atomic_dec(&ma->mca_refcnt);
2167 delay = ma->mca_timer.expires - jiffies;
2168 }
2169
2170 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2171 atomic_inc(&ma->mca_refcnt);
2172 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2173 spin_unlock_bh(&ma->mca_lock);
2174}
2175
2176static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2177 struct inet6_dev *idev)
2178{
2179 int err;
2180
David L Stevens5ab4a6c2005-12-27 14:03:00 -08002181 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2182 * so no other readers or writers of iml or its sflist
2183 */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002184 if (!iml->sflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 /* any-source empty exclude case */
2186 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2187 }
2188 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2189 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2190 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2191 iml->sflist = NULL;
2192 return err;
2193}
2194
2195static void igmp6_leave_group(struct ifmcaddr6 *ma)
2196{
2197 if (MLD_V1_SEEN(ma->idev)) {
2198 if (ma->mca_flags & MAF_LAST_REPORTER)
2199 igmp6_send(&ma->mca_addr, ma->idev->dev,
2200 ICMPV6_MGM_REDUCTION);
2201 } else {
2202 mld_add_delrec(ma->idev, ma);
2203 mld_ifc_event(ma->idev);
2204 }
2205}
2206
2207static void mld_gq_timer_expire(unsigned long data)
2208{
2209 struct inet6_dev *idev = (struct inet6_dev *)data;
2210
2211 idev->mc_gq_running = 0;
2212 mld_send_report(idev, NULL);
2213 __in6_dev_put(idev);
2214}
2215
2216static void mld_ifc_timer_expire(unsigned long data)
2217{
2218 struct inet6_dev *idev = (struct inet6_dev *)data;
2219
2220 mld_send_cr(idev);
2221 if (idev->mc_ifc_count) {
2222 idev->mc_ifc_count--;
2223 if (idev->mc_ifc_count)
2224 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2225 }
2226 __in6_dev_put(idev);
2227}
2228
2229static void mld_ifc_event(struct inet6_dev *idev)
2230{
2231 if (MLD_V1_SEEN(idev))
2232 return;
2233 idev->mc_ifc_count = idev->mc_qrv;
2234 mld_ifc_start_timer(idev, 1);
2235}
2236
2237
2238static void igmp6_timer_handler(unsigned long data)
2239{
2240 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2241
2242 if (MLD_V1_SEEN(ma->idev))
2243 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2244 else
2245 mld_send_report(ma->idev, ma);
2246
2247 spin_lock(&ma->mca_lock);
2248 ma->mca_flags |= MAF_LAST_REPORTER;
2249 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2250 spin_unlock(&ma->mca_lock);
2251 ma_put(ma);
2252}
2253
Moni Shoua75c78502009-09-15 02:37:40 -07002254/* Device changing type */
2255
2256void ipv6_mc_unmap(struct inet6_dev *idev)
2257{
2258 struct ifmcaddr6 *i;
2259
2260 /* Install multicast list, except for all-nodes (already installed) */
2261
2262 read_lock_bh(&idev->lock);
2263 for (i = idev->mc_list; i; i = i->next)
2264 igmp6_group_dropped(i);
2265 read_unlock_bh(&idev->lock);
2266}
2267
2268void ipv6_mc_remap(struct inet6_dev *idev)
2269{
2270 ipv6_mc_up(idev);
2271}
2272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273/* Device going down */
2274
2275void ipv6_mc_down(struct inet6_dev *idev)
2276{
2277 struct ifmcaddr6 *i;
2278
2279 /* Withdraw multicast list */
2280
2281 read_lock_bh(&idev->lock);
2282 idev->mc_ifc_count = 0;
2283 if (del_timer(&idev->mc_ifc_timer))
2284 __in6_dev_put(idev);
2285 idev->mc_gq_running = 0;
2286 if (del_timer(&idev->mc_gq_timer))
2287 __in6_dev_put(idev);
2288
2289 for (i = idev->mc_list; i; i=i->next)
2290 igmp6_group_dropped(i);
2291 read_unlock_bh(&idev->lock);
2292
2293 mld_clear_delrec(idev);
2294}
2295
2296
2297/* Device going up */
2298
2299void ipv6_mc_up(struct inet6_dev *idev)
2300{
2301 struct ifmcaddr6 *i;
2302
2303 /* Install multicast list, except for all-nodes (already installed) */
2304
2305 read_lock_bh(&idev->lock);
2306 for (i = idev->mc_list; i; i=i->next)
2307 igmp6_group_added(i);
2308 read_unlock_bh(&idev->lock);
2309}
2310
2311/* IPv6 device initialization. */
2312
2313void ipv6_mc_init_dev(struct inet6_dev *idev)
2314{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 write_lock_bh(&idev->lock);
Stephen Hemminger6457d262010-02-17 18:48:44 -08002316 spin_lock_init(&idev->mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 idev->mc_gq_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002318 setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2319 (unsigned long)idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 idev->mc_tomb = NULL;
2321 idev->mc_ifc_count = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002322 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2323 (unsigned long)idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 idev->mc_qrv = MLD_QRV_DEFAULT;
2325 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2326 idev->mc_v1_seen = 0;
2327 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328}
2329
2330/*
2331 * Device is about to be destroyed: clean up.
2332 */
2333
2334void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2335{
2336 struct ifmcaddr6 *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
2338 /* Deactivate timers */
2339 ipv6_mc_down(idev);
2340
2341 /* Delete all-nodes address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2343 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2344 * fail.
2345 */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002346 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002348 if (idev->cnf.forwarding)
2349 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
2351 write_lock_bh(&idev->lock);
2352 while ((i = idev->mc_list) != NULL) {
2353 idev->mc_list = i->next;
2354 write_unlock_bh(&idev->lock);
2355
2356 igmp6_group_dropped(i);
2357 ma_put(i);
2358
2359 write_lock_bh(&idev->lock);
2360 }
2361 write_unlock_bh(&idev->lock);
2362}
2363
2364#ifdef CONFIG_PROC_FS
2365struct igmp6_mc_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002366 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 struct net_device *dev;
2368 struct inet6_dev *idev;
2369};
2370
2371#define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2372
2373static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2374{
2375 struct ifmcaddr6 *im = NULL;
2376 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002377 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Pavel Emelianov7562f872007-05-03 15:13:45 -07002379 state->idev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002380 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 struct inet6_dev *idev;
Eric Dumazetce81b762009-11-11 17:34:30 +00002382 idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 if (!idev)
2384 continue;
2385 read_lock_bh(&idev->lock);
2386 im = idev->mc_list;
2387 if (im) {
2388 state->idev = idev;
2389 break;
2390 }
2391 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393 return im;
2394}
2395
2396static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2397{
2398 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2399
2400 im = im->next;
2401 while (!im) {
Eric Dumazetce81b762009-11-11 17:34:30 +00002402 if (likely(state->idev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 read_unlock_bh(&state->idev->lock);
Eric Dumazetce81b762009-11-11 17:34:30 +00002404
2405 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 if (!state->dev) {
2407 state->idev = NULL;
2408 break;
2409 }
Eric Dumazetce81b762009-11-11 17:34:30 +00002410 state->idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 if (!state->idev)
2412 continue;
2413 read_lock_bh(&state->idev->lock);
2414 im = state->idev->mc_list;
2415 }
2416 return im;
2417}
2418
2419static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2420{
2421 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2422 if (im)
2423 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2424 --pos;
2425 return pos ? NULL : im;
2426}
2427
2428static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetce81b762009-11-11 17:34:30 +00002429 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
Eric Dumazetce81b762009-11-11 17:34:30 +00002431 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 return igmp6_mc_get_idx(seq, *pos);
2433}
2434
2435static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2436{
Eric Dumazetce81b762009-11-11 17:34:30 +00002437 struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 ++*pos;
2440 return im;
2441}
2442
2443static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetce81b762009-11-11 17:34:30 +00002444 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445{
2446 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
Eric Dumazetce81b762009-11-11 17:34:30 +00002447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (likely(state->idev != NULL)) {
2449 read_unlock_bh(&state->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 state->idev = NULL;
2451 }
2452 state->dev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002453 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454}
2455
2456static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2457{
2458 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2459 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2460
2461 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002462 "%-4d %-15s %pi6 %5d %08X %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 state->dev->ifindex, state->dev->name,
Harvey Harrisonb0711952008-10-28 16:05:40 -07002464 &im->mca_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 im->mca_users, im->mca_flags,
2466 (im->mca_flags&MAF_TIMER_RUNNING) ?
2467 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2468 return 0;
2469}
2470
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002471static const struct seq_operations igmp6_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 .start = igmp6_mc_seq_start,
2473 .next = igmp6_mc_seq_next,
2474 .stop = igmp6_mc_seq_stop,
2475 .show = igmp6_mc_seq_show,
2476};
2477
2478static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2479{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002480 return seq_open_net(inode, file, &igmp6_mc_seq_ops,
2481 sizeof(struct igmp6_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
Arjan van de Ven9a321442007-02-12 00:55:35 -08002484static const struct file_operations igmp6_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 .owner = THIS_MODULE,
2486 .open = igmp6_mc_seq_open,
2487 .read = seq_read,
2488 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002489 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490};
2491
2492struct igmp6_mcf_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002493 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 struct net_device *dev;
2495 struct inet6_dev *idev;
2496 struct ifmcaddr6 *im;
2497};
2498
2499#define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2500
2501static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2502{
2503 struct ip6_sf_list *psf = NULL;
2504 struct ifmcaddr6 *im = NULL;
2505 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002506 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Pavel Emelianov7562f872007-05-03 15:13:45 -07002508 state->idev = NULL;
2509 state->im = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002510 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 struct inet6_dev *idev;
Eric Dumazetce81b762009-11-11 17:34:30 +00002512 idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 if (unlikely(idev == NULL))
2514 continue;
2515 read_lock_bh(&idev->lock);
2516 im = idev->mc_list;
2517 if (likely(im != NULL)) {
2518 spin_lock_bh(&im->mca_lock);
2519 psf = im->mca_sources;
2520 if (likely(psf != NULL)) {
2521 state->im = im;
2522 state->idev = idev;
2523 break;
2524 }
2525 spin_unlock_bh(&im->mca_lock);
2526 }
2527 read_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 }
2529 return psf;
2530}
2531
2532static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2533{
2534 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2535
2536 psf = psf->sf_next;
2537 while (!psf) {
2538 spin_unlock_bh(&state->im->mca_lock);
2539 state->im = state->im->next;
2540 while (!state->im) {
Eric Dumazetce81b762009-11-11 17:34:30 +00002541 if (likely(state->idev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 read_unlock_bh(&state->idev->lock);
Eric Dumazetce81b762009-11-11 17:34:30 +00002543
2544 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 if (!state->dev) {
2546 state->idev = NULL;
2547 goto out;
2548 }
Eric Dumazetce81b762009-11-11 17:34:30 +00002549 state->idev = __in6_dev_get(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 if (!state->idev)
2551 continue;
2552 read_lock_bh(&state->idev->lock);
2553 state->im = state->idev->mc_list;
2554 }
2555 if (!state->im)
2556 break;
2557 spin_lock_bh(&state->im->mca_lock);
2558 psf = state->im->mca_sources;
2559 }
2560out:
2561 return psf;
2562}
2563
2564static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2565{
2566 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2567 if (psf)
2568 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2569 --pos;
2570 return pos ? NULL : psf;
2571}
2572
2573static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetce81b762009-11-11 17:34:30 +00002574 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575{
Eric Dumazetce81b762009-11-11 17:34:30 +00002576 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2578}
2579
2580static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2581{
2582 struct ip6_sf_list *psf;
2583 if (v == SEQ_START_TOKEN)
2584 psf = igmp6_mcf_get_first(seq);
2585 else
2586 psf = igmp6_mcf_get_next(seq, v);
2587 ++*pos;
2588 return psf;
2589}
2590
2591static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetce81b762009-11-11 17:34:30 +00002592 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593{
2594 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2595 if (likely(state->im != NULL)) {
2596 spin_unlock_bh(&state->im->mca_lock);
2597 state->im = NULL;
2598 }
2599 if (likely(state->idev != NULL)) {
2600 read_unlock_bh(&state->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 state->idev = NULL;
2602 }
2603 state->dev = NULL;
Eric Dumazetce81b762009-11-11 17:34:30 +00002604 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605}
2606
2607static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2608{
2609 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2610 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2611
2612 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002613 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 "%3s %6s "
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002615 "%32s %32s %6s %6s\n", "Idx",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 "Device", "Multicast Address",
2617 "Source Address", "INC", "EXC");
2618 } else {
2619 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002620 "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 state->dev->ifindex, state->dev->name,
Harvey Harrisonb0711952008-10-28 16:05:40 -07002622 &state->im->mca_addr,
2623 &psf->sf_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 psf->sf_count[MCAST_INCLUDE],
2625 psf->sf_count[MCAST_EXCLUDE]);
2626 }
2627 return 0;
2628}
2629
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002630static const struct seq_operations igmp6_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 .start = igmp6_mcf_seq_start,
2632 .next = igmp6_mcf_seq_next,
2633 .stop = igmp6_mcf_seq_stop,
2634 .show = igmp6_mcf_seq_show,
2635};
2636
2637static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2638{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002639 return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
2640 sizeof(struct igmp6_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
2642
Arjan van de Ven9a321442007-02-12 00:55:35 -08002643static const struct file_operations igmp6_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 .owner = THIS_MODULE,
2645 .open = igmp6_mcf_seq_open,
2646 .read = seq_read,
2647 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002648 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649};
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002650
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002651static int __net_init igmp6_proc_init(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002652{
2653 int err;
2654
2655 err = -ENOMEM;
2656 if (!proc_net_fops_create(net, "igmp6", S_IRUGO, &igmp6_mc_seq_fops))
2657 goto out;
2658 if (!proc_net_fops_create(net, "mcfilter6", S_IRUGO,
2659 &igmp6_mcf_seq_fops))
2660 goto out_proc_net_igmp6;
2661
2662 err = 0;
2663out:
2664 return err;
2665
2666out_proc_net_igmp6:
2667 proc_net_remove(net, "igmp6");
2668 goto out;
2669}
2670
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002671static void __net_exit igmp6_proc_exit(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002672{
2673 proc_net_remove(net, "mcfilter6");
2674 proc_net_remove(net, "igmp6");
2675}
2676#else
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002677static inline int igmp6_proc_init(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002678{
2679 return 0;
2680}
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002681static inline void igmp6_proc_exit(struct net *net)
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002682{
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002683}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684#endif
2685
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002686static int __net_init igmp6_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 int err;
2689
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002690 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2691 SOCK_RAW, IPPROTO_ICMPV6, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 if (err < 0) {
2693 printk(KERN_ERR
2694 "Failed to initialize the IGMP6 control socket (err %d).\n",
2695 err);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002696 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 }
2698
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002699 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002701 err = igmp6_proc_init(net);
2702 if (err)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002703 goto out_sock_create;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002704out:
2705 return err;
2706
2707out_sock_create:
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002708 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002709 goto out;
2710}
2711
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002712static void __net_exit igmp6_net_exit(struct net *net)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002713{
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002714 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002715 igmp6_proc_exit(net);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002716}
2717
2718static struct pernet_operations igmp6_net_ops = {
2719 .init = igmp6_net_init,
2720 .exit = igmp6_net_exit,
2721};
2722
2723int __init igmp6_init(void)
2724{
2725 return register_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726}
2727
2728void igmp6_cleanup(void)
2729{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002730 unregister_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731}