blob: a51fb33e6864f7d588c8ed81821378d710081411 [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>
46
47#include <linux/netfilter.h>
48#include <linux/netfilter_ipv6.h>
49
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020050#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <net/sock.h>
52#include <net/snmp.h>
53
54#include <net/ipv6.h>
55#include <net/protocol.h>
56#include <net/if_inet6.h>
57#include <net/ndisc.h>
58#include <net/addrconf.h>
59#include <net/ip6_route.h>
Denis V. Lunev1ed85162008-04-03 14:31:03 -070060#include <net/inet_common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#include <net/ip6_checksum.h>
63
64/* Set to 3 to get tracing... */
65#define MCAST_DEBUG 2
66
67#if MCAST_DEBUG >= 3
68#define MDBG(x) printk x
69#else
70#define MDBG(x)
71#endif
72
73/*
74 * These header formats should be in a separate include file, but icmpv6.h
75 * doesn't have in6_addr defined in all cases, there is no __u128, and no
76 * other files reference these.
77 *
78 * +-DLS 4/14/03
79 */
80
81/* Multicast Listener Discovery version 2 headers */
82
83struct mld2_grec {
84 __u8 grec_type;
85 __u8 grec_auxwords;
Al Viro8a74ff72006-11-08 00:22:08 -080086 __be16 grec_nsrcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 struct in6_addr grec_mca;
88 struct in6_addr grec_src[0];
89};
90
91struct mld2_report {
92 __u8 type;
93 __u8 resv1;
Al Viro868c86b2006-11-14 21:35:48 -080094 __sum16 csum;
Al Viro8a74ff72006-11-08 00:22:08 -080095 __be16 resv2;
96 __be16 ngrec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 struct mld2_grec grec[0];
98};
99
100struct mld2_query {
101 __u8 type;
102 __u8 code;
Al Viro868c86b2006-11-14 21:35:48 -0800103 __sum16 csum;
Al Viro8a74ff72006-11-08 00:22:08 -0800104 __be16 mrc;
105 __be16 resv1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct in6_addr mca;
107#if defined(__LITTLE_ENDIAN_BITFIELD)
108 __u8 qrv:3,
109 suppress:1,
110 resv2:4;
111#elif defined(__BIG_ENDIAN_BITFIELD)
112 __u8 resv2:4,
113 suppress:1,
114 qrv:3;
115#else
116#error "Please fix <asm/byteorder.h>"
117#endif
118 __u8 qqic;
Al Viro8a74ff72006-11-08 00:22:08 -0800119 __be16 nsrcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 struct in6_addr srcs[0];
121};
122
123static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT;
124
125/* Big mc list lock for all the sockets */
126static DEFINE_RWLOCK(ipv6_sk_mc_lock);
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static void igmp6_join_group(struct ifmcaddr6 *ma);
129static void igmp6_leave_group(struct ifmcaddr6 *ma);
130static void igmp6_timer_handler(unsigned long data);
131
132static void mld_gq_timer_expire(unsigned long data);
133static void mld_ifc_timer_expire(unsigned long data);
134static void mld_ifc_event(struct inet6_dev *idev);
135static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc);
136static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *addr);
137static void mld_clear_delrec(struct inet6_dev *idev);
138static int sf_setstate(struct ifmcaddr6 *pmc);
139static void sf_markstate(struct ifmcaddr6 *pmc);
140static void ip6_mc_clear_src(struct ifmcaddr6 *pmc);
141static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
142 int sfmode, int sfcount, struct in6_addr *psfsrc,
143 int delta);
144static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
145 int sfmode, int sfcount, struct in6_addr *psfsrc,
146 int delta);
147static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
148 struct inet6_dev *idev);
149
150
151#define IGMP6_UNSOLICITED_IVAL (10*HZ)
152#define MLD_QRV_DEFAULT 2
153
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700154#define MLD_V1_SEEN(idev) (dev_net((idev)->dev)->ipv6.devconf_all->force_mld_version == 1 || \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 (idev)->cnf.force_mld_version == 1 || \
156 ((idev)->mc_v1_seen && \
157 time_before(jiffies, (idev)->mc_v1_seen)))
158
159#define MLDV2_MASK(value, nb) ((nb)>=32 ? (value) : ((1<<(nb))-1) & (value))
160#define MLDV2_EXP(thresh, nbmant, nbexp, value) \
161 ((value) < (thresh) ? (value) : \
Yan Zhengf12baea2005-10-29 00:02:32 +0800162 ((MLDV2_MASK(value, nbmant) | (1<<(nbmant))) << \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 (MLDV2_MASK((value) >> (nbmant), nbexp) + (nbexp))))
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#define MLDV2_MRC(value) MLDV2_EXP(0x8000, 12, 3, value)
166
David L Stevens6f4353d2005-12-26 17:03:46 -0800167#define IPV6_MLD_MAX_MSF 64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Brian Haleyab32ea52006-09-22 14:15:41 -0700169int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/*
172 * socket join on multicast group
173 */
174
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900175int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct net_device *dev = NULL;
178 struct ipv6_mc_socklist *mc_lst;
179 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900180 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 int err;
182
183 if (!ipv6_addr_is_multicast(addr))
184 return -EINVAL;
185
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700186 read_lock_bh(&ipv6_sk_mc_lock);
187 for (mc_lst=np->ipv6_mc_list; mc_lst; mc_lst=mc_lst->next) {
188 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
189 ipv6_addr_equal(&mc_lst->addr, addr)) {
190 read_unlock_bh(&ipv6_sk_mc_lock);
191 return -EADDRINUSE;
192 }
193 }
194 read_unlock_bh(&ipv6_sk_mc_lock);
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
197
198 if (mc_lst == NULL)
199 return -ENOMEM;
200
201 mc_lst->next = NULL;
202 ipv6_addr_copy(&mc_lst->addr, addr);
203
204 if (ifindex == 0) {
205 struct rt6_info *rt;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800206 rt = rt6_lookup(net, addr, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (rt) {
208 dev = rt->rt6i_dev;
209 dev_hold(dev);
210 dst_release(&rt->u.dst);
211 }
212 } else
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800213 dev = dev_get_by_index(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 if (dev == NULL) {
216 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
217 return -ENODEV;
218 }
219
220 mc_lst->ifindex = dev->ifindex;
221 mc_lst->sfmode = MCAST_EXCLUDE;
YOSHIFUJI Hideaki196433c2006-01-04 13:56:31 -0800222 rwlock_init(&mc_lst->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 mc_lst->sflist = NULL;
224
225 /*
226 * now add/increase the group membership on the device
227 */
228
229 err = ipv6_dev_mc_inc(dev, addr);
230
231 if (err) {
232 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
233 dev_put(dev);
234 return err;
235 }
236
237 write_lock_bh(&ipv6_sk_mc_lock);
238 mc_lst->next = np->ipv6_mc_list;
239 np->ipv6_mc_list = mc_lst;
240 write_unlock_bh(&ipv6_sk_mc_lock);
241
242 dev_put(dev);
243
244 return 0;
245}
246
247/*
248 * socket leave on multicast group
249 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900250int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 struct ipv6_pinfo *np = inet6_sk(sk);
253 struct ipv6_mc_socklist *mc_lst, **lnk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900254 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 write_lock_bh(&ipv6_sk_mc_lock);
257 for (lnk = &np->ipv6_mc_list; (mc_lst = *lnk) !=NULL ; lnk = &mc_lst->next) {
258 if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
259 ipv6_addr_equal(&mc_lst->addr, addr)) {
260 struct net_device *dev;
261
262 *lnk = mc_lst->next;
263 write_unlock_bh(&ipv6_sk_mc_lock);
264
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800265 dev = dev_get_by_index(net, mc_lst->ifindex);
266 if (dev != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct inet6_dev *idev = in6_dev_get(dev);
268
David L Stevensacd6e002006-08-17 16:27:39 -0700269 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
272 in6_dev_put(idev);
273 }
274 dev_put(dev);
David L Stevensacd6e002006-08-17 16:27:39 -0700275 } else
276 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
278 return 0;
279 }
280 }
281 write_unlock_bh(&ipv6_sk_mc_lock);
282
David L Stevens9951f032005-07-08 17:47:28 -0700283 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800286static struct inet6_dev *ip6_mc_find_dev(struct net *net,
287 struct in6_addr *group,
288 int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 struct net_device *dev = NULL;
291 struct inet6_dev *idev = NULL;
292
293 if (ifindex == 0) {
294 struct rt6_info *rt;
295
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800296 rt = rt6_lookup(net, group, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (rt) {
298 dev = rt->rt6i_dev;
299 dev_hold(dev);
300 dst_release(&rt->u.dst);
301 }
302 } else
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800303 dev = dev_get_by_index(net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 if (!dev)
Ilpo Järvinen448eb712008-12-14 23:15:21 -0800306 goto nodev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 idev = in6_dev_get(dev);
Ilpo Järvinen448eb712008-12-14 23:15:21 -0800308 if (!idev)
309 goto release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 read_lock_bh(&idev->lock);
Ilpo Järvinen448eb712008-12-14 23:15:21 -0800311 if (idev->dead)
312 goto unlock_release;
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return idev;
Ilpo Järvinen448eb712008-12-14 23:15:21 -0800315
316unlock_release:
317 read_unlock_bh(&idev->lock);
318 in6_dev_put(idev);
319release:
320 dev_put(dev);
321nodev:
322 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325void ipv6_sock_mc_close(struct sock *sk)
326{
327 struct ipv6_pinfo *np = inet6_sk(sk);
328 struct ipv6_mc_socklist *mc_lst;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900329 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 write_lock_bh(&ipv6_sk_mc_lock);
332 while ((mc_lst = np->ipv6_mc_list) != NULL) {
333 struct net_device *dev;
334
335 np->ipv6_mc_list = mc_lst->next;
336 write_unlock_bh(&ipv6_sk_mc_lock);
337
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800338 dev = dev_get_by_index(net, mc_lst->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (dev) {
340 struct inet6_dev *idev = in6_dev_get(dev);
341
David L Stevensacd6e002006-08-17 16:27:39 -0700342 (void) ip6_mc_leave_src(sk, mc_lst, idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
345 in6_dev_put(idev);
346 }
347 dev_put(dev);
David L Stevensacd6e002006-08-17 16:27:39 -0700348 } else
349 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
352
353 write_lock_bh(&ipv6_sk_mc_lock);
354 }
355 write_unlock_bh(&ipv6_sk_mc_lock);
356}
357
358int ip6_mc_source(int add, int omode, struct sock *sk,
359 struct group_source_req *pgsr)
360{
361 struct in6_addr *source, *group;
362 struct ipv6_mc_socklist *pmc;
363 struct net_device *dev;
364 struct inet6_dev *idev;
365 struct ipv6_pinfo *inet6 = inet6_sk(sk);
366 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900367 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 int i, j, rv;
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700369 int leavegroup = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800370 int pmclocked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 int err;
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr;
374 group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr;
375
376 if (!ipv6_addr_is_multicast(group))
377 return -EINVAL;
378
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800379 idev = ip6_mc_find_dev(net, group, pgsr->gsr_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (!idev)
381 return -ENODEV;
382 dev = idev->dev;
383
384 err = -EADDRNOTAVAIL;
385
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700386 read_lock_bh(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
388 if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
389 continue;
390 if (ipv6_addr_equal(&pmc->addr, group))
391 break;
392 }
David L Stevens917f2f12005-07-08 17:45:16 -0700393 if (!pmc) { /* must have a prior join */
394 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /* if a source filter was set, must be the same mode as before */
398 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -0700399 if (pmc->sfmode != omode) {
400 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 } else if (pmc->sfmode != omode) {
404 /* allow mode switches for empty-set filters */
405 ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
406 ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
407 pmc->sfmode = omode;
408 }
409
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800410 write_lock_bh(&pmc->sflock);
411 pmclocked = 1;
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 psl = pmc->sflist;
414 if (!add) {
415 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -0700416 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 rv = !0;
418 for (i=0; i<psl->sl_count; i++) {
419 rv = memcmp(&psl->sl_addr[i], source,
420 sizeof(struct in6_addr));
421 if (rv == 0)
422 break;
423 }
424 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -0700425 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700427 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
428 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
429 leavegroup = 1;
430 goto done;
431 }
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 /* update the interface filter */
434 ip6_mc_del_src(idev, group, omode, 1, source, 1);
435
436 for (j=i+1; j<psl->sl_count; j++)
437 psl->sl_addr[j-1] = psl->sl_addr[j];
438 psl->sl_count--;
439 err = 0;
440 goto done;
441 }
442 /* else, add a new source to the filter */
443
444 if (psl && psl->sl_count >= sysctl_mld_max_msf) {
445 err = -ENOBUFS;
446 goto done;
447 }
448 if (!psl || psl->sl_count == psl->sl_max) {
449 struct ip6_sf_socklist *newpsl;
450 int count = IP6_SFBLOCK;
451
452 if (psl)
453 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800454 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (!newpsl) {
456 err = -ENOBUFS;
457 goto done;
458 }
459 newpsl->sl_max = count;
460 newpsl->sl_count = count - IP6_SFBLOCK;
461 if (psl) {
462 for (i=0; i<psl->sl_count; i++)
463 newpsl->sl_addr[i] = psl->sl_addr[i];
464 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
465 }
466 pmc->sflist = psl = newpsl;
467 }
468 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
469 for (i=0; i<psl->sl_count; i++) {
470 rv = memcmp(&psl->sl_addr[i], source, sizeof(struct in6_addr));
471 if (rv == 0)
472 break;
473 }
474 if (rv == 0) /* address already there is an error */
475 goto done;
476 for (j=psl->sl_count-1; j>=i; j--)
477 psl->sl_addr[j+1] = psl->sl_addr[j];
478 psl->sl_addr[i] = *source;
479 psl->sl_count++;
480 err = 0;
481 /* update the interface list */
482 ip6_mc_add_src(idev, group, omode, 1, source, 1);
483done:
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800484 if (pmclocked)
485 write_unlock_bh(&pmc->sflock);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700486 read_unlock_bh(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 read_unlock_bh(&idev->lock);
488 in6_dev_put(idev);
489 dev_put(dev);
David L Stevensc9e3e8b2005-06-21 13:58:25 -0700490 if (leavegroup)
491 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return err;
493}
494
495int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf)
496{
497 struct in6_addr *group;
498 struct ipv6_mc_socklist *pmc;
499 struct net_device *dev;
500 struct inet6_dev *idev;
501 struct ipv6_pinfo *inet6 = inet6_sk(sk);
502 struct ip6_sf_socklist *newpsl, *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900503 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -0700504 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 int i, err;
506
507 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
508
509 if (!ipv6_addr_is_multicast(group))
510 return -EINVAL;
511 if (gsf->gf_fmode != MCAST_INCLUDE &&
512 gsf->gf_fmode != MCAST_EXCLUDE)
513 return -EINVAL;
514
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800515 idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 if (!idev)
518 return -ENODEV;
519 dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
David S. Miller9c059892005-07-08 21:44:39 -0700521 err = 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800522 read_lock_bh(&ipv6_sk_mc_lock);
523
David L Stevens9951f032005-07-08 17:47:28 -0700524 if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) {
525 leavegroup = 1;
526 goto done;
527 }
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
530 if (pmc->ifindex != gsf->gf_interface)
531 continue;
532 if (ipv6_addr_equal(&pmc->addr, group))
533 break;
534 }
David L Stevens917f2f12005-07-08 17:45:16 -0700535 if (!pmc) { /* must have a prior join */
536 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -0700538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (gsf->gf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800540 newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc),
541 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (!newpsl) {
543 err = -ENOBUFS;
544 goto done;
545 }
546 newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc;
547 for (i=0; i<newpsl->sl_count; ++i) {
548 struct sockaddr_in6 *psin6;
549
550 psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i];
551 newpsl->sl_addr[i] = psin6->sin6_addr;
552 }
553 err = ip6_mc_add_src(idev, group, gsf->gf_fmode,
554 newpsl->sl_count, newpsl->sl_addr, 0);
555 if (err) {
556 sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max));
557 goto done;
558 }
Yan Zheng8713dbf2005-10-28 08:02:08 +0800559 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +0800561 (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0);
562 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800563
564 write_lock_bh(&pmc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 psl = pmc->sflist;
566 if (psl) {
567 (void) ip6_mc_del_src(idev, group, pmc->sfmode,
568 psl->sl_count, psl->sl_addr, 0);
569 sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max));
570 } else
571 (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
572 pmc->sflist = newpsl;
573 pmc->sfmode = gsf->gf_fmode;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800574 write_unlock_bh(&pmc->sflock);
David L Stevens917f2f12005-07-08 17:45:16 -0700575 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576done:
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800577 read_unlock_bh(&ipv6_sk_mc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 read_unlock_bh(&idev->lock);
579 in6_dev_put(idev);
580 dev_put(dev);
David L Stevens9951f032005-07-08 17:47:28 -0700581 if (leavegroup)
582 err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return err;
584}
585
586int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
587 struct group_filter __user *optval, int __user *optlen)
588{
589 int err, i, count, copycount;
590 struct in6_addr *group;
591 struct ipv6_mc_socklist *pmc;
592 struct inet6_dev *idev;
593 struct net_device *dev;
594 struct ipv6_pinfo *inet6 = inet6_sk(sk);
595 struct ip6_sf_socklist *psl;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900596 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
599
600 if (!ipv6_addr_is_multicast(group))
601 return -EINVAL;
602
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800603 idev = ip6_mc_find_dev(net, group, gsf->gf_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 if (!idev)
606 return -ENODEV;
607
608 dev = idev->dev;
609
610 err = -EADDRNOTAVAIL;
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800611 /*
612 * changes to the ipv6_mc_list require the socket lock and
613 * a read lock on ip6_sk_mc_lock. We have the socket lock,
614 * so reading the list is safe.
615 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 for (pmc=inet6->ipv6_mc_list; pmc; pmc=pmc->next) {
618 if (pmc->ifindex != gsf->gf_interface)
619 continue;
620 if (ipv6_addr_equal(group, &pmc->addr))
621 break;
622 }
623 if (!pmc) /* must have a prior join */
624 goto done;
625 gsf->gf_fmode = pmc->sfmode;
626 psl = pmc->sflist;
627 count = psl ? psl->sl_count : 0;
628 read_unlock_bh(&idev->lock);
629 in6_dev_put(idev);
630 dev_put(dev);
631
632 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
633 gsf->gf_numsrc = count;
634 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
635 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
636 return -EFAULT;
637 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800638 /* changes to psl require the socket lock, a read lock on
639 * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We
640 * have the socket lock, so reading here is safe.
641 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 for (i=0; i<copycount; i++) {
643 struct sockaddr_in6 *psin6;
644 struct sockaddr_storage ss;
645
646 psin6 = (struct sockaddr_in6 *)&ss;
647 memset(&ss, 0, sizeof(ss));
648 psin6->sin6_family = AF_INET6;
649 psin6->sin6_addr = psl->sl_addr[i];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900650 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return -EFAULT;
652 }
653 return 0;
654done:
655 read_unlock_bh(&idev->lock);
656 in6_dev_put(idev);
657 dev_put(dev);
658 return err;
659}
660
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900661int inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
662 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 struct ipv6_pinfo *np = inet6_sk(sk);
665 struct ipv6_mc_socklist *mc;
666 struct ip6_sf_socklist *psl;
667 int rv = 1;
668
669 read_lock(&ipv6_sk_mc_lock);
670 for (mc = np->ipv6_mc_list; mc; mc = mc->next) {
671 if (ipv6_addr_equal(&mc->addr, mc_addr))
672 break;
673 }
674 if (!mc) {
675 read_unlock(&ipv6_sk_mc_lock);
676 return 1;
677 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800678 read_lock(&mc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 psl = mc->sflist;
680 if (!psl) {
681 rv = mc->sfmode == MCAST_EXCLUDE;
682 } else {
683 int i;
684
685 for (i=0; i<psl->sl_count; i++) {
686 if (ipv6_addr_equal(&psl->sl_addr[i], src_addr))
687 break;
688 }
689 if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
690 rv = 0;
691 if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
692 rv = 0;
693 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -0800694 read_unlock(&mc->sflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 read_unlock(&ipv6_sk_mc_lock);
696
697 return rv;
698}
699
700static void ma_put(struct ifmcaddr6 *mc)
701{
702 if (atomic_dec_and_test(&mc->mca_refcnt)) {
703 in6_dev_put(mc->idev);
704 kfree(mc);
705 }
706}
707
708static void igmp6_group_added(struct ifmcaddr6 *mc)
709{
710 struct net_device *dev = mc->idev->dev;
711 char buf[MAX_ADDR_LEN];
712
713 spin_lock_bh(&mc->mca_lock);
714 if (!(mc->mca_flags&MAF_LOADED)) {
715 mc->mca_flags |= MAF_LOADED;
716 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
717 dev_mc_add(dev, buf, dev->addr_len, 0);
718 }
719 spin_unlock_bh(&mc->mca_lock);
720
721 if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT))
722 return;
723
724 if (MLD_V1_SEEN(mc->idev)) {
725 igmp6_join_group(mc);
726 return;
727 }
728 /* else v2 */
729
730 mc->mca_crcount = mc->idev->mc_qrv;
731 mld_ifc_event(mc->idev);
732}
733
734static void igmp6_group_dropped(struct ifmcaddr6 *mc)
735{
736 struct net_device *dev = mc->idev->dev;
737 char buf[MAX_ADDR_LEN];
738
739 spin_lock_bh(&mc->mca_lock);
740 if (mc->mca_flags&MAF_LOADED) {
741 mc->mca_flags &= ~MAF_LOADED;
742 if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0)
743 dev_mc_delete(dev, buf, dev->addr_len, 0);
744 }
745
746 if (mc->mca_flags & MAF_NOREPORT)
747 goto done;
748 spin_unlock_bh(&mc->mca_lock);
749
750 if (!mc->idev->dead)
751 igmp6_leave_group(mc);
752
753 spin_lock_bh(&mc->mca_lock);
754 if (del_timer(&mc->mca_timer))
755 atomic_dec(&mc->mca_refcnt);
756done:
757 ip6_mc_clear_src(mc);
758 spin_unlock_bh(&mc->mca_lock);
759}
760
761/*
762 * deleted ifmcaddr6 manipulation
763 */
764static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
765{
766 struct ifmcaddr6 *pmc;
767
768 /* this is an "ifmcaddr6" for convenience; only the fields below
769 * are actually used. In particular, the refcnt and users are not
770 * used for management of the delete list. Using the same structure
771 * for deleted items allows change reports to use common code with
772 * non-deleted or query-response MCA's.
773 */
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800774 pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (!pmc)
776 return;
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 spin_lock_bh(&im->mca_lock);
779 spin_lock_init(&pmc->mca_lock);
780 pmc->idev = im->idev;
781 in6_dev_hold(idev);
782 pmc->mca_addr = im->mca_addr;
783 pmc->mca_crcount = idev->mc_qrv;
784 pmc->mca_sfmode = im->mca_sfmode;
785 if (pmc->mca_sfmode == MCAST_INCLUDE) {
786 struct ip6_sf_list *psf;
787
788 pmc->mca_tomb = im->mca_tomb;
789 pmc->mca_sources = im->mca_sources;
790 im->mca_tomb = im->mca_sources = NULL;
791 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
792 psf->sf_crcount = pmc->mca_crcount;
793 }
794 spin_unlock_bh(&im->mca_lock);
795
796 write_lock_bh(&idev->mc_lock);
797 pmc->next = idev->mc_tomb;
798 idev->mc_tomb = pmc;
799 write_unlock_bh(&idev->mc_lock);
800}
801
802static void mld_del_delrec(struct inet6_dev *idev, struct in6_addr *pmca)
803{
804 struct ifmcaddr6 *pmc, *pmc_prev;
805 struct ip6_sf_list *psf, *psf_next;
806
807 write_lock_bh(&idev->mc_lock);
808 pmc_prev = NULL;
809 for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) {
810 if (ipv6_addr_equal(&pmc->mca_addr, pmca))
811 break;
812 pmc_prev = pmc;
813 }
814 if (pmc) {
815 if (pmc_prev)
816 pmc_prev->next = pmc->next;
817 else
818 idev->mc_tomb = pmc->next;
819 }
820 write_unlock_bh(&idev->mc_lock);
821 if (pmc) {
822 for (psf=pmc->mca_tomb; psf; psf=psf_next) {
823 psf_next = psf->sf_next;
824 kfree(psf);
825 }
826 in6_dev_put(pmc->idev);
827 kfree(pmc);
828 }
829}
830
831static void mld_clear_delrec(struct inet6_dev *idev)
832{
833 struct ifmcaddr6 *pmc, *nextpmc;
834
835 write_lock_bh(&idev->mc_lock);
836 pmc = idev->mc_tomb;
837 idev->mc_tomb = NULL;
838 write_unlock_bh(&idev->mc_lock);
839
840 for (; pmc; pmc = nextpmc) {
841 nextpmc = pmc->next;
842 ip6_mc_clear_src(pmc);
843 in6_dev_put(pmc->idev);
844 kfree(pmc);
845 }
846
847 /* clear dead sources, too */
848 read_lock_bh(&idev->lock);
849 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
850 struct ip6_sf_list *psf, *psf_next;
851
852 spin_lock_bh(&pmc->mca_lock);
853 psf = pmc->mca_tomb;
854 pmc->mca_tomb = NULL;
855 spin_unlock_bh(&pmc->mca_lock);
856 for (; psf; psf=psf_next) {
857 psf_next = psf->sf_next;
858 kfree(psf);
859 }
860 }
861 read_unlock_bh(&idev->lock);
862}
863
864
865/*
866 * device multicast group inc (add if not found)
867 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900868int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 struct ifmcaddr6 *mc;
871 struct inet6_dev *idev;
872
873 idev = in6_dev_get(dev);
874
875 if (idev == NULL)
876 return -EINVAL;
877
878 write_lock_bh(&idev->lock);
879 if (idev->dead) {
880 write_unlock_bh(&idev->lock);
881 in6_dev_put(idev);
882 return -ENODEV;
883 }
884
885 for (mc = idev->mc_list; mc; mc = mc->next) {
886 if (ipv6_addr_equal(&mc->mca_addr, addr)) {
887 mc->mca_users++;
888 write_unlock_bh(&idev->lock);
889 ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0,
890 NULL, 0);
891 in6_dev_put(idev);
892 return 0;
893 }
894 }
895
896 /*
897 * not found: create a new one.
898 */
899
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800900 mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 if (mc == NULL) {
903 write_unlock_bh(&idev->lock);
904 in6_dev_put(idev);
905 return -ENOMEM;
906 }
907
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800908 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 ipv6_addr_copy(&mc->mca_addr, addr);
911 mc->idev = idev;
912 mc->mca_users = 1;
913 /* mca_stamp should be updated upon changes */
914 mc->mca_cstamp = mc->mca_tstamp = jiffies;
915 atomic_set(&mc->mca_refcnt, 2);
916 spin_lock_init(&mc->mca_lock);
917
918 /* initial mode is (EX, empty) */
919 mc->mca_sfmode = MCAST_EXCLUDE;
920 mc->mca_sfcount[MCAST_EXCLUDE] = 1;
921
922 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
923 IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
924 mc->mca_flags |= MAF_NOREPORT;
925
926 mc->next = idev->mc_list;
927 idev->mc_list = mc;
928 write_unlock_bh(&idev->lock);
929
930 mld_del_delrec(idev, &mc->mca_addr);
931 igmp6_group_added(mc);
932 ma_put(mc);
933 return 0;
934}
935
936/*
937 * device multicast group del
938 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900939int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 struct ifmcaddr6 *ma, **map;
942
943 write_lock_bh(&idev->lock);
944 for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) {
945 if (ipv6_addr_equal(&ma->mca_addr, addr)) {
946 if (--ma->mca_users == 0) {
947 *map = ma->next;
948 write_unlock_bh(&idev->lock);
949
950 igmp6_group_dropped(ma);
951
952 ma_put(ma);
953 return 0;
954 }
955 write_unlock_bh(&idev->lock);
956 return 0;
957 }
958 }
959 write_unlock_bh(&idev->lock);
960
961 return -ENOENT;
962}
963
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900964int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 struct inet6_dev *idev = in6_dev_get(dev);
967 int err;
968
969 if (!idev)
970 return -ENODEV;
971
972 err = __ipv6_dev_mc_dec(idev, addr);
973
974 in6_dev_put(idev);
975
976 return err;
977}
978
979/*
980 * identify MLD packets for MLD filter exceptions
981 */
982int ipv6_is_mld(struct sk_buff *skb, int nexthdr)
983{
984 struct icmp6hdr *pic;
985
986 if (nexthdr != IPPROTO_ICMPV6)
987 return 0;
988
989 if (!pskb_may_pull(skb, sizeof(struct icmp6hdr)))
990 return 0;
991
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -0300992 pic = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 switch (pic->icmp6_type) {
995 case ICMPV6_MGM_QUERY:
996 case ICMPV6_MGM_REPORT:
997 case ICMPV6_MGM_REDUCTION:
998 case ICMPV6_MLD2_REPORT:
999 return 1;
1000 default:
1001 break;
1002 }
1003 return 0;
1004}
1005
1006/*
1007 * check if the interface/address pair is valid
1008 */
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001009int ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
1010 const struct in6_addr *src_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
1012 struct inet6_dev *idev;
1013 struct ifmcaddr6 *mc;
1014 int rv = 0;
1015
1016 idev = in6_dev_get(dev);
1017 if (idev) {
1018 read_lock_bh(&idev->lock);
1019 for (mc = idev->mc_list; mc; mc=mc->next) {
1020 if (ipv6_addr_equal(&mc->mca_addr, group))
1021 break;
1022 }
1023 if (mc) {
1024 if (src_addr && !ipv6_addr_any(src_addr)) {
1025 struct ip6_sf_list *psf;
1026
1027 spin_lock_bh(&mc->mca_lock);
1028 for (psf=mc->mca_sources;psf;psf=psf->sf_next) {
1029 if (ipv6_addr_equal(&psf->sf_addr, src_addr))
1030 break;
1031 }
1032 if (psf)
1033 rv = psf->sf_count[MCAST_INCLUDE] ||
1034 psf->sf_count[MCAST_EXCLUDE] !=
1035 mc->mca_sfcount[MCAST_EXCLUDE];
1036 else
1037 rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0;
1038 spin_unlock_bh(&mc->mca_lock);
1039 } else
1040 rv = 1; /* don't filter unspecified source */
1041 }
1042 read_unlock_bh(&idev->lock);
1043 in6_dev_put(idev);
1044 }
1045 return rv;
1046}
1047
1048static void mld_gq_start_timer(struct inet6_dev *idev)
1049{
1050 int tv = net_random() % idev->mc_maxdelay;
1051
1052 idev->mc_gq_running = 1;
1053 if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2))
1054 in6_dev_hold(idev);
1055}
1056
1057static void mld_ifc_start_timer(struct inet6_dev *idev, int delay)
1058{
1059 int tv = net_random() % delay;
1060
1061 if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2))
1062 in6_dev_hold(idev);
1063}
1064
1065/*
1066 * IGMP handling (alias multicast ICMPv6 messages)
1067 */
1068
1069static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime)
1070{
1071 unsigned long delay = resptime;
1072
1073 /* Do not start timer for these addresses */
1074 if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) ||
1075 IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL)
1076 return;
1077
1078 if (del_timer(&ma->mca_timer)) {
1079 atomic_dec(&ma->mca_refcnt);
1080 delay = ma->mca_timer.expires - jiffies;
1081 }
1082
1083 if (delay >= resptime) {
1084 if (resptime)
1085 delay = net_random() % resptime;
1086 else
1087 delay = 1;
1088 }
1089 ma->mca_timer.expires = jiffies + delay;
1090 if (!mod_timer(&ma->mca_timer, jiffies + delay))
1091 atomic_inc(&ma->mca_refcnt);
1092 ma->mca_flags |= MAF_TIMER_RUNNING;
1093}
1094
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001095/* mark EXCLUDE-mode sources */
1096static int mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 struct in6_addr *srcs)
1098{
1099 struct ip6_sf_list *psf;
1100 int i, scount;
1101
1102 scount = 0;
1103 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1104 if (scount == nsrcs)
1105 break;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001106 for (i=0; i<nsrcs; i++) {
1107 /* skip inactive filters */
1108 if (pmc->mca_sfcount[MCAST_INCLUDE] ||
1109 pmc->mca_sfcount[MCAST_EXCLUDE] !=
1110 psf->sf_count[MCAST_EXCLUDE])
1111 continue;
1112 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1113 scount++;
1114 break;
1115 }
1116 }
1117 }
1118 pmc->mca_flags &= ~MAF_GSQUERY;
1119 if (scount == nsrcs) /* all sources excluded */
1120 return 0;
1121 return 1;
1122}
1123
1124static int mld_marksources(struct ifmcaddr6 *pmc, int nsrcs,
1125 struct in6_addr *srcs)
1126{
1127 struct ip6_sf_list *psf;
1128 int i, scount;
1129
1130 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1131 return mld_xmarksources(pmc, nsrcs, srcs);
1132
1133 /* mark INCLUDE-mode sources */
1134
1135 scount = 0;
1136 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1137 if (scount == nsrcs)
1138 break;
1139 for (i=0; i<nsrcs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) {
1141 psf->sf_gsresp = 1;
1142 scount++;
1143 break;
1144 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001147 if (!scount) {
1148 pmc->mca_flags &= ~MAF_GSQUERY;
1149 return 0;
1150 }
1151 pmc->mca_flags |= MAF_GSQUERY;
1152 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
1155int igmp6_event_query(struct sk_buff *skb)
1156{
Yan Zheng97300b52005-10-31 20:09:45 +08001157 struct mld2_query *mlh2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 struct ifmcaddr6 *ma;
1159 struct in6_addr *group;
1160 unsigned long max_delay;
1161 struct inet6_dev *idev;
1162 struct icmp6hdr *hdr;
1163 int group_type;
1164 int mark = 0;
1165 int len;
1166
1167 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1168 return -EINVAL;
1169
1170 /* compute payload length excluding extension headers */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001171 len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001172 len -= skb_network_header_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 /* Drop queries with not link local source */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001175 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return -EINVAL;
1177
1178 idev = in6_dev_get(skb->dev);
1179
1180 if (idev == NULL)
1181 return 0;
1182
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -03001183 hdr = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 group = (struct in6_addr *) (hdr + 1);
1185 group_type = ipv6_addr_type(group);
1186
1187 if (group_type != IPV6_ADDR_ANY &&
1188 !(group_type&IPV6_ADDR_MULTICAST)) {
1189 in6_dev_put(idev);
1190 return -EINVAL;
1191 }
1192
1193 if (len == 24) {
1194 int switchback;
1195 /* MLDv1 router present */
1196
1197 /* Translate milliseconds to jiffies */
1198 max_delay = (ntohs(hdr->icmp6_maxdelay)*HZ)/1000;
1199
1200 switchback = (idev->mc_qrv + 1) * max_delay;
1201 idev->mc_v1_seen = jiffies + switchback;
1202
1203 /* cancel the interface change timer */
1204 idev->mc_ifc_count = 0;
1205 if (del_timer(&idev->mc_ifc_timer))
1206 __in6_dev_put(idev);
1207 /* clear deleted report items */
1208 mld_clear_delrec(idev);
1209 } else if (len >= 28) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001210 int srcs_offset = sizeof(struct mld2_query) -
Yan Zheng97300b52005-10-31 20:09:45 +08001211 sizeof(struct icmp6hdr);
1212 if (!pskb_may_pull(skb, srcs_offset)) {
1213 in6_dev_put(idev);
1214 return -EINVAL;
1215 }
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001216 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 max_delay = (MLDV2_MRC(ntohs(mlh2->mrc))*HZ)/1000;
1218 if (!max_delay)
1219 max_delay = 1;
1220 idev->mc_maxdelay = max_delay;
1221 if (mlh2->qrv)
1222 idev->mc_qrv = mlh2->qrv;
1223 if (group_type == IPV6_ADDR_ANY) { /* general query */
1224 if (mlh2->nsrcs) {
1225 in6_dev_put(idev);
1226 return -EINVAL; /* no sources allowed */
1227 }
1228 mld_gq_start_timer(idev);
1229 in6_dev_put(idev);
1230 return 0;
1231 }
1232 /* mark sources to include, if group & source-specific */
Yan Zheng97300b52005-10-31 20:09:45 +08001233 if (mlh2->nsrcs != 0) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001234 if (!pskb_may_pull(skb, srcs_offset +
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001235 ntohs(mlh2->nsrcs) * sizeof(struct in6_addr))) {
Yan Zheng97300b52005-10-31 20:09:45 +08001236 in6_dev_put(idev);
1237 return -EINVAL;
1238 }
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001239 mlh2 = (struct mld2_query *)skb_transport_header(skb);
Yan Zheng97300b52005-10-31 20:09:45 +08001240 mark = 1;
1241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 } else {
1243 in6_dev_put(idev);
1244 return -EINVAL;
1245 }
1246
1247 read_lock_bh(&idev->lock);
1248 if (group_type == IPV6_ADDR_ANY) {
1249 for (ma = idev->mc_list; ma; ma=ma->next) {
1250 spin_lock_bh(&ma->mca_lock);
1251 igmp6_group_queried(ma, max_delay);
1252 spin_unlock_bh(&ma->mca_lock);
1253 }
1254 } else {
1255 for (ma = idev->mc_list; ma; ma=ma->next) {
David L Stevens7add2a42006-01-24 13:06:39 -08001256 if (!ipv6_addr_equal(group, &ma->mca_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 continue;
1258 spin_lock_bh(&ma->mca_lock);
1259 if (ma->mca_flags & MAF_TIMER_RUNNING) {
1260 /* gsquery <- gsquery && mark */
1261 if (!mark)
1262 ma->mca_flags &= ~MAF_GSQUERY;
1263 } else {
1264 /* gsquery <- mark */
1265 if (mark)
1266 ma->mca_flags |= MAF_GSQUERY;
1267 else
1268 ma->mca_flags &= ~MAF_GSQUERY;
1269 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001270 if (!(ma->mca_flags & MAF_GSQUERY) ||
David L Stevens7add2a42006-01-24 13:06:39 -08001271 mld_marksources(ma, ntohs(mlh2->nsrcs), mlh2->srcs))
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001272 igmp6_group_queried(ma, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 spin_unlock_bh(&ma->mca_lock);
David L Stevens7add2a42006-01-24 13:06:39 -08001274 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
1276 }
1277 read_unlock_bh(&idev->lock);
1278 in6_dev_put(idev);
1279
1280 return 0;
1281}
1282
1283
1284int igmp6_event_report(struct sk_buff *skb)
1285{
1286 struct ifmcaddr6 *ma;
1287 struct in6_addr *addrp;
1288 struct inet6_dev *idev;
1289 struct icmp6hdr *hdr;
1290 int addr_type;
1291
1292 /* Our own report looped back. Ignore it. */
1293 if (skb->pkt_type == PACKET_LOOPBACK)
1294 return 0;
1295
David Stevens24c69272005-12-02 20:32:59 -08001296 /* send our report if the MC router may not have heard this report */
1297 if (skb->pkt_type != PACKET_MULTICAST &&
1298 skb->pkt_type != PACKET_BROADCAST)
1299 return 0;
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
1302 return -EINVAL;
1303
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -03001304 hdr = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 /* Drop reports with not link local source */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001307 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001308 if (addr_type != IPV6_ADDR_ANY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 !(addr_type&IPV6_ADDR_LINKLOCAL))
1310 return -EINVAL;
1311
1312 addrp = (struct in6_addr *) (hdr + 1);
1313
1314 idev = in6_dev_get(skb->dev);
1315 if (idev == NULL)
1316 return -ENODEV;
1317
1318 /*
1319 * Cancel the timer for this group
1320 */
1321
1322 read_lock_bh(&idev->lock);
1323 for (ma = idev->mc_list; ma; ma=ma->next) {
1324 if (ipv6_addr_equal(&ma->mca_addr, addrp)) {
1325 spin_lock(&ma->mca_lock);
1326 if (del_timer(&ma->mca_timer))
1327 atomic_dec(&ma->mca_refcnt);
1328 ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING);
1329 spin_unlock(&ma->mca_lock);
1330 break;
1331 }
1332 }
1333 read_unlock_bh(&idev->lock);
1334 in6_dev_put(idev);
1335 return 0;
1336}
1337
1338static int is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type,
1339 int gdeleted, int sdeleted)
1340{
1341 switch (type) {
1342 case MLD2_MODE_IS_INCLUDE:
1343 case MLD2_MODE_IS_EXCLUDE:
1344 if (gdeleted || sdeleted)
1345 return 0;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001346 if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) {
1347 if (pmc->mca_sfmode == MCAST_INCLUDE)
1348 return 1;
1349 /* don't include if this source is excluded
1350 * in all filters
1351 */
1352 if (psf->sf_count[MCAST_INCLUDE])
David L Stevens7add2a42006-01-24 13:06:39 -08001353 return type == MLD2_MODE_IS_INCLUDE;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001354 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1355 psf->sf_count[MCAST_EXCLUDE];
1356 }
1357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 case MLD2_CHANGE_TO_INCLUDE:
1359 if (gdeleted || sdeleted)
1360 return 0;
1361 return psf->sf_count[MCAST_INCLUDE] != 0;
1362 case MLD2_CHANGE_TO_EXCLUDE:
1363 if (gdeleted || sdeleted)
1364 return 0;
1365 if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 ||
1366 psf->sf_count[MCAST_INCLUDE])
1367 return 0;
1368 return pmc->mca_sfcount[MCAST_EXCLUDE] ==
1369 psf->sf_count[MCAST_EXCLUDE];
1370 case MLD2_ALLOW_NEW_SOURCES:
1371 if (gdeleted || !psf->sf_crcount)
1372 return 0;
1373 return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted;
1374 case MLD2_BLOCK_OLD_SOURCES:
1375 if (pmc->mca_sfmode == MCAST_INCLUDE)
1376 return gdeleted || (psf->sf_crcount && sdeleted);
1377 return psf->sf_crcount && !gdeleted && !sdeleted;
1378 }
1379 return 0;
1380}
1381
1382static int
1383mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted)
1384{
1385 struct ip6_sf_list *psf;
1386 int scount = 0;
1387
1388 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1389 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1390 continue;
1391 scount++;
1392 }
1393 return scount;
1394}
1395
1396static struct sk_buff *mld_newpack(struct net_device *dev, int size)
1397{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001398 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001399 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 struct sk_buff *skb;
1401 struct mld2_report *pmr;
1402 struct in6_addr addr_buf;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001403 const struct in6_addr *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 int err;
1405 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1406 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1407 IPV6_TLV_PADN, 0 };
1408
1409 /* we assume size > sizeof(ra) here */
Johannes Bergf5184d22008-05-12 20:48:31 -07001410 skb = sock_alloc_send_skb(sk, size + LL_ALLOCATED_SPACE(dev), 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001412 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return NULL;
1414
1415 skb_reserve(skb, LL_RESERVED_SPACE(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Neil Horman95c385b2007-04-25 17:08:10 -07001417 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001419 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 * when a valid link-local address is not available.
1421 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001422 saddr = &in6addr_any;
1423 } else
1424 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001426 ip6_nd_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1429
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001430 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -03001431 skb_put(skb, sizeof(*pmr));
1432 pmr = (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 pmr->type = ICMPV6_MLD2_REPORT;
1434 pmr->resv1 = 0;
1435 pmr->csum = 0;
1436 pmr->resv2 = 0;
1437 pmr->ngrec = 0;
1438 return skb;
1439}
1440
1441static void mld_sendpack(struct sk_buff *skb)
1442{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001443 struct ipv6hdr *pip6 = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001444 struct mld2_report *pmr =
1445 (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 int payload_len, mldlen;
1447 struct inet6_dev *idev = in6_dev_get(skb->dev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001448 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 int err;
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001450 struct flowi fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001452 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTREQUESTS);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001453 payload_len = (skb->tail - skb->network_header) - sizeof(*pip6);
1454 mldlen = skb->tail - skb->transport_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 pip6->payload_len = htons(payload_len);
1456
1457 pmr->csum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen,
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001458 IPPROTO_ICMPV6, csum_partial(skb_transport_header(skb),
1459 mldlen, 0));
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001460
1461 skb->dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
1462
1463 if (!skb->dst) {
1464 err = -ENOMEM;
1465 goto err_out;
1466 }
1467
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001468 icmpv6_flow_init(net->ipv6.igmp_sk, &fl, ICMPV6_MLD2_REPORT,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001469 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1470 skb->dev->ifindex);
1471
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001472 err = xfrm_lookup(net, &skb->dst, &fl, NULL, 0);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001473 if (err)
1474 goto err_out;
1475
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001476 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001477 dst_output);
1478out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (!err) {
Denis V. Lunev5a57d4c2008-10-08 10:34:14 -07001480 ICMP6MSGOUT_INC_STATS_BH(net, idev, ICMPV6_MLD2_REPORT);
Denis V. Luneve41b5362008-10-08 10:33:26 -07001481 ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
Denis V. Lunev483a47d2008-10-08 11:09:27 -07001482 IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_OUTMCASTPKTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 } else
Denis V. Lunev483a47d2008-10-08 11:09:27 -07001484 IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 if (likely(idev != NULL))
1487 in6_dev_put(idev);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001488 return;
1489
1490err_out:
1491 kfree_skb(skb);
1492 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
1495static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel)
1496{
Yan Zhengfab10fe2005-10-05 12:08:13 -07001497 return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
1500static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1501 int type, struct mld2_grec **ppgr)
1502{
1503 struct net_device *dev = pmc->idev->dev;
1504 struct mld2_report *pmr;
1505 struct mld2_grec *pgr;
1506
1507 if (!skb)
1508 skb = mld_newpack(dev, dev->mtu);
1509 if (!skb)
1510 return NULL;
1511 pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec));
1512 pgr->grec_type = type;
1513 pgr->grec_auxwords = 0;
1514 pgr->grec_nsrcs = 0;
1515 pgr->grec_mca = pmc->mca_addr; /* structure copy */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001516 pmr = (struct mld2_report *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 pmr->ngrec = htons(ntohs(pmr->ngrec)+1);
1518 *ppgr = pgr;
1519 return skb;
1520}
1521
1522#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1523 skb_tailroom(skb)) : 0)
1524
1525static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc,
1526 int type, int gdeleted, int sdeleted)
1527{
1528 struct net_device *dev = pmc->idev->dev;
1529 struct mld2_report *pmr;
1530 struct mld2_grec *pgr = NULL;
1531 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001532 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 if (pmc->mca_flags & MAF_NOREPORT)
1535 return skb;
1536
1537 isquery = type == MLD2_MODE_IS_INCLUDE ||
1538 type == MLD2_MODE_IS_EXCLUDE;
1539 truncate = type == MLD2_MODE_IS_EXCLUDE ||
1540 type == MLD2_CHANGE_TO_EXCLUDE;
1541
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001542 stotal = scount = 0;
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources;
1545
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001546 if (!*psf_list)
1547 goto empty_source;
1548
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001549 pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 /* EX and TO_EX get a fresh packet, if needed */
1552 if (truncate) {
1553 if (pmr && pmr->ngrec &&
1554 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1555 if (skb)
1556 mld_sendpack(skb);
1557 skb = mld_newpack(dev, dev->mtu);
1558 }
1559 }
1560 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 psf_prev = NULL;
1562 for (psf=*psf_list; psf; psf=psf_next) {
1563 struct in6_addr *psrc;
1564
1565 psf_next = psf->sf_next;
1566
1567 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1568 psf_prev = psf;
1569 continue;
1570 }
1571
1572 /* clear marks on query responses */
1573 if (isquery)
1574 psf->sf_gsresp = 0;
1575
1576 if (AVAILABLE(skb) < sizeof(*psrc) +
1577 first*sizeof(struct mld2_grec)) {
1578 if (truncate && !first)
1579 break; /* truncate these */
1580 if (pgr)
1581 pgr->grec_nsrcs = htons(scount);
1582 if (skb)
1583 mld_sendpack(skb);
1584 skb = mld_newpack(dev, dev->mtu);
1585 first = 1;
1586 scount = 0;
1587 }
1588 if (first) {
1589 skb = add_grhead(skb, pmc, type, &pgr);
1590 first = 0;
1591 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -08001592 if (!skb)
1593 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc));
1595 *psrc = psf->sf_addr;
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001596 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if ((type == MLD2_ALLOW_NEW_SOURCES ||
1598 type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
1599 psf->sf_crcount--;
1600 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
1601 if (psf_prev)
1602 psf_prev->sf_next = psf->sf_next;
1603 else
1604 *psf_list = psf->sf_next;
1605 kfree(psf);
1606 continue;
1607 }
1608 }
1609 psf_prev = psf;
1610 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001611
1612empty_source:
1613 if (!stotal) {
1614 if (type == MLD2_ALLOW_NEW_SOURCES ||
1615 type == MLD2_BLOCK_OLD_SOURCES)
1616 return skb;
1617 if (pmc->mca_crcount || isquery) {
1618 /* make sure we have room for group header */
1619 if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) {
1620 mld_sendpack(skb);
1621 skb = NULL; /* add_grhead will get a new one */
1622 }
1623 skb = add_grhead(skb, pmc, type, &pgr);
1624 }
1625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (pgr)
1627 pgr->grec_nsrcs = htons(scount);
1628
1629 if (isquery)
1630 pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */
1631 return skb;
1632}
1633
1634static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc)
1635{
1636 struct sk_buff *skb = NULL;
1637 int type;
1638
1639 if (!pmc) {
1640 read_lock_bh(&idev->lock);
1641 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1642 if (pmc->mca_flags & MAF_NOREPORT)
1643 continue;
1644 spin_lock_bh(&pmc->mca_lock);
1645 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1646 type = MLD2_MODE_IS_EXCLUDE;
1647 else
1648 type = MLD2_MODE_IS_INCLUDE;
1649 skb = add_grec(skb, pmc, type, 0, 0);
1650 spin_unlock_bh(&pmc->mca_lock);
1651 }
1652 read_unlock_bh(&idev->lock);
1653 } else {
1654 spin_lock_bh(&pmc->mca_lock);
1655 if (pmc->mca_sfcount[MCAST_EXCLUDE])
1656 type = MLD2_MODE_IS_EXCLUDE;
1657 else
1658 type = MLD2_MODE_IS_INCLUDE;
1659 skb = add_grec(skb, pmc, type, 0, 0);
1660 spin_unlock_bh(&pmc->mca_lock);
1661 }
1662 if (skb)
1663 mld_sendpack(skb);
1664}
1665
1666/*
1667 * remove zero-count source records from a source filter list
1668 */
1669static void mld_clear_zeros(struct ip6_sf_list **ppsf)
1670{
1671 struct ip6_sf_list *psf_prev, *psf_next, *psf;
1672
1673 psf_prev = NULL;
1674 for (psf=*ppsf; psf; psf = psf_next) {
1675 psf_next = psf->sf_next;
1676 if (psf->sf_crcount == 0) {
1677 if (psf_prev)
1678 psf_prev->sf_next = psf->sf_next;
1679 else
1680 *ppsf = psf->sf_next;
1681 kfree(psf);
1682 } else
1683 psf_prev = psf;
1684 }
1685}
1686
1687static void mld_send_cr(struct inet6_dev *idev)
1688{
1689 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1690 struct sk_buff *skb = NULL;
1691 int type, dtype;
1692
1693 read_lock_bh(&idev->lock);
1694 write_lock_bh(&idev->mc_lock);
1695
1696 /* deleted MCA's */
1697 pmc_prev = NULL;
1698 for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) {
1699 pmc_next = pmc->next;
1700 if (pmc->mca_sfmode == MCAST_INCLUDE) {
1701 type = MLD2_BLOCK_OLD_SOURCES;
1702 dtype = MLD2_BLOCK_OLD_SOURCES;
1703 skb = add_grec(skb, pmc, type, 1, 0);
1704 skb = add_grec(skb, pmc, dtype, 1, 1);
1705 }
1706 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (pmc->mca_sfmode == MCAST_EXCLUDE) {
1708 type = MLD2_CHANGE_TO_INCLUDE;
1709 skb = add_grec(skb, pmc, type, 1, 0);
1710 }
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001711 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (pmc->mca_crcount == 0) {
1713 mld_clear_zeros(&pmc->mca_tomb);
1714 mld_clear_zeros(&pmc->mca_sources);
1715 }
1716 }
1717 if (pmc->mca_crcount == 0 && !pmc->mca_tomb &&
1718 !pmc->mca_sources) {
1719 if (pmc_prev)
1720 pmc_prev->next = pmc_next;
1721 else
1722 idev->mc_tomb = pmc_next;
1723 in6_dev_put(pmc->idev);
1724 kfree(pmc);
1725 } else
1726 pmc_prev = pmc;
1727 }
1728 write_unlock_bh(&idev->mc_lock);
1729
1730 /* change recs */
1731 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1732 spin_lock_bh(&pmc->mca_lock);
1733 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1734 type = MLD2_BLOCK_OLD_SOURCES;
1735 dtype = MLD2_ALLOW_NEW_SOURCES;
1736 } else {
1737 type = MLD2_ALLOW_NEW_SOURCES;
1738 dtype = MLD2_BLOCK_OLD_SOURCES;
1739 }
1740 skb = add_grec(skb, pmc, type, 0, 0);
1741 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
1742
1743 /* filter mode changes */
1744 if (pmc->mca_crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (pmc->mca_sfmode == MCAST_EXCLUDE)
1746 type = MLD2_CHANGE_TO_EXCLUDE;
1747 else
1748 type = MLD2_CHANGE_TO_INCLUDE;
1749 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevens5ab4a6c2005-12-27 14:03:00 -08001750 pmc->mca_crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
1752 spin_unlock_bh(&pmc->mca_lock);
1753 }
1754 read_unlock_bh(&idev->lock);
1755 if (!skb)
1756 return;
1757 (void) mld_sendpack(skb);
1758}
1759
1760static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1761{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001762 struct net *net = dev_net(dev);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001763 struct sock *sk = net->ipv6.igmp_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 struct inet6_dev *idev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001765 struct sk_buff *skb;
1766 struct icmp6hdr *hdr;
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001767 const struct in6_addr *snd_addr, *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 struct in6_addr *addrp;
1769 struct in6_addr addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 int err, len, payload_len, full_len;
1771 u8 ra[8] = { IPPROTO_ICMPV6, 0,
1772 IPV6_TLV_ROUTERALERT, 2, 0, 0,
1773 IPV6_TLV_PADN, 0 };
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001774 struct flowi fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001776 rcu_read_lock();
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001777 IP6_INC_STATS(net, __in6_dev_get(dev),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001778 IPSTATS_MIB_OUTREQUESTS);
1779 rcu_read_unlock();
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09001780 if (type == ICMPV6_MGM_REDUCTION)
1781 snd_addr = &in6addr_linklocal_allrouters;
1782 else
1783 snd_addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785 len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
1786 payload_len = len + sizeof(ra);
1787 full_len = sizeof(struct ipv6hdr) + payload_len;
1788
Johannes Bergf5184d22008-05-12 20:48:31 -07001789 skb = sock_alloc_send_skb(sk, LL_ALLOCATED_SPACE(dev) + full_len, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791 if (skb == NULL) {
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001792 rcu_read_lock();
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001793 IP6_INC_STATS(net, __in6_dev_get(dev),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +09001794 IPSTATS_MIB_OUTDISCARDS);
1795 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return;
1797 }
1798
1799 skb_reserve(skb, LL_RESERVED_SPACE(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Neil Horman95c385b2007-04-25 17:08:10 -07001801 if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 /* <draft-ietf-magma-mld-source-05.txt>:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001803 * use unspecified address as the source address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 * when a valid link-local address is not available.
1805 */
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001806 saddr = &in6addr_any;
1807 } else
1808 saddr = &addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001810 ip6_nd_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra));
1813
1814 hdr = (struct icmp6hdr *) skb_put(skb, sizeof(struct icmp6hdr));
1815 memset(hdr, 0, sizeof(struct icmp6hdr));
1816 hdr->icmp6_type = type;
1817
1818 addrp = (struct in6_addr *) skb_put(skb, sizeof(struct in6_addr));
1819 ipv6_addr_copy(addrp, addr);
1820
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09001821 hdr->icmp6_cksum = csum_ipv6_magic(saddr, snd_addr, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 IPPROTO_ICMPV6,
Joe Perches07f07572008-11-19 15:44:53 -08001823 csum_partial(hdr, len, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 idev = in6_dev_get(skb->dev);
1826
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001827 skb->dst = icmp6_dst_alloc(skb->dev, NULL, &ipv6_hdr(skb)->daddr);
1828 if (!skb->dst) {
1829 err = -ENOMEM;
1830 goto err_out;
1831 }
1832
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08001833 icmpv6_flow_init(sk, &fl, type,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001834 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
1835 skb->dev->ifindex);
1836
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001837 err = xfrm_lookup(net, &skb->dst, &fl, NULL, 0);
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001838 if (err)
1839 goto err_out;
1840
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001841 err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev,
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001842 dst_output);
1843out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (!err) {
Denis V. Lunev5c5d2442008-10-08 10:33:50 -07001845 ICMP6MSGOUT_INC_STATS(net, idev, type);
Denis V. Luneva862f6a2008-10-08 10:33:06 -07001846 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001847 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTMCASTPKTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 } else
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001849 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 if (likely(idev != NULL))
1852 in6_dev_put(idev);
1853 return;
YOSHIFUJI Hideaki41927172007-12-06 17:40:56 -08001854
1855err_out:
1856 kfree_skb(skb);
1857 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
1859
1860static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
1861 struct in6_addr *psfsrc)
1862{
1863 struct ip6_sf_list *psf, *psf_prev;
1864 int rv = 0;
1865
1866 psf_prev = NULL;
1867 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1868 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1869 break;
1870 psf_prev = psf;
1871 }
1872 if (!psf || psf->sf_count[sfmode] == 0) {
1873 /* source filter not found, or count wrong => bug */
1874 return -ESRCH;
1875 }
1876 psf->sf_count[sfmode]--;
1877 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1878 struct inet6_dev *idev = pmc->idev;
1879
1880 /* no more filters for this source */
1881 if (psf_prev)
1882 psf_prev->sf_next = psf->sf_next;
1883 else
1884 pmc->mca_sources = psf->sf_next;
1885 if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
1886 !MLD_V1_SEEN(idev)) {
1887 psf->sf_crcount = idev->mc_qrv;
1888 psf->sf_next = pmc->mca_tomb;
1889 pmc->mca_tomb = psf;
1890 rv = 1;
1891 } else
1892 kfree(psf);
1893 }
1894 return rv;
1895}
1896
1897static int ip6_mc_del_src(struct inet6_dev *idev, struct in6_addr *pmca,
1898 int sfmode, int sfcount, struct in6_addr *psfsrc,
1899 int delta)
1900{
1901 struct ifmcaddr6 *pmc;
1902 int changerec = 0;
1903 int i, err;
1904
1905 if (!idev)
1906 return -ENODEV;
1907 read_lock_bh(&idev->lock);
1908 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
1909 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
1910 break;
1911 }
1912 if (!pmc) {
1913 /* MCA not found?? bug */
1914 read_unlock_bh(&idev->lock);
1915 return -ESRCH;
1916 }
1917 spin_lock_bh(&pmc->mca_lock);
1918 sf_markstate(pmc);
1919 if (!delta) {
1920 if (!pmc->mca_sfcount[sfmode]) {
1921 spin_unlock_bh(&pmc->mca_lock);
1922 read_unlock_bh(&idev->lock);
1923 return -EINVAL;
1924 }
1925 pmc->mca_sfcount[sfmode]--;
1926 }
1927 err = 0;
1928 for (i=0; i<sfcount; i++) {
1929 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1930
1931 changerec |= rv > 0;
1932 if (!err && rv < 0)
1933 err = rv;
1934 }
1935 if (pmc->mca_sfmode == MCAST_EXCLUDE &&
1936 pmc->mca_sfcount[MCAST_EXCLUDE] == 0 &&
1937 pmc->mca_sfcount[MCAST_INCLUDE]) {
1938 struct ip6_sf_list *psf;
1939
1940 /* filter mode change */
1941 pmc->mca_sfmode = MCAST_INCLUDE;
1942 pmc->mca_crcount = idev->mc_qrv;
1943 idev->mc_ifc_count = pmc->mca_crcount;
1944 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
1945 psf->sf_crcount = 0;
1946 mld_ifc_event(pmc->idev);
1947 } else if (sf_setstate(pmc) || changerec)
1948 mld_ifc_event(pmc->idev);
1949 spin_unlock_bh(&pmc->mca_lock);
1950 read_unlock_bh(&idev->lock);
1951 return err;
1952}
1953
1954/*
1955 * Add multicast single-source filter to the interface list
1956 */
1957static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
1958 struct in6_addr *psfsrc, int delta)
1959{
1960 struct ip6_sf_list *psf, *psf_prev;
1961
1962 psf_prev = NULL;
1963 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
1964 if (ipv6_addr_equal(&psf->sf_addr, psfsrc))
1965 break;
1966 psf_prev = psf;
1967 }
1968 if (!psf) {
Ingo Oeser0c600ed2006-03-20 23:01:32 -08001969 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (!psf)
1971 return -ENOBUFS;
Ingo Oeser0c600ed2006-03-20 23:01:32 -08001972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 psf->sf_addr = *psfsrc;
1974 if (psf_prev) {
1975 psf_prev->sf_next = psf;
1976 } else
1977 pmc->mca_sources = psf;
1978 }
1979 psf->sf_count[sfmode]++;
1980 return 0;
1981}
1982
1983static void sf_markstate(struct ifmcaddr6 *pmc)
1984{
1985 struct ip6_sf_list *psf;
1986 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
1987
1988 for (psf=pmc->mca_sources; psf; psf=psf->sf_next)
1989 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
1990 psf->sf_oldin = mca_xcount ==
1991 psf->sf_count[MCAST_EXCLUDE] &&
1992 !psf->sf_count[MCAST_INCLUDE];
1993 } else
1994 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1995}
1996
1997static int sf_setstate(struct ifmcaddr6 *pmc)
1998{
David L Stevens7add2a42006-01-24 13:06:39 -08001999 struct ip6_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE];
2001 int qrv = pmc->idev->mc_qrv;
2002 int new_in, rv;
2003
2004 rv = 0;
2005 for (psf=pmc->mca_sources; psf; psf=psf->sf_next) {
2006 if (pmc->mca_sfcount[MCAST_EXCLUDE]) {
2007 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2008 !psf->sf_count[MCAST_INCLUDE];
2009 } else
2010 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevens7add2a42006-01-24 13:06:39 -08002011 if (new_in) {
2012 if (!psf->sf_oldin) {
Al Viroe80e28b2006-02-03 20:10:03 -05002013 struct ip6_sf_list *prev = NULL;
David L Stevens7add2a42006-01-24 13:06:39 -08002014
2015 for (dpsf=pmc->mca_tomb; dpsf;
2016 dpsf=dpsf->sf_next) {
2017 if (ipv6_addr_equal(&dpsf->sf_addr,
2018 &psf->sf_addr))
2019 break;
2020 prev = dpsf;
2021 }
2022 if (dpsf) {
2023 if (prev)
2024 prev->sf_next = dpsf->sf_next;
2025 else
2026 pmc->mca_tomb = dpsf->sf_next;
2027 kfree(dpsf);
2028 }
2029 psf->sf_crcount = qrv;
2030 rv++;
2031 }
2032 } else if (psf->sf_oldin) {
2033 psf->sf_crcount = 0;
2034 /*
2035 * add or update "delete" records if an active filter
2036 * is now inactive
2037 */
2038 for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next)
2039 if (ipv6_addr_equal(&dpsf->sf_addr,
2040 &psf->sf_addr))
2041 break;
2042 if (!dpsf) {
2043 dpsf = (struct ip6_sf_list *)
2044 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2045 if (!dpsf)
2046 continue;
2047 *dpsf = *psf;
2048 /* pmc->mca_lock held by callers */
2049 dpsf->sf_next = pmc->mca_tomb;
2050 pmc->mca_tomb = dpsf;
2051 }
2052 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 rv++;
2054 }
2055 }
2056 return rv;
2057}
2058
2059/*
2060 * Add multicast source filter list to the interface list
2061 */
2062static int ip6_mc_add_src(struct inet6_dev *idev, struct in6_addr *pmca,
2063 int sfmode, int sfcount, struct in6_addr *psfsrc,
2064 int delta)
2065{
2066 struct ifmcaddr6 *pmc;
2067 int isexclude;
2068 int i, err;
2069
2070 if (!idev)
2071 return -ENODEV;
2072 read_lock_bh(&idev->lock);
2073 for (pmc=idev->mc_list; pmc; pmc=pmc->next) {
2074 if (ipv6_addr_equal(pmca, &pmc->mca_addr))
2075 break;
2076 }
2077 if (!pmc) {
2078 /* MCA not found?? bug */
2079 read_unlock_bh(&idev->lock);
2080 return -ESRCH;
2081 }
2082 spin_lock_bh(&pmc->mca_lock);
2083
2084 sf_markstate(pmc);
2085 isexclude = pmc->mca_sfmode == MCAST_EXCLUDE;
2086 if (!delta)
2087 pmc->mca_sfcount[sfmode]++;
2088 err = 0;
2089 for (i=0; i<sfcount; i++) {
2090 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
2091 if (err)
2092 break;
2093 }
2094 if (err) {
2095 int j;
2096
2097 if (!delta)
2098 pmc->mca_sfcount[sfmode]--;
2099 for (j=0; j<i; j++)
2100 (void) ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
2101 } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) {
2102 struct inet6_dev *idev = pmc->idev;
2103 struct ip6_sf_list *psf;
2104
2105 /* filter mode change */
2106 if (pmc->mca_sfcount[MCAST_EXCLUDE])
2107 pmc->mca_sfmode = MCAST_EXCLUDE;
2108 else if (pmc->mca_sfcount[MCAST_INCLUDE])
2109 pmc->mca_sfmode = MCAST_INCLUDE;
2110 /* else no filters; keep old mode for reports */
2111
2112 pmc->mca_crcount = idev->mc_qrv;
2113 idev->mc_ifc_count = pmc->mca_crcount;
2114 for (psf=pmc->mca_sources; psf; psf = psf->sf_next)
2115 psf->sf_crcount = 0;
2116 mld_ifc_event(idev);
2117 } else if (sf_setstate(pmc))
2118 mld_ifc_event(idev);
2119 spin_unlock_bh(&pmc->mca_lock);
2120 read_unlock_bh(&idev->lock);
2121 return err;
2122}
2123
2124static void ip6_mc_clear_src(struct ifmcaddr6 *pmc)
2125{
2126 struct ip6_sf_list *psf, *nextpsf;
2127
2128 for (psf=pmc->mca_tomb; psf; psf=nextpsf) {
2129 nextpsf = psf->sf_next;
2130 kfree(psf);
2131 }
2132 pmc->mca_tomb = NULL;
2133 for (psf=pmc->mca_sources; psf; psf=nextpsf) {
2134 nextpsf = psf->sf_next;
2135 kfree(psf);
2136 }
2137 pmc->mca_sources = NULL;
2138 pmc->mca_sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002139 pmc->mca_sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 pmc->mca_sfcount[MCAST_EXCLUDE] = 1;
2141}
2142
2143
2144static void igmp6_join_group(struct ifmcaddr6 *ma)
2145{
2146 unsigned long delay;
2147
2148 if (ma->mca_flags & MAF_NOREPORT)
2149 return;
2150
2151 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2152
2153 delay = net_random() % IGMP6_UNSOLICITED_IVAL;
2154
2155 spin_lock_bh(&ma->mca_lock);
2156 if (del_timer(&ma->mca_timer)) {
2157 atomic_dec(&ma->mca_refcnt);
2158 delay = ma->mca_timer.expires - jiffies;
2159 }
2160
2161 if (!mod_timer(&ma->mca_timer, jiffies + delay))
2162 atomic_inc(&ma->mca_refcnt);
2163 ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER;
2164 spin_unlock_bh(&ma->mca_lock);
2165}
2166
2167static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
2168 struct inet6_dev *idev)
2169{
2170 int err;
2171
David L Stevens5ab4a6c2005-12-27 14:03:00 -08002172 /* callers have the socket lock and a write lock on ipv6_sk_mc_lock,
2173 * so no other readers or writers of iml or its sflist
2174 */
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002175 if (!iml->sflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 /* any-source empty exclude case */
2177 return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0);
2178 }
2179 err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode,
2180 iml->sflist->sl_count, iml->sflist->sl_addr, 0);
2181 sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max));
2182 iml->sflist = NULL;
2183 return err;
2184}
2185
2186static void igmp6_leave_group(struct ifmcaddr6 *ma)
2187{
2188 if (MLD_V1_SEEN(ma->idev)) {
2189 if (ma->mca_flags & MAF_LAST_REPORTER)
2190 igmp6_send(&ma->mca_addr, ma->idev->dev,
2191 ICMPV6_MGM_REDUCTION);
2192 } else {
2193 mld_add_delrec(ma->idev, ma);
2194 mld_ifc_event(ma->idev);
2195 }
2196}
2197
2198static void mld_gq_timer_expire(unsigned long data)
2199{
2200 struct inet6_dev *idev = (struct inet6_dev *)data;
2201
2202 idev->mc_gq_running = 0;
2203 mld_send_report(idev, NULL);
2204 __in6_dev_put(idev);
2205}
2206
2207static void mld_ifc_timer_expire(unsigned long data)
2208{
2209 struct inet6_dev *idev = (struct inet6_dev *)data;
2210
2211 mld_send_cr(idev);
2212 if (idev->mc_ifc_count) {
2213 idev->mc_ifc_count--;
2214 if (idev->mc_ifc_count)
2215 mld_ifc_start_timer(idev, idev->mc_maxdelay);
2216 }
2217 __in6_dev_put(idev);
2218}
2219
2220static void mld_ifc_event(struct inet6_dev *idev)
2221{
2222 if (MLD_V1_SEEN(idev))
2223 return;
2224 idev->mc_ifc_count = idev->mc_qrv;
2225 mld_ifc_start_timer(idev, 1);
2226}
2227
2228
2229static void igmp6_timer_handler(unsigned long data)
2230{
2231 struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data;
2232
2233 if (MLD_V1_SEEN(ma->idev))
2234 igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT);
2235 else
2236 mld_send_report(ma->idev, ma);
2237
2238 spin_lock(&ma->mca_lock);
2239 ma->mca_flags |= MAF_LAST_REPORTER;
2240 ma->mca_flags &= ~MAF_TIMER_RUNNING;
2241 spin_unlock(&ma->mca_lock);
2242 ma_put(ma);
2243}
2244
2245/* Device going down */
2246
2247void ipv6_mc_down(struct inet6_dev *idev)
2248{
2249 struct ifmcaddr6 *i;
2250
2251 /* Withdraw multicast list */
2252
2253 read_lock_bh(&idev->lock);
2254 idev->mc_ifc_count = 0;
2255 if (del_timer(&idev->mc_ifc_timer))
2256 __in6_dev_put(idev);
2257 idev->mc_gq_running = 0;
2258 if (del_timer(&idev->mc_gq_timer))
2259 __in6_dev_put(idev);
2260
2261 for (i = idev->mc_list; i; i=i->next)
2262 igmp6_group_dropped(i);
2263 read_unlock_bh(&idev->lock);
2264
2265 mld_clear_delrec(idev);
2266}
2267
2268
2269/* Device going up */
2270
2271void ipv6_mc_up(struct inet6_dev *idev)
2272{
2273 struct ifmcaddr6 *i;
2274
2275 /* Install multicast list, except for all-nodes (already installed) */
2276
2277 read_lock_bh(&idev->lock);
2278 for (i = idev->mc_list; i; i=i->next)
2279 igmp6_group_added(i);
2280 read_unlock_bh(&idev->lock);
2281}
2282
2283/* IPv6 device initialization. */
2284
2285void ipv6_mc_init_dev(struct inet6_dev *idev)
2286{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 write_lock_bh(&idev->lock);
2288 rwlock_init(&idev->mc_lock);
2289 idev->mc_gq_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002290 setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire,
2291 (unsigned long)idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 idev->mc_tomb = NULL;
2293 idev->mc_ifc_count = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08002294 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2295 (unsigned long)idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 idev->mc_qrv = MLD_QRV_DEFAULT;
2297 idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL;
2298 idev->mc_v1_seen = 0;
2299 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300}
2301
2302/*
2303 * Device is about to be destroyed: clean up.
2304 */
2305
2306void ipv6_mc_destroy_dev(struct inet6_dev *idev)
2307{
2308 struct ifmcaddr6 *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
2310 /* Deactivate timers */
2311 ipv6_mc_down(idev);
2312
2313 /* Delete all-nodes address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 /* We cannot call ipv6_dev_mc_dec() directly, our caller in
2315 * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will
2316 * fail.
2317 */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002318 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09002320 if (idev->cnf.forwarding)
2321 __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 write_lock_bh(&idev->lock);
2324 while ((i = idev->mc_list) != NULL) {
2325 idev->mc_list = i->next;
2326 write_unlock_bh(&idev->lock);
2327
2328 igmp6_group_dropped(i);
2329 ma_put(i);
2330
2331 write_lock_bh(&idev->lock);
2332 }
2333 write_unlock_bh(&idev->lock);
2334}
2335
2336#ifdef CONFIG_PROC_FS
2337struct igmp6_mc_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002338 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 struct net_device *dev;
2340 struct inet6_dev *idev;
2341};
2342
2343#define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2344
2345static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq)
2346{
2347 struct ifmcaddr6 *im = NULL;
2348 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002349 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Pavel Emelianov7562f872007-05-03 15:13:45 -07002351 state->idev = NULL;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002352 for_each_netdev(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 struct inet6_dev *idev;
2354 idev = in6_dev_get(state->dev);
2355 if (!idev)
2356 continue;
2357 read_lock_bh(&idev->lock);
2358 im = idev->mc_list;
2359 if (im) {
2360 state->idev = idev;
2361 break;
2362 }
2363 read_unlock_bh(&idev->lock);
2364 in6_dev_put(idev);
2365 }
2366 return im;
2367}
2368
2369static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im)
2370{
2371 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2372
2373 im = im->next;
2374 while (!im) {
2375 if (likely(state->idev != NULL)) {
2376 read_unlock_bh(&state->idev->lock);
2377 in6_dev_put(state->idev);
2378 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07002379 state->dev = next_net_device(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 if (!state->dev) {
2381 state->idev = NULL;
2382 break;
2383 }
2384 state->idev = in6_dev_get(state->dev);
2385 if (!state->idev)
2386 continue;
2387 read_lock_bh(&state->idev->lock);
2388 im = state->idev->mc_list;
2389 }
2390 return im;
2391}
2392
2393static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos)
2394{
2395 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2396 if (im)
2397 while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL)
2398 --pos;
2399 return pos ? NULL : im;
2400}
2401
2402static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002403 __acquires(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
2405 read_lock(&dev_base_lock);
2406 return igmp6_mc_get_idx(seq, *pos);
2407}
2408
2409static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2410{
2411 struct ifmcaddr6 *im;
2412 im = igmp6_mc_get_next(seq, v);
2413 ++*pos;
2414 return im;
2415}
2416
2417static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002418 __releases(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
2420 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2421 if (likely(state->idev != NULL)) {
2422 read_unlock_bh(&state->idev->lock);
2423 in6_dev_put(state->idev);
2424 state->idev = NULL;
2425 }
2426 state->dev = NULL;
2427 read_unlock(&dev_base_lock);
2428}
2429
2430static int igmp6_mc_seq_show(struct seq_file *seq, void *v)
2431{
2432 struct ifmcaddr6 *im = (struct ifmcaddr6 *)v;
2433 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2434
2435 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002436 "%-4d %-15s %pi6 %5d %08X %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 state->dev->ifindex, state->dev->name,
Harvey Harrisonb0711952008-10-28 16:05:40 -07002438 &im->mca_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 im->mca_users, im->mca_flags,
2440 (im->mca_flags&MAF_TIMER_RUNNING) ?
2441 jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0);
2442 return 0;
2443}
2444
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002445static const struct seq_operations igmp6_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 .start = igmp6_mc_seq_start,
2447 .next = igmp6_mc_seq_next,
2448 .stop = igmp6_mc_seq_stop,
2449 .show = igmp6_mc_seq_show,
2450};
2451
2452static int igmp6_mc_seq_open(struct inode *inode, struct file *file)
2453{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002454 return seq_open_net(inode, file, &igmp6_mc_seq_ops,
2455 sizeof(struct igmp6_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
2457
Arjan van de Ven9a321442007-02-12 00:55:35 -08002458static const struct file_operations igmp6_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 .owner = THIS_MODULE,
2460 .open = igmp6_mc_seq_open,
2461 .read = seq_read,
2462 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002463 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464};
2465
2466struct igmp6_mcf_iter_state {
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002467 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 struct net_device *dev;
2469 struct inet6_dev *idev;
2470 struct ifmcaddr6 *im;
2471};
2472
2473#define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2474
2475static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2476{
2477 struct ip6_sf_list *psf = NULL;
2478 struct ifmcaddr6 *im = NULL;
2479 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002480 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Pavel Emelianov7562f872007-05-03 15:13:45 -07002482 state->idev = NULL;
2483 state->im = NULL;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002484 for_each_netdev(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 struct inet6_dev *idev;
2486 idev = in6_dev_get(state->dev);
2487 if (unlikely(idev == NULL))
2488 continue;
2489 read_lock_bh(&idev->lock);
2490 im = idev->mc_list;
2491 if (likely(im != NULL)) {
2492 spin_lock_bh(&im->mca_lock);
2493 psf = im->mca_sources;
2494 if (likely(psf != NULL)) {
2495 state->im = im;
2496 state->idev = idev;
2497 break;
2498 }
2499 spin_unlock_bh(&im->mca_lock);
2500 }
2501 read_unlock_bh(&idev->lock);
2502 in6_dev_put(idev);
2503 }
2504 return psf;
2505}
2506
2507static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf)
2508{
2509 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2510
2511 psf = psf->sf_next;
2512 while (!psf) {
2513 spin_unlock_bh(&state->im->mca_lock);
2514 state->im = state->im->next;
2515 while (!state->im) {
2516 if (likely(state->idev != NULL)) {
2517 read_unlock_bh(&state->idev->lock);
2518 in6_dev_put(state->idev);
2519 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07002520 state->dev = next_net_device(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 if (!state->dev) {
2522 state->idev = NULL;
2523 goto out;
2524 }
2525 state->idev = in6_dev_get(state->dev);
2526 if (!state->idev)
2527 continue;
2528 read_lock_bh(&state->idev->lock);
2529 state->im = state->idev->mc_list;
2530 }
2531 if (!state->im)
2532 break;
2533 spin_lock_bh(&state->im->mca_lock);
2534 psf = state->im->mca_sources;
2535 }
2536out:
2537 return psf;
2538}
2539
2540static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos)
2541{
2542 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2543 if (psf)
2544 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL)
2545 --pos;
2546 return pos ? NULL : psf;
2547}
2548
2549static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002550 __acquires(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
2552 read_lock(&dev_base_lock);
2553 return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2554}
2555
2556static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2557{
2558 struct ip6_sf_list *psf;
2559 if (v == SEQ_START_TOKEN)
2560 psf = igmp6_mcf_get_first(seq);
2561 else
2562 psf = igmp6_mcf_get_next(seq, v);
2563 ++*pos;
2564 return psf;
2565}
2566
2567static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002568 __releases(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569{
2570 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2571 if (likely(state->im != NULL)) {
2572 spin_unlock_bh(&state->im->mca_lock);
2573 state->im = NULL;
2574 }
2575 if (likely(state->idev != NULL)) {
2576 read_unlock_bh(&state->idev->lock);
2577 in6_dev_put(state->idev);
2578 state->idev = NULL;
2579 }
2580 state->dev = NULL;
2581 read_unlock(&dev_base_lock);
2582}
2583
2584static int igmp6_mcf_seq_show(struct seq_file *seq, void *v)
2585{
2586 struct ip6_sf_list *psf = (struct ip6_sf_list *)v;
2587 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2588
2589 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002590 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 "%3s %6s "
YOSHIFUJI Hideaki9343e792006-01-17 02:10:53 -08002592 "%32s %32s %6s %6s\n", "Idx",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 "Device", "Multicast Address",
2594 "Source Address", "INC", "EXC");
2595 } else {
2596 seq_printf(seq,
Harvey Harrison4b7a4272008-10-29 12:50:24 -07002597 "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 state->dev->ifindex, state->dev->name,
Harvey Harrisonb0711952008-10-28 16:05:40 -07002599 &state->im->mca_addr,
2600 &psf->sf_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 psf->sf_count[MCAST_INCLUDE],
2602 psf->sf_count[MCAST_EXCLUDE]);
2603 }
2604 return 0;
2605}
2606
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002607static const struct seq_operations igmp6_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 .start = igmp6_mcf_seq_start,
2609 .next = igmp6_mcf_seq_next,
2610 .stop = igmp6_mcf_seq_stop,
2611 .show = igmp6_mcf_seq_show,
2612};
2613
2614static int igmp6_mcf_seq_open(struct inode *inode, struct file *file)
2615{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002616 return seq_open_net(inode, file, &igmp6_mcf_seq_ops,
2617 sizeof(struct igmp6_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618}
2619
Arjan van de Ven9a321442007-02-12 00:55:35 -08002620static const struct file_operations igmp6_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 .owner = THIS_MODULE,
2622 .open = igmp6_mcf_seq_open,
2623 .read = seq_read,
2624 .llseek = seq_lseek,
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002625 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626};
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002627
2628static int igmp6_proc_init(struct net *net)
2629{
2630 int err;
2631
2632 err = -ENOMEM;
2633 if (!proc_net_fops_create(net, "igmp6", S_IRUGO, &igmp6_mc_seq_fops))
2634 goto out;
2635 if (!proc_net_fops_create(net, "mcfilter6", S_IRUGO,
2636 &igmp6_mcf_seq_fops))
2637 goto out_proc_net_igmp6;
2638
2639 err = 0;
2640out:
2641 return err;
2642
2643out_proc_net_igmp6:
2644 proc_net_remove(net, "igmp6");
2645 goto out;
2646}
2647
2648static void igmp6_proc_exit(struct net *net)
2649{
2650 proc_net_remove(net, "mcfilter6");
2651 proc_net_remove(net, "igmp6");
2652}
2653#else
2654static int igmp6_proc_init(struct net *net)
2655{
2656 return 0;
2657}
2658static void igmp6_proc_exit(struct net *net)
2659{
2660 ;
2661}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662#endif
2663
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002664static int igmp6_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 int err;
2667
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002668 err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6,
2669 SOCK_RAW, IPPROTO_ICMPV6, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 if (err < 0) {
2671 printk(KERN_ERR
2672 "Failed to initialize the IGMP6 control socket (err %d).\n",
2673 err);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002674 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 }
2676
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002677 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002679 err = igmp6_proc_init(net);
2680 if (err)
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002681 goto out_sock_create;
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002682out:
2683 return err;
2684
2685out_sock_create:
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002686 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002687 goto out;
2688}
2689
2690static void igmp6_net_exit(struct net *net)
2691{
Denis V. Lunev1ed85162008-04-03 14:31:03 -07002692 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
Daniel Lezcanoea82edf2008-03-21 04:10:53 -07002693 igmp6_proc_exit(net);
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002694}
2695
2696static struct pernet_operations igmp6_net_ops = {
2697 .init = igmp6_net_init,
2698 .exit = igmp6_net_exit,
2699};
2700
2701int __init igmp6_init(void)
2702{
2703 return register_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704}
2705
2706void igmp6_cleanup(void)
2707{
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -08002708 unregister_pernet_subsys(&igmp6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709}